blob: afc8bff91766bd038c06a9a975187a1151fdd7b8 [file] [log] [blame]
Aurimas Liutikas88c7ff12023-08-10 12:42:26 -07001/*
2 * Copyright (C) 2023 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
17package com.android.systemui.screenshot.appclips;
18
19import android.content.Context;
20import android.content.Intent;
21import android.graphics.Bitmap;
22import android.os.UserManager;
23
24import androidx.annotation.Nullable;
25
26import com.android.internal.infra.AndroidFuture;
27import com.android.internal.infra.ServiceConnector;
28import com.android.systemui.dagger.SysUISingleton;
29import com.android.systemui.dagger.qualifiers.Application;
30import com.android.systemui.settings.DisplayTracker;
31
32import javax.inject.Inject;
33
34/** An intermediary singleton object to help communicating with the cross process service. */
35@SysUISingleton
36class AppClipsCrossProcessHelper {
37
38 private final ServiceConnector<IAppClipsScreenshotHelperService> mProxyConnector;
39 private final DisplayTracker mDisplayTracker;
40
41 @Inject
42 AppClipsCrossProcessHelper(@Application Context context, UserManager userManager,
43 DisplayTracker displayTracker) {
44 // Start a service as main user so that even if the app clips activity is running as work
45 // profile user the service is able to use correct instance of Bubbles to grab a screenshot
46 // excluding the bubble layer.
47 mProxyConnector = new ServiceConnector.Impl<>(context,
48 new Intent(context, AppClipsScreenshotHelperService.class),
49 Context.BIND_AUTO_CREATE | Context.BIND_WAIVE_PRIORITY
50 | Context.BIND_NOT_VISIBLE, userManager.getMainUser().getIdentifier(),
51 IAppClipsScreenshotHelperService.Stub::asInterface);
52 mDisplayTracker = displayTracker;
53 }
54
55 /**
56 * Returns a {@link Bitmap} captured in the SysUI process, {@code null} in case of an error.
57 *
58 * <p>Note: The SysUI process captures a {@link ScreenshotHardwareBufferInternal} which is ok to
59 * pass around but not a {@link Bitmap}.
60 */
61 @Nullable
62 Bitmap takeScreenshot() {
63 try {
64 AndroidFuture<ScreenshotHardwareBufferInternal> future =
65 mProxyConnector.postForResult(
66 service ->
67 // Take a screenshot of the default display of the user.
68 service.takeScreenshot(mDisplayTracker.getDefaultDisplayId()));
69 return future.get().createBitmapThenCloseBuffer();
70 } catch (Exception e) {
71 return null;
72 }
73 }
74}