LoginActivity.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. package com.cygnusa.hyperm;
  2. import android.annotation.SuppressLint;
  3. import android.app.Activity;
  4. import android.content.ContentValues;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.content.SharedPreferences;
  8. import android.content.pm.PackageManager;
  9. import android.graphics.Color;
  10. import android.os.Build;
  11. import android.os.Bundle;
  12. import android.text.SpannableString;
  13. import android.text.TextUtils;
  14. import android.text.style.UnderlineSpan;
  15. import android.view.KeyEvent;
  16. import android.view.LayoutInflater;
  17. import android.view.MotionEvent;
  18. import android.view.View;
  19. import android.view.View.OnClickListener;
  20. import android.view.ViewGroup;
  21. import android.view.inputmethod.InputMethodManager;
  22. import android.widget.EditText;
  23. import android.widget.TextView;
  24. import android.widget.Toast;
  25. import androidx.appcompat.app.AppCompatActivity;
  26. import com.google.gson.Gson;
  27. import com.google.gson.JsonElement;
  28. import com.google.gson.reflect.TypeToken;
  29. import com.cygnusa.hyperm.tooltip.Tooltip;
  30. import com.cygnusa.hyperm.util.ApiService;
  31. import com.cygnusa.hyperm.util.GlobalData;
  32. import com.cygnusa.hyperm.util.MainApplication;
  33. import com.cygnusa.hyperm.util.StorePreference;
  34. import com.cygnusa.hyperm.util.TransparentProgressDialog;
  35. import com.cygnusa.hyperm.util.UtilService;
  36. import com.cygnusa.hyperm.wrappers.ErrorResponse;
  37. import com.cygnusa.hyperm.wrappers.UserResponse;
  38. import java.io.File;
  39. import java.util.HashMap;
  40. import java.util.Objects;
  41. import retrofit2.Call;
  42. import retrofit2.Callback;
  43. import retrofit2.Response;
  44. public class LoginActivity extends AppCompatActivity {
  45. EditText mPasswordView, mEmailView;
  46. ApiService restService;
  47. TransparentProgressDialog progressDialog;
  48. SharedPreferences preferences;
  49. SharedPreferences.Editor editor;
  50. StorePreference storePreference;
  51. @SuppressLint("ClickableViewAccessibility")
  52. @Override
  53. protected void onCreate(Bundle savedInstanceState) {
  54. super.onCreate(savedInstanceState);
  55. setContentView(R.layout.activity_login);
  56. setupWindowAnimations();
  57. setupUI(findViewById(R.id.bgLayout));
  58. restService = ((MainApplication) getApplication()).getClient();
  59. mEmailView = findViewById(R.id.userName);
  60. mPasswordView = findViewById(R.id.password);
  61. storePreference = new StorePreference(this);
  62. TextView textView = findViewById(R.id.notRegistered);
  63. SpannableString loginText = new SpannableString("Signup");
  64. loginText.setSpan(new UnderlineSpan(), 0, loginText.length(), 0);
  65. textView.setText(loginText);
  66. mPasswordView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
  67. @Override
  68. public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) {
  69. if (id == 2276) {
  70. attemptLogin();
  71. return true;
  72. }
  73. return false;
  74. }
  75. });
  76. findViewById(R.id.email_sign_in_button).setOnClickListener(new OnClickListener() {
  77. @Override
  78. public void onClick(View view) {
  79. attemptLogin();
  80. }
  81. });
  82. findViewById(R.id.notRegistered).setOnClickListener(new OnClickListener() {
  83. @Override
  84. public void onClick(View view) {
  85. startActivity(new Intent(LoginActivity.this, SignUpActivity.class));
  86. }
  87. });
  88. findViewById(R.id.info).setOnClickListener(new OnClickListener() {
  89. @Override
  90. public void onClick(View view) {
  91. String header = "-If you are dealer staff enter the SMPIN for registering and logging into the system\n" +
  92. "\n-If you are HYPER-M staff enter your Employee ID for registering and logging into the system";
  93. View mView = LayoutInflater.from(LoginActivity.this).inflate(R.layout.login_tooltip_text, null);
  94. TextView text = mView.findViewById(R.id.text11);
  95. text.setText(header);
  96. Tooltip.TooltipView tooltip = Tooltip.make(LoginActivity.this,
  97. new Tooltip.Builder(101)
  98. .anchor(view, Tooltip.Gravity.TOP)
  99. .closePolicy(new Tooltip.ClosePolicy()
  100. .insidePolicy(true, false)
  101. .outsidePolicy(true, false), 0)
  102. .activateDelay(300)
  103. .withStyleId(R.style.ToolTipLayoutCustomStyle)
  104. .showDelay(300)
  105. .text("")
  106. .setBgColor(getResources().getColor(R.color.bg_color))
  107. .view(mView)
  108. .maxWidth(300)
  109. .withArrow(true)
  110. .build()
  111. );
  112. tooltip.setTextColor(Color.WHITE);
  113. tooltip.show();
  114. }
  115. });
  116. TextView mTextView = findViewById(R.id.forgotPassword);
  117. String udata = mTextView.getText().toString().trim();
  118. SpannableString content = new SpannableString(udata);
  119. content.setSpan(new UnderlineSpan(), 0, udata.length(), 0);
  120. mTextView.setText(content);
  121. mTextView.setOnClickListener(new OnClickListener() {
  122. @Override
  123. public void onClick(View view) {
  124. startActivity(new Intent(getBaseContext(), ForgotPasswordActivity.class));
  125. }
  126. });
  127. findViewById(R.id.faq).setOnClickListener(new OnClickListener() {
  128. @Override
  129. public void onClick(View v) {
  130. startActivity(new Intent(LoginActivity.this, FaqActivity.class));
  131. finish();
  132. }
  133. });
  134. findViewById(R.id.faq1).setOnClickListener(new OnClickListener() {
  135. @Override
  136. public void onClick(View v) {
  137. findViewById(R.id.faq).performClick();
  138. }
  139. });
  140. }
  141. private void setupWindowAnimations() {
  142. this.overridePendingTransition(R.anim.animation_enter,
  143. R.anim.animation_leave);
  144. }
  145. private void attemptLogin() {
  146. String email = mEmailView.getText().toString().trim();
  147. String password = mPasswordView.getText().toString().trim();
  148. boolean cancel = false;
  149. View focusView = null;
  150. if (TextUtils.isEmpty(email) && TextUtils.isEmpty(password)) {
  151. GlobalData.showToast(getBaseContext(), "Please fill in all necessary fields", Toast.LENGTH_SHORT).show();
  152. focusView = mEmailView;
  153. cancel = true;
  154. } else if (TextUtils.isEmpty(email)) {
  155. GlobalData.showToast(getBaseContext(), "Please enter the SMPIN", Toast.LENGTH_SHORT).show();
  156. focusView = mEmailView;
  157. cancel = true;
  158. } else if (TextUtils.isEmpty(password)) {
  159. GlobalData.showToast(getBaseContext(), "Please enter the password", Toast.LENGTH_SHORT).show();
  160. focusView = mPasswordView;
  161. cancel = true;
  162. }
  163. if (cancel) {
  164. focusView.requestFocus();
  165. } else {
  166. if (new UtilService().isNetworkAvailable(LoginActivity.this)) {
  167. progressDialog = new TransparentProgressDialog(LoginActivity.this);
  168. progressDialog.show();
  169. try {
  170. HashMap<String, String> map = new HashMap<>();
  171. map.put("username", email);
  172. map.put("password", password);
  173. restService.login(map).enqueue(new Callback<UserResponse>() {
  174. @Override
  175. public void onResponse(Call<UserResponse> call, Response<UserResponse> response) {
  176. GlobalData.user = response.body();
  177. if (progressDialog != null && progressDialog.isShowing()) {
  178. progressDialog.dismiss();
  179. }
  180. if (GlobalData.user != null && GlobalData.user.getStatus() == 200) {
  181. getSharedPreferences("FCM_TOKEN", Context.MODE_PRIVATE)
  182. .edit().putString("FCM_TOKEN_ID", "").apply();
  183. if (Build.VERSION.SDK_INT >= 23) {
  184. if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
  185. == PackageManager.PERMISSION_GRANTED) {
  186. File f1 = new File(Objects.requireNonNull(getApplicationContext()).getExternalFilesDir(null)
  187. + "/" + ".HYPERM", "PDF");
  188. if (f1.exists()) {
  189. f1.delete();
  190. }
  191. }
  192. } else {
  193. File f1 = new File(Objects.requireNonNull(getApplicationContext()).getExternalFilesDir(null)
  194. + "/" + ".HYPERM", "PDF");
  195. if (f1.exists()) {
  196. f1.delete();
  197. }
  198. }
  199. GlobalData.sdb.delete("session", null, null);
  200. preferences = getSharedPreferences("UserDetails",
  201. Context.MODE_PRIVATE);
  202. if (!Objects.requireNonNull(preferences.getString("inputUsername", "")).trim()
  203. .equalsIgnoreCase(mEmailView.getText().toString().trim())) {
  204. GlobalData.sdb.delete("training", null, null);
  205. GlobalData.sdb.delete("assessment", null, null);
  206. GlobalData.sdb.delete("assessment_question", null, null);
  207. GlobalData.sdb.delete("assessment_answer", null, null);
  208. GlobalData.sdb.delete("notifications", null, null);
  209. GlobalData.sdb.delete("notification_readed", null, null);
  210. GlobalData.sdb.delete("notification_deleted", null, null);
  211. GlobalData.sdb.delete("banner", null, null);
  212. GlobalData.sdb.delete("topic", null, null);
  213. GlobalData.sdb.delete("topic_content", null, null);
  214. }
  215. editor = preferences.edit();
  216. editor.putString("inputUsername", mEmailView.getText().toString().trim());
  217. editor.putString("inputPassword", mPasswordView.getText().toString().trim());
  218. editor.apply();
  219. ((MainApplication) getApplication()).addAccessToken(String.format("Bearer %s", GlobalData.user.getAccess_token().trim()));
  220. restService = ((MainApplication) getApplication()).getClient();
  221. if (GlobalData.user.getData().getIs_first_login().trim().equalsIgnoreCase("1")) {
  222. startActivity(new Intent(getBaseContext(), SetNewPasswordActivity.class));
  223. finish();
  224. } else if (GlobalData.user.getData().getIs_password_expired().trim().equalsIgnoreCase("1")) {
  225. GlobalData.showToast(LoginActivity.this, "Your password is expired, please create new password", Toast.LENGTH_SHORT).show();
  226. startActivity(new Intent(getBaseContext(), SetNewPasswordActivity.class));
  227. finish();
  228. } else if (GlobalData.user.getData().getIs_profile_updated().equalsIgnoreCase("0")) {
  229. startActivity(new Intent(getBaseContext(), MainProfileActivity.class));
  230. finish();
  231. } else if (GlobalData.user.getData().getRole().equalsIgnoreCase("16")) {
  232. GlobalData.showToast(getBaseContext(), "You don’t have required permission to log in to the STAR app", Toast.LENGTH_LONG).show();
  233. } else {
  234. Gson gson = new Gson();
  235. JsonElement jsonElement = gson.toJsonTree(
  236. GlobalData.user, new TypeToken<UserResponse>() {
  237. }.getType());
  238. ContentValues values = new ContentValues();
  239. values.put("user", jsonElement.toString());
  240. GlobalData.sdb.insert("session", null, values);
  241. GlobalData.zmEmail = "none";
  242. GlobalData.rmEmail = "none";
  243. GlobalData.amEmail = "none";
  244. GlobalData.canLogout = false;
  245. if (GlobalData.user.getData().getRole().equalsIgnoreCase("4")
  246. || GlobalData.user.getData().getRole().equalsIgnoreCase("6")
  247. || GlobalData.user.getData().getRole().equalsIgnoreCase("7")
  248. || GlobalData.user.getData().getRole().equalsIgnoreCase("3")
  249. || GlobalData.user.getData().getRole().equalsIgnoreCase("5")
  250. || GlobalData.user.getData().getRole().equalsIgnoreCase("14")) {
  251. storePreference.setBoolean(GlobalData.ISDSELOGIN, false);
  252. startActivity(new Intent(LoginActivity.this, AMHomeActivity.class));
  253. finish();
  254. } else if (GlobalData.user.getData().getRole().equalsIgnoreCase("15")) {
  255. storePreference.setBoolean(GlobalData.ISDSELOGIN, false);
  256. startActivity(new Intent(LoginActivity.this, SEHomeActivity.class));
  257. finish();
  258. } else {
  259. storePreference.setBoolean(GlobalData.ISDSELOGIN, true);
  260. startActivity(new Intent(LoginActivity.this, HomeActivity.class));
  261. finish();
  262. }
  263. }
  264. } else if (GlobalData.user != null) {
  265. GlobalData.showToast(getBaseContext(), GlobalData.user.getMessage(), Toast.LENGTH_SHORT).show();
  266. } else if (response.errorBody() != null) {
  267. Gson gson = new Gson();
  268. ErrorResponse message = gson.fromJson(response.errorBody().charStream(), ErrorResponse.class);
  269. GlobalData.showToast(getBaseContext(), message.getMessage(), Toast.LENGTH_SHORT).show();
  270. }
  271. }
  272. @Override
  273. public void onFailure(Call<UserResponse> call, Throwable t) {
  274. if (progressDialog != null && progressDialog.isShowing()) {
  275. progressDialog.dismiss();
  276. }
  277. GlobalData.showToast(getBaseContext(), "Connection Failure, try again!", Toast.LENGTH_SHORT).show();
  278. }
  279. });
  280. } catch (Exception e) {
  281. e.printStackTrace();
  282. }
  283. } else {
  284. GlobalData.showToast(getBaseContext(), "Please check your internet connection", Toast.LENGTH_SHORT).show();
  285. }
  286. }
  287. }
  288. @Override
  289. public void onBackPressed() {
  290. super.onBackPressed();
  291. this.overridePendingTransition(R.anim.animation_f_enter,
  292. R.anim.animation_f_leave);
  293. }
  294. public void setupUI(View view) {
  295. try {
  296. if (!(view instanceof EditText)) {
  297. view.setOnTouchListener(new View.OnTouchListener() {
  298. @SuppressLint("ClickableViewAccessibility")
  299. public boolean onTouch(View v, MotionEvent event) {
  300. hideSoftKeyboard(LoginActivity.this);
  301. return false;
  302. }
  303. });
  304. }
  305. if (view instanceof ViewGroup) {
  306. for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
  307. View innerView = ((ViewGroup) view).getChildAt(i);
  308. setupUI(innerView);
  309. }
  310. }
  311. } catch (Exception e) {
  312. e.printStackTrace();
  313. }
  314. }
  315. public static void hideSoftKeyboard(Activity activity) {
  316. try {
  317. InputMethodManager inputMethodManager =
  318. (InputMethodManager) activity.getSystemService(
  319. Activity.INPUT_METHOD_SERVICE);
  320. inputMethodManager.hideSoftInputFromWindow(
  321. Objects.requireNonNull(activity.getCurrentFocus()).getWindowToken(), 0);
  322. } catch (Exception e) {
  323. e.printStackTrace();
  324. }
  325. }
  326. }