Back to project page on-the-hook.
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.
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; } }