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