mirror of
https://codeberg.org/monocles/monocles_chat.git
synced 2025-01-15 22:22:22 +01:00
App lock
This commit is contained in:
parent
fe677337b7
commit
bea5183c05
6 changed files with 157 additions and 3 deletions
|
@ -128,9 +128,10 @@ dependencies {
|
|||
implementation "com.daimajia.swipelayout:library:1.2.0@aar"
|
||||
implementation 'com.nineoldandroids:library:2.4.0'
|
||||
implementation "androidx.core:core-ktx:1.12.0"
|
||||
implementation "androidx.compose.material3:material3-android:1.2.0-rc01"
|
||||
implementation "androidx.compose.material3:material3-android:1.2.0"
|
||||
implementation "androidx.emoji2:emoji2-emojipicker:1.4.0"
|
||||
implementation 'com.github.Priyansh-Kedia:OpenGraphParser:2.5.6'
|
||||
implementation 'androidx.preference:preference:1.2.1'
|
||||
}
|
||||
|
||||
ext {
|
||||
|
|
|
@ -8,6 +8,7 @@ import android.os.Bundle;
|
|||
import android.util.Log;
|
||||
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import android.preference.PreferenceManager;
|
||||
|
||||
import eu.siacs.conversations.Config;
|
||||
import eu.siacs.conversations.R;
|
||||
|
@ -15,8 +16,16 @@ import eu.siacs.conversations.ui.ConversationsActivity;
|
|||
import eu.siacs.conversations.ui.util.IntroHelper;
|
||||
public class StartUI extends AppCompatActivity {
|
||||
|
||||
private static final long LOCK_TIME = 30_000;
|
||||
private static boolean lockedPreference = false;
|
||||
private static boolean isLocked = true;
|
||||
private static long lastInteraction = 0;
|
||||
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
final var prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
|
||||
lockedPreference = prefs.getBoolean("pref_key_lock", false);
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_start_ui);
|
||||
IntroHelper.showIntro(this, false);
|
||||
|
@ -39,4 +48,24 @@ public class StartUI extends AppCompatActivity {
|
|||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
public static void setLockedPreference(boolean lockedPreference) {
|
||||
Log.i("App lock", "New locked preference: " + lockedPreference);
|
||||
StartUI.lockedPreference = lockedPreference;
|
||||
}
|
||||
|
||||
public static boolean isLocked() {
|
||||
if (!isLocked && System.currentTimeMillis() > (LOCK_TIME + lastInteraction)) {
|
||||
isLocked = true;
|
||||
}
|
||||
return lockedPreference && isLocked;
|
||||
}
|
||||
|
||||
public static void unlock() {
|
||||
isLocked = false;
|
||||
}
|
||||
|
||||
public static void updateLastInteraction() {
|
||||
lastInteraction = System.currentTimeMillis();
|
||||
}
|
||||
}
|
||||
|
|
114
src/main/java/de/monocles/chat/LockedActivity.java
Normal file
114
src/main/java/de/monocles/chat/LockedActivity.java
Normal file
|
@ -0,0 +1,114 @@
|
|||
package de.monocles.chat;
|
||||
|
||||
import android.app.KeyguardManager;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.view.WindowManager;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.preference.PreferenceManager;
|
||||
|
||||
|
||||
import de.monocles.chat.ui.StartUI;
|
||||
import eu.siacs.conversations.R;
|
||||
import eu.siacs.conversations.utils.ExceptionHandler;
|
||||
|
||||
public abstract class LockedActivity extends StartUI {
|
||||
|
||||
private static final String TAG = LockedActivity.class.getSimpleName();
|
||||
|
||||
private static final int REQUEST_CODE_UNLOCK = 100;
|
||||
|
||||
|
||||
@Override
|
||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
Thread.currentThread().setUncaughtExceptionHandler(new ExceptionHandler(this));
|
||||
|
||||
if (PreferenceManager.getDefaultSharedPreferences(this).getBoolean("screen_security", false)) {
|
||||
getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);
|
||||
}
|
||||
|
||||
if (isTaskRoot()) {
|
||||
askToUnlock();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
|
||||
if (!isTaskRoot()) {
|
||||
askToUnlock();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStop() {
|
||||
super.onStop();
|
||||
if (isTaskRoot()) {
|
||||
StartUI.updateLastInteraction();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBackPressed() {
|
||||
super.onBackPressed();
|
||||
StartUI.updateLastInteraction();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void startActivityForResult(Intent intent, int requestCode, @Nullable Bundle options) {
|
||||
StartUI.updateLastInteraction();
|
||||
super.startActivityForResult(intent, requestCode, options);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void startActivityForResult(Intent intent, int requestCode) {
|
||||
StartUI.updateLastInteraction();
|
||||
super.startActivityForResult(intent, requestCode);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void startActivity(Intent intent) {
|
||||
StartUI.updateLastInteraction();
|
||||
super.startActivity(intent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void startActivity(Intent intent, @Nullable Bundle options) {
|
||||
StartUI.updateLastInteraction();
|
||||
super.startActivity(intent, options);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
|
||||
super.onActivityResult(requestCode, resultCode, data);
|
||||
|
||||
if (requestCode == REQUEST_CODE_UNLOCK) {
|
||||
if (resultCode == RESULT_OK) {
|
||||
Log.v(TAG, "Successfully unlocked device");
|
||||
StartUI.unlock();
|
||||
} else {
|
||||
Log.e(TAG, "Result code of unlocking was " + resultCode);
|
||||
finish();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void askToUnlock() {
|
||||
if (StartUI.isLocked()) {
|
||||
final var keyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
|
||||
if (keyguardManager != null) {
|
||||
final var intent = keyguardManager.createConfirmDeviceCredentialIntent(getString(R.string.unlock_app), null);
|
||||
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
|
||||
startActivityForResult(intent, REQUEST_CODE_UNLOCK);
|
||||
} else {
|
||||
Log.e(TAG, "Keyguard manager is null");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -16,7 +16,7 @@ public class ExceptionHandler implements UncaughtExceptionHandler {
|
|||
private final UncaughtExceptionHandler defaultHandler;
|
||||
private final Context context;
|
||||
|
||||
ExceptionHandler(final Context context) {
|
||||
public ExceptionHandler(final Context context) {
|
||||
this.context = context;
|
||||
this.defaultHandler = Thread.getDefaultUncaughtExceptionHandler();
|
||||
}
|
||||
|
|
|
@ -1413,4 +1413,8 @@
|
|||
<string name="subject">Subject</string>
|
||||
<string name="add_subject">Add Subject</string>
|
||||
<string name="recent_threads">Recent threads</string>
|
||||
<string name="pref_value_lock">App lock</string>
|
||||
<string name="settings_lock">App Lock (Beta)</string>
|
||||
<string name="settings_lock_summary">Lock the app with your devices password or fingerprint</string>
|
||||
<string name="unlock_app">Unlock monocles chat</string>
|
||||
</resources>
|
||||
|
|
|
@ -507,7 +507,7 @@
|
|||
android:key="use_internal_updater"
|
||||
android:summary="@string/pref_use_internal_updater_summary"
|
||||
android:title="@string/pref_use_internal_updater" />
|
||||
<!--
|
||||
<!-- TODO: Reactivate
|
||||
<SwitchPreference
|
||||
android:defaultValue="@bool/show_links_inside"
|
||||
android:key="show_links_inside"
|
||||
|
@ -668,6 +668,12 @@
|
|||
android:summary="@string/pref_show_date_in_quotes_summary"
|
||||
android:title="@string/pref_show_date_in_quotes" />
|
||||
-->
|
||||
<SwitchPreference
|
||||
android:defaultValue="@string/pref_value_lock"
|
||||
android:icon="?attr/icon_small_lock"
|
||||
android:key="pref_key_lock"
|
||||
android:summary="@string/settings_lock_summary"
|
||||
android:title="@string/settings_lock" />
|
||||
<SwitchPreference
|
||||
android:defaultValue="@bool/use_bundled_emoji"
|
||||
android:key="use_bundled_emoji"
|
||||
|
|
Loading…
Reference in a new issue