PassCodeManager.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * ownCloud Android client application
  3. *
  4. * @author David A. Velasco
  5. * Copyright (C) 2015 ownCloud Inc.
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2,
  9. * as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. package com.owncloud.android.authentication;
  21. import android.app.Activity;
  22. import android.content.Context;
  23. import android.content.Intent;
  24. import android.os.Build;
  25. import android.os.PowerManager;
  26. import android.view.Window;
  27. import android.view.WindowManager;
  28. import com.nextcloud.client.preferences.PreferenceManager;
  29. import com.owncloud.android.MainApp;
  30. import com.owncloud.android.ui.activity.PassCodeActivity;
  31. import com.owncloud.android.ui.activity.RequestCredentialsActivity;
  32. import com.owncloud.android.ui.activity.SettingsActivity;
  33. import com.owncloud.android.utils.DeviceCredentialUtils;
  34. import java.util.HashSet;
  35. import java.util.Set;
  36. public final class PassCodeManager {
  37. private static final Set<Class> exemptOfPasscodeActivities;
  38. public static final int PASSCODE_ACTIVITY = 9999;
  39. static {
  40. exemptOfPasscodeActivities = new HashSet<>();
  41. exemptOfPasscodeActivities.add(PassCodeActivity.class);
  42. exemptOfPasscodeActivities.add(RequestCredentialsActivity.class);
  43. // other activities may be exempted, if needed
  44. }
  45. private static final int PASS_CODE_TIMEOUT = 5000;
  46. // keeping a "low" positive value is the easiest way to prevent the pass code is requested on rotations
  47. private static PassCodeManager passCodeManagerInstance;
  48. private int visibleActivitiesCounter;
  49. public static PassCodeManager getPassCodeManager() {
  50. if (passCodeManagerInstance == null) {
  51. passCodeManagerInstance = new PassCodeManager();
  52. }
  53. return passCodeManagerInstance;
  54. }
  55. private PassCodeManager() {}
  56. public void onActivityCreated(Activity activity) {
  57. Window window = activity.getWindow();
  58. if (window != null) {
  59. if (passCodeIsEnabled() || deviceCredentialsAreEnabled(activity)) {
  60. window.addFlags(WindowManager.LayoutParams.FLAG_SECURE);
  61. } else {
  62. window.clearFlags(WindowManager.LayoutParams.FLAG_SECURE);
  63. }
  64. }
  65. }
  66. public void onActivityStarted(Activity activity) {
  67. Long timestamp = PreferenceManager.getLockTimestamp(activity);
  68. if (!exemptOfPasscodeActivities.contains(activity.getClass()) && passCodeShouldBeRequested(timestamp)) {
  69. Intent i = new Intent(MainApp.getAppContext(), PassCodeActivity.class);
  70. i.setAction(PassCodeActivity.ACTION_CHECK);
  71. i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
  72. activity.startActivityForResult(i, PASSCODE_ACTIVITY);
  73. }
  74. if (!exemptOfPasscodeActivities.contains(activity.getClass()) &&
  75. Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && deviceCredentialsShouldBeRequested(timestamp, activity)) {
  76. Intent i = new Intent(MainApp.getAppContext(), RequestCredentialsActivity.class);
  77. i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
  78. activity.startActivityForResult(i, PASSCODE_ACTIVITY);
  79. }
  80. visibleActivitiesCounter++; // keep it AFTER passCodeShouldBeRequested was checked
  81. }
  82. public void onActivityStopped(Activity activity) {
  83. if (visibleActivitiesCounter > 0) {
  84. visibleActivitiesCounter--;
  85. }
  86. setUnlockTimestamp(activity);
  87. PowerManager powerMgr = (PowerManager) activity.getSystemService(Context.POWER_SERVICE);
  88. if ((passCodeIsEnabled() || deviceCredentialsAreEnabled(activity)) && powerMgr != null
  89. && !powerMgr.isScreenOn()) {
  90. activity.moveTaskToBack(true);
  91. }
  92. }
  93. private void setUnlockTimestamp(Activity activity) {
  94. PreferenceManager.setLockTimestamp(activity, System.currentTimeMillis());
  95. }
  96. private boolean passCodeShouldBeRequested(Long timestamp) {
  97. return (System.currentTimeMillis() - timestamp) > PASS_CODE_TIMEOUT &&
  98. visibleActivitiesCounter <= 0 && passCodeIsEnabled();
  99. }
  100. private boolean passCodeIsEnabled() {
  101. return SettingsActivity.LOCK_PASSCODE.equals(PreferenceManager.getLockPreference(MainApp.getAppContext()));
  102. }
  103. private boolean deviceCredentialsShouldBeRequested(Long timestamp, Activity activity) {
  104. return (System.currentTimeMillis() - timestamp) > PASS_CODE_TIMEOUT && visibleActivitiesCounter <= 0 &&
  105. deviceCredentialsAreEnabled(activity);
  106. }
  107. private boolean deviceCredentialsAreEnabled(Activity activity) {
  108. return SettingsActivity.LOCK_DEVICE_CREDENTIALS.equals(PreferenceManager.getLockPreference(MainApp.getAppContext()))
  109. || Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
  110. (PreferenceManager.isUseFingerprint(MainApp.getAppContext())
  111. && DeviceCredentialUtils.areCredentialsAvailable(activity));
  112. }
  113. }