Created
May 16, 2018 05:53
-
-
Save vishalhalani/0980c63fd8830415be2c916cdeea77f8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LoginActivity extends AppCompatActivity implements OnWebAPIResponseListener, View.OnClickListener { | |
private final String TAG = LoginActivity.class.getSimpleName(); | |
private final int ACCESS_TOKEN_REQUEST_CODE = 1; | |
private static final int RC_LOGIN = 2; | |
private SharedPreferences pref; | |
private String username, password; | |
private LoginController controller; | |
private TextInputEditText usernameEt; | |
private TextInputEditText passwordEt; | |
private Button signinBtn; | |
private RelativeLayout layout; | |
private ProgressBar progressview; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
// Making notification bar transparent | |
if (Build.VERSION.SDK_INT >= 21) { | |
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); | |
} | |
setContentView(R.layout.activity_login); | |
setComponants(); | |
// Toolbar toolbar = (Toolbar) findViewById(R.id.activity_login_toolbar); | |
TextView mTitle = (TextView) findViewById(R.id.activity_login_toolbar_title); | |
// setSupportActionBar(toolbar); | |
// ActionBar actionBar = getSupportActionBar(); | |
// if (actionBar != null) { | |
// actionBar.setDisplayHomeAsUpEnabled(false); | |
// actionBar.setDisplayShowTitleEnabled(false); | |
mTitle.setText("Sign In"); | |
// } | |
pref = getSharedPreferences(MyPREFERENCES, MODE_PRIVATE); | |
boolean isLogged = pref.getBoolean(ISLOGGED, false); | |
controller = new LoginController(LoginActivity.this, LoginActivity.this); | |
if (isLogged) { | |
Intent intent = new Intent(LoginActivity.this, HomeActivity.class); | |
startActivity(intent); | |
finish(); | |
overridePendingTransition(R.anim.left_in, R.anim.right_out); | |
} | |
signinBtn.setOnClickListener(this); | |
} | |
/* | |
Bind all Layout widgets | |
*/ | |
private void setComponants() { | |
progressview = (ProgressBar) findViewById(R.id.activity_login_progressview); | |
usernameEt = (TextInputEditText) findViewById(R.id.activity_login_username_et); | |
passwordEt = (TextInputEditText) findViewById(R.id.activity_login_password_et); | |
signinBtn = (Button) findViewById(R.id.activity_login_signin_btn); | |
layout = (RelativeLayout) findViewById(R.id.linearLayout); | |
} | |
/** | |
* @return true or false if there is any validation not fullfill | |
*/ | |
private boolean checkValidations() { | |
if (TextUtils.isEmpty(usernameEt.getText().toString())) { | |
usernameEt.requestFocus(); | |
Const.showSnackbar(getResources().getString(R.string.empty_username_password), layout, true); | |
return false; | |
} else if (TextUtils.isEmpty(passwordEt.getText().toString())) { | |
passwordEt.requestFocus(); | |
Const.showSnackbar(getResources().getString(R.string.empty_username_password), layout, true); | |
return false; | |
} | |
return true; | |
} | |
@Override | |
public void onCallComplete(Object object, int requestCode) { | |
switch (requestCode) { | |
case ACCESS_TOKEN_REQUEST_CODE: | |
if (object != null) { | |
if (object instanceof AccessTokenModel) { | |
AccessTokenModel tokenmodel = ((AccessTokenModel) object); | |
Log.d(TAG, "onCallComplete: accessToken=->" + tokenmodel.getAccessToken()); | |
SharedPreferences pref = getSharedPreferences(MyPREFERENCES, MODE_PRIVATE); | |
SharedPreferences.Editor editor = pref.edit(); | |
editor.putString(ACCESSTOKEN, ((AccessTokenModel) object).getAccessToken()); | |
editor.apply(); | |
checkUserLogin(RC_LOGIN, username, password); | |
} | |
} | |
break; | |
case RC_LOGIN: | |
if (object != null) { | |
if (object instanceof UsersList) { | |
UsersList mList = ((UsersList) object); | |
List<UserModel> userlist = mList.getUserModelList(); | |
if (userlist.size() > 0) { | |
for (UserModel m : userlist) { | |
if (m.getEmail().equals(usernameEt.getText().toString().trim()) && m.getPassword().equals(passwordEt.getText().toString().trim())) { | |
SharedPreferences pref = getSharedPreferences(MyPREFERENCES, MODE_PRIVATE); | |
SharedPreferences.Editor editor = pref.edit(); | |
editor.putString(USERFULLNAME, m.getFullname()); | |
editor.putString(LOGGEDUSERID, m.getID()); | |
editor.putString(PASSWORD, m.getPassword()); | |
editor.putString(ROLE, m.getRole()); | |
Set<String> companySet = new HashSet<>(); | |
for (int i = 0; i < m.getCompanyModel().size(); i++) { | |
companySet.add(m.getCompanyModel().get(i).getID()); | |
} | |
editor.putStringSet(COMPANY, companySet); | |
Set<String> userAccessSet = new HashSet<>(); | |
for (int i = 0; i < m.getUserAccessList().size(); i++) { | |
userAccessSet.add(m.getUserAccessList().get(i).getID()); | |
} | |
editor.putStringSet(Const.USERACCESS, userAccessSet); | |
editor.putBoolean(ISLOGGED, true); | |
editor.apply(); | |
cancelProgressDialog(); | |
Intent intent = new Intent(LoginActivity.this, HomeActivity.class); | |
startActivity(intent); | |
finish(); | |
overridePendingTransition(R.anim.right_in, R.anim.left_out); | |
} else { | |
cancelProgressDialog(); | |
Const.showSnackbar(getResources().getString(R.string.error_login), layout, true); | |
} | |
// Log.d("User Company Name",m.getCompanyModel().getCompanyName()); | |
} | |
} else { | |
cancelProgressDialog(); | |
Const.showSnackbar(getResources().getString(R.string.error_login), layout, true); | |
} | |
} | |
} | |
break; | |
} | |
} | |
/* | |
Show progress bar | |
*/ | |
private void showProgressDialog() { | |
if (progressview != null) { | |
progressview.setVisibility(View.VISIBLE); | |
getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, | |
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE); | |
} | |
} | |
/* | |
Hide progress bar | |
*/ | |
private void cancelProgressDialog() { | |
if (progressview != null) { | |
progressview.setVisibility(View.GONE); | |
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE); | |
} | |
} | |
@Override | |
public void onCallError(APIError errorModel, int requestCode) { | |
switch (requestCode) { | |
case RC_LOGIN: | |
if (errorModel != null) { | |
if (errorModel.getErrorCode() == 401) { | |
getRefreshToken(RC_LOGIN); | |
} else { | |
cancelProgressDialog(); | |
Const.showSnackbar(errorModel.getMessage(), layout, true); | |
} | |
} | |
break; | |
case ACCESS_TOKEN_REQUEST_CODE: | |
if (errorModel != null) { | |
Log.e(TAG, "onCallError: " + errorModel.getErrorCode()); | |
Const.showSnackbar(errorModel.getMessage(), layout, true); | |
cancelProgressDialog(); | |
} | |
break; | |
} | |
} | |
@Override | |
public void onCallFailure(String errorMessage, int requestCode) { | |
cancelProgressDialog(); | |
Const.showSnackbar(errorMessage, layout, true); | |
} | |
@Override | |
public void OnTokenRefresh(boolean isRefresh, int requestCode) { | |
if (isRefresh) { | |
switch (requestCode) { | |
case RC_LOGIN: | |
checkUserLogin(RC_LOGIN, username, password); | |
break; | |
case ACCESS_TOKEN_REQUEST_CODE: | |
getAccessToken(ACCESS_TOKEN_REQUEST_CODE); | |
break; | |
} | |
} | |
} | |
@Override | |
public void onCallComplete(List obj, int requestCode) { | |
} | |
@Override | |
public void onBackPressed() { | |
super.onBackPressed(); | |
overridePendingTransition(R.anim.left_in, R.anim.right_out); | |
} | |
@Override | |
public void onClick(View v) { | |
try { | |
Const.closeKeyboard(LoginActivity.this); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
boolean isnoError = checkValidations(); | |
if (ApplicationUtil.isInternetOn(LoginActivity.this)) { | |
if (isnoError) { | |
username = usernameEt.getText().toString(); | |
password = passwordEt.getText().toString(); | |
pref = getSharedPreferences(MyPREFERENCES, MODE_PRIVATE); | |
String token = pref.getString(ACCESSTOKEN, null); | |
if (token != null) { | |
checkUserLogin(RC_LOGIN, username, password); | |
} else { | |
getAccessToken(ACCESS_TOKEN_REQUEST_CODE); | |
} | |
} | |
} else { | |
Const.showSnackbar(getResources().getString(R.string.no_internet), layout, true); | |
} | |
} | |
/** | |
* Make web call to get new access token | |
* | |
* @param requestCode Request code for web call | |
*/ | |
private void getRefreshToken(final int requestCode) { | |
if (Const.isInternetOn(LoginActivity.this)) { | |
controller.getRefreshAccessToken(requestCode); | |
} else { | |
android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(LoginActivity.this); | |
builder.setTitle(getResources().getString(R.string.internet_connetion_title)); | |
builder.setMessage(getResources().getString(R.string.connetion_message)); | |
builder.setCancelable(false); | |
builder.setPositiveButton(getResources().getString(R.string.try_again), new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int which) { | |
dialog.dismiss(); | |
getRefreshToken(requestCode); | |
} | |
}); | |
builder.show(); | |
} | |
} | |
/** | |
* Make web call to get access token | |
* | |
* @param requestCode Request code for web call | |
*/ | |
private void getAccessToken(final int requestCode) { | |
if (Const.isInternetOn(LoginActivity.this)) { | |
showProgressDialog(); | |
controller.getAccessToken(requestCode); | |
} else { | |
android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(LoginActivity.this); | |
builder.setTitle(getResources().getString(R.string.internet_connetion_title)); | |
builder.setMessage(getResources().getString(R.string.connetion_message)); | |
builder.setCancelable(false); | |
builder.setPositiveButton(getResources().getString(R.string.try_again), new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int which) { | |
dialog.dismiss(); | |
getAccessToken(requestCode); | |
} | |
}); | |
builder.show(); | |
} | |
} | |
/** | |
* Make Web call to check user Login | |
* | |
* @param requestCode Request code for web call | |
* @param username username or email enter by user | |
* @param password password enter by user | |
*/ | |
private void checkUserLogin(final int requestCode, final String username, final String password) { | |
if (Const.isInternetOn(LoginActivity.this)) { | |
showProgressDialog(); | |
controller.checkUsersCredential(requestCode, username, password); | |
} else { | |
android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(LoginActivity.this); | |
builder.setTitle(getResources().getString(R.string.internet_connetion_title)); | |
builder.setMessage(getResources().getString(R.string.connetion_message)); | |
builder.setCancelable(false); | |
builder.setPositiveButton(getResources().getString(R.string.try_again), new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int which) { | |
dialog.dismiss(); | |
checkUserLogin(requestCode, username, password); | |
} | |
}); | |
builder.show(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment