Skip to content

Commit ed22a02

Browse files
author
maskara
committed
Cusom Mediawiki api layer
1 parent 8f83bfd commit ed22a02

File tree

5 files changed

+419
-59
lines changed

5 files changed

+419
-59
lines changed

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

+33-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import static android.accounts.AccountManager.ERROR_CODE_REMOTE_EXCEPTION;
1414
import static android.accounts.AccountManager.KEY_ACCOUNT_NAME;
1515
import static android.accounts.AccountManager.KEY_ACCOUNT_TYPE;
16+
import static android.accounts.AccountManager.KEY_PASSWORD;
17+
import static android.icu.lang.UCharacter.GraphemeClusterBreak.L;
1618
import static fr.free.nrw.commons.contributions.ContributionsContentProvider.CONTRIBUTION_AUTHORITY;
1719
import static fr.free.nrw.commons.modifications.ModificationsContentProvider.MODIFICATIONS_AUTHORITY;
1820

@@ -31,7 +33,7 @@ public void createAccount(@Nullable AccountAuthenticatorResponse response,
3133
String username, String password) {
3234

3335
Account account = new Account(username, ACCOUNT_TYPE);
34-
boolean created = accountManager().addAccountExplicitly(account, password, null);
36+
boolean created = accountManager(context).addAccountExplicitly(account, password, null);
3537

3638
Timber.d("account creation " + (created ? "successful" : "failure"));
3739

@@ -40,7 +42,7 @@ public void createAccount(@Nullable AccountAuthenticatorResponse response,
4042
Bundle bundle = new Bundle();
4143
bundle.putString(KEY_ACCOUNT_NAME, username);
4244
bundle.putString(KEY_ACCOUNT_TYPE, ACCOUNT_TYPE);
43-
45+
bundle.putString(KEY_PASSWORD, password);
4446

4547
response.onResult(bundle);
4648
}
@@ -57,7 +59,35 @@ public void createAccount(@Nullable AccountAuthenticatorResponse response,
5759
ContentResolver.setSyncAutomatically(account, MODIFICATIONS_AUTHORITY, true); // Enable sync by default!
5860
}
5961

60-
private AccountManager accountManager() {
62+
/**
63+
* @return Account|null
64+
*/
65+
@Nullable
66+
public static Account account(Context context) {
67+
try {
68+
Account[] accounts = accountManager(context).getAccountsByType(ACCOUNT_TYPE);
69+
if (accounts.length > 0) {
70+
return accounts[0];
71+
}
72+
} catch (SecurityException e) {
73+
Timber.e(e);
74+
}
75+
return null;
76+
}
77+
78+
@Nullable
79+
public static String getUserName(Context context) {
80+
Account account = account(context);
81+
return account == null ? null : account.name;
82+
}
83+
84+
@Nullable
85+
public static String getPassword(Context context) {
86+
Account account = account(context);
87+
return account == null ? null : accountManager(context).getPassword(account);
88+
}
89+
90+
private static AccountManager accountManager(Context context) {
6191
return AccountManager.get(context);
6292
}
6393

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

+18-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,22 @@ public Account getCurrentAccount() {
4545
return currentAccount;
4646
}
4747

48+
@Nullable
49+
public String getUserName() {
50+
Account account = getCurrentAccount();
51+
return account == null ? null : account.name;
52+
}
53+
54+
@Nullable
55+
public String getPassword() {
56+
Account account = getCurrentAccount();
57+
return account == null ? null : accountManager().getPassword(account);
58+
}
59+
60+
private AccountManager accountManager() {
61+
return AccountManager.get(context);
62+
}
63+
4864
public Boolean revalidateAuthToken() {
4965
AccountManager accountManager = AccountManager.get(context);
5066
Account curAccount = getCurrentAccount();
@@ -53,12 +69,13 @@ public Boolean revalidateAuthToken() {
5369
return false; // This should never happen
5470
}
5571

56-
accountManager.invalidateAuthToken(ACCOUNT_TYPE, mediaWikiApi.getAuthCookie());
72+
accountManager.invalidateAuthToken(ACCOUNT_TYPE, null);
5773
String authCookie = getAuthCookie();
5874

5975
if (authCookie == null) {
6076
return false;
6177
}
78+
6279
mediaWikiApi.setAuthCookie(authCookie);
6380
return true;
6481
}

0 commit comments

Comments
 (0)