Skip to content

Commit dc51632

Browse files
Merge branch 'master' into feature/bug_fix#1793
2 parents 6004d7c + 23014e0 commit dc51632

File tree

75 files changed

+1346
-300
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+1346
-300
lines changed

app/build.gradle

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ android {
120120
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt', 'proguard-glide.txt'
121121
}
122122
debug {
123-
applicationIdSuffix ".debug"
124123
testCoverageEnabled true
125124
versionNameSuffix "-debug-" + getBranchName() + "~" + getBuildVersion()
126125
}
@@ -129,6 +128,9 @@ android {
129128
flavorDimensions 'tier'
130129
productFlavors {
131130
prod {
131+
132+
applicationId 'fr.free.nrw.commons'
133+
132134
buildConfigField "String", "WIKIMEDIA_API_POTD", "\"https://commons.wikimedia.org/w/api.php?action=featuredfeed&feed=potd&feedformat=rss&language=en\""
133135
buildConfigField "String", "WIKIMEDIA_API_HOST", "\"https://commons.wikimedia.org/w/api.php\""
134136
buildConfigField "String", "WIKIDATA_API_HOST", "\"https://www.wikidata.org/w/api.php\""
@@ -142,10 +144,18 @@ android {
142144
buildConfigField "String", "SIGNUP_LANDING_URL", "\"https://commons.m.wikimedia.org/w/index.php?title=Special:CreateAccount&returnto=Main+Page&returntoquery=welcome%3Dyes\""
143145
buildConfigField "String", "SIGNUP_SUCCESS_REDIRECTION_URL", "\"https://commons.m.wikimedia.org/w/index.php?title=Main_Page&welcome=yes\""
144146
buildConfigField "String", "FORGOT_PASSWORD_URL", "\"https://commons.wikimedia.org/wiki/Special:PasswordReset\""
147+
148+
buildConfigField "String", "ACCOUNT_TYPE", "\"fr.free.nrw.commons\""
149+
buildConfigField "String", "CONTRIBUTION_AUTHORITY", "\"fr.free.nrw.commons.contributions.contentprovider\""
150+
buildConfigField "String", "MODIFICATION_AUTHORITY", "\"fr.free.nrw.commons.modifications.contentprovider\""
151+
buildConfigField "String", "CATEGORY_AUTHORITY", "\"fr.free.nrw.commons.categories.contentprovider\""
152+
145153
dimension 'tier'
146154
}
147155

148156
beta {
157+
applicationId 'fr.free.nrw.commons.beta'
158+
149159
// What values do we need to hit the BETA versions of the site / api ?
150160
buildConfigField "String", "WIKIMEDIA_API_POTD", "\"https://commons.wikimedia.org/w/api.php?action=featuredfeed&feed=potd&feedformat=rss&language=en\""
151161
buildConfigField "String", "WIKIMEDIA_API_HOST", "\"https://commons.wikimedia.beta.wmflabs.org/w/api.php\""
@@ -160,6 +170,12 @@ android {
160170
buildConfigField "String", "SIGNUP_LANDING_URL", "\"https://commons.m.wikimedia.beta.wmflabs.org/w/index.php?title=Special:CreateAccount&returnto=Main+Page&returntoquery=welcome%3Dyes\""
161171
buildConfigField "String", "SIGNUP_SUCCESS_REDIRECTION_URL", "\"https://commons.m.wikimedia.beta.wmflabs.org/w/index.php?title=Main_Page&welcome=yes\""
162172
buildConfigField "String", "FORGOT_PASSWORD_URL", "\"https://commons.wikimedia.beta.wmflabs.org/wiki/Special:PasswordReset\""
173+
174+
buildConfigField "String", "ACCOUNT_TYPE", "\"fr.free.nrw.commons.beta\""
175+
buildConfigField "String", "CONTRIBUTION_AUTHORITY", "\"fr.free.nrw.commons.beta.contributions.contentprovider\""
176+
buildConfigField "String", "MODIFICATION_AUTHORITY", "\"fr.free.nrw.commons.beta.modifications.contentprovider\""
177+
buildConfigField "String", "CATEGORY_AUTHORITY", "\"fr.free.nrw.commons.beta.categories.contentprovider\""
178+
163179
dimension 'tier'
164180
}
165181
}

app/src/beta/res/values/adapter.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<string name="account_type">fr.free.nrw.commons.beta</string>
4+
<string name="contribution_authority">fr.free.nrw.commons.beta.contributions.contentprovider</string>
5+
<string name="modification_authority">fr.free.nrw.commons.beta.modifications.contentprovider</string>
6+
<string name="category_authority">fr.free.nrw.commons.beta.categories.contentprovider</string>
7+
</resources>

app/src/debug/res/values/placeholder_strings.xml

Lines changed: 0 additions & 6 deletions
This file was deleted.

app/src/main/AndroidManifest.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,21 +169,21 @@
169169

170170
<provider
171171
android:name=".contributions.ContributionsContentProvider"
172-
android:authorities="fr.free.nrw.commons.contributions.contentprovider"
172+
android:authorities="${applicationId}.contributions.contentprovider"
173173
android:exported="false"
174174
android:label="@string/provider_contributions"
175175
android:syncable="true" />
176176

177177
<provider
178178
android:name=".modifications.ModificationsContentProvider"
179-
android:authorities="fr.free.nrw.commons.modifications.contentprovider"
179+
android:authorities="${applicationId}.modifications.contentprovider"
180180
android:exported="false"
181181
android:label="@string/provider_modifications"
182182
android:syncable="true" />
183183

184184
<provider
185185
android:name=".category.CategoryContentProvider"
186-
android:authorities="fr.free.nrw.commons.categories.contentprovider"
186+
android:authorities="${applicationId}.categories.contentprovider"
187187
android:exported="false"
188188
android:label="@string/provider_categories"
189189
android:syncable="false" />

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import fr.free.nrw.commons.di.ApplicationlessInjection;
2828
import fr.free.nrw.commons.modifications.ModifierSequenceDao;
2929
import fr.free.nrw.commons.upload.FileUtils;
30+
import fr.free.nrw.commons.utils.ContributionUtils;
3031
import io.reactivex.android.schedulers.AndroidSchedulers;
3132
import io.reactivex.schedulers.Schedulers;
3233
import timber.log.Timber;
@@ -68,7 +69,6 @@ public class CommonsApplication extends MultiDexApplication {
6869
@Override
6970
public void onCreate() {
7071
super.onCreate();
71-
7272
ApplicationlessInjection
7373
.getInstance(this)
7474
.getCommonsApplicationComponent()
@@ -81,6 +81,8 @@ public void onCreate() {
8181
if (setupLeakCanary() == RefWatcher.DISABLED) {
8282
return;
8383
}
84+
// Empty temp directory in case some temp files are created and never removed.
85+
ContributionUtils.emptyTemporaryDirectory();
8486

8587
Timber.plant(new Timber.DebugTree());
8688

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

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,10 @@ public void setMedia(Media media) {
5151
return;
5252
}
5353

54-
if(media.getFilename() != null) {
55-
if (thumbnailUrlCache.get(media.getFilename()) != null) {
56-
setImageUrl(thumbnailUrlCache.get(media.getFilename()));
57-
} else {
58-
setImageUrl(null);
59-
currentThumbnailTask = new ThumbnailFetchTask(media, mwApi);
60-
currentThumbnailTask.execute(media.getFilename());
61-
}
62-
} else { // local image
63-
setImageUrl(media.getLocalUri().toString());
54+
if (media.getFilename() != null && thumbnailUrlCache.get(media.getFilename()) != null) {
55+
setImageUrl(thumbnailUrlCache.get(media.getFilename()));
56+
} else {
57+
setImageUrl(null);
6458
currentThumbnailTask = new ThumbnailFetchTask(media, mwApi);
6559
currentThumbnailTask.execute(media.getFilename());
6660
}

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

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,15 @@
11
package fr.free.nrw.commons.auth;
22

33
import android.accounts.Account;
4-
import android.accounts.AccountAuthenticatorResponse;
54
import android.accounts.AccountManager;
6-
import android.content.ContentResolver;
75
import android.content.Context;
8-
import android.os.Bundle;
96
import android.support.annotation.Nullable;
107

8+
import fr.free.nrw.commons.BuildConfig;
119
import timber.log.Timber;
1210

13-
import static android.accounts.AccountManager.ERROR_CODE_REMOTE_EXCEPTION;
14-
import static android.accounts.AccountManager.KEY_ACCOUNT_NAME;
15-
import static android.accounts.AccountManager.KEY_ACCOUNT_TYPE;
16-
import static fr.free.nrw.commons.contributions.ContributionsContentProvider.CONTRIBUTION_AUTHORITY;
17-
import static fr.free.nrw.commons.modifications.ModificationsContentProvider.MODIFICATIONS_AUTHORITY;
18-
1911
public class AccountUtil {
2012

21-
public static final String ACCOUNT_TYPE = "fr.free.nrw.commons";
2213
public static final String AUTH_COOKIE = "authCookie";
2314
public static final String AUTH_TOKEN_TYPE = "CommonsAndroid";
2415
private final Context context;
@@ -27,37 +18,35 @@ public AccountUtil(Context context) {
2718
this.context = context;
2819
}
2920

30-
public void createAccount(@Nullable AccountAuthenticatorResponse response,
31-
String username, String password) {
32-
33-
Account account = new Account(username, ACCOUNT_TYPE);
34-
boolean created = accountManager().addAccountExplicitly(account, password, null);
35-
36-
Timber.d("account creation " + (created ? "successful" : "failure"));
37-
38-
if (created) {
39-
if (response != null) {
40-
Bundle bundle = new Bundle();
41-
bundle.putString(KEY_ACCOUNT_NAME, username);
42-
bundle.putString(KEY_ACCOUNT_TYPE, ACCOUNT_TYPE);
43-
44-
45-
response.onResult(bundle);
21+
/**
22+
* @return Account|null
23+
*/
24+
@Nullable
25+
public static Account account(Context context) {
26+
try {
27+
Account[] accounts = accountManager(context).getAccountsByType(BuildConfig.ACCOUNT_TYPE);
28+
if (accounts.length > 0) {
29+
return accounts[0];
4630
}
47-
48-
} else {
49-
if (response != null) {
50-
response.onError(ERROR_CODE_REMOTE_EXCEPTION, "");
51-
}
52-
Timber.d("account creation failure");
31+
} catch (SecurityException e) {
32+
Timber.e(e);
5333
}
34+
return null;
35+
}
36+
37+
@Nullable
38+
public static String getUserName(Context context) {
39+
Account account = account(context);
40+
return account == null ? null : account.name;
41+
}
5442

55-
// FIXME: If the user turns it off, it shouldn't be auto turned back on
56-
ContentResolver.setSyncAutomatically(account, CONTRIBUTION_AUTHORITY, true); // Enable sync by default!
57-
ContentResolver.setSyncAutomatically(account, MODIFICATIONS_AUTHORITY, true); // Enable sync by default!
43+
@Nullable
44+
public static String getPassword(Context context) {
45+
Account account = account(context);
46+
return account == null ? null : accountManager(context).getPassword(account);
5847
}
5948

60-
private AccountManager accountManager() {
49+
private static AccountManager accountManager(Context context) {
6150
return AccountManager.get(context);
6251
}
6352

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,13 @@
5555
import static android.view.KeyEvent.KEYCODE_ENTER;
5656
import static android.view.View.VISIBLE;
5757
import static android.view.inputmethod.EditorInfo.IME_ACTION_DONE;
58-
import static fr.free.nrw.commons.auth.AccountUtil.ACCOUNT_TYPE;
5958
import static fr.free.nrw.commons.auth.AccountUtil.AUTH_TOKEN_TYPE;
6059

6160
public class LoginActivity extends AccountAuthenticatorActivity {
6261

6362
public static final String PARAM_USERNAME = "fr.free.nrw.commons.login.username";
6463

6564
@Inject MediaWikiApi mwApi;
66-
@Inject AccountUtil accountUtil;
6765
@Inject SessionManager sessionManager;
6866
@Inject @Named("application_preferences") SharedPreferences prefs;
6967
@Inject @Named("default_preferences") SharedPreferences defaultPrefs;
@@ -243,12 +241,12 @@ private void handlePassResult(String username, String password) {
243241
if (response != null) {
244242
Bundle authResult = new Bundle();
245243
authResult.putString(AccountManager.KEY_ACCOUNT_NAME, username);
246-
authResult.putString(AccountManager.KEY_ACCOUNT_TYPE, ACCOUNT_TYPE);
244+
authResult.putString(AccountManager.KEY_ACCOUNT_TYPE, BuildConfig.ACCOUNT_TYPE);
247245
response.onResult(authResult);
248246
}
249247
}
250248

251-
accountUtil.createAccount(response, username, password);
249+
sessionManager.createAccount(response, username, password);
252250
startMainActivity();
253251
}
254252

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

Lines changed: 68 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
package fr.free.nrw.commons.auth;
22

33
import android.accounts.Account;
4+
import android.accounts.AccountAuthenticatorResponse;
45
import android.accounts.AccountManager;
6+
import android.content.ContentResolver;
57
import android.content.Context;
68
import android.content.SharedPreferences;
9+
import android.os.Bundle;
710

811
import javax.annotation.Nullable;
912

13+
import fr.free.nrw.commons.BuildConfig;
1014
import fr.free.nrw.commons.mwapi.MediaWikiApi;
1115
import io.reactivex.Completable;
1216
import io.reactivex.Observable;
1317
import timber.log.Timber;
1418

15-
import static fr.free.nrw.commons.auth.AccountUtil.ACCOUNT_TYPE;
19+
import static android.accounts.AccountManager.ERROR_CODE_REMOTE_EXCEPTION;
20+
import static android.accounts.AccountManager.KEY_ACCOUNT_NAME;
21+
import static android.accounts.AccountManager.KEY_ACCOUNT_TYPE;
1622

1723
/**
1824
* Manage the current logged in user session.
@@ -23,28 +29,84 @@ public class SessionManager {
2329
private Account currentAccount; // Unlike a savings account... ;-)
2430
private SharedPreferences sharedPreferences;
2531

26-
public SessionManager(Context context, MediaWikiApi mediaWikiApi, SharedPreferences sharedPreferences) {
32+
33+
public SessionManager(Context context,
34+
MediaWikiApi mediaWikiApi,
35+
SharedPreferences sharedPreferences) {
2736
this.context = context;
2837
this.mediaWikiApi = mediaWikiApi;
2938
this.currentAccount = null;
3039
this.sharedPreferences = sharedPreferences;
3140
}
3241

42+
/**
43+
* Creata a new account
44+
*
45+
* @param response
46+
* @param username
47+
* @param password
48+
*/
49+
public void createAccount(@Nullable AccountAuthenticatorResponse response,
50+
String username, String password) {
51+
52+
Account account = new Account(username, BuildConfig.ACCOUNT_TYPE);
53+
boolean created = accountManager().addAccountExplicitly(account, password, null);
54+
55+
Timber.d("account creation " + (created ? "successful" : "failure"));
56+
57+
if (created) {
58+
if (response != null) {
59+
Bundle bundle = new Bundle();
60+
bundle.putString(KEY_ACCOUNT_NAME, username);
61+
bundle.putString(KEY_ACCOUNT_TYPE, BuildConfig.ACCOUNT_TYPE);
62+
63+
64+
response.onResult(bundle);
65+
}
66+
67+
} else {
68+
if (response != null) {
69+
response.onError(ERROR_CODE_REMOTE_EXCEPTION, "");
70+
}
71+
Timber.d("account creation failure");
72+
}
73+
74+
// FIXME: If the user turns it off, it shouldn't be auto turned back on
75+
ContentResolver.setSyncAutomatically(account, BuildConfig.CONTRIBUTION_AUTHORITY, true); // Enable sync by default!
76+
ContentResolver.setSyncAutomatically(account, BuildConfig.MODIFICATION_AUTHORITY, true); // Enable sync by default!
77+
}
78+
3379
/**
3480
* @return Account|null
3581
*/
3682
@Nullable
3783
public Account getCurrentAccount() {
3884
if (currentAccount == null) {
3985
AccountManager accountManager = AccountManager.get(context);
40-
Account[] allAccounts = accountManager.getAccountsByType(ACCOUNT_TYPE);
86+
Account[] allAccounts = accountManager.getAccountsByType(BuildConfig.ACCOUNT_TYPE);
4187
if (allAccounts.length != 0) {
4288
currentAccount = allAccounts[0];
4389
}
4490
}
4591
return currentAccount;
4692
}
4793

94+
@Nullable
95+
public String getUserName() {
96+
Account account = getCurrentAccount();
97+
return account == null ? null : account.name;
98+
}
99+
100+
@Nullable
101+
public String getPassword() {
102+
Account account = getCurrentAccount();
103+
return account == null ? null : accountManager().getPassword(account);
104+
}
105+
106+
private AccountManager accountManager() {
107+
return AccountManager.get(context);
108+
}
109+
48110
public Boolean revalidateAuthToken() {
49111
AccountManager accountManager = AccountManager.get(context);
50112
Account curAccount = getCurrentAccount();
@@ -53,12 +115,13 @@ public Boolean revalidateAuthToken() {
53115
return false; // This should never happen
54116
}
55117

56-
accountManager.invalidateAuthToken(ACCOUNT_TYPE, mediaWikiApi.getAuthCookie());
118+
accountManager.invalidateAuthToken(BuildConfig.ACCOUNT_TYPE, null);
57119
String authCookie = getAuthCookie();
58120

59121
if (authCookie == null) {
60122
return false;
61123
}
124+
62125
mediaWikiApi.setAuthCookie(authCookie);
63126
return true;
64127
}
@@ -92,7 +155,7 @@ public void forceLogin(Context context) {
92155

93156
public Completable clearAllAccounts() {
94157
AccountManager accountManager = AccountManager.get(context);
95-
Account[] allAccounts = accountManager.getAccountsByType(ACCOUNT_TYPE);
158+
Account[] allAccounts = accountManager.getAccountsByType(BuildConfig.ACCOUNT_TYPE);
96159
return Completable.fromObservable(Observable.fromArray(allAccounts)
97160
.map(a -> accountManager.removeAccount(a, null, null).getResult()))
98161
.doOnComplete(() -> currentAccount = null);

0 commit comments

Comments
 (0)