Advertisement
Guest User

LoginActivity

a guest
Dec 30th, 2018
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 15.80 KB | None | 0 0
  1. package arkamaya.co.id.lsr_mobile;
  2.  
  3. import android.Manifest;
  4. import android.app.ProgressDialog;
  5. import android.content.Context;
  6. import android.content.DialogInterface;
  7. import android.content.Intent;
  8. import android.content.SharedPreferences;
  9. import android.graphics.Color;
  10. import android.net.Uri;
  11. import android.os.Bundle;
  12. import android.preference.PreferenceManager;
  13. import android.provider.Settings;
  14. import android.support.v7.app.AlertDialog;
  15. import android.support.v7.app.AppCompatActivity;
  16. import android.util.Log;
  17. import android.view.LayoutInflater;
  18. import android.view.View;
  19. import android.view.ViewGroup;
  20. import android.widget.Button;
  21. import android.widget.EditText;
  22. import android.widget.TextView;
  23. import android.widget.Toast;
  24.  
  25. import com.karumi.dexter.Dexter;
  26. import com.karumi.dexter.MultiplePermissionsReport;
  27. import com.karumi.dexter.PermissionToken;
  28. import com.karumi.dexter.listener.DexterError;
  29. import com.karumi.dexter.listener.PermissionRequest;
  30. import com.karumi.dexter.listener.PermissionRequestErrorListener;
  31. import com.karumi.dexter.listener.multi.MultiplePermissionsListener;
  32.  
  33. import java.util.List;
  34.  
  35. import arkamaya.co.id.lsr_mobile.activity.MenuActivity;
  36. import arkamaya.co.id.lsr_mobile.model.NotificationResponse;
  37. import arkamaya.co.id.lsr_mobile.network.ApiClient;
  38. import arkamaya.co.id.lsr_mobile.network.ApiInterface;
  39. import arkamaya.co.id.lsr_mobile.utils.Config;
  40. import pojo.CheckLogin;
  41. import retrofit.RestAdapter;
  42. import retrofit.RetrofitError;
  43. import retrofit2.Call;
  44. import retrofit2.Callback;
  45. import retrofit2.Response;
  46.  
  47.  
  48. public class LoginActivity extends AppCompatActivity {
  49.     Button btnLogin,btnSetting, btnAbout;
  50.  
  51.     public String ENDPOINT="http://arkamaya.net:12223";
  52.  
  53.     EditText txtUserName;
  54.     EditText txtPassword;
  55.     private ProgressDialog progress;
  56.     SharedPreferences pref;
  57.  
  58.     TextView tx1;
  59.     int counter = 3;
  60.     private Context mContext;
  61.  
  62.     @Override
  63.     protected void onCreate(Bundle savedInstanceState) {
  64.         super.onCreate(savedInstanceState);
  65.         setContentView(R.layout.activity_login);
  66.         mContext = this;
  67.         //requestStoragePermission();
  68.  
  69.         txtUserName = (EditText)findViewById(R.id.input_email);
  70.         txtPassword = (EditText)findViewById(R.id.input_password);
  71.  
  72.         final Button button = (Button)findViewById(R.id.btn_login);
  73.         button.setOnClickListener(new View.OnClickListener() {
  74.             public void onClick(View v) {
  75.                 onClickAction(v);
  76.             }
  77.         });
  78.  
  79.         btnSetting=(Button)findViewById(R.id.btn_setting);
  80.         btnSetting.setOnClickListener(new View.OnClickListener() {
  81.             public void onClick(View v) {
  82.  
  83.                 moveToSettingActivity();
  84.             }
  85.         });
  86.  
  87.         btnAbout = (Button)findViewById(R.id.btn_about);
  88.         btnAbout.setOnClickListener(new View.OnClickListener() {
  89.             public void onClick(View v) {
  90.  
  91.                 Intent intent = new Intent(getApplicationContext(), AboutActivity.class);
  92.                 startActivity(intent);
  93.             }
  94.         });
  95.  
  96.         pref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
  97.  
  98.         if(pref.contains("URLEndPoint")) {
  99.             ENDPOINT = pref.getString("URLEndPoint", "");
  100.         }else{
  101. //            Toast.makeText(getApplicationContext(), "Web Service URL Not Set..", Toast.LENGTH_SHORT).show();
  102. //            moveToSettingActivity();
  103.         }
  104.  
  105. //        if(pref.contains("UserName")){
  106. //            moveToMainActivity();
  107. //        }
  108.  
  109.     }
  110.  
  111.     public void moveToSettingActivity(){
  112.         Intent intent = new Intent(getApplicationContext(), SettingActivity.class);
  113. //        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
  114. //        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  115.         startActivity(intent);
  116.     }
  117.  
  118.     public void moveToMainActivity(){
  119.         Intent intent = new Intent(LoginActivity.this, MenuActivity.class);
  120.         intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
  121.         intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  122.         startActivity(intent);
  123.     }
  124.  
  125.     public  void getUserProfile(String username){
  126. //        RestAdapter restAdapter = new RestAdapter.Builder()
  127. //                .setEndpoint(ENDPOINT)
  128. //                .setLogLevel(RestAdapter.LogLevel.FULL)
  129. //                .build();
  130. //
  131. //        ApiLogin restInterface = restAdapter.create(ApiLogin.class);
  132. //
  133. //        restInterface.getUserProfile(username, new Callback<CheckLogin>() {
  134. //            @Override
  135. //            public void success(CheckLogin m, Response response) {
  136. //                SharedPreferences.Editor editor = pref.edit();
  137. //                editor.putString("UserRealName", m.getUSERREALNAME());
  138. //                editor.putString("ShiftName", m.getSHIFTNM());
  139. //                editor.putString("RoleId", m.getROLEID());
  140. //                editor.putString("UserName",m.getUSERNAME());
  141. //                editor.putBoolean("isLogin",true);
  142. //                editor.putBoolean("isExit",false);
  143. //                editor.commit();
  144. //                moveToMainActivity();
  145. //            }
  146. //
  147. //            @Override
  148. //            public void failure(RetrofitError error) {
  149. //                Toast.makeText(mContext,"Something wrong :" + error.getLocalizedMessage(),Toast.LENGTH_SHORT).show();
  150. //            }
  151. //        });
  152.  
  153.         ApiInterface service = ApiClient.getClient(ENDPOINT).create(ApiInterface.class);
  154.  
  155.         /*Call the method with parameter in the interface to get the employee data*/
  156.         Call<CheckLogin> call = service.getUserProfile(username);
  157.  
  158.         /*Log the URL called*/
  159.         Log.e(Config.TAG, call.request().url().toString() + "");
  160.  
  161.         call.enqueue(new Callback<CheckLogin>() {
  162.             @Override
  163.             public void onResponse(Call<CheckLogin> call, Response<CheckLogin> response) {
  164.                 if(response.isSuccessful()){
  165.                     //Toast.makeText(mContext, response.body().getUSERNAME(), Toast.LENGTH_SHORT).show();
  166.                     SharedPreferences.Editor editor = pref.edit();
  167.                     editor.putString("UserRealName", response.body().getUSERREALNAME());
  168.                     editor.putString("ShiftName", response.body().getSHIFTNM());
  169.                     editor.putString("RoleId", response.body().getROLEID());
  170.                     editor.putString("UserName",response.body().getUSERNAME());
  171.                     editor.putBoolean("isLogin",true);
  172.                     editor.putBoolean("isExit",false);
  173.                     editor.commit();
  174.                     moveToMainActivity();
  175.                 }
  176.  
  177.  
  178.             }
  179.  
  180.             @Override
  181.             public void onFailure(Call<CheckLogin> call, Throwable t) {
  182.                 // Toast.makeText(mContext, "Something went wrong...Please try later!", Toast.LENGTH_SHORT).show();
  183.                 Log.e("ERROR", t.getMessage());
  184.             }
  185.         });
  186.  
  187.  
  188.     }
  189.     public void onClickAction(View v){
  190.         if(!pref.contains("URLEndPoint")) {
  191.             new android.app.AlertDialog.Builder(mContext)
  192.                     .setMessage("Web Service URL Not Set! Setting now?")
  193.                     .setCancelable(false)
  194.                     .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
  195.                         public void onClick(DialogInterface dialog, int id) {
  196.                             moveToSettingActivity();
  197.                         }
  198.                     }).setNegativeButton("No", null)
  199.                     .show();
  200.         }else{
  201.  
  202.             final String username=txtUserName.getText().toString();
  203.             final String password=txtPassword.getText().toString();
  204.  
  205.             progress = new ProgressDialog(v.getContext());
  206.             progress.setMessage("Login...");
  207.             progress.setIndeterminate(true);
  208.             progress.setCancelable(false);
  209.             progress.show();
  210.  
  211.             final RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(ENDPOINT).setLogLevel(RestAdapter.LogLevel.FULL).build();
  212.  
  213. //        Log.d("Yudha", USER_NAME + "-" + PASSWORD);
  214.             final ApiLogin restInterface = restAdapter.create(ApiLogin.class);
  215.  
  216.             int versionCode = BuildConfig.VERSION_CODE;
  217.             String versionName = BuildConfig.VERSION_NAME;
  218.  
  219.             //restInterface.checkLogin(username,password, versionCode, versionName, new Callback<CheckLogin>() {
  220. //            restInterface.checkLogin2(username, password, new Callback<String>() {
  221. //                @Override
  222. //                public void success(String s, Response response) {
  223. //                     Toast.makeText(getApplicationContext(), response.getBody().toString(), Toast.LENGTH_SHORT).show();
  224. //
  225. //                    //if(m.getMsgType().toLowerCase().equals("info")){
  226. //                    //if(m.getResult() == null){
  227. //                    if(s.equals("true") || s.equals("")){
  228. //                        getUserProfile(username);
  229. //                        /*SharedPreferences.Editor editor = pref.edit();
  230. //                        editor.putString("UserRealName", m.getUSERREALNAME());
  231. //                        editor.putString("ShiftName", m.getSHIFTNM());
  232. //                        editor.putString("RoleId", m.getROLEID());
  233. //                        editor.putString("UserName",username);
  234. //                        editor.putBoolean("isLogin",true);
  235. //                        editor.putBoolean("isExit",false);
  236. //                        editor.commit();
  237. //                        moveToMainActivity();*/
  238. //                    }else{
  239. //                        Toast.makeText(getApplicationContext(), "User / Password doesn't exists", Toast.LENGTH_SHORT).show();
  240. //                    }
  241. //
  242. //                    //}
  243. //                    if (progress != null) {
  244. //                        progress.dismiss();
  245. //                    }
  246. //                }
  247. //
  248. //                @Override
  249. //                public void failure(RetrofitError error) {
  250. //                    Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
  251. //                    if (progress != null) {
  252. //                        progress.dismiss();
  253. //                    }
  254. //                }
  255. //            });
  256.  
  257.             ApiInterface service = ApiClient.getClient(ENDPOINT).create(ApiInterface.class);
  258.  
  259.             /*Call the method with parameter in the interface to get the employee data*/
  260.             Call<String> call = service.getValues(username,password);
  261.  
  262.             /*Log the URL called*/
  263.             Log.e(Config.TAG, call.request().url().toString() + "");
  264.  
  265.             call.enqueue(new Callback<String>() {
  266.                 @Override
  267.                 public void onResponse(Call<String> call, Response<String> response) {
  268.                     if(response.isSuccessful()){
  269.                        // Toast.makeText(mContext, response.body().toString(), Toast.LENGTH_SHORT).show();
  270.                         if(response.body().equals("true") || response.body().equals("")){
  271.                             getUserProfile(username);
  272.                         }else{
  273.                             Toast.makeText(getApplicationContext(), "User / Password doesn't exists", Toast.LENGTH_SHORT).show();
  274.                         }
  275.  
  276.                         //}
  277.                         if (progress != null) {
  278.                             progress.dismiss();
  279.                         }
  280.                     }
  281.  
  282.  
  283.                 }
  284.  
  285.                 @Override
  286.                 public void onFailure(Call<String> call, Throwable t) {
  287.                     // Toast.makeText(mContext, "Something went wrong...Please try later!", Toast.LENGTH_SHORT).show();
  288.                     Log.e("ERROR", t.getMessage());
  289.                 }
  290.             });
  291.         }
  292.  
  293.     }
  294.  
  295.     private void requestStoragePermission() {
  296.         Dexter.withActivity(this)
  297.                 .withPermissions(
  298.                         Manifest.permission.INTERNET,
  299.                         Manifest.permission.CAMERA,
  300.                         Manifest.permission.READ_EXTERNAL_STORAGE,
  301.                         Manifest.permission.WRITE_EXTERNAL_STORAGE
  302.                         )
  303.                 .withListener(new MultiplePermissionsListener() {
  304.                     @Override
  305.                     public void onPermissionsChecked(MultiplePermissionsReport report) {
  306.                         // check if all permissions are granted
  307.                         if (report.areAllPermissionsGranted()) {
  308.                            // Toast.makeText(getApplicationContext(), "All permissions are granted!", Toast.LENGTH_SHORT).show();
  309.                         }
  310.  
  311.                         // check for permanent denial of any permission
  312.                         if (report.isAnyPermissionPermanentlyDenied()) {
  313.                             // show alert dialog navigating to Settings
  314.                             showSettingsDialog();
  315.                         }
  316.                     }
  317.  
  318.                     @Override
  319.                     public void onPermissionRationaleShouldBeShown(List<PermissionRequest> permissions, PermissionToken token) {
  320.                         token.continuePermissionRequest();
  321.                     }
  322.                 }).
  323.                 withErrorListener(new PermissionRequestErrorListener() {
  324.                     @Override
  325.                     public void onError(DexterError error) {
  326.                         Toast.makeText(getApplicationContext(), "Error occurred! ", Toast.LENGTH_SHORT).show();
  327.                     }
  328.                 })
  329.                 .onSameThread()
  330.                 .check();
  331.     }
  332.  
  333.     private void showSettingsDialog() {
  334.         AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
  335.         builder.setTitle("Need Permissions");
  336.         builder.setMessage("This app needs permission to use this feature. You can grant them in app settings.");
  337.         builder.setPositiveButton("GOTO SETTINGS", new DialogInterface.OnClickListener() {
  338.             @Override
  339.             public void onClick(DialogInterface dialog, int which) {
  340.                 dialog.cancel();
  341.                 openSettings();
  342.             }
  343.         });
  344.         builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  345.             @Override
  346.             public void onClick(DialogInterface dialog, int which) {
  347.                 dialog.cancel();
  348.             }
  349.         });
  350.         builder.show();
  351.  
  352.     }
  353.  
  354.     // navigating user to app settings
  355.     private void openSettings() {
  356.         Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
  357.         Uri uri = Uri.fromParts("package", getPackageName(), null);
  358.         intent.setData(uri);
  359.         startActivityForResult(intent, 101);
  360.     }
  361.  
  362.     @Override
  363.     public void onStart(){
  364.         super.onStart();
  365.         //requestStoragePermission();
  366.     }
  367.     @Override
  368.     public void onResume(){
  369.         super.onResume();
  370.  
  371.         pref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
  372.         Boolean login = pref.getBoolean("isLogin", false);
  373.         Boolean exit = pref.getBoolean("isExit", false);
  374.  
  375.         if((login) && (exit)){
  376.             SharedPreferences.Editor editor = pref.edit();
  377.             editor.putBoolean("isExit",false);
  378.             editor.commit();
  379.             finish();
  380.  
  381.         }else{
  382.             //moveToMainActivity();
  383. //            if(pref.contains("URLEndPoint")) {
  384. //                ENDPOINT = pref.getString("URLEndPoint", "");
  385. //            }else{
  386. //                Toast.makeText(getApplicationContext(), "Web Service URL Not Set..", Toast.LENGTH_SHORT).show();
  387. //                moveToSettingActivity();
  388. //            }
  389. //
  390.             if(pref.contains("UserName") && login){
  391.                 moveToMainActivity();
  392.             }
  393.         }
  394.  
  395.     }
  396.  
  397.     @Override
  398.     public void onBackPressed() {
  399.         super.onBackPressed();
  400.         finish();
  401.     }
  402.  
  403. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement