• 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.anomaly;
18 
19 import android.content.Context;
20 import android.support.annotation.VisibleForTesting;
21 import android.support.v7.preference.Preference;
22 
23 import com.android.settings.R;
24 import com.android.settings.SettingsActivity;
25 import com.android.settings.core.InstrumentedPreferenceFragment;
26 import com.android.settings.fuelgauge.BatteryUtils;
27 import com.android.settings.fuelgauge.PowerUsageAnomalyDetails;
28 
29 import java.util.List;
30 
31 /**
32  * Manager that responsible for updating high usage preference and handling preference click.
33  */
34 public class AnomalySummaryPreferenceController {
35     private static final String TAG = "HighUsagePreferenceController";
36 
37     public static final String ANOMALY_KEY = "high_usage";
38 
39     private static final int REQUEST_ANOMALY_ACTION = 0;
40     private InstrumentedPreferenceFragment mFragment;
41     @VisibleForTesting
42     Preference mAnomalyPreference;
43     @VisibleForTesting
44     List<Anomaly> mAnomalies;
45     @VisibleForTesting
46     BatteryUtils mBatteryUtils;
47     private SettingsActivity mSettingsActivity;
48 
49     /**
50      * Metrics key about fragment that create this controller
51      *
52      * @see com.android.internal.logging.nano.MetricsProto.MetricsEvent
53      */
54     private int mMetricsKey;
55 
AnomalySummaryPreferenceController(SettingsActivity activity, InstrumentedPreferenceFragment fragment)56     public AnomalySummaryPreferenceController(SettingsActivity activity,
57             InstrumentedPreferenceFragment fragment) {
58         mFragment = fragment;
59         mSettingsActivity = activity;
60         mAnomalyPreference = mFragment.getPreferenceScreen().findPreference(ANOMALY_KEY);
61         mMetricsKey = fragment.getMetricsCategory();
62         mBatteryUtils = BatteryUtils.getInstance(activity.getApplicationContext());
63         hideHighUsagePreference();
64     }
65 
onPreferenceTreeClick(Preference preference)66     public boolean onPreferenceTreeClick(Preference preference) {
67         if (mAnomalies != null && ANOMALY_KEY.equals(preference.getKey())) {
68             if (mAnomalies.size() == 1) {
69                 final Anomaly anomaly = mAnomalies.get(0);
70                 AnomalyDialogFragment dialogFragment = AnomalyDialogFragment.newInstance(anomaly,
71                         mMetricsKey);
72                 dialogFragment.setTargetFragment(mFragment, REQUEST_ANOMALY_ACTION);
73                 dialogFragment.show(mFragment.getFragmentManager(), TAG);
74             } else {
75                 PowerUsageAnomalyDetails.startBatteryAbnormalPage(mSettingsActivity, mFragment,
76                         mAnomalies);
77             }
78             return true;
79         }
80         return false;
81     }
82 
83     /**
84      * Update anomaly preference based on {@code anomalies}, also store a reference
85      * of {@paramref anomalies}, which would be used in {@link #onPreferenceTreeClick(Preference)}
86      *
87      * @param anomalies used to update the summary, this method will store a reference of it
88      */
updateAnomalySummaryPreference(List<Anomaly> anomalies)89     public void updateAnomalySummaryPreference(List<Anomaly> anomalies) {
90         final Context context = mFragment.getContext();
91         mAnomalies = anomalies;
92 
93         if (!mAnomalies.isEmpty()) {
94             mAnomalyPreference.setVisible(true);
95             final int count = mAnomalies.size();
96             final String title = context.getResources().getQuantityString(
97                     R.plurals.power_high_usage_title, count, mAnomalies.get(0).displayName);
98             final String summary = count > 1 ?
99                     context.getString(R.string.battery_abnormal_apps_summary, count)
100                     : context.getString(
101                             mBatteryUtils.getSummaryResIdFromAnomalyType(mAnomalies.get(0).type));
102 
103             mAnomalyPreference.setTitle(title);
104             mAnomalyPreference.setSummary(summary);
105         } else {
106             mAnomalyPreference.setVisible(false);
107         }
108     }
109 
hideHighUsagePreference()110     public void hideHighUsagePreference() {
111         mAnomalyPreference.setVisible(false);
112     }
113 }
114