FileBackend.java

   1package eu.siacs.conversations.persistance;
   2
   3import android.annotation.TargetApi;
   4import android.content.ContentResolver;
   5import android.content.Context;
   6import android.database.Cursor;
   7import android.graphics.Bitmap;
   8import android.graphics.BitmapFactory;
   9import android.graphics.Canvas;
  10import android.graphics.Color;
  11import android.graphics.Matrix;
  12import android.graphics.Paint;
  13import android.graphics.RectF;
  14import android.graphics.pdf.PdfRenderer;
  15import android.media.MediaMetadataRetriever;
  16import android.media.MediaScannerConnection;
  17import android.net.Uri;
  18import android.os.Build;
  19import android.os.Environment;
  20import android.os.ParcelFileDescriptor;
  21import android.provider.MediaStore;
  22import android.provider.OpenableColumns;
  23import android.system.Os;
  24import android.system.StructStat;
  25import android.util.Base64;
  26import android.util.Base64OutputStream;
  27import android.util.DisplayMetrics;
  28import android.util.Log;
  29import android.util.LruCache;
  30
  31import androidx.annotation.RequiresApi;
  32import androidx.annotation.StringRes;
  33import androidx.core.content.FileProvider;
  34import androidx.exifinterface.media.ExifInterface;
  35
  36import com.google.common.base.Strings;
  37import com.google.common.io.ByteStreams;
  38
  39import java.io.ByteArrayOutputStream;
  40import java.io.Closeable;
  41import java.io.File;
  42import java.io.FileDescriptor;
  43import java.io.FileInputStream;
  44import java.io.FileNotFoundException;
  45import java.io.FileOutputStream;
  46import java.io.IOException;
  47import java.io.InputStream;
  48import java.io.OutputStream;
  49import java.net.ServerSocket;
  50import java.net.Socket;
  51import java.security.DigestOutputStream;
  52import java.security.MessageDigest;
  53import java.security.NoSuchAlgorithmException;
  54import java.text.SimpleDateFormat;
  55import java.util.ArrayList;
  56import java.util.Date;
  57import java.util.List;
  58import java.util.Locale;
  59import java.util.UUID;
  60
  61import eu.siacs.conversations.Config;
  62import eu.siacs.conversations.R;
  63import eu.siacs.conversations.entities.DownloadableFile;
  64import eu.siacs.conversations.entities.Message;
  65import eu.siacs.conversations.services.AttachFileToConversationRunnable;
  66import eu.siacs.conversations.services.XmppConnectionService;
  67import eu.siacs.conversations.ui.util.Attachment;
  68import eu.siacs.conversations.utils.Compatibility;
  69import eu.siacs.conversations.utils.CryptoHelper;
  70import eu.siacs.conversations.utils.FileUtils;
  71import eu.siacs.conversations.utils.FileWriterException;
  72import eu.siacs.conversations.utils.MimeUtils;
  73import eu.siacs.conversations.xmpp.pep.Avatar;
  74
  75public class FileBackend {
  76
  77    private static final Object THUMBNAIL_LOCK = new Object();
  78
  79    private static final SimpleDateFormat IMAGE_DATE_FORMAT =
  80            new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US);
  81
  82    private static final String FILE_PROVIDER = ".files";
  83    private static final float IGNORE_PADDING = 0.15f;
  84    private final XmppConnectionService mXmppConnectionService;
  85
  86    public FileBackend(XmppConnectionService service) {
  87        this.mXmppConnectionService = service;
  88    }
  89
  90    public static long getFileSize(Context context, Uri uri) {
  91        try {
  92            final Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
  93            if (cursor != null && cursor.moveToFirst()) {
  94                long size = cursor.getLong(cursor.getColumnIndex(OpenableColumns.SIZE));
  95                cursor.close();
  96                return size;
  97            } else {
  98                return -1;
  99            }
 100        } catch (Exception e) {
 101            return -1;
 102        }
 103    }
 104
 105    public static boolean allFilesUnderSize(
 106            Context context, List<Attachment> attachments, long max) {
 107        final boolean compressVideo =
 108                !AttachFileToConversationRunnable.getVideoCompression(context)
 109                        .equals("uncompressed");
 110        if (max <= 0) {
 111            Log.d(Config.LOGTAG, "server did not report max file size for http upload");
 112            return true; // exception to be compatible with HTTP Upload < v0.2
 113        }
 114        for (Attachment attachment : attachments) {
 115            if (attachment.getType() != Attachment.Type.FILE) {
 116                continue;
 117            }
 118            String mime = attachment.getMime();
 119            if (mime != null && mime.startsWith("video/") && compressVideo) {
 120                try {
 121                    Dimensions dimensions =
 122                            FileBackend.getVideoDimensions(context, attachment.getUri());
 123                    if (dimensions.getMin() > 720) {
 124                        Log.d(
 125                                Config.LOGTAG,
 126                                "do not consider video file with min width larger than 720 for size check");
 127                        continue;
 128                    }
 129                } catch (NotAVideoFile notAVideoFile) {
 130                    // ignore and fall through
 131                }
 132            }
 133            if (FileBackend.getFileSize(context, attachment.getUri()) > max) {
 134                Log.d(
 135                        Config.LOGTAG,
 136                        "not all files are under "
 137                                + max
 138                                + " bytes. suggesting falling back to jingle");
 139                return false;
 140            }
 141        }
 142        return true;
 143    }
 144
 145    public static File getBackupDirectory(final Context context) {
 146        final File conversationsDownloadDirectory =
 147                new File(
 148                        Environment.getExternalStoragePublicDirectory(
 149                                Environment.DIRECTORY_DOWNLOADS),
 150                        context.getString(R.string.app_name));
 151        return new File(conversationsDownloadDirectory, "Backup");
 152    }
 153
 154    public static File getLegacyBackupDirectory(final String app) {
 155        final File appDirectory = new File(Environment.getExternalStorageDirectory(), app);
 156        return new File(appDirectory, "Backup");
 157    }
 158
 159    private static Bitmap rotate(final Bitmap bitmap, final int degree) {
 160        if (degree == 0) {
 161            return bitmap;
 162        }
 163        final int w = bitmap.getWidth();
 164        final int h = bitmap.getHeight();
 165        final Matrix matrix = new Matrix();
 166        matrix.postRotate(degree);
 167        final Bitmap result = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);
 168        if (!bitmap.isRecycled()) {
 169            bitmap.recycle();
 170        }
 171        return result;
 172    }
 173
 174    public static boolean isPathBlacklisted(String path) {
 175        final String androidDataPath =
 176                Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/";
 177        return path.startsWith(androidDataPath);
 178    }
 179
 180    private static Paint createAntiAliasingPaint() {
 181        Paint paint = new Paint();
 182        paint.setAntiAlias(true);
 183        paint.setFilterBitmap(true);
 184        paint.setDither(true);
 185        return paint;
 186    }
 187
 188    private static String getTakePhotoPath() {
 189        return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)
 190                + "/Camera/";
 191    }
 192
 193    public static Uri getUriForUri(Context context, Uri uri) {
 194        if ("file".equals(uri.getScheme())) {
 195            return getUriForFile(context, new File(uri.getPath()));
 196        } else {
 197            return uri;
 198        }
 199    }
 200
 201    public static Uri getUriForFile(Context context, File file) {
 202        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N || Config.ONLY_INTERNAL_STORAGE) {
 203            try {
 204                return FileProvider.getUriForFile(context, getAuthority(context), file);
 205            } catch (IllegalArgumentException e) {
 206                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
 207                    throw new SecurityException(e);
 208                } else {
 209                    return Uri.fromFile(file);
 210                }
 211            }
 212        } else {
 213            return Uri.fromFile(file);
 214        }
 215    }
 216
 217    public static String getAuthority(Context context) {
 218        return context.getPackageName() + FILE_PROVIDER;
 219    }
 220
 221    private static boolean hasAlpha(final Bitmap bitmap) {
 222        final int w = bitmap.getWidth();
 223        final int h = bitmap.getHeight();
 224        final int yStep = Math.max(1, w / 100);
 225        final int xStep = Math.max(1, h / 100);
 226        for (int x = 0; x < w; x += xStep) {
 227            for (int y = 0; y < h; y += yStep) {
 228                if (Color.alpha(bitmap.getPixel(x, y)) < 255) {
 229                    return true;
 230                }
 231            }
 232        }
 233        return false;
 234    }
 235
 236    private static int calcSampleSize(File image, int size) {
 237        BitmapFactory.Options options = new BitmapFactory.Options();
 238        options.inJustDecodeBounds = true;
 239        BitmapFactory.decodeFile(image.getAbsolutePath(), options);
 240        return calcSampleSize(options, size);
 241    }
 242
 243    private static int calcSampleSize(BitmapFactory.Options options, int size) {
 244        int height = options.outHeight;
 245        int width = options.outWidth;
 246        int inSampleSize = 1;
 247
 248        if (height > size || width > size) {
 249            int halfHeight = height / 2;
 250            int halfWidth = width / 2;
 251
 252            while ((halfHeight / inSampleSize) > size && (halfWidth / inSampleSize) > size) {
 253                inSampleSize *= 2;
 254            }
 255        }
 256        return inSampleSize;
 257    }
 258
 259    private static Dimensions getVideoDimensions(Context context, Uri uri) throws NotAVideoFile {
 260        MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
 261        try {
 262            mediaMetadataRetriever.setDataSource(context, uri);
 263        } catch (RuntimeException e) {
 264            throw new NotAVideoFile(e);
 265        }
 266        return getVideoDimensions(mediaMetadataRetriever);
 267    }
 268
 269    private static Dimensions getVideoDimensionsOfFrame(
 270            MediaMetadataRetriever mediaMetadataRetriever) {
 271        Bitmap bitmap = null;
 272        try {
 273            bitmap = mediaMetadataRetriever.getFrameAtTime();
 274            return new Dimensions(bitmap.getHeight(), bitmap.getWidth());
 275        } catch (Exception e) {
 276            return null;
 277        } finally {
 278            if (bitmap != null) {
 279                bitmap.recycle();
 280            }
 281        }
 282    }
 283
 284    private static Dimensions getVideoDimensions(MediaMetadataRetriever metadataRetriever)
 285            throws NotAVideoFile {
 286        String hasVideo =
 287                metadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_HAS_VIDEO);
 288        if (hasVideo == null) {
 289            throw new NotAVideoFile();
 290        }
 291        Dimensions dimensions = getVideoDimensionsOfFrame(metadataRetriever);
 292        if (dimensions != null) {
 293            return dimensions;
 294        }
 295        final int rotation = extractRotationFromMediaRetriever(metadataRetriever);
 296        boolean rotated = rotation == 90 || rotation == 270;
 297        int height;
 298        try {
 299            String h =
 300                    metadataRetriever.extractMetadata(
 301                            MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT);
 302            height = Integer.parseInt(h);
 303        } catch (Exception e) {
 304            height = -1;
 305        }
 306        int width;
 307        try {
 308            String w =
 309                    metadataRetriever.extractMetadata(
 310                            MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH);
 311            width = Integer.parseInt(w);
 312        } catch (Exception e) {
 313            width = -1;
 314        }
 315        metadataRetriever.release();
 316        Log.d(Config.LOGTAG, "extracted video dims " + width + "x" + height);
 317        return rotated ? new Dimensions(width, height) : new Dimensions(height, width);
 318    }
 319
 320    private static int extractRotationFromMediaRetriever(MediaMetadataRetriever metadataRetriever) {
 321        String r =
 322                metadataRetriever.extractMetadata(
 323                        MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION);
 324        try {
 325            return Integer.parseInt(r);
 326        } catch (Exception e) {
 327            return 0;
 328        }
 329    }
 330
 331    public static void close(final Closeable stream) {
 332        if (stream != null) {
 333            try {
 334                stream.close();
 335            } catch (Exception e) {
 336                Log.d(Config.LOGTAG, "unable to close stream", e);
 337            }
 338        }
 339    }
 340
 341    public static void close(final Socket socket) {
 342        if (socket != null) {
 343            try {
 344                socket.close();
 345            } catch (IOException e) {
 346                Log.d(Config.LOGTAG, "unable to close socket", e);
 347            }
 348        }
 349    }
 350
 351    public static void close(final ServerSocket socket) {
 352        if (socket != null) {
 353            try {
 354                socket.close();
 355            } catch (IOException e) {
 356                Log.d(Config.LOGTAG, "unable to close server socket", e);
 357            }
 358        }
 359    }
 360
 361    public static boolean weOwnFile(Context context, Uri uri) {
 362        if (uri == null || !ContentResolver.SCHEME_FILE.equals(uri.getScheme())) {
 363            return false;
 364        } else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
 365            return fileIsInFilesDir(context, uri);
 366        } else {
 367            return weOwnFileLollipop(uri);
 368        }
 369    }
 370
 371    /**
 372     * This is more than hacky but probably way better than doing nothing Further 'optimizations'
 373     * might contain to get the parents of CacheDir and NoBackupDir and check against those as well
 374     */
 375    private static boolean fileIsInFilesDir(Context context, Uri uri) {
 376        try {
 377            final String haystack = context.getFilesDir().getParentFile().getCanonicalPath();
 378            final String needle = new File(uri.getPath()).getCanonicalPath();
 379            return needle.startsWith(haystack);
 380        } catch (IOException e) {
 381            return false;
 382        }
 383    }
 384
 385    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
 386    private static boolean weOwnFileLollipop(Uri uri) {
 387        try {
 388            File file = new File(uri.getPath());
 389            FileDescriptor fd =
 390                    ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY)
 391                            .getFileDescriptor();
 392            StructStat st = Os.fstat(fd);
 393            return st.st_uid == android.os.Process.myUid();
 394        } catch (FileNotFoundException e) {
 395            return false;
 396        } catch (Exception e) {
 397            return true;
 398        }
 399    }
 400
 401    public static Uri getMediaUri(Context context, File file) {
 402        final String filePath = file.getAbsolutePath();
 403        final Cursor cursor;
 404        try {
 405            cursor =
 406                    context.getContentResolver()
 407                            .query(
 408                                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
 409                                    new String[] {MediaStore.Images.Media._ID},
 410                                    MediaStore.Images.Media.DATA + "=? ",
 411                                    new String[] {filePath},
 412                                    null);
 413        } catch (SecurityException e) {
 414            return null;
 415        }
 416        if (cursor != null && cursor.moveToFirst()) {
 417            final int id = cursor.getInt(cursor.getColumnIndex(MediaStore.MediaColumns._ID));
 418            cursor.close();
 419            return Uri.withAppendedPath(
 420                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI, String.valueOf(id));
 421        } else {
 422            return null;
 423        }
 424    }
 425
 426    public static void updateFileParams(Message message, String url, long size) {
 427        final StringBuilder body = new StringBuilder();
 428        body.append(url).append('|').append(size);
 429        message.setBody(body.toString());
 430    }
 431
 432    public Bitmap getPreviewForUri(Attachment attachment, int size, boolean cacheOnly) {
 433        final String key = "attachment_" + attachment.getUuid().toString() + "_" + size;
 434        final LruCache<String, Bitmap> cache = mXmppConnectionService.getBitmapCache();
 435        Bitmap bitmap = cache.get(key);
 436        if (bitmap != null || cacheOnly) {
 437            return bitmap;
 438        }
 439        final String mime = attachment.getMime();
 440        if ("application/pdf".equals(mime) && Compatibility.runsTwentyOne()) {
 441            bitmap = cropCenterSquarePdf(attachment.getUri(), size);
 442            drawOverlay(
 443                    bitmap,
 444                    paintOverlayBlackPdf(bitmap)
 445                            ? R.drawable.open_pdf_black
 446                            : R.drawable.open_pdf_white,
 447                    0.75f);
 448        } else if (mime != null && mime.startsWith("video/")) {
 449            bitmap = cropCenterSquareVideo(attachment.getUri(), size);
 450            drawOverlay(
 451                    bitmap,
 452                    paintOverlayBlack(bitmap)
 453                            ? R.drawable.play_video_black
 454                            : R.drawable.play_video_white,
 455                    0.75f);
 456        } else {
 457            bitmap = cropCenterSquare(attachment.getUri(), size);
 458            if (bitmap != null && "image/gif".equals(mime)) {
 459                Bitmap withGifOverlay = bitmap.copy(Bitmap.Config.ARGB_8888, true);
 460                drawOverlay(
 461                        withGifOverlay,
 462                        paintOverlayBlack(withGifOverlay)
 463                                ? R.drawable.play_gif_black
 464                                : R.drawable.play_gif_white,
 465                        1.0f);
 466                bitmap.recycle();
 467                bitmap = withGifOverlay;
 468            }
 469        }
 470        if (bitmap != null) {
 471            cache.put(key, bitmap);
 472        }
 473        return bitmap;
 474    }
 475
 476    private void createNoMedia(File diretory) {
 477        final File noMedia = new File(diretory, ".nomedia");
 478        if (!noMedia.exists()) {
 479            try {
 480                if (!noMedia.createNewFile()) {
 481                    Log.d(Config.LOGTAG, "created nomedia file " + noMedia.getAbsolutePath());
 482                }
 483            } catch (Exception e) {
 484                Log.d(Config.LOGTAG, "could not create nomedia file");
 485            }
 486        }
 487    }
 488
 489    public void updateMediaScanner(File file) {
 490        updateMediaScanner(file, null);
 491    }
 492
 493    public void updateMediaScanner(File file, final Runnable callback) {
 494        MediaScannerConnection.scanFile(
 495                mXmppConnectionService,
 496                new String[] {file.getAbsolutePath()},
 497                null,
 498                new MediaScannerConnection.MediaScannerConnectionClient() {
 499                    @Override
 500                    public void onMediaScannerConnected() {}
 501
 502                    @Override
 503                    public void onScanCompleted(String path, Uri uri) {
 504                        if (callback != null && file.getAbsolutePath().equals(path)) {
 505                            callback.run();
 506                        } else {
 507                            Log.d(Config.LOGTAG, "media scanner scanned wrong file");
 508                            if (callback != null) {
 509                                callback.run();
 510                            }
 511                        }
 512                    }
 513                });
 514    }
 515
 516    public boolean deleteFile(Message message) {
 517        File file = getFile(message);
 518        if (file.delete()) {
 519            updateMediaScanner(file);
 520            return true;
 521        } else {
 522            return false;
 523        }
 524    }
 525
 526    public DownloadableFile getFile(Message message) {
 527        return getFile(message, true);
 528    }
 529
 530    public DownloadableFile getFileForPath(String path) {
 531        return getFileForPath(
 532                path,
 533                MimeUtils.guessMimeTypeFromExtension(MimeUtils.extractRelevantExtension(path)));
 534    }
 535
 536    private DownloadableFile getFileForPath(final String path, final String mime) {
 537        if (path.startsWith("/")) {
 538            return new DownloadableFile(path);
 539        } else {
 540            return getLegacyFileForFilename(path, mime);
 541        }
 542    }
 543
 544    public DownloadableFile getLegacyFileForFilename(final String filename, final String mime) {
 545        if (Strings.isNullOrEmpty(mime)) {
 546            return new DownloadableFile(getLegacyStorageLocation("Files"), filename);
 547        } else if (mime.startsWith("image/")) {
 548            return new DownloadableFile(getLegacyStorageLocation("Images"), filename);
 549        } else if (mime.startsWith("video/")) {
 550            return new DownloadableFile(getLegacyStorageLocation("Videos"), filename);
 551        } else {
 552            return new DownloadableFile(getLegacyStorageLocation("Files"), filename);
 553        }
 554    }
 555
 556    public boolean isInternalFile(final File file) {
 557        final File internalFile = getFileForPath(file.getName());
 558        return file.getAbsolutePath().equals(internalFile.getAbsolutePath());
 559    }
 560
 561    public DownloadableFile getFile(Message message, boolean decrypted) {
 562        final boolean encrypted =
 563                !decrypted
 564                        && (message.getEncryption() == Message.ENCRYPTION_PGP
 565                                || message.getEncryption() == Message.ENCRYPTION_DECRYPTED);
 566        String path = message.getRelativeFilePath();
 567        if (path == null) {
 568            path = message.getUuid();
 569        }
 570        final DownloadableFile file = getFileForPath(path, message.getMimeType());
 571        if (encrypted) {
 572            return new DownloadableFile(getLegacyStorageLocation("Files"), file.getName() + ".pgp");
 573        } else {
 574            return file;
 575        }
 576    }
 577
 578    public List<Attachment> convertToAttachments(List<DatabaseBackend.FilePath> relativeFilePaths) {
 579        final List<Attachment> attachments = new ArrayList<>();
 580        for (final DatabaseBackend.FilePath relativeFilePath : relativeFilePaths) {
 581            final String mime =
 582                    MimeUtils.guessMimeTypeFromExtension(
 583                            MimeUtils.extractRelevantExtension(relativeFilePath.path));
 584            final File file = getFileForPath(relativeFilePath.path, mime);
 585            attachments.add(Attachment.of(relativeFilePath.uuid, file, mime));
 586        }
 587        return attachments;
 588    }
 589
 590    private File getLegacyStorageLocation(final String type) {
 591        if (Config.ONLY_INTERNAL_STORAGE) {
 592            return new File(mXmppConnectionService.getFilesDir(), type);
 593        } else {
 594            final File appDirectory =
 595                    new File(
 596                            Environment.getExternalStorageDirectory(),
 597                            mXmppConnectionService.getString(R.string.app_name));
 598            final File appMediaDirectory = new File(appDirectory, "Media");
 599            final String locationName =
 600                    String.format(
 601                            "%s %s", mXmppConnectionService.getString(R.string.app_name), type);
 602            return new File(appMediaDirectory, locationName);
 603        }
 604    }
 605
 606    private Bitmap resize(final Bitmap originalBitmap, int size) throws IOException {
 607        int w = originalBitmap.getWidth();
 608        int h = originalBitmap.getHeight();
 609        if (w <= 0 || h <= 0) {
 610            throw new IOException("Decoded bitmap reported bounds smaller 0");
 611        } else if (Math.max(w, h) > size) {
 612            int scalledW;
 613            int scalledH;
 614            if (w <= h) {
 615                scalledW = Math.max((int) (w / ((double) h / size)), 1);
 616                scalledH = size;
 617            } else {
 618                scalledW = size;
 619                scalledH = Math.max((int) (h / ((double) w / size)), 1);
 620            }
 621            final Bitmap result =
 622                    Bitmap.createScaledBitmap(originalBitmap, scalledW, scalledH, true);
 623            if (!originalBitmap.isRecycled()) {
 624                originalBitmap.recycle();
 625            }
 626            return result;
 627        } else {
 628            return originalBitmap;
 629        }
 630    }
 631
 632    public boolean useImageAsIs(final Uri uri) {
 633        final String path = getOriginalPath(uri);
 634        if (path == null || isPathBlacklisted(path)) {
 635            return false;
 636        }
 637        final File file = new File(path);
 638        long size = file.length();
 639        if (size == 0
 640                || size
 641                        >= mXmppConnectionService
 642                                .getResources()
 643                                .getInteger(R.integer.auto_accept_filesize)) {
 644            return false;
 645        }
 646        BitmapFactory.Options options = new BitmapFactory.Options();
 647        options.inJustDecodeBounds = true;
 648        try {
 649            final InputStream inputStream =
 650                    mXmppConnectionService.getContentResolver().openInputStream(uri);
 651            BitmapFactory.decodeStream(inputStream, null, options);
 652            close(inputStream);
 653            if (options.outMimeType == null || options.outHeight <= 0 || options.outWidth <= 0) {
 654                return false;
 655            }
 656            return (options.outWidth <= Config.IMAGE_SIZE
 657                    && options.outHeight <= Config.IMAGE_SIZE
 658                    && options.outMimeType.contains(Config.IMAGE_FORMAT.name().toLowerCase()));
 659        } catch (FileNotFoundException e) {
 660            Log.d(Config.LOGTAG, "unable to get image dimensions", e);
 661            return false;
 662        }
 663    }
 664
 665    public String getOriginalPath(Uri uri) {
 666        return FileUtils.getPath(mXmppConnectionService, uri);
 667    }
 668
 669    private void copyFileToPrivateStorage(File file, Uri uri) throws FileCopyException {
 670        Log.d(
 671                Config.LOGTAG,
 672                "copy file (" + uri.toString() + ") to private storage " + file.getAbsolutePath());
 673        file.getParentFile().mkdirs();
 674        try {
 675            file.createNewFile();
 676        } catch (IOException e) {
 677            throw new FileCopyException(R.string.error_unable_to_create_temporary_file);
 678        }
 679        try (final OutputStream os = new FileOutputStream(file);
 680                final InputStream is =
 681                        mXmppConnectionService.getContentResolver().openInputStream(uri)) {
 682            if (is == null) {
 683                throw new FileCopyException(R.string.error_file_not_found);
 684            }
 685            try {
 686                ByteStreams.copy(is, os);
 687            } catch (IOException e) {
 688                throw new FileWriterException();
 689            }
 690            try {
 691                os.flush();
 692            } catch (IOException e) {
 693                throw new FileWriterException();
 694            }
 695        } catch (final FileNotFoundException e) {
 696            cleanup(file);
 697            throw new FileCopyException(R.string.error_file_not_found);
 698        } catch (final FileWriterException e) {
 699            cleanup(file);
 700            throw new FileCopyException(R.string.error_unable_to_create_temporary_file);
 701        } catch (final SecurityException e) {
 702            cleanup(file);
 703            throw new FileCopyException(R.string.error_security_exception);
 704        } catch (final IOException e) {
 705            cleanup(file);
 706            throw new FileCopyException(R.string.error_io_exception);
 707        }
 708    }
 709
 710    public void copyFileToPrivateStorage(Message message, Uri uri, String type)
 711            throws FileCopyException {
 712        String mime = MimeUtils.guessMimeTypeFromUriAndMime(mXmppConnectionService, uri, type);
 713        Log.d(Config.LOGTAG, "copy " + uri.toString() + " to private storage (mime=" + mime + ")");
 714        String extension = MimeUtils.guessExtensionFromMimeType(mime);
 715        if (extension == null) {
 716            Log.d(Config.LOGTAG, "extension from mime type was null");
 717            extension = getExtensionFromUri(uri);
 718        }
 719        if ("ogg".equals(extension) && type != null && type.startsWith("audio/")) {
 720            extension = "oga";
 721        }
 722        setupRelativeFilePath(message, String.format("%s.%s", message.getUuid(), extension));
 723        copyFileToPrivateStorage(mXmppConnectionService.getFileBackend().getFile(message), uri);
 724    }
 725
 726    private String getExtensionFromUri(Uri uri) {
 727        String[] projection = {MediaStore.MediaColumns.DATA};
 728        String filename = null;
 729        Cursor cursor;
 730        try {
 731            cursor =
 732                    mXmppConnectionService
 733                            .getContentResolver()
 734                            .query(uri, projection, null, null, null);
 735        } catch (IllegalArgumentException e) {
 736            cursor = null;
 737        }
 738        if (cursor != null) {
 739            try {
 740                if (cursor.moveToFirst()) {
 741                    filename = cursor.getString(0);
 742                }
 743            } catch (Exception e) {
 744                filename = null;
 745            } finally {
 746                cursor.close();
 747            }
 748        }
 749        if (filename == null) {
 750            final List<String> segments = uri.getPathSegments();
 751            if (segments.size() > 0) {
 752                filename = segments.get(segments.size() - 1);
 753            }
 754        }
 755        int pos = filename == null ? -1 : filename.lastIndexOf('.');
 756        return pos > 0 ? filename.substring(pos + 1) : null;
 757    }
 758
 759    private void copyImageToPrivateStorage(File file, Uri image, int sampleSize)
 760            throws FileCopyException, ImageCompressionException {
 761        final File parent = file.getParentFile();
 762        if (parent != null && parent.mkdirs()) {
 763            Log.d(Config.LOGTAG, "created parent directory");
 764        }
 765        InputStream is = null;
 766        OutputStream os = null;
 767        try {
 768            if (!file.exists() && !file.createNewFile()) {
 769                throw new FileCopyException(R.string.error_unable_to_create_temporary_file);
 770            }
 771            is = mXmppConnectionService.getContentResolver().openInputStream(image);
 772            if (is == null) {
 773                throw new FileCopyException(R.string.error_not_an_image_file);
 774            }
 775            final Bitmap originalBitmap;
 776            final BitmapFactory.Options options = new BitmapFactory.Options();
 777            final int inSampleSize = (int) Math.pow(2, sampleSize);
 778            Log.d(Config.LOGTAG, "reading bitmap with sample size " + inSampleSize);
 779            options.inSampleSize = inSampleSize;
 780            originalBitmap = BitmapFactory.decodeStream(is, null, options);
 781            is.close();
 782            if (originalBitmap == null) {
 783                throw new ImageCompressionException("Source file was not an image");
 784            }
 785            if (!"image/jpeg".equals(options.outMimeType) && hasAlpha(originalBitmap)) {
 786                originalBitmap.recycle();
 787                throw new ImageCompressionException("Source file had alpha channel");
 788            }
 789            Bitmap scaledBitmap = resize(originalBitmap, Config.IMAGE_SIZE);
 790            final int rotation = getRotation(image);
 791            scaledBitmap = rotate(scaledBitmap, rotation);
 792            boolean targetSizeReached = false;
 793            int quality = Config.IMAGE_QUALITY;
 794            final int imageMaxSize =
 795                    mXmppConnectionService
 796                            .getResources()
 797                            .getInteger(R.integer.auto_accept_filesize);
 798            while (!targetSizeReached) {
 799                os = new FileOutputStream(file);
 800                Log.d(Config.LOGTAG, "compressing image with quality " + quality);
 801                boolean success = scaledBitmap.compress(Config.IMAGE_FORMAT, quality, os);
 802                if (!success) {
 803                    throw new FileCopyException(R.string.error_compressing_image);
 804                }
 805                os.flush();
 806                final long fileSize = file.length();
 807                Log.d(Config.LOGTAG, "achieved file size of " + fileSize);
 808                targetSizeReached = fileSize <= imageMaxSize || quality <= 50;
 809                quality -= 5;
 810            }
 811            scaledBitmap.recycle();
 812        } catch (final FileNotFoundException e) {
 813            cleanup(file);
 814            throw new FileCopyException(R.string.error_file_not_found);
 815        } catch (final IOException e) {
 816            cleanup(file);
 817            throw new FileCopyException(R.string.error_io_exception);
 818        } catch (SecurityException e) {
 819            cleanup(file);
 820            throw new FileCopyException(R.string.error_security_exception_during_image_copy);
 821        } catch (final OutOfMemoryError e) {
 822            ++sampleSize;
 823            if (sampleSize <= 3) {
 824                copyImageToPrivateStorage(file, image, sampleSize);
 825            } else {
 826                throw new FileCopyException(R.string.error_out_of_memory);
 827            }
 828        } finally {
 829            close(os);
 830            close(is);
 831        }
 832    }
 833
 834    private static void cleanup(final File file) {
 835        try {
 836            file.delete();
 837        } catch (Exception e) {
 838
 839        }
 840    }
 841
 842    public void copyImageToPrivateStorage(File file, Uri image)
 843            throws FileCopyException, ImageCompressionException {
 844        Log.d(
 845                Config.LOGTAG,
 846                "copy image ("
 847                        + image.toString()
 848                        + ") to private storage "
 849                        + file.getAbsolutePath());
 850        copyImageToPrivateStorage(file, image, 0);
 851    }
 852
 853    public void copyImageToPrivateStorage(Message message, Uri image)
 854            throws FileCopyException, ImageCompressionException {
 855        final String filename;
 856        switch (Config.IMAGE_FORMAT) {
 857            case JPEG:
 858                filename = String.format("%s.%s", message.getUuid(), "jpg");
 859                break;
 860            case PNG:
 861                filename = String.format("%s.%s", message.getUuid(), "png");
 862                break;
 863            case WEBP:
 864                filename = String.format("%s.%s", message.getUuid(), "webp");
 865                break;
 866            default:
 867                throw new IllegalStateException("Unknown image format");
 868        }
 869        setupRelativeFilePath(message, filename);
 870        copyImageToPrivateStorage(getFile(message), image);
 871        updateFileParams(message);
 872    }
 873
 874    public void setupRelativeFilePath(final Message message, final String filename) {
 875        final String extension = MimeUtils.extractRelevantExtension(filename);
 876        final String mime = MimeUtils.guessMimeTypeFromExtension(extension);
 877        setupRelativeFilePath(message, filename, mime);
 878    }
 879
 880    public File getStorageLocation(final String filename, final String mime) {
 881        final File parentDirectory;
 882        if (Strings.isNullOrEmpty(mime)) {
 883            parentDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
 884        } else if (mime.startsWith("image/")) {
 885            parentDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
 886        } else if (mime.startsWith("video/")) {
 887            parentDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES);
 888        } else {
 889            parentDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
 890        }
 891        final File appDirectory = new File(parentDirectory, mXmppConnectionService.getString(R.string.app_name));
 892        return new File(appDirectory, filename);
 893    }
 894
 895    public void setupRelativeFilePath(final Message message, final String filename,  final String mime) {
 896        final File file = getStorageLocation(filename, mime);
 897        message.setRelativeFilePath(file.getAbsolutePath());
 898    }
 899
 900    public boolean unusualBounds(final Uri image) {
 901        try {
 902            final BitmapFactory.Options options = new BitmapFactory.Options();
 903            options.inJustDecodeBounds = true;
 904            final InputStream inputStream =
 905                    mXmppConnectionService.getContentResolver().openInputStream(image);
 906            BitmapFactory.decodeStream(inputStream, null, options);
 907            close(inputStream);
 908            float ratio = (float) options.outHeight / options.outWidth;
 909            return ratio > (21.0f / 9.0f) || ratio < (9.0f / 21.0f);
 910        } catch (final Exception e) {
 911            Log.w(Config.LOGTAG, "unable to detect image bounds", e);
 912            return false;
 913        }
 914    }
 915
 916    private int getRotation(final File file) {
 917        try (final InputStream inputStream = new FileInputStream(file)) {
 918            return getRotation(inputStream);
 919        } catch (Exception e) {
 920            return 0;
 921        }
 922    }
 923
 924    private int getRotation(final Uri image) {
 925        try (final InputStream is =
 926                mXmppConnectionService.getContentResolver().openInputStream(image)) {
 927            return is == null ? 0 : getRotation(is);
 928        } catch (final Exception e) {
 929            return 0;
 930        }
 931    }
 932
 933    private static int getRotation(final InputStream inputStream) throws IOException {
 934        final ExifInterface exif = new ExifInterface(inputStream);
 935        final int orientation =
 936                exif.getAttributeInt(
 937                        ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
 938        switch (orientation) {
 939            case ExifInterface.ORIENTATION_ROTATE_180:
 940                return 180;
 941            case ExifInterface.ORIENTATION_ROTATE_90:
 942                return 90;
 943            case ExifInterface.ORIENTATION_ROTATE_270:
 944                return 270;
 945            default:
 946                return 0;
 947        }
 948    }
 949
 950    public Bitmap getThumbnail(Message message, int size, boolean cacheOnly) throws IOException {
 951        final String uuid = message.getUuid();
 952        final LruCache<String, Bitmap> cache = mXmppConnectionService.getBitmapCache();
 953        Bitmap thumbnail = cache.get(uuid);
 954        if ((thumbnail == null) && (!cacheOnly)) {
 955            synchronized (THUMBNAIL_LOCK) {
 956                thumbnail = cache.get(uuid);
 957                if (thumbnail != null) {
 958                    return thumbnail;
 959                }
 960                DownloadableFile file = getFile(message);
 961                final String mime = file.getMimeType();
 962                if ("application/pdf".equals(mime) && Compatibility.runsTwentyOne()) {
 963                    thumbnail = getPdfDocumentPreview(file, size);
 964                } else if (mime.startsWith("video/")) {
 965                    thumbnail = getVideoPreview(file, size);
 966                } else {
 967                    final Bitmap fullSize = getFullSizeImagePreview(file, size);
 968                    if (fullSize == null) {
 969                        throw new FileNotFoundException();
 970                    }
 971                    thumbnail = resize(fullSize, size);
 972                    thumbnail = rotate(thumbnail, getRotation(file));
 973                    if (mime.equals("image/gif")) {
 974                        Bitmap withGifOverlay = thumbnail.copy(Bitmap.Config.ARGB_8888, true);
 975                        drawOverlay(
 976                                withGifOverlay,
 977                                paintOverlayBlack(withGifOverlay)
 978                                        ? R.drawable.play_gif_black
 979                                        : R.drawable.play_gif_white,
 980                                1.0f);
 981                        thumbnail.recycle();
 982                        thumbnail = withGifOverlay;
 983                    }
 984                }
 985                cache.put(uuid, thumbnail);
 986            }
 987        }
 988        return thumbnail;
 989    }
 990
 991    private Bitmap getFullSizeImagePreview(File file, int size) {
 992        BitmapFactory.Options options = new BitmapFactory.Options();
 993        options.inSampleSize = calcSampleSize(file, size);
 994        try {
 995            return BitmapFactory.decodeFile(file.getAbsolutePath(), options);
 996        } catch (OutOfMemoryError e) {
 997            options.inSampleSize *= 2;
 998            return BitmapFactory.decodeFile(file.getAbsolutePath(), options);
 999        }
1000    }
1001
1002    private void drawOverlay(Bitmap bitmap, int resource, float factor) {
1003        Bitmap overlay =
1004                BitmapFactory.decodeResource(mXmppConnectionService.getResources(), resource);
1005        Canvas canvas = new Canvas(bitmap);
1006        float targetSize = Math.min(canvas.getWidth(), canvas.getHeight()) * factor;
1007        Log.d(
1008                Config.LOGTAG,
1009                "target size overlay: "
1010                        + targetSize
1011                        + " overlay bitmap size was "
1012                        + overlay.getHeight());
1013        float left = (canvas.getWidth() - targetSize) / 2.0f;
1014        float top = (canvas.getHeight() - targetSize) / 2.0f;
1015        RectF dst = new RectF(left, top, left + targetSize - 1, top + targetSize - 1);
1016        canvas.drawBitmap(overlay, null, dst, createAntiAliasingPaint());
1017    }
1018
1019    /** https://stackoverflow.com/a/3943023/210897 */
1020    private boolean paintOverlayBlack(final Bitmap bitmap) {
1021        final int h = bitmap.getHeight();
1022        final int w = bitmap.getWidth();
1023        int record = 0;
1024        for (int y = Math.round(h * IGNORE_PADDING); y < h - Math.round(h * IGNORE_PADDING); ++y) {
1025            for (int x = Math.round(w * IGNORE_PADDING);
1026                    x < w - Math.round(w * IGNORE_PADDING);
1027                    ++x) {
1028                int pixel = bitmap.getPixel(x, y);
1029                if ((Color.red(pixel) * 0.299
1030                                + Color.green(pixel) * 0.587
1031                                + Color.blue(pixel) * 0.114)
1032                        > 186) {
1033                    --record;
1034                } else {
1035                    ++record;
1036                }
1037            }
1038        }
1039        return record < 0;
1040    }
1041
1042    private boolean paintOverlayBlackPdf(final Bitmap bitmap) {
1043        final int h = bitmap.getHeight();
1044        final int w = bitmap.getWidth();
1045        int white = 0;
1046        for (int y = 0; y < h; ++y) {
1047            for (int x = 0; x < w; ++x) {
1048                int pixel = bitmap.getPixel(x, y);
1049                if ((Color.red(pixel) * 0.299
1050                                + Color.green(pixel) * 0.587
1051                                + Color.blue(pixel) * 0.114)
1052                        > 186) {
1053                    white++;
1054                }
1055            }
1056        }
1057        return white > (h * w * 0.4f);
1058    }
1059
1060    private Bitmap cropCenterSquareVideo(Uri uri, int size) {
1061        MediaMetadataRetriever metadataRetriever = new MediaMetadataRetriever();
1062        Bitmap frame;
1063        try {
1064            metadataRetriever.setDataSource(mXmppConnectionService, uri);
1065            frame = metadataRetriever.getFrameAtTime(0);
1066            metadataRetriever.release();
1067            return cropCenterSquare(frame, size);
1068        } catch (Exception e) {
1069            frame = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
1070            frame.eraseColor(0xff000000);
1071            return frame;
1072        }
1073    }
1074
1075    private Bitmap getVideoPreview(final File file, final int size) {
1076        final MediaMetadataRetriever metadataRetriever = new MediaMetadataRetriever();
1077        Bitmap frame;
1078        try {
1079            metadataRetriever.setDataSource(file.getAbsolutePath());
1080            frame = metadataRetriever.getFrameAtTime(0);
1081            metadataRetriever.release();
1082            frame = resize(frame, size);
1083        } catch (IOException | RuntimeException e) {
1084            frame = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
1085            frame.eraseColor(0xff000000);
1086        }
1087        drawOverlay(
1088                frame,
1089                paintOverlayBlack(frame)
1090                        ? R.drawable.play_video_black
1091                        : R.drawable.play_video_white,
1092                0.75f);
1093        return frame;
1094    }
1095
1096    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
1097    private Bitmap getPdfDocumentPreview(final File file, final int size) {
1098        try {
1099            final ParcelFileDescriptor fileDescriptor =
1100                    ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
1101            final Bitmap rendered = renderPdfDocument(fileDescriptor, size, true);
1102            drawOverlay(
1103                    rendered,
1104                    paintOverlayBlackPdf(rendered)
1105                            ? R.drawable.open_pdf_black
1106                            : R.drawable.open_pdf_white,
1107                    0.75f);
1108            return rendered;
1109        } catch (final IOException | SecurityException e) {
1110            Log.d(Config.LOGTAG, "unable to render PDF document preview", e);
1111            final Bitmap placeholder = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
1112            placeholder.eraseColor(0xff000000);
1113            return placeholder;
1114        }
1115    }
1116
1117    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
1118    private Bitmap cropCenterSquarePdf(final Uri uri, final int size) {
1119        try {
1120            ParcelFileDescriptor fileDescriptor =
1121                    mXmppConnectionService.getContentResolver().openFileDescriptor(uri, "r");
1122            final Bitmap bitmap = renderPdfDocument(fileDescriptor, size, false);
1123            return cropCenterSquare(bitmap, size);
1124        } catch (Exception e) {
1125            final Bitmap placeholder = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
1126            placeholder.eraseColor(0xff000000);
1127            return placeholder;
1128        }
1129    }
1130
1131    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
1132    private Bitmap renderPdfDocument(
1133            ParcelFileDescriptor fileDescriptor, int targetSize, boolean fit) throws IOException {
1134        final PdfRenderer pdfRenderer = new PdfRenderer(fileDescriptor);
1135        final PdfRenderer.Page page = pdfRenderer.openPage(0);
1136        final Dimensions dimensions =
1137                scalePdfDimensions(
1138                        new Dimensions(page.getHeight(), page.getWidth()), targetSize, fit);
1139        final Bitmap rendered =
1140                Bitmap.createBitmap(dimensions.width, dimensions.height, Bitmap.Config.ARGB_8888);
1141        rendered.eraseColor(0xffffffff);
1142        page.render(rendered, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
1143        page.close();
1144        pdfRenderer.close();
1145        fileDescriptor.close();
1146        return rendered;
1147    }
1148
1149    public Uri getTakePhotoUri() {
1150        File file;
1151        if (Config.ONLY_INTERNAL_STORAGE) {
1152            file =
1153                    new File(
1154                            mXmppConnectionService.getCacheDir().getAbsolutePath(),
1155                            "Camera/IMG_" + IMAGE_DATE_FORMAT.format(new Date()) + ".jpg");
1156        } else {
1157            file =
1158                    new File(
1159                            getTakePhotoPath()
1160                                    + "IMG_"
1161                                    + IMAGE_DATE_FORMAT.format(new Date())
1162                                    + ".jpg");
1163        }
1164        file.getParentFile().mkdirs();
1165        return getUriForFile(mXmppConnectionService, file);
1166    }
1167
1168    public Avatar getPepAvatar(Uri image, int size, Bitmap.CompressFormat format) {
1169
1170        final Avatar uncompressAvatar = getUncompressedAvatar(image);
1171        if (uncompressAvatar != null
1172                && uncompressAvatar.image.length() <= Config.AVATAR_CHAR_LIMIT) {
1173            return uncompressAvatar;
1174        }
1175        if (uncompressAvatar != null) {
1176            Log.d(
1177                    Config.LOGTAG,
1178                    "uncompressed avatar exceeded char limit by "
1179                            + (uncompressAvatar.image.length() - Config.AVATAR_CHAR_LIMIT));
1180        }
1181
1182        Bitmap bm = cropCenterSquare(image, size);
1183        if (bm == null) {
1184            return null;
1185        }
1186        if (hasAlpha(bm)) {
1187            Log.d(Config.LOGTAG, "alpha in avatar detected; uploading as PNG");
1188            bm.recycle();
1189            bm = cropCenterSquare(image, 96);
1190            return getPepAvatar(bm, Bitmap.CompressFormat.PNG, 100);
1191        }
1192        return getPepAvatar(bm, format, 100);
1193    }
1194
1195    private Avatar getUncompressedAvatar(Uri uri) {
1196        Bitmap bitmap = null;
1197        try {
1198            bitmap =
1199                    BitmapFactory.decodeStream(
1200                            mXmppConnectionService.getContentResolver().openInputStream(uri));
1201            return getPepAvatar(bitmap, Bitmap.CompressFormat.PNG, 100);
1202        } catch (Exception e) {
1203            return null;
1204        } finally {
1205            if (bitmap != null) {
1206                bitmap.recycle();
1207            }
1208        }
1209    }
1210
1211    private Avatar getPepAvatar(Bitmap bitmap, Bitmap.CompressFormat format, int quality) {
1212        try {
1213            ByteArrayOutputStream mByteArrayOutputStream = new ByteArrayOutputStream();
1214            Base64OutputStream mBase64OutputStream =
1215                    new Base64OutputStream(mByteArrayOutputStream, Base64.DEFAULT);
1216            MessageDigest digest = MessageDigest.getInstance("SHA-1");
1217            DigestOutputStream mDigestOutputStream =
1218                    new DigestOutputStream(mBase64OutputStream, digest);
1219            if (!bitmap.compress(format, quality, mDigestOutputStream)) {
1220                return null;
1221            }
1222            mDigestOutputStream.flush();
1223            mDigestOutputStream.close();
1224            long chars = mByteArrayOutputStream.size();
1225            if (format != Bitmap.CompressFormat.PNG
1226                    && quality >= 50
1227                    && chars >= Config.AVATAR_CHAR_LIMIT) {
1228                int q = quality - 2;
1229                Log.d(
1230                        Config.LOGTAG,
1231                        "avatar char length was " + chars + " reducing quality to " + q);
1232                return getPepAvatar(bitmap, format, q);
1233            }
1234            Log.d(Config.LOGTAG, "settled on char length " + chars + " with quality=" + quality);
1235            final Avatar avatar = new Avatar();
1236            avatar.sha1sum = CryptoHelper.bytesToHex(digest.digest());
1237            avatar.image = new String(mByteArrayOutputStream.toByteArray());
1238            if (format.equals(Bitmap.CompressFormat.WEBP)) {
1239                avatar.type = "image/webp";
1240            } else if (format.equals(Bitmap.CompressFormat.JPEG)) {
1241                avatar.type = "image/jpeg";
1242            } else if (format.equals(Bitmap.CompressFormat.PNG)) {
1243                avatar.type = "image/png";
1244            }
1245            avatar.width = bitmap.getWidth();
1246            avatar.height = bitmap.getHeight();
1247            return avatar;
1248        } catch (OutOfMemoryError e) {
1249            Log.d(Config.LOGTAG, "unable to convert avatar to base64 due to low memory");
1250            return null;
1251        } catch (Exception e) {
1252            return null;
1253        }
1254    }
1255
1256    public Avatar getStoredPepAvatar(String hash) {
1257        if (hash == null) {
1258            return null;
1259        }
1260        Avatar avatar = new Avatar();
1261        final File file = getAvatarFile(hash);
1262        FileInputStream is = null;
1263        try {
1264            avatar.size = file.length();
1265            BitmapFactory.Options options = new BitmapFactory.Options();
1266            options.inJustDecodeBounds = true;
1267            BitmapFactory.decodeFile(file.getAbsolutePath(), options);
1268            is = new FileInputStream(file);
1269            ByteArrayOutputStream mByteArrayOutputStream = new ByteArrayOutputStream();
1270            Base64OutputStream mBase64OutputStream =
1271                    new Base64OutputStream(mByteArrayOutputStream, Base64.DEFAULT);
1272            MessageDigest digest = MessageDigest.getInstance("SHA-1");
1273            DigestOutputStream os = new DigestOutputStream(mBase64OutputStream, digest);
1274            byte[] buffer = new byte[4096];
1275            int length;
1276            while ((length = is.read(buffer)) > 0) {
1277                os.write(buffer, 0, length);
1278            }
1279            os.flush();
1280            os.close();
1281            avatar.sha1sum = CryptoHelper.bytesToHex(digest.digest());
1282            avatar.image = new String(mByteArrayOutputStream.toByteArray());
1283            avatar.height = options.outHeight;
1284            avatar.width = options.outWidth;
1285            avatar.type = options.outMimeType;
1286            return avatar;
1287        } catch (NoSuchAlgorithmException | IOException e) {
1288            return null;
1289        } finally {
1290            close(is);
1291        }
1292    }
1293
1294    public boolean isAvatarCached(Avatar avatar) {
1295        final File file = getAvatarFile(avatar.getFilename());
1296        return file.exists();
1297    }
1298
1299    public boolean save(final Avatar avatar) {
1300        File file;
1301        if (isAvatarCached(avatar)) {
1302            file = getAvatarFile(avatar.getFilename());
1303            avatar.size = file.length();
1304        } else {
1305            file =
1306                    new File(
1307                            mXmppConnectionService.getCacheDir().getAbsolutePath()
1308                                    + "/"
1309                                    + UUID.randomUUID().toString());
1310            if (file.getParentFile().mkdirs()) {
1311                Log.d(Config.LOGTAG, "created cache directory");
1312            }
1313            OutputStream os = null;
1314            try {
1315                if (!file.createNewFile()) {
1316                    Log.d(
1317                            Config.LOGTAG,
1318                            "unable to create temporary file " + file.getAbsolutePath());
1319                }
1320                os = new FileOutputStream(file);
1321                MessageDigest digest = MessageDigest.getInstance("SHA-1");
1322                digest.reset();
1323                DigestOutputStream mDigestOutputStream = new DigestOutputStream(os, digest);
1324                final byte[] bytes = avatar.getImageAsBytes();
1325                mDigestOutputStream.write(bytes);
1326                mDigestOutputStream.flush();
1327                mDigestOutputStream.close();
1328                String sha1sum = CryptoHelper.bytesToHex(digest.digest());
1329                if (sha1sum.equals(avatar.sha1sum)) {
1330                    final File outputFile = getAvatarFile(avatar.getFilename());
1331                    if (outputFile.getParentFile().mkdirs()) {
1332                        Log.d(Config.LOGTAG, "created avatar directory");
1333                    }
1334                    final File avatarFile = getAvatarFile(avatar.getFilename());
1335                    if (!file.renameTo(avatarFile)) {
1336                        Log.d(
1337                                Config.LOGTAG,
1338                                "unable to rename " + file.getAbsolutePath() + " to " + outputFile);
1339                        return false;
1340                    }
1341                } else {
1342                    Log.d(Config.LOGTAG, "sha1sum mismatch for " + avatar.owner);
1343                    if (!file.delete()) {
1344                        Log.d(Config.LOGTAG, "unable to delete temporary file");
1345                    }
1346                    return false;
1347                }
1348                avatar.size = bytes.length;
1349            } catch (IllegalArgumentException | IOException | NoSuchAlgorithmException e) {
1350                return false;
1351            } finally {
1352                close(os);
1353            }
1354        }
1355        return true;
1356    }
1357
1358    public void deleteHistoricAvatarPath() {
1359        delete(getHistoricAvatarPath());
1360    }
1361
1362    private void delete(final File file) {
1363        if (file.isDirectory()) {
1364            final File[] files = file.listFiles();
1365            if (files != null) {
1366                for (final File f : files) {
1367                    delete(f);
1368                }
1369            }
1370        }
1371        if (file.delete()) {
1372            Log.d(Config.LOGTAG, "deleted " + file.getAbsolutePath());
1373        }
1374    }
1375
1376    private File getHistoricAvatarPath() {
1377        return new File(mXmppConnectionService.getFilesDir(), "/avatars/");
1378    }
1379
1380    private File getAvatarFile(String avatar) {
1381        return new File(mXmppConnectionService.getCacheDir(), "/avatars/" + avatar);
1382    }
1383
1384    public Uri getAvatarUri(String avatar) {
1385        return Uri.fromFile(getAvatarFile(avatar));
1386    }
1387
1388    public Bitmap cropCenterSquare(Uri image, int size) {
1389        if (image == null) {
1390            return null;
1391        }
1392        InputStream is = null;
1393        try {
1394            BitmapFactory.Options options = new BitmapFactory.Options();
1395            options.inSampleSize = calcSampleSize(image, size);
1396            is = mXmppConnectionService.getContentResolver().openInputStream(image);
1397            if (is == null) {
1398                return null;
1399            }
1400            Bitmap input = BitmapFactory.decodeStream(is, null, options);
1401            if (input == null) {
1402                return null;
1403            } else {
1404                input = rotate(input, getRotation(image));
1405                return cropCenterSquare(input, size);
1406            }
1407        } catch (FileNotFoundException | SecurityException e) {
1408            Log.d(Config.LOGTAG, "unable to open file " + image.toString(), e);
1409            return null;
1410        } finally {
1411            close(is);
1412        }
1413    }
1414
1415    public Bitmap cropCenter(Uri image, int newHeight, int newWidth) {
1416        if (image == null) {
1417            return null;
1418        }
1419        InputStream is = null;
1420        try {
1421            BitmapFactory.Options options = new BitmapFactory.Options();
1422            options.inSampleSize = calcSampleSize(image, Math.max(newHeight, newWidth));
1423            is = mXmppConnectionService.getContentResolver().openInputStream(image);
1424            if (is == null) {
1425                return null;
1426            }
1427            Bitmap source = BitmapFactory.decodeStream(is, null, options);
1428            if (source == null) {
1429                return null;
1430            }
1431            int sourceWidth = source.getWidth();
1432            int sourceHeight = source.getHeight();
1433            float xScale = (float) newWidth / sourceWidth;
1434            float yScale = (float) newHeight / sourceHeight;
1435            float scale = Math.max(xScale, yScale);
1436            float scaledWidth = scale * sourceWidth;
1437            float scaledHeight = scale * sourceHeight;
1438            float left = (newWidth - scaledWidth) / 2;
1439            float top = (newHeight - scaledHeight) / 2;
1440
1441            RectF targetRect = new RectF(left, top, left + scaledWidth, top + scaledHeight);
1442            Bitmap dest = Bitmap.createBitmap(newWidth, newHeight, Bitmap.Config.ARGB_8888);
1443            Canvas canvas = new Canvas(dest);
1444            canvas.drawBitmap(source, null, targetRect, createAntiAliasingPaint());
1445            if (source.isRecycled()) {
1446                source.recycle();
1447            }
1448            return dest;
1449        } catch (SecurityException e) {
1450            return null; // android 6.0 with revoked permissions for example
1451        } catch (FileNotFoundException e) {
1452            return null;
1453        } finally {
1454            close(is);
1455        }
1456    }
1457
1458    public Bitmap cropCenterSquare(Bitmap input, int size) {
1459        int w = input.getWidth();
1460        int h = input.getHeight();
1461
1462        float scale = Math.max((float) size / h, (float) size / w);
1463
1464        float outWidth = scale * w;
1465        float outHeight = scale * h;
1466        float left = (size - outWidth) / 2;
1467        float top = (size - outHeight) / 2;
1468        RectF target = new RectF(left, top, left + outWidth, top + outHeight);
1469
1470        Bitmap output = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
1471        Canvas canvas = new Canvas(output);
1472        canvas.drawBitmap(input, null, target, createAntiAliasingPaint());
1473        if (!input.isRecycled()) {
1474            input.recycle();
1475        }
1476        return output;
1477    }
1478
1479    private int calcSampleSize(Uri image, int size)
1480            throws FileNotFoundException, SecurityException {
1481        final BitmapFactory.Options options = new BitmapFactory.Options();
1482        options.inJustDecodeBounds = true;
1483        final InputStream inputStream =
1484                mXmppConnectionService.getContentResolver().openInputStream(image);
1485        BitmapFactory.decodeStream(inputStream, null, options);
1486        close(inputStream);
1487        return calcSampleSize(options, size);
1488    }
1489
1490    public void updateFileParams(Message message) {
1491        updateFileParams(message, null);
1492    }
1493
1494    public void updateFileParams(Message message, String url) {
1495        DownloadableFile file = getFile(message);
1496        final String mime = file.getMimeType();
1497        final boolean privateMessage = message.isPrivateMessage();
1498        final boolean image =
1499                message.getType() == Message.TYPE_IMAGE
1500                        || (mime != null && mime.startsWith("image/"));
1501        final boolean video = mime != null && mime.startsWith("video/");
1502        final boolean audio = mime != null && mime.startsWith("audio/");
1503        final boolean pdf = "application/pdf".equals(mime);
1504        final StringBuilder body = new StringBuilder();
1505        if (url != null) {
1506            body.append(url);
1507        }
1508        body.append('|').append(file.getSize());
1509        if (image || video || (pdf && Compatibility.runsTwentyOne())) {
1510            try {
1511                final Dimensions dimensions;
1512                if (video) {
1513                    dimensions = getVideoDimensions(file);
1514                } else if (pdf && Compatibility.runsTwentyOne()) {
1515                    dimensions = getPdfDocumentDimensions(file);
1516                } else {
1517                    dimensions = getImageDimensions(file);
1518                }
1519                if (dimensions.valid()) {
1520                    body.append('|').append(dimensions.width).append('|').append(dimensions.height);
1521                }
1522            } catch (NotAVideoFile notAVideoFile) {
1523                Log.d(
1524                        Config.LOGTAG,
1525                        "file with mime type " + file.getMimeType() + " was not a video file");
1526                // fall threw
1527            }
1528        } else if (audio) {
1529            body.append("|0|0|").append(getMediaRuntime(file));
1530        }
1531        message.setBody(body.toString());
1532        message.setDeleted(false);
1533        message.setType(
1534                privateMessage
1535                        ? Message.TYPE_PRIVATE_FILE
1536                        : (image ? Message.TYPE_IMAGE : Message.TYPE_FILE));
1537    }
1538
1539    private int getMediaRuntime(File file) {
1540        try {
1541            MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
1542            mediaMetadataRetriever.setDataSource(file.toString());
1543            return Integer.parseInt(
1544                    mediaMetadataRetriever.extractMetadata(
1545                            MediaMetadataRetriever.METADATA_KEY_DURATION));
1546        } catch (RuntimeException e) {
1547            return 0;
1548        }
1549    }
1550
1551    private Dimensions getImageDimensions(File file) {
1552        BitmapFactory.Options options = new BitmapFactory.Options();
1553        options.inJustDecodeBounds = true;
1554        BitmapFactory.decodeFile(file.getAbsolutePath(), options);
1555        int rotation = getRotation(file);
1556        boolean rotated = rotation == 90 || rotation == 270;
1557        int imageHeight = rotated ? options.outWidth : options.outHeight;
1558        int imageWidth = rotated ? options.outHeight : options.outWidth;
1559        return new Dimensions(imageHeight, imageWidth);
1560    }
1561
1562    private Dimensions getVideoDimensions(File file) throws NotAVideoFile {
1563        MediaMetadataRetriever metadataRetriever = new MediaMetadataRetriever();
1564        try {
1565            metadataRetriever.setDataSource(file.getAbsolutePath());
1566        } catch (RuntimeException e) {
1567            throw new NotAVideoFile(e);
1568        }
1569        return getVideoDimensions(metadataRetriever);
1570    }
1571
1572    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
1573    private Dimensions getPdfDocumentDimensions(final File file) {
1574        final ParcelFileDescriptor fileDescriptor;
1575        try {
1576            fileDescriptor = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
1577            if (fileDescriptor == null) {
1578                return new Dimensions(0, 0);
1579            }
1580        } catch (FileNotFoundException e) {
1581            return new Dimensions(0, 0);
1582        }
1583        try {
1584            final PdfRenderer pdfRenderer = new PdfRenderer(fileDescriptor);
1585            final PdfRenderer.Page page = pdfRenderer.openPage(0);
1586            final int height = page.getHeight();
1587            final int width = page.getWidth();
1588            page.close();
1589            pdfRenderer.close();
1590            return scalePdfDimensions(new Dimensions(height, width));
1591        } catch (IOException | SecurityException e) {
1592            Log.d(Config.LOGTAG, "unable to get dimensions for pdf document", e);
1593            return new Dimensions(0, 0);
1594        }
1595    }
1596
1597    private Dimensions scalePdfDimensions(Dimensions in) {
1598        final DisplayMetrics displayMetrics =
1599                mXmppConnectionService.getResources().getDisplayMetrics();
1600        final int target = (int) (displayMetrics.density * 288);
1601        return scalePdfDimensions(in, target, true);
1602    }
1603
1604    private static Dimensions scalePdfDimensions(
1605            final Dimensions in, final int target, final boolean fit) {
1606        final int w, h;
1607        if (fit == (in.width <= in.height)) {
1608            w = Math.max((int) (in.width / ((double) in.height / target)), 1);
1609            h = target;
1610        } else {
1611            w = target;
1612            h = Math.max((int) (in.height / ((double) in.width / target)), 1);
1613        }
1614        return new Dimensions(h, w);
1615    }
1616
1617    public Bitmap getAvatar(String avatar, int size) {
1618        if (avatar == null) {
1619            return null;
1620        }
1621        Bitmap bm = cropCenter(getAvatarUri(avatar), size, size);
1622        return bm;
1623    }
1624
1625    private static class Dimensions {
1626        public final int width;
1627        public final int height;
1628
1629        Dimensions(int height, int width) {
1630            this.width = width;
1631            this.height = height;
1632        }
1633
1634        public int getMin() {
1635            return Math.min(width, height);
1636        }
1637
1638        public boolean valid() {
1639            return width > 0 && height > 0;
1640        }
1641    }
1642
1643    private static class NotAVideoFile extends Exception {
1644        public NotAVideoFile(Throwable t) {
1645            super(t);
1646        }
1647
1648        public NotAVideoFile() {
1649            super();
1650        }
1651    }
1652
1653    public static class ImageCompressionException extends Exception {
1654
1655        ImageCompressionException(String message) {
1656            super(message);
1657        }
1658    }
1659
1660    public static class FileCopyException extends Exception {
1661        private final int resId;
1662
1663        private FileCopyException(@StringRes int resId) {
1664            this.resId = resId;
1665        }
1666
1667        public @StringRes int getResId() {
1668            return resId;
1669        }
1670    }
1671}