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

Commit df5e4b2e authored by Vania Desmonda's avatar Vania Desmonda
Browse files

Fixing color hints adjustment on live wallpapers and lock screen when

wallpaper is dimmed

When dimming is applied on live wallpapers, remove
HINT_SUPPORTS_DARK_TEXT/_THEME hints from the color hints to disable
dark text by default.

Test: manual, atest WallpaperManagerServiceTest
Bug: 217367978
Change-Id: Ia614c0b36f84c707a7eb3dabdc7a1eb90992bf3c
parent c4837b13
Loading
Loading
Loading
Loading
+14 −5
Original line number Diff line number Diff line
@@ -26,6 +26,8 @@ import static android.view.SurfaceControl.METADATA_WINDOW_TYPE;
import static android.view.View.SYSTEM_UI_FLAG_VISIBLE;
import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.annotation.FloatRange;
import android.annotation.NonNull;
@@ -890,8 +892,6 @@ public abstract class WallpaperService extends Service {
         * @param dimAmount Float amount between [0.0, 1.0] to dim the wallpaper.
         */
        private void updateWallpaperDimming(float dimAmount) {
            mPreviousWallpaperDimAmount = mWallpaperDimAmount;

            // Custom dim amount cannot be less than the default dim amount.
            mWallpaperDimAmount = Math.max(mDefaultDimAmount, dimAmount);
            // If dim amount is 0f (additional dimming is removed), then the wallpaper should dim
@@ -909,15 +909,15 @@ public abstract class WallpaperService extends Service {
            SurfaceControl.Transaction surfaceControlTransaction = new SurfaceControl.Transaction();
            // TODO: apply the dimming to preview as well once surface transparency works in
            // preview mode.
            if (!isPreview() && mShouldDim) {
            if ((!isPreview() && mShouldDim)
                    || mPreviousWallpaperDimAmount != mWallpaperDimAmount) {
                Log.v(TAG, "Setting wallpaper dimming: " + mWallpaperDimAmount);

                // Animate dimming to gradually change the wallpaper alpha from the previous
                // dim amount to the new amount only if the dim amount changed.
                ValueAnimator animator = ValueAnimator.ofFloat(
                        mPreviousWallpaperDimAmount, mWallpaperDimAmount);
                animator.setDuration(mPreviousWallpaperDimAmount == mWallpaperDimAmount
                        ? 0 : DIMMING_ANIMATION_DURATION_MS);
                animator.setDuration(DIMMING_ANIMATION_DURATION_MS);
                animator.addUpdateListener((ValueAnimator va) -> {
                    final float dimValue = (float) va.getAnimatedValue();
                    if (mBbqSurfaceControl != null) {
@@ -925,11 +925,19 @@ public abstract class WallpaperService extends Service {
                                .setAlpha(mBbqSurfaceControl, 1 - dimValue).apply();
                    }
                });
                animator.addListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        updateSurface(false, false, true);
                    }
                });
                animator.start();
            } else {
                Log.v(TAG, "Setting wallpaper dimming: " + 0);
                surfaceControlTransaction.setAlpha(mBbqSurfaceControl, 1.0f).apply();
            }

            mPreviousWallpaperDimAmount = mWallpaperDimAmount;
        }

        /**
@@ -1332,6 +1340,7 @@ public abstract class WallpaperService extends Service {
                            resetWindowPages();
                            mSession.finishDrawing(mWindow, null /* postDrawTransaction */);
                            processLocalColors(mPendingXOffset, mPendingXOffsetStep);
                            notifyColorsChanged();
                        }
                        reposition();
                        reportEngineShown(shouldWaitForEngineShown());
+27 −3
Original line number Diff line number Diff line
@@ -429,7 +429,8 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
        if (needsExtraction) {
            extractColors(wallpaper);
        }
        notifyColorListeners(wallpaper.primaryColors, which, wallpaper.userId, displayId);
        notifyColorListeners(getAdjustedWallpaperColorsOnDimming(wallpaper), which,
                wallpaper.userId, displayId);
    }

    private static <T extends IInterface> boolean emptyCallbackList(RemoteCallbackList<T> list) {
@@ -1519,7 +1520,6 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
                if (mWallpaper.mWallpaperDimAmount != 0f) {
                    try {
                        connector.mEngine.applyDimming(mWallpaper.mWallpaperDimAmount);
                        notifyWallpaperColorsChanged(mWallpaper, FLAG_SYSTEM);
                    } catch (RemoteException e) {
                        Slog.w(TAG, "Failed to dim wallpaper", e);
                    }
@@ -2699,8 +2699,31 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
            extractColors(wallpaperData);
        }

        return getAdjustedWallpaperColorsOnDimming(wallpaperData);
    }

    /**
     * Gets the adjusted {@link WallpaperColors} if the wallpaper colors were not extracted from
     * bitmap (i.e. it's a live wallpaper) and the dim amount is not 0. If these conditions apply,
     * default to using color hints that do not support dark theme and dark text.
     *
     * @param wallpaperData WallpaperData containing the WallpaperColors and mWallpaperDimAmount
     */
    WallpaperColors getAdjustedWallpaperColorsOnDimming(WallpaperData wallpaperData) {
        synchronized (mLock) {
            return wallpaperData.primaryColors;
            WallpaperColors wallpaperColors = wallpaperData.primaryColors;

            if (wallpaperColors != null
                    && (wallpaperColors.getColorHints() & WallpaperColors.HINT_FROM_BITMAP) == 0
                    && wallpaperData.mWallpaperDimAmount != 0f) {
                int adjustedColorHints = wallpaperColors.getColorHints()
                        & ~WallpaperColors.HINT_SUPPORTS_DARK_TEXT
                        & ~WallpaperColors.HINT_SUPPORTS_DARK_THEME;
                return new WallpaperColors(
                        wallpaperColors.getPrimaryColor(), wallpaperColors.getSecondaryColor(),
                        wallpaperColors.getTertiaryColor(), adjustedColorHints);
            }
            return wallpaperColors;
        }
    }

