From 3c30db7edf8f8f1fed027a389725a10520b2b298 Mon Sep 17 00:00:00 2001 From: Moxie Marlinspike Date: Sun, 18 Mar 2018 15:06:51 -0700 Subject: [PATCH] No longer generate image thumbnails manually Delete manually calculated image thumbnails --- .../database/AttachmentDatabase.java | 4 +- .../database/helpers/SQLCipherOpenHelper.java | 33 +++++++++++++-- .../securesms/util/MediaUtil.java | 41 ------------------- 3 files changed, 31 insertions(+), 47 deletions(-) diff --git a/src/org/thoughtcrime/securesms/database/AttachmentDatabase.java b/src/org/thoughtcrime/securesms/database/AttachmentDatabase.java index db796a7f4a..05cdf1fc3d 100644 --- a/src/org/thoughtcrime/securesms/database/AttachmentDatabase.java +++ b/src/org/thoughtcrime/securesms/database/AttachmentDatabase.java @@ -642,12 +642,10 @@ public class AttachmentDatabase extends Database { return null; } - ThumbnailData data; + ThumbnailData data = null; if (MediaUtil.isVideoType(attachment.getContentType())) { data = generateVideoThumbnail(attachmentId); - } else{ - data = MediaUtil.generateThumbnail(context, attachment.getContentType(), attachment.getDataUri()); } if (data == null) { diff --git a/src/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java b/src/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java index 4d605ed538..92752a3570 100644 --- a/src/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java +++ b/src/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java @@ -1,7 +1,9 @@ package org.thoughtcrime.securesms.database.helpers; +import android.content.ContentValues; import android.content.Context; +import android.database.Cursor; import android.support.annotation.NonNull; import android.util.Log; @@ -29,14 +31,17 @@ import org.thoughtcrime.securesms.jobs.RefreshPreKeysJob; import org.thoughtcrime.securesms.service.KeyCachingService; import org.thoughtcrime.securesms.util.TextSecurePreferences; +import java.io.File; + public class SQLCipherOpenHelper extends SQLiteOpenHelper { @SuppressWarnings("unused") private static final String TAG = SQLCipherOpenHelper.class.getSimpleName(); - private static final int RECIPIENT_CALL_RINGTONE_VERSION = 2; - private static final int MIGRATE_PREKEYS_VERSION = 3; - private static final int MIGRATE_SESSIONS_VERSION = 4; + private static final int RECIPIENT_CALL_RINGTONE_VERSION = 2; + private static final int MIGRATE_PREKEYS_VERSION = 3; + private static final int MIGRATE_SESSIONS_VERSION = 4; + private static final int NO_MORE_IMAGE_THUMBNAILS_VERSION = 5; private static final int DATABASE_VERSION = 4; private static final String DATABASE_NAME = "signal.db"; @@ -134,6 +139,28 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper { SessionStoreMigrationHelper.migrateSessions(context, db); } + if (oldVersion < NO_MORE_IMAGE_THUMBNAILS_VERSION) { + ContentValues update = new ContentValues(); + update.put("thumbnail", (String)null); + update.put("aspect_ratio", (String)null); + update.put("thumbnail_random", (String)null); + + try (Cursor cursor = db.query("part", new String[] {"_id", "ct", "thumbnail"}, "thumbnail IS NOT NULL", null, null, null, null)) { + while (cursor != null && cursor.moveToNext()) { + long id = cursor.getLong(cursor.getColumnIndexOrThrow("_id")); + String contentType = cursor.getString(cursor.getColumnIndexOrThrow("ct")); + + if (contentType != null && !contentType.startsWith("video")) { + String thumbnailPath = cursor.getString(cursor.getColumnIndexOrThrow("thumbnail")); + File thumbnailFile = new File(thumbnailPath); + thumbnailFile.delete(); + + db.update("part", update, "_id = ?", new String[] {String.valueOf(id)}); + } + } + } + } + db.setTransactionSuccessful(); } finally { db.endTransaction(); diff --git a/src/org/thoughtcrime/securesms/util/MediaUtil.java b/src/org/thoughtcrime/securesms/util/MediaUtil.java index e284e0a4fa..2f023e1c77 100644 --- a/src/org/thoughtcrime/securesms/util/MediaUtil.java +++ b/src/org/thoughtcrime/securesms/util/MediaUtil.java @@ -11,14 +11,10 @@ import android.text.TextUtils; import android.util.Log; import android.webkit.MimeTypeMap; -import org.thoughtcrime.securesms.R; import org.thoughtcrime.securesms.attachments.Attachment; -import org.thoughtcrime.securesms.crypto.MasterSecret; import org.thoughtcrime.securesms.mms.AudioSlide; -import org.thoughtcrime.securesms.mms.DecryptableStreamUriLoader.DecryptableUri; import org.thoughtcrime.securesms.mms.DocumentSlide; import org.thoughtcrime.securesms.mms.GifSlide; -import org.thoughtcrime.securesms.mms.GlideApp; import org.thoughtcrime.securesms.mms.ImageSlide; import org.thoughtcrime.securesms.mms.MmsSlide; import org.thoughtcrime.securesms.mms.PartAuthority; @@ -28,7 +24,6 @@ import org.thoughtcrime.securesms.providers.PersistentBlobProvider; import java.io.IOException; import java.io.InputStream; -import java.util.concurrent.ExecutionException; public class MediaUtil { @@ -42,42 +37,6 @@ public class MediaUtil { public static final String VIDEO_UNSPECIFIED = "video/*"; - public static @Nullable ThumbnailData generateThumbnail(Context context, String contentType, Uri uri) - throws BitmapDecodingException - { - long startMillis = System.currentTimeMillis(); - ThumbnailData data = null; - - if (isImageType(contentType)) { - data = new ThumbnailData(generateImageThumbnail(context, uri)); - } - - if (data != null) { - Log.w(TAG, String.format("generated thumbnail for part, %dx%d (%.3f:1) in %dms", - data.getBitmap().getWidth(), data.getBitmap().getHeight(), - data.getAspectRatio(), System.currentTimeMillis() - startMillis)); - } - - return data; - } - - private static Bitmap generateImageThumbnail(Context context, Uri uri) - throws BitmapDecodingException - { - try { - int maxSize = context.getResources().getDimensionPixelSize(R.dimen.media_bubble_height); - return GlideApp.with(context.getApplicationContext()) - .asBitmap() - .load(new DecryptableUri(uri)) - .centerCrop() - .into(maxSize, maxSize) - .get(); - } catch (InterruptedException | ExecutionException e) { - Log.w(TAG, e); - throw new BitmapDecodingException(e); - } - } - public static Slide getSlideForAttachment(Context context, Attachment attachment) { Slide slide = null; if (isGif(attachment.getContentType())) {