1 /*
2  * Copyright (C) 2022 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.users;
18 
19 import static android.provider.Settings.Secure.TIMEOUT_TO_DOCK_USER;
20 
21 import android.app.settings.SettingsEnums;
22 import android.content.Context;
23 import android.graphics.drawable.Drawable;
24 import android.os.UserHandle;
25 import android.provider.Settings;
26 
27 import com.android.settings.R;
28 import com.android.settings.widget.RadioButtonPickerFragment;
29 import com.android.settingslib.widget.CandidateInfo;
30 
31 import java.util.ArrayList;
32 import java.util.List;
33 
34 /**
35  * Setting screen that lists options for users to configure whether to automatically switch to the
36  * Dock User when the device is docked, and if so duration of the timeout.
37  */
38 public class TimeoutToDockUserSettings extends RadioButtonPickerFragment {
39     // Index of the default key of the timeout setting if it hasn't been changed by the user.
40     // Default to the smallest non-zero option (which is currently 1 minute).
41     public static final int DEFAULT_TIMEOUT_SETTING_VALUE_INDEX = 1;
42 
43     // Labels of the options, for example, "never", "after 5 minutes".
44     private String[] mEntries;
45 
46     // Values and keys of the options.
47     private String[] mValues;
48 
49     @Override
getMetricsCategory()50     public int getMetricsCategory() {
51         return SettingsEnums.TIMEOUT_TO_USER_ZERO;
52     }
53 
54     @Override
getPreferenceScreenResId()55     protected int getPreferenceScreenResId() {
56         return R.xml.user_timeout_to_dock_user_settings;
57     }
58 
59     @Override
onAttach(Context context)60     public void onAttach(Context context) {
61         super.onAttach(context);
62 
63         mEntries = getContext().getResources().getStringArray(
64                 R.array.switch_to_dock_user_when_docked_timeout_entries);
65         mValues = getContext().getResources().getStringArray(
66                 R.array.switch_to_dock_user_when_docked_timeout_values);
67     }
68 
69     @Override
getCandidates()70     protected List<? extends CandidateInfo> getCandidates() {
71         final List<CandidateInfo> candidates = new ArrayList<>();
72 
73         if (mEntries == null || mValues == null) {
74             return candidates;
75         }
76 
77         for (int i = 0; i < mValues.length; i++) {
78             candidates.add(new TimeoutCandidateInfo(mEntries[i], mValues[i], true));
79         }
80 
81         return candidates;
82     }
83 
84     @Override
getDefaultKey()85     protected String getDefaultKey() {
86         final String defaultKey = Settings.Secure.getStringForUser(
87                 getContext().getContentResolver(), TIMEOUT_TO_DOCK_USER, UserHandle.myUserId());
88         return defaultKey != null ? defaultKey : mValues[DEFAULT_TIMEOUT_SETTING_VALUE_INDEX];
89     }
90 
91     @Override
setDefaultKey(String key)92     protected boolean setDefaultKey(String key) {
93         Settings.Secure.putStringForUser(getContext().getContentResolver(), TIMEOUT_TO_DOCK_USER,
94                 key, UserHandle.myUserId());
95         return true;
96     }
97 
98     private static class TimeoutCandidateInfo extends CandidateInfo {
99         private final CharSequence mLabel;
100         private final String mKey;
101 
TimeoutCandidateInfo(CharSequence label, String key, boolean enabled)102         TimeoutCandidateInfo(CharSequence label, String key, boolean enabled) {
103             super(enabled);
104             mLabel = label;
105             mKey = key;
106         }
107 
108         @Override
loadLabel()109         public CharSequence loadLabel() {
110             return mLabel;
111         }
112 
113         @Override
loadIcon()114         public Drawable loadIcon() {
115             return null;
116         }
117 
118         @Override
getKey()119         public String getKey() {
120             return mKey;
121         }
122     }
123 }
124