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