@@ -2770,6 +2793,7 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
                    wallpaper.fromForegroundApp = fromForegroundApp;
                    wallpaper.cropHint.set(cropHint);
                    wallpaper.allowBackup = allowBackup;
                    wallpaper.mWallpaperDimAmount = getWallpaperDimAmount();
                }
                return pfd;
            } finally {
+43 −0
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@ import static com.android.server.wallpaper.WallpaperManagerService.WALLPAPER_CRO

import static org.hamcrest.core.IsNot.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
@@ -445,6 +446,48 @@ public class WallpaperManagerServiceTests {
                mService.getWallpaperDimAmount(), dimAmount, 0.0);
    }

    @Test
    public void testGetAdjustedWallpaperColorsOnDimming() throws RemoteException {
        final int testUserId = USER_SYSTEM;
        mService.switchUser(testUserId, null);
        mService.setWallpaperComponent(sDefaultWallpaperComponent);
        WallpaperData wallpaper = mService.getCurrentWallpaperData(FLAG_SYSTEM, testUserId);

        // Mock a wallpaper data with color hints that support dark text and dark theme
        // but not HINT_FROM_BITMAP
        wallpaper.primaryColors = new WallpaperColors(Color.valueOf(Color.WHITE), null, null,
                WallpaperColors.HINT_SUPPORTS_DARK_TEXT | WallpaperColors.HINT_SUPPORTS_DARK_THEME);
        mService.setWallpaperDimAmount(0.6f);
        int colorHints = mService.getAdjustedWallpaperColorsOnDimming(wallpaper).getColorHints();
        // Dimmed wallpaper not extracted from bitmap does not support dark text and dark theme
        assertNotEquals(WallpaperColors.HINT_SUPPORTS_DARK_TEXT,
                colorHints & WallpaperColors.HINT_SUPPORTS_DARK_TEXT);
        assertNotEquals(WallpaperColors.HINT_SUPPORTS_DARK_THEME,
                colorHints & WallpaperColors.HINT_SUPPORTS_DARK_THEME);

        // Remove dimming
        mService.setWallpaperDimAmount(0f);
        colorHints = mService.getAdjustedWallpaperColorsOnDimming(wallpaper).getColorHints();
        // Undimmed wallpaper not extracted from bitmap does support dark text and dark theme
        assertEquals(WallpaperColors.HINT_SUPPORTS_DARK_TEXT,
                colorHints & WallpaperColors.HINT_SUPPORTS_DARK_TEXT);
        assertEquals(WallpaperColors.HINT_SUPPORTS_DARK_THEME,
                colorHints & WallpaperColors.HINT_SUPPORTS_DARK_THEME);

        // Mock a wallpaper data with color hints that support dark text and dark theme
        // and was extracted from bitmap
        wallpaper.primaryColors = new WallpaperColors(Color.valueOf(Color.WHITE), null, null,
                WallpaperColors.HINT_SUPPORTS_DARK_TEXT | WallpaperColors.HINT_SUPPORTS_DARK_THEME
                        | WallpaperColors.HINT_FROM_BITMAP);
        mService.setWallpaperDimAmount(0.6f);
        colorHints = mService.getAdjustedWallpaperColorsOnDimming(wallpaper).getColorHints();
        // Dimmed wallpaper should still support dark text and dark theme
        assertEquals(WallpaperColors.HINT_SUPPORTS_DARK_TEXT,
                colorHints & WallpaperColors.HINT_SUPPORTS_DARK_TEXT);
        assertEquals(WallpaperColors.HINT_SUPPORTS_DARK_THEME,
                colorHints & WallpaperColors.HINT_SUPPORTS_DARK_THEME);
    }

    // Verify that after continue switch user from userId 0 to lastUserId, the wallpaper data for
    // non-current user must not bind to wallpaper service.
    private void verifyNoConnectionBeforeLastUser(int lastUserId) {