Add a setting for allowing overlays on Settings app

In the past, we allowed non system overlays on user-debug rom.
Acoording to the user-debug guildline, we should not have differnt
behavior between the user build and user debug rom.

To maintain the consistency between user and user debug rom,
We're creating a new developer setting for allowing non-system overlay on
Settings. By default, we don't allow any non-system app overlays on
Settings app unless user turns it on explictly.

Test: Run robotest. Turn on setting, see the overlays on Settings.
Turn off setting, do not see the overlays on Settings.
Fix: 144989059

Change-Id: I87f00a2eda91de003c6e542e7ec45a066f46fbf7
This commit is contained in:
Tsung-Mao Fang
2020-05-29 20:54:42 +08:00
parent 4432ac9c7e
commit f6841ef548
7 changed files with 241 additions and 10 deletions

View File

@@ -12094,4 +12094,9 @@
<!-- Developer settings: app freezer title [CHAR LIMIT=50]-->
<string name="cached_apps_freezer">Suspend execution for cached apps</string>
<!-- Title for allowing screen overlays on Settings app. [CHAR LIMIT=50]-->
<string name="overlay_settings_title">Allow screen overlays on Settings</string>
<!-- Summary for allowing screen overlays on Settings app. [CHAR LIMIT=NONE]-->
<string name="overlay_settings_summary">Allow apps that can display over other apps to overlay Settings screens</string>
</resources>

View File

@@ -251,6 +251,11 @@
android:title="@string/show_refresh_rate"
android:summary="@string/show_refresh_rate_summary" />
<SwitchPreference
android:key="overlay_settings"
android:title="@string/overlay_settings_title"
android:summary="@string/overlay_settings_summary" />
</PreferenceCategory>
<PreferenceCategory

View File

@@ -22,7 +22,6 @@ import static androidx.lifecycle.Lifecycle.Event.ON_START;
import static androidx.lifecycle.Lifecycle.Event.ON_STOP;
import android.app.Activity;
import android.os.Build;
import android.view.Window;
import android.view.WindowManager;
@@ -30,6 +29,8 @@ import androidx.annotation.VisibleForTesting;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;
import com.android.settings.development.OverlaySettingsPreferenceController;
/**
* A mixin that adds window flag to prevent non-system overlays showing on top of Settings
@@ -45,7 +46,7 @@ public class HideNonSystemOverlayMixin implements LifecycleObserver {
@VisibleForTesting
boolean isEnabled() {
return !Build.IS_DEBUGGABLE;
return !OverlaySettingsPreferenceController.isOverlaySettingsEnabled(mActivity);
}
@OnLifecycleEvent(ON_START)

View File

@@ -539,6 +539,7 @@ public class DevelopmentSettingsDashboardFragment extends RestrictedDashboardFra
controllers.add(new BluetoothHDAudioPreferenceController(context, lifecycle,
bluetoothA2dpConfigStore, fragment));
controllers.add(new SharedDataPreferenceController(context));
controllers.add(new OverlaySettingsPreferenceController(context));
return controllers;
}

View File

@@ -0,0 +1,88 @@
/*
* Copyright (C) 2020 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.settings.development;
import android.content.Context;
import android.content.SharedPreferences;
import androidx.annotation.VisibleForTesting;
import androidx.preference.Preference;
import androidx.preference.SwitchPreference;
import com.android.settings.core.PreferenceControllerMixin;
import com.android.settingslib.development.DeveloperOptionsPreferenceController;
/**
* A controller helps enable or disable a developer setting which allows non system overlays on
* Settings app.
*/
public class OverlaySettingsPreferenceController extends DeveloperOptionsPreferenceController
implements Preference.OnPreferenceChangeListener, PreferenceControllerMixin {
public static final String SHARE_PERFS = "overlay_settings";
private static final String KEY_OVERLAY_SETTINGS = "overlay_settings";
public OverlaySettingsPreferenceController(Context context) {
super(context);
}
@Override
public boolean isAvailable() {
return true;
}
@Override
public String getPreferenceKey() {
return KEY_OVERLAY_SETTINGS;
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
setOverlaySettingsEnabled(mContext, (Boolean) newValue);
return true;
}
@Override
public void updateState(Preference preference) {
((SwitchPreference) preference).setChecked(isOverlaySettingsEnabled(mContext));
}
/**
* Check if this setting is enabled or not.
*/
public static boolean isOverlaySettingsEnabled(Context context) {
final SharedPreferences editor = context.getSharedPreferences(SHARE_PERFS,
Context.MODE_PRIVATE);
return editor.getBoolean(SHARE_PERFS, false /* defValue */);
}
/**
* Enable this setting.
*/
@VisibleForTesting
static void setOverlaySettingsEnabled(Context context, boolean enabled) {
final SharedPreferences editor = context.getSharedPreferences(SHARE_PERFS,
Context.MODE_PRIVATE);
editor.edit().putBoolean(SHARE_PERFS, enabled).apply();
}
@Override
protected void onDeveloperOptionsSwitchDisabled() {
super.onDeveloperOptionsSwitchDisabled();
setOverlaySettingsEnabled(mContext, false);
}
}

View File

