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