Merge "Use new OverlayManager categories"
This commit is contained in:
@@ -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)) {
|
||||
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
|
||||
@@ -180,4 +167,5 @@ public class EmulateDisplayCutoutPreferenceController extends
|
||||
updateState(mPreference);
|
||||
mPreference.setEnabled(false);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -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,16 +131,12 @@ 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)) {
|
||||
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;
|
||||
}
|
||||
@@ -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());
|
||||
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()]);
|
||||
}
|
||||
}
|
||||
|
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;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user