Permalink
Browse files

LoginActivity: Do the account addition in an AsyncTask.

Because StrictMode complains about this at least on API 15 and
API 16 targets.
  • Loading branch information...
murraycu committed Nov 21, 2014
1 parent 6a18ce0 commit 426918f8c43e5aab0c9946c6fa7e8eebe2f5b531
@@ -26,6 +26,7 @@
import android.animation.AnimatorListenerAdapter;
import android.annotation.TargetApi;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Build;
@@ -46,6 +47,8 @@

import com.murrayc.galaxyzoo.app.provider.client.ZooniverseClient;

import java.lang.ref.WeakReference;

//TODO: Use the toolbar, but we cannot derive from ActionBarActivity from AppCompat.
//Android's standard AccountAuthenticatorActivity doesn't let us use the toolbar,
//because it doesn't deried from ActionBarActivity,
@@ -274,44 +277,62 @@ public void onAnimationEnd(Animator animation) {
}
}

private void finishWithResult(final LoginUtils.LoginResult result) {
boolean loggedIn = false;
if ((result != null) && result.getSuccess()) {
loggedIn = true;
}
private static class AccountSaveTask extends AsyncTask<Void, Void, Void> {

if(loggedIn) {
UiUtils.showLoggedInToast(this);
private final WeakReference<Context> contextReference;
private final LoginUtils.LoginResult loginResult;
private final String existingAccountName;

private final boolean existingAccountIsAnonymous;

AccountSaveTask(final Context context, final LoginUtils.LoginResult loginResult, final String existingAccountName, boolean existingAccountIsAnonymous) {
this.contextReference = new WeakReference<>(context);
this.loginResult = loginResult;
this.existingAccountName = existingAccountName;
this.existingAccountIsAnonymous = existingAccountIsAnonymous;
}

if (loggedIn) {
final AccountManager accountManager = AccountManager.get(this);
@Override
protected Void doInBackground(Void... params) {

if (contextReference == null) {
return null;
}

final Context context = contextReference.get();
if (context == null) {
return null;
}

final String accountName = loginResult.getName();

final AccountManager accountManager = AccountManager.get(context);

boolean addingAccount = false;
if (mExistingAccountIsAnonymous) {
if (existingAccountIsAnonymous) {
//Remove the existing account so we can add the new one.
//TODO: Find a way to just change the name,
//though we don't lose any ItemsContentProvider data when we delete an Account.
LoginUtils.removeAnonymousAccount(this);
LoginUtils.removeAnonymousAccount(context);
addingAccount = true;
} else if(!TextUtils.equals(mExistingAccountName, result.getName())) {
} else if(!TextUtils.equals(existingAccountName, accountName)) {
//Remove any existing account so we can add the new one.
//TODO: Find a way to just change the name,
if (!TextUtils.isEmpty(mExistingAccountName)) {
LoginUtils.removeAccount(this, mExistingAccountName);
if (!TextUtils.isEmpty(existingAccountName)) {
LoginUtils.removeAccount(context, existingAccountName);
}

addingAccount = true;
}

//TODO: Get the existing Account instance?
final Account account = new Account(result.getName(), LoginUtils.ACCOUNT_TYPE);

final Account account = new Account(accountName, LoginUtils.ACCOUNT_TYPE);
if (addingAccount) {
accountManager.addAccountExplicitly(account, null, null);
Utils.copyPrefsToAccount(this, accountManager, account);
Utils.copyPrefsToAccount(context, accountManager, account);

//Tell the SyncAdapter to sync whenever the network is reconnected:
LoginUtils.setAutomaticAccountSync(this, account);
LoginUtils.setAutomaticAccountSync(context, account);
}

//TODO? ContentResolver.setSyncAutomatically(account, ContactsContract.AUTHORITY, true)
@@ -320,11 +341,30 @@ private void finishWithResult(final LoginUtils.LoginResult result) {
//if this activity was launched from our Authenticator, for instance if our
//Authenticator found that the accounts' existing auth token was invalid.
//Presumably it is necessary if this activity is launched from our app.
accountManager.setAuthToken(account, LoginUtils.ACCOUNT_AUTHTOKEN_TYPE, result.getApiKey());
accountManager.setAuthToken(account, LoginUtils.ACCOUNT_AUTHTOKEN_TYPE, loginResult.getApiKey());

return null;
}
}

private void finishWithResult(final LoginUtils.LoginResult result) {
boolean loggedIn = false;
if ((result != null) && result.getSuccess()) {
loggedIn = true;
}

if(loggedIn) {
UiUtils.showLoggedInToast(this);
}


final Intent intent = new Intent();

if (loggedIn) {
final AccountSaveTask task = new AccountSaveTask(this, result, mExistingAccountName, mExistingAccountIsAnonymous);
task.execute();

//Set the accountName in the intent result:
intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, result.getName());
intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, LoginUtils.ACCOUNT_TYPE);
}
@@ -207,10 +207,18 @@ static void setAutomaticAccountSync(final Context context, final Account account
ContentResolver.setSyncAutomatically(account, Item.AUTHORITY, true);
}

/** Don't call this from the main UI thread.
*
* @param context
*/
public static void removeAnonymousAccount(final Context context) {
removeAccount(context, ACCOUNT_NAME_ANONYMOUS);
}

/** Don't call this from the main UI thread.
*
* @param context
*/
public static void removeAccount(final Context context, final String accountName) {
final AccountManager accountManager = AccountManager.get(context);
final Account account = new Account(accountName, LoginUtils.ACCOUNT_TYPE);

0 comments on commit 426918f

Please sign in to comment.