Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package arkamaya.co.id.lsr_mobile;
- import android.Manifest;
- import android.app.ProgressDialog;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.graphics.Color;
- import android.net.Uri;
- import android.os.Bundle;
- import android.preference.PreferenceManager;
- import android.provider.Settings;
- import android.support.v7.app.AlertDialog;
- import android.support.v7.app.AppCompatActivity;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.karumi.dexter.Dexter;
- import com.karumi.dexter.MultiplePermissionsReport;
- import com.karumi.dexter.PermissionToken;
- import com.karumi.dexter.listener.DexterError;
- import com.karumi.dexter.listener.PermissionRequest;
- import com.karumi.dexter.listener.PermissionRequestErrorListener;
- import com.karumi.dexter.listener.multi.MultiplePermissionsListener;
- import java.util.List;
- import arkamaya.co.id.lsr_mobile.activity.MenuActivity;
- import arkamaya.co.id.lsr_mobile.model.NotificationResponse;
- import arkamaya.co.id.lsr_mobile.network.ApiClient;
- import arkamaya.co.id.lsr_mobile.network.ApiInterface;
- import arkamaya.co.id.lsr_mobile.utils.Config;
- import pojo.CheckLogin;
- import retrofit.RestAdapter;
- import retrofit.RetrofitError;
- import retrofit2.Call;
- import retrofit2.Callback;
- import retrofit2.Response;
- public class LoginActivity extends AppCompatActivity {
- Button btnLogin,btnSetting, btnAbout;
- public String ENDPOINT="http://arkamaya.net:12223";
- EditText txtUserName;
- EditText txtPassword;
- private ProgressDialog progress;
- SharedPreferences pref;
- TextView tx1;
- int counter = 3;
- private Context mContext;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
- mContext = this;
- //requestStoragePermission();
- txtUserName = (EditText)findViewById(R.id.input_email);
- txtPassword = (EditText)findViewById(R.id.input_password);
- final Button button = (Button)findViewById(R.id.btn_login);
- button.setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
- onClickAction(v);
- }
- });
- btnSetting=(Button)findViewById(R.id.btn_setting);
- btnSetting.setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
- moveToSettingActivity();
- }
- });
- btnAbout = (Button)findViewById(R.id.btn_about);
- btnAbout.setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
- Intent intent = new Intent(getApplicationContext(), AboutActivity.class);
- startActivity(intent);
- }
- });
- pref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
- if(pref.contains("URLEndPoint")) {
- ENDPOINT = pref.getString("URLEndPoint", "");
- }else{
- // Toast.makeText(getApplicationContext(), "Web Service URL Not Set..", Toast.LENGTH_SHORT).show();
- // moveToSettingActivity();
- }
- // if(pref.contains("UserName")){
- // moveToMainActivity();
- // }
- }
- public void moveToSettingActivity(){
- Intent intent = new Intent(getApplicationContext(), SettingActivity.class);
- // intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
- // intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- startActivity(intent);
- }
- public void moveToMainActivity(){
- Intent intent = new Intent(LoginActivity.this, MenuActivity.class);
- intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
- intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- startActivity(intent);
- }
- public void getUserProfile(String username){
- // RestAdapter restAdapter = new RestAdapter.Builder()
- // .setEndpoint(ENDPOINT)
- // .setLogLevel(RestAdapter.LogLevel.FULL)
- // .build();
- //
- // ApiLogin restInterface = restAdapter.create(ApiLogin.class);
- //
- // restInterface.getUserProfile(username, new Callback<CheckLogin>() {
- // @Override
- // public void success(CheckLogin m, Response response) {
- // SharedPreferences.Editor editor = pref.edit();
- // editor.putString("UserRealName", m.getUSERREALNAME());
- // editor.putString("ShiftName", m.getSHIFTNM());
- // editor.putString("RoleId", m.getROLEID());
- // editor.putString("UserName",m.getUSERNAME());
- // editor.putBoolean("isLogin",true);
- // editor.putBoolean("isExit",false);
- // editor.commit();
- // moveToMainActivity();
- // }
- //
- // @Override
- // public void failure(RetrofitError error) {
- // Toast.makeText(mContext,"Something wrong :" + error.getLocalizedMessage(),Toast.LENGTH_SHORT).show();
- // }
- // });
- ApiInterface service = ApiClient.getClient(ENDPOINT).create(ApiInterface.class);
- /*Call the method with parameter in the interface to get the employee data*/
- Call<CheckLogin> call = service.getUserProfile(username);
- /*Log the URL called*/
- Log.e(Config.TAG, call.request().url().toString() + "");
- call.enqueue(new Callback<CheckLogin>() {
- @Override
- public void onResponse(Call<CheckLogin> call, Response<CheckLogin> response) {
- if(response.isSuccessful()){
- //Toast.makeText(mContext, response.body().getUSERNAME(), Toast.LENGTH_SHORT).show();
- SharedPreferences.Editor editor = pref.edit();
- editor.putString("UserRealName", response.body().getUSERREALNAME());
- editor.putString("ShiftName", response.body().getSHIFTNM());
- editor.putString("RoleId", response.body().getROLEID());
- editor.putString("UserName",response.body().getUSERNAME());
- editor.putBoolean("isLogin",true);
- editor.putBoolean("isExit",false);
- editor.commit();
- moveToMainActivity();
- }
- }
- @Override
- public void onFailure(Call<CheckLogin> call, Throwable t) {
- // Toast.makeText(mContext, "Something went wrong...Please try later!", Toast.LENGTH_SHORT).show();
- Log.e("ERROR", t.getMessage());
- }
- });
- }
- public void onClickAction(View v){
- if(!pref.contains("URLEndPoint")) {
- new android.app.AlertDialog.Builder(mContext)
- .setMessage("Web Service URL Not Set! Setting now?")
- .setCancelable(false)
- .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int id) {
- moveToSettingActivity();
- }
- }).setNegativeButton("No", null)
- .show();
- }else{
- final String username=txtUserName.getText().toString();
- final String password=txtPassword.getText().toString();
- progress = new ProgressDialog(v.getContext());
- progress.setMessage("Login...");
- progress.setIndeterminate(true);
- progress.setCancelable(false);
- progress.show();
- final RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(ENDPOINT).setLogLevel(RestAdapter.LogLevel.FULL).build();
- // Log.d("Yudha", USER_NAME + "-" + PASSWORD);
- final ApiLogin restInterface = restAdapter.create(ApiLogin.class);
- int versionCode = BuildConfig.VERSION_CODE;
- String versionName = BuildConfig.VERSION_NAME;
- //restInterface.checkLogin(username,password, versionCode, versionName, new Callback<CheckLogin>() {
- // restInterface.checkLogin2(username, password, new Callback<String>() {
- // @Override
- // public void success(String s, Response response) {
- // Toast.makeText(getApplicationContext(), response.getBody().toString(), Toast.LENGTH_SHORT).show();
- //
- // //if(m.getMsgType().toLowerCase().equals("info")){
- // //if(m.getResult() == null){
- // if(s.equals("true") || s.equals("")){
- // getUserProfile(username);
- // /*SharedPreferences.Editor editor = pref.edit();
- // editor.putString("UserRealName", m.getUSERREALNAME());
- // editor.putString("ShiftName", m.getSHIFTNM());
- // editor.putString("RoleId", m.getROLEID());
- // editor.putString("UserName",username);
- // editor.putBoolean("isLogin",true);
- // editor.putBoolean("isExit",false);
- // editor.commit();
- // moveToMainActivity();*/
- // }else{
- // Toast.makeText(getApplicationContext(), "User / Password doesn't exists", Toast.LENGTH_SHORT).show();
- // }
- //
- // //}
- // if (progress != null) {
- // progress.dismiss();
- // }
- // }
- //
- // @Override
- // public void failure(RetrofitError error) {
- // Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
- // if (progress != null) {
- // progress.dismiss();
- // }
- // }
- // });
- ApiInterface service = ApiClient.getClient(ENDPOINT).create(ApiInterface.class);
- /*Call the method with parameter in the interface to get the employee data*/
- Call<String> call = service.getValues(username,password);
- /*Log the URL called*/
- Log.e(Config.TAG, call.request().url().toString() + "");
- call.enqueue(new Callback<String>() {
- @Override
- public void onResponse(Call<String> call, Response<String> response) {
- if(response.isSuccessful()){
- // Toast.makeText(mContext, response.body().toString(), Toast.LENGTH_SHORT).show();
- if(response.body().equals("true") || response.body().equals("")){
- getUserProfile(username);
- }else{
- Toast.makeText(getApplicationContext(), "User / Password doesn't exists", Toast.LENGTH_SHORT).show();
- }
- //}
- if (progress != null) {
- progress.dismiss();
- }
- }
- }
- @Override
- public void onFailure(Call<String> call, Throwable t) {
- // Toast.makeText(mContext, "Something went wrong...Please try later!", Toast.LENGTH_SHORT).show();
- Log.e("ERROR", t.getMessage());
- }
- });
- }
- }
- private void requestStoragePermission() {
- Dexter.withActivity(this)
- .withPermissions(
- Manifest.permission.INTERNET,
- Manifest.permission.CAMERA,
- Manifest.permission.READ_EXTERNAL_STORAGE,
- Manifest.permission.WRITE_EXTERNAL_STORAGE
- )
- .withListener(new MultiplePermissionsListener() {
- @Override
- public void onPermissionsChecked(MultiplePermissionsReport report) {
- // check if all permissions are granted
- if (report.areAllPermissionsGranted()) {
- // Toast.makeText(getApplicationContext(), "All permissions are granted!", Toast.LENGTH_SHORT).show();
- }
- // check for permanent denial of any permission
- if (report.isAnyPermissionPermanentlyDenied()) {
- // show alert dialog navigating to Settings
- showSettingsDialog();
- }
- }
- @Override
- public void onPermissionRationaleShouldBeShown(List<PermissionRequest> permissions, PermissionToken token) {
- token.continuePermissionRequest();
- }
- }).
- withErrorListener(new PermissionRequestErrorListener() {
- @Override
- public void onError(DexterError error) {
- Toast.makeText(getApplicationContext(), "Error occurred! ", Toast.LENGTH_SHORT).show();
- }
- })
- .onSameThread()
- .check();
- }
- private void showSettingsDialog() {
- AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
- builder.setTitle("Need Permissions");
- builder.setMessage("This app needs permission to use this feature. You can grant them in app settings.");
- builder.setPositiveButton("GOTO SETTINGS", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- dialog.cancel();
- openSettings();
- }
- });
- builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- dialog.cancel();
- }
- });
- builder.show();
- }
- // navigating user to app settings
- private void openSettings() {
- Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
- Uri uri = Uri.fromParts("package", getPackageName(), null);
- intent.setData(uri);
- startActivityForResult(intent, 101);
- }
- @Override
- public void onStart(){
- super.onStart();
- //requestStoragePermission();
- }
- @Override
- public void onResume(){
- super.onResume();
- pref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
- Boolean login = pref.getBoolean("isLogin", false);
- Boolean exit = pref.getBoolean("isExit", false);
- if((login) && (exit)){
- SharedPreferences.Editor editor = pref.edit();
- editor.putBoolean("isExit",false);
- editor.commit();
- finish();
- }else{
- //moveToMainActivity();
- // if(pref.contains("URLEndPoint")) {
- // ENDPOINT = pref.getString("URLEndPoint", "");
- // }else{
- // Toast.makeText(getApplicationContext(), "Web Service URL Not Set..", Toast.LENGTH_SHORT).show();
- // moveToSettingActivity();
- // }
- //
- if(pref.contains("UserName") && login){
- moveToMainActivity();
- }
- }
- }
- @Override
- public void onBackPressed() {
- super.onBackPressed();
- finish();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement