Skip to content

Commit a208c9f

Browse files
committed
Further reduced coupling between classes and the need to @Inject the account utils class, fixed imports.
1 parent 9c0cbe7 commit a208c9f

19 files changed

+66
-84
lines changed

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

-4
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@
55

66
import java.io.UnsupportedEncodingException;
77
import java.net.URLEncoder;
8-
import java.text.ParseException;
9-
import java.text.SimpleDateFormat;
10-
import java.util.Date;
118
import java.util.Locale;
12-
import java.util.TimeZone;
139
import java.util.regex.Matcher;
1410
import java.util.regex.Pattern;
1511

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

+10-11
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,20 @@
66
import android.content.ContentResolver;
77
import android.content.Context;
88
import android.os.Bundle;
9-
import android.support.annotation.NonNull;
109
import android.support.annotation.Nullable;
1110

1211
import fr.free.nrw.commons.contributions.ContributionsContentProvider;
1312
import fr.free.nrw.commons.modifications.ModificationsContentProvider;
1413
import timber.log.Timber;
1514

15+
import static android.accounts.AccountManager.ERROR_CODE_REMOTE_EXCEPTION;
16+
import static android.accounts.AccountManager.KEY_ACCOUNT_NAME;
17+
import static android.accounts.AccountManager.KEY_ACCOUNT_TYPE;
18+
1619
public class AccountUtil {
1720

18-
private Context context;
21+
static final String ACCOUNT_TYPE = "fr.free.nrw.commons";
22+
private final Context context;
1923

2024
public AccountUtil(Context context) {
2125
this.context = context;
@@ -24,24 +28,24 @@ public AccountUtil(Context context) {
2428
public void createAccount(@Nullable AccountAuthenticatorResponse response,
2529
String username, String password) {
2630

27-
Account account = new Account(username, accountType());
31+
Account account = new Account(username, ACCOUNT_TYPE);
2832
boolean created = accountManager().addAccountExplicitly(account, password, null);
2933

3034
Timber.d("account creation " + (created ? "successful" : "failure"));
3135

3236
if (created) {
3337
if (response != null) {
3438
Bundle bundle = new Bundle();
35-
bundle.putString(AccountManager.KEY_ACCOUNT_NAME, username);
36-
bundle.putString(AccountManager.KEY_ACCOUNT_TYPE, accountType());
39+
bundle.putString(KEY_ACCOUNT_NAME, username);
40+
bundle.putString(KEY_ACCOUNT_TYPE, ACCOUNT_TYPE);
3741

3842

3943
response.onResult(bundle);
4044
}
4145

4246
} else {
4347
if (response != null) {
44-
response.onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, "");
48+
response.onError(ERROR_CODE_REMOTE_EXCEPTION, "");
4549
}
4650
Timber.d("account creation failure");
4751
}
@@ -51,11 +55,6 @@ public void createAccount(@Nullable AccountAuthenticatorResponse response,
5155
ContentResolver.setSyncAutomatically(account, ModificationsContentProvider.AUTHORITY, true); // Enable sync by default!
5256
}
5357

54-
@NonNull
55-
public String accountType() {
56-
return "fr.free.nrw.commons";
57-
}
58-
5958
private AccountManager accountManager() {
6059
return AccountManager.get(context);
6160
}

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

+9-6
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
import io.reactivex.schedulers.Schedulers;
1414
import timber.log.Timber;
1515

16+
import static android.accounts.AccountManager.KEY_ACCOUNT_NAME;
17+
import static fr.free.nrw.commons.auth.AccountUtil.ACCOUNT_TYPE;
18+
1619
public abstract class AuthenticatedActivity extends NavigationBaseActivity {
1720

18-
@Inject AccountUtil accountUtil;
1921
@Inject SessionManager sessionManager;
2022

2123
private String authCookie;
@@ -31,20 +33,21 @@ private void getAuthCookie(Account account, AccountManager accountManager) {
3133
}
3234

3335
private void addAccount(AccountManager accountManager) {
34-
Single.just(accountManager.addAccount(accountUtil.accountType(), null, null, null, AuthenticatedActivity.this, null, null))
36+
Single.just(accountManager.addAccount(ACCOUNT_TYPE, null, null,
37+
null, AuthenticatedActivity.this, null, null))
3538
.subscribeOn(Schedulers.io())
3639
.map(AccountManagerFuture::getResult)
3740
.doOnEvent((bundle, throwable) -> {
38-
if (!bundle.containsKey(AccountManager.KEY_ACCOUNT_NAME)) {
41+
if (!bundle.containsKey(KEY_ACCOUNT_NAME)) {
3942
throw new RuntimeException("Bundle doesn't contain account-name key: "
40-
+ AccountManager.KEY_ACCOUNT_NAME);
43+
+ KEY_ACCOUNT_NAME);
4144
}
4245
})
43-
.map(bundle -> bundle.getString(AccountManager.KEY_ACCOUNT_NAME))
46+
.map(bundle -> bundle.getString(KEY_ACCOUNT_NAME))
4447
.doOnError(Timber::e)
4548
.observeOn(AndroidSchedulers.mainThread())
4649
.subscribe(s -> {
47-
Account[] allAccounts = accountManager.getAccountsByType(accountUtil.accountType());
50+
Account[] allAccounts = accountManager.getAccountsByType(ACCOUNT_TYPE);
4851
Account curAccount = allAccounts[0];
4952
getAuthCookie(curAccount, accountManager);
5053
},

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

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package fr.free.nrw.commons.auth;
22

33
import android.accounts.AccountAuthenticatorResponse;
4-
import android.accounts.AccountManager;
54
import android.app.ProgressDialog;
65
import android.content.Context;
76
import android.os.AsyncTask;
@@ -15,6 +14,11 @@
1514
import fr.free.nrw.commons.mwapi.MediaWikiApi;
1615
import timber.log.Timber;
1716

17+
import static android.accounts.AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE;
18+
import static android.accounts.AccountManager.KEY_ACCOUNT_NAME;
19+
import static android.accounts.AccountManager.KEY_ACCOUNT_TYPE;
20+
import static fr.free.nrw.commons.auth.AccountUtil.ACCOUNT_TYPE;
21+
1822
class LoginTask extends AsyncTask<String, String, String> {
1923

2024
private LoginActivity loginActivity;
@@ -87,11 +91,11 @@ private void handlePassResult() {
8791
Bundle extras = loginActivity.getIntent().getExtras();
8892
if (extras != null) {
8993
Timber.d("Bundle of extras: %s", extras);
90-
response = extras.getParcelable(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE);
94+
response = extras.getParcelable(KEY_ACCOUNT_AUTHENTICATOR_RESPONSE);
9195
if (response != null) {
9296
Bundle authResult = new Bundle();
93-
authResult.putString(AccountManager.KEY_ACCOUNT_NAME, username);
94-
authResult.putString(AccountManager.KEY_ACCOUNT_TYPE, accountUtil.accountType());
97+
authResult.putString(KEY_ACCOUNT_NAME, username);
98+
authResult.putString(KEY_ACCOUNT_TYPE, ACCOUNT_TYPE);
9599
response.onResult(authResult);
96100
}
97101
}

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@
1212
import io.reactivex.Completable;
1313
import io.reactivex.Observable;
1414

15+
import static fr.free.nrw.commons.auth.AccountUtil.ACCOUNT_TYPE;
16+
1517
/**
1618
* Manage the current logged in user session.
1719
*/
1820
public class SessionManager {
1921
private final Context context;
20-
private final AccountUtil accountUtil;
2122
private final MediaWikiApi mediaWikiApi;
2223
private Account currentAccount; // Unlike a savings account... ;-)
2324

24-
public SessionManager(Context context, AccountUtil accountUtil, MediaWikiApi mediaWikiApi) {
25+
public SessionManager(Context context, MediaWikiApi mediaWikiApi) {
2526
this.context = context;
26-
this.accountUtil = accountUtil;
2727
this.mediaWikiApi = mediaWikiApi;
2828
this.currentAccount = null;
2929
}
@@ -34,7 +34,7 @@ public SessionManager(Context context, AccountUtil accountUtil, MediaWikiApi med
3434
public Account getCurrentAccount() {
3535
if (currentAccount == null) {
3636
AccountManager accountManager = AccountManager.get(context);
37-
Account[] allAccounts = accountManager.getAccountsByType(accountUtil.accountType());
37+
Account[] allAccounts = accountManager.getAccountsByType(ACCOUNT_TYPE);
3838
if (allAccounts.length != 0) {
3939
currentAccount = allAccounts[0];
4040
}
@@ -50,7 +50,7 @@ public Boolean revalidateAuthToken() {
5050
return false; // This should never happen
5151
}
5252

53-
accountManager.invalidateAuthToken(accountUtil.accountType(), mediaWikiApi.getAuthCookie());
53+
accountManager.invalidateAuthToken(ACCOUNT_TYPE, mediaWikiApi.getAuthCookie());
5454
try {
5555
String authCookie = accountManager.blockingGetAuthToken(curAccount, "", false);
5656
mediaWikiApi.setAuthCookie(authCookie);
@@ -63,7 +63,7 @@ public Boolean revalidateAuthToken() {
6363

6464
public Completable clearAllAccounts() {
6565
AccountManager accountManager = AccountManager.get(context);
66-
Account[] allAccounts = accountManager.getAccountsByType(accountUtil.accountType());
66+
Account[] allAccounts = accountManager.getAccountsByType(ACCOUNT_TYPE);
6767
return Completable.fromObservable(Observable.fromArray(allAccounts)
6868
.map(a -> accountManager.removeAccount(a, null, null).getResult()))
6969
.doOnComplete(() -> currentAccount = null);

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

+27-16
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,41 @@
1515

1616
import fr.free.nrw.commons.mwapi.MediaWikiApi;
1717

18+
import static android.accounts.AccountManager.ERROR_CODE_UNSUPPORTED_OPERATION;
19+
import static android.accounts.AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE;
20+
import static android.accounts.AccountManager.KEY_ACCOUNT_NAME;
21+
import static android.accounts.AccountManager.KEY_ACCOUNT_TYPE;
22+
import static android.accounts.AccountManager.KEY_AUTHTOKEN;
23+
import static android.accounts.AccountManager.KEY_BOOLEAN_RESULT;
24+
import static android.accounts.AccountManager.KEY_ERROR_CODE;
25+
import static android.accounts.AccountManager.KEY_ERROR_MESSAGE;
26+
import static android.accounts.AccountManager.KEY_INTENT;
27+
import static fr.free.nrw.commons.auth.AccountUtil.ACCOUNT_TYPE;
28+
import static fr.free.nrw.commons.auth.LoginActivity.PARAM_USERNAME;
29+
1830
public class WikiAccountAuthenticator extends AbstractAccountAuthenticator {
1931

2032
private final Context context;
21-
private final AccountUtil accountUtil;
2233
private MediaWikiApi mediaWikiApi;
2334

24-
public WikiAccountAuthenticator(Context context, AccountUtil accountUtil, MediaWikiApi mwApi) {
35+
public WikiAccountAuthenticator(Context context, MediaWikiApi mwApi) {
2536
super(context);
2637
this.context = context;
27-
this.accountUtil = accountUtil;
2838
this.mediaWikiApi = mwApi;
2939
}
3040

3141
private Bundle unsupportedOperation() {
3242
Bundle bundle = new Bundle();
33-
bundle.putInt(AccountManager.KEY_ERROR_CODE, AccountManager.ERROR_CODE_UNSUPPORTED_OPERATION);
43+
bundle.putInt(KEY_ERROR_CODE, ERROR_CODE_UNSUPPORTED_OPERATION);
3444

3545
// HACK: the docs indicate that this is a required key bit it's not displayed to the user.
36-
bundle.putString(AccountManager.KEY_ERROR_MESSAGE, "");
46+
bundle.putString(KEY_ERROR_MESSAGE, "");
3747

3848
return bundle;
3949
}
4050

4151
private boolean supportedAccountType(@Nullable String type) {
42-
return accountUtil.accountType().equals(type);
52+
return ACCOUNT_TYPE.equals(type);
4353
}
4454

4555
@Override
@@ -57,10 +67,10 @@ public Bundle addAccount(@NonNull AccountAuthenticatorResponse response,
5767

5868
private Bundle addAccount(AccountAuthenticatorResponse response) {
5969
Intent Intent = new Intent(context, LoginActivity.class);
60-
Intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
70+
Intent.putExtra(KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
6171

6272
Bundle bundle = new Bundle();
63-
bundle.putParcelable(AccountManager.KEY_INTENT, Intent);
73+
bundle.putParcelable(KEY_INTENT, Intent);
6474

6575
return bundle;
6676
}
@@ -80,12 +90,13 @@ public Bundle editProperties(AccountAuthenticatorResponse response, String accou
8090
private String getAuthCookie(String username, String password) throws IOException {
8191
//TODO add 2fa support here
8292
String result = mediaWikiApi.login(username, password);
83-
if(result.equals("PASS")) {
93+
if (result.equals("PASS")) {
8494
return mediaWikiApi.getAuthCookie();
8595
} else {
8696
return null;
8797
}
8898
}
99+
89100
@Override
90101
public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options) throws NetworkErrorException {
91102
// Extract the username and password from the Account Manager, and ask
@@ -103,9 +114,9 @@ public Bundle getAuthToken(AccountAuthenticatorResponse response, Account accoun
103114
}
104115
if (authCookie != null) {
105116
final Bundle result = new Bundle();
106-
result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
107-
result.putString(AccountManager.KEY_ACCOUNT_TYPE, accountUtil.accountType());
108-
result.putString(AccountManager.KEY_AUTHTOKEN, authCookie);
117+
result.putString(KEY_ACCOUNT_NAME, account.name);
118+
result.putString(KEY_ACCOUNT_TYPE, ACCOUNT_TYPE);
119+
result.putString(KEY_AUTHTOKEN, authCookie);
109120
return result;
110121
}
111122
}
@@ -114,10 +125,10 @@ public Bundle getAuthToken(AccountAuthenticatorResponse response, Account accoun
114125
// need to re-prompt them for their credentials. We do that by creating
115126
// an intent to display our AuthenticatorActivity panel.
116127
final Intent intent = new Intent(context, LoginActivity.class);
117-
intent.putExtra(LoginActivity.PARAM_USERNAME, account.name);
118-
intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
128+
intent.putExtra(PARAM_USERNAME, account.name);
129+
intent.putExtra(KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
119130
final Bundle bundle = new Bundle();
120-
bundle.putParcelable(AccountManager.KEY_INTENT, intent);
131+
bundle.putParcelable(KEY_INTENT, intent);
121132
return bundle;
122133
}
123134

@@ -135,7 +146,7 @@ public Bundle hasFeatures(@NonNull AccountAuthenticatorResponse response,
135146
@NonNull Account account, @NonNull String[] features)
136147
throws NetworkErrorException {
137148
Bundle bundle = new Bundle();
138-
bundle.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, false);
149+
bundle.putBoolean(KEY_BOOLEAN_RESULT, false);
139150
return bundle;
140151
}
141152

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

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,28 @@
11
package fr.free.nrw.commons.auth;
22

3-
import android.accounts.AccountManager;
4-
import android.app.Service;
53
import android.content.Intent;
64
import android.os.IBinder;
75

86
import javax.inject.Inject;
97

108
import dagger.android.DaggerService;
11-
import fr.free.nrw.commons.CommonsApplication;
129
import fr.free.nrw.commons.mwapi.MediaWikiApi;
1310

11+
import static android.accounts.AccountManager.ACTION_AUTHENTICATOR_INTENT;
12+
1413
public class WikiAccountAuthenticatorService extends DaggerService {
1514

1615
@Inject MediaWikiApi mwApi;
17-
@Inject AccountUtil accountUtil;
18-
1916
private WikiAccountAuthenticator wikiAccountAuthenticator = null;
2017

2118
@Override
2219
public IBinder onBind(Intent intent) {
23-
if (!intent.getAction().equals(AccountManager.ACTION_AUTHENTICATOR_INTENT)) {
20+
if (!intent.getAction().equals(ACTION_AUTHENTICATOR_INTENT)) {
2421
return null;
2522
}
2623

2724
if (wikiAccountAuthenticator == null) {
28-
wikiAccountAuthenticator = new WikiAccountAuthenticator(this, accountUtil, mwApi);
25+
wikiAccountAuthenticator = new WikiAccountAuthenticator(this, mwApi);
2926
}
3027
return wikiAccountAuthenticator.getIBinder();
3128
}

app/src/main/java/fr/free/nrw/commons/category/CategorizationFragment.java

-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import android.content.SharedPreferences;
55
import android.os.Bundle;
66
import android.preference.PreferenceManager;
7-
import android.support.v4.app.Fragment;
87
import android.support.v7.app.AlertDialog;
98
import android.support.v7.widget.LinearLayoutManager;
109
import android.support.v7.widget.RecyclerView;
@@ -35,7 +34,6 @@
3534
import butterknife.BindView;
3635
import butterknife.ButterKnife;
3736
import dagger.android.support.DaggerFragment;
38-
import fr.free.nrw.commons.CommonsApplication;
3937
import fr.free.nrw.commons.R;
4038
import fr.free.nrw.commons.data.Category;
4139
import fr.free.nrw.commons.mwapi.MediaWikiApi;

app/src/main/java/fr/free/nrw/commons/di/CommonsApplicationComponent.java

-2
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@
88
import dagger.android.support.AndroidSupportInjectionModule;
99
import fr.free.nrw.commons.CommonsApplication;
1010
import fr.free.nrw.commons.MediaWikiImageView;
11-
import fr.free.nrw.commons.auth.WikiAccountAuthenticatorService;
1211
import fr.free.nrw.commons.contributions.ContributionsSyncAdapter;
1312
import fr.free.nrw.commons.modifications.ModificationsSyncAdapter;
14-
import fr.free.nrw.commons.upload.UploadService;
1513

1614
@Singleton
1715
@Component(modules = {

app/src/main/java/fr/free/nrw/commons/di/CommonsApplicationModule.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ public AccountUtil providesAccountUtil() {
3030

3131
@Provides
3232
@Singleton
33-
public SessionManager providesSessionManager(AccountUtil accountUtil, MediaWikiApi mediaWikiApi) {
34-
return new SessionManager(application, accountUtil, mediaWikiApi);
33+
public SessionManager providesSessionManager(MediaWikiApi mediaWikiApi) {
34+
return new SessionManager(application, mediaWikiApi);
3535
}
3636

3737
@Provides

app/src/main/java/fr/free/nrw/commons/di/ContentProviderBuilderModule.java

-3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22

33
import dagger.Module;
44
import dagger.android.ContributesAndroidInjector;
5-
import fr.free.nrw.commons.auth.LoginActivity;
6-
import fr.free.nrw.commons.auth.SignupActivity;
75
import fr.free.nrw.commons.category.CategoryContentProvider;
8-
import fr.free.nrw.commons.contributions.ContributionsActivity;
96
import fr.free.nrw.commons.contributions.ContributionsContentProvider;
107
import fr.free.nrw.commons.modifications.ModificationsContentProvider;
118

0 commit comments

Comments
 (0)