Skip to content

Commit f66554c

Browse files
committed
Implement lock time and failures-to-wipe in a separate Fragment
Change-Id: Ie5dd17e2bee493cf1671dc7382edcce32d426857
1 parent 43813b6 commit f66554c

File tree

5 files changed

+216
-33
lines changed

5 files changed

+216
-33
lines changed

app/src/main/java/com/afwsamples/testdpc/policy/PolicyManagementFragment.java

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
import com.afwsamples.testdpc.policy.blockuninstallation.BlockUninstallationInfoArrayAdapter;
7272
import com.afwsamples.testdpc.policy.certificate.DelegatedCertInstallerFragment;
7373
import com.afwsamples.testdpc.policy.datausage.NetworkUsageStatsFragment;
74+
import com.afwsamples.testdpc.policy.keyguard.LockScreenPolicyFragment;
7475
import com.afwsamples.testdpc.policy.keyguard.PasswordConstraintsFragment;
7576
import com.afwsamples.testdpc.policy.locktask.KioskModeActivity;
7677
import com.afwsamples.testdpc.policy.locktask.LockTaskAppInfoArrayAdapter;
@@ -210,8 +211,7 @@ public class PolicyManagementFragment extends PreferenceFragment implements
210211
= "keyguard_disable_unredacted_notifications";
211212
private static final String KEYGUARD_DISABLE_WIDGETS = "keyguard_disable_widgets";
212213
private static final String KEYGUARD_PREFERENCES = "keyguard_preferences";
213-
private static final String KEY_MAX_FAILS_BEFORE_WIPE = "key_max_fails_before_wipe";
214-
private static final String KEY_MAX_TIME_SCREEN_LOCK = "key_max_time_screen_lock";
214+
private static final String LOCK_SCREEN_POLICY_KEY = "lock_screen_policy";
215215
private static final String MANAGE_APP_PERMISSIONS_KEY = "manage_app_permissions";
216216
private static final String MANAGE_APP_RESTRICTIONS_KEY = "manage_app_restrictions";
217217
private static final String MANAGED_PROFILE_SPECIFIC_POLICIES_KEY = "managed_profile_policies";
@@ -363,6 +363,7 @@ public void onCreate(Bundle savedInstanceState) {
363363
mDisableScreenCaptureSwitchPreference = (SwitchPreference) findPreference(
364364
DISABLE_SCREEN_CAPTURE_KEY);
365365
mDisableScreenCaptureSwitchPreference.setOnPreferenceChangeListener(this);
366+
findPreference(LOCK_SCREEN_POLICY_KEY).setOnPreferenceClickListener(this);
366367
findPreference(PASSWORD_CONSTRAINTS_KEY).setOnPreferenceClickListener(this);
367368
findPreference(SYSTEM_UPDATE_POLICY_KEY).setOnPreferenceClickListener(this);
368369
findPreference(NETWORK_STATS_KEY).setOnPreferenceClickListener(this);
@@ -396,8 +397,6 @@ public void onCreate(Bundle savedInstanceState) {
396397
findPreference(MANAGED_PROFILE_SPECIFIC_POLICIES_KEY).setOnPreferenceClickListener(this);
397398
findPreference(SET_PERMISSION_POLICY_KEY).setOnPreferenceClickListener(this);
398399
findPreference(MANAGE_APP_PERMISSIONS_KEY).setOnPreferenceClickListener(this);
399-
findPreference(KEY_MAX_TIME_SCREEN_LOCK).setOnPreferenceChangeListener(this);
400-
findPreference(KEY_MAX_FAILS_BEFORE_WIPE).setOnPreferenceChangeListener(this);
401400
findPreference(CREATE_WIFI_CONFIGURATION_KEY).setOnPreferenceClickListener(this);
402401
findPreference(WIFI_CONFIG_LOCKDOWN_ENABLE_KEY).setOnPreferenceChangeListener(this);
403402
findPreference(MODIFY_WIFI_CONFIGURATION_KEY).setOnPreferenceClickListener(this);
@@ -547,6 +546,9 @@ public void onPositiveButtonClicked(String[] lockTaskArray) {
547546
case MANAGED_PROFILE_SPECIFIC_POLICIES_KEY:
548547
showFragment(new ProfilePolicyManagementFragment());
549548
return true;
549+
case LOCK_SCREEN_POLICY_KEY:
550+
showFragment(new LockScreenPolicyFragment());
551+
return true;
550552
case PASSWORD_CONSTRAINTS_KEY:
551553
showFragment(new PasswordConstraintsFragment());
552554
return true;
@@ -632,22 +634,6 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
632634
// Reload UI to verify that screen capture was enabled / disabled correctly.
633635
reloadScreenCaptureDisableUi();
634636
return true;
635-
case KEY_MAX_TIME_SCREEN_LOCK:
636-
try {
637-
mDevicePolicyManager.setMaximumTimeToLock(mAdminComponentName,
638-
Long.parseLong((String) newValue) * MS_PER_SECOND);
639-
} catch (NumberFormatException e) {
640-
showToast(R.string.not_valid_input);
641-
}
642-
return true;
643-
case KEY_MAX_FAILS_BEFORE_WIPE:
644-
try {
645-
mDevicePolicyManager.setMaximumFailedPasswordsForWipe(mAdminComponentName,
646-
Integer.parseInt((String) newValue));
647-
} catch (NumberFormatException e) {
648-
showToast(R.string.not_valid_input);
649-
}
650-
return true;
651637
case STAY_ON_WHILE_PLUGGED_IN:
652638
mDevicePolicyManager.setGlobalSetting(mAdminComponentName,
653639
Settings.Global.STAY_ON_WHILE_PLUGGED_IN,
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
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+
}

app/src/main/res/values/strings.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,8 +453,13 @@
453453

454454
<!-- Strings for screen lock policies -->
455455
<string name="lock_category">Lock screen</string>
456-
<string name="maximum_lock_time">Max time to screen lock (seconds)</string>
456+
457+
<string name="lock_screen_policy">Lock screen restrictions</string>
458+
<string name="maximum_lock_time_category">Max time to screen lock</string>
459+
<string name="maximum_lock_time_seconds">Max time to screen lock (seconds)</string>
460+
<string name="maximum_lock_time_all_admins">Max time as set by all/other admins</string>
457461
<string name="maximum_password_fails">Max password failures for local wipe</string>
462+
<string name="maximum_password_fails_all_admins">Max failures as set by all/other admins</string>
458463
<string name="not_valid_input">Failed to update: Not a valid input</string>
459464

460465
<!-- Strings for password policies -->

app/src/main/res/xml/device_policy_header.xml

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -160,18 +160,9 @@
160160

161161
<PreferenceCategory
162162
android:title="@string/lock_category" >
163-
<EditTextPreference
164-
android:key="key_max_time_screen_lock"
165-
android:title="@string/maximum_lock_time"
166-
android:dialogTitle="@string/maximum_lock_time"
167-
android:inputType="number"
168-
android:defaultValue="0" />
169-
<EditTextPreference
170-
android:key="key_max_fails_before_wipe"
171-
android:title="@string/maximum_password_fails"
172-
android:dialogTitle="@string/maximum_password_fails"
173-
android:inputType="number"
174-
android:defaultValue="0" />
163+
<Preference
164+
android:key="lock_screen_policy"
165+
android:title="@string/lock_screen_policy"/>
175166
<Preference
176167
android:key="password_constraints"
177168
android:title="@string/password_constraints"/>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
Copyright (C) 2016 The Android Open Source Project
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
18+
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
19+
<PreferenceCategory
20+
android:title="@string/maximum_lock_time_category" >
21+
<EditTextPreference
22+
android:key="key_max_time_screen_lock"
23+
android:title="@string/maximum_lock_time_seconds"
24+
android:dialogTitle="@string/maximum_lock_time_seconds"
25+
android:inputType="number"
26+
android:defaultValue="0" />
27+
<Preference
28+
android:key="key_max_time_screen_lock_aggregate"
29+
android:title="@string/maximum_lock_time_all_admins"
30+
android:selectable="false" />
31+
</PreferenceCategory>
32+
33+
<PreferenceCategory
34+
android:title="@string/maximum_password_fails" >
35+
<EditTextPreference
36+
android:key="key_max_fails_before_wipe"
37+
android:title="@string/maximum_password_fails"
38+
android:dialogTitle="@string/maximum_password_fails"
39+
android:inputType="number"
40+
android:defaultValue="0" />
41+
<Preference
42+
android:key="key_max_fails_before_wipe_aggregate"
43+
android:title="@string/maximum_password_fails_all_admins"
44+
android:selectable="false" />
45+
</PreferenceCategory>
46+
</PreferenceScreen>

0 commit comments

Comments
 (0)