Skip to content

Commit 23014e0

Browse files
committed
Merge remote-tracking branch 'refs/remotes/origin/2.8-release'
2 parents 6f86299 + 143ad00 commit 23014e0

13 files changed

+459
-85
lines changed
Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
package fr.free.nrw.commons.auth;
22

3+
import android.accounts.Account;
4+
import android.accounts.AccountManager;
35
import android.content.Context;
6+
import android.support.annotation.Nullable;
7+
8+
import fr.free.nrw.commons.BuildConfig;
9+
import timber.log.Timber;
410

511
public class AccountUtil {
612

7-
public static final String ACCOUNT_TYPE = "fr.free.nrw.commons";
813
public static final String AUTH_COOKIE = "authCookie";
914
public static final String AUTH_TOKEN_TYPE = "CommonsAndroid";
1015
private final Context context;
@@ -13,4 +18,36 @@ public AccountUtil(Context context) {
1318
this.context = context;
1419
}
1520

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];
30+
}
31+
} catch (SecurityException e) {
32+
Timber.e(e);
33+
}
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+
}
42+
43+
@Nullable
44+
public static String getPassword(Context context) {
45+
Account account = account(context);
46+
return account == null ? null : accountManager(context).getPassword(account);
47+
}
48+
49+
private static AccountManager accountManager(Context context) {
50+
return AccountManager.get(context);
51+
}
52+
1653
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
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 {
@@ -242,7 +241,7 @@ private void handlePassResult(String username, String password) {
242241
if (response != null) {
243242
Bundle authResult = new Bundle();
244243
authResult.putString(AccountManager.KEY_ACCOUNT_NAME, username);
245-
authResult.putString(AccountManager.KEY_ACCOUNT_TYPE, ACCOUNT_TYPE);
244+
authResult.putString(AccountManager.KEY_ACCOUNT_TYPE, BuildConfig.ACCOUNT_TYPE);
246245
response.onResult(authResult);
247246
}
248247
}

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,6 @@ public void createAccount(@Nullable AccountAuthenticatorResponse response,
7676
ContentResolver.setSyncAutomatically(account, BuildConfig.MODIFICATION_AUTHORITY, true); // Enable sync by default!
7777
}
7878

79-
private AccountManager accountManager() {
80-
return AccountManager.get(context);
81-
}
82-
8379
/**
8480
* @return Account|null
8581
*/
@@ -95,6 +91,22 @@ public Account getCurrentAccount() {
9591
return currentAccount;
9692
}
9793

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+
98110
public Boolean revalidateAuthToken() {
99111
AccountManager accountManager = AccountManager.get(context);
100112
Account curAccount = getCurrentAccount();
@@ -103,12 +115,13 @@ public Boolean revalidateAuthToken() {
103115
return false; // This should never happen
104116
}
105117

106-
accountManager.invalidateAuthToken(BuildConfig.ACCOUNT_TYPE, mediaWikiApi.getAuthCookie());
118+
accountManager.invalidateAuthToken(BuildConfig.ACCOUNT_TYPE, null);
107119
String authCookie = getAuthCookie();
108120

109121
if (authCookie == null) {
110122
return false;
111123
}
124+
112125
mediaWikiApi.setAuthCookie(authCookie);
113126
return true;
114127
}

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@
1313
import android.support.annotation.Nullable;
1414

1515
import fr.free.nrw.commons.BuildConfig;
16-
import fr.free.nrw.commons.contributions.ContributionsContentProvider;
17-
import fr.free.nrw.commons.modifications.ModificationsContentProvider;
1816

19-
import static fr.free.nrw.commons.auth.AccountUtil.ACCOUNT_TYPE;
2017
import static fr.free.nrw.commons.auth.AccountUtil.AUTH_TOKEN_TYPE;
2118

2219
public class WikiAccountAuthenticator extends AbstractAccountAuthenticator {
@@ -99,7 +96,7 @@ public Bundle hasFeatures(@NonNull AccountAuthenticatorResponse response,
9996
}
10097

10198
private boolean supportedAccountType(@Nullable String type) {
102-
return ACCOUNT_TYPE.equals(type);
99+
return BuildConfig.ACCOUNT_TYPE.equals(type);
103100
}
104101

105102
private Bundle addAccount(AccountAuthenticatorResponse response) {

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public void startGalleryPick() {
9595

9696
public void handleImagePicked(int requestCode, @Nullable Uri uri, boolean isDirectUpload, String wikiDataEntityId) {
9797
FragmentActivity activity = fragment.getActivity();
98-
Timber.d("handleImagePicked() called with onActivityResult()");
98+
Timber.d("handleImagePicked() called with onActivityResult(). Boolean isDirectUpload: " + isDirectUpload + "String wikiDataEntityId: " + wikiDataEntityId);
9999
Intent shareIntent = new Intent(activity, ShareActivity.class);
100100
shareIntent.setAction(ACTION_SEND);
101101
switch (requestCode) {
@@ -113,21 +113,26 @@ public void handleImagePicked(int requestCode, @Nullable Uri uri, boolean isDire
113113
shareIntent.setType("image/jpeg");
114114
shareIntent.putExtra(EXTRA_STREAM, lastGeneratedCaptureUri);
115115
shareIntent.putExtra(EXTRA_SOURCE, SOURCE_CAMERA);
116-
117116
break;
118117
default:
119118
break;
120119
}
120+
121121
Timber.i("Image selected");
122+
shareIntent.putExtra("isDirectUpload", isDirectUpload);
123+
Timber.d("Put extras into image intent, isDirectUpload is " + isDirectUpload);
124+
122125
try {
123-
shareIntent.putExtra("isDirectUpload", isDirectUpload);
124126
if (wikiDataEntityId != null && !wikiDataEntityId.equals("")) {
125127
shareIntent.putExtra(WIKIDATA_ENTITY_ID_PREF, wikiDataEntityId);
126128
}
127-
activity.startActivity(shareIntent);
128129
} catch (SecurityException e) {
129130
Timber.e(e, "Security Exception");
130131
}
132+
133+
if (activity != null) {
134+
activity.startActivity(shareIntent);
135+
}
131136
}
132137

133138
void saveState(Bundle outState) {

0 commit comments

Comments
 (0)