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 
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 public class TelephonyServiceManager {
31     /**
32      * @hide
33      */
TelephonyServiceManager()34     public TelephonyServiceManager() {
35     }
36 
37     /**
38      * A class that exposes the methods to register and obtain each system service.
39      */
40     public static final class ServiceRegisterer {
41         private final String mServiceName;
42 
43         /**
44          * @hide
45          */
ServiceRegisterer(String serviceName)46         public ServiceRegisterer(String serviceName) {
47             mServiceName = serviceName;
48         }
49 
50         /**
51          * Register a system server binding object for a service.
52          */
register(@onNull IBinder service)53         public void register(@NonNull IBinder service) {
54             ServiceManager.addService(mServiceName, service);
55         }
56 
57         /**
58          * Get the system server binding object for a service.
59          *
60          * <p>This blocks until the service instance is ready,
61          * or a timeout happens, in which case it returns null.
62          */
63         @Nullable
get()64         public IBinder get() {
65             return ServiceManager.getService(mServiceName);
66         }
67 
68         /**
69          * Get the system server binding object for a service.
70          *
71          * <p>This blocks until the service instance is ready,
72          * or a timeout happens, in which case it throws {@link ServiceNotFoundException}.
73          */
74         @NonNull
getOrThrow()75         public IBinder getOrThrow() throws ServiceNotFoundException {
76             try {
77                 return ServiceManager.getServiceOrThrow(mServiceName);
78             } catch (ServiceManager.ServiceNotFoundException e) {
79                 throw new ServiceNotFoundException(mServiceName);
80             }
81         }
82 
83         /**
84          * Get the system server binding object for a service. If the specified service is
85          * not available, it returns null.
86          */
87         @Nullable
tryGet()88         public IBinder tryGet() {
89             return ServiceManager.checkService(mServiceName);
90         }
91     }
92 
93     /**
94      * See {@link ServiceRegisterer#getOrThrow}.
95      *
96      * @hide
97      */
98     public static class ServiceNotFoundException extends ServiceManager.ServiceNotFoundException {
99         /**
100          * Constructor.
101          *
102          * @param name the name of the binder service that cannot be found.
103          *
104          */
ServiceNotFoundException(@onNull String name)105         public ServiceNotFoundException(@NonNull String name) {
106             super(name);
107         }
108     }
109 
110     /**
111      * Returns {@link ServiceRegisterer} for the "telephony" service.
112      */
113     @NonNull
getTelephonyServiceRegisterer()114     public ServiceRegisterer getTelephonyServiceRegisterer() {
115         return new ServiceRegisterer(Context.TELEPHONY_SERVICE);
116     }
117 
118     /**
119      * Returns {@link ServiceRegisterer} for the telephony IMS service.
120      */
121     @NonNull
getTelephonyImsServiceRegisterer()122     public ServiceRegisterer getTelephonyImsServiceRegisterer() {
123         return new ServiceRegisterer(Context.TELEPHONY_IMS_SERVICE);
124     }
125 
126     /**
127      * Returns {@link ServiceRegisterer} for the telephony RCS message service.
128      */
129     @NonNull
getTelephonyRcsMessageServiceRegisterer()130     public ServiceRegisterer getTelephonyRcsMessageServiceRegisterer() {
131         return new ServiceRegisterer(Context.TELEPHONY_RCS_MESSAGE_SERVICE);
132     }
133 
134     /**
135      * Returns {@link ServiceRegisterer} for the subscription service.
136      */
137     @NonNull
getSubscriptionServiceRegisterer()138     public ServiceRegisterer getSubscriptionServiceRegisterer() {
139         return new ServiceRegisterer("isub");
140     }
141 
142     /**
143      * Returns {@link ServiceRegisterer} for the phone sub service.
144      */
145     @NonNull
getPhoneSubServiceRegisterer()146     public ServiceRegisterer getPhoneSubServiceRegisterer() {
147         return new ServiceRegisterer("iphonesubinfo");
148     }
149 
150     /**
151      * Returns {@link ServiceRegisterer} for the opportunistic network service.
152      */
153     @NonNull
getOpportunisticNetworkServiceRegisterer()154     public ServiceRegisterer getOpportunisticNetworkServiceRegisterer() {
155         return new ServiceRegisterer("ions");
156     }
157 
158     /**
159      * Returns {@link ServiceRegisterer} for the carrier config service.
160      */
161     @NonNull
getCarrierConfigServiceRegisterer()162     public ServiceRegisterer getCarrierConfigServiceRegisterer() {
163         return new ServiceRegisterer(Context.CARRIER_CONFIG_SERVICE);
164     }
165 
166     /**
167      * Returns {@link ServiceRegisterer} for the "SMS" service.
168      */
169     @NonNull
getSmsServiceRegisterer()170     public ServiceRegisterer getSmsServiceRegisterer() {
171         return new ServiceRegisterer("isms");
172     }
173 
174     /**
175      * Returns {@link ServiceRegisterer} for the eUICC controller service.
176      */
177     @NonNull
getEuiccControllerService()178     public ServiceRegisterer getEuiccControllerService() {
179         return new ServiceRegisterer("econtroller");
180     }
181 
182     /**
183      * Returns {@link ServiceRegisterer} for the eUICC card controller service.
184      */
185     @NonNull
getEuiccCardControllerServiceRegisterer()186     public ServiceRegisterer getEuiccCardControllerServiceRegisterer() {
187         return new ServiceRegisterer("euicc_card_controller");
188     }
189 
190     /**
191      * Returns {@link ServiceRegisterer} for the ICC phone book service.
192      */
193     @NonNull
getIccPhoneBookServiceRegisterer()194     public ServiceRegisterer getIccPhoneBookServiceRegisterer() {
195         return new ServiceRegisterer("simphonebook");
196     }
197 }
198