| 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 | |
| 18 | /********************************************************************** |
| 19 | * This file is not a part of the NFC mainline modure * |
| 20 | * *******************************************************************/ |
| 21 | |
| 22 | package android.nfc; |
| 23 | |
| 24 | import android.annotation.NonNull; |
| 25 | import android.annotation.Nullable; |
| 26 | import android.annotation.SystemApi; |
| 27 | import android.annotation.SystemApi.Client; |
| 28 | import android.content.Context; |
| 29 | import android.os.IBinder; |
| 30 | import android.os.ServiceManager; |
| 31 | |
| 32 | /** |
| 33 | * Provides a way to register and obtain the system service binder objects managed by the nfc |
| 34 | * service. |
| 35 | * |
| 36 | * @hide |
| 37 | */ |
| 38 | @SystemApi(client = Client.MODULE_LIBRARIES) |
| 39 | public class NfcServiceManager { |
| 40 | |
| 41 | /** |
| 42 | * @hide |
| 43 | */ |
| 44 | public NfcServiceManager() { |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * A class that exposes the methods to register and obtain each system service. |
| 49 | */ |
| 50 | public static final class ServiceRegisterer { |
| 51 | private final String mServiceName; |
| 52 | |
| 53 | /** |
| 54 | * @hide |
| 55 | */ |
| 56 | public ServiceRegisterer(String serviceName) { |
| 57 | mServiceName = serviceName; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Register a system server binding object for a service. |
| 62 | */ |
| 63 | public void register(@NonNull IBinder service) { |
| 64 | ServiceManager.addService(mServiceName, service); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Get the system server binding object for a service. |
| 69 | * |
| 70 | * <p>This blocks until the service instance is ready, |
| 71 | * or a timeout happens, in which case it returns null. |
| 72 | */ |
| 73 | @Nullable |
| 74 | public IBinder get() { |
| 75 | return ServiceManager.getService(mServiceName); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Get the system server binding object for a service. |
| 80 | * |
| 81 | * <p>This blocks until the service instance is ready, |
| 82 | * or a timeout happens, in which case it throws {@link ServiceNotFoundException}. |
| 83 | */ |
| 84 | @NonNull |
| 85 | public IBinder getOrThrow() throws ServiceNotFoundException { |
| 86 | try { |
| 87 | return ServiceManager.getServiceOrThrow(mServiceName); |
| 88 | } catch (ServiceManager.ServiceNotFoundException e) { |
| 89 | throw new ServiceNotFoundException(mServiceName); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Get the system server binding object for a service. If the specified service is |
| 95 | * not available, it returns null. |
| 96 | */ |
| 97 | @Nullable |
| 98 | public IBinder tryGet() { |
| 99 | return ServiceManager.checkService(mServiceName); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * See {@link ServiceRegisterer#getOrThrow}. |
| 105 | * |
| 106 | */ |
| 107 | public static class ServiceNotFoundException extends ServiceManager.ServiceNotFoundException { |
| 108 | /** |
| 109 | * Constructor. |
| 110 | * |
| 111 | * @param name the name of the binder service that cannot be found. |
| 112 | * |
| 113 | */ |
| 114 | public ServiceNotFoundException(@NonNull String name) { |
| 115 | super(name); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Returns {@link ServiceRegisterer} for the "nfc" service. |
| 121 | */ |
| 122 | @NonNull |
| 123 | public ServiceRegisterer getNfcManagerServiceRegisterer() { |
| 124 | return new ServiceRegisterer(Context.NFC_SERVICE); |
| 125 | } |
| 126 | } |