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.content.Context; 19 import android.content.Intent; 20 import android.content.IntentFilter; 21 import android.os.BatteryManager; 22 import android.os.Bundle; 23 import android.provider.SearchIndexableResource; 24 import android.support.annotation.VisibleForTesting; 25 import android.view.Menu; 26 import android.view.MenuInflater; 27 import android.view.MenuItem; 28 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.overlay.FeatureFactory; 33 import com.android.settings.search.BaseSearchIndexProvider; 34 import com.android.settingslib.core.AbstractPreferenceController; 35 36 import java.util.ArrayList; 37 import java.util.Arrays; 38 import java.util.List; 39 40 public class PowerUsageAdvanced extends PowerUsageBase { 41 private static final String TAG = "AdvancedBatteryUsage"; 42 private static final String KEY_BATTERY_GRAPH = "battery_graph"; 43 private static final String KEY_APP_LIST = "app_list"; 44 private static final String KEY_SHOW_ALL_APPS = "show_all_apps"; 45 @VisibleForTesting 46 static final int MENU_TOGGLE_APPS = Menu.FIRST + 1; 47 48 @VisibleForTesting 49 BatteryHistoryPreference mHistPref; 50 private BatteryUtils mBatteryUtils; 51 private PowerUsageFeatureProvider mPowerUsageFeatureProvider; 52 private BatteryAppListPreferenceController mBatteryAppListPreferenceController; 53 @VisibleForTesting 54 boolean mShowAllApps = false; 55 56 @Override 57 public void onCreate(Bundle icicle) { 58 super.onCreate(icicle); 59 final Context context = getContext(); 60 61 mHistPref = (BatteryHistoryPreference) findPreference(KEY_BATTERY_GRAPH); 62 mPowerUsageFeatureProvider = FeatureFactory.getFactory(context) 63 .getPowerUsageFeatureProvider(context); 64 mBatteryUtils = BatteryUtils.getInstance(context); 65 66 // init the summary so other preferences won't have unnecessary move 67 updateHistPrefSummary(context); 68 restoreSavedInstance(icicle); 69 } 70 71 @Override 72 public void onDestroy() { 73 super.onDestroy(); 74 if (getActivity().isChangingConfigurations()) { 75 BatteryEntry.clearUidCache(); 76 } 77 } 78 79 @Override 80 public int getMetricsCategory() { 81 return MetricsProto.MetricsEvent.FUELGAUGE_BATTERY_HISTORY_DETAIL; 82 } 83 84 @Override 85 protected String getLogTag() { 86 return TAG; 87 } 88 89 @Override 90 protected int getPreferenceScreenResId() { 91 return R.xml.power_usage_advanced; 92 } 93 94 @Override 95 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 96 menu.add(Menu.NONE, MENU_TOGGLE_APPS, Menu.NONE, 97 mShowAllApps ? R.string.hide_extra_apps : R.string.show_all_apps); 98 super.onCreateOptionsMenu(menu, inflater); 99 } 100 101 @Override 102 public boolean onOptionsItemSelected(MenuItem item) { 103 switch (item.getItemId()) { 104 case MENU_TOGGLE_APPS: 105 mShowAllApps = !mShowAllApps; 106 item.setTitle(mShowAllApps ? R.string.hide_extra_apps : R.string.show_all_apps); 107 mMetricsFeatureProvider.action(getContext(), 108 MetricsProto.MetricsEvent.ACTION_SETTINGS_MENU_BATTERY_APPS_TOGGLE, 109 mShowAllApps); 110 restartBatteryStatsLoader(BatteryUpdateType.MANUAL); 111 return true; 112 default: 113 return super.onOptionsItemSelected(item); 114 } 115 } 116 117 @VisibleForTesting 118 void restoreSavedInstance(Bundle savedInstance) { 119 if (savedInstance != null) { 120 mShowAllApps = savedInstance.getBoolean(KEY_SHOW_ALL_APPS, false); 121 } 122 } 123 124 @Override 125 public void onSaveInstanceState(Bundle outState) { 126 super.onSaveInstanceState(outState); 127 outState.putBoolean(KEY_SHOW_ALL_APPS, mShowAllApps); 128 } 129 130 @Override 131 protected List<AbstractPreferenceController> createPreferenceControllers(Context context) { 132 final List<AbstractPreferenceController> controllers = new ArrayList<>(); 133 134 mBatteryAppListPreferenceController = new BatteryAppListPreferenceController(context, 135 KEY_APP_LIST, getLifecycle(), (SettingsActivity) getActivity(), this); 136 controllers.add(mBatteryAppListPreferenceController); 137 138 return controllers; 139 } 140 141 @Override 142 protected void refreshUi(@BatteryUpdateType int refreshType) { 143 final Context context = getContext(); 144 if (context == null) { 145 return; 146 } 147 updatePreference(mHistPref); 148 updateHistPrefSummary(context); 149 150 mBatteryAppListPreferenceController.refreshAppListGroup(mStatsHelper, mShowAllApps); 151 } 152 153 private void updateHistPrefSummary(Context context) { 154 Intent batteryIntent = 155 context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); 156 final boolean plugged = batteryIntent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1) != 0; 157 158 if (mPowerUsageFeatureProvider.isEnhancedBatteryPredictionEnabled(context) && !plugged) { 159 mHistPref.setBottomSummary( 160 mPowerUsageFeatureProvider.getAdvancedUsageScreenInfoString()); 161 } else { 162 mHistPref.hideBottomSummary(); 163 } 164 } 165 166 public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = 167 new BaseSearchIndexProvider() { 168 @Override 169 public List<SearchIndexableResource> getXmlResourcesToIndex( 170 Context context, boolean enabled) { 171 final SearchIndexableResource sir = new SearchIndexableResource(context); 172 sir.xmlResId = R.xml.power_usage_advanced; 173 return Arrays.asList(sir); 174 } 175 176 @Override 177 public List<AbstractPreferenceController> createPreferenceControllers( 178 Context context) { 179 final List<AbstractPreferenceController> controllers = new ArrayList<>(); 180 controllers.add(new BatteryAppListPreferenceController(context, 181 KEY_APP_LIST, null /* lifecycle */, null /* activity */, 182 null /* fragment */)); 183 return controllers; 184 } 185 }; 186 187 } 188