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