38
38
import android .widget .ListView ;
39
39
import android .widget .RadioGroup ;
40
40
import android .widget .Toast ;
41
+
41
42
import com .afwsamples .testdpc .DeviceAdminReceiver ;
42
43
import com .afwsamples .testdpc .R ;
43
- import com .afwsamples .testdpc .common .ReflectionUtil ;
44
- import java .lang .reflect .InvocationTargetException ;
44
+
45
45
import java .time .LocalDate ;
46
46
import java .time .format .DateTimeFormatter ;
47
47
import java .util .ArrayList ;
@@ -251,7 +251,7 @@ private void promptToSetFreezePeriod(FreezePeriodPickResult callback, final Loca
251
251
});
252
252
}
253
253
254
- @ TargetApi (Build . VERSION_CODES . P )
254
+ @ TargetApi (28 )
255
255
private boolean setSystemUpdatePolicy () {
256
256
SystemUpdatePolicy newPolicy ;
257
257
switch (mSystemUpdatePolicySelection .getCheckedRadioButtonId ()) {
@@ -269,33 +269,21 @@ private boolean setSystemUpdatePolicy() {
269
269
default :
270
270
newPolicy = null ;
271
271
}
272
- try {
273
- if (BuildCompat .isAtLeastP () && newPolicy != null && mFreezePeriods .size () != 0 ) {
274
- List <Pair <Integer , Integer >> periods = new ArrayList <>(mFreezePeriods .size ());
275
- for (Period p : mFreezePeriods ) {
276
- periods .add (p .toIntegers ());
277
- }
278
- ReflectionUtil .invoke (newPolicy , "setFreezePeriods" ,
279
- new Class []{List .class }, periods );
272
+ if (BuildCompat .isAtLeastP () && newPolicy != null && mFreezePeriods .size () != 0 ) {
273
+ List <Pair <Integer , Integer >> periods = new ArrayList <>(mFreezePeriods .size ());
274
+ for (Period p : mFreezePeriods ) {
275
+ periods .add (p .toIntegers ());
280
276
}
281
- mDpm .setSystemUpdatePolicy (DeviceAdminReceiver .getComponentName (getActivity ()),
282
- newPolicy );
283
- Toast .makeText (getContext (), "Policy set successfully" , Toast .LENGTH_SHORT ).show ();
284
- return true ;
285
- } catch (ReflectionUtil .ReflectionIsTemporaryException e ) {
286
- Throwable cause = e .getCause ();
287
- if (cause instanceof InvocationTargetException ) {
288
- if (cause .getCause ().getClass ().getSimpleName ().equals (
289
- "ValidationFailedException" )) {
290
- cause = cause .getCause ();
291
- }
277
+ try {
278
+ newPolicy .setFreezePeriods (periods );
279
+ mDpm .setSystemUpdatePolicy (DeviceAdminReceiver .getComponentName (getActivity ()),
280
+ newPolicy );
281
+ Toast .makeText (getContext (), "Policy set successfully" , Toast .LENGTH_LONG ).show ();
282
+ return true ;
283
+ } catch (IllegalArgumentException e ) {
284
+ Toast .makeText (getContext (), "Failed to set system update policy: " + e .getMessage (),
285
+ Toast .LENGTH_LONG ).show ();
292
286
}
293
- cause .printStackTrace ();
294
- Toast .makeText (getContext (), "Fail to set policy: " + cause .getMessage (),
295
- Toast .LENGTH_LONG ).show ();
296
- } catch (IllegalArgumentException e ) {
297
- Toast .makeText (getContext (), "setSystemUpdatePolicy fails: " + e .getMessage (),
298
- Toast .LENGTH_LONG ).show ();
299
287
}
300
288
return false ;
301
289
}
@@ -309,7 +297,7 @@ private void updateMaintenanceWindowDisplay() {
309
297
mSetMaintenanceWindowEnd .setText (formatMinutes (mMaintenanceEnd ));
310
298
}
311
299
312
- @ TargetApi (Build . VERSION_CODES . P )
300
+ @ TargetApi (28 )
313
301
private void reloadSystemUpdatePolicy () {
314
302
SystemUpdatePolicy policy = mDpm .getSystemUpdatePolicy ();
315
303
String policyDescription = "Unknown" ;
@@ -344,18 +332,13 @@ private void reloadSystemUpdatePolicy() {
344
332
break ;
345
333
}
346
334
if (BuildCompat .isAtLeastP ()) {
347
- try {
348
- List <Pair <Integer , Integer >> freezePeriods = (List <Pair <Integer , Integer >>)
349
- ReflectionUtil .invoke (policy , "getFreezePeriods" );
350
- mFreezePeriods .clear ();
351
- for (Pair <Integer , Integer > period : freezePeriods ) {
352
- Period p = new Period (period .first , period .second );
353
- mFreezePeriods .add (p );
354
- }
355
- mFreezePeriodAdapter .notifyDataSetChanged ();
356
- } catch (ReflectionUtil .ReflectionIsTemporaryException e ) {
357
- e .printStackTrace ();
335
+ List <Pair <Integer , Integer >> freezePeriods = policy .getFreezePeriods ();
336
+ mFreezePeriods .clear ();
337
+ for (Pair <Integer , Integer > period : freezePeriods ) {
338
+ Period p = new Period (period .first , period .second );
339
+ mFreezePeriods .add (p );
358
340
}
341
+ mFreezePeriodAdapter .notifyDataSetChanged ();
359
342
mFreezePeriodPanel .setVisibility (View .VISIBLE );
360
343
}
361
344
}
0 commit comments