FileBackend.java

   1package eu.siacs.conversations.persistance;
   2
   3import android.content.ContentResolver;
   4import android.content.Context;
   5import android.content.res.AssetFileDescriptor;
   6import android.content.res.Resources;
   7import android.database.Cursor;
   8import android.graphics.Bitmap;
   9import android.graphics.BitmapFactory;
  10import android.graphics.Canvas;
  11import android.graphics.Color;
  12import android.graphics.drawable.BitmapDrawable;
  13import android.graphics.drawable.Drawable;
  14import android.graphics.ImageDecoder;
  15import android.graphics.Matrix;
  16import android.graphics.Paint;
  17import android.graphics.Rect;
  18import android.graphics.RectF;
  19import android.graphics.pdf.PdfRenderer;
  20import android.media.MediaMetadataRetriever;
  21import android.media.MediaScannerConnection;
  22import android.net.Uri;
  23import android.os.Build;
  24import android.os.Environment;
  25import android.os.ParcelFileDescriptor;
  26import android.provider.MediaStore;
  27import android.provider.OpenableColumns;
  28import android.system.Os;
  29import android.system.StructStat;
  30import android.util.Base64;
  31import android.util.Base64OutputStream;
  32import android.util.DisplayMetrics;
  33import android.util.Pair;
  34import android.util.Log;
  35import android.util.LruCache;
  36
  37import androidx.annotation.RequiresApi;
  38import androidx.annotation.StringRes;
  39import androidx.core.content.FileProvider;
  40import androidx.documentfile.provider.DocumentFile;
  41import androidx.exifinterface.media.ExifInterface;
  42
  43import com.caverock.androidsvg.SVG;
  44import com.caverock.androidsvg.SVGParseException;
  45
  46import com.cheogram.android.BobTransfer;
  47
  48import com.google.common.base.Strings;
  49import com.google.common.collect.ImmutableList;
  50import com.google.common.io.ByteStreams;
  51
  52import com.madebyevan.thumbhash.ThumbHash;
  53
  54import com.wolt.blurhashkt.BlurHashDecoder;
  55
  56import eu.siacs.conversations.Config;
  57import eu.siacs.conversations.R;
  58import eu.siacs.conversations.entities.Conversation;
  59import eu.siacs.conversations.entities.DownloadableFile;
  60import eu.siacs.conversations.entities.Message;
  61import eu.siacs.conversations.services.AttachFileToConversationRunnable;
  62import eu.siacs.conversations.services.XmppConnectionService;
  63import eu.siacs.conversations.ui.adapter.MediaAdapter;
  64import eu.siacs.conversations.ui.util.Attachment;
  65import eu.siacs.conversations.utils.CryptoHelper;
  66import eu.siacs.conversations.utils.FileUtils;
  67import eu.siacs.conversations.utils.FileWriterException;
  68import eu.siacs.conversations.utils.MimeUtils;
  69import eu.siacs.conversations.xmpp.pep.Avatar;
  70
  71import java.io.ByteArrayInputStream;
  72import java.io.ByteArrayOutputStream;
  73import java.io.Closeable;
  74import java.io.File;
  75import java.io.FileDescriptor;
  76import java.io.FileInputStream;
  77import java.io.FileNotFoundException;
  78import java.io.FileOutputStream;
  79import java.io.IOException;
  80import java.io.InputStream;
  81import java.io.OutputStream;
  82import java.net.ServerSocket;
  83import java.net.Socket;
  84import java.nio.ByteBuffer;
  85import java.security.DigestOutputStream;
  86import java.security.MessageDigest;
  87import java.security.NoSuchAlgorithmException;
  88import java.text.SimpleDateFormat;
  89import java.util.ArrayList;
  90import java.util.Arrays;
  91import java.util.Date;
  92import java.util.List;
  93import java.util.Locale;
  94import java.util.UUID;
  95import java.util.zip.ZipEntry;
  96import java.util.zip.ZipFile;
  97
  98import org.tomlj.Toml;
  99import org.tomlj.TomlTable;
 100
 101import io.ipfs.cid.Cid;
 102
 103public class FileBackend {
 104
 105    private static final Object THUMBNAIL_LOCK = new Object();
 106
 107    private static final SimpleDateFormat IMAGE_DATE_FORMAT =
 108            new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US);
 109
 110    private static final String FILE_PROVIDER = ".files";
 111    private static final float IGNORE_PADDING = 0.15f;
 112    private final XmppConnectionService mXmppConnectionService;
 113
 114    private static final List<String> STORAGE_TYPES;
 115
 116    static {
 117        final ImmutableList.Builder<String> builder =
 118                new ImmutableList.Builder<String>()
 119                        .add(
 120                                Environment.DIRECTORY_DOWNLOADS,
 121                                Environment.DIRECTORY_PICTURES,
 122                                Environment.DIRECTORY_MOVIES);
 123        if (Build.VERSION.SDK_INT >= 19) {
 124            builder.add(Environment.DIRECTORY_DOCUMENTS);
 125        }
 126        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
 127            builder.add(Environment.DIRECTORY_RECORDINGS);
 128        }
 129        STORAGE_TYPES = builder.build();
 130    }
 131
 132    public FileBackend(XmppConnectionService service) {
 133        this.mXmppConnectionService = service;
 134    }
 135
 136    public static long getFileSize(Context context, Uri uri) {
 137        try (final Cursor cursor =
 138                context.getContentResolver().query(uri, null, null, null, null)) {
 139            if (cursor != null && cursor.moveToFirst()) {
 140                final int index = cursor.getColumnIndex(OpenableColumns.SIZE);
 141                if (index == -1) {
 142                    return -1;
 143                }
 144                return cursor.getLong(index);
 145            }
 146            return -1;
 147        } catch (final Exception ignored) {
 148            return -1;
 149        }
 150    }
 151
 152    public static boolean allFilesUnderSize(
 153            Context context, List<Attachment> attachments, long max) {
 154        final boolean compressVideo =
 155                !AttachFileToConversationRunnable.getVideoCompression(context)
 156                        .equals("uncompressed");
 157        if (max <= 0) {
 158            Log.d(Config.LOGTAG, "server did not report max file size for http upload");
 159            return true; // exception to be compatible with HTTP Upload < v0.2
 160        }
 161        for (Attachment attachment : attachments) {
 162            if (attachment.getType() != Attachment.Type.FILE) {
 163                continue;
 164            }
 165            String mime = attachment.getMime();
 166            if (mime != null && mime.startsWith("video/") && compressVideo) {
 167                try {
 168                    Dimensions dimensions =
 169                            FileBackend.getVideoDimensions(context, attachment.getUri());
 170                    if (dimensions.getMin() > 720) {
 171                        Log.d(
 172                                Config.LOGTAG,
 173                                "do not consider video file with min width larger than 720 for size check");
 174                        continue;
 175                    }
 176                } catch (final IOException | NotAVideoFile e) {
 177                    // ignore and fall through
 178                }
 179            }
 180            if (FileBackend.getFileSize(context, attachment.getUri()) > max) {
 181                Log.d(
 182                        Config.LOGTAG,
 183                        "not all files are under "
 184                                + max
 185                                + " bytes. suggesting falling back to jingle");
 186                return false;
 187            }
 188        }
 189        return true;
 190    }
 191
 192    public static File getBackupDirectory(final Context context) {
 193        final File conversationsDownloadDirectory =
 194                new File(
 195                        Environment.getExternalStoragePublicDirectory(
 196                                Environment.DIRECTORY_DOWNLOADS),
 197                        context.getString(R.string.app_name));
 198        return new File(conversationsDownloadDirectory, "Backup");
 199    }
 200
 201    public static File getLegacyBackupDirectory(final String app) {
 202        final File appDirectory = new File(Environment.getExternalStorageDirectory(), app);
 203        return new File(appDirectory, "Backup");
 204    }
 205
 206    private static Bitmap rotate(final Bitmap bitmap, final int degree) {
 207        if (degree == 0) {
 208            return bitmap;
 209        }
 210        final int w = bitmap.getWidth();
 211        final int h = bitmap.getHeight();
 212        final Matrix matrix = new Matrix();
 213        matrix.postRotate(degree);
 214        final Bitmap result = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);
 215        if (!bitmap.isRecycled()) {
 216            bitmap.recycle();
 217        }
 218        return result;
 219    }
 220
 221    public static boolean isPathBlacklisted(String path) {
 222        final String androidDataPath =
 223                Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/";
 224        final File f = new File(path);
 225        return path.startsWith(androidDataPath) || !f.canRead();
 226    }
 227
 228    private static Paint createAntiAliasingPaint() {
 229        Paint paint = new Paint();
 230        paint.setAntiAlias(true);
 231        paint.setFilterBitmap(true);
 232        paint.setDither(true);
 233        return paint;
 234    }
 235
 236    public static Uri getUriForUri(Context context, Uri uri) {
 237        if ("file".equals(uri.getScheme())) {
 238            return getUriForFile(context, new File(uri.getPath()));
 239        } else {
 240            return uri;
 241        }
 242    }
 243
 244    public static Uri getUriForFile(Context context, File file) {
 245        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N || Config.ONLY_INTERNAL_STORAGE || file.toString().startsWith(context.getCacheDir().toString())) {
 246            try {
 247                return FileProvider.getUriForFile(context, getAuthority(context), file);
 248            } catch (IllegalArgumentException e) {
 249                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
 250                    throw new SecurityException(e);
 251                } else {
 252                    return Uri.fromFile(file);
 253                }
 254            }
 255        } else {
 256            return Uri.fromFile(file);
 257        }
 258    }
 259
 260    public static String getAuthority(Context context) {
 261        return context.getPackageName() + FILE_PROVIDER;
 262    }
 263
 264    private static boolean hasAlpha(final Bitmap bitmap) {
 265        final int w = bitmap.getWidth();
 266        final int h = bitmap.getHeight();
 267        final int yStep = Math.max(1, w / 100);
 268        final int xStep = Math.max(1, h / 100);
 269        for (int x = 0; x < w; x += xStep) {
 270            for (int y = 0; y < h; y += yStep) {
 271                if (Color.alpha(bitmap.getPixel(x, y)) < 255) {
 272                    return true;
 273                }
 274            }
 275        }
 276        return false;
 277    }
 278
 279    private static int calcSampleSize(File image, int size) {
 280        BitmapFactory.Options options = new BitmapFactory.Options();
 281        options.inJustDecodeBounds = true;
 282        BitmapFactory.decodeFile(image.getAbsolutePath(), options);
 283        return calcSampleSize(options, size);
 284    }
 285
 286    private static int calcSampleSize(BitmapFactory.Options options, int size) {
 287        int height = options.outHeight;
 288        int width = options.outWidth;
 289        int inSampleSize = 1;
 290
 291        if (height > size || width > size) {
 292            int halfHeight = height / 2;
 293            int halfWidth = width / 2;
 294
 295            while ((halfHeight / inSampleSize) > size && (halfWidth / inSampleSize) > size) {
 296                inSampleSize *= 2;
 297            }
 298        }
 299        return inSampleSize;
 300    }
 301
 302    private static Dimensions getVideoDimensions(Context context, Uri uri) throws NotAVideoFile, IOException {
 303        MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
 304        try {
 305            mediaMetadataRetriever.setDataSource(context, uri);
 306        } catch (RuntimeException e) {
 307            throw new NotAVideoFile(e);
 308        }
 309        return getVideoDimensions(mediaMetadataRetriever);
 310    }
 311
 312    private static Dimensions getVideoDimensionsOfFrame(
 313            MediaMetadataRetriever mediaMetadataRetriever) {
 314        Bitmap bitmap = null;
 315        try {
 316            bitmap = mediaMetadataRetriever.getFrameAtTime();
 317            return new Dimensions(bitmap.getHeight(), bitmap.getWidth());
 318        } catch (Exception e) {
 319            return null;
 320        } finally {
 321            if (bitmap != null) {
 322                bitmap.recycle();
 323            }
 324        }
 325    }
 326
 327    private static Dimensions getVideoDimensions(MediaMetadataRetriever metadataRetriever)
 328            throws NotAVideoFile, IOException {
 329        String hasVideo =
 330                metadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_HAS_VIDEO);
 331        if (hasVideo == null) {
 332            throw new NotAVideoFile();
 333        }
 334        Dimensions dimensions = getVideoDimensionsOfFrame(metadataRetriever);
 335        if (dimensions != null) {
 336            return dimensions;
 337        }
 338        final int rotation = extractRotationFromMediaRetriever(metadataRetriever);
 339        boolean rotated = rotation == 90 || rotation == 270;
 340        int height;
 341        try {
 342            String h =
 343                    metadataRetriever.extractMetadata(
 344                            MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT);
 345            height = Integer.parseInt(h);
 346        } catch (Exception e) {
 347            height = -1;
 348        }
 349        int width;
 350        try {
 351            String w =
 352                    metadataRetriever.extractMetadata(
 353                            MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH);
 354            width = Integer.parseInt(w);
 355        } catch (Exception e) {
 356            width = -1;
 357        }
 358        try {
 359            metadataRetriever.release();
 360        } catch (final IOException e) {
 361            throw new NotAVideoFile();
 362        }
 363        Log.d(Config.LOGTAG, "extracted video dims " + width + "x" + height);
 364        return rotated ? new Dimensions(width, height) : new Dimensions(height, width);
 365    }
 366
 367    private static int extractRotationFromMediaRetriever(MediaMetadataRetriever metadataRetriever) {
 368        String r =
 369                metadataRetriever.extractMetadata(
 370                        MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION);
 371        try {
 372            return Integer.parseInt(r);
 373        } catch (Exception e) {
 374            return 0;
 375        }
 376    }
 377
 378    public static void close(final Closeable stream) {
 379        if (stream != null) {
 380            try {
 381                stream.close();
 382            } catch (Exception e) {
 383                Log.d(Config.LOGTAG, "unable to close stream", e);
 384            }
 385        }
 386    }
 387
 388    public static void close(final Socket socket) {
 389        if (socket != null) {
 390            try {
 391                socket.close();
 392            } catch (IOException e) {
 393                Log.d(Config.LOGTAG, "unable to close socket", e);
 394            }
 395        }
 396    }
 397
 398    public static void close(final ServerSocket socket) {
 399        if (socket != null) {
 400            try {
 401                socket.close();
 402            } catch (IOException e) {
 403                Log.d(Config.LOGTAG, "unable to close server socket", e);
 404            }
 405        }
 406    }
 407
 408    public static boolean dangerousFile(final Uri uri) {
 409        if (uri == null || Strings.isNullOrEmpty(uri.getScheme())) {
 410            return true;
 411        }
 412        if (ContentResolver.SCHEME_FILE.equals(uri.getScheme())) {
 413            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
 414                // On Android 7 (and apps that target 7) it is now longer possible to share files
 415                // with a file scheme. By now you should probably not be running apps that target
 416                // anything less than 7 any more
 417                return true;
 418            } else {
 419                return isFileOwnedByProcess(uri);
 420            }
 421        }
 422        return false;
 423    }
 424
 425    private static boolean isFileOwnedByProcess(final Uri uri) {
 426        final String path = uri.getPath();
 427        if (path == null) {
 428            return true;
 429        }
 430        try (final var pfd =
 431                ParcelFileDescriptor.open(new File(path), ParcelFileDescriptor.MODE_READ_ONLY)) {
 432            final FileDescriptor fd = pfd.getFileDescriptor();
 433            final StructStat st = Os.fstat(fd);
 434            return st.st_uid == android.os.Process.myUid();
 435        } catch (final Exception e) {
 436            // when in doubt. better safe than sorry
 437            return true;
 438        }
 439    }
 440
 441    public static Uri getMediaUri(Context context, File file) {
 442        final String filePath = file.getAbsolutePath();
 443        try (final Cursor cursor =
 444                context.getContentResolver()
 445                        .query(
 446                                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
 447                                new String[] {MediaStore.Images.Media._ID},
 448                                MediaStore.Images.Media.DATA + "=? ",
 449                                new String[] {filePath},
 450                                null)) {
 451            if (cursor != null && cursor.moveToFirst()) {
 452                final int id =
 453                        cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns._ID));
 454                return Uri.withAppendedPath(
 455                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI, String.valueOf(id));
 456            } else {
 457                return null;
 458            }
 459        } catch (final Exception e) {
 460            return null;
 461        }
 462    }
 463
 464    public static void updateFileParams(Message message, String url, long size) {
 465        Message.FileParams fileParams = new Message.FileParams();
 466        fileParams.url = url;
 467        fileParams.size = size;
 468        message.setFileParams(fileParams);
 469    }
 470
 471    public Bitmap getPreviewForUri(Attachment attachment, int size, boolean cacheOnly) {
 472        final String key = "attachment_" + attachment.getUuid().toString() + "_" + size;
 473        final LruCache<String, Drawable> cache = mXmppConnectionService.getDrawableCache();
 474        Drawable drawable = cache.get(key);
 475        if (drawable != null || cacheOnly) {
 476            return drawDrawable(drawable);
 477        }
 478        Bitmap bitmap = null;
 479        final String mime = attachment.getMime();
 480        if ("application/pdf".equals(mime)) {
 481            bitmap = cropCenterSquarePdf(attachment.getUri(), size);
 482            drawOverlay(
 483                    bitmap,
 484                    paintOverlayBlackPdf(bitmap)
 485                            ? R.drawable.open_pdf_black
 486                            : R.drawable.open_pdf_white,
 487                    0.75f);
 488        } else if (mime != null && mime.startsWith("video/")) {
 489            bitmap = cropCenterSquareVideo(attachment.getUri(), size);
 490            drawOverlay(
 491                    bitmap,
 492                    paintOverlayBlack(bitmap)
 493                            ? R.drawable.play_video_black
 494                            : R.drawable.play_video_white,
 495                    0.75f);
 496        } else {
 497            bitmap = cropCenterSquare(attachment.getUri(), size);
 498            if (bitmap != null && "image/gif".equals(mime)) {
 499                Bitmap withGifOverlay = bitmap.copy(Bitmap.Config.ARGB_8888, true);
 500                drawOverlay(
 501                        withGifOverlay,
 502                        paintOverlayBlack(withGifOverlay)
 503                                ? R.drawable.play_gif_black
 504                                : R.drawable.play_gif_white,
 505                        1.0f);
 506                bitmap.recycle();
 507                bitmap = withGifOverlay;
 508            }
 509        }
 510        if (key != null && bitmap != null) {
 511            cache.put(key, new BitmapDrawable(bitmap));
 512        }
 513        return bitmap;
 514    }
 515
 516    public void updateMediaScanner(File file) {
 517        updateMediaScanner(file, null);
 518    }
 519
 520    public void updateMediaScanner(File file, final Runnable callback) {
 521        MediaScannerConnection.scanFile(
 522                mXmppConnectionService,
 523                new String[] {file.getAbsolutePath()},
 524                null,
 525                new MediaScannerConnection.MediaScannerConnectionClient() {
 526                    @Override
 527                    public void onMediaScannerConnected() {}
 528
 529                    @Override
 530                    public void onScanCompleted(String path, Uri uri) {
 531                        if (callback != null && file.getAbsolutePath().equals(path)) {
 532                            callback.run();
 533                        } else {
 534                            Log.d(Config.LOGTAG, "media scanner scanned wrong file");
 535                            if (callback != null) {
 536                                callback.run();
 537                            }
 538                        }
 539                    }
 540                });
 541    }
 542
 543    public boolean deleteFile(Message message) {
 544        File file = getFile(message);
 545        if (file.delete()) {
 546            message.setDeleted(true);
 547            updateMediaScanner(file);
 548            return true;
 549        } else {
 550            return false;
 551        }
 552    }
 553
 554    public DownloadableFile getFile(Message message) {
 555        return getFile(message, true);
 556    }
 557
 558    public DownloadableFile getFileForPath(String path) {
 559        return getFileForPath(
 560                path,
 561                MimeUtils.guessMimeTypeFromExtension(MimeUtils.extractRelevantExtension(path)));
 562    }
 563
 564    private DownloadableFile getFileForPath(final String path, final String mime) {
 565        if (path.startsWith("/")) {
 566            return new DownloadableFile(path);
 567        } else {
 568            return getLegacyFileForFilename(path, mime);
 569        }
 570    }
 571
 572    public DownloadableFile getLegacyFileForFilename(final String filename, final String mime) {
 573        if (Strings.isNullOrEmpty(mime)) {
 574            return new DownloadableFile(getLegacyStorageLocation("Files"), filename);
 575        } else if (mime.startsWith("image/")) {
 576            return new DownloadableFile(getLegacyStorageLocation("Images"), filename);
 577        } else if (mime.startsWith("video/")) {
 578            return new DownloadableFile(getLegacyStorageLocation("Videos"), filename);
 579        } else {
 580            return new DownloadableFile(getLegacyStorageLocation("Files"), filename);
 581        }
 582    }
 583
 584    public boolean isInternalFile(final File file) {
 585        final File internalFile = getFileForPath(file.getName());
 586        return file.getAbsolutePath().equals(internalFile.getAbsolutePath());
 587    }
 588
 589    public DownloadableFile getFile(Message message, boolean decrypted) {
 590        final boolean encrypted =
 591                !decrypted
 592                        && (message.getEncryption() == Message.ENCRYPTION_PGP
 593                                || message.getEncryption() == Message.ENCRYPTION_DECRYPTED);
 594        String path = message.getRelativeFilePath();
 595        if (path == null) {
 596            path = message.getUuid();
 597        }
 598        final var msgFile = getFileForPath(path, message.getMimeType());
 599
 600        final DownloadableFile file;
 601        if (encrypted) {
 602            file = new DownloadableFile(
 603                    mXmppConnectionService.getCacheDir(),
 604                    String.format("%s.%s", msgFile.getName(), "pgp"));
 605        } else {
 606            file = msgFile;
 607        }
 608
 609        try {
 610            if (file.exists() && file.toString().startsWith(mXmppConnectionService.getCacheDir().toString()) && Build.VERSION.SDK_INT >= 26) {
 611                java.nio.file.Files.setAttribute(file.toPath(), "lastAccessTime", java.nio.file.attribute.FileTime.fromMillis(System.currentTimeMillis()));
 612            }
 613        } catch (final IOException e) {
 614            Log.w(Config.LOGTAG, "unable to set lastAccessTime for " + file);
 615        }
 616
 617        return file;
 618    }
 619
 620    public List<Attachment> convertToAttachments(List<DatabaseBackend.FilePath> relativeFilePaths) {
 621        final List<Attachment> attachments = new ArrayList<>();
 622        for (final DatabaseBackend.FilePath relativeFilePath : relativeFilePaths) {
 623            final String mime =
 624                    MimeUtils.guessMimeTypeFromExtension(
 625                            MimeUtils.extractRelevantExtension(relativeFilePath.path));
 626            final File file = getFileForPath(relativeFilePath.path, mime);
 627            attachments.add(Attachment.of(relativeFilePath.uuid, file, mime));
 628        }
 629        return attachments;
 630    }
 631
 632    private File getLegacyStorageLocation(final String type) {
 633        if (Config.ONLY_INTERNAL_STORAGE) {
 634            return new File(mXmppConnectionService.getFilesDir(), type);
 635        } else {
 636            final File appDirectory =
 637                    new File(
 638                            Environment.getExternalStorageDirectory(),
 639                            mXmppConnectionService.getString(R.string.app_name));
 640            final File appMediaDirectory = new File(appDirectory, "Media");
 641            final String locationName =
 642                    String.format(
 643                            "%s %s", mXmppConnectionService.getString(R.string.app_name), type);
 644            return new File(appMediaDirectory, locationName);
 645        }
 646    }
 647
 648    private Bitmap resize(final Bitmap originalBitmap, int size) throws IOException {
 649        int w = originalBitmap.getWidth();
 650        int h = originalBitmap.getHeight();
 651        if (w <= 0 || h <= 0) {
 652            throw new IOException("Decoded bitmap reported bounds smaller 0");
 653        } else if (Math.max(w, h) > size) {
 654            int scalledW;
 655            int scalledH;
 656            if (w <= h) {
 657                scalledW = Math.max((int) (w / ((double) h / size)), 1);
 658                scalledH = size;
 659            } else {
 660                scalledW = size;
 661                scalledH = Math.max((int) (h / ((double) w / size)), 1);
 662            }
 663            final Bitmap result =
 664                    Bitmap.createScaledBitmap(originalBitmap, scalledW, scalledH, true);
 665            if (!originalBitmap.isRecycled()) {
 666                originalBitmap.recycle();
 667            }
 668            return result;
 669        } else {
 670            return originalBitmap;
 671        }
 672    }
 673
 674    public long getUriSize(final Uri uri) {
 675        Cursor cursor = null;
 676        try {
 677            cursor = mXmppConnectionService.getContentResolver().query(uri, null, null, null, null);
 678            if (cursor != null && cursor.moveToFirst()) {
 679                int sizeIndex = cursor.getColumnIndex(OpenableColumns.SIZE);
 680                return cursor.getLong(sizeIndex);
 681            }
 682        } finally {
 683            if (cursor != null) {
 684                cursor.close();
 685            }
 686        }
 687        return 0; // Return 0 if the size information is not available
 688    }
 689
 690    public boolean useImageAsIs(final Uri uri) {
 691        try {
 692            for (Cid cid : calculateCids(uri)) {
 693                if (mXmppConnectionService.getUrlForCid(cid) != null) return true;
 694            }
 695
 696            long fsize = getUriSize(uri);
 697            if (fsize == 0 || fsize >= mXmppConnectionService.getResources().getInteger(R.integer.auto_accept_filesize)) {
 698                return false;
 699            }
 700
 701            if (android.os.Build.VERSION.SDK_INT >= 28) {
 702                ImageDecoder.Source source = ImageDecoder.createSource(mXmppConnectionService.getContentResolver(), uri);
 703                int[] size = new int[] { 0, 0 };
 704                boolean[] animated = new boolean[] { false };
 705                String[] mimeType = new String[] { null };
 706                Drawable drawable = ImageDecoder.decodeDrawable(source, (decoder, info, src) -> {
 707                    mimeType[0] = info.getMimeType();
 708                    animated[0] = info.isAnimated();
 709                    size[0] = info.getSize().getWidth();
 710                    size[1] = info.getSize().getHeight();
 711                });
 712
 713                return animated[0] || (size[0] <= Config.IMAGE_SIZE && size[1] <= Config.IMAGE_SIZE);
 714            }
 715
 716            BitmapFactory.Options options = new BitmapFactory.Options();
 717            options.inJustDecodeBounds = true;
 718            final InputStream inputStream =
 719                    mXmppConnectionService.getContentResolver().openInputStream(uri);
 720            BitmapFactory.decodeStream(inputStream, null, options);
 721            close(inputStream);
 722            if (options.outMimeType == null || options.outHeight <= 0 || options.outWidth <= 0) {
 723                return false;
 724            }
 725            return (options.outWidth <= Config.IMAGE_SIZE && options.outHeight <= Config.IMAGE_SIZE);
 726        } catch (final IOException e) {
 727            Log.d(Config.LOGTAG, "unable to get image dimensions", e);
 728            return false;
 729        }
 730    }
 731
 732    public String getOriginalPath(final Uri uri) {
 733        return FileUtils.getPath(mXmppConnectionService, uri);
 734    }
 735
 736    public void copyFileToDocumentFile(Context ctx, File file, DocumentFile df) throws FileCopyException {
 737        Log.d(
 738                Config.LOGTAG,
 739                "copy file (" + file + ") to " + df);
 740        try (final InputStream is = new FileInputStream(file);
 741                final OutputStream os =
 742                        mXmppConnectionService.getContentResolver().openOutputStream(df.getUri())) {
 743            if (is == null) {
 744                throw new FileCopyException(R.string.error_file_not_found);
 745            }
 746            try {
 747                ByteStreams.copy(is, os);
 748                os.flush();
 749            } catch (IOException e) {
 750                throw new FileWriterException(file);
 751            }
 752        } catch (final FileNotFoundException e) {
 753            throw new FileCopyException(R.string.error_file_not_found);
 754        } catch (final FileWriterException e) {
 755            throw new FileCopyException(R.string.error_unable_to_create_temporary_file);
 756        } catch (final SecurityException | IllegalStateException e) {
 757            throw new FileCopyException(R.string.error_security_exception);
 758        } catch (final IOException e) {
 759            throw new FileCopyException(R.string.error_io_exception);
 760        }
 761    }
 762
 763    private InputStream openInputStream(Uri uri) throws IOException {
 764        if (uri != null && "data".equals(uri.getScheme())) {
 765            String[] parts = uri.getSchemeSpecificPart().split(",", 2);
 766            byte[] data;
 767            if (Arrays.asList(parts[0].split(";")).contains("base64")) {
 768                String[] parts2 = parts[0].split(";", 2);
 769                parts[0] = parts2[0];
 770                data = Base64.decode(parts[1], 0);
 771            } else {
 772                try {
 773                    data = parts[1].getBytes("UTF-8");
 774                } catch (final IOException e) {
 775                    data = new byte[0];
 776                }
 777            }
 778            return new ByteArrayInputStream(data);
 779        }
 780        final InputStream is = mXmppConnectionService.getContentResolver().openInputStream(uri);
 781        if (is == null) throw new FileNotFoundException("File not found");
 782        return is;
 783    }
 784
 785    private void copyFileToPrivateStorage(File file, Uri uri) throws FileCopyException {
 786        final var parentDirectory = file.getParentFile();
 787        if (parentDirectory != null && parentDirectory.mkdirs()) {
 788            Log.d(Config.LOGTAG,"created directory "+parentDirectory.getAbsolutePath());
 789        }
 790        try {
 791            if (!file.createNewFile() && file.length() > 0) {
 792                if (file.canRead() && file.getName().startsWith("zb2")) return; // We have this content already
 793                throw new FileCopyException(R.string.error_unable_to_create_temporary_file);
 794            }
 795        } catch (final IOException e) {
 796            throw new FileCopyException(R.string.error_unable_to_create_temporary_file);
 797        }
 798        try (final OutputStream os = new FileOutputStream(file);
 799                final InputStream is = openInputStream(uri)) {
 800            if (is == null) {
 801                throw new FileCopyException(R.string.error_file_not_found);
 802            }
 803            try {
 804                ByteStreams.copy(is, os);
 805            } catch (final IOException e) {
 806                throw new FileWriterException(file);
 807            }
 808            try {
 809                os.flush();
 810            } catch (final IOException e) {
 811                throw new FileWriterException(file);
 812            }
 813        } catch (final FileNotFoundException e) {
 814            cleanup(file);
 815            throw new FileCopyException(R.string.error_file_not_found);
 816        } catch (final FileWriterException e) {
 817            cleanup(file);
 818            throw new FileCopyException(R.string.error_unable_to_create_temporary_file);
 819        } catch (final SecurityException | IllegalStateException | IllegalArgumentException e) {
 820            cleanup(file);
 821            throw new FileCopyException(R.string.error_security_exception);
 822        } catch (final IOException e) {
 823            cleanup(file);
 824            throw new FileCopyException(R.string.error_io_exception);
 825        }
 826    }
 827
 828    public void copyFileToPrivateStorage(Message message, Uri uri, String type)
 829            throws FileCopyException {
 830        final String mime = MimeUtils.guessMimeTypeFromUriAndMime(mXmppConnectionService, uri, type);
 831        Log.d(Config.LOGTAG, "copy " + uri.toString() + " to private storage (mime=" + mime + ")");
 832        String extension = MimeUtils.guessExtensionFromMimeType(mime);
 833        if (extension == null) {
 834            Log.d(Config.LOGTAG, "extension from mime type was null");
 835            extension = getExtensionFromUri(uri);
 836        }
 837        if ("ogg".equals(extension) && type != null && type.startsWith("audio/")) {
 838            extension = "oga";
 839        }
 840
 841        try {
 842            setupRelativeFilePath(message, uri, extension);
 843            copyFileToPrivateStorage(mXmppConnectionService.getFileBackend().getFile(message), uri);
 844            final String name = getDisplayNameFromUri(uri);
 845            if (name != null) {
 846                message.getFileParams().setName(name);
 847            }
 848        } catch (final XmppConnectionService.BlockedMediaException e) {
 849            message.setRelativeFilePath(null);
 850            message.setDeleted(true);
 851        }
 852    }
 853
 854    private String getDisplayNameFromUri(final Uri uri) {
 855        final String[] projection = {OpenableColumns.DISPLAY_NAME};
 856        String filename = null;
 857        try (final Cursor cursor =
 858                mXmppConnectionService
 859                        .getContentResolver()
 860                        .query(uri, projection, null, null, null)) {
 861            if (cursor != null && cursor.moveToFirst()) {
 862                filename = cursor.getString(0);
 863            }
 864        } catch (final Exception e) {
 865            filename = null;
 866        }
 867        return filename;
 868    }
 869
 870    private String getExtensionFromUri(final Uri uri) {
 871        final String[] projection = {MediaStore.MediaColumns.DATA};
 872        String filename = null;
 873        try (final Cursor cursor =
 874                mXmppConnectionService
 875                        .getContentResolver()
 876                        .query(uri, projection, null, null, null)) {
 877            if (cursor != null && cursor.moveToFirst()) {
 878                filename = cursor.getString(0);
 879            }
 880        } catch (final Exception e) {
 881            filename = null;
 882        }
 883        if (filename == null) {
 884            final List<String> segments = uri.getPathSegments();
 885            if (segments.size() > 0) {
 886                filename = segments.get(segments.size() - 1);
 887            }
 888        }
 889        final int pos = filename == null ? -1 : filename.lastIndexOf('.');
 890        return pos > 0 ? filename.substring(pos + 1) : null;
 891    }
 892
 893    private void copyImageToPrivateStorage(File file, Uri image, int sampleSize)
 894            throws FileCopyException, ImageCompressionException {
 895        final File parent = file.getParentFile();
 896        if (parent != null && parent.mkdirs()) {
 897            Log.d(Config.LOGTAG, "created parent directory");
 898        }
 899        InputStream is = null;
 900        OutputStream os = null;
 901        try {
 902            if (!file.exists() && !file.createNewFile()) {
 903                throw new FileCopyException(R.string.error_unable_to_create_temporary_file);
 904            }
 905            is = mXmppConnectionService.getContentResolver().openInputStream(image);
 906            if (is == null) {
 907                throw new FileCopyException(R.string.error_not_an_image_file);
 908            }
 909            final Bitmap originalBitmap;
 910            final BitmapFactory.Options options = new BitmapFactory.Options();
 911            final int inSampleSize = (int) Math.pow(2, sampleSize);
 912            Log.d(Config.LOGTAG, "reading bitmap with sample size " + inSampleSize);
 913            options.inSampleSize = inSampleSize;
 914            originalBitmap = BitmapFactory.decodeStream(is, null, options);
 915            is.close();
 916            if (originalBitmap == null) {
 917                throw new ImageCompressionException("Source file was not an image");
 918            }
 919            if (!"image/jpeg".equals(options.outMimeType) && hasAlpha(originalBitmap)) {
 920                originalBitmap.recycle();
 921                throw new ImageCompressionException("Source file had alpha channel");
 922            }
 923            Bitmap scaledBitmap = resize(originalBitmap, Config.IMAGE_SIZE);
 924            final int rotation = getRotation(image);
 925            scaledBitmap = rotate(scaledBitmap, rotation);
 926            boolean targetSizeReached = false;
 927            int quality = Config.IMAGE_QUALITY;
 928            final int imageMaxSize =
 929                    mXmppConnectionService
 930                            .getResources()
 931                            .getInteger(R.integer.auto_accept_filesize);
 932            while (!targetSizeReached) {
 933                os = new FileOutputStream(file);
 934                Log.d(Config.LOGTAG, "compressing image with quality " + quality);
 935                boolean success = scaledBitmap.compress(Config.IMAGE_FORMAT, quality, os);
 936                if (!success) {
 937                    throw new FileCopyException(R.string.error_compressing_image);
 938                }
 939                os.flush();
 940                final long fileSize = file.length();
 941                Log.d(Config.LOGTAG, "achieved file size of " + fileSize);
 942                targetSizeReached = fileSize <= imageMaxSize || quality <= 50;
 943                quality -= 5;
 944            }
 945            scaledBitmap.recycle();
 946        } catch (final FileNotFoundException e) {
 947            cleanup(file);
 948            throw new FileCopyException(R.string.error_file_not_found);
 949        } catch (final IOException e) {
 950            cleanup(file);
 951            throw new FileCopyException(R.string.error_io_exception);
 952        } catch (SecurityException e) {
 953            cleanup(file);
 954            throw new FileCopyException(R.string.error_security_exception_during_image_copy);
 955        } catch (final OutOfMemoryError e) {
 956            ++sampleSize;
 957            if (sampleSize <= 3) {
 958                copyImageToPrivateStorage(file, image, sampleSize);
 959            } else {
 960                throw new FileCopyException(R.string.error_out_of_memory);
 961            }
 962        } finally {
 963            close(os);
 964            close(is);
 965        }
 966    }
 967
 968    private static void cleanup(final File file) {
 969        try {
 970            file.delete();
 971        } catch (Exception e) {
 972
 973        }
 974    }
 975
 976    public void copyImageToPrivateStorage(File file, Uri image)
 977            throws FileCopyException, ImageCompressionException {
 978        Log.d(
 979                Config.LOGTAG,
 980                "copy image ("
 981                        + image.toString()
 982                        + ") to private storage "
 983                        + file.getAbsolutePath());
 984        copyImageToPrivateStorage(file, image, 0);
 985    }
 986
 987    public void copyImageToPrivateStorage(Message message, Uri image)
 988            throws FileCopyException, ImageCompressionException {
 989        final String filename;
 990        switch (Config.IMAGE_FORMAT) {
 991            case JPEG:
 992                filename = String.format("%s.%s", message.getUuid(), "jpg");
 993                break;
 994            case PNG:
 995                filename = String.format("%s.%s", message.getUuid(), "png");
 996                break;
 997            case WEBP:
 998                filename = String.format("%s.%s", message.getUuid(), "webp");
 999                break;
1000            default:
1001                throw new IllegalStateException("Unknown image format");
1002        }
1003        setupRelativeFilePath(message, filename);
1004        final File tmp = getFile(message);
1005        copyImageToPrivateStorage(tmp, image);
1006        final String extension = MimeUtils.extractRelevantExtension(filename);
1007        try {
1008            setupRelativeFilePath(message, new FileInputStream(tmp), extension);
1009        } catch (final FileNotFoundException e) {
1010            throw new FileCopyException(R.string.error_file_not_found);
1011        } catch (final IOException e) {
1012            throw new FileCopyException(R.string.error_io_exception);
1013        } catch (final XmppConnectionService.BlockedMediaException e) {
1014            tmp.delete();
1015            message.setRelativeFilePath(null);
1016            message.setDeleted(true);
1017            return;
1018        }
1019        tmp.renameTo(getFile(message));
1020        updateFileParams(message, null, false);
1021    }
1022
1023    public void setupRelativeFilePath(final Message message, final Uri uri, final String extension) throws FileCopyException, XmppConnectionService.BlockedMediaException {
1024        try {
1025            setupRelativeFilePath(message, openInputStream(uri), extension);
1026        } catch (final FileNotFoundException e) {
1027            throw new FileCopyException(R.string.error_file_not_found);
1028        } catch (final IOException e) {
1029            throw new FileCopyException(R.string.error_io_exception);
1030        }
1031    }
1032
1033    public Cid[] calculateCids(final Uri uri) throws IOException {
1034        return calculateCids(mXmppConnectionService.getContentResolver().openInputStream(uri));
1035    }
1036
1037    public Cid[] calculateCids(final InputStream is) throws IOException {
1038        try {
1039            return CryptoHelper.cid(is, new String[]{"SHA-256", "SHA-1", "SHA-512"});
1040        } catch (final NoSuchAlgorithmException e) {
1041            throw new AssertionError(e);
1042        }
1043    }
1044
1045    public void setupRelativeFilePath(final Message message, final InputStream is, final String extension) throws IOException, XmppConnectionService.BlockedMediaException {
1046        message.setRelativeFilePath(getStorageLocation(message, is, extension).getAbsolutePath());
1047    }
1048
1049    public void setupRelativeFilePath(final Message message, final String filename) {
1050        final String extension = MimeUtils.extractRelevantExtension(filename);
1051        final String mime = MimeUtils.guessMimeTypeFromExtension(extension);
1052        setupRelativeFilePath(message, filename, mime);
1053    }
1054
1055    public File getStorageLocation(final Message message, final InputStream is, final String extension) throws IOException, XmppConnectionService.BlockedMediaException {
1056        final String mime = MimeUtils.guessMimeTypeFromExtension(extension);
1057        Cid[] cids = calculateCids(is);
1058        String base = cids[0].toString();
1059
1060        File file = null;
1061        while (file == null || (file.exists() && !file.canRead())) {
1062            file = getStorageLocation(message, String.format("%s.%s", base, extension), mime);
1063            base += "_";
1064        }
1065        for (int i = 0; i < cids.length; i++) {
1066            mXmppConnectionService.saveCid(cids[i], file);
1067        }
1068        return file;
1069    }
1070
1071    public File getStorageLocation(final Message message, final String filename, final String mime) {
1072        final File parentDirectory;
1073        if (Strings.isNullOrEmpty(mime)) {
1074            parentDirectory =
1075                    Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
1076        } else if (mime.startsWith("image/")) {
1077            parentDirectory =
1078                    Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
1079        } else if (mime.startsWith("video/")) {
1080            parentDirectory =
1081                    Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES);
1082        } else if (MediaAdapter.DOCUMENT_MIMES.contains(mime)) {
1083            parentDirectory =
1084                    Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
1085        } else {
1086            parentDirectory =
1087                    Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
1088        }
1089        final File appDirectory =
1090                new File(parentDirectory, mXmppConnectionService.getString(R.string.app_name));
1091        if (message == null || message.getStatus() == Message.STATUS_DUMMY || (message.getConversation() instanceof Conversation && ((Conversation) message.getConversation()).storeInCache())) {
1092            final var mediaCache = new File(mXmppConnectionService.getCacheDir(), "/media");
1093            return new File(mediaCache, filename);
1094        } else {
1095            return new File(appDirectory, filename);
1096        }
1097    }
1098
1099    public static boolean inConversationsDirectory(final Context context, String path) {
1100        final File fileDirectory = new File(path).getParentFile();
1101        for (final String type : STORAGE_TYPES) {
1102            final File typeDirectory =
1103                    new File(
1104                            Environment.getExternalStoragePublicDirectory(type),
1105                            context.getString(R.string.app_name));
1106            if (typeDirectory.equals(fileDirectory)) {
1107                return true;
1108            }
1109        }
1110        return false;
1111    }
1112
1113    public void setupRelativeFilePath(
1114            final Message message, final String filename, final String mime) {
1115        final File file = getStorageLocation(message, filename, mime);
1116        message.setRelativeFilePath(file.getAbsolutePath());
1117    }
1118
1119    public boolean unusualBounds(final Uri image) {
1120        try {
1121            final BitmapFactory.Options options = new BitmapFactory.Options();
1122            options.inJustDecodeBounds = true;
1123            final InputStream inputStream =
1124                    mXmppConnectionService.getContentResolver().openInputStream(image);
1125            BitmapFactory.decodeStream(inputStream, null, options);
1126            close(inputStream);
1127            float ratio = (float) options.outHeight / options.outWidth;
1128            return ratio > (21.0f / 9.0f) || ratio < (9.0f / 21.0f);
1129        } catch (final Exception e) {
1130            Log.w(Config.LOGTAG, "unable to detect image bounds", e);
1131            return false;
1132        }
1133    }
1134
1135    private int getRotation(final File file) {
1136        try (final InputStream inputStream = new FileInputStream(file)) {
1137            return getRotation(inputStream);
1138        } catch (Exception e) {
1139            return 0;
1140        }
1141    }
1142
1143    private int getRotation(final Uri image) {
1144        try (final InputStream is =
1145                mXmppConnectionService.getContentResolver().openInputStream(image)) {
1146            return is == null ? 0 : getRotation(is);
1147        } catch (final Exception e) {
1148            return 0;
1149        }
1150    }
1151
1152    private static int getRotation(final InputStream inputStream) throws IOException {
1153        final ExifInterface exif = new ExifInterface(inputStream);
1154        final int orientation =
1155                exif.getAttributeInt(
1156                        ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
1157        switch (orientation) {
1158            case ExifInterface.ORIENTATION_ROTATE_180:
1159                return 180;
1160            case ExifInterface.ORIENTATION_ROTATE_90:
1161                return 90;
1162            case ExifInterface.ORIENTATION_ROTATE_270:
1163                return 270;
1164            default:
1165                return 0;
1166        }
1167    }
1168
1169    public BitmapDrawable getFallbackThumbnail(final Message message, int size, boolean cacheOnly) {
1170        final var thumbs = message.getFileParams() != null ? message.getFileParams().getThumbnails() : null;
1171        if (thumbs != null && !thumbs.isEmpty()) {
1172            for (final var thumb : thumbs) {
1173                final var uriS = thumb.getAttribute("uri");
1174                if (uriS == null) continue;
1175                Uri uri = Uri.parse(uriS);
1176                if (uri.getScheme().equals("data")) {
1177                    String[] parts = uri.getSchemeSpecificPart().split(",", 2);
1178
1179                    final LruCache<String, Drawable> cache = mXmppConnectionService.getDrawableCache();
1180                    BitmapDrawable cached = (BitmapDrawable) cache.get(parts[1]);
1181                    if (cached != null || cacheOnly) return cached;
1182
1183                    byte[] data;
1184                    if (Arrays.asList(parts[0].split(";")).contains("base64")) {
1185                        String[] parts2 = parts[0].split(";", 2);
1186                        parts[0] = parts2[0];
1187                        data = Base64.decode(parts[1], 0);
1188                    } else {
1189                        try {
1190                            data = parts[1].getBytes("UTF-8");
1191                        } catch (final IOException e) {
1192                            data = new byte[0];
1193                        }
1194                    }
1195
1196                    if (parts[0].equals("image/blurhash")) {
1197                        int width = message.getFileParams().width;
1198                        if (width < 1 && thumb.getAttribute("width") != null) width = Integer.parseInt(thumb.getAttribute("width"));
1199                        if (width < 1) width = 1920;
1200
1201                        int height = message.getFileParams().height;
1202                        if (height < 1 && thumb.getAttribute("height") != null) height = Integer.parseInt(thumb.getAttribute("height"));
1203                        if (height < 1) height = 1080;
1204                        Rect r = rectForSize(width, height, size);
1205
1206                        Bitmap blurhash = BlurHashDecoder.INSTANCE.decode(parts[1], r.width(), r.height(), 1.0f, false);
1207                        if (blurhash != null) {
1208                            cached = new BitmapDrawable(blurhash);
1209                            if (parts[1] != null && cached != null) cache.put(parts[1], cached);
1210                            return cached;
1211                        }
1212                    } else if (parts[0].equals("image/thumbhash")) {
1213                        ThumbHash.Image image;
1214                        try {
1215                            image = ThumbHash.thumbHashToRGBA(data);
1216                        } catch (final Exception e) {
1217                            continue;
1218                        }
1219                        int[] pixels = new int[image.width * image.height];
1220                        for (int i = 0; i < pixels.length; i++) {
1221                            pixels[i] = Color.argb(image.rgba[(i*4)+3] & 0xff, image.rgba[i*4] & 0xff, image.rgba[(i*4)+1] & 0xff, image.rgba[(i*4)+2] & 0xff);
1222                        }
1223                        cached = new BitmapDrawable(Bitmap.createBitmap(pixels, image.width, image.height, Bitmap.Config.ARGB_8888));
1224                        if (parts[1] != null && cached != null) cache.put(parts[1], cached);
1225                        return cached;
1226                    }
1227                }
1228            }
1229         }
1230
1231        return null;
1232    }
1233
1234    public Drawable getThumbnail(Message message, Resources res, int size, boolean cacheOnly) throws IOException {
1235        final LruCache<String, Drawable> cache = mXmppConnectionService.getDrawableCache();
1236        DownloadableFile file = getFile(message);
1237        Drawable thumbnail = cache.get(file.getAbsolutePath());
1238        if (thumbnail != null) return thumbnail;
1239
1240        if ((thumbnail == null) && (!cacheOnly)) {
1241            synchronized (THUMBNAIL_LOCK) {
1242                final var thumbs = message.getFileParams() != null ? message.getFileParams().getThumbnails() : null;
1243                if (thumbs != null && !thumbs.isEmpty()) {
1244                    for (final var thumb : thumbs) {
1245                        final var uriS = thumb.getAttribute("uri");
1246                        if (uriS == null) continue;
1247                        Uri uri = Uri.parse(uriS);
1248                        if (uri.getScheme().equals("data")) {
1249                            if (android.os.Build.VERSION.SDK_INT < 28) continue;
1250                            String[] parts = uri.getSchemeSpecificPart().split(",", 2);
1251
1252                            byte[] data;
1253                            if (Arrays.asList(parts[0].split(";")).contains("base64")) {
1254                                String[] parts2 = parts[0].split(";", 2);
1255                                parts[0] = parts2[0];
1256                                data = Base64.decode(parts[1], 0);
1257                            } else {
1258                                data = parts[1].getBytes("UTF-8");
1259                            }
1260
1261                            if (parts[0].equals("image/blurhash")) continue; // blurhash only for fallback
1262                            if (parts[0].equals("image/thumbhash")) continue; // thumbhash only for fallback
1263
1264                            ImageDecoder.Source source = ImageDecoder.createSource(ByteBuffer.wrap(data));
1265                            thumbnail = ImageDecoder.decodeDrawable(source, (decoder, info, src) -> {
1266                                int w = info.getSize().getWidth();
1267                                int h = info.getSize().getHeight();
1268                                Rect r = rectForSize(w, h, size);
1269                                decoder.setTargetSize(r.width(), r.height());
1270                            });
1271
1272                            if (thumbnail != null && file.getAbsolutePath() != null) {
1273                                cache.put(file.getAbsolutePath(), thumbnail);
1274                                return thumbnail;
1275                            }
1276                        } else if (uri.getScheme().equals("cid")) {
1277                            Cid cid = BobTransfer.cid(uri);
1278                            if (cid == null) continue;
1279                            DownloadableFile f = mXmppConnectionService.getFileForCid(cid);
1280                            if (f != null && f.canRead()) {
1281                                return getThumbnail(f, res, size, cacheOnly);
1282                            }
1283                        }
1284                    }
1285                }
1286            }
1287        }
1288
1289        return getThumbnail(file, res, size, cacheOnly);
1290    }
1291
1292    public Drawable getThumbnail(DownloadableFile file, Resources res, int size, boolean cacheOnly) throws IOException {
1293        return getThumbnail(file, res, size, cacheOnly, file.getAbsolutePath());
1294    }
1295
1296    public Drawable getThumbnail(DownloadableFile file, Resources res, int size, boolean cacheOnly, String cacheKey) throws IOException {
1297        final LruCache<String, Drawable> cache = mXmppConnectionService.getDrawableCache();
1298        Drawable thumbnail = cache.get(cacheKey);
1299        if ((thumbnail == null) && (!cacheOnly) && file.exists()) {
1300            synchronized (THUMBNAIL_LOCK) {
1301                thumbnail = cache.get(cacheKey);
1302                if (thumbnail != null) {
1303                    return thumbnail;
1304                }
1305                final String mime = file.getMimeType();
1306                if ("image/svg+xml".equals(mime)) {
1307                    thumbnail = getSVG(file, size);
1308                } else if ("application/pdf".equals(mime)) {
1309                    thumbnail = new BitmapDrawable(res, getPdfDocumentPreview(file, size));
1310                } else if (mime.startsWith("video/")) {
1311                    thumbnail = new BitmapDrawable(res, getVideoPreview(file, size));
1312                } else if (mime.startsWith("audio/")) {
1313                    thumbnail = res.getDrawable(R.drawable.audio_file_24dp);
1314                } else {
1315                    thumbnail = getImagePreview(file, res, size, mime);
1316                    if (thumbnail == null) {
1317                        throw new FileNotFoundException();
1318                    }
1319                }
1320                if (cacheKey != null && thumbnail != null) cache.put(cacheKey, thumbnail);
1321            }
1322        }
1323        return thumbnail;
1324    }
1325
1326    public Bitmap getThumbnailBitmap(Message message, Resources res, int size) throws IOException {
1327          final Drawable drawable = getThumbnail(message, res, size, false);
1328          if (drawable == null) return null;
1329          return drawDrawable(drawable);
1330    }
1331
1332    public Bitmap getThumbnailBitmap(DownloadableFile file, Resources res, int size, String cacheKey) throws IOException {
1333          final Drawable drawable = getThumbnail(file, res, size, false, cacheKey);
1334          if (drawable == null) return null;
1335          return drawDrawable(drawable);
1336    }
1337
1338    public static Rect rectForSize(int w, int h, int size) {
1339        int scalledW;
1340        int scalledH;
1341        if (w <= h) {
1342            scalledW = Math.max((int) (w / ((double) h / size)), 1);
1343            scalledH = size;
1344        } else {
1345            scalledW = size;
1346            scalledH = Math.max((int) (h / ((double) w / size)), 1);
1347        }
1348
1349        if (scalledW > w || scalledH > h) return new Rect(0, 0, w, h);
1350
1351        return new Rect(0, 0, scalledW, scalledH);
1352    }
1353
1354    private Drawable getImagePreview(File file, Resources res, int size, final String mime) throws IOException {
1355        if (android.os.Build.VERSION.SDK_INT >= 28) {
1356            ImageDecoder.Source source = ImageDecoder.createSource(file);
1357            return ImageDecoder.decodeDrawable(source, (decoder, info, src) -> {
1358                int w = info.getSize().getWidth();
1359                int h = info.getSize().getHeight();
1360                Rect r = rectForSize(w, h, size);
1361                decoder.setTargetSize(r.width(), r.height());
1362            });
1363        } else {
1364            BitmapFactory.Options options = new BitmapFactory.Options();
1365            options.inSampleSize = calcSampleSize(file, size);
1366            Bitmap bitmap = null;
1367            try {
1368                bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), options);
1369            } catch (OutOfMemoryError e) {
1370                options.inSampleSize *= 2;
1371                bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), options);
1372            }
1373            if (bitmap == null) return null;
1374
1375            bitmap = resize(bitmap, size);
1376            bitmap = rotate(bitmap, getRotation(file));
1377            if (mime.equals("image/gif")) {
1378                Bitmap withGifOverlay = bitmap.copy(Bitmap.Config.ARGB_8888, true);
1379                drawOverlay(withGifOverlay, paintOverlayBlack(withGifOverlay) ? R.drawable.play_gif_black : R.drawable.play_gif_white, 1.0f);
1380                bitmap.recycle();
1381                bitmap = withGifOverlay;
1382            }
1383            return new BitmapDrawable(res, bitmap);
1384        }
1385    }
1386
1387    public static Bitmap drawDrawable(Drawable drawable) {
1388        if (drawable == null) return null;
1389
1390        Bitmap bitmap = null;
1391
1392        if (drawable instanceof BitmapDrawable) {
1393            bitmap = ((BitmapDrawable) drawable).getBitmap();
1394            if (bitmap != null) return bitmap;
1395        }
1396
1397        Rect bounds = drawable.getBounds();
1398        int width = drawable.getIntrinsicWidth();
1399        if (width < 1) width = bounds == null || bounds.right < 1 ? 256 : bounds.right;
1400        int height = drawable.getIntrinsicHeight();
1401        if (height < 1) height = bounds == null || bounds.bottom < 1 ? 256 : bounds.bottom;
1402
1403        if (width < 1) {
1404            Log.w(Config.LOGTAG, "Drawable with no width: " + drawable);
1405            width = 48;
1406        }
1407        if (height < 1) {
1408            Log.w(Config.LOGTAG, "Drawable with no height: " + drawable);
1409            height = 48;
1410        }
1411
1412        bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
1413        Canvas canvas = new Canvas(bitmap);
1414        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
1415        drawable.draw(canvas);
1416        return bitmap;
1417    }
1418
1419    private void drawOverlay(Bitmap bitmap, int resource, float factor) {
1420        Bitmap overlay =
1421                BitmapFactory.decodeResource(mXmppConnectionService.getResources(), resource);
1422        Canvas canvas = new Canvas(bitmap);
1423        float targetSize = Math.min(canvas.getWidth(), canvas.getHeight()) * factor;
1424        Log.d(
1425                Config.LOGTAG,
1426                "target size overlay: "
1427                        + targetSize
1428                        + " overlay bitmap size was "
1429                        + overlay.getHeight());
1430        float left = (canvas.getWidth() - targetSize) / 2.0f;
1431        float top = (canvas.getHeight() - targetSize) / 2.0f;
1432        RectF dst = new RectF(left, top, left + targetSize - 1, top + targetSize - 1);
1433        canvas.drawBitmap(overlay, null, dst, createAntiAliasingPaint());
1434    }
1435
1436    /** https://stackoverflow.com/a/3943023/210897 */
1437    private boolean paintOverlayBlack(final Bitmap bitmap) {
1438        final int h = bitmap.getHeight();
1439        final int w = bitmap.getWidth();
1440        int record = 0;
1441        for (int y = Math.round(h * IGNORE_PADDING); y < h - Math.round(h * IGNORE_PADDING); ++y) {
1442            for (int x = Math.round(w * IGNORE_PADDING);
1443                    x < w - Math.round(w * IGNORE_PADDING);
1444                    ++x) {
1445                int pixel = bitmap.getPixel(x, y);
1446                if ((Color.red(pixel) * 0.299
1447                                + Color.green(pixel) * 0.587
1448                                + Color.blue(pixel) * 0.114)
1449                        > 186) {
1450                    --record;
1451                } else {
1452                    ++record;
1453                }
1454            }
1455        }
1456        return record < 0;
1457    }
1458
1459    private boolean paintOverlayBlackPdf(final Bitmap bitmap) {
1460        final int h = bitmap.getHeight();
1461        final int w = bitmap.getWidth();
1462        int white = 0;
1463        for (int y = 0; y < h; ++y) {
1464            for (int x = 0; x < w; ++x) {
1465                int pixel = bitmap.getPixel(x, y);
1466                if ((Color.red(pixel) * 0.299
1467                                + Color.green(pixel) * 0.587
1468                                + Color.blue(pixel) * 0.114)
1469                        > 186) {
1470                    white++;
1471                }
1472            }
1473        }
1474        return white > (h * w * 0.4f);
1475    }
1476
1477    private Bitmap cropCenterSquareVideo(Uri uri, int size) {
1478        MediaMetadataRetriever metadataRetriever = new MediaMetadataRetriever();
1479        Bitmap frame;
1480        try {
1481            metadataRetriever.setDataSource(mXmppConnectionService, uri);
1482            frame = metadataRetriever.getFrameAtTime(0);
1483            metadataRetriever.release();
1484            return cropCenterSquare(frame, size);
1485        } catch (Exception e) {
1486            frame = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
1487            frame.eraseColor(0xff000000);
1488            return frame;
1489        }
1490    }
1491
1492    private Bitmap getVideoPreview(final File file, final int size) {
1493        final MediaMetadataRetriever metadataRetriever = new MediaMetadataRetriever();
1494        Bitmap frame;
1495        try {
1496            metadataRetriever.setDataSource(file.getAbsolutePath());
1497            frame = metadataRetriever.getFrameAtTime(0);
1498            metadataRetriever.release();
1499            frame = resize(frame, size);
1500        } catch (IOException | RuntimeException e) {
1501            frame = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
1502            frame.eraseColor(0xff000000);
1503        }
1504        drawOverlay(
1505                frame,
1506                paintOverlayBlack(frame)
1507                        ? R.drawable.play_video_black
1508                        : R.drawable.play_video_white,
1509                0.75f);
1510        return frame;
1511    }
1512
1513    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
1514    private Bitmap getPdfDocumentPreview(final File file, final int size) {
1515        try {
1516            final ParcelFileDescriptor fileDescriptor =
1517                    ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
1518            final Bitmap rendered = renderPdfDocument(fileDescriptor, size, true);
1519            drawOverlay(
1520                    rendered,
1521                    paintOverlayBlackPdf(rendered)
1522                            ? R.drawable.open_pdf_black
1523                            : R.drawable.open_pdf_white,
1524                    0.75f);
1525            return rendered;
1526        } catch (final IOException | SecurityException e) {
1527            Log.d(Config.LOGTAG, "unable to render PDF document preview", e);
1528            final Bitmap placeholder = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
1529            placeholder.eraseColor(0xff000000);
1530            return placeholder;
1531        }
1532    }
1533
1534    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
1535    private Bitmap cropCenterSquarePdf(final Uri uri, final int size) {
1536        try {
1537            ParcelFileDescriptor fileDescriptor =
1538                    mXmppConnectionService.getContentResolver().openFileDescriptor(uri, "r");
1539            final Bitmap bitmap = renderPdfDocument(fileDescriptor, size, false);
1540            return cropCenterSquare(bitmap, size);
1541        } catch (Exception e) {
1542            final Bitmap placeholder = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
1543            placeholder.eraseColor(0xff000000);
1544            return placeholder;
1545        }
1546    }
1547
1548    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
1549    private Bitmap renderPdfDocument(
1550            ParcelFileDescriptor fileDescriptor, int targetSize, boolean fit) throws IOException {
1551        final PdfRenderer pdfRenderer = new PdfRenderer(fileDescriptor);
1552        final PdfRenderer.Page page = pdfRenderer.openPage(0);
1553        final Dimensions dimensions =
1554                scalePdfDimensions(
1555                        new Dimensions(page.getHeight(), page.getWidth()), targetSize, fit);
1556        final Bitmap rendered =
1557                Bitmap.createBitmap(dimensions.width, dimensions.height, Bitmap.Config.ARGB_8888);
1558        rendered.eraseColor(0xffffffff);
1559        page.render(rendered, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
1560        page.close();
1561        pdfRenderer.close();
1562        fileDescriptor.close();
1563        return rendered;
1564    }
1565
1566    public Uri getTakePhotoUri() {
1567        final String filename =
1568                String.format("IMG_%s.%s", IMAGE_DATE_FORMAT.format(new Date()), "jpg");
1569        final File directory;
1570        if (Config.ONLY_INTERNAL_STORAGE) {
1571            directory = new File(mXmppConnectionService.getCacheDir(), "Camera");
1572        } else {
1573            directory =
1574                    new File(
1575                            Environment.getExternalStoragePublicDirectory(
1576                                    Environment.DIRECTORY_DCIM),
1577                            "Camera");
1578        }
1579        final File file = new File(directory, filename);
1580        file.getParentFile().mkdirs();
1581        return getUriForFile(mXmppConnectionService, file);
1582    }
1583
1584    public Avatar getPepAvatar(Uri image, int size, Bitmap.CompressFormat format) {
1585
1586        final Pair<Avatar,Boolean> uncompressAvatar = getUncompressedAvatar(image);
1587        if (uncompressAvatar != null && uncompressAvatar.first != null &&
1588                (uncompressAvatar.first.image.length() <= Config.AVATAR_CHAR_LIMIT || uncompressAvatar.second)) {
1589            return uncompressAvatar.first;
1590        }
1591        if (uncompressAvatar != null && uncompressAvatar.first != null) {
1592            Log.d(
1593                    Config.LOGTAG,
1594                    "uncompressed avatar exceeded char limit by "
1595                            + (uncompressAvatar.first.image.length() - Config.AVATAR_CHAR_LIMIT));
1596        }
1597
1598        Bitmap bm = cropCenterSquare(image, size);
1599        if (bm == null) {
1600            return null;
1601        }
1602        if (hasAlpha(bm)) {
1603            Log.d(Config.LOGTAG, "alpha in avatar detected; uploading as PNG");
1604            bm.recycle();
1605            bm = cropCenterSquare(image, 96);
1606            return getPepAvatar(bm, Bitmap.CompressFormat.PNG, 100);
1607        }
1608        return getPepAvatar(bm, format, 100);
1609    }
1610
1611    private Pair<Avatar,Boolean> getUncompressedAvatar(Uri uri) {
1612        try {
1613            if (android.os.Build.VERSION.SDK_INT >= 28) {
1614                ImageDecoder.Source source = ImageDecoder.createSource(mXmppConnectionService.getContentResolver(), uri);
1615                int[] size = new int[] { 0, 0 };
1616                boolean[] animated = new boolean[] { false };
1617                String[] mimeType = new String[] { null };
1618                Drawable drawable = ImageDecoder.decodeDrawable(source, (decoder, info, src) -> {
1619                    mimeType[0] = info.getMimeType();
1620                    animated[0] = info.isAnimated();
1621                    size[0] = info.getSize().getWidth();
1622                    size[1] = info.getSize().getHeight();
1623                });
1624
1625                if (animated[0]) {
1626                    Avatar avatar = getPepAvatar(uri, size[0], size[1], mimeType[0]);
1627                    if (avatar != null) return new Pair(avatar, true);
1628                }
1629
1630                return new Pair(getPepAvatar(drawDrawable(drawable), Bitmap.CompressFormat.PNG, 100), false);
1631            } else {
1632                Bitmap bitmap =
1633                    BitmapFactory.decodeStream(
1634                            mXmppConnectionService.getContentResolver().openInputStream(uri));
1635                return new Pair(getPepAvatar(bitmap, Bitmap.CompressFormat.PNG, 100), false);
1636            }
1637        } catch (Exception e) {
1638            try {
1639                final SVG svg = SVG.getFromInputStream(mXmppConnectionService.getContentResolver().openInputStream(uri));
1640                return new Pair(getPepAvatar(uri, (int) svg.getDocumentWidth(), (int) svg.getDocumentHeight(), "image/svg+xml"), true);
1641            } catch (Exception e2) {
1642                return null;
1643            }
1644        }
1645    }
1646
1647    private Avatar getPepAvatar(Uri uri, int width, int height, final String mimeType) throws IOException, NoSuchAlgorithmException {
1648        AssetFileDescriptor fd = mXmppConnectionService.getContentResolver().openAssetFileDescriptor(uri, "r");
1649        if (fd.getLength() > 100000) return null; // Too big to use raw file
1650
1651        ByteArrayOutputStream mByteArrayOutputStream = new ByteArrayOutputStream();
1652        Base64OutputStream mBase64OutputStream =
1653                new Base64OutputStream(mByteArrayOutputStream, Base64.DEFAULT);
1654        MessageDigest digest = MessageDigest.getInstance("SHA-1");
1655        DigestOutputStream mDigestOutputStream =
1656                new DigestOutputStream(mBase64OutputStream, digest);
1657
1658        ByteStreams.copy(fd.createInputStream(), mDigestOutputStream);
1659        mDigestOutputStream.flush();
1660        mDigestOutputStream.close();
1661
1662        final Avatar avatar = new Avatar();
1663        avatar.sha1sum = CryptoHelper.bytesToHex(digest.digest());
1664        avatar.image = new String(mByteArrayOutputStream.toByteArray());
1665        avatar.type = mimeType;
1666        avatar.width = width;
1667        avatar.height = height;
1668        return avatar;
1669    }
1670
1671    private Avatar getPepAvatar(Bitmap bitmap, Bitmap.CompressFormat format, int quality) {
1672        try {
1673            ByteArrayOutputStream mByteArrayOutputStream = new ByteArrayOutputStream();
1674            Base64OutputStream mBase64OutputStream =
1675                    new Base64OutputStream(mByteArrayOutputStream, Base64.DEFAULT);
1676            MessageDigest digest = MessageDigest.getInstance("SHA-1");
1677            DigestOutputStream mDigestOutputStream =
1678                    new DigestOutputStream(mBase64OutputStream, digest);
1679            if (!bitmap.compress(format, quality, mDigestOutputStream)) {
1680                return null;
1681            }
1682            mDigestOutputStream.flush();
1683            mDigestOutputStream.close();
1684            long chars = mByteArrayOutputStream.size();
1685            if (format != Bitmap.CompressFormat.PNG
1686                    && quality >= 50
1687                    && chars >= Config.AVATAR_CHAR_LIMIT) {
1688                int q = quality - 2;
1689                Log.d(
1690                        Config.LOGTAG,
1691                        "avatar char length was " + chars + " reducing quality to " + q);
1692                return getPepAvatar(bitmap, format, q);
1693            }
1694            Log.d(Config.LOGTAG, "settled on char length " + chars + " with quality=" + quality);
1695            final Avatar avatar = new Avatar();
1696            avatar.sha1sum = CryptoHelper.bytesToHex(digest.digest());
1697            avatar.image = new String(mByteArrayOutputStream.toByteArray());
1698            if (format.equals(Bitmap.CompressFormat.WEBP)) {
1699                avatar.type = "image/webp";
1700            } else if (format.equals(Bitmap.CompressFormat.JPEG)) {
1701                avatar.type = "image/jpeg";
1702            } else if (format.equals(Bitmap.CompressFormat.PNG)) {
1703                avatar.type = "image/png";
1704            }
1705            avatar.width = bitmap.getWidth();
1706            avatar.height = bitmap.getHeight();
1707            return avatar;
1708        } catch (OutOfMemoryError e) {
1709            Log.d(Config.LOGTAG, "unable to convert avatar to base64 due to low memory");
1710            return null;
1711        } catch (Exception e) {
1712            return null;
1713        }
1714    }
1715
1716    public Avatar getStoredPepAvatar(String hash) {
1717        if (hash == null) {
1718            return null;
1719        }
1720        Avatar avatar = new Avatar();
1721        final File file = getAvatarFile(hash);
1722        FileInputStream is = null;
1723        try {
1724            avatar.size = file.length();
1725            BitmapFactory.Options options = new BitmapFactory.Options();
1726            options.inJustDecodeBounds = true;
1727            BitmapFactory.decodeFile(file.getAbsolutePath(), options);
1728            is = new FileInputStream(file);
1729            ByteArrayOutputStream mByteArrayOutputStream = new ByteArrayOutputStream();
1730            Base64OutputStream mBase64OutputStream =
1731                    new Base64OutputStream(mByteArrayOutputStream, Base64.DEFAULT);
1732            MessageDigest digest = MessageDigest.getInstance("SHA-1");
1733            DigestOutputStream os = new DigestOutputStream(mBase64OutputStream, digest);
1734            byte[] buffer = new byte[4096];
1735            int length;
1736            while ((length = is.read(buffer)) > 0) {
1737                os.write(buffer, 0, length);
1738            }
1739            os.flush();
1740            os.close();
1741            avatar.sha1sum = CryptoHelper.bytesToHex(digest.digest());
1742            avatar.image = new String(mByteArrayOutputStream.toByteArray());
1743            avatar.height = options.outHeight;
1744            avatar.width = options.outWidth;
1745            avatar.type = options.outMimeType;
1746            return avatar;
1747        } catch (NoSuchAlgorithmException | IOException e) {
1748            return null;
1749        } finally {
1750            close(is);
1751        }
1752    }
1753
1754    public boolean isAvatarCached(Avatar avatar) {
1755        final File file = getAvatarFile(avatar.getFilename());
1756        return file.exists();
1757    }
1758
1759    public boolean save(final Avatar avatar) {
1760        File file;
1761        if (isAvatarCached(avatar)) {
1762            file = getAvatarFile(avatar.getFilename());
1763            avatar.size = file.length();
1764        } else {
1765            file =
1766                    new File(
1767                            mXmppConnectionService.getCacheDir().getAbsolutePath()
1768                                    + "/"
1769                                    + UUID.randomUUID().toString());
1770            if (file.getParentFile().mkdirs()) {
1771                Log.d(Config.LOGTAG, "created cache directory");
1772            }
1773            OutputStream os = null;
1774            try {
1775                if (!file.createNewFile()) {
1776                    Log.d(
1777                            Config.LOGTAG,
1778                            "unable to create temporary file " + file.getAbsolutePath());
1779                }
1780                os = new FileOutputStream(file);
1781                MessageDigest digest = MessageDigest.getInstance("SHA-1");
1782                digest.reset();
1783                DigestOutputStream mDigestOutputStream = new DigestOutputStream(os, digest);
1784                final byte[] bytes = avatar.getImageAsBytes();
1785                mDigestOutputStream.write(bytes);
1786                mDigestOutputStream.flush();
1787                mDigestOutputStream.close();
1788                String sha1sum = CryptoHelper.bytesToHex(digest.digest());
1789                if (sha1sum.equals(avatar.sha1sum)) {
1790                    final File outputFile = getAvatarFile(avatar.getFilename());
1791                    if (outputFile.getParentFile().mkdirs()) {
1792                        Log.d(Config.LOGTAG, "created avatar directory");
1793                    }
1794                    final File avatarFile = getAvatarFile(avatar.getFilename());
1795                    if (!file.renameTo(avatarFile)) {
1796                        Log.d(
1797                                Config.LOGTAG,
1798                                "unable to rename " + file.getAbsolutePath() + " to " + outputFile);
1799                        return false;
1800                    }
1801                } else {
1802                    Log.d(Config.LOGTAG, "sha1sum mismatch for " + avatar.owner);
1803                    if (!file.delete()) {
1804                        Log.d(Config.LOGTAG, "unable to delete temporary file");
1805                    }
1806                    return false;
1807                }
1808                avatar.size = bytes.length;
1809            } catch (IllegalArgumentException | IOException | NoSuchAlgorithmException e) {
1810                return false;
1811            } finally {
1812                close(os);
1813            }
1814        }
1815        return true;
1816    }
1817
1818    public void deleteHistoricAvatarPath() {
1819        delete(getHistoricAvatarPath());
1820    }
1821
1822    private void delete(final File file) {
1823        if (file.isDirectory()) {
1824            final File[] files = file.listFiles();
1825            if (files != null) {
1826                for (final File f : files) {
1827                    delete(f);
1828                }
1829            }
1830        }
1831        if (file.delete()) {
1832            Log.d(Config.LOGTAG, "deleted " + file.getAbsolutePath());
1833        }
1834    }
1835
1836    private File getHistoricAvatarPath() {
1837        return new File(mXmppConnectionService.getFilesDir(), "/avatars/");
1838    }
1839
1840    public File getAvatarFile(String avatar) {
1841        final var f = new File(mXmppConnectionService.getCacheDir(), "/avatars/" + avatar);
1842        if (Build.VERSION.SDK_INT < 26) return f; // Doesn't support file.toPath
1843        try {
1844            if (f.exists()) java.nio.file.Files.setAttribute(f.toPath(), "lastAccessTime", java.nio.file.attribute.FileTime.fromMillis(System.currentTimeMillis()));
1845        } catch (final IOException e) {
1846            Log.w(Config.LOGTAG, "unable to set lastAccessTime for " + f);
1847        }
1848        return f;
1849    }
1850
1851    public Uri getAvatarUri(String avatar) {
1852        return Uri.fromFile(getAvatarFile(avatar));
1853    }
1854
1855    public Drawable cropCenterSquareDrawable(Uri image, int size) {
1856        if (android.os.Build.VERSION.SDK_INT >= 28) {
1857            try {
1858                ImageDecoder.Source source = ImageDecoder.createSource(mXmppConnectionService.getContentResolver(), image);
1859                return ImageDecoder.decodeDrawable(source, (decoder, info, src) -> {
1860                    int w = info.getSize().getWidth();
1861                    int h = info.getSize().getHeight();
1862                    Rect r = rectForSize(w, h, size);
1863                    decoder.setTargetSize(r.width(), r.height());
1864
1865                    int newSize = Math.min(r.width(), r.height());
1866                    int left = (r.width() - newSize) / 2;
1867                    int top = (r.height() - newSize) / 2;
1868                    decoder.setCrop(new Rect(left, top, left + newSize, top + newSize));
1869                });
1870            } catch (final IOException e) {
1871                return getSVGSquare(image, size);
1872            }
1873        } else {
1874            Bitmap bitmap = cropCenterSquare(image, size);
1875            return bitmap == null ? null : new BitmapDrawable(bitmap);
1876        }
1877    }
1878
1879    public Bitmap cropCenterSquare(Uri image, int size) {
1880        if (image == null) {
1881            return null;
1882        }
1883        InputStream is = null;
1884        try {
1885            BitmapFactory.Options options = new BitmapFactory.Options();
1886            options.inSampleSize = calcSampleSize(image, size);
1887            is = mXmppConnectionService.getContentResolver().openInputStream(image);
1888            if (is == null) {
1889                return null;
1890            }
1891            Bitmap input = BitmapFactory.decodeStream(is, null, options);
1892            if (input == null) {
1893                return null;
1894            } else {
1895                input = rotate(input, getRotation(image));
1896                return cropCenterSquare(input, size);
1897            }
1898        } catch (FileNotFoundException | SecurityException e) {
1899            Log.d(Config.LOGTAG, "unable to open file " + image.toString(), e);
1900            return null;
1901        } finally {
1902            close(is);
1903        }
1904    }
1905
1906    public Bitmap cropCenter(Uri image, int newHeight, int newWidth) {
1907        if (image == null) {
1908            return null;
1909        }
1910        InputStream is = null;
1911        try {
1912            BitmapFactory.Options options = new BitmapFactory.Options();
1913            options.inSampleSize = calcSampleSize(image, Math.max(newHeight, newWidth));
1914            is = mXmppConnectionService.getContentResolver().openInputStream(image);
1915            if (is == null) {
1916                return null;
1917            }
1918            Bitmap source = BitmapFactory.decodeStream(is, null, options);
1919            if (source == null) {
1920                return null;
1921            }
1922            int sourceWidth = source.getWidth();
1923            int sourceHeight = source.getHeight();
1924            float xScale = (float) newWidth / sourceWidth;
1925            float yScale = (float) newHeight / sourceHeight;
1926            float scale = Math.max(xScale, yScale);
1927            float scaledWidth = scale * sourceWidth;
1928            float scaledHeight = scale * sourceHeight;
1929            float left = (newWidth - scaledWidth) / 2;
1930            float top = (newHeight - scaledHeight) / 2;
1931
1932            RectF targetRect = new RectF(left, top, left + scaledWidth, top + scaledHeight);
1933            Bitmap dest = Bitmap.createBitmap(newWidth, newHeight, Bitmap.Config.ARGB_8888);
1934            Canvas canvas = new Canvas(dest);
1935            canvas.drawBitmap(source, null, targetRect, createAntiAliasingPaint());
1936            if (source.isRecycled()) {
1937                source.recycle();
1938            }
1939            return dest;
1940        } catch (SecurityException e) {
1941            return null; // android 6.0 with revoked permissions for example
1942        } catch (FileNotFoundException e) {
1943            return null;
1944        } finally {
1945            close(is);
1946        }
1947    }
1948
1949    public Bitmap cropCenterSquare(Bitmap input, int size) {
1950        int w = input.getWidth();
1951        int h = input.getHeight();
1952
1953        float scale = Math.max((float) size / h, (float) size / w);
1954
1955        float outWidth = scale * w;
1956        float outHeight = scale * h;
1957        float left = (size - outWidth) / 2;
1958        float top = (size - outHeight) / 2;
1959        RectF target = new RectF(left, top, left + outWidth, top + outHeight);
1960
1961        Bitmap output = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
1962        Canvas canvas = new Canvas(output);
1963        canvas.drawBitmap(input, null, target, createAntiAliasingPaint());
1964        if (!input.isRecycled()) {
1965            input.recycle();
1966        }
1967        return output;
1968    }
1969
1970    private int calcSampleSize(Uri image, int size)
1971            throws FileNotFoundException, SecurityException {
1972        final BitmapFactory.Options options = new BitmapFactory.Options();
1973        options.inJustDecodeBounds = true;
1974        final InputStream inputStream =
1975                mXmppConnectionService.getContentResolver().openInputStream(image);
1976        BitmapFactory.decodeStream(inputStream, null, options);
1977        close(inputStream);
1978        return calcSampleSize(options, size);
1979    }
1980
1981    public void updateFileParams(Message message) {
1982        updateFileParams(message, null);
1983    }
1984
1985    public void updateFileParams(final Message message, final String url) {
1986        updateFileParams(message, url, true);
1987    }
1988
1989    public void updateFileParams(final Message message, String url, boolean updateCids) {
1990        final boolean encrypted =
1991                message.getEncryption() == Message.ENCRYPTION_PGP
1992                        || message.getEncryption() == Message.ENCRYPTION_DECRYPTED;
1993        final DownloadableFile file = getFile(message);
1994        final String mime = file.getMimeType();
1995        final boolean privateMessage = message.isPrivateMessage();
1996        final boolean image =
1997                message.getType() == Message.TYPE_IMAGE
1998                        || (mime != null && mime.startsWith("image/"));
1999        Message.FileParams fileParams = message.getFileParams();
2000        if (fileParams == null) fileParams = new Message.FileParams();
2001        Cid[] cids = new Cid[0];
2002        try {
2003            cids = calculateCids(new FileInputStream(file));
2004            fileParams.setCids(List.of(cids));
2005        } catch (final IOException | NoSuchAlgorithmException e) { }
2006        if (url == null) {
2007            for (Cid cid : cids) {
2008                url = mXmppConnectionService.getUrlForCid(cid);
2009                if (url != null) {
2010                    fileParams.url = url;
2011                    break;
2012                }
2013            }
2014        } else {
2015            fileParams.url = url;
2016        }
2017        if (fileParams.getName() == null) fileParams.setName(file.getName());
2018        fileParams.setMediaType(mime);
2019        if (encrypted && !file.exists()) {
2020            Log.d(Config.LOGTAG, "skipping updateFileParams because file is encrypted");
2021            final DownloadableFile encryptedFile = getFile(message, false);
2022            if (encryptedFile.canRead()) fileParams.size = encryptedFile.getSize();
2023        } else {
2024            Log.d(Config.LOGTAG, "running updateFileParams");
2025            final boolean ambiguous = MimeUtils.AMBIGUOUS_CONTAINER_FORMATS.contains(mime);
2026            final boolean video = mime != null && mime.startsWith("video/");
2027            final boolean audio = mime != null && mime.startsWith("audio/");
2028            final boolean pdf = "application/pdf".equals(mime);
2029            if (file.canRead()) fileParams.size = file.getSize();
2030            if (ambiguous) {
2031                try {
2032                    final Dimensions dimensions = getVideoDimensions(file);
2033                    if (dimensions.valid()) {
2034                        Log.d(Config.LOGTAG, "ambiguous file " + mime + " is video");
2035                        fileParams.width = dimensions.width;
2036                        fileParams.height = dimensions.height;
2037                    } else {
2038                        Log.d(Config.LOGTAG, "ambiguous file " + mime + " is audio");
2039                        fileParams.runtime = getMediaRuntime(file);
2040                    }
2041                } catch (final IOException | NotAVideoFile e) {
2042                    Log.d(Config.LOGTAG, "ambiguous file " + mime + " is audio");
2043                    fileParams.runtime = getMediaRuntime(file);
2044                }
2045            } else if (image || video || pdf) {
2046                try {
2047                    final Dimensions dimensions;
2048                    if (video) {
2049                        dimensions = getVideoDimensions(file);
2050                    } else if (pdf) {
2051                        dimensions = getPdfDocumentDimensions(file);
2052                    } else if ("image/svg+xml".equals(mime)) {
2053                        SVG svg = SVG.getFromInputStream(new FileInputStream(file));
2054                        dimensions = new Dimensions((int) svg.getDocumentHeight(), (int) svg.getDocumentWidth());
2055                    } else {
2056                        dimensions = getImageDimensions(file);
2057                    }
2058                    if (dimensions.valid()) {
2059                        fileParams.width = dimensions.width;
2060                        fileParams.height = dimensions.height;
2061                    }
2062                } catch (final IOException | SVGParseException | NotAVideoFile notAVideoFile) {
2063                    Log.d(
2064                            Config.LOGTAG,
2065                            "file with mime type " + file.getMimeType() + " was not a video file");
2066                    // fall threw
2067                }
2068            } else if (audio) {
2069                fileParams.runtime = getMediaRuntime(file);
2070            }
2071            if ("application/webxdc+zip".equals(mime)) {
2072                try {
2073                    final var zip = new ZipFile(file);
2074                    final ZipEntry manifestEntry = zip == null ? null : zip.getEntry("manifest.toml");
2075                    if (manifestEntry != null) {
2076                        final var manifest = Toml.parse(zip.getInputStream(manifestEntry));
2077                        if (manifest != null) {
2078                            final var name = manifest.getString("name");
2079                            if (name != null) fileParams.setName(name);
2080                        }
2081                    }
2082                } catch (final IOException e2) { }
2083            }
2084            try {
2085                Bitmap thumb = getThumbnailBitmap(file, mXmppConnectionService.getResources(), 100, file.getAbsolutePath() + " x 100");
2086                if (thumb != null) {
2087                    int[] pixels = new int[thumb.getWidth() * thumb.getHeight()];
2088                    byte[] rgba = new byte[pixels.length * 4];
2089                    try {
2090                        thumb.getPixels(pixels, 0, thumb.getWidth(), 0, 0, thumb.getWidth(), thumb.getHeight());
2091                    } catch (final IllegalStateException e) {
2092                        Bitmap softThumb = thumb.copy(Bitmap.Config.ARGB_8888, false);
2093                        softThumb.getPixels(pixels, 0, thumb.getWidth(), 0, 0, thumb.getWidth(), thumb.getHeight());
2094                        softThumb.recycle();
2095                    }
2096                    for (int i = 0; i < pixels.length; i++) {
2097                        rgba[i*4] = (byte)((pixels[i] >> 16) & 0xff);
2098                        rgba[(i*4)+1] = (byte)((pixels[i] >> 8) & 0xff);
2099                        rgba[(i*4)+2] = (byte)(pixels[i] & 0xff);
2100                        rgba[(i*4)+3] = (byte)((pixels[i] >> 24) & 0xff);
2101                    }
2102                    fileParams.addThumbnail(thumb.getWidth(), thumb.getHeight(), "image/thumbhash", "data:image/thumbhash;base64," + Base64.encodeToString(ThumbHash.rgbaToThumbHash(thumb.getWidth(), thumb.getHeight(), rgba), Base64.NO_WRAP));
2103                }
2104            } catch (final IOException e) { }
2105        }
2106        message.setFileParams(fileParams);
2107        message.setDeleted(false);
2108        message.setType(
2109                privateMessage
2110                        ? Message.TYPE_PRIVATE_FILE
2111                        : (image ? Message.TYPE_IMAGE : Message.TYPE_FILE));
2112
2113        if (updateCids) {
2114            try {
2115                for (int i = 0; i < cids.length; i++) {
2116                    mXmppConnectionService.saveCid(cids[i], file);
2117                }
2118            } catch (XmppConnectionService.BlockedMediaException e) { }
2119        }
2120    }
2121
2122    private int getMediaRuntime(final File file) {
2123        try {
2124            final MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
2125            mediaMetadataRetriever.setDataSource(file.toString());
2126            final String value =
2127                    mediaMetadataRetriever.extractMetadata(
2128                            MediaMetadataRetriever.METADATA_KEY_DURATION);
2129            if (Strings.isNullOrEmpty(value)) {
2130                return 0;
2131            }
2132            return Integer.parseInt(value);
2133        } catch (final Exception e) {
2134            return 0;
2135        }
2136    }
2137
2138    private Dimensions getImageDimensions(File file) {
2139        final BitmapFactory.Options options = new BitmapFactory.Options();
2140        options.inJustDecodeBounds = true;
2141        BitmapFactory.decodeFile(file.getAbsolutePath(), options);
2142        final int rotation = getRotation(file);
2143        final boolean rotated = rotation == 90 || rotation == 270;
2144        final int imageHeight = rotated ? options.outWidth : options.outHeight;
2145        final int imageWidth = rotated ? options.outHeight : options.outWidth;
2146        return new Dimensions(imageHeight, imageWidth);
2147    }
2148
2149    private Dimensions getVideoDimensions(final File file) throws NotAVideoFile, IOException {
2150        MediaMetadataRetriever metadataRetriever = new MediaMetadataRetriever();
2151        try {
2152            metadataRetriever.setDataSource(file.getAbsolutePath());
2153        } catch (RuntimeException e) {
2154            throw new NotAVideoFile(e);
2155        }
2156        return getVideoDimensions(metadataRetriever);
2157    }
2158
2159    private Dimensions getPdfDocumentDimensions(final File file) {
2160        final ParcelFileDescriptor fileDescriptor;
2161        try {
2162            fileDescriptor = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
2163            if (fileDescriptor == null) {
2164                return new Dimensions(0, 0);
2165            }
2166        } catch (final FileNotFoundException e) {
2167            return new Dimensions(0, 0);
2168        }
2169        try {
2170            final PdfRenderer pdfRenderer = new PdfRenderer(fileDescriptor);
2171            final PdfRenderer.Page page = pdfRenderer.openPage(0);
2172            final int height = page.getHeight();
2173            final int width = page.getWidth();
2174            page.close();
2175            pdfRenderer.close();
2176            return scalePdfDimensions(new Dimensions(height, width));
2177        } catch (final IOException | SecurityException e) {
2178            Log.d(Config.LOGTAG, "unable to get dimensions for pdf document", e);
2179            return new Dimensions(0, 0);
2180        }
2181    }
2182
2183    private Dimensions scalePdfDimensions(Dimensions in) {
2184        final DisplayMetrics displayMetrics =
2185                mXmppConnectionService.getResources().getDisplayMetrics();
2186        final int target = (int) (displayMetrics.density * 288);
2187        return scalePdfDimensions(in, target, true);
2188    }
2189
2190    private static Dimensions scalePdfDimensions(
2191            final Dimensions in, final int target, final boolean fit) {
2192        final int w, h;
2193        if (fit == (in.width <= in.height)) {
2194            w = Math.max((int) (in.width / ((double) in.height / target)), 1);
2195            h = target;
2196        } else {
2197            w = target;
2198            h = Math.max((int) (in.height / ((double) in.width / target)), 1);
2199        }
2200        return new Dimensions(h, w);
2201    }
2202
2203    public Drawable getAvatar(String avatar, int size) {
2204        if (avatar == null) {
2205            return null;
2206        }
2207
2208        if (android.os.Build.VERSION.SDK_INT >= 28) {
2209            try {
2210                ImageDecoder.Source source = ImageDecoder.createSource(getAvatarFile(avatar));
2211                return ImageDecoder.decodeDrawable(source, (decoder, info, src) -> {
2212                    int w = info.getSize().getWidth();
2213                    int h = info.getSize().getHeight();
2214                    Rect r = rectForSize(w, h, size);
2215                    decoder.setTargetSize(r.width(), r.height());
2216
2217                    int newSize = Math.min(r.width(), r.height());
2218                    int left = (r.width() - newSize) / 2;
2219                    int top = (r.height() - newSize) / 2;
2220                    decoder.setCrop(new Rect(left, top, left + newSize, top + newSize));
2221                });
2222            } catch (final IOException e) {
2223                return getSVGSquare(getAvatarUri(avatar), size);
2224            }
2225        } else {
2226            Bitmap bm = cropCenter(getAvatarUri(avatar), size, size);
2227            return bm == null ? null : new BitmapDrawable(bm);
2228        }
2229    }
2230
2231    public Drawable getSVGSquare(Uri uri, int size) {
2232        try {
2233            SVG svg = SVG.getFromInputStream(mXmppConnectionService.getContentResolver().openInputStream(uri));
2234            svg.setDocumentPreserveAspectRatio(com.caverock.androidsvg.PreserveAspectRatio.FULLSCREEN);
2235
2236            float w = svg.getDocumentWidth();
2237            float h = svg.getDocumentHeight();
2238            float scale = Math.max((float) size / h, (float) size / w);
2239            float outWidth = scale * w;
2240            float outHeight = scale * h;
2241            float left = (size - outWidth) / 2;
2242            float top = (size - outHeight) / 2;
2243            RectF target = new RectF(left, top, left + outWidth, top + outHeight);
2244            if (svg.getDocumentViewBox() == null) svg.setDocumentViewBox(0, 0, w, h);
2245            svg.setDocumentWidth("100%");
2246            svg.setDocumentHeight("100%");
2247
2248            Bitmap output = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
2249            Canvas canvas = new Canvas(output);
2250            svg.renderToCanvas(canvas, target);
2251
2252            return new SVGDrawable(output);
2253        } catch (final IOException | SVGParseException | IllegalArgumentException e) {
2254            Log.w(Config.LOGTAG, "Could not parse SVG: " + e);
2255            return null;
2256        }
2257    }
2258
2259    public Drawable getSVG(File file, int size) {
2260        try {
2261            SVG svg = SVG.getFromInputStream(new FileInputStream(file));
2262            return drawSVG(svg, size);
2263        } catch (final IOException | SVGParseException | IllegalArgumentException e) {
2264            Log.w(Config.LOGTAG, "Could not parse SVG: " + e);
2265            return null;
2266        }
2267    }
2268
2269    public Drawable drawSVG(SVG svg, int size) {
2270        try {
2271            svg.setDocumentPreserveAspectRatio(com.caverock.androidsvg.PreserveAspectRatio.LETTERBOX);
2272
2273            float w = svg.getDocumentWidth();
2274            float h = svg.getDocumentHeight();
2275            Rect r = rectForSize(w < 1 ? size : (int) w, h < 1 ? size : (int) h, size);
2276            if (svg.getDocumentViewBox() == null) svg.setDocumentViewBox(0, 0, w, h);
2277            svg.setDocumentWidth("100%");
2278            svg.setDocumentHeight("100%");
2279
2280            Bitmap output = Bitmap.createBitmap(r.width(), r.height(), Bitmap.Config.ARGB_8888);
2281            Canvas canvas = new Canvas(output);
2282            svg.renderToCanvas(canvas);
2283
2284            return new SVGDrawable(output);
2285        } catch (final SVGParseException e) {
2286            Log.w(Config.LOGTAG, "Could not parse SVG: " + e);
2287            return null;
2288        }
2289    }
2290
2291    private static class Dimensions {
2292        public final int width;
2293        public final int height;
2294
2295        Dimensions(int height, int width) {
2296            this.width = width;
2297            this.height = height;
2298        }
2299
2300        public int getMin() {
2301            return Math.min(width, height);
2302        }
2303
2304        public boolean valid() {
2305            return width > 0 && height > 0;
2306        }
2307    }
2308
2309    private static class NotAVideoFile extends Exception {
2310        public NotAVideoFile(Throwable t) {
2311            super(t);
2312        }
2313
2314        public NotAVideoFile() {
2315            super();
2316        }
2317    }
2318
2319    public static class ImageCompressionException extends Exception {
2320
2321        ImageCompressionException(String message) {
2322            super(message);
2323        }
2324    }
2325
2326    public static class FileCopyException extends Exception {
2327        private final int resId;
2328
2329        private FileCopyException(@StringRes int resId) {
2330            this.resId = resId;
2331        }
2332
2333        public @StringRes int getResId() {
2334            return resId;
2335        }
2336    }
2337
2338    public static class SVGDrawable extends BitmapDrawable {
2339        public SVGDrawable(Bitmap bm) { super(bm); }
2340    }
2341}