2 * Copyright (C) 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings;
19 import android.app.AlertDialog;
20 import android.app.Dialog;
21 import android.app.admin.DevicePolicyManager;
22 import android.content.Context;
23 import android.content.DialogInterface;
24 import android.util.AttributeSet;
25 import android.view.View;
27 import com.android.settingslib.RestrictedLockUtils;
29 import java.util.ArrayList;
31 import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
33 public class TimeoutListPreference extends RestrictedListPreference {
34 private EnforcedAdmin mAdmin;
35 private final CharSequence[] mInitialEntries;
36 private final CharSequence[] mInitialValues;
38 public TimeoutListPreference(Context context, AttributeSet attrs) {
39 super(context, attrs);
40 mInitialEntries = getEntries();
41 mInitialValues = getEntryValues();
45 protected void onPrepareDialogBuilder(AlertDialog.Builder builder,
46 DialogInterface.OnClickListener listener) {
47 super.onPrepareDialogBuilder(builder, listener);
49 builder.setView(R.layout.admin_disabled_other_options_footer);
51 builder.setView(null);
56 protected void onDialogCreated(Dialog dialog) {
57 super.onDialogCreated(dialog);
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() {
64 public void onClick(View view) {
65 RestrictedLockUtils.sendShowAdminSupportDetailsIntent(
66 getContext(), mAdmin);
72 public void removeUnusableTimeouts(long maxTimeout, EnforcedAdmin admin) {
73 final DevicePolicyManager dpm = (DevicePolicyManager) getContext().getSystemService(
74 Context.DEVICE_POLICY_SERVICE);
79 if (admin == null && mAdmin == null && !isDisabledByAdmin()) {
83 maxTimeout = Long.MAX_VALUE;
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]);
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);
102 setDisabledByAdmin(null);
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]));
110 if (userPreference <= maxTimeout) {
111 setValue(String.valueOf(userPreference));
112 } else if (revisedValues.size() > 0
113 && Long.parseLong(revisedValues.get(revisedValues.size() - 1).toString())
115 // If the last one happens to be the same as the max timeout, select that
116 setValue(String.valueOf(maxTimeout));
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.