• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 android.app;
18 
19 import android.annotation.CallbackExecutor;
20 import android.annotation.NonNull;
21 import android.os.IBinder;
22 import android.os.RemoteException;
23 import android.util.ArrayMap;
24 
25 import java.util.concurrent.Executor;
26 
27 /** Handles screen capture callbacks.
28  * @hide
29  **/
30 public class ScreenCaptureCallbackHandler {
31 
32     private final IBinder mActivityToken;
33     private final ScreenCaptureObserver mObserver;
34     private final ArrayMap<Activity.ScreenCaptureCallback, ScreenCaptureRegistration>
35             mScreenCaptureRegistrations = new ArrayMap<>();
36 
ScreenCaptureCallbackHandler(@onNull IBinder activityToken)37     public ScreenCaptureCallbackHandler(@NonNull IBinder activityToken) {
38         mActivityToken = activityToken;
39         mObserver = new ScreenCaptureObserver(mScreenCaptureRegistrations);
40     }
41 
42     private static class ScreenCaptureRegistration {
43         Executor mExecutor;
44         Activity.ScreenCaptureCallback mCallback;
45 
ScreenCaptureRegistration(Executor executor, Activity.ScreenCaptureCallback callback)46         ScreenCaptureRegistration(Executor executor, Activity.ScreenCaptureCallback callback) {
47             this.mExecutor = executor;
48             this.mCallback = callback;
49         }
50     }
51 
52     private static class ScreenCaptureObserver extends IScreenCaptureObserver.Stub {
53         ArrayMap<Activity.ScreenCaptureCallback, ScreenCaptureRegistration> mRegistrations;
54 
ScreenCaptureObserver( ArrayMap<Activity.ScreenCaptureCallback, ScreenCaptureRegistration> registrations)55         ScreenCaptureObserver(
56                 ArrayMap<Activity.ScreenCaptureCallback, ScreenCaptureRegistration>
57                         registrations) {
58             this.mRegistrations = registrations;
59         }
60 
61         @Override
onScreenCaptured()62         public void onScreenCaptured() {
63             for (ScreenCaptureRegistration registration : mRegistrations.values()) {
64                 registration.mExecutor.execute(
65                         () -> {
66                             registration.mCallback.onScreenCaptured();
67                         });
68             }
69         }
70     }
71 
72     /**
73      * Start monitoring for screen captures of the activity, the callback will be triggered whenever
74      * a screen capture is attempted. This callback will be executed on the thread of the passed
75      * {@code executor}. If the window is FLAG_SECURE, the callback will not be triggered.
76      */
registerScreenCaptureCallback( @onNull @allbackExecutor Executor executor, @NonNull Activity.ScreenCaptureCallback callback)77     public void registerScreenCaptureCallback(
78             @NonNull @CallbackExecutor Executor executor,
79             @NonNull Activity.ScreenCaptureCallback callback) {
80         ScreenCaptureRegistration registration =
81                 new ScreenCaptureRegistration(executor, callback);
82         synchronized (mScreenCaptureRegistrations) {
83             if (mScreenCaptureRegistrations.containsKey(callback)) {
84                 throw new IllegalStateException(
85                         "Capture observer already registered with the activity");
86             }
87             mScreenCaptureRegistrations.put(callback, registration);
88             // register with system server only once.
89             if (mScreenCaptureRegistrations.size() == 1) {
90                 try {
91                     ActivityTaskManager.getService()
92                             .registerScreenCaptureObserver(mActivityToken, mObserver);
93                 } catch (RemoteException e) {
94                     e.rethrowFromSystemServer();
95                 }
96             }
97         }
98     }
99     /** Stop monitoring for screen captures of the activity */
unregisterScreenCaptureCallback(@onNull Activity.ScreenCaptureCallback callback)100     public void unregisterScreenCaptureCallback(@NonNull Activity.ScreenCaptureCallback callback) {
101         synchronized (mScreenCaptureRegistrations) {
102             if (!mScreenCaptureRegistrations.containsKey(callback)) {
103                 throw new IllegalStateException(
104                         "Capture observer not registered with the activity");
105             }
106             mScreenCaptureRegistrations.remove(callback);
107             // unregister only if no more registrations are left
108             if (mScreenCaptureRegistrations.size() == 0) {
109                 try {
110                     ActivityTaskManager.getService().unregisterScreenCaptureObserver(mActivityToken,
111                             mObserver);
112                 } catch (RemoteException e) {
113                     e.rethrowFromSystemServer();
114                 }
115             }
116         }
117     }
118 }
119