From c3f60fb9441cc6267c0bc96ab61bb3625d824ee0 Mon Sep 17 00:00:00 2001 From: Alex Stetson Date: Thu, 6 May 2021 08:22:12 -0700 Subject: [PATCH] Move HideNonSystemOverlayMixin to SettingsLib To be re-used by settings applications for other form factors. Bug: 184967544 Test: make RunSettingsLibRoboTests -j40 ROBOTEST_FILTER=com.android.settingslib.core.lifecycle Change-Id: I749dbd3f24bf77453d3ecc5d4374d11656498850 --- .../lifecycle/HideNonSystemOverlayMixin.java | 78 ++++++++++++++ .../HideNonSystemOverlayMixinTest.java | 100 ++++++++++++++++++ 2 files changed, 178 insertions(+) create mode 100644 packages/SettingsLib/src/com/android/settingslib/core/lifecycle/HideNonSystemOverlayMixin.java create mode 100644 packages/SettingsLib/tests/robotests/src/com/android/settingslib/core/lifecycle/HideNonSystemOverlayMixinTest.java diff --git a/packages/SettingsLib/src/com/android/settingslib/core/lifecycle/HideNonSystemOverlayMixin.java b/packages/SettingsLib/src/com/android/settingslib/core/lifecycle/HideNonSystemOverlayMixin.java new file mode 100644 index 0000000000000..d7e9dc6f9c4c0 --- /dev/null +++ b/packages/SettingsLib/src/com/android/settingslib/core/lifecycle/HideNonSystemOverlayMixin.java @@ -0,0 +1,78 @@ +/* + * Copyright (C) 2021 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settingslib.core.lifecycle; + +import static android.view.WindowManager.LayoutParams.SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS; + +import static androidx.lifecycle.Lifecycle.Event.ON_START; +import static androidx.lifecycle.Lifecycle.Event.ON_STOP; + +import android.app.Activity; +import android.provider.Settings; +import android.view.Window; +import android.view.WindowManager; + +import androidx.annotation.VisibleForTesting; +import androidx.lifecycle.LifecycleObserver; +import androidx.lifecycle.OnLifecycleEvent; + +/** + * A mixin that adds window flag to prevent non-system overlays showing on top of Settings + * activities. + */ +public class HideNonSystemOverlayMixin implements LifecycleObserver { + + public static final String SECURE_OVERLAY_SETTINGS = "secure_overlay_settings"; + + private final Activity mActivity; + + public HideNonSystemOverlayMixin(Activity activity) { + mActivity = activity; + } + + @VisibleForTesting + boolean isEnabled() { + return Settings.Secure.getInt(mActivity.getContentResolver(), + SECURE_OVERLAY_SETTINGS, 0 /* defValue */) == 0; + } + + /** + * Start Lifecycle event + */ + @OnLifecycleEvent(ON_START) + public void onStart() { + if (mActivity == null || !isEnabled()) { + return; + } + mActivity.getWindow().addSystemFlags(SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS); + android.util.EventLog.writeEvent(0x534e4554, "120484087", -1, ""); + } + + /** + * Stop Lifecycle event + */ + @OnLifecycleEvent(ON_STOP) + public void onStop() { + if (mActivity == null || !isEnabled()) { + return; + } + final Window window = mActivity.getWindow(); + final WindowManager.LayoutParams attrs = window.getAttributes(); + attrs.privateFlags &= ~SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS; + window.setAttributes(attrs); + } +} diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/core/lifecycle/HideNonSystemOverlayMixinTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/core/lifecycle/HideNonSystemOverlayMixinTest.java new file mode 100644 index 0000000000000..cf702b531a3c2 --- /dev/null +++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/core/lifecycle/HideNonSystemOverlayMixinTest.java @@ -0,0 +1,100 @@ +/* + * Copyright (C) 2021 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settingslib.core.lifecycle; + +import static android.view.WindowManager.LayoutParams.SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS; + +import static com.android.settingslib.core.lifecycle.HideNonSystemOverlayMixin.SECURE_OVERLAY_SETTINGS; + +import static com.google.common.truth.Truth.assertThat; + +import android.os.Bundle; +import android.provider.Settings; +import android.view.WindowManager; + +import androidx.annotation.Nullable; +import androidx.appcompat.app.AppCompatActivity; + +import com.android.settingslib.R; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.Robolectric; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.android.controller.ActivityController; + +@RunWith(RobolectricTestRunner.class) +public class HideNonSystemOverlayMixinTest { + + private ActivityController mActivityController; + + @Before + public void setUp() { + mActivityController = Robolectric.buildActivity(TestActivity.class); + } + + @Test + public void startActivity_shouldHideNonSystemOverlay() { + mActivityController.setup(); + TestActivity activity = mActivityController.get(); + + // Activity start: HIDE_NON_SYSTEM_OVERLAY should be set. + final WindowManager.LayoutParams attrs = activity.getWindow().getAttributes(); + assertThat(attrs.privateFlags & SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS) + .isNotEqualTo(0); + } + + @Test + public void stopActivity_shouldUnhideNonSystemOverlay() { + mActivityController.setup().stop(); + TestActivity activity = mActivityController.get(); + + final WindowManager.LayoutParams attrs = activity.getWindow().getAttributes(); + assertThat(attrs.privateFlags & SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS) + .isEqualTo(0); + } + + @Test + public void isEnabled_isAllowedOverlaySettings_returnFalse() { + mActivityController.setup(); + final TestActivity activity = mActivityController.get(); + Settings.Secure.putInt(activity.getContentResolver(), + SECURE_OVERLAY_SETTINGS, 1); + + assertThat(new HideNonSystemOverlayMixin(activity).isEnabled()).isFalse(); + } + + @Test + public void isEnabled_isNotAllowedOverlaySettings_returnTrue() { + mActivityController.setup(); + TestActivity activity = mActivityController.get(); + Settings.Secure.putInt(activity.getContentResolver(), + SECURE_OVERLAY_SETTINGS, 0); + + assertThat(new HideNonSystemOverlayMixin(activity).isEnabled()).isTrue(); + } + + public static class TestActivity extends AppCompatActivity { + @Override + protected void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setTheme(R.style.Theme_AppCompat); + getLifecycle().addObserver(new HideNonSystemOverlayMixin(this)); + } + } +}