Skip to content

Fix failed uploads #1790

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion app/src/main/java/fr/free/nrw/commons/auth/AccountUtil.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package fr.free.nrw.commons.auth;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.Context;
import android.support.annotation.Nullable;

import fr.free.nrw.commons.BuildConfig;
import timber.log.Timber;

public class AccountUtil {

public static final String ACCOUNT_TYPE = "fr.free.nrw.commons";
public static final String AUTH_COOKIE = "authCookie";
public static final String AUTH_TOKEN_TYPE = "CommonsAndroid";
private final Context context;
Expand All @@ -13,4 +18,36 @@ public AccountUtil(Context context) {
this.context = context;
}

/**
* @return Account|null
*/
@Nullable
public static Account account(Context context) {
try {
Account[] accounts = accountManager(context).getAccountsByType(BuildConfig.ACCOUNT_TYPE);
if (accounts.length > 0) {
return accounts[0];
}
} catch (SecurityException e) {
Timber.e(e);
}
return null;
}

@Nullable
public static String getUserName(Context context) {
Account account = account(context);
return account == null ? null : account.name;
}

@Nullable
public static String getPassword(Context context) {
Account account = account(context);
return account == null ? null : accountManager(context).getPassword(account);
}

private static AccountManager accountManager(Context context) {
return AccountManager.get(context);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
import static android.view.KeyEvent.KEYCODE_ENTER;
import static android.view.View.VISIBLE;
import static android.view.inputmethod.EditorInfo.IME_ACTION_DONE;
import static fr.free.nrw.commons.auth.AccountUtil.ACCOUNT_TYPE;
import static fr.free.nrw.commons.auth.AccountUtil.AUTH_TOKEN_TYPE;

public class LoginActivity extends AccountAuthenticatorActivity {
Expand Down Expand Up @@ -242,7 +241,7 @@ private void handlePassResult(String username, String password) {
if (response != null) {
Bundle authResult = new Bundle();
authResult.putString(AccountManager.KEY_ACCOUNT_NAME, username);
authResult.putString(AccountManager.KEY_ACCOUNT_TYPE, ACCOUNT_TYPE);
authResult.putString(AccountManager.KEY_ACCOUNT_TYPE, BuildConfig.ACCOUNT_TYPE);
response.onResult(authResult);
}
}
Expand Down
23 changes: 18 additions & 5 deletions app/src/main/java/fr/free/nrw/commons/auth/SessionManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,6 @@ public void createAccount(@Nullable AccountAuthenticatorResponse response,
ContentResolver.setSyncAutomatically(account, BuildConfig.MODIFICATION_AUTHORITY, true); // Enable sync by default!
}

private AccountManager accountManager() {
return AccountManager.get(context);
}

/**
* @return Account|null
*/
Expand All @@ -95,6 +91,22 @@ public Account getCurrentAccount() {
return currentAccount;
}

@Nullable
public String getUserName() {
Account account = getCurrentAccount();
return account == null ? null : account.name;
}

@Nullable
public String getPassword() {
Account account = getCurrentAccount();
return account == null ? null : accountManager().getPassword(account);
}

private AccountManager accountManager() {
return AccountManager.get(context);
}

public Boolean revalidateAuthToken() {
AccountManager accountManager = AccountManager.get(context);
Account curAccount = getCurrentAccount();
Expand All @@ -103,12 +115,13 @@ public Boolean revalidateAuthToken() {
return false; // This should never happen
}

accountManager.invalidateAuthToken(BuildConfig.ACCOUNT_TYPE, mediaWikiApi.getAuthCookie());
accountManager.invalidateAuthToken(BuildConfig.ACCOUNT_TYPE, null);
String authCookie = getAuthCookie();

if (authCookie == null) {
return false;
}

mediaWikiApi.setAuthCookie(authCookie);
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@
import android.support.annotation.Nullable;

import fr.free.nrw.commons.BuildConfig;
import fr.free.nrw.commons.contributions.ContributionsContentProvider;
import fr.free.nrw.commons.modifications.ModificationsContentProvider;

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

public class WikiAccountAuthenticator extends AbstractAccountAuthenticator {
Expand Down Expand Up @@ -99,7 +96,7 @@ public Bundle hasFeatures(@NonNull AccountAuthenticatorResponse response,
}

private boolean supportedAccountType(@Nullable String type) {
return ACCOUNT_TYPE.equals(type);
return BuildConfig.ACCOUNT_TYPE.equals(type);
}

private Bundle addAccount(AccountAuthenticatorResponse response) {
Expand Down
Loading