1

I am creating an android application and I have to implement a feature where my app has "Display on other apps" permission, and from Accessibility Service it need to display a transparent camera overlay over other apps.

What is required?

Display a Transparent Camera Overlay over WhatsApp app from my app as soon as WhatsApp is open. Screenshot attached.

enter image description here

What I have done

1- Added Display over apps permission working

2- Added Accessibility Service which will trigger onAccessibilityEvent whenever WhatsApp is opened.

What I tried to add camera overlay when WhatsApp is open

1- I added Capture Preview class mentioned here

2- Tried using cameraX Preview but no luck

Below is my AccessibilityService code from where I want to start a Transparent Camera Overlay

class CameraAccessibilityService : AccessibilityService() {
    val tinyDB: TinyDB by inject()

    override fun onInterrupt() {}

    override fun onAccessibilityEvent(event: AccessibilityEvent?) {
        if (event?.eventType?.equals(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED)!! || event?.eventType?.equals(
                AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
            )!!
        ) {
            if (event.packageName == Constants.WHATSAPP_PKG_NAME || event.packageName == Constants.WHATSAPP_BUSINESS_PKG_NAME) {
                if (tinyDB.getBoolean(Constants.isCameraOverLayEnabled)) {
                    Toast.makeText(applicationContext, "Setting ON", Toast.LENGTH_SHORT)
                        .show()
                    val preview=CapturePreview(this)
                    val wm = getSystemService(Context.WINDOW_SERVICE) as WindowManager

                    val metrics = DisplayMetrics()
                    wm.defaultDisplay.getMetrics(metrics)


                    val params = WindowManager.LayoutParams(
                        WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,  // TYPE_SYSTEM_ALERT is denied in apiLevel >=19
                        WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN or WindowManager.LayoutParams.FLAG_FULLSCREEN,
                        PixelFormat.TRANSLUCENT
                    )
                    params.title = "Touhou"

                    wm.addView(preview, params)

                } else {
                    Toast.makeText(applicationContext, "Setting OFF", Toast.LENGTH_SHORT)
                        .show()
                }

            }

        }
    }

    override fun onServiceConnected() {
        this.serviceInfo.apply {
            eventTypes =
                AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED or AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
            packageNames =
                arrayOf(Constants.WHATSAPP_PKG_NAME, Constants.WHATSAPP_BUSINESS_PKG_NAME)
            feedbackType = AccessibilityServiceInfo.FEEDBACK_ALL_MASK
            notificationTimeout = 100
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.d(TAG, "Save Status CameraAccessibilityService Destroyed")
    }



}

Can somebody please help me out with this. Any help will be appreciated.

User
  • 692
  • 2
  • 11
  • 29
  • I am not sure how display on other apps feature works but I would go for a very naive first approach. What happens If you set your preview view's and the activity's(that include the preview view) background to transparent? Maybe this would help: https://stackoverflow.com/a/2700683/2895045 – Orcun Sep 08 '21 at 08:58
  • "but no luck" -- this is not a very useful explanation of your problems. What happened when you tried this? Did the app crash (and, if so, what is the stack trace)? Did you get a compiler error? Did things run but not give you the desired output (and, if so, what output did you get)? – CommonsWare Sep 09 '21 at 19:39
  • @CommonsWare "but no luck" obvious meaning is it didn't worked. No output displayed. the camera shows up same as normal with no transparency (i.e less opacity) – User Sep 09 '21 at 19:51
  • 1
    "obvious meaning is it didn't worked" -- yes, but the details matter. "the camera shows up same as normal with no transparency (i.e less opacity)" -- at best, that might be possible if you are using a `TextureView` for the preview. AFAIK, `SurfaceView` does not support transparency. – CommonsWare Sep 09 '21 at 19:58
  • @User did you manage to do this? – user16930239 Sep 25 '21 at 22:26

1 Answers1

0

try this

SurfaceView surface1 = (SurfaceView)findViewById(R.id.surface1);
surface1.setZOrderOnTop(true);
SurfaceHolder holder1 = surface1.getHolder();
holder1.setFormat(PixelFormat.TRANSPARENT);
user16930239
  • 6,319
  • 2
  • 9
  • 33