16
16
17
17
package com .afwsamples .testdpc ;
18
18
19
- import android .app .admin .DevicePolicyManager ;
19
+ import android .app .AlarmManager ;
20
+ import android .app .PendingIntent ;
20
21
import android .content .BroadcastReceiver ;
21
22
import android .content .ComponentName ;
22
23
import android .content .Context ;
23
24
import android .content .Intent ;
24
25
import android .content .pm .PackageManager ;
26
+ import android .os .SystemClock ;
25
27
import android .util .Log ;
26
28
27
29
import com .afwsamples .testdpc .provision .CheckInState ;
@@ -40,12 +42,19 @@ public class FirstAccountReadyBroadcastReceiver extends BroadcastReceiver {
40
42
private static final String FIRST_ACCOUNT_READY_ACTION =
41
43
"com.google.android.work.action.FIRST_ACCOUNT_READY" ;
42
44
45
+ public static final String FIRST_ACCOUNT_READY_TIMEOUT_ACTION =
46
+ "com.afwsamples.testdpc.FIRST_ACCOUNT_READY_TIMEOUT" ;
47
+
43
48
public void onReceive (Context context , Intent intent ) {
44
- Log .d (TAG , "Received: " + intent .getAction ());
45
- if (FIRST_ACCOUNT_READY_ACTION .equals (intent .getAction ())) {
49
+ final String action = intent .getAction ();
50
+ Log .d (TAG , "Received: " + action );
51
+ if (FIRST_ACCOUNT_READY_ACTION .equals (action ) ||
52
+ FIRST_ACCOUNT_READY_TIMEOUT_ACTION .equals (action )) {
46
53
CheckInState checkInState = new CheckInState (context );
47
- checkInState .setFirstAccountReady ();
48
- ProvisioningUtil .enableProfile (context );
54
+ if (!checkInState .isFirstAccountReady ()) {
55
+ checkInState .setFirstAccountReady ();
56
+ ProvisioningUtil .enableProfile (context );
57
+ }
49
58
// This receiver is disabled in ProvisioningUtil.enableProfile, no more code should
50
59
// be put after it.
51
60
}
@@ -59,4 +68,26 @@ public static void setEnabled(Context context, boolean enabled) {
59
68
DONT_KILL_APP
60
69
);
61
70
}
71
+
72
+ /**
73
+ * Enable profile anyway if we cannot receive the broadcast after certain amount time.
74
+ */
75
+ public static void scheduleFirstAccountReadyTimeoutAlarm (Context context , long timeout ) {
76
+ AlarmManager alarmManager = (AlarmManager ) context .getSystemService (Context .ALARM_SERVICE );
77
+ alarmManager .set (AlarmManager .ELAPSED_REALTIME_WAKEUP ,
78
+ SystemClock .elapsedRealtime () + timeout ,
79
+ createFirstAccountReadyTimeoutPendingIntent (context ));
80
+ }
81
+
82
+ public static void cancelFirstAccountReadyTimeoutAlarm (Context context ) {
83
+ AlarmManager alarmManager = (AlarmManager ) context .getSystemService (Context .ALARM_SERVICE );
84
+ alarmManager .cancel (createFirstAccountReadyTimeoutPendingIntent (context ));
85
+ }
86
+
87
+ private static PendingIntent createFirstAccountReadyTimeoutPendingIntent (Context context ) {
88
+ Intent intent = new Intent (context , FirstAccountReadyBroadcastReceiver .class );
89
+ intent .setAction (FirstAccountReadyBroadcastReceiver .FIRST_ACCOUNT_READY_TIMEOUT_ACTION );
90
+ return PendingIntent .getBroadcast (
91
+ context , 0 , intent , PendingIntent .FLAG_UPDATE_CURRENT );
92
+ }
62
93
}
0 commit comments