Skip to content

Commit 70c9e22

Browse files
author
Alexandru-Andrei Rotaru
committed
Added save and restore configuration functions for kiosk mode.
Bug: 30062948 Change-Id: I758efd0b9113564dcb92b68671e4852769370c1b
1 parent 9e8ce04 commit 70c9e22

File tree

1 file changed

+48
-6
lines changed

1 file changed

+48
-6
lines changed

app/src/main/java/com/afwsamples/testdpc/policy/locktask/KioskModeActivity.java

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
import android.content.pm.PackageManager;
3636
import android.os.Build;
3737
import android.os.Bundle;
38+
import android.support.v4.os.BuildCompat;
39+
import android.util.ArrayMap;
3840
import android.util.Log;
3941
import android.view.LayoutInflater;
4042
import android.view.View;
@@ -75,6 +77,13 @@ public class KioskModeActivity extends Activity {
7577
public static final String LOCKED_APP_PACKAGE_LIST
7678
= "com.afwsamples.testdpc.policy.locktask.LOCKED_APP_PACKAGE_LIST";
7779

80+
private static final String[] KIOSK_USER_RESTRICTIONS = {
81+
DISALLOW_SAFE_BOOT,
82+
DISALLOW_FACTORY_RESET,
83+
DISALLOW_ADD_USER,
84+
DISALLOW_MOUNT_PHYSICAL_MEDIA,
85+
DISALLOW_ADJUST_VOLUME };
86+
7887
private ComponentName mAdminComponentName;
7988
private ArrayList<String> mKioskPackages;
8089
private DevicePolicyManager mDevicePolicyManager;
@@ -98,6 +107,7 @@ protected void onCreate(Bundle savedInstanceState) {
98107
}
99108
mKioskPackages.remove(getPackageName());
100109
mKioskPackages.add(getPackageName());
110+
101111
setDefaultKioskPolicies(true);
102112
} else {
103113
// after a reboot there is no need to set the policies again
@@ -164,12 +174,17 @@ private void setUserRestriction(String restriction, boolean disallow) {
164174
}
165175

166176
private void setDefaultKioskPolicies(boolean active) {
167-
// set user restrictions
168-
setUserRestriction(DISALLOW_SAFE_BOOT, active);
169-
setUserRestriction(DISALLOW_FACTORY_RESET, active);
170-
setUserRestriction(DISALLOW_ADD_USER, active);
171-
setUserRestriction(DISALLOW_MOUNT_PHYSICAL_MEDIA, active);
172-
setUserRestriction(DISALLOW_ADJUST_VOLUME, active);
177+
// restore or save previous configuration
178+
if (active) {
179+
saveCurrentConfiguration();
180+
setUserRestriction(DISALLOW_SAFE_BOOT, active);
181+
setUserRestriction(DISALLOW_FACTORY_RESET, active);
182+
setUserRestriction(DISALLOW_ADD_USER, active);
183+
setUserRestriction(DISALLOW_MOUNT_PHYSICAL_MEDIA, active);
184+
setUserRestriction(DISALLOW_ADJUST_VOLUME, active);
185+
} else {
186+
restorePreviousConfiguration();
187+
}
173188

174189
// disable keyguard and status bar
175190
mDevicePolicyManager.setKeyguardDisabled(mAdminComponentName, active);
@@ -198,6 +213,33 @@ private void setDefaultKioskPolicies(boolean active) {
198213
editor.commit();
199214
}
200215

216+
@TargetApi(Build.VERSION_CODES.N)
217+
private void saveCurrentConfiguration() {
218+
if (BuildCompat.isAtLeastN()) {
219+
Bundle settingsBundle = mDevicePolicyManager.getUserRestrictions(mAdminComponentName);
220+
SharedPreferences.Editor editor = getSharedPreferences(KIOSK_PREFERENCE_FILE,
221+
MODE_PRIVATE).edit();
222+
223+
for (String userRestriction : KIOSK_USER_RESTRICTIONS) {
224+
boolean currentSettingValue = settingsBundle.getBoolean(userRestriction);
225+
editor.putBoolean(userRestriction, currentSettingValue);
226+
}
227+
editor.commit();
228+
}
229+
}
230+
231+
private void restorePreviousConfiguration() {
232+
if (BuildCompat.isAtLeastN()) {
233+
SharedPreferences sharedPreferences = getSharedPreferences(KIOSK_PREFERENCE_FILE,
234+
MODE_PRIVATE);
235+
236+
for (String userRestriction : KIOSK_USER_RESTRICTIONS) {
237+
boolean prevSettingValue = sharedPreferences.getBoolean(userRestriction, false);
238+
setUserRestriction(userRestriction, prevSettingValue);
239+
}
240+
}
241+
}
242+
201243
private class KioskAppsArrayAdapter extends ArrayAdapter<String> implements
202244
AdapterView.OnItemClickListener {
203245

0 commit comments

Comments
 (0)