1 /* 2 * Copyright (C) 2023 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.eco; 18 19 import static com.android.tv.settings.device.eco.EnergyModesHelper.isLowPowerStandbySupported; 20 21 import android.content.Context; 22 import android.content.Intent; 23 import android.os.Bundle; 24 import android.os.PowerManager; 25 import android.text.TextUtils; 26 27 import androidx.annotation.Keep; 28 import androidx.preference.Preference; 29 import androidx.preference.TwoStatePreference; 30 31 import com.android.tv.settings.R; 32 import com.android.tv.settings.SettingsPreferenceFragment; 33 import com.android.tv.settings.customization.CustomizationConstants; 34 import com.android.tv.settings.customization.Partner; 35 import com.android.tv.settings.customization.PartnerPreferencesMerger; 36 import com.android.tv.settings.device.LimitNetworkInStandbyConfirmationDialogActivity; 37 import com.android.tv.settings.library.util.SliceUtils; 38 import com.android.tv.twopanelsettings.slices.SlicePreference; 39 40 /** Power and energy settings. */ 41 @Keep 42 public class PowerAndEnergyFragment extends SettingsPreferenceFragment { 43 private static final String KEY_LIMIT_NETWORK = "limit_network_in_standby"; 44 private static final String KEY_ENERGY_MODES = "energy_modes"; 45 46 @Override onCreatePreferences(Bundle bundle, String s)47 public void onCreatePreferences(Bundle bundle, String s) { 48 setPreferencesFromResource(R.xml.power_and_energy, null); 49 50 updateLowPowerStandbyPreferences(); 51 updatePowerOnBehaviourPreference(); 52 53 if (Partner.getInstance(getContext()).isCustomizationPackageProvided()) { 54 PartnerPreferencesMerger.mergePreferences( 55 getContext(), 56 getPreferenceScreen(), 57 CustomizationConstants.POWER_AND_ENERGY_SCREEN 58 ); 59 } 60 } 61 updateLowPowerStandbyPreferences()62 private void updateLowPowerStandbyPreferences() { 63 final Context context = getContext(); 64 final Preference limitNetworkPreference = findPreference(KEY_LIMIT_NETWORK); 65 final Preference energyModesPreference = findPreference(KEY_ENERGY_MODES); 66 67 final EnergyModesHelper energyModesHelper = new EnergyModesHelper(context); 68 final boolean lowPowerStandbySupported = isLowPowerStandbySupported(context); 69 final boolean enableEnergyModes = energyModesHelper.areEnergyModesAvailable(); 70 71 if (limitNetworkPreference != null) { 72 limitNetworkPreference.setVisible(lowPowerStandbySupported && !enableEnergyModes); 73 } 74 if (energyModesPreference != null) { 75 energyModesPreference.setVisible(lowPowerStandbySupported && enableEnergyModes); 76 } 77 78 if (limitNetworkPreference != null && limitNetworkPreference.isVisible()) { 79 final PowerManager powerManager = context.getSystemService(PowerManager.class); 80 final boolean lowPowerStandbyEnabled = powerManager.isLowPowerStandbyEnabled(); 81 ((TwoStatePreference) limitNetworkPreference).setChecked(lowPowerStandbyEnabled); 82 } 83 } 84 updatePowerOnBehaviourPreference()85 private void updatePowerOnBehaviourPreference() { 86 final Preference powerOnBehaviorPreference = findPreference("power_on_behavior"); 87 if (powerOnBehaviorPreference != null && SliceUtils.isSliceProviderValid( 88 getContext(), ((SlicePreference) powerOnBehaviorPreference).getUri())) { 89 powerOnBehaviorPreference.setVisible(true); 90 } 91 } 92 93 @Override onResume()94 public void onResume() { 95 super.onResume(); 96 updateLowPowerStandbyPreferences(); 97 } 98 99 @Override onPreferenceTreeClick(Preference preference)100 public boolean onPreferenceTreeClick(Preference preference) { 101 final String key = preference.getKey(); 102 switch (key) { 103 case KEY_LIMIT_NETWORK: 104 if (!((TwoStatePreference) preference).isChecked()) { 105 // Confirmation dialog for disabling Low Power Standby 106 Intent intent = new Intent(getContext(), 107 LimitNetworkInStandbyConfirmationDialogActivity.class); 108 getContext().startActivity(intent); 109 } else { 110 // Enable Low Power Standby 111 PowerManager powerManager = getContext().getSystemService(PowerManager.class); 112 powerManager.setLowPowerStandbyEnabled(true); 113 } 114 break; 115 } 116 return super.onPreferenceTreeClick(preference); 117 } 118 119 /** Returns whether this fragment will only present the EnergySaver preference */ hasOnlyEnergySaverPreference(Context context)120 public static boolean hasOnlyEnergySaverPreference(Context context) { 121 if (isLowPowerStandbySupported(context)) { 122 // Has "Limit Network in Standby" / Energy Modes preference 123 return false; 124 } 125 126 if (!TextUtils.isEmpty(context.getString(R.string.power_boot_resume_slice_uri))) { 127 // Has "Power On Behaviour" preference 128 return false; 129 } 130 131 return true; 132 } 133 } 134