1 /*
<lambda>null2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.systemui.screenshot.proxy
18 
19 import android.annotation.SuppressLint
20 import android.content.Context
21 import android.content.Intent
22 import android.util.Log
23 import com.android.internal.infra.ServiceConnector
24 import com.android.systemui.dagger.qualifiers.Application
25 import com.android.systemui.screenshot.IOnDoneCallback
26 import com.android.systemui.screenshot.IScreenshotProxy
27 import com.android.systemui.screenshot.ScreenshotProxyService
28 import javax.inject.Inject
29 import kotlin.coroutines.resume
30 import kotlin.coroutines.suspendCoroutine
31 import kotlinx.coroutines.CompletableDeferred
32 
33 private const val TAG = "SystemUiProxy"
34 
35 /** An implementation of [SystemUiProxy] using [ScreenshotProxyService]. */
36 class SystemUiProxyClient @Inject constructor(@Application context: Context) : SystemUiProxy {
37     @SuppressLint("ImplicitSamInstance")
38     private val proxyConnector: ServiceConnector<IScreenshotProxy> =
39         ServiceConnector.Impl(
40             context,
41             Intent(context, ScreenshotProxyService::class.java),
42             Context.BIND_AUTO_CREATE or Context.BIND_WAIVE_PRIORITY or Context.BIND_NOT_VISIBLE,
43             context.userId,
44             IScreenshotProxy.Stub::asInterface
45         )
46 
47     override suspend fun isNotificationShadeExpanded(): Boolean = suspendCoroutine { k ->
48         proxyConnector
49             .postForResult { it.isNotificationShadeExpanded }
50             .whenComplete { expanded, error ->
51                 error?.also { Log.wtf(TAG, "isNotificationShadeExpanded", it) }
52                 k.resume(expanded ?: false)
53             }
54     }
55 
56     override suspend fun dismissKeyguard() {
57         val completion = CompletableDeferred<Unit>()
58         val onDoneBinder =
59             object : IOnDoneCallback.Stub() {
60                 override fun onDone(success: Boolean) {
61                     completion.complete(Unit)
62                 }
63             }
64         if (proxyConnector.run { it.dismissKeyguard(onDoneBinder) }) {
65             completion.await()
66         } else {
67             Log.wtf(TAG, "Keyguard dismissal request failed")
68         }
69     }
70 }
71