1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11  * KIND, either express or implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */
14 package com.android.settings.fuelgauge;
15 
16 import static com.android.settings.fuelgauge.BatteryBroadcastReceiver.BatteryUpdateType;
17 
18 import android.app.settings.SettingsEnums;
19 import android.content.Context;
20 import android.content.Intent;
21 import android.content.IntentFilter;
22 import android.os.BatteryManager;
23 import android.os.Bundle;
24 import android.provider.SearchIndexableResource;
25 import android.view.Menu;
26 import android.view.MenuInflater;
27 import android.view.MenuItem;
28 
29 import androidx.annotation.VisibleForTesting;
30 
31 import com.android.settings.R;
32 import com.android.settings.SettingsActivity;
33 import com.android.settings.overlay.FeatureFactory;
34 import com.android.settings.search.BaseSearchIndexProvider;
35 import com.android.settingslib.core.AbstractPreferenceController;
36 import com.android.settingslib.search.SearchIndexable;
37 
38 import java.util.ArrayList;
39 import java.util.Arrays;
40 import java.util.List;
41 
42 @SearchIndexable(forTarget = SearchIndexable.ALL & ~SearchIndexable.ARC)
43 public class PowerUsageAdvanced extends PowerUsageBase {
44     private static final String TAG = "AdvancedBatteryUsage";
45     private static final String KEY_BATTERY_GRAPH = "battery_graph";
46     private static final String KEY_APP_LIST = "app_list";
47     private static final String KEY_SHOW_ALL_APPS = "show_all_apps";
48     @VisibleForTesting
49     static final int MENU_TOGGLE_APPS = Menu.FIRST + 1;
50 
51     @VisibleForTesting
52     BatteryHistoryPreference mHistPref;
53     private BatteryUtils mBatteryUtils;
54     private PowerUsageFeatureProvider mPowerUsageFeatureProvider;
55     private BatteryAppListPreferenceController mBatteryAppListPreferenceController;
56     @VisibleForTesting
57     boolean mShowAllApps = false;
58 
59     @Override
onCreate(Bundle icicle)60     public void onCreate(Bundle icicle) {
61         super.onCreate(icicle);
62         final Context context = getContext();
63 
64         mHistPref = (BatteryHistoryPreference) findPreference(KEY_BATTERY_GRAPH);
65         mPowerUsageFeatureProvider = FeatureFactory.getFactory(context)
66                 .getPowerUsageFeatureProvider(context);
67         mBatteryUtils = BatteryUtils.getInstance(context);
68 
69         // init the summary so other preferences won't have unnecessary move
70         updateHistPrefSummary(context);
71         restoreSavedInstance(icicle);
72     }
73 
74     @Override
onDestroy()75     public void onDestroy() {
76         super.onDestroy();
77         if (getActivity().isChangingConfigurations()) {
78             BatteryEntry.clearUidCache();
79         }
80     }
81 
82     @Override
getMetricsCategory()83     public int getMetricsCategory() {
84         return SettingsEnums.FUELGAUGE_BATTERY_HISTORY_DETAIL;
85     }
86 
87     @Override
getLogTag()88     protected String getLogTag() {
89         return TAG;
90     }
91 
92     @Override
getPreferenceScreenResId()93     protected int getPreferenceScreenResId() {
94         return R.xml.power_usage_advanced;
95     }
96 
97     @Override
onCreateOptionsMenu(Menu menu, MenuInflater inflater)98     public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
99         menu.add(Menu.NONE, MENU_TOGGLE_APPS, Menu.NONE,
100                 mShowAllApps ? R.string.hide_extra_apps : R.string.show_all_apps);
101         super.onCreateOptionsMenu(menu, inflater);
102     }
103 
104     @Override
onOptionsItemSelected(MenuItem item)105     public boolean onOptionsItemSelected(MenuItem item) {
106         switch (item.getItemId()) {
107             case MENU_TOGGLE_APPS:
108                 mShowAllApps = !mShowAllApps;
109                 item.setTitle(mShowAllApps ? R.string.hide_extra_apps : R.string.show_all_apps);
110                 mMetricsFeatureProvider.action(getContext(),
111                         SettingsEnums.ACTION_SETTINGS_MENU_BATTERY_APPS_TOGGLE,
112                         mShowAllApps);
113                 restartBatteryStatsLoader(BatteryUpdateType.MANUAL);
114                 return true;
115             default:
116                 return super.onOptionsItemSelected(item);
117         }
118     }
119 
120     @VisibleForTesting
restoreSavedInstance(Bundle savedInstance)121     void restoreSavedInstance(Bundle savedInstance) {
122         if (savedInstance != null) {
123             mShowAllApps = savedInstance.getBoolean(KEY_SHOW_ALL_APPS, false);
124         }
125     }
126 
127     @Override
onSaveInstanceState(Bundle outState)128     public void onSaveInstanceState(Bundle outState) {
129         super.onSaveInstanceState(outState);
130         outState.putBoolean(KEY_SHOW_ALL_APPS, mShowAllApps);
131     }
132 
133     @Override
createPreferenceControllers(Context context)134     protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
135         final List<AbstractPreferenceController> controllers = new ArrayList<>();
136 
137         mBatteryAppListPreferenceController = new BatteryAppListPreferenceController(context,
138                 KEY_APP_LIST, getSettingsLifecycle(), (SettingsActivity) getActivity(), this);
139         controllers.add(mBatteryAppListPreferenceController);
140 
141         return controllers;
142     }
143 
144     @Override
refreshUi(@atteryUpdateType int refreshType)145     protected void refreshUi(@BatteryUpdateType int refreshType) {
146         final Context context = getContext();
147         if (context == null) {
148             return;
149         }
150         updatePreference(mHistPref);
151         updateHistPrefSummary(context);
152 
153         mBatteryAppListPreferenceController.refreshAppListGroup(mStatsHelper, mShowAllApps);
154     }
155 
updateHistPrefSummary(Context context)156     private void updateHistPrefSummary(Context context) {
157         Intent batteryIntent =
158                 context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
159         final boolean plugged = batteryIntent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1) != 0;
160 
161         if (mPowerUsageFeatureProvider.isEnhancedBatteryPredictionEnabled(context) && !plugged) {
162             mHistPref.setBottomSummary(
163                     mPowerUsageFeatureProvider.getAdvancedUsageScreenInfoString());
164         } else {
165             mHistPref.hideBottomSummary();
166         }
167     }
168 
169     public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
170             new BaseSearchIndexProvider() {
171                 @Override
172                 public List<SearchIndexableResource> getXmlResourcesToIndex(
173                         Context context, boolean enabled) {
174                     final SearchIndexableResource sir = new SearchIndexableResource(context);
175                     sir.xmlResId = R.xml.power_usage_advanced;
176                     return Arrays.asList(sir);
177                 }
178 
179                 @Override
180                 public List<AbstractPreferenceController> createPreferenceControllers(
181                         Context context) {
182                     final List<AbstractPreferenceController> controllers = new ArrayList<>();
183                     controllers.add(new BatteryAppListPreferenceController(context,
184                             KEY_APP_LIST, null /* lifecycle */, null /* activity */,
185                             null /* fragment */));
186                     return controllers;
187                 }
188             };
189 
190 }
191