mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-27 12:53:55 +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.Account;
|
||||||
import android.accounts.AccountManager;
|
import android.accounts.AccountManager;
|
||||||
import android.accounts.AccountManagerFuture;
|
import android.accounts.AccountManagerFuture;
|
||||||
import android.accounts.AuthenticatorException;
|
|
||||||
import android.accounts.OperationCanceledException;
|
|
||||||
import android.os.AsyncTask;
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
import fr.free.nrw.commons.CommonsApplication;
|
import fr.free.nrw.commons.CommonsApplication;
|
||||||
import fr.free.nrw.commons.theme.NavigationBaseActivity;
|
import fr.free.nrw.commons.theme.NavigationBaseActivity;
|
||||||
import io.reactivex.Single;
|
import io.reactivex.Single;
|
||||||
|
|
@ -38,49 +33,24 @@ public abstract class AuthenticatedActivity extends NavigationBaseActivity {
|
||||||
throwable -> onAuthFailure());
|
throwable -> onAuthFailure());
|
||||||
}
|
}
|
||||||
|
|
||||||
private class AddAccountTask extends AsyncTask<Void, String, String> {
|
private void addAccount(AccountManager accountManager) {
|
||||||
private AccountManager accountManager;
|
Single.just(accountManager.addAccount(accountType, null, null, null, AuthenticatedActivity.this, null, null))
|
||||||
public AddAccountTask(AccountManager accountManager) {
|
.subscribeOn(Schedulers.io())
|
||||||
this.accountManager = accountManager;
|
.map(AccountManagerFuture::getResult)
|
||||||
|
.doOnEvent((bundle, throwable) -> {
|
||||||
|
if (bundle.containsKey(AccountManager.KEY_ACCOUNT_NAME)) {
|
||||||
|
throw new RuntimeException();
|
||||||
}
|
}
|
||||||
|
})
|
||||||
@Override
|
.map(bundle -> bundle.getString(AccountManager.KEY_ACCOUNT_NAME))
|
||||||
protected void onPostExecute(String result) {
|
.doOnError(Timber::e)
|
||||||
super.onPostExecute(result);
|
.observeOn(AndroidSchedulers.mainThread())
|
||||||
if(result != null) {
|
.subscribe(s -> {
|
||||||
Account[] allAccounts = accountManager.getAccountsByType(accountType);
|
Account[] allAccounts = accountManager.getAccountsByType(accountType);
|
||||||
Account curAccount = allAccounts[0];
|
Account curAccount = allAccounts[0];
|
||||||
getAuthCookie(curAccount, accountManager);
|
getAuthCookie(curAccount, accountManager);
|
||||||
} else {
|
},
|
||||||
onAuthFailure();
|
throwable -> 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void requestAuthToken() {
|
protected void requestAuthToken() {
|
||||||
|
|
@ -91,14 +61,7 @@ public abstract class AuthenticatedActivity extends NavigationBaseActivity {
|
||||||
AccountManager accountManager = AccountManager.get(this);
|
AccountManager accountManager = AccountManager.get(this);
|
||||||
Account curAccount = app.getCurrentAccount();
|
Account curAccount = app.getCurrentAccount();
|
||||||
if(curAccount == null) {
|
if(curAccount == null) {
|
||||||
AddAccountTask addAccountTask = new AddAccountTask(accountManager);
|
addAccount(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);
|
|
||||||
} else {
|
} else {
|
||||||
getAuthCookie(curAccount, accountManager);
|
getAuthCookie(curAccount, accountManager);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue