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.tv.settings.system.development;
18 
19 import android.app.usage.UsageStatsManager;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.pm.PackageManager;
23 import android.content.pm.ResolveInfo;
24 import android.os.Bundle;
25 
26 import androidx.annotation.Keep;
27 import androidx.preference.Preference;
28 import androidx.preference.PreferenceScreen;
29 
30 import com.android.internal.logging.nano.MetricsProto;
31 import com.android.tv.settings.R;
32 import com.android.tv.settings.SettingsPreferenceFragment;
33 
34 import java.util.List;
35 
36 @Keep
37 public class InactiveApps extends SettingsPreferenceFragment implements
38         Preference.OnPreferenceClickListener {
39 
40     private UsageStatsManager mUsageStats;
41 
42     @Override
onCreate(Bundle icicle)43     public void onCreate(Bundle icicle) {
44         mUsageStats = getActivity().getSystemService(UsageStatsManager.class);
45         super.onCreate(icicle);
46     }
47 
48     @Override
onResume()49     public void onResume() {
50         super.onResume();
51         init();
52     }
53 
54     @Override
onCreatePreferences(Bundle savedInstanceState, String rootKey)55     public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
56         final Context themedContext = getPreferenceManager().getContext();
57         final PreferenceScreen screen = getPreferenceManager()
58                 .createPreferenceScreen(themedContext);
59         screen.setTitle(R.string.inactive_apps_title);
60         setPreferenceScreen(screen);
61     }
62 
init()63     private void init() {
64         final Context themedContext = getPreferenceManager().getContext();
65         final PreferenceScreen screen = getPreferenceScreen();
66         screen.removeAll();
67         screen.setOrderingAsAdded(false);
68         final PackageManager pm = getActivity().getPackageManager();
69 
70         Intent launcherIntent = new Intent(Intent.ACTION_MAIN);
71         launcherIntent.addCategory(Intent.CATEGORY_LAUNCHER);
72         List<ResolveInfo> apps = pm.queryIntentActivities(launcherIntent, 0);
73         for (ResolveInfo app : apps) {
74             String packageName = app.activityInfo.applicationInfo.packageName;
75             Preference p = new Preference(themedContext);
76             p.setTitle(app.loadLabel(pm));
77             p.setIcon(app.loadIcon(pm));
78             p.setKey(packageName);
79             updateSummary(p);
80             p.setOnPreferenceClickListener(this);
81 
82             screen.addPreference(p);
83         }
84     }
85 
updateSummary(Preference p)86     private void updateSummary(Preference p) {
87         boolean inactive = mUsageStats.isAppInactive(p.getKey());
88         p.setSummary(inactive
89                 ? R.string.inactive_app_inactive_summary
90                 : R.string.inactive_app_active_summary);
91     }
92 
93     @Override
onPreferenceClick(Preference preference)94     public boolean onPreferenceClick(Preference preference) {
95         String packageName = preference.getKey();
96         mUsageStats.setAppInactive(packageName, !mUsageStats.isAppInactive(packageName));
97         updateSummary(preference);
98         return false;
99     }
100 
101     @Override
getMetricsCategory()102     public int getMetricsCategory() {
103         return MetricsProto.MetricsEvent.FUELGAUGE_INACTIVE_APPS;
104     }
105 }
106