1 /* 2 * Copyright (C) 2025 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.communal; 18 19 import static android.provider.Settings.Secure.GLANCEABLE_HUB_START_CHARGING; 20 import static android.provider.Settings.Secure.GLANCEABLE_HUB_START_CHARGING_UPRIGHT; 21 import static android.provider.Settings.Secure.GLANCEABLE_HUB_START_DOCKED; 22 import static android.provider.Settings.Secure.GLANCEABLE_HUB_START_NEVER; 23 import static android.provider.Settings.Secure.WHEN_TO_START_GLANCEABLE_HUB; 24 25 import android.app.settings.SettingsEnums; 26 import android.content.Context; 27 import android.graphics.drawable.Drawable; 28 import android.provider.Settings; 29 30 import androidx.annotation.NonNull; 31 import androidx.annotation.Nullable; 32 33 import com.android.settings.R; 34 import com.android.settings.widget.RadioButtonPickerFragment; 35 import com.android.settingslib.widget.CandidateInfo; 36 37 import java.util.ArrayList; 38 import java.util.List; 39 40 /** 41 * Fragment that provides radio buttons to allow the user to choose when the hub should auto-start. 42 */ 43 public class WhenToStartHubPicker extends RadioButtonPickerFragment { 44 private static final String TAG = "WhenToStartHubPicker"; 45 private static final String SHOW_WHILE_CHARGING = "while_charging"; 46 private static final String SHOW_WHILE_DOCKED = "while_docked"; 47 private static final String SHOW_WHILE_CHARGING_AND_UPRIGHT = "while_charging_and_upright"; 48 private static final String SHOW_NEVER = "never"; 49 50 private Context mContext; 51 52 @Override onAttach(@onNull Context context)53 public void onAttach(@NonNull Context context) { 54 super.onAttach(context); 55 56 mContext = context; 57 } 58 59 @Override getPreferenceScreenResId()60 protected int getPreferenceScreenResId() { 61 return R.xml.when_to_start_hubmode_settings; 62 } 63 64 @Override getMetricsCategory()65 public int getMetricsCategory() { 66 return SettingsEnums.WHEN_TO_SHOW_WIDGETS_ON_LOCKSCREEN; 67 } 68 69 @Override getCandidates()70 protected List<? extends CandidateInfo> getCandidates() { 71 final List<WhenToStartHubCandidateInfo> candidates = new ArrayList<>(); 72 73 final String[] entries = entries(); 74 final String[] values = keys(); 75 76 if (entries == null || entries.length <= 0) return candidates; 77 if (values == null || values.length != entries.length) { 78 throw new IllegalArgumentException("Entries and values must be of the same length."); 79 } 80 81 for (int i = 0; i < entries.length; i++) { 82 candidates.add(new WhenToStartHubCandidateInfo(entries[i], values[i])); 83 } 84 85 return candidates; 86 } 87 entries()88 private String[] entries() { 89 return getResources().getStringArray(R.array.when_to_start_hubmode_entries); 90 } 91 keys()92 private String[] keys() { 93 return getResources().getStringArray(R.array.when_to_start_hubmode_values); 94 } 95 96 @Override getDefaultKey()97 protected String getDefaultKey() { 98 final int defaultValue = mContext.getResources().getInteger( 99 com.android.internal.R.integer.config_whenToStartHubModeDefault); 100 final int setting = Settings.Secure.getInt( 101 mContext.getContentResolver(), WHEN_TO_START_GLANCEABLE_HUB, defaultValue); 102 return getKeyFromSetting(setting); 103 } 104 105 @Override setDefaultKey(String key)106 protected boolean setDefaultKey(String key) { 107 Settings.Secure.putInt( 108 mContext.getContentResolver(), 109 WHEN_TO_START_GLANCEABLE_HUB, 110 getSettingFromPrefKey(key)); 111 112 return true; 113 } 114 115 @Override onSelectionPerformed(boolean success)116 protected void onSelectionPerformed(boolean success) { 117 super.onSelectionPerformed(success); 118 119 getActivity().finish(); 120 } 121 122 123 @Settings.Secure.WhenToStartGlanceableHub getSettingFromPrefKey(String key)124 private static int getSettingFromPrefKey(String key) { 125 switch (key) { 126 case SHOW_WHILE_CHARGING: 127 return GLANCEABLE_HUB_START_CHARGING; 128 case SHOW_WHILE_DOCKED: 129 return GLANCEABLE_HUB_START_DOCKED; 130 case SHOW_WHILE_CHARGING_AND_UPRIGHT: 131 return GLANCEABLE_HUB_START_CHARGING_UPRIGHT; 132 case SHOW_NEVER: 133 default: 134 return GLANCEABLE_HUB_START_NEVER; 135 } 136 } 137 getKeyFromSetting(@ettings.Secure.WhenToStartGlanceableHub int setting)138 private static String getKeyFromSetting(@Settings.Secure.WhenToStartGlanceableHub int setting) { 139 switch (setting) { 140 case GLANCEABLE_HUB_START_CHARGING: 141 return SHOW_WHILE_CHARGING; 142 case GLANCEABLE_HUB_START_DOCKED: 143 return SHOW_WHILE_DOCKED; 144 case GLANCEABLE_HUB_START_CHARGING_UPRIGHT: 145 return SHOW_WHILE_CHARGING_AND_UPRIGHT; 146 case GLANCEABLE_HUB_START_NEVER: 147 default: 148 return SHOW_NEVER; 149 } 150 } 151 152 private static final class WhenToStartHubCandidateInfo extends CandidateInfo { 153 private final String mName; 154 private final String mKey; 155 WhenToStartHubCandidateInfo(String title, String value)156 WhenToStartHubCandidateInfo(String title, String value) { 157 super(true); 158 159 mName = title; 160 mKey = value; 161 } 162 163 @Override loadLabel()164 public CharSequence loadLabel() { 165 return mName; 166 } 167 168 @Override 169 @Nullable loadIcon()170 public Drawable loadIcon() { 171 return null; 172 } 173 174 @Override getKey()175 public String getKey() { 176 return mKey; 177 } 178 } 179 } 180