• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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 com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
20 
21 import android.app.Dialog;
22 import android.app.admin.DevicePolicyManager;
23 import android.content.Context;
24 import android.content.DialogInterface;
25 import android.util.AttributeSet;
26 import android.util.Log;
27 import android.view.View;
28 
29 import androidx.appcompat.app.AlertDialog.Builder;
30 
31 import com.android.settings.R;
32 import com.android.settings.RestrictedListPreference;
33 import com.android.settingslib.RestrictedLockUtils;
34 
35 import java.util.ArrayList;
36 
37 
38 public class TimeoutListPreference extends RestrictedListPreference {
39     private static final String TAG = "TimeoutListPreference";
40     private EnforcedAdmin mAdmin;
41     private final CharSequence[] mInitialEntries;
42     private final CharSequence[] mInitialValues;
43 
TimeoutListPreference(Context context, AttributeSet attrs)44     public TimeoutListPreference(Context context, AttributeSet attrs) {
45         super(context, attrs);
46         mInitialEntries = getEntries();
47         mInitialValues = getEntryValues();
48     }
49 
50     @Override
onPrepareDialogBuilder(Builder builder, DialogInterface.OnClickListener listener)51     protected void onPrepareDialogBuilder(Builder builder,
52             DialogInterface.OnClickListener listener) {
53         super.onPrepareDialogBuilder(builder, listener);
54         if (mAdmin != null) {
55             builder.setView(R.layout.admin_disabled_other_options_footer);
56         } else {
57             builder.setView(null);
58         }
59     }
60 
61     @Override
onDialogCreated(Dialog dialog)62     protected void onDialogCreated(Dialog dialog) {
63         super.onDialogCreated(dialog);
64         dialog.create();
65         if (mAdmin != null) {
66             View footerView = dialog.findViewById(R.id.admin_disabled_other_options);
67             footerView.findViewById(R.id.admin_more_details_link).setOnClickListener(
68                     new View.OnClickListener() {
69                         @Override
70                         public void onClick(View view) {
71                             RestrictedLockUtils.sendShowAdminSupportDetailsIntent(
72                                     getContext(), mAdmin);
73                         }
74                     });
75         }
76     }
77 
removeUnusableTimeouts(long maxTimeout, EnforcedAdmin admin)78     public void removeUnusableTimeouts(long maxTimeout, EnforcedAdmin admin) {
79         final DevicePolicyManager dpm = (DevicePolicyManager) getContext().getSystemService(
80                 Context.DEVICE_POLICY_SERVICE);
81         if (dpm == null) {
82             return;
83         }
84 
85         if (admin == null && mAdmin == null && !isDisabledByAdmin()) {
86             return;
87         }
88         if (admin == null) {
89             maxTimeout = Long.MAX_VALUE;
90         }
91 
92         ArrayList<CharSequence> revisedEntries = new ArrayList<CharSequence>();
93         ArrayList<CharSequence> revisedValues = new ArrayList<CharSequence>();
94         for (int i = 0; i < mInitialValues.length; ++i) {
95             long timeout = Long.parseLong(mInitialValues[i].toString());
96             if (timeout <= maxTimeout) {
97                 revisedEntries.add(mInitialEntries[i]);
98                 revisedValues.add(mInitialValues[i]);
99             }
100         }
101 
102         // If there are no possible options for the user, then set this preference as disabled
103         // by admin, otherwise remove the padlock in case it was set earlier.
104         if (revisedValues.size() == 0) {
105             setDisabledByAdmin(admin);
106             return;
107         } else {
108             setDisabledByAdmin(null);
109         }
110 
111         if (revisedEntries.size() != getEntries().length) {
112             final int userPreference = Integer.parseInt(getValue());
113             setEntries(revisedEntries.toArray(new CharSequence[0]));
114             setEntryValues(revisedValues.toArray(new CharSequence[0]));
115             mAdmin = admin;
116             if (userPreference <= maxTimeout) {
117                 setValue(String.valueOf(userPreference));
118             } else if (revisedValues.size() > 0
119                     && Long.parseLong(revisedValues.get(revisedValues.size() - 1).toString())
120                             == maxTimeout) {
121                 // If the last one happens to be the same as the max timeout, select that
122                 setValue(String.valueOf(maxTimeout));
123             } else {
124                 // The selected time out value is longer than the max timeout allowed by the admin.
125                 // Select the largest value from the list by default.
126                 Log.w(TAG, "Default to longest timeout. Value disabled by admin:" + userPreference);
127                 setValue(revisedValues.get(revisedValues.size() - 1).toString());
128             }
129         }
130     }
131 }
132