From 900adce6ca6fa6e0034b43898ed663f0386ef998 Mon Sep 17 00:00:00 2001 From: Sudheer Shanka Date: Fri, 4 Mar 2016 16:58:34 -0800 Subject: [PATCH] Update the timeout list dialog in display and security settings. Currently, we add padlocks to all the timeout options which are more than the timeout enforced by the admin. Instead of adding the padlocks to the options, updated the timeout list dialog to have a disabled by admin message as dialog footer. Bug: 27564363 Change-Id: I66ce35e92c044ce1864d14e1fc0e83881f8f4ab0 --- .../admin_disabled_other_options_footer.xml | 36 +++++ res/values/strings.xml | 4 + res/xml/display_settings.xml | 2 +- res/xml/security_settings_password_sub.xml | 2 +- res/xml/security_settings_pattern_sub.xml | 2 +- res/xml/security_settings_pin_sub.xml | 2 +- src/com/android/settings/DisplaySettings.java | 72 ++-------- .../android/settings/SecuritySettings.java | 70 ++-------- .../settings/TimeoutListPreference.java | 124 ++++++++++++++++++ 9 files changed, 191 insertions(+), 123 deletions(-) create mode 100644 res/layout/admin_disabled_other_options_footer.xml create mode 100644 src/com/android/settings/TimeoutListPreference.java diff --git a/res/layout/admin_disabled_other_options_footer.xml b/res/layout/admin_disabled_other_options_footer.xml new file mode 100644 index 00000000000..31327bbfe72 --- /dev/null +++ b/res/layout/admin_disabled_other_options_footer.xml @@ -0,0 +1,36 @@ + + + + + + + \ No newline at end of file diff --git a/res/values/strings.xml b/res/values/strings.xml index 6a1ab9c6793..5c9e910e536 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -4677,6 +4677,10 @@ By proceeding, your User will be managed by your Administrator which may also be able to store associated data, in addition to your personal data.\n\nYour Administrator has the ability to monitor and manage settings, access, apps, and data associated with this User, including network activity and your device\u2019s location information. + + Other options are disabled by your administrator. + More details + Untitled diff --git a/res/xml/display_settings.xml b/res/xml/display_settings.xml index 039cc404a18..c19f8d9349d 100644 --- a/res/xml/display_settings.xml +++ b/res/xml/display_settings.xml @@ -47,7 +47,7 @@ settings:keywords="@string/keywords_display_wallpaper" android:fragment="com.android.settings.WallpaperTypeSettings" /> - - - - = timeout) { best = i; @@ -292,59 +288,6 @@ public class DisplaySettings extends SettingsPreferenceFragment implements preference.setSummary(summary); } - private void disableUnusableTimeouts(RestrictedListPreference screenTimeoutPreference) { - final EnforcedAdmin admin = RestrictedLockUtils.checkIfMaximumTimeToLockIsSet( - getActivity()); - if (admin == null) { - return; // policy not enforced - } - - final DevicePolicyManager dpm = (DevicePolicyManager) getActivity().getSystemService( - Context.DEVICE_POLICY_SERVICE); - if (dpm == null) { - return; - } - final long maxTimeout = dpm.getMaximumTimeToLock(null); - final CharSequence[] entries = screenTimeoutPreference.getEntries(); - final CharSequence[] values = screenTimeoutPreference.getEntryValues(); - long maxTimeoutSelectable = 0; - int maxTimeoutEntryIndex = -1; - for (int i = 0; i < values.length; i++) { - long timeout = Long.parseLong(values[i].toString()); - if (timeout > maxTimeout) { - break; - } - maxTimeoutSelectable = timeout; - maxTimeoutEntryIndex = i; - } - // If there are no possible options for the user, then set this preference as disabled - // by admin, otherwise remove the padlock in case it was set earlier. - if (maxTimeoutSelectable == 0) { - screenTimeoutPreference.setDisabledByAdmin(admin); - return; - } else { - screenTimeoutPreference.setDisabledByAdmin(null); - } - - screenTimeoutPreference.clearRestrictedItems(); - // Set all the entries after the maximum selectable timeout as disabled by admin. - for (int i = maxTimeoutEntryIndex + 1; i < values.length; i++) { - screenTimeoutPreference.addRestrictedItem( - new RestrictedItem(entries[i], values[i], admin)); - } - - final int userPreference = Integer.parseInt(screenTimeoutPreference.getValue()); - if (userPreference <= maxTimeout) { - screenTimeoutPreference.setValue(String.valueOf(userPreference)); - } else if (maxTimeoutSelectable == maxTimeout) { - screenTimeoutPreference.setValue(String.valueOf(maxTimeout)); - } else { - // There will be no highlighted selection since nothing in the list matches - // maxTimeout. The user can still select anything less than maxTimeout. - // TODO: maybe append maxTimeout to the list and mark selected. - } - } - @Override public void onResume() { super.onResume(); @@ -354,7 +297,14 @@ public class DisplaySettings extends SettingsPreferenceFragment implements SCREEN_OFF_TIMEOUT, FALLBACK_SCREEN_TIMEOUT_VALUE); mScreenTimeoutPreference.setValue(String.valueOf(currentTimeout)); mScreenTimeoutPreference.setOnPreferenceChangeListener(this); - disableUnusableTimeouts(mScreenTimeoutPreference); + final DevicePolicyManager dpm = (DevicePolicyManager) getActivity().getSystemService( + Context.DEVICE_POLICY_SERVICE); + if (dpm != null) { + final EnforcedAdmin admin = RestrictedLockUtils.checkIfMaximumTimeToLockIsSet( + getActivity()); + final long maxTimeout = dpm.getMaximumTimeToLock(null); + mScreenTimeoutPreference.removeUnusableTimeouts(maxTimeout, admin); + } updateTimeoutPreferenceDescription(currentTimeout); } diff --git a/src/com/android/settings/SecuritySettings.java b/src/com/android/settings/SecuritySettings.java index 568ecf9cd96..4f4e543f353 100644 --- a/src/com/android/settings/SecuritySettings.java +++ b/src/com/android/settings/SecuritySettings.java @@ -55,7 +55,7 @@ import android.util.Log; import com.android.internal.logging.MetricsProto.MetricsEvent; import com.android.internal.widget.LockPatternUtils; -import com.android.settings.RestrictedListPreference; +import com.android.settings.TimeoutListPreference; import com.android.settings.TrustAgentUtils.TrustAgentComponentInfo; import com.android.settings.fingerprint.FingerprintSettings; import com.android.settings.search.BaseSearchIndexProvider; @@ -71,7 +71,6 @@ import java.util.List; import static android.provider.Settings.System.SCREEN_OFF_TIMEOUT; -import static com.android.settings.RestrictedListPreference.RestrictedItem; import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin; /** @@ -937,7 +936,7 @@ public class SecuritySettings extends SettingsPreferenceFragment private static final String SWITCH_PREFERENCE_KEYS[] = { KEY_LOCK_AFTER_TIMEOUT, KEY_VISIBLE_PATTERN, KEY_POWER_INSTANTLY_LOCKS, KEY_REQUIRE_CRED_BEFORE_STARTUP }; - private RestrictedListPreference mLockAfter; + private TimeoutListPreference mLockAfter; private SwitchPreference mVisiblePattern; private SwitchPreference mPowerButtonInstantlyLocks; private RestrictedPreference mOwnerInfoPref; @@ -994,7 +993,7 @@ public class SecuritySettings extends SettingsPreferenceFragment addPreferencesFromResource(resid); // lock after preference - mLockAfter = (RestrictedListPreference) findPreference(KEY_LOCK_AFTER_TIMEOUT); + mLockAfter = (TimeoutListPreference) findPreference(KEY_LOCK_AFTER_TIMEOUT); if (mLockAfter != null) { setupLockAfterPreference(); updateLockAfterPreferenceSummary(); @@ -1065,18 +1064,17 @@ public class SecuritySettings extends SettingsPreferenceFragment Settings.Secure.LOCK_SCREEN_LOCK_AFTER_TIMEOUT, 5000); mLockAfter.setValue(String.valueOf(currentTimeout)); mLockAfter.setOnPreferenceChangeListener(this); - final EnforcedAdmin admin = RestrictedLockUtils.checkIfMaximumTimeToLockIsSet( - getActivity()); - if (admin != null) { - final long adminTimeout = (mDPM != null ? mDPM.getMaximumTimeToLock(null) : 0); + if (mDPM != null) { + final EnforcedAdmin admin = RestrictedLockUtils.checkIfMaximumTimeToLockIsSet( + getActivity()); + final long adminTimeout = mDPM.getMaximumTimeToLock(null); final long displayTimeout = Math.max(0, Settings.System.getInt(getContentResolver(), SCREEN_OFF_TIMEOUT, 0)); - if (adminTimeout > 0) { - // This setting is a slave to display timeout when a device policy is enforced. - // As such, maxLockTimeout = adminTimeout - displayTimeout. - // If there isn't enough time, shows "immediately" setting. - disableUnusableTimeouts(Math.max(0, adminTimeout - displayTimeout), admin); - } + // This setting is a slave to display timeout when a device policy is enforced. + // As such, maxLockTimeout = adminTimeout - displayTimeout. + // If there isn't enough time, shows "immediately" setting. + final long maxTimeout = Math.max(0, adminTimeout - displayTimeout); + mLockAfter.removeUnusableTimeouts(maxTimeout, admin); } } @@ -1092,9 +1090,6 @@ public class SecuritySettings extends SettingsPreferenceFragment final CharSequence[] values = mLockAfter.getEntryValues(); int best = 0; for (int i = 0; i < values.length; i++) { - if (mLockAfter.isRestrictedForEntry(entries[i])) { - break; - } long timeout = Long.valueOf(values[i].toString()); if (currentTimeout >= timeout) { best = i; @@ -1117,47 +1112,6 @@ public class SecuritySettings extends SettingsPreferenceFragment mLockAfter.setSummary(summary); } - private void disableUnusableTimeouts(long maxTimeout, EnforcedAdmin admin) { - final CharSequence[] entries = mLockAfter.getEntries(); - final CharSequence[] values = mLockAfter.getEntryValues(); - long maxTimeoutSelectable = 0; - int maxTimeoutEntryIndex = -1; - for (int i = 0; i < values.length; i++) { - long timeout = Long.parseLong(values[i].toString()); - if (timeout > maxTimeout) { - break; - } - maxTimeoutSelectable = timeout; - maxTimeoutEntryIndex = i; - } - // If there are no possible options for the user, then set this preference as - // disabled by admin, otherwise remove the padlock in case it was set earlier. - if (maxTimeoutSelectable == 0) { - mLockAfter.setDisabledByAdmin(admin); - return; - } else { - mLockAfter.setDisabledByAdmin(null); - } - - mLockAfter.clearRestrictedItems(); - // Set all the entries after the maximum selectable timeout as disabled by admin. - for (int i = maxTimeoutEntryIndex + 1; i < values.length; i++) { - mLockAfter.addRestrictedItem( - new RestrictedItem(entries[i], values[i], admin)); - } - - final int userPreference = Integer.valueOf(mLockAfter.getValue()); - if (userPreference <= maxTimeout) { - mLockAfter.setValue(String.valueOf(userPreference)); - } else if (maxTimeoutSelectable == maxTimeout) { - mLockAfter.setValue(String.valueOf(maxTimeout)); - } else { - // There will be no highlighted selection since nothing in the list matches - // maxTimeout. The user can still select anything less than maxTimeout. - // TODO: maybe append maxTimeout to the list and mark selected. - } - } - public void updateOwnerInfo() { if (mOwnerInfoPref != null) { if (mLockPatternUtils.isDeviceOwnerInfoEnabled()) { diff --git a/src/com/android/settings/TimeoutListPreference.java b/src/com/android/settings/TimeoutListPreference.java new file mode 100644 index 00000000000..4b0fd9d0451 --- /dev/null +++ b/src/com/android/settings/TimeoutListPreference.java @@ -0,0 +1,124 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settings; + +import android.app.AlertDialog; +import android.app.Dialog; +import android.app.admin.DevicePolicyManager; +import android.content.Context; +import android.content.DialogInterface; +import android.util.AttributeSet; +import android.view.View; + +import com.android.settingslib.RestrictedLockUtils; + +import java.util.ArrayList; + +import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin; + +public class TimeoutListPreference extends RestrictedListPreference { + private EnforcedAdmin mAdmin; + private final CharSequence[] mInitialEntries; + private final CharSequence[] mInitialValues; + + public TimeoutListPreference(Context context, AttributeSet attrs) { + super(context, attrs); + mInitialEntries = getEntries(); + mInitialValues = getEntryValues(); + } + + @Override + protected void onPrepareDialogBuilder(AlertDialog.Builder builder, + DialogInterface.OnClickListener listener) { + super.onPrepareDialogBuilder(builder, listener); + if (mAdmin != null) { + builder.setView(R.layout.admin_disabled_other_options_footer); + } else { + builder.setView(null); + } + } + + @Override + protected void onDialogCreated(Dialog dialog) { + super.onDialogCreated(dialog); + dialog.create(); + if (mAdmin != null) { + View footerView = dialog.findViewById(R.id.admin_disabled_other_options); + footerView.findViewById(R.id.admin_more_details_link).setOnClickListener( + new View.OnClickListener() { + @Override + public void onClick(View view) { + RestrictedLockUtils.sendShowAdminSupportDetailsIntent( + getContext(), mAdmin); + } + }); + } + } + + public void removeUnusableTimeouts(long maxTimeout, EnforcedAdmin admin) { + final DevicePolicyManager dpm = (DevicePolicyManager) getContext().getSystemService( + Context.DEVICE_POLICY_SERVICE); + if (dpm == null) { + return; + } + + if (admin == null && mAdmin == null && !isDisabledByAdmin()) { + return; + } + if (admin == null) { + maxTimeout = Long.MAX_VALUE; + } + + ArrayList revisedEntries = new ArrayList(); + ArrayList revisedValues = new ArrayList(); + for (int i = 0; i < mInitialValues.length; ++i) { + long timeout = Long.parseLong(mInitialValues[i].toString()); + if (timeout <= maxTimeout) { + revisedEntries.add(mInitialEntries[i]); + revisedValues.add(mInitialValues[i]); + } + } + + // If there are no possible options for the user, then set this preference as disabled + // by admin, otherwise remove the padlock in case it was set earlier. + if (revisedValues.size() == 0) { + setDisabledByAdmin(admin); + return; + } else { + setDisabledByAdmin(null); + } + + if (revisedEntries.size() != getEntries().length) { + final int userPreference = Integer.parseInt(getValue()); + setEntries(revisedEntries.toArray(new CharSequence[0])); + setEntryValues(revisedValues.toArray(new CharSequence[0])); + mAdmin = admin; + if (userPreference <= maxTimeout) { + setValue(String.valueOf(userPreference)); + } else if (revisedValues.size() > 0 + && Long.parseLong(revisedValues.get(revisedValues.size() - 1).toString()) + == maxTimeout) { + // If the last one happens to be the same as the max timeout, select that + setValue(String.valueOf(maxTimeout)); + } else { + // There will be no highlighted selection since nothing in the list matches + // maxTimeout. The user can still select anything less than maxTimeout. + // TODO: maybe append maxTimeout to the list and mark selected. + } + } + } +} \ No newline at end of file