PageRenderTime 55ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/app/src/main/java/com/example/pariwisata/transaksi/LoginActivity.java

https://gitlab.com/christianwp/pariwisata
Java | 254 lines | 231 code | 23 blank | 0 comment | 25 complexity | 884202f9e3ef6e711a72f63b229ea3ef MD5 | raw file
  1. package com.example.pariwisata.transaksi;
  2. import android.Manifest;
  3. import android.app.ProgressDialog;
  4. import android.content.Intent;
  5. import android.content.pm.PackageManager;
  6. import android.os.Build;
  7. import android.os.Bundle;
  8. import android.view.View;
  9. import android.view.WindowManager;
  10. import android.widget.Button;
  11. import android.widget.TextView;
  12. import android.widget.Toast;
  13. import com.example.pariwisata.R;
  14. import com.example.pariwisata.service.BaseApiService;
  15. import com.example.pariwisata.utilities.Link;
  16. import com.example.pariwisata.utilities.PrefUtil;
  17. import com.example.pariwisata.utilities.RequestPermissionHandler;
  18. import com.example.pariwisata.utilities.Utils;
  19. import com.google.android.material.textfield.TextInputEditText;
  20. import com.google.android.material.textfield.TextInputLayout;
  21. import org.json.JSONException;
  22. import org.json.JSONObject;
  23. import java.io.IOException;
  24. import java.text.DateFormat;
  25. import java.text.SimpleDateFormat;
  26. import java.util.ArrayList;
  27. import java.util.List;
  28. import androidx.appcompat.app.AppCompatActivity;
  29. import androidx.core.content.ContextCompat;
  30. import okhttp3.ResponseBody;
  31. import retrofit2.Call;
  32. import retrofit2.Callback;
  33. import retrofit2.Response;
  34. public class LoginActivity extends AppCompatActivity {
  35. private ProgressDialog pDialog;
  36. private DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  37. private BaseApiService mApiService;
  38. private PrefUtil prefUtil;
  39. private RequestPermissionHandler mRequestPermissionHandler;
  40. private List<String> listPermissionsNeeded;
  41. private Button btnLogin, btnClearUser;
  42. private TextInputEditText eUserID, ePassword;
  43. private TextInputLayout inputLayoutUser, inputLayoutPasw;
  44. private TextView txtSignUp;
  45. @Override
  46. protected void onCreate( Bundle savedInstanceState) {
  47. super.onCreate(savedInstanceState);
  48. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  49. mRequestPermissionHandler = new RequestPermissionHandler();
  50. checkAndRequestPermissions();
  51. openPermission();
  52. }
  53. setContentView(R.layout.login);
  54. mApiService = Link.getAPIService();
  55. prefUtil = new PrefUtil(this);
  56. pDialog = new ProgressDialog(this);
  57. pDialog.setCancelable(false);
  58. btnLogin = (Button) findViewById(R.id.bLoginLogin);
  59. btnClearUser = (Button) findViewById(R.id.btnLoginClearUser);
  60. inputLayoutUser = (TextInputLayout)findViewById(R.id.input_layout_login_userid);
  61. inputLayoutPasw = (TextInputLayout)findViewById(R.id.input_layout_login_sandi);
  62. eUserID = (TextInputEditText)findViewById(R.id.eLoginUserID);
  63. ePassword = (TextInputEditText)findViewById(R.id.eLoginSandi);
  64. txtSignUp = (TextView) findViewById(R.id.txtLoginSignUp);
  65. btnLogin.setOnClickListener(new View.OnClickListener() {
  66. @Override
  67. public void onClick(View v) {
  68. if(validateUser() && validatePasw(ePassword.length())){
  69. requestLogin(eUserID.getText().toString(), ePassword.getText().toString());
  70. }
  71. }
  72. });
  73. btnClearUser.setOnClickListener(new View.OnClickListener() {
  74. @Override
  75. public void onClick(View v) {
  76. eUserID.setText("");
  77. btnClearUser.setVisibility(View.INVISIBLE);
  78. }
  79. });
  80. txtSignUp.setOnClickListener(new View.OnClickListener() {
  81. @Override
  82. public void onClick(View v) {
  83. startActivityForResult(new Intent(LoginActivity.this, RegisterActivity.class),3);
  84. overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
  85. }
  86. });
  87. }
  88. private void checkAndRequestPermissions() {
  89. int writeExtStorage = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
  90. int readExtStorage = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);
  91. int lokasi = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION);
  92. int lokasi2 = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
  93. listPermissionsNeeded = new ArrayList<>();
  94. if (writeExtStorage != PackageManager.PERMISSION_GRANTED) {
  95. listPermissionsNeeded.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
  96. }
  97. if (readExtStorage != PackageManager.PERMISSION_GRANTED) {
  98. listPermissionsNeeded.add(Manifest.permission.READ_EXTERNAL_STORAGE);
  99. }
  100. if (lokasi != PackageManager.PERMISSION_GRANTED) {
  101. listPermissionsNeeded.add(Manifest.permission.ACCESS_COARSE_LOCATION);
  102. }
  103. if (lokasi2 != PackageManager.PERMISSION_GRANTED) {
  104. listPermissionsNeeded.add(Manifest.permission.ACCESS_FINE_LOCATION);
  105. }
  106. }
  107. private void openPermission(){
  108. if (!listPermissionsNeeded.isEmpty()) {
  109. mRequestPermissionHandler.requestPermission(this,
  110. listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]),
  111. 123, new RequestPermissionHandler.RequestPermissionListener() {
  112. @Override
  113. public void onSuccess() {
  114. Toast.makeText(LoginActivity.this, "Request permission success", Toast.LENGTH_SHORT).show();
  115. }
  116. @Override
  117. public void onFailed() {
  118. Toast.makeText(LoginActivity.this, "Request permission failed", Toast.LENGTH_SHORT).show();
  119. }
  120. });
  121. }
  122. }
  123. private void requestLogin(String id, final String pasw){
  124. pDialog.setMessage("Login ...\nHarap Tunggu");
  125. showDialog();
  126. mApiService.loginRequest(id, pasw, "LOGIN")
  127. .enqueue(new Callback<ResponseBody>() {
  128. @Override
  129. public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
  130. if (response.isSuccessful()){
  131. try {
  132. JSONObject jsonRESULTS = new JSONObject(response.body().string());
  133. if (jsonRESULTS.getString("value").equals("false")){
  134. String nama = jsonRESULTS.getJSONObject("user").getString("vc_username")==null?"":
  135. jsonRESULTS.getJSONObject("user").getString("vc_username");
  136. String uId = jsonRESULTS.getJSONObject("user").getString("c_userid")==null?"":
  137. jsonRESULTS.getJSONObject("user").getString("c_userid");
  138. String telp = jsonRESULTS.getJSONObject("user").getString("c_telp")==null?"":
  139. jsonRESULTS.getJSONObject("user").getString("c_telp");
  140. String status = jsonRESULTS.getJSONObject("user").getString("c_status")==null?"":
  141. jsonRESULTS.getJSONObject("user").getString("c_status");
  142. String email = jsonRESULTS.getJSONObject("user").getString("vc_email")==null?"":
  143. jsonRESULTS.getJSONObject("user").getString("vc_email");
  144. String path = jsonRESULTS.getJSONObject("user").getString("vc_pathfoto")==null?"":
  145. jsonRESULTS.getJSONObject("user").getString("vc_pathfoto");
  146. String kab = jsonRESULTS.getJSONObject("user").getString("c_idkabkota")==null?"":
  147. jsonRESULTS.getJSONObject("user").getString("c_idkabkota");
  148. String kota = jsonRESULTS.getJSONObject("user").getString("c_idprovinsi")==null?"":
  149. jsonRESULTS.getJSONObject("user").getString("c_idprovinsi");
  150. hideDialog();
  151. prefUtil.saveUserInfo(uId, nama, status, path, pasw, telp, email, kab, kota);
  152. Toast.makeText(LoginActivity.this, jsonRESULTS.getString("message"), Toast.LENGTH_SHORT).show();
  153. Intent i = new Intent(LoginActivity.this, MainActivity.class);
  154. i.putExtra("status", "LOGIN");
  155. startActivity(i);
  156. overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
  157. finish();
  158. } else {
  159. hideDialog();
  160. String error_message = jsonRESULTS.getString("message");
  161. Toast.makeText(LoginActivity.this, error_message, Toast.LENGTH_LONG).show();
  162. }
  163. } catch (JSONException e) {
  164. hideDialog();
  165. e.printStackTrace();
  166. } catch (IOException e) {
  167. hideDialog();
  168. e.printStackTrace();
  169. }
  170. } else {
  171. hideDialog();
  172. Toast.makeText(LoginActivity.this, "GAGAL LOGIN", Toast.LENGTH_LONG).show();
  173. }
  174. }
  175. @Override
  176. public void onFailure(Call<ResponseBody> call, Throwable t) {
  177. hideDialog();
  178. Toast.makeText(LoginActivity.this, "Koneksi Internet Bermasalah", Toast.LENGTH_LONG).show();
  179. }
  180. });
  181. }
  182. private boolean validateUser() {
  183. boolean value;
  184. if (eUserID.getText().toString().isEmpty()){
  185. value=false;
  186. requestFocus(eUserID);
  187. inputLayoutUser.setError(getString(R.string.err_msg_user));
  188. } else{
  189. value=true;
  190. inputLayoutUser.setError(null);
  191. }
  192. return value;
  193. }
  194. private boolean validatePasw(int length) {
  195. boolean value = true;
  196. int minValue = 6;
  197. if (ePassword.getText().toString().trim().isEmpty()) {
  198. value=false;
  199. requestFocus(ePassword);
  200. inputLayoutPasw.setError(getString(R.string.err_msg_sandi));
  201. } else if (length > inputLayoutPasw.getCounterMaxLength()) {
  202. value=false;
  203. inputLayoutPasw.setError("Max character password length is " + inputLayoutPasw.getCounterMaxLength());
  204. } else if (length < minValue) {
  205. value=false;
  206. inputLayoutPasw.setError("Min character password length is 6" );
  207. } else{
  208. value=true;
  209. inputLayoutPasw.setError(null);}
  210. return value;
  211. }
  212. private void showDialog() {
  213. if (!pDialog.isShowing())
  214. pDialog.show();
  215. }
  216. private void hideDialog() {
  217. if (pDialog.isShowing())
  218. pDialog.dismiss();
  219. }
  220. private void requestFocus(View view) {
  221. if (view.requestFocus()) {
  222. getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
  223. }
  224. }
  225. @Override
  226. protected void onDestroy() {
  227. Utils.freeMemory();
  228. super.onDestroy();
  229. Utils.trimCache(this);
  230. }
  231. }