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 
17 package android.telephony;
18 
19 import android.annotation.NonNull;
20 import android.app.SystemServiceRegistry;
21 import android.content.Context;
22 import android.os.TelephonyServiceManager;
23 import android.telephony.euicc.EuiccCardManager;
24 import android.telephony.euicc.EuiccManager;
25 import android.telephony.ims.ImsManager;
26 import android.telephony.satellite.SatelliteManager;
27 
28 import com.android.internal.util.Preconditions;
29 
30 
31 /**
32  * Class for performing registration for all telephony services.
33  *
34  * @hide
35  */
36 public class TelephonyFrameworkInitializer {
37 
TelephonyFrameworkInitializer()38     private TelephonyFrameworkInitializer() {
39     }
40 
41     private static volatile TelephonyServiceManager sTelephonyServiceManager;
42 
43     /**
44      * Sets an instance of {@link TelephonyServiceManager} that allows
45      * the telephony mainline module to register/obtain telephony binder services. This is called
46      * by the platform during the system initialization.
47      *
48      * @param telephonyServiceManager instance of {@link TelephonyServiceManager} that allows
49      * the telephony mainline module to register/obtain telephony binder services.
50      */
setTelephonyServiceManager( @onNull TelephonyServiceManager telephonyServiceManager)51     public static void setTelephonyServiceManager(
52             @NonNull TelephonyServiceManager telephonyServiceManager) {
53         Preconditions.checkState(sTelephonyServiceManager == null,
54                 "setTelephonyServiceManager called twice!");
55         sTelephonyServiceManager = Preconditions.checkNotNull(telephonyServiceManager);
56     }
57 
58     /**
59      * Called by {@link SystemServiceRegistry}'s static initializer and registers all telephony
60      * services to {@link Context}, so that {@link Context#getSystemService} can return them.
61      *
62      * @throws IllegalStateException if this is called from anywhere besides
63      * {@link SystemServiceRegistry}
64      */
registerServiceWrappers()65     public static void registerServiceWrappers() {
66         SystemServiceRegistry.registerContextAwareService(
67                 Context.TELEPHONY_SERVICE,
68                 TelephonyManager.class,
69                 context -> new TelephonyManager(context)
70         );
71         SystemServiceRegistry.registerContextAwareService(
72                 Context.TELEPHONY_SUBSCRIPTION_SERVICE,
73                 SubscriptionManager.class,
74                 context -> new SubscriptionManager(context)
75         );
76         SystemServiceRegistry.registerContextAwareService(
77                 Context.CARRIER_CONFIG_SERVICE,
78                 CarrierConfigManager.class,
79                 context -> new CarrierConfigManager(context)
80         );
81         SystemServiceRegistry.registerContextAwareService(
82                 Context.EUICC_SERVICE,
83                 EuiccManager.class,
84                 context -> new EuiccManager(context)
85         );
86         SystemServiceRegistry.registerContextAwareService(
87                 Context.EUICC_CARD_SERVICE,
88                 EuiccCardManager.class,
89                 context -> new EuiccCardManager(context)
90         );
91         SystemServiceRegistry.registerContextAwareService(
92                 Context.TELEPHONY_IMS_SERVICE,
93                 ImsManager.class,
94                 context -> new ImsManager(context)
95         );
96         SystemServiceRegistry.registerContextAwareService(
97                 Context.SMS_SERVICE,
98                 SmsManager.class,
99                 context -> SmsManager.getSmsManagerForContextAndSubscriptionId(context,
100                         SubscriptionManager.DEFAULT_SUBSCRIPTION_ID)
101         );
102         SystemServiceRegistry.registerContextAwareService(
103                 Context.SATELLITE_SERVICE,
104                 SatelliteManager.class,
105                 context -> new SatelliteManager(context)
106         );
107     }
108 
109     /** @hide */
getTelephonyServiceManager()110     public static TelephonyServiceManager getTelephonyServiceManager() {
111         return sTelephonyServiceManager;
112     }
113 }
114