18
18
19
19
import android .annotation .TargetApi ;
20
20
import android .app .Activity ;
21
- import android .app .AlertDialog ;
21
+ import android .app .AlertDialog . Builder ;
22
22
import android .app .admin .DevicePolicyManager ;
23
23
import android .content .ComponentName ;
24
24
import android .content .Context ;
25
- import android .content .DialogInterface ;
25
+ import android .content .pm .PackageManager ;
26
+ import android .os .Build ;
26
27
import android .os .Bundle ;
27
28
import android .support .v7 .preference .Preference ;
28
29
import android .util .ArraySet ;
29
- import android .util .Log ;
30
30
import android .view .View ;
31
31
import android .widget .EditText ;
32
32
import android .widget .Toast ;
33
-
34
33
import com .afwsamples .testdpc .DeviceAdminReceiver ;
35
34
import com .afwsamples .testdpc .R ;
36
- import com .afwsamples .testdpc .common .AppInfoArrayAdapter ;
37
35
import com .afwsamples .testdpc .common .BaseSearchablePolicyPreferenceFragment ;
38
- import com .afwsamples .testdpc .common .ReflectionUtil ;
39
- import com .afwsamples .testdpc .common .ReflectionUtil . ReflectionIsTemporaryException ;
40
-
41
- import java .util .ArrayList ;
36
+ import com .afwsamples .testdpc .common .preference . DpcPreference ;
37
+ import com .afwsamples .testdpc .common .preference . DpcSwitchPreference ;
38
+ import java . util . Arrays ;
39
+ import java .util .Collections ;
42
40
import java .util .Set ;
43
41
44
- /**
45
- * TODO: Cleanup reflection usages once SDK is updated. b/120765156.
46
- */
47
- @ TargetApi (29 )
42
+ @ TargetApi (Build .VERSION_CODES .Q )
48
43
public class CrossProfileCalendarFragment extends BaseSearchablePolicyPreferenceFragment implements
49
- Preference .OnPreferenceClickListener {
44
+ Preference .OnPreferenceClickListener , Preference . OnPreferenceChangeListener {
50
45
51
- private static String LOG_TAG = "CrossProfileCalendarFragment" ;
52
-
53
- private static final String CROSS_PROFILE_CALENDAR_ADD_PACKAGE_KEY =
54
- "cross_profile_calendar_add_package" ;
55
- private static final String CROSS_PROFILE_CALENDAR_DELETE_PACKAGE_KEY =
56
- "cross_profile_calendar_delete_package" ;
57
- private static final String CROSS_PROFILE_CALENDAR_LIST_PACKAGE_KEY =
58
- "cross_profile_calendar_list_package" ;
46
+ private static final String CROSS_PROFILE_CALENDAR_SET_ALLOWED_PACKAGES_KEY =
47
+ "cross_profile_calendar_set_allowed_packages" ;
48
+ private static final String CROSS_PROFILE_CALENDAR_ALLOW_ALL_PACKAGES_KEY =
49
+ "cross_profile_calendar_allow_all_packages" ;
59
50
60
51
private DevicePolicyManager mDevicePolicyManager ;
52
+
61
53
private ComponentName mAdminComponentName ;
54
+ private DpcPreference mSetAllowedPackagesPreference ;
55
+ private DpcSwitchPreference mAllowAllPackagesPreference ;
62
56
63
57
@ Override
64
58
public void onCreate (Bundle savedInstanceState ) {
65
- mDevicePolicyManager = (DevicePolicyManager ) getActivity ().getSystemService (
66
- Context .DEVICE_POLICY_SERVICE );
59
+ mDevicePolicyManager = getActivity ().getSystemService (DevicePolicyManager .class );
67
60
mAdminComponentName = DeviceAdminReceiver .getComponentName (getActivity ());
68
61
getActivity ().getActionBar ().setTitle (R .string .cross_profile_calendar );
69
62
super .onCreate (savedInstanceState );
@@ -73,10 +66,15 @@ public void onCreate(Bundle savedInstanceState) {
73
66
public void onCreatePreferences (Bundle savedInstanceState , String rootKey ) {
74
67
addPreferencesFromResource (R .xml .cross_profile_calendar_preferences );
75
68
76
- findPreference (CROSS_PROFILE_CALENDAR_ADD_PACKAGE_KEY ).setOnPreferenceClickListener (this );
77
- findPreference (CROSS_PROFILE_CALENDAR_DELETE_PACKAGE_KEY )
78
- .setOnPreferenceClickListener (this );
79
- findPreference (CROSS_PROFILE_CALENDAR_LIST_PACKAGE_KEY ).setOnPreferenceClickListener (this );
69
+ mSetAllowedPackagesPreference = (DpcPreference ) findPreference (
70
+ CROSS_PROFILE_CALENDAR_SET_ALLOWED_PACKAGES_KEY );
71
+ mSetAllowedPackagesPreference .setOnPreferenceClickListener (this );
72
+
73
+ mAllowAllPackagesPreference = (DpcSwitchPreference ) findPreference (
74
+ CROSS_PROFILE_CALENDAR_ALLOW_ALL_PACKAGES_KEY );
75
+ mAllowAllPackagesPreference .setOnPreferenceChangeListener (this );
76
+
77
+ reloadAllowAllPackagesUi ();
80
78
}
81
79
82
80
@ Override
@@ -88,136 +86,62 @@ public boolean isAvailable(Context context) {
88
86
public boolean onPreferenceClick (Preference preference ) {
89
87
String key = preference .getKey ();
90
88
switch (key ) {
91
- case CROSS_PROFILE_CALENDAR_ADD_PACKAGE_KEY :
92
- showAddPackageDialog ();
93
- return true ;
94
- case CROSS_PROFILE_CALENDAR_DELETE_PACKAGE_KEY :
95
- showDeletePackageDialog ();
96
- return true ;
97
- case CROSS_PROFILE_CALENDAR_LIST_PACKAGE_KEY :
98
- showListPackageDialog ();
89
+ case CROSS_PROFILE_CALENDAR_SET_ALLOWED_PACKAGES_KEY :
90
+ showSetPackagesDialog ();
99
91
return true ;
100
92
}
101
93
return false ;
102
94
}
103
95
104
- /**
105
- * Shows a dialog that asks the user for a package name to be whitelisted.
106
- */
107
- private void showAddPackageDialog () {
108
- if (getActivity () == null || getActivity ().isFinishing ()) {
109
- return ;
96
+ @ Override
97
+ public boolean onPreferenceChange (Preference preference , Object newValue ) {
98
+ String key = preference .getKey ();
99
+ switch (key ) {
100
+ case CROSS_PROFILE_CALENDAR_ALLOW_ALL_PACKAGES_KEY :
101
+ mDevicePolicyManager .setCrossProfileCalendarPackages (
102
+ mAdminComponentName , newValue .equals (true ) ? null : Collections .emptySet ());
103
+ reloadAllowAllPackagesUi ();
110
104
}
105
+ return false ;
106
+ }
111
107
112
- final View dialogView = getActivity ().getLayoutInflater ().inflate (
113
- R .layout .simple_edittext , null );
114
- final EditText addPackageEditText = (EditText ) dialogView .findViewById (
115
- R .id .input );
116
-
117
- new AlertDialog .Builder (getActivity ())
118
- .setTitle (R .string .cross_profile_calendar_add_package )
119
- .setView (dialogView )
120
- .setPositiveButton (android .R .string .ok , (dialogInterface , i ) -> {
121
- final String packageName = addPackageEditText .getText ().toString ();
122
- if (packageName .isEmpty ()) {
123
- showToast (R .string .cross_profile_calendar_no_package );
124
- return ;
125
- }
126
- try {
127
- ReflectionUtil .invoke (mDevicePolicyManager , "addCrossProfileCalendarPackage" ,
128
- mAdminComponentName , packageName );
129
- showToast (String .format ("Successfully whitelisted package %s for cross profile "
130
- + "calendar" , packageName ));
131
- } catch (ReflectionIsTemporaryException e ) {
132
- Log .e (LOG_TAG , "Failed to invoke addCrossProfileCalendarPackage" , e );
133
- }
134
- })
135
- .setNegativeButton (android .R .string .cancel , null )
136
- .show ();
108
+ @ TargetApi (Build .VERSION_CODES .Q )
109
+ private void reloadAllowAllPackagesUi () {
110
+ final Set <String > packages =
111
+ mDevicePolicyManager .getCrossProfileCalendarPackages (mAdminComponentName );
112
+ mAllowAllPackagesPreference .setChecked (packages == null );
113
+ mSetAllowedPackagesPreference .setEnabled (!mAllowAllPackagesPreference .isChecked ());
137
114
}
138
115
139
116
/**
140
- * Shows a dialog that asks the user for a package name to be removed from the whitelist .
117
+ * Shows a dialog that asks the user for a set of package names to be allowed .
141
118
*/
142
- private void showDeletePackageDialog () {
119
+ private void showSetPackagesDialog () {
143
120
if (getActivity () == null || getActivity ().isFinishing ()) {
144
121
return ;
145
122
}
146
123
147
124
final View dialogView = getActivity ().getLayoutInflater ().inflate (
148
125
R .layout .simple_edittext , null );
149
- final EditText deletePackageEditText = (EditText ) dialogView .findViewById (
126
+ final EditText setPackagesEditText = (EditText ) dialogView .findViewById (
150
127
R .id .input );
151
128
152
- new AlertDialog .Builder (getActivity ())
153
- .setTitle (R .string .cross_profile_calendar_delete_package )
129
+ setPackagesEditText .setText (String .join ("," ,
130
+ mDevicePolicyManager .getCrossProfileCalendarPackages (mAdminComponentName )));
131
+
132
+ new Builder (getActivity ())
133
+ .setTitle (R .string .cross_profile_calendar_set_allowed_packages_title )
154
134
.setView (dialogView )
155
135
.setPositiveButton (android .R .string .ok , (dialogInterface , i ) -> {
156
- final String packageName = deletePackageEditText .getText ().toString ();
157
- if (packageName .isEmpty ()) {
158
- showToast (R .string .cross_profile_calendar_no_package );
159
- return ;
160
- }
161
- try {
162
- boolean succeed = (Boolean ) ReflectionUtil .invoke (mDevicePolicyManager ,
163
- "removeCrossProfileCalendarPackage" , mAdminComponentName , packageName );
164
- if (succeed ) {
165
- showToast (String .format ("Successfully removed package %s for cross profile "
166
- + "calendar" , packageName ));
167
- } else {
168
- showToast (String .format ("Failed to remove package %s for cross profile "
169
- + "calendar" , packageName ));
170
- }
171
- } catch (ReflectionIsTemporaryException e ) {
172
- Log .e (LOG_TAG , "Failed to invoke removeCrossProfileCalendarPackage" , e );
173
- }
136
+ final String packageNamesString = setPackagesEditText .getText ().toString ();
137
+ final Set <String > packageNames = packageNamesString .isEmpty ()
138
+ ? Collections .emptySet ()
139
+ : new ArraySet <>(Arrays .asList (
140
+ packageNamesString .replace (" " , "" ).split ("," )));
141
+ mDevicePolicyManager .setCrossProfileCalendarPackages (
142
+ mAdminComponentName , packageNames );
174
143
})
175
144
.setNegativeButton (android .R .string .cancel , null )
176
145
.show ();
177
146
}
178
-
179
- /**
180
- * Shows a dialog that displays all the packages that have been whitelisted.
181
- */
182
- private void showListPackageDialog () {
183
- Set <String > packages = new ArraySet <String >();
184
- try {
185
- packages = (Set <String >) ReflectionUtil .invoke (mDevicePolicyManager ,
186
- "getCrossProfileCalendarPackages" , mAdminComponentName );
187
- } catch (ReflectionIsTemporaryException e ) {
188
- Log .e (LOG_TAG , "Failed to invoke getCrossProfileCalendarPackages" , e );
189
- }
190
-
191
- if (packages .isEmpty ()) {
192
- showToast (R .string .cross_profile_calendar_list_package_empty );
193
- } else {
194
- AppInfoArrayAdapter appInfoArrayAdapter = new AppInfoArrayAdapter (getActivity (),
195
- R .id .pkg_name , new ArrayList <String >(packages ), true );
196
- new AlertDialog .Builder (getActivity ())
197
- .setTitle (getString (R .string .cross_profile_calendar_list_package_title ))
198
- .setAdapter (appInfoArrayAdapter , new DialogInterface .OnClickListener () {
199
- @ Override
200
- public void onClick (DialogInterface dialog , int position ) {
201
- // Do nothing.
202
- }
203
- })
204
- .show ();
205
- }
206
- }
207
-
208
- private void showToast (int msgId , Object ... args ) {
209
- showToast (getString (msgId , args ), Toast .LENGTH_SHORT );
210
- }
211
-
212
- private void showToast (String msg ) {
213
- showToast (msg , Toast .LENGTH_SHORT );
214
- }
215
-
216
- private void showToast (String msg , int duration ) {
217
- Activity activity = getActivity ();
218
- if (activity == null || activity .isFinishing ()) {
219
- return ;
220
- }
221
- Toast .makeText (activity , msg , duration ).show ();
222
- }
223
147
}
0 commit comments