I realize I made quite a mess by opening two SO questions about essentially the same topic. So, it is a good time to consolidate the answers. I was searching for direct getter / setter methods in GDAA but found only the 'setter' - setAccountName()) - SO question 21583828 (actually did not, but Burcu helped me).
On the other side, 'getter' can be substituted by getting the account name from "onActivityResult()" - SO question 21501829
And yet another SO question - this one - on the same topic has been resolved as well.
So the conclusion is:
- get account from 'onActivityResult()'
- set account in 'setAccountName()'
- keep your current account email around, so you can detect a new one (should user decide to switch) and reset Google Account Client if necessary.
UPDATE 2014-11-04:
Here is a wrapper that I use to persist and manage the Google accounts within my app.
import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import com.google.android.gms.auth.GoogleAuthUtil;
public class GooAccMgr {
private static final String ACC_NAME = "account_name";
public static final int FAIL = -1;
public static final int UNCHANGED = 0;
public static final int CHANGED = +1;
private String mCurrEmail = null; // cache locally
public Account[] getAllAccnts(Context ctx) {
return AccountManager.get(acx(ctx)).getAccountsByType(GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE);
}
public Account getPrimaryAccnt(Context ctx) {
Account[] accts = getAllAccnts(ctx);
return accts == null || accts.length == 0 ? null : accts[0];
}
public Account getActiveAccnt(Context ctx) {
return email2Accnt(ctx, getActiveEmail(ctx));
}
public String getActiveEmail(Context ctx) {
if (mCurrEmail != null) {
return mCurrEmail;
}
mCurrEmail = ctx == null ? null : pfs(ctx).getString(ACC_NAME, null);
return mCurrEmail;
}
public Account email2Accnt(Context ctx, String emil) {
if (emil != null) {
Account[] accounts =
AccountManager.get(acx(ctx)).getAccountsByType(GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE);
for (Account account : accounts) {
if (emil.equalsIgnoreCase(account.name)) {
return account;
}
}
}
return null;
}
/**
* Stores a new email in persistent app storage, reporting result
* @param ctx activity context
* @param newEmail new email, optionally null
* @return FAIL, CHANGED or UNCHANGED (based on the following table)
* OLD NEW SAVED RESULT
* ERROR FAIL
* null null null FAIL
* null new new CHANGED
* old null old UNCHANGED
* old != new new CHANGED
* old == new new UNCHANGED
*/
public int setEmail(Context ctx, String newEmail) {
int result = FAIL; // 0 0
String prevEmail = getActiveEmail(ctx);
if ((prevEmail == null) && (newEmail != null)) {
result = CHANGED;
} else if ((prevEmail != null) && (newEmail == null)) {
result = UNCHANGED;
} else if ((prevEmail != null) && (newEmail != null)) {
result = prevEmail.equalsIgnoreCase(newEmail) ? UNCHANGED : CHANGED;
}
if (result == CHANGED) {
mCurrEmail = newEmail;
pfs(ctx).edit().putString(ACC_NAME, newEmail).apply();
}
return result;
}
private Context acx(Context ctx) {
return ctx == null ? null : ctx.getApplicationContext();
}
private SharedPreferences pfs(Context ctx) {
return ctx == null ? null : PreferenceManager.getDefaultSharedPreferences(acx(ctx));
}
}
Hat-tip to Alex Lockwood for initial inspiration. Unfortunately, I can't find reference to his original code.