From 77f06b962d85e88ce00ca61060d9209353a9b3be Mon Sep 17 00:00:00 2001
From: Benoit Marty <benoit@matrix.org>
Date: Mon, 28 Sep 2020 16:57:36 +0200
Subject: [PATCH] PIN code: request PIN code if phone has been locked

---
 CHANGES.md                                     |  2 +-
 .../java/im/vector/app/VectorApplication.kt    | 18 ++++++++++++++++++
 .../im/vector/app/features/pin/PinLocker.kt    |  5 +++++
 3 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/CHANGES.md b/CHANGES.md
index 813450fd0c..25da8f0108 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -5,7 +5,7 @@ Features ✨:
  -
 
 Improvements 🙌:
- -
+ - PIN code: request PIN code if phone has been locked
 
 Bugfix 🐛:
  - Fix Splash layout on small screens
diff --git a/vector/src/main/java/im/vector/app/VectorApplication.kt b/vector/src/main/java/im/vector/app/VectorApplication.kt
index 4b0ef66459..4f89763cda 100644
--- a/vector/src/main/java/im/vector/app/VectorApplication.kt
+++ b/vector/src/main/java/im/vector/app/VectorApplication.kt
@@ -17,7 +17,10 @@
 package im.vector.app
 
 import android.app.Application
+import android.content.BroadcastReceiver
 import android.content.Context
+import android.content.Intent
+import android.content.IntentFilter
 import android.content.res.Configuration
 import android.os.Handler
 import android.os.HandlerThread
@@ -92,6 +95,15 @@ class VectorApplication :
     // font thread handler
     private var fontThreadHandler: Handler? = null
 
+    private val powerKeyReceiver = object : BroadcastReceiver() {
+        override fun onReceive(context: Context?, intent: Intent) {
+            if (intent.action == Intent.ACTION_SCREEN_OFF
+                    && vectorPreferences.useFlagPinCode()) {
+                pinLocker.screenIsOff()
+            }
+        }
+    }
+
     override fun onCreate() {
         enableStrictModeIfNeeded()
         super.onCreate()
@@ -163,6 +175,12 @@ class VectorApplication :
         ProcessLifecycleOwner.get().lifecycle.addObserver(pinLocker)
         // This should be done as early as possible
         // initKnownEmojiHashSet(appContext)
+
+        applicationContext.registerReceiver(powerKeyReceiver, IntentFilter().apply {
+            // Looks like i cannot receive OFF, if i don't have both ON and OFF
+            addAction(Intent.ACTION_SCREEN_OFF)
+            addAction(Intent.ACTION_SCREEN_ON)
+        })
     }
 
     private fun enableStrictModeIfNeeded() {
diff --git a/vector/src/main/java/im/vector/app/features/pin/PinLocker.kt b/vector/src/main/java/im/vector/app/features/pin/PinLocker.kt
index 3fc4152ee6..adc618d82e 100644
--- a/vector/src/main/java/im/vector/app/features/pin/PinLocker.kt
+++ b/vector/src/main/java/im/vector/app/features/pin/PinLocker.kt
@@ -81,6 +81,11 @@ class PinLocker @Inject constructor(
         computeState()
     }
 
+    fun screenIsOff() {
+        shouldBeLocked = true
+        computeState()
+    }
+
     @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
     fun entersForeground() {
         val timeElapsedSinceBackground = SystemClock.elapsedRealtime() - entersBackgroundTs