From 1a288176bba2aa7ec81d0e647e925e82fe315ae3 Mon Sep 17 00:00:00 2001 From: addshore Date: Sun, 14 May 2017 11:37:36 +0200 Subject: [PATCH] Cleanup WikiAccountAuthenticator logic Some of this logic comes from the wikipedia app, and basically makes sure that we return 'nice' objects everywhere rather than lots of null. There is one note left in the class about the getAuthTokenLabel method and the fact that we should probably return something there --- .../auth/WikiAccountAuthenticator.java | 74 +++++++++++++++---- 1 file changed, 59 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/fr/free/nrw/commons/auth/WikiAccountAuthenticator.java b/app/src/main/java/fr/free/nrw/commons/auth/WikiAccountAuthenticator.java index c134af4da..295de0f8c 100644 --- a/app/src/main/java/fr/free/nrw/commons/auth/WikiAccountAuthenticator.java +++ b/app/src/main/java/fr/free/nrw/commons/auth/WikiAccountAuthenticator.java @@ -11,35 +11,69 @@ import android.os.Bundle; import java.io.IOException; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import fr.free.nrw.commons.CommonsApplication; 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) { super(context); this.context = context; } + private Bundle unsupportedOperation() { + Bundle bundle = new Bundle(); + bundle.putInt(AccountManager.KEY_ERROR_CODE, AccountManager.ERROR_CODE_UNSUPPORTED_OPERATION); + + // HACK: the docs indicate that this is a required key bit it's not displayed to the user. + bundle.putString(AccountManager.KEY_ERROR_MESSAGE, ""); + + return bundle; + } + + private boolean supportedAccountType(@Nullable String type) { + return COMMONS_ACCOUNT_TYPE.equals(type); + } + @Override - public Bundle addAccount(AccountAuthenticatorResponse response, String accountType, String authTokenType, String[] requiredFeatures, Bundle options) throws NetworkErrorException { - final Intent intent = new Intent(context, LoginActivity.class); - intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response); - final Bundle bundle = new Bundle(); - bundle.putParcelable(AccountManager.KEY_INTENT, intent); + public Bundle addAccount(@NonNull AccountAuthenticatorResponse response, + @NonNull String accountType, @Nullable String authTokenType, + @Nullable String[] requiredFeatures, @Nullable Bundle options) + throws NetworkErrorException { + + if (!supportedAccountType(accountType)) { + return unsupportedOperation(); + } + + return addAccount(response); + } + + private Bundle addAccount(AccountAuthenticatorResponse response) { + Intent Intent = new Intent(context, LoginActivity.class); + Intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response); + + Bundle bundle = new Bundle(); + bundle.putParcelable(AccountManager.KEY_INTENT, Intent); + return bundle; } @Override - public Bundle confirmCredentials(AccountAuthenticatorResponse response, Account account, Bundle options) throws NetworkErrorException { - return null; + public Bundle confirmCredentials(@NonNull AccountAuthenticatorResponse response, + @NonNull Account account, @Nullable Bundle options) + throws NetworkErrorException { + return unsupportedOperation(); } @Override public Bundle editProperties(AccountAuthenticatorResponse response, String accountType) { - return null; + return unsupportedOperation(); } private String getAuthCookie(String username, String password) throws IOException { @@ -87,21 +121,31 @@ public class WikiAccountAuthenticator extends AbstractAccountAuthenticator { return bundle; } + @Nullable @Override - public String getAuthTokenLabel(String authTokenType) { + public String getAuthTokenLabel(@NonNull String authTokenType) { + //Note: the wikipedia app actually returns a string here.... + //return supportedAccountType(authTokenType) ? context.getString(R.string.wikimedia) : null; return null; } + @Nullable @Override - public Bundle hasFeatures(AccountAuthenticatorResponse response, Account account, String[] features) throws NetworkErrorException { - final Bundle result = new Bundle(); - result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, false); - return result; + public Bundle hasFeatures(@NonNull AccountAuthenticatorResponse response, + @NonNull Account account, @NonNull String[] features) + throws NetworkErrorException { + Bundle bundle = new Bundle(); + bundle.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, false); + return bundle; } + @Nullable @Override - public Bundle updateCredentials(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options) throws NetworkErrorException { - return null; + public Bundle updateCredentials(@NonNull AccountAuthenticatorResponse response, + @NonNull Account account, @Nullable String authTokenType, + @Nullable Bundle options) + throws NetworkErrorException { + return unsupportedOperation(); } }