17
17
package com .afwsamples .testdpc ;
18
18
19
19
import static android .app .admin .DevicePolicyManager .EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE ;
20
+ import static androidx .lifecycle .Lifecycle .State .STARTED ;
20
21
21
22
import android .accounts .Account ;
22
23
import android .accounts .AccountManager ;
30
31
import android .content .Intent ;
31
32
import android .os .Build .VERSION_CODES ;
32
33
import android .os .Bundle ;
34
+ import android .os .Handler ;
33
35
import android .os .UserManager ;
34
36
import android .text .TextUtils ;
35
37
import android .util .Log ;
36
38
import android .view .View ;
37
39
import android .widget .EditText ;
38
40
import android .widget .RadioGroup ;
39
41
import android .widget .Toast ;
42
+ import androidx .lifecycle .ProcessLifecycleOwner ;
40
43
import com .afwsamples .testdpc .common .Util ;
41
44
import com .android .setupwizardlib .GlifLayout ;
42
45
import java .io .IOException ;
@@ -50,6 +53,7 @@ public class AddAccountActivity extends Activity {
50
53
51
54
private static final String TAG = "AddAccountActivity" ;
52
55
private static final String GOOGLE_ACCOUNT_TYPE = "com.google" ;
56
+ private static final long WAIT_FOR_FOREGROUND_DELAY_MS = 10 ;
53
57
54
58
public static final String EXTRA_NEXT_ACTIVITY_INTENT = "nextActivityIntent" ;
55
59
@@ -100,26 +104,47 @@ private void addAccount(String accountName) {
100
104
}
101
105
102
106
accountManager .addAccount (GOOGLE_ACCOUNT_TYPE , null , null , bundle , this ,
103
- accountManagerFuture -> {
104
- try {
105
- Bundle result = accountManagerFuture .getResult ();
106
- String accountNameAdded = result .getString (AccountManager .KEY_ACCOUNT_NAME );
107
- Log .d (TAG , "addAccount - accountNameAdded: " + accountNameAdded );
108
- if (mNextActivityIntent != null ) {
109
- startActivity (mNextActivityIntent );
110
- }
111
- final Intent resultIntent = new Intent ();
112
- resultIntent .putExtra (EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE ,
113
- new Account (accountNameAdded , GOOGLE_ACCOUNT_TYPE ));
114
- setResult (RESULT_OK , resultIntent );
115
- finish ();
116
- } catch (OperationCanceledException | AuthenticatorException
117
- | IOException e ) {
118
- Log .e (TAG , "addAccount - failed" , e );
119
- Toast .makeText (AddAccountActivity .this ,
120
- R .string .fail_to_add_account , Toast .LENGTH_LONG ).show ();
121
- }
122
- }, null );
107
+ accountManagerFuture -> {
108
+ try {
109
+ Bundle result = accountManagerFuture .getResult ();
110
+ // This callback executes slightly before the app is back in the foreground
111
+ // so we need to wait.
112
+ waitForForeground (() -> accountCreated (result ), 1000 );
113
+ } catch (OperationCanceledException | AuthenticatorException | IOException e ) {
114
+ Log .e (TAG , "addAccount - failed" , e );
115
+ Toast .makeText (AddAccountActivity .this ,
116
+ R .string .fail_to_add_account , Toast .LENGTH_LONG ).show ();
117
+ }
118
+ }, null );
119
+ }
120
+
121
+ private void waitForForeground (Runnable r , long timeout ) {
122
+ if (timeout <= 0 ) {
123
+ throw new RuntimeException ("Timed out waiting for foreground." );
124
+ }
125
+ boolean isAppInForeground =
126
+ ProcessLifecycleOwner .get ().getLifecycle ().getCurrentState ().isAtLeast (STARTED );
127
+ if (isAppInForeground ) {
128
+ r .run ();
129
+ } else {
130
+ new Handler ().postDelayed (
131
+ () -> waitForForeground (r , timeout - WAIT_FOR_FOREGROUND_DELAY_MS ),
132
+ WAIT_FOR_FOREGROUND_DELAY_MS );
133
+ }
134
+ }
135
+
136
+ private void accountCreated (Bundle result ) {
137
+ String accountNameAdded = result .getString (AccountManager .KEY_ACCOUNT_NAME );
138
+ Log .d (TAG , "addAccount - accountNameAdded: " + accountNameAdded );
139
+ if (mNextActivityIntent != null ) {
140
+ startActivity (mNextActivityIntent );
141
+ }
142
+ final Intent resultIntent = new Intent ();
143
+ resultIntent .putExtra (EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE ,
144
+ new Account (accountNameAdded , GOOGLE_ACCOUNT_TYPE ));
145
+ setResult (RESULT_OK , resultIntent );
146
+ finish ();
147
+
123
148
}
124
149
125
150
private void disableUserRestrictions () {
0 commit comments