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.privatespace.autolock;
18 
19 import android.app.settings.SettingsEnums;
20 import android.content.Context;
21 import android.graphics.drawable.Drawable;
22 import android.os.Bundle;
23 import android.provider.Settings;
24 import android.util.Log;
25 
26 import androidx.annotation.NonNull;
27 import androidx.annotation.Nullable;
28 import androidx.preference.PreferenceScreen;
29 
30 import com.android.settings.R;
31 import com.android.settings.privatespace.PrivateSpaceMaintainer;
32 import com.android.settings.widget.RadioButtonPickerFragment;
33 import com.android.settingslib.widget.CandidateInfo;
34 import com.android.settingslib.widget.TopIntroPreference;
35 
36 import java.util.ArrayList;
37 import java.util.List;
38 
39 public class AutoLockSettingsFragment extends RadioButtonPickerFragment {
40     private static final String TAG = "PSAutoLockSetting";
41 
42     private static final String AUTOLOCK_METRIC_KEY = "private_space_autolock_mode";
43     private PrivateSpaceMaintainer mPrivateSpaceMaintainer;
44     private CharSequence[] mAutoLockRadioOptions;
45     private CharSequence[] mAutoLockRadioValues;
46 
47     @Override
onCreate(@onNull Bundle icicle)48     public void onCreate(@NonNull Bundle icicle) {
49         if (android.os.Flags.allowPrivateProfile()
50                 && android.multiuser.Flags.supportAutolockForPrivateSpace()
51                 && android.multiuser.Flags.enablePrivateSpaceFeatures()) {
52             super.onCreate(icicle);
53         }
54     }
55 
56     @Override
onStart()57     public void onStart() {
58         super.onStart();
59         if (mPrivateSpaceMaintainer.isPrivateSpaceLocked()) {
60             finish();
61         }
62     }
63 
64     @Override
onAttach(@onNull Context context)65     public void onAttach(@NonNull Context context) {
66         super.onAttach(context);
67         mPrivateSpaceMaintainer = PrivateSpaceMaintainer.getInstance(context);
68         mAutoLockRadioOptions =
69                 context.getResources().getStringArray(R.array.private_space_auto_lock_options);
70         mAutoLockRadioValues =
71                 context.getResources()
72                         .getStringArray(R.array.private_space_auto_lock_options_values);
73     }
74 
75     @Override
addStaticPreferences(PreferenceScreen screen)76     protected void addStaticPreferences(PreferenceScreen screen) {
77         final TopIntroPreference introPreference = new TopIntroPreference(screen.getContext());
78         introPreference.setTitle(R.string.private_space_auto_lock_page_summary);
79         screen.addPreference(introPreference);
80     }
81 
82     @Override
getMetricsCategory()83     public int getMetricsCategory() {
84         return SettingsEnums.PRIVATE_SPACE_SETTINGS;
85     }
86 
87     @Override
getPreferenceScreenResId()88     protected int getPreferenceScreenResId() {
89         return R.xml.private_space_auto_lock_settings;
90     }
91 
92     @Override
getCandidates()93     protected List<? extends CandidateInfo> getCandidates() {
94         final List<CandidateInfo> candidates = new ArrayList<>();
95         if (mAutoLockRadioValues != null) {
96             for (int i = 0; i < mAutoLockRadioValues.length; ++i) {
97                 candidates.add(
98                         new AutoLockCandidateInfo(
99                                 mAutoLockRadioOptions[i], mAutoLockRadioValues[i].toString()));
100             }
101         } else {
102             Log.e(TAG, "Autolock options do not exist.");
103         }
104         return candidates;
105     }
106 
107     @Override
getDefaultKey()108     protected String getDefaultKey() {
109         return Integer.toString(mPrivateSpaceMaintainer.getPrivateSpaceAutoLockSetting());
110     }
111 
112     @Override
setDefaultKey(String key)113     protected boolean setDefaultKey(String key) {
114         try {
115             @Settings.Secure.PrivateSpaceAutoLockOption final int value = Integer.parseInt(key);
116             mPrivateSpaceMaintainer.setPrivateSpaceAutoLockSetting(value);
117             mMetricsFeatureProvider.action(
118                     mMetricsFeatureProvider.getAttribution(getActivity()),
119                     SettingsEnums.ACTION_SET_PRIVATE_SPACE_AUTOLOCK,
120                     getMetricsCategory(),
121                     AUTOLOCK_METRIC_KEY,
122                     value /* value */);
123         } catch (NumberFormatException e) {
124             Log.e(TAG, "could not persist screen timeout setting", e);
125         }
126         return true;
127     }
128 
129     private static class AutoLockCandidateInfo extends CandidateInfo {
130         private final CharSequence mLabel;
131         private final String mKey;
132 
AutoLockCandidateInfo(CharSequence label, String key)133         AutoLockCandidateInfo(CharSequence label, String key) {
134             super(true);
135             mLabel = label;
136             mKey = key;
137         }
138 
139         @NonNull
140         @Override
loadLabel()141         public CharSequence loadLabel() {
142             return mLabel;
143         }
144 
145         @Nullable
146         @Override
loadIcon()147         public Drawable loadIcon() {
148             return null;
149         }
150 
151         @NonNull
152         @Override
getKey()153         public String getKey() {
154             return mKey;
155         }
156     }
157 }
158