Android Open Source - unicef_gis_mobile Authenticator Activity

From Project

Back to project page unicef_gis_mobile.


The source code is released under:

MIT License

If you think the Android project unicef_gis_mobile listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.unicef.gis.ui;
/*from  w  w  w.j  a v  a2 s  .co m*/
import org.unicef.gis.R;
import org.unicef.gis.auth.Authenticator;
import org.unicef.gis.infrastructure.UnicefGisApi;

import android.accounts.Account;
import android.accounts.AccountAuthenticatorActivity;
import android.accounts.AccountManager;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class AuthenticatorActivity extends AccountAuthenticatorActivity {

  public static final String PARAM_AUTHTOKEN_TYPE = "authenticator_authtoken_type";
  public static final String PARAM_ACCOUNT_TYPE = "authenticator_account_type";
  public static final String PARAM_USER_PASS = "authenticator_user_pass";
  public static final String PARAM_NEW_ACCOUNT = "authenticator_new_account";
  public static final String PARAM_SHOULD_AUTHENTICATE = "authenticator_should_authenticate";
  private UnicefGisApi api = null;
  private AccountManager accountManager = null;
  private EditText editEmail = null;
  private EditText editPassword = null;
  private Button login = null;
  protected void onCreate(Bundle icicle) {
    editEmail = (EditText) findViewById(;
    editPassword = (EditText) findViewById(;
    login = (Button) findViewById(;
    api = new UnicefGisApi(this);
    accountManager = AccountManager.get(this);
  public void submit(View view) {
      final String email = editEmail.getText().toString();
      final String password = editPassword.getText().toString();
      final boolean shouldAuthenticate = getIntent().getExtras().getBoolean(AuthenticatorActivity.PARAM_SHOULD_AUTHENTICATE, true);
      AsyncTask<Void, Void, Intent> authentication = new AsyncTask<Void, Void, Intent>() {
          protected Intent doInBackground(Void... params) {
            String authtoken = "";
            if (shouldAuthenticate)
              authtoken = api.authenticate(email, password);
              final Intent res = new Intent();
              res.putExtra(AccountManager.KEY_ACCOUNT_NAME, email);
              res.putExtra(AccountManager.KEY_ACCOUNT_TYPE, Authenticator.ACCOUNT_TYPE);
              res.putExtra(AccountManager.KEY_AUTHTOKEN, authtoken);
              res.putExtra(PARAM_USER_PASS, password);
              return res;
          protected void onPostExecute(Intent intent) {
              finishLogin(intent, email, password);

  private void disableSubmitButton() {    
  private void reenableSubmitButton() {

  private void finishLogin(Intent intent, String email, String password) {
    String authToken = intent.getStringExtra(AccountManager.KEY_AUTHTOKEN);
    Account targetAccount = updateAccounts(email, password, authToken);
    if (shouldHaveAuthenticated() && authFailed(authToken)){
    } else {
      accountManager.setAuthToken(targetAccount, Authenticator.AUTH_TOKEN_TYPE, authToken);

  private boolean authFailed(String authToken) {
    return authToken == null || authToken.isEmpty();

  private boolean shouldHaveAuthenticated() {
    return !getIntent().getBooleanExtra(PARAM_NEW_ACCOUNT, false);

  private Account updateAccounts(String email, String password, String authToken) {
    Account targetAccount = null;
    Account[] accounts = accountManager.getAccountsByType(Authenticator.ACCOUNT_TYPE);
    for (int i = 0; i < accounts.length; i++) {
      if (accounts[i].name == email)
        targetAccount = accounts[i];
        accountManager.removeAccount(accounts[i], null, null);
    if (targetAccount == null){
      targetAccount = new Account(email, Authenticator.ACCOUNT_TYPE);
      accountManager.addAccountExplicitly(targetAccount, password, null);
    return targetAccount;

  private void finishWithAuthOk(Intent intent) {
    setResult(RESULT_OK, intent);

  private void showInvalidCredentialsFeedback() {
    TextView feedback = (TextView) findViewById(;

Java Source Code List