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

Commit db5a7a4c authored by James O'Leary's avatar James O'Leary Committed by Android (Google) Code Review
Browse files

Merge "Fix WallpaperPicker not being able to assign colors" into sc-dev

parents c28d1e24 edb7c22c
Loading
Loading
Loading
Loading
+16 −7
Original line number Original line Diff line number Diff line
@@ -450,19 +450,23 @@ public class ThemeOverlayController extends SystemUI implements Dumpable {
        OverlayIdentifier systemPalette = categoryToPackage.get(OVERLAY_CATEGORY_SYSTEM_PALETTE);
        OverlayIdentifier systemPalette = categoryToPackage.get(OVERLAY_CATEGORY_SYSTEM_PALETTE);
        if (mIsMonetEnabled && systemPalette != null && systemPalette.getPackageName() != null) {
        if (mIsMonetEnabled && systemPalette != null && systemPalette.getPackageName() != null) {
            try {
            try {
                int color = Integer.parseInt(systemPalette.getPackageName().toLowerCase(), 16);
                String colorString =  systemPalette.getPackageName().toLowerCase();
                if (!colorString.startsWith("#")) {
                    colorString = "#" + colorString;
                }
                int color = Color.parseColor(colorString);
                mNeutralOverlay = getOverlay(color, NEUTRAL);
                mNeutralOverlay = getOverlay(color, NEUTRAL);
                mNeedsOverlayCreation = true;
                mNeedsOverlayCreation = true;
                categoryToPackage.remove(OVERLAY_CATEGORY_SYSTEM_PALETTE);
                categoryToPackage.remove(OVERLAY_CATEGORY_SYSTEM_PALETTE);
            } catch (NumberFormatException e) {
            } catch (Exception e) {
                Log.w(TAG, "Invalid color definition: " + systemPalette.getPackageName());
                // Color.parseColor doesn't catch any exceptions from the calls it makes
                Log.w(TAG, "Invalid color definition: " + systemPalette.getPackageName(), e);
            }
            }
        } else if (!mIsMonetEnabled && systemPalette != null) {
        } else if (!mIsMonetEnabled && systemPalette != null) {
            try {
            try {
                // It's possible that we flipped the flag off and still have a @ColorInt in the
                // It's possible that we flipped the flag off and still have a @ColorInt in the
                // setting. We need to sanitize the input, otherwise the overlay transaction will
                // setting. We need to sanitize the input, otherwise the overlay transaction will
                // fail.
                // fail.
                Integer.parseInt(systemPalette.getPackageName().toLowerCase(), 16);
                categoryToPackage.remove(OVERLAY_CATEGORY_SYSTEM_PALETTE);
                categoryToPackage.remove(OVERLAY_CATEGORY_SYSTEM_PALETTE);
            } catch (NumberFormatException e) {
            } catch (NumberFormatException e) {
                // This is a package name. All good, let's continue
                // This is a package name. All good, let's continue
@@ -473,12 +477,17 @@ public class ThemeOverlayController extends SystemUI implements Dumpable {
        OverlayIdentifier accentPalette = categoryToPackage.get(OVERLAY_CATEGORY_ACCENT_COLOR);
        OverlayIdentifier accentPalette = categoryToPackage.get(OVERLAY_CATEGORY_ACCENT_COLOR);
        if (mIsMonetEnabled && accentPalette != null && accentPalette.getPackageName() != null) {
        if (mIsMonetEnabled && accentPalette != null && accentPalette.getPackageName() != null) {
            try {
            try {
                int color = Integer.parseInt(accentPalette.getPackageName().toLowerCase(), 16);
                String colorString =  accentPalette.getPackageName().toLowerCase();
                if (!colorString.startsWith("#")) {
                    colorString = "#" + colorString;
                }
                int color = Color.parseColor(colorString);
                mSecondaryOverlay = getOverlay(color, ACCENT);
                mSecondaryOverlay = getOverlay(color, ACCENT);
                mNeedsOverlayCreation = true;
                mNeedsOverlayCreation = true;
                categoryToPackage.remove(OVERLAY_CATEGORY_ACCENT_COLOR);
                categoryToPackage.remove(OVERLAY_CATEGORY_ACCENT_COLOR);
            } catch (NumberFormatException e) {
            } catch (Exception e) {
                Log.w(TAG, "Invalid color definition: " + accentPalette.getPackageName());
                // Color.parseColor doesn't catch any exceptions from the calls it makes
                Log.w(TAG, "Invalid color definition: " + accentPalette.getPackageName(), e);
            }
            }
        } else if (!mIsMonetEnabled && accentPalette != null) {
        } else if (!mIsMonetEnabled && accentPalette != null) {
            try {
            try {
+38 −0
Original line number Original line Diff line number Diff line
@@ -465,6 +465,44 @@ public class ThemeOverlayControllerTest extends SysuiTestCase {
                any());
                any());
    }
    }


    @Test
    public void catchException_whenPackageNameIsOverlayName() {
        mDeviceProvisionedController = mock(DeviceProvisionedController.class);
        mThemeOverlayApplier = mock(ThemeOverlayApplier.class);
        mWallpaperManager = mock(WallpaperManager.class);

        // Assume we have some wallpaper colors at boot.
        when(mWallpaperManager.getWallpaperColors(anyInt()))
                .thenReturn(new WallpaperColors(Color.valueOf(Color.GRAY), null, null));

        Executor executor = MoreExecutors.directExecutor();

        mThemeOverlayController = new ThemeOverlayController(null /* context */,
                mBroadcastDispatcher, mBgHandler, executor, executor, mThemeOverlayApplier,
                mSecureSettings, mWallpaperManager, mUserManager, mDeviceProvisionedController,
                mUserTracker, mDumpManager, mFeatureFlags, mWakefulnessLifecycle) {
            @Nullable
            @Override
            protected FabricatedOverlay getOverlay(int color, int type) {
                FabricatedOverlay overlay = mock(FabricatedOverlay.class);
                when(overlay.getIdentifier())
                        .thenReturn(new OverlayIdentifier("com.thebest.livewallpaperapp.ever"));

                return overlay;
            }

        };
        mThemeOverlayController.start();

        verify(mWallpaperManager).addOnColorsChangedListener(mColorsListener.capture(), eq(null),
                eq(UserHandle.USER_ALL));
        verify(mDeviceProvisionedController).addCallback(mDeviceProvisionedListener.capture());

        // Colors were applied during controller initialization.
        verify(mThemeOverlayApplier).applyCurrentUserOverlays(any(), any(), anyInt(), any());
        clearInvocations(mThemeOverlayApplier);
    }

    @Test
    @Test
    public void onWallpaperColorsChanged_defersUntilSetupIsCompleted_ifHasColors() {
    public void onWallpaperColorsChanged_defersUntilSetupIsCompleted_ifHasColors() {
        mDeviceProvisionedController = mock(DeviceProvisionedController.class);
        mDeviceProvisionedController = mock(DeviceProvisionedController.class);