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 {
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 MWApi createMWApi() {
@ -42,10 +43,24 @@ public class CommonsApplication extends Application {
return api;
}
public Boolean revalidateAuthToken() {
public Account getCurrentAccount() {
if(currentAccount == null) {
AccountManager accountManager = AccountManager.get(this);
Account[] allAccounts = accountManager.getAccountsByType(WikiAccountAuthenticator.COMMONS_ACCOUNT_TYPE);
Account curAccount = allAccounts[0];
if(allAccounts.length != 0) {
currentAccount = allAccounts[0];
}
}
return currentAccount;
}
public Boolean revalidateAuthToken() {
AccountManager accountManager = AccountManager.get(this);
Account curAccount = getCurrentAccount();
if(curAccount == null) {
return false; // This should never happen
}
accountManager.invalidateAuthToken(WikiAccountAuthenticator.COMMONS_ACCOUNT_TYPE, api.getAuthCookie());
try {

View file

@ -2,6 +2,8 @@ package org.wikimedia.commons.auth;
import java.io.IOException;
import org.wikimedia.commons.CommonsApplication;
import android.accounts.*;
import android.app.Activity;
import android.content.*;
@ -14,6 +16,8 @@ public class AuthenticatedActivity extends Activity {
String accountType;
CommonsApplication app;
public AuthenticatedActivity(String accountType) {
this.accountType = accountType;
}
@ -102,9 +106,8 @@ public class AuthenticatedActivity extends Activity {
}
protected void requestAuthToken() {
AccountManager accountManager = AccountManager.get(this);
Account[] allAccounts =accountManager.getAccountsByType(accountType);
Account curAccount = null;
if(allAccounts.length == 0) {
Account curAccount = app.getCurrentAccount();
if(curAccount == null) {
AddAccountTask addAccountTask = new AddAccountTask(accountManager);
// This AsyncTask blocks until the Login Activity returns
// 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
addAccountTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
curAccount = allAccounts[0];
GetAuthCookieTask task = new GetAuthCookieTask(curAccount, accountManager);
task.execute();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
app = (CommonsApplication)this.getApplicationContext();
}
protected void onAuthCookieAcquired(String authCookie) {
}