Skip to content

Commit 25b4639

Browse files
Vivek Maskaramisaochan
authored andcommitted
Fix conflict between different product flavors (commons-app#1726)
* Fix conflict between different product flavors so that beta and prod apps could coexist * Fix category content provider
1 parent 2d831c0 commit 25b4639

21 files changed

+120
-101
lines changed

app/build.gradle

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ android {
125125
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt', 'proguard-glide.txt'
126126
}
127127
debug {
128-
applicationIdSuffix ".debug"
129128
testCoverageEnabled true
130129
versionNameSuffix "-debug-" + getBranchName() + "~" + getBuildVersion()
131130
}
@@ -134,6 +133,8 @@ android {
134133
flavorDimensions 'tier'
135134
productFlavors {
136135
prod {
136+
applicationId 'fr.free.nrw.commons'
137+
137138
buildConfigField "String", "WIKIMEDIA_API_HOST", "\"https://commons.wikimedia.org/w/api.php\""
138139
buildConfigField "String", "WIKIDATA_API_HOST", "\"https://www.wikidata.org/w/api.php\""
139140
buildConfigField "String", "WIKIMEDIA_FORGE_API_HOST", "\"https://tools.wmflabs.org/\""
@@ -146,10 +147,18 @@ android {
146147
buildConfigField "String", "SIGNUP_LANDING_URL", "\"https://commons.m.wikimedia.org/w/index.php?title=Special:CreateAccount&returnto=Main+Page&returntoquery=welcome%3Dyes\""
147148
buildConfigField "String", "SIGNUP_SUCCESS_REDIRECTION_URL", "\"https://commons.m.wikimedia.org/w/index.php?title=Main_Page&welcome=yes\""
148149
buildConfigField "String", "FORGOT_PASSWORD_URL", "\"https://commons.wikimedia.org/wiki/Special:PasswordReset\""
150+
151+
buildConfigField "String", "ACCOUNT_TYPE", "\"fr.free.nrw.commons\""
152+
buildConfigField "String", "CONTRIBUTION_AUTHORITY", "\"fr.free.nrw.commons.contributions.contentprovider\""
153+
buildConfigField "String", "MODIFICATION_AUTHORITY", "\"fr.free.nrw.commons.modifications.contentprovider\""
154+
buildConfigField "String", "CATEGORY_AUTHORITY", "\"fr.free.nrw.commons.categories.contentprovider\""
155+
149156
dimension 'tier'
150157
}
151158

152159
beta {
160+
applicationId 'fr.free.nrw.commons.beta'
161+
153162
// What values do we need to hit the BETA versions of the site / api ?
154163
buildConfigField "String", "WIKIMEDIA_API_HOST", "\"https://commons.wikimedia.beta.wmflabs.org/w/api.php\""
155164
buildConfigField "String", "WIKIDATA_API_HOST", "\"https://www.wikidata.org/w/api.php\""
@@ -163,6 +172,12 @@ android {
163172
buildConfigField "String", "SIGNUP_LANDING_URL", "\"https://commons.m.wikimedia.beta.wmflabs.org/w/index.php?title=Special:CreateAccount&returnto=Main+Page&returntoquery=welcome%3Dyes\""
164173
buildConfigField "String", "SIGNUP_SUCCESS_REDIRECTION_URL", "\"https://commons.m.wikimedia.beta.wmflabs.org/w/index.php?title=Main_Page&welcome=yes\""
165174
buildConfigField "String", "FORGOT_PASSWORD_URL", "\"https://commons.wikimedia.beta.wmflabs.org/wiki/Special:PasswordReset\""
175+
176+
buildConfigField "String", "ACCOUNT_TYPE", "\"fr.free.nrw.commons.beta\""
177+
buildConfigField "String", "CONTRIBUTION_AUTHORITY", "\"fr.free.nrw.commons.beta.contributions.contentprovider\""
178+
buildConfigField "String", "MODIFICATION_AUTHORITY", "\"fr.free.nrw.commons.beta.modifications.contentprovider\""
179+
buildConfigField "String", "CATEGORY_AUTHORITY", "\"fr.free.nrw.commons.beta.categories.contentprovider\""
180+
166181
dimension 'tier'
167182
}
168183
}
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
@@ -146,21 +146,21 @@
146146

147147
<provider
148148
android:name=".contributions.ContributionsContentProvider"
149-
android:authorities="fr.free.nrw.commons.contributions.contentprovider"
149+
android:authorities="${applicationId}.contributions.contentprovider"
150150
android:exported="false"
151151
android:label="@string/provider_contributions"
152152
android:syncable="true" />
153153

154154
<provider
155155
android:name=".modifications.ModificationsContentProvider"
156-
android:authorities="fr.free.nrw.commons.modifications.contentprovider"
156+
android:authorities="${applicationId}.modifications.contentprovider"
157157
android:exported="false"
158158
android:label="@string/provider_modifications"
159159
android:syncable="true" />
160160

161161
<provider
162162
android:name=".category.CategoryContentProvider"
163-
android:authorities="fr.free.nrw.commons.categories.contentprovider"
163+
android:authorities="${applicationId}.categories.contentprovider"
164164
android:exported="false"
165165
android:label="@string/provider_categories"
166166
android:syncable="false" />
Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,6 @@
11
package fr.free.nrw.commons.auth;
22

3-
import android.accounts.Account;
4-
import android.accounts.AccountAuthenticatorResponse;
5-
import android.accounts.AccountManager;
6-
import android.content.ContentResolver;
73
import android.content.Context;
8-
import android.os.Bundle;
9-
import android.support.annotation.Nullable;
10-
11-
import timber.log.Timber;
12-
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;
184

195
public class AccountUtil {
206

@@ -27,38 +13,4 @@ public AccountUtil(Context context) {
2713
this.context = context;
2814
}
2915

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);
46-
}
47-
48-
} else {
49-
if (response != null) {
50-
response.onError(ERROR_CODE_REMOTE_EXCEPTION, "");
51-
}
52-
Timber.d("account creation failure");
53-
}
54-
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!
58-
}
59-
60-
private AccountManager accountManager() {
61-
return AccountManager.get(context);
62-
}
63-
6416
}

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import android.accounts.AccountAuthenticatorActivity;
55
import android.accounts.AccountAuthenticatorResponse;
66
import android.accounts.AccountManager;
7-
import android.app.Activity;
87
import android.app.ProgressDialog;
98
import android.content.Context;
109
import android.content.Intent;
@@ -24,7 +23,6 @@
2423
import android.view.MenuItem;
2524
import android.view.View;
2625
import android.view.ViewGroup;
27-
import android.view.inputmethod.InputMethodManager;
2826
import android.widget.Button;
2927
import android.widget.EditText;
3028
import android.widget.TextView;
@@ -65,7 +63,6 @@ public class LoginActivity extends AccountAuthenticatorActivity {
6563
public static final String PARAM_USERNAME = "fr.free.nrw.commons.login.username";
6664

6765
@Inject MediaWikiApi mwApi;
68-
@Inject AccountUtil accountUtil;
6966
@Inject SessionManager sessionManager;
7067
@Inject @Named("application_preferences") SharedPreferences prefs;
7168
@Inject @Named("default_preferences") SharedPreferences defaultPrefs;
@@ -254,7 +251,7 @@ private void handlePassResult(String username, String password) {
254251
}
255252
}
256253

