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;
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;
@VisibleForTesting
EmulateDisplayCutoutPreferenceController(Context context, PackageManager packageManager,
- IOverlayManager overlayManager) {
+ OverlayManagerWrapper overlayManager) {
super(context);
mOverlayManager = overlayManager;
mPackageManager = packageManager;
}
public EmulateDisplayCutoutPreferenceController(Context context) {
- this(context, context.getPackageManager(), IOverlayManager.Stub.asInterface(
- ServiceManager.getService(Context.OVERLAY_SERVICE)));
+ this(context, context.getPackageManager(), new OverlayManagerWrapper());
}
@Override
private boolean setEmulationOverlay(String packageName) {
OverlayInfo[] overlays = getOverlayInfos();
- CharSequence currentPackageName = null;
+ String currentPackageName = null;
for (OverlayInfo o : overlays) {
if (o.isEnabled()) {
currentPackageName = o.packageName;
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
}
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)) {
- overlayInfos.remove(i);
- }
+ @SuppressWarnings("unchecked") List<OverlayInfo> overlayInfos =
+ mOverlayManager.getOverlayInfosForTarget("android", USER_SYSTEM);
+ for (int i = overlayInfos.size() - 1; i >= 0; i--) {
+ 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();
}
+ return overlayInfos.toArray(new OverlayInfo[overlayInfos.size()]);
}
@Override
updateState(mPreference);
mPreference.setEnabled(false);
}
+
}
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;
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;
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();
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;
}
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)) {
- return infos.get(i).packageName;
- }
+ List<OverlayInfo> infos = mOverlayService.getOverlayInfosForTarget("android",
+ UserHandle.myUserId());
+ for (int i = 0, size = infos.size(); i < size; i++) {
+ if (infos.get(i).isEnabled() && isTheme(infos.get(i))) {
+ return infos.get(i).packageName;
}
- } catch (RemoteException e) {
}
return null;
}
@VisibleForTesting
String[] getAvailableThemes() {
- try {
- List<OverlayInfo> infos = mOverlayService.getOverlayInfosForTarget("android",
- UserHandle.myUserId());
- List<String> pkgs = new ArrayList(infos.size());
- for (int i = 0, size = infos.size(); i < size; i++) {
- if (isChangeableOverlay(infos.get(i).packageName)) {
- pkgs.add(infos.get(i).packageName);
- }
+ List<OverlayInfo> infos = mOverlayService.getOverlayInfosForTarget("android",
+ UserHandle.myUserId());
+ List<String> pkgs = new ArrayList<>(infos.size());
+ for (int i = 0, size = infos.size(); i < size; i++) {
+ 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);
}
+ return pkgs.toArray(new String[pkgs.size()]);
}
}
--- /dev/null
+/*
+ * 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;
+ }
+ }
+}
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;
}
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() {
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;
@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;
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
mController.onDeveloperOptionsSwitchEnabled();
verify(mPreference).setEnabled(true);
- verify(mOverlayManager, never()).setEnabled(any(), eq(true), anyInt());
+ verify(mOverlayManager, never()).setEnabledExclusiveInCategory(any(), anyInt());
}
@Test
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);
}
}
\ No newline at end of file
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;
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
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;
@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
@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])) {
@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])) {
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();
}
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;
}