blob: d83f10d109c945166c0e6ade08be2c777a2c2e03 [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.PackageManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070026import android.net.Uri;
27import android.os.AsyncResult;
28import android.os.Binder;
29import android.os.Bundle;
30import android.os.Handler;
31import android.os.Looper;
32import android.os.Message;
33import android.os.Process;
34import android.os.ServiceManager;
35import android.os.UserHandle;
Derek Tan97ebb422014-09-05 16:55:38 -070036import android.preference.PreferenceManager;
Ihab Awadf2177b72013-11-25 13:33:23 -080037import android.provider.Settings;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070038import android.telephony.CellInfo;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -070039import android.telephony.IccOpenLogicalChannelResponse;
Jake Hambye994d462014-02-03 13:10:13 -080040import android.telephony.NeighboringCellInfo;
Wink Saville5d475dd2014-10-17 15:00:58 -070041import android.telephony.RadioAccessFamily;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070042import android.telephony.ServiceState;
Wink Saville0f3b5fc2014-11-11 08:40:49 -080043import android.telephony.SubscriptionInfo;
Ihab Awadf2177b72013-11-25 13:33:23 -080044import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070045import android.text.TextUtils;
46import android.util.Log;
Jake Hambye994d462014-02-03 13:10:13 -080047import android.util.Pair;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070048
Andrew Lee312e8172014-10-23 17:01:36 -070049import com.android.ims.ImsManager;
Shishir Agrawal566b7612013-10-28 14:41:00 -070050import com.android.internal.telephony.CallManager;
51import com.android.internal.telephony.CommandException;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070052import com.android.internal.telephony.DefaultPhoneNotifier;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070053import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -080054import com.android.internal.telephony.IccCard;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070055import com.android.internal.telephony.Phone;
Wink Saville36469e72014-06-11 15:17:00 -070056import com.android.internal.telephony.PhoneFactory;
Wink Saville5d475dd2014-10-17 15:00:58 -070057import com.android.internal.telephony.ProxyController;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070058import com.android.internal.telephony.PhoneConstants;
Wink Savilleac1bdfd2014-11-20 23:04:44 -080059import com.android.internal.telephony.SubscriptionController;
Shishir Agrawal566b7612013-10-28 14:41:00 -070060import com.android.internal.telephony.uicc.IccIoResult;
61import com.android.internal.telephony.uicc.IccUtils;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -070062import com.android.internal.telephony.uicc.UiccCard;
Shishir Agrawal566b7612013-10-28 14:41:00 -070063import com.android.internal.telephony.uicc.UiccController;
Jake Hambye994d462014-02-03 13:10:13 -080064import com.android.internal.util.HexDump;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070065
Wink Saville36469e72014-06-11 15:17:00 -070066import static com.android.internal.telephony.PhoneConstants.SUBSCRIPTION_KEY;
67
Santos Cordon7d4ddf62013-07-10 11:58:08 -070068import java.util.ArrayList;
Jake Hambye994d462014-02-03 13:10:13 -080069import java.util.List;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070070
71/**
72 * Implementation of the ITelephony interface.
73 */
Santos Cordon117fee72014-05-16 17:56:12 -070074public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -070075 private static final String LOG_TAG = "PhoneInterfaceManager";
76 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
77 private static final boolean DBG_LOC = false;
78
79 // Message codes used with mMainThreadHandler
80 private static final int CMD_HANDLE_PIN_MMI = 1;
81 private static final int CMD_HANDLE_NEIGHBORING_CELL = 2;
82 private static final int EVENT_NEIGHBORING_CELL_DONE = 3;
83 private static final int CMD_ANSWER_RINGING_CALL = 4;
84 private static final int CMD_END_CALL = 5; // not used yet
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -070085 private static final int CMD_TRANSMIT_APDU_LOGICAL_CHANNEL = 7;
86 private static final int EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE = 8;
Shishir Agrawal566b7612013-10-28 14:41:00 -070087 private static final int CMD_OPEN_CHANNEL = 9;
88 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
89 private static final int CMD_CLOSE_CHANNEL = 11;
90 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -080091 private static final int CMD_NV_READ_ITEM = 13;
92 private static final int EVENT_NV_READ_ITEM_DONE = 14;
93 private static final int CMD_NV_WRITE_ITEM = 15;
94 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
95 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
96 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
97 private static final int CMD_NV_RESET_CONFIG = 19;
98 private static final int EVENT_NV_RESET_CONFIG_DONE = 20;
Jake Hamby7c27be32014-03-03 13:25:59 -080099 private static final int CMD_GET_PREFERRED_NETWORK_TYPE = 21;
100 private static final int EVENT_GET_PREFERRED_NETWORK_TYPE_DONE = 22;
101 private static final int CMD_SET_PREFERRED_NETWORK_TYPE = 23;
102 private static final int EVENT_SET_PREFERRED_NETWORK_TYPE_DONE = 24;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800103 private static final int CMD_SEND_ENVELOPE = 25;
104 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Derek Tan6b088ee2014-09-05 14:15:18 -0700105 private static final int CMD_INVOKE_OEM_RIL_REQUEST_RAW = 27;
106 private static final int EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE = 28;
107 private static final int CMD_TRANSMIT_APDU_BASIC_CHANNEL = 29;
108 private static final int EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE = 30;
109 private static final int CMD_EXCHANGE_SIM_IO = 31;
110 private static final int EVENT_EXCHANGE_SIM_IO_DONE = 32;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800111 private static final int CMD_SET_VOICEMAIL_NUMBER = 33;
112 private static final int EVENT_SET_VOICEMAIL_NUMBER_DONE = 34;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700113
114 /** The singleton instance. */
115 private static PhoneInterfaceManager sInstance;
116
Wink Saville3ab207e2014-11-20 13:07:20 -0800117 private PhoneGlobals mApp;
118 private Phone mPhone;
119 private CallManager mCM;
120 private AppOpsManager mAppOps;
121 private MainThreadHandler mMainThreadHandler;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800122 private SubscriptionController mSubscriptionController;
Wink Saville3ab207e2014-11-20 13:07:20 -0800123 private SharedPreferences mTelephonySharedPreferences;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700124
Derek Tan97ebb422014-09-05 16:55:38 -0700125 private static final String PREF_CARRIERS_ALPHATAG_PREFIX = "carrier_alphtag_";
126 private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
Andrew Leedf14ead2014-10-17 14:22:52 -0700127 private static final String PREF_ENABLE_VIDEO_CALLING = "enable_video_calling";
Derek Tan89e89d42014-07-08 17:00:10 -0700128
129 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700130 * A request object to use for transmitting data to an ICC.
131 */
132 private static final class IccAPDUArgument {
133 public int channel, cla, command, p1, p2, p3;
134 public String data;
135
136 public IccAPDUArgument(int channel, int cla, int command,
137 int p1, int p2, int p3, String data) {
138 this.channel = channel;
139 this.cla = cla;
140 this.command = command;
141 this.p1 = p1;
142 this.p2 = p2;
143 this.p3 = p3;
144 this.data = data;
145 }
146 }
147
148 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700149 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
150 * request after sending. The main thread will notify the request when it is complete.
151 */
152 private static final class MainThreadRequest {
153 /** The argument to use for the request */
154 public Object argument;
155 /** The result of the request that is run on the main thread */
156 public Object result;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800157 /** The subscriber id that this request applies to. Null if default. */
158 public Integer subId;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700159
160 public MainThreadRequest(Object argument) {
161 this.argument = argument;
162 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800163
164 public MainThreadRequest(Object argument, Integer subId) {
165 this.argument = argument;
166 this.subId = subId;
167 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700168 }
169
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800170 private static final class IncomingThirdPartyCallArgs {
171 public final ComponentName component;
172 public final String callId;
173 public final String callerDisplayName;
174
175 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
176 String callerDisplayName) {
177 this.component = component;
178 this.callId = callId;
179 this.callerDisplayName = callerDisplayName;
180 }
181 }
182
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700183 /**
184 * A handler that processes messages on the main thread in the phone process. Since many
185 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
186 * inbound binder threads to the main thread in the phone process. The Binder thread
187 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
188 * on, which will be notified when the operation completes and will contain the result of the
189 * request.
190 *
191 * <p>If a MainThreadRequest object is provided in the msg.obj field,
192 * note that request.result must be set to something non-null for the calling thread to
193 * unblock.
194 */
195 private final class MainThreadHandler extends Handler {
196 @Override
197 public void handleMessage(Message msg) {
198 MainThreadRequest request;
199 Message onCompleted;
200 AsyncResult ar;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700201 UiccCard uiccCard = UiccController.getInstance().getUiccCard();
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700202 IccAPDUArgument iccArgument;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700203
204 switch (msg.what) {
205 case CMD_HANDLE_PIN_MMI:
206 request = (MainThreadRequest) msg.obj;
Jake Hambye994d462014-02-03 13:10:13 -0800207 request.result = mPhone.handlePinMmi((String) request.argument);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700208 // Wake up the requesting thread
209 synchronized (request) {
210 request.notifyAll();
211 }
212 break;
213
214 case CMD_HANDLE_NEIGHBORING_CELL:
215 request = (MainThreadRequest) msg.obj;
216 onCompleted = obtainMessage(EVENT_NEIGHBORING_CELL_DONE,
217 request);
218 mPhone.getNeighboringCids(onCompleted);
219 break;
220
221 case EVENT_NEIGHBORING_CELL_DONE:
222 ar = (AsyncResult) msg.obj;
223 request = (MainThreadRequest) ar.userObj;
224 if (ar.exception == null && ar.result != null) {
225 request.result = ar.result;
226 } else {
227 // create an empty list to notify the waiting thread
Jake Hambye994d462014-02-03 13:10:13 -0800228 request.result = new ArrayList<NeighboringCellInfo>(0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700229 }
230 // Wake up the requesting thread
231 synchronized (request) {
232 request.notifyAll();
233 }
234 break;
235
236 case CMD_ANSWER_RINGING_CALL:
Wink Saville08874612014-08-31 19:19:58 -0700237 request = (MainThreadRequest) msg.obj;
Wink Savilleb564aae2014-10-23 10:18:09 -0700238 int answer_subId = ((Integer)request.argument).intValue();
Wink Saville08874612014-08-31 19:19:58 -0700239 answerRingingCallInternal(answer_subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700240 break;
241
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700242 case CMD_END_CALL:
243 request = (MainThreadRequest) msg.obj;
Wink Savilleb564aae2014-10-23 10:18:09 -0700244 int end_subId = ((Integer)request.argument).intValue();
Wink Saville08874612014-08-31 19:19:58 -0700245 final boolean hungUp;
246 int phoneType = getPhone(end_subId).getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700247 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
Wink Saville08874612014-08-31 19:19:58 -0700250 hungUp = PhoneUtils.hangupRingingAndActive(getPhone(end_subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700251 } 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
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800594 case CMD_SET_VOICEMAIL_NUMBER:
595 request = (MainThreadRequest) msg.obj;
596 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
597 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
598 Phone phone = (request.subId == null) ? mPhone : getPhone(request.subId);
599 phone.setVoiceMailNumber(tagNum.first, tagNum.second, onCompleted);
600 break;
601
602 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
603 handleNullReturnEvent(msg, "setVoicemailNumber");
604 break;
605
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700606 default:
607 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
608 break;
609 }
610 }
Jake Hambye994d462014-02-03 13:10:13 -0800611
612 private void handleNullReturnEvent(Message msg, String command) {
613 AsyncResult ar = (AsyncResult) msg.obj;
614 MainThreadRequest request = (MainThreadRequest) ar.userObj;
615 if (ar.exception == null) {
616 request.result = true;
617 } else {
618 request.result = false;
619 if (ar.exception instanceof CommandException) {
620 loge(command + ": CommandException: " + ar.exception);
621 } else {
622 loge(command + ": Unknown exception");
623 }
624 }
625 synchronized (request) {
626 request.notifyAll();
627 }
628 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700629 }
630
631 /**
632 * Posts the specified command to be executed on the main thread,
633 * waits for the request to complete, and returns the result.
634 * @see #sendRequestAsync
635 */
636 private Object sendRequest(int command, Object argument) {
Santos Cordon500b0e02014-06-17 10:33:33 -0700637 return sendRequest(command, argument, null);
Wink Saville36469e72014-06-11 15:17:00 -0700638 }
639
640 /**
641 * Posts the specified command to be executed on the main thread,
642 * waits for the request to complete, and returns the result.
643 * @see #sendRequestAsync
644 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800645 private Object sendRequest(int command, Object argument, Integer subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700646 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
647 throw new RuntimeException("This method will deadlock if called from the main thread.");
648 }
649
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800650 MainThreadRequest request = new MainThreadRequest(argument, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700651 Message msg = mMainThreadHandler.obtainMessage(command, request);
652 msg.sendToTarget();
653
654 // Wait for the request to complete
655 synchronized (request) {
656 while (request.result == null) {
657 try {
658 request.wait();
659 } catch (InterruptedException e) {
660 // Do nothing, go back and wait until the request is complete
661 }
662 }
663 }
664 return request.result;
665 }
666
667 /**
668 * Asynchronous ("fire and forget") version of sendRequest():
669 * Posts the specified command to be executed on the main thread, and
670 * returns immediately.
671 * @see #sendRequest
672 */
673 private void sendRequestAsync(int command) {
674 mMainThreadHandler.sendEmptyMessage(command);
675 }
676
677 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -0700678 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
679 * @see {@link #sendRequest(int,Object)}
680 */
681 private void sendRequestAsync(int command, Object argument) {
682 MainThreadRequest request = new MainThreadRequest(argument);
683 Message msg = mMainThreadHandler.obtainMessage(command, request);
684 msg.sendToTarget();
685 }
686
687 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700688 * Initialize the singleton PhoneInterfaceManager instance.
689 * This is only done once, at startup, from PhoneApp.onCreate().
690 */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700691 /* package */ static PhoneInterfaceManager init(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700692 synchronized (PhoneInterfaceManager.class) {
693 if (sInstance == null) {
Sailesh Nepal194161e2014-07-03 08:57:44 -0700694 sInstance = new PhoneInterfaceManager(app, phone);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700695 } else {
696 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
697 }
698 return sInstance;
699 }
700 }
701
702 /** Private constructor; @see init() */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700703 private PhoneInterfaceManager(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700704 mApp = app;
705 mPhone = phone;
706 mCM = PhoneGlobals.getInstance().mCM;
707 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
708 mMainThreadHandler = new MainThreadHandler();
Andrew Leedf14ead2014-10-17 14:22:52 -0700709 mTelephonySharedPreferences =
Derek Tan97ebb422014-09-05 16:55:38 -0700710 PreferenceManager.getDefaultSharedPreferences(mPhone.getContext());
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800711 mSubscriptionController = SubscriptionController.getInstance();
Wink Saville3ab207e2014-11-20 13:07:20 -0800712
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700713 publish();
714 }
715
716 private void publish() {
717 if (DBG) log("publish: " + this);
718
719 ServiceManager.addService("phone", this);
720 }
721
Wink Saville36469e72014-06-11 15:17:00 -0700722 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -0700723 private Phone getPhone(int subId) {
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800724 return PhoneFactory.getPhone(mSubscriptionController.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -0700725 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700726 //
727 // Implementation of the ITelephony interface.
728 //
729
730 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700731 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -0700732 }
733
Wink Savilleb564aae2014-10-23 10:18:09 -0700734 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700735 if (DBG) log("dial: " + number);
736 // No permission check needed here: This is just a wrapper around the
737 // ACTION_DIAL intent, which is available to any app since it puts up
738 // the UI before it does anything.
739
740 String url = createTelUrl(number);
741 if (url == null) {
742 return;
743 }
744
745 // PENDING: should we just silently fail if phone is offhook or ringing?
Wink Saville36469e72014-06-11 15:17:00 -0700746 PhoneConstants.State state = mCM.getState(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700747 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
748 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
749 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
750 mApp.startActivity(intent);
751 }
752 }
753
754 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700755 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -0700756 }
757
Wink Savilleb564aae2014-10-23 10:18:09 -0700758 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700759 if (DBG) log("call: " + number);
760
761 // This is just a wrapper around the ACTION_CALL intent, but we still
762 // need to do a permission check since we're calling startActivity()
763 // from the context of the phone app.
764 enforceCallPermission();
765
766 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
767 != AppOpsManager.MODE_ALLOWED) {
768 return;
769 }
770
771 String url = createTelUrl(number);
772 if (url == null) {
773 return;
774 }
775
Wink Saville08874612014-08-31 19:19:58 -0700776 boolean isValid = false;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800777 List<SubscriptionInfo> slist = mSubscriptionController.getActiveSubscriptionInfoList();
Wink Saville3ab207e2014-11-20 13:07:20 -0800778 if (slist != null) {
779 for (SubscriptionInfo subInfoRecord : slist) {
780 if (subInfoRecord.getSubscriptionId() == subId) {
781 isValid = true;
782 break;
783 }
Wink Saville08874612014-08-31 19:19:58 -0700784 }
785 }
786 if (isValid == false) {
787 return;
788 }
789
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700790 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
Wink Saville36469e72014-06-11 15:17:00 -0700791 intent.putExtra(SUBSCRIPTION_KEY, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700792 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
793 mApp.startActivity(intent);
794 }
795
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700796 /**
797 * End a call based on call state
798 * @return true is a call was ended
799 */
800 public boolean endCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700801 return endCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700802 }
803
804 /**
805 * End a call based on the call state of the subId
806 * @return true is a call was ended
807 */
Wink Savilleb564aae2014-10-23 10:18:09 -0700808 public boolean endCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700809 enforceCallPermission();
Wink Saville9f73bda2014-11-05 08:13:36 -0800810 return (Boolean) sendRequest(CMD_END_CALL, new Integer(subId), null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700811 }
812
813 public void answerRingingCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700814 answerRingingCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700815 }
816
Wink Savilleb564aae2014-10-23 10:18:09 -0700817 public void answerRingingCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700818 if (DBG) log("answerRingingCall...");
819 // TODO: there should eventually be a separate "ANSWER_PHONE" permission,
820 // but that can probably wait till the big TelephonyManager API overhaul.
821 // For now, protect this call with the MODIFY_PHONE_STATE permission.
822 enforceModifyPermission();
Wink Saville9f73bda2014-11-05 08:13:36 -0800823 sendRequest(CMD_ANSWER_RINGING_CALL, new Integer(subId), null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700824 }
825
826 /**
827 * Make the actual telephony calls to implement answerRingingCall().
828 * This should only be called from the main thread of the Phone app.
829 * @see #answerRingingCall
830 *
831 * TODO: it would be nice to return true if we answered the call, or
832 * false if there wasn't actually a ringing incoming call, or some
833 * other error occurred. (In other words, pass back the return value
834 * from PhoneUtils.answerCall() or PhoneUtils.answerAndEndActive().)
835 * But that would require calling this method via sendRequest() rather
836 * than sendRequestAsync(), and right now we don't actually *need* that
837 * return value, so let's just return void for now.
838 */
Wink Savilleb564aae2014-10-23 10:18:09 -0700839 private void answerRingingCallInternal(int subId) {
Wink Saville08874612014-08-31 19:19:58 -0700840 final boolean hasRingingCall = !getPhone(subId).getRingingCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700841 if (hasRingingCall) {
Wink Saville08874612014-08-31 19:19:58 -0700842 final boolean hasActiveCall = !getPhone(subId).getForegroundCall().isIdle();
843 final boolean hasHoldingCall = !getPhone(subId).getBackgroundCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700844 if (hasActiveCall && hasHoldingCall) {
845 // Both lines are in use!
846 // TODO: provide a flag to let the caller specify what
847 // policy to use if both lines are in use. (The current
848 // behavior is hardwired to "answer incoming, end ongoing",
849 // which is how the CALL button is specced to behave.)
850 PhoneUtils.answerAndEndActive(mCM, mCM.getFirstActiveRingingCall());
851 return;
852 } else {
853 // answerCall() will automatically hold the current active
854 // call, if there is one.
855 PhoneUtils.answerCall(mCM.getFirstActiveRingingCall());
856 return;
857 }
858 } else {
859 // No call was ringing.
860 return;
861 }
862 }
863
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700864 /**
Santos Cordon5422a8d2014-09-12 04:20:56 -0700865 * This method is no longer used and can be removed once TelephonyManager stops referring to it.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700866 */
Santos Cordon5422a8d2014-09-12 04:20:56 -0700867 public void silenceRinger() {
868 Log.e(LOG_TAG, "silenseRinger not supported");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700869 }
870
871 public boolean isOffhook() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700872 return isOffhookForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700873 }
874
Wink Savilleb564aae2014-10-23 10:18:09 -0700875 public boolean isOffhookForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -0700876 return (getPhone(subId).getState() == PhoneConstants.State.OFFHOOK);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700877 }
878
879 public boolean isRinging() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700880 return (isRingingForSubscriber(getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -0700881 }
882
Wink Savilleb564aae2014-10-23 10:18:09 -0700883 public boolean isRingingForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -0700884 return (getPhone(subId).getState() == PhoneConstants.State.RINGING);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700885 }
886
887 public boolean isIdle() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700888 return isIdleForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700889 }
890
Wink Savilleb564aae2014-10-23 10:18:09 -0700891 public boolean isIdleForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -0700892 return (getPhone(subId).getState() == PhoneConstants.State.IDLE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700893 }
894
895 public boolean isSimPinEnabled() {
896 enforceReadPermission();
897 return (PhoneGlobals.getInstance().isSimPinEnabled());
898 }
899
900 public boolean supplyPin(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700901 return supplyPinForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -0700902 }
903
Wink Savilleb564aae2014-10-23 10:18:09 -0700904 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700905 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -0700906 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
907 }
908
909 public boolean supplyPuk(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700910 return supplyPukForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -0700911 }
912
Wink Savilleb564aae2014-10-23 10:18:09 -0700913 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700914 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -0700915 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
916 }
917
918 /** {@hide} */
919 public int[] supplyPinReportResult(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700920 return supplyPinReportResultForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -0700921 }
922
Wink Savilleb564aae2014-10-23 10:18:09 -0700923 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700924 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -0700925 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700926 checkSimPin.start();
927 return checkSimPin.unlockSim(null, pin);
928 }
929
Wink Saville9de0f752013-10-22 19:04:03 -0700930 /** {@hide} */
931 public int[] supplyPukReportResult(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700932 return supplyPukReportResultForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -0700933 }
934
Wink Savilleb564aae2014-10-23 10:18:09 -0700935 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700936 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -0700937 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700938 checkSimPuk.start();
939 return checkSimPuk.unlockSim(puk, pin);
940 }
941
942 /**
Wink Saville9de0f752013-10-22 19:04:03 -0700943 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700944 * a synchronous one.
945 */
946 private static class UnlockSim extends Thread {
947
948 private final IccCard mSimCard;
949
950 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -0700951 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
952 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700953
954 // For replies from SimCard interface
955 private Handler mHandler;
956
957 // For async handler to identify request type
958 private static final int SUPPLY_PIN_COMPLETE = 100;
959
960 public UnlockSim(IccCard simCard) {
961 mSimCard = simCard;
962 }
963
964 @Override
965 public void run() {
966 Looper.prepare();
967 synchronized (UnlockSim.this) {
968 mHandler = new Handler() {
969 @Override
970 public void handleMessage(Message msg) {
971 AsyncResult ar = (AsyncResult) msg.obj;
972 switch (msg.what) {
973 case SUPPLY_PIN_COMPLETE:
974 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
975 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -0700976 mRetryCount = msg.arg1;
977 if (ar.exception != null) {
978 if (ar.exception instanceof CommandException &&
979 ((CommandException)(ar.exception)).getCommandError()
980 == CommandException.Error.PASSWORD_INCORRECT) {
981 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
982 } else {
983 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
984 }
985 } else {
986 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
987 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700988 mDone = true;
989 UnlockSim.this.notifyAll();
990 }
991 break;
992 }
993 }
994 };
995 UnlockSim.this.notifyAll();
996 }
997 Looper.loop();
998 }
999
1000 /*
1001 * Use PIN or PUK to unlock SIM card
1002 *
1003 * If PUK is null, unlock SIM card with PIN
1004 *
1005 * If PUK is not null, unlock SIM card with PUK and set PIN code
1006 */
Wink Saville9de0f752013-10-22 19:04:03 -07001007 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001008
1009 while (mHandler == null) {
1010 try {
1011 wait();
1012 } catch (InterruptedException e) {
1013 Thread.currentThread().interrupt();
1014 }
1015 }
1016 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
1017
1018 if (puk == null) {
1019 mSimCard.supplyPin(pin, callback);
1020 } else {
1021 mSimCard.supplyPuk(puk, pin, callback);
1022 }
1023
1024 while (!mDone) {
1025 try {
1026 Log.d(LOG_TAG, "wait for done");
1027 wait();
1028 } catch (InterruptedException e) {
1029 // Restore the interrupted status
1030 Thread.currentThread().interrupt();
1031 }
1032 }
1033 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07001034 int[] resultArray = new int[2];
1035 resultArray[0] = mResult;
1036 resultArray[1] = mRetryCount;
1037 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001038 }
1039 }
1040
1041 public void updateServiceLocation() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001042 updateServiceLocationForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001043
1044 }
1045
Wink Savilleb564aae2014-10-23 10:18:09 -07001046 public void updateServiceLocationForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001047 // No permission check needed here: this call is harmless, and it's
1048 // needed for the ServiceState.requestStateUpdate() call (which is
1049 // already intentionally exposed to 3rd parties.)
Wink Saville36469e72014-06-11 15:17:00 -07001050 getPhone(subId).updateServiceLocation();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001051 }
1052
1053 public boolean isRadioOn() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001054 return isRadioOnForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001055 }
1056
Wink Savilleb564aae2014-10-23 10:18:09 -07001057 public boolean isRadioOnForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001058 return getPhone(subId).getServiceState().getState() != ServiceState.STATE_POWER_OFF;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001059 }
1060
1061 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001062 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001063
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001064 }
Wink Saville36469e72014-06-11 15:17:00 -07001065
Wink Savilleb564aae2014-10-23 10:18:09 -07001066 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001067 enforceModifyPermission();
Wink Savilleadd7cc52014-09-08 14:23:09 -07001068 getPhone(subId).setRadioPower(!isRadioOnForSubscriber(subId));
Wink Saville36469e72014-06-11 15:17:00 -07001069 }
1070
1071 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001072 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001073 }
1074
Wink Savilleb564aae2014-10-23 10:18:09 -07001075 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07001076 enforceModifyPermission();
1077 if ((getPhone(subId).getServiceState().getState() !=
1078 ServiceState.STATE_POWER_OFF) != turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001079 toggleRadioOnOffForSubscriber(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001080 }
1081 return true;
1082 }
Wink Saville36469e72014-06-11 15:17:00 -07001083
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001084 public boolean needMobileRadioShutdown() {
1085 /*
1086 * If any of the Radios are available, it will need to be
1087 * shutdown. So return true if any Radio is available.
1088 */
1089 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1090 Phone phone = PhoneFactory.getPhone(i);
1091 if (phone != null && phone.isRadioAvailable()) return true;
1092 }
1093 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
1094 return false;
1095 }
1096
1097 public void shutdownMobileRadios() {
1098 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1099 logv("Shutting down Phone " + i);
1100 shutdownRadioUsingPhoneId(i);
1101 }
1102 }
1103
1104 private void shutdownRadioUsingPhoneId(int phoneId) {
1105 enforceModifyPermission();
1106 Phone phone = PhoneFactory.getPhone(phoneId);
1107 if (phone != null && phone.isRadioAvailable()) {
1108 phone.shutdownRadio();
1109 }
1110 }
1111
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001112 public boolean setRadioPower(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001113 return setRadioPowerForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001114 }
1115
Wink Savilleb564aae2014-10-23 10:18:09 -07001116 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001117 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001118 getPhone(subId).setRadioPower(turnOn);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001119 return true;
1120 }
1121
Wink Saville36469e72014-06-11 15:17:00 -07001122 // FIXME: subId version needed
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001123 public boolean enableDataConnectivity() {
1124 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001125 int subId = mSubscriptionController.getDefaultDataSubId();
Wink Saville36469e72014-06-11 15:17:00 -07001126 getPhone(subId).setDataEnabled(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001127 return true;
1128 }
1129
Wink Saville36469e72014-06-11 15:17:00 -07001130 // FIXME: subId version needed
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001131 public boolean disableDataConnectivity() {
1132 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001133 int subId = mSubscriptionController.getDefaultDataSubId();
Wink Saville36469e72014-06-11 15:17:00 -07001134 getPhone(subId).setDataEnabled(false);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001135 return true;
1136 }
1137
Wink Saville36469e72014-06-11 15:17:00 -07001138 // FIXME: subId version needed
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001139 public boolean isDataConnectivityPossible() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001140 int subId = mSubscriptionController.getDefaultDataSubId();
Wink Saville36469e72014-06-11 15:17:00 -07001141 return getPhone(subId).isDataConnectivityPossible();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001142 }
1143
1144 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001145 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07001146 }
1147
Wink Savilleb564aae2014-10-23 10:18:09 -07001148 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001149 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001150 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001151 }
1152
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001153 public int getCallState() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001154 return getCallStateForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001155 }
1156
Wink Savilleb564aae2014-10-23 10:18:09 -07001157 public int getCallStateForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001158 return DefaultPhoneNotifier.convertCallState(getPhone(subId).getState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001159 }
1160
1161 public int getDataState() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001162 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Wink Saville36469e72014-06-11 15:17:00 -07001163 return DefaultPhoneNotifier.convertDataState(phone.getDataConnectionState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001164 }
1165
1166 public int getDataActivity() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001167 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Wink Saville36469e72014-06-11 15:17:00 -07001168 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001169 }
1170
1171 @Override
1172 public Bundle getCellLocation() {
1173 try {
1174 mApp.enforceCallingOrSelfPermission(
1175 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1176 } catch (SecurityException e) {
1177 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1178 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1179 // is the weaker precondition
1180 mApp.enforceCallingOrSelfPermission(
1181 android.Manifest.permission.ACCESS_COARSE_LOCATION, null);
1182 }
1183
Jake Hambye994d462014-02-03 13:10:13 -08001184 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001185 if (DBG_LOC) log("getCellLocation: is active user");
1186 Bundle data = new Bundle();
1187 mPhone.getCellLocation().fillInNotifierBundle(data);
1188 return data;
1189 } else {
1190 if (DBG_LOC) log("getCellLocation: suppress non-active user");
1191 return null;
1192 }
1193 }
1194
1195 @Override
1196 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001197 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001198 }
1199
Wink Savilleb564aae2014-10-23 10:18:09 -07001200 public void enableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001201 mApp.enforceCallingOrSelfPermission(
1202 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Wink Saville36469e72014-06-11 15:17:00 -07001203 getPhone(subId).enableLocationUpdates();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001204 }
1205
1206 @Override
1207 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001208 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001209 }
1210
Wink Savilleb564aae2014-10-23 10:18:09 -07001211 public void disableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001212 mApp.enforceCallingOrSelfPermission(
1213 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Wink Saville36469e72014-06-11 15:17:00 -07001214 getPhone(subId).disableLocationUpdates();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001215 }
1216
1217 @Override
1218 @SuppressWarnings("unchecked")
1219 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
1220 try {
1221 mApp.enforceCallingOrSelfPermission(
1222 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1223 } catch (SecurityException e) {
1224 // If we have ACCESS_FINE_LOCATION permission, skip the check
1225 // for ACCESS_COARSE_LOCATION
1226 // A failure should throw the SecurityException from
1227 // ACCESS_COARSE_LOCATION since this is the weaker precondition
1228 mApp.enforceCallingOrSelfPermission(
1229 android.Manifest.permission.ACCESS_COARSE_LOCATION, null);
1230 }
1231
1232 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
1233 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1234 return null;
1235 }
Jake Hambye994d462014-02-03 13:10:13 -08001236 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001237 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
1238
1239 ArrayList<NeighboringCellInfo> cells = null;
1240
1241 try {
1242 cells = (ArrayList<NeighboringCellInfo>) sendRequest(
Wink Saville36469e72014-06-11 15:17:00 -07001243 CMD_HANDLE_NEIGHBORING_CELL, null, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001244 } catch (RuntimeException e) {
Wink Saville36469e72014-06-11 15:17:00 -07001245 Log.e(LOG_TAG, "getNeighboringCellInfo " + e);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001246 }
1247 return cells;
1248 } else {
1249 if (DBG_LOC) log("getNeighboringCellInfo: suppress non-active user");
1250 return null;
1251 }
1252 }
1253
1254
1255 @Override
1256 public List<CellInfo> getAllCellInfo() {
1257 try {
1258 mApp.enforceCallingOrSelfPermission(
1259 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1260 } catch (SecurityException e) {
1261 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1262 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1263 // is the weaker precondition
1264 mApp.enforceCallingOrSelfPermission(
1265 android.Manifest.permission.ACCESS_COARSE_LOCATION, null);
1266 }
1267
Jake Hambye994d462014-02-03 13:10:13 -08001268 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001269 if (DBG_LOC) log("getAllCellInfo: is active user");
1270 return mPhone.getAllCellInfo();
1271 } else {
1272 if (DBG_LOC) log("getAllCellInfo: suppress non-active user");
1273 return null;
1274 }
1275 }
1276
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001277 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001278 public void setCellInfoListRate(int rateInMillis) {
1279 mPhone.setCellInfoListRate(rateInMillis);
1280 }
1281
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001282 //
1283 // Internal helper methods.
1284 //
1285
Jake Hambye994d462014-02-03 13:10:13 -08001286 private static boolean checkIfCallerIsSelfOrForegroundUser() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001287 boolean ok;
1288
1289 boolean self = Binder.getCallingUid() == Process.myUid();
1290 if (!self) {
1291 // Get the caller's user id then clear the calling identity
1292 // which will be restored in the finally clause.
1293 int callingUser = UserHandle.getCallingUserId();
1294 long ident = Binder.clearCallingIdentity();
1295
1296 try {
1297 // With calling identity cleared the current user is the foreground user.
1298 int foregroundUser = ActivityManager.getCurrentUser();
1299 ok = (foregroundUser == callingUser);
1300 if (DBG_LOC) {
1301 log("checkIfCallerIsSelfOrForegoundUser: foregroundUser=" + foregroundUser
1302 + " callingUser=" + callingUser + " ok=" + ok);
1303 }
1304 } catch (Exception ex) {
1305 if (DBG_LOC) loge("checkIfCallerIsSelfOrForegoundUser: Exception ex=" + ex);
1306 ok = false;
1307 } finally {
1308 Binder.restoreCallingIdentity(ident);
1309 }
1310 } else {
1311 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: is self");
1312 ok = true;
1313 }
1314 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: ret=" + ok);
1315 return ok;
1316 }
1317
1318 /**
1319 * Make sure the caller has the READ_PHONE_STATE permission.
1320 *
1321 * @throws SecurityException if the caller does not have the required permission
1322 */
1323 private void enforceReadPermission() {
1324 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE, null);
1325 }
1326
1327 /**
1328 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1329 *
1330 * @throws SecurityException if the caller does not have the required permission
1331 */
1332 private void enforceModifyPermission() {
1333 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1334 }
1335
1336 /**
Junda Liua2e36012014-07-09 18:30:01 -07001337 * Make sure either system app or the caller has carrier privilege.
1338 *
1339 * @throws SecurityException if the caller does not have the required permission/privilege
1340 */
1341 private void enforceModifyPermissionOrCarrierPrivilege() {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001342 int permission = mApp.checkCallingOrSelfPermission(
1343 android.Manifest.permission.MODIFY_PHONE_STATE);
1344 if (permission == PackageManager.PERMISSION_GRANTED) {
1345 return;
1346 }
1347
1348 log("No modify permission, check carrier privilege next.");
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08001349 if (getCarrierPrivilegeStatus() != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001350 loge("No Carrier Privilege.");
1351 throw new SecurityException("No modify permission or carrier privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001352 }
1353 }
1354
1355 /**
1356 * Make sure the caller has carrier privilege.
1357 *
1358 * @throws SecurityException if the caller does not have the required permission
1359 */
1360 private void enforceCarrierPrivilege() {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08001361 if (getCarrierPrivilegeStatus() != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001362 loge("No Carrier Privilege.");
1363 throw new SecurityException("No Carrier Privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001364 }
1365 }
1366
1367 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001368 * Make sure the caller has the CALL_PHONE permission.
1369 *
1370 * @throws SecurityException if the caller does not have the required permission
1371 */
1372 private void enforceCallPermission() {
1373 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1374 }
1375
Shishir Agrawal566b7612013-10-28 14:41:00 -07001376 /**
Gabriel Peal36ebb0d2014-03-20 09:20:43 -07001377 * Make sure the caller has the READ_PRIVILEGED_PHONE_STATE permission.
1378 *
1379 * @throws SecurityException if the caller does not have the required permission
1380 */
1381 private void enforcePrivilegedPhoneStatePermission() {
1382 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
1383 null);
1384 }
1385
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001386 private String createTelUrl(String number) {
1387 if (TextUtils.isEmpty(number)) {
1388 return null;
1389 }
1390
Jake Hambye994d462014-02-03 13:10:13 -08001391 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001392 }
1393
Ihab Awadf9e92732013-12-05 18:02:52 -08001394 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001395 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1396 }
1397
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001398 private static void logv(String msg) {
1399 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
1400 }
1401
Ihab Awadf9e92732013-12-05 18:02:52 -08001402 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001403 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1404 }
1405
1406 public int getActivePhoneType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001407 return getActivePhoneTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001408 }
1409
Wink Savilleb564aae2014-10-23 10:18:09 -07001410 public int getActivePhoneTypeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001411 return getPhone(subId).getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001412 }
1413
1414 /**
1415 * Returns the CDMA ERI icon index to display
1416 */
1417 public int getCdmaEriIconIndex() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001418 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001419
1420 }
1421
Wink Savilleb564aae2014-10-23 10:18:09 -07001422 public int getCdmaEriIconIndexForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001423 return getPhone(subId).getCdmaEriIconIndex();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001424 }
1425
1426 /**
1427 * Returns the CDMA ERI icon mode,
1428 * 0 - ON
1429 * 1 - FLASHING
1430 */
1431 public int getCdmaEriIconMode() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001432 return getCdmaEriIconModeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001433 }
1434
Wink Savilleb564aae2014-10-23 10:18:09 -07001435 public int getCdmaEriIconModeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001436 return getPhone(subId).getCdmaEriIconMode();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001437 }
1438
1439 /**
1440 * Returns the CDMA ERI text,
1441 */
1442 public String getCdmaEriText() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001443 return getCdmaEriTextForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001444 }
1445
Wink Savilleb564aae2014-10-23 10:18:09 -07001446 public String getCdmaEriTextForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001447 return getPhone(subId).getCdmaEriText();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001448 }
1449
1450 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07001451 * Returns the CDMA MDN.
1452 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001453 public String getCdmaMdn(int subId) {
Junda Liuca05d5d2014-08-14 22:36:34 -07001454 enforceModifyPermissionOrCarrierPrivilege();
1455 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1456 return getPhone(subId).getLine1Number();
1457 } else {
1458 return null;
1459 }
1460 }
1461
1462 /**
1463 * Returns the CDMA MIN.
1464 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001465 public String getCdmaMin(int subId) {
Junda Liuca05d5d2014-08-14 22:36:34 -07001466 enforceModifyPermissionOrCarrierPrivilege();
1467 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1468 return getPhone(subId).getCdmaMin();
1469 } else {
1470 return null;
1471 }
1472 }
1473
1474 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001475 * Returns true if CDMA provisioning needs to run.
1476 */
1477 public boolean needsOtaServiceProvisioning() {
1478 return mPhone.needsOtaServiceProvisioning();
1479 }
1480
1481 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001482 * Sets the voice mail number of a given subId.
1483 */
1484 @Override
1485 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08001486 enforceCarrierPrivilege();
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001487 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
1488 new Pair<String, String>(alphaTag, number), new Integer(subId));
1489 return success;
1490 }
1491
1492 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001493 * Returns the unread count of voicemails
1494 */
1495 public int getVoiceMessageCount() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001496 return getVoiceMessageCountForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001497 }
1498
1499 /**
1500 * Returns the unread count of voicemails for a subId
1501 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001502 public int getVoiceMessageCountForSubscriber( int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001503 return getPhone(subId).getVoiceMessageCount();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001504 }
1505
1506 /**
1507 * Returns the data network type
1508 *
1509 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
1510 */
1511 @Override
1512 public int getNetworkType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001513 return getNetworkTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001514 }
1515
1516 /**
1517 * Returns the network type for a subId
1518 */
1519 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001520 public int getNetworkTypeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001521 return getPhone(subId).getServiceState().getDataNetworkType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001522 }
1523
1524 /**
1525 * Returns the data network type
1526 */
1527 @Override
1528 public int getDataNetworkType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001529 return getDataNetworkTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001530 }
1531
1532 /**
1533 * Returns the data network type for a subId
1534 */
1535 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001536 public int getDataNetworkTypeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001537 return getPhone(subId).getServiceState().getDataNetworkType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001538 }
1539
1540 /**
1541 * Returns the data network type
1542 */
1543 @Override
1544 public int getVoiceNetworkType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001545 return getVoiceNetworkTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001546 }
1547
1548 /**
1549 * Returns the Voice network type for a subId
1550 */
1551 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001552 public int getVoiceNetworkTypeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001553 return getPhone(subId).getServiceState().getVoiceNetworkType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001554 }
1555
1556 /**
1557 * @return true if a ICC card is present
1558 */
1559 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07001560 // FIXME Make changes to pass defaultSimId of type int
1561 return hasIccCardUsingSlotId(getDefaultSubscription());
1562 }
1563
1564 /**
1565 * @return true if a ICC card is present for a slotId
1566 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001567 public boolean hasIccCardUsingSlotId(int slotId) {
Wink Saville36469e72014-06-11 15:17:00 -07001568 return getPhone(slotId).getIccCard().hasIccCard();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001569 }
1570
1571 /**
1572 * Return if the current radio is LTE on CDMA. This
1573 * is a tri-state return value as for a period of time
1574 * the mode may be unknown.
1575 *
1576 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08001577 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001578 */
1579 public int getLteOnCdmaMode() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001580 return getLteOnCdmaModeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001581 }
1582
Wink Savilleb564aae2014-10-23 10:18:09 -07001583 public int getLteOnCdmaModeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001584 return getPhone(subId).getLteOnCdmaMode();
1585 }
1586
1587 public void setPhone(Phone phone) {
1588 mPhone = phone;
1589 }
1590
1591 /**
1592 * {@hide}
1593 * Returns Default subId, 0 in the case of single standby.
1594 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001595 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001596 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07001597 }
1598
Wink Savilleb564aae2014-10-23 10:18:09 -07001599 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001600 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001601 }
Ihab Awadf2177b72013-11-25 13:33:23 -08001602
1603 /**
1604 * @see android.telephony.TelephonyManager.WifiCallingChoices
1605 */
1606 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08001607 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
1608 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08001609 }
1610
1611 /**
1612 * @see android.telephony.TelephonyManager.WifiCallingChoices
1613 */
1614 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08001615 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
1616 Settings.System.putInt(mPhone.getContext().getContentResolver(),
1617 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08001618 }
1619
Sailesh Nepald1e68152013-12-12 19:08:02 -08001620 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07001621 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08001622 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08001623 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08001624
Shishir Agrawal566b7612013-10-28 14:41:00 -07001625 @Override
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001626 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(String AID) {
Junda Liua2e36012014-07-09 18:30:01 -07001627 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001628
1629 if (DBG) log("iccOpenLogicalChannel: " + AID);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001630 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
1631 CMD_OPEN_CHANNEL, AID);
1632 if (DBG) log("iccOpenLogicalChannel: " + response);
1633 return response;
Shishir Agrawal566b7612013-10-28 14:41:00 -07001634 }
1635
1636 @Override
1637 public boolean iccCloseLogicalChannel(int channel) {
Junda Liua2e36012014-07-09 18:30:01 -07001638 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001639
1640 if (DBG) log("iccCloseLogicalChannel: " + channel);
1641 if (channel < 0) {
1642 return false;
1643 }
Jake Hambye994d462014-02-03 13:10:13 -08001644 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07001645 if (DBG) log("iccCloseLogicalChannel: " + success);
1646 return success;
1647 }
1648
1649 @Override
1650 public String iccTransmitApduLogicalChannel(int channel, int cla,
1651 int command, int p1, int p2, int p3, String data) {
Junda Liua2e36012014-07-09 18:30:01 -07001652 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001653
1654 if (DBG) {
1655 log("iccTransmitApduLogicalChannel: chnl=" + channel + " cla=" + cla +
1656 " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
1657 " data=" + data);
1658 }
1659
1660 if (channel < 0) {
1661 return "";
1662 }
1663
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001664 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Shishir Agrawal566b7612013-10-28 14:41:00 -07001665 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data));
1666 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
1667
Shishir Agrawal566b7612013-10-28 14:41:00 -07001668 // Append the returned status code to the end of the response payload.
1669 String s = Integer.toHexString(
1670 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07001671 if (response.payload != null) {
1672 s = IccUtils.bytesToHexString(response.payload) + s;
1673 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07001674 return s;
1675 }
Jake Hambye994d462014-02-03 13:10:13 -08001676
Evan Charltonc66da362014-05-16 14:06:40 -07001677 @Override
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001678 public String iccTransmitApduBasicChannel(int cla, int command, int p1, int p2,
1679 int p3, String data) {
1680 enforceModifyPermissionOrCarrierPrivilege();
1681
1682 if (DBG) {
1683 log("iccTransmitApduBasicChannel: cla=" + cla + " cmd=" + command + " p1="
1684 + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
1685 }
1686
1687 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
1688 new IccAPDUArgument(0, cla, command, p1, p2, p3, data));
1689 if (DBG) log("iccTransmitApduBasicChannel: " + response);
1690
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001691 // Append the returned status code to the end of the response payload.
1692 String s = Integer.toHexString(
1693 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07001694 if (response.payload != null) {
1695 s = IccUtils.bytesToHexString(response.payload) + s;
1696 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001697 return s;
1698 }
1699
1700 @Override
1701 public byte[] iccExchangeSimIO(int fileID, int command, int p1, int p2, int p3,
1702 String filePath) {
1703 enforceModifyPermissionOrCarrierPrivilege();
1704
1705 if (DBG) {
1706 log("Exchange SIM_IO " + fileID + ":" + command + " " +
1707 p1 + " " + p2 + " " + p3 + ":" + filePath);
1708 }
1709
1710 IccIoResult response =
1711 (IccIoResult)sendRequest(CMD_EXCHANGE_SIM_IO,
Yong Jiang3edf3782014-10-03 13:23:28 -05001712 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath));
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001713
1714 if (DBG) {
1715 log("Exchange SIM_IO [R]" + response);
1716 }
1717
1718 byte[] result = null;
1719 int length = 2;
1720 if (response.payload != null) {
1721 length = 2 + response.payload.length;
1722 result = new byte[length];
1723 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
1724 } else {
1725 result = new byte[length];
1726 }
1727
1728 result[length - 1] = (byte) response.sw2;
1729 result[length - 2] = (byte) response.sw1;
1730 return result;
1731 }
1732
1733 @Override
Evan Charltonc66da362014-05-16 14:06:40 -07001734 public String sendEnvelopeWithStatus(String content) {
Junda Liua2e36012014-07-09 18:30:01 -07001735 enforceModifyPermissionOrCarrierPrivilege();
Evan Charltonc66da362014-05-16 14:06:40 -07001736
1737 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content);
1738 if (response.payload == null) {
1739 return "";
1740 }
1741
1742 // Append the returned status code to the end of the response payload.
1743 String s = Integer.toHexString(
1744 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
1745 s = IccUtils.bytesToHexString(response.payload) + s;
1746 return s;
1747 }
1748
Jake Hambye994d462014-02-03 13:10:13 -08001749 /**
1750 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
1751 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
1752 *
1753 * @param itemID the ID of the item to read
1754 * @return the NV item as a String, or null on error.
1755 */
1756 @Override
1757 public String nvReadItem(int itemID) {
Junda Liua2e36012014-07-09 18:30:01 -07001758 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001759 if (DBG) log("nvReadItem: item " + itemID);
1760 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
1761 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
1762 return value;
1763 }
1764
1765 /**
1766 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
1767 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
1768 *
1769 * @param itemID the ID of the item to read
1770 * @param itemValue the value to write, as a String
1771 * @return true on success; false on any failure
1772 */
1773 @Override
1774 public boolean nvWriteItem(int itemID, String itemValue) {
Junda Liua2e36012014-07-09 18:30:01 -07001775 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001776 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
1777 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
1778 new Pair<Integer, String>(itemID, itemValue));
1779 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
1780 return success;
1781 }
1782
1783 /**
1784 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
1785 * Used for device configuration by some CDMA operators.
1786 *
1787 * @param preferredRoamingList byte array containing the new PRL
1788 * @return true on success; false on any failure
1789 */
1790 @Override
1791 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Junda Liua2e36012014-07-09 18:30:01 -07001792 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001793 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
1794 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
1795 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
1796 return success;
1797 }
1798
1799 /**
1800 * Perform the specified type of NV config reset.
1801 * Used for device configuration by some CDMA operators.
1802 *
1803 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
1804 * @return true on success; false on any failure
1805 */
1806 @Override
1807 public boolean nvResetConfig(int resetType) {
Junda Liua2e36012014-07-09 18:30:01 -07001808 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001809 if (DBG) log("nvResetConfig: type " + resetType);
1810 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
1811 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
1812 return success;
1813 }
Jake Hamby7c27be32014-03-03 13:25:59 -08001814
1815 /**
Wink Saville36469e72014-06-11 15:17:00 -07001816 * {@hide}
1817 * Returns Default sim, 0 in the case of single standby.
1818 */
1819 public int getDefaultSim() {
1820 //TODO Need to get it from Telephony Devcontroller
1821 return 0;
1822 }
1823
ram87fca6f2014-07-18 18:58:44 +05301824 public String[] getPcscfAddress(String apnType) {
Wink Saville36469e72014-06-11 15:17:00 -07001825 enforceReadPermission();
ram87fca6f2014-07-18 18:58:44 +05301826 return mPhone.getPcscfAddress(apnType);
Wink Saville36469e72014-06-11 15:17:00 -07001827 }
1828
1829 public void setImsRegistrationState(boolean registered) {
1830 enforceModifyPermission();
1831 mPhone.setImsRegistrationState(registered);
1832 }
1833
1834 /**
Junda Liu84d15a22014-07-02 11:21:04 -07001835 * Get the calculated preferred network type.
1836 * Used for debugging incorrect network type.
1837 *
1838 * @return the preferred network type, defined in RILConstants.java.
1839 */
1840 @Override
1841 public int getCalculatedPreferredNetworkType() {
1842 enforceReadPermission();
Amit Mahajan43330e02014-11-18 11:54:45 -08001843 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext(), 0); // wink FIXME: need to get SubId from somewhere.
Junda Liu84d15a22014-07-02 11:21:04 -07001844 }
1845
1846 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08001847 * Get the preferred network type.
1848 * Used for device configuration by some CDMA operators.
1849 *
1850 * @return the preferred network type, defined in RILConstants.java.
1851 */
1852 @Override
1853 public int getPreferredNetworkType() {
Junda Liua2e36012014-07-09 18:30:01 -07001854 enforceModifyPermissionOrCarrierPrivilege();
Jake Hamby7c27be32014-03-03 13:25:59 -08001855 if (DBG) log("getPreferredNetworkType");
1856 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null);
1857 int networkType = (result != null ? result[0] : -1);
1858 if (DBG) log("getPreferredNetworkType: " + networkType);
1859 return networkType;
1860 }
1861
1862 /**
1863 * Set the preferred network type.
1864 * Used for device configuration by some CDMA operators.
1865 *
1866 * @param networkType the preferred network type, defined in RILConstants.java.
1867 * @return true on success; false on any failure.
1868 */
1869 @Override
1870 public boolean setPreferredNetworkType(int networkType) {
Junda Liua2e36012014-07-09 18:30:01 -07001871 enforceModifyPermissionOrCarrierPrivilege();
PauloftheWest3c6ce5e2014-11-03 07:09:47 -08001872 final int phoneSubId = mPhone.getSubId();
Jake Hamby7c27be32014-03-03 13:25:59 -08001873 if (DBG) log("setPreferredNetworkType: type " + networkType);
1874 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType);
1875 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
Junda Liu80bc0d12014-07-14 16:36:44 -07001876 if (success) {
1877 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
PauloftheWest3c6ce5e2014-11-03 07:09:47 -08001878 Settings.Global.PREFERRED_NETWORK_MODE + phoneSubId, networkType);
Junda Liu80bc0d12014-07-14 16:36:44 -07001879 }
Jake Hamby7c27be32014-03-03 13:25:59 -08001880 return success;
1881 }
Robert Greenwalted86e582014-05-21 20:03:20 -07001882
1883 /**
Junda Liu475951f2014-11-07 16:45:03 -08001884 * Check TETHER_DUN_REQUIRED and TETHER_DUN_APN settings, net.tethering.noprovisioning
1885 * SystemProperty, and config_tether_apndata to decide whether DUN APN is required for
1886 * tethering.
1887 *
1888 * @return 0: Not required. 1: required. 2: Not set.
1889 * @hide
1890 */
1891 @Override
1892 public int getTetherApnRequired() {
1893 enforceModifyPermissionOrCarrierPrivilege();
1894 int dunRequired = Settings.Global.getInt(mPhone.getContext().getContentResolver(),
1895 Settings.Global.TETHER_DUN_REQUIRED, 2);
1896 // If not set, check net.tethering.noprovisioning, TETHER_DUN_APN setting and
1897 // config_tether_apndata.
1898 if (dunRequired == 2 && mPhone.hasMatchedTetherApnSetting()) {
1899 dunRequired = 1;
1900 }
1901 return dunRequired;
1902 }
1903
1904 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07001905 * Set mobile data enabled
1906 * Used by the user through settings etc to turn on/off mobile data
1907 *
1908 * @param enable {@code true} turn turn data on, else {@code false}
1909 */
1910 @Override
1911 public void setDataEnabled(boolean enable) {
1912 enforceModifyPermission();
1913 mPhone.setDataEnabled(enable);
1914 }
1915
1916 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07001917 * Get whether mobile data is enabled.
1918 *
1919 * Note that this used to be available from ConnectivityService, gated by
1920 * ACCESS_NETWORK_STATE permission, so this will accept either that or
1921 * our MODIFY_PHONE_STATE.
Robert Greenwalted86e582014-05-21 20:03:20 -07001922 *
1923 * @return {@code true} if data is enabled else {@code false}
1924 */
1925 @Override
1926 public boolean getDataEnabled() {
Robert Greenwalt646120a2014-05-23 11:54:03 -07001927 try {
1928 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
1929 null);
1930 } catch (Exception e) {
1931 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE,
1932 null);
1933 }
Robert Greenwalted86e582014-05-21 20:03:20 -07001934 return mPhone.getDataEnabled();
1935 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07001936
1937 @Override
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08001938 public int getCarrierPrivilegeStatus() {
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07001939 UiccCard card = UiccController.getInstance().getUiccCard();
1940 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08001941 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07001942 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
1943 }
1944 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001945 mPhone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07001946 }
Junda Liu29340342014-07-10 15:23:27 -07001947
1948 @Override
Shishir Agrawal6d5a2852014-07-11 16:32:57 -07001949 public int checkCarrierPrivilegesForPackage(String pkgname) {
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07001950 UiccCard card = UiccController.getInstance().getUiccCard();
1951 if (card == null) {
1952 loge("checkCarrierPrivilegesForPackage: No UICC");
1953 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
1954 }
1955 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgname);
Junda Liu29340342014-07-10 15:23:27 -07001956 }
Derek Tan89e89d42014-07-08 17:00:10 -07001957
1958 @Override
Diego Pontorieroaf74c862014-08-28 11:51:16 -07001959 public List<String> getCarrierPackageNamesForIntent(Intent intent) {
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07001960 UiccCard card = UiccController.getInstance().getUiccCard();
1961 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07001962 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07001963 return null ;
1964 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07001965 return card.getCarrierPackageNamesForIntent(
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07001966 mPhone.getContext().getPackageManager(), intent);
1967 }
1968
Wink Savilleb564aae2014-10-23 10:18:09 -07001969 private String getIccId(int subId) {
Derek Tan97ebb422014-09-05 16:55:38 -07001970 UiccCard card = getPhone(subId).getUiccCard();
1971 if (card == null) {
1972 loge("getIccId: No UICC");
1973 return null;
1974 }
1975 String iccId = card.getIccId();
1976 if (TextUtils.isEmpty(iccId)) {
1977 loge("getIccId: ICC ID is null or empty.");
1978 return null;
1979 }
1980 return iccId;
1981 }
1982
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07001983 @Override
Shishir Agrawal495d7e12014-12-01 11:50:28 -08001984 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag, String number) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08001985 enforceCarrierPrivilege();
Derek Tan97ebb422014-09-05 16:55:38 -07001986
1987 String iccId = getIccId(subId);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08001988 if (TextUtils.isEmpty(iccId)) {
1989 return false;
Derek Tan97ebb422014-09-05 16:55:38 -07001990 }
Shishir Agrawal495d7e12014-12-01 11:50:28 -08001991
1992 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
1993 SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
1994 if (alphaTag == null) {
1995 editor.remove(alphaTagPrefKey);
1996 } else {
1997 editor.putString(alphaTagPrefKey, alphaTag);
1998 }
1999
2000 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2001 if (number == null) {
2002 editor.remove(numberPrefKey);
2003 } else {
2004 editor.putString(numberPrefKey, number);
2005 }
2006 editor.commit();
2007 return true;
Derek Tan7226c842014-07-02 17:42:23 -07002008 }
2009
2010 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002011 public String getLine1NumberForDisplay(int subId) {
Derek Tan7226c842014-07-02 17:42:23 -07002012 enforceReadPermission();
Derek Tan97ebb422014-09-05 16:55:38 -07002013
2014 String iccId = getIccId(subId);
2015 if (iccId != null) {
2016 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002017 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002018 }
Derek Tan97ebb422014-09-05 16:55:38 -07002019 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002020 }
2021
2022 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002023 public String getLine1AlphaTagForDisplay(int subId) {
Derek Tan7226c842014-07-02 17:42:23 -07002024 enforceReadPermission();
Derek Tan97ebb422014-09-05 16:55:38 -07002025
2026 String iccId = getIccId(subId);
2027 if (iccId != null) {
2028 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002029 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002030 }
Derek Tan97ebb422014-09-05 16:55:38 -07002031 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002032 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002033
2034 @Override
Shishir Agrawala3dfd752014-09-04 13:25:42 -07002035 public boolean setOperatorBrandOverride(String brand) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002036 enforceCarrierPrivilege();
Shishir Agrawala3dfd752014-09-04 13:25:42 -07002037 return mPhone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002038 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05002039
2040 @Override
2041 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
2042 enforceModifyPermission();
2043
2044 int returnValue = 0;
2045 try {
2046 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
2047 if(result.exception == null) {
2048 if (result.result != null) {
2049 byte[] responseData = (byte[])(result.result);
2050 if(responseData.length > oemResp.length) {
2051 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
2052 responseData.length + "bytes. Buffer Size is " +
2053 oemResp.length + "bytes.");
2054 }
2055 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
2056 returnValue = responseData.length;
2057 }
2058 } else {
2059 CommandException ex = (CommandException) result.exception;
2060 returnValue = ex.getCommandError().ordinal();
2061 if(returnValue > 0) returnValue *= -1;
2062 }
2063 } catch (RuntimeException e) {
2064 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
2065 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
2066 if(returnValue > 0) returnValue *= -1;
2067 }
2068
2069 return returnValue;
2070 }
Wink Saville5d475dd2014-10-17 15:00:58 -07002071
2072 @Override
2073 public void setRadioCapability(RadioAccessFamily[] rafs) {
2074 try {
2075 ProxyController.getInstance().setRadioCapability(rafs);
2076 } catch (RuntimeException e) {
2077 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
2078 }
2079 }
2080
2081 @Override
2082 public int getRadioAccessFamily(int phoneId) {
2083 return ProxyController.getInstance().getRadioAccessFamily(phoneId);
2084 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002085
2086 @Override
2087 public void enableVideoCalling(boolean enable) {
2088 enforceModifyPermission();
2089 SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2090 editor.putBoolean(PREF_ENABLE_VIDEO_CALLING, enable);
2091 editor.commit();
2092 }
2093
2094 @Override
2095 public boolean isVideoCallingEnabled() {
2096 enforceReadPermission();
Andrew Lee77527ac2014-10-21 16:57:39 -07002097 // Check the user preference and the system-level IMS setting. Even if the user has
2098 // enabled video calling, if IMS is disabled we aren't able to support video calling.
2099 // In the long run, we may instead need to check if there exists a connection service
2100 // which can support video calling.
Andrew Lee312e8172014-10-23 17:01:36 -07002101 return ImsManager.isVtEnabledByPlatform(mPhone.getContext())
2102 && ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
2103 && mTelephonySharedPreferences.getBoolean(PREF_ENABLE_VIDEO_CALLING, true);
Andrew Leedf14ead2014-10-17 14:22:52 -07002104 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002105}