|
| 1 | +package com.freeraspreactnative |
| 2 | + |
| 3 | +import android.annotation.SuppressLint |
| 4 | +import android.annotation.TargetApi |
| 5 | +import android.app.Activity |
| 6 | +import android.app.Activity.ScreenCaptureCallback |
| 7 | +import android.content.Context |
| 8 | +import android.content.pm.PackageManager |
| 9 | +import android.os.Build |
| 10 | +import android.util.Log |
| 11 | +import android.view.WindowManager.SCREEN_RECORDING_STATE_VISIBLE |
| 12 | +import androidx.annotation.RequiresApi |
| 13 | +import androidx.core.content.ContextCompat |
| 14 | + |
| 15 | +import com.aheaditec.talsec_security.security.api.Talsec |
| 16 | +import java.util.function.Consumer |
| 17 | + |
| 18 | +@RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE) |
| 19 | +internal object ScreenProtector { |
| 20 | + private const val TAG = "TalsecScreenProtector" |
| 21 | + private const val SCREEN_CAPTURE_PERMISSION = "android.permission.DETECT_SCREEN_CAPTURE" |
| 22 | + private const val SCREEN_RECORDING_PERMISSION = "android.permission.DETECT_SCREEN_RECORDING" |
| 23 | + private var registered = false |
| 24 | + private val screenCaptureCallback = ScreenCaptureCallback { Talsec.onScreenshotDetected() } |
| 25 | + private val screenRecordCallback: Consumer<Int> = Consumer<Int> { state -> |
| 26 | + if (state == SCREEN_RECORDING_STATE_VISIBLE) { |
| 27 | + Talsec.onScreenRecordingDetected() |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Registers screenshot and screen recording detector with the given activity |
| 33 | + * |
| 34 | + * **IMPORTANT**: android.permission.DETECT_SCREEN_CAPTURE and |
| 35 | + * android.permission.DETECT_SCREEN_RECORDING must be |
| 36 | + * granted for the app in the AndroidManifest.xml |
| 37 | + */ |
| 38 | + internal fun register(activity: Activity) { |
| 39 | + if (!FreeraspReactNativeModule.talsecStarted || registered) { |
| 40 | + return |
| 41 | + } |
| 42 | + |
| 43 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { |
| 44 | + registerScreenCapture(activity) |
| 45 | + } |
| 46 | + |
| 47 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM) { |
| 48 | + registerScreenRecording(activity) |
| 49 | + } |
| 50 | + registered = true |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Register Talsec Screen Capture (screenshot) Detector for given activity instance. |
| 55 | + * The MainActivity of the app is registered by the plugin itself, other |
| 56 | + * activities bust be registered manually as described in the integration guide. |
| 57 | + * |
| 58 | + * Missing permission is suppressed because the decision to use the screen |
| 59 | + * capture API is made by developer, and not enforced by the library. |
| 60 | + * |
| 61 | + * **IMPORTANT**: android.permission.DETECT_SCREEN_CAPTURE (API 34+) must be |
| 62 | + * granted for the app in the AndroidManifest.xml |
| 63 | + */ |
| 64 | + @RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE) |
| 65 | + @SuppressLint("MissingPermission") |
| 66 | + private fun registerScreenCapture(activity: Activity) { |
| 67 | + val context = activity.applicationContext |
| 68 | + if (!hasPermission(context, SCREEN_CAPTURE_PERMISSION)) { |
| 69 | + reportMissingPermission("screenshot", SCREEN_CAPTURE_PERMISSION) |
| 70 | + return |
| 71 | + } |
| 72 | + |
| 73 | + activity.registerScreenCaptureCallback(context.mainExecutor, screenCaptureCallback) |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Register Talsec Screen Recording Detector for given activity instance. |
| 78 | + * The MainActivity of the app is registered by the plugin itself, other |
| 79 | + * activities bust be registered manually as described in the integration guide. |
| 80 | + * |
| 81 | + * Missing permission is suppressed because the decision to use the screen |
| 82 | + * capture API is made by developer, and not enforced by the library. |
| 83 | + * |
| 84 | + * **IMPORTANT**: android.permission.DETECT_SCREEN_RECORDING (API 35+) must be |
| 85 | + * granted for the app in the AndroidManifest.xml |
| 86 | + */ |
| 87 | + @SuppressLint("MissingPermission") |
| 88 | + @RequiresApi(Build.VERSION_CODES.VANILLA_ICE_CREAM) |
| 89 | + private fun registerScreenRecording(activity: Activity) { |
| 90 | + val context = activity.applicationContext |
| 91 | + if (!hasPermission(context, SCREEN_RECORDING_PERMISSION)) { |
| 92 | + reportMissingPermission("screen record", SCREEN_RECORDING_PERMISSION) |
| 93 | + return |
| 94 | + } |
| 95 | + |
| 96 | + val initialState = activity.windowManager.addScreenRecordingCallback( |
| 97 | + context.mainExecutor, screenRecordCallback |
| 98 | + ) |
| 99 | + screenRecordCallback.accept(initialState) |
| 100 | + |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Unregisters screenshot and screen recording detector with the given activity |
| 105 | + * |
| 106 | + * **IMPORTANT**: android.permission.DETECT_SCREEN_CAPTURE and |
| 107 | + * android.permission.DETECT_SCREEN_RECORDING must be |
| 108 | + * granted for the app in the AndroidManifest.xml |
| 109 | + */ |
| 110 | + @SuppressLint("MissingPermission") |
| 111 | + @RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE) |
| 112 | + internal fun unregister(activity: Activity) { |
| 113 | + if (!FreeraspReactNativeModule.talsecStarted || !registered) { |
| 114 | + return |
| 115 | + } |
| 116 | + |
| 117 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { |
| 118 | + unregisterScreenCapture(activity) |
| 119 | + } |
| 120 | + |
| 121 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM) { |
| 122 | + unregisterScreenRecording(activity) |
| 123 | + } |
| 124 | + registered = false |
| 125 | + } |
| 126 | + |
| 127 | + // Missing permission is suppressed because the decision to use the screen capture API is made |
| 128 | + // by developer, and not enforced by the library. |
| 129 | + @SuppressLint("MissingPermission") |
| 130 | + @RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE) |
| 131 | + private fun unregisterScreenCapture(activity: Activity) { |
| 132 | + val context = activity.applicationContext |
| 133 | + if (!hasPermission(context, SCREEN_CAPTURE_PERMISSION)) { |
| 134 | + return |
| 135 | + } |
| 136 | + activity.unregisterScreenCaptureCallback(screenCaptureCallback) |
| 137 | + } |
| 138 | + |
| 139 | + // Missing permission is suppressed because the decision to use the screen capture API is made |
| 140 | + // by developer, and not enforced by the library. |
| 141 | + @SuppressLint("MissingPermission") |
| 142 | + @RequiresApi(Build.VERSION_CODES.VANILLA_ICE_CREAM) |
| 143 | + private fun unregisterScreenRecording(activity: Activity) { |
| 144 | + val context = activity.applicationContext |
| 145 | + if (!hasPermission(context, SCREEN_RECORDING_PERMISSION)) { |
| 146 | + return |
| 147 | + } |
| 148 | + |
| 149 | + activity.windowManager?.removeScreenRecordingCallback(screenRecordCallback) |
| 150 | + } |
| 151 | + |
| 152 | + private fun hasPermission(context: Context, permission: String): Boolean { |
| 153 | + return ContextCompat.checkSelfPermission( |
| 154 | + context, permission |
| 155 | + ) == PackageManager.PERMISSION_GRANTED |
| 156 | + } |
| 157 | + |
| 158 | + private fun reportMissingPermission(protectionType: String, permission: String) { |
| 159 | + Log.e( |
| 160 | + TAG, |
| 161 | + "Failed to register $protectionType callback. Check if $permission permission is granted in AndroidManifest.xml" |
| 162 | + ) |
| 163 | + } |
| 164 | +} |
0 commit comments