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:
@@ -19,20 +19,18 @@ package com.android.settings.development;
|
|||||||
import static android.os.UserHandle.USER_SYSTEM;
|
import static android.os.UserHandle.USER_SYSTEM;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.om.IOverlayManager;
|
|
||||||
import android.content.om.OverlayInfo;
|
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
import android.os.RemoteException;
|
|
||||||
import android.os.ServiceManager;
|
|
||||||
import android.support.annotation.VisibleForTesting;
|
import android.support.annotation.VisibleForTesting;
|
||||||
import android.support.v7.preference.ListPreference;
|
import android.support.v7.preference.ListPreference;
|
||||||
import android.support.v7.preference.Preference;
|
import android.support.v7.preference.Preference;
|
||||||
import android.support.v7.preference.PreferenceScreen;
|
import android.support.v7.preference.PreferenceScreen;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
|
import android.view.DisplayCutout;
|
||||||
|
|
||||||
import com.android.internal.util.ArrayUtils;
|
|
||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
import com.android.settings.core.PreferenceControllerMixin;
|
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 com.android.settingslib.development.DeveloperOptionsPreferenceController;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -41,11 +39,9 @@ public class EmulateDisplayCutoutPreferenceController extends
|
|||||||
DeveloperOptionsPreferenceController implements Preference.OnPreferenceChangeListener,
|
DeveloperOptionsPreferenceController implements Preference.OnPreferenceChangeListener,
|
||||||
PreferenceControllerMixin {
|
PreferenceControllerMixin {
|
||||||
|
|
||||||
public static final String EMULATION_OVERLAY_PREFIX =
|
|
||||||
"com.android.internal.display.cutout.emulation.";
|
|
||||||
private static final String KEY = "display_cutout_emulation";
|
private static final String KEY = "display_cutout_emulation";
|
||||||
|
|
||||||
private final IOverlayManager mOverlayManager;
|
private final OverlayManagerWrapper mOverlayManager;
|
||||||
private final boolean mAvailable;
|
private final boolean mAvailable;
|
||||||
|
|
||||||
private ListPreference mPreference;
|
private ListPreference mPreference;
|
||||||
@@ -53,7 +49,7 @@ public class EmulateDisplayCutoutPreferenceController extends
|
|||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
EmulateDisplayCutoutPreferenceController(Context context, PackageManager packageManager,
|
EmulateDisplayCutoutPreferenceController(Context context, PackageManager packageManager,
|
||||||
IOverlayManager overlayManager) {
|
OverlayManagerWrapper overlayManager) {
|
||||||
super(context);
|
super(context);
|
||||||
mOverlayManager = overlayManager;
|
mOverlayManager = overlayManager;
|
||||||
mPackageManager = packageManager;
|
mPackageManager = packageManager;
|
||||||
@@ -61,8 +57,7 @@ public class EmulateDisplayCutoutPreferenceController extends
|
|||||||
}
|
}
|
||||||
|
|
||||||
public EmulateDisplayCutoutPreferenceController(Context context) {
|
public EmulateDisplayCutoutPreferenceController(Context context) {
|
||||||
this(context, context.getPackageManager(), IOverlayManager.Stub.asInterface(
|
this(context, context.getPackageManager(), new OverlayManagerWrapper());
|
||||||
ServiceManager.getService(Context.OVERLAY_SERVICE)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -93,7 +88,7 @@ public class EmulateDisplayCutoutPreferenceController extends
|
|||||||
|
|
||||||
private boolean setEmulationOverlay(String packageName) {
|
private boolean setEmulationOverlay(String packageName) {
|
||||||
OverlayInfo[] overlays = getOverlayInfos();
|
OverlayInfo[] overlays = getOverlayInfos();
|
||||||
CharSequence currentPackageName = null;
|
String currentPackageName = null;
|
||||||
for (OverlayInfo o : overlays) {
|
for (OverlayInfo o : overlays) {
|
||||||
if (o.isEnabled()) {
|
if (o.isEnabled()) {
|
||||||
currentPackageName = o.packageName;
|
currentPackageName = o.packageName;
|
||||||
@@ -106,19 +101,14 @@ public class EmulateDisplayCutoutPreferenceController extends
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (OverlayInfo o : overlays) {
|
final boolean result;
|
||||||
boolean isEnabled = o.isEnabled();
|
if (TextUtils.isEmpty(packageName)) {
|
||||||
boolean shouldBeEnabled = TextUtils.equals(o.packageName, packageName);
|
result = mOverlayManager.setEnabled(currentPackageName, false, USER_SYSTEM);
|
||||||
if (isEnabled != shouldBeEnabled) {
|
} else {
|
||||||
try {
|
result = mOverlayManager.setEnabledExclusiveInCategory(packageName, USER_SYSTEM);
|
||||||
mOverlayManager.setEnabled(o.packageName, shouldBeEnabled, USER_SYSTEM);
|
|
||||||
} catch (RemoteException e) {
|
|
||||||
throw e.rethrowFromSystemServer();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
updateState(mPreference);
|
updateState(mPreference);
|
||||||
return true;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -155,18 +145,15 @@ public class EmulateDisplayCutoutPreferenceController extends
|
|||||||
}
|
}
|
||||||
|
|
||||||
private OverlayInfo[] getOverlayInfos() {
|
private OverlayInfo[] getOverlayInfos() {
|
||||||
try {
|
@SuppressWarnings("unchecked") List<OverlayInfo> overlayInfos =
|
||||||
@SuppressWarnings("unchecked") List<OverlayInfo> overlayInfos =
|
mOverlayManager.getOverlayInfosForTarget("android", USER_SYSTEM);
|
||||||
mOverlayManager.getOverlayInfosForTarget("android", USER_SYSTEM);
|
for (int i = overlayInfos.size() - 1; i >= 0; i--) {
|
||||||
for (int i = overlayInfos.size() - 1; i >= 0; i--) {
|
if (!DisplayCutout.EMULATION_OVERLAY_CATEGORY.equals(
|
||||||
if (!overlayInfos.get(i).packageName.startsWith(EMULATION_OVERLAY_PREFIX)) {
|
overlayInfos.get(i).category)) {
|
||||||
overlayInfos.remove(i);
|
overlayInfos.remove(i);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return overlayInfos.toArray(new OverlayInfo[overlayInfos.size()]);
|
|
||||||
} catch (RemoteException e) {
|
|
||||||
throw e.rethrowFromSystemServer();
|
|
||||||
}
|
}
|
||||||
|
return overlayInfos.toArray(new OverlayInfo[overlayInfos.size()]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -180,4 +167,5 @@ public class EmulateDisplayCutoutPreferenceController extends
|
|||||||
updateState(mPreference);
|
updateState(mPreference);
|
||||||
mPreference.setEnabled(false);
|
mPreference.setEnabled(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -14,8 +14,6 @@
|
|||||||
package com.android.settings.display;
|
package com.android.settings.display;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.om.IOverlayManager;
|
|
||||||
import android.content.om.OverlayInfo;
|
|
||||||
import android.content.pm.PackageInfo;
|
import android.content.pm.PackageInfo;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
import android.content.pm.PackageManager.NameNotFoundException;
|
import android.content.pm.PackageManager.NameNotFoundException;
|
||||||
@@ -30,6 +28,8 @@ import android.text.TextUtils;
|
|||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
import com.android.settings.core.PreferenceControllerMixin;
|
import com.android.settings.core.PreferenceControllerMixin;
|
||||||
import com.android.settings.overlay.FeatureFactory;
|
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.AbstractPreferenceController;
|
||||||
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
|
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 static final String KEY_THEME = "theme";
|
||||||
|
|
||||||
private final MetricsFeatureProvider mMetricsFeatureProvider;
|
private final MetricsFeatureProvider mMetricsFeatureProvider;
|
||||||
private final OverlayManager mOverlayService;
|
private final OverlayManagerWrapper mOverlayService;
|
||||||
private final PackageManager mPackageManager;
|
private final PackageManager mPackageManager;
|
||||||
|
|
||||||
public ThemePreferenceController(Context context) {
|
public ThemePreferenceController(Context context) {
|
||||||
this(context, ServiceManager.getService(Context.OVERLAY_SERVICE) != null
|
this(context, ServiceManager.getService(Context.OVERLAY_SERVICE) != null
|
||||||
? new OverlayManager() : null);
|
? new OverlayManagerWrapper() : null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
ThemePreferenceController(Context context, OverlayManager overlayManager) {
|
ThemePreferenceController(Context context, OverlayManagerWrapper overlayManager) {
|
||||||
super(context);
|
super(context);
|
||||||
mOverlayService = overlayManager;
|
mOverlayService = overlayManager;
|
||||||
mPackageManager = context.getPackageManager();
|
mPackageManager = context.getPackageManager();
|
||||||
@@ -114,17 +114,16 @@ public class ThemePreferenceController extends AbstractPreferenceController impl
|
|||||||
if (Objects.equal(newValue, current)) {
|
if (Objects.equal(newValue, current)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
try {
|
mOverlayService.setEnabledExclusiveInCategory((String) newValue, UserHandle.myUserId());
|
||||||
mOverlayService.setEnabledExclusive((String) newValue, true, UserHandle.myUserId());
|
|
||||||
} catch (RemoteException e) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isChangeableOverlay(String packageName) {
|
private boolean isTheme(OverlayInfo oi) {
|
||||||
|
if (!OverlayInfo.CATEGORY_THEME.equals(oi.category)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
PackageInfo pi = mPackageManager.getPackageInfo(packageName, 0);
|
PackageInfo pi = mPackageManager.getPackageInfo(oi.packageName, 0);
|
||||||
return pi != null && !pi.isStaticOverlayPackage();
|
return pi != null && !pi.isStaticOverlayPackage();
|
||||||
} catch (PackageManager.NameNotFoundException e) {
|
} catch (PackageManager.NameNotFoundException e) {
|
||||||
return false;
|
return false;
|
||||||
@@ -132,16 +131,12 @@ public class ThemePreferenceController extends AbstractPreferenceController impl
|
|||||||
}
|
}
|
||||||
|
|
||||||
private String getTheme() {
|
private String getTheme() {
|
||||||
try {
|
List<OverlayInfo> infos = mOverlayService.getOverlayInfosForTarget("android",
|
||||||
List<OverlayInfo> infos = mOverlayService.getOverlayInfosForTarget("android",
|
UserHandle.myUserId());
|
||||||
UserHandle.myUserId());
|
for (int i = 0, size = infos.size(); i < size; i++) {
|
||||||
for (int i = 0, size = infos.size(); i < size; i++) {
|
if (infos.get(i).isEnabled() && isTheme(infos.get(i))) {
|
||||||
if (infos.get(i).isEnabled() &&
|
return infos.get(i).packageName;
|
||||||
isChangeableOverlay(infos.get(i).packageName)) {
|
|
||||||
return infos.get(i).packageName;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} catch (RemoteException e) {
|
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -161,37 +156,14 @@ public class ThemePreferenceController extends AbstractPreferenceController impl
|
|||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
String[] getAvailableThemes() {
|
String[] getAvailableThemes() {
|
||||||
try {
|
List<OverlayInfo> infos = mOverlayService.getOverlayInfosForTarget("android",
|
||||||
List<OverlayInfo> infos = mOverlayService.getOverlayInfosForTarget("android",
|
UserHandle.myUserId());
|
||||||
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++) {
|
||||||
for (int i = 0, size = infos.size(); i < size; i++) {
|
if (isTheme(infos.get(i))) {
|
||||||
if (isChangeableOverlay(infos.get(i).packageName)) {
|
pkgs.add(infos.get(i).packageName);
|
||||||
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()]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
102
src/com/android/settings/wrapper/OverlayManagerWrapper.java
Normal file
102
src/com/android/settings/wrapper/OverlayManagerWrapper.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -16,18 +16,19 @@ package android.content.om;
|
|||||||
|
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.List;
|
||||||
import java.util.LinkedList;
|
|
||||||
|
|
||||||
public interface IOverlayManager {
|
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) {
|
public static IOverlayManager asInterface(IBinder b) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@@ -15,14 +15,18 @@
|
|||||||
package android.content.om;
|
package android.content.om;
|
||||||
|
|
||||||
import android.annotation.NonNull;
|
import android.annotation.NonNull;
|
||||||
|
import android.annotation.Nullable;
|
||||||
|
|
||||||
public class OverlayInfo {
|
public class OverlayInfo {
|
||||||
|
|
||||||
public final String packageName;
|
public final String packageName;
|
||||||
|
|
||||||
|
public final String category;
|
||||||
|
|
||||||
public OverlayInfo(@NonNull String packageName, @NonNull String targetPackageName,
|
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.packageName = packageName;
|
||||||
|
this.category = category;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isEnabled() {
|
public boolean isEnabled() {
|
||||||
|
@@ -16,27 +16,24 @@
|
|||||||
|
|
||||||
package com.android.settings.development;
|
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 com.google.common.truth.Truth.assertThat;
|
||||||
|
|
||||||
import static org.mockito.ArgumentMatchers.any;
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
import static org.mockito.ArgumentMatchers.anyInt;
|
import static org.mockito.ArgumentMatchers.anyInt;
|
||||||
import static org.mockito.ArgumentMatchers.eq;
|
import static org.mockito.ArgumentMatchers.eq;
|
||||||
import static org.mockito.Mockito.mock;
|
|
||||||
import static org.mockito.Mockito.never;
|
import static org.mockito.Mockito.never;
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.om.IOverlayManager;
|
|
||||||
import android.content.om.OverlayInfo;
|
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
import android.support.v7.preference.ListPreference;
|
import android.support.v7.preference.ListPreference;
|
||||||
|
import android.view.DisplayCutout;
|
||||||
|
|
||||||
import com.android.settings.TestConfig;
|
import com.android.settings.TestConfig;
|
||||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
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.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@@ -51,17 +48,13 @@ import java.util.Arrays;
|
|||||||
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
||||||
public class EmulateDisplayCutoutPreferenceControllerTest {
|
public class EmulateDisplayCutoutPreferenceControllerTest {
|
||||||
|
|
||||||
static final OverlayInfo ONE_DISABLED =
|
static final OverlayInfo ONE_DISABLED = createFakeOverlay("emulation.one", false);
|
||||||
new FakeOverlay(EMULATION_OVERLAY_PREFIX + ".one", false);
|
static final OverlayInfo ONE_ENABLED = createFakeOverlay("emulation.one", true);
|
||||||
static final OverlayInfo ONE_ENABLED =
|
static final OverlayInfo TWO_DISABLED = createFakeOverlay("emulation.two", false);
|
||||||
new FakeOverlay(EMULATION_OVERLAY_PREFIX + ".one", true);
|
static final OverlayInfo TWO_ENABLED = createFakeOverlay("emulation.two", 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);
|
|
||||||
|
|
||||||
@Mock Context mContext;
|
@Mock Context mContext;
|
||||||
@Mock IOverlayManager mOverlayManager;
|
@Mock OverlayManagerWrapper mOverlayManager;
|
||||||
@Mock PackageManager mPackageManager;
|
@Mock PackageManager mPackageManager;
|
||||||
@Mock ListPreference mPreference;
|
@Mock ListPreference mPreference;
|
||||||
EmulateDisplayCutoutPreferenceController mController;
|
EmulateDisplayCutoutPreferenceController mController;
|
||||||
@@ -101,7 +94,8 @@ public class EmulateDisplayCutoutPreferenceControllerTest {
|
|||||||
|
|
||||||
mController.onPreferenceChange(null, TWO_DISABLED.packageName);
|
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
|
@Test
|
||||||
@@ -138,7 +132,7 @@ public class EmulateDisplayCutoutPreferenceControllerTest {
|
|||||||
mController.onDeveloperOptionsSwitchEnabled();
|
mController.onDeveloperOptionsSwitchEnabled();
|
||||||
|
|
||||||
verify(mPreference).setEnabled(true);
|
verify(mPreference).setEnabled(true);
|
||||||
verify(mOverlayManager, never()).setEnabled(any(), eq(true), anyInt());
|
verify(mOverlayManager, never()).setEnabledExclusiveInCategory(any(), anyInt());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -156,17 +150,7 @@ public class EmulateDisplayCutoutPreferenceControllerTest {
|
|||||||
mOverlayManager);
|
mOverlayManager);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class FakeOverlay extends OverlayInfo {
|
private static OverlayInfo createFakeOverlay(String pkg, boolean enabled) {
|
||||||
private final boolean mEnabled;
|
return new OverlayInfo(pkg, DisplayCutout.EMULATION_OVERLAY_CATEGORY, enabled);
|
||||||
|
|
||||||
public FakeOverlay(String pkg, boolean enabled) {
|
|
||||||
super(pkg, "android", "/", 0, 0);
|
|
||||||
mEnabled = enabled;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isEnabled() {
|
|
||||||
return mEnabled;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -33,9 +33,9 @@ import android.support.v7.preference.ListPreference;
|
|||||||
|
|
||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
import com.android.settings.TestConfig;
|
import com.android.settings.TestConfig;
|
||||||
import com.android.settings.display.ThemePreferenceController.OverlayManager;
|
|
||||||
import com.android.settings.testutils.FakeFeatureFactory;
|
import com.android.settings.testutils.FakeFeatureFactory;
|
||||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||||
|
import com.android.settings.wrapper.OverlayManagerWrapper;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@@ -70,7 +70,8 @@ public class ThemePreferenceControllerTest {
|
|||||||
when(mContext.getString(R.string.default_theme))
|
when(mContext.getString(R.string.default_theme))
|
||||||
.thenReturn(RuntimeEnvironment.application.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
|
@Test
|
||||||
|
@@ -35,8 +35,7 @@ import android.support.test.filters.SmallTest;
|
|||||||
import android.support.test.runner.AndroidJUnit4;
|
import android.support.test.runner.AndroidJUnit4;
|
||||||
import android.support.v7.preference.ListPreference;
|
import android.support.v7.preference.ListPreference;
|
||||||
|
|
||||||
import com.android.settings.display.ThemePreferenceController;
|
import com.android.settings.wrapper.OverlayManagerWrapper;
|
||||||
import com.android.settings.display.ThemePreferenceController.OverlayManager;
|
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@@ -49,14 +48,14 @@ import java.util.ArrayList;
|
|||||||
@RunWith(AndroidJUnit4.class)
|
@RunWith(AndroidJUnit4.class)
|
||||||
public class ThemePreferenceControllerTest {
|
public class ThemePreferenceControllerTest {
|
||||||
|
|
||||||
private OverlayManager mMockOverlayManager;
|
private OverlayManagerWrapper mMockOverlayManager;
|
||||||
private ContextWrapper mContext;
|
private ContextWrapper mContext;
|
||||||
private ThemePreferenceController mPreferenceController;
|
private ThemePreferenceController mPreferenceController;
|
||||||
private PackageManager mMockPackageManager;
|
private PackageManager mMockPackageManager;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setup() {
|
public void setup() {
|
||||||
mMockOverlayManager = mock(OverlayManager.class);
|
mMockOverlayManager = mock(OverlayManagerWrapper.class);
|
||||||
mMockPackageManager = mock(PackageManager.class);
|
mMockPackageManager = mock(PackageManager.class);
|
||||||
mContext = new ContextWrapper(InstrumentationRegistry.getTargetContext()) {
|
mContext = new ContextWrapper(InstrumentationRegistry.getTargetContext()) {
|
||||||
@Override
|
@Override
|
||||||
@@ -70,9 +69,9 @@ public class ThemePreferenceControllerTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testUpdateState() throws Exception {
|
public void testUpdateState() throws Exception {
|
||||||
OverlayInfo info1 = new OverlayInfo("com.android.Theme1", "android",
|
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 info2 = new OverlayInfo("com.android.Theme2", "android",
|
||||||
"", 0, 0);
|
"", "", 0, 0);
|
||||||
when(mMockPackageManager.getApplicationInfo(any(), anyInt())).thenAnswer(inv -> {
|
when(mMockPackageManager.getApplicationInfo(any(), anyInt())).thenAnswer(inv -> {
|
||||||
ApplicationInfo info = mock(ApplicationInfo.class);
|
ApplicationInfo info = mock(ApplicationInfo.class);
|
||||||
if ("com.android.Theme1".equals(inv.getArguments()[0])) {
|
if ("com.android.Theme1".equals(inv.getArguments()[0])) {
|
||||||
@@ -106,9 +105,9 @@ public class ThemePreferenceControllerTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testUpdateState_withStaticOverlay() throws Exception {
|
public void testUpdateState_withStaticOverlay() throws Exception {
|
||||||
OverlayInfo info1 = new OverlayInfo("com.android.Theme1", "android",
|
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 info2 = new OverlayInfo("com.android.Theme2", "android",
|
||||||
"", OverlayInfo.STATE_ENABLED, 0);
|
"", "", OverlayInfo.STATE_ENABLED, 0);
|
||||||
when(mMockPackageManager.getApplicationInfo(any(), anyInt())).thenAnswer(inv -> {
|
when(mMockPackageManager.getApplicationInfo(any(), anyInt())).thenAnswer(inv -> {
|
||||||
ApplicationInfo info = mock(ApplicationInfo.class);
|
ApplicationInfo info = mock(ApplicationInfo.class);
|
||||||
if ("com.android.Theme1".equals(inv.getArguments()[0])) {
|
if ("com.android.Theme1".equals(inv.getArguments()[0])) {
|
||||||
@@ -146,7 +145,7 @@ public class ThemePreferenceControllerTest {
|
|||||||
when(mMockPackageManager.getPackageInfo(anyString(), anyInt())).thenReturn(
|
when(mMockPackageManager.getPackageInfo(anyString(), anyInt())).thenReturn(
|
||||||
new PackageInfo());
|
new PackageInfo());
|
||||||
when(mMockOverlayManager.getOverlayInfosForTarget(any(), anyInt()))
|
when(mMockOverlayManager.getOverlayInfosForTarget(any(), anyInt()))
|
||||||
.thenReturn(list(new OverlayInfo("", "", "", 0, 0)));
|
.thenReturn(list(new OverlayInfo("", "", "", "", 0, 0)));
|
||||||
assertThat(mPreferenceController.isAvailable()).isFalse();
|
assertThat(mPreferenceController.isAvailable()).isFalse();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -155,15 +154,15 @@ public class ThemePreferenceControllerTest {
|
|||||||
when(mMockPackageManager.getPackageInfo(anyString(), anyInt())).thenReturn(
|
when(mMockPackageManager.getPackageInfo(anyString(), anyInt())).thenReturn(
|
||||||
new PackageInfo());
|
new PackageInfo());
|
||||||
when(mMockOverlayManager.getOverlayInfosForTarget(any(), anyInt()))
|
when(mMockOverlayManager.getOverlayInfosForTarget(any(), anyInt()))
|
||||||
.thenReturn(list(new OverlayInfo("", "", "", 0, 0),
|
.thenReturn(list(new OverlayInfo("", "", "", "", 0, 0),
|
||||||
new OverlayInfo("", "", "", 0, 0)));
|
new OverlayInfo("", "", "", "", 0, 0)));
|
||||||
assertThat(mPreferenceController.isAvailable()).isTrue();
|
assertThat(mPreferenceController.isAvailable()).isTrue();
|
||||||
}
|
}
|
||||||
|
|
||||||
private ArrayList<OverlayInfo> list(OverlayInfo... infos) {
|
private ArrayList<OverlayManagerWrapper.OverlayInfo> list(OverlayInfo... infos) {
|
||||||
ArrayList<OverlayInfo> list = new ArrayList<>();
|
ArrayList<OverlayManagerWrapper.OverlayInfo> list = new ArrayList<>();
|
||||||
for (int i = 0; i < infos.length; i++) {
|
for (OverlayInfo info : infos) {
|
||||||
list.add(infos[i]);
|
list.add(new OverlayManagerWrapper.OverlayInfo(info));
|
||||||
}
|
}
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user