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 package com.android.federatedcompute.internal.util;
17 
18 import android.content.Context;
19 import android.os.IBinder;
20 
21 import java.util.List;
22 import java.util.concurrent.Executor;
23 import java.util.function.Function;
24 
25 /**
26  * Abstracts how to find and connect the service binder object.
27  *
28  * @param <T> The type of Service Binder.
29  * @hide
30  */
31 public abstract class AbstractServiceBinder<T> {
32     /** Get the {@link AbstractServiceBinder} suitable for the configuration. */
getServiceBinderByIntent( Context context, String serviceIntentAction, String servicePackage, Function<IBinder, T2> converter)33     public static <T2> AbstractServiceBinder<T2> getServiceBinderByIntent(
34             Context context,
35             String serviceIntentAction,
36             String servicePackage,
37             Function<IBinder, T2> converter) {
38         return new AndroidServiceBinder<>(
39                 context, serviceIntentAction, servicePackage, converter);
40     }
41 
42     /** Get the {@link AbstractServiceBinder} suitable for the configuration. */
getServiceBinderByServiceName( Context context, String serviceName, String servicePackage, Function<IBinder, T2> converter)43     public static <T2> AbstractServiceBinder<T2> getServiceBinderByServiceName(
44             Context context,
45             String serviceName,
46             String servicePackage,
47             Function<IBinder, T2> converter) {
48         return new AndroidServiceBinder<>(
49                 context, serviceName, servicePackage, true, converter);
50     }
51 
52     /** Get the {@link AbstractServiceBinder} suitable for the configuration. */
getServiceBinderByIntent( Context context, String serviceIntentAction, List<String> servicePackages, Function<IBinder, T2> converter)53     public static <T2> AbstractServiceBinder<T2> getServiceBinderByIntent(
54             Context context,
55             String serviceIntentAction,
56             List<String> servicePackages,
57             Function<IBinder, T2> converter) {
58         return new AndroidServiceBinder<>(
59                 context, serviceIntentAction, servicePackages, converter);
60     }
61 
62     /** Get the {@link AbstractServiceBinder} suitable for the configuration. */
getServiceBinderByIntent( Context context, String serviceIntentAction, List<String> servicePackages, int bindFlags, Function<IBinder, T2> converter)63     public static <T2> AbstractServiceBinder<T2> getServiceBinderByIntent(
64             Context context,
65             String serviceIntentAction,
66             List<String> servicePackages,
67             int bindFlags,
68             Function<IBinder, T2> converter) {
69         return new AndroidServiceBinder<>(
70                 context, serviceIntentAction, servicePackages, bindFlags, converter);
71     }
72 
73     /** Get the {@link AbstractServiceBinder} suitable for an isolated service by name. */
getIsolatedServiceBinderByServiceName( Context context, String serviceName, String servicePackage, String isolatedProcessName, int bindFlags, Function<IBinder, T2> converter)74     public static <T2> AbstractServiceBinder<T2> getIsolatedServiceBinderByServiceName(
75             Context context,
76             String serviceName,
77             String servicePackage,
78             String isolatedProcessName,
79             int bindFlags,
80             Function<IBinder, T2> converter) {
81         return new AndroidServiceBinder<>(
82                 context, serviceName, servicePackage, isolatedProcessName,
83                 /* enableLookupByName= */ true,
84                 bindFlags,
85                 converter);
86     }
87 
88     /** Get the binder service. */
getService(Executor executor)89     public abstract T getService(Executor executor);
90 
91     /**
92      * The service is in an APK (as opposed to the system service), unbind it from the service to
93      * allow the APK process to die.
94      */
unbindFromService()95     public abstract void unbindFromService();
96 }
97