From 0559176a8b2064de134cbe9a725dc027da6694d1 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Fri, 3 Sep 2021 16:57:12 +0800 Subject: [PATCH] New options: App Lock and App Lock Timeout. --- .../infinityforreddit/AppModule.java | 7 ++++ .../infinityforreddit/Infinity.java | 29 ++++++++++---- .../activities/LockScreenActivity.java | 11 ++++++ .../events/ChangeAppLockEvent.java | 11 ++++++ .../settings/SecurityPreferenceFragment.java | 19 ++++++++++ .../utils/SharedPreferencesUtils.java | 9 ++++- app/src/main/res/values/arrays.xml | 38 +++++++++++++++++++ app/src/main/res/values/strings.xml | 19 ++++++++++ app/src/main/res/xml/security_preferences.xml | 18 +++++++++ 9 files changed, 151 insertions(+), 10 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/events/ChangeAppLockEvent.java diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java index 49120eca..f2fd9ed8 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java @@ -282,6 +282,13 @@ class AppModule { return mApplication.getSharedPreferences(SharedPreferencesUtils.POST_DETAILS_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE); } + @Provides + @Named("security") + @Singleton + SharedPreferences provideSecuritySharedPreferences() { + return mApplication.getSharedPreferences(SharedPreferencesUtils.SECURITY_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE); + } + @Provides @Singleton CustomThemeWrapper provideCustomThemeWrapper(@Named("light_theme") SharedPreferences lightThemeSharedPreferences, diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Infinity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Infinity.java index 63df2552..39e56874 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Infinity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Infinity.java @@ -29,6 +29,7 @@ import javax.inject.Named; import ml.docilealligator.infinityforreddit.activities.LockScreenActivity; import ml.docilealligator.infinityforreddit.broadcastreceivers.NetworkWifiStatusReceiver; import ml.docilealligator.infinityforreddit.broadcastreceivers.WallpaperChangeReceiver; +import ml.docilealligator.infinityforreddit.events.ChangeAppLockEvent; import ml.docilealligator.infinityforreddit.events.ChangeNetworkStatusEvent; import ml.docilealligator.infinityforreddit.events.ToggleSecureModeEvent; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; @@ -37,11 +38,13 @@ import ml.docilealligator.infinityforreddit.utils.Utils; public class Infinity extends Application implements LifecycleObserver { private AppComponent mAppComponent; private NetworkWifiStatusReceiver mNetworkWifiStatusReceiver; - private boolean lock = false; + private boolean appLock; + private long appLockTimeout; + private boolean canStartLockScreenActivity = false; private boolean isSecureMode; @Inject - @Named("default") - SharedPreferences mSharedPreferences; + @Named("security") + SharedPreferences mSecuritySharedPreferences; @Override public void onCreate() { @@ -53,7 +56,9 @@ public class Infinity extends Application implements LifecycleObserver { mAppComponent.inject(this); - isSecureMode = mSharedPreferences.getBoolean(SharedPreferencesUtils.SECURE_MODE, false); + appLock = mSecuritySharedPreferences.getBoolean(SharedPreferencesUtils.APP_LOCK, false); + appLockTimeout = Long.parseLong(mSecuritySharedPreferences.getString(SharedPreferencesUtils.APP_LOCK_TIMEOUT, "600000")); + isSecureMode = mSecuritySharedPreferences.getBoolean(SharedPreferencesUtils.SECURE_MODE, false); registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() { @Override @@ -70,11 +75,13 @@ public class Infinity extends Application implements LifecycleObserver { @Override public void onActivityResumed(@NonNull Activity activity) { - if (lock && !(activity instanceof LockScreenActivity)) { - lock = false; + if (canStartLockScreenActivity && appLock + && System.currentTimeMillis() - mSecuritySharedPreferences.getLong(SharedPreferencesUtils.LAST_UNLOCK_TIME, 0) >= appLockTimeout + && !(activity instanceof LockScreenActivity)) { Intent intent = new Intent(activity, LockScreenActivity.class); activity.startActivity(intent); } + canStartLockScreenActivity = false; } @Override @@ -125,11 +132,11 @@ public class Infinity extends Application implements LifecycleObserver { @OnLifecycleEvent(Lifecycle.Event.ON_START) public void appInForeground() { - lock = true; + canStartLockScreenActivity = true; } @OnLifecycleEvent(Lifecycle.Event.ON_STOP) - public void appInBackground(){ + public void appInBackground() { } @@ -141,4 +148,10 @@ public class Infinity extends Application implements LifecycleObserver { public void onToggleSecureModeEvent(ToggleSecureModeEvent secureModeEvent) { isSecureMode = secureModeEvent.isSecureMode; } + + @Subscribe + public void onChangeAppLockEvent(ChangeAppLockEvent changeAppLockEvent) { + appLock = changeAppLockEvent.appLock; + appLockTimeout = changeAppLockEvent.appLockTimeout; + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/LockScreenActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/LockScreenActivity.java index 42544ecd..7569ab66 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/LockScreenActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/LockScreenActivity.java @@ -24,6 +24,7 @@ import butterknife.ButterKnife; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; +import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; public class LockScreenActivity extends BaseActivity { @@ -35,6 +36,9 @@ public class LockScreenActivity extends BaseActivity { @Named("default") SharedPreferences mSharedPreferences; @Inject + @Named("security") + SharedPreferences mSecurityPreferences; + @Inject CustomThemeWrapper mCustomThemeWrapper; @Override @@ -67,6 +71,7 @@ public class LockScreenActivity extends BaseActivity { public void onAuthenticationSucceeded( @NonNull BiometricPrompt.AuthenticationResult result) { super.onAuthenticationSucceeded(result); + saveUnlockTime(); finish(); } }); @@ -78,10 +83,16 @@ public class LockScreenActivity extends BaseActivity { biometricPrompt.authenticate(promptInfo); } else { + saveUnlockTime(); finish(); } } + private void saveUnlockTime() { + mSecurityPreferences.edit().putLong(SharedPreferencesUtils.LAST_UNLOCK_TIME, System.currentTimeMillis()).apply(); + } + + @Override protected SharedPreferences getDefaultSharedPreferences() { return mSharedPreferences; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/events/ChangeAppLockEvent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/events/ChangeAppLockEvent.java new file mode 100644 index 00000000..affa5125 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/events/ChangeAppLockEvent.java @@ -0,0 +1,11 @@ +package ml.docilealligator.infinityforreddit.events; + +public class ChangeAppLockEvent { + public boolean appLock; + public long appLockTimeout; + + public ChangeAppLockEvent(boolean appLock, long appLockTimeout) { + this.appLock = appLock; + this.appLockTimeout = appLockTimeout; + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/settings/SecurityPreferenceFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/settings/SecurityPreferenceFragment.java index c4dd2b16..83163020 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/settings/SecurityPreferenceFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/settings/SecurityPreferenceFragment.java @@ -11,7 +11,9 @@ import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.biometric.BiometricPrompt; import androidx.core.content.ContextCompat; +import androidx.preference.ListPreference; import androidx.preference.PreferenceFragmentCompat; +import androidx.preference.PreferenceManager; import androidx.preference.SwitchPreference; import org.greenrobot.eventbus.EventBus; @@ -23,6 +25,7 @@ import javax.inject.Named; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.events.ChangeAppLockEvent; import ml.docilealligator.infinityforreddit.events.ChangeRequireAuthToAccountSectionEvent; import ml.docilealligator.infinityforreddit.events.ToggleSecureModeEvent; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; @@ -36,12 +39,16 @@ public class SecurityPreferenceFragment extends PreferenceFragmentCompat { @Override public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { + PreferenceManager preferenceManager = getPreferenceManager(); + preferenceManager.setSharedPreferencesName(SharedPreferencesUtils.SECURITY_SHARED_PREFERENCES_FILE); setPreferencesFromResource(R.xml.security_preferences, rootKey); ((Infinity) activity.getApplication()).getAppComponent().inject(this); SwitchPreference requireAuthToAccountSectionSwitch = findPreference(SharedPreferencesUtils.REQUIRE_AUTHENTICATION_TO_GO_TO_ACCOUNT_SECTION_IN_NAVIGATION_DRAWER); SwitchPreference secureModeSwitch = findPreference(SharedPreferencesUtils.SECURE_MODE); + SwitchPreference appLockSwitch = findPreference(SharedPreferencesUtils.APP_LOCK); + ListPreference appLockTimeoutListPreference = findPreference(SharedPreferencesUtils.APP_LOCK_TIMEOUT); if (requireAuthToAccountSectionSwitch != null) { requireAuthToAccountSectionSwitch.setOnPreferenceChangeListener((preference, newValue) -> { @@ -56,6 +63,18 @@ public class SecurityPreferenceFragment extends PreferenceFragmentCompat { return true; }); } + + if (appLockSwitch != null && appLockTimeoutListPreference != null) { + appLockSwitch.setOnPreferenceChangeListener((preference, newValue) -> { + EventBus.getDefault().post(new ChangeAppLockEvent((Boolean) newValue, Long.parseLong(appLockTimeoutListPreference.getValue()))); + return true; + }); + + appLockTimeoutListPreference.setOnPreferenceChangeListener((preference, newValue) -> { + EventBus.getDefault().post(new ChangeAppLockEvent(appLockSwitch.isChecked(), Long.parseLong((String) newValue))); + return true; + }); + } } @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/SharedPreferencesUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/SharedPreferencesUtils.java index 7760f465..a2d243b2 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/SharedPreferencesUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/SharedPreferencesUtils.java @@ -132,7 +132,6 @@ public class SharedPreferencesUtils { public static final String ENABLE_SWIPE_ACTION = "enable_swipe_action"; public static final String SWIPE_ACTION_THRESHOLD = "swipe_action_threshold"; public static final String PULL_TO_REFRESH = "pull_to_refresh"; - public static final String REQUIRE_AUTHENTICATION_TO_GO_TO_ACCOUNT_SECTION_IN_NAVIGATION_DRAWER = "require_auth_to_account_section"; public static final String LONG_PRESS_TO_HIDE_TOOLBAR_IN_COMPACT_LAYOUT = "long_press_to_hide_toolbar_in_compact_layout"; public static final String POST_COMPACT_LAYOUT_TOOLBAR_HIDDEN_BY_DEFAULT = "post_compact_layout_toolbar_hidden_by_default"; public static final String SECURITY = "security"; @@ -186,7 +185,6 @@ public class SharedPreferencesUtils { public static final String REMEMBER_MUTING_OPTION_IN_POST_FEED = "remember_muting_option_in_post_feed"; public static final String DEFAULT_LINK_POST_LAYOUT_KEY = "default_link_post_layout"; public static final String USE_BOTTOM_TOOLBAR_IN_MEDIA_VIEWER = "use_bottom_toolbar_in_media_viewer"; - public static final String SECURE_MODE = "secure_mode"; public static final String LOCK_SCREEN_ANIMATION = "lock_screen_animation"; public static final String DEFAULT_PREFERENCES_FILE = "ml.docilealligator.infinityforreddit_preferences"; @@ -326,6 +324,13 @@ public class SharedPreferencesUtils { public static final String SEPARATE_POST_AND_COMMENTS_IN_PORTRAIT_MODE = "separate_post_and_comments_in_portrait_mode"; public static final String SEPARATE_POST_AND_COMMENTS_IN_LANDSCAPE_MODE = "separate_post_and_comments_in_landscape_mode"; + public static final String SECURITY_SHARED_PREFERENCES_FILE = "ml.docilealligator.infinityforreddit.security"; + public static final String REQUIRE_AUTHENTICATION_TO_GO_TO_ACCOUNT_SECTION_IN_NAVIGATION_DRAWER = "require_auth_to_account_section"; + public static final String SECURE_MODE = "secure_mode"; + public static final String APP_LOCK = "app_lock"; + public static final String APP_LOCK_TIMEOUT = "app_lock_timeout"; + public static final String LAST_UNLOCK_TIME = "last_unlock_time"; + //Legacy Settings public static final String MAIN_PAGE_TAB_1_TITLE_LEGACY = "main_page_tab_1_title"; public static final String MAIN_PAGE_TAB_2_TITLE_LEGACY = "main_page_tab_2_title"; diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml index a2d77399..f8097c33 100644 --- a/app/src/main/res/values/arrays.xml +++ b/app/src/main/res/values/arrays.xml @@ -466,4 +466,42 @@ @string/exclude_domain + + @string/app_lock_timeout_immediately + @string/app_lock_timeout_1_min + @string/app_lock_timeout_2_mins + @string/app_lock_timeout_5_mins + @string/app_lock_timeout_10_mins + @string/app_lock_timeout_15_mins + @string/app_lock_timeout_20_mins + @string/app_lock_timeout_30_mins + @string/app_lock_timeout_1_hour + @string/app_lock_timeout_2_hours + @string/app_lock_timeout_3_hours + @string/app_lock_timeout_4_hours + @string/app_lock_timeout_5_hours + @string/app_lock_timeout_6_hours + @string/app_lock_timeout_12_hours + @string/app_lock_timeout_24_hours + + + + 0 + 60000 + 120000 + 300000 + 600000 + 900000 + 1200000 + 1800000 + 3600000 + 7200000 + 10800000 + 14400000 + 18000000 + 21600000 + 43200000 + 86400000 + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 1de4ba0f..68ab820b 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -597,6 +597,9 @@ Secure Mode Screenshot and video recording are not allowed. No preview in recent app screen. Lock Screen Animation + App Lock + Require authentication before using the app + App Lock Timeout Cannot get the link @@ -1171,5 +1174,21 @@ Applying Material You Whoa there!!! + Immediately + 1 minute + 2 minutes + 5 minutes + 10 minutes + 15 minutes + 20 minutes + 30 minutes + 1 hour + 2 hours + 3 hours + 4 hours + 5 hours + 6 hours + 12 hours + 24 hours diff --git a/app/src/main/res/xml/security_preferences.xml b/app/src/main/res/xml/security_preferences.xml index 636f1798..d1f2fbc2 100644 --- a/app/src/main/res/xml/security_preferences.xml +++ b/app/src/main/res/xml/security_preferences.xml @@ -11,4 +11,22 @@ app:title="@string/settings_secure_mode_title" app:summary="@string/settings_secure_mode_summary" /> + + + + + + \ No newline at end of file