• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.settings.fuelgauge;
18 
19 import android.content.Context;
20 import android.content.pm.PackageManager;
21 import android.graphics.drawable.Drawable;
22 import android.os.Bundle;
23 import android.os.UserHandle;
24 import android.support.v7.preference.Preference;
25 import android.support.v7.preference.PreferenceGroup;
26 import android.util.IconDrawableFactory;
27 
28 import com.android.internal.annotations.VisibleForTesting;
29 import com.android.internal.logging.nano.MetricsProto;
30 import com.android.settings.R;
31 import com.android.settings.SettingsActivity;
32 import com.android.settings.Utils;
33 import com.android.settings.core.InstrumentedPreferenceFragment;
34 import com.android.settings.core.SubSettingLauncher;
35 import com.android.settings.dashboard.DashboardFragment;
36 import com.android.settings.fuelgauge.anomaly.Anomaly;
37 import com.android.settings.fuelgauge.anomaly.AnomalyDialogFragment;
38 import com.android.settings.fuelgauge.anomaly.AnomalyPreference;
39 import com.android.settingslib.core.AbstractPreferenceController;
40 
41 import java.util.List;
42 
43 /**
44  * Fragment to show a list of anomaly apps, where user could handle these anomalies
45  */
46 public class PowerUsageAnomalyDetails extends DashboardFragment implements
47         AnomalyDialogFragment.AnomalyDialogListener {
48 
49     public static final String TAG = "PowerAbnormalUsageDetail";
50     @VisibleForTesting
51     static final String EXTRA_ANOMALY_LIST = "anomaly_list";
52     private static final int REQUEST_ANOMALY_ACTION = 0;
53     private static final String KEY_PREF_ANOMALY_LIST = "app_abnormal_list";
54 
55     @VisibleForTesting
56     List<Anomaly> mAnomalies;
57     @VisibleForTesting
58     PreferenceGroup mAbnormalListGroup;
59     @VisibleForTesting
60     PackageManager mPackageManager;
61     @VisibleForTesting
62     BatteryUtils mBatteryUtils;
63     @VisibleForTesting
64     IconDrawableFactory mIconDrawableFactory;
65 
startBatteryAbnormalPage(SettingsActivity caller, InstrumentedPreferenceFragment fragment, List<Anomaly> anomalies)66     public static void startBatteryAbnormalPage(SettingsActivity caller,
67             InstrumentedPreferenceFragment fragment, List<Anomaly> anomalies) {
68         Bundle args = new Bundle();
69         args.putParcelableList(EXTRA_ANOMALY_LIST, anomalies);
70 
71         new SubSettingLauncher(caller)
72                 .setDestination(PowerUsageAnomalyDetails.class.getName())
73                 .setTitle(R.string.battery_abnormal_details_title)
74                 .setArguments(args)
75                 .setSourceMetricsCategory(fragment.getMetricsCategory())
76                 .launch();
77     }
78 
79     @Override
onCreate(Bundle icicle)80     public void onCreate(Bundle icicle) {
81         super.onCreate(icicle);
82         final Context context = getContext();
83 
84         mAnomalies = getArguments().getParcelableArrayList(EXTRA_ANOMALY_LIST);
85         mAbnormalListGroup = (PreferenceGroup) findPreference(KEY_PREF_ANOMALY_LIST);
86         mPackageManager = context.getPackageManager();
87         mIconDrawableFactory = IconDrawableFactory.newInstance(context);
88         mBatteryUtils = BatteryUtils.getInstance(context);
89     }
90 
91     @Override
onResume()92     public void onResume() {
93         super.onResume();
94 
95         refreshUi();
96     }
97 
98     @Override
onPreferenceTreeClick(Preference preference)99     public boolean onPreferenceTreeClick(Preference preference) {
100         if (preference instanceof AnomalyPreference) {
101             AnomalyPreference anomalyPreference = (AnomalyPreference) preference;
102             final Anomaly anomaly = anomalyPreference.getAnomaly();
103 
104             AnomalyDialogFragment dialogFragment = AnomalyDialogFragment.newInstance(anomaly,
105                     MetricsProto.MetricsEvent.FUELGAUGE_ANOMALY_DETAIL);
106             dialogFragment.setTargetFragment(this, REQUEST_ANOMALY_ACTION);
107             dialogFragment.show(getFragmentManager(), TAG);
108 
109             return true;
110         }
111 
112         return super.onPreferenceTreeClick(preference);
113     }
114 
115     @Override
getLogTag()116     protected String getLogTag() {
117         return TAG;
118     }
119 
120     @Override
getPreferenceScreenResId()121     protected int getPreferenceScreenResId() {
122         return R.xml.power_abnormal_detail;
123     }
124 
125     @Override
createPreferenceControllers(Context context)126     protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
127         return null;
128     }
129 
130     @Override
getMetricsCategory()131     public int getMetricsCategory() {
132         return MetricsProto.MetricsEvent.FUELGAUGE_ANOMALY_DETAIL;
133     }
134 
refreshUi()135     void refreshUi() {
136         mAbnormalListGroup.removeAll();
137         for (int i = 0, size = mAnomalies.size(); i < size; i++) {
138             final Anomaly anomaly = mAnomalies.get(i);
139             Preference pref = new AnomalyPreference(getPrefContext(), anomaly);
140             pref.setSummary(mBatteryUtils.getSummaryResIdFromAnomalyType(anomaly.type));
141             Drawable icon = getBadgedIcon(anomaly.packageName, UserHandle.getUserId(anomaly.uid));
142             if (icon != null) {
143                 pref.setIcon(icon);
144             }
145 
146             mAbnormalListGroup.addPreference(pref);
147         }
148     }
149 
150     @Override
onAnomalyHandled(Anomaly anomaly)151     public void onAnomalyHandled(Anomaly anomaly) {
152         mAnomalies.remove(anomaly);
153         refreshUi();
154     }
155 
156     @VisibleForTesting
getBadgedIcon(String packageName, int userId)157     Drawable getBadgedIcon(String packageName, int userId) {
158         return Utils.getBadgedIcon(mIconDrawableFactory, mPackageManager, packageName, userId);
159     }
160 }
161