Use new OverlayManager categories

Using this, we split overlays based on their category, so that
cutout emulation and theme overlays no longer clash in their
respective settings.

Bug: 72436677
Test: make ROBOTEST_FILTER=EmulateDisplayCutoutPreferenceControllerTest RunSettingsRoboTests
Change-Id: I23f22113351b3948beb9e3a1fb969700852539cc
This commit is contained in:
Adrian Roos
2018-01-24 16:36:38 +01:00
parent 1bbe424e6e
commit 807cf8d2c5
8 changed files with 188 additions and 137 deletions

View File

@@ -19,20 +19,18 @@ package com.android.settings.development;
import static android.os.UserHandle.USER_SYSTEM;
import android.content.Context;
import android.content.om.IOverlayManager;
import android.content.om.OverlayInfo;
import android.content.pm.PackageManager;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.support.annotation.VisibleForTesting;
import android.support.v7.preference.ListPreference;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import android.text.TextUtils;
import android.view.DisplayCutout;
import com.android.internal.util.ArrayUtils;
import com.android.settings.R;
import com.android.settings.core.PreferenceControllerMixin;
import com.android.settings.wrapper.OverlayManagerWrapper;
import com.android.settings.wrapper.OverlayManagerWrapper.OverlayInfo;
import com.android.settingslib.development.DeveloperOptionsPreferenceController;
import java.util.List;
@@ -41,11 +39,9 @@ public class EmulateDisplayCutoutPreferenceController extends
DeveloperOptionsPreferenceController implements Preference.OnPreferenceChangeListener,
PreferenceControllerMixin {
public static final String EMULATION_OVERLAY_PREFIX =
"com.android.internal.display.cutout.emulation.";
private static final String KEY = "display_cutout_emulation";
private final IOverlayManager mOverlayManager;
private final OverlayManagerWrapper mOverlayManager;
private final boolean mAvailable;
private ListPreference mPreference;
@@ -53,7 +49,7 @@ public class EmulateDisplayCutoutPreferenceController extends
@VisibleForTesting
EmulateDisplayCutoutPreferenceController(Context context, PackageManager packageManager,
IOverlayManager overlayManager) {
OverlayManagerWrapper overlayManager) {
super(context);
mOverlayManager = overlayManager;
mPackageManager = packageManager;
@@ -61,8 +57,7 @@ public class EmulateDisplayCutoutPreferenceController extends
}
public EmulateDisplayCutoutPreferenceController(Context context) {
this(context, context.getPackageManager(), IOverlayManager.Stub.asInterface(
ServiceManager.getService(Context.OVERLAY_SERVICE)));
this(context, context.getPackageManager(), new OverlayManagerWrapper());
}
@Override
@@ -93,7 +88,7 @@ public class EmulateDisplayCutoutPreferenceController extends
private boolean setEmulationOverlay(String packageName) {
OverlayInfo[] overlays = getOverlayInfos();
CharSequence currentPackageName = null;
String currentPackageName = null;
for (OverlayInfo o : overlays) {
if (o.isEnabled()) {
currentPackageName = o.packageName;
@@ -106,19 +101,14 @@ public class EmulateDisplayCutoutPreferenceController extends
return true;
}
for (OverlayInfo o : overlays) {
boolean isEnabled = o.isEnabled();
boolean shouldBeEnabled = TextUtils.equals(o.packageName, packageName);
if (isEnabled != shouldBeEnabled) {
try {
mOverlayManager.setEnabled(o.packageName, shouldBeEnabled, USER_SYSTEM);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
final boolean result;
if (TextUtils.isEmpty(packageName)) {
result = mOverlayManager.setEnabled(currentPackageName, false, USER_SYSTEM);
} else {
result = mOverlayManager.setEnabledExclusiveInCategory(packageName, USER_SYSTEM);
}
updateState(mPreference);
return true;
return result;
}
@Override
@@ -155,18 +145,15 @@ public class EmulateDisplayCutoutPreferenceController extends
}
private OverlayInfo[] getOverlayInfos() {
try {
@SuppressWarnings("unchecked") List<OverlayInfo> overlayInfos =
mOverlayManager.getOverlayInfosForTarget("android", USER_SYSTEM);
for (int i = overlayInfos.size() - 1; i >= 0; i--) {
if (!overlayInfos.get(i).packageName.startsWith(EMULATION_OVERLAY_PREFIX)) {
if (!DisplayCutout.EMULATION_OVERLAY_CATEGORY.equals(
overlayInfos.get(i).category)) {
overlayInfos.remove(i);
}
}
return overlayInfos.toArray(new OverlayInfo[overlayInfos.size()]);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
@Override
@@ -180,4 +167,5 @@ public class EmulateDisplayCutoutPreferenceController extends
updateState(mPreference);
mPreference.setEnabled(false);
}
}

View File

@@ -14,8 +14,6 @@
package com.android.settings.display;
import android.content.Context;
import android.content.om.IOverlayManager;
import android.content.om.OverlayInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
@@ -30,6 +28,8 @@ import android.text.TextUtils;
import com.android.settings.R;
import com.android.settings.core.PreferenceControllerMixin;
import com.android.settings.overlay.FeatureFactory;
import com.android.settings.wrapper.OverlayManagerWrapper;
import com.android.settings.wrapper.OverlayManagerWrapper.OverlayInfo;
import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
@@ -46,16 +46,16 @@ public class ThemePreferenceController extends AbstractPreferenceController impl
private static final String KEY_THEME = "theme";
private final MetricsFeatureProvider mMetricsFeatureProvider;
private final OverlayManager mOverlayService;
private final OverlayManagerWrapper mOverlayService;
private final PackageManager mPackageManager;
public ThemePreferenceController(Context context) {
this(context, ServiceManager.getService(Context.OVERLAY_SERVICE) != null
? new OverlayManager() : null);
? new OverlayManagerWrapper() : null);
}
@VisibleForTesting
ThemePreferenceController(Context context, OverlayManager overlayManager) {
ThemePreferenceController(Context context, OverlayManagerWrapper overlayManager) {
super(context);
mOverlayService = overlayManager;
mPackageManager = context.getPackageManager();
@@ -114,17 +114,16 @@ public class ThemePreferenceController extends AbstractPreferenceController impl
if (Objects.equal(newValue, current)) {
return true;
}
try {
mOverlayService.setEnabledExclusive((String) newValue, true, UserHandle.myUserId());
} catch (RemoteException e) {
return false;
}
mOverlayService.setEnabledExclusiveInCategory((String) newValue, UserHandle.myUserId());
return true;
}
private boolean isChangeableOverlay(String packageName) {
private boolean isTheme(OverlayInfo oi) {
if (!OverlayInfo.CATEGORY_THEME.equals(oi.category)) {
return false;
}
try {
PackageInfo pi = mPackageManager.getPackageInfo(packageName, 0);
PackageInfo pi = mPackageManager.getPackageInfo(oi.packageName, 0);
return pi != null && !pi.isStaticOverlayPackage();
} catch (PackageManager.NameNotFoundException e) {
return false;
@@ -132,17 +131,13 @@ public class ThemePreferenceController extends AbstractPreferenceController impl
}
private String getTheme() {
try {
List<OverlayInfo> infos = mOverlayService.getOverlayInfosForTarget("android",
UserHandle.myUserId());
for (int i = 0, size = infos.size(); i < size; i++) {
if (infos.get(i).isEnabled() &&
isChangeableOverlay(infos.get(i).packageName)) {
if (infos.get(i).isEnabled() && isTheme(infos.get(i))) {
return infos.get(i).packageName;
}
}
} catch (RemoteException e) {
}
return null;
}
@@ -161,37 +156,14 @@ public class ThemePreferenceController extends AbstractPreferenceController impl
@VisibleForTesting
String[] getAvailableThemes() {
try {
List<OverlayInfo> infos = mOverlayService.getOverlayInfosForTarget("android",
UserHandle.myUserId());
List<String> pkgs = new ArrayList(infos.size());
List<String> pkgs = new ArrayList<>(infos.size());
for (int i = 0, size = infos.size(); i < size; i++) {
if (isChangeableOverlay(infos.get(i).packageName)) {
if (isTheme(infos.get(i))) {
pkgs.add(infos.get(i).packageName);
}
}
return pkgs.toArray(new String[pkgs.size()]);
} catch (RemoteException e) {
}
return new String[0];
}
public static class OverlayManager {
private final IOverlayManager mService;
public OverlayManager() {
mService = IOverlayManager.Stub.asInterface(
ServiceManager.getService(Context.OVERLAY_SERVICE));
}
public void setEnabledExclusive(String pkg, boolean enabled, int userId)
throws RemoteException {
mService.setEnabledExclusive(pkg, enabled, userId);
}
public List<OverlayInfo> getOverlayInfosForTarget(String target, int userId)
throws RemoteException {
return mService.getOverlayInfosForTarget(target, userId);
}
}
}

View File

@@ -0,0 +1,102 @@
/*
* Copyright (C) 2018 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.wrapper;
import android.content.Context;
import android.content.om.IOverlayManager;
import android.content.om.OverlayInfo;
import android.os.RemoteException;
import android.os.ServiceManager;
import java.util.ArrayList;
import java.util.List;
public class OverlayManagerWrapper {
private final IOverlayManager mOverlayManager;
public OverlayManagerWrapper(IOverlayManager overlayManager) {
mOverlayManager = overlayManager;
}
public OverlayManagerWrapper() {
this(IOverlayManager.Stub.asInterface(ServiceManager.getService(Context.OVERLAY_SERVICE)));
}
public List<OverlayInfo> getOverlayInfosForTarget(String overlay, int userId) {
if (mOverlayManager == null) {
return new ArrayList<>();
}
try {
List<android.content.om.OverlayInfo> infos
= mOverlayManager.getOverlayInfosForTarget(overlay, userId);
ArrayList<OverlayInfo> result = new ArrayList<>(infos.size());
for (int i = 0; i < infos.size(); i++) {
result.add(new OverlayInfo(infos.get(i)));
}
return result;
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
public boolean setEnabled(String overlay, boolean enabled, int userId) {
if (mOverlayManager == null) {
return false;
}
try {
return mOverlayManager.setEnabled(overlay, enabled, userId);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
public boolean setEnabledExclusiveInCategory(String overlay, int userId) {
if (mOverlayManager == null) {
return false;
}
try {
return mOverlayManager.setEnabledExclusiveInCategory(overlay, userId);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
public static class OverlayInfo {
public static final String CATEGORY_THEME = android.content.om.OverlayInfo.CATEGORY_THEME;
public final String packageName;
public final String category;
private final boolean mEnabled;
public OverlayInfo(String packageName, String category, boolean enabled) {
this.packageName = packageName;
this.category = category;
mEnabled = enabled;
}
public OverlayInfo(android.content.om.OverlayInfo info) {
mEnabled = info.isEnabled();
category = info.category;
packageName = info.packageName;
}
public boolean isEnabled() {
return mEnabled;
}
}
}

View File

@@ -16,18 +16,19 @@ package android.content.om;
import android.os.IBinder;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public interface IOverlayManager {
public OverlayInfo getOverlayInfo(String packageName, int userId);
OverlayInfo getOverlayInfo(String packageName, int userId);
public java.util.List getOverlayInfosForTarget(java.lang.String targetPackageName, int userId);
List getOverlayInfosForTarget(String targetPackageName, int userId);
public boolean setEnabled(java.lang.String packageName, boolean enable, int userId);
boolean setEnabled(String packageName, boolean enabled, int userId);
public static class Stub {
boolean setEnabledExclusiveInCategory(String packageName, int userId);
class Stub {
public static IOverlayManager asInterface(IBinder b) {
return null;
}

View File

@@ -15,14 +15,18 @@
package android.content.om;
import android.annotation.NonNull;
import android.annotation.Nullable;
public class OverlayInfo {
public final String packageName;
public final String category;
public OverlayInfo(@NonNull String packageName, @NonNull String targetPackageName,
@NonNull String baseCodePath, int state, int userId) {
@Nullable String category, @NonNull String baseCodePath, int state, int userId) {
this.packageName = packageName;
this.category = category;
}
public boolean isEnabled() {

View File

@@ -16,27 +16,24 @@
package com.android.settings.development;
import static com.android.settings.development.EmulateDisplayCutoutPreferenceController
.EMULATION_OVERLAY_PREFIX;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.om.IOverlayManager;
import android.content.om.OverlayInfo;
import android.content.pm.PackageManager;
import android.support.v7.preference.ListPreference;
import android.view.DisplayCutout;
import com.android.settings.TestConfig;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.wrapper.OverlayManagerWrapper;
import com.android.settings.wrapper.OverlayManagerWrapper.OverlayInfo;
import org.junit.Before;
import org.junit.Test;
@@ -51,17 +48,13 @@ import java.util.Arrays;
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class EmulateDisplayCutoutPreferenceControllerTest {
static final OverlayInfo ONE_DISABLED =
new FakeOverlay(EMULATION_OVERLAY_PREFIX + ".one", false);
static final OverlayInfo ONE_ENABLED =
new FakeOverlay(EMULATION_OVERLAY_PREFIX + ".one", true);
static final OverlayInfo TWO_DISABLED =
new FakeOverlay(EMULATION_OVERLAY_PREFIX + ".two", false);
static final OverlayInfo TWO_ENABLED =
new FakeOverlay(EMULATION_OVERLAY_PREFIX + ".two", true);
static final OverlayInfo ONE_DISABLED = createFakeOverlay("emulation.one", false);
static final OverlayInfo ONE_ENABLED = createFakeOverlay("emulation.one", true);
static final OverlayInfo TWO_DISABLED = createFakeOverlay("emulation.two", false);
static final OverlayInfo TWO_ENABLED = createFakeOverlay("emulation.two", true);
@Mock Context mContext;
@Mock IOverlayManager mOverlayManager;
@Mock OverlayManagerWrapper mOverlayManager;
@Mock PackageManager mPackageManager;
@Mock ListPreference mPreference;
EmulateDisplayCutoutPreferenceController mController;
@@ -101,7 +94,8 @@ public class EmulateDisplayCutoutPreferenceControllerTest {
mController.onPreferenceChange(null, TWO_DISABLED.packageName);
verify(mOverlayManager).setEnabled(eq(TWO_DISABLED.packageName), eq(true), anyInt());
verify(mOverlayManager).setEnabledExclusiveInCategory(
eq(TWO_DISABLED.packageName), anyInt());
}
@Test
@@ -138,7 +132,7 @@ public class EmulateDisplayCutoutPreferenceControllerTest {
mController.onDeveloperOptionsSwitchEnabled();
verify(mPreference).setEnabled(true);
verify(mOverlayManager, never()).setEnabled(any(), eq(true), anyInt());
verify(mOverlayManager, never()).setEnabledExclusiveInCategory(any(), anyInt());
}
@Test
@@ -156,17 +150,7 @@ public class EmulateDisplayCutoutPreferenceControllerTest {
mOverlayManager);
}
private static class FakeOverlay extends OverlayInfo {
private final boolean mEnabled;
public FakeOverlay(String pkg, boolean enabled) {
super(pkg, "android", "/", 0, 0);
mEnabled = enabled;
}
@Override
public boolean isEnabled() {
return mEnabled;
}
private static OverlayInfo createFakeOverlay(String pkg, boolean enabled) {
return new OverlayInfo(pkg, DisplayCutout.EMULATION_OVERLAY_CATEGORY, enabled);
}
}

View File

@@ -33,9 +33,9 @@ import android.support.v7.preference.ListPreference;
import com.android.settings.R;
import com.android.settings.TestConfig;
import com.android.settings.display.ThemePreferenceController.OverlayManager;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.wrapper.OverlayManagerWrapper;
import org.junit.Before;
import org.junit.Test;
@@ -70,7 +70,8 @@ public class ThemePreferenceControllerTest {
when(mContext.getString(R.string.default_theme))
.thenReturn(RuntimeEnvironment.application.getString(R.string.default_theme));
mController = spy(new ThemePreferenceController(mContext, mock(OverlayManager.class)));
mController = spy(new ThemePreferenceController(mContext,
mock(OverlayManagerWrapper.class)));
}
@Test

View File

@@ -35,8 +35,7 @@ import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4;
import android.support.v7.preference.ListPreference;
import com.android.settings.display.ThemePreferenceController;
import com.android.settings.display.ThemePreferenceController.OverlayManager;
import com.android.settings.wrapper.OverlayManagerWrapper;
import org.junit.Before;
import org.junit.Test;
@@ -49,14 +48,14 @@ import java.util.ArrayList;
@RunWith(AndroidJUnit4.class)
public class ThemePreferenceControllerTest {
private OverlayManager mMockOverlayManager;
private OverlayManagerWrapper mMockOverlayManager;
private ContextWrapper mContext;
private ThemePreferenceController mPreferenceController;
private PackageManager mMockPackageManager;
@Before
public void setup() {
mMockOverlayManager = mock(OverlayManager.class);
mMockOverlayManager = mock(OverlayManagerWrapper.class);
mMockPackageManager = mock(PackageManager.class);
mContext = new ContextWrapper(InstrumentationRegistry.getTargetContext()) {
@Override
@@ -70,9 +69,9 @@ public class ThemePreferenceControllerTest {
@Test
public void testUpdateState() throws Exception {
OverlayInfo info1 = new OverlayInfo("com.android.Theme1", "android",
"", OverlayInfo.STATE_ENABLED, 0);
"", "", OverlayInfo.STATE_ENABLED, 0);
OverlayInfo info2 = new OverlayInfo("com.android.Theme2", "android",
"", 0, 0);
"", "", 0, 0);
when(mMockPackageManager.getApplicationInfo(any(), anyInt())).thenAnswer(inv -> {
ApplicationInfo info = mock(ApplicationInfo.class);
if ("com.android.Theme1".equals(inv.getArguments()[0])) {
@@ -106,9 +105,9 @@ public class ThemePreferenceControllerTest {
@Test
public void testUpdateState_withStaticOverlay() throws Exception {
OverlayInfo info1 = new OverlayInfo("com.android.Theme1", "android",
"", OverlayInfo.STATE_ENABLED, 0);
"", "", OverlayInfo.STATE_ENABLED, 0);
OverlayInfo info2 = new OverlayInfo("com.android.Theme2", "android",
"", OverlayInfo.STATE_ENABLED, 0);
"", "", OverlayInfo.STATE_ENABLED, 0);
when(mMockPackageManager.getApplicationInfo(any(), anyInt())).thenAnswer(inv -> {
ApplicationInfo info = mock(ApplicationInfo.class);
if ("com.android.Theme1".equals(inv.getArguments()[0])) {
@@ -146,7 +145,7 @@ public class ThemePreferenceControllerTest {
when(mMockPackageManager.getPackageInfo(anyString(), anyInt())).thenReturn(
new PackageInfo());
when(mMockOverlayManager.getOverlayInfosForTarget(any(), anyInt()))
.thenReturn(list(new OverlayInfo("", "", "", 0, 0)));
.thenReturn(list(new OverlayInfo("", "", "", "", 0, 0)));
assertThat(mPreferenceController.isAvailable()).isFalse();
}
@@ -155,15 +154,15 @@ public class ThemePreferenceControllerTest {
when(mMockPackageManager.getPackageInfo(anyString(), anyInt())).thenReturn(
new PackageInfo());
when(mMockOverlayManager.getOverlayInfosForTarget(any(), anyInt()))
.thenReturn(list(new OverlayInfo("", "", "", 0, 0),
new OverlayInfo("", "", "", 0, 0)));
.thenReturn(list(new OverlayInfo("", "", "", "", 0, 0),
new OverlayInfo("", "", "", "", 0, 0)));
assertThat(mPreferenceController.isAvailable()).isTrue();
}
private ArrayList<OverlayInfo> list(OverlayInfo... infos) {
ArrayList<OverlayInfo> list = new ArrayList<>();
for (int i = 0; i < infos.length; i++) {
list.add(infos[i]);
private ArrayList<OverlayManagerWrapper.OverlayInfo> list(OverlayInfo... infos) {
ArrayList<OverlayManagerWrapper.OverlayInfo> list = new ArrayList<>();
for (OverlayInfo info : infos) {
list.add(new OverlayManagerWrapper.OverlayInfo(info));
}
return list;
}