/* * Copyright (C) 2025 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.eyedropper.util import android.content.Context import android.graphics.PixelFormat import android.hardware.display.DisplayManager import android.view.Gravity import android.view.View import android.view.WindowManager import javax.inject.Inject import javax.inject.Singleton @Singleton class WindowHelper @Inject constructor( private val displayManager: DisplayManager ) { private fun getFocusedTransparentOverlayLayoutParams(): WindowManager.LayoutParams { val layoutFlags = WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN or // Ignore parent constraints WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED or WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS // Extend out of screen val layoutParams = WindowManager.LayoutParams( WindowManager.LayoutParams.MATCH_PARENT, // Span entire width WindowManager.LayoutParams.MATCH_PARENT, // Span entire height WindowManager.LayoutParams.TYPE_SCREENSHOT, // Highest Z-order to emulate pointer layoutFlags, PixelFormat.TRANSLUCENT // Make window background transparent ).apply { title = "EyeDropperOverlay" gravity = Gravity.TOP or Gravity.START // Set origin to be top left } return layoutParams } fun addFocusedTransparentOverlay(windowManager: WindowManager, view: View) { val layoutParams = getFocusedTransparentOverlayLayoutParams() windowManager.addView(view, layoutParams) } fun removeView(windowManager: WindowManager, view: View) { windowManager.removeView(view) } fun getWindowContext(context: Context, displayId: Int): Context { val defaultDisplay = displayManager.getDisplay(displayId) ?: throw IllegalArgumentException("Display $displayId not found") val displayContext = context.createDisplayContext(defaultDisplay) return displayContext.createWindowContext(WindowManager.LayoutParams.TYPE_SCREENSHOT, null) } fun getWindowManager(displayContext: Context): WindowManager { return displayContext.getSystemService(WindowManager::class.java) } }