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