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

View File

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

View File

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

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.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

View File

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

View File

@ -466,4 +466,42 @@
<item>@string/exclude_domain</item>
</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>

View File

@ -597,6 +597,9 @@
<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_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>
@ -1171,5 +1174,21 @@
<string name="material_you_notification_title">Applying Material You</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>

View File

@ -11,4 +11,22 @@
app:title="@string/settings_secure_mode_title"
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>