From 14b4e2069bc875724c4ec83aa591c8afd6b0ea5c Mon Sep 17 00:00:00 2001
From: Metin Kaya <metin.kaya@arm.com>
Date: Mon, 8 Jan 2024 09:05:06 +0000
Subject: [PATCH] target: Add helper function to check Android screen's locking
 state

Introduce is_screen_locked() which returns true if device screen is
locked and false otherwise.

This will be useful to automate unlocking the screen [1].

Also fix a typo in is_screen_on()'s documentation.

[1] https://github.com/ARM-software/workload-automation/pull/1246

Signed-off-by: Metin Kaya <metin.kaya@arm.com>
---
 devlib/target.py | 5 +++++
 doc/target.rst   | 7 ++++++-
 2 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/devlib/target.py b/devlib/target.py
index fdef1bd..ae2d559 100644
--- a/devlib/target.py
+++ b/devlib/target.py
@@ -2213,6 +2213,11 @@ class AndroidTarget(Target):
     def reboot_bootloader(self, timeout=30):
         self.conn.reboot_bootloader()
 
+    @asyn.asyncf
+    async def is_screen_locked(self):
+        screen_state = await self.execute.asyn('dumpsys window')
+        return 'mDreamingLockscreen=true' in screen_state
+
     @asyn.asyncf
     async def is_screen_on(self):
         output = await self.execute.asyn('dumpsys power')
diff --git a/doc/target.rst b/doc/target.rst
index 17b2bbd..ad641b1 100644
--- a/doc/target.rst
+++ b/doc/target.rst
@@ -747,10 +747,15 @@ Android Target
 
 .. method:: AndroidTarget.is_screen_on()
 
-   Returns ``True`` if the targets screen is currently on and ``False``
+   Returns ``True`` if the target's screen is currently on and ``False``
    otherwise. If the display is in a "Doze" mode or similar always on state,
    this will return ``True``.
 
+.. method:: AndroidTarget.is_screen_locked()
+
+   Returns ``True`` if the target's screen is currently locked and ``False``
+   otherwise.
+
 .. method:: AndroidTarget.wait_for_device(timeout=30)
 
     Returns when the devices becomes available withing the given timeout