@@ -20,7 +20,8 @@ import static android.view.WindowManager.LayoutParams.SYSTEM_FLAG_HIDE_NON_SYSTE
import static com.google.common.truth.Truth.assertThat;
import android.os.Build;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.WindowManager;
@@ -28,6 +29,7 @@ import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.android.settings.R;
import com.android.settings.development.OverlaySettingsPreferenceController;
import org.junit.Before;
import org.junit.Test;
@@ -35,7 +37,6 @@ import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.android.controller.ActivityController;
import org.robolectric.util.ReflectionHelpers;
@RunWith(RobolectricTestRunner.class)
public class HideNonSystemOverlayMixinTest {
@@ -69,17 +70,27 @@ public class HideNonSystemOverlayMixinTest {
}
@Test
public void isEnabled_debug_false() {
ReflectionHelpers.setStaticField(Build.class, "IS_DEBUGGABLE", true);
public void isEnabled_isAllowedOverlaySettings_returnFalse() {
mActivityController.setup();
final TestActivity activity = mActivityController.get();
final SharedPreferences editor = activity.getSharedPreferences(
OverlaySettingsPreferenceController.SHARE_PERFS,
Context.MODE_PRIVATE);
editor.edit().putBoolean(OverlaySettingsPreferenceController.SHARE_PERFS, true).apply();
assertThat(new HideNonSystemOverlayMixin(null).isEnabled()).isFalse();
assertThat(new HideNonSystemOverlayMixin(activity).isEnabled()).isFalse();
}
@Test
public void isEnabled_user_true() {
ReflectionHelpers.setStaticField(Build.class, "IS_DEBUGGABLE", false);
public void isEnabled_isNotAllowedOverlaySettings_returnTrue() {
mActivityController.setup();
TestActivity activity = mActivityController.get();
final SharedPreferences editor = activity.getSharedPreferences(
OverlaySettingsPreferenceController.SHARE_PERFS,
Context.MODE_PRIVATE);
editor.edit().putBoolean(OverlaySettingsPreferenceController.SHARE_PERFS, false).apply();
assertThat(new HideNonSystemOverlayMixin(null).isEnabled()).isTrue();
assertThat(new HideNonSystemOverlayMixin(activity).isEnabled()).isTrue();
}
public static class TestActivity extends AppCompatActivity {

View File

@@ -0,0 +1,120 @@
/*
* Copyright (C) 2020 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.settings.development;
import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
import android.content.SharedPreferences;
import androidx.preference.SwitchPreference;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
public class OverlaySettingsPreferenceControllerTest {
private Context mContext;
private SwitchPreference mPreference;
private OverlaySettingsPreferenceController mController;
@Before
public void setUp() {
mContext = RuntimeEnvironment.application;
mController = new OverlaySettingsPreferenceController(mContext);
mPreference = new SwitchPreference(mContext);
}
@Test
public void isAvailable_shouldReturnTrue() {
assertThat(mController.isAvailable()).isTrue();
}
@Test
public void updateState_isOverlaySettingsEnabled_shouldCheckPreference() {
OverlaySettingsPreferenceController.setOverlaySettingsEnabled(mContext, true);
mController.updateState(mPreference);
assertThat(mPreference.isChecked()).isTrue();
}
@Test
public void updateState_isOverlaySettingsDisabled_shouldUncheckPreference() {
OverlaySettingsPreferenceController.setOverlaySettingsEnabled(mContext, false);
mController.updateState(mPreference);
assertThat(mPreference.isChecked()).isFalse();
}
@Test
public void onPreferenceChange_preferenceChecked_shouldEnableSettings() {
mController.onPreferenceChange(mPreference, true);
assertThat(OverlaySettingsPreferenceController.isOverlaySettingsEnabled(mContext)).isTrue();
}
@Test
public void onPreferenceChange_preferenceUnchecked_shouldDisableSettings() {
mController.onPreferenceChange(mPreference, false);
assertThat(
OverlaySettingsPreferenceController.isOverlaySettingsEnabled(mContext)).isFalse();
}
@Test
public void isOverlaySettingsEnabled_sharePreferenceSetTrue_shouldReturnTrue() {
final SharedPreferences editor = mContext.getSharedPreferences(
OverlaySettingsPreferenceController.SHARE_PERFS,
Context.MODE_PRIVATE);
editor.edit().putBoolean(OverlaySettingsPreferenceController.SHARE_PERFS, true).apply();
assertThat(OverlaySettingsPreferenceController.isOverlaySettingsEnabled(mContext)).isTrue();
}
@Test
public void isOverlaySettingsEnabled_sharePreferenceSetFalse_shouldReturnFalse() {
final SharedPreferences editor = mContext.getSharedPreferences(
OverlaySettingsPreferenceController.SHARE_PERFS,
Context.MODE_PRIVATE);
editor.edit().putBoolean(OverlaySettingsPreferenceController.SHARE_PERFS, false).apply();
assertThat(
OverlaySettingsPreferenceController.isOverlaySettingsEnabled(mContext)).isFalse();
}
@Test
public void setOverlaySettingsEnabled_setTrue_shouldStoreTrue() {
OverlaySettingsPreferenceController.setOverlaySettingsEnabled(mContext, true);
assertThat(
OverlaySettingsPreferenceController.isOverlaySettingsEnabled(mContext)).isTrue();
}
@Test
public void setOverlaySettingsEnabled_setFalse_shouldStoreTrue() {
OverlaySettingsPreferenceController.setOverlaySettingsEnabled(mContext, false);
assertThat(
OverlaySettingsPreferenceController.isOverlaySettingsEnabled(mContext)).isFalse();
}
}