Keep state about currently selected account in application

This commit is contained in:
YuviPanda 2012-11-03 03:53:38 +05:30
parent 11685e5ec1
commit 3857a18cf2
2 changed files with 33 additions and 6 deletions

View file

@ -18,6 +18,7 @@ import org.apache.http.params.CoreProtocolPNames;
public class CommonsApplication extends Application { public class CommonsApplication extends Application {
private MWApi api; private MWApi api;
private Account currentAccount = null; // Unlike a savings account...
public static final String API_URL = "http://test.wikipedia.org/w/api.php"; public static final String API_URL = "http://test.wikipedia.org/w/api.php";
public static MWApi createMWApi() { public static MWApi createMWApi() {
@ -42,10 +43,24 @@ public class CommonsApplication extends Application {
return api; return api;
} }
public Account getCurrentAccount() {
if(currentAccount == null) {
AccountManager accountManager = AccountManager.get(this);
Account[] allAccounts = accountManager.getAccountsByType(WikiAccountAuthenticator.COMMONS_ACCOUNT_TYPE);
if(allAccounts.length != 0) {
currentAccount = allAccounts[0];
}
}
return currentAccount;
}
public Boolean revalidateAuthToken() { public Boolean revalidateAuthToken() {
AccountManager accountManager = AccountManager.get(this); AccountManager accountManager = AccountManager.get(this);
Account[] allAccounts =accountManager.getAccountsByType(WikiAccountAuthenticator.COMMONS_ACCOUNT_TYPE); Account curAccount = getCurrentAccount();
Account curAccount = allAccounts[0];
if(curAccount == null) {
return false; // This should never happen
}
accountManager.invalidateAuthToken(WikiAccountAuthenticator.COMMONS_ACCOUNT_TYPE, api.getAuthCookie()); accountManager.invalidateAuthToken(WikiAccountAuthenticator.COMMONS_ACCOUNT_TYPE, api.getAuthCookie());
try { try {

View file

@ -2,6 +2,8 @@ package org.wikimedia.commons.auth;
import java.io.IOException; import java.io.IOException;
import org.wikimedia.commons.CommonsApplication;
import android.accounts.*; import android.accounts.*;
import android.app.Activity; import android.app.Activity;
import android.content.*; import android.content.*;
@ -14,6 +16,8 @@ public class AuthenticatedActivity extends Activity {
String accountType; String accountType;
CommonsApplication app;
public AuthenticatedActivity(String accountType) { public AuthenticatedActivity(String accountType) {
this.accountType = accountType; this.accountType = accountType;
} }
@ -102,9 +106,8 @@ public class AuthenticatedActivity extends Activity {
} }
protected void requestAuthToken() { protected void requestAuthToken() {
AccountManager accountManager = AccountManager.get(this); AccountManager accountManager = AccountManager.get(this);
Account[] allAccounts =accountManager.getAccountsByType(accountType); Account curAccount = app.getCurrentAccount();
Account curAccount = null; if(curAccount == null) {
if(allAccounts.length == 0) {
AddAccountTask addAccountTask = new AddAccountTask(accountManager); AddAccountTask addAccountTask = new AddAccountTask(accountManager);
// This AsyncTask blocks until the Login Activity returns // This AsyncTask blocks until the Login Activity returns
// And since in Android 4.x+ only one background thread runs all AsyncTasks // And since in Android 4.x+ only one background thread runs all AsyncTasks
@ -114,12 +117,21 @@ public class AuthenticatedActivity extends Activity {
// See: https://groups.google.com/forum/?fromgroups=#!topic/android-developers/8M0RTFfO7-M // See: https://groups.google.com/forum/?fromgroups=#!topic/android-developers/8M0RTFfO7-M
addAccountTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); addAccountTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else { } else {
curAccount = allAccounts[0];
GetAuthCookieTask task = new GetAuthCookieTask(curAccount, accountManager); GetAuthCookieTask task = new GetAuthCookieTask(curAccount, accountManager);
task.execute(); task.execute();
} }
} }
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
app = (CommonsApplication)this.getApplicationContext();
}
protected void onAuthCookieAcquired(String authCookie) { protected void onAuthCookieAcquired(String authCookie) {
} }