Skip to content

Commit 9c0cbe7

Browse files
committed
Further reduce code calling back to the CommonsApplication by pulling out a SessionManager to manage our current account.
1 parent e7d0c64 commit 9c0cbe7

25 files changed

+273
-294
lines changed

app/src/main/java/fr/free/nrw/commons/CommonsApplication.java

+10-82
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
package fr.free.nrw.commons;
22

3-
import android.accounts.Account;
4-
import android.accounts.AccountManager;
5-
import android.accounts.AccountManagerCallback;
6-
import android.accounts.AccountManagerFuture;
7-
import android.accounts.AuthenticatorException;
8-
import android.accounts.OperationCanceledException;
93
import android.content.Context;
104
import android.content.SharedPreferences;
115
import android.database.sqlite.SQLiteDatabase;
@@ -20,13 +14,12 @@
2014
import org.acra.annotation.ReportsCrashes;
2115

2216
import java.io.File;
23-
import java.io.IOException;
2417

2518
import javax.inject.Inject;
2619

2720
import dagger.android.AndroidInjector;
2821
import dagger.android.DaggerApplication;
29-
import fr.free.nrw.commons.auth.AccountUtil;
22+
import fr.free.nrw.commons.auth.SessionManager;
3023
import fr.free.nrw.commons.contributions.Contribution;
3124
import fr.free.nrw.commons.data.Category;
3225
import fr.free.nrw.commons.data.DBOpenHelper;
@@ -37,6 +30,8 @@
3730
import fr.free.nrw.commons.mwapi.MediaWikiApi;
3831
import fr.free.nrw.commons.theme.NavigationBaseActivity;
3932
import fr.free.nrw.commons.utils.FileUtils;
33+
import io.reactivex.android.schedulers.AndroidSchedulers;
34+
import io.reactivex.schedulers.Schedulers;
4035
import timber.log.Timber;
4136

