Browse files

Update account credentials when 401 occurs

This starts the LoginActivity when a RequestException occurs
in ThrowableLoader that will cause the credentials to be updated
and the data to be re-requested
  • Loading branch information...
1 parent 8367ca8 commit 643b7d7ae85f4dc8d393498a7bcfe04713578eae @kevinsawicki kevinsawicki committed Aug 8, 2012
View
26 app/src/main/java/com/github/mobile/ThrowableLoader.java
@@ -15,11 +15,19 @@
*/
package com.github.mobile;
+import static java.net.HttpURLConnection.HTTP_UNAUTHORIZED;
+import android.accounts.Account;
+import android.accounts.AccountsException;
import android.content.Context;
import android.util.Log;
+import com.github.mobile.accounts.AccountUtils;
import com.github.mobile.accounts.AuthenticatedUserLoader;
+import java.io.IOException;
+
+import org.eclipse.egit.github.core.client.RequestException;
+
/**
* Loader that support throwing an exception when loading in the background
*
@@ -51,11 +59,27 @@ protected D getAccountFailureData() {
}
@Override
- public D load() {
+ public D load(final Account account) {
exception = null;
try {
return loadData();
} catch (Exception e) {
+ if (e instanceof RequestException
+ && HTTP_UNAUTHORIZED == ((RequestException) e).getStatus())
+ try {
+ AccountUtils.updateAccount(account, activity);
+ try {
+ return loadData();
+ } catch (Exception ignored) {
+ Log.d(TAG, "Exception loading data", e);
+ exception = e;
+ return data;
+ }
+ } catch (IOException ignored) {
+ // Ignored
+ } catch (AccountsException ignored) {
+ // Ignored
+ }
Log.d(TAG, "Exception loading data", e);
exception = e;
return data;
View
13 app/src/main/java/com/github/mobile/accounts/AccountAuthenticator.java
@@ -18,15 +18,17 @@
import static android.accounts.AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE;
import static android.accounts.AccountManager.KEY_BOOLEAN_RESULT;
import static android.accounts.AccountManager.KEY_INTENT;
-import static com.github.mobile.accounts.LoginActivity.PARAM_AUTHTOKEN_TYPE;
import static com.github.mobile.accounts.AccountConstants.ACCOUNT_TYPE;
+import static com.github.mobile.accounts.LoginActivity.PARAM_AUTHTOKEN_TYPE;
+import static com.github.mobile.accounts.LoginActivity.PARAM_USERNAME;
import android.accounts.AbstractAccountAuthenticator;
import android.accounts.Account;
import android.accounts.AccountAuthenticatorResponse;
import android.accounts.NetworkErrorException;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
+import android.text.TextUtils;
class AccountAuthenticator extends AbstractAccountAuthenticator {
@@ -94,6 +96,13 @@ public Bundle hasFeatures(AccountAuthenticatorResponse response,
@Override
public Bundle updateCredentials(AccountAuthenticatorResponse response,
Account account, String authTokenType, Bundle options) {
- return null;
+ final Intent intent = new Intent(context, LoginActivity.class);
+ intent.putExtra(PARAM_AUTHTOKEN_TYPE, authTokenType);
+ intent.putExtra(KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
+ if (!TextUtils.isEmpty(account.name))
+ intent.putExtra(PARAM_USERNAME, account.name);
+ final Bundle bundle = new Bundle();
+ bundle.putParcelable(KEY_INTENT, intent);
+ return bundle;
}
}
View
37 app/src/main/java/com/github/mobile/accounts/AccountUtils.java
@@ -218,6 +218,43 @@ public void run() {
}
/**
+ * Update account
+ *
+ * @param account
+ * @param activity
+ * @throws IOException
+ * @throws AccountsException
+ */
+ public static void updateAccount(final Account account,
+ final Activity activity) throws IOException, AccountsException {
+ AccountManager manager = AccountManager.get(activity);
+ try {
+ if (!hasAuthenticator(manager))
+ throw new AuthenticatorConflictException();
+ manager.updateCredentials(account, ACCOUNT_TYPE, null, activity,
+ null, null).getResult();
+ } catch (OperationCanceledException e) {
+ Log.d(TAG, "Excepting retrieving account", e);
+ activity.finish();
+ throw e;
+ } catch (AccountsException e) {
+ Log.d(TAG, "Excepting retrieving account", e);
+ throw e;
+ } catch (AuthenticatorConflictException e) {
+ activity.runOnUiThread(new Runnable() {
+
+ public void run() {
+ showConflictMessage(activity);
+ }
+ });
+ throw e;
+ } catch (IOException e) {
+ Log.d(TAG, "Excepting retrieving account", e);
+ throw e;
+ }
+ }
+
+ /**
* Show conflict message about previously registered authenticator from
* another application
*
View
7 app/src/main/java/com/github/mobile/accounts/AuthenticatedUserLoader.java
@@ -31,7 +31,7 @@
/**
* Base loader class that ensures an authenticated account exists before
- * {@link #load()} is called
+ * {@link #load(Account)} is called
*
* @param <D>
*/
@@ -83,7 +83,7 @@ public final D loadInBackground() {
try {
contextScope.enter(getContext());
try {
- return load();
+ return load(account);
} finally {
contextScope.exit(getContext());
}
@@ -95,7 +95,8 @@ public final D loadInBackground() {
/**
* Load data
*
+ * @param account
* @return data
*/
- public abstract D load();
+ public abstract D load(Account account);
}
View
12 app/src/main/java/com/github/mobile/accounts/LoginActivity.java
@@ -86,9 +86,12 @@
*/
public static final String PARAM_AUTHTOKEN_TYPE = "authtokenType";
- private static final String PARAM_CONFIRMCREDENTIALS = "confirmCredentials";
+ /**
+ * Initial user name
+ */
+ public static final String PARAM_USERNAME = "username";
- private static final String PARAM_USERNAME = "username";
+ private static final String PARAM_CONFIRMCREDENTIALS = "confirmCredentials";
private static final String TAG = "LoginActivity";
@@ -172,6 +175,11 @@ public void onCreate(Bundle savedInstanceState) {
signupText.setMovementMethod(LinkMovementMethod.getInstance());
signupText.setText(Html.fromHtml(getString(string.signup_link)));
+ if (!TextUtils.isEmpty(username)) {
+ loginText.setText(username);
+ loginText.setEnabled(false);
+ }
+
TextWatcher watcher = new TextWatcherAdapter() {
@Override
View
3 app/src/main/java/com/github/mobile/ui/repo/OrganizationLoader.java
@@ -15,6 +15,7 @@
*/
package com.github.mobile.ui.repo;
+import android.accounts.Account;
import android.app.Activity;
import android.util.Log;
@@ -66,7 +67,7 @@ public OrganizationLoader(Activity activity,
}
@Override
- public List<User> load() {
+ public List<User> load(final Account account) {
List<User> orgs;
try {
orgs = accountDataManager.getOrgs();

0 comments on commit 643b7d7

Please sign in to comment.