Search code examples
androidsecuritylayoutscreenshotandroid-recents

How to make screen blank in recent apps but allow screenshots


I want to make view solid white or black in recent apps screen but don't want to hide it completely. And also I want to enable screenshot feature without affecting user experience.

I saw the same implemented in whatsapp when fingerprint lock is enabled(beta apk). Anyone here having any idea about how that is implemented?

Please don't mark it as duplicate because following threads did not answer my question

Hide screen in 'Recent Apps List', but allow screenshots,

Question 2,

Android: Customizing recent apps thumbnail (screenshot by default)


Solution

  • After a lot of investigation I came up with following conclusion

    Android 7.0 or older(<= SDK 25):

    Currently it is impossible to achieve this and there are no direct callbacks before system actually takes screenshot of the layout.

    Android 8.0 and newer(>= SDK 26):

    Anyhow adding secure layout flag to window in onPause() and clearing it in onResume() will work exactly as expected.

    To make it common for all activities, In Application class register an Application.ActivityLifecycleCallbacks and add following lines inside specific overridden functions.

    fun onActivityResumed(activity: Activity) {
        activity?.window?.clearFlags(WindowManager.LayoutParams.FLAG_SECURE)
    }
    
    fun onActivityPaused(activity: Activity) {
        activity?.window?.addFlags(WindowManager.LayoutParams.FLAG_SECURE)
    }