Skip to content

Commit e22a1a5

Browse files
author
maskara
committed
Cusom Mediawiki api layer
1 parent d29aa2e commit e22a1a5

10 files changed

+431
-72
lines changed
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

+1-2
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

+18-5
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

+1-4
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) {

0 commit comments

Comments
 (0)