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