35
35
import android .content .pm .PackageManager ;
36
36
import android .os .Build ;
37
37
import android .os .Bundle ;
38
+ import android .support .v4 .os .BuildCompat ;
39
+ import android .util .ArrayMap ;
38
40
import android .util .Log ;
39
41
import android .view .LayoutInflater ;
40
42
import android .view .View ;
@@ -75,6 +77,13 @@ public class KioskModeActivity extends Activity {
75
77
public static final String LOCKED_APP_PACKAGE_LIST
76
78
= "com.afwsamples.testdpc.policy.locktask.LOCKED_APP_PACKAGE_LIST" ;
77
79
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
+
78
87
private ComponentName mAdminComponentName ;
79
88
private ArrayList <String > mKioskPackages ;
80
89
private DevicePolicyManager mDevicePolicyManager ;
@@ -98,6 +107,7 @@ protected void onCreate(Bundle savedInstanceState) {
98
107
}
99
108
mKioskPackages .remove (getPackageName ());
100
109
mKioskPackages .add (getPackageName ());
110
+
101
111
setDefaultKioskPolicies (true );
102
112
} else {
103
113
// after a reboot there is no need to set the policies again
@@ -164,12 +174,17 @@ private void setUserRestriction(String restriction, boolean disallow) {
164
174
}
165
175
166
176
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
+ }
173
188
174
189
// disable keyguard and status bar
175
190
mDevicePolicyManager .setKeyguardDisabled (mAdminComponentName , active );
@@ -198,6 +213,33 @@ private void setDefaultKioskPolicies(boolean active) {
198
213
editor .commit ();
199
214
}
200
215
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
+
201
243
private class KioskAppsArrayAdapter extends ArrayAdapter <String > implements
202
244
AdapterView .OnItemClickListener {
203
245
0 commit comments