4237
// TODO: Use ProGuard to rip out reporting when publishing
@@ -51,10 +46,9 @@
5146
public class CommonsApplication extends DaggerApplication {
5247

5348
@Inject MediaWikiApi mediaWikiApi;
54-
@Inject AccountUtil accountUtil;
49+
@Inject SessionManager sessionManager;
5550
@Inject DBOpenHelper dbOpenHelper;
5651

57-
private Account currentAccount = null; // Unlike a savings account...
5852
public static final String API_URL = "https://commons.wikimedia.org/w/api.php";
5953
public static final String IMAGE_URL_BASE = "https://upload.wikimedia.org/wikipedia/commons";
6054
public static final String HOME_URL = "https://commons.wikimedia.org/wiki/";
@@ -113,39 +107,6 @@ public CommonsApplicationComponent injector() {
113107
return component;
114108
}
115109

116-
/**
117-
* @return Account|null
118-
*/
119-
public Account getCurrentAccount() {
120-
if (currentAccount == null) {
121-
AccountManager accountManager = AccountManager.get(this);
122-
Account[] allAccounts = accountManager.getAccountsByType(accountUtil.accountType());
123-
if (allAccounts.length != 0) {
124-
currentAccount = allAccounts[0];
125-
}
126-
}
127-
return currentAccount;
128-
}
129-
130-
public Boolean revalidateAuthToken() {
131-
AccountManager accountManager = AccountManager.get(this);
132-
Account curAccount = getCurrentAccount();
133-
134-
if (curAccount == null) {
135-
return false; // This should never happen
136-
}
137-
138-
accountManager.invalidateAuthToken(accountUtil.accountType(), mediaWikiApi.getAuthCookie());
139-
try {
140-
String authCookie = accountManager.blockingGetAuthToken(curAccount, "", false);
141-
mediaWikiApi.setAuthCookie(authCookie);
142-
return true;
143-
} catch (OperationCanceledException | NullPointerException | IOException | AuthenticatorException e) {
144-
e.printStackTrace();
145-
return false;
146-
}
147-
}
148-
149110
public void clearApplicationData(Context context, NavigationBaseActivity.LogoutListener logoutListener) {
150111
File cacheDirectory = context.getCacheDir();
151112
File applicationDirectory = new File(cacheDirectory.getParent());
@@ -158,36 +119,10 @@ public void clearApplicationData(Context context, NavigationBaseActivity.LogoutL
158119
}
159120
}
160121

161-
AccountManager accountManager = AccountManager.get(this);
162-
Account[] allAccounts = accountManager.getAccountsByType(accountUtil.accountType());
163-
164-
AccountManagerCallback<Boolean> amCallback = new AccountManagerCallback<Boolean>() {
165-
166-
private int index = 0;
167-
168-
void setIndex(int index) {
169-
this.index = index;
170-
}
171-
172-
int getIndex() {
173-
return index;
174-
}
175-
176-
@Override
177-
public void run(AccountManagerFuture<Boolean> accountManagerFuture) {
178-
setIndex(getIndex() + 1);
179-
180-
try {
181-
if (accountManagerFuture != null) {
182-
if (accountManagerFuture.getResult()) {
183-
Timber.d("Account removed successfully.");
184-
}
185-
}
186-
} catch (OperationCanceledException | IOException | AuthenticatorException e) {
187-
e.printStackTrace();
188-
}
189-
190-
if (getIndex() == allAccounts.length) {
122+
sessionManager.clearAllAccounts()
123+
.subscribeOn(Schedulers.io())
124+
.observeOn(AndroidSchedulers.mainThread())
125+
.subscribe(() -> {
191126
Timber.d("All accounts have been removed");
192127
//TODO: fix preference manager
193128
PreferenceManager.getDefaultSharedPreferences(CommonsApplication.this).edit().clear().commit();
@@ -197,22 +132,15 @@ public void run(AccountManagerFuture<Boolean> accountManagerFuture) {
197132
context.getSharedPreferences("prefs", Context.MODE_PRIVATE).edit().clear().commit();
198133
preferences.edit().putBoolean("firstrun", false).apply();
199134
updateAllDatabases();
200-
currentAccount = null;
201135

202136
logoutListener.onLogoutComplete();
203-
}
204-
}
205-
};
206-
207-
for (Account account : allAccounts) {
208-
accountManager.removeAccount(account, amCallback, null);
209-
}
137+
});
210138
}
211139

212140
/**
213141
* Deletes all tables and re-creates them.
214142
*/
215-
public void updateAllDatabases() {
143+
private void updateAllDatabases() {
216144
dbOpenHelper.getReadableDatabase().close();
217145
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
218146

app/src/main/java/fr/free/nrw/commons/MediaWikiImageView.java

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import timber.log.Timber;
1919

2020
public class MediaWikiImageView extends SimpleDraweeView {
21-
@Inject CommonsApplication application;
2221
@Inject MediaWikiApi mwApi;
2322
@Inject LruCache<String, String> thumbnailUrlCache;
2423

app/src/main/java/fr/free/nrw/commons/auth/AccountUtil.java

+5-8
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,21 @@
44
import android.accounts.AccountAuthenticatorResponse;
55
import android.accounts.AccountManager;
66
import android.content.ContentResolver;
7+
import android.content.Context;
78
import android.os.Bundle;
89
import android.support.annotation.NonNull;
910
import android.support.annotation.Nullable;
1011

11-
import javax.inject.Inject;
12-
13-
import fr.free.nrw.commons.CommonsApplication;
1412
import fr.free.nrw.commons.contributions.ContributionsContentProvider;
1513
import fr.free.nrw.commons.modifications.ModificationsContentProvider;
1614
import timber.log.Timber;
1715

1816
public class AccountUtil {
1917

20-
private final CommonsApplication application;
18+
private Context context;
2119

22-
@Inject
23-
public AccountUtil(CommonsApplication application) {
24-
this.application = application;
20+
public AccountUtil(Context context) {
21+
this.context = context;
2522
}
2623

2724
public void createAccount(@Nullable AccountAuthenticatorResponse response,
@@ -60,7 +57,7 @@ public String accountType() {
6057
}
6158

6259
private AccountManager accountManager() {
63-
return AccountManager.get(application);
60+
return AccountManager.get(context);
6461
}
6562

6663
}

app/src/main/java/fr/free/nrw/commons/auth/AuthenticatedActivity.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
import javax.inject.Inject;
99

10-
import dagger.android.AndroidInjection;
11-
import fr.free.nrw.commons.CommonsApplication;
1210
import fr.free.nrw.commons.theme.NavigationBaseActivity;
1311
import io.reactivex.Single;
1412
import io.reactivex.android.schedulers.AndroidSchedulers;
@@ -17,8 +15,8 @@
1715

1816
public abstract class AuthenticatedActivity extends NavigationBaseActivity {
1917

20-
@Inject CommonsApplication app;
2118
@Inject AccountUtil accountUtil;
19+
@Inject SessionManager sessionManager;
2220

2321
private String authCookie;
2422

@@ -59,7 +57,7 @@ protected void requestAuthToken() {
5957
return;
6058
}
6159
AccountManager accountManager = AccountManager.get(this);
62-
Account curAccount = app.getCurrentAccount();
60+
Account curAccount = sessionManager.getCurrentAccount();
6361
if(curAccount == null) {
6462
addAccount(accountManager);
6563
} else {

app/src/main/java/fr/free/nrw/commons/auth/LoginActivity.java

+4-6
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,9 @@
1919

2020
import dagger.android.AndroidInjection;
2121
import fr.free.nrw.commons.BuildConfig;
22-
import fr.free.nrw.commons.CommonsApplication;
22+
import fr.free.nrw.commons.PageTitle;
2323
import fr.free.nrw.commons.R;
2424
import fr.free.nrw.commons.WelcomeActivity;
25-
26-
import fr.free.nrw.commons.PageTitle;
2725
import fr.free.nrw.commons.contributions.ContributionsActivity;
2826
import fr.free.nrw.commons.mwapi.MediaWikiApi;
2927
import timber.log.Timber;
@@ -36,9 +34,9 @@ public class LoginActivity extends AccountAuthenticatorActivity {
3634

3735
public static final String PARAM_USERNAME = "fr.free.nrw.commons.login.username";
3836

39-
@Inject CommonsApplication application;
4037
@Inject MediaWikiApi mwApi;
4138
@Inject AccountUtil accountUtil;
39+
@Inject SessionManager sessionManager;
4240

4341
private SharedPreferences prefs = null;
4442

@@ -114,7 +112,7 @@ protected void onResume() {
114112
WelcomeActivity.startYourself(this);
115113
prefs.edit().putBoolean("firstrun", false).apply();
116114
}
117-
if (application.getCurrentAccount() != null) {
115+
if (sessionManager.getCurrentAccount() != null) {
118116
startMainActivity();
119117
}
120118
}
@@ -147,7 +145,7 @@ private LoginTask getLoginTask() {
147145
canonicializeUsername(usernameEdit.getText().toString()),
148146
passwordEdit.getText().toString(),
149147
twoFactorEdit.getText().toString(),
150-
accountUtil, application, mwApi
148+
accountUtil, this, mwApi
151149
);
152150
}
153151

app/src/main/java/fr/free/nrw/commons/auth/LoginTask.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import android.accounts.AccountAuthenticatorResponse;
44
import android.accounts.AccountManager;
55
import android.app.ProgressDialog;
6+
import android.content.Context;
67
import android.os.AsyncTask;
78
import android.os.Bundle;
89

@@ -21,16 +22,18 @@ class LoginTask extends AsyncTask<String, String, String> {
2122
private String password;
2223
private String twoFactorCode = "";
2324
private AccountUtil accountUtil;
24-
private CommonsApplication app;
25+
private Context context;
2526
private MediaWikiApi mwApi;
2627

27-
public LoginTask(LoginActivity loginActivity, String username, String password, String twoFactorCode, AccountUtil accountUtil, CommonsApplication application, MediaWikiApi mwApi) {
28+
public LoginTask(LoginActivity loginActivity, String username, String password,
29+
String twoFactorCode, AccountUtil accountUtil,
30+
Context context, MediaWikiApi mwApi) {
2831
this.loginActivity = loginActivity;
2932
this.username = username;
3033
this.password = password;
3134
this.twoFactorCode = twoFactorCode;
3235
this.accountUtil = accountUtil;
33-
this.app = application;
36+
this.context = context;
3437
this.mwApi = mwApi;
3538
}
3639

@@ -64,7 +67,7 @@ protected void onPostExecute(String result) {
6467
super.onPostExecute(result);
6568
Timber.d("Login done!");
6669

67-
EventLog.schema(CommonsApplication.EVENT_LOGIN_ATTEMPT, app, mwApi)
70+
EventLog.schema(CommonsApplication.EVENT_LOGIN_ATTEMPT, context, mwApi)
6871
.param("username", username)
6972
.param("result", result)
7073
.log();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package fr.free.nrw.commons.auth;
2+
3+
import android.accounts.Account;
4+
import android.accounts.AccountManager;
5+
import android.accounts.AuthenticatorException;
6+
import android.accounts.OperationCanceledException;
7+
import android.content.Context;
8+
9+
import java.io.IOException;
10+
11+
import fr.free.nrw.commons.mwapi.MediaWikiApi;
12+
import io.reactivex.Completable;
13+
import io.reactivex.Observable;
14+
15+
/**
16+
* Manage the current logged in user session.
17+
*/
18+
public class SessionManager {
19+
private final Context context;
20+
private final AccountUtil accountUtil;
21+
private final MediaWikiApi mediaWikiApi;
22+
private Account currentAccount; // Unlike a savings account... ;-)
23+
24+
public SessionManager(Context context, AccountUtil accountUtil, MediaWikiApi mediaWikiApi) {
25+
this.context = context;
26+
this.accountUtil = accountUtil;
27+
this.mediaWikiApi = mediaWikiApi;
28+
this.currentAccount = null;
29+
}
30+
31+
/**
32+
* @return Account|null
33+
*/
34+
public Account getCurrentAccount() {
35+
if (currentAccount == null) {
36+
AccountManager accountManager = AccountManager.get(context);
37+
Account[] allAccounts = accountManager.getAccountsByType(accountUtil.accountType());
38+
if (allAccounts.length != 0) {
39+
currentAccount = allAccounts[0];
40+
}
41+
}
42+
return currentAccount;
43+
}
44+
45+
public Boolean revalidateAuthToken() {
46+
AccountManager accountManager = AccountManager.get(context);
47+
Account curAccount = getCurrentAccount();
48+
49+
if (curAccount == null) {
50+
return false; // This should never happen
51+
}
52+
53+
accountManager.invalidateAuthToken(accountUtil.accountType(), mediaWikiApi.getAuthCookie());
54+
try {
55+
String authCookie = accountManager.blockingGetAuthToken(curAccount, "", false);
56+
mediaWikiApi.setAuthCookie(authCookie);
57+
return true;
58+
} catch (OperationCanceledException | NullPointerException | IOException | AuthenticatorException e) {
59+
e.printStackTrace();
60+
return false;
61+
}
62+
}
63+
64+
public Completable clearAllAccounts() {
65+
AccountManager accountManager = AccountManager.get(context);
66+
Account[] allAccounts = accountManager.getAccountsByType(accountUtil.accountType());
67+
return Completable.fromObservable(Observable.fromArray(allAccounts)
68+
.map(a -> accountManager.removeAccount(a, null, null).getResult()))
69+
.doOnComplete(() -> currentAccount = null);
70+
}
71+
}

0 commit comments

Comments
 (0)