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 com.android.wm.shell.common;
18 
19 import android.Manifest;
20 import android.os.IBinder;
21 import android.util.Slog;
22 
23 import java.util.function.Consumer;
24 
25 /**
26  * An interface for binders which can be registered to be sent to other processes.
27  */
28 public interface ExternalInterfaceBinder {
29     /**
30      * Invalidates this binder (detaches it from the controller it would call).
31      */
invalidate()32     void invalidate();
33 
34     /**
35      * Returns the IBinder to send.
36      */
asBinder()37     IBinder asBinder();
38 
39     /**
40      * Checks that the caller has the MANAGE_ACTIVITY_TASKS permission and executes the given
41      * callback.
42      */
executeRemoteCallWithTaskPermission(RemoteCallable<T> controllerInstance, String log, Consumer<T> callback)43     default <T> void executeRemoteCallWithTaskPermission(RemoteCallable<T> controllerInstance,
44             String log, Consumer<T> callback) {
45         executeRemoteCallWithTaskPermission(controllerInstance, log, callback,
46                 false /* blocking */);
47     }
48 
49     /**
50      * Checks that the caller has the MANAGE_ACTIVITY_TASKS permission and executes the given
51      * callback.
52      */
executeRemoteCallWithTaskPermission(RemoteCallable<T> controllerInstance, String log, Consumer<T> callback, boolean blocking)53     default <T> void executeRemoteCallWithTaskPermission(RemoteCallable<T> controllerInstance,
54             String log, Consumer<T> callback, boolean blocking) {
55         if (controllerInstance == null) return;
56 
57         final RemoteCallable<T> controller = controllerInstance;
58         controllerInstance.getContext().enforceCallingPermission(
59                 Manifest.permission.MANAGE_ACTIVITY_TASKS, log);
60         if (blocking) {
61             try {
62                 controllerInstance.getRemoteCallExecutor().executeBlocking(() -> {
63                     callback.accept((T) controller);
64                 });
65             } catch (InterruptedException e) {
66                 Slog.e("ExternalInterfaceBinder", "Remote call failed", e);
67             }
68         } else {
69             controllerInstance.getRemoteCallExecutor().execute(() -> {
70                 callback.accept((T) controller);
71             });
72         }
73     }
74 }
75