Advertisement
Guest User

java

a guest
May 18th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.18 KB | None | 0 0
  1. package com.example.project.bulog;
  2.  
  3. import android.app.Activity;
  4. import android.app.ProgressDialog;
  5. import android.content.Intent;
  6. import android.media.Image;
  7. import android.os.AsyncTask;
  8. import android.os.Bundle;
  9. import android.text.Editable;
  10. import android.text.TextWatcher;
  11. import android.text.method.PasswordTransformationMethod;
  12. import android.util.Log;
  13. import android.view.KeyEvent;
  14. import android.view.View;
  15. import android.view.inputmethod.EditorInfo;
  16. import android.widget.Button;
  17. import android.widget.EditText;
  18. import android.widget.FrameLayout;
  19. import android.widget.ImageButton;
  20. import android.widget.TextView;
  21. import android.widget.Toast;
  22. import android.widget.ToggleButton;
  23.  
  24. import com.example.project.bulog.controller.UserController;
  25. import com.example.project.bulog.model.ErrorBaseModel;
  26. import com.example.project.bulog.model.User;
  27. import com.example.project.bulog.utils.ProgressDialogUtil;
  28. import com.example.project.bulog.utils.SessionManager;
  29.  
  30. import java.net.UnknownHostException;
  31. import java.security.MessageDigest;
  32. import java.security.NoSuchAlgorithmException;
  33. import java.util.HashMap;
  34. import java.util.Map;
  35.  
  36.  
  37. public class LoginActivity extends Activity {
  38.  
  39.  
  40. EditText password, username;
  41. Button btnMasuk;
  42. SessionManager session;
  43. ImageButton ibtnShow, ibtnHide;
  44. FrameLayout ibtnClear;
  45. TextView lupaPassword, daftarBaru;
  46.  
  47.  
  48. @Override
  49. protected void onCreate(Bundle savedInstanceState) {
  50. super.onCreate(savedInstanceState);
  51. setContentView(R.layout.activity_login);
  52.  
  53.  
  54. session = new SessionManager(getApplicationContext());
  55.  
  56. if(session.isLoggedIn()){
  57. Intent intent = new Intent(getApplicationContext(),MainActivity.class);
  58. startActivity(intent);
  59. finish();
  60. }
  61.  
  62. password = (EditText)findViewById(R.id.input_password);
  63. username = (EditText)findViewById(R.id.input_nama);
  64. ibtnClear = (FrameLayout)findViewById(R.id.clear);
  65. ibtnHide = (ImageButton)findViewById(R.id.hide);
  66. ibtnShow = (ImageButton)findViewById(R.id.show);
  67. lupaPassword = (TextView)findViewById(R.id.lupaPassword);
  68. daftarBaru = (TextView)findViewById(R.id.daftarBaru);
  69.  
  70. //Deteksi Inputan user di field username
  71. username.addTextChangedListener(new TextWatcher() {
  72.  
  73. public void afterTextChanged(Editable s) {
  74.  
  75. // you can call or do what you want with your EditText here
  76. //kalau nilai dari inputan username tidak ada maka button cleat akan menghilang
  77. if (username.getText().toString().length() == 0) {
  78. ibtnClear.setVisibility(View.INVISIBLE);
  79. } else {
  80. username.setError(null);
  81. }
  82.  
  83. }
  84.  
  85. public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  86.  
  87. }
  88.  
  89. public void onTextChanged(CharSequence s, int start, int before, int count) {
  90. ibtnClear.setVisibility(View.VISIBLE);
  91. }
  92. });
  93.  
  94. //Deteksi Inputan user di field password
  95. password.addTextChangedListener(new TextWatcher() {
  96.  
  97. public void afterTextChanged(Editable s) {
  98.  
  99. // you can call or do what you want with your EditText here
  100. if (password.getText().toString().length() == 0) {
  101.  
  102. //membuat button show dan buttin hide tidak terlihat
  103. ibtnShow.setVisibility(View.INVISIBLE);
  104. ibtnHide.setVisibility(View.INVISIBLE);
  105. }else if(ibtnHide.getVisibility()==View.VISIBLE){
  106. ibtnShow.setVisibility(View.INVISIBLE);
  107. }else{
  108. ibtnShow.setVisibility(View.VISIBLE);
  109. }
  110.  
  111. }
  112.  
  113. public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  114. }
  115.  
  116. public void onTextChanged(CharSequence s, int start, int before, int count) {
  117.  
  118. }
  119. });
  120.  
  121. //deteksi klik dari button clear
  122. ibtnClear.setOnClickListener(new View.OnClickListener() {
  123. @Override
  124. public void onClick(View v) {
  125.  
  126. //clear username
  127. username.setText("");
  128. }
  129. });
  130.  
  131.  
  132. //deteksi button daftar baru
  133. daftarBaru.setOnClickListener(new View.OnClickListener() {
  134. @Override
  135. public void onClick(View v) {
  136. Toast.makeText(LoginActivity.this, "Daftar baru!", Toast.LENGTH_SHORT).show();
  137. }
  138. });
  139.  
  140. //deteksi button lupa password
  141. lupaPassword.setOnClickListener(new View.OnClickListener() {
  142. @Override
  143. public void onClick(View v) {
  144. Toast.makeText(LoginActivity.this, "Lupa Password!", Toast.LENGTH_SHORT).show();
  145. }
  146.  
  147. });
  148.  
  149. //deteksi klik dari button show
  150. ibtnShow.setOnClickListener(new View.OnClickListener() {
  151. @Override
  152. public void onClick(View v) {
  153.  
  154.  
  155. //buat field password jadi terlihat
  156. password.setTransformationMethod(null);
  157.  
  158. //menampilkan button hide dan menyembunyikan button show
  159. ibtnShow.setVisibility(View.INVISIBLE);
  160. ibtnHide.setVisibility(View.VISIBLE);
  161. }
  162. });
  163.  
  164. //deteksi button hide
  165. ibtnHide.setOnClickListener(new View.OnClickListener() {
  166. @Override
  167. public void onClick(View v) {
  168. Toast.makeText(LoginActivity.this, "Clicked 2", Toast.LENGTH_SHORT).show();
  169.  
  170. //untuk hide passwordnya
  171. password.setTransformationMethod(new PasswordTransformationMethod());
  172.  
  173. //menampilkan button show dan menyembunyikan button hide
  174. ibtnShow.setVisibility(View.VISIBLE);
  175. ibtnHide.setVisibility(View.INVISIBLE);
  176. }
  177. });
  178.  
  179.  
  180.  
  181.  
  182. Toast.makeText(getApplicationContext(), "User Login Status: " + session.isLoggedIn(), Toast.LENGTH_LONG).show();
  183.  
  184.  
  185.  
  186. btnMasuk = (Button)findViewById(R.id.btnMasuk);
  187. btnMasuk.setOnClickListener(new View.OnClickListener() {
  188. @Override
  189. public void onClick(View v) {
  190. password = (EditText)findViewById(R.id.input_password);
  191. username = (EditText)findViewById(R.id.input_nama);
  192. String passwordValue = password.getText().toString();
  193. String usernameValue = username.getText().toString();
  194.  
  195. //Untuk handler
  196. if(!passwordValue.equals("") && !usernameValue.equals("")){
  197. new DoLogin().execute();
  198. }else if(passwordValue.equals("") && usernameValue.equals("")){
  199. password.setError("Harus diisi!");
  200. username.setError("Harus diisi!");
  201. }else if(passwordValue.equals("")){
  202. password.setError("Harus diisi");
  203. }else if(usernameValue.equals("")){
  204. username.setError("Harus diisi");
  205. }else{
  206. Toast.makeText(LoginActivity.this, "Isi data dengan benar!", Toast.LENGTH_SHORT).show();
  207. }
  208.  
  209.  
  210.  
  211. }
  212. });
  213. }
  214.  
  215.  
  216. //enkripsi data
  217. public String md5(String s) {
  218. try {
  219. // Create MD5 Hash
  220. MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
  221. digest.update(s.getBytes());
  222. byte messageDigest[] = digest.digest();
  223.  
  224. // Create Hex String
  225. StringBuffer hexString = new StringBuffer();
  226. for (int i=0; i<messageDigest.length; i++)
  227. hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
  228. return hexString.toString();
  229.  
  230. } catch (NoSuchAlgorithmException e) {
  231. e.printStackTrace();
  232. }
  233. return "";
  234. }
  235.  
  236. private class DoLogin extends AsyncTask<String, String,User> {
  237. private User user;
  238. private ErrorBaseModel errorBaseModel = new ErrorBaseModel();
  239. private ProgressDialogUtil pDialog;
  240. @Override
  241. protected User doInBackground(String... strings) {
  242. UserController controller = new UserController(LoginActivity.this);
  243. try{
  244. String valuePassword = md5(password.getText().toString());
  245. String valueUsername = username.getText().toString();
  246.  
  247. Map<String, Object> param = new HashMap<>();
  248. param.put("username",valueUsername);
  249. param.put("password",valuePassword);
  250. user = controller.doLogin(param);
  251. return user;
  252. }catch (UnknownHostException ex){
  253. errorBaseModel.setStatus("No internet connection!");
  254. Log.e("err", ex.getMessage());
  255. }catch (Exception ex){
  256. errorBaseModel.setStatus(ex.getMessage());
  257. Log.e("err", ex.getMessage());
  258. }
  259. return null;
  260. }
  261.  
  262. @Override
  263. protected void onPostExecute(User user) {
  264. super.onPostExecute(user);
  265. if(user==null){
  266. Toast.makeText(LoginActivity.this,errorBaseModel.getStatus(),Toast.LENGTH_LONG).show();
  267. }else if(user.getStatus().equals("failed")){
  268. Toast.makeText(LoginActivity.this,"Invalid login!",Toast.LENGTH_LONG).show();
  269. }else{
  270. session.createLoginSession(user.getUsername(),user.getUsername());
  271. Toast.makeText(LoginActivity.this,"Welcome, "+user.getUsername(),Toast.LENGTH_LONG).show();
  272. Intent intent = new Intent (getApplicationContext(), MainActivity.class);
  273. startActivity(intent);
  274. finish();
  275. }
  276. pDialog.dismiss();
  277. }
  278.  
  279. @Override
  280. protected void onPreExecute() {
  281. super.onPreExecute();
  282. pDialog = new ProgressDialogUtil(LoginActivity.this, ProgressDialog.STYLE_SPINNER);
  283. pDialog.show();
  284. }
  285.  
  286. }
  287. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement