From a1be4f72849142ef99b32f7f782347d8ca22e45c Mon Sep 17 00:00:00 2001 From: stefan-niedermann Date: Sat, 25 Jan 2020 13:05:51 +0100 Subject: [PATCH] #365 Password protection --- .../android/activity/LockedActivity.java | 49 ++++++++++++++----- 1 file changed, 37 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/it/niedermann/owncloud/notes/android/activity/LockedActivity.java b/app/src/main/java/it/niedermann/owncloud/notes/android/activity/LockedActivity.java index 1aff8c05..09e6939e 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/android/activity/LockedActivity.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/android/activity/LockedActivity.java @@ -3,6 +3,7 @@ package it.niedermann.owncloud.notes.android.activity; import android.app.KeyguardManager; import android.content.Context; import android.content.Intent; +import android.os.Bundle; import android.util.Log; import androidx.annotation.Nullable; @@ -18,23 +19,32 @@ public abstract class LockedActivity extends AppCompatActivity { 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(isTaskRoot()) { + askToUnlock(); + } + } + @Override protected void onResume() { super.onResume(); - Thread.currentThread().setUncaughtExceptionHandler(new ExceptionHandler(this)); + if (!isTaskRoot()) { + askToUnlock(); + } + } - if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) { - if (Notes.isLocked()) { - KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE); - if (keyguardManager != null) { - Intent i = keyguardManager.createConfirmDeviceCredentialIntent(getString(R.string.unlock_notes), null); - i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); - startActivityForResult(i, REQUEST_CODE_UNLOCK); - } else { - Log.e(TAG, "Keyguard manager is null"); - } - } + @Override + protected void onStop() { + super.onStop(); + if (isTaskRoot()) { + Notes.lock(); } } @@ -54,4 +64,19 @@ public abstract class LockedActivity extends AppCompatActivity { } } } + + private void askToUnlock() { + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) { + if (Notes.isLocked()) { + KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE); + if (keyguardManager != null) { + Intent i = keyguardManager.createConfirmDeviceCredentialIntent(getString(R.string.unlock_notes), null); + i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); + startActivityForResult(i, REQUEST_CODE_UNLOCK); + } else { + Log.e(TAG, "Keyguard manager is null"); + } + } + } + } }