Android Open Source - on-the-hook Login Activity






From Project

Back to project page on-the-hook.

License

The source code is released under:

MIT License

If you think the Android project on-the-hook 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 com.yoandinkov.onthehook;
/*  w  w  w.  j a va 2s .c o m*/
import org.apache.http.HttpStatus;

import com.yoandinkov.onthehook.crypt.JsonConverter;
import com.yoandinkov.onthehook.crypt.SHA1Converter;
import com.yoandinkov.onthehook.models.HttpResponseWrapper;
import com.yoandinkov.onthehook.models.Login;
import com.yoandinkov.onthehook.models.Response;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;

public class LoginActivity extends RequestActivity{

  private Button ButtonLogin;
  private Button ButtonRegister;
  private EditText EditTextUsername;
  private String usernameInput;
  private ProgressDialog dialog;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
  
    this.setButtonLogin((Button)findViewById(R.id.loginButtonLogin));
    this.setButtonRegister((Button)findViewById(R.id.loginButtonRegister));
    this.setEditTextUsername((EditText)findViewById(R.id.loginEditTextUsername));
    
    this.getButtonLogin().setOnClickListener(this);
    this.getButtonRegister().setOnClickListener(this);
    
    String sessionKey = this.getCredentials();
    
    if(sessionKey != null) {
      this.startMainMenuActivity(sessionKey);
    }
  }
  
  @Override
  protected void onResume() {
    super.onResume();
    
    String input = this.getUsernameInput();
    this.getEditTextUsername().setText(input);
  }
  
  @Override
  protected void onPause() {
    super.onPause();
    
    String input = this.getEditTextUsername().getText().toString();
    this.setUsernameInput(input);
  }
  
  @Override
  public void onClick(View clickedView) {
    int clickedViewId = clickedView.getId();
    
    switch(clickedViewId) {
    case R.id.loginButtonLogin :
      this.buttonLoginClick(clickedView);
      break;
    case R.id.loginButtonRegister :
      this.buttonRegisterClick(clickedView);
      break;
    default:
      // TODO implement
      break;
    }
  }

  private void buttonLoginClick(View currentView) {
    Login currentLogin = new Login();
    
    String authKey = SHA1Converter
        .encryptPassword(((EditText) findViewById(R.id.loginEditTextPassword))
            .getText().toString());
    String username = this.getEditTextUsername().getText().toString();
    
    currentLogin.setAuthKey(authKey);
    currentLogin.setUsername(username);
    
    this.logUser(currentLogin);
  }
  
  protected void logUser(Login login) {
    String loginAsJson = JsonConverter.toJson(login);
    String url = this.getApiUrl() + "/auth/login";
    
    this.setDialog(ProgressDialog.show(this, "Logging ..", ""));
    
    HttpPostRequester currentRequest = new HttpPostRequester();
    
    currentRequest.execute(url, loginAsJson);
  }
  
  @Override
  protected void handleResponse(HttpResponseWrapper responseWrapper) {
    Response currentResponse = responseWrapper.getResponse();
    
    if (currentResponse.getStatusCode() == HttpStatus.SC_OK) {
      String sessionKey = currentResponse.getBody();
            
      this.startMainMenuActivity(sessionKey);
    } else {
      this.dialog.hide();
      
      AlertDialog.Builder errorDisplay = new AlertDialog.Builder(this);
      
      errorDisplay.setTitle("Error")
        .setMessage(currentResponse.getBody())
        .setPositiveButton("OK",null);
        
        errorDisplay.show();
    }  
  }
  
  private String getCredentials() {
    String result = "";
    
    SharedPreferences credentials = getSharedPreferences("credentials", MODE_PRIVATE);
    
    result = credentials.getString("sessionKey", null);
    
    return result;
  }
    
  private void startMainMenuActivity(String sessionKey) {
    Intent startMainMenuIntention = new Intent(this, MainMenuActivity.class);
    
    startMainMenuIntention.putExtra("sessionKey", sessionKey);
    
    startActivity(startMainMenuIntention);
    
    finish();
  }
  
  // RegisterActivity redirection
  private void buttonRegisterClick(View currentView) {
    Intent startRegisterActivity = new Intent(this, RegisterActivity.class);
    
    startActivity(startRegisterActivity);
  }
  
  // Auto-generated getters/setters
  public Button getButtonLogin() {
    return ButtonLogin;
  }

  public void setButtonLogin(Button buttonLogin) {
    ButtonLogin = buttonLogin;
  }

  public Button getButtonRegister() {
    return ButtonRegister;
  }

  public void setButtonRegister(Button buttonRegister) {
    ButtonRegister = buttonRegister;
  }

  public EditText getEditTextUsername() {
    return EditTextUsername;
  }

  public void setEditTextUsername(EditText editTextUsername) {
    EditTextUsername = editTextUsername;
  }

  public String getUsernameInput() {
    return usernameInput;
  }

  public void setUsernameInput(String usernameInput) {
    this.usernameInput = usernameInput;
  }

  public ProgressDialog getDialog() {
    return dialog;
  }

  public void setDialog(ProgressDialog dialog) {
    this.dialog = dialog;
  }

}




Java Source Code List

com.yoandinkov.onthehook.DraughtActivity.java
com.yoandinkov.onthehook.FishActivity.java
com.yoandinkov.onthehook.FishingActivity.java
com.yoandinkov.onthehook.LoginActivity.java
com.yoandinkov.onthehook.MainMenuActivity.java
com.yoandinkov.onthehook.RegisterActivity.java
com.yoandinkov.onthehook.RequestActivity.java
com.yoandinkov.onthehook.adapters.FishAdapter.java
com.yoandinkov.onthehook.crypt.JsonConverter.java
com.yoandinkov.onthehook.crypt.SHA1Converter.java
com.yoandinkov.onthehook.db.DatabaseHelper.java
com.yoandinkov.onthehook.db.DatabaseManager.java
com.yoandinkov.onthehook.db.models.FishDbModel.java
com.yoandinkov.onthehook.models.Coordinates.java
com.yoandinkov.onthehook.models.Credentials.java
com.yoandinkov.onthehook.models.FishButton.java
com.yoandinkov.onthehook.models.FishSpecies.java
com.yoandinkov.onthehook.models.Fish.java
com.yoandinkov.onthehook.models.Fishing.java
com.yoandinkov.onthehook.models.HttpResponseWrapper.java
com.yoandinkov.onthehook.models.HttpType.java
com.yoandinkov.onthehook.models.Login.java
com.yoandinkov.onthehook.models.NewFishing.java
com.yoandinkov.onthehook.models.Registration.java
com.yoandinkov.onthehook.models.RequestPurpose.java
com.yoandinkov.onthehook.models.Response.java
com.yoandinkov.onthehook.models.WaterLocation.java