blob: 4b0fd9d04518a6d7205efa8997fe1abee7fd1424 [file] [log] [blame]
Sudheer Shanka900adce2016-03-04 16:58:34 -08001/*
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
17package com.android.settings;
18
19import android.app.AlertDialog;
20import android.app.Dialog;
21import android.app.admin.DevicePolicyManager;
22import android.content.Context;
23import android.content.DialogInterface;
24import android.util.AttributeSet;
25import android.view.View;
26
27import com.android.settingslib.RestrictedLockUtils;
28
29import java.util.ArrayList;
30
31import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
32
33public class TimeoutListPreference extends RestrictedListPreference {
34 private EnforcedAdmin mAdmin;
35 private final CharSequence[] mInitialEntries;
36 private final CharSequence[] mInitialValues;
37
38 public TimeoutListPreference(Context context, AttributeSet attrs) {
39 super(context, attrs);
40 mInitialEntries = getEntries();
41 mInitialValues = getEntryValues();
42 }
43
44 @Override
45 protected void onPrepareDialogBuilder(AlertDialog.Builder builder,
46 DialogInterface.OnClickListener listener) {
47 super.onPrepareDialogBuilder(builder, listener);
48 if (mAdmin != null) {
49 builder.setView(R.layout.admin_disabled_other_options_footer);
50 } else {
51 builder.setView(null);
52 }
53 }
54
55 @Override
56 protected void onDialogCreated(Dialog dialog) {
57 super.onDialogCreated(dialog);
58 dialog.create();
59 if (mAdmin != null) {
60 View footerView = dialog.findViewById(R.id.admin_disabled_other_options);
61 footerView.findViewById(R.id.admin_more_details_link).setOnClickListener(
62 new View.OnClickListener() {
63 @Override
64 public void onClick(View view) {
65 RestrictedLockUtils.sendShowAdminSupportDetailsIntent(
66 getContext(), mAdmin);
67 }
68 });
69 }
70 }
71
72 public void removeUnusableTimeouts(long maxTimeout, EnforcedAdmin admin) {
73 final DevicePolicyManager dpm = (DevicePolicyManager) getContext().getSystemService(
74 Context.DEVICE_POLICY_SERVICE);
75 if (dpm == null) {
76 return;
77 }
78
79 if (admin == null && mAdmin == null && !isDisabledByAdmin()) {
80 return;
81 }
82 if (admin == null) {
83 maxTimeout = Long.MAX_VALUE;
84 }
85
86 ArrayList<CharSequence> revisedEntries = new ArrayList<CharSequence>();
87 ArrayList<CharSequence> revisedValues = new ArrayList<CharSequence>();
88 for (int i = 0; i < mInitialValues.length; ++i) {
89 long timeout = Long.parseLong(mInitialValues[i].toString());
90 if (timeout <= maxTimeout) {
91 revisedEntries.add(mInitialEntries[i]);
92 revisedValues.add(mInitialValues[i]);
93 }
94 }
95
96 // If there are no possible options for the user, then set this preference as disabled
97 // by admin, otherwise remove the padlock in case it was set earlier.
98 if (revisedValues.size() == 0) {
99 setDisabledByAdmin(admin);
100 return;
101 } else {
102 setDisabledByAdmin(null);
103 }
104
105 if (revisedEntries.size() != getEntries().length) {
106 final int userPreference = Integer.parseInt(getValue());
107 setEntries(revisedEntries.toArray(new CharSequence[0]));
108 setEntryValues(revisedValues.toArray(new CharSequence[0]));
109 mAdmin = admin;
110 if (userPreference <= maxTimeout) {
111 setValue(String.valueOf(userPreference));
112 } else if (revisedValues.size() > 0
113 && Long.parseLong(revisedValues.get(revisedValues.size() - 1).toString())
114 == maxTimeout) {
115 // If the last one happens to be the same as the max timeout, select that
116 setValue(String.valueOf(maxTimeout));
117 } else {
118 // There will be no highlighted selection since nothing in the list matches
119 // maxTimeout. The user can still select anything less than maxTimeout.
120 // TODO: maybe append maxTimeout to the list and mark selected.
121 }
122 }
123 }
124}