Update adb backup/restore confirmation activity on new intent

Now, if adb backup/restore is currently active and the activity is
passed a new intent, the activity instance would be updated with the new intent.
Same for if the activity is relaunched.

Before, the activity would ignore intent changes and use old tokens and
views, leading to backing up/restoring wrong files.

Bug: 65723308
Test: adb backup -f old.ab -all; ctrl-c; adb backup -f new.ab -all;
gts-tradefed run commandAndExit gts-dev -m GtsBackupHostTestCases
Change-Id: Ifa11efecf82a682a578dab7e5795328ab7d0c054
This commit is contained in:
Annie Meng
2018-01-25 12:46:07 +00:00
parent c41d14828f
commit d38749b6ff
3 changed files with 67 additions and 23 deletions

View File

@@ -30,6 +30,7 @@
android:title=""
android:windowSoftInputMode="stateAlwaysHidden"
android:excludeFromRecents="true"
android:launchMode="singleTop"
android:exported="true" >
</activity>
</application>

View File

@@ -52,7 +52,9 @@ public class BackupRestoreConfirmation extends Activity {
static final String TAG = "BackupRestoreConfirmation";
static final boolean DEBUG = true;
static final String DID_ACKNOWLEDGE = "did_acknowledge";
static final String KEY_DID_ACKNOWLEDGE = "did_acknowledge";
static final String KEY_TOKEN = "token";
static final String KEY_ACTION = "action";
static final int MSG_START_BACKUP = 1;
static final int MSG_BACKUP_PACKAGE = 2;
@@ -69,6 +71,7 @@ public class BackupRestoreConfirmation extends Activity {
int mToken;
boolean mIsEncrypted;
boolean mDidAcknowledge;
String mAction;
TextView mStatusView;
TextView mCurPassword;
@@ -134,26 +137,9 @@ public class BackupRestoreConfirmation extends Activity {
super.onCreate(icicle);
final Intent intent = getIntent();
final String action = intent.getAction();
final int layoutId;
final int titleId;
if (action.equals(FullBackup.FULL_BACKUP_INTENT_ACTION)) {
layoutId = R.layout.confirm_backup;
titleId = R.string.backup_confirm_title;
} else if (action.equals(FullBackup.FULL_RESTORE_INTENT_ACTION)) {
layoutId = R.layout.confirm_restore;
titleId = R.string.restore_confirm_title;
} else {
Slog.w(TAG, "Backup/restore confirmation activity launched with invalid action!");
finish();
return;
}
mToken = intent.getIntExtra(FullBackup.CONF_TOKEN_INTENT_EXTRA, -1);
if (mToken < 0) {
Slog.e(TAG, "Backup/restore confirmation requested but no token passed!");
finish();
boolean tokenValid = setTokenOrFinish(intent, icicle);
if (!tokenValid) { // already called finish()
return;
}
@@ -169,6 +155,61 @@ public class BackupRestoreConfirmation extends Activity {
mObserver.setHandler(mHandler);
}
setViews(intent, icicle);
}
@Override
public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
boolean tokenValid = setTokenOrFinish(intent, null);
if (!tokenValid) { // already called finish()
return;
}
setViews(intent, null);
}
private boolean setTokenOrFinish(Intent intent, Bundle icicle) {
mToken = intent.getIntExtra(FullBackup.CONF_TOKEN_INTENT_EXTRA, -1);
// for relaunch, we try to use the last token before exit
if (icicle != null) {
mToken = icicle.getInt(KEY_TOKEN, mToken);
}
if (mToken < 0) {
Slog.e(TAG, "Backup/restore confirmation requested but no token passed!");
finish();
return false;
}
return true;
}
private void setViews(Intent intent, Bundle icicle) {
mAction = intent.getAction();
// for relaunch, we try to use the last action before exit
if (icicle != null) {
mAction = icicle.getString(KEY_ACTION, mAction);
}
final int layoutId;
final int titleId;
if (mAction.equals(FullBackup.FULL_BACKUP_INTENT_ACTION)) {
layoutId = R.layout.confirm_backup;
titleId = R.string.backup_confirm_title;
} else if (mAction.equals(FullBackup.FULL_RESTORE_INTENT_ACTION)) {
layoutId = R.layout.confirm_restore;
titleId = R.string.restore_confirm_title;
} else {
Slog.w(TAG, "Backup/restore confirmation activity launched with invalid action!");
finish();
return;
}
setTitle(titleId);
setContentView(layoutId);
@@ -202,7 +243,7 @@ public class BackupRestoreConfirmation extends Activity {
// if we're a relaunch we may need to adjust button enable state
if (icicle != null) {
mDidAcknowledge = icicle.getBoolean(DID_ACKNOWLEDGE, false);
mDidAcknowledge = icicle.getBoolean(KEY_DID_ACKNOWLEDGE, false);
mAllowButton.setEnabled(!mDidAcknowledge);
mDenyButton.setEnabled(!mDidAcknowledge);
}
@@ -249,7 +290,9 @@ public class BackupRestoreConfirmation extends Activity {
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putBoolean(DID_ACKNOWLEDGE, mDidAcknowledge);
outState.putBoolean(KEY_DID_ACKNOWLEDGE, mDidAcknowledge);
outState.putInt(KEY_TOKEN, mToken);
outState.putString(KEY_ACTION, mAction);
}
void sendAcknowledgement(int token, boolean allow, IFullBackupRestoreObserver observer) {

View File

@@ -2638,7 +2638,7 @@ public class BackupManagerService implements BackupManagerServiceInterface {
confIntent.setClassName("com.android.backupconfirm",
"com.android.backupconfirm.BackupRestoreConfirmation");
confIntent.putExtra(FullBackup.CONF_TOKEN_INTENT_EXTRA, token);
confIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
confIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
mContext.startActivityAsUser(confIntent, UserHandle.SYSTEM);
} catch (ActivityNotFoundException e) {
return false;