|
| 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.afwsamples.testdpc.policy.keyguard; |
| 18 | + |
| 19 | +import android.app.Fragment; |
| 20 | +import android.app.admin.DevicePolicyManager; |
| 21 | +import android.content.ComponentName; |
| 22 | +import android.content.Context; |
| 23 | +import android.os.Bundle; |
| 24 | +import android.preference.EditTextPreference; |
| 25 | +import android.preference.Preference; |
| 26 | +import android.preference.PreferenceFragment; |
| 27 | +import android.widget.Toast; |
| 28 | + |
| 29 | +import com.afwsamples.testdpc.DeviceAdminReceiver; |
| 30 | +import com.afwsamples.testdpc.R; |
| 31 | + |
| 32 | +import java.util.concurrent.TimeUnit; |
| 33 | + |
| 34 | +/** |
| 35 | + * This fragment provides functionalities to set policies on keyguard interaction as a profile |
| 36 | + * or device owner. |
| 37 | + */ |
| 38 | +public final class LockScreenPolicyFragment extends PreferenceFragment implements |
| 39 | + Preference.OnPreferenceChangeListener { |
| 40 | + |
| 41 | + private DevicePolicyManager mDevicePolicyManager; |
| 42 | + private ComponentName mAdminComponent; |
| 43 | + |
| 44 | + private DevicePolicyManager getDpm() { |
| 45 | + return mDevicePolicyManager; |
| 46 | + } |
| 47 | + |
| 48 | + private ComponentName getAdmin() { |
| 49 | + return mAdminComponent; |
| 50 | + } |
| 51 | + |
| 52 | + abstract static class Keys { |
| 53 | + static final String MAX_FAILS_BEFORE_WIPE = "key_max_fails_before_wipe"; |
| 54 | + static final String MAX_FAILS_BEFORE_WIPE_ALL = "key_max_fails_before_wipe_aggregate"; |
| 55 | + |
| 56 | + static final String MAX_TIME_SCREEN_LOCK = "key_max_time_screen_lock"; |
| 57 | + static final String MAX_TIME_SCREEN_LOCK_ALL = "key_max_time_screen_lock_aggregate"; |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public void onCreate(Bundle savedInstanceState) { |
| 62 | + super.onCreate(savedInstanceState); |
| 63 | + getActivity().getActionBar().setTitle(R.string.lock_screen_policy); |
| 64 | + |
| 65 | + mDevicePolicyManager = (DevicePolicyManager) |
| 66 | + getActivity().getSystemService(Context.DEVICE_POLICY_SERVICE); |
| 67 | + mAdminComponent = DeviceAdminReceiver.getComponentName(getActivity()); |
| 68 | + |
| 69 | + addPreferencesFromResource(R.xml.lock_screen_preferences); |
| 70 | + |
| 71 | + setup(Keys.MAX_FAILS_BEFORE_WIPE, getDpm().getMaximumFailedPasswordsForWipe(getAdmin())); |
| 72 | + setup(Keys.MAX_TIME_SCREEN_LOCK, |
| 73 | + TimeUnit.MILLISECONDS.toSeconds(getDpm().getMaximumTimeToLock(getAdmin()))); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public void onResume() { |
| 78 | + super.onResume(); |
| 79 | + updateAggregates(); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public boolean onPreferenceChange(Preference preference, Object newValue) { |
| 84 | + switch (preference.getKey()) { |
| 85 | + case Keys.MAX_FAILS_BEFORE_WIPE: |
| 86 | + try { |
| 87 | + final int setting = parseInt((String) newValue); |
| 88 | + getDpm().setMaximumFailedPasswordsForWipe(getAdmin(), setting); |
| 89 | + preference.setSummary(setting != 0 ? Integer.toString(setting) : null); |
| 90 | + } catch (NumberFormatException e) { |
| 91 | + showToast(R.string.not_valid_input); |
| 92 | + return false; |
| 93 | + } |
| 94 | + break; |
| 95 | + case Keys.MAX_TIME_SCREEN_LOCK: |
| 96 | + try { |
| 97 | + final long setting = parseLong((String) newValue); |
| 98 | + getDpm().setMaximumTimeToLock(getAdmin(), TimeUnit.SECONDS.toMillis(setting)); |
| 99 | + preference.setSummary(setting != 0 ? Long.toString(setting) : null); |
| 100 | + } catch (NumberFormatException e) { |
| 101 | + showToast(R.string.not_valid_input); |
| 102 | + return false; |
| 103 | + } |
| 104 | + break; |
| 105 | + default: |
| 106 | + return false; |
| 107 | + } |
| 108 | + |
| 109 | + updateAggregates(); |
| 110 | + return true; |
| 111 | + } |
| 112 | + |
| 113 | + private void updateAggregates() { |
| 114 | + final int maxFailedPasswords = getDpm().getMaximumFailedPasswordsForWipe(null); |
| 115 | + findPreference(Keys.MAX_FAILS_BEFORE_WIPE_ALL).setSummary( |
| 116 | + maxFailedPasswords != 0 |
| 117 | + ? Integer.toString(maxFailedPasswords) |
| 118 | + : null); |
| 119 | + |
| 120 | + final long maxTimeToLock = getDpm().getMaximumTimeToLock(null); |
| 121 | + findPreference(Keys.MAX_TIME_SCREEN_LOCK_ALL).setSummary( |
| 122 | + maxTimeToLock != 0 |
| 123 | + ? Long.toString(TimeUnit.MILLISECONDS.toSeconds(maxTimeToLock)) |
| 124 | + : null); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Set an initial value. Updates the summary to match. |
| 129 | + */ |
| 130 | + private void setup(String key, long adminSetting) { |
| 131 | + Preference pref = findPreference(key); |
| 132 | + pref.setOnPreferenceChangeListener(this); |
| 133 | + |
| 134 | + if (adminSetting != 0) { |
| 135 | + final String stringSetting = Long.toString(adminSetting); |
| 136 | + |
| 137 | + if (pref instanceof EditTextPreference) { |
| 138 | + ((EditTextPreference) pref).setText(stringSetting); |
| 139 | + } |
| 140 | + pref.setSummary(stringSetting); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + private int parseInt(String value) throws NumberFormatException { |
| 145 | + return value.length() != 0 ? Integer.parseInt(value) : 0; |
| 146 | + } |
| 147 | + |
| 148 | + private long parseLong(String value) throws NumberFormatException { |
| 149 | + return value.length() != 0 ? Long.parseLong(value) : 0L; |
| 150 | + } |
| 151 | + |
| 152 | + private void showToast(int titleId) { |
| 153 | + Toast.makeText(getActivity(), titleId, Toast.LENGTH_SHORT).show(); |
| 154 | + } |
| 155 | +} |
0 commit comments