Android Open Source - unicef_gis_mobile Authenticator Activity






From Project

Back to project page unicef_gis_mobile.

License

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;
  
  @Override
  protected void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.activity_login);
    
    editEmail = (EditText) findViewById(R.id.edit_email);
    editPassword = (EditText) findViewById(R.id.edit_password);
    login = (Button) findViewById(R.id.login_button);
    
    api = new UnicefGisApi(this);
    accountManager = AccountManager.get(this);
  }
  
  
  public void submit(View view) {
    disableSubmitButton();
    
      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>() {
          @Override
          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;
          }
          @Override
          protected void onPostExecute(Intent intent) {
              finishLogin(intent, email, password);
          }
      };
      
      authentication.execute();      
  }

  private void disableSubmitButton() {    
    login.setText(R.string.saving_account_details);
    login.setEnabled(false);
  }
  
  private void reenableSubmitButton() {
    login.setText(R.string.button_login);
    login.setEnabled(true);
  }


  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)){
      showInvalidCredentialsFeedback();          
    } else {
      accountManager.setAuthToken(targetAccount, Authenticator.AUTH_TOKEN_TYPE, authToken);
      finishWithAuthOk(intent);  
    }
        
    reenableSubmitButton();
  }


  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];
      else
        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) {
    setAccountAuthenticatorResult(intent.getExtras());
    setResult(RESULT_OK, intent);
    finish();
  }

  private void showInvalidCredentialsFeedback() {
    TextView feedback = (TextView) findViewById(R.id.message);
    feedback.setText(R.string.please_enter_valid_email_and_password);
  }
}




Java Source Code List

com.couchbase.cblite.ektorp.CBLiteHttpClient.java
com.couchbase.cblite.ektorp.CBLiteHttpResponse.java
edu.mit.mobile.android.utils.StreamUtils.java
org.unicef.gis.auth.AuthenticatorService.java
org.unicef.gis.auth.Authenticator.java
org.unicef.gis.infrastructure.CompileTimeSettings.java
org.unicef.gis.infrastructure.ILocationServiceConsumer.java
org.unicef.gis.infrastructure.LocationService.java
org.unicef.gis.infrastructure.Network.java
org.unicef.gis.infrastructure.Notificator.java
org.unicef.gis.infrastructure.RoutesResolver.java
org.unicef.gis.infrastructure.ServerUrlPreferenceNotSetException.java
org.unicef.gis.infrastructure.UnicefGisApi.java
org.unicef.gis.infrastructure.data.CouchDbLiteStoreAdapter.java
org.unicef.gis.infrastructure.data.UnicefGisContentProvider.java
org.unicef.gis.infrastructure.data.UnicefGisStore.java
org.unicef.gis.infrastructure.image.AsyncDrawable.java
org.unicef.gis.infrastructure.image.BitmapWorkerTask.java
org.unicef.gis.infrastructure.image.Camera.java
org.unicef.gis.model.Report.java
org.unicef.gis.model.Tag.java
org.unicef.gis.model.couchdb.NullReduce.java
org.unicef.gis.model.couchdb.ReportLoader.java
org.unicef.gis.model.couchdb.views.AllReportsByTimestampDesc.java
org.unicef.gis.model.couchdb.views.PendingSyncReports.java
org.unicef.gis.model.couchdb.views.UnicefGisView.java
org.unicef.gis.model.couchdb.views.UploadedReports.java
org.unicef.gis.sync.SyncAdapter.java
org.unicef.gis.sync.SyncService.java
org.unicef.gis.ui.AlertDialogFragment.java
org.unicef.gis.ui.AuthenticatorActivity.java
org.unicef.gis.ui.ConfigureServerUrlActivity.java
org.unicef.gis.ui.DeleteUploadedReportsTask.java
org.unicef.gis.ui.FetchTagsActivity.java
org.unicef.gis.ui.FetchTagsTask.java
org.unicef.gis.ui.MyReportsActivity.java
org.unicef.gis.ui.SettingsActivity.java
org.unicef.gis.ui.SettingsFragment.java
org.unicef.gis.ui.report.ChooseTagsFragment.java
org.unicef.gis.ui.report.CreateReportActivityConstants.java
org.unicef.gis.ui.report.CreateReportActivity.java
org.unicef.gis.ui.report.GetTagsTaskFragment.java
org.unicef.gis.ui.report.GetTagsTask.java
org.unicef.gis.ui.report.IChooseTagsCallbacks.java
org.unicef.gis.ui.report.IGetTagsCallback.java
org.unicef.gis.ui.report.IGetTagsTaskFragmentCallbacks.java
org.unicef.gis.ui.report.IReportSummaryCallbacks.java
org.unicef.gis.ui.report.ReportRowAdapter.java
org.unicef.gis.ui.report.ReportSummaryFragment.java
org.unicef.gis.ui.report.ReportViewModel.java
org.unicef.gis.ui.report.ToggleTagAdapter.java