Merge "Add developer settings for theme overlays."
This commit is contained in:
committed by
Android (Google) Code Review
commit
44ad7fc002
@@ -9507,14 +9507,25 @@
|
|||||||
<!-- Notification log debug tool: the word 'none' -->
|
<!-- Notification log debug tool: the word 'none' -->
|
||||||
<string name="notification_log_details_ranking_none">Ranking object doesn\'t contain this key.</string>
|
<string name="notification_log_details_ranking_none">Ranking object doesn\'t contain this key.</string>
|
||||||
|
|
||||||
|
<!-- [CHAR_LIMIT=NONE] Developer Settings: Title of the settings category for theme overlays. -->
|
||||||
|
<string name="theme_customization_category">Theming</string>
|
||||||
|
<!-- [CHAR_LIMIT=NONE] Developer Settings: Title of the setting which enables overlays to customize accent color. -->
|
||||||
|
<string name="theme_customization_accent_color_title">Accent color</string>
|
||||||
|
<!-- [CHAR_LIMIT=NONE] Developer Settings: Title of the setting which enables overlays to customize headline and body fonts. -->
|
||||||
|
<string name="theme_customization_font_title">Headline / Body font</string>
|
||||||
|
<!-- [CHAR_LIMIT=NONE] Developer Settings: Title of the setting which enables overlays to customize the adaptive icon shape (e.g. launcher and quick settings icons). -->
|
||||||
|
<string name="theme_customization_icon_shape_title">Icon shape</string>
|
||||||
|
<!-- [CHAR_LIMIT=NONE] Developer Settings: Label for the option that turns off customizations for a given category.-->
|
||||||
|
<string name="theme_customization_device_default">Device default</string>
|
||||||
|
|
||||||
<!-- [CHAR_LIMIT=NONE] Developer Settings: Title of the setting which turns on emulation of a display cutout. -->
|
<!-- [CHAR_LIMIT=NONE] Developer Settings: Title of the setting which turns on emulation of a display cutout. -->
|
||||||
<string name="display_cutout_emulation">Display cutout</string>
|
<string name="display_cutout_emulation">Display cutout</string>
|
||||||
|
|
||||||
<!-- [CHAR_LIMIT=NONE] Developer Settings: Search keywords for the setting which turns on emulation of a display cutout. -->
|
<!-- [CHAR_LIMIT=NONE] Developer Settings: Search keywords for the setting which turns on emulation of a display cutout. -->
|
||||||
<string name="display_cutout_emulation_keywords">display cutout, notch</string>
|
<string name="display_cutout_emulation_keywords">display cutout, notch</string>
|
||||||
|
|
||||||
<!-- [CHAR_LIMIT=NONE] Developer Settings: Label for the option that turns off display cutout emulation, (i.e. on devices whose screen actually has a cutout, selecting this option will show that cutout).-->
|
<!-- [CHAR_LIMIT=NONE] Developer Settings: Label for the option that turns off all overlays in a given category. -->
|
||||||
<string name="display_cutout_emulation_device_default">Device default</string>
|
<string name="overlay_option_device_default">Device default</string>
|
||||||
|
|
||||||
<!-- [CHAR_LIMIT=60] Label for special access screen -->
|
<!-- [CHAR_LIMIT=60] Label for special access screen -->
|
||||||
<string name="special_access">Special app access</string>
|
<string name="special_access">Special app access</string>
|
||||||
|
@@ -559,4 +559,19 @@
|
|||||||
|
|
||||||
</com.android.settings.development.autofill.AutofillPreferenceCategory>
|
</com.android.settings.development.autofill.AutofillPreferenceCategory>
|
||||||
|
|
||||||
|
<PreferenceCategory
|
||||||
|
android:key="theme_customization_category"
|
||||||
|
android:title="@string/theme_customization_category"
|
||||||
|
android:order="1200"> <!-- Incremented by 100 from last order (i.e. 1100) -->
|
||||||
|
|
||||||
|
<ListPreference
|
||||||
|
android:key="android.theme.customization.accent_color"
|
||||||
|
android:title="@string/theme_customization_accent_color_title"/>
|
||||||
|
<ListPreference
|
||||||
|
android:key="android.theme.customization.font"
|
||||||
|
android:title="@string/theme_customization_font_title"/>
|
||||||
|
<ListPreference
|
||||||
|
android:key="android.theme.customization.adaptive_icon_shape"
|
||||||
|
android:title="@string/theme_customization_icon_shape_title"/>
|
||||||
|
</PreferenceCategory>
|
||||||
</PreferenceScreen>
|
</PreferenceScreen>
|
||||||
|
@@ -492,6 +492,12 @@ public class DevelopmentSettingsDashboardFragment extends RestrictedDashboardFra
|
|||||||
controllers.add(new DefaultLaunchPreferenceController(context, "inactive_apps"));
|
controllers.add(new DefaultLaunchPreferenceController(context, "inactive_apps"));
|
||||||
controllers.add(new AutofillLoggingLevelPreferenceController(context, lifecycle));
|
controllers.add(new AutofillLoggingLevelPreferenceController(context, lifecycle));
|
||||||
controllers.add(new AutofillResetOptionsPreferenceController(context));
|
controllers.add(new AutofillResetOptionsPreferenceController(context));
|
||||||
|
controllers.add(new OverlayCategoryPreferenceController(context,
|
||||||
|
"android.theme.customization.accent_color"));
|
||||||
|
controllers.add(new OverlayCategoryPreferenceController(context,
|
||||||
|
"android.theme.customization.font"));
|
||||||
|
controllers.add(new OverlayCategoryPreferenceController(context,
|
||||||
|
"android.theme.customization.adaptive_icon_shape"));
|
||||||
return controllers;
|
return controllers;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -16,50 +16,22 @@
|
|||||||
|
|
||||||
package com.android.settings.development;
|
package com.android.settings.development;
|
||||||
|
|
||||||
import static android.os.UserHandle.USER_SYSTEM;
|
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.om.IOverlayManager;
|
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.os.ServiceManager;
|
||||||
import android.text.TextUtils;
|
|
||||||
import android.view.DisplayCutout;
|
import android.view.DisplayCutout;
|
||||||
|
|
||||||
import androidx.annotation.VisibleForTesting;
|
import androidx.annotation.VisibleForTesting;
|
||||||
import androidx.preference.ListPreference;
|
|
||||||
import androidx.preference.Preference;
|
|
||||||
import androidx.preference.PreferenceScreen;
|
|
||||||
|
|
||||||
import com.android.settings.R;
|
public class EmulateDisplayCutoutPreferenceController extends OverlayCategoryPreferenceController {
|
||||||
import com.android.settings.core.PreferenceControllerMixin;
|
|
||||||
import com.android.settingslib.development.DeveloperOptionsPreferenceController;
|
|
||||||
|
|
||||||
import java.util.Comparator;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class EmulateDisplayCutoutPreferenceController extends
|
|
||||||
DeveloperOptionsPreferenceController implements Preference.OnPreferenceChangeListener,
|
|
||||||
PreferenceControllerMixin {
|
|
||||||
|
|
||||||
private static final String KEY = "display_cutout_emulation";
|
private static final String KEY = "display_cutout_emulation";
|
||||||
private static final Comparator<OverlayInfo> OVERLAY_INFO_COMPARATOR =
|
|
||||||
Comparator.comparingInt(a -> a.priority);
|
|
||||||
|
|
||||||
private final IOverlayManager mOverlayManager;
|
|
||||||
private final boolean mAvailable;
|
|
||||||
|
|
||||||
private ListPreference mPreference;
|
|
||||||
private PackageManager mPackageManager;
|
|
||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
EmulateDisplayCutoutPreferenceController(Context context, PackageManager packageManager,
|
EmulateDisplayCutoutPreferenceController(Context context, PackageManager packageManager,
|
||||||
IOverlayManager overlayManager) {
|
IOverlayManager overlayManager) {
|
||||||
super(context);
|
super(context, packageManager, overlayManager, DisplayCutout.EMULATION_OVERLAY_CATEGORY);
|
||||||
mOverlayManager = overlayManager;
|
|
||||||
mPackageManager = packageManager;
|
|
||||||
mAvailable = overlayManager != null && getOverlayInfos().length > 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public EmulateDisplayCutoutPreferenceController(Context context) {
|
public EmulateDisplayCutoutPreferenceController(Context context) {
|
||||||
@@ -67,116 +39,8 @@ public class EmulateDisplayCutoutPreferenceController extends
|
|||||||
.asInterface(ServiceManager.getService(Context.OVERLAY_SERVICE)));
|
.asInterface(ServiceManager.getService(Context.OVERLAY_SERVICE)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isAvailable() {
|
|
||||||
return mAvailable;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getPreferenceKey() {
|
public String getPreferenceKey() {
|
||||||
return KEY;
|
return KEY;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void displayPreference(PreferenceScreen screen) {
|
|
||||||
super.displayPreference(screen);
|
|
||||||
setPreference((ListPreference) screen.findPreference(getPreferenceKey()));
|
|
||||||
}
|
|
||||||
|
|
||||||
@VisibleForTesting
|
|
||||||
void setPreference(ListPreference preference) {
|
|
||||||
mPreference = preference;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean onPreferenceChange(Preference preference, Object newValue) {
|
|
||||||
return setEmulationOverlay((String) newValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean setEmulationOverlay(String packageName) {
|
|
||||||
OverlayInfo[] overlays = getOverlayInfos();
|
|
||||||
String currentPackageName = null;
|
|
||||||
for (OverlayInfo o : overlays) {
|
|
||||||
if (o.isEnabled()) {
|
|
||||||
currentPackageName = o.packageName;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (TextUtils.isEmpty(packageName) && TextUtils.isEmpty(currentPackageName)
|
|
||||||
|| TextUtils.equals(packageName, currentPackageName)) {
|
|
||||||
// Already set.
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
final boolean result;
|
|
||||||
try {
|
|
||||||
if (TextUtils.isEmpty(packageName)) {
|
|
||||||
result = mOverlayManager.setEnabled(currentPackageName, false, USER_SYSTEM);
|
|
||||||
} else {
|
|
||||||
result = mOverlayManager.setEnabledExclusiveInCategory(packageName, USER_SYSTEM);
|
|
||||||
}
|
|
||||||
} catch (RemoteException re) {
|
|
||||||
throw re.rethrowFromSystemServer();
|
|
||||||
}
|
|
||||||
updateState(mPreference);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void updateState(Preference preference) {
|
|
||||||
OverlayInfo[] overlays = getOverlayInfos();
|
|
||||||
|
|
||||||
CharSequence[] pkgs = new CharSequence[overlays.length + 1];
|
|
||||||
CharSequence[] labels = new CharSequence[pkgs.length];
|
|
||||||
|
|
||||||
int current = 0;
|
|
||||||
pkgs[0] = "";
|
|
||||||
labels[0] = mContext.getString(R.string.display_cutout_emulation_device_default);
|
|
||||||
|
|
||||||
for (int i = 0; i < overlays.length; i++) {
|
|
||||||
OverlayInfo o = overlays[i];
|
|
||||||
pkgs[i+1] = o.packageName;
|
|
||||||
if (o.isEnabled()) {
|
|
||||||
current = i+1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (int i = 1; i < pkgs.length; i++) {
|
|
||||||
try {
|
|
||||||
labels[i] = mPackageManager.getApplicationInfo(pkgs[i].toString(), 0)
|
|
||||||
.loadLabel(mPackageManager);
|
|
||||||
} catch (PackageManager.NameNotFoundException e) {
|
|
||||||
labels[i] = pkgs[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
mPreference.setEntries(labels);
|
|
||||||
mPreference.setEntryValues(pkgs);
|
|
||||||
mPreference.setValueIndex(current);
|
|
||||||
mPreference.setSummary(labels[current]);
|
|
||||||
}
|
|
||||||
|
|
||||||
private OverlayInfo[] getOverlayInfos() {
|
|
||||||
List<OverlayInfo> overlayInfos;
|
|
||||||
try {
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (RemoteException re) {
|
|
||||||
throw re.rethrowFromSystemServer();
|
|
||||||
}
|
|
||||||
overlayInfos.sort(OVERLAY_INFO_COMPARATOR);
|
|
||||||
return overlayInfos.toArray(new OverlayInfo[overlayInfos.size()]);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onDeveloperOptionsSwitchDisabled() {
|
|
||||||
super.onDeveloperOptionsSwitchDisabled();
|
|
||||||
setEmulationOverlay("");
|
|
||||||
updateState(mPreference);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -0,0 +1,188 @@
|
|||||||
|
/*
|
||||||
|
* 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.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.text.TextUtils;
|
||||||
|
|
||||||
|
import com.android.settings.R;
|
||||||
|
import com.android.settings.core.PreferenceControllerMixin;
|
||||||
|
import com.android.settingslib.development.DeveloperOptionsPreferenceController;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import androidx.annotation.VisibleForTesting;
|
||||||
|
import androidx.preference.ListPreference;
|
||||||
|
import androidx.preference.Preference;
|
||||||
|
import androidx.preference.PreferenceScreen;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Preference controller to allow users to choose an overlay from a list for a given category.
|
||||||
|
* The chosen overlay is enabled exclusively within its category. A default option is also
|
||||||
|
* exposed that disables all overlays in the given category.
|
||||||
|
*/
|
||||||
|
public class OverlayCategoryPreferenceController extends DeveloperOptionsPreferenceController
|
||||||
|
implements Preference.OnPreferenceChangeListener, PreferenceControllerMixin {
|
||||||
|
@VisibleForTesting
|
||||||
|
static final String PACKAGE_DEVICE_DEFAULT = "package_device_default";
|
||||||
|
private static final String OVERLAY_TARGET_PACKAGE = "android";
|
||||||
|
private static final Comparator<OverlayInfo> OVERLAY_INFO_COMPARATOR =
|
||||||
|
Comparator.comparingInt(a -> a.priority);
|
||||||
|
private final IOverlayManager mOverlayManager;
|
||||||
|
private final boolean mAvailable;
|
||||||
|
private final String mCategory;
|
||||||
|
private final PackageManager mPackageManager;
|
||||||
|
|
||||||
|
private ListPreference mPreference;
|
||||||
|
|
||||||
|
@VisibleForTesting
|
||||||
|
OverlayCategoryPreferenceController(Context context, PackageManager packageManager,
|
||||||
|
IOverlayManager overlayManager, String category) {
|
||||||
|
super(context);
|
||||||
|
mOverlayManager = overlayManager;
|
||||||
|
mPackageManager = packageManager;
|
||||||
|
mCategory = category;
|
||||||
|
mAvailable = overlayManager != null && !getOverlayInfos().isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
public OverlayCategoryPreferenceController(Context context, String category) {
|
||||||
|
this(context, context.getPackageManager(), IOverlayManager.Stub
|
||||||
|
.asInterface(ServiceManager.getService(Context.OVERLAY_SERVICE)), category);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isAvailable() {
|
||||||
|
return mAvailable;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getPreferenceKey() {
|
||||||
|
return mCategory;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void displayPreference(PreferenceScreen screen) {
|
||||||
|
super.displayPreference(screen);
|
||||||
|
setPreference((ListPreference) screen.findPreference(getPreferenceKey()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@VisibleForTesting
|
||||||
|
void setPreference(ListPreference preference) {
|
||||||
|
mPreference = preference;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onPreferenceChange(Preference preference, Object newValue) {
|
||||||
|
return setOverlay((String) newValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean setOverlay(String packageName) {
|
||||||
|
String currentPackageName = null;
|
||||||
|
for (OverlayInfo o : getOverlayInfos()) {
|
||||||
|
if (o.isEnabled()) {
|
||||||
|
currentPackageName = o.packageName;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (PACKAGE_DEVICE_DEFAULT.equals(packageName) && TextUtils.isEmpty(currentPackageName)
|
||||||
|
|| TextUtils.equals(packageName, currentPackageName)) {
|
||||||
|
// Already set.
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
final boolean result;
|
||||||
|
try {
|
||||||
|
if (PACKAGE_DEVICE_DEFAULT.equals(packageName)) {
|
||||||
|
result = mOverlayManager.setEnabled(currentPackageName, false, USER_SYSTEM);
|
||||||
|
} else {
|
||||||
|
result = mOverlayManager.setEnabledExclusiveInCategory(packageName, USER_SYSTEM);
|
||||||
|
}
|
||||||
|
} catch (RemoteException re) {
|
||||||
|
throw re.rethrowFromSystemServer();
|
||||||
|
}
|
||||||
|
updateState(mPreference);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateState(Preference preference) {
|
||||||
|
final List<String> pkgs = new ArrayList<>();
|
||||||
|
final List<String> labels = new ArrayList<>();
|
||||||
|
|
||||||
|
String selectedPkg = PACKAGE_DEVICE_DEFAULT;
|
||||||
|
String selectedLabel = mContext.getString(R.string.overlay_option_device_default);
|
||||||
|
|
||||||
|
// Add the default package / label before all of the overlays
|
||||||
|
pkgs.add(selectedPkg);
|
||||||
|
labels.add(selectedLabel);
|
||||||
|
|
||||||
|
for (OverlayInfo overlayInfo : getOverlayInfos()) {
|
||||||
|
pkgs.add(overlayInfo.packageName);
|
||||||
|
try {
|
||||||
|
labels.add(mPackageManager.getApplicationInfo(overlayInfo.packageName, 0)
|
||||||
|
.loadLabel(mPackageManager).toString());
|
||||||
|
} catch (PackageManager.NameNotFoundException e) {
|
||||||
|
labels.add(overlayInfo.packageName);
|
||||||
|
}
|
||||||
|
if (overlayInfo.isEnabled()) {
|
||||||
|
selectedPkg = pkgs.get(pkgs.size() - 1);
|
||||||
|
selectedLabel = labels.get(labels.size() - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mPreference.setEntries(labels.toArray(new String[labels.size()]));
|
||||||
|
mPreference.setEntryValues(pkgs.toArray(new String[pkgs.size()]));
|
||||||
|
mPreference.setValue(selectedPkg);
|
||||||
|
mPreference.setSummary(selectedLabel);
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<OverlayInfo> getOverlayInfos() {
|
||||||
|
final List<OverlayInfo> filteredInfos = new ArrayList<>();
|
||||||
|
try {
|
||||||
|
List<OverlayInfo> overlayInfos = mOverlayManager
|
||||||
|
.getOverlayInfosForTarget(OVERLAY_TARGET_PACKAGE, USER_SYSTEM);
|
||||||
|
for (OverlayInfo overlayInfo : overlayInfos) {
|
||||||
|
if (mCategory.equals(overlayInfo.category)) {
|
||||||
|
filteredInfos.add(overlayInfo);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (RemoteException re) {
|
||||||
|
throw re.rethrowFromSystemServer();
|
||||||
|
}
|
||||||
|
filteredInfos.sort(OVERLAY_INFO_COMPARATOR);
|
||||||
|
return filteredInfos;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onDeveloperOptionsSwitchDisabled() {
|
||||||
|
super.onDeveloperOptionsSwitchDisabled();
|
||||||
|
// STOPSHIP b/122308197: reset the overlays to the set in
|
||||||
|
// Settings.Secure.THEME_CUSTOMIZATION_OVERLAY_PACKAGES
|
||||||
|
setOverlay(PACKAGE_DEVICE_DEFAULT);
|
||||||
|
updateState(mPreference);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -18,44 +18,25 @@ package com.android.settings.development;
|
|||||||
|
|
||||||
import static com.google.common.truth.Truth.assertThat;
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
|
||||||
import static org.mockito.AdditionalMatchers.aryEq;
|
|
||||||
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.Mockito.mock;
|
|
||||||
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.om.IOverlayManager;
|
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.view.DisplayCutout;
|
|
||||||
|
|
||||||
import androidx.preference.ListPreference;
|
|
||||||
import androidx.preference.PreferenceScreen;
|
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.mockito.Mock;
|
import org.mockito.Mock;
|
||||||
import org.mockito.MockitoAnnotations;
|
import org.mockito.MockitoAnnotations;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import org.robolectric.RobolectricTestRunner;
|
import org.robolectric.RobolectricTestRunner;
|
||||||
|
import org.robolectric.RuntimeEnvironment;
|
||||||
|
|
||||||
|
import androidx.preference.ListPreference;
|
||||||
|
|
||||||
@RunWith(RobolectricTestRunner.class)
|
@RunWith(RobolectricTestRunner.class)
|
||||||
public class EmulateDisplayCutoutPreferenceControllerTest {
|
public class EmulateDisplayCutoutPreferenceControllerTest {
|
||||||
|
|
||||||
private static final OverlayInfo ONE_DISABLED = createFakeOverlay("emulation.one", false, 1);
|
|
||||||
private static final OverlayInfo ONE_ENABLED = createFakeOverlay("emulation.one", true, 1);
|
|
||||||
private static final OverlayInfo TWO_DISABLED = createFakeOverlay("emulation.two", false, 2);
|
|
||||||
private static final OverlayInfo TWO_ENABLED = createFakeOverlay("emulation.two", true, 2);
|
|
||||||
|
|
||||||
@Mock
|
|
||||||
private Context mContext;
|
|
||||||
@Mock
|
@Mock
|
||||||
private IOverlayManager mOverlayManager;
|
private IOverlayManager mOverlayManager;
|
||||||
@Mock
|
@Mock
|
||||||
@@ -67,112 +48,19 @@ public class EmulateDisplayCutoutPreferenceControllerTest {
|
|||||||
@Before
|
@Before
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
MockitoAnnotations.initMocks(this);
|
MockitoAnnotations.initMocks(this);
|
||||||
when(mContext.getSystemService(Context.OVERLAY_SERVICE)).thenReturn(mOverlayManager);
|
|
||||||
mockCurrentOverlays();
|
|
||||||
when(mPackageManager.getApplicationInfo(any(), anyInt()))
|
when(mPackageManager.getApplicationInfo(any(), anyInt()))
|
||||||
.thenThrow(PackageManager.NameNotFoundException.class);
|
.thenThrow(PackageManager.NameNotFoundException.class);
|
||||||
mController = createController();
|
mController = createController();
|
||||||
mController.setPreference(mPreference);
|
mController.setPreference(mPreference);
|
||||||
}
|
}
|
||||||
|
|
||||||
Object mockCurrentOverlays(OverlayInfo... overlays) {
|
|
||||||
try {
|
|
||||||
return when(mOverlayManager.getOverlayInfosForTarget(eq("android"), anyInt()))
|
|
||||||
.thenReturn(Arrays.asList(overlays));
|
|
||||||
} catch (RemoteException re) {
|
|
||||||
return new ArrayList<OverlayInfo>();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void isAvailable_true() {
|
public void getKey_returnsDisplayCutoutEmulation() {
|
||||||
mockCurrentOverlays(ONE_DISABLED, TWO_DISABLED);
|
assertThat(createController().getPreferenceKey()).isEqualTo("display_cutout_emulation");
|
||||||
|
|
||||||
assertThat(createController().isAvailable()).isTrue();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void isAvailable_false() {
|
|
||||||
mockCurrentOverlays();
|
|
||||||
|
|
||||||
assertThat(createController().isAvailable()).isFalse();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void onPreferenceChange_enable() throws Exception {
|
|
||||||
mockCurrentOverlays(ONE_DISABLED, TWO_DISABLED);
|
|
||||||
|
|
||||||
mController.onPreferenceChange(null, TWO_DISABLED.packageName);
|
|
||||||
|
|
||||||
verify(mOverlayManager)
|
|
||||||
.setEnabledExclusiveInCategory(eq(TWO_DISABLED.packageName), anyInt());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void onPreferenceChange_disable() throws Exception {
|
|
||||||
mockCurrentOverlays(ONE_DISABLED, TWO_ENABLED);
|
|
||||||
|
|
||||||
mController.onPreferenceChange(null, "");
|
|
||||||
|
|
||||||
verify(mOverlayManager).setEnabled(eq(TWO_ENABLED.packageName), eq(false), anyInt());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void updateState_enabled() {
|
|
||||||
mockCurrentOverlays(ONE_DISABLED, TWO_ENABLED);
|
|
||||||
|
|
||||||
mController.updateState(null);
|
|
||||||
|
|
||||||
verify(mPreference).setValueIndex(2);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void updateState_disabled() {
|
|
||||||
mockCurrentOverlays(ONE_DISABLED, TWO_DISABLED);
|
|
||||||
|
|
||||||
mController.updateState(null);
|
|
||||||
|
|
||||||
verify(mPreference).setValueIndex(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void ordered_by_priority() {
|
|
||||||
mockCurrentOverlays(TWO_DISABLED, ONE_DISABLED);
|
|
||||||
|
|
||||||
mController.updateState(null);
|
|
||||||
|
|
||||||
verify(mPreference).setEntryValues(
|
|
||||||
aryEq(new String[]{"", ONE_DISABLED.packageName, TWO_DISABLED.packageName}));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void onDeveloperOptionsSwitchDisabled() throws Exception {
|
|
||||||
mockCurrentOverlays(ONE_ENABLED, TWO_DISABLED);
|
|
||||||
final PreferenceScreen screen = mock(PreferenceScreen.class);
|
|
||||||
when(screen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
|
|
||||||
mController.displayPreference(screen);
|
|
||||||
|
|
||||||
mController.onDeveloperOptionsSwitchDisabled();
|
|
||||||
|
|
||||||
verify(mPreference).setEnabled(false);
|
|
||||||
verify(mOverlayManager).setEnabled(eq(ONE_ENABLED.packageName), eq(false), anyInt());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private EmulateDisplayCutoutPreferenceController createController() {
|
private EmulateDisplayCutoutPreferenceController createController() {
|
||||||
return new EmulateDisplayCutoutPreferenceController(mContext, mPackageManager,
|
return new EmulateDisplayCutoutPreferenceController(RuntimeEnvironment.application,
|
||||||
mOverlayManager);
|
mPackageManager, mOverlayManager);
|
||||||
}
|
|
||||||
|
|
||||||
private static OverlayInfo createFakeOverlay(String pkg, boolean enabled, int priority) {
|
|
||||||
final int state = (enabled) ? OverlayInfo.STATE_ENABLED : OverlayInfo.STATE_DISABLED;
|
|
||||||
|
|
||||||
return new OverlayInfo(pkg /* packageName */,
|
|
||||||
"android" /* targetPackageName */,
|
|
||||||
DisplayCutout.EMULATION_OVERLAY_CATEGORY /* category */,
|
|
||||||
pkg + ".baseCodePath" /* baseCodePath */,
|
|
||||||
state /* state */,
|
|
||||||
0 /* userId */,
|
|
||||||
priority,
|
|
||||||
true /* isStatic */);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -0,0 +1,184 @@
|
|||||||
|
/*
|
||||||
|
* 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.development;
|
||||||
|
|
||||||
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
|
||||||
|
import static org.mockito.AdditionalMatchers.aryEq;
|
||||||
|
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.verify;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import android.content.om.IOverlayManager;
|
||||||
|
import android.content.om.OverlayInfo;
|
||||||
|
import android.content.pm.PackageManager;
|
||||||
|
import android.os.RemoteException;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.MockitoAnnotations;
|
||||||
|
import org.robolectric.RobolectricTestRunner;
|
||||||
|
import org.robolectric.RuntimeEnvironment;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
import androidx.preference.ListPreference;
|
||||||
|
import androidx.preference.PreferenceScreen;
|
||||||
|
|
||||||
|
@RunWith(RobolectricTestRunner.class)
|
||||||
|
public class OverlayCategoryPreferenceControllerTest {
|
||||||
|
|
||||||
|
private static final OverlayInfo ONE_DISABLED = createFakeOverlay("overlay.one", false, 1);
|
||||||
|
private static final OverlayInfo ONE_ENABLED = createFakeOverlay("overlay.one", true, 1);
|
||||||
|
private static final OverlayInfo TWO_DISABLED = createFakeOverlay("overlay.two", false, 2);
|
||||||
|
private static final OverlayInfo TWO_ENABLED = createFakeOverlay("overlay.two", true, 2);
|
||||||
|
private static final String TEST_CATEGORY = "android.test.category";
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private IOverlayManager mOverlayManager;
|
||||||
|
@Mock
|
||||||
|
private PackageManager mPackageManager;
|
||||||
|
@Mock
|
||||||
|
private ListPreference mPreference;
|
||||||
|
private OverlayCategoryPreferenceController mController;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() throws Exception {
|
||||||
|
MockitoAnnotations.initMocks(this);
|
||||||
|
mockCurrentOverlays();
|
||||||
|
when(mPackageManager.getApplicationInfo(any(), anyInt()))
|
||||||
|
.thenThrow(PackageManager.NameNotFoundException.class);
|
||||||
|
mController = createController();
|
||||||
|
mController.setPreference(mPreference);
|
||||||
|
}
|
||||||
|
|
||||||
|
Object mockCurrentOverlays(OverlayInfo... overlays) {
|
||||||
|
try {
|
||||||
|
return when(mOverlayManager.getOverlayInfosForTarget(eq("android"), anyInt()))
|
||||||
|
.thenReturn(Arrays.asList(overlays));
|
||||||
|
} catch (RemoteException re) {
|
||||||
|
return new ArrayList<OverlayInfo>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getKey_returnsCategory() {
|
||||||
|
assertThat(createController().getPreferenceKey()).isEqualTo(TEST_CATEGORY);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void isAvailable_true() {
|
||||||
|
mockCurrentOverlays(ONE_DISABLED, TWO_DISABLED);
|
||||||
|
|
||||||
|
assertThat(createController().isAvailable()).isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void isAvailable_false() {
|
||||||
|
mockCurrentOverlays();
|
||||||
|
|
||||||
|
assertThat(createController().isAvailable()).isFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void onPreferenceChange_enable() throws Exception {
|
||||||
|
mockCurrentOverlays(ONE_DISABLED, TWO_DISABLED);
|
||||||
|
|
||||||
|
mController.onPreferenceChange(null, TWO_DISABLED.packageName);
|
||||||
|
|
||||||
|
verify(mOverlayManager)
|
||||||
|
.setEnabledExclusiveInCategory(eq(TWO_DISABLED.packageName), anyInt());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void onPreferenceChange_disable() throws Exception {
|
||||||
|
mockCurrentOverlays(ONE_DISABLED, TWO_ENABLED);
|
||||||
|
|
||||||
|
mController.onPreferenceChange(
|
||||||
|
null, OverlayCategoryPreferenceController.PACKAGE_DEVICE_DEFAULT);
|
||||||
|
|
||||||
|
verify(mOverlayManager).setEnabled(eq(TWO_ENABLED.packageName), eq(false), anyInt());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void updateState_enabled() {
|
||||||
|
mockCurrentOverlays(ONE_DISABLED, TWO_ENABLED);
|
||||||
|
|
||||||
|
mController.updateState(null);
|
||||||
|
|
||||||
|
verify(mPreference).setValue(TWO_ENABLED.packageName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void updateState_disabled() {
|
||||||
|
mockCurrentOverlays(ONE_DISABLED, TWO_DISABLED);
|
||||||
|
|
||||||
|
mController.updateState(null);
|
||||||
|
|
||||||
|
verify(mPreference).setValue(OverlayCategoryPreferenceController.PACKAGE_DEVICE_DEFAULT);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void ordered_by_priority() {
|
||||||
|
mockCurrentOverlays(TWO_DISABLED, ONE_DISABLED);
|
||||||
|
|
||||||
|
mController.updateState(null);
|
||||||
|
|
||||||
|
verify(mPreference).setEntryValues(
|
||||||
|
aryEq(new String[]{
|
||||||
|
OverlayCategoryPreferenceController.PACKAGE_DEVICE_DEFAULT,
|
||||||
|
ONE_DISABLED.packageName,
|
||||||
|
TWO_DISABLED.packageName}));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void onDeveloperOptionsSwitchDisabled() throws Exception {
|
||||||
|
mockCurrentOverlays(ONE_ENABLED, TWO_DISABLED);
|
||||||
|
final PreferenceScreen screen = mock(PreferenceScreen.class);
|
||||||
|
when(screen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
|
||||||
|
mController.displayPreference(screen);
|
||||||
|
|
||||||
|
mController.onDeveloperOptionsSwitchDisabled();
|
||||||
|
|
||||||
|
verify(mPreference).setEnabled(false);
|
||||||
|
verify(mOverlayManager).setEnabled(eq(ONE_ENABLED.packageName), eq(false), anyInt());
|
||||||
|
}
|
||||||
|
|
||||||
|
private OverlayCategoryPreferenceController createController() {
|
||||||
|
return new OverlayCategoryPreferenceController(RuntimeEnvironment.application,
|
||||||
|
mPackageManager, mOverlayManager, TEST_CATEGORY);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static OverlayInfo createFakeOverlay(String pkg, boolean enabled, int priority) {
|
||||||
|
final int state = (enabled) ? OverlayInfo.STATE_ENABLED : OverlayInfo.STATE_DISABLED;
|
||||||
|
|
||||||
|
return new OverlayInfo(pkg /* packageName */,
|
||||||
|
"android" /* targetPackageName */,
|
||||||
|
TEST_CATEGORY/* category */,
|
||||||
|
pkg + ".baseCodePath" /* baseCodePath */,
|
||||||
|
state /* state */,
|
||||||
|
0 /* userId */,
|
||||||
|
priority,
|
||||||
|
true /* isStatic */);
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user