• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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.car.builtin.os;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.annotation.SystemApi;
22 import android.os.IBinder;
23 import android.os.IServiceCallback;
24 import android.os.RemoteException;
25 import android.os.ServiceManager;
26 
27 /**
28  * Helper class for {@code ServiceManager} API
29  *
30  * @hide
31  */
32 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
33 public final class ServiceManagerHelper {
34 
ServiceManagerHelper()35     private ServiceManagerHelper()  {
36         throw new UnsupportedOperationException();
37     }
38 
39     /** Check {@link ServiceManager#getService(String)} */
40     @Nullable
getService(@onNull String name)41     public static IBinder getService(@NonNull String name) {
42         return ServiceManager.getService(name);
43     }
44 
45     /** Check {@link ServiceManager#checkService(String)} */
46     @Nullable
checkService(@onNull String name)47     public static IBinder checkService(@NonNull String name) {
48         return ServiceManager.checkService(name);
49     }
50 
51     /** Check {@link ServiceManager#waitForDeclaredService(String)} */
52     @Nullable
waitForDeclaredService(@onNull String name)53     public static IBinder waitForDeclaredService(@NonNull String name) {
54         return ServiceManager.waitForDeclaredService(name);
55     }
56 
57     /** Check {@link ServiceManager#addService(String, IBinder)} */
addService(@onNull String name, @NonNull IBinder service)58     public static void addService(@NonNull String name, @NonNull IBinder service) {
59         ServiceManager.addService(name, service);
60     }
61 
62     /** Check {@link ServiceManager#getDeclaredInstances(String)} */
63     @Nullable
getDeclaredInstances(@onNull String iface)64     public static String[] getDeclaredInstances(@NonNull String iface) {
65         return ServiceManager.getDeclaredInstances(iface);
66     }
67 
68     /**
69      * The callback interface for {@link registerForNotifications}.
70      */
71     public interface IServiceRegistrationCallback {
72         /**
73          * Called when a service is registered.
74          *
75          * @param name the service name that has been registered with
76          * @param binder the binder that is registered
77          */
onRegistration(@onNull String name, IBinder binder)78         void onRegistration(@NonNull String name, IBinder binder);
79     }
80 
81     private static final class ServiceCallbackImpl extends IServiceCallback.Stub {
82         private final IServiceRegistrationCallback mClientCallback;
83 
ServiceCallbackImpl(IServiceRegistrationCallback clientCallback)84         ServiceCallbackImpl(IServiceRegistrationCallback clientCallback) {
85             mClientCallback = clientCallback;
86         }
87 
88         @Override
onRegistration(String name, IBinder binder)89         public void onRegistration(String name, IBinder binder) {
90             mClientCallback.onRegistration(name, binder);
91         }
92     }
93 
94     /**
95      * Register callback for service registration notifications.
96      *
97      * @throws RemoteException for underlying error.
98      */
registerForNotifications( @onNull String name, @NonNull IServiceRegistrationCallback callback)99     public static void registerForNotifications(
100             @NonNull String name, @NonNull IServiceRegistrationCallback callback)
101             throws RemoteException {
102         ServiceManager.registerForNotifications(name, new ServiceCallbackImpl(callback));
103     }
104 }
105