New options: App Lock and App Lock Timeout.

This commit is contained in:
Alex Ning 2021-09-03 16:57:12 +08:00
parent c37d372b81
commit 0559176a8b
9 changed files with 151 additions and 10 deletions

View File

@ -282,6 +282,13 @@ class AppModule {
return mApplication.getSharedPreferences(SharedPreferencesUtils.POST_DETAILS_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE); 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 @Provides
@Singleton @Singleton
CustomThemeWrapper provideCustomThemeWrapper(@Named("light_theme") SharedPreferences lightThemeSharedPreferences, CustomThemeWrapper provideCustomThemeWrapper(@Named("light_theme") SharedPreferences lightThemeSharedPreferences,

View File

@ -29,6 +29,7 @@ import javax.inject.Named;
import ml.docilealligator.infinityforreddit.activities.LockScreenActivity; import ml.docilealligator.infinityforreddit.activities.LockScreenActivity;
import ml.docilealligator.infinityforreddit.broadcastreceivers.NetworkWifiStatusReceiver; import ml.docilealligator.infinityforreddit.broadcastreceivers.NetworkWifiStatusReceiver;
import ml.docilealligator.infinityforreddit.broadcastreceivers.WallpaperChangeReceiver; import ml.docilealligator.infinityforreddit.broadcastreceivers.WallpaperChangeReceiver;
import ml.docilealligator.infinityforreddit.events.ChangeAppLockEvent;
import ml.docilealligator.infinityforreddit.events.ChangeNetworkStatusEvent; import ml.docilealligator.infinityforreddit.events.ChangeNetworkStatusEvent;
import ml.docilealligator.infinityforreddit.events.ToggleSecureModeEvent; import ml.docilealligator.infinityforreddit.events.ToggleSecureModeEvent;
import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils;
@ -37,11 +38,13 @@ import ml.docilealligator.infinityforreddit.utils.Utils;
public class Infinity extends Application implements LifecycleObserver { public class Infinity extends Application implements LifecycleObserver {
private AppComponent mAppComponent; private AppComponent mAppComponent;
private NetworkWifiStatusReceiver mNetworkWifiStatusReceiver; private NetworkWifiStatusReceiver mNetworkWifiStatusReceiver;
private boolean lock = false; private boolean appLock;
private long appLockTimeout;
private boolean canStartLockScreenActivity = false;
private boolean isSecureMode; private boolean isSecureMode;
@Inject @Inject
@Named("default") @Named("security")
SharedPreferences mSharedPreferences; SharedPreferences mSecuritySharedPreferences;
@Override @Override
public void onCreate() { public void onCreate() {
@ -53,7 +56,9 @@ public class Infinity extends Application implements LifecycleObserver {
mAppComponent.inject(this); 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() { registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
@Override @Override
@ -70,11 +75,13 @@ public class Infinity extends Application implements LifecycleObserver {
@Override @Override
public void onActivityResumed(@NonNull Activity activity) { public void onActivityResumed(@NonNull Activity activity) {
if (lock && !(activity instanceof LockScreenActivity)) { if (canStartLockScreenActivity && appLock
lock = false; && System.currentTimeMillis() - mSecuritySharedPreferences.getLong(SharedPreferencesUtils.LAST_UNLOCK_TIME, 0) >= appLockTimeout
&& !(activity instanceof LockScreenActivity)) {
Intent intent = new Intent(activity, LockScreenActivity.class); Intent intent = new Intent(activity, LockScreenActivity.class);
activity.startActivity(intent); activity.startActivity(intent);
} }
canStartLockScreenActivity = false;
} }
@Override @Override
@ -125,7 +132,7 @@ public class Infinity extends Application implements LifecycleObserver {
@OnLifecycleEvent(Lifecycle.Event.ON_START) @OnLifecycleEvent(Lifecycle.Event.ON_START)
public void appInForeground() { public void appInForeground() {
lock = true; canStartLockScreenActivity = true;
} }
@OnLifecycleEvent(Lifecycle.Event.ON_STOP) @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
@ -141,4 +148,10 @@ public class Infinity extends Application implements LifecycleObserver {
public void onToggleSecureModeEvent(ToggleSecureModeEvent secureModeEvent) { public void onToggleSecureModeEvent(ToggleSecureModeEvent secureModeEvent) {
isSecureMode = secureModeEvent.isSecureMode; isSecureMode = secureModeEvent.isSecureMode;
} }
@Subscribe
public void onChangeAppLockEvent(ChangeAppLockEvent changeAppLockEvent) {
appLock = changeAppLockEvent.appLock;
appLockTimeout = changeAppLockEvent.appLockTimeout;
}
} }

View File

@ -24,6 +24,7 @@ import butterknife.ButterKnife;
import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.Infinity;
import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper;
import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils;
public class LockScreenActivity extends BaseActivity { public class LockScreenActivity extends BaseActivity {
@ -35,6 +36,9 @@ public class LockScreenActivity extends BaseActivity {
@Named("default") @Named("default")
SharedPreferences mSharedPreferences; SharedPreferences mSharedPreferences;
@Inject @Inject
@Named("security")
SharedPreferences mSecurityPreferences;
@Inject
CustomThemeWrapper mCustomThemeWrapper; CustomThemeWrapper mCustomThemeWrapper;
@Override @Override
@ -67,6 +71,7 @@ public class LockScreenActivity extends BaseActivity {
public void onAuthenticationSucceeded( public void onAuthenticationSucceeded(
@NonNull BiometricPrompt.AuthenticationResult result) { @NonNull BiometricPrompt.AuthenticationResult result) {
super.onAuthenticationSucceeded(result); super.onAuthenticationSucceeded(result);
saveUnlockTime();
finish(); finish();
} }
}); });
@ -78,10 +83,16 @@ public class LockScreenActivity extends BaseActivity {
biometricPrompt.authenticate(promptInfo); biometricPrompt.authenticate(promptInfo);
} else { } else {
saveUnlockTime();
finish(); finish();
} }
} }
private void saveUnlockTime() {
mSecurityPreferences.edit().putLong(SharedPreferencesUtils.LAST_UNLOCK_TIME, System.currentTimeMillis()).apply();
}
@Override @Override
protected SharedPreferences getDefaultSharedPreferences() { protected SharedPreferences getDefaultSharedPreferences() {
return mSharedPreferences; return mSharedPreferences;

View File

@ -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;
}
}

View File

@ -11,7 +11,9 @@ import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.biometric.BiometricPrompt; import androidx.biometric.BiometricPrompt;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
import androidx.preference.ListPreference;
import androidx.preference.PreferenceFragmentCompat; import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceManager;
import androidx.preference.SwitchPreference; import androidx.preference.SwitchPreference;
import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.EventBus;
@ -23,6 +25,7 @@ import javax.inject.Named;
import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.Infinity;
import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.events.ChangeAppLockEvent;
import ml.docilealligator.infinityforreddit.events.ChangeRequireAuthToAccountSectionEvent; import ml.docilealligator.infinityforreddit.events.ChangeRequireAuthToAccountSectionEvent;
import ml.docilealligator.infinityforreddit.events.ToggleSecureModeEvent; import ml.docilealligator.infinityforreddit.events.ToggleSecureModeEvent;
import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils;
@ -36,12 +39,16 @@ public class SecurityPreferenceFragment extends PreferenceFragmentCompat {
@Override @Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
PreferenceManager preferenceManager = getPreferenceManager();
preferenceManager.setSharedPreferencesName(SharedPreferencesUtils.SECURITY_SHARED_PREFERENCES_FILE);
setPreferencesFromResource(R.xml.security_preferences, rootKey); setPreferencesFromResource(R.xml.security_preferences, rootKey);
((Infinity) activity.getApplication()).getAppComponent().inject(this); ((Infinity) activity.getApplication()).getAppComponent().inject(this);
SwitchPreference requireAuthToAccountSectionSwitch = findPreference(SharedPreferencesUtils.REQUIRE_AUTHENTICATION_TO_GO_TO_ACCOUNT_SECTION_IN_NAVIGATION_DRAWER); SwitchPreference requireAuthToAccountSectionSwitch = findPreference(SharedPreferencesUtils.REQUIRE_AUTHENTICATION_TO_GO_TO_ACCOUNT_SECTION_IN_NAVIGATION_DRAWER);
SwitchPreference secureModeSwitch = findPreference(SharedPreferencesUtils.SECURE_MODE); SwitchPreference secureModeSwitch = findPreference(SharedPreferencesUtils.SECURE_MODE);
SwitchPreference appLockSwitch = findPreference(SharedPreferencesUtils.APP_LOCK);
ListPreference appLockTimeoutListPreference = findPreference(SharedPreferencesUtils.APP_LOCK_TIMEOUT);
if (requireAuthToAccountSectionSwitch != null) { if (requireAuthToAccountSectionSwitch != null) {
requireAuthToAccountSectionSwitch.setOnPreferenceChangeListener((preference, newValue) -> { requireAuthToAccountSectionSwitch.setOnPreferenceChangeListener((preference, newValue) -> {
@ -56,6 +63,18 @@ public class SecurityPreferenceFragment extends PreferenceFragmentCompat {
return true; 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 @Override

View File

@ -132,7 +132,6 @@ public class SharedPreferencesUtils {
public static final String ENABLE_SWIPE_ACTION = "enable_swipe_action"; public static final String ENABLE_SWIPE_ACTION = "enable_swipe_action";
public static final String SWIPE_ACTION_THRESHOLD = "swipe_action_threshold"; public static final String SWIPE_ACTION_THRESHOLD = "swipe_action_threshold";
public static final String PULL_TO_REFRESH = "pull_to_refresh"; 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 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 POST_COMPACT_LAYOUT_TOOLBAR_HIDDEN_BY_DEFAULT = "post_compact_layout_toolbar_hidden_by_default";
public static final String SECURITY = "security"; 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 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 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 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 LOCK_SCREEN_ANIMATION = "lock_screen_animation";
public static final String DEFAULT_PREFERENCES_FILE = "ml.docilealligator.infinityforreddit_preferences"; 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_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 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 //Legacy Settings
public static final String MAIN_PAGE_TAB_1_TITLE_LEGACY = "main_page_tab_1_title"; 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"; public static final String MAIN_PAGE_TAB_2_TITLE_LEGACY = "main_page_tab_2_title";

View File

@ -466,4 +466,42 @@
<item>@string/exclude_domain</item> <item>@string/exclude_domain</item>
</string-array> </string-array>
<string-array name="settings_app_lock_timeout">
<item>@string/app_lock_timeout_immediately</item>
<item>@string/app_lock_timeout_1_min</item>
<item>@string/app_lock_timeout_2_mins</item>
<item>@string/app_lock_timeout_5_mins</item>
<item>@string/app_lock_timeout_10_mins</item>
<item>@string/app_lock_timeout_15_mins</item>
<item>@string/app_lock_timeout_20_mins</item>
<item>@string/app_lock_timeout_30_mins</item>
<item>@string/app_lock_timeout_1_hour</item>
<item>@string/app_lock_timeout_2_hours</item>
<item>@string/app_lock_timeout_3_hours</item>
<item>@string/app_lock_timeout_4_hours</item>
<item>@string/app_lock_timeout_5_hours</item>
<item>@string/app_lock_timeout_6_hours</item>
<item>@string/app_lock_timeout_12_hours</item>
<item>@string/app_lock_timeout_24_hours</item>
</string-array>
<string-array name="settings_app_lock_timeout_values">
<item>0</item>
<item>60000</item>
<item>120000</item>
<item>300000</item>
<item>600000</item>
<item>900000</item>
<item>1200000</item>
<item>1800000</item>
<item>3600000</item>
<item>7200000</item>
<item>10800000</item>
<item>14400000</item>
<item>18000000</item>
<item>21600000</item>
<item>43200000</item>
<item>86400000</item>
</string-array>
</resources> </resources>

View File

@ -597,6 +597,9 @@
<string name="settings_secure_mode_title">Secure Mode</string> <string name="settings_secure_mode_title">Secure Mode</string>
<string name="settings_secure_mode_summary">Screenshot and video recording are not allowed. No preview in recent app screen.</string> <string name="settings_secure_mode_summary">Screenshot and video recording are not allowed. No preview in recent app screen.</string>
<string name="settings_credits_lock_screen_animation_title">Lock Screen Animation</string> <string name="settings_credits_lock_screen_animation_title">Lock Screen Animation</string>
<string name="settings_app_lock_title">App Lock</string>
<string name="settings_app_lock_summary">Require authentication before using the app</string>
<string name="settings_app_lock_timeout_title">App Lock Timeout</string>
<string name="no_link_available">Cannot get the link</string> <string name="no_link_available">Cannot get the link</string>
@ -1171,5 +1174,21 @@
<string name="material_you_notification_title">Applying Material You</string> <string name="material_you_notification_title">Applying Material You</string>
<string name="lock_screen_text">Whoa there!!!</string> <string name="lock_screen_text">Whoa there!!!</string>
<string name="app_lock_timeout_immediately">Immediately</string>
<string name="app_lock_timeout_1_min">1 minute</string>
<string name="app_lock_timeout_2_mins">2 minutes</string>
<string name="app_lock_timeout_5_mins">5 minutes</string>
<string name="app_lock_timeout_10_mins">10 minutes</string>
<string name="app_lock_timeout_15_mins">15 minutes</string>
<string name="app_lock_timeout_20_mins">20 minutes</string>
<string name="app_lock_timeout_30_mins">30 minutes</string>
<string name="app_lock_timeout_1_hour">1 hour</string>
<string name="app_lock_timeout_2_hours">2 hours</string>
<string name="app_lock_timeout_3_hours">3 hours</string>
<string name="app_lock_timeout_4_hours">4 hours</string>
<string name="app_lock_timeout_5_hours">5 hours</string>
<string name="app_lock_timeout_6_hours">6 hours</string>
<string name="app_lock_timeout_12_hours">12 hours</string>
<string name="app_lock_timeout_24_hours">24 hours</string>
</resources> </resources>

View File

@ -11,4 +11,22 @@
app:title="@string/settings_secure_mode_title" app:title="@string/settings_secure_mode_title"
app:summary="@string/settings_secure_mode_summary" /> app:summary="@string/settings_secure_mode_summary" />
<SwitchPreference
app:defaultValue="false"
app:key="app_lock"
app:icon="@drawable/ic_outline_lock_24dp"
app:title="@string/settings_app_lock_title"
app:summary="@string/settings_app_lock_summary" />
<ListPreference
app:defaultValue="600000"
app:entries="@array/settings_app_lock_timeout"
app:entryValues="@array/settings_app_lock_timeout_values"
app:key="app_lock_timeout"
app:icon="@drawable/ic_outline_access_time_24dp"
app:title="@string/settings_app_lock_timeout_title"
app:useSimpleSummaryProvider="true" />
</PreferenceScreen> </PreferenceScreen>