mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 20:33:53 +01:00
Simplify AddAccount task
This commit is contained in:
parent
6b80bec3c9
commit
f2e8891db9
1 changed files with 19 additions and 56 deletions
|
|
@ -3,13 +3,8 @@ package fr.free.nrw.commons.auth;
|
|||
import android.accounts.Account;
|
||||
import android.accounts.AccountManager;
|
||||
import android.accounts.AccountManagerFuture;
|
||||
import android.accounts.AuthenticatorException;
|
||||
import android.accounts.OperationCanceledException;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import fr.free.nrw.commons.CommonsApplication;
|
||||
import fr.free.nrw.commons.theme.NavigationBaseActivity;
|
||||
import io.reactivex.Single;
|
||||
|
|
@ -37,50 +32,25 @@ public abstract class AuthenticatedActivity extends NavigationBaseActivity {
|
|||
cookie -> onAuthCookieAcquired(cookie),
|
||||
throwable -> onAuthFailure());
|
||||
}
|
||||
|
||||
private class AddAccountTask extends AsyncTask<Void, String, String> {
|
||||
private AccountManager accountManager;
|
||||
public AddAccountTask(AccountManager accountManager) {
|
||||
this.accountManager = accountManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(String result) {
|
||||
super.onPostExecute(result);
|
||||
if(result != null) {
|
||||
Account[] allAccounts = accountManager.getAccountsByType(accountType);
|
||||
Account curAccount = allAccounts[0];
|
||||
getAuthCookie(curAccount, accountManager);
|
||||
} else {
|
||||
onAuthFailure();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String doInBackground(Void... params) {
|
||||
AccountManagerFuture<Bundle> resultFuture = accountManager.addAccount(accountType, null, null, null, AuthenticatedActivity.this, null, null);
|
||||
Bundle result;
|
||||
try {
|
||||
result = resultFuture.getResult();
|
||||
} catch (OperationCanceledException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
} catch (AuthenticatorException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
if(result.containsKey(AccountManager.KEY_ACCOUNT_NAME)) {
|
||||
return result.getString(AccountManager.KEY_ACCOUNT_NAME);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
private void addAccount(AccountManager accountManager) {
|
||||
Single.just(accountManager.addAccount(accountType, null, null, null, AuthenticatedActivity.this, null, null))
|
||||
.subscribeOn(Schedulers.io())
|
||||
.map(AccountManagerFuture::getResult)
|
||||
.doOnEvent((bundle, throwable) -> {
|
||||
if (bundle.containsKey(AccountManager.KEY_ACCOUNT_NAME)) {
|
||||
throw new RuntimeException();
|
||||
}
|
||||
})
|
||||
.map(bundle -> bundle.getString(AccountManager.KEY_ACCOUNT_NAME))
|
||||
.doOnError(Timber::e)
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(s -> {
|
||||
Account[] allAccounts = accountManager.getAccountsByType(accountType);
|
||||
Account curAccount = allAccounts[0];
|
||||
getAuthCookie(curAccount, accountManager);
|
||||
},
|
||||
throwable -> onAuthFailure());
|
||||
}
|
||||
|
||||
protected void requestAuthToken() {
|
||||
|
|
@ -91,14 +61,7 @@ public abstract class AuthenticatedActivity extends NavigationBaseActivity {
|
|||
AccountManager accountManager = AccountManager.get(this);
|
||||
Account curAccount = app.getCurrentAccount();
|
||||
if(curAccount == null) {
|
||||
AddAccountTask addAccountTask = new AddAccountTask(accountManager);
|
||||
// This AsyncTask blocks until the Login Activity returns
|
||||
// And since in Android 4.x+ only one background thread runs all AsyncTasks
|
||||
// And since LoginActivity can't return until it's own AsyncTask (that does the login)
|
||||
// returns, we have a deadlock!
|
||||
// Fixed by explicitly asking this to be executed in parallel
|
||||
// See: https://groups.google.com/forum/?fromgroups=#!topic/android-developers/8M0RTFfO7-M
|
||||
addAccountTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||
addAccount(accountManager);
|
||||
} else {
|
||||
getAuthCookie(curAccount, accountManager);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue