• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_RESTRICTED;
25 import static android.app.usage.UsageStatsManager.STANDBY_BUCKET_WORKING_SET;
26 
27 import android.app.settings.SettingsEnums;
28 import android.app.usage.UsageStatsManager;
29 import android.content.Context;
30 import android.content.Intent;
31 import android.content.pm.PackageManager;
32 import android.content.pm.ResolveInfo;
33 import android.content.res.Resources;
34 import android.os.Bundle;
35 import android.provider.Settings;
36 import android.text.TextUtils;
37 
38 import androidx.preference.ListPreference;
39 import androidx.preference.Preference;
40 import androidx.preference.PreferenceGroup;
41 
42 import com.android.settings.R;
43 import com.android.settings.SettingsPreferenceFragment;
44 
45 import java.util.Arrays;
46 import java.util.List;
47 
48 public class InactiveApps extends SettingsPreferenceFragment
49         implements Preference.OnPreferenceChangeListener {
50 
51     private static final CharSequence[] FULL_SETTABLE_BUCKETS_NAMES =
52             {"ACTIVE", "WORKING_SET", "FREQUENT", "RARE", "RESTRICTED"};
53 
54     private static final CharSequence[] REDUCED_SETTABLE_BUCKETS_NAMES =
55             Arrays.copyOfRange(FULL_SETTABLE_BUCKETS_NAMES, 0, 4);
56 
57     private static final CharSequence[] FULL_SETTABLE_BUCKETS_VALUES = {
58             Integer.toString(STANDBY_BUCKET_ACTIVE),
59             Integer.toString(STANDBY_BUCKET_WORKING_SET),
60             Integer.toString(STANDBY_BUCKET_FREQUENT),
61             Integer.toString(STANDBY_BUCKET_RARE),
62             Integer.toString(STANDBY_BUCKET_RESTRICTED)
63     };
64 
65     private static final CharSequence[] REDUCED_SETTABLE_BUCKETS_VALUES =
66             Arrays.copyOfRange(FULL_SETTABLE_BUCKETS_VALUES, 0, 4);
67 
68     private UsageStatsManager mUsageStats;
69 
70     @Override
getMetricsCategory()71     public int getMetricsCategory() {
72         return SettingsEnums.FUELGAUGE_INACTIVE_APPS;
73     }
74 
75     @Override
onCreate(Bundle icicle)76     public void onCreate(Bundle icicle) {
77         super.onCreate(icicle);
78 
79         mUsageStats = getActivity().getSystemService(UsageStatsManager.class);
80         addPreferencesFromResource(R.xml.placeholder_preference_screen);
81         getActivity().setTitle(R.string.inactive_apps_title);
82     }
83 
84     @Override
onResume()85     public void onResume() {
86         super.onResume();
87         init();
88     }
89 
init()90     private void init() {
91         PreferenceGroup screen = getPreferenceScreen();
92         screen.removeAll();
93         screen.setOrderingAsAdded(false);
94         final Context context = getActivity();
95         final PackageManager pm = context.getPackageManager();
96         final String settingsPackage = context.getPackageName();
97         final boolean allowRestrictedBucket = Settings.Global.getInt(getContentResolver(),
98                 Settings.Global.ENABLE_RESTRICTED_BUCKET,
99                 Settings.Global.DEFAULT_ENABLE_RESTRICTED_BUCKET) == 1;
100         final CharSequence[] bucketNames = allowRestrictedBucket
101                 ? FULL_SETTABLE_BUCKETS_NAMES : REDUCED_SETTABLE_BUCKETS_NAMES;
102         final CharSequence[] bucketValues = allowRestrictedBucket
103                 ? FULL_SETTABLE_BUCKETS_VALUES : REDUCED_SETTABLE_BUCKETS_VALUES;
104 
105         Intent launcherIntent = new Intent(Intent.ACTION_MAIN);
106         launcherIntent.addCategory(Intent.CATEGORY_LAUNCHER);
107         List<ResolveInfo> apps = pm.queryIntentActivities(launcherIntent, 0);
108         for (ResolveInfo app : apps) {
109             String packageName = app.activityInfo.applicationInfo.packageName;
110             ListPreference p = new ListPreference(getPrefContext());
111             p.setTitle(app.loadLabel(pm));
112             p.setIcon(app.loadIcon(pm));
113             p.setKey(packageName);
114             p.setEntries(bucketNames);
115             p.setEntryValues(bucketValues);
116             updateSummary(p);
117             // Don't allow Settings to change its own standby bucket.
118             if (TextUtils.equals(packageName, settingsPackage)) {
119                 p.setEnabled(false);
120             }
121             p.setOnPreferenceChangeListener(this);
122 
123             screen.addPreference(p);
124         }
125     }
126 
bucketToName(int bucket)127     static String bucketToName(int bucket) {
128         switch (bucket) {
129             case STANDBY_BUCKET_EXEMPTED: return "EXEMPTED";
130             case STANDBY_BUCKET_ACTIVE: return "ACTIVE";
131             case STANDBY_BUCKET_WORKING_SET: return "WORKING_SET";
132             case STANDBY_BUCKET_FREQUENT: return "FREQUENT";
133             case STANDBY_BUCKET_RARE: return "RARE";
134             case STANDBY_BUCKET_RESTRICTED: return "RESTRICTED";
135             case STANDBY_BUCKET_NEVER: return "NEVER";
136         }
137         return "";
138     }
139 
updateSummary(ListPreference p)140     private void updateSummary(ListPreference p) {
141         final Resources res = getActivity().getResources();
142         final int appBucket = mUsageStats.getAppStandbyBucket(p.getKey());
143         final String bucketName = bucketToName(appBucket);
144         p.setSummary(res.getString(R.string.standby_bucket_summary, bucketName));
145         // Buckets outside of the range of the dynamic ones are only used for special
146         // purposes and can either not be changed out of, or might have undesirable
147         // side-effects in combination with other assumptions.
148         final boolean changeable = appBucket >= STANDBY_BUCKET_ACTIVE
149                 && appBucket <= STANDBY_BUCKET_RESTRICTED;
150         if (changeable) {
151             p.setValue(Integer.toString(appBucket));
152         }
153         p.setEnabled(changeable);
154     }
155 
156     @Override
onPreferenceChange(Preference preference, Object newValue)157     public boolean onPreferenceChange(Preference preference, Object newValue) {
158         mUsageStats.setAppStandbyBucket(preference.getKey(), Integer.parseInt((String) newValue));
159         updateSummary((ListPreference) preference);
160         return false;
161     }
162 }
163