Remove the anomaly detection added in O-DR
1. Remove unused class and resources 2. Update function in AdvancedPowerUsageDetail since we don't need pass in anomaly anymore Change-Id: I96fbe5ddaae902b34b756c7aae0338e49afef1f3 Bug: 74529048 Test: 1. manual test in settings page. 2. robo test still pass
This commit is contained in:
@@ -99,13 +99,11 @@ public class AppBatteryPreferenceController extends BasePreferenceController
|
||||
(UserManager) mContext.getSystemService(Context.USER_SERVICE);
|
||||
final BatteryEntry entry = new BatteryEntry(mContext, null, userManager, mSipper);
|
||||
entry.defaultPackageName = mPackageName;
|
||||
AdvancedPowerUsageDetail.startBatteryDetailPage(
|
||||
(SettingsActivity) mParent.getActivity(), mParent, mBatteryHelper,
|
||||
BatteryStats.STATS_SINCE_CHARGED, entry, mBatteryPercent,
|
||||
null /* mAnomalies */);
|
||||
AdvancedPowerUsageDetail.startBatteryDetailPage(mParent.getActivity(), mParent,
|
||||
mBatteryHelper, BatteryStats.STATS_SINCE_CHARGED, entry, mBatteryPercent);
|
||||
} else {
|
||||
AdvancedPowerUsageDetail.startBatteryDetailPage(
|
||||
(SettingsActivity) mParent.getActivity(), mParent, mPackageName);
|
||||
AdvancedPowerUsageDetail.startBatteryDetailPage(mParent.getActivity(), mParent,
|
||||
mPackageName);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
@@ -42,11 +42,6 @@ import com.android.settings.applications.appinfo.ButtonActionDialogFragment;
|
||||
import com.android.settings.core.InstrumentedPreferenceFragment;
|
||||
import com.android.settings.core.SubSettingLauncher;
|
||||
import com.android.settings.dashboard.DashboardFragment;
|
||||
import com.android.settings.fuelgauge.anomaly.Anomaly;
|
||||
import com.android.settings.fuelgauge.anomaly.AnomalyDialogFragment;
|
||||
import com.android.settings.fuelgauge.anomaly.AnomalyLoader;
|
||||
import com.android.settings.fuelgauge.anomaly.AnomalySummaryPreferenceController;
|
||||
import com.android.settings.fuelgauge.anomaly.AnomalyUtils;
|
||||
import com.android.settings.fuelgauge.batterytip.BatteryTipPreferenceController;
|
||||
import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
|
||||
import com.android.settings.widget.EntityHeaderController;
|
||||
@@ -71,8 +66,6 @@ import androidx.preference.Preference;
|
||||
*/
|
||||
public class AdvancedPowerUsageDetail extends DashboardFragment implements
|
||||
ButtonActionDialogFragment.AppButtonsDialogListener,
|
||||
AnomalyDialogFragment.AnomalyDialogListener,
|
||||
LoaderManager.LoaderCallbacks<List<Anomaly>>,
|
||||
BatteryTipPreferenceController.BatteryTipListener {
|
||||
|
||||
public static final String TAG = "AdvancedPowerDetail";
|
||||
@@ -84,7 +77,6 @@ public class AdvancedPowerUsageDetail extends DashboardFragment implements
|
||||
public static final String EXTRA_ICON_ID = "extra_icon_id";
|
||||
public static final String EXTRA_POWER_USAGE_PERCENT = "extra_power_usage_percent";
|
||||
public static final String EXTRA_POWER_USAGE_AMOUNT = "extra_power_usage_amount";
|
||||
public static final String EXTRA_ANOMALY_LIST = "extra_anomaly_list";
|
||||
|
||||
private static final String KEY_PREF_FOREGROUND = "app_usage_foreground";
|
||||
private static final String KEY_PREF_BACKGROUND = "app_usage_background";
|
||||
@@ -93,8 +85,6 @@ public class AdvancedPowerUsageDetail extends DashboardFragment implements
|
||||
private static final int REQUEST_UNINSTALL = 0;
|
||||
private static final int REQUEST_REMOVE_DEVICE_ADMIN = 1;
|
||||
|
||||
private static final int ANOMALY_LOADER = 0;
|
||||
|
||||
@VisibleForTesting
|
||||
LayoutPreference mHeaderPreference;
|
||||
@VisibleForTesting
|
||||
@@ -108,18 +98,15 @@ public class AdvancedPowerUsageDetail extends DashboardFragment implements
|
||||
Preference mForegroundPreference;
|
||||
@VisibleForTesting
|
||||
Preference mBackgroundPreference;
|
||||
@VisibleForTesting
|
||||
AnomalySummaryPreferenceController mAnomalySummaryPreferenceController;
|
||||
private AppButtonsPreferenceController mAppButtonsPreferenceController;
|
||||
private BackgroundActivityPreferenceController mBackgroundActivityPreferenceController;
|
||||
|
||||
private List<Anomaly> mAnomalies;
|
||||
private String mPackageName;
|
||||
|
||||
@VisibleForTesting
|
||||
static void startBatteryDetailPage(Activity caller, BatteryUtils batteryUtils,
|
||||
InstrumentedPreferenceFragment fragment, BatteryStatsHelper helper, int which,
|
||||
BatteryEntry entry, String usagePercent, List<Anomaly> anomalies) {
|
||||
BatteryEntry entry, String usagePercent) {
|
||||
// Initialize mStats if necessary.
|
||||
helper.getStats();
|
||||
|
||||
@@ -150,7 +137,6 @@ public class AdvancedPowerUsageDetail extends DashboardFragment implements
|
||||
args.putLong(EXTRA_FOREGROUND_TIME, foregroundTimeMs);
|
||||
args.putString(EXTRA_POWER_USAGE_PERCENT, usagePercent);
|
||||
args.putInt(EXTRA_POWER_USAGE_AMOUNT, (int) sipper.totalPowerMah);
|
||||
args.putParcelableList(EXTRA_ANOMALY_LIST, anomalies);
|
||||
|
||||
new SubSettingLauncher(caller)
|
||||
.setDestination(AdvancedPowerUsageDetail.class.getName())
|
||||
@@ -171,9 +157,9 @@ public class AdvancedPowerUsageDetail extends DashboardFragment implements
|
||||
|
||||
public static void startBatteryDetailPage(Activity caller,
|
||||
InstrumentedPreferenceFragment fragment, BatteryStatsHelper helper, int which,
|
||||
BatteryEntry entry, String usagePercent, List<Anomaly> anomalies) {
|
||||
BatteryEntry entry, String usagePercent) {
|
||||
startBatteryDetailPage(caller, BatteryUtils.getInstance(caller), fragment, helper, which,
|
||||
entry, usagePercent, anomalies);
|
||||
entry, usagePercent);
|
||||
}
|
||||
|
||||
public static void startBatteryDetailPage(Activity caller,
|
||||
@@ -209,15 +195,12 @@ public class AdvancedPowerUsageDetail extends DashboardFragment implements
|
||||
super.onCreate(icicle);
|
||||
|
||||
mPackageName = getArguments().getString(EXTRA_PACKAGE_NAME);
|
||||
mAnomalySummaryPreferenceController = new AnomalySummaryPreferenceController(
|
||||
(SettingsActivity) getActivity(), this);
|
||||
mForegroundPreference = findPreference(KEY_PREF_FOREGROUND);
|
||||
mBackgroundPreference = findPreference(KEY_PREF_BACKGROUND);
|
||||
mHeaderPreference = (LayoutPreference) findPreference(KEY_PREF_HEADER);
|
||||
|
||||
if (mPackageName != null) {
|
||||
mAppEntry = mState.getEntry(mPackageName, UserHandle.myUserId());
|
||||
initAnomalyInfo();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -229,16 +212,6 @@ public class AdvancedPowerUsageDetail extends DashboardFragment implements
|
||||
initPreference();
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
void initAnomalyInfo() {
|
||||
mAnomalies = getArguments().getParcelableArrayList(EXTRA_ANOMALY_LIST);
|
||||
if (mAnomalies == null) {
|
||||
getLoaderManager().initLoader(ANOMALY_LOADER, Bundle.EMPTY, this);
|
||||
} else if (mAnomalies != null) {
|
||||
mAnomalySummaryPreferenceController.updateAnomalySummaryPreference(mAnomalies);
|
||||
}
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
void initHeader() {
|
||||
final View appSnippet = mHeaderPreference.findViewById(R.id.entity_header);
|
||||
@@ -280,8 +253,6 @@ public class AdvancedPowerUsageDetail extends DashboardFragment implements
|
||||
|
||||
final long foregroundTimeMs = bundle.getLong(EXTRA_FOREGROUND_TIME);
|
||||
final long backgroundTimeMs = bundle.getLong(EXTRA_BACKGROUND_TIME);
|
||||
final String usagePercent = bundle.getString(EXTRA_POWER_USAGE_PERCENT);
|
||||
final int powerMah = bundle.getInt(EXTRA_POWER_USAGE_AMOUNT);
|
||||
mForegroundPreference.setSummary(
|
||||
TextUtils.expandTemplate(getText(R.string.battery_used_for),
|
||||
StringUtil.formatElapsedTime(context, foregroundTimeMs, false)));
|
||||
@@ -290,15 +261,6 @@ public class AdvancedPowerUsageDetail extends DashboardFragment implements
|
||||
StringUtil.formatElapsedTime(context, backgroundTimeMs, false)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onPreferenceTreeClick(Preference preference) {
|
||||
if (TextUtils.equals(preference.getKey(), AnomalySummaryPreferenceController.ANOMALY_KEY)) {
|
||||
mAnomalySummaryPreferenceController.onPreferenceTreeClick(preference);
|
||||
return true;
|
||||
}
|
||||
return super.onPreferenceTreeClick(preference);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMetricsCategory() {
|
||||
return MetricsEvent.FUELGAUGE_POWER_USAGE_DETAIL;
|
||||
@@ -349,29 +311,6 @@ public class AdvancedPowerUsageDetail extends DashboardFragment implements
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAnomalyHandled(Anomaly anomaly) {
|
||||
mAnomalySummaryPreferenceController.hideHighUsagePreference();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Loader<List<Anomaly>> onCreateLoader(int id, Bundle args) {
|
||||
return new AnomalyLoader(getContext(), mPackageName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoadFinished(Loader<List<Anomaly>> loader, List<Anomaly> data) {
|
||||
final AnomalyUtils anomalyUtils = AnomalyUtils.getInstance(getContext());
|
||||
anomalyUtils.logAnomalies(mMetricsFeatureProvider, data,
|
||||
MetricsEvent.FUELGAUGE_POWER_USAGE_DETAIL);
|
||||
mAnomalySummaryPreferenceController.updateAnomalySummaryPreference(data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoaderReset(Loader<List<Anomaly>> loader) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBatteryTipHandled(BatteryTip batteryTip) {
|
||||
mBackgroundActivityPreferenceController.updateSummary(
|
||||
|
@@ -41,7 +41,6 @@ import com.android.settings.R;
|
||||
import com.android.settings.SettingsActivity;
|
||||
import com.android.settings.core.InstrumentedPreferenceFragment;
|
||||
import com.android.settings.core.PreferenceControllerMixin;
|
||||
import com.android.settings.fuelgauge.anomaly.Anomaly;
|
||||
import com.android.settingslib.core.AbstractPreferenceController;
|
||||
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||
import com.android.settingslib.core.lifecycle.LifecycleObserver;
|
||||
@@ -79,7 +78,6 @@ public class BatteryAppListPreferenceController extends AbstractPreferenceContro
|
||||
private SettingsActivity mActivity;
|
||||
private InstrumentedPreferenceFragment mFragment;
|
||||
private Context mPrefContext;
|
||||
SparseArray<List<Anomaly>> mAnomalySparseArray;
|
||||
|
||||
private Handler mHandler = new Handler(Looper.getMainLooper()) {
|
||||
@Override
|
||||
@@ -162,30 +160,13 @@ public class BatteryAppListPreferenceController extends AbstractPreferenceContro
|
||||
if (preference instanceof PowerGaugePreference) {
|
||||
PowerGaugePreference pgp = (PowerGaugePreference) preference;
|
||||
BatteryEntry entry = pgp.getInfo();
|
||||
AdvancedPowerUsageDetail.startBatteryDetailPage(mActivity,
|
||||
mFragment, mBatteryStatsHelper, STATS_TYPE, entry, pgp.getPercent(),
|
||||
mAnomalySparseArray != null ? mAnomalySparseArray.get(entry.sipper.getUid())
|
||||
: null);
|
||||
AdvancedPowerUsageDetail.startBatteryDetailPage(mActivity, mBatteryUtils,
|
||||
mFragment, mBatteryStatsHelper, STATS_TYPE, entry, pgp.getPercent());
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void refreshAnomalyIcon(final SparseArray<List<Anomaly>> anomalySparseArray) {
|
||||
if (!isAvailable()) {
|
||||
return;
|
||||
}
|
||||
mAnomalySparseArray = anomalySparseArray;
|
||||
for (int i = 0, size = anomalySparseArray.size(); i < size; i++) {
|
||||
final String key = extractKeyFromUid(anomalySparseArray.keyAt(i));
|
||||
final PowerGaugePreference pref = (PowerGaugePreference) mAppListGroup.findPreference(
|
||||
key);
|
||||
if (pref != null) {
|
||||
pref.shouldShowAnomalyIcon(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void refreshAppListGroup(BatteryStatsHelper statsHelper, boolean showAllApps) {
|
||||
if (!isAvailable()) {
|
||||
return;
|
||||
|
@@ -38,7 +38,6 @@ import com.android.internal.os.BatterySipper;
|
||||
import com.android.internal.os.BatteryStatsHelper;
|
||||
import com.android.internal.util.ArrayUtils;
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.fuelgauge.anomaly.Anomaly;
|
||||
import com.android.settings.fuelgauge.batterytip.AnomalyInfo;
|
||||
import com.android.settings.fuelgauge.batterytip.StatsManagerConfig;
|
||||
import com.android.settings.overlay.FeatureFactory;
|
||||
@@ -392,20 +391,6 @@ public class BatteryUtils {
|
||||
}
|
||||
}
|
||||
|
||||
@StringRes
|
||||
public int getSummaryResIdFromAnomalyType(@Anomaly.AnomalyType int type) {
|
||||
switch (type) {
|
||||
case Anomaly.AnomalyType.WAKE_LOCK:
|
||||
return R.string.battery_abnormal_wakelock_summary;
|
||||
case Anomaly.AnomalyType.WAKEUP_ALARM:
|
||||
return R.string.battery_abnormal_wakeup_alarm_summary;
|
||||
case Anomaly.AnomalyType.BLUETOOTH_SCAN:
|
||||
return R.string.battery_abnormal_location_summary;
|
||||
default:
|
||||
throw new IllegalArgumentException("Incorrect anomaly type: " + type);
|
||||
}
|
||||
}
|
||||
|
||||
public void setForceAppStandby(int uid, String packageName,
|
||||
int mode) {
|
||||
final boolean isPreOApp = isPreOApp(packageName);
|
||||
|
@@ -1,161 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2017 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;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Bundle;
|
||||
import android.os.UserHandle;
|
||||
import android.util.IconDrawableFactory;
|
||||
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
import com.android.internal.logging.nano.MetricsProto;
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.SettingsActivity;
|
||||
import com.android.settings.Utils;
|
||||
import com.android.settings.core.InstrumentedPreferenceFragment;
|
||||
import com.android.settings.core.SubSettingLauncher;
|
||||
import com.android.settings.dashboard.DashboardFragment;
|
||||
import com.android.settings.fuelgauge.anomaly.Anomaly;
|
||||
import com.android.settings.fuelgauge.anomaly.AnomalyDialogFragment;
|
||||
import com.android.settings.fuelgauge.anomaly.AnomalyPreference;
|
||||
import com.android.settingslib.core.AbstractPreferenceController;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceGroup;
|
||||
|
||||
/**
|
||||
* Fragment to show a list of anomaly apps, where user could handle these anomalies
|
||||
*/
|
||||
public class PowerUsageAnomalyDetails extends DashboardFragment implements
|
||||
AnomalyDialogFragment.AnomalyDialogListener {
|
||||
|
||||
public static final String TAG = "PowerAbnormalUsageDetail";
|
||||
@VisibleForTesting
|
||||
static final String EXTRA_ANOMALY_LIST = "anomaly_list";
|
||||
private static final int REQUEST_ANOMALY_ACTION = 0;
|
||||
private static final String KEY_PREF_ANOMALY_LIST = "app_abnormal_list";
|
||||
|
||||
@VisibleForTesting
|
||||
List<Anomaly> mAnomalies;
|
||||
@VisibleForTesting
|
||||
PreferenceGroup mAbnormalListGroup;
|
||||
@VisibleForTesting
|
||||
PackageManager mPackageManager;
|
||||
@VisibleForTesting
|
||||
BatteryUtils mBatteryUtils;
|
||||
@VisibleForTesting
|
||||
IconDrawableFactory mIconDrawableFactory;
|
||||
|
||||
public static void startBatteryAbnormalPage(SettingsActivity caller,
|
||||
InstrumentedPreferenceFragment fragment, List<Anomaly> anomalies) {
|
||||
Bundle args = new Bundle();
|
||||
args.putParcelableList(EXTRA_ANOMALY_LIST, anomalies);
|
||||
|
||||
new SubSettingLauncher(caller)
|
||||
.setDestination(PowerUsageAnomalyDetails.class.getName())
|
||||
.setTitleRes(R.string.battery_abnormal_details_title)
|
||||
.setArguments(args)
|
||||
.setSourceMetricsCategory(fragment.getMetricsCategory())
|
||||
.launch();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle icicle) {
|
||||
super.onCreate(icicle);
|
||||
final Context context = getContext();
|
||||
|
||||
mAnomalies = getArguments().getParcelableArrayList(EXTRA_ANOMALY_LIST);
|
||||
mAbnormalListGroup = (PreferenceGroup) findPreference(KEY_PREF_ANOMALY_LIST);
|
||||
mPackageManager = context.getPackageManager();
|
||||
mIconDrawableFactory = IconDrawableFactory.newInstance(context);
|
||||
mBatteryUtils = BatteryUtils.getInstance(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
|
||||
refreshUi();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onPreferenceTreeClick(Preference preference) {
|
||||
if (preference instanceof AnomalyPreference) {
|
||||
AnomalyPreference anomalyPreference = (AnomalyPreference) preference;
|
||||
final Anomaly anomaly = anomalyPreference.getAnomaly();
|
||||
|
||||
AnomalyDialogFragment dialogFragment = AnomalyDialogFragment.newInstance(anomaly,
|
||||
MetricsProto.MetricsEvent.FUELGAUGE_ANOMALY_DETAIL);
|
||||
dialogFragment.setTargetFragment(this, REQUEST_ANOMALY_ACTION);
|
||||
dialogFragment.show(getFragmentManager(), TAG);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return super.onPreferenceTreeClick(preference);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getLogTag() {
|
||||
return TAG;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getPreferenceScreenResId() {
|
||||
return R.xml.power_abnormal_detail;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMetricsCategory() {
|
||||
return MetricsProto.MetricsEvent.FUELGAUGE_ANOMALY_DETAIL;
|
||||
}
|
||||
|
||||
void refreshUi() {
|
||||
mAbnormalListGroup.removeAll();
|
||||
for (int i = 0, size = mAnomalies.size(); i < size; i++) {
|
||||
final Anomaly anomaly = mAnomalies.get(i);
|
||||
Preference pref = new AnomalyPreference(getPrefContext(), anomaly);
|
||||
pref.setSummary(mBatteryUtils.getSummaryResIdFromAnomalyType(anomaly.type));
|
||||
Drawable icon = getBadgedIcon(anomaly.packageName, UserHandle.getUserId(anomaly.uid));
|
||||
if (icon != null) {
|
||||
pref.setIcon(icon);
|
||||
}
|
||||
|
||||
mAbnormalListGroup.addPreference(pref);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAnomalyHandled(Anomaly anomaly) {
|
||||
mAnomalies.remove(anomaly);
|
||||
refreshUi();
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
Drawable getBadgedIcon(String packageName, int userId) {
|
||||
return Utils.getBadgedIcon(mIconDrawableFactory, mPackageManager, packageName, userId);
|
||||
}
|
||||
}
|
@@ -41,8 +41,6 @@ import com.android.settings.applications.LayoutPreference;
|
||||
import com.android.settings.core.SubSettingLauncher;
|
||||
import com.android.settings.dashboard.SummaryLoader;
|
||||
import com.android.settings.display.BatteryPercentagePreferenceController;
|
||||
import com.android.settings.fuelgauge.anomaly.Anomaly;
|
||||
import com.android.settings.fuelgauge.anomaly.AnomalyDetectionPolicy;
|
||||
import com.android.settings.fuelgauge.batterytip.BatteryTipLoader;
|
||||
import com.android.settings.fuelgauge.batterytip.BatteryTipPreferenceController;
|
||||
import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
|
||||
@@ -104,11 +102,6 @@ public class PowerUsageSummary extends PowerUsageBase implements OnLongClickList
|
||||
@VisibleForTesting
|
||||
BatteryInfo mBatteryInfo;
|
||||
|
||||
/**
|
||||
* SparseArray that maps uid to {@link Anomaly}, so we could find {@link Anomaly} by uid
|
||||
*/
|
||||
@VisibleForTesting
|
||||
SparseArray<List<Anomaly>> mAnomalySparseArray;
|
||||
@VisibleForTesting
|
||||
BatteryHeaderPreferenceController mBatteryHeaderPreferenceController;
|
||||
@VisibleForTesting
|
||||
@@ -217,7 +210,6 @@ public class PowerUsageSummary extends PowerUsageBase implements OnLongClickList
|
||||
KEY_TIME_SINCE_LAST_FULL_CHARGE);
|
||||
mFooterPreferenceMixin.createFooterPreference().setTitle(R.string.battery_footer_summary);
|
||||
mBatteryUtils = BatteryUtils.getInstance(getContext());
|
||||
mAnomalySparseArray = new SparseArray<>();
|
||||
|
||||
restartBatteryInfoLoader();
|
||||
mBatteryTipPreferenceController.restoreInstanceState(icicle);
|
||||
@@ -327,11 +319,6 @@ public class PowerUsageSummary extends PowerUsageBase implements OnLongClickList
|
||||
mBatteryLayoutPref = layoutPreference;
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
AnomalyDetectionPolicy getAnomalyDetectionPolicy() {
|
||||
return new AnomalyDetectionPolicy(getContext());
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
void updateLastFullChargePreference() {
|
||||
if (mBatteryInfo != null && mBatteryInfo.averageTimeToDischarge
|
||||
@@ -368,17 +355,6 @@ public class PowerUsageSummary extends PowerUsageBase implements OnLongClickList
|
||||
.getPowerUsageFeatureProvider(context);
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
void updateAnomalySparseArray(List<Anomaly> anomalies) {
|
||||
mAnomalySparseArray.clear();
|
||||
for (final Anomaly anomaly : anomalies) {
|
||||
if (mAnomalySparseArray.get(anomaly.uid) == null) {
|
||||
mAnomalySparseArray.append(anomaly.uid, new ArrayList<>());
|
||||
}
|
||||
mAnomalySparseArray.get(anomaly.uid).add(anomaly);
|
||||
}
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
void restartBatteryInfoLoader() {
|
||||
getLoaderManager().restartLoader(BATTERY_INFO_LOADER, Bundle.EMPTY,
|
||||
|
@@ -1,247 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2017 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.anomaly;
|
||||
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.util.Objects;
|
||||
|
||||
import androidx.annotation.IntDef;
|
||||
|
||||
/**
|
||||
* Data that represents an app has been detected as anomaly. It contains
|
||||
*
|
||||
* 1. Basic information of the app(i.e. uid, package name)
|
||||
* 2. Type of anomaly
|
||||
* 3. Data that has been detected as anomaly(i.e wakelock time)
|
||||
*/
|
||||
public class Anomaly implements Parcelable {
|
||||
@Retention(RetentionPolicy.SOURCE)
|
||||
@IntDef({AnomalyType.WAKE_LOCK,
|
||||
AnomalyType.WAKEUP_ALARM,
|
||||
AnomalyType.BLUETOOTH_SCAN})
|
||||
public @interface AnomalyType {
|
||||
int WAKE_LOCK = 0;
|
||||
int WAKEUP_ALARM = 1;
|
||||
int BLUETOOTH_SCAN = 2;
|
||||
}
|
||||
|
||||
@Retention(RetentionPolicy.SOURCE)
|
||||
@IntDef({AnomalyActionType.FORCE_STOP,
|
||||
AnomalyActionType.BACKGROUND_CHECK,
|
||||
AnomalyActionType.LOCATION_CHECK,
|
||||
AnomalyActionType.STOP_AND_BACKGROUND_CHECK})
|
||||
public @interface AnomalyActionType {
|
||||
int FORCE_STOP = 0;
|
||||
int BACKGROUND_CHECK = 1;
|
||||
int LOCATION_CHECK = 2;
|
||||
int STOP_AND_BACKGROUND_CHECK = 3;
|
||||
}
|
||||
|
||||
@AnomalyType
|
||||
public static final int[] ANOMALY_TYPE_LIST = {
|
||||
AnomalyType.WAKE_LOCK,
|
||||
AnomalyType.WAKEUP_ALARM,
|
||||
AnomalyType.BLUETOOTH_SCAN};
|
||||
|
||||
/**
|
||||
* Type of this this anomaly
|
||||
*/
|
||||
public final int type;
|
||||
public final int uid;
|
||||
public final int targetSdkVersion;
|
||||
public final long wakelockTimeMs;
|
||||
public final long bluetoothScanningTimeMs;
|
||||
public final int wakeupAlarmCount;
|
||||
/**
|
||||
* {@code true} if background restriction is enabled
|
||||
*
|
||||
* @see android.app.AppOpsManager.OP_RUN_IN_BACKGROUND
|
||||
*/
|
||||
public final boolean backgroundRestrictionEnabled;
|
||||
/**
|
||||
* Display name of this anomaly, usually it is the app name
|
||||
*/
|
||||
public final CharSequence displayName;
|
||||
public final String packageName;
|
||||
|
||||
private Anomaly(Builder builder) {
|
||||
type = builder.mType;
|
||||
uid = builder.mUid;
|
||||
displayName = builder.mDisplayName;
|
||||
packageName = builder.mPackageName;
|
||||
wakelockTimeMs = builder.mWakeLockTimeMs;
|
||||
targetSdkVersion = builder.mTargetSdkVersion;
|
||||
backgroundRestrictionEnabled = builder.mBgRestrictionEnabled;
|
||||
bluetoothScanningTimeMs = builder.mBluetoothScanningTimeMs;
|
||||
wakeupAlarmCount = builder.mWakeupAlarmCount;
|
||||
}
|
||||
|
||||
private Anomaly(Parcel in) {
|
||||
type = in.readInt();
|
||||
uid = in.readInt();
|
||||
displayName = in.readCharSequence();
|
||||
packageName = in.readString();
|
||||
wakelockTimeMs = in.readLong();
|
||||
targetSdkVersion = in.readInt();
|
||||
backgroundRestrictionEnabled = in.readBoolean();
|
||||
wakeupAlarmCount = in.readInt();
|
||||
bluetoothScanningTimeMs = in.readLong();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int describeContents() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToParcel(Parcel dest, int flags) {
|
||||
dest.writeInt(type);
|
||||
dest.writeInt(uid);
|
||||
dest.writeCharSequence(displayName);
|
||||
dest.writeString(packageName);
|
||||
dest.writeLong(wakelockTimeMs);
|
||||
dest.writeInt(targetSdkVersion);
|
||||
dest.writeBoolean(backgroundRestrictionEnabled);
|
||||
dest.writeInt(wakeupAlarmCount);
|
||||
dest.writeLong(bluetoothScanningTimeMs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (!(obj instanceof Anomaly)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Anomaly other = (Anomaly) obj;
|
||||
return type == other.type
|
||||
&& uid == other.uid
|
||||
&& wakelockTimeMs == other.wakelockTimeMs
|
||||
&& TextUtils.equals(displayName, other.displayName)
|
||||
&& TextUtils.equals(packageName, other.packageName)
|
||||
&& targetSdkVersion == other.targetSdkVersion
|
||||
&& backgroundRestrictionEnabled == other.backgroundRestrictionEnabled
|
||||
&& wakeupAlarmCount == other.wakeupAlarmCount
|
||||
&& bluetoothScanningTimeMs == other.bluetoothScanningTimeMs;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(type, uid, displayName, packageName, wakelockTimeMs, targetSdkVersion,
|
||||
backgroundRestrictionEnabled, wakeupAlarmCount, bluetoothScanningTimeMs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "type=" + toAnomalyTypeText(type) + " uid=" + uid + " package=" + packageName +
|
||||
" displayName=" + displayName + " wakelockTimeMs=" + wakelockTimeMs +
|
||||
" wakeupAlarmCount=" + wakeupAlarmCount + " bluetoothTimeMs="
|
||||
+ bluetoothScanningTimeMs;
|
||||
}
|
||||
|
||||
private String toAnomalyTypeText(@AnomalyType int type) {
|
||||
switch (type) {
|
||||
case AnomalyType.WAKEUP_ALARM:
|
||||
return "wakeupAlarm";
|
||||
case AnomalyType.WAKE_LOCK:
|
||||
return "wakelock";
|
||||
case AnomalyType.BLUETOOTH_SCAN:
|
||||
return "unoptimizedBluetoothScan";
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
|
||||
public Anomaly createFromParcel(Parcel in) {
|
||||
return new Anomaly(in);
|
||||
}
|
||||
|
||||
public Anomaly[] newArray(int size) {
|
||||
return new Anomaly[size];
|
||||
}
|
||||
};
|
||||
|
||||
public static final class Builder {
|
||||
@AnomalyType
|
||||
private int mType;
|
||||
private int mUid;
|
||||
private int mTargetSdkVersion;
|
||||
private CharSequence mDisplayName;
|
||||
private String mPackageName;
|
||||
private long mWakeLockTimeMs;
|
||||
private boolean mBgRestrictionEnabled;
|
||||
private int mWakeupAlarmCount;
|
||||
private long mBluetoothScanningTimeMs;
|
||||
|
||||
public Builder setType(@AnomalyType int type) {
|
||||
mType = type;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setUid(int uid) {
|
||||
mUid = uid;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setDisplayName(CharSequence displayName) {
|
||||
mDisplayName = displayName;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setPackageName(String packageName) {
|
||||
mPackageName = packageName;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setWakeLockTimeMs(long wakeLockTimeMs) {
|
||||
mWakeLockTimeMs = wakeLockTimeMs;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setTargetSdkVersion(int targetSdkVersion) {
|
||||
mTargetSdkVersion = targetSdkVersion;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setBackgroundRestrictionEnabled(boolean bgRestrictionEnabled) {
|
||||
mBgRestrictionEnabled = bgRestrictionEnabled;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setWakeupAlarmCount(int wakeupAlarmCount) {
|
||||
mWakeupAlarmCount = wakeupAlarmCount;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setBluetoothScanningTimeMs(long bluetoothScanningTimeMs) {
|
||||
mBluetoothScanningTimeMs = bluetoothScanningTimeMs;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Anomaly build() {
|
||||
return new Anomaly(this);
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,175 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2017 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.anomaly;
|
||||
|
||||
import android.content.Context;
|
||||
import android.net.Uri;
|
||||
import android.provider.Settings;
|
||||
import android.text.format.DateUtils;
|
||||
import android.util.KeyValueListParser;
|
||||
import android.util.Log;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
|
||||
/**
|
||||
* Class to store the policy for anomaly detection, which comes from
|
||||
* {@link android.provider.Settings.Global}
|
||||
*/
|
||||
public class AnomalyDetectionPolicy {
|
||||
public static final String TAG = "AnomalyDetectionPolicy";
|
||||
|
||||
@VisibleForTesting
|
||||
static final String KEY_ANOMALY_DETECTION_ENABLED = "anomaly_detection_enabled";
|
||||
@VisibleForTesting
|
||||
static final String KEY_WAKELOCK_DETECTION_ENABLED = "wakelock_enabled";
|
||||
@VisibleForTesting
|
||||
static final String KEY_WAKEUP_ALARM_DETECTION_ENABLED = "wakeup_alarm_enabled";
|
||||
@VisibleForTesting
|
||||
static final String KEY_BLUETOOTH_SCAN_DETECTION_ENABLED = "bluetooth_scan_enabled";
|
||||
@VisibleForTesting
|
||||
static final String KEY_WAKELOCK_THRESHOLD = "wakelock_threshold";
|
||||
@VisibleForTesting
|
||||
static final String KEY_WAKEUP_ALARM_THRESHOLD = "wakeup_alarm_threshold";
|
||||
@VisibleForTesting
|
||||
static final String KEY_WAKEUP_BLACKLISTED_TAGS = "wakeup_blacklisted_tags";
|
||||
@VisibleForTesting
|
||||
static final String KEY_BLUETOOTH_SCAN_THRESHOLD = "bluetooth_scan_threshold";
|
||||
|
||||
/**
|
||||
* {@code true} if general anomaly detection is enabled
|
||||
*
|
||||
* @see Settings.Global#ANOMALY_DETECTION_CONSTANTS
|
||||
* @see #KEY_ANOMALY_DETECTION_ENABLED
|
||||
*/
|
||||
final boolean anomalyDetectionEnabled;
|
||||
|
||||
/**
|
||||
* {@code true} if wakelock anomaly detection is enabled
|
||||
*
|
||||
* @see Settings.Global#ANOMALY_DETECTION_CONSTANTS
|
||||
* @see #KEY_WAKELOCK_DETECTION_ENABLED
|
||||
*/
|
||||
final boolean wakeLockDetectionEnabled;
|
||||
|
||||
/**
|
||||
* {@code true} if wakeup alarm detection is enabled
|
||||
*
|
||||
* @see Settings.Global#ANOMALY_DETECTION_CONSTANTS
|
||||
* @see #KEY_WAKEUP_ALARM_DETECTION_ENABLED
|
||||
*/
|
||||
final boolean wakeupAlarmDetectionEnabled;
|
||||
|
||||
/**
|
||||
* {@code true} if bluetooth scanning detection is enabled
|
||||
*
|
||||
* @see Settings.Global#ANOMALY_DETECTION_CONSTANTS
|
||||
* @see #KEY_BLUETOOTH_SCAN_THRESHOLD
|
||||
*/
|
||||
final boolean bluetoothScanDetectionEnabled;
|
||||
|
||||
/**
|
||||
* Threshold for wakelock time in milli seconds
|
||||
*
|
||||
* @see Settings.Global#ANOMALY_DETECTION_CONSTANTS
|
||||
* @see #KEY_WAKELOCK_THRESHOLD
|
||||
*/
|
||||
public final long wakeLockThreshold;
|
||||
|
||||
/**
|
||||
* Threshold for wakeup alarm count per hour
|
||||
*
|
||||
* @see Settings.Global#ANOMALY_DETECTION_CONSTANTS
|
||||
* @see #KEY_WAKEUP_ALARM_THRESHOLD
|
||||
*/
|
||||
public final long wakeupAlarmThreshold;
|
||||
|
||||
/**
|
||||
* Array of blacklisted wakeups, by tag.
|
||||
*
|
||||
* @see Settings.Global#ANOMALY_DETECTION_CONSTANTS
|
||||
* @see #KEY_WAKEUP_BLACKLISTED_TAGS
|
||||
*/
|
||||
public final Set<String> wakeupBlacklistedTags;
|
||||
|
||||
/**
|
||||
* Threshold for bluetooth unoptimized scanning time in milli seconds
|
||||
*
|
||||
* @see Settings.Global#ANOMALY_DETECTION_CONSTANTS
|
||||
* @see #KEY_BLUETOOTH_SCAN_THRESHOLD
|
||||
*/
|
||||
public final long bluetoothScanThreshold;
|
||||
|
||||
private final KeyValueListParser mParser;
|
||||
|
||||
public AnomalyDetectionPolicy(Context context) {
|
||||
mParser = new KeyValueListParser(',');
|
||||
final String value = Settings.Global.getString(context.getContentResolver(),
|
||||
Settings.Global.ANOMALY_DETECTION_CONSTANTS);
|
||||
|
||||
try {
|
||||
mParser.setString(value);
|
||||
} catch (IllegalArgumentException e) {
|
||||
Log.e(TAG, "Bad anomaly detection constants");
|
||||
}
|
||||
|
||||
anomalyDetectionEnabled =
|
||||
mParser.getBoolean(KEY_ANOMALY_DETECTION_ENABLED, false);
|
||||
wakeLockDetectionEnabled =
|
||||
mParser.getBoolean(KEY_WAKELOCK_DETECTION_ENABLED,false);
|
||||
wakeupAlarmDetectionEnabled =
|
||||
mParser.getBoolean(KEY_WAKEUP_ALARM_DETECTION_ENABLED,false);
|
||||
bluetoothScanDetectionEnabled = mParser.getBoolean(
|
||||
KEY_BLUETOOTH_SCAN_DETECTION_ENABLED, false);
|
||||
wakeLockThreshold = mParser.getLong(KEY_WAKELOCK_THRESHOLD,
|
||||
DateUtils.HOUR_IN_MILLIS);
|
||||
wakeupAlarmThreshold = mParser.getLong(KEY_WAKEUP_ALARM_THRESHOLD, 10);
|
||||
wakeupBlacklistedTags = parseStringSet(KEY_WAKEUP_BLACKLISTED_TAGS, null);
|
||||
bluetoothScanThreshold = mParser.getLong(KEY_BLUETOOTH_SCAN_THRESHOLD,
|
||||
30 * DateUtils.MINUTE_IN_MILLIS);
|
||||
}
|
||||
|
||||
public boolean isAnomalyDetectionEnabled() {
|
||||
return anomalyDetectionEnabled;
|
||||
}
|
||||
|
||||
public boolean isAnomalyDetectorEnabled(@Anomaly.AnomalyType int type) {
|
||||
switch (type) {
|
||||
case Anomaly.AnomalyType.WAKE_LOCK:
|
||||
return wakeLockDetectionEnabled;
|
||||
case Anomaly.AnomalyType.WAKEUP_ALARM:
|
||||
return wakeupAlarmDetectionEnabled;
|
||||
case Anomaly.AnomalyType.BLUETOOTH_SCAN:
|
||||
return bluetoothScanDetectionEnabled;
|
||||
default:
|
||||
return false; // Disabled when no this type
|
||||
}
|
||||
}
|
||||
|
||||
private Set<String> parseStringSet(final String key, final Set<String> defaultSet) {
|
||||
final String value = mParser.getString(key, null);
|
||||
if (value != null) {
|
||||
return Arrays.stream(value.split(":"))
|
||||
.map(String::trim).map(Uri::decode).collect(Collectors.toSet());
|
||||
} else {
|
||||
return defaultSet;
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,143 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2017 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.anomaly;
|
||||
|
||||
import android.app.AlertDialog;
|
||||
import android.app.Dialog;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.os.Bundle;
|
||||
|
||||
import com.android.internal.logging.nano.MetricsProto;
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
|
||||
import com.android.settings.fuelgauge.anomaly.action.AnomalyAction;
|
||||
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
|
||||
/**
|
||||
* Dialog Fragment to show action dialog for each anomaly
|
||||
*/
|
||||
public class AnomalyDialogFragment extends InstrumentedDialogFragment implements
|
||||
DialogInterface.OnClickListener {
|
||||
|
||||
private static final String ARG_ANOMALY = "anomaly";
|
||||
private static final String ARG_METRICS_KEY = "metrics_key";
|
||||
|
||||
@VisibleForTesting
|
||||
Anomaly mAnomaly;
|
||||
@VisibleForTesting
|
||||
AnomalyUtils mAnomalyUtils;
|
||||
|
||||
/**
|
||||
* Listener to give the control back to target fragment
|
||||
*/
|
||||
public interface AnomalyDialogListener {
|
||||
/**
|
||||
* This method is invoked once anomaly is handled, then target fragment could do
|
||||
* extra work. One example is that fragment could remove the anomaly preference
|
||||
* since it has been handled
|
||||
*
|
||||
* @param anomaly that has been handled
|
||||
*/
|
||||
void onAnomalyHandled(Anomaly anomaly);
|
||||
}
|
||||
|
||||
public static AnomalyDialogFragment newInstance(Anomaly anomaly, int metricsKey) {
|
||||
AnomalyDialogFragment dialogFragment = new AnomalyDialogFragment();
|
||||
|
||||
Bundle args = new Bundle(2);
|
||||
args.putParcelable(ARG_ANOMALY, anomaly);
|
||||
args.putInt(ARG_METRICS_KEY, metricsKey);
|
||||
dialogFragment.setArguments(args);
|
||||
|
||||
return dialogFragment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
initAnomalyUtils();
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
void initAnomalyUtils() {
|
||||
mAnomalyUtils = AnomalyUtils.getInstance(getContext());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMetricsCategory() {
|
||||
return MetricsProto.MetricsEvent.DIALOG_HANDLE_ANOMALY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
final AnomalyDialogListener lsn = (AnomalyDialogListener) getTargetFragment();
|
||||
if (lsn == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
final AnomalyAction anomalyAction = mAnomalyUtils.getAnomalyAction(mAnomaly);
|
||||
final int metricsKey = getArguments().getInt(ARG_METRICS_KEY);
|
||||
|
||||
anomalyAction.handlePositiveAction(mAnomaly, metricsKey);
|
||||
lsn.onAnomalyHandled(mAnomaly);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
||||
final Bundle bundle = getArguments();
|
||||
final Context context = getContext();
|
||||
final AnomalyUtils anomalyUtils = AnomalyUtils.getInstance(context);
|
||||
|
||||
mAnomaly = bundle.getParcelable(ARG_ANOMALY);
|
||||
anomalyUtils.logAnomaly(mMetricsFeatureProvider, mAnomaly,
|
||||
MetricsProto.MetricsEvent.DIALOG_HANDLE_ANOMALY);
|
||||
|
||||
final AnomalyAction anomalyAction = mAnomalyUtils.getAnomalyAction(mAnomaly);
|
||||
switch (anomalyAction.getActionType()) {
|
||||
case Anomaly.AnomalyActionType.FORCE_STOP:
|
||||
return new AlertDialog.Builder(context)
|
||||
.setTitle(R.string.dialog_stop_title)
|
||||
.setMessage(getString(mAnomaly.type == Anomaly.AnomalyType.WAKE_LOCK
|
||||
? R.string.dialog_stop_message
|
||||
: R.string.dialog_stop_message_wakeup_alarm, mAnomaly.displayName))
|
||||
.setPositiveButton(R.string.dialog_stop_ok, this)
|
||||
.setNegativeButton(R.string.dlg_cancel, null)
|
||||
.create();
|
||||
case Anomaly.AnomalyActionType.STOP_AND_BACKGROUND_CHECK:
|
||||
return new AlertDialog.Builder(context)
|
||||
.setTitle(R.string.dialog_background_check_title)
|
||||
.setMessage(getString(R.string.dialog_background_check_message,
|
||||
mAnomaly.displayName))
|
||||
.setPositiveButton(R.string.dialog_background_check_ok, this)
|
||||
.setNegativeButton(R.string.dlg_cancel, null)
|
||||
.create();
|
||||
case Anomaly.AnomalyActionType.LOCATION_CHECK:
|
||||
return new AlertDialog.Builder(context)
|
||||
.setTitle(R.string.dialog_location_title)
|
||||
.setMessage(getString(R.string.dialog_location_message,
|
||||
mAnomaly.displayName))
|
||||
.setPositiveButton(R.string.dialog_location_ok, this)
|
||||
.setNegativeButton(R.string.dlg_cancel, null)
|
||||
.create();
|
||||
default:
|
||||
throw new IllegalArgumentException("unknown type " + mAnomaly.type);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -1,135 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2017 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.anomaly;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.BatteryStats;
|
||||
import android.os.Bundle;
|
||||
import android.os.UserManager;
|
||||
import android.util.Log;
|
||||
|
||||
import com.android.internal.os.BatteryStatsHelper;
|
||||
import com.android.settingslib.utils.AsyncLoaderCompat;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
|
||||
/**
|
||||
* Loader to compute which apps are anomaly and return a anomaly list. It will return
|
||||
* an empty list if there is no anomaly.
|
||||
*/
|
||||
public class AnomalyLoader extends AsyncLoaderCompat<List<Anomaly>> {
|
||||
private static final String TAG = "AnomalyLoader";
|
||||
|
||||
private static final boolean USE_FAKE_DATA = false;
|
||||
private BatteryStatsHelper mBatteryStatsHelper;
|
||||
private String mPackageName;
|
||||
private UserManager mUserManager;
|
||||
@VisibleForTesting
|
||||
AnomalyUtils mAnomalyUtils;
|
||||
@VisibleForTesting
|
||||
AnomalyDetectionPolicy mPolicy;
|
||||
|
||||
/**
|
||||
* Create {@link AnomalyLoader} that runs anomaly check for all apps.
|
||||
*/
|
||||
public AnomalyLoader(Context context, BatteryStatsHelper batteryStatsHelper) {
|
||||
this(context, batteryStatsHelper, null, new AnomalyDetectionPolicy(context));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Create {@link AnomalyLoader} with {@code packageName}, so this loader will only
|
||||
* detect anomalies related to {@code packageName}, or check all apps if {@code packageName}
|
||||
* is {@code null}.
|
||||
*
|
||||
* This constructor will create {@link BatteryStatsHelper} in background thread.
|
||||
*
|
||||
* @param packageName if set, only finds anomalies for this package. If {@code null},
|
||||
* detects all anomalies of this type.
|
||||
*/
|
||||
public AnomalyLoader(Context context, String packageName) {
|
||||
this(context, null, packageName, new AnomalyDetectionPolicy(context));
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
AnomalyLoader(Context context, BatteryStatsHelper batteryStatsHelper,
|
||||
String packageName, AnomalyDetectionPolicy policy) {
|
||||
super(context);
|
||||
mBatteryStatsHelper = batteryStatsHelper;
|
||||
mPackageName = packageName;
|
||||
mAnomalyUtils = AnomalyUtils.getInstance(context);
|
||||
mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
|
||||
mPolicy = policy;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDiscardResult(List<Anomaly> result) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Anomaly> loadInBackground() {
|
||||
if (USE_FAKE_DATA) {
|
||||
return generateFakeData();
|
||||
}
|
||||
if (mBatteryStatsHelper == null) {
|
||||
mBatteryStatsHelper = new BatteryStatsHelper(getContext());
|
||||
mBatteryStatsHelper.create((Bundle) null);
|
||||
mBatteryStatsHelper.refreshStats(BatteryStats.STATS_SINCE_CHARGED,
|
||||
mUserManager.getUserProfiles());
|
||||
}
|
||||
|
||||
return mAnomalyUtils.detectAnomalies(mBatteryStatsHelper, mPolicy, mPackageName);
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
List<Anomaly> generateFakeData() {
|
||||
final List<Anomaly> anomalies = new ArrayList<>();
|
||||
final Context context = getContext();
|
||||
final String packageName = "com.android.settings";
|
||||
final CharSequence displayName = "Settings";
|
||||
try {
|
||||
final int uid = context.getPackageManager().getPackageUid(packageName, 0);
|
||||
|
||||
anomalies.add(new Anomaly.Builder()
|
||||
.setUid(uid)
|
||||
.setType(Anomaly.AnomalyType.WAKE_LOCK)
|
||||
.setPackageName(packageName)
|
||||
.setDisplayName(displayName)
|
||||
.build());
|
||||
anomalies.add(new Anomaly.Builder()
|
||||
.setUid(uid)
|
||||
.setType(Anomaly.AnomalyType.WAKEUP_ALARM)
|
||||
.setPackageName(packageName)
|
||||
.setDisplayName(displayName)
|
||||
.build());
|
||||
anomalies.add(new Anomaly.Builder()
|
||||
.setUid(uid)
|
||||
.setType(Anomaly.AnomalyType.BLUETOOTH_SCAN)
|
||||
.setPackageName(packageName)
|
||||
.setDisplayName(displayName)
|
||||
.build());
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
Log.e(TAG, "Cannot find package by name: " + packageName, e);
|
||||
}
|
||||
return anomalies;
|
||||
}
|
||||
|
||||
}
|
@@ -1,26 +0,0 @@
|
||||
package com.android.settings.fuelgauge.anomaly;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import androidx.preference.Preference;
|
||||
|
||||
/**
|
||||
* Preference that stores {@link Anomaly}
|
||||
*/
|
||||
public class AnomalyPreference extends Preference {
|
||||
private Anomaly mAnomaly;
|
||||
|
||||
public AnomalyPreference(Context context, Anomaly anomaly) {
|
||||
super(context);
|
||||
mAnomaly = anomaly;
|
||||
|
||||
if (anomaly != null) {
|
||||
setTitle(anomaly.displayName);
|
||||
}
|
||||
}
|
||||
|
||||
public Anomaly getAnomaly() {
|
||||
return mAnomaly;
|
||||
}
|
||||
|
||||
}
|
@@ -1,114 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2017 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.anomaly;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.SettingsActivity;
|
||||
import com.android.settings.core.InstrumentedPreferenceFragment;
|
||||
import com.android.settings.fuelgauge.BatteryUtils;
|
||||
import com.android.settings.fuelgauge.PowerUsageAnomalyDetails;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
import androidx.preference.Preference;
|
||||
|
||||
/**
|
||||
* Manager that responsible for updating high usage preference and handling preference click.
|
||||
*/
|
||||
public class AnomalySummaryPreferenceController {
|
||||
private static final String TAG = "HighUsagePreferenceController";
|
||||
|
||||
public static final String ANOMALY_KEY = "high_usage";
|
||||
|
||||
private static final int REQUEST_ANOMALY_ACTION = 0;
|
||||
private InstrumentedPreferenceFragment mFragment;
|
||||
@VisibleForTesting
|
||||
Preference mAnomalyPreference;
|
||||
@VisibleForTesting
|
||||
List<Anomaly> mAnomalies;
|
||||
@VisibleForTesting
|
||||
BatteryUtils mBatteryUtils;
|
||||
private SettingsActivity mSettingsActivity;
|
||||
|
||||
/**
|
||||
* Metrics key about fragment that create this controller
|
||||
*
|
||||
* @see com.android.internal.logging.nano.MetricsProto.MetricsEvent
|
||||
*/
|
||||
private int mMetricsKey;
|
||||
|
||||
public AnomalySummaryPreferenceController(SettingsActivity activity,
|
||||
InstrumentedPreferenceFragment fragment) {
|
||||
mFragment = fragment;
|
||||
mSettingsActivity = activity;
|
||||
mAnomalyPreference = mFragment.getPreferenceScreen().findPreference(ANOMALY_KEY);
|
||||
mMetricsKey = fragment.getMetricsCategory();
|
||||
mBatteryUtils = BatteryUtils.getInstance(activity.getApplicationContext());
|
||||
hideHighUsagePreference();
|
||||
}
|
||||
|
||||
public boolean onPreferenceTreeClick(Preference preference) {
|
||||
if (mAnomalies != null && ANOMALY_KEY.equals(preference.getKey())) {
|
||||
if (mAnomalies.size() == 1) {
|
||||
final Anomaly anomaly = mAnomalies.get(0);
|
||||
AnomalyDialogFragment dialogFragment = AnomalyDialogFragment.newInstance(anomaly,
|
||||
mMetricsKey);
|
||||
dialogFragment.setTargetFragment(mFragment, REQUEST_ANOMALY_ACTION);
|
||||
dialogFragment.show(mFragment.getFragmentManager(), TAG);
|
||||
} else {
|
||||
PowerUsageAnomalyDetails.startBatteryAbnormalPage(mSettingsActivity, mFragment,
|
||||
mAnomalies);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update anomaly preference based on {@code anomalies}, also store a reference
|
||||
* of {@paramref anomalies}, which would be used in {@link #onPreferenceTreeClick(Preference)}
|
||||
*
|
||||
* @param anomalies used to update the summary, this method will store a reference of it
|
||||
*/
|
||||
public void updateAnomalySummaryPreference(List<Anomaly> anomalies) {
|
||||
final Context context = mFragment.getContext();
|
||||
mAnomalies = anomalies;
|
||||
|
||||
if (!mAnomalies.isEmpty()) {
|
||||
mAnomalyPreference.setVisible(true);
|
||||
final int count = mAnomalies.size();
|
||||
final String title = context.getResources().getQuantityString(
|
||||
R.plurals.power_high_usage_title, count, mAnomalies.get(0).displayName);
|
||||
final String summary = count > 1 ?
|
||||
context.getString(R.string.battery_abnormal_apps_summary, count)
|
||||
: context.getString(
|
||||
mBatteryUtils.getSummaryResIdFromAnomalyType(mAnomalies.get(0).type));
|
||||
|
||||
mAnomalyPreference.setTitle(title);
|
||||
mAnomalyPreference.setSummary(summary);
|
||||
} else {
|
||||
mAnomalyPreference.setVisible(false);
|
||||
}
|
||||
}
|
||||
|
||||
public void hideHighUsagePreference() {
|
||||
mAnomalyPreference.setVisible(false);
|
||||
}
|
||||
}
|
@@ -1,172 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2017 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.anomaly;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Build;
|
||||
import android.util.Pair;
|
||||
import android.util.SparseIntArray;
|
||||
|
||||
import com.android.internal.logging.nano.MetricsProto;
|
||||
import com.android.internal.os.BatteryStatsHelper;
|
||||
import com.android.settings.fuelgauge.anomaly.action.AnomalyAction;
|
||||
import com.android.settings.fuelgauge.anomaly.action.ForceStopAction;
|
||||
import com.android.settings.fuelgauge.anomaly.action.LocationCheckAction;
|
||||
import com.android.settings.fuelgauge.anomaly.action.StopAndBackgroundCheckAction;
|
||||
import com.android.settings.fuelgauge.anomaly.checker.AnomalyDetector;
|
||||
import com.android.settings.fuelgauge.anomaly.checker.BluetoothScanAnomalyDetector;
|
||||
import com.android.settings.fuelgauge.anomaly.checker.WakeLockAnomalyDetector;
|
||||
import com.android.settings.fuelgauge.anomaly.checker.WakeupAlarmAnomalyDetector;
|
||||
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
|
||||
/**
|
||||
* Utility class for anomaly detection
|
||||
*/
|
||||
public class AnomalyUtils {
|
||||
private Context mContext;
|
||||
private static AnomalyUtils sInstance;
|
||||
|
||||
private static final SparseIntArray mMetricArray;
|
||||
static {
|
||||
mMetricArray = new SparseIntArray();
|
||||
mMetricArray.append(Anomaly.AnomalyType.WAKE_LOCK,
|
||||
MetricsProto.MetricsEvent.ANOMALY_TYPE_WAKELOCK);
|
||||
mMetricArray.append(Anomaly.AnomalyType.WAKEUP_ALARM,
|
||||
MetricsProto.MetricsEvent.ANOMALY_TYPE_WAKEUP_ALARM);
|
||||
mMetricArray.append(Anomaly.AnomalyType.BLUETOOTH_SCAN,
|
||||
MetricsProto.MetricsEvent.ANOMALY_TYPE_UNOPTIMIZED_BT);
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
AnomalyUtils(Context context) {
|
||||
mContext = context.getApplicationContext();
|
||||
}
|
||||
|
||||
public static AnomalyUtils getInstance(Context context) {
|
||||
if (sInstance == null) {
|
||||
sInstance = new AnomalyUtils(context);
|
||||
}
|
||||
return sInstance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the corresponding {@link AnomalyAction} according to
|
||||
* {@link com.android.settings.fuelgauge.anomaly.Anomaly}
|
||||
*
|
||||
* @return corresponding {@link AnomalyAction}, or null if cannot find it.
|
||||
*/
|
||||
public AnomalyAction getAnomalyAction(Anomaly anomaly) {
|
||||
switch (anomaly.type) {
|
||||
case Anomaly.AnomalyType.WAKE_LOCK:
|
||||
return new ForceStopAction(mContext);
|
||||
case Anomaly.AnomalyType.WAKEUP_ALARM:
|
||||
if (anomaly.targetSdkVersion >= Build.VERSION_CODES.O
|
||||
|| (anomaly.targetSdkVersion < Build.VERSION_CODES.O
|
||||
&& anomaly.backgroundRestrictionEnabled)) {
|
||||
return new ForceStopAction(mContext);
|
||||
} else {
|
||||
return new StopAndBackgroundCheckAction(mContext);
|
||||
}
|
||||
case Anomaly.AnomalyType.BLUETOOTH_SCAN:
|
||||
return new LocationCheckAction(mContext);
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the corresponding {@link AnomalyDetector} according to
|
||||
* {@link com.android.settings.fuelgauge.anomaly.Anomaly.AnomalyType}
|
||||
*
|
||||
* @return corresponding {@link AnomalyDetector}, or null if cannot find it.
|
||||
*/
|
||||
public AnomalyDetector getAnomalyDetector(@Anomaly.AnomalyType int anomalyType) {
|
||||
switch (anomalyType) {
|
||||
case Anomaly.AnomalyType.WAKE_LOCK:
|
||||
return new WakeLockAnomalyDetector(mContext);
|
||||
case Anomaly.AnomalyType.WAKEUP_ALARM:
|
||||
return new WakeupAlarmAnomalyDetector(mContext);
|
||||
case Anomaly.AnomalyType.BLUETOOTH_SCAN:
|
||||
return new BluetoothScanAnomalyDetector(mContext);
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Detect whether application with {@code targetPackageName} has anomaly. When
|
||||
* {@code targetPackageName} is null, start detection among all the applications.
|
||||
*
|
||||
* @param batteryStatsHelper contains battery stats, used to detect anomaly
|
||||
* @param policy contains configuration about anomaly check
|
||||
* @param targetPackageName represents the app need to be detected
|
||||
* @return the list of anomalies
|
||||
*/
|
||||
public List<Anomaly> detectAnomalies(BatteryStatsHelper batteryStatsHelper,
|
||||
AnomalyDetectionPolicy policy, String targetPackageName) {
|
||||
final List<Anomaly> anomalies = new ArrayList<>();
|
||||
for (@Anomaly.AnomalyType int type : Anomaly.ANOMALY_TYPE_LIST) {
|
||||
if (policy.isAnomalyDetectorEnabled(type)) {
|
||||
anomalies.addAll(getAnomalyDetector(type).detectAnomalies(
|
||||
batteryStatsHelper, targetPackageName));
|
||||
}
|
||||
}
|
||||
|
||||
return anomalies;
|
||||
}
|
||||
|
||||
/**
|
||||
* Log the list of {@link Anomaly} using {@link MetricsFeatureProvider}, which contains
|
||||
* anomaly type, package name, field_context, field_action_type
|
||||
*
|
||||
* @param provider provider to do the logging
|
||||
* @param anomalies contains the data to log
|
||||
* @param contextId which page invoke this logging
|
||||
* @see #logAnomaly(MetricsFeatureProvider, Anomaly, int)
|
||||
*/
|
||||
public void logAnomalies(MetricsFeatureProvider provider, List<Anomaly> anomalies,
|
||||
int contextId) {
|
||||
for (int i = 0, size = anomalies.size(); i < size; i++) {
|
||||
logAnomaly(provider, anomalies.get(i), contextId);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Log the {@link Anomaly} using {@link MetricsFeatureProvider}, which contains
|
||||
* anomaly type, package name, field_context, field_action_type
|
||||
*
|
||||
* @param provider provider to do the logging
|
||||
* @param anomaly contains the data to log
|
||||
* @param contextId which page invoke this logging
|
||||
* @see #logAnomalies(MetricsFeatureProvider, List, int)
|
||||
*/
|
||||
public void logAnomaly(MetricsFeatureProvider provider, Anomaly anomaly, int contextId) {
|
||||
provider.action(
|
||||
mContext,
|
||||
mMetricArray.get(anomaly.type, MetricsProto.MetricsEvent.VIEW_UNKNOWN),
|
||||
anomaly.packageName,
|
||||
Pair.create(MetricsProto.MetricsEvent.FIELD_CONTEXT, contextId),
|
||||
Pair.create(MetricsProto.MetricsEvent.FIELD_ANOMALY_ACTION_TYPE,
|
||||
getAnomalyAction(anomaly).getActionType()));
|
||||
}
|
||||
|
||||
}
|
@@ -1,63 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2017 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.anomaly.action;
|
||||
|
||||
import android.content.Context;
|
||||
import android.util.Pair;
|
||||
|
||||
import com.android.internal.logging.nano.MetricsProto;
|
||||
import com.android.settings.fuelgauge.anomaly.Anomaly;
|
||||
import com.android.settings.overlay.FeatureFactory;
|
||||
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
|
||||
|
||||
/**
|
||||
* Abstract class for anomaly action, which is triggered if we need to handle the anomaly
|
||||
*/
|
||||
public abstract class AnomalyAction {
|
||||
protected Context mContext;
|
||||
protected int mActionMetricKey;
|
||||
|
||||
private MetricsFeatureProvider mMetricsFeatureProvider;
|
||||
|
||||
public AnomalyAction(Context context) {
|
||||
mContext = context;
|
||||
mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider();
|
||||
}
|
||||
|
||||
/**
|
||||
* handle the action when user clicks positive button
|
||||
*
|
||||
* @param anomaly about the app that we need to handle
|
||||
* @param contextMetricsKey key for the page that invokes the action
|
||||
* @see com.android.internal.logging.nano.MetricsProto
|
||||
*/
|
||||
public void handlePositiveAction(Anomaly anomaly, int contextMetricsKey) {
|
||||
mMetricsFeatureProvider.action(mContext, mActionMetricKey, anomaly.packageName,
|
||||
Pair.create(MetricsProto.MetricsEvent.FIELD_CONTEXT, contextMetricsKey));
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether the action is active for {@code anomaly}
|
||||
*
|
||||
* @param anomaly about the app that we need to handle
|
||||
* @return {@code true} if action is active, otherwise return {@code false}
|
||||
*/
|
||||
public abstract boolean isActionActive(Anomaly anomaly);
|
||||
|
||||
@Anomaly.AnomalyActionType
|
||||
public abstract int getActionType();
|
||||
}
|
@@ -1,65 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2017 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.anomaly.action;
|
||||
|
||||
import android.app.AppOpsManager;
|
||||
import android.content.Context;
|
||||
import android.os.Build;
|
||||
|
||||
import com.android.internal.logging.nano.MetricsProto;
|
||||
import com.android.settings.fuelgauge.BatteryUtils;
|
||||
import com.android.settings.fuelgauge.anomaly.Anomaly;
|
||||
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
|
||||
/**
|
||||
* Background check action for anomaly app, which means to stop app running in the background
|
||||
*/
|
||||
public class BackgroundCheckAction extends AnomalyAction {
|
||||
|
||||
private AppOpsManager mAppOpsManager;
|
||||
@VisibleForTesting
|
||||
BatteryUtils mBatteryUtils;
|
||||
|
||||
public BackgroundCheckAction(Context context) {
|
||||
super(context);
|
||||
mAppOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
|
||||
mActionMetricKey = MetricsProto.MetricsEvent.ACTION_APP_BACKGROUND_CHECK;
|
||||
mBatteryUtils = BatteryUtils.getInstance(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handlePositiveAction(Anomaly anomaly, int contextMetricsKey) {
|
||||
super.handlePositiveAction(anomaly, contextMetricsKey);
|
||||
if (anomaly.targetSdkVersion < Build.VERSION_CODES.O) {
|
||||
mAppOpsManager.setMode(AppOpsManager.OP_RUN_IN_BACKGROUND, anomaly.uid,
|
||||
anomaly.packageName,
|
||||
AppOpsManager.MODE_IGNORED);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isActionActive(Anomaly anomaly) {
|
||||
return !mBatteryUtils.isBackgroundRestrictionEnabled(anomaly.targetSdkVersion, anomaly.uid,
|
||||
anomaly.packageName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getActionType() {
|
||||
return Anomaly.AnomalyActionType.BACKGROUND_CHECK;
|
||||
}
|
||||
}
|
@@ -1,68 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2017 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.anomaly.action;
|
||||
|
||||
import android.app.ActivityManager;
|
||||
import android.content.Context;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.util.Log;
|
||||
|
||||
import com.android.internal.logging.nano.MetricsProto;
|
||||
import com.android.settings.fuelgauge.anomaly.Anomaly;
|
||||
|
||||
/**
|
||||
* Force stop action for anomaly app, which means to stop the app which causes anomaly
|
||||
*/
|
||||
public class ForceStopAction extends AnomalyAction {
|
||||
private static final String TAG = "ForceStopAction";
|
||||
|
||||
private ActivityManager mActivityManager;
|
||||
private PackageManager mPackageManager;
|
||||
|
||||
public ForceStopAction(Context context) {
|
||||
super(context);
|
||||
mActivityManager = (ActivityManager) context.getSystemService(
|
||||
Context.ACTIVITY_SERVICE);
|
||||
mPackageManager = context.getPackageManager();
|
||||
mActionMetricKey = MetricsProto.MetricsEvent.ACTION_APP_FORCE_STOP;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handlePositiveAction(Anomaly anomaly, int contextMetricsKey) {
|
||||
super.handlePositiveAction(anomaly, contextMetricsKey);
|
||||
|
||||
mActivityManager.forceStopPackage(anomaly.packageName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isActionActive(Anomaly anomaly) {
|
||||
try {
|
||||
ApplicationInfo info = mPackageManager.getApplicationInfo(anomaly.packageName,
|
||||
PackageManager.GET_META_DATA);
|
||||
return (info.flags & ApplicationInfo.FLAG_STOPPED) == 0;
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
Log.e(TAG, "Cannot find info for app: " + anomaly.packageName);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getActionType() {
|
||||
return Anomaly.AnomalyActionType.FORCE_STOP;
|
||||
}
|
||||
}
|
@@ -1,73 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2017 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.anomaly.action;
|
||||
|
||||
import android.Manifest;
|
||||
import android.content.Context;
|
||||
import android.content.pm.permission.RuntimePermissionPresenter;
|
||||
|
||||
import com.android.internal.logging.nano.MetricsProto;
|
||||
import com.android.settings.fuelgauge.anomaly.Anomaly;
|
||||
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
import androidx.core.content.PermissionChecker;
|
||||
|
||||
/**
|
||||
* Location action for anomaly app, which means to turn off location permission for this app
|
||||
*/
|
||||
public class LocationCheckAction extends AnomalyAction {
|
||||
|
||||
private static final String TAG = "LocationCheckAction";
|
||||
|
||||
private final RuntimePermissionPresenter mRuntimePermissionPresenter;
|
||||
|
||||
public LocationCheckAction(Context context) {
|
||||
this(context, RuntimePermissionPresenter.getInstance(context));
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
LocationCheckAction(Context context, RuntimePermissionPresenter runtimePermissionPresenter) {
|
||||
super(context);
|
||||
mRuntimePermissionPresenter = runtimePermissionPresenter;
|
||||
mActionMetricKey = MetricsProto.MetricsEvent.ACTION_APP_LOCATION_CHECK;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handlePositiveAction(Anomaly anomaly, int contextMetricsKey) {
|
||||
super.handlePositiveAction(anomaly, contextMetricsKey);
|
||||
mRuntimePermissionPresenter.revokeRuntimePermission(anomaly.packageName,
|
||||
Manifest.permission.ACCESS_COARSE_LOCATION);
|
||||
mRuntimePermissionPresenter.revokeRuntimePermission(anomaly.packageName,
|
||||
Manifest.permission.ACCESS_FINE_LOCATION);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isActionActive(Anomaly anomaly) {
|
||||
return isPermissionGranted(anomaly, Manifest.permission.ACCESS_COARSE_LOCATION)
|
||||
|| isPermissionGranted(anomaly, Manifest.permission.ACCESS_FINE_LOCATION);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getActionType() {
|
||||
return Anomaly.AnomalyActionType.LOCATION_CHECK;
|
||||
}
|
||||
|
||||
private boolean isPermissionGranted(Anomaly anomaly, String permission) {
|
||||
return PermissionChecker.checkPermission(mContext, permission, -1, anomaly.uid,
|
||||
anomaly.packageName) == PermissionChecker.PERMISSION_GRANTED;
|
||||
}
|
||||
}
|
@@ -1,67 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2017 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.anomaly.action;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.android.internal.logging.nano.MetricsProto;
|
||||
import com.android.settings.fuelgauge.anomaly.Anomaly;
|
||||
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
|
||||
/**
|
||||
* Force stop and background check action for anomaly app, this action will
|
||||
* 1. Force stop the app
|
||||
* 2. Turn on background check
|
||||
*/
|
||||
public class StopAndBackgroundCheckAction extends AnomalyAction {
|
||||
@VisibleForTesting
|
||||
ForceStopAction mForceStopAction;
|
||||
@VisibleForTesting
|
||||
BackgroundCheckAction mBackgroundCheckAction;
|
||||
|
||||
public StopAndBackgroundCheckAction(Context context) {
|
||||
this(context, new ForceStopAction(context), new BackgroundCheckAction(context));
|
||||
mActionMetricKey = MetricsProto.MetricsEvent.ACTION_APP_STOP_AND_BACKGROUND_CHECK;
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
StopAndBackgroundCheckAction(Context context, ForceStopAction forceStopAction,
|
||||
BackgroundCheckAction backgroundCheckAction) {
|
||||
super(context);
|
||||
mForceStopAction = forceStopAction;
|
||||
mBackgroundCheckAction = backgroundCheckAction;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handlePositiveAction(Anomaly anomaly, int metricsKey) {
|
||||
super.handlePositiveAction(anomaly, metricsKey);
|
||||
mForceStopAction.handlePositiveAction(anomaly, metricsKey);
|
||||
mBackgroundCheckAction.handlePositiveAction(anomaly, metricsKey);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isActionActive(Anomaly anomaly) {
|
||||
return mForceStopAction.isActionActive(anomaly)
|
||||
&& mBackgroundCheckAction.isActionActive(anomaly);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getActionType() {
|
||||
return Anomaly.AnomalyActionType.STOP_AND_BACKGROUND_CHECK;
|
||||
}
|
||||
}
|
@@ -1,46 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2017 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.anomaly.checker;
|
||||
|
||||
import android.annotation.Nullable;
|
||||
|
||||
import com.android.internal.os.BatteryStatsHelper;
|
||||
import com.android.settings.fuelgauge.anomaly.Anomaly;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface AnomalyDetector {
|
||||
/**
|
||||
* Detect whether there is anomaly among all the applications in the device
|
||||
*
|
||||
* @param batteryStatsHelper used to detect the anomaly
|
||||
* @return anomaly list
|
||||
*/
|
||||
List<Anomaly> detectAnomalies(BatteryStatsHelper batteryStatsHelper);
|
||||
|
||||
/**
|
||||
* Detect whether application with {@code targetPackageName} has anomaly. When
|
||||
* {@code targetPackageName} is null, start detection among all the applications.
|
||||
*
|
||||
* @param batteryStatsHelper used to detect the anomaly
|
||||
* @param targetPackageName represents the app need to be detected
|
||||
* @return anomaly list
|
||||
*/
|
||||
List<Anomaly> detectAnomalies(BatteryStatsHelper batteryStatsHelper,
|
||||
@Nullable String targetPackageName);
|
||||
}
|
@@ -1,113 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2017 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.anomaly.checker;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.BatteryStats;
|
||||
import android.os.SystemClock;
|
||||
|
||||
import com.android.internal.os.BatterySipper;
|
||||
import com.android.internal.os.BatteryStatsHelper;
|
||||
import com.android.settings.Utils;
|
||||
import com.android.settings.fuelgauge.BatteryUtils;
|
||||
import com.android.settings.fuelgauge.anomaly.Anomaly;
|
||||
import com.android.settings.fuelgauge.anomaly.AnomalyDetectionPolicy;
|
||||
import com.android.settings.fuelgauge.anomaly.AnomalyUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
|
||||
/**
|
||||
* Check whether apps have unoptimized bluetooth scanning in the background
|
||||
*/
|
||||
public class BluetoothScanAnomalyDetector implements AnomalyDetector {
|
||||
private static final String TAG = "BluetoothScanAnomalyDetector";
|
||||
@VisibleForTesting
|
||||
BatteryUtils mBatteryUtils;
|
||||
private long mBluetoothScanningThreshold;
|
||||
private Context mContext;
|
||||
private AnomalyUtils mAnomalyUtils;
|
||||
|
||||
public BluetoothScanAnomalyDetector(Context context) {
|
||||
this(context, new AnomalyDetectionPolicy(context), AnomalyUtils.getInstance(context));
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
BluetoothScanAnomalyDetector(Context context, AnomalyDetectionPolicy policy,
|
||||
AnomalyUtils anomalyUtils) {
|
||||
mContext = context;
|
||||
mBatteryUtils = BatteryUtils.getInstance(context);
|
||||
mBluetoothScanningThreshold = policy.bluetoothScanThreshold;
|
||||
mAnomalyUtils = anomalyUtils;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Anomaly> detectAnomalies(BatteryStatsHelper batteryStatsHelper) {
|
||||
// Detect all apps if targetPackageName is null
|
||||
return detectAnomalies(batteryStatsHelper, null /* targetPackageName */);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Anomaly> detectAnomalies(BatteryStatsHelper batteryStatsHelper,
|
||||
String targetPackageName) {
|
||||
final List<BatterySipper> batterySippers = batteryStatsHelper.getUsageList();
|
||||
final List<Anomaly> anomalies = new ArrayList<>();
|
||||
final int targetUid = mBatteryUtils.getPackageUid(targetPackageName);
|
||||
final long elapsedRealtimeMs = SystemClock.elapsedRealtime();
|
||||
|
||||
for (int i = 0, size = batterySippers.size(); i < size; i++) {
|
||||
final BatterySipper sipper = batterySippers.get(i);
|
||||
final BatteryStats.Uid uid = sipper.uidObj;
|
||||
if (uid == null
|
||||
|| mBatteryUtils.shouldHideSipper(sipper)
|
||||
|| (targetUid != BatteryUtils.UID_NULL && targetUid != uid.getUid())) {
|
||||
continue;
|
||||
}
|
||||
|
||||
final long bluetoothTimeMs = getBluetoothUnoptimizedBgTimeMs(uid, elapsedRealtimeMs);
|
||||
if (bluetoothTimeMs > mBluetoothScanningThreshold) {
|
||||
final String packageName = mBatteryUtils.getPackageName(uid.getUid());
|
||||
final CharSequence displayName = Utils.getApplicationLabel(mContext,
|
||||
packageName);
|
||||
|
||||
Anomaly anomaly = new Anomaly.Builder()
|
||||
.setUid(uid.getUid())
|
||||
.setType(Anomaly.AnomalyType.BLUETOOTH_SCAN)
|
||||
.setDisplayName(displayName)
|
||||
.setPackageName(packageName)
|
||||
.setBluetoothScanningTimeMs(bluetoothTimeMs)
|
||||
.build();
|
||||
|
||||
if (mAnomalyUtils.getAnomalyAction(anomaly).isActionActive(anomaly)) {
|
||||
anomalies.add(anomaly);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return anomalies;
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public long getBluetoothUnoptimizedBgTimeMs(BatteryStats.Uid uid, long elapsedRealtimeMs) {
|
||||
BatteryStats.Timer timer = uid.getBluetoothUnoptimizedScanBackgroundTimer();
|
||||
|
||||
return timer != null ? timer.getTotalDurationMsLocked(elapsedRealtimeMs) : 0;
|
||||
}
|
||||
|
||||
}
|
@@ -1,126 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2017 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.anomaly.checker;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.BatteryStats;
|
||||
import android.os.SystemClock;
|
||||
|
||||
import com.android.internal.os.BatterySipper;
|
||||
import com.android.internal.os.BatteryStatsHelper;
|
||||
import com.android.settings.Utils;
|
||||
import com.android.settings.fuelgauge.BatteryUtils;
|
||||
import com.android.settings.fuelgauge.anomaly.Anomaly;
|
||||
import com.android.settings.fuelgauge.anomaly.AnomalyDetectionPolicy;
|
||||
import com.android.settings.fuelgauge.anomaly.AnomalyUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
|
||||
/**
|
||||
* Check whether apps holding wakelock too long
|
||||
*/
|
||||
public class WakeLockAnomalyDetector implements AnomalyDetector {
|
||||
private static final String TAG = "WakeLockAnomalyChecker";
|
||||
@VisibleForTesting
|
||||
BatteryUtils mBatteryUtils;
|
||||
@VisibleForTesting
|
||||
long mWakeLockThresholdMs;
|
||||
private PackageManager mPackageManager;
|
||||
private Context mContext;
|
||||
private AnomalyUtils mAnomalyUtils;
|
||||
|
||||
public WakeLockAnomalyDetector(Context context) {
|
||||
this(context, new AnomalyDetectionPolicy(context), AnomalyUtils.getInstance(context));
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
WakeLockAnomalyDetector(Context context, AnomalyDetectionPolicy policy,
|
||||
AnomalyUtils anomalyUtils) {
|
||||
mContext = context;
|
||||
mPackageManager = context.getPackageManager();
|
||||
mBatteryUtils = BatteryUtils.getInstance(context);
|
||||
mAnomalyUtils = anomalyUtils;
|
||||
mWakeLockThresholdMs = policy.wakeLockThreshold;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Anomaly> detectAnomalies(BatteryStatsHelper batteryStatsHelper) {
|
||||
// Detect all apps if targetPackageName is null
|
||||
return detectAnomalies(batteryStatsHelper, null /* targetPackageName */);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Anomaly> detectAnomalies(BatteryStatsHelper batteryStatsHelper,
|
||||
String targetPackageName) {
|
||||
final List<BatterySipper> batterySippers = batteryStatsHelper.getUsageList();
|
||||
final List<Anomaly> anomalies = new ArrayList<>();
|
||||
final long rawRealtime = SystemClock.elapsedRealtime();
|
||||
final int targetUid = mBatteryUtils.getPackageUid(targetPackageName);
|
||||
|
||||
// Check the app one by one
|
||||
for (int i = 0, size = batterySippers.size(); i < size; i++) {
|
||||
final BatterySipper sipper = batterySippers.get(i);
|
||||
final BatteryStats.Uid uid = sipper.uidObj;
|
||||
if (uid == null
|
||||
|| mBatteryUtils.shouldHideSipper(sipper)
|
||||
|| (targetUid != BatteryUtils.UID_NULL && targetUid != uid.getUid())) {
|
||||
continue;
|
||||
}
|
||||
|
||||
final long currentDurationMs = getCurrentDurationMs(uid, rawRealtime);
|
||||
final long backgroundDurationMs = getBackgroundTotalDurationMs(uid, rawRealtime);
|
||||
|
||||
if (backgroundDurationMs > mWakeLockThresholdMs && currentDurationMs != 0) {
|
||||
final String packageName = mBatteryUtils.getPackageName(uid.getUid());
|
||||
final CharSequence displayName = Utils.getApplicationLabel(mContext,
|
||||
packageName);
|
||||
|
||||
Anomaly anomaly = new Anomaly.Builder()
|
||||
.setUid(uid.getUid())
|
||||
.setType(Anomaly.AnomalyType.WAKE_LOCK)
|
||||
.setDisplayName(displayName)
|
||||
.setPackageName(packageName)
|
||||
.setWakeLockTimeMs(backgroundDurationMs)
|
||||
.build();
|
||||
|
||||
if (mAnomalyUtils.getAnomalyAction(anomaly).isActionActive(anomaly)) {
|
||||
anomalies.add(anomaly);
|
||||
}
|
||||
}
|
||||
}
|
||||
return anomalies;
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
long getCurrentDurationMs(BatteryStats.Uid uid, long elapsedRealtimeMs) {
|
||||
BatteryStats.Timer timer = uid.getAggregatedPartialWakelockTimer();
|
||||
|
||||
return timer != null ? timer.getCurrentDurationMsLocked(elapsedRealtimeMs) : 0;
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
long getBackgroundTotalDurationMs(BatteryStats.Uid uid, long elapsedRealtimeMs) {
|
||||
BatteryStats.Timer timer = uid.getAggregatedPartialWakelockTimer();
|
||||
BatteryStats.Timer subTimer = timer != null ? timer.getSubTimer() : null;
|
||||
|
||||
return subTimer != null ? subTimer.getTotalDurationMsLocked(elapsedRealtimeMs) : 0;
|
||||
}
|
||||
}
|
@@ -1,143 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2017 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.anomaly.checker;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.BatteryStats;
|
||||
import android.text.format.DateUtils;
|
||||
import android.util.ArrayMap;
|
||||
|
||||
import com.android.internal.os.BatterySipper;
|
||||
import com.android.internal.os.BatteryStatsHelper;
|
||||
import com.android.settings.Utils;
|
||||
import com.android.settings.fuelgauge.BatteryUtils;
|
||||
import com.android.settings.fuelgauge.anomaly.Anomaly;
|
||||
import com.android.settings.fuelgauge.anomaly.AnomalyDetectionPolicy;
|
||||
import com.android.settings.fuelgauge.anomaly.AnomalyUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
|
||||
/**
|
||||
* Check whether apps has too many wakeup alarms
|
||||
*/
|
||||
public class WakeupAlarmAnomalyDetector implements AnomalyDetector {
|
||||
private static final String TAG = "WakeupAlarmAnomalyDetector";
|
||||
@VisibleForTesting
|
||||
BatteryUtils mBatteryUtils;
|
||||
private long mWakeupAlarmThreshold;
|
||||
private Set<String> mWakeupBlacklistedTags;
|
||||
private Context mContext;
|
||||
private AnomalyUtils mAnomalyUtils;
|
||||
|
||||
public WakeupAlarmAnomalyDetector(Context context) {
|
||||
this(context, new AnomalyDetectionPolicy(context), AnomalyUtils.getInstance(context));
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
WakeupAlarmAnomalyDetector(Context context, AnomalyDetectionPolicy policy,
|
||||
AnomalyUtils anomalyUtils) {
|
||||
mContext = context;
|
||||
mBatteryUtils = BatteryUtils.getInstance(context);
|
||||
mAnomalyUtils = anomalyUtils;
|
||||
mWakeupAlarmThreshold = policy.wakeupAlarmThreshold;
|
||||
mWakeupBlacklistedTags = policy.wakeupBlacklistedTags;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Anomaly> detectAnomalies(BatteryStatsHelper batteryStatsHelper) {
|
||||
// Detect all apps if targetPackageName is null
|
||||
return detectAnomalies(batteryStatsHelper, null /* targetPackageName */);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Anomaly> detectAnomalies(BatteryStatsHelper batteryStatsHelper,
|
||||
String targetPackageName) {
|
||||
final List<BatterySipper> batterySippers = batteryStatsHelper.getUsageList();
|
||||
final List<Anomaly> anomalies = new ArrayList<>();
|
||||
final double totalRunningHours = mBatteryUtils.calculateRunningTimeBasedOnStatsType(
|
||||
batteryStatsHelper, BatteryStats.STATS_SINCE_CHARGED)
|
||||
/ (double) DateUtils.HOUR_IN_MILLIS;
|
||||
final int targetUid = mBatteryUtils.getPackageUid(targetPackageName);
|
||||
|
||||
if (totalRunningHours >= 1) {
|
||||
for (int i = 0, size = batterySippers.size(); i < size; i++) {
|
||||
final BatterySipper sipper = batterySippers.get(i);
|
||||
final BatteryStats.Uid uid = sipper.uidObj;
|
||||
if (uid == null
|
||||
|| mBatteryUtils.shouldHideSipper(sipper)
|
||||
|| (targetUid != BatteryUtils.UID_NULL && targetUid != uid.getUid())) {
|
||||
continue;
|
||||
}
|
||||
|
||||
final int wakeupAlarmCount = (int) (getWakeupAlarmCountFromUid(uid)
|
||||
/ totalRunningHours);
|
||||
if (wakeupAlarmCount > mWakeupAlarmThreshold) {
|
||||
final String packageName = mBatteryUtils.getPackageName(uid.getUid());
|
||||
final CharSequence displayName = Utils.getApplicationLabel(mContext,
|
||||
packageName);
|
||||
final int targetSdkVersion = mBatteryUtils.getTargetSdkVersion(packageName);
|
||||
|
||||
Anomaly anomaly = new Anomaly.Builder()
|
||||
.setUid(uid.getUid())
|
||||
.setType(Anomaly.AnomalyType.WAKEUP_ALARM)
|
||||
.setDisplayName(displayName)
|
||||
.setPackageName(packageName)
|
||||
.setTargetSdkVersion(targetSdkVersion)
|
||||
.setBackgroundRestrictionEnabled(
|
||||
mBatteryUtils.isBackgroundRestrictionEnabled(targetSdkVersion,
|
||||
uid.getUid(), packageName))
|
||||
.setWakeupAlarmCount(wakeupAlarmCount)
|
||||
.build();
|
||||
|
||||
if (mAnomalyUtils.getAnomalyAction(anomaly).isActionActive(anomaly)) {
|
||||
anomalies.add(anomaly);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return anomalies;
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
int getWakeupAlarmCountFromUid(BatteryStats.Uid uid) {
|
||||
int wakeups = 0;
|
||||
final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
|
||||
= uid.getPackageStats();
|
||||
for (int ipkg = packageStats.size() - 1; ipkg >= 0; ipkg--) {
|
||||
final BatteryStats.Uid.Pkg ps = packageStats.valueAt(ipkg);
|
||||
final ArrayMap<String, ? extends BatteryStats.Counter> alarms =
|
||||
ps.getWakeupAlarmStats();
|
||||
for (Map.Entry<String, ? extends BatteryStats.Counter> alarm : alarms.entrySet()) {
|
||||
if (mWakeupBlacklistedTags != null
|
||||
&& mWakeupBlacklistedTags.contains(alarm.getKey())) {
|
||||
continue;
|
||||
}
|
||||
int count = alarm.getValue().getCountLocked(BatteryStats.STATS_SINCE_CHARGED);
|
||||
wakeups += count;
|
||||
}
|
||||
}
|
||||
|
||||
return wakeups;
|
||||
}
|
||||
|
||||
}
|
@@ -21,8 +21,6 @@ import android.database.sqlite.SQLiteDatabase;
|
||||
import android.database.sqlite.SQLiteOpenHelper;
|
||||
import android.util.Log;
|
||||
|
||||
import com.android.settings.fuelgauge.anomaly.Anomaly;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
|
||||
@@ -62,7 +60,7 @@ public class AnomalyDatabaseHelper extends SQLiteOpenHelper {
|
||||
String UID = "uid";
|
||||
/**
|
||||
* The type of the anomaly app
|
||||
* @see Anomaly.AnomalyType
|
||||
* @see StatsManagerConfig.AnomalyType
|
||||
*/
|
||||
String ANOMALY_TYPE = "anomaly_type";
|
||||
/**
|
||||
|
@@ -21,8 +21,6 @@ import android.os.Parcelable;
|
||||
import android.text.TextUtils;
|
||||
import android.util.ArraySet;
|
||||
|
||||
import com.android.settings.fuelgauge.anomaly.Anomaly;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
@@ -34,7 +32,7 @@ public class AppInfo implements Comparable<AppInfo>, Parcelable {
|
||||
public final String packageName;
|
||||
/**
|
||||
* Anomaly type of the app
|
||||
* @see Anomaly.AnomalyType
|
||||
* @see StatsManagerConfig.AnomalyType
|
||||
*/
|
||||
public final ArraySet<Integer> anomalyTypes;
|
||||
public final long screenOnTimeMs;
|
||||
|
Reference in New Issue
Block a user