Merge "Add action to open the anomaly detail page"
This commit is contained in:
@@ -22,11 +22,14 @@ import android.content.Context;
|
|||||||
import android.support.annotation.VisibleForTesting;
|
import android.support.annotation.VisibleForTesting;
|
||||||
import android.support.v7.preference.Preference;
|
import android.support.v7.preference.Preference;
|
||||||
|
|
||||||
|
import com.android.internal.util.CollectionUtils;
|
||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
import com.android.settings.SettingsActivity;
|
import com.android.settings.SettingsActivity;
|
||||||
import com.android.settings.core.BasePreferenceController;
|
import com.android.settings.core.BasePreferenceController;
|
||||||
import com.android.settings.core.InstrumentedPreferenceFragment;
|
import com.android.settings.core.InstrumentedPreferenceFragment;
|
||||||
|
import com.android.settings.fuelgauge.batterytip.AppInfo;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -37,7 +40,7 @@ public class RestrictAppPreferenceController extends BasePreferenceController {
|
|||||||
static final String KEY_RESTRICT_APP = "restricted_app";
|
static final String KEY_RESTRICT_APP = "restricted_app";
|
||||||
|
|
||||||
private AppOpsManager mAppOpsManager;
|
private AppOpsManager mAppOpsManager;
|
||||||
private List<AppOpsManager.PackageOps> mPackageOps;
|
private List<AppInfo> mAppInfos;
|
||||||
private SettingsActivity mSettingsActivity;
|
private SettingsActivity mSettingsActivity;
|
||||||
private InstrumentedPreferenceFragment mPreferenceFragment;
|
private InstrumentedPreferenceFragment mPreferenceFragment;
|
||||||
|
|
||||||
@@ -62,9 +65,17 @@ public class RestrictAppPreferenceController extends BasePreferenceController {
|
|||||||
public void updateState(Preference preference) {
|
public void updateState(Preference preference) {
|
||||||
super.updateState(preference);
|
super.updateState(preference);
|
||||||
|
|
||||||
mPackageOps = mAppOpsManager.getPackagesForOps(
|
final List<AppOpsManager.PackageOps> packageOpsList = mAppOpsManager.getPackagesForOps(
|
||||||
new int[]{AppOpsManager.OP_RUN_ANY_IN_BACKGROUND});
|
new int[]{AppOpsManager.OP_RUN_ANY_IN_BACKGROUND});
|
||||||
final int num = mPackageOps != null ? mPackageOps.size() : 0;
|
final int num = CollectionUtils.size(packageOpsList);
|
||||||
|
mAppInfos = new ArrayList<>();
|
||||||
|
|
||||||
|
for (int i = 0; i < num; i++) {
|
||||||
|
final AppOpsManager.PackageOps packageOps = packageOpsList.get(i);
|
||||||
|
mAppInfos.add(new AppInfo.Builder()
|
||||||
|
.setPackageName(packageOps.getPackageName())
|
||||||
|
.build());
|
||||||
|
}
|
||||||
|
|
||||||
// Enable the preference if some apps already been restricted, otherwise disable it
|
// Enable the preference if some apps already been restricted, otherwise disable it
|
||||||
preference.setEnabled(num > 0);
|
preference.setEnabled(num > 0);
|
||||||
@@ -78,7 +89,7 @@ public class RestrictAppPreferenceController extends BasePreferenceController {
|
|||||||
if (getPreferenceKey().equals(preference.getKey())) {
|
if (getPreferenceKey().equals(preference.getKey())) {
|
||||||
// start fragment
|
// start fragment
|
||||||
RestrictedAppDetails.startRestrictedAppDetails(mSettingsActivity, mPreferenceFragment,
|
RestrictedAppDetails.startRestrictedAppDetails(mSettingsActivity, mPreferenceFragment,
|
||||||
mPackageOps);
|
mAppInfos);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -17,6 +17,7 @@
|
|||||||
package com.android.settings.fuelgauge;
|
package com.android.settings.fuelgauge;
|
||||||
|
|
||||||
import android.app.AppOpsManager;
|
import android.app.AppOpsManager;
|
||||||
|
import android.app.Fragment;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.pm.ApplicationInfo;
|
import android.content.pm.ApplicationInfo;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
@@ -35,6 +36,7 @@ import com.android.settings.Utils;
|
|||||||
import com.android.settings.core.InstrumentedPreferenceFragment;
|
import com.android.settings.core.InstrumentedPreferenceFragment;
|
||||||
import com.android.settings.core.SubSettingLauncher;
|
import com.android.settings.core.SubSettingLauncher;
|
||||||
import com.android.settings.dashboard.DashboardFragment;
|
import com.android.settings.dashboard.DashboardFragment;
|
||||||
|
import com.android.settings.fuelgauge.batterytip.AppInfo;
|
||||||
import com.android.settings.widget.AppCheckBoxPreference;
|
import com.android.settings.widget.AppCheckBoxPreference;
|
||||||
import com.android.settingslib.core.AbstractPreferenceController;
|
import com.android.settingslib.core.AbstractPreferenceController;
|
||||||
|
|
||||||
@@ -47,11 +49,12 @@ public class RestrictedAppDetails extends DashboardFragment {
|
|||||||
|
|
||||||
public static final String TAG = "RestrictedAppDetails";
|
public static final String TAG = "RestrictedAppDetails";
|
||||||
|
|
||||||
private static final String EXTRA_PACKAGE_OPS_LIST = "package_ops_list";
|
@VisibleForTesting
|
||||||
|
static final String EXTRA_APP_INFO_LIST = "app_info_list";
|
||||||
private static final String KEY_PREF_RESTRICTED_APP_LIST = "restrict_app_list";
|
private static final String KEY_PREF_RESTRICTED_APP_LIST = "restrict_app_list";
|
||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
List<AppOpsManager.PackageOps> mPackageOpsList;
|
List<AppInfo> mAppInfos;
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
IconDrawableFactory mIconDrawableFactory;
|
IconDrawableFactory mIconDrawableFactory;
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
@@ -62,9 +65,9 @@ public class RestrictedAppDetails extends DashboardFragment {
|
|||||||
PackageManager mPackageManager;
|
PackageManager mPackageManager;
|
||||||
|
|
||||||
public static void startRestrictedAppDetails(SettingsActivity caller,
|
public static void startRestrictedAppDetails(SettingsActivity caller,
|
||||||
InstrumentedPreferenceFragment fragment, List<AppOpsManager.PackageOps> packageOpsList) {
|
InstrumentedPreferenceFragment fragment, List<AppInfo> appInfos) {
|
||||||
final Bundle args = new Bundle();
|
final Bundle args = new Bundle();
|
||||||
args.putParcelableList(EXTRA_PACKAGE_OPS_LIST, packageOpsList);
|
args.putParcelableList(EXTRA_APP_INFO_LIST, appInfos);
|
||||||
|
|
||||||
new SubSettingLauncher(caller)
|
new SubSettingLauncher(caller)
|
||||||
.setDestination(RestrictedAppDetails.class.getName())
|
.setDestination(RestrictedAppDetails.class.getName())
|
||||||
@@ -80,7 +83,7 @@ public class RestrictedAppDetails extends DashboardFragment {
|
|||||||
final Context context = getContext();
|
final Context context = getContext();
|
||||||
|
|
||||||
mRestrictedAppListGroup = (PreferenceGroup) findPreference(KEY_PREF_RESTRICTED_APP_LIST);
|
mRestrictedAppListGroup = (PreferenceGroup) findPreference(KEY_PREF_RESTRICTED_APP_LIST);
|
||||||
mPackageOpsList = getArguments().getParcelableArrayList(EXTRA_PACKAGE_OPS_LIST);
|
mAppInfos = getArguments().getParcelableArrayList(EXTRA_APP_INFO_LIST);
|
||||||
mPackageManager = context.getPackageManager();
|
mPackageManager = context.getPackageManager();
|
||||||
mIconDrawableFactory = IconDrawableFactory.newInstance(context);
|
mIconDrawableFactory = IconDrawableFactory.newInstance(context);
|
||||||
mBatteryUtils = BatteryUtils.getInstance(context);
|
mBatteryUtils = BatteryUtils.getInstance(context);
|
||||||
@@ -119,19 +122,20 @@ public class RestrictedAppDetails extends DashboardFragment {
|
|||||||
mRestrictedAppListGroup.removeAll();
|
mRestrictedAppListGroup.removeAll();
|
||||||
final Context context = getPrefContext();
|
final Context context = getPrefContext();
|
||||||
|
|
||||||
for (int i = 0, size = mPackageOpsList.size(); i < size; i++) {
|
for (int i = 0, size = mAppInfos.size(); i < size; i++) {
|
||||||
final CheckBoxPreference checkBoxPreference = new AppCheckBoxPreference(context);
|
final CheckBoxPreference checkBoxPreference = new AppCheckBoxPreference(context);
|
||||||
final AppOpsManager.PackageOps packageOps = mPackageOpsList.get(i);
|
final AppInfo appInfo = mAppInfos.get(i);
|
||||||
try {
|
try {
|
||||||
final ApplicationInfo applicationInfo = mPackageManager.getApplicationInfo(
|
final ApplicationInfo applicationInfo = mPackageManager.getApplicationInfo(
|
||||||
packageOps.getPackageName(), 0 /* flags */);
|
appInfo.packageName, 0 /* flags */);
|
||||||
checkBoxPreference.setChecked(true);
|
checkBoxPreference.setChecked(true);
|
||||||
checkBoxPreference.setTitle(mPackageManager.getApplicationLabel(applicationInfo));
|
checkBoxPreference.setTitle(mPackageManager.getApplicationLabel(applicationInfo));
|
||||||
checkBoxPreference.setKey(packageOps.getPackageName());
|
checkBoxPreference.setKey(appInfo.packageName);
|
||||||
checkBoxPreference.setIcon(
|
checkBoxPreference.setIcon(
|
||||||
Utils.getBadgedIcon(mIconDrawableFactory, mPackageManager,
|
Utils.getBadgedIcon(mIconDrawableFactory, mPackageManager,
|
||||||
packageOps.getPackageName(),
|
appInfo.packageName,
|
||||||
UserHandle.getUserId(packageOps.getUid())));
|
UserHandle.getUserId(
|
||||||
|
mBatteryUtils.getPackageUid(appInfo.packageName))));
|
||||||
checkBoxPreference.setOnPreferenceChangeListener((pref, value) -> {
|
checkBoxPreference.setOnPreferenceChangeListener((pref, value) -> {
|
||||||
// change the toggle
|
// change the toggle
|
||||||
final int mode = (Boolean) value ? AppOpsManager.MODE_IGNORED
|
final int mode = (Boolean) value ? AppOpsManager.MODE_IGNORED
|
||||||
|
@@ -29,6 +29,7 @@ import android.view.LayoutInflater;
|
|||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
import com.android.settings.SettingsActivity;
|
import com.android.settings.SettingsActivity;
|
||||||
import com.android.settings.Utils;
|
import com.android.settings.Utils;
|
||||||
|
import com.android.settings.core.InstrumentedPreferenceFragment;
|
||||||
import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
|
import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
|
||||||
import com.android.settings.fuelgauge.batterytip.BatteryTipPreferenceController.BatteryTipListener;
|
import com.android.settings.fuelgauge.batterytip.BatteryTipPreferenceController.BatteryTipListener;
|
||||||
import com.android.settings.fuelgauge.batterytip.actions.BatteryTipAction;
|
import com.android.settings.fuelgauge.batterytip.actions.BatteryTipAction;
|
||||||
@@ -141,7 +142,8 @@ public class BatteryTipDialogFragment extends InstrumentedDialogFragment impleme
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
final BatteryTipAction action = BatteryTipUtils.getActionForBatteryTip(mBatteryTip,
|
final BatteryTipAction action = BatteryTipUtils.getActionForBatteryTip(mBatteryTip,
|
||||||
(SettingsActivity) getActivity(), this);
|
(SettingsActivity) getActivity(),
|
||||||
|
(InstrumentedPreferenceFragment) getTargetFragment());
|
||||||
if (action != null) {
|
if (action != null) {
|
||||||
action.handlePositiveAction();
|
action.handlePositiveAction();
|
||||||
}
|
}
|
||||||
|
@@ -25,6 +25,7 @@ import android.support.v7.preference.PreferenceScreen;
|
|||||||
|
|
||||||
import com.android.settings.SettingsActivity;
|
import com.android.settings.SettingsActivity;
|
||||||
import com.android.settings.core.BasePreferenceController;
|
import com.android.settings.core.BasePreferenceController;
|
||||||
|
import com.android.settings.core.InstrumentedPreferenceFragment;
|
||||||
import com.android.settings.fuelgauge.batterytip.actions.BatteryTipAction;
|
import com.android.settings.fuelgauge.batterytip.actions.BatteryTipAction;
|
||||||
import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
|
import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
|
||||||
import com.android.settings.fuelgauge.batterytip.tips.SummaryTip;
|
import com.android.settings.fuelgauge.batterytip.tips.SummaryTip;
|
||||||
@@ -48,14 +49,14 @@ public class BatteryTipPreferenceController extends BasePreferenceController {
|
|||||||
PreferenceGroup mPreferenceGroup;
|
PreferenceGroup mPreferenceGroup;
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
Context mPrefContext;
|
Context mPrefContext;
|
||||||
PreferenceFragment mFragment;
|
InstrumentedPreferenceFragment mFragment;
|
||||||
|
|
||||||
public BatteryTipPreferenceController(Context context, String preferenceKey) {
|
public BatteryTipPreferenceController(Context context, String preferenceKey) {
|
||||||
this(context, preferenceKey, null, null, null);
|
this(context, preferenceKey, null, null, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public BatteryTipPreferenceController(Context context, String preferenceKey,
|
public BatteryTipPreferenceController(Context context, String preferenceKey,
|
||||||
SettingsActivity settingsActivity, PreferenceFragment fragment,
|
SettingsActivity settingsActivity, InstrumentedPreferenceFragment fragment,
|
||||||
BatteryTipListener batteryTipListener) {
|
BatteryTipListener batteryTipListener) {
|
||||||
super(context, preferenceKey);
|
super(context, preferenceKey);
|
||||||
mBatteryTipListener = batteryTipListener;
|
mBatteryTipListener = batteryTipListener;
|
||||||
|
@@ -17,11 +17,12 @@
|
|||||||
package com.android.settings.fuelgauge.batterytip;
|
package com.android.settings.fuelgauge.batterytip;
|
||||||
|
|
||||||
import android.app.Fragment;
|
import android.app.Fragment;
|
||||||
import android.content.Context;
|
|
||||||
|
|
||||||
import com.android.settings.SettingsActivity;
|
import com.android.settings.SettingsActivity;
|
||||||
|
import com.android.settings.core.InstrumentedPreferenceFragment;
|
||||||
import com.android.settings.fuelgauge.batterytip.actions.BatterySaverAction;
|
import com.android.settings.fuelgauge.batterytip.actions.BatterySaverAction;
|
||||||
import com.android.settings.fuelgauge.batterytip.actions.BatteryTipAction;
|
import com.android.settings.fuelgauge.batterytip.actions.BatteryTipAction;
|
||||||
|
import com.android.settings.fuelgauge.batterytip.actions.OpenRestrictAppFragmentAction;
|
||||||
import com.android.settings.fuelgauge.batterytip.actions.RestrictAppAction;
|
import com.android.settings.fuelgauge.batterytip.actions.RestrictAppAction;
|
||||||
import com.android.settings.fuelgauge.batterytip.actions.SmartBatteryAction;
|
import com.android.settings.fuelgauge.batterytip.actions.SmartBatteryAction;
|
||||||
import com.android.settings.fuelgauge.batterytip.actions.UnrestrictAppAction;
|
import com.android.settings.fuelgauge.batterytip.actions.UnrestrictAppAction;
|
||||||
@@ -42,14 +43,19 @@ public class BatteryTipUtils {
|
|||||||
* @return an action for {@code batteryTip}
|
* @return an action for {@code batteryTip}
|
||||||
*/
|
*/
|
||||||
public static BatteryTipAction getActionForBatteryTip(BatteryTip batteryTip,
|
public static BatteryTipAction getActionForBatteryTip(BatteryTip batteryTip,
|
||||||
SettingsActivity settingsActivity, Fragment fragment) {
|
SettingsActivity settingsActivity, InstrumentedPreferenceFragment fragment) {
|
||||||
switch (batteryTip.getType()) {
|
switch (batteryTip.getType()) {
|
||||||
case BatteryTip.TipType.SMART_BATTERY_MANAGER:
|
case BatteryTip.TipType.SMART_BATTERY_MANAGER:
|
||||||
return new SmartBatteryAction(settingsActivity, fragment);
|
return new SmartBatteryAction(settingsActivity, fragment);
|
||||||
case BatteryTip.TipType.BATTERY_SAVER:
|
case BatteryTip.TipType.BATTERY_SAVER:
|
||||||
return new BatterySaverAction(settingsActivity);
|
return new BatterySaverAction(settingsActivity);
|
||||||
case BatteryTip.TipType.APP_RESTRICTION:
|
case BatteryTip.TipType.APP_RESTRICTION:
|
||||||
|
if (batteryTip.getState() == BatteryTip.StateType.HANDLED) {
|
||||||
|
return new OpenRestrictAppFragmentAction(settingsActivity, fragment,
|
||||||
|
(RestrictAppTip) batteryTip);
|
||||||
|
} else {
|
||||||
return new RestrictAppAction(settingsActivity, (RestrictAppTip) batteryTip);
|
return new RestrictAppAction(settingsActivity, (RestrictAppTip) batteryTip);
|
||||||
|
}
|
||||||
case BatteryTip.TipType.REMOVE_APP_RESTRICTION:
|
case BatteryTip.TipType.REMOVE_APP_RESTRICTION:
|
||||||
return new UnrestrictAppAction(settingsActivity, (UnrestrictAppTip) batteryTip);
|
return new UnrestrictAppAction(settingsActivity, (UnrestrictAppTip) batteryTip);
|
||||||
default:
|
default:
|
||||||
|
@@ -0,0 +1,57 @@
|
|||||||
|
/*
|
||||||
|
* 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.fuelgauge.batterytip.actions;
|
||||||
|
|
||||||
|
import android.app.Fragment;
|
||||||
|
|
||||||
|
import com.android.settings.SettingsActivity;
|
||||||
|
import com.android.settings.core.InstrumentedPreferenceFragment;
|
||||||
|
import com.android.settings.fuelgauge.BatteryUtils;
|
||||||
|
import com.android.settings.fuelgauge.RestrictedAppDetails;
|
||||||
|
import com.android.settings.fuelgauge.batterytip.AppInfo;
|
||||||
|
import com.android.settings.fuelgauge.batterytip.tips.RestrictAppTip;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Action to open the {@link com.android.settings.fuelgauge.RestrictedAppDetails}
|
||||||
|
*/
|
||||||
|
public class OpenRestrictAppFragmentAction extends BatteryTipAction {
|
||||||
|
private final RestrictAppTip mRestrictAppTip;
|
||||||
|
private final BatteryUtils mBatteryUtils;
|
||||||
|
private final SettingsActivity mSettingsActivity;
|
||||||
|
private final InstrumentedPreferenceFragment mFragment;
|
||||||
|
|
||||||
|
public OpenRestrictAppFragmentAction(SettingsActivity settingsActivity,
|
||||||
|
InstrumentedPreferenceFragment fragment, RestrictAppTip tip) {
|
||||||
|
super(fragment.getContext());
|
||||||
|
mSettingsActivity = settingsActivity;
|
||||||
|
mFragment = fragment;
|
||||||
|
mRestrictAppTip = tip;
|
||||||
|
mBatteryUtils = BatteryUtils.getInstance(mContext);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle the action when user clicks positive button
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void handlePositiveAction() {
|
||||||
|
final List<AppInfo> mAppInfos = mRestrictAppTip.getRestrictAppList();
|
||||||
|
RestrictedAppDetails.startRestrictedAppDetails(mSettingsActivity, mFragment,
|
||||||
|
mAppInfos);
|
||||||
|
}
|
||||||
|
}
|
@@ -35,12 +35,12 @@ public class RestrictAppTip extends BatteryTip {
|
|||||||
private List<AppInfo> mRestrictAppList;
|
private List<AppInfo> mRestrictAppList;
|
||||||
|
|
||||||
public RestrictAppTip(@StateType int state, List<AppInfo> restrictApps) {
|
public RestrictAppTip(@StateType int state, List<AppInfo> restrictApps) {
|
||||||
super(TipType.APP_RESTRICTION, state, true /* showDialog */);
|
super(TipType.APP_RESTRICTION, state, state == StateType.NEW /* showDialog */);
|
||||||
mRestrictAppList = restrictApps;
|
mRestrictAppList = restrictApps;
|
||||||
}
|
}
|
||||||
|
|
||||||
public RestrictAppTip(@StateType int state, AppInfo appInfo) {
|
public RestrictAppTip(@StateType int state, AppInfo appInfo) {
|
||||||
super(TipType.APP_RESTRICTION, state, true /* showDialog */);
|
super(TipType.APP_RESTRICTION, state, state == StateType.NEW /* showDialog */);
|
||||||
mRestrictAppList = new ArrayList<>();
|
mRestrictAppList = new ArrayList<>();
|
||||||
mRestrictAppList.add(appInfo);
|
mRestrictAppList.add(appInfo);
|
||||||
}
|
}
|
||||||
@@ -85,9 +85,11 @@ public class RestrictAppTip extends BatteryTip {
|
|||||||
// Display it if new anomaly comes
|
// Display it if new anomaly comes
|
||||||
mState = StateType.NEW;
|
mState = StateType.NEW;
|
||||||
mRestrictAppList = ((RestrictAppTip) tip).mRestrictAppList;
|
mRestrictAppList = ((RestrictAppTip) tip).mRestrictAppList;
|
||||||
|
mShowDialog = true;
|
||||||
} else if (mState == StateType.NEW && tip.mState == StateType.INVISIBLE) {
|
} else if (mState == StateType.NEW && tip.mState == StateType.INVISIBLE) {
|
||||||
// If anomaly becomes invisible, show it as handled
|
// If anomaly becomes invisible, show it as handled
|
||||||
mState = StateType.HANDLED;
|
mState = StateType.HANDLED;
|
||||||
|
mShowDialog = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -18,24 +18,30 @@ package com.android.settings.fuelgauge;
|
|||||||
|
|
||||||
import static com.google.common.truth.Truth.assertThat;
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
|
||||||
|
import static org.mockito.Mockito.doAnswer;
|
||||||
import static org.mockito.Mockito.doReturn;
|
import static org.mockito.Mockito.doReturn;
|
||||||
import static org.mockito.Mockito.spy;
|
import static org.mockito.Mockito.spy;
|
||||||
|
|
||||||
import android.app.AppOpsManager;
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.content.Intent;
|
||||||
import android.content.pm.ApplicationInfo;
|
import android.content.pm.ApplicationInfo;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
|
import android.os.Bundle;
|
||||||
import android.support.v7.preference.Preference;
|
import android.support.v7.preference.Preference;
|
||||||
import android.support.v7.preference.PreferenceCategory;
|
import android.support.v7.preference.PreferenceCategory;
|
||||||
import android.support.v7.preference.PreferenceManager;
|
import android.support.v7.preference.PreferenceManager;
|
||||||
import android.util.IconDrawableFactory;
|
import android.util.IconDrawableFactory;
|
||||||
|
|
||||||
|
import com.android.settings.SettingsActivity;
|
||||||
import com.android.settings.TestConfig;
|
import com.android.settings.TestConfig;
|
||||||
|
import com.android.settings.core.InstrumentedPreferenceFragment;
|
||||||
|
import com.android.settings.fuelgauge.batterytip.AppInfo;
|
||||||
|
|
||||||
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.Answers;
|
import org.mockito.Answers;
|
||||||
|
import org.mockito.ArgumentCaptor;
|
||||||
import org.mockito.Mock;
|
import org.mockito.Mock;
|
||||||
import org.mockito.MockitoAnnotations;
|
import org.mockito.MockitoAnnotations;
|
||||||
import org.robolectric.RobolectricTestRunner;
|
import org.robolectric.RobolectricTestRunner;
|
||||||
@@ -43,13 +49,13 @@ import org.robolectric.RuntimeEnvironment;
|
|||||||
import org.robolectric.annotation.Config;
|
import org.robolectric.annotation.Config;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@RunWith(RobolectricTestRunner.class)
|
@RunWith(RobolectricTestRunner.class)
|
||||||
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
||||||
public class RestrictedAppDetailsTest {
|
public class RestrictedAppDetailsTest {
|
||||||
private static final String PACKAGE_NAME = "com.android.app";
|
private static final String PACKAGE_NAME = "com.android.app";
|
||||||
private static final String APP_NAME = "app";
|
private static final String APP_NAME = "app";
|
||||||
private static final int UID = 1234;
|
|
||||||
@Mock
|
@Mock
|
||||||
private PackageManager mPackageManager;
|
private PackageManager mPackageManager;
|
||||||
@Mock
|
@Mock
|
||||||
@@ -58,25 +64,33 @@ public class RestrictedAppDetailsTest {
|
|||||||
private IconDrawableFactory mIconDrawableFactory;
|
private IconDrawableFactory mIconDrawableFactory;
|
||||||
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||||
private PreferenceManager mPreferenceManager;
|
private PreferenceManager mPreferenceManager;
|
||||||
private RestrictedAppDetails mFragment;
|
@Mock
|
||||||
|
private SettingsActivity mSettingsActivity;
|
||||||
|
@Mock
|
||||||
|
private InstrumentedPreferenceFragment mFragment;
|
||||||
|
private RestrictedAppDetails mRestrictedAppDetails;
|
||||||
private Context mContext;
|
private Context mContext;
|
||||||
|
private Intent mIntent;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
MockitoAnnotations.initMocks(this);
|
MockitoAnnotations.initMocks(this);
|
||||||
|
|
||||||
mContext = spy(RuntimeEnvironment.application);
|
mContext = spy(RuntimeEnvironment.application);
|
||||||
mFragment = spy(new RestrictedAppDetails());
|
mRestrictedAppDetails = spy(new RestrictedAppDetails());
|
||||||
|
|
||||||
doReturn(mPreferenceManager).when(mFragment).getPreferenceManager();
|
doReturn(mPreferenceManager).when(mRestrictedAppDetails).getPreferenceManager();
|
||||||
doReturn(mContext).when(mPreferenceManager).getContext();
|
doReturn(mContext).when(mPreferenceManager).getContext();
|
||||||
mFragment.mPackageManager = mPackageManager;
|
mRestrictedAppDetails.mPackageManager = mPackageManager;
|
||||||
mFragment.mIconDrawableFactory = mIconDrawableFactory;
|
mRestrictedAppDetails.mIconDrawableFactory = mIconDrawableFactory;
|
||||||
mFragment.mPackageOpsList = new ArrayList<>();
|
mRestrictedAppDetails.mAppInfos = new ArrayList<>();
|
||||||
mFragment.mPackageOpsList.add(
|
mRestrictedAppDetails.mAppInfos.add(new AppInfo.Builder()
|
||||||
new AppOpsManager.PackageOps(PACKAGE_NAME, UID, null /* entries */));
|
.setPackageName(PACKAGE_NAME)
|
||||||
mFragment.mRestrictedAppListGroup = spy(new PreferenceCategory(mContext));
|
.build());
|
||||||
doReturn(mPreferenceManager).when(mFragment.mRestrictedAppListGroup).getPreferenceManager();
|
mRestrictedAppDetails.mRestrictedAppListGroup = spy(new PreferenceCategory(mContext));
|
||||||
|
mRestrictedAppDetails.mBatteryUtils = new BatteryUtils(mContext);
|
||||||
|
doReturn(mPreferenceManager).when(
|
||||||
|
mRestrictedAppDetails.mRestrictedAppListGroup).getPreferenceManager();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -84,12 +98,33 @@ public class RestrictedAppDetailsTest {
|
|||||||
doReturn(mApplicationInfo).when(mPackageManager).getApplicationInfo(PACKAGE_NAME, 0);
|
doReturn(mApplicationInfo).when(mPackageManager).getApplicationInfo(PACKAGE_NAME, 0);
|
||||||
doReturn(APP_NAME).when(mPackageManager).getApplicationLabel(mApplicationInfo);
|
doReturn(APP_NAME).when(mPackageManager).getApplicationLabel(mApplicationInfo);
|
||||||
|
|
||||||
mFragment.refreshUi();
|
mRestrictedAppDetails.refreshUi();
|
||||||
|
|
||||||
assertThat(mFragment.mRestrictedAppListGroup.getPreferenceCount()).isEqualTo(1);
|
assertThat(mRestrictedAppDetails.mRestrictedAppListGroup.getPreferenceCount()).isEqualTo(1);
|
||||||
final Preference preference = mFragment.mRestrictedAppListGroup.getPreference(0);
|
final Preference preference = mRestrictedAppDetails.mRestrictedAppListGroup.getPreference(
|
||||||
|
0);
|
||||||
assertThat(preference.getKey()).isEqualTo(PACKAGE_NAME);
|
assertThat(preference.getKey()).isEqualTo(PACKAGE_NAME);
|
||||||
assertThat(preference.getTitle()).isEqualTo(APP_NAME);
|
assertThat(preference.getTitle()).isEqualTo(APP_NAME);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testStartRestrictedAppDetails_startWithCorrectData() {
|
||||||
|
final ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class);
|
||||||
|
doAnswer(invocation -> {
|
||||||
|
// Get the intent in which it has the app info bundle
|
||||||
|
mIntent = captor.getValue();
|
||||||
|
return true;
|
||||||
|
}).when(mSettingsActivity).startActivity(captor.capture());
|
||||||
|
|
||||||
|
RestrictedAppDetails.startRestrictedAppDetails(mSettingsActivity, mFragment,
|
||||||
|
mRestrictedAppDetails.mAppInfos);
|
||||||
|
|
||||||
|
final Bundle bundle = mIntent.getBundleExtra(
|
||||||
|
SettingsActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS);
|
||||||
|
// Verify the bundle has the correct info
|
||||||
|
final List<AppInfo> appInfos = bundle.getParcelableArrayList(
|
||||||
|
RestrictedAppDetails.EXTRA_APP_INFO_LIST);
|
||||||
|
assertThat(appInfos).hasSize(1);
|
||||||
|
assertThat(appInfos.get(0).packageName).isEqualTo(PACKAGE_NAME);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -0,0 +1,78 @@
|
|||||||
|
/*
|
||||||
|
* 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.fuelgauge.batterytip;
|
||||||
|
|
||||||
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
|
||||||
|
import static org.mockito.Mockito.doReturn;
|
||||||
|
import static org.mockito.Mockito.spy;
|
||||||
|
|
||||||
|
import com.android.settings.SettingsActivity;
|
||||||
|
import com.android.settings.TestConfig;
|
||||||
|
import com.android.settings.core.InstrumentedPreferenceFragment;
|
||||||
|
import com.android.settings.fuelgauge.batterytip.actions.OpenRestrictAppFragmentAction;
|
||||||
|
import com.android.settings.fuelgauge.batterytip.actions.RestrictAppAction;
|
||||||
|
import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
|
||||||
|
import com.android.settings.fuelgauge.batterytip.tips.RestrictAppTip;
|
||||||
|
import com.android.settings.testutils.FakeFeatureFactory;
|
||||||
|
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.MockitoAnnotations;
|
||||||
|
import org.robolectric.RuntimeEnvironment;
|
||||||
|
import org.robolectric.annotation.Config;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
@RunWith(SettingsRobolectricTestRunner.class)
|
||||||
|
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
||||||
|
public class BatteryTipUtilsTest {
|
||||||
|
@Mock
|
||||||
|
private SettingsActivity mSettingsActivity;
|
||||||
|
@Mock
|
||||||
|
private InstrumentedPreferenceFragment mFragment;
|
||||||
|
private RestrictAppTip mRestrictAppTip;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
MockitoAnnotations.initMocks(this);
|
||||||
|
|
||||||
|
FakeFeatureFactory.setupForTest();
|
||||||
|
doReturn(RuntimeEnvironment.application).when(mFragment).getContext();
|
||||||
|
mRestrictAppTip = spy(new RestrictAppTip(BatteryTip.StateType.NEW, new ArrayList<>()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetActionForBatteryTip_typeRestrictStateNew_returnActionRestrict() {
|
||||||
|
doReturn(BatteryTip.StateType.NEW).when(mRestrictAppTip).getState();
|
||||||
|
|
||||||
|
assertThat(BatteryTipUtils.getActionForBatteryTip(mRestrictAppTip, mSettingsActivity,
|
||||||
|
mFragment)).isInstanceOf(RestrictAppAction.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetActionForBatteryTip_typeRestrictStateHandled_returnActionOpen() {
|
||||||
|
doReturn(BatteryTip.StateType.HANDLED).when(mRestrictAppTip).getState();
|
||||||
|
|
||||||
|
assertThat(BatteryTipUtils.getActionForBatteryTip(mRestrictAppTip, mSettingsActivity,
|
||||||
|
mFragment)).isInstanceOf(OpenRestrictAppFragmentAction.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Reference in New Issue
Block a user