BaseDemoActivity.java を利用すると最初に使用アカウントを選択した後、アカウントを変更することができません。
そこで、サインアウトするコードを追加しました。
BaseDemoActivity.java の抜粋(灰色部分が変更箇所)
public abstract class BaseDemoActivity extends Activity {
/**
* Google sign-in client
*/
private GoogleSignInClient mGoogleSignInClient;
/**
* Google sign-in account
*/
private GoogleSignInAccount mGoogleSignInAccount;
/**
* Starts the sign-in process and initializes the Drive client.
*/
protected void signIn() {
Set requiredScopes = new HashSet<>(2);
requiredScopes.add(Drive.SCOPE_FILE);
requiredScopes.add(Drive.SCOPE_APPFOLDER);
GoogleSignInAccount signInAccount = GoogleSignIn.getLastSignedInAccount(this);
GoogleSignInOptions signInOptions =
new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestScopes(Drive.SCOPE_FILE)
.requestScopes(Drive.SCOPE_APPFOLDER)
.build();
mGoogleSignInClient = GoogleSignIn.getClient(this, signInOptions);
if (signInAccount != null &&
signInAccount.getGrantedScopes().containsAll(requiredScopes)) {
initializeDriveClient(signInAccount);
} else {
startActivityForResult(mGoogleSignInClient.getSignInIntent(),
REQUEST_CODE_SIGN_IN);
}
}
/**
* Account Signout
*/
protected void signOut() {
if (mGoogleSignInClient != null) {
mGoogleSignInClient.signOut();
}
}
/**
* Continues the sign-in process, initializing the Drive clients with the current
* user's account.
*/
private void initializeDriveClient(GoogleSignInAccount signInAccount) {
mGoogleSignInAccount = signInAccount;
mDriveClient = Drive.getDriveClient(getApplicationContext(), signInAccount);
mDriveResourceClient = Drive.getDriveResourceClient(getApplicationContext(), signInAccount);
onDriveClientReady();
}
/**
* Get google sign-in account
*/
protected GoogleSignInAccount getGoogleSignInAccount() {
return mGoogleSignInAccount;
}
}
BaseDemoActivity から派生した Activity でのサインアウト処理です。
signOut(); finish();
サインアウトした後は、Googleドライブが利用できませんので、finish() します。
これで、再び Activity を呼び出したときにアカウント選択できるようになります。