Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit fa24e4f6 authored by wilsonshih's avatar wilsonshih
Browse files

Refine extract default image wallpaper colors method.

Refine extract default image colors method, also cache the default
image wallpaper colors when we first read the image file.

Fix: 123490371
Test: change to default image wallpaper and can change theme color.

Change-Id: Iacb7280b0bae7f7075684a14522b95d3adbfe2c7
parent df44944f
Loading
Loading
Loading
Loading
+38 −38
Original line number Diff line number Diff line
@@ -474,7 +474,13 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
        int wallpaperId;

        if (wallpaper.equals(mFallbackWallpaper)) {
            extractDefaultImageWallpaperColors();
            synchronized (mLock) {
                if (mFallbackWallpaper.primaryColors != null) return;
            }
            final WallpaperColors colors = extractDefaultImageWallpaperColors();
            synchronized (mLock) {
                mFallbackWallpaper.primaryColors = colors;
            }
            return;
        }

@@ -499,23 +505,7 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
            }
        } else if (defaultImageWallpaper) {
            // There is no crop and source file because this is default image wallpaper.
            try (final InputStream is =
                         WallpaperManager.openDefaultWallpaper(mContext, FLAG_SYSTEM)) {
                if (is != null) {
                    try {
                        final BitmapFactory.Options options = new BitmapFactory.Options();
                        final Bitmap bitmap = BitmapFactory.decodeStream(is, null, options);
                        if (bitmap != null) {
                            colors = WallpaperColors.fromBitmap(bitmap);
                            bitmap.recycle();
                        }
                    } catch (OutOfMemoryError e) {
                        Slog.w(TAG, "Can't decode default wallpaper stream", e);
                    }
                }
            } catch (IOException e) {
                Slog.w(TAG, "Can't close default wallpaper stream", e);
            }
            colors = extractDefaultImageWallpaperColors();
        }

        if (colors == null) {
@@ -535,16 +525,20 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
        }
    }

    private void extractDefaultImageWallpaperColors() {
    private WallpaperColors extractDefaultImageWallpaperColors() {
        if (DEBUG) Slog.d(TAG, "Extract default image wallpaper colors");

        synchronized (mLock) {
            if (mFallbackWallpaper.primaryColors != null) return;
            if (mCacheDefaultImageWallpaperColors != null) return mCacheDefaultImageWallpaperColors;
        }

        if (DEBUG) Slog.d(TAG, "Extract default image wallpaper colors");
        WallpaperColors colors = null;
        final InputStream is = WallpaperManager.openDefaultWallpaper(mContext, FLAG_SYSTEM);
        if (is != null) {
            try {
        try (InputStream is = WallpaperManager.openDefaultWallpaper(mContext, FLAG_SYSTEM)) {
            if (is == null) {
                Slog.w(TAG, "Can't open default wallpaper stream");
                return null;
            }

            final BitmapFactory.Options options = new BitmapFactory.Options();
            final Bitmap bitmap = BitmapFactory.decodeStream(is, null, options);
            if (bitmap != null) {
@@ -553,21 +547,21 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
            }
        } catch (OutOfMemoryError e) {
            Slog.w(TAG, "Can't decode default wallpaper stream", e);
            } finally {
                IoUtils.closeQuietly(is);
            }
        } catch (IOException e) {
            Slog.w(TAG, "Can't close default wallpaper stream", e);
        }

        if (colors == null) {
            Slog.e(TAG, "Extract default image wallpaper colors failed");
            return;
        }

        } else {
            synchronized (mLock) {
            mFallbackWallpaper.primaryColors = colors;
                mCacheDefaultImageWallpaperColors = colors;
            }
        }

        return colors;
    }

    /**
     * Once a new wallpaper has been written via setWallpaper(...), it needs to be cropped
     * for display.
@@ -814,6 +808,12 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
     */
    private final ComponentName mImageWallpaper;

    /**
     * Default image wallpaper shall never changed after system service started, caching it when we
     * first read the image file.
     */
    private WallpaperColors mCacheDefaultImageWallpaperColors;

    /**
     * Name of the default wallpaper component; might be different from mImageWallpaper
     */