257-
accountUtil.createAccount(response, username, password);
254+
sessionManager.createAccount(response, username, password);
258255
startMainActivity();
259256
}
260257

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

Lines changed: 55 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,21 +29,65 @@ 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+
79+
private AccountManager accountManager() {
80+
return AccountManager.get(context);
81+
}
82+
3383
/**
3484
* @return Account|null
3585
*/
3686
@Nullable
3787
public Account getCurrentAccount() {
3888
if (currentAccount == null) {
3989
AccountManager accountManager = AccountManager.get(context);
40-
Account[] allAccounts = accountManager.getAccountsByType(ACCOUNT_TYPE);
90+
Account[] allAccounts = accountManager.getAccountsByType(BuildConfig.ACCOUNT_TYPE);
4191
if (allAccounts.length != 0) {
4292
currentAccount = allAccounts[0];
4393
}
@@ -53,7 +103,7 @@ public Boolean revalidateAuthToken() {
53103
return false; // This should never happen
54104
}
55105

56-
accountManager.invalidateAuthToken(ACCOUNT_TYPE, mediaWikiApi.getAuthCookie());
106+
accountManager.invalidateAuthToken(BuildConfig.ACCOUNT_TYPE, mediaWikiApi.getAuthCookie());
57107
String authCookie = getAuthCookie();
58108

59109
if (authCookie == null) {
@@ -92,7 +142,7 @@ public void forceLogin(Context context) {
92142

93143
public Completable clearAllAccounts() {
94144
AccountManager accountManager = AccountManager.get(context);
95-
Account[] allAccounts = accountManager.getAccountsByType(ACCOUNT_TYPE);
145+
Account[] allAccounts = accountManager.getAccountsByType(BuildConfig.ACCOUNT_TYPE);
96146
return Completable.fromObservable(Observable.fromArray(allAccounts)
97147
.map(a -> accountManager.removeAccount(a, null, null).getResult()))
98148
.doOnComplete(() -> currentAccount = null);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@
1212
import android.support.annotation.NonNull;
1313
import android.support.annotation.Nullable;
1414

15+
import fr.free.nrw.commons.BuildConfig;
1516
import fr.free.nrw.commons.contributions.ContributionsContentProvider;
1617
import fr.free.nrw.commons.modifications.ModificationsContentProvider;
1718

1819
import static fr.free.nrw.commons.auth.AccountUtil.ACCOUNT_TYPE;
1920
import static fr.free.nrw.commons.auth.AccountUtil.AUTH_TOKEN_TYPE;
2021

2122
public class WikiAccountAuthenticator extends AbstractAccountAuthenticator {
22-
private static final String[] SYNC_AUTHORITIES = {ContributionsContentProvider.CONTRIBUTION_AUTHORITY, ModificationsContentProvider.MODIFICATIONS_AUTHORITY};
23+
private static final String[] SYNC_AUTHORITIES = {BuildConfig.CONTRIBUTION_AUTHORITY, BuildConfig.MODIFICATION_AUTHORITY};
2324

2425
@NonNull
2526
private final Context context;

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import javax.inject.Inject;
1313

14+
import fr.free.nrw.commons.BuildConfig;
1415
import fr.free.nrw.commons.data.DBOpenHelper;
1516
import fr.free.nrw.commons.di.CommonsDaggerContentProvider;
1617
import timber.log.Timber;
@@ -22,19 +23,18 @@
2223

2324
public class CategoryContentProvider extends CommonsDaggerContentProvider {
2425

25-
public static final String AUTHORITY = "fr.free.nrw.commons.categories.contentprovider";
2626
// For URI matcher
2727
private static final int CATEGORIES = 1;
2828
private static final int CATEGORIES_ID = 2;
2929
private static final String BASE_PATH = "categories";
3030

31-
public static final Uri BASE_URI = Uri.parse("content://" + AUTHORITY + "/" + BASE_PATH);
31+
public static final Uri BASE_URI = Uri.parse("content://" + BuildConfig.CATEGORY_AUTHORITY + "/" + BASE_PATH);
3232

3333
private static final UriMatcher uriMatcher = new UriMatcher(NO_MATCH);
3434

3535
static {
36-
uriMatcher.addURI(AUTHORITY, BASE_PATH, CATEGORIES);
37-
uriMatcher.addURI(AUTHORITY, BASE_PATH + "/#", CATEGORIES_ID);
36+
uriMatcher.addURI(BuildConfig.CATEGORY_AUTHORITY, BASE_PATH, CATEGORIES);
37+
uriMatcher.addURI(BuildConfig.CATEGORY_AUTHORITY, BASE_PATH + "/#", CATEGORIES_ID);
3838
}
3939

4040
public static Uri uriForId(int id) {

app/src/main/java/fr/free/nrw/commons/contributions/ContributionsActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ protected void onResume() {
107107
@Override
108108
protected void onAuthCookieAcquired(String authCookie) {
109109
// Do a sync everytime we get here!
110-
requestSync(sessionManager.getCurrentAccount(), ContributionsContentProvider.CONTRIBUTION_AUTHORITY, new Bundle());
110+
requestSync(sessionManager.getCurrentAccount(), BuildConfig.CONTRIBUTION_AUTHORITY, new Bundle());
111111
Intent uploadServiceIntent = new Intent(this, UploadService.class);
112112
uploadServiceIntent.setAction(UploadService.ACTION_START_SERVICE);
113113
startService(uploadServiceIntent);

0 commit comments

Comments
 (0)