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.settings.display; 18 19 import static android.provider.Settings.System.FOLD_LOCK_BEHAVIOR; 20 21 import android.app.settings.SettingsEnums; 22 import android.content.Context; 23 import android.os.UserHandle; 24 import android.provider.Settings; 25 import android.util.Log; 26 27 import com.android.internal.foldables.FoldGracePeriodProvider; 28 import com.android.settings.R; 29 import com.android.settings.support.actionbar.HelpResourceProvider; 30 import com.android.settings.utils.CandidateInfoExtra; 31 import com.android.settings.widget.RadioButtonPickerFragment; 32 import com.android.settingslib.widget.CandidateInfo; 33 import com.android.settingslib.widget.SelectorWithWidgetPreference; 34 35 import java.util.ArrayList; 36 import java.util.HashSet; 37 import java.util.List; 38 import java.util.Set; 39 40 /** 41 * Fragment that is used to control fold setting. 42 * 43 * Keep the setting values in this class in sync with the values in 44 * {@link com.android.server.utils.FoldSettingProvider} 45 */ 46 public class FoldLockBehaviorSettings extends RadioButtonPickerFragment implements 47 HelpResourceProvider { 48 49 public static final String SETTING_VALUE_STAY_AWAKE_ON_FOLD = "stay_awake_on_fold_key"; 50 public static final String SETTING_VALUE_SELECTIVE_STAY_AWAKE = "selective_stay_awake_key"; 51 public static final String SETTING_VALUE_SLEEP_ON_FOLD = "sleep_on_fold_key"; 52 public static final String TAG = "FoldLockBehaviorSetting"; 53 public static final HashSet<String> SETTING_VALUES = new HashSet<>( 54 Set.of(SETTING_VALUE_STAY_AWAKE_ON_FOLD, SETTING_VALUE_SELECTIVE_STAY_AWAKE, 55 SETTING_VALUE_SLEEP_ON_FOLD)); 56 private static final String SETTING_VALUE_DEFAULT = SETTING_VALUE_SELECTIVE_STAY_AWAKE; 57 private Context mContext; 58 private final FoldGracePeriodProvider mFoldGracePeriodProvider = new FoldGracePeriodProvider(); 59 60 @Override onAttach(Context context)61 public void onAttach(Context context) { 62 super.onAttach(context); 63 mContext = context; 64 setIllustrationLottieAnimation(getDefaultKey()); 65 } 66 67 @Override getCandidates()68 protected List<? extends CandidateInfo> getCandidates() { 69 List<CandidateInfoExtra> candidates = new ArrayList<>(); 70 candidates.add(new CandidateInfoExtra( 71 resourceToString(R.string.stay_awake_on_fold_title), 72 resourceToString(R.string.stay_awake_on_fold_summary), 73 SETTING_VALUE_STAY_AWAKE_ON_FOLD, /* enabled */ true)); 74 if (mFoldGracePeriodProvider.isEnabled()) { 75 candidates.add(new CandidateInfoExtra( 76 resourceToString(R.string.stay_awake_on_lockscreen_title), 77 resourceToString(R.string.stay_awake_on_lockscreen_summary), 78 SETTING_VALUE_SELECTIVE_STAY_AWAKE, /* enabled */ true)); 79 } else { 80 candidates.add(new CandidateInfoExtra( 81 resourceToString(R.string.selective_stay_awake_title), 82 resourceToString(R.string.selective_stay_awake_summary), 83 SETTING_VALUE_SELECTIVE_STAY_AWAKE, /* enabled */ true)); 84 } 85 candidates.add(new CandidateInfoExtra( 86 resourceToString(R.string.sleep_on_fold_title), 87 resourceToString(R.string.sleep_on_fold_summary), 88 SETTING_VALUE_SLEEP_ON_FOLD, /* enabled */ true)); 89 return candidates; 90 } 91 92 @Override bindPreferenceExtra(SelectorWithWidgetPreference pref, String key, CandidateInfo info, String defaultKey, String systemDefaultKey)93 public void bindPreferenceExtra(SelectorWithWidgetPreference pref, 94 String key, CandidateInfo info, String defaultKey, String systemDefaultKey) { 95 if (!(info instanceof CandidateInfoExtra)) { 96 return; 97 } 98 99 pref.setSummary(((CandidateInfoExtra) info).loadSummary()); 100 } 101 102 @Override getDefaultKey()103 protected String getDefaultKey() { 104 String foldSettingValue = getCurrentFoldSettingValue(); 105 foldSettingValue = (foldSettingValue != null) ? foldSettingValue : SETTING_VALUE_DEFAULT; 106 if (!SETTING_VALUES.contains(foldSettingValue)) { 107 Log.e(TAG, 108 "getDefaultKey: Invalid setting value, returning default setting value"); 109 foldSettingValue = SETTING_VALUE_DEFAULT; 110 } 111 112 return foldSettingValue; 113 } 114 115 @Override setDefaultKey(String key)116 protected boolean setDefaultKey(String key) { 117 if (!SETTING_VALUES.contains(key)) { 118 Log.e(TAG, "setDefaultKey: Can not set invalid key: " + key); 119 key = SETTING_VALUE_SELECTIVE_STAY_AWAKE; 120 } 121 setCurrentFoldSettingValue(key); 122 return true; 123 } 124 125 @Override getMetricsCategory()126 public int getMetricsCategory() { 127 return SettingsEnums.FOLD_LOCK_BEHAVIOR; 128 } 129 130 @Override getPreferenceScreenResId()131 protected int getPreferenceScreenResId() { 132 return R.xml.fold_lock_behavior_settings; 133 } 134 getCurrentFoldSettingValue()135 private String getCurrentFoldSettingValue() { 136 return Settings.System.getStringForUser(mContext.getContentResolver(), 137 FOLD_LOCK_BEHAVIOR, 138 UserHandle.USER_CURRENT); 139 } 140 setCurrentFoldSettingValue(String key)141 private void setCurrentFoldSettingValue(String key) { 142 Settings.System.putStringForUser(mContext.getContentResolver(), 143 FOLD_LOCK_BEHAVIOR, 144 key, 145 UserHandle.USER_CURRENT); 146 } 147 148 @Override onSelectionPerformed(boolean success)149 protected void onSelectionPerformed(boolean success) { 150 if (success) { 151 setIllustrationLottieAnimation(getDefaultKey()); 152 updateCandidates(); 153 } 154 } 155 setIllustrationLottieAnimation(String foldSettingValue)156 private void setIllustrationLottieAnimation(String foldSettingValue) { 157 switch (foldSettingValue) { 158 case SETTING_VALUE_STAY_AWAKE_ON_FOLD: 159 setIllustration(R.raw.fold_setting_stay_awake_on_fold_lottie, 160 IllustrationType.LOTTIE_ANIMATION); 161 break; 162 case SETTING_VALUE_SELECTIVE_STAY_AWAKE: 163 if (mFoldGracePeriodProvider.isEnabled()) { 164 setIllustration(R.raw.fold_setting_grace_period_selective_stay_awake_lottie, 165 IllustrationType.LOTTIE_ANIMATION); 166 } else { 167 setIllustration(R.raw.fold_setting_selective_stay_awake_lottie, 168 IllustrationType.LOTTIE_ANIMATION); 169 } 170 break; 171 case SETTING_VALUE_SLEEP_ON_FOLD: 172 setIllustration(R.raw.fold_setting_sleep_on_fold_lottie, 173 IllustrationType.LOTTIE_ANIMATION); 174 break; 175 } 176 } 177 resourceToString(int resource)178 private String resourceToString(int resource) { 179 return mContext.getText(resource).toString(); 180 } 181 } 182