diff --git a/AndroidManifest.xml b/AndroidManifest.xml index e73336ac8..a0a565851 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -10,7 +10,8 @@ - + + { + + private Account account; + private AccountManager accountManager; + public GetAuthCookieTask(Account account, AccountManager accountManager) { + this.account = account; + this.accountManager = accountManager; + } + + @Override + protected void onPostExecute(String result) { + super.onPostExecute(result); + if(result != null) { + onAuthCookieAcquired(result); + } else { + onAuthFailure(); + } + } + + @Override + protected String doInBackground(String... params) { + try { + return accountManager.blockingGetAuthToken(account, "", false); + } catch (OperationCanceledException e) { + e.printStackTrace(); + return null; + } catch (AuthenticatorException e) { + e.printStackTrace(); + return null; + } catch (IOException e) { + e.printStackTrace(); + return null; + } + } + + } +// private class onTokenAcquired implements AccountManagerCallback { +// +// @Override +// public void run(AccountManagerFuture result) { +// Bundle bundle; +// try { +// bundle = result.getResult(); +// } catch (OperationCanceledException e) { +// // TODO Auto-generated catch block +// e.printStackTrace(); +// throw new RuntimeException(e); +// } catch (AuthenticatorException e) { +// // TODO Auto-generated catch block +// e.printStackTrace(); +// throw new RuntimeException(e); +// } catch (IOException e) { +// // TODO Auto-generated catch block +// e.printStackTrace(); +// throw new RuntimeException(e); +// } +// Log.d("Commons", "Token Found!"); +// if(bundle.containsKey(AccountManager.KEY_AUTHTOKEN)) { +// String authCookie = bundle.getString(AccountManager.KEY_AUTHTOKEN); +// onAuthCookieAcquired(authCookie); +// } else { +// if(bundle.containsKey(AccountManager.KEY_INTENT)) { +// Intent launchIntent = (Intent) bundle.get(AccountManager.KEY_INTENT); +// startActivityForResult(launchIntent, 0); +// } else { +// +// } +// } +// +// } +// } +// +// +// @Override +// protected void onActivityResult(int requestCode, int resultCode, Intent data) { +// super.onActivityResult(requestCode, resultCode, data); +// Log.d("Commons", "Result of the loginactivity!"); +// if(resultCode == Activity.RESULT_OK) { +// requestAuthToken(); +// } +// } +// +// private void requestAuthToken() { +// AccountManager accountManager = AccountManager.get(this); +// Account[] allAccounts =accountManager.getAccountsByType(accountType); +// if(allAccounts.length == 0) { +// Log.d("Commons", "No accounts yet!"); +// // No Commons Accounts yet! +// accountManager.addAccount(WikiAccountAuthenticator.COMMONS_ACCOUNT_TYPE, "", null, null, this, new onTokenAcquired(), null); +// return; +// } +// +// Log.d("Commons", "Accounts found!"); +// // For now - always pick the first account +// Account curAccount = allAccounts[0]; +// Bundle cookieOptions = new Bundle(); +// accountManager.getAuthToken(curAccount, "", cookieOptions, this, new onTokenAcquired(), null); +// } + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + AccountManager accountManager = AccountManager.get(this); + Account[] allAccounts =accountManager.getAccountsByType(accountType); + if(allAccounts.length == 0) { + // No Commons Accounts yet! + accountManager.addAccount(WikiAccountAuthenticator.COMMONS_ACCOUNT_TYPE, "", null, null, this, null, null); + return; + } + + // For now - always pick the first account + Account curAccount = allAccounts[0]; + GetAuthCookieTask task = new GetAuthCookieTask(curAccount, accountManager); + task.execute(""); + } + protected void onAuthCookieAcquired(String authCookie) { + + } + protected void onAuthFailure() { + + } +} diff --git a/src/org/wikimedia/commons/auth/WikiAccountAuthenticator.java b/src/org/wikimedia/commons/auth/WikiAccountAuthenticator.java index 1da6d5259..692e93969 100644 --- a/src/org/wikimedia/commons/auth/WikiAccountAuthenticator.java +++ b/src/org/wikimedia/commons/auth/WikiAccountAuthenticator.java @@ -14,6 +14,7 @@ import android.accounts.NetworkErrorException; import android.content.Context; import android.content.Intent; import android.os.Bundle; +import android.util.Log; public class WikiAccountAuthenticator extends AbstractAccountAuthenticator { @@ -47,7 +48,7 @@ public class WikiAccountAuthenticator extends AbstractAccountAuthenticator { } private String getAuthCookie(String username, String password) throws IOException { - MWApi api = ((CommonsApplication)context.getApplicationContext()).createMWApi(); + MWApi api = CommonsApplication.createMWApi(); String result = api.login(username, password); if(result.equals("Success")) { return api.getAuthCookie(); @@ -57,14 +58,6 @@ public class WikiAccountAuthenticator extends AbstractAccountAuthenticator { } @Override public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options) throws NetworkErrorException { - // If the caller requested an authToken type we don't support, then - // return an error - if (!authTokenType.equals(COMMONS_ACCOUNT_TYPE)) { - final Bundle result = new Bundle(); - result.putString(AccountManager.KEY_ERROR_MESSAGE, "invalid authTokenType"); - return result; - } - // Extract the username and password from the Account Manager, and ask // the server for an appropriate AuthToken. final AccountManager am = AccountManager.get(context); @@ -75,6 +68,7 @@ public class WikiAccountAuthenticator extends AbstractAccountAuthenticator { authCookie = getAuthCookie(account.name, password); } catch (IOException e) { // Network error! + e.printStackTrace(); throw new NetworkErrorException(e); } if (authCookie != null) {