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
17package com.android.tv.settings.system.development;
18
19import android.app.usage.UsageStatsManager;
20import android.content.Context;
21import android.content.Intent;
22import android.content.pm.PackageManager;
23import android.content.pm.ResolveInfo;
24import android.os.Bundle;
25import android.support.annotation.Keep;
26import android.support.v17.preference.LeanbackPreferenceFragment;
27import android.support.v7.preference.Preference;
28import android.support.v7.preference.PreferenceScreen;
29
30import com.android.tv.settings.R;
31
32import java.util.List;
33
34@Keep
35public class InactiveApps extends LeanbackPreferenceFragment implements
36        Preference.OnPreferenceClickListener {
37
38    private UsageStatsManager mUsageStats;
39
40    @Override
41    public void onCreate(Bundle icicle) {
42        mUsageStats = getActivity().getSystemService(UsageStatsManager.class);
43        super.onCreate(icicle);
44    }
45
46    @Override
47    public void onResume() {
48        super.onResume();
49        init();
50    }
51
52    @Override
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
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
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
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