1 /*
2  * Copyright (C) 2017 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.device.apps.specialaccess;
18 
19 import android.content.Context;
20 import android.os.Bundle;
21 
22 import androidx.annotation.Keep;
23 import androidx.annotation.NonNull;
24 import androidx.preference.Preference;
25 import androidx.preference.PreferenceGroup;
26 import androidx.preference.SwitchPreference;
27 import androidx.preference.TwoStatePreference;
28 
29 import com.android.internal.logging.nano.MetricsProto;
30 import com.android.settingslib.applications.ApplicationsState;
31 import com.android.settingslib.fuelgauge.PowerWhitelistBackend;
32 import com.android.tv.settings.R;
33 import com.android.tv.settings.SettingsPreferenceFragment;
34 
35 /**
36  * Fragment for managing power save whitelist
37  */
38 @Keep
39 public class HighPower extends SettingsPreferenceFragment implements
40         ManageApplicationsController.Callback {
41 
42     private PowerWhitelistBackend mPowerWhitelistBackend;
43     private ManageApplicationsController mManageApplicationsController;
44     private final ApplicationsState.AppFilter mFilter =
45             new ApplicationsState.CompoundFilter(
46                     new ApplicationsState.CompoundFilter(
47                             ApplicationsState.FILTER_WITHOUT_DISABLED_UNTIL_USED,
48                             ApplicationsState.FILTER_ALL_ENABLED),
49                     new ApplicationsState.AppFilter() {
50                         @Override
51                         public void init() {}
52 
53                         @Override
54                         public boolean filterApp(ApplicationsState.AppEntry info) {
55                             info.extraInfo =
56                                     mPowerWhitelistBackend.isWhitelisted(info.info.packageName);
57                             return !ManageAppOp.shouldIgnorePackage(getContext(),
58                                     info.info.packageName);
59                         }
60                     });
61 
62     @Override
getMetricsCategory()63     public int getMetricsCategory() {
64         return MetricsProto.MetricsEvent.APPLICATIONS_HIGH_POWER_APPS;
65     }
66 
67     @Override
onAttach(Context context)68     public void onAttach(Context context) {
69         super.onAttach(context);
70         mPowerWhitelistBackend = PowerWhitelistBackend.getInstance(context);
71         mManageApplicationsController = new ManageApplicationsController(context, this,
72                 getLifecycle(), mFilter, ApplicationsState.ALPHA_COMPARATOR);
73     }
74 
75     @Override
onCreatePreferences(Bundle savedInstanceState, String rootKey)76     public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
77         setPreferencesFromResource(R.xml.manage_high_power, null);
78     }
79 
80     @Override
onResume()81     public void onResume() {
82         super.onResume();
83         mManageApplicationsController.updateAppList();
84     }
85 
86     @NonNull
87     @Override
bindPreference(@onNull Preference preference, ApplicationsState.AppEntry entry)88     public Preference bindPreference(@NonNull Preference preference,
89             ApplicationsState.AppEntry entry) {
90         final TwoStatePreference switchPref = (SwitchPreference) preference;
91         switchPref.setTitle(entry.label);
92         switchPref.setKey(entry.info.packageName);
93         switchPref.setIcon(entry.icon);
94         if (mPowerWhitelistBackend.isSysWhitelisted(entry.info.packageName)) {
95             switchPref.setChecked(false);
96             switchPref.setEnabled(false);
97         } else {
98             switchPref.setEnabled(true);
99             switchPref.setChecked(!(Boolean) entry.extraInfo);
100             switchPref.setOnPreferenceChangeListener((pref, newValue) -> {
101                 final String pkg = pref.getKey();
102                 if ((Boolean) newValue) {
103                     mPowerWhitelistBackend.removeApp(pkg);
104                 } else {
105                     mPowerWhitelistBackend.addApp(pkg);
106                 }
107                 updateSummary(pref);
108                 return true;
109             });
110         }
111         updateSummary(switchPref);
112         return switchPref;
113     }
114 
updateSummary(Preference preference)115     private void updateSummary(Preference preference) {
116         final String pkg = preference.getKey();
117         if (mPowerWhitelistBackend.isSysWhitelisted(pkg)) {
118             preference.setSummary(R.string.high_power_system);
119         } else if (mPowerWhitelistBackend.isWhitelisted(pkg)) {
120             preference.setSummary(R.string.high_power_on);
121         } else {
122             preference.setSummary(R.string.high_power_off);
123         }
124     }
125 
126     @NonNull
127     @Override
createAppPreference()128     public Preference createAppPreference() {
129         return new SwitchPreference(getPreferenceManager().getContext());
130     }
131 
132     @NonNull
133     @Override
getEmptyPreference()134     public Preference getEmptyPreference() {
135         final Preference empty = new Preference(getPreferenceManager().getContext());
136         empty.setKey("empty");
137         empty.setTitle(R.string.high_power_apps_empty);
138         empty.setEnabled(false);
139         return empty;
140     }
141 
142     @NonNull
143     @Override
getAppPreferenceGroup()144     public PreferenceGroup getAppPreferenceGroup() {
145         return getPreferenceScreen();
146     }
147 }
148