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

Commit dff28db1 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Guard against crashes in effects services" into main

parents 4d9d7a73 f71d6a6e
Loading
Loading
Loading
Loading
+34 −13
Original line number Original line Diff line number Diff line
@@ -32,12 +32,13 @@ import android.service.notification.DeviceEffectsApplier;
import android.service.notification.ZenDeviceEffects;
import android.service.notification.ZenDeviceEffects;
import android.service.notification.ZenModeConfig;
import android.service.notification.ZenModeConfig;
import android.service.notification.ZenModeConfig.ConfigChangeOrigin;
import android.service.notification.ZenModeConfig.ConfigChangeOrigin;
import android.util.Slog;


import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.GuardedBy;


/** Default implementation for {@link DeviceEffectsApplier}. */
/** Default implementation for {@link DeviceEffectsApplier}. */
class DefaultDeviceEffectsApplier implements DeviceEffectsApplier {
class DefaultDeviceEffectsApplier implements DeviceEffectsApplier {

    private static final String TAG = "DeviceEffectsApplier";
    private static final String SUPPRESS_AMBIENT_DISPLAY_TOKEN =
    private static final String SUPPRESS_AMBIENT_DISPLAY_TOKEN =
            "DefaultDeviceEffectsApplier:SuppressAmbientDisplay";
            "DefaultDeviceEffectsApplier:SuppressAmbientDisplay";
    private static final int SATURATION_LEVEL_GRAYSCALE = 0;
    private static final int SATURATION_LEVEL_GRAYSCALE = 0;
@@ -75,28 +76,44 @@ class DefaultDeviceEffectsApplier implements DeviceEffectsApplier {
        Binder.withCleanCallingIdentity(() -> {
        Binder.withCleanCallingIdentity(() -> {
            if (mLastAppliedEffects.shouldSuppressAmbientDisplay()
            if (mLastAppliedEffects.shouldSuppressAmbientDisplay()
                    != effects.shouldSuppressAmbientDisplay()) {
                    != effects.shouldSuppressAmbientDisplay()) {
                try {
                    mPowerManager.suppressAmbientDisplay(SUPPRESS_AMBIENT_DISPLAY_TOKEN,
                    mPowerManager.suppressAmbientDisplay(SUPPRESS_AMBIENT_DISPLAY_TOKEN,
                            effects.shouldSuppressAmbientDisplay());
                            effects.shouldSuppressAmbientDisplay());
                } catch (Exception e) {
                    Slog.e(TAG, "Could not change AOD override", e);
                }
            }
            }


            if (mLastAppliedEffects.shouldDisplayGrayscale() != effects.shouldDisplayGrayscale()) {
            if (mLastAppliedEffects.shouldDisplayGrayscale() != effects.shouldDisplayGrayscale()) {
                if (mColorDisplayManager != null) {
                if (mColorDisplayManager != null) {
                    try {
                        mColorDisplayManager.setSaturationLevel(
                        mColorDisplayManager.setSaturationLevel(
                                effects.shouldDisplayGrayscale() ? SATURATION_LEVEL_GRAYSCALE
                                effects.shouldDisplayGrayscale() ? SATURATION_LEVEL_GRAYSCALE
                                        : SATURATION_LEVEL_FULL_COLOR);
                                        : SATURATION_LEVEL_FULL_COLOR);
                    } catch (Exception e) {
                        Slog.e(TAG, "Could not change grayscale override", e);
                    }
                }
                }
            }
            }


            if (mLastAppliedEffects.shouldDimWallpaper() != effects.shouldDimWallpaper()) {
            if (mLastAppliedEffects.shouldDimWallpaper() != effects.shouldDimWallpaper()) {
                if (mWallpaperManager != null) {
                if (mWallpaperManager != null) {
                    try {
                        mWallpaperManager.setWallpaperDimAmount(
                        mWallpaperManager.setWallpaperDimAmount(
                                effects.shouldDimWallpaper() ? WALLPAPER_DIM_AMOUNT_DIMMED
                                effects.shouldDimWallpaper() ? WALLPAPER_DIM_AMOUNT_DIMMED
                                        : WALLPAPER_DIM_AMOUNT_NORMAL);
                                        : WALLPAPER_DIM_AMOUNT_NORMAL);
                    } catch (Exception e) {
                        Slog.e(TAG, "Could not change wallpaper override", e);
                    }
                }
                }
            }
            }


            if (mLastAppliedEffects.shouldUseNightMode() != effects.shouldUseNightMode()) {
            if (mLastAppliedEffects.shouldUseNightMode() != effects.shouldUseNightMode()) {
                try {
                    updateOrScheduleNightMode(effects.shouldUseNightMode(), origin);
                    updateOrScheduleNightMode(effects.shouldUseNightMode(), origin);
                } catch (Exception e) {
                    Slog.e(TAG, "Could not change dark theme override", e);
                }
            }
            }
        });
        });


@@ -131,9 +148,13 @@ class DefaultDeviceEffectsApplier implements DeviceEffectsApplier {


    private void updateNightModeImmediately(boolean useNightMode) {
    private void updateNightModeImmediately(boolean useNightMode) {
        Binder.withCleanCallingIdentity(() -> {
        Binder.withCleanCallingIdentity(() -> {
            try {
                mUiModeManager.setAttentionModeThemeOverlay(
                mUiModeManager.setAttentionModeThemeOverlay(
                        useNightMode ? MODE_ATTENTION_THEME_OVERLAY_NIGHT
                        useNightMode ? MODE_ATTENTION_THEME_OVERLAY_NIGHT
                                : MODE_ATTENTION_THEME_OVERLAY_OFF);
                                : MODE_ATTENTION_THEME_OVERLAY_OFF);
            } catch (Exception e) {
                Slog.e(TAG, "Could not change wallpaper override", e);
            }
        });
        });
    }
    }


+24 −0
Original line number Original line Diff line number Diff line
@@ -25,10 +25,12 @@ import static com.google.common.truth.Truth.assertThat;


import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyFloat;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.spy;
@@ -322,4 +324,26 @@ public class DefaultDeviceEffectsApplierTest {
                argThat(filter -> Intent.ACTION_SCREEN_OFF.equals(filter.getAction(0))),
                argThat(filter -> Intent.ACTION_SCREEN_OFF.equals(filter.getAction(0))),
                anyInt());
                anyInt());
    }
    }

    @Test
    public void apply_servicesThrow_noCrash() {
        mSetFlagsRule.enableFlags(android.app.Flags.FLAG_MODES_API);

        doThrow(new RuntimeException()).when(mPowerManager)
                .suppressAmbientDisplay(anyString(), anyBoolean());
        doThrow(new RuntimeException()).when(mColorDisplayManager).setSaturationLevel(anyInt());
        doThrow(new RuntimeException()).when(mWallpaperManager).setWallpaperDimAmount(anyFloat());
        doThrow(new RuntimeException()).when(mUiModeManager).setAttentionModeThemeOverlay(anyInt());
        mApplier = new DefaultDeviceEffectsApplier(mContext);

        ZenDeviceEffects effects = new ZenDeviceEffects.Builder()
                .setShouldSuppressAmbientDisplay(true)
                .setShouldDimWallpaper(true)
                .setShouldDisplayGrayscale(true)
                .setShouldUseNightMode(true)
                .build();
        mApplier.apply(effects, UPDATE_ORIGIN_USER);

        // No crashes
    }
}
}