blob: c6a1921fb1c444ca44f8853ef4b16ae2e97f8e94 [file] [log] [blame]
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001/**
2 * Copyright (c) 2015, 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
18package com.android.phone;
19
20import static android.Manifest.permission.READ_PHONE_STATE;
21import static com.android.internal.telephony.uicc.IccCardProxy.ACTION_INTERNAL_SIM_STATE_CHANGED;
22
23import android.app.ActivityManagerNative;
24import android.content.BroadcastReceiver;
25import android.content.ComponentName;
26import android.content.Context;
27import android.content.Intent;
28import android.content.IntentFilter;
29import android.content.ServiceConnection;
30import android.database.sqlite.SQLiteDatabase;
31import android.database.sqlite.SQLiteOpenHelper;
32import android.os.AsyncResult;
33import android.os.Bundle;
34import android.os.Handler;
35import android.os.IBinder;
36import android.os.Message;
37import android.os.RemoteException;
38import android.os.ServiceManager;
39import android.os.UserHandle;
40import android.service.carrier.CarrierConfigService;
41import android.service.carrier.CarrierIdentifier;
42import android.service.carrier.ICarrierConfigService;
43import android.telephony.CarrierConfigManager;
44import android.telephony.SubscriptionManager;
45import android.telephony.TelephonyManager;
46import android.util.Log;
47
48import com.android.internal.telephony.ICarrierConfigLoader;
49import com.android.internal.telephony.IccCardConstants;
50import com.android.internal.telephony.Phone;
51import com.android.internal.telephony.PhoneConstants;
52import com.android.internal.telephony.PhoneFactory;
53import com.android.internal.telephony.TelephonyIntents;
54
55import java.util.List;
56
57/**
58 * CarrierConfigLoader binds to privileged carrier apps to fetch carrier config overlays.
59 * TODO: implement persist cache
60 * TODO: add gid2 to phone
61 * TODO: handle package install/uninstall events
62 */
63
64public class CarrierConfigLoader extends ICarrierConfigLoader.Stub {
65 private static final String TAG = "CarrierConfigLoader";
66 // Package name for default carrier config app, bundled with system image.
67 private static final String DEFAULT_CARRIER_CONFIG_PACKAGE = "com.android.carrierconfig";
68
69 /** The singleton instance. */
70 private static CarrierConfigLoader sInstance;
71 // The context for phone app, passed from PhoneGlobals.
72 private Context mContext;
73 // Carrier configs from default app, indexed by phoneID.
74 private Bundle[] mConfigFromDefaultApp;
75 // Carrier configs from privileged carrier config app, indexed by phoneID.
76 private Bundle[] mConfigFromCarrierApp;
77 // Service connection for binding to config app.
78 private ConfigServiceConnection[] mServiceConnection;
79
80 // Broadcast receiver for SIM and pkg intents, register intent filter in constructor.
81 private final BroadcastReceiver mReceiver = new ConfigLoaderBroadcastReceiver();
82
83 // Message codes; see mHandler below.
84 // Request from SubscriptionInfoUpdater when SIM becomes absent or error.
85 private static final int EVENT_CLEAR_CONFIG = 0;
86 // Request from SubscriptionInfoUpdater to update config.
87 private static final int EVENT_UPDATE_CONFIG = 1;
88 // Request from carrier app to reload config.
89 private static final int EVENT_RELOAD_CONFIG = 2;
90 // Has connected to default app.
91 private static final int EVENT_CONNECTED_TO_DEFAULT = 3;
92 // Has connected to carrier app.
93 private static final int EVENT_CONNECTED_TO_CARRIER = 4;
94 // Config has been loaded from default app.
95 private static final int EVENT_LOADED_FROM_DEFAULT = 5;
96 // Config has been loaded from carrier app.
97 private static final int EVENT_LOADED_FROM_CARRIER = 6;
98
99
100 // Handler to process various events.
101 // For each phoneId, state transition should be: default app bind->connected->loaded,
102 // carrier app (if exists) bind-> connected->loaded. At any time, at most one connection is active.
103 // If events are not in this order, previous connection will be unbind, so only latest event takes effect.
104 // We broadcast config change when:
105 // 1. loaded from carrier app
106 // 2. loaded from default app if no carrier app
107 // 3. config cleared, possibly due to sim removed
108 // 4. bind or IPC error
109 private Handler mHandler = new Handler() {
110 @Override
111 public void handleMessage(Message msg) {
112 int phoneId = msg.arg1;
113 log("mHandler: " + msg.what + " phoneId: " + phoneId);
114 CarrierIdentifier carrierId;
115 ConfigServiceConnection conn;
116 Bundle config;
117 switch (msg.what) {
118 case EVENT_CLEAR_CONFIG:
119 mConfigFromDefaultApp[phoneId] = null;
120 mConfigFromCarrierApp[phoneId] = null;
121 mServiceConnection[phoneId] = null;
122 broadcastConfigChangedIntent(phoneId);
123 break;
124 case EVENT_UPDATE_CONFIG:
125 // Use persist cache to avoid loading from app.
126 // Fall through to next event if cache not hit.
127 case EVENT_RELOAD_CONFIG:
128 if (!bindToConfigPackage(DEFAULT_CARRIER_CONFIG_PACKAGE,
129 phoneId, EVENT_CONNECTED_TO_DEFAULT)) {
130 //Send bcast if bind fails
131 broadcastConfigChangedIntent(phoneId);
132 }
133 break;
134
135 case EVENT_CONNECTED_TO_DEFAULT:
136 carrierId = getCarrierIdForPhoneId(phoneId);
137 conn = (ConfigServiceConnection) msg.obj;
138 // If new service connection has been created, unbind.
139 if (mServiceConnection[phoneId] != conn || conn.service == null) {
140 mContext.unbindService(conn);
141 break;
142 }
143 try {
144 ICarrierConfigService configService = ICarrierConfigService.Stub.asInterface(conn.service);
145 config = configService.getCarrierConfig(carrierId);
146 mConfigFromDefaultApp[phoneId] = config;
147 mHandler.sendMessage(mHandler.obtainMessage(EVENT_LOADED_FROM_DEFAULT, phoneId));
148 } catch (RemoteException ex) {
149 Log.e(TAG, "Failed to get carrier config: " + ex.toString());
150 } finally {
151 mContext.unbindService(mServiceConnection[phoneId]);
152 }
153 break;
154
155 case EVENT_LOADED_FROM_DEFAULT:
156 if (mServiceConnection[phoneId] == null) {
157 break;
158 }
159 List<String> carrierPackageNames = TelephonyManager.from(mContext)
160 .getCarrierPackageNamesForIntent(new Intent(CarrierConfigService.SERVICE_INTERFACE));
161 log("Found carrier config app: " + carrierPackageNames);
162 if (carrierPackageNames != null && carrierPackageNames.size() > 0) {
163 if (!bindToConfigPackage(carrierPackageNames.get(0),
164 phoneId, EVENT_CONNECTED_TO_CARRIER)) {
165 broadcastConfigChangedIntent(phoneId);
166 }
167 } else {
168 broadcastConfigChangedIntent(phoneId);
169 }
170 break;
171
172 case EVENT_CONNECTED_TO_CARRIER:
173 carrierId = getCarrierIdForPhoneId(phoneId);
174 conn = (ConfigServiceConnection) msg.obj;
175 // If new service connection has been created, unbind.
176 if (mServiceConnection[phoneId] != conn ||
177 conn.service == null) {
178 mContext.unbindService(conn);
179 break;
180 }
181 try {
182 ICarrierConfigService configService = ICarrierConfigService.Stub.asInterface(conn.service);
183 config = configService.getCarrierConfig(carrierId);
184 mConfigFromCarrierApp[phoneId] = config;
185 mHandler.sendMessage(mHandler.obtainMessage(EVENT_LOADED_FROM_CARRIER, phoneId));
186 } catch (RemoteException ex) {
187 Log.e(TAG, "Failed to get carrier config: " + ex.toString());
188 } finally {
189 mContext.unbindService(mServiceConnection[phoneId]);
190 }
191 break;
192
193 case EVENT_LOADED_FROM_CARRIER:
194 if (mServiceConnection[phoneId] == null) {
195 break;
196 }
197 broadcastConfigChangedIntent(phoneId);
198 break;
199 }
200 }
201 };
202
203 /**
204 * Constructs a CarrierConfigLoader, registers it as a service, and registers a broadcast
205 * receiver for relevant events.
206 */
207 private CarrierConfigLoader(Context context) {
208 mContext = context;
209
210 // Register for package updates.
211 IntentFilter triggers = new IntentFilter();
212 triggers.addAction(Intent.ACTION_PACKAGE_ADDED);
213 triggers.addAction(Intent.ACTION_PACKAGE_CHANGED);
214 triggers.addAction(Intent.ACTION_PACKAGE_REMOVED);
215 mContext.registerReceiver(mReceiver, triggers);
216
217 int numPhones = TelephonyManager.from(context).getPhoneCount();
218 mConfigFromDefaultApp = new Bundle[numPhones];
219 mConfigFromCarrierApp = new Bundle[numPhones];
220 mServiceConnection = new ConfigServiceConnection[numPhones];
221 // Make this service available through ServiceManager.
222 ServiceManager.addService(Context.CARRIER_CONFIG_SERVICE, this);
223 log("CarrierConfigLoader has started");
224 }
225
226 /**
227 * Initialize the singleton CarrierConfigLoader instance.
228 *
229 * This is only done once, at startup, from {@link com.android.phone.PhoneApp#onCreate}.
230 */
231 /* package */
232 static CarrierConfigLoader init(Context context) {
233 synchronized (CarrierConfigLoader.class) {
234 if (sInstance == null) {
235 sInstance = new CarrierConfigLoader(context);
236 } else {
237 Log.wtf(TAG, "init() called multiple times! sInstance = " + sInstance);
238 }
239 return sInstance;
240 }
241 }
242
243 private void broadcastConfigChangedIntent(int phoneId) {
244 Intent intent = new Intent(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
245 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
246 SubscriptionManager.putPhoneIdAndSubIdExtra(intent, phoneId);
247 ActivityManagerNative.broadcastStickyIntent(intent, READ_PHONE_STATE,
248 UserHandle.USER_ALL);
249 }
250
251 /** Binds to the default or carrier config app. */
252 private boolean bindToConfigPackage(String pkgName, int phoneId, int eventId) {
253 log("Binding to " + pkgName + " for phone " + phoneId);
254 Intent carrierConfigService = new Intent(CarrierConfigService.SERVICE_INTERFACE);
255 carrierConfigService.setPackage(pkgName);
256 mServiceConnection[phoneId] = new ConfigServiceConnection(phoneId, eventId);
257 try {
258 return mContext.bindService(carrierConfigService, mServiceConnection[phoneId],
259 Context.BIND_AUTO_CREATE);
260 } catch (SecurityException ex) {
261 return false;
262 }
263 }
264
265 private CarrierIdentifier getCarrierIdForPhoneId(int phoneId) {
266 String mcc = "";
267 String mnc = "";
268 String imsi = "";
269 String gid1 = "";
270 String gid2 = "";
271 String spn = TelephonyManager.from(mContext).getSimOperatorNameForPhone(phoneId);
272 String simOperator = TelephonyManager.from(mContext).getSimOperatorNumericForPhone(phoneId);
Jonathan Basseri1fa437c2015-04-20 11:08:18 -0700273 // A valid simOperator should be 5 or 6 digits, depending on the length of the MNC.
274 if (simOperator != null && simOperator.length() >= 3) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800275 mcc = simOperator.substring(0, 3);
276 mnc = simOperator.substring(3);
277 }
278 Phone phone = PhoneFactory.getPhone(phoneId);
279 if (phone != null) {
280 imsi = phone.getSubscriberId();
281 gid1 = phone.getGroupIdLevel1();
282 // add gid2 after phone supports it.
283 }
284
285 return new CarrierIdentifier(mcc, mnc, spn, imsi, gid1, gid2);
286 }
287
288 @Override
289 public Bundle getConfigForSubId(int subId) {
290 int phoneId = SubscriptionManager.getPhoneId(subId);
291 Bundle retConfig = CarrierConfigManager.getDefaultConfig();
292 if (SubscriptionManager.isValidPhoneId(phoneId)) {
293 Bundle config = mConfigFromDefaultApp[phoneId];
294 if (config != null) retConfig.putAll(config);
295 config = mConfigFromCarrierApp[phoneId];
296 if (config != null) retConfig.putAll(config);
297 }
298 return retConfig;
299 }
300
301 @Override
302 public void reloadCarrierConfigForSubId(int subId) {
303 int phoneId = SubscriptionManager.getPhoneId(subId);
304 if (SubscriptionManager.isValidPhoneId(phoneId)) {
305 mHandler.sendMessage(mHandler.obtainMessage(EVENT_RELOAD_CONFIG, phoneId));
306 } else {
307 log("Ignore invalid phoneId: " + phoneId + " for subId: " + subId);
308 }
309 }
310
311 @Override
312 public void updateConfigForPhoneId(int phoneId, String simState) {
313 log("update config for phoneId: " + phoneId + " simState: " + simState);
314 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
315 return;
316 }
317 // requires Java 7 for switch on string.
318 switch (simState) {
319 case IccCardConstants.INTENT_VALUE_ICC_ABSENT:
320 case IccCardConstants.INTENT_VALUE_ICC_CARD_IO_ERROR:
321 case IccCardConstants.INTENT_VALUE_ICC_UNKNOWN:
322 mHandler.sendMessage(mHandler.obtainMessage(EVENT_CLEAR_CONFIG, phoneId));
323 break;
324 case IccCardConstants.INTENT_VALUE_ICC_LOADED:
325 case IccCardConstants.INTENT_VALUE_ICC_LOCKED:
326 mHandler.sendMessage(mHandler.obtainMessage(EVENT_UPDATE_CONFIG, phoneId));
327 break;
328 }
329 }
330
331 private class ConfigServiceConnection implements ServiceConnection {
332 int phoneId;
333 int eventId;
334 IBinder service;
335
336 public ConfigServiceConnection(int phoneId, int eventId) {
337 this.phoneId = phoneId;
338 this.eventId = eventId;
339 }
340
341 @Override
342 public void onServiceConnected(ComponentName name, IBinder service) {
343 log("Connected to config app: " + name.flattenToString());
344 this.service = service;
345 mHandler.sendMessage(mHandler.obtainMessage(eventId, phoneId, -1, this));
346 }
347
348 @Override
349 public void onServiceDisconnected(ComponentName name) {
350 this.service = null;
351 }
352 }
353
354 private class ConfigLoaderBroadcastReceiver extends BroadcastReceiver {
355 @Override
356 public void onReceive(Context context, Intent intent) {
357 String action = intent.getAction();
358 log("Receive action: " + action);
359 }
360 }
361
362 private static void log(String msg) {
363 Log.d(TAG, msg);
364 }
365}
366