Advertisement
ricky_yulianto

Untitled

Jan 31st, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.55 KB | None | 0 0
  1. public class BaseActivity extends AppCompatActivity implements LogOutTimerUtil.LogOutListener{
  2.  
  3. private static final int MY_PERMISSIONS_REQUEST_CALL_PHONE = 9999;
  4. protected Context context;
  5. protected boolean isActive = true;
  6.  
  7. protected DialogProgress dialogProgress;
  8.  
  9. private Toast mToast = null;
  10. private String selectedPhone;
  11. private String TAG = getClass().getSimpleName();
  12.  
  13. private AlertDialog dialogLogout;
  14.  
  15.  
  16. @Override
  17. protected void onStart() {
  18. super.onStart();
  19.  
  20. if(getClass().getSimpleName().equals("SplashActivity"))
  21. return;
  22.  
  23. if(getClass().getSimpleName().equals("LanguageActivity"))
  24. return;
  25.  
  26. if(getClass().getSimpleName().equals("LoginActivity"))
  27. return;
  28.  
  29. if(getClass().getSimpleName().equals("NewIssueActivity"))
  30. return;
  31.  
  32. if(getClass().getSimpleName().equals("IssueSubmitActivity"))
  33. return;
  34.  
  35. if(getClass().getSimpleName().equals("RegisterActivity"))
  36. return;
  37.  
  38. if(getClass().getSimpleName().equals("ForgotActivity"))
  39. return;
  40.  
  41. if(getClass().getSimpleName().equals("DownloadActivity"))
  42. return;
  43.  
  44. if(getClass().getSimpleName().equals("SelectActivity"))
  45. return;
  46.  
  47. checkLoginTime();
  48.  
  49. }
  50.  
  51. @Override
  52. protected void onStop() {
  53. super.onStop();
  54. // Unbind from the service
  55.  
  56. }
  57.  
  58. @Override
  59. public void onUserInteraction() {
  60. super.onUserInteraction();
  61.  
  62. if(getClass().getSimpleName().equals("SplashActivity"))
  63. return;
  64.  
  65. if(getClass().getSimpleName().equals("LanguageActivity"))
  66. return;
  67.  
  68. if(getClass().getSimpleName().equals("LoginActivity"))
  69. return;
  70.  
  71. if(getClass().getSimpleName().equals("NewIssueActivity"))
  72. return;
  73.  
  74. if(getClass().getSimpleName().equals("IssueSubmitActivity"))
  75. return;
  76.  
  77. if(getClass().getSimpleName().equals("RegisterActivity"))
  78. return;
  79.  
  80. if(getClass().getSimpleName().equals("ForgotActivity"))
  81. return;
  82.  
  83. if(getClass().getSimpleName().equals("DownloadActivity"))
  84. return;
  85.  
  86. if(getClass().getSimpleName().equals("SelectActivity"))
  87. return;
  88.  
  89. if(DataManager.getInstance().isLogin())
  90. LogOutTimerUtil.startLogoutTimer(this, this);
  91.  
  92. Log.e(TAG, "User interacting with screen");
  93. }
  94.  
  95. @Override
  96. protected void onCreate(Bundle savedInstanceState) {
  97. super.onCreate(savedInstanceState);
  98. context = this;
  99. }
  100.  
  101. @Override
  102. protected void onDestroy() {
  103. super.onDestroy();
  104.  
  105.  
  106. // list before login skip check if login
  107. if(getClass().getSimpleName().equals("SplashActivity"))
  108. return;
  109.  
  110. if(getClass().getSimpleName().equals("LanguageActivity"))
  111. return;
  112.  
  113. if(getClass().getSimpleName().equals("LoginActivity"))
  114. return;
  115.  
  116. if(getClass().getSimpleName().equals("NewIssueActivity"))
  117. return;
  118.  
  119. if(getClass().getSimpleName().equals("IssueSubmitActivity"))
  120. return;
  121.  
  122. if(getClass().getSimpleName().equals("RegisterActivity"))
  123. return;
  124.  
  125. if(getClass().getSimpleName().equals("ForgotActivity"))
  126. return;
  127.  
  128. if(getClass().getSimpleName().equals("DownloadActivity"))
  129. return;
  130.  
  131.  
  132. if(getClass().getSimpleName().equals("SelectActivity"))
  133. return;
  134.  
  135. LogOutTimerUtil.stopLogoutTimer();
  136. }
  137.  
  138. @Override
  139. protected void onResume() {
  140. super.onResume();
  141. isActive = true;
  142.  
  143.  
  144. // list before login skip check if login
  145. if(getClass().getSimpleName().equals("SplashActivity"))
  146. return;
  147.  
  148. if(getClass().getSimpleName().equals("LanguageActivity"))
  149. return;
  150.  
  151. if(getClass().getSimpleName().equals("LoginActivity"))
  152. return;
  153.  
  154. if(getClass().getSimpleName().equals("NewIssueActivity"))
  155. return;
  156.  
  157. if(getClass().getSimpleName().equals("IssueSubmitActivity"))
  158. return;
  159.  
  160. if(getClass().getSimpleName().equals("RegisterActivity"))
  161. return;
  162.  
  163. if(getClass().getSimpleName().equals("ForgotActivity"))
  164. return;
  165.  
  166. if(getClass().getSimpleName().equals("DownloadActivity"))
  167. return;
  168.  
  169.  
  170. if(getClass().getSimpleName().equals("SelectActivity"))
  171. return;
  172.  
  173. if(!DataManager.getInstance().isLogin()) {
  174. processAutoLogout();
  175. }
  176.  
  177. }
  178.  
  179. private void checkLoginTime() {
  180. SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault());
  181. try {
  182. Date dateLast = sdf.parse(DataManager.getInstance().getLastLogin());
  183. Date dateNow = new Date();
  184.  
  185. long diffMs = dateNow.getTime() - dateLast.getTime();
  186.  
  187. Log.e(TAG, "checkLoginTime: "+diffMs );
  188.  
  189. long durationInMs = AppConstant.TIMER_AUTO_LOGOUT;
  190.  
  191. if(!TextUtils.isEmpty(DataManager.getInstance().getLogoutDuration())){
  192. durationInMs = Long.parseLong(DataManager.getInstance().getLogoutDuration()) * 60000;
  193. }
  194.  
  195. if(diffMs >= durationInMs){
  196. doLogout();
  197. }
  198.  
  199. } catch (ParseException e) {
  200. e.printStackTrace();
  201. }
  202.  
  203.  
  204. LogOutTimerUtil.startLogoutTimer(this, this);
  205.  
  206. }
  207.  
  208. @Override
  209. protected void onPause() {
  210. super.onPause();
  211. isActive = false;
  212. }
  213.  
  214. @Override
  215. public void onConfigurationChanged(Configuration newConfig) {
  216. super.onConfigurationChanged(newConfig);
  217. }
  218.  
  219. public void showDialogProgress(String message) {
  220. if (message != null) {
  221. dialogProgress = new DialogProgress(context, message, true);
  222. dialogProgress.setCancelable(false);
  223. dialogProgress.show();
  224. } else {
  225. dialogProgress = new DialogProgress(context, "Loading ...", false);
  226. dialogProgress.setCancelable(false);
  227. dialogProgress.show();
  228. }
  229. }
  230.  
  231. public void hideDialogProgress() {
  232. if (dialogProgress != null) {
  233. if (dialogProgress.isShowing()) {
  234. dialogProgress.dismiss();
  235. }
  236. }
  237. }
  238.  
  239.  
  240. public void showToast(String val) {
  241. if (mToast != null) mToast.cancel();
  242. mToast = Toast.makeText(context, val, Toast.LENGTH_SHORT);
  243. mToast.show();
  244. }
  245.  
  246. public void hideKeyboard() {
  247. InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
  248. if (imm != null) {
  249. imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
  250. }
  251. }
  252.  
  253.  
  254. public void openCall(String phone) {
  255. selectedPhone = phone;
  256. if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
  257. // TODO: Consider calling
  258. // ActivityCompat#requestPermissions
  259. // here to request the missing permissions, and then overriding
  260. // public void onRequestPermissionsResult(int requestCode, String[] permissions,
  261. // int[] grantResults)
  262. // to handle the case where the user grants the permission. See the documentation
  263. // for ActivityCompat#requestPermissions for more details.
  264.  
  265. ActivityCompat.requestPermissions(this,
  266. new String[]{Manifest.permission.CALL_PHONE},
  267. MY_PERMISSIONS_REQUEST_CALL_PHONE);
  268.  
  269. return;
  270. }
  271.  
  272. doCall();
  273. }
  274.  
  275. public void doCall() {
  276. if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
  277. Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + selectedPhone));
  278. startActivity(intent);
  279. }
  280. }
  281.  
  282. @Override
  283. public void onRequestPermissionsResult(int requestCode,
  284. @NonNull String permissions[], @NonNull int[] grantResults) {
  285. switch (requestCode) {
  286. case MY_PERMISSIONS_REQUEST_CALL_PHONE: {
  287. // If request is cancelled, the result arrays are empty.
  288. if (grantResults.length > 0
  289. && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  290.  
  291. // permission was granted, yay! Do the
  292. // contacts-related task you need to do.
  293.  
  294. doCall();
  295. } else {
  296.  
  297. // permission denied, boo! Disable the
  298. // functionality that depends on this permission.
  299. }
  300. return;
  301. }
  302.  
  303. // other 'case' lines to check for other
  304. // permissions this app might request
  305. }
  306. }
  307.  
  308. public void processAutoLogout(){
  309. dialogLogoutDismiss();
  310. Intent intent = new Intent(this, LoginActivity.class);
  311. intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  312. intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
  313. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  314. startActivity(intent);
  315. }
  316.  
  317. @Override
  318. public void doLogoutDialog() {
  319. AlertDialog.Builder builder = new AlertDialog.Builder(this);
  320. builder.setMessage(getString(R.string.logout_dialog));
  321. builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  322. @Override
  323. public void onClick(DialogInterface dialogInterface, int i) {
  324. LogOutTimerUtil.startLogoutTimer(BaseActivity.this,BaseActivity.this);
  325. }
  326. });
  327. dialogLogout = builder.show();
  328. }
  329.  
  330. @Override
  331. public void doLogout(){
  332. RetrofitInterface apiService = ApiUtils.getAPIService();
  333. String auth = AppConstant.AuthValue + DataManager.getInstance().getToken();
  334. Call<DoPost> call = apiService.doLogout(auth);
  335. call.enqueue(new Callback<DoPost>() {
  336. @Override
  337. public void onResponse(@NonNull Call<DoPost> call, @NonNull Response<DoPost> data) {
  338. DataManager.getInstance().doLogout();
  339. processAutoLogout();
  340. }
  341.  
  342. @Override
  343. public void onFailure(@NonNull Call<DoPost> call, @NonNull Throwable t) {
  344. DataManager.getInstance().doLogout();
  345. processAutoLogout();
  346. }
  347. });
  348. }
  349.  
  350. @Override
  351. public void doLogoutBackground() {
  352. RetrofitInterface apiService = ApiUtils.getAPIService();
  353. String auth = AppConstant.AuthValue + DataManager.getInstance().getToken();
  354. Call<DoPost> call = apiService.doLogout(auth);
  355. call.enqueue(new Callback<DoPost>() {
  356. @Override
  357. public void onResponse(@NonNull Call<DoPost> call, @NonNull Response<DoPost> data) {
  358. DataManager.getInstance().doLogout();
  359. }
  360.  
  361. @Override
  362. public void onFailure(@NonNull Call<DoPost> call, @NonNull Throwable t) {
  363. DataManager.getInstance().doLogout();
  364. }
  365. });
  366. }
  367.  
  368. private void dialogLogoutDismiss(){
  369. if(dialogLogout != null){
  370. if(dialogLogout.isShowing()){
  371. dialogLogout.dismiss();
  372. }
  373. }
  374. }
  375.  
  376.  
  377.  
  378.  
  379. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement