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.UserHandle; 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 AppClipsCrossProcessHelper(@pplication Context context, DisplayTracker displayTracker)42 AppClipsCrossProcessHelper(@Application Context context, DisplayTracker displayTracker) { 43 // Start a service as main user so that even if the app clips activity is running as work 44 // profile user the service is able to use correct instance of Bubbles to grab a screenshot 45 // excluding the bubble layer. 46 mProxyConnector = new ServiceConnector.Impl<>(context, 47 new Intent(context, AppClipsScreenshotHelperService.class), 48 Context.BIND_AUTO_CREATE | Context.BIND_WAIVE_PRIORITY 49 | Context.BIND_NOT_VISIBLE, UserHandle.USER_SYSTEM, 50 IAppClipsScreenshotHelperService.Stub::asInterface); 51 mDisplayTracker = displayTracker; 52 } 53 54 /** 55 * Returns a {@link Bitmap} captured in the SysUI process, {@code null} in case of an error. 56 * 57 * <p>Note: The SysUI process captures a {@link ScreenshotHardwareBufferInternal} which is ok to 58 * pass around but not a {@link Bitmap}. 59 */ 60 @Nullable takeScreenshot()61 Bitmap takeScreenshot() { 62 try { 63 AndroidFuture<ScreenshotHardwareBufferInternal> future = 64 mProxyConnector.postForResult( 65 service -> 66 // Take a screenshot of the default display of the user. 67 service.takeScreenshot(mDisplayTracker.getDefaultDisplayId())); 68 return future.get().createBitmapThenCloseBuffer(); 69 } catch (Exception e) { 70 return null; 71 } 72 } 73 } 74