mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-29 22:03:55 +01:00
Further reduce code calling back to the CommonsApplication by pulling out a SessionManager to manage our current account.
This commit is contained in:
parent
e7d0c647c2
commit
9c0cbe7ad5
25 changed files with 273 additions and 294 deletions
|
|
@ -4,24 +4,21 @@ import android.accounts.Account;
|
|||
import android.accounts.AccountAuthenticatorResponse;
|
||||
import android.accounts.AccountManager;
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import fr.free.nrw.commons.CommonsApplication;
|
||||
import fr.free.nrw.commons.contributions.ContributionsContentProvider;
|
||||
import fr.free.nrw.commons.modifications.ModificationsContentProvider;
|
||||
import timber.log.Timber;
|
||||
|
||||
public class AccountUtil {
|
||||
|
||||
private final CommonsApplication application;
|
||||
private Context context;
|
||||
|
||||
@Inject
|
||||
public AccountUtil(CommonsApplication application) {
|
||||
this.application = application;
|
||||
public AccountUtil(Context context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
public void createAccount(@Nullable AccountAuthenticatorResponse response,
|
||||
|
|
@ -60,7 +57,7 @@ public class AccountUtil {
|
|||
}
|
||||
|
||||
private AccountManager accountManager() {
|
||||
return AccountManager.get(application);
|
||||
return AccountManager.get(context);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,8 +7,6 @@ import android.os.Bundle;
|
|||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import dagger.android.AndroidInjection;
|
||||
import fr.free.nrw.commons.CommonsApplication;
|
||||
import fr.free.nrw.commons.theme.NavigationBaseActivity;
|
||||
import io.reactivex.Single;
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers;
|
||||
|
|
@ -17,8 +15,8 @@ import timber.log.Timber;
|
|||
|
||||
public abstract class AuthenticatedActivity extends NavigationBaseActivity {
|
||||
|
||||
@Inject CommonsApplication app;
|
||||
@Inject AccountUtil accountUtil;
|
||||
@Inject SessionManager sessionManager;
|
||||
|
||||
private String authCookie;
|
||||
|
||||
|
|
@ -59,7 +57,7 @@ public abstract class AuthenticatedActivity extends NavigationBaseActivity {
|
|||
return;
|
||||
}
|
||||
AccountManager accountManager = AccountManager.get(this);
|
||||
Account curAccount = app.getCurrentAccount();
|
||||
Account curAccount = sessionManager.getCurrentAccount();
|
||||
if(curAccount == null) {
|
||||
addAccount(accountManager);
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -19,11 +19,9 @@ import javax.inject.Inject;
|
|||
|
||||
import dagger.android.AndroidInjection;
|
||||
import fr.free.nrw.commons.BuildConfig;
|
||||
import fr.free.nrw.commons.CommonsApplication;
|
||||
import fr.free.nrw.commons.PageTitle;
|
||||
import fr.free.nrw.commons.R;
|
||||
import fr.free.nrw.commons.WelcomeActivity;
|
||||
|
||||
import fr.free.nrw.commons.PageTitle;
|
||||
import fr.free.nrw.commons.contributions.ContributionsActivity;
|
||||
import fr.free.nrw.commons.mwapi.MediaWikiApi;
|
||||
import timber.log.Timber;
|
||||
|
|
@ -36,9 +34,9 @@ public class LoginActivity extends AccountAuthenticatorActivity {
|
|||
|
||||
public static final String PARAM_USERNAME = "fr.free.nrw.commons.login.username";
|
||||
|
||||
@Inject CommonsApplication application;
|
||||
@Inject MediaWikiApi mwApi;
|
||||
@Inject AccountUtil accountUtil;
|
||||
@Inject SessionManager sessionManager;
|
||||
|
||||
private SharedPreferences prefs = null;
|
||||
|
||||
|
|
@ -114,7 +112,7 @@ public class LoginActivity extends AccountAuthenticatorActivity {
|
|||
WelcomeActivity.startYourself(this);
|
||||
prefs.edit().putBoolean("firstrun", false).apply();
|
||||
}
|
||||
if (application.getCurrentAccount() != null) {
|
||||
if (sessionManager.getCurrentAccount() != null) {
|
||||
startMainActivity();
|
||||
}
|
||||
}
|
||||
|
|
@ -147,7 +145,7 @@ public class LoginActivity extends AccountAuthenticatorActivity {
|
|||
canonicializeUsername(usernameEdit.getText().toString()),
|
||||
passwordEdit.getText().toString(),
|
||||
twoFactorEdit.getText().toString(),
|
||||
accountUtil, application, mwApi
|
||||
accountUtil, this, mwApi
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package fr.free.nrw.commons.auth;
|
|||
import android.accounts.AccountAuthenticatorResponse;
|
||||
import android.accounts.AccountManager;
|
||||
import android.app.ProgressDialog;
|
||||
import android.content.Context;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
|
||||
|
|
@ -21,16 +22,18 @@ class LoginTask extends AsyncTask<String, String, String> {
|
|||
private String password;
|
||||
private String twoFactorCode = "";
|
||||
private AccountUtil accountUtil;
|
||||
private CommonsApplication app;
|
||||
private Context context;
|
||||
private MediaWikiApi mwApi;
|
||||
|
||||
public LoginTask(LoginActivity loginActivity, String username, String password, String twoFactorCode, AccountUtil accountUtil, CommonsApplication application, MediaWikiApi mwApi) {
|
||||
public LoginTask(LoginActivity loginActivity, String username, String password,
|
||||
String twoFactorCode, AccountUtil accountUtil,
|
||||
Context context, MediaWikiApi mwApi) {
|
||||
this.loginActivity = loginActivity;
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
this.twoFactorCode = twoFactorCode;
|
||||
this.accountUtil = accountUtil;
|
||||
this.app = application;
|
||||
this.context = context;
|
||||
this.mwApi = mwApi;
|
||||
}
|
||||
|
||||
|
|
@ -64,7 +67,7 @@ class LoginTask extends AsyncTask<String, String, String> {
|
|||
super.onPostExecute(result);
|
||||
Timber.d("Login done!");
|
||||
|
||||
EventLog.schema(CommonsApplication.EVENT_LOGIN_ATTEMPT, app, mwApi)
|
||||
EventLog.schema(CommonsApplication.EVENT_LOGIN_ATTEMPT, context, mwApi)
|
||||
.param("username", username)
|
||||
.param("result", result)
|
||||
.log();
|
||||
|
|
|
|||
|
|
@ -0,0 +1,71 @@
|
|||
package fr.free.nrw.commons.auth;
|
||||
|
||||
import android.accounts.Account;
|
||||
import android.accounts.AccountManager;
|
||||
import android.accounts.AuthenticatorException;
|
||||
import android.accounts.OperationCanceledException;
|
||||
import android.content.Context;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import fr.free.nrw.commons.mwapi.MediaWikiApi;
|
||||
import io.reactivex.Completable;
|
||||
import io.reactivex.Observable;
|
||||
|
||||
/**
|
||||
* Manage the current logged in user session.
|
||||
*/
|
||||
public class SessionManager {
|
||||
private final Context context;
|
||||
private final AccountUtil accountUtil;
|
||||
private final MediaWikiApi mediaWikiApi;
|
||||
private Account currentAccount; // Unlike a savings account... ;-)
|
||||
|
||||
public SessionManager(Context context, AccountUtil accountUtil, MediaWikiApi mediaWikiApi) {
|
||||
this.context = context;
|
||||
this.accountUtil = accountUtil;
|
||||
this.mediaWikiApi = mediaWikiApi;
|
||||
this.currentAccount = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Account|null
|
||||
*/
|
||||
public Account getCurrentAccount() {
|
||||
if (currentAccount == null) {
|
||||
AccountManager accountManager = AccountManager.get(context);
|
||||
Account[] allAccounts = accountManager.getAccountsByType(accountUtil.accountType());
|
||||
if (allAccounts.length != 0) {
|
||||
currentAccount = allAccounts[0];
|
||||
}
|
||||
}
|
||||
return currentAccount;
|
||||
}
|
||||
|
||||
public Boolean revalidateAuthToken() {
|
||||
AccountManager accountManager = AccountManager.get(context);
|
||||
Account curAccount = getCurrentAccount();
|
||||
|
||||
if (curAccount == null) {
|
||||
return false; // This should never happen
|
||||
}
|
||||
|
||||
accountManager.invalidateAuthToken(accountUtil.accountType(), mediaWikiApi.getAuthCookie());
|
||||
try {
|
||||
String authCookie = accountManager.blockingGetAuthToken(curAccount, "", false);
|
||||
mediaWikiApi.setAuthCookie(authCookie);
|
||||
return true;
|
||||
} catch (OperationCanceledException | NullPointerException | IOException | AuthenticatorException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public Completable clearAllAccounts() {
|
||||
AccountManager accountManager = AccountManager.get(context);
|
||||
Account[] allAccounts = accountManager.getAccountsByType(accountUtil.accountType());
|
||||
return Completable.fromObservable(Observable.fromArray(allAccounts)
|
||||
.map(a -> accountManager.removeAccount(a, null, null).getResult()))
|
||||
.doOnComplete(() -> currentAccount = null);
|
||||
}
|
||||
}
|
||||
|
|
@ -6,16 +6,11 @@ import android.webkit.WebView;
|
|||
import android.webkit.WebViewClient;
|
||||
import android.widget.Toast;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import dagger.android.AndroidInjection;
|
||||
import fr.free.nrw.commons.CommonsApplication;
|
||||
import fr.free.nrw.commons.theme.BaseActivity;
|
||||
import timber.log.Timber;
|
||||
|
||||
public class SignupActivity extends BaseActivity {
|
||||
|
||||
@Inject CommonsApplication application;
|
||||
private WebView webView;
|
||||
|
||||
@Override
|
||||
|
|
@ -41,11 +36,8 @@ public class SignupActivity extends BaseActivity {
|
|||
//Signup success, so clear cookies, notify user, and load LoginActivity again
|
||||
Timber.d("Overriding URL %s", url);
|
||||
|
||||
Toast toast = Toast.makeText(
|
||||
application,
|
||||
"Account created!",
|
||||
Toast.LENGTH_LONG
|
||||
);
|
||||
Toast toast = Toast.makeText(SignupActivity.this,
|
||||
"Account created!", Toast.LENGTH_LONG);
|
||||
toast.show();
|
||||
// terminate on task completion.
|
||||
finish();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue