/* * 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.desktop import android.content.ComponentName import android.content.ServiceConnection import android.graphics.Bitmap import android.os.IBinder import android.util.Log import kotlinx.coroutines.Deferred /** * Encapsulates logic to manage connection to [DesktopControllerService] service. * * This connection is responsible for obtaining the service instance upon connection * and triggering the display of the UI overlay. The caller is responsible for * calling bindService] with this instance and unbindService once the connection is unneeded. * * @param onSelectColor Callback invoked when a color is selected in the overlay. * @param onAbort Callback invoked if the overlay is dismissed without selection. */ class DesktopServiceConnection( private val deferredScreenshotsMap: Map>, private val onSelectColor: (Int) -> Unit, private val onAbort: () -> Unit, ) : ServiceConnection { private lateinit var desktopController: DesktopControllerService var isBound: Boolean = false override fun onServiceConnected(className: ComponentName, service: IBinder) { Log.d("DesktopConnection", "onServiceConnected called") desktopController = (service as DesktopControllerService.LocalBinder).getService() desktopController.displayIdToDeferredScreenshotMap = deferredScreenshotsMap desktopController.showUiOverlay(onSelectColor, onAbort) isBound = true // Set in here instead of after call b/c async } override fun onServiceDisconnected(arg0: ComponentName) { isBound = false // Also set isBound here to handle unexpected interruptions } }