diff options
Diffstat (limited to 'app/src/main/java')
6 files changed, 76 insertions, 10 deletions
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 @@ -283,6 +283,13 @@ class AppModule { } @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, @Named("dark_theme") SharedPreferences darkThemeSharedPreferences, 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"; |