Skip to content

Commit e11de3f

Browse files
committed
Wait for app to be in foreground before completing add account.
Test: Manual Fixes: 124286399 Change-Id: If11239474b16857f40a313b58853e048a836b962
1 parent e5b3977 commit e11de3f

File tree

2 files changed

+49
-20
lines changed

2 files changed

+49
-20
lines changed

app/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ private void stripTestOnlyForBuild(flavor, buildType) {
125125
}
126126

127127
dependencies {
128+
def lifecycle_version = "2.0.0"
129+
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
128130
implementation 'androidx.enterprise:enterprise-feedback:1.0.0-alpha01'
129131
implementation 'androidx.multidex:multidex:2.0.0'
130132
implementation 'androidx.legacy:legacy-preference-v14:1.0.0'
@@ -135,4 +137,6 @@ dependencies {
135137
implementation 'org.bouncycastle:bcpkix-jdk15on:1.56'
136138
implementation 'org.bouncycastle:bcprov-jdk15on:1.56'
137139
implementation 'com.google.guava:guava:23.6-android'
140+
141+
annotationProcessor "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"
138142
}

app/src/main/java/com/afwsamples/testdpc/AddAccountActivity.java

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.afwsamples.testdpc;
1818

1919
import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE;
20+
import static androidx.lifecycle.Lifecycle.State.STARTED;
2021

2122
import android.accounts.Account;
2223
import android.accounts.AccountManager;
@@ -30,13 +31,15 @@
3031
import android.content.Intent;
3132
import android.os.Build.VERSION_CODES;
3233
import android.os.Bundle;
34+
import android.os.Handler;
3335
import android.os.UserManager;
3436
import android.text.TextUtils;
3537
import android.util.Log;
3638
import android.view.View;
3739
import android.widget.EditText;
3840
import android.widget.RadioGroup;
3941
import android.widget.Toast;
42+
import androidx.lifecycle.ProcessLifecycleOwner;
4043
import com.afwsamples.testdpc.common.Util;
4144
import com.android.setupwizardlib.GlifLayout;
4245
import java.io.IOException;
@@ -50,6 +53,7 @@ public class AddAccountActivity extends Activity {
5053

5154
private static final String TAG = "AddAccountActivity";
5255
private static final String GOOGLE_ACCOUNT_TYPE = "com.google";
56+
private static final long WAIT_FOR_FOREGROUND_DELAY_MS = 10;
5357

5458
public static final String EXTRA_NEXT_ACTIVITY_INTENT = "nextActivityIntent";
5559

@@ -100,26 +104,47 @@ private void addAccount(String accountName) {
100104
}
101105

102106
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+
123148
}
124149

125150
private void disableUserRestrictions() {

0 commit comments

Comments
 (0)