Create AccountUtil class

This commit is contained in:
addshore 2017-05-14 13:32:48 +02:00
parent a1d8008901
commit eb58a847b1
8 changed files with 77 additions and 46 deletions

View file

@ -0,0 +1,61 @@
package fr.free.nrw.commons.auth;
import android.accounts.Account;
import android.accounts.AccountAuthenticatorResponse;
import android.accounts.AccountManager;
import android.content.ContentResolver;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import fr.free.nrw.commons.CommonsApplication;
import fr.free.nrw.commons.contributions.ContributionsContentProvider;
import fr.free.nrw.commons.modifications.ModificationsContentProvider;
import timber.log.Timber;
public class AccountUtil {
public static void createAccount(@Nullable AccountAuthenticatorResponse response,
String username, String password) {
Account account = new Account(username, accountType());
boolean created = accountManager().addAccountExplicitly(account, password, null);
Timber.d("account creation " + (created ? "successful" : "failure"));
if (created) {
if (response != null) {
Bundle bundle = new Bundle();
bundle.putString(AccountManager.KEY_ACCOUNT_NAME, username);
bundle.putString(AccountManager.KEY_ACCOUNT_TYPE, accountType());
response.onResult(bundle);
}
} else {
if (response != null) {
response.onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, "");
}
Timber.d("account creation failure");
}
// FIXME: If the user turns it off, it shouldn't be auto turned back on
ContentResolver.setSyncAutomatically(account, ContributionsContentProvider.AUTHORITY, true); // Enable sync by default!
ContentResolver.setSyncAutomatically(account, ModificationsContentProvider.AUTHORITY, true); // Enable sync by default!
}
@NonNull
public static String accountType() {
return "fr.free.nrw.commons";
}
private static AccountManager accountManager() {
return AccountManager.get(app());
}
@NonNull
private static CommonsApplication app() {
return CommonsApplication.app;
}
}

View file

@ -20,8 +20,8 @@ public abstract class AuthenticatedActivity extends BaseActivity {
private String authCookie;
public AuthenticatedActivity(String accountType) {
this.accountType = accountType;
public AuthenticatedActivity() {
this.accountType = AccountUtil.accountType();
}
private class GetAuthCookieTask extends AsyncTask<Void, String, String> {

View file

@ -1,10 +1,8 @@
package fr.free.nrw.commons.auth;
import android.accounts.Account;
import android.accounts.AccountAuthenticatorResponse;
import android.accounts.AccountManager;
import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
@ -12,8 +10,6 @@ import fr.free.nrw.commons.CommonsApplication;
import fr.free.nrw.commons.EventLog;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.contributions.ContributionsActivity;
import fr.free.nrw.commons.contributions.ContributionsContentProvider;
import fr.free.nrw.commons.modifications.ModificationsContentProvider;
import timber.log.Timber;
import java.io.IOException;
@ -79,33 +75,21 @@ class LoginTask extends AsyncTask<String, String, String> {
private void handlePassResult() {
loginActivity.showSuccessToastAndDismissDialog();
Account account = new Account(username, WikiAccountAuthenticator.COMMONS_ACCOUNT_TYPE);
boolean accountCreated = AccountManager.get(loginActivity).addAccount(account, password, null);
boolean accountCreated = AccountManager.get(loginActivity).addAccountExplicitly(account, password, null);
AccountAuthenticatorResponse response = null;
Bundle extras = loginActivity.getIntent().getExtras();
if (extras != null) {
Timber.d("Bundle of extras: %s", extras);
if (accountCreated) { // Pass the new account back to the account manager
AccountAuthenticatorResponse response = extras.getParcelable(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE);
response = extras.getParcelable(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE);
if (response != null) {
Bundle authResult = new Bundle();
authResult.putString(AccountManager.KEY_ACCOUNT_NAME, username);
authResult.putString(AccountManager.KEY_ACCOUNT_TYPE, WikiAccountAuthenticator.COMMONS_ACCOUNT_TYPE);
if (response != null) {
response.onResult(authResult);
}
authResult.putString(AccountManager.KEY_ACCOUNT_TYPE, AccountUtil.accountType());
response.onResult(authResult);
}
}
// FIXME: If the user turns it off, it shouldn't be auto turned back on
ContentResolver.setSyncAutomatically(account, ContributionsContentProvider.AUTHORITY, true); // Enable sync by default!
ContentResolver.setSyncAutomatically(account, ModificationsContentProvider.AUTHORITY, true); // Enable sync by default!
AccountUtil.createAccount( response, username, password );
Intent intent = new Intent(loginActivity, ContributionsActivity.class);
loginActivity.startActivity(intent);

View file

@ -18,8 +18,6 @@ import fr.free.nrw.commons.MWApi;
public class WikiAccountAuthenticator extends AbstractAccountAuthenticator {
public static final String COMMONS_ACCOUNT_TYPE = "fr.free.nrw.commons";
private Context context;
public WikiAccountAuthenticator(Context context) {
@ -38,7 +36,7 @@ public class WikiAccountAuthenticator extends AbstractAccountAuthenticator {
}
private boolean supportedAccountType(@Nullable String type) {
return COMMONS_ACCOUNT_TYPE.equals(type);
return AccountUtil.accountType().equals(type);
}
@Override
@ -104,7 +102,7 @@ public class WikiAccountAuthenticator extends AbstractAccountAuthenticator {
if (authCookie != null) {
final Bundle result = new Bundle();
result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
result.putString(AccountManager.KEY_ACCOUNT_TYPE, COMMONS_ACCOUNT_TYPE);
result.putString(AccountManager.KEY_ACCOUNT_TYPE, AccountUtil.accountType());
result.putString(AccountManager.KEY_AUTHTOKEN, authCookie);
return result;
}