blob: 26e31b0e531d740cf46abfecc819bc2515256aab [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;
40import android.os.UserHandle;
Derek Tan97ebb422014-09-05 16:55:38 -070041import android.preference.PreferenceManager;
Ihab Awadf2177b72013-11-25 13:33:23 -080042import android.provider.Settings;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070043import android.telephony.CellInfo;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -070044import android.telephony.IccOpenLogicalChannelResponse;
Jake Hambye994d462014-02-03 13:10:13 -080045import android.telephony.NeighboringCellInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070046import android.telephony.ServiceState;
Wink Saville36469e72014-06-11 15:17:00 -070047import android.telephony.SubscriptionManager;
Ihab Awadf2177b72013-11-25 13:33:23 -080048import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070049import android.text.TextUtils;
50import android.util.Log;
Jake Hambye994d462014-02-03 13:10:13 -080051import android.util.Pair;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070052
Shishir Agrawal566b7612013-10-28 14:41:00 -070053import com.android.internal.telephony.CallManager;
54import com.android.internal.telephony.CommandException;
Gabriel Peal36ebb0d2014-03-20 09:20:43 -070055import com.android.internal.telephony.Connection;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070056import com.android.internal.telephony.DefaultPhoneNotifier;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070057import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -080058import com.android.internal.telephony.IccCard;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070059import com.android.internal.telephony.Phone;
Wink Saville36469e72014-06-11 15:17:00 -070060import com.android.internal.telephony.PhoneFactory;
61import com.android.internal.telephony.CallManager;
62import com.android.internal.telephony.CommandException;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070063import com.android.internal.telephony.PhoneConstants;
Wink Saville36469e72014-06-11 15:17:00 -070064import com.android.internal.telephony.dataconnection.DctController;
Derek Tan7226c842014-07-02 17:42:23 -070065import com.android.internal.telephony.uicc.AdnRecord;
Shishir Agrawal566b7612013-10-28 14:41:00 -070066import com.android.internal.telephony.uicc.IccIoResult;
67import com.android.internal.telephony.uicc.IccUtils;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -070068import com.android.internal.telephony.uicc.UiccCard;
Shishir Agrawal60f9c952014-06-23 12:00:43 -070069import com.android.internal.telephony.uicc.UiccCarrierPrivilegeRules;
Shishir Agrawal566b7612013-10-28 14:41:00 -070070import com.android.internal.telephony.uicc.UiccController;
Jake Hambye994d462014-02-03 13:10:13 -080071import com.android.internal.util.HexDump;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070072
Wink Saville36469e72014-06-11 15:17:00 -070073import static com.android.internal.telephony.PhoneConstants.SUBSCRIPTION_KEY;
74
Santos Cordon7d4ddf62013-07-10 11:58:08 -070075import java.util.ArrayList;
Gabriel Peal36ebb0d2014-03-20 09:20:43 -070076import java.util.HashMap;
Derek Tan89e89d42014-07-08 17:00:10 -070077import java.util.HashSet;
Gabriel Peal36ebb0d2014-03-20 09:20:43 -070078import java.util.Iterator;
Jake Hambye994d462014-02-03 13:10:13 -080079import java.util.List;
Gabriel Peal36ebb0d2014-03-20 09:20:43 -070080import java.util.Map;
Derek Tan89e89d42014-07-08 17:00:10 -070081import java.util.Set;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070082
83/**
84 * Implementation of the ITelephony interface.
85 */
Santos Cordon117fee72014-05-16 17:56:12 -070086public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -070087 private static final String LOG_TAG = "PhoneInterfaceManager";
88 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
89 private static final boolean DBG_LOC = false;
90
91 // Message codes used with mMainThreadHandler
92 private static final int CMD_HANDLE_PIN_MMI = 1;
93 private static final int CMD_HANDLE_NEIGHBORING_CELL = 2;
94 private static final int EVENT_NEIGHBORING_CELL_DONE = 3;
95 private static final int CMD_ANSWER_RINGING_CALL = 4;
96 private static final int CMD_END_CALL = 5; // not used yet
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -070097 private static final int CMD_TRANSMIT_APDU_LOGICAL_CHANNEL = 7;
98 private static final int EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE = 8;
Shishir Agrawal566b7612013-10-28 14:41:00 -070099 private static final int CMD_OPEN_CHANNEL = 9;
100 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
101 private static final int CMD_CLOSE_CHANNEL = 11;
102 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -0800103 private static final int CMD_NV_READ_ITEM = 13;
104 private static final int EVENT_NV_READ_ITEM_DONE = 14;
105 private static final int CMD_NV_WRITE_ITEM = 15;
106 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
107 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
108 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
109 private static final int CMD_NV_RESET_CONFIG = 19;
110 private static final int EVENT_NV_RESET_CONFIG_DONE = 20;
Jake Hamby7c27be32014-03-03 13:25:59 -0800111 private static final int CMD_GET_PREFERRED_NETWORK_TYPE = 21;
112 private static final int EVENT_GET_PREFERRED_NETWORK_TYPE_DONE = 22;
113 private static final int CMD_SET_PREFERRED_NETWORK_TYPE = 23;
114 private static final int EVENT_SET_PREFERRED_NETWORK_TYPE_DONE = 24;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800115 private static final int CMD_SEND_ENVELOPE = 25;
116 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Derek Tan6b088ee2014-09-05 14:15:18 -0700117 private static final int CMD_INVOKE_OEM_RIL_REQUEST_RAW = 27;
118 private static final int EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE = 28;
119 private static final int CMD_TRANSMIT_APDU_BASIC_CHANNEL = 29;
120 private static final int EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE = 30;
121 private static final int CMD_EXCHANGE_SIM_IO = 31;
122 private static final int EVENT_EXCHANGE_SIM_IO_DONE = 32;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700123
124 /** The singleton instance. */
125 private static PhoneInterfaceManager sInstance;
126
127 PhoneGlobals mApp;
128 Phone mPhone;
129 CallManager mCM;
130 AppOpsManager mAppOps;
131 MainThreadHandler mMainThreadHandler;
132
Derek Tan97ebb422014-09-05 16:55:38 -0700133 SharedPreferences carrierPrivilegeConfigs;
134 private static final String PREF_CARRIERS_ALPHATAG_PREFIX = "carrier_alphtag_";
135 private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
136 private static final String PREF_CARRIERS_SIMPLIFIED_NETWORK_SETTINGS_PREFIX =
137 "carrier_simplified_network_settings_";
Derek Tan89e89d42014-07-08 17:00:10 -0700138
139 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700140 * A request object to use for transmitting data to an ICC.
141 */
142 private static final class IccAPDUArgument {
143 public int channel, cla, command, p1, p2, p3;
144 public String data;
145
146 public IccAPDUArgument(int channel, int cla, int command,
147 int p1, int p2, int p3, String data) {
148 this.channel = channel;
149 this.cla = cla;
150 this.command = command;
151 this.p1 = p1;
152 this.p2 = p2;
153 this.p3 = p3;
154 this.data = data;
155 }
156 }
157
158 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700159 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
160 * request after sending. The main thread will notify the request when it is complete.
161 */
162 private static final class MainThreadRequest {
163 /** The argument to use for the request */
164 public Object argument;
165 /** The result of the request that is run on the main thread */
166 public Object result;
167
168 public MainThreadRequest(Object argument) {
169 this.argument = argument;
170 }
171 }
172
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800173 private static final class IncomingThirdPartyCallArgs {
174 public final ComponentName component;
175 public final String callId;
176 public final String callerDisplayName;
177
178 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
179 String callerDisplayName) {
180 this.component = component;
181 this.callId = callId;
182 this.callerDisplayName = callerDisplayName;
183 }
184 }
185
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700186 /**
187 * A handler that processes messages on the main thread in the phone process. Since many
188 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
189 * inbound binder threads to the main thread in the phone process. The Binder thread
190 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
191 * on, which will be notified when the operation completes and will contain the result of the
192 * request.
193 *
194 * <p>If a MainThreadRequest object is provided in the msg.obj field,
195 * note that request.result must be set to something non-null for the calling thread to
196 * unblock.
197 */
198 private final class MainThreadHandler extends Handler {
199 @Override
200 public void handleMessage(Message msg) {
201 MainThreadRequest request;
202 Message onCompleted;
203 AsyncResult ar;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700204 UiccCard uiccCard = UiccController.getInstance().getUiccCard();
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700205 IccAPDUArgument iccArgument;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700206
207 switch (msg.what) {
208 case CMD_HANDLE_PIN_MMI:
209 request = (MainThreadRequest) msg.obj;
Jake Hambye994d462014-02-03 13:10:13 -0800210 request.result = mPhone.handlePinMmi((String) request.argument);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700211 // Wake up the requesting thread
212 synchronized (request) {
213 request.notifyAll();
214 }
215 break;
216
217 case CMD_HANDLE_NEIGHBORING_CELL:
218 request = (MainThreadRequest) msg.obj;
219 onCompleted = obtainMessage(EVENT_NEIGHBORING_CELL_DONE,
220 request);
221 mPhone.getNeighboringCids(onCompleted);
222 break;
223
224 case EVENT_NEIGHBORING_CELL_DONE:
225 ar = (AsyncResult) msg.obj;
226 request = (MainThreadRequest) ar.userObj;
227 if (ar.exception == null && ar.result != null) {
228 request.result = ar.result;
229 } else {
230 // create an empty list to notify the waiting thread
Jake Hambye994d462014-02-03 13:10:13 -0800231 request.result = new ArrayList<NeighboringCellInfo>(0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700232 }
233 // Wake up the requesting thread
234 synchronized (request) {
235 request.notifyAll();
236 }
237 break;
238
239 case CMD_ANSWER_RINGING_CALL:
240 answerRingingCallInternal();
241 break;
242
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700243 case CMD_END_CALL:
244 request = (MainThreadRequest) msg.obj;
Jake Hambye994d462014-02-03 13:10:13 -0800245 boolean hungUp;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700246 int phoneType = mPhone.getPhoneType();
247 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
248 // CDMA: If the user presses the Power button we treat it as
249 // ending the complete call session
250 hungUp = PhoneUtils.hangupRingingAndActive(mPhone);
251 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
252 // GSM: End the call as per the Phone state
253 hungUp = PhoneUtils.hangup(mCM);
254 } else {
255 throw new IllegalStateException("Unexpected phone type: " + phoneType);
256 }
257 if (DBG) log("CMD_END_CALL: " + (hungUp ? "hung up!" : "no call to hang up"));
258 request.result = hungUp;
259 // Wake up the requesting thread
260 synchronized (request) {
261 request.notifyAll();
262 }
263 break;
264
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700265 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700266 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700267 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700268 if (uiccCard == null) {
269 loge("iccTransmitApduLogicalChannel: No UICC");
270 request.result = new IccIoResult(0x6F, 0, (byte[])null);
271 synchronized (request) {
272 request.notifyAll();
273 }
274 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700275 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
276 request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700277 uiccCard.iccTransmitApduLogicalChannel(
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700278 iccArgument.channel, iccArgument.cla, iccArgument.command,
279 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
Shishir Agrawal566b7612013-10-28 14:41:00 -0700280 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700281 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700282 break;
283
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700284 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700285 ar = (AsyncResult) msg.obj;
286 request = (MainThreadRequest) ar.userObj;
287 if (ar.exception == null && ar.result != null) {
288 request.result = ar.result;
289 } else {
290 request.result = new IccIoResult(0x6F, 0, (byte[])null);
291 if (ar.result == null) {
292 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800293 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700294 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800295 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700296 } else {
297 loge("iccTransmitApduLogicalChannel: Unknown exception");
298 }
299 }
300 synchronized (request) {
301 request.notifyAll();
302 }
303 break;
304
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700305 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
306 request = (MainThreadRequest) msg.obj;
307 iccArgument = (IccAPDUArgument) request.argument;
308 if (uiccCard == null) {
309 loge("iccTransmitApduBasicChannel: No UICC");
310 request.result = new IccIoResult(0x6F, 0, (byte[])null);
311 synchronized (request) {
312 request.notifyAll();
313 }
314 } else {
315 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
316 request);
317 uiccCard.iccTransmitApduBasicChannel(
318 iccArgument.cla, iccArgument.command, iccArgument.p1, iccArgument.p2,
319 iccArgument.p3, iccArgument.data, onCompleted);
320 }
321 break;
322
323 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
324 ar = (AsyncResult) msg.obj;
325 request = (MainThreadRequest) ar.userObj;
326 if (ar.exception == null && ar.result != null) {
327 request.result = ar.result;
328 } else {
329 request.result = new IccIoResult(0x6F, 0, (byte[])null);
330 if (ar.result == null) {
331 loge("iccTransmitApduBasicChannel: Empty response");
332 } else if (ar.exception instanceof CommandException) {
333 loge("iccTransmitApduBasicChannel: CommandException: " +
334 ar.exception);
335 } else {
336 loge("iccTransmitApduBasicChannel: Unknown exception");
337 }
338 }
339 synchronized (request) {
340 request.notifyAll();
341 }
342 break;
343
344 case CMD_EXCHANGE_SIM_IO:
345 request = (MainThreadRequest) msg.obj;
346 iccArgument = (IccAPDUArgument) request.argument;
347 if (uiccCard == null) {
348 loge("iccExchangeSimIO: No UICC");
349 request.result = new IccIoResult(0x6F, 0, (byte[])null);
350 synchronized (request) {
351 request.notifyAll();
352 }
353 } else {
354 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
355 request);
356 uiccCard.iccExchangeSimIO(iccArgument.cla, /* fileID */
357 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
358 iccArgument.data, onCompleted);
359 }
360 break;
361
362 case EVENT_EXCHANGE_SIM_IO_DONE:
363 ar = (AsyncResult) msg.obj;
364 request = (MainThreadRequest) ar.userObj;
365 if (ar.exception == null && ar.result != null) {
366 request.result = ar.result;
367 } else {
368 request.result = new IccIoResult(0x6f, 0, (byte[])null);
369 }
370 synchronized (request) {
371 request.notifyAll();
372 }
373 break;
374
Derek Tan4d5e5c12014-02-04 11:54:58 -0800375 case CMD_SEND_ENVELOPE:
376 request = (MainThreadRequest) msg.obj;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700377 if (uiccCard == null) {
378 loge("sendEnvelopeWithStatus: No UICC");
379 request.result = new IccIoResult(0x6F, 0, (byte[])null);
380 synchronized (request) {
381 request.notifyAll();
382 }
383 } else {
384 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
385 uiccCard.sendEnvelopeWithStatus((String)request.argument, onCompleted);
386 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800387 break;
388
389 case EVENT_SEND_ENVELOPE_DONE:
390 ar = (AsyncResult) msg.obj;
391 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700392 if (ar.exception == null && ar.result != null) {
393 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800394 } else {
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700395 request.result = new IccIoResult(0x6F, 0, (byte[])null);
396 if (ar.result == null) {
397 loge("sendEnvelopeWithStatus: Empty response");
398 } else if (ar.exception instanceof CommandException) {
399 loge("sendEnvelopeWithStatus: CommandException: " +
400 ar.exception);
401 } else {
402 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
403 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800404 }
405 synchronized (request) {
406 request.notifyAll();
407 }
408 break;
409
Shishir Agrawal566b7612013-10-28 14:41:00 -0700410 case CMD_OPEN_CHANNEL:
411 request = (MainThreadRequest) msg.obj;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700412 if (uiccCard == null) {
413 loge("iccOpenLogicalChannel: No UICC");
414 request.result = new IccIoResult(0x6F, 0, (byte[])null);
415 synchronized (request) {
416 request.notifyAll();
417 }
418 } else {
419 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
420 uiccCard.iccOpenLogicalChannel((String)request.argument, onCompleted);
421 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700422 break;
423
424 case EVENT_OPEN_CHANNEL_DONE:
425 ar = (AsyncResult) msg.obj;
426 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700427 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700428 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700429 int[] result = (int[]) ar.result;
430 int channelId = result[0];
431 byte[] selectResponse = null;
432 if (result.length > 1) {
433 selectResponse = new byte[result.length - 1];
434 for (int i = 1; i < result.length; ++i) {
435 selectResponse[i - 1] = (byte) result[i];
436 }
437 }
438 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700439 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700440 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700441 if (ar.result == null) {
442 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700443 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700444 if (ar.exception != null) {
445 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
446 }
447
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700448 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700449 if ((ar.exception != null) && (ar.exception instanceof CommandException)) {
450 if (ar.exception.getMessage().compareTo("MISSING_RESOURCE") == 0) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700451 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700452 } else if (ar.exception.getMessage().compareTo("NO_SUCH_ELEMENT") == 0) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700453 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700454 }
455 }
456 openChannelResp = new IccOpenLogicalChannelResponse(
457 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700458 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700459 request.result = openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700460 synchronized (request) {
461 request.notifyAll();
462 }
463 break;
464
465 case CMD_CLOSE_CHANNEL:
466 request = (MainThreadRequest) msg.obj;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700467 if (uiccCard == null) {
468 loge("iccCloseLogicalChannel: No UICC");
469 request.result = new IccIoResult(0x6F, 0, (byte[])null);
470 synchronized (request) {
471 request.notifyAll();
472 }
473 } else {
474 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
475 uiccCard.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
476 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700477 break;
478
479 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800480 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
481 break;
482
483 case CMD_NV_READ_ITEM:
484 request = (MainThreadRequest) msg.obj;
485 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
486 mPhone.nvReadItem((Integer) request.argument, onCompleted);
487 break;
488
489 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700490 ar = (AsyncResult) msg.obj;
491 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800492 if (ar.exception == null && ar.result != null) {
493 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700494 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800495 request.result = "";
496 if (ar.result == null) {
497 loge("nvReadItem: Empty response");
498 } else if (ar.exception instanceof CommandException) {
499 loge("nvReadItem: CommandException: " +
500 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700501 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800502 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700503 }
504 }
505 synchronized (request) {
506 request.notifyAll();
507 }
508 break;
509
Jake Hambye994d462014-02-03 13:10:13 -0800510 case CMD_NV_WRITE_ITEM:
511 request = (MainThreadRequest) msg.obj;
512 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
513 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
514 mPhone.nvWriteItem(idValue.first, idValue.second, onCompleted);
515 break;
516
517 case EVENT_NV_WRITE_ITEM_DONE:
518 handleNullReturnEvent(msg, "nvWriteItem");
519 break;
520
521 case CMD_NV_WRITE_CDMA_PRL:
522 request = (MainThreadRequest) msg.obj;
523 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
524 mPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
525 break;
526
527 case EVENT_NV_WRITE_CDMA_PRL_DONE:
528 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
529 break;
530
531 case CMD_NV_RESET_CONFIG:
532 request = (MainThreadRequest) msg.obj;
533 onCompleted = obtainMessage(EVENT_NV_RESET_CONFIG_DONE, request);
534 mPhone.nvResetConfig((Integer) request.argument, onCompleted);
535 break;
536
537 case EVENT_NV_RESET_CONFIG_DONE:
538 handleNullReturnEvent(msg, "nvResetConfig");
539 break;
540
Jake Hamby7c27be32014-03-03 13:25:59 -0800541 case CMD_GET_PREFERRED_NETWORK_TYPE:
542 request = (MainThreadRequest) msg.obj;
543 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
544 mPhone.getPreferredNetworkType(onCompleted);
545 break;
546
547 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
548 ar = (AsyncResult) msg.obj;
549 request = (MainThreadRequest) ar.userObj;
550 if (ar.exception == null && ar.result != null) {
551 request.result = ar.result; // Integer
552 } else {
553 request.result = -1;
554 if (ar.result == null) {
555 loge("getPreferredNetworkType: Empty response");
556 } else if (ar.exception instanceof CommandException) {
557 loge("getPreferredNetworkType: CommandException: " +
558 ar.exception);
559 } else {
560 loge("getPreferredNetworkType: Unknown exception");
561 }
562 }
563 synchronized (request) {
564 request.notifyAll();
565 }
566 break;
567
568 case CMD_SET_PREFERRED_NETWORK_TYPE:
569 request = (MainThreadRequest) msg.obj;
570 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
571 int networkType = (Integer) request.argument;
572 mPhone.setPreferredNetworkType(networkType, onCompleted);
573 break;
574
575 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
576 handleNullReturnEvent(msg, "setPreferredNetworkType");
577 break;
578
Steven Liu4bf01bc2014-07-17 11:05:29 -0500579 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
580 request = (MainThreadRequest)msg.obj;
581 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
582 mPhone.invokeOemRilRequestRaw((byte[])request.argument, onCompleted);
583 break;
584
585 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
586 ar = (AsyncResult)msg.obj;
587 request = (MainThreadRequest)ar.userObj;
588 request.result = ar;
589 synchronized (request) {
590 request.notifyAll();
591 }
592 break;
593
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700594 default:
595 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
596 break;
597 }
598 }
Jake Hambye994d462014-02-03 13:10:13 -0800599
600 private void handleNullReturnEvent(Message msg, String command) {
601 AsyncResult ar = (AsyncResult) msg.obj;
602 MainThreadRequest request = (MainThreadRequest) ar.userObj;
603 if (ar.exception == null) {
604 request.result = true;
605 } else {
606 request.result = false;
607 if (ar.exception instanceof CommandException) {
608 loge(command + ": CommandException: " + ar.exception);
609 } else {
610 loge(command + ": Unknown exception");
611 }
612 }
613 synchronized (request) {
614 request.notifyAll();
615 }
616 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700617 }
618
619 /**
620 * Posts the specified command to be executed on the main thread,
621 * waits for the request to complete, and returns the result.
622 * @see #sendRequestAsync
623 */
624 private Object sendRequest(int command, Object argument) {
Santos Cordon500b0e02014-06-17 10:33:33 -0700625 return sendRequest(command, argument, null);
Wink Saville36469e72014-06-11 15:17:00 -0700626 }
627
628 /**
629 * Posts the specified command to be executed on the main thread,
630 * waits for the request to complete, and returns the result.
631 * @see #sendRequestAsync
632 */
633 private Object sendRequest(int command, Object argument, Object argument2) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700634 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
635 throw new RuntimeException("This method will deadlock if called from the main thread.");
636 }
637
638 MainThreadRequest request = new MainThreadRequest(argument);
639 Message msg = mMainThreadHandler.obtainMessage(command, request);
640 msg.sendToTarget();
641
642 // Wait for the request to complete
643 synchronized (request) {
644 while (request.result == null) {
645 try {
646 request.wait();
647 } catch (InterruptedException e) {
648 // Do nothing, go back and wait until the request is complete
649 }
650 }
651 }
652 return request.result;
653 }
654
655 /**
656 * Asynchronous ("fire and forget") version of sendRequest():
657 * Posts the specified command to be executed on the main thread, and
658 * returns immediately.
659 * @see #sendRequest
660 */
661 private void sendRequestAsync(int command) {
662 mMainThreadHandler.sendEmptyMessage(command);
663 }
664
665 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -0700666 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
667 * @see {@link #sendRequest(int,Object)}
668 */
669 private void sendRequestAsync(int command, Object argument) {
670 MainThreadRequest request = new MainThreadRequest(argument);
671 Message msg = mMainThreadHandler.obtainMessage(command, request);
672 msg.sendToTarget();
673 }
674
675 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700676 * Initialize the singleton PhoneInterfaceManager instance.
677 * This is only done once, at startup, from PhoneApp.onCreate().
678 */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700679 /* package */ static PhoneInterfaceManager init(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700680 synchronized (PhoneInterfaceManager.class) {
681 if (sInstance == null) {
Sailesh Nepal194161e2014-07-03 08:57:44 -0700682 sInstance = new PhoneInterfaceManager(app, phone);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700683 } else {
684 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
685 }
686 return sInstance;
687 }
688 }
689
690 /** Private constructor; @see init() */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700691 private PhoneInterfaceManager(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700692 mApp = app;
693 mPhone = phone;
694 mCM = PhoneGlobals.getInstance().mCM;
695 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
696 mMainThreadHandler = new MainThreadHandler();
Derek Tan97ebb422014-09-05 16:55:38 -0700697 carrierPrivilegeConfigs =
698 PreferenceManager.getDefaultSharedPreferences(mPhone.getContext());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700699 publish();
700 }
701
702 private void publish() {
703 if (DBG) log("publish: " + this);
704
705 ServiceManager.addService("phone", this);
706 }
707
Wink Saville36469e72014-06-11 15:17:00 -0700708 // returns phone associated with the subId.
709 // getPhone(0) returns default phone in single SIM mode.
710 private Phone getPhone(long subId) {
711 // FIXME: hack for the moment
712 return mPhone;
Wink Savilleadd7cc52014-09-08 14:23:09 -0700713 // return PhoneUtils.getPhoneForSubscriber(subId);
Wink Saville36469e72014-06-11 15:17:00 -0700714 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700715 //
716 // Implementation of the ITelephony interface.
717 //
718
719 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700720 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -0700721 }
722
Wink Savilleadd7cc52014-09-08 14:23:09 -0700723 public void dialForSubscriber(long subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700724 if (DBG) log("dial: " + number);
725 // No permission check needed here: This is just a wrapper around the
726 // ACTION_DIAL intent, which is available to any app since it puts up
727 // the UI before it does anything.
728
729 String url = createTelUrl(number);
730 if (url == null) {
731 return;
732 }
733
734 // PENDING: should we just silently fail if phone is offhook or ringing?
Wink Saville36469e72014-06-11 15:17:00 -0700735 PhoneConstants.State state = mCM.getState(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700736 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
737 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
738 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Wink Saville36469e72014-06-11 15:17:00 -0700739 intent.putExtra(SUBSCRIPTION_KEY, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700740 mApp.startActivity(intent);
741 }
742 }
743
744 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700745 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -0700746 }
747
Wink Savilleadd7cc52014-09-08 14:23:09 -0700748 public void callForSubscriber(long subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700749 if (DBG) log("call: " + number);
750
751 // This is just a wrapper around the ACTION_CALL intent, but we still
752 // need to do a permission check since we're calling startActivity()
753 // from the context of the phone app.
754 enforceCallPermission();
755
756 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
757 != AppOpsManager.MODE_ALLOWED) {
758 return;
759 }
760
761 String url = createTelUrl(number);
762 if (url == null) {
763 return;
764 }
765
766 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
Wink Saville36469e72014-06-11 15:17:00 -0700767 intent.putExtra(SUBSCRIPTION_KEY, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700768 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
769 mApp.startActivity(intent);
770 }
771
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700772 /**
773 * End a call based on call state
774 * @return true is a call was ended
775 */
776 public boolean endCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700777 return endCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700778 }
779
780 /**
781 * End a call based on the call state of the subId
782 * @return true is a call was ended
783 */
Wink Savilleadd7cc52014-09-08 14:23:09 -0700784 public boolean endCallForSubscriber(long subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700785 enforceCallPermission();
Wink Saville36469e72014-06-11 15:17:00 -0700786 return (Boolean) sendRequest(CMD_END_CALL, subId, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700787 }
788
789 public void answerRingingCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700790 answerRingingCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700791 }
792
Wink Savilleadd7cc52014-09-08 14:23:09 -0700793 public void answerRingingCallForSubscriber(long subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700794 if (DBG) log("answerRingingCall...");
795 // TODO: there should eventually be a separate "ANSWER_PHONE" permission,
796 // but that can probably wait till the big TelephonyManager API overhaul.
797 // For now, protect this call with the MODIFY_PHONE_STATE permission.
798 enforceModifyPermission();
799 sendRequestAsync(CMD_ANSWER_RINGING_CALL);
800 }
801
802 /**
803 * Make the actual telephony calls to implement answerRingingCall().
804 * This should only be called from the main thread of the Phone app.
805 * @see #answerRingingCall
806 *
807 * TODO: it would be nice to return true if we answered the call, or
808 * false if there wasn't actually a ringing incoming call, or some
809 * other error occurred. (In other words, pass back the return value
810 * from PhoneUtils.answerCall() or PhoneUtils.answerAndEndActive().)
811 * But that would require calling this method via sendRequest() rather
812 * than sendRequestAsync(), and right now we don't actually *need* that
813 * return value, so let's just return void for now.
814 */
815 private void answerRingingCallInternal() {
816 final boolean hasRingingCall = !mPhone.getRingingCall().isIdle();
817 if (hasRingingCall) {
818 final boolean hasActiveCall = !mPhone.getForegroundCall().isIdle();
819 final boolean hasHoldingCall = !mPhone.getBackgroundCall().isIdle();
820 if (hasActiveCall && hasHoldingCall) {
821 // Both lines are in use!
822 // TODO: provide a flag to let the caller specify what
823 // policy to use if both lines are in use. (The current
824 // behavior is hardwired to "answer incoming, end ongoing",
825 // which is how the CALL button is specced to behave.)
826 PhoneUtils.answerAndEndActive(mCM, mCM.getFirstActiveRingingCall());
827 return;
828 } else {
829 // answerCall() will automatically hold the current active
830 // call, if there is one.
831 PhoneUtils.answerCall(mCM.getFirstActiveRingingCall());
832 return;
833 }
834 } else {
835 // No call was ringing.
836 return;
837 }
838 }
839
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700840 /**
Santos Cordon5422a8d2014-09-12 04:20:56 -0700841 * This method is no longer used and can be removed once TelephonyManager stops referring to it.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700842 */
Santos Cordon5422a8d2014-09-12 04:20:56 -0700843 public void silenceRinger() {
844 Log.e(LOG_TAG, "silenseRinger not supported");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700845 }
846
847 public boolean isOffhook() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700848 return isOffhookForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700849 }
850
Wink Savilleadd7cc52014-09-08 14:23:09 -0700851 public boolean isOffhookForSubscriber(long subId) {
Wink Saville36469e72014-06-11 15:17:00 -0700852 return (getPhone(subId).getState() == PhoneConstants.State.OFFHOOK);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700853 }
854
855 public boolean isRinging() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700856 return (isRingingForSubscriber(getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -0700857 }
858
Wink Savilleadd7cc52014-09-08 14:23:09 -0700859 public boolean isRingingForSubscriber(long subId) {
Wink Saville36469e72014-06-11 15:17:00 -0700860 return (getPhone(subId).getState() == PhoneConstants.State.RINGING);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700861 }
862
863 public boolean isIdle() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700864 return isIdleForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700865 }
866
Wink Savilleadd7cc52014-09-08 14:23:09 -0700867 public boolean isIdleForSubscriber(long subId) {
Wink Saville36469e72014-06-11 15:17:00 -0700868 return (getPhone(subId).getState() == PhoneConstants.State.IDLE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700869 }
870
871 public boolean isSimPinEnabled() {
872 enforceReadPermission();
873 return (PhoneGlobals.getInstance().isSimPinEnabled());
874 }
875
876 public boolean supplyPin(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700877 return supplyPinForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -0700878 }
879
Wink Savilleadd7cc52014-09-08 14:23:09 -0700880 public boolean supplyPinForSubscriber(long subId, String pin) {
881 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -0700882 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
883 }
884
885 public boolean supplyPuk(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700886 return supplyPukForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -0700887 }
888
Wink Savilleadd7cc52014-09-08 14:23:09 -0700889 public boolean supplyPukForSubscriber(long subId, String puk, String pin) {
890 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -0700891 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
892 }
893
894 /** {@hide} */
895 public int[] supplyPinReportResult(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700896 return supplyPinReportResultForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -0700897 }
898
Wink Savilleadd7cc52014-09-08 14:23:09 -0700899 public int[] supplyPinReportResultForSubscriber(long subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700900 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -0700901 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700902 checkSimPin.start();
903 return checkSimPin.unlockSim(null, pin);
904 }
905
Wink Saville9de0f752013-10-22 19:04:03 -0700906 /** {@hide} */
907 public int[] supplyPukReportResult(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700908 return supplyPukReportResultForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -0700909 }
910
Wink Savilleadd7cc52014-09-08 14:23:09 -0700911 public int[] supplyPukReportResultForSubscriber(long subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700912 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -0700913 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700914 checkSimPuk.start();
915 return checkSimPuk.unlockSim(puk, pin);
916 }
917
918 /**
Wink Saville9de0f752013-10-22 19:04:03 -0700919 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700920 * a synchronous one.
921 */
922 private static class UnlockSim extends Thread {
923
924 private final IccCard mSimCard;
925
926 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -0700927 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
928 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700929
930 // For replies from SimCard interface
931 private Handler mHandler;
932
933 // For async handler to identify request type
934 private static final int SUPPLY_PIN_COMPLETE = 100;
935
936 public UnlockSim(IccCard simCard) {
937 mSimCard = simCard;
938 }
939
940 @Override
941 public void run() {
942 Looper.prepare();
943 synchronized (UnlockSim.this) {
944 mHandler = new Handler() {
945 @Override
946 public void handleMessage(Message msg) {
947 AsyncResult ar = (AsyncResult) msg.obj;
948 switch (msg.what) {
949 case SUPPLY_PIN_COMPLETE:
950 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
951 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -0700952 mRetryCount = msg.arg1;
953 if (ar.exception != null) {
954 if (ar.exception instanceof CommandException &&
955 ((CommandException)(ar.exception)).getCommandError()
956 == CommandException.Error.PASSWORD_INCORRECT) {
957 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
958 } else {
959 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
960 }
961 } else {
962 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
963 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700964 mDone = true;
965 UnlockSim.this.notifyAll();
966 }
967 break;
968 }
969 }
970 };
971 UnlockSim.this.notifyAll();
972 }
973 Looper.loop();
974 }
975
976 /*
977 * Use PIN or PUK to unlock SIM card
978 *
979 * If PUK is null, unlock SIM card with PIN
980 *
981 * If PUK is not null, unlock SIM card with PUK and set PIN code
982 */
Wink Saville9de0f752013-10-22 19:04:03 -0700983 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700984
985 while (mHandler == null) {
986 try {
987 wait();
988 } catch (InterruptedException e) {
989 Thread.currentThread().interrupt();
990 }
991 }
992 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
993
994 if (puk == null) {
995 mSimCard.supplyPin(pin, callback);
996 } else {
997 mSimCard.supplyPuk(puk, pin, callback);
998 }
999
1000 while (!mDone) {
1001 try {
1002 Log.d(LOG_TAG, "wait for done");
1003 wait();
1004 } catch (InterruptedException e) {
1005 // Restore the interrupted status
1006 Thread.currentThread().interrupt();
1007 }
1008 }
1009 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07001010 int[] resultArray = new int[2];
1011 resultArray[0] = mResult;
1012 resultArray[1] = mRetryCount;
1013 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001014 }
1015 }
1016
1017 public void updateServiceLocation() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001018 updateServiceLocationForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001019
1020 }
1021
Wink Savilleadd7cc52014-09-08 14:23:09 -07001022 public void updateServiceLocationForSubscriber(long subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001023 // No permission check needed here: this call is harmless, and it's
1024 // needed for the ServiceState.requestStateUpdate() call (which is
1025 // already intentionally exposed to 3rd parties.)
Wink Saville36469e72014-06-11 15:17:00 -07001026 getPhone(subId).updateServiceLocation();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001027 }
1028
1029 public boolean isRadioOn() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001030 return isRadioOnForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001031 }
1032
Wink Savilleadd7cc52014-09-08 14:23:09 -07001033 public boolean isRadioOnForSubscriber(long subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001034 return getPhone(subId).getServiceState().getState() != ServiceState.STATE_POWER_OFF;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001035 }
1036
1037 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001038 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001039
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001040 }
Wink Saville36469e72014-06-11 15:17:00 -07001041
Wink Savilleadd7cc52014-09-08 14:23:09 -07001042 public void toggleRadioOnOffForSubscriber(long subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001043 enforceModifyPermission();
Wink Savilleadd7cc52014-09-08 14:23:09 -07001044 getPhone(subId).setRadioPower(!isRadioOnForSubscriber(subId));
Wink Saville36469e72014-06-11 15:17:00 -07001045 }
1046
1047 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001048 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001049 }
1050
Wink Savilleadd7cc52014-09-08 14:23:09 -07001051 public boolean setRadioForSubscriber(long subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07001052 enforceModifyPermission();
1053 if ((getPhone(subId).getServiceState().getState() !=
1054 ServiceState.STATE_POWER_OFF) != turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001055 toggleRadioOnOffForSubscriber(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001056 }
1057 return true;
1058 }
Wink Saville36469e72014-06-11 15:17:00 -07001059
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001060 public boolean needMobileRadioShutdown() {
1061 /*
1062 * If any of the Radios are available, it will need to be
1063 * shutdown. So return true if any Radio is available.
1064 */
1065 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1066 Phone phone = PhoneFactory.getPhone(i);
1067 if (phone != null && phone.isRadioAvailable()) return true;
1068 }
1069 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
1070 return false;
1071 }
1072
1073 public void shutdownMobileRadios() {
1074 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1075 logv("Shutting down Phone " + i);
1076 shutdownRadioUsingPhoneId(i);
1077 }
1078 }
1079
1080 private void shutdownRadioUsingPhoneId(int phoneId) {
1081 enforceModifyPermission();
1082 Phone phone = PhoneFactory.getPhone(phoneId);
1083 if (phone != null && phone.isRadioAvailable()) {
1084 phone.shutdownRadio();
1085 }
1086 }
1087
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001088 public boolean setRadioPower(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001089 return setRadioPowerForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001090 }
1091
Wink Savilleadd7cc52014-09-08 14:23:09 -07001092 public boolean setRadioPowerForSubscriber(long subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001093 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001094 getPhone(subId).setRadioPower(turnOn);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001095 return true;
1096 }
1097
Wink Saville36469e72014-06-11 15:17:00 -07001098 // FIXME: subId version needed
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001099 public boolean enableDataConnectivity() {
1100 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001101 long subId = SubscriptionManager.getDefaultDataSubId();
1102 getPhone(subId).setDataEnabled(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001103 return true;
1104 }
1105
Wink Saville36469e72014-06-11 15:17:00 -07001106 // FIXME: subId version needed
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001107 public boolean disableDataConnectivity() {
1108 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001109 long subId = SubscriptionManager.getDefaultDataSubId();
1110 getPhone(subId).setDataEnabled(false);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001111 return true;
1112 }
1113
Wink Saville36469e72014-06-11 15:17:00 -07001114 // FIXME: subId version needed
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001115 public boolean isDataConnectivityPossible() {
Wink Saville36469e72014-06-11 15:17:00 -07001116 long subId = SubscriptionManager.getDefaultDataSubId();
1117 return getPhone(subId).isDataConnectivityPossible();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001118 }
1119
1120 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001121 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07001122 }
1123
Wink Savilleadd7cc52014-09-08 14:23:09 -07001124 public boolean handlePinMmiForSubscriber(long subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001125 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001126 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001127 }
1128
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001129 public int getCallState() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001130 return getCallStateForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001131 }
1132
Wink Savilleadd7cc52014-09-08 14:23:09 -07001133 public int getCallStateForSubscriber(long subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001134 return DefaultPhoneNotifier.convertCallState(getPhone(subId).getState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001135 }
1136
1137 public int getDataState() {
Wink Saville36469e72014-06-11 15:17:00 -07001138 Phone phone = getPhone(SubscriptionManager.getDefaultDataSubId());
1139 return DefaultPhoneNotifier.convertDataState(phone.getDataConnectionState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001140 }
1141
1142 public int getDataActivity() {
Wink Saville36469e72014-06-11 15:17:00 -07001143 Phone phone = getPhone(SubscriptionManager.getDefaultDataSubId());
1144 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001145 }
1146
1147 @Override
1148 public Bundle getCellLocation() {
1149 try {
1150 mApp.enforceCallingOrSelfPermission(
1151 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1152 } catch (SecurityException e) {
1153 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1154 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1155 // is the weaker precondition
1156 mApp.enforceCallingOrSelfPermission(
1157 android.Manifest.permission.ACCESS_COARSE_LOCATION, null);
1158 }
1159
Jake Hambye994d462014-02-03 13:10:13 -08001160 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001161 if (DBG_LOC) log("getCellLocation: is active user");
1162 Bundle data = new Bundle();
1163 mPhone.getCellLocation().fillInNotifierBundle(data);
1164 return data;
1165 } else {
1166 if (DBG_LOC) log("getCellLocation: suppress non-active user");
1167 return null;
1168 }
1169 }
1170
1171 @Override
1172 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001173 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001174 }
1175
Wink Savilleadd7cc52014-09-08 14:23:09 -07001176 public void enableLocationUpdatesForSubscriber(long subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001177 mApp.enforceCallingOrSelfPermission(
1178 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Wink Saville36469e72014-06-11 15:17:00 -07001179 getPhone(subId).enableLocationUpdates();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001180 }
1181
1182 @Override
1183 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001184 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001185 }
1186
Wink Savilleadd7cc52014-09-08 14:23:09 -07001187 public void disableLocationUpdatesForSubscriber(long subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001188 mApp.enforceCallingOrSelfPermission(
1189 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Wink Saville36469e72014-06-11 15:17:00 -07001190 getPhone(subId).disableLocationUpdates();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001191 }
1192
1193 @Override
1194 @SuppressWarnings("unchecked")
1195 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
1196 try {
1197 mApp.enforceCallingOrSelfPermission(
1198 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1199 } catch (SecurityException e) {
1200 // If we have ACCESS_FINE_LOCATION permission, skip the check
1201 // for ACCESS_COARSE_LOCATION
1202 // A failure should throw the SecurityException from
1203 // ACCESS_COARSE_LOCATION since this is the weaker precondition
1204 mApp.enforceCallingOrSelfPermission(
1205 android.Manifest.permission.ACCESS_COARSE_LOCATION, null);
1206 }
1207
1208 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
1209 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1210 return null;
1211 }
Jake Hambye994d462014-02-03 13:10:13 -08001212 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001213 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
1214
1215 ArrayList<NeighboringCellInfo> cells = null;
1216
1217 try {
1218 cells = (ArrayList<NeighboringCellInfo>) sendRequest(
Wink Saville36469e72014-06-11 15:17:00 -07001219 CMD_HANDLE_NEIGHBORING_CELL, null, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001220 } catch (RuntimeException e) {
Wink Saville36469e72014-06-11 15:17:00 -07001221 Log.e(LOG_TAG, "getNeighboringCellInfo " + e);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001222 }
1223 return cells;
1224 } else {
1225 if (DBG_LOC) log("getNeighboringCellInfo: suppress non-active user");
1226 return null;
1227 }
1228 }
1229
1230
1231 @Override
1232 public List<CellInfo> getAllCellInfo() {
1233 try {
1234 mApp.enforceCallingOrSelfPermission(
1235 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1236 } catch (SecurityException e) {
1237 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1238 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1239 // is the weaker precondition
1240 mApp.enforceCallingOrSelfPermission(
1241 android.Manifest.permission.ACCESS_COARSE_LOCATION, null);
1242 }
1243
Jake Hambye994d462014-02-03 13:10:13 -08001244 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001245 if (DBG_LOC) log("getAllCellInfo: is active user");
1246 return mPhone.getAllCellInfo();
1247 } else {
1248 if (DBG_LOC) log("getAllCellInfo: suppress non-active user");
1249 return null;
1250 }
1251 }
1252
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001253 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001254 public void setCellInfoListRate(int rateInMillis) {
1255 mPhone.setCellInfoListRate(rateInMillis);
1256 }
1257
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001258 //
1259 // Internal helper methods.
1260 //
1261
Jake Hambye994d462014-02-03 13:10:13 -08001262 private static boolean checkIfCallerIsSelfOrForegroundUser() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001263 boolean ok;
1264
1265 boolean self = Binder.getCallingUid() == Process.myUid();
1266 if (!self) {
1267 // Get the caller's user id then clear the calling identity
1268 // which will be restored in the finally clause.
1269 int callingUser = UserHandle.getCallingUserId();
1270 long ident = Binder.clearCallingIdentity();
1271
1272 try {
1273 // With calling identity cleared the current user is the foreground user.
1274 int foregroundUser = ActivityManager.getCurrentUser();
1275 ok = (foregroundUser == callingUser);
1276 if (DBG_LOC) {
1277 log("checkIfCallerIsSelfOrForegoundUser: foregroundUser=" + foregroundUser
1278 + " callingUser=" + callingUser + " ok=" + ok);
1279 }
1280 } catch (Exception ex) {
1281 if (DBG_LOC) loge("checkIfCallerIsSelfOrForegoundUser: Exception ex=" + ex);
1282 ok = false;
1283 } finally {
1284 Binder.restoreCallingIdentity(ident);
1285 }
1286 } else {
1287 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: is self");
1288 ok = true;
1289 }
1290 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: ret=" + ok);
1291 return ok;
1292 }
1293
1294 /**
1295 * Make sure the caller has the READ_PHONE_STATE permission.
1296 *
1297 * @throws SecurityException if the caller does not have the required permission
1298 */
1299 private void enforceReadPermission() {
1300 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE, null);
1301 }
1302
1303 /**
1304 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1305 *
1306 * @throws SecurityException if the caller does not have the required permission
1307 */
1308 private void enforceModifyPermission() {
1309 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1310 }
1311
1312 /**
Junda Liua2e36012014-07-09 18:30:01 -07001313 * Make sure either system app or the caller has carrier privilege.
1314 *
1315 * @throws SecurityException if the caller does not have the required permission/privilege
1316 */
1317 private void enforceModifyPermissionOrCarrierPrivilege() {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001318 int permission = mApp.checkCallingOrSelfPermission(
1319 android.Manifest.permission.MODIFY_PHONE_STATE);
1320 if (permission == PackageManager.PERMISSION_GRANTED) {
1321 return;
1322 }
1323
1324 log("No modify permission, check carrier privilege next.");
1325 if (hasCarrierPrivileges() != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
1326 loge("No Carrier Privilege.");
1327 throw new SecurityException("No modify permission or carrier privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001328 }
1329 }
1330
1331 /**
1332 * Make sure the caller has carrier privilege.
1333 *
1334 * @throws SecurityException if the caller does not have the required permission
1335 */
1336 private void enforceCarrierPrivilege() {
1337 if (hasCarrierPrivileges() != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001338 loge("No Carrier Privilege.");
1339 throw new SecurityException("No Carrier Privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001340 }
1341 }
1342
1343 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001344 * Make sure the caller has the CALL_PHONE permission.
1345 *
1346 * @throws SecurityException if the caller does not have the required permission
1347 */
1348 private void enforceCallPermission() {
1349 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1350 }
1351
Shishir Agrawal566b7612013-10-28 14:41:00 -07001352 /**
Gabriel Peal36ebb0d2014-03-20 09:20:43 -07001353 * Make sure the caller has the READ_PRIVILEGED_PHONE_STATE permission.
1354 *
1355 * @throws SecurityException if the caller does not have the required permission
1356 */
1357 private void enforcePrivilegedPhoneStatePermission() {
1358 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
1359 null);
1360 }
1361
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001362 private String createTelUrl(String number) {
1363 if (TextUtils.isEmpty(number)) {
1364 return null;
1365 }
1366
Jake Hambye994d462014-02-03 13:10:13 -08001367 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001368 }
1369
Ihab Awadf9e92732013-12-05 18:02:52 -08001370 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001371 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1372 }
1373
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001374 private static void logv(String msg) {
1375 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
1376 }
1377
Ihab Awadf9e92732013-12-05 18:02:52 -08001378 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001379 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1380 }
1381
1382 public int getActivePhoneType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001383 return getActivePhoneTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001384 }
1385
Wink Savilleadd7cc52014-09-08 14:23:09 -07001386 public int getActivePhoneTypeForSubscriber(long subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001387 return getPhone(subId).getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001388 }
1389
1390 /**
1391 * Returns the CDMA ERI icon index to display
1392 */
1393 public int getCdmaEriIconIndex() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001394 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001395
1396 }
1397
Wink Savilleadd7cc52014-09-08 14:23:09 -07001398 public int getCdmaEriIconIndexForSubscriber(long subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001399 return getPhone(subId).getCdmaEriIconIndex();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001400 }
1401
1402 /**
1403 * Returns the CDMA ERI icon mode,
1404 * 0 - ON
1405 * 1 - FLASHING
1406 */
1407 public int getCdmaEriIconMode() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001408 return getCdmaEriIconModeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001409 }
1410
Wink Savilleadd7cc52014-09-08 14:23:09 -07001411 public int getCdmaEriIconModeForSubscriber(long subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001412 return getPhone(subId).getCdmaEriIconMode();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001413 }
1414
1415 /**
1416 * Returns the CDMA ERI text,
1417 */
1418 public String getCdmaEriText() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001419 return getCdmaEriTextForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001420 }
1421
Wink Savilleadd7cc52014-09-08 14:23:09 -07001422 public String getCdmaEriTextForSubscriber(long subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001423 return getPhone(subId).getCdmaEriText();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001424 }
1425
1426 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07001427 * Returns the CDMA MDN.
1428 */
1429 public String getCdmaMdn(long subId) {
1430 enforceModifyPermissionOrCarrierPrivilege();
1431 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1432 return getPhone(subId).getLine1Number();
1433 } else {
1434 return null;
1435 }
1436 }
1437
1438 /**
1439 * Returns the CDMA MIN.
1440 */
1441 public String getCdmaMin(long subId) {
1442 enforceModifyPermissionOrCarrierPrivilege();
1443 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1444 return getPhone(subId).getCdmaMin();
1445 } else {
1446 return null;
1447 }
1448 }
1449
1450 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001451 * Returns true if CDMA provisioning needs to run.
1452 */
1453 public boolean needsOtaServiceProvisioning() {
1454 return mPhone.needsOtaServiceProvisioning();
1455 }
1456
1457 /**
1458 * Returns the unread count of voicemails
1459 */
1460 public int getVoiceMessageCount() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001461 return getVoiceMessageCountForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001462 }
1463
1464 /**
1465 * Returns the unread count of voicemails for a subId
1466 */
Wink Savilleadd7cc52014-09-08 14:23:09 -07001467 public int getVoiceMessageCountForSubscriber( long subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001468 return getPhone(subId).getVoiceMessageCount();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001469 }
1470
1471 /**
1472 * Returns the data network type
1473 *
1474 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
1475 */
1476 @Override
1477 public int getNetworkType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001478 return getNetworkTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001479 }
1480
1481 /**
1482 * Returns the network type for a subId
1483 */
1484 @Override
Wink Savilleadd7cc52014-09-08 14:23:09 -07001485 public int getNetworkTypeForSubscriber(long subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001486 return getPhone(subId).getServiceState().getDataNetworkType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001487 }
1488
1489 /**
1490 * Returns the data network type
1491 */
1492 @Override
1493 public int getDataNetworkType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001494 return getDataNetworkTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001495 }
1496
1497 /**
1498 * Returns the data network type for a subId
1499 */
1500 @Override
Wink Savilleadd7cc52014-09-08 14:23:09 -07001501 public int getDataNetworkTypeForSubscriber(long subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001502 return getPhone(subId).getServiceState().getDataNetworkType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001503 }
1504
1505 /**
1506 * Returns the data network type
1507 */
1508 @Override
1509 public int getVoiceNetworkType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001510 return getVoiceNetworkTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001511 }
1512
1513 /**
1514 * Returns the Voice network type for a subId
1515 */
1516 @Override
Wink Savilleadd7cc52014-09-08 14:23:09 -07001517 public int getVoiceNetworkTypeForSubscriber(long subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001518 return getPhone(subId).getServiceState().getVoiceNetworkType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001519 }
1520
1521 /**
1522 * @return true if a ICC card is present
1523 */
1524 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07001525 // FIXME Make changes to pass defaultSimId of type int
1526 return hasIccCardUsingSlotId(getDefaultSubscription());
1527 }
1528
1529 /**
1530 * @return true if a ICC card is present for a slotId
1531 */
1532 public boolean hasIccCardUsingSlotId(long slotId) {
1533 return getPhone(slotId).getIccCard().hasIccCard();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001534 }
1535
1536 /**
1537 * Return if the current radio is LTE on CDMA. This
1538 * is a tri-state return value as for a period of time
1539 * the mode may be unknown.
1540 *
1541 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08001542 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001543 */
1544 public int getLteOnCdmaMode() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001545 return getLteOnCdmaModeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001546 }
1547
Wink Savilleadd7cc52014-09-08 14:23:09 -07001548 public int getLteOnCdmaModeForSubscriber(long subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001549 return getPhone(subId).getLteOnCdmaMode();
1550 }
1551
1552 public void setPhone(Phone phone) {
1553 mPhone = phone;
1554 }
1555
1556 /**
1557 * {@hide}
1558 * Returns Default subId, 0 in the case of single standby.
1559 */
1560 private long getDefaultSubscription() {
1561 return SubscriptionManager.getDefaultSubId();
1562 }
1563
1564 private long getPreferredVoiceSubscription() {
1565 return SubscriptionManager.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001566 }
Ihab Awadf2177b72013-11-25 13:33:23 -08001567
1568 /**
1569 * @see android.telephony.TelephonyManager.WifiCallingChoices
1570 */
1571 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08001572 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
1573 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08001574 }
1575
1576 /**
1577 * @see android.telephony.TelephonyManager.WifiCallingChoices
1578 */
1579 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08001580 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
1581 Settings.System.putInt(mPhone.getContext().getContentResolver(),
1582 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08001583 }
1584
Sailesh Nepald1e68152013-12-12 19:08:02 -08001585 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07001586 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08001587 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08001588 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08001589
Shishir Agrawal566b7612013-10-28 14:41:00 -07001590 @Override
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001591 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(String AID) {
Junda Liua2e36012014-07-09 18:30:01 -07001592 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001593
1594 if (DBG) log("iccOpenLogicalChannel: " + AID);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001595 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
1596 CMD_OPEN_CHANNEL, AID);
1597 if (DBG) log("iccOpenLogicalChannel: " + response);
1598 return response;
Shishir Agrawal566b7612013-10-28 14:41:00 -07001599 }
1600
1601 @Override
1602 public boolean iccCloseLogicalChannel(int channel) {
Junda Liua2e36012014-07-09 18:30:01 -07001603 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001604
1605 if (DBG) log("iccCloseLogicalChannel: " + channel);
1606 if (channel < 0) {
1607 return false;
1608 }
Jake Hambye994d462014-02-03 13:10:13 -08001609 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07001610 if (DBG) log("iccCloseLogicalChannel: " + success);
1611 return success;
1612 }
1613
1614 @Override
1615 public String iccTransmitApduLogicalChannel(int channel, int cla,
1616 int command, int p1, int p2, int p3, String data) {
Junda Liua2e36012014-07-09 18:30:01 -07001617 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001618
1619 if (DBG) {
1620 log("iccTransmitApduLogicalChannel: chnl=" + channel + " cla=" + cla +
1621 " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
1622 " data=" + data);
1623 }
1624
1625 if (channel < 0) {
1626 return "";
1627 }
1628
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001629 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Shishir Agrawal566b7612013-10-28 14:41:00 -07001630 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data));
1631 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
1632
Shishir Agrawal566b7612013-10-28 14:41:00 -07001633 // Append the returned status code to the end of the response payload.
1634 String s = Integer.toHexString(
1635 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07001636 if (response.payload != null) {
1637 s = IccUtils.bytesToHexString(response.payload) + s;
1638 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07001639 return s;
1640 }
Jake Hambye994d462014-02-03 13:10:13 -08001641
Evan Charltonc66da362014-05-16 14:06:40 -07001642 @Override
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001643 public String iccTransmitApduBasicChannel(int cla, int command, int p1, int p2,
1644 int p3, String data) {
1645 enforceModifyPermissionOrCarrierPrivilege();
1646
1647 if (DBG) {
1648 log("iccTransmitApduBasicChannel: cla=" + cla + " cmd=" + command + " p1="
1649 + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
1650 }
1651
1652 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
1653 new IccAPDUArgument(0, cla, command, p1, p2, p3, data));
1654 if (DBG) log("iccTransmitApduBasicChannel: " + response);
1655
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001656 // Append the returned status code to the end of the response payload.
1657 String s = Integer.toHexString(
1658 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07001659 if (response.payload != null) {
1660 s = IccUtils.bytesToHexString(response.payload) + s;
1661 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001662 return s;
1663 }
1664
1665 @Override
1666 public byte[] iccExchangeSimIO(int fileID, int command, int p1, int p2, int p3,
1667 String filePath) {
1668 enforceModifyPermissionOrCarrierPrivilege();
1669
1670 if (DBG) {
1671 log("Exchange SIM_IO " + fileID + ":" + command + " " +
1672 p1 + " " + p2 + " " + p3 + ":" + filePath);
1673 }
1674
1675 IccIoResult response =
1676 (IccIoResult)sendRequest(CMD_EXCHANGE_SIM_IO,
Yong Jiang3edf3782014-10-03 13:23:28 -05001677 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath));
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001678
1679 if (DBG) {
1680 log("Exchange SIM_IO [R]" + response);
1681 }
1682
1683 byte[] result = null;
1684 int length = 2;
1685 if (response.payload != null) {
1686 length = 2 + response.payload.length;
1687 result = new byte[length];
1688 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
1689 } else {
1690 result = new byte[length];
1691 }
1692
1693 result[length - 1] = (byte) response.sw2;
1694 result[length - 2] = (byte) response.sw1;
1695 return result;
1696 }
1697
1698 @Override
Evan Charltonc66da362014-05-16 14:06:40 -07001699 public String sendEnvelopeWithStatus(String content) {
Junda Liua2e36012014-07-09 18:30:01 -07001700 enforceModifyPermissionOrCarrierPrivilege();
Evan Charltonc66da362014-05-16 14:06:40 -07001701
1702 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content);
1703 if (response.payload == null) {
1704 return "";
1705 }
1706
1707 // Append the returned status code to the end of the response payload.
1708 String s = Integer.toHexString(
1709 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
1710 s = IccUtils.bytesToHexString(response.payload) + s;
1711 return s;
1712 }
1713
Jake Hambye994d462014-02-03 13:10:13 -08001714 /**
1715 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
1716 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
1717 *
1718 * @param itemID the ID of the item to read
1719 * @return the NV item as a String, or null on error.
1720 */
1721 @Override
1722 public String nvReadItem(int itemID) {
Junda Liua2e36012014-07-09 18:30:01 -07001723 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001724 if (DBG) log("nvReadItem: item " + itemID);
1725 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
1726 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
1727 return value;
1728 }
1729
1730 /**
1731 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
1732 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
1733 *
1734 * @param itemID the ID of the item to read
1735 * @param itemValue the value to write, as a String
1736 * @return true on success; false on any failure
1737 */
1738 @Override
1739 public boolean nvWriteItem(int itemID, String itemValue) {
Junda Liua2e36012014-07-09 18:30:01 -07001740 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001741 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
1742 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
1743 new Pair<Integer, String>(itemID, itemValue));
1744 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
1745 return success;
1746 }
1747
1748 /**
1749 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
1750 * Used for device configuration by some CDMA operators.
1751 *
1752 * @param preferredRoamingList byte array containing the new PRL
1753 * @return true on success; false on any failure
1754 */
1755 @Override
1756 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Junda Liua2e36012014-07-09 18:30:01 -07001757 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001758 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
1759 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
1760 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
1761 return success;
1762 }
1763
1764 /**
1765 * Perform the specified type of NV config reset.
1766 * Used for device configuration by some CDMA operators.
1767 *
1768 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
1769 * @return true on success; false on any failure
1770 */
1771 @Override
1772 public boolean nvResetConfig(int resetType) {
Junda Liua2e36012014-07-09 18:30:01 -07001773 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001774 if (DBG) log("nvResetConfig: type " + resetType);
1775 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
1776 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
1777 return success;
1778 }
Jake Hamby7c27be32014-03-03 13:25:59 -08001779
1780 /**
Wink Saville36469e72014-06-11 15:17:00 -07001781 * {@hide}
1782 * Returns Default sim, 0 in the case of single standby.
1783 */
1784 public int getDefaultSim() {
1785 //TODO Need to get it from Telephony Devcontroller
1786 return 0;
1787 }
1788
ram87fca6f2014-07-18 18:58:44 +05301789 public String[] getPcscfAddress(String apnType) {
Wink Saville36469e72014-06-11 15:17:00 -07001790 enforceReadPermission();
ram87fca6f2014-07-18 18:58:44 +05301791 return mPhone.getPcscfAddress(apnType);
Wink Saville36469e72014-06-11 15:17:00 -07001792 }
1793
1794 public void setImsRegistrationState(boolean registered) {
1795 enforceModifyPermission();
1796 mPhone.setImsRegistrationState(registered);
1797 }
1798
1799 /**
Junda Liu84d15a22014-07-02 11:21:04 -07001800 * Get the calculated preferred network type.
1801 * Used for debugging incorrect network type.
1802 *
1803 * @return the preferred network type, defined in RILConstants.java.
1804 */
1805 @Override
1806 public int getCalculatedPreferredNetworkType() {
1807 enforceReadPermission();
1808 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext());
1809 }
1810
1811 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08001812 * Get the preferred network type.
1813 * Used for device configuration by some CDMA operators.
1814 *
1815 * @return the preferred network type, defined in RILConstants.java.
1816 */
1817 @Override
1818 public int getPreferredNetworkType() {
Junda Liua2e36012014-07-09 18:30:01 -07001819 enforceModifyPermissionOrCarrierPrivilege();
Jake Hamby7c27be32014-03-03 13:25:59 -08001820 if (DBG) log("getPreferredNetworkType");
1821 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null);
1822 int networkType = (result != null ? result[0] : -1);
1823 if (DBG) log("getPreferredNetworkType: " + networkType);
1824 return networkType;
1825 }
1826
1827 /**
1828 * Set the preferred network type.
1829 * Used for device configuration by some CDMA operators.
1830 *
1831 * @param networkType the preferred network type, defined in RILConstants.java.
1832 * @return true on success; false on any failure.
1833 */
1834 @Override
1835 public boolean setPreferredNetworkType(int networkType) {
Junda Liua2e36012014-07-09 18:30:01 -07001836 enforceModifyPermissionOrCarrierPrivilege();
Jake Hamby7c27be32014-03-03 13:25:59 -08001837 if (DBG) log("setPreferredNetworkType: type " + networkType);
1838 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType);
1839 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
Junda Liu80bc0d12014-07-14 16:36:44 -07001840 if (success) {
1841 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
1842 Settings.Global.PREFERRED_NETWORK_MODE, networkType);
1843 }
Jake Hamby7c27be32014-03-03 13:25:59 -08001844 return success;
1845 }
Robert Greenwalted86e582014-05-21 20:03:20 -07001846
1847 /**
1848 * Set mobile data enabled
1849 * Used by the user through settings etc to turn on/off mobile data
1850 *
1851 * @param enable {@code true} turn turn data on, else {@code false}
1852 */
1853 @Override
1854 public void setDataEnabled(boolean enable) {
1855 enforceModifyPermission();
1856 mPhone.setDataEnabled(enable);
1857 }
1858
1859 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07001860 * Get whether mobile data is enabled.
1861 *
1862 * Note that this used to be available from ConnectivityService, gated by
1863 * ACCESS_NETWORK_STATE permission, so this will accept either that or
1864 * our MODIFY_PHONE_STATE.
Robert Greenwalted86e582014-05-21 20:03:20 -07001865 *
1866 * @return {@code true} if data is enabled else {@code false}
1867 */
1868 @Override
1869 public boolean getDataEnabled() {
Robert Greenwalt646120a2014-05-23 11:54:03 -07001870 try {
1871 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
1872 null);
1873 } catch (Exception e) {
1874 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE,
1875 null);
1876 }
Robert Greenwalted86e582014-05-21 20:03:20 -07001877 return mPhone.getDataEnabled();
1878 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07001879
1880 @Override
1881 public int hasCarrierPrivileges() {
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07001882 UiccCard card = UiccController.getInstance().getUiccCard();
1883 if (card == null) {
1884 loge("hasCarrierPrivileges: No UICC");
1885 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
1886 }
1887 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001888 mPhone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07001889 }
Junda Liu29340342014-07-10 15:23:27 -07001890
1891 @Override
Shishir Agrawal6d5a2852014-07-11 16:32:57 -07001892 public int checkCarrierPrivilegesForPackage(String pkgname) {
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07001893 UiccCard card = UiccController.getInstance().getUiccCard();
1894 if (card == null) {
1895 loge("checkCarrierPrivilegesForPackage: No UICC");
1896 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
1897 }
1898 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgname);
Junda Liu29340342014-07-10 15:23:27 -07001899 }
Derek Tan89e89d42014-07-08 17:00:10 -07001900
1901 @Override
Diego Pontorieroaf74c862014-08-28 11:51:16 -07001902 public List<String> getCarrierPackageNamesForIntent(Intent intent) {
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07001903 UiccCard card = UiccController.getInstance().getUiccCard();
1904 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07001905 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07001906 return null ;
1907 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07001908 return card.getCarrierPackageNamesForIntent(
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07001909 mPhone.getContext().getPackageManager(), intent);
1910 }
1911
Derek Tan97ebb422014-09-05 16:55:38 -07001912 private String getIccId(long subId) {
1913 UiccCard card = getPhone(subId).getUiccCard();
1914 if (card == null) {
1915 loge("getIccId: No UICC");
1916 return null;
1917 }
1918 String iccId = card.getIccId();
1919 if (TextUtils.isEmpty(iccId)) {
1920 loge("getIccId: ICC ID is null or empty.");
1921 return null;
1922 }
1923 return iccId;
1924 }
1925
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07001926 @Override
Junda Liudf6b6b92014-09-05 18:13:46 -07001927 public void enableSimplifiedNetworkSettingsForSubscriber(long subId, boolean enable) {
Derek Tan352d8cd2014-07-12 12:57:11 -07001928 enforceModifyPermissionOrCarrierPrivilege();
Derek Tan97ebb422014-09-05 16:55:38 -07001929
1930 String iccId = getIccId(subId);
1931 if (iccId != null) {
1932 String snsPrefKey = PREF_CARRIERS_SIMPLIFIED_NETWORK_SETTINGS_PREFIX + iccId;
1933 SharedPreferences.Editor editor = carrierPrivilegeConfigs.edit();
1934 if (enable) {
1935 editor.putBoolean(snsPrefKey, true);
1936 } else {
1937 editor.remove(snsPrefKey);
1938 }
1939 editor.commit();
Derek Tan89e89d42014-07-08 17:00:10 -07001940 }
1941 }
1942
1943 @Override
Junda Liudf6b6b92014-09-05 18:13:46 -07001944 public boolean getSimplifiedNetworkSettingsEnabledForSubscriber(long subId) {
Derek Tan89e89d42014-07-08 17:00:10 -07001945 enforceReadPermission();
Derek Tan97ebb422014-09-05 16:55:38 -07001946 String iccId = getIccId(subId);
1947 if (iccId != null) {
1948 String snsPrefKey = PREF_CARRIERS_SIMPLIFIED_NETWORK_SETTINGS_PREFIX + iccId;
1949 return carrierPrivilegeConfigs.getBoolean(snsPrefKey, false);
1950 }
1951 return false;
Derek Tan89e89d42014-07-08 17:00:10 -07001952 }
Derek Tan7226c842014-07-02 17:42:23 -07001953
1954 @Override
Junda Liudf6b6b92014-09-05 18:13:46 -07001955 public void setLine1NumberForDisplayForSubscriber(long subId, String alphaTag, String number) {
Derek Tan352d8cd2014-07-12 12:57:11 -07001956 enforceModifyPermissionOrCarrierPrivilege();
Derek Tan97ebb422014-09-05 16:55:38 -07001957
1958 String iccId = getIccId(subId);
1959 if (iccId != null) {
1960 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
1961 SharedPreferences.Editor editor = carrierPrivilegeConfigs.edit();
1962 if (alphaTag == null) {
1963 editor.remove(alphaTagPrefKey);
1964 } else {
1965 editor.putString(alphaTagPrefKey, alphaTag);
1966 }
1967
1968 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
1969 if (number == null) {
1970 editor.remove(numberPrefKey);
1971 } else {
1972 editor.putString(numberPrefKey, number);
1973 }
1974 editor.commit();
1975 }
Derek Tan7226c842014-07-02 17:42:23 -07001976 }
1977
1978 @Override
1979 public String getLine1NumberForDisplay(long subId) {
1980 enforceReadPermission();
Derek Tan97ebb422014-09-05 16:55:38 -07001981
1982 String iccId = getIccId(subId);
1983 if (iccId != null) {
1984 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
1985 return carrierPrivilegeConfigs.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07001986 }
Derek Tan97ebb422014-09-05 16:55:38 -07001987 return null;
Derek Tan7226c842014-07-02 17:42:23 -07001988 }
1989
1990 @Override
1991 public String getLine1AlphaTagForDisplay(long subId) {
1992 enforceReadPermission();
Derek Tan97ebb422014-09-05 16:55:38 -07001993
1994 String iccId = getIccId(subId);
1995 if (iccId != null) {
1996 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
1997 return carrierPrivilegeConfigs.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07001998 }
Derek Tan97ebb422014-09-05 16:55:38 -07001999 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002000 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002001
2002 @Override
Shishir Agrawala3dfd752014-09-04 13:25:42 -07002003 public boolean setOperatorBrandOverride(String brand) {
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002004 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawala3dfd752014-09-04 13:25:42 -07002005 return mPhone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002006 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05002007
2008 @Override
2009 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
2010 enforceModifyPermission();
2011
2012 int returnValue = 0;
2013 try {
2014 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
2015 if(result.exception == null) {
2016 if (result.result != null) {
2017 byte[] responseData = (byte[])(result.result);
2018 if(responseData.length > oemResp.length) {
2019 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
2020 responseData.length + "bytes. Buffer Size is " +
2021 oemResp.length + "bytes.");
2022 }
2023 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
2024 returnValue = responseData.length;
2025 }
2026 } else {
2027 CommandException ex = (CommandException) result.exception;
2028 returnValue = ex.getCommandError().ordinal();
2029 if(returnValue > 0) returnValue *= -1;
2030 }
2031 } catch (RuntimeException e) {
2032 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
2033 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
2034 if(returnValue > 0) returnValue *= -1;
2035 }
2036
2037 return returnValue;
2038 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002039}