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