blob: 50f5bca91f6ff8e237020e1a71b8c5279746c73d [file] [log] [blame]
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001/*
2 * Copyright (C) 2006 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
17package com.android.phone;
18
19import android.app.ActivityManager;
20import android.app.AppOpsManager;
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -070021import android.content.ComponentName;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070022import android.content.Context;
23import android.content.Intent;
Derek Tan97ebb422014-09-05 16:55:38 -070024import android.content.SharedPreferences;
Shishir Agrawal60f9c952014-06-23 12:00:43 -070025import android.content.pm.PackageInfo;
26import android.content.pm.PackageManager;
27import android.content.pm.Signature;
Wink Saville36469e72014-06-11 15:17:00 -070028import android.net.ConnectivityManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070029import android.net.Uri;
30import android.os.AsyncResult;
31import android.os.Binder;
32import android.os.Bundle;
33import android.os.Handler;
Gabriel Peal36ebb0d2014-03-20 09:20:43 -070034import android.os.IBinder;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070035import android.os.Looper;
36import android.os.Message;
37import android.os.Process;
Gabriel Peal36ebb0d2014-03-20 09:20:43 -070038import android.os.RemoteException;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070039import android.os.ServiceManager;
Wink Saville08874612014-08-31 19:19:58 -070040import android.os.SystemProperties;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070041import android.os.UserHandle;
Derek Tan97ebb422014-09-05 16:55:38 -070042import android.preference.PreferenceManager;
Ihab Awadf2177b72013-11-25 13:33:23 -080043import android.provider.Settings;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070044import android.telephony.CellInfo;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -070045import android.telephony.IccOpenLogicalChannelResponse;
Jake Hambye994d462014-02-03 13:10:13 -080046import android.telephony.NeighboringCellInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070047import android.telephony.ServiceState;
Wink Saville36469e72014-06-11 15:17:00 -070048import android.telephony.SubscriptionManager;
Wink Saville08874612014-08-31 19:19:58 -070049import android.telephony.SubInfoRecord;
Ihab Awadf2177b72013-11-25 13:33:23 -080050import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070051import android.text.TextUtils;
52import android.util.Log;
Jake Hambye994d462014-02-03 13:10:13 -080053import android.util.Pair;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070054
Shishir Agrawal566b7612013-10-28 14:41:00 -070055import com.android.internal.telephony.CallManager;
56import com.android.internal.telephony.CommandException;
Gabriel Peal36ebb0d2014-03-20 09:20:43 -070057import com.android.internal.telephony.Connection;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070058import com.android.internal.telephony.DefaultPhoneNotifier;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070059import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -080060import com.android.internal.telephony.IccCard;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070061import com.android.internal.telephony.Phone;
Wink Saville36469e72014-06-11 15:17:00 -070062import com.android.internal.telephony.PhoneFactory;
63import com.android.internal.telephony.CallManager;
64import com.android.internal.telephony.CommandException;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070065import com.android.internal.telephony.PhoneConstants;
Wink Saville08874612014-08-31 19:19:58 -070066import com.android.internal.telephony.TelephonyIntents;
Wink Saville36469e72014-06-11 15:17:00 -070067import com.android.internal.telephony.dataconnection.DctController;
Derek Tan7226c842014-07-02 17:42:23 -070068import com.android.internal.telephony.uicc.AdnRecord;
Shishir Agrawal566b7612013-10-28 14:41:00 -070069import com.android.internal.telephony.uicc.IccIoResult;
70import com.android.internal.telephony.uicc.IccUtils;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -070071import com.android.internal.telephony.uicc.UiccCard;
Shishir Agrawal60f9c952014-06-23 12:00:43 -070072import com.android.internal.telephony.uicc.UiccCarrierPrivilegeRules;
Shishir Agrawal566b7612013-10-28 14:41:00 -070073import com.android.internal.telephony.uicc.UiccController;
Jake Hambye994d462014-02-03 13:10:13 -080074import com.android.internal.util.HexDump;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070075
Wink Saville36469e72014-06-11 15:17:00 -070076import static com.android.internal.telephony.PhoneConstants.SUBSCRIPTION_KEY;
77
Santos Cordon7d4ddf62013-07-10 11:58:08 -070078import java.util.ArrayList;
Gabriel Peal36ebb0d2014-03-20 09:20:43 -070079import java.util.HashMap;
Derek Tan89e89d42014-07-08 17:00:10 -070080import java.util.HashSet;
Gabriel Peal36ebb0d2014-03-20 09:20:43 -070081import java.util.Iterator;
Jake Hambye994d462014-02-03 13:10:13 -080082import java.util.List;
Gabriel Peal36ebb0d2014-03-20 09:20:43 -070083import java.util.Map;
Derek Tan89e89d42014-07-08 17:00:10 -070084import java.util.Set;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070085
86/**
87 * Implementation of the ITelephony interface.
88 */
Santos Cordon117fee72014-05-16 17:56:12 -070089public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -070090 private static final String LOG_TAG = "PhoneInterfaceManager";
91 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
92 private static final boolean DBG_LOC = false;
93
94 // Message codes used with mMainThreadHandler
95 private static final int CMD_HANDLE_PIN_MMI = 1;
96 private static final int CMD_HANDLE_NEIGHBORING_CELL = 2;
97 private static final int EVENT_NEIGHBORING_CELL_DONE = 3;
98 private static final int CMD_ANSWER_RINGING_CALL = 4;
99 private static final int CMD_END_CALL = 5; // not used yet
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700100 private static final int CMD_TRANSMIT_APDU_LOGICAL_CHANNEL = 7;
101 private static final int EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE = 8;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700102 private static final int CMD_OPEN_CHANNEL = 9;
103 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
104 private static final int CMD_CLOSE_CHANNEL = 11;
105 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -0800106 private static final int CMD_NV_READ_ITEM = 13;
107 private static final int EVENT_NV_READ_ITEM_DONE = 14;
108 private static final int CMD_NV_WRITE_ITEM = 15;
109 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
110 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
111 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
112 private static final int CMD_NV_RESET_CONFIG = 19;
113 private static final int EVENT_NV_RESET_CONFIG_DONE = 20;
Jake Hamby7c27be32014-03-03 13:25:59 -0800114 private static final int CMD_GET_PREFERRED_NETWORK_TYPE = 21;
115 private static final int EVENT_GET_PREFERRED_NETWORK_TYPE_DONE = 22;
116 private static final int CMD_SET_PREFERRED_NETWORK_TYPE = 23;
117 private static final int EVENT_SET_PREFERRED_NETWORK_TYPE_DONE = 24;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800118 private static final int CMD_SEND_ENVELOPE = 25;
119 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Derek Tan6b088ee2014-09-05 14:15:18 -0700120 private static final int CMD_INVOKE_OEM_RIL_REQUEST_RAW = 27;
121 private static final int EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE = 28;
122 private static final int CMD_TRANSMIT_APDU_BASIC_CHANNEL = 29;
123 private static final int EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE = 30;
124 private static final int CMD_EXCHANGE_SIM_IO = 31;
125 private static final int EVENT_EXCHANGE_SIM_IO_DONE = 32;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700126
127 /** The singleton instance. */
128 private static PhoneInterfaceManager sInstance;
129
130 PhoneGlobals mApp;
131 Phone mPhone;
132 CallManager mCM;
133 AppOpsManager mAppOps;
134 MainThreadHandler mMainThreadHandler;
135
Derek Tan97ebb422014-09-05 16:55:38 -0700136 SharedPreferences carrierPrivilegeConfigs;
137 private static final String PREF_CARRIERS_ALPHATAG_PREFIX = "carrier_alphtag_";
138 private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
139 private static final String PREF_CARRIERS_SIMPLIFIED_NETWORK_SETTINGS_PREFIX =
140 "carrier_simplified_network_settings_";
Derek Tan89e89d42014-07-08 17:00:10 -0700141
142 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700143 * A request object to use for transmitting data to an ICC.
144 */
145 private static final class IccAPDUArgument {
146 public int channel, cla, command, p1, p2, p3;
147 public String data;
148
149 public IccAPDUArgument(int channel, int cla, int command,
150 int p1, int p2, int p3, String data) {
151 this.channel = channel;
152 this.cla = cla;
153 this.command = command;
154 this.p1 = p1;
155 this.p2 = p2;
156 this.p3 = p3;
157 this.data = data;
158 }
159 }
160
161 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700162 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
163 * request after sending. The main thread will notify the request when it is complete.
164 */
165 private static final class MainThreadRequest {
166 /** The argument to use for the request */
167 public Object argument;
168 /** The result of the request that is run on the main thread */
169 public Object result;
170
171 public MainThreadRequest(Object argument) {
172 this.argument = argument;
173 }
174 }
175
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800176 private static final class IncomingThirdPartyCallArgs {
177 public final ComponentName component;
178 public final String callId;
179 public final String callerDisplayName;
180
181 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
182 String callerDisplayName) {
183 this.component = component;
184 this.callId = callId;
185 this.callerDisplayName = callerDisplayName;
186 }
187 }
188
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700189 /**
190 * A handler that processes messages on the main thread in the phone process. Since many
191 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
192 * inbound binder threads to the main thread in the phone process. The Binder thread
193 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
194 * on, which will be notified when the operation completes and will contain the result of the
195 * request.
196 *
197 * <p>If a MainThreadRequest object is provided in the msg.obj field,
198 * note that request.result must be set to something non-null for the calling thread to
199 * unblock.
200 */
201 private final class MainThreadHandler extends Handler {
202 @Override
203 public void handleMessage(Message msg) {
204 MainThreadRequest request;
205 Message onCompleted;
206 AsyncResult ar;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700207 UiccCard uiccCard = UiccController.getInstance().getUiccCard();
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700208 IccAPDUArgument iccArgument;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700209
210 switch (msg.what) {
211 case CMD_HANDLE_PIN_MMI:
212 request = (MainThreadRequest) msg.obj;
Jake Hambye994d462014-02-03 13:10:13 -0800213 request.result = mPhone.handlePinMmi((String) request.argument);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700214 // Wake up the requesting thread
215 synchronized (request) {
216 request.notifyAll();
217 }
218 break;
219
220 case CMD_HANDLE_NEIGHBORING_CELL:
221 request = (MainThreadRequest) msg.obj;
222 onCompleted = obtainMessage(EVENT_NEIGHBORING_CELL_DONE,
223 request);
224 mPhone.getNeighboringCids(onCompleted);
225 break;
226
227 case EVENT_NEIGHBORING_CELL_DONE:
228 ar = (AsyncResult) msg.obj;
229 request = (MainThreadRequest) ar.userObj;
230 if (ar.exception == null && ar.result != null) {
231 request.result = ar.result;
232 } else {
233 // create an empty list to notify the waiting thread
Jake Hambye994d462014-02-03 13:10:13 -0800234 request.result = new ArrayList<NeighboringCellInfo>(0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700235 }
236 // Wake up the requesting thread
237 synchronized (request) {
238 request.notifyAll();
239 }
240 break;
241
242 case CMD_ANSWER_RINGING_CALL:
Wink Saville08874612014-08-31 19:19:58 -0700243 request = (MainThreadRequest) msg.obj;
244 long answer_subId = ((Long)request.argument).longValue();
245 answerRingingCallInternal(answer_subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700246 break;
247
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700248 case CMD_END_CALL:
249 request = (MainThreadRequest) msg.obj;
Wink Saville08874612014-08-31 19:19:58 -0700250 long end_subId = ((Long)request.argument).longValue();
251 final boolean hungUp;
252 int phoneType = getPhone(end_subId).getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700253 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
254 // CDMA: If the user presses the Power button we treat it as
255 // ending the complete call session
Wink Saville08874612014-08-31 19:19:58 -0700256 hungUp = PhoneUtils.hangupRingingAndActive(getPhone(end_subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700257 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
258 // GSM: End the call as per the Phone state
259 hungUp = PhoneUtils.hangup(mCM);
260 } else {
261 throw new IllegalStateException("Unexpected phone type: " + phoneType);
262 }
263 if (DBG) log("CMD_END_CALL: " + (hungUp ? "hung up!" : "no call to hang up"));
264 request.result = hungUp;
265 // Wake up the requesting thread
266 synchronized (request) {
267 request.notifyAll();
268 }
269 break;
270
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700271 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700272 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700273 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700274 if (uiccCard == null) {
275 loge("iccTransmitApduLogicalChannel: No UICC");
276 request.result = new IccIoResult(0x6F, 0, (byte[])null);
277 synchronized (request) {
278 request.notifyAll();
279 }
280 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700281 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
282 request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700283 uiccCard.iccTransmitApduLogicalChannel(
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700284 iccArgument.channel, iccArgument.cla, iccArgument.command,
285 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
Shishir Agrawal566b7612013-10-28 14:41:00 -0700286 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700287 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700288 break;
289
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700290 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700291 ar = (AsyncResult) msg.obj;
292 request = (MainThreadRequest) ar.userObj;
293 if (ar.exception == null && ar.result != null) {
294 request.result = ar.result;
295 } else {
296 request.result = new IccIoResult(0x6F, 0, (byte[])null);
297 if (ar.result == null) {
298 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800299 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700300 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800301 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700302 } else {
303 loge("iccTransmitApduLogicalChannel: Unknown exception");
304 }
305 }
306 synchronized (request) {
307 request.notifyAll();
308 }
309 break;
310
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700311 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
312 request = (MainThreadRequest) msg.obj;
313 iccArgument = (IccAPDUArgument) request.argument;
314 if (uiccCard == null) {
315 loge("iccTransmitApduBasicChannel: No UICC");
316 request.result = new IccIoResult(0x6F, 0, (byte[])null);
317 synchronized (request) {
318 request.notifyAll();
319 }
320 } else {
321 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
322 request);
323 uiccCard.iccTransmitApduBasicChannel(
324 iccArgument.cla, iccArgument.command, iccArgument.p1, iccArgument.p2,
325 iccArgument.p3, iccArgument.data, onCompleted);
326 }
327 break;
328
329 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
330 ar = (AsyncResult) msg.obj;
331 request = (MainThreadRequest) ar.userObj;
332 if (ar.exception == null && ar.result != null) {
333 request.result = ar.result;
334 } else {
335 request.result = new IccIoResult(0x6F, 0, (byte[])null);
336 if (ar.result == null) {
337 loge("iccTransmitApduBasicChannel: Empty response");
338 } else if (ar.exception instanceof CommandException) {
339 loge("iccTransmitApduBasicChannel: CommandException: " +
340 ar.exception);
341 } else {
342 loge("iccTransmitApduBasicChannel: Unknown exception");
343 }
344 }
345 synchronized (request) {
346 request.notifyAll();
347 }
348 break;
349
350 case CMD_EXCHANGE_SIM_IO:
351 request = (MainThreadRequest) msg.obj;
352 iccArgument = (IccAPDUArgument) request.argument;
353 if (uiccCard == null) {
354 loge("iccExchangeSimIO: No UICC");
355 request.result = new IccIoResult(0x6F, 0, (byte[])null);
356 synchronized (request) {
357 request.notifyAll();
358 }
359 } else {
360 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
361 request);
362 uiccCard.iccExchangeSimIO(iccArgument.cla, /* fileID */
363 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
364 iccArgument.data, onCompleted);
365 }
366 break;
367
368 case EVENT_EXCHANGE_SIM_IO_DONE:
369 ar = (AsyncResult) msg.obj;
370 request = (MainThreadRequest) ar.userObj;
371 if (ar.exception == null && ar.result != null) {
372 request.result = ar.result;
373 } else {
374 request.result = new IccIoResult(0x6f, 0, (byte[])null);
375 }
376 synchronized (request) {
377 request.notifyAll();
378 }
379 break;
380
Derek Tan4d5e5c12014-02-04 11:54:58 -0800381 case CMD_SEND_ENVELOPE:
382 request = (MainThreadRequest) msg.obj;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700383 if (uiccCard == null) {
384 loge("sendEnvelopeWithStatus: No UICC");
385 request.result = new IccIoResult(0x6F, 0, (byte[])null);
386 synchronized (request) {
387 request.notifyAll();
388 }
389 } else {
390 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
391 uiccCard.sendEnvelopeWithStatus((String)request.argument, onCompleted);
392 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800393 break;
394
395 case EVENT_SEND_ENVELOPE_DONE:
396 ar = (AsyncResult) msg.obj;
397 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700398 if (ar.exception == null && ar.result != null) {
399 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800400 } else {
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700401 request.result = new IccIoResult(0x6F, 0, (byte[])null);
402 if (ar.result == null) {
403 loge("sendEnvelopeWithStatus: Empty response");
404 } else if (ar.exception instanceof CommandException) {
405 loge("sendEnvelopeWithStatus: CommandException: " +
406 ar.exception);
407 } else {
408 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
409 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800410 }
411 synchronized (request) {
412 request.notifyAll();
413 }
414 break;
415
Shishir Agrawal566b7612013-10-28 14:41:00 -0700416 case CMD_OPEN_CHANNEL:
417 request = (MainThreadRequest) msg.obj;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700418 if (uiccCard == null) {
419 loge("iccOpenLogicalChannel: No UICC");
420 request.result = new IccIoResult(0x6F, 0, (byte[])null);
421 synchronized (request) {
422 request.notifyAll();
423 }
424 } else {
425 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
426 uiccCard.iccOpenLogicalChannel((String)request.argument, onCompleted);
427 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700428 break;
429
430 case EVENT_OPEN_CHANNEL_DONE:
431 ar = (AsyncResult) msg.obj;
432 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700433 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700434 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700435 int[] result = (int[]) ar.result;
436 int channelId = result[0];
437 byte[] selectResponse = null;
438 if (result.length > 1) {
439 selectResponse = new byte[result.length - 1];
440 for (int i = 1; i < result.length; ++i) {
441 selectResponse[i - 1] = (byte) result[i];
442 }
443 }
444 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700445 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700446 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700447 if (ar.result == null) {
448 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700449 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700450 if (ar.exception != null) {
451 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
452 }
453
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700454 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700455 if ((ar.exception != null) && (ar.exception instanceof CommandException)) {
456 if (ar.exception.getMessage().compareTo("MISSING_RESOURCE") == 0) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700457 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700458 } else if (ar.exception.getMessage().compareTo("NO_SUCH_ELEMENT") == 0) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700459 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700460 }
461 }
462 openChannelResp = new IccOpenLogicalChannelResponse(
463 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700464 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700465 request.result = openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700466 synchronized (request) {
467 request.notifyAll();
468 }
469 break;
470
471 case CMD_CLOSE_CHANNEL:
472 request = (MainThreadRequest) msg.obj;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700473 if (uiccCard == null) {
474 loge("iccCloseLogicalChannel: No UICC");
475 request.result = new IccIoResult(0x6F, 0, (byte[])null);
476 synchronized (request) {
477 request.notifyAll();
478 }
479 } else {
480 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
481 uiccCard.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
482 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700483 break;
484
485 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800486 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
487 break;
488
489 case CMD_NV_READ_ITEM:
490 request = (MainThreadRequest) msg.obj;
491 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
492 mPhone.nvReadItem((Integer) request.argument, onCompleted);
493 break;
494
495 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700496 ar = (AsyncResult) msg.obj;
497 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800498 if (ar.exception == null && ar.result != null) {
499 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700500 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800501 request.result = "";
502 if (ar.result == null) {
503 loge("nvReadItem: Empty response");
504 } else if (ar.exception instanceof CommandException) {
505 loge("nvReadItem: CommandException: " +
506 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700507 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800508 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700509 }
510 }
511 synchronized (request) {
512 request.notifyAll();
513 }
514 break;
515
Jake Hambye994d462014-02-03 13:10:13 -0800516 case CMD_NV_WRITE_ITEM:
517 request = (MainThreadRequest) msg.obj;
518 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
519 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
520 mPhone.nvWriteItem(idValue.first, idValue.second, onCompleted);
521 break;
522
523 case EVENT_NV_WRITE_ITEM_DONE:
524 handleNullReturnEvent(msg, "nvWriteItem");
525 break;
526
527 case CMD_NV_WRITE_CDMA_PRL:
528 request = (MainThreadRequest) msg.obj;
529 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
530 mPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
531 break;
532
533 case EVENT_NV_WRITE_CDMA_PRL_DONE:
534 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
535 break;
536
537 case CMD_NV_RESET_CONFIG:
538 request = (MainThreadRequest) msg.obj;
539 onCompleted = obtainMessage(EVENT_NV_RESET_CONFIG_DONE, request);
540 mPhone.nvResetConfig((Integer) request.argument, onCompleted);
541 break;
542
543 case EVENT_NV_RESET_CONFIG_DONE:
544 handleNullReturnEvent(msg, "nvResetConfig");
545 break;
546
Jake Hamby7c27be32014-03-03 13:25:59 -0800547 case CMD_GET_PREFERRED_NETWORK_TYPE:
548 request = (MainThreadRequest) msg.obj;
549 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
550 mPhone.getPreferredNetworkType(onCompleted);
551 break;
552
553 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
554 ar = (AsyncResult) msg.obj;
555 request = (MainThreadRequest) ar.userObj;
556 if (ar.exception == null && ar.result != null) {
557 request.result = ar.result; // Integer
558 } else {
559 request.result = -1;
560 if (ar.result == null) {
561 loge("getPreferredNetworkType: Empty response");
562 } else if (ar.exception instanceof CommandException) {
563 loge("getPreferredNetworkType: CommandException: " +
564 ar.exception);
565 } else {
566 loge("getPreferredNetworkType: Unknown exception");
567 }
568 }
569 synchronized (request) {
570 request.notifyAll();
571 }
572 break;
573
574 case CMD_SET_PREFERRED_NETWORK_TYPE:
575 request = (MainThreadRequest) msg.obj;
576 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
577 int networkType = (Integer) request.argument;
578 mPhone.setPreferredNetworkType(networkType, onCompleted);
579 break;
580
581 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
582 handleNullReturnEvent(msg, "setPreferredNetworkType");
583 break;
584
Steven Liu4bf01bc2014-07-17 11:05:29 -0500585 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
586 request = (MainThreadRequest)msg.obj;
587 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
588 mPhone.invokeOemRilRequestRaw((byte[])request.argument, onCompleted);
589 break;
590
591 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
592 ar = (AsyncResult)msg.obj;
593 request = (MainThreadRequest)ar.userObj;
594 request.result = ar;
595 synchronized (request) {
596 request.notifyAll();
597 }
598 break;
599
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700600 default:
601 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
602 break;
603 }
604 }
Jake Hambye994d462014-02-03 13:10:13 -0800605
606 private void handleNullReturnEvent(Message msg, String command) {
607 AsyncResult ar = (AsyncResult) msg.obj;
608 MainThreadRequest request = (MainThreadRequest) ar.userObj;
609 if (ar.exception == null) {
610 request.result = true;
611 } else {
612 request.result = false;
613 if (ar.exception instanceof CommandException) {
614 loge(command + ": CommandException: " + ar.exception);
615 } else {
616 loge(command + ": Unknown exception");
617 }
618 }
619 synchronized (request) {
620 request.notifyAll();
621 }
622 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700623 }
624
625 /**
626 * Posts the specified command to be executed on the main thread,
627 * waits for the request to complete, and returns the result.
628 * @see #sendRequestAsync
629 */
630 private Object sendRequest(int command, Object argument) {
Santos Cordon500b0e02014-06-17 10:33:33 -0700631 return sendRequest(command, argument, null);
Wink Saville36469e72014-06-11 15:17:00 -0700632 }
633
634 /**
635 * Posts the specified command to be executed on the main thread,
636 * waits for the request to complete, and returns the result.
637 * @see #sendRequestAsync
638 */
639 private Object sendRequest(int command, Object argument, Object argument2) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700640 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
641 throw new RuntimeException("This method will deadlock if called from the main thread.");
642 }
643
644 MainThreadRequest request = new MainThreadRequest(argument);
645 Message msg = mMainThreadHandler.obtainMessage(command, request);
646 msg.sendToTarget();
647
648 // Wait for the request to complete
649 synchronized (request) {
650 while (request.result == null) {
651 try {
652 request.wait();
653 } catch (InterruptedException e) {
654 // Do nothing, go back and wait until the request is complete
655 }
656 }
657 }
658 return request.result;
659 }
660
661 /**
662 * Asynchronous ("fire and forget") version of sendRequest():
663 * Posts the specified command to be executed on the main thread, and
664 * returns immediately.
665 * @see #sendRequest
666 */
667 private void sendRequestAsync(int command) {
668 mMainThreadHandler.sendEmptyMessage(command);
669 }
670
671 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -0700672 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
673 * @see {@link #sendRequest(int,Object)}
674 */
675 private void sendRequestAsync(int command, Object argument) {
676 MainThreadRequest request = new MainThreadRequest(argument);
677 Message msg = mMainThreadHandler.obtainMessage(command, request);
678 msg.sendToTarget();
679 }
680
681 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700682 * Initialize the singleton PhoneInterfaceManager instance.
683 * This is only done once, at startup, from PhoneApp.onCreate().
684 */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700685 /* package */ static PhoneInterfaceManager init(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700686 synchronized (PhoneInterfaceManager.class) {
687 if (sInstance == null) {
Sailesh Nepal194161e2014-07-03 08:57:44 -0700688 sInstance = new PhoneInterfaceManager(app, phone);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700689 } else {
690 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
691 }
692 return sInstance;
693 }
694 }
695
696 /** Private constructor; @see init() */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700697 private PhoneInterfaceManager(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700698 mApp = app;
699 mPhone = phone;
700 mCM = PhoneGlobals.getInstance().mCM;
701 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
702 mMainThreadHandler = new MainThreadHandler();
Derek Tan97ebb422014-09-05 16:55:38 -0700703 carrierPrivilegeConfigs =
704 PreferenceManager.getDefaultSharedPreferences(mPhone.getContext());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700705 publish();
706 }
707
708 private void publish() {
709 if (DBG) log("publish: " + this);
710
711 ServiceManager.addService("phone", this);
712 }
713
Wink Saville36469e72014-06-11 15:17:00 -0700714 // returns phone associated with the subId.
Wink Saville36469e72014-06-11 15:17:00 -0700715 private Phone getPhone(long subId) {
Legler Wuf8733b42014-09-25 11:00:54 +0800716 return PhoneFactory.getPhone(SubscriptionManager.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -0700717 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700718 //
719 // Implementation of the ITelephony interface.
720 //
721
722 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700723 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -0700724 }
725
Wink Savilleadd7cc52014-09-08 14:23:09 -0700726 public void dialForSubscriber(long subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700727 if (DBG) log("dial: " + number);
728 // No permission check needed here: This is just a wrapper around the
729 // ACTION_DIAL intent, which is available to any app since it puts up
730 // the UI before it does anything.
731
732 String url = createTelUrl(number);
733 if (url == null) {
734 return;
735 }
736
737 // PENDING: should we just silently fail if phone is offhook or ringing?
Wink Saville36469e72014-06-11 15:17:00 -0700738 PhoneConstants.State state = mCM.getState(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700739 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
740 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
741 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
742 mApp.startActivity(intent);
743 }
744 }
745
746 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700747 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -0700748 }
749
Wink Savilleadd7cc52014-09-08 14:23:09 -0700750 public void callForSubscriber(long subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700751 if (DBG) log("call: " + number);
752
753 // This is just a wrapper around the ACTION_CALL intent, but we still
754 // need to do a permission check since we're calling startActivity()
755 // from the context of the phone app.
756 enforceCallPermission();
757
758 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
759 != AppOpsManager.MODE_ALLOWED) {
760 return;
761 }
762
763 String url = createTelUrl(number);
764 if (url == null) {
765 return;
766 }
767
Wink Saville08874612014-08-31 19:19:58 -0700768 boolean isValid = false;
Wink Saville10aa2e92014-09-04 10:46:34 -0700769 List<SubInfoRecord> slist = SubscriptionManager.getActiveSubInfoList();
Wink Saville08874612014-08-31 19:19:58 -0700770 for (SubInfoRecord subInfoRecord : slist) {
Wink Savilleb30c9a22014-09-04 22:42:02 -0700771 if (subInfoRecord.subId == subId) {
Wink Saville08874612014-08-31 19:19:58 -0700772 isValid = true;
773 break;
774 }
775 }
776 if (isValid == false) {
777 return;
778 }
779
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700780 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
Wink Saville36469e72014-06-11 15:17:00 -0700781 intent.putExtra(SUBSCRIPTION_KEY, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700782 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
783 mApp.startActivity(intent);
784 }
785
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700786 /**
787 * End a call based on call state
788 * @return true is a call was ended
789 */
790 public boolean endCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700791 return endCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700792 }
793
794 /**
795 * End a call based on the call state of the subId
796 * @return true is a call was ended
797 */
Wink Savilleadd7cc52014-09-08 14:23:09 -0700798 public boolean endCallForSubscriber(long subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700799 enforceCallPermission();
Wink Saville08874612014-08-31 19:19:58 -0700800 return (Boolean) sendRequest(CMD_END_CALL, new Long(subId), null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700801 }
802
803 public void answerRingingCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700804 answerRingingCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700805 }
806
Wink Savilleadd7cc52014-09-08 14:23:09 -0700807 public void answerRingingCallForSubscriber(long subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700808 if (DBG) log("answerRingingCall...");
809 // TODO: there should eventually be a separate "ANSWER_PHONE" permission,
810 // but that can probably wait till the big TelephonyManager API overhaul.
811 // For now, protect this call with the MODIFY_PHONE_STATE permission.
812 enforceModifyPermission();
Wink Saville08874612014-08-31 19:19:58 -0700813 sendRequest(CMD_ANSWER_RINGING_CALL, new Long(subId), null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700814 }
815
816 /**
817 * Make the actual telephony calls to implement answerRingingCall().
818 * This should only be called from the main thread of the Phone app.
819 * @see #answerRingingCall
820 *
821 * TODO: it would be nice to return true if we answered the call, or
822 * false if there wasn't actually a ringing incoming call, or some
823 * other error occurred. (In other words, pass back the return value
824 * from PhoneUtils.answerCall() or PhoneUtils.answerAndEndActive().)
825 * But that would require calling this method via sendRequest() rather
826 * than sendRequestAsync(), and right now we don't actually *need* that
827 * return value, so let's just return void for now.
828 */
Wink Saville08874612014-08-31 19:19:58 -0700829 private void answerRingingCallInternal(long subId) {
830 final boolean hasRingingCall = !getPhone(subId).getRingingCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700831 if (hasRingingCall) {
Wink Saville08874612014-08-31 19:19:58 -0700832 final boolean hasActiveCall = !getPhone(subId).getForegroundCall().isIdle();
833 final boolean hasHoldingCall = !getPhone(subId).getBackgroundCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700834 if (hasActiveCall && hasHoldingCall) {
835 // Both lines are in use!
836 // TODO: provide a flag to let the caller specify what
837 // policy to use if both lines are in use. (The current
838 // behavior is hardwired to "answer incoming, end ongoing",
839 // which is how the CALL button is specced to behave.)
840 PhoneUtils.answerAndEndActive(mCM, mCM.getFirstActiveRingingCall());
841 return;
842 } else {
843 // answerCall() will automatically hold the current active
844 // call, if there is one.
845 PhoneUtils.answerCall(mCM.getFirstActiveRingingCall());
846 return;
847 }
848 } else {
849 // No call was ringing.
850 return;
851 }
852 }
853
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700854 /**
Santos Cordon5422a8d2014-09-12 04:20:56 -0700855 * This method is no longer used and can be removed once TelephonyManager stops referring to it.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700856 */
Santos Cordon5422a8d2014-09-12 04:20:56 -0700857 public void silenceRinger() {
858 Log.e(LOG_TAG, "silenseRinger not supported");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700859 }
860
861 public boolean isOffhook() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700862 return isOffhookForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700863 }
864
Wink Savilleadd7cc52014-09-08 14:23:09 -0700865 public boolean isOffhookForSubscriber(long subId) {
Wink Saville36469e72014-06-11 15:17:00 -0700866 return (getPhone(subId).getState() == PhoneConstants.State.OFFHOOK);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700867 }
868
869 public boolean isRinging() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700870 return (isRingingForSubscriber(getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -0700871 }
872
Wink Savilleadd7cc52014-09-08 14:23:09 -0700873 public boolean isRingingForSubscriber(long subId) {
Wink Saville36469e72014-06-11 15:17:00 -0700874 return (getPhone(subId).getState() == PhoneConstants.State.RINGING);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700875 }
876
877 public boolean isIdle() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700878 return isIdleForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700879 }
880
Wink Savilleadd7cc52014-09-08 14:23:09 -0700881 public boolean isIdleForSubscriber(long subId) {
Wink Saville36469e72014-06-11 15:17:00 -0700882 return (getPhone(subId).getState() == PhoneConstants.State.IDLE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700883 }
884
885 public boolean isSimPinEnabled() {
886 enforceReadPermission();
887 return (PhoneGlobals.getInstance().isSimPinEnabled());
888 }
889
890 public boolean supplyPin(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700891 return supplyPinForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -0700892 }
893
Wink Savilleadd7cc52014-09-08 14:23:09 -0700894 public boolean supplyPinForSubscriber(long subId, String pin) {
895 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -0700896 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
897 }
898
899 public boolean supplyPuk(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700900 return supplyPukForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -0700901 }
902
Wink Savilleadd7cc52014-09-08 14:23:09 -0700903 public boolean supplyPukForSubscriber(long subId, String puk, String pin) {
904 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -0700905 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
906 }
907
908 /** {@hide} */
909 public int[] supplyPinReportResult(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700910 return supplyPinReportResultForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -0700911 }
912
Wink Savilleadd7cc52014-09-08 14:23:09 -0700913 public int[] supplyPinReportResultForSubscriber(long subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700914 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -0700915 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700916 checkSimPin.start();
917 return checkSimPin.unlockSim(null, pin);
918 }
919
Wink Saville9de0f752013-10-22 19:04:03 -0700920 /** {@hide} */
921 public int[] supplyPukReportResult(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700922 return supplyPukReportResultForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -0700923 }
924
Wink Savilleadd7cc52014-09-08 14:23:09 -0700925 public int[] supplyPukReportResultForSubscriber(long subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700926 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -0700927 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700928 checkSimPuk.start();
929 return checkSimPuk.unlockSim(puk, pin);
930 }
931
932 /**
Wink Saville9de0f752013-10-22 19:04:03 -0700933 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700934 * a synchronous one.
935 */
936 private static class UnlockSim extends Thread {
937
938 private final IccCard mSimCard;
939
940 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -0700941 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
942 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700943
944 // For replies from SimCard interface
945 private Handler mHandler;
946
947 // For async handler to identify request type
948 private static final int SUPPLY_PIN_COMPLETE = 100;
949
950 public UnlockSim(IccCard simCard) {
951 mSimCard = simCard;
952 }
953
954 @Override
955 public void run() {
956 Looper.prepare();
957 synchronized (UnlockSim.this) {
958 mHandler = new Handler() {
959 @Override
960 public void handleMessage(Message msg) {
961 AsyncResult ar = (AsyncResult) msg.obj;
962 switch (msg.what) {
963 case SUPPLY_PIN_COMPLETE:
964 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
965 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -0700966 mRetryCount = msg.arg1;
967 if (ar.exception != null) {
968 if (ar.exception instanceof CommandException &&
969 ((CommandException)(ar.exception)).getCommandError()
970 == CommandException.Error.PASSWORD_INCORRECT) {
971 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
972 } else {
973 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
974 }
975 } else {
976 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
977 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700978 mDone = true;
979 UnlockSim.this.notifyAll();
980 }
981 break;
982 }
983 }
984 };
985 UnlockSim.this.notifyAll();
986 }
987 Looper.loop();
988 }
989
990 /*
991 * Use PIN or PUK to unlock SIM card
992 *
993 * If PUK is null, unlock SIM card with PIN
994 *
995 * If PUK is not null, unlock SIM card with PUK and set PIN code
996 */
Wink Saville9de0f752013-10-22 19:04:03 -0700997 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700998
999 while (mHandler == null) {
1000 try {
1001 wait();
1002 } catch (InterruptedException e) {
1003 Thread.currentThread().interrupt();
1004 }
1005 }
1006 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
1007
1008 if (puk == null) {
1009 mSimCard.supplyPin(pin, callback);
1010 } else {
1011 mSimCard.supplyPuk(puk, pin, callback);
1012 }
1013
1014 while (!mDone) {
1015 try {
1016 Log.d(LOG_TAG, "wait for done");
1017 wait();
1018 } catch (InterruptedException e) {
1019 // Restore the interrupted status
1020 Thread.currentThread().interrupt();
1021 }
1022 }
1023 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07001024 int[] resultArray = new int[2];
1025 resultArray[0] = mResult;
1026 resultArray[1] = mRetryCount;
1027 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001028 }
1029 }
1030
1031 public void updateServiceLocation() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001032 updateServiceLocationForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001033
1034 }
1035
Wink Savilleadd7cc52014-09-08 14:23:09 -07001036 public void updateServiceLocationForSubscriber(long subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001037 // No permission check needed here: this call is harmless, and it's
1038 // needed for the ServiceState.requestStateUpdate() call (which is
1039 // already intentionally exposed to 3rd parties.)
Wink Saville36469e72014-06-11 15:17:00 -07001040 getPhone(subId).updateServiceLocation();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001041 }
1042
1043 public boolean isRadioOn() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001044 return isRadioOnForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001045 }
1046
Wink Savilleadd7cc52014-09-08 14:23:09 -07001047 public boolean isRadioOnForSubscriber(long subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001048 return getPhone(subId).getServiceState().getState() != ServiceState.STATE_POWER_OFF;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001049 }
1050
1051 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001052 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001053
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001054 }
Wink Saville36469e72014-06-11 15:17:00 -07001055
Wink Savilleadd7cc52014-09-08 14:23:09 -07001056 public void toggleRadioOnOffForSubscriber(long subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001057 enforceModifyPermission();
Wink Savilleadd7cc52014-09-08 14:23:09 -07001058 getPhone(subId).setRadioPower(!isRadioOnForSubscriber(subId));
Wink Saville36469e72014-06-11 15:17:00 -07001059 }
1060
1061 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001062 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001063 }
1064
Wink Savilleadd7cc52014-09-08 14:23:09 -07001065 public boolean setRadioForSubscriber(long subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07001066 enforceModifyPermission();
1067 if ((getPhone(subId).getServiceState().getState() !=
1068 ServiceState.STATE_POWER_OFF) != turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001069 toggleRadioOnOffForSubscriber(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001070 }
1071 return true;
1072 }
Wink Saville36469e72014-06-11 15:17:00 -07001073
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001074 public boolean needMobileRadioShutdown() {
1075 /*
1076 * If any of the Radios are available, it will need to be
1077 * shutdown. So return true if any Radio is available.
1078 */
1079 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1080 Phone phone = PhoneFactory.getPhone(i);
1081 if (phone != null && phone.isRadioAvailable()) return true;
1082 }
1083 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
1084 return false;
1085 }
1086
1087 public void shutdownMobileRadios() {
1088 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1089 logv("Shutting down Phone " + i);
1090 shutdownRadioUsingPhoneId(i);
1091 }
1092 }
1093
1094 private void shutdownRadioUsingPhoneId(int phoneId) {
1095 enforceModifyPermission();
1096 Phone phone = PhoneFactory.getPhone(phoneId);
1097 if (phone != null && phone.isRadioAvailable()) {
1098 phone.shutdownRadio();
1099 }
1100 }
1101
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001102 public boolean setRadioPower(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001103 return setRadioPowerForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001104 }
1105
Wink Savilleadd7cc52014-09-08 14:23:09 -07001106 public boolean setRadioPowerForSubscriber(long subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001107 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001108 getPhone(subId).setRadioPower(turnOn);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001109 return true;
1110 }
1111
Wink Saville36469e72014-06-11 15:17:00 -07001112 // FIXME: subId version needed
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001113 public boolean enableDataConnectivity() {
1114 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001115 long subId = SubscriptionManager.getDefaultDataSubId();
1116 getPhone(subId).setDataEnabled(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001117 return true;
1118 }
1119
Wink Saville36469e72014-06-11 15:17:00 -07001120 // FIXME: subId version needed
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001121 public boolean disableDataConnectivity() {
1122 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001123 long subId = SubscriptionManager.getDefaultDataSubId();
1124 getPhone(subId).setDataEnabled(false);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001125 return true;
1126 }
1127
Wink Saville36469e72014-06-11 15:17:00 -07001128 // FIXME: subId version needed
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001129 public boolean isDataConnectivityPossible() {
Wink Saville36469e72014-06-11 15:17:00 -07001130 long subId = SubscriptionManager.getDefaultDataSubId();
1131 return getPhone(subId).isDataConnectivityPossible();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001132 }
1133
1134 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001135 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07001136 }
1137
Wink Savilleadd7cc52014-09-08 14:23:09 -07001138 public boolean handlePinMmiForSubscriber(long subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001139 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001140 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001141 }
1142
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001143 public int getCallState() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001144 return getCallStateForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001145 }
1146
Wink Savilleadd7cc52014-09-08 14:23:09 -07001147 public int getCallStateForSubscriber(long subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001148 return DefaultPhoneNotifier.convertCallState(getPhone(subId).getState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001149 }
1150
1151 public int getDataState() {
Wink Saville36469e72014-06-11 15:17:00 -07001152 Phone phone = getPhone(SubscriptionManager.getDefaultDataSubId());
1153 return DefaultPhoneNotifier.convertDataState(phone.getDataConnectionState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001154 }
1155
1156 public int getDataActivity() {
Wink Saville36469e72014-06-11 15:17:00 -07001157 Phone phone = getPhone(SubscriptionManager.getDefaultDataSubId());
1158 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001159 }
1160
1161 @Override
1162 public Bundle getCellLocation() {
1163 try {
1164 mApp.enforceCallingOrSelfPermission(
1165 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1166 } catch (SecurityException e) {
1167 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1168 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1169 // is the weaker precondition
1170 mApp.enforceCallingOrSelfPermission(
1171 android.Manifest.permission.ACCESS_COARSE_LOCATION, null);
1172 }
1173
Jake Hambye994d462014-02-03 13:10:13 -08001174 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001175 if (DBG_LOC) log("getCellLocation: is active user");
1176 Bundle data = new Bundle();
1177 mPhone.getCellLocation().fillInNotifierBundle(data);
1178 return data;
1179 } else {
1180 if (DBG_LOC) log("getCellLocation: suppress non-active user");
1181 return null;
1182 }
1183 }
1184
1185 @Override
1186 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001187 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001188 }
1189
Wink Savilleadd7cc52014-09-08 14:23:09 -07001190 public void enableLocationUpdatesForSubscriber(long subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001191 mApp.enforceCallingOrSelfPermission(
1192 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Wink Saville36469e72014-06-11 15:17:00 -07001193 getPhone(subId).enableLocationUpdates();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001194 }
1195
1196 @Override
1197 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001198 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001199 }
1200
Wink Savilleadd7cc52014-09-08 14:23:09 -07001201 public void disableLocationUpdatesForSubscriber(long subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001202 mApp.enforceCallingOrSelfPermission(
1203 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Wink Saville36469e72014-06-11 15:17:00 -07001204 getPhone(subId).disableLocationUpdates();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001205 }
1206
1207 @Override
1208 @SuppressWarnings("unchecked")
1209 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
1210 try {
1211 mApp.enforceCallingOrSelfPermission(
1212 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1213 } catch (SecurityException e) {
1214 // If we have ACCESS_FINE_LOCATION permission, skip the check
1215 // for ACCESS_COARSE_LOCATION
1216 // A failure should throw the SecurityException from
1217 // ACCESS_COARSE_LOCATION since this is the weaker precondition
1218 mApp.enforceCallingOrSelfPermission(
1219 android.Manifest.permission.ACCESS_COARSE_LOCATION, null);
1220 }
1221
1222 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
1223 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1224 return null;
1225 }
Jake Hambye994d462014-02-03 13:10:13 -08001226 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001227 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
1228
1229 ArrayList<NeighboringCellInfo> cells = null;
1230
1231 try {
1232 cells = (ArrayList<NeighboringCellInfo>) sendRequest(
Wink Saville36469e72014-06-11 15:17:00 -07001233 CMD_HANDLE_NEIGHBORING_CELL, null, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001234 } catch (RuntimeException e) {
Wink Saville36469e72014-06-11 15:17:00 -07001235 Log.e(LOG_TAG, "getNeighboringCellInfo " + e);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001236 }
1237 return cells;
1238 } else {
1239 if (DBG_LOC) log("getNeighboringCellInfo: suppress non-active user");
1240 return null;
1241 }
1242 }
1243
1244
1245 @Override
1246 public List<CellInfo> getAllCellInfo() {
1247 try {
1248 mApp.enforceCallingOrSelfPermission(
1249 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1250 } catch (SecurityException e) {
1251 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1252 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1253 // is the weaker precondition
1254 mApp.enforceCallingOrSelfPermission(
1255 android.Manifest.permission.ACCESS_COARSE_LOCATION, null);
1256 }
1257
Jake Hambye994d462014-02-03 13:10:13 -08001258 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001259 if (DBG_LOC) log("getAllCellInfo: is active user");
1260 return mPhone.getAllCellInfo();
1261 } else {
1262 if (DBG_LOC) log("getAllCellInfo: suppress non-active user");
1263 return null;
1264 }
1265 }
1266
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001267 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001268 public void setCellInfoListRate(int rateInMillis) {
1269 mPhone.setCellInfoListRate(rateInMillis);
1270 }
1271
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001272 //
1273 // Internal helper methods.
1274 //
1275
Jake Hambye994d462014-02-03 13:10:13 -08001276 private static boolean checkIfCallerIsSelfOrForegroundUser() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001277 boolean ok;
1278
1279 boolean self = Binder.getCallingUid() == Process.myUid();
1280 if (!self) {
1281 // Get the caller's user id then clear the calling identity
1282 // which will be restored in the finally clause.
1283 int callingUser = UserHandle.getCallingUserId();
1284 long ident = Binder.clearCallingIdentity();
1285
1286 try {
1287 // With calling identity cleared the current user is the foreground user.
1288 int foregroundUser = ActivityManager.getCurrentUser();
1289 ok = (foregroundUser == callingUser);
1290 if (DBG_LOC) {
1291 log("checkIfCallerIsSelfOrForegoundUser: foregroundUser=" + foregroundUser
1292 + " callingUser=" + callingUser + " ok=" + ok);
1293 }
1294 } catch (Exception ex) {
1295 if (DBG_LOC) loge("checkIfCallerIsSelfOrForegoundUser: Exception ex=" + ex);
1296 ok = false;
1297 } finally {
1298 Binder.restoreCallingIdentity(ident);
1299 }
1300 } else {
1301 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: is self");
1302 ok = true;
1303 }
1304 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: ret=" + ok);
1305 return ok;
1306 }
1307
1308 /**
1309 * Make sure the caller has the READ_PHONE_STATE permission.
1310 *
1311 * @throws SecurityException if the caller does not have the required permission
1312 */
1313 private void enforceReadPermission() {
1314 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE, null);
1315 }
1316
1317 /**
1318 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1319 *
1320 * @throws SecurityException if the caller does not have the required permission
1321 */
1322 private void enforceModifyPermission() {
1323 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1324 }
1325
1326 /**
Junda Liua2e36012014-07-09 18:30:01 -07001327 * Make sure either system app or the caller has carrier privilege.
1328 *
1329 * @throws SecurityException if the caller does not have the required permission/privilege
1330 */
1331 private void enforceModifyPermissionOrCarrierPrivilege() {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001332 int permission = mApp.checkCallingOrSelfPermission(
1333 android.Manifest.permission.MODIFY_PHONE_STATE);
1334 if (permission == PackageManager.PERMISSION_GRANTED) {
1335 return;
1336 }
1337
1338 log("No modify permission, check carrier privilege next.");
1339 if (hasCarrierPrivileges() != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
1340 loge("No Carrier Privilege.");
1341 throw new SecurityException("No modify permission or carrier privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001342 }
1343 }
1344
1345 /**
1346 * Make sure the caller has carrier privilege.
1347 *
1348 * @throws SecurityException if the caller does not have the required permission
1349 */
1350 private void enforceCarrierPrivilege() {
1351 if (hasCarrierPrivileges() != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001352 loge("No Carrier Privilege.");
1353 throw new SecurityException("No Carrier Privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001354 }
1355 }
1356
1357 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001358 * Make sure the caller has the CALL_PHONE permission.
1359 *
1360 * @throws SecurityException if the caller does not have the required permission
1361 */
1362 private void enforceCallPermission() {
1363 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1364 }
1365
Shishir Agrawal566b7612013-10-28 14:41:00 -07001366 /**
Gabriel Peal36ebb0d2014-03-20 09:20:43 -07001367 * Make sure the caller has the READ_PRIVILEGED_PHONE_STATE permission.
1368 *
1369 * @throws SecurityException if the caller does not have the required permission
1370 */
1371 private void enforcePrivilegedPhoneStatePermission() {
1372 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
1373 null);
1374 }
1375
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001376 private String createTelUrl(String number) {
1377 if (TextUtils.isEmpty(number)) {
1378 return null;
1379 }
1380
Jake Hambye994d462014-02-03 13:10:13 -08001381 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001382 }
1383
Ihab Awadf9e92732013-12-05 18:02:52 -08001384 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001385 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1386 }
1387
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001388 private static void logv(String msg) {
1389 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
1390 }
1391
Ihab Awadf9e92732013-12-05 18:02:52 -08001392 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001393 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1394 }
1395
1396 public int getActivePhoneType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001397 return getActivePhoneTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001398 }
1399
Wink Savilleadd7cc52014-09-08 14:23:09 -07001400 public int getActivePhoneTypeForSubscriber(long subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001401 return getPhone(subId).getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001402 }
1403
1404 /**
1405 * Returns the CDMA ERI icon index to display
1406 */
1407 public int getCdmaEriIconIndex() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001408 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001409
1410 }
1411
Wink Savilleadd7cc52014-09-08 14:23:09 -07001412 public int getCdmaEriIconIndexForSubscriber(long subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001413 return getPhone(subId).getCdmaEriIconIndex();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001414 }
1415
1416 /**
1417 * Returns the CDMA ERI icon mode,
1418 * 0 - ON
1419 * 1 - FLASHING
1420 */
1421 public int getCdmaEriIconMode() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001422 return getCdmaEriIconModeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001423 }
1424
Wink Savilleadd7cc52014-09-08 14:23:09 -07001425 public int getCdmaEriIconModeForSubscriber(long subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001426 return getPhone(subId).getCdmaEriIconMode();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001427 }
1428
1429 /**
1430 * Returns the CDMA ERI text,
1431 */
1432 public String getCdmaEriText() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001433 return getCdmaEriTextForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001434 }
1435
Wink Savilleadd7cc52014-09-08 14:23:09 -07001436 public String getCdmaEriTextForSubscriber(long subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001437 return getPhone(subId).getCdmaEriText();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001438 }
1439
1440 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07001441 * Returns the CDMA MDN.
1442 */
1443 public String getCdmaMdn(long subId) {
1444 enforceModifyPermissionOrCarrierPrivilege();
1445 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1446 return getPhone(subId).getLine1Number();
1447 } else {
1448 return null;
1449 }
1450 }
1451
1452 /**
1453 * Returns the CDMA MIN.
1454 */
1455 public String getCdmaMin(long subId) {
1456 enforceModifyPermissionOrCarrierPrivilege();
1457 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1458 return getPhone(subId).getCdmaMin();
1459 } else {
1460 return null;
1461 }
1462 }
1463
1464 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001465 * Returns true if CDMA provisioning needs to run.
1466 */
1467 public boolean needsOtaServiceProvisioning() {
1468 return mPhone.needsOtaServiceProvisioning();
1469 }
1470
1471 /**
1472 * Returns the unread count of voicemails
1473 */
1474 public int getVoiceMessageCount() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001475 return getVoiceMessageCountForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001476 }
1477
1478 /**
1479 * Returns the unread count of voicemails for a subId
1480 */
Wink Savilleadd7cc52014-09-08 14:23:09 -07001481 public int getVoiceMessageCountForSubscriber( long subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001482 return getPhone(subId).getVoiceMessageCount();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001483 }
1484
1485 /**
1486 * Returns the data network type
1487 *
1488 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
1489 */
1490 @Override
1491 public int getNetworkType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001492 return getNetworkTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001493 }
1494
1495 /**
1496 * Returns the network type for a subId
1497 */
1498 @Override
Wink Savilleadd7cc52014-09-08 14:23:09 -07001499 public int getNetworkTypeForSubscriber(long subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001500 return getPhone(subId).getServiceState().getDataNetworkType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001501 }
1502
1503 /**
1504 * Returns the data network type
1505 */
1506 @Override
1507 public int getDataNetworkType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001508 return getDataNetworkTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001509 }
1510
1511 /**
1512 * Returns the data network type for a subId
1513 */
1514 @Override
Wink Savilleadd7cc52014-09-08 14:23:09 -07001515 public int getDataNetworkTypeForSubscriber(long subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001516 return getPhone(subId).getServiceState().getDataNetworkType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001517 }
1518
1519 /**
1520 * Returns the data network type
1521 */
1522 @Override
1523 public int getVoiceNetworkType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001524 return getVoiceNetworkTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001525 }
1526
1527 /**
1528 * Returns the Voice network type for a subId
1529 */
1530 @Override
Wink Savilleadd7cc52014-09-08 14:23:09 -07001531 public int getVoiceNetworkTypeForSubscriber(long subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001532 return getPhone(subId).getServiceState().getVoiceNetworkType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001533 }
1534
1535 /**
1536 * @return true if a ICC card is present
1537 */
1538 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07001539 // FIXME Make changes to pass defaultSimId of type int
1540 return hasIccCardUsingSlotId(getDefaultSubscription());
1541 }
1542
1543 /**
1544 * @return true if a ICC card is present for a slotId
1545 */
1546 public boolean hasIccCardUsingSlotId(long slotId) {
1547 return getPhone(slotId).getIccCard().hasIccCard();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001548 }
1549
1550 /**
1551 * Return if the current radio is LTE on CDMA. This
1552 * is a tri-state return value as for a period of time
1553 * the mode may be unknown.
1554 *
1555 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08001556 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001557 */
1558 public int getLteOnCdmaMode() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001559 return getLteOnCdmaModeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001560 }
1561
Wink Savilleadd7cc52014-09-08 14:23:09 -07001562 public int getLteOnCdmaModeForSubscriber(long subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001563 return getPhone(subId).getLteOnCdmaMode();
1564 }
1565
1566 public void setPhone(Phone phone) {
1567 mPhone = phone;
1568 }
1569
1570 /**
1571 * {@hide}
1572 * Returns Default subId, 0 in the case of single standby.
1573 */
1574 private long getDefaultSubscription() {
1575 return SubscriptionManager.getDefaultSubId();
1576 }
1577
1578 private long getPreferredVoiceSubscription() {
1579 return SubscriptionManager.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001580 }
Ihab Awadf2177b72013-11-25 13:33:23 -08001581
1582 /**
1583 * @see android.telephony.TelephonyManager.WifiCallingChoices
1584 */
1585 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08001586 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
1587 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08001588 }
1589
1590 /**
1591 * @see android.telephony.TelephonyManager.WifiCallingChoices
1592 */
1593 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08001594 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
1595 Settings.System.putInt(mPhone.getContext().getContentResolver(),
1596 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08001597 }
1598
Sailesh Nepald1e68152013-12-12 19:08:02 -08001599 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07001600 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08001601 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08001602 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08001603
Shishir Agrawal566b7612013-10-28 14:41:00 -07001604 @Override
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001605 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(String AID) {
Junda Liua2e36012014-07-09 18:30:01 -07001606 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001607
1608 if (DBG) log("iccOpenLogicalChannel: " + AID);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001609 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
1610 CMD_OPEN_CHANNEL, AID);
1611 if (DBG) log("iccOpenLogicalChannel: " + response);
1612 return response;
Shishir Agrawal566b7612013-10-28 14:41:00 -07001613 }
1614
1615 @Override
1616 public boolean iccCloseLogicalChannel(int channel) {
Junda Liua2e36012014-07-09 18:30:01 -07001617 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001618
1619 if (DBG) log("iccCloseLogicalChannel: " + channel);
1620 if (channel < 0) {
1621 return false;
1622 }
Jake Hambye994d462014-02-03 13:10:13 -08001623 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07001624 if (DBG) log("iccCloseLogicalChannel: " + success);
1625 return success;
1626 }
1627
1628 @Override
1629 public String iccTransmitApduLogicalChannel(int channel, int cla,
1630 int command, int p1, int p2, int p3, String data) {
Junda Liua2e36012014-07-09 18:30:01 -07001631 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001632
1633 if (DBG) {
1634 log("iccTransmitApduLogicalChannel: chnl=" + channel + " cla=" + cla +
1635 " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
1636 " data=" + data);
1637 }
1638
1639 if (channel < 0) {
1640 return "";
1641 }
1642
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001643 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Shishir Agrawal566b7612013-10-28 14:41:00 -07001644 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data));
1645 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
1646
Shishir Agrawal566b7612013-10-28 14:41:00 -07001647 // Append the returned status code to the end of the response payload.
1648 String s = Integer.toHexString(
1649 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07001650 if (response.payload != null) {
1651 s = IccUtils.bytesToHexString(response.payload) + s;
1652 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07001653 return s;
1654 }
Jake Hambye994d462014-02-03 13:10:13 -08001655
Evan Charltonc66da362014-05-16 14:06:40 -07001656 @Override
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001657 public String iccTransmitApduBasicChannel(int cla, int command, int p1, int p2,
1658 int p3, String data) {
1659 enforceModifyPermissionOrCarrierPrivilege();
1660
1661 if (DBG) {
1662 log("iccTransmitApduBasicChannel: cla=" + cla + " cmd=" + command + " p1="
1663 + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
1664 }
1665
1666 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
1667 new IccAPDUArgument(0, cla, command, p1, p2, p3, data));
1668 if (DBG) log("iccTransmitApduBasicChannel: " + response);
1669
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001670 // Append the returned status code to the end of the response payload.
1671 String s = Integer.toHexString(
1672 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07001673 if (response.payload != null) {
1674 s = IccUtils.bytesToHexString(response.payload) + s;
1675 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001676 return s;
1677 }
1678
1679 @Override
1680 public byte[] iccExchangeSimIO(int fileID, int command, int p1, int p2, int p3,
1681 String filePath) {
1682 enforceModifyPermissionOrCarrierPrivilege();
1683
1684 if (DBG) {
1685 log("Exchange SIM_IO " + fileID + ":" + command + " " +
1686 p1 + " " + p2 + " " + p3 + ":" + filePath);
1687 }
1688
1689 IccIoResult response =
1690 (IccIoResult)sendRequest(CMD_EXCHANGE_SIM_IO,
1691 new IccAPDUArgument(fileID, command, -1, p1, p2, p3, filePath));
1692
1693 if (DBG) {
1694 log("Exchange SIM_IO [R]" + response);
1695 }
1696
1697 byte[] result = null;
1698 int length = 2;
1699 if (response.payload != null) {
1700 length = 2 + response.payload.length;
1701 result = new byte[length];
1702 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
1703 } else {
1704 result = new byte[length];
1705 }
1706
1707 result[length - 1] = (byte) response.sw2;
1708 result[length - 2] = (byte) response.sw1;
1709 return result;
1710 }
1711
1712 @Override
Evan Charltonc66da362014-05-16 14:06:40 -07001713 public String sendEnvelopeWithStatus(String content) {
Junda Liua2e36012014-07-09 18:30:01 -07001714 enforceModifyPermissionOrCarrierPrivilege();
Evan Charltonc66da362014-05-16 14:06:40 -07001715
1716 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content);
1717 if (response.payload == null) {
1718 return "";
1719 }
1720
1721 // Append the returned status code to the end of the response payload.
1722 String s = Integer.toHexString(
1723 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
1724 s = IccUtils.bytesToHexString(response.payload) + s;
1725 return s;
1726 }
1727
Jake Hambye994d462014-02-03 13:10:13 -08001728 /**
1729 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
1730 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
1731 *
1732 * @param itemID the ID of the item to read
1733 * @return the NV item as a String, or null on error.
1734 */
1735 @Override
1736 public String nvReadItem(int itemID) {
Junda Liua2e36012014-07-09 18:30:01 -07001737 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001738 if (DBG) log("nvReadItem: item " + itemID);
1739 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
1740 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
1741 return value;
1742 }
1743
1744 /**
1745 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
1746 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
1747 *
1748 * @param itemID the ID of the item to read
1749 * @param itemValue the value to write, as a String
1750 * @return true on success; false on any failure
1751 */
1752 @Override
1753 public boolean nvWriteItem(int itemID, String itemValue) {
Junda Liua2e36012014-07-09 18:30:01 -07001754 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001755 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
1756 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
1757 new Pair<Integer, String>(itemID, itemValue));
1758 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
1759 return success;
1760 }
1761
1762 /**
1763 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
1764 * Used for device configuration by some CDMA operators.
1765 *
1766 * @param preferredRoamingList byte array containing the new PRL
1767 * @return true on success; false on any failure
1768 */
1769 @Override
1770 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Junda Liua2e36012014-07-09 18:30:01 -07001771 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001772 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
1773 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
1774 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
1775 return success;
1776 }
1777
1778 /**
1779 * Perform the specified type of NV config reset.
1780 * Used for device configuration by some CDMA operators.
1781 *
1782 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
1783 * @return true on success; false on any failure
1784 */
1785 @Override
1786 public boolean nvResetConfig(int resetType) {
Junda Liua2e36012014-07-09 18:30:01 -07001787 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001788 if (DBG) log("nvResetConfig: type " + resetType);
1789 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
1790 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
1791 return success;
1792 }
Jake Hamby7c27be32014-03-03 13:25:59 -08001793
1794 /**
Wink Saville36469e72014-06-11 15:17:00 -07001795 * {@hide}
1796 * Returns Default sim, 0 in the case of single standby.
1797 */
1798 public int getDefaultSim() {
1799 //TODO Need to get it from Telephony Devcontroller
1800 return 0;
1801 }
1802
ram87fca6f2014-07-18 18:58:44 +05301803 public String[] getPcscfAddress(String apnType) {
Wink Saville36469e72014-06-11 15:17:00 -07001804 enforceReadPermission();
ram87fca6f2014-07-18 18:58:44 +05301805 return mPhone.getPcscfAddress(apnType);
Wink Saville36469e72014-06-11 15:17:00 -07001806 }
1807
1808 public void setImsRegistrationState(boolean registered) {
1809 enforceModifyPermission();
1810 mPhone.setImsRegistrationState(registered);
1811 }
1812
1813 /**
Junda Liu84d15a22014-07-02 11:21:04 -07001814 * Get the calculated preferred network type.
1815 * Used for debugging incorrect network type.
1816 *
1817 * @return the preferred network type, defined in RILConstants.java.
1818 */
1819 @Override
1820 public int getCalculatedPreferredNetworkType() {
1821 enforceReadPermission();
Wink Saville08874612014-08-31 19:19:58 -07001822 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext(), 0); // wink FIXME: need to get PhoneId from somewhere.
Junda Liu84d15a22014-07-02 11:21:04 -07001823 }
1824
1825 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08001826 * Get the preferred network type.
1827 * Used for device configuration by some CDMA operators.
1828 *
1829 * @return the preferred network type, defined in RILConstants.java.
1830 */
1831 @Override
1832 public int getPreferredNetworkType() {
Junda Liua2e36012014-07-09 18:30:01 -07001833 enforceModifyPermissionOrCarrierPrivilege();
Jake Hamby7c27be32014-03-03 13:25:59 -08001834 if (DBG) log("getPreferredNetworkType");
1835 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null);
1836 int networkType = (result != null ? result[0] : -1);
1837 if (DBG) log("getPreferredNetworkType: " + networkType);
1838 return networkType;
1839 }
1840
1841 /**
1842 * Set the preferred network type.
1843 * Used for device configuration by some CDMA operators.
1844 *
1845 * @param networkType the preferred network type, defined in RILConstants.java.
1846 * @return true on success; false on any failure.
1847 */
1848 @Override
1849 public boolean setPreferredNetworkType(int networkType) {
Junda Liua2e36012014-07-09 18:30:01 -07001850 enforceModifyPermissionOrCarrierPrivilege();
Jake Hamby7c27be32014-03-03 13:25:59 -08001851 if (DBG) log("setPreferredNetworkType: type " + networkType);
1852 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType);
1853 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
Junda Liu80bc0d12014-07-14 16:36:44 -07001854 if (success) {
1855 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
1856 Settings.Global.PREFERRED_NETWORK_MODE, networkType);
1857 }
Jake Hamby7c27be32014-03-03 13:25:59 -08001858 return success;
1859 }
Robert Greenwalted86e582014-05-21 20:03:20 -07001860
1861 /**
1862 * Set mobile data enabled
1863 * Used by the user through settings etc to turn on/off mobile data
1864 *
1865 * @param enable {@code true} turn turn data on, else {@code false}
1866 */
1867 @Override
1868 public void setDataEnabled(boolean enable) {
1869 enforceModifyPermission();
1870 mPhone.setDataEnabled(enable);
1871 }
1872
1873 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07001874 * Get whether mobile data is enabled.
1875 *
1876 * Note that this used to be available from ConnectivityService, gated by
1877 * ACCESS_NETWORK_STATE permission, so this will accept either that or
1878 * our MODIFY_PHONE_STATE.
Robert Greenwalted86e582014-05-21 20:03:20 -07001879 *
1880 * @return {@code true} if data is enabled else {@code false}
1881 */
1882 @Override
1883 public boolean getDataEnabled() {
Robert Greenwalt646120a2014-05-23 11:54:03 -07001884 try {
1885 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
1886 null);
1887 } catch (Exception e) {
1888 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE,
1889 null);
1890 }
Robert Greenwalted86e582014-05-21 20:03:20 -07001891 return mPhone.getDataEnabled();
1892 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07001893
1894 @Override
1895 public int hasCarrierPrivileges() {
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07001896 UiccCard card = UiccController.getInstance().getUiccCard();
1897 if (card == null) {
1898 loge("hasCarrierPrivileges: No UICC");
1899 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
1900 }
1901 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001902 mPhone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07001903 }
Junda Liu29340342014-07-10 15:23:27 -07001904
1905 @Override
Shishir Agrawal6d5a2852014-07-11 16:32:57 -07001906 public int checkCarrierPrivilegesForPackage(String pkgname) {
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07001907 UiccCard card = UiccController.getInstance().getUiccCard();
1908 if (card == null) {
1909 loge("checkCarrierPrivilegesForPackage: No UICC");
1910 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
1911 }
1912 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgname);
Junda Liu29340342014-07-10 15:23:27 -07001913 }
Derek Tan89e89d42014-07-08 17:00:10 -07001914
1915 @Override
Diego Pontorieroaf74c862014-08-28 11:51:16 -07001916 public List<String> getCarrierPackageNamesForIntent(Intent intent) {
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07001917 UiccCard card = UiccController.getInstance().getUiccCard();
1918 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07001919 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07001920 return null ;
1921 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07001922 return card.getCarrierPackageNamesForIntent(
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07001923 mPhone.getContext().getPackageManager(), intent);
1924 }
1925
Derek Tan97ebb422014-09-05 16:55:38 -07001926 private String getIccId(long subId) {
1927 UiccCard card = getPhone(subId).getUiccCard();
1928 if (card == null) {
1929 loge("getIccId: No UICC");
1930 return null;
1931 }
1932 String iccId = card.getIccId();
1933 if (TextUtils.isEmpty(iccId)) {
1934 loge("getIccId: ICC ID is null or empty.");
1935 return null;
1936 }
1937 return iccId;
1938 }
1939
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07001940 @Override
Junda Liudf6b6b92014-09-05 18:13:46 -07001941 public void enableSimplifiedNetworkSettingsForSubscriber(long subId, boolean enable) {
Derek Tan352d8cd2014-07-12 12:57:11 -07001942 enforceModifyPermissionOrCarrierPrivilege();
Derek Tan97ebb422014-09-05 16:55:38 -07001943
1944 String iccId = getIccId(subId);
1945 if (iccId != null) {
1946 String snsPrefKey = PREF_CARRIERS_SIMPLIFIED_NETWORK_SETTINGS_PREFIX + iccId;
1947 SharedPreferences.Editor editor = carrierPrivilegeConfigs.edit();
1948 if (enable) {
1949 editor.putBoolean(snsPrefKey, true);
1950 } else {
1951 editor.remove(snsPrefKey);
1952 }
1953 editor.commit();
Derek Tan89e89d42014-07-08 17:00:10 -07001954 }
1955 }
1956
1957 @Override
Junda Liudf6b6b92014-09-05 18:13:46 -07001958 public boolean getSimplifiedNetworkSettingsEnabledForSubscriber(long subId) {
Derek Tan89e89d42014-07-08 17:00:10 -07001959 enforceReadPermission();
Derek Tan97ebb422014-09-05 16:55:38 -07001960 String iccId = getIccId(subId);
1961 if (iccId != null) {
1962 String snsPrefKey = PREF_CARRIERS_SIMPLIFIED_NETWORK_SETTINGS_PREFIX + iccId;
1963 return carrierPrivilegeConfigs.getBoolean(snsPrefKey, false);
1964 }
1965 return false;
Derek Tan89e89d42014-07-08 17:00:10 -07001966 }
Derek Tan7226c842014-07-02 17:42:23 -07001967
1968 @Override
Junda Liudf6b6b92014-09-05 18:13:46 -07001969 public void setLine1NumberForDisplayForSubscriber(long subId, String alphaTag, String number) {
Derek Tan352d8cd2014-07-12 12:57:11 -07001970 enforceModifyPermissionOrCarrierPrivilege();
Derek Tan97ebb422014-09-05 16:55:38 -07001971
1972 String iccId = getIccId(subId);
1973 if (iccId != null) {
1974 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
1975 SharedPreferences.Editor editor = carrierPrivilegeConfigs.edit();
1976 if (alphaTag == null) {
1977 editor.remove(alphaTagPrefKey);
1978 } else {
1979 editor.putString(alphaTagPrefKey, alphaTag);
1980 }
1981
1982 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
1983 if (number == null) {
1984 editor.remove(numberPrefKey);
1985 } else {
1986 editor.putString(numberPrefKey, number);
1987 }
1988 editor.commit();
1989 }
Derek Tan7226c842014-07-02 17:42:23 -07001990 }
1991
1992 @Override
1993 public String getLine1NumberForDisplay(long subId) {
1994 enforceReadPermission();
Derek Tan97ebb422014-09-05 16:55:38 -07001995
1996 String iccId = getIccId(subId);
1997 if (iccId != null) {
1998 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
1999 return carrierPrivilegeConfigs.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002000 }
Derek Tan97ebb422014-09-05 16:55:38 -07002001 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002002 }
2003
2004 @Override
2005 public String getLine1AlphaTagForDisplay(long subId) {
2006 enforceReadPermission();
Derek Tan97ebb422014-09-05 16:55:38 -07002007
2008 String iccId = getIccId(subId);
2009 if (iccId != null) {
2010 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
2011 return carrierPrivilegeConfigs.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002012 }
Derek Tan97ebb422014-09-05 16:55:38 -07002013 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002014 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002015
2016 @Override
Shishir Agrawala3dfd752014-09-04 13:25:42 -07002017 public boolean setOperatorBrandOverride(String brand) {
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002018 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawala3dfd752014-09-04 13:25:42 -07002019 return mPhone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002020 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05002021
2022 @Override
2023 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
2024 enforceModifyPermission();
2025
2026 int returnValue = 0;
2027 try {
2028 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
2029 if(result.exception == null) {
2030 if (result.result != null) {
2031 byte[] responseData = (byte[])(result.result);
2032 if(responseData.length > oemResp.length) {
2033 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
2034 responseData.length + "bytes. Buffer Size is " +
2035 oemResp.length + "bytes.");
2036 }
2037 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
2038 returnValue = responseData.length;
2039 }
2040 } else {
2041 CommandException ex = (CommandException) result.exception;
2042 returnValue = ex.getCommandError().ordinal();
2043 if(returnValue > 0) returnValue *= -1;
2044 }
2045 } catch (RuntimeException e) {
2046 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
2047 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
2048 if(returnValue > 0) returnValue *= -1;
2049 }
2050
2051 return returnValue;
2052 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002053}