mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-04-20 00:29:11 +01:00
Improve wallpaper load speed.
This commit is contained in:
committed by
Cody Henthorne
parent
63e48efdfe
commit
d504bd593a
@@ -1,9 +1,11 @@
|
||||
package org.thoughtcrime.securesms.wallpaper;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Parcelable;
|
||||
import android.widget.ImageView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.WorkerThread;
|
||||
|
||||
import org.thoughtcrime.securesms.conversation.colors.ChatColors;
|
||||
import org.thoughtcrime.securesms.conversation.colors.ChatColorsMapper;
|
||||
@@ -26,6 +28,11 @@ public interface ChatWallpaper extends Parcelable {
|
||||
|
||||
void loadInto(@NonNull ImageView imageView);
|
||||
|
||||
@WorkerThread
|
||||
default boolean prefetch(@NonNull Context context, long maxWaitTime) {
|
||||
return true;
|
||||
}
|
||||
|
||||
default boolean isPhoto() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
package org.thoughtcrime.securesms.wallpaper;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.net.Uri;
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
import android.util.LruCache;
|
||||
import android.widget.ImageView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
@@ -18,11 +21,26 @@ import org.signal.core.util.logging.Log;
|
||||
import org.thoughtcrime.securesms.database.model.databaseprotos.Wallpaper;
|
||||
import org.thoughtcrime.securesms.mms.DecryptableStreamUriLoader;
|
||||
import org.thoughtcrime.securesms.mms.GlideApp;
|
||||
import org.thoughtcrime.securesms.util.ByteUnit;
|
||||
import org.thoughtcrime.securesms.util.LRUCache;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
final class UriChatWallpaper implements ChatWallpaper, Parcelable {
|
||||
|
||||
private static final LruCache<Uri, Bitmap> CACHE = new LruCache<Uri, Bitmap>((int) Runtime.getRuntime().maxMemory() / 8) {
|
||||
@Override
|
||||
protected int sizeOf(Uri key, Bitmap value) {
|
||||
return value.getByteCount();
|
||||
}
|
||||
};
|
||||
|
||||
private static final String TAG = Log.tag(UriChatWallpaper.class);
|
||||
|
||||
private final Uri uri;
|
||||
@@ -45,22 +63,58 @@ final class UriChatWallpaper implements ChatWallpaper, Parcelable {
|
||||
|
||||
@Override
|
||||
public void loadInto(@NonNull ImageView imageView) {
|
||||
GlideApp.with(imageView)
|
||||
.load(new DecryptableStreamUriLoader.DecryptableUri(uri))
|
||||
.addListener(new RequestListener<Drawable>() {
|
||||
@Override
|
||||
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
|
||||
Log.w(TAG, "Failed to load wallpaper " + uri);
|
||||
return false;
|
||||
}
|
||||
Bitmap cached = CACHE.get(uri);
|
||||
if (cached != null) {
|
||||
Log.d(TAG, "Using cached value.");
|
||||
imageView.setImageBitmap(CACHE.get(uri));
|
||||
} else {
|
||||
Log.d(TAG, "Not in cache. Fetching using Glide.");
|
||||
GlideApp.with(imageView)
|
||||
.load(new DecryptableStreamUriLoader.DecryptableUri(uri))
|
||||
.addListener(new RequestListener<Drawable>() {
|
||||
@Override
|
||||
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
|
||||
Log.w(TAG, "Failed to load wallpaper " + uri);
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
|
||||
Log.i(TAG, "Loaded wallpaper " + uri);
|
||||
return false;
|
||||
}
|
||||
})
|
||||
.into(imageView);
|
||||
@Override
|
||||
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
|
||||
Log.i(TAG, "Loaded wallpaper " + uri);
|
||||
return false;
|
||||
}
|
||||
})
|
||||
.into(imageView);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean prefetch(@NonNull Context context, long maxWaitTime) {
|
||||
Bitmap cached = CACHE.get(uri);
|
||||
if (cached != null) {
|
||||
Log.d(TAG, "Already cached, skipping prefetch.");
|
||||
return true;
|
||||
}
|
||||
|
||||
long startTime = System.currentTimeMillis();
|
||||
try {
|
||||
Bitmap bitmap = GlideApp.with(context)
|
||||
.asBitmap()
|
||||
.load(new DecryptableStreamUriLoader.DecryptableUri(uri))
|
||||
.submit()
|
||||
.get(maxWaitTime, TimeUnit.MILLISECONDS);
|
||||
|
||||
CACHE.put(uri, bitmap);
|
||||
Log.d(TAG, "Prefetched wallpaper in " + (System.currentTimeMillis() - startTime) + " ms.");
|
||||
|
||||
return true;
|
||||
} catch (ExecutionException | InterruptedException e) {
|
||||
Log.w(TAG, "Failed to prefetch wallpaper.", e);
|
||||
} catch (TimeoutException e) {
|
||||
Log.w(TAG, "Timed out waiting for prefetch.");
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user