16
16
17
17
package com .afwsamples .testdpc .policy .locktask ;
18
18
19
+ import static android .app .admin .DevicePolicyManager .LOCK_TASK_FEATURE_GLOBAL_ACTIONS ;
20
+ import static android .app .admin .DevicePolicyManager .LOCK_TASK_FEATURE_HOME ;
21
+ import static android .app .admin .DevicePolicyManager .LOCK_TASK_FEATURE_KEYGUARD ;
22
+ import static android .app .admin .DevicePolicyManager .LOCK_TASK_FEATURE_NOTIFICATIONS ;
23
+ import static android .app .admin .DevicePolicyManager .LOCK_TASK_FEATURE_SYSTEM_INFO ;
24
+
19
25
import android .annotation .TargetApi ;
20
26
import android .app .admin .DevicePolicyManager ;
21
27
import android .content .ComponentName ;
28
34
import com .afwsamples .testdpc .DeviceAdminReceiver ;
29
35
import com .afwsamples .testdpc .R ;
30
36
import com .afwsamples .testdpc .common .BaseSearchablePolicyPreferenceFragment ;
37
+ import com .afwsamples .testdpc .common .ReflectionUtil ;
31
38
import com .afwsamples .testdpc .common .preference .DpcSwitchPreference ;
32
39
import java .util .Map ;
33
40
@@ -49,19 +56,31 @@ public class SetLockTaskFeaturesFragment
49
56
private static final String KEY_SYSTEM_INFO = "lock_task_feature_system_info" ;
50
57
private static final String KEY_NOTIFICATIONS = "lock_task_feature_notifications" ;
51
58
private static final String KEY_HOME = "lock_task_feature_home" ;
52
- private static final String KEY_RECENTS = "lock_task_feature_recents " ;
59
+ private static final String KEY_OVERVIEW = "lock_task_feature_overview " ;
53
60
private static final String KEY_GLOBAL_ACTIONS = "lock_task_feature_global_actions" ;
54
61
private static final String KEY_KEYGUARD = "lock_task_feature_keyguard" ;
55
62
63
+ private static final int LOCK_TASK_FEATURE_OVERVIEW ;
64
+ static {
65
+ int flag = 1 << 3 ;
66
+ try {
67
+ flag = ReflectionUtil .intConstant (
68
+ DevicePolicyManager .class , "LOCK_TASK_FEATURE_OVERVIEW" );
69
+ } catch (ReflectionUtil .ReflectionIsTemporaryException e ) {
70
+ } finally {
71
+ LOCK_TASK_FEATURE_OVERVIEW = flag ;
72
+ }
73
+ }
74
+
56
75
/** Maps from preference keys to {@link DevicePolicyManager#setLockTaskFeatures}'s flags. */
57
76
private static final ArrayMap <String , Integer > FEATURE_FLAGS = new ArrayMap <>();
58
77
static {
59
- FEATURE_FLAGS .put (KEY_SYSTEM_INFO , DevicePolicyManager . LOCK_TASK_FEATURE_SYSTEM_INFO );
60
- FEATURE_FLAGS .put (KEY_NOTIFICATIONS , DevicePolicyManager . LOCK_TASK_FEATURE_NOTIFICATIONS );
61
- FEATURE_FLAGS .put (KEY_HOME , DevicePolicyManager . LOCK_TASK_FEATURE_HOME );
62
- FEATURE_FLAGS .put (KEY_RECENTS , DevicePolicyManager . LOCK_TASK_FEATURE_RECENTS );
63
- FEATURE_FLAGS .put (KEY_GLOBAL_ACTIONS , DevicePolicyManager . LOCK_TASK_FEATURE_GLOBAL_ACTIONS );
64
- FEATURE_FLAGS .put (KEY_KEYGUARD , DevicePolicyManager . LOCK_TASK_FEATURE_KEYGUARD );
78
+ FEATURE_FLAGS .put (KEY_SYSTEM_INFO , LOCK_TASK_FEATURE_SYSTEM_INFO );
79
+ FEATURE_FLAGS .put (KEY_NOTIFICATIONS , LOCK_TASK_FEATURE_NOTIFICATIONS );
80
+ FEATURE_FLAGS .put (KEY_HOME , LOCK_TASK_FEATURE_HOME );
81
+ FEATURE_FLAGS .put (KEY_OVERVIEW , LOCK_TASK_FEATURE_OVERVIEW );
82
+ FEATURE_FLAGS .put (KEY_GLOBAL_ACTIONS , LOCK_TASK_FEATURE_GLOBAL_ACTIONS );
83
+ FEATURE_FLAGS .put (KEY_KEYGUARD , LOCK_TASK_FEATURE_KEYGUARD );
65
84
}
66
85
67
86
private DevicePolicyManager mDpm ;
@@ -94,6 +113,7 @@ public void onResume() {
94
113
DpcSwitchPreference pref = (DpcSwitchPreference ) findPreference (entry .getKey ());
95
114
pref .setChecked ((enabledFeatures & entry .getValue ()) != 0 );
96
115
}
116
+ enforceEnablingRestrictions (enabledFeatures );
97
117
}
98
118
99
119
@ Override
@@ -105,14 +125,19 @@ public boolean onPreferenceChange(Preference pref, Object val) {
105
125
}
106
126
107
127
final int flagsBefore = getLockTaskFeatures ();
108
- final int flagsAfter = (Boolean ) val
128
+ int flagsAfter = (Boolean ) val
109
129
? flagsBefore | FEATURE_FLAGS .get (key )
110
130
: flagsBefore & ~FEATURE_FLAGS .get (key );
131
+ if ((flagsAfter & LOCK_TASK_FEATURE_HOME ) == 0 ) {
132
+ // Disable OVERVIEW when HOME is disabled
133
+ flagsAfter &= ~LOCK_TASK_FEATURE_OVERVIEW ;
134
+ }
111
135
if (flagsAfter != flagsBefore ) {
112
136
Log .i (TAG , "LockTask feature flags changing from 0x" + Integer .toHexString (flagsBefore )
113
137
+ " to 0x" + Integer .toHexString (flagsAfter ));
114
138
try {
115
139
setLockTaskFeatures (flagsAfter );
140
+ enforceEnablingRestrictions (flagsAfter );
116
141
return true ;
117
142
} catch (SecurityException e ) {
118
143
Log .e (TAG , "setLockTaskFeatures() can only be called by DO and affiliated PO" );
@@ -129,6 +154,14 @@ public boolean isAvailable(Context context) {
129
154
return true ;
130
155
}
131
156
157
+ private void enforceEnablingRestrictions (int enabledFeatures ) {
158
+ DpcSwitchPreference pref = (DpcSwitchPreference ) findPreference (KEY_OVERVIEW );
159
+ pref .setEnabled ((enabledFeatures & LOCK_TASK_FEATURE_HOME ) != 0 );
160
+ if (!pref .isEnabled () && pref .isChecked ()) {
161
+ pref .setChecked (false );
162
+ }
163
+ }
164
+
132
165
@ TargetApi (28 )
133
166
private int getLockTaskFeatures () {
134
167
return mDpm .getLockTaskFeatures (mAdmin );
0 commit comments