Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.project.bulog;
- import android.app.Activity;
- import android.app.ProgressDialog;
- import android.content.Intent;
- import android.media.Image;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.text.Editable;
- import android.text.TextWatcher;
- import android.text.method.PasswordTransformationMethod;
- import android.util.Log;
- import android.view.KeyEvent;
- import android.view.View;
- import android.view.inputmethod.EditorInfo;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.FrameLayout;
- import android.widget.ImageButton;
- import android.widget.TextView;
- import android.widget.Toast;
- import android.widget.ToggleButton;
- import com.example.project.bulog.controller.UserController;
- import com.example.project.bulog.model.ErrorBaseModel;
- import com.example.project.bulog.model.User;
- import com.example.project.bulog.utils.ProgressDialogUtil;
- import com.example.project.bulog.utils.SessionManager;
- import java.net.UnknownHostException;
- import java.security.MessageDigest;
- import java.security.NoSuchAlgorithmException;
- import java.util.HashMap;
- import java.util.Map;
- public class LoginActivity extends Activity {
- EditText password, username;
- Button btnMasuk;
- SessionManager session;
- ImageButton ibtnShow, ibtnHide;
- FrameLayout ibtnClear;
- TextView lupaPassword, daftarBaru;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
- session = new SessionManager(getApplicationContext());
- if(session.isLoggedIn()){
- Intent intent = new Intent(getApplicationContext(),MainActivity.class);
- startActivity(intent);
- finish();
- }
- password = (EditText)findViewById(R.id.input_password);
- username = (EditText)findViewById(R.id.input_nama);
- ibtnClear = (FrameLayout)findViewById(R.id.clear);
- ibtnHide = (ImageButton)findViewById(R.id.hide);
- ibtnShow = (ImageButton)findViewById(R.id.show);
- lupaPassword = (TextView)findViewById(R.id.lupaPassword);
- daftarBaru = (TextView)findViewById(R.id.daftarBaru);
- //Deteksi Inputan user di field username
- username.addTextChangedListener(new TextWatcher() {
- public void afterTextChanged(Editable s) {
- // you can call or do what you want with your EditText here
- //kalau nilai dari inputan username tidak ada maka button cleat akan menghilang
- if (username.getText().toString().length() == 0) {
- ibtnClear.setVisibility(View.INVISIBLE);
- } else {
- username.setError(null);
- }
- }
- public void beforeTextChanged(CharSequence s, int start, int count, int after) {
- }
- public void onTextChanged(CharSequence s, int start, int before, int count) {
- ibtnClear.setVisibility(View.VISIBLE);
- }
- });
- //Deteksi Inputan user di field password
- password.addTextChangedListener(new TextWatcher() {
- public void afterTextChanged(Editable s) {
- // you can call or do what you want with your EditText here
- if (password.getText().toString().length() == 0) {
- //membuat button show dan buttin hide tidak terlihat
- ibtnShow.setVisibility(View.INVISIBLE);
- ibtnHide.setVisibility(View.INVISIBLE);
- }else if(ibtnHide.getVisibility()==View.VISIBLE){
- ibtnShow.setVisibility(View.INVISIBLE);
- }else{
- ibtnShow.setVisibility(View.VISIBLE);
- }
- }
- public void beforeTextChanged(CharSequence s, int start, int count, int after) {
- }
- public void onTextChanged(CharSequence s, int start, int before, int count) {
- }
- });
- //deteksi klik dari button clear
- ibtnClear.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- //clear username
- username.setText("");
- }
- });
- //deteksi button daftar baru
- daftarBaru.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Toast.makeText(LoginActivity.this, "Daftar baru!", Toast.LENGTH_SHORT).show();
- }
- });
- //deteksi button lupa password
- lupaPassword.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Toast.makeText(LoginActivity.this, "Lupa Password!", Toast.LENGTH_SHORT).show();
- }
- });
- //deteksi klik dari button show
- ibtnShow.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- //buat field password jadi terlihat
- password.setTransformationMethod(null);
- //menampilkan button hide dan menyembunyikan button show
- ibtnShow.setVisibility(View.INVISIBLE);
- ibtnHide.setVisibility(View.VISIBLE);
- }
- });
- //deteksi button hide
- ibtnHide.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Toast.makeText(LoginActivity.this, "Clicked 2", Toast.LENGTH_SHORT).show();
- //untuk hide passwordnya
- password.setTransformationMethod(new PasswordTransformationMethod());
- //menampilkan button show dan menyembunyikan button hide
- ibtnShow.setVisibility(View.VISIBLE);
- ibtnHide.setVisibility(View.INVISIBLE);
- }
- });
- Toast.makeText(getApplicationContext(), "User Login Status: " + session.isLoggedIn(), Toast.LENGTH_LONG).show();
- btnMasuk = (Button)findViewById(R.id.btnMasuk);
- btnMasuk.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- password = (EditText)findViewById(R.id.input_password);
- username = (EditText)findViewById(R.id.input_nama);
- String passwordValue = password.getText().toString();
- String usernameValue = username.getText().toString();
- //Untuk handler
- if(!passwordValue.equals("") && !usernameValue.equals("")){
- new DoLogin().execute();
- }else if(passwordValue.equals("") && usernameValue.equals("")){
- password.setError("Harus diisi!");
- username.setError("Harus diisi!");
- }else if(passwordValue.equals("")){
- password.setError("Harus diisi");
- }else if(usernameValue.equals("")){
- username.setError("Harus diisi");
- }else{
- Toast.makeText(LoginActivity.this, "Isi data dengan benar!", Toast.LENGTH_SHORT).show();
- }
- }
- });
- }
- //enkripsi data
- public String md5(String s) {
- try {
- // Create MD5 Hash
- MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
- digest.update(s.getBytes());
- byte messageDigest[] = digest.digest();
- // Create Hex String
- StringBuffer hexString = new StringBuffer();
- for (int i=0; i<messageDigest.length; i++)
- hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
- return hexString.toString();
- } catch (NoSuchAlgorithmException e) {
- e.printStackTrace();
- }
- return "";
- }
- private class DoLogin extends AsyncTask<String, String,User> {
- private User user;
- private ErrorBaseModel errorBaseModel = new ErrorBaseModel();
- private ProgressDialogUtil pDialog;
- @Override
- protected User doInBackground(String... strings) {
- UserController controller = new UserController(LoginActivity.this);
- try{
- String valuePassword = md5(password.getText().toString());
- String valueUsername = username.getText().toString();
- Map<String, Object> param = new HashMap<>();
- param.put("username",valueUsername);
- param.put("password",valuePassword);
- user = controller.doLogin(param);
- return user;
- }catch (UnknownHostException ex){
- errorBaseModel.setStatus("No internet connection!");
- Log.e("err", ex.getMessage());
- }catch (Exception ex){
- errorBaseModel.setStatus(ex.getMessage());
- Log.e("err", ex.getMessage());
- }
- return null;
- }
- @Override
- protected void onPostExecute(User user) {
- super.onPostExecute(user);
- if(user==null){
- Toast.makeText(LoginActivity.this,errorBaseModel.getStatus(),Toast.LENGTH_LONG).show();
- }else if(user.getStatus().equals("failed")){
- Toast.makeText(LoginActivity.this,"Invalid login!",Toast.LENGTH_LONG).show();
- }else{
- session.createLoginSession(user.getUsername(),user.getUsername());
- Toast.makeText(LoginActivity.this,"Welcome, "+user.getUsername(),Toast.LENGTH_LONG).show();
- Intent intent = new Intent (getApplicationContext(), MainActivity.class);
- startActivity(intent);
- finish();
- }
- pDialog.dismiss();
- }
- @Override
- protected void onPreExecute() {
- super.onPreExecute();
- pDialog = new ProgressDialogUtil(LoginActivity.this, ProgressDialog.STYLE_SPINNER);
- pDialog.show();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement