Aurimas Liutikas | 88c7ff1 | 2023-08-10 12:42:26 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.systemui.screenshot.appclips; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.content.Intent; |
| 21 | import android.graphics.Bitmap; |
| 22 | import android.os.UserManager; |
| 23 | |
| 24 | import androidx.annotation.Nullable; |
| 25 | |
| 26 | import com.android.internal.infra.AndroidFuture; |
| 27 | import com.android.internal.infra.ServiceConnector; |
| 28 | import com.android.systemui.dagger.SysUISingleton; |
| 29 | import com.android.systemui.dagger.qualifiers.Application; |
| 30 | import com.android.systemui.settings.DisplayTracker; |
| 31 | |
| 32 | import javax.inject.Inject; |
| 33 | |
| 34 | /** An intermediary singleton object to help communicating with the cross process service. */ |
| 35 | @SysUISingleton |
| 36 | class 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 | } |