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