From 32ebb8bb7abd2fc6031a68d9eec70d83e218b79d Mon Sep 17 00:00:00 2001 From: DJAB HipHop Date: Wed, 26 May 2021 20:19:17 +0200 Subject: [PATCH] Backgrounds: Optimize builtin wallpaper loading code Test in 16.0 by adding extra 4k paper wallpaper I took with my iPhone 11 pro max & opening the app Change-Id: Ic3901bda473aaa9872baeb8a89958eb1339113fa Signed-off-by: DJAB HipHop --- .../factory/BuiltInWallpaperFactory.java | 7 +++- .../lineageos/backgrounds/util/UiUtils.java | 40 +++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/org/lineageos/backgrounds/factory/BuiltInWallpaperFactory.java b/app/src/main/java/org/lineageos/backgrounds/factory/BuiltInWallpaperFactory.java index 7508d3c..264fa6b 100644 --- a/app/src/main/java/org/lineageos/backgrounds/factory/BuiltInWallpaperFactory.java +++ b/app/src/main/java/org/lineageos/backgrounds/factory/BuiltInWallpaperFactory.java @@ -18,6 +18,7 @@ import android.app.WallpaperManager; import android.content.res.Resources; import android.graphics.drawable.Drawable; +import android.graphics.drawable.BitmapDrawable; import androidx.annotation.DrawableRes; import androidx.annotation.NonNull; @@ -25,6 +26,7 @@ import org.lineageos.backgrounds.R; import org.lineageos.backgrounds.bundle.WallpaperBundle; import org.lineageos.backgrounds.bundle.WallpaperType; +import org.lineageos.backgrounds.util.UiUtils; public final class BuiltInWallpaperFactory { @@ -33,8 +35,9 @@ private BuiltInWallpaperFactory() { public static WallpaperBundle build(@NonNull final String name, @NonNull final Resources res, - @DrawableRes final int drawableRes) { - Drawable drawable = res.getDrawable(drawableRes, res.newTheme()); + @DrawableRes final int drawableRes) { + Drawable drawable = new BitmapDrawable(res, UiUtils.decodeSampledBitmapFromResource(res, drawableRes, 250, 500)); + return new WallpaperBundle(name, drawable, drawableRes, WallpaperType.BUILT_IN); } diff --git a/app/src/main/java/org/lineageos/backgrounds/util/UiUtils.java b/app/src/main/java/org/lineageos/backgrounds/util/UiUtils.java index 2d5b798..b0de83b 100644 --- a/app/src/main/java/org/lineageos/backgrounds/util/UiUtils.java +++ b/app/src/main/java/org/lineageos/backgrounds/util/UiUtils.java @@ -17,6 +17,9 @@ import android.view.View; import android.view.Window; +import android.content.res.Resources; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; import androidx.annotation.ColorInt; import androidx.annotation.NonNull; @@ -38,4 +41,41 @@ public static void setStatusBarColor(@NonNull final Window window, @ColorInt fin window.getDecorView().setSystemUiVisibility(flags); } + + public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { + // Raw height and width of image + final int height = options.outHeight; + final int width = options.outWidth; + int inSampleSize = 1; + + if (height > reqHeight || width > reqWidth) { + + final int halfHeight = height / 2; + final int halfWidth = width / 2; + + // Calculate the largest inSampleSize value that is a power of 2 and keeps both + // height and width larger than the requested height and width. + while ((halfHeight / inSampleSize) >= reqHeight + && (halfWidth / inSampleSize) >= reqWidth) { + inSampleSize *= 2; + } + } + + return inSampleSize; + } + + public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) { + + // First decode with inJustDecodeBounds=true to check dimensions + final BitmapFactory.Options options = new BitmapFactory.Options(); + options.inJustDecodeBounds = true; + BitmapFactory.decodeResource(res, resId, options); + + // Calculate inSampleSize + options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); + + // Decode bitmap with inSampleSize set + options.inJustDecodeBounds = false; + return BitmapFactory.decodeResource(res, resId, options); + } }