1 /* 2 * Copyright (C) 2015 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 static android.app.usage.UsageStatsManager.STANDBY_BUCKET_ACTIVE; 20 import static android.app.usage.UsageStatsManager.STANDBY_BUCKET_EXEMPTED; 21 import static android.app.usage.UsageStatsManager.STANDBY_BUCKET_FREQUENT; 22 import static android.app.usage.UsageStatsManager.STANDBY_BUCKET_NEVER; 23 import static android.app.usage.UsageStatsManager.STANDBY_BUCKET_RARE; 24 import static android.app.usage.UsageStatsManager.STANDBY_BUCKET_WORKING_SET; 25 26 import android.app.settings.SettingsEnums; 27 import android.app.usage.UsageStatsManager; 28 import android.content.Context; 29 import android.content.Intent; 30 import android.content.pm.PackageManager; 31 import android.content.pm.ResolveInfo; 32 import android.content.res.Resources; 33 import android.os.Bundle; 34 35 import androidx.preference.ListPreference; 36 import androidx.preference.Preference; 37 import androidx.preference.PreferenceGroup; 38 39 import com.android.settings.R; 40 import com.android.settings.SettingsPreferenceFragment; 41 42 import java.util.List; 43 44 public class InactiveApps extends SettingsPreferenceFragment 45 implements Preference.OnPreferenceChangeListener { 46 47 private static final CharSequence[] SETTABLE_BUCKETS_NAMES = 48 {"ACTIVE", "WORKING_SET", "FREQUENT", "RARE"}; 49 50 private static final CharSequence[] SETTABLE_BUCKETS_VALUES = { 51 Integer.toString(STANDBY_BUCKET_ACTIVE), 52 Integer.toString(STANDBY_BUCKET_WORKING_SET), 53 Integer.toString(STANDBY_BUCKET_FREQUENT), 54 Integer.toString(STANDBY_BUCKET_RARE) 55 }; 56 57 private UsageStatsManager mUsageStats; 58 59 @Override getMetricsCategory()60 public int getMetricsCategory() { 61 return SettingsEnums.FUELGAUGE_INACTIVE_APPS; 62 } 63 64 @Override onCreate(Bundle icicle)65 public void onCreate(Bundle icicle) { 66 super.onCreate(icicle); 67 68 mUsageStats = getActivity().getSystemService(UsageStatsManager.class); 69 addPreferencesFromResource(R.xml.inactive_apps); 70 } 71 72 @Override onResume()73 public void onResume() { 74 super.onResume(); 75 init(); 76 } 77 init()78 private void init() { 79 PreferenceGroup screen = getPreferenceScreen(); 80 screen.removeAll(); 81 screen.setOrderingAsAdded(false); 82 final Context context = getActivity(); 83 final PackageManager pm = context.getPackageManager(); 84 final UsageStatsManager usm = context.getSystemService(UsageStatsManager.class); 85 86 Intent launcherIntent = new Intent(Intent.ACTION_MAIN); 87 launcherIntent.addCategory(Intent.CATEGORY_LAUNCHER); 88 List<ResolveInfo> apps = pm.queryIntentActivities(launcherIntent, 0); 89 for (ResolveInfo app : apps) { 90 String packageName = app.activityInfo.applicationInfo.packageName; 91 ListPreference p = new ListPreference(getPrefContext()); 92 p.setTitle(app.loadLabel(pm)); 93 p.setIcon(app.loadIcon(pm)); 94 p.setKey(packageName); 95 p.setEntries(SETTABLE_BUCKETS_NAMES); 96 p.setEntryValues(SETTABLE_BUCKETS_VALUES); 97 updateSummary(p); 98 p.setOnPreferenceChangeListener(this); 99 100 screen.addPreference(p); 101 } 102 } 103 bucketToName(int bucket)104 static String bucketToName(int bucket) { 105 switch (bucket) { 106 case STANDBY_BUCKET_EXEMPTED: return "EXEMPTED"; 107 case STANDBY_BUCKET_ACTIVE: return "ACTIVE"; 108 case STANDBY_BUCKET_WORKING_SET: return "WORKING_SET"; 109 case STANDBY_BUCKET_FREQUENT: return "FREQUENT"; 110 case STANDBY_BUCKET_RARE: return "RARE"; 111 case STANDBY_BUCKET_NEVER: return "NEVER"; 112 } 113 return ""; 114 } 115 updateSummary(ListPreference p)116 private void updateSummary(ListPreference p) { 117 final Resources res = getActivity().getResources(); 118 final int appBucket = mUsageStats.getAppStandbyBucket(p.getKey()); 119 final String bucketName = bucketToName(appBucket); 120 p.setSummary(res.getString(R.string.standby_bucket_summary, bucketName)); 121 // Buckets outside of the range of the dynamic ones are only used for special 122 // purposes and can either not be changed out of, or might have undesirable 123 // side-effects in combination with other assumptions. 124 final boolean changeable = appBucket >= STANDBY_BUCKET_ACTIVE 125 && appBucket <= STANDBY_BUCKET_RARE; 126 if (changeable) { 127 p.setValue(Integer.toString(appBucket)); 128 } 129 p.setEnabled(changeable); 130 } 131 132 @Override onPreferenceChange(Preference preference, Object newValue)133 public boolean onPreferenceChange(Preference preference, Object newValue) { 134 mUsageStats.setAppStandbyBucket(preference.getKey(), Integer.parseInt((String) newValue)); 135 updateSummary((ListPreference) preference); 136 return false; 137 } 138 } 139