blob: e3d1a080a6019a64b2658b79a35955135d613020 [file] [log] [blame]
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.phone;
18
19import android.app.ActivityManager;
20import android.app.AppOpsManager;
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -070021import android.content.ComponentName;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070022import android.content.Context;
23import android.content.Intent;
Derek Tan97ebb422014-09-05 16:55:38 -070024import android.content.SharedPreferences;
Shishir Agrawal60f9c952014-06-23 12:00:43 -070025import android.content.pm.PackageInfo;
26import android.content.pm.PackageManager;
27import android.content.pm.Signature;
Wink Saville36469e72014-06-11 15:17:00 -070028import android.net.ConnectivityManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070029import android.net.Uri;
30import android.os.AsyncResult;
31import android.os.Binder;
32import android.os.Bundle;
33import android.os.Handler;
Gabriel Peal36ebb0d2014-03-20 09:20:43 -070034import android.os.IBinder;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070035import android.os.Looper;
36import android.os.Message;
37import android.os.Process;
Gabriel Peal36ebb0d2014-03-20 09:20:43 -070038import android.os.RemoteException;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070039import android.os.ServiceManager;
Wink Saville08874612014-08-31 19:19:58 -070040import android.os.SystemProperties;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070041import android.os.UserHandle;
Derek Tan97ebb422014-09-05 16:55:38 -070042import android.preference.PreferenceManager;
Ihab Awadf2177b72013-11-25 13:33:23 -080043import android.provider.Settings;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070044import android.telephony.CellInfo;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -070045import android.telephony.IccOpenLogicalChannelResponse;
Jake Hambye994d462014-02-03 13:10:13 -080046import android.telephony.NeighboringCellInfo;
Wink Saville5d475dd2014-10-17 15:00:58 -070047import android.telephony.RadioAccessFamily;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070048import android.telephony.ServiceState;
Wink Saville36469e72014-06-11 15:17:00 -070049import android.telephony.SubscriptionManager;
Wink Saville08874612014-08-31 19:19:58 -070050import android.telephony.SubInfoRecord;
Ihab Awadf2177b72013-11-25 13:33:23 -080051import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070052import android.text.TextUtils;
53import android.util.Log;
Jake Hambye994d462014-02-03 13:10:13 -080054import android.util.Pair;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070055
Andrew Lee312e8172014-10-23 17:01:36 -070056import com.android.ims.ImsManager;
Shishir Agrawal566b7612013-10-28 14:41:00 -070057import com.android.internal.telephony.CallManager;
58import com.android.internal.telephony.CommandException;
Gabriel Peal36ebb0d2014-03-20 09:20:43 -070059import com.android.internal.telephony.Connection;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070060import com.android.internal.telephony.DefaultPhoneNotifier;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070061import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -080062import com.android.internal.telephony.IccCard;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070063import com.android.internal.telephony.Phone;
Wink Saville36469e72014-06-11 15:17:00 -070064import com.android.internal.telephony.PhoneFactory;
Wink Saville5d475dd2014-10-17 15:00:58 -070065import com.android.internal.telephony.PhoneProxy;
66import com.android.internal.telephony.ProxyController;
Wink Saville36469e72014-06-11 15:17:00 -070067import com.android.internal.telephony.CallManager;
68import com.android.internal.telephony.CommandException;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070069import com.android.internal.telephony.PhoneConstants;
Wink Saville08874612014-08-31 19:19:58 -070070import com.android.internal.telephony.TelephonyIntents;
Wink Saville36469e72014-06-11 15:17:00 -070071import com.android.internal.telephony.dataconnection.DctController;
Derek Tan7226c842014-07-02 17:42:23 -070072import com.android.internal.telephony.uicc.AdnRecord;
Shishir Agrawal566b7612013-10-28 14:41:00 -070073import com.android.internal.telephony.uicc.IccIoResult;
74import com.android.internal.telephony.uicc.IccUtils;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -070075import com.android.internal.telephony.uicc.UiccCard;
Shishir Agrawal60f9c952014-06-23 12:00:43 -070076import com.android.internal.telephony.uicc.UiccCarrierPrivilegeRules;
Shishir Agrawal566b7612013-10-28 14:41:00 -070077import com.android.internal.telephony.uicc.UiccController;
Jake Hambye994d462014-02-03 13:10:13 -080078import com.android.internal.util.HexDump;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070079
Wink Saville36469e72014-06-11 15:17:00 -070080import static com.android.internal.telephony.PhoneConstants.SUBSCRIPTION_KEY;
81
Santos Cordon7d4ddf62013-07-10 11:58:08 -070082import java.util.ArrayList;
Gabriel Peal36ebb0d2014-03-20 09:20:43 -070083import java.util.HashMap;
Derek Tan89e89d42014-07-08 17:00:10 -070084import java.util.HashSet;
Gabriel Peal36ebb0d2014-03-20 09:20:43 -070085import java.util.Iterator;
Jake Hambye994d462014-02-03 13:10:13 -080086import java.util.List;
Gabriel Peal36ebb0d2014-03-20 09:20:43 -070087import java.util.Map;
Derek Tan89e89d42014-07-08 17:00:10 -070088import java.util.Set;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070089
90/**
91 * Implementation of the ITelephony interface.
92 */
Santos Cordon117fee72014-05-16 17:56:12 -070093public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -070094 private static final String LOG_TAG = "PhoneInterfaceManager";
95 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
96 private static final boolean DBG_LOC = false;
97
98 // Message codes used with mMainThreadHandler
99 private static final int CMD_HANDLE_PIN_MMI = 1;
100 private static final int CMD_HANDLE_NEIGHBORING_CELL = 2;
101 private static final int EVENT_NEIGHBORING_CELL_DONE = 3;
102 private static final int CMD_ANSWER_RINGING_CALL = 4;
103 private static final int CMD_END_CALL = 5; // not used yet
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700104 private static final int CMD_TRANSMIT_APDU_LOGICAL_CHANNEL = 7;
105 private static final int EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE = 8;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700106 private static final int CMD_OPEN_CHANNEL = 9;
107 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
108 private static final int CMD_CLOSE_CHANNEL = 11;
109 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -0800110 private static final int CMD_NV_READ_ITEM = 13;
111 private static final int EVENT_NV_READ_ITEM_DONE = 14;
112 private static final int CMD_NV_WRITE_ITEM = 15;
113 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
114 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
115 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
116 private static final int CMD_NV_RESET_CONFIG = 19;
117 private static final int EVENT_NV_RESET_CONFIG_DONE = 20;
Jake Hamby7c27be32014-03-03 13:25:59 -0800118 private static final int CMD_GET_PREFERRED_NETWORK_TYPE = 21;
119 private static final int EVENT_GET_PREFERRED_NETWORK_TYPE_DONE = 22;
120 private static final int CMD_SET_PREFERRED_NETWORK_TYPE = 23;
121 private static final int EVENT_SET_PREFERRED_NETWORK_TYPE_DONE = 24;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800122 private static final int CMD_SEND_ENVELOPE = 25;
123 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Derek Tan6b088ee2014-09-05 14:15:18 -0700124 private static final int CMD_INVOKE_OEM_RIL_REQUEST_RAW = 27;
125 private static final int EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE = 28;
126 private static final int CMD_TRANSMIT_APDU_BASIC_CHANNEL = 29;
127 private static final int EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE = 30;
128 private static final int CMD_EXCHANGE_SIM_IO = 31;
129 private static final int EVENT_EXCHANGE_SIM_IO_DONE = 32;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700130
131 /** The singleton instance. */
132 private static PhoneInterfaceManager sInstance;
133
134 PhoneGlobals mApp;
135 Phone mPhone;
136 CallManager mCM;
137 AppOpsManager mAppOps;
138 MainThreadHandler mMainThreadHandler;
139
Andrew Leedf14ead2014-10-17 14:22:52 -0700140 SharedPreferences mTelephonySharedPreferences;
Derek Tan97ebb422014-09-05 16:55:38 -0700141 private static final String PREF_CARRIERS_ALPHATAG_PREFIX = "carrier_alphtag_";
142 private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
143 private static final String PREF_CARRIERS_SIMPLIFIED_NETWORK_SETTINGS_PREFIX =
144 "carrier_simplified_network_settings_";
Andrew Leedf14ead2014-10-17 14:22:52 -0700145 private static final String PREF_ENABLE_VIDEO_CALLING = "enable_video_calling";
Derek Tan89e89d42014-07-08 17:00:10 -0700146
147 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700148 * A request object to use for transmitting data to an ICC.
149 */
150 private static final class IccAPDUArgument {
151 public int channel, cla, command, p1, p2, p3;
152 public String data;
153
154 public IccAPDUArgument(int channel, int cla, int command,
155 int p1, int p2, int p3, String data) {
156 this.channel = channel;
157 this.cla = cla;
158 this.command = command;
159 this.p1 = p1;
160 this.p2 = p2;
161 this.p3 = p3;
162 this.data = data;
163 }
164 }
165
166 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700167 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
168 * request after sending. The main thread will notify the request when it is complete.
169 */
170 private static final class MainThreadRequest {
171 /** The argument to use for the request */
172 public Object argument;
173 /** The result of the request that is run on the main thread */
174 public Object result;
175
176 public MainThreadRequest(Object argument) {
177 this.argument = argument;
178 }
179 }
180
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800181 private static final class IncomingThirdPartyCallArgs {
182 public final ComponentName component;
183 public final String callId;
184 public final String callerDisplayName;
185
186 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
187 String callerDisplayName) {
188 this.component = component;
189 this.callId = callId;
190 this.callerDisplayName = callerDisplayName;
191 }
192 }
193
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700194 /**
195 * A handler that processes messages on the main thread in the phone process. Since many
196 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
197 * inbound binder threads to the main thread in the phone process. The Binder thread
198 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
199 * on, which will be notified when the operation completes and will contain the result of the
200 * request.
201 *
202 * <p>If a MainThreadRequest object is provided in the msg.obj field,
203 * note that request.result must be set to something non-null for the calling thread to
204 * unblock.
205 */
206 private final class MainThreadHandler extends Handler {
207 @Override
208 public void handleMessage(Message msg) {
209 MainThreadRequest request;
210 Message onCompleted;
211 AsyncResult ar;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700212 UiccCard uiccCard = UiccController.getInstance().getUiccCard();
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700213 IccAPDUArgument iccArgument;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700214
215 switch (msg.what) {
216 case CMD_HANDLE_PIN_MMI:
217 request = (MainThreadRequest) msg.obj;
Jake Hambye994d462014-02-03 13:10:13 -0800218 request.result = mPhone.handlePinMmi((String) request.argument);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700219 // Wake up the requesting thread
220 synchronized (request) {
221 request.notifyAll();
222 }
223 break;
224
225 case CMD_HANDLE_NEIGHBORING_CELL:
226 request = (MainThreadRequest) msg.obj;
227 onCompleted = obtainMessage(EVENT_NEIGHBORING_CELL_DONE,
228 request);
229 mPhone.getNeighboringCids(onCompleted);
230 break;
231
232 case EVENT_NEIGHBORING_CELL_DONE:
233 ar = (AsyncResult) msg.obj;
234 request = (MainThreadRequest) ar.userObj;
235 if (ar.exception == null && ar.result != null) {
236 request.result = ar.result;
237 } else {
238 // create an empty list to notify the waiting thread
Jake Hambye994d462014-02-03 13:10:13 -0800239 request.result = new ArrayList<NeighboringCellInfo>(0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700240 }
241 // Wake up the requesting thread
242 synchronized (request) {
243 request.notifyAll();
244 }
245 break;
246
247 case CMD_ANSWER_RINGING_CALL:
Wink Saville08874612014-08-31 19:19:58 -0700248 request = (MainThreadRequest) msg.obj;
Wink Savilleb564aae2014-10-23 10:18:09 -0700249 int answer_subId = ((Integer)request.argument).intValue();
Wink Saville08874612014-08-31 19:19:58 -0700250 answerRingingCallInternal(answer_subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700251 break;
252
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700253 case CMD_END_CALL:
254 request = (MainThreadRequest) msg.obj;
Wink Savilleb564aae2014-10-23 10:18:09 -0700255 int end_subId = ((Integer)request.argument).intValue();
Wink Saville08874612014-08-31 19:19:58 -0700256 final boolean hungUp;
257 int phoneType = getPhone(end_subId).getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700258 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
259 // CDMA: If the user presses the Power button we treat it as
260 // ending the complete call session
Wink Saville08874612014-08-31 19:19:58 -0700261 hungUp = PhoneUtils.hangupRingingAndActive(getPhone(end_subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700262 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
263 // GSM: End the call as per the Phone state
264 hungUp = PhoneUtils.hangup(mCM);
265 } else {
266 throw new IllegalStateException("Unexpected phone type: " + phoneType);
267 }
268 if (DBG) log("CMD_END_CALL: " + (hungUp ? "hung up!" : "no call to hang up"));
269 request.result = hungUp;
270 // Wake up the requesting thread
271 synchronized (request) {
272 request.notifyAll();
273 }
274 break;
275
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700276 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700277 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700278 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700279 if (uiccCard == null) {
280 loge("iccTransmitApduLogicalChannel: No UICC");
281 request.result = new IccIoResult(0x6F, 0, (byte[])null);
282 synchronized (request) {
283 request.notifyAll();
284 }
285 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700286 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
287 request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700288 uiccCard.iccTransmitApduLogicalChannel(
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700289 iccArgument.channel, iccArgument.cla, iccArgument.command,
290 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
Shishir Agrawal566b7612013-10-28 14:41:00 -0700291 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700292 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700293 break;
294
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700295 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700296 ar = (AsyncResult) msg.obj;
297 request = (MainThreadRequest) ar.userObj;
298 if (ar.exception == null && ar.result != null) {
299 request.result = ar.result;
300 } else {
301 request.result = new IccIoResult(0x6F, 0, (byte[])null);
302 if (ar.result == null) {
303 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800304 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700305 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800306 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700307 } else {
308 loge("iccTransmitApduLogicalChannel: Unknown exception");
309 }
310 }
311 synchronized (request) {
312 request.notifyAll();
313 }
314 break;
315
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700316 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
317 request = (MainThreadRequest) msg.obj;
318 iccArgument = (IccAPDUArgument) request.argument;
319 if (uiccCard == null) {
320 loge("iccTransmitApduBasicChannel: No UICC");
321 request.result = new IccIoResult(0x6F, 0, (byte[])null);
322 synchronized (request) {
323 request.notifyAll();
324 }
325 } else {
326 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
327 request);
328 uiccCard.iccTransmitApduBasicChannel(
329 iccArgument.cla, iccArgument.command, iccArgument.p1, iccArgument.p2,
330 iccArgument.p3, iccArgument.data, onCompleted);
331 }
332 break;
333
334 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
335 ar = (AsyncResult) msg.obj;
336 request = (MainThreadRequest) ar.userObj;
337 if (ar.exception == null && ar.result != null) {
338 request.result = ar.result;
339 } else {
340 request.result = new IccIoResult(0x6F, 0, (byte[])null);
341 if (ar.result == null) {
342 loge("iccTransmitApduBasicChannel: Empty response");
343 } else if (ar.exception instanceof CommandException) {
344 loge("iccTransmitApduBasicChannel: CommandException: " +
345 ar.exception);
346 } else {
347 loge("iccTransmitApduBasicChannel: Unknown exception");
348 }
349 }
350 synchronized (request) {
351 request.notifyAll();
352 }
353 break;
354
355 case CMD_EXCHANGE_SIM_IO:
356 request = (MainThreadRequest) msg.obj;
357 iccArgument = (IccAPDUArgument) request.argument;
358 if (uiccCard == null) {
359 loge("iccExchangeSimIO: No UICC");
360 request.result = new IccIoResult(0x6F, 0, (byte[])null);
361 synchronized (request) {
362 request.notifyAll();
363 }
364 } else {
365 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
366 request);
367 uiccCard.iccExchangeSimIO(iccArgument.cla, /* fileID */
368 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
369 iccArgument.data, onCompleted);
370 }
371 break;
372
373 case EVENT_EXCHANGE_SIM_IO_DONE:
374 ar = (AsyncResult) msg.obj;
375 request = (MainThreadRequest) ar.userObj;
376 if (ar.exception == null && ar.result != null) {
377 request.result = ar.result;
378 } else {
379 request.result = new IccIoResult(0x6f, 0, (byte[])null);
380 }
381 synchronized (request) {
382 request.notifyAll();
383 }
384 break;
385
Derek Tan4d5e5c12014-02-04 11:54:58 -0800386 case CMD_SEND_ENVELOPE:
387 request = (MainThreadRequest) msg.obj;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700388 if (uiccCard == null) {
389 loge("sendEnvelopeWithStatus: No UICC");
390 request.result = new IccIoResult(0x6F, 0, (byte[])null);
391 synchronized (request) {
392 request.notifyAll();
393 }
394 } else {
395 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
396 uiccCard.sendEnvelopeWithStatus((String)request.argument, onCompleted);
397 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800398 break;
399
400 case EVENT_SEND_ENVELOPE_DONE:
401 ar = (AsyncResult) msg.obj;
402 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700403 if (ar.exception == null && ar.result != null) {
404 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800405 } else {
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700406 request.result = new IccIoResult(0x6F, 0, (byte[])null);
407 if (ar.result == null) {
408 loge("sendEnvelopeWithStatus: Empty response");
409 } else if (ar.exception instanceof CommandException) {
410 loge("sendEnvelopeWithStatus: CommandException: " +
411 ar.exception);
412 } else {
413 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
414 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800415 }
416 synchronized (request) {
417 request.notifyAll();
418 }
419 break;
420
Shishir Agrawal566b7612013-10-28 14:41:00 -0700421 case CMD_OPEN_CHANNEL:
422 request = (MainThreadRequest) msg.obj;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700423 if (uiccCard == null) {
424 loge("iccOpenLogicalChannel: No UICC");
425 request.result = new IccIoResult(0x6F, 0, (byte[])null);
426 synchronized (request) {
427 request.notifyAll();
428 }
429 } else {
430 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
431 uiccCard.iccOpenLogicalChannel((String)request.argument, onCompleted);
432 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700433 break;
434
435 case EVENT_OPEN_CHANNEL_DONE:
436 ar = (AsyncResult) msg.obj;
437 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700438 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700439 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700440 int[] result = (int[]) ar.result;
441 int channelId = result[0];
442 byte[] selectResponse = null;
443 if (result.length > 1) {
444 selectResponse = new byte[result.length - 1];
445 for (int i = 1; i < result.length; ++i) {
446 selectResponse[i - 1] = (byte) result[i];
447 }
448 }
449 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700450 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700451 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700452 if (ar.result == null) {
453 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700454 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700455 if (ar.exception != null) {
456 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
457 }
458
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700459 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700460 if ((ar.exception != null) && (ar.exception instanceof CommandException)) {
461 if (ar.exception.getMessage().compareTo("MISSING_RESOURCE") == 0) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700462 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700463 } else if (ar.exception.getMessage().compareTo("NO_SUCH_ELEMENT") == 0) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700464 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700465 }
466 }
467 openChannelResp = new IccOpenLogicalChannelResponse(
468 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700469 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700470 request.result = openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700471 synchronized (request) {
472 request.notifyAll();
473 }
474 break;
475
476 case CMD_CLOSE_CHANNEL:
477 request = (MainThreadRequest) msg.obj;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700478 if (uiccCard == null) {
479 loge("iccCloseLogicalChannel: No UICC");
480 request.result = new IccIoResult(0x6F, 0, (byte[])null);
481 synchronized (request) {
482 request.notifyAll();
483 }
484 } else {
485 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
486 uiccCard.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
487 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700488 break;
489
490 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800491 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
492 break;
493
494 case CMD_NV_READ_ITEM:
495 request = (MainThreadRequest) msg.obj;
496 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
497 mPhone.nvReadItem((Integer) request.argument, onCompleted);
498 break;
499
500 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700501 ar = (AsyncResult) msg.obj;
502 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800503 if (ar.exception == null && ar.result != null) {
504 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700505 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800506 request.result = "";
507 if (ar.result == null) {
508 loge("nvReadItem: Empty response");
509 } else if (ar.exception instanceof CommandException) {
510 loge("nvReadItem: CommandException: " +
511 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700512 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800513 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700514 }
515 }
516 synchronized (request) {
517 request.notifyAll();
518 }
519 break;
520
Jake Hambye994d462014-02-03 13:10:13 -0800521 case CMD_NV_WRITE_ITEM:
522 request = (MainThreadRequest) msg.obj;
523 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
524 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
525 mPhone.nvWriteItem(idValue.first, idValue.second, onCompleted);
526 break;
527
528 case EVENT_NV_WRITE_ITEM_DONE:
529 handleNullReturnEvent(msg, "nvWriteItem");
530 break;
531
532 case CMD_NV_WRITE_CDMA_PRL:
533 request = (MainThreadRequest) msg.obj;
534 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
535 mPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
536 break;
537
538 case EVENT_NV_WRITE_CDMA_PRL_DONE:
539 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
540 break;
541
542 case CMD_NV_RESET_CONFIG:
543 request = (MainThreadRequest) msg.obj;
544 onCompleted = obtainMessage(EVENT_NV_RESET_CONFIG_DONE, request);
545 mPhone.nvResetConfig((Integer) request.argument, onCompleted);
546 break;
547
548 case EVENT_NV_RESET_CONFIG_DONE:
549 handleNullReturnEvent(msg, "nvResetConfig");
550 break;
551
Jake Hamby7c27be32014-03-03 13:25:59 -0800552 case CMD_GET_PREFERRED_NETWORK_TYPE:
553 request = (MainThreadRequest) msg.obj;
554 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
555 mPhone.getPreferredNetworkType(onCompleted);
556 break;
557
558 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
559 ar = (AsyncResult) msg.obj;
560 request = (MainThreadRequest) ar.userObj;
561 if (ar.exception == null && ar.result != null) {
562 request.result = ar.result; // Integer
563 } else {
564 request.result = -1;
565 if (ar.result == null) {
566 loge("getPreferredNetworkType: Empty response");
567 } else if (ar.exception instanceof CommandException) {
568 loge("getPreferredNetworkType: CommandException: " +
569 ar.exception);
570 } else {
571 loge("getPreferredNetworkType: Unknown exception");
572 }
573 }
574 synchronized (request) {
575 request.notifyAll();
576 }
577 break;
578
579 case CMD_SET_PREFERRED_NETWORK_TYPE:
580 request = (MainThreadRequest) msg.obj;
581 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
582 int networkType = (Integer) request.argument;
583 mPhone.setPreferredNetworkType(networkType, onCompleted);
584 break;
585
586 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
587 handleNullReturnEvent(msg, "setPreferredNetworkType");
588 break;
589
Steven Liu4bf01bc2014-07-17 11:05:29 -0500590 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
591 request = (MainThreadRequest)msg.obj;
592 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
593 mPhone.invokeOemRilRequestRaw((byte[])request.argument, onCompleted);
594 break;
595
596 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
597 ar = (AsyncResult)msg.obj;
598 request = (MainThreadRequest)ar.userObj;
599 request.result = ar;
600 synchronized (request) {
601 request.notifyAll();
602 }
603 break;
604
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700605 default:
606 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
607 break;
608 }
609 }
Jake Hambye994d462014-02-03 13:10:13 -0800610
611 private void handleNullReturnEvent(Message msg, String command) {
612 AsyncResult ar = (AsyncResult) msg.obj;
613 MainThreadRequest request = (MainThreadRequest) ar.userObj;
614 if (ar.exception == null) {
615 request.result = true;
616 } else {
617 request.result = false;
618 if (ar.exception instanceof CommandException) {
619 loge(command + ": CommandException: " + ar.exception);
620 } else {
621 loge(command + ": Unknown exception");
622 }
623 }
624 synchronized (request) {
625 request.notifyAll();
626 }
627 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700628 }
629
630 /**
631 * Posts the specified command to be executed on the main thread,
632 * waits for the request to complete, and returns the result.
633 * @see #sendRequestAsync
634 */
635 private Object sendRequest(int command, Object argument) {
Santos Cordon500b0e02014-06-17 10:33:33 -0700636 return sendRequest(command, argument, null);
Wink Saville36469e72014-06-11 15:17:00 -0700637 }
638
639 /**
640 * Posts the specified command to be executed on the main thread,
641 * waits for the request to complete, and returns the result.
642 * @see #sendRequestAsync
643 */
644 private Object sendRequest(int command, Object argument, Object argument2) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700645 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
646 throw new RuntimeException("This method will deadlock if called from the main thread.");
647 }
648
649 MainThreadRequest request = new MainThreadRequest(argument);
650 Message msg = mMainThreadHandler.obtainMessage(command, request);
651 msg.sendToTarget();
652
653 // Wait for the request to complete
654 synchronized (request) {
655 while (request.result == null) {
656 try {
657 request.wait();
658 } catch (InterruptedException e) {
659 // Do nothing, go back and wait until the request is complete
660 }
661 }
662 }
663 return request.result;
664 }
665
666 /**
667 * Asynchronous ("fire and forget") version of sendRequest():
668 * Posts the specified command to be executed on the main thread, and
669 * returns immediately.
670 * @see #sendRequest
671 */
672 private void sendRequestAsync(int command) {
673 mMainThreadHandler.sendEmptyMessage(command);
674 }
675
676 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -0700677 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
678 * @see {@link #sendRequest(int,Object)}
679 */
680 private void sendRequestAsync(int command, Object argument) {
681 MainThreadRequest request = new MainThreadRequest(argument);
682 Message msg = mMainThreadHandler.obtainMessage(command, request);
683 msg.sendToTarget();
684 }
685
686 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700687 * Initialize the singleton PhoneInterfaceManager instance.
688 * This is only done once, at startup, from PhoneApp.onCreate().
689 */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700690 /* package */ static PhoneInterfaceManager init(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700691 synchronized (PhoneInterfaceManager.class) {
692 if (sInstance == null) {
Sailesh Nepal194161e2014-07-03 08:57:44 -0700693 sInstance = new PhoneInterfaceManager(app, phone);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700694 } else {
695 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
696 }
697 return sInstance;
698 }
699 }
700
701 /** Private constructor; @see init() */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700702 private PhoneInterfaceManager(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700703 mApp = app;
704 mPhone = phone;
705 mCM = PhoneGlobals.getInstance().mCM;
706 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
707 mMainThreadHandler = new MainThreadHandler();
Andrew Leedf14ead2014-10-17 14:22:52 -0700708 mTelephonySharedPreferences =
Derek Tan97ebb422014-09-05 16:55:38 -0700709 PreferenceManager.getDefaultSharedPreferences(mPhone.getContext());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700710 publish();
711 }
712
713 private void publish() {
714 if (DBG) log("publish: " + this);
715
716 ServiceManager.addService("phone", this);
717 }
718
Wink Saville36469e72014-06-11 15:17:00 -0700719 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -0700720 private Phone getPhone(int subId) {
Legler Wuf8733b42014-09-25 11:00:54 +0800721 return PhoneFactory.getPhone(SubscriptionManager.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -0700722 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700723 //
724 // Implementation of the ITelephony interface.
725 //
726
727 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700728 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -0700729 }
730
Wink Savilleb564aae2014-10-23 10:18:09 -0700731 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700732 if (DBG) log("dial: " + number);
733 // No permission check needed here: This is just a wrapper around the
734 // ACTION_DIAL intent, which is available to any app since it puts up
735 // the UI before it does anything.
736
737 String url = createTelUrl(number);
738 if (url == null) {
739 return;
740 }
741
742 // PENDING: should we just silently fail if phone is offhook or ringing?
Wink Saville36469e72014-06-11 15:17:00 -0700743 PhoneConstants.State state = mCM.getState(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700744 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
745 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
746 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
747 mApp.startActivity(intent);
748 }
749 }
750
751 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700752 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -0700753 }
754
Wink Savilleb564aae2014-10-23 10:18:09 -0700755 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700756 if (DBG) log("call: " + number);
757
758 // This is just a wrapper around the ACTION_CALL intent, but we still
759 // need to do a permission check since we're calling startActivity()
760 // from the context of the phone app.
761 enforceCallPermission();
762
763 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
764 != AppOpsManager.MODE_ALLOWED) {
765 return;
766 }
767
768 String url = createTelUrl(number);
769 if (url == null) {
770 return;
771 }
772
Wink Saville08874612014-08-31 19:19:58 -0700773 boolean isValid = false;
Wink Saville10aa2e92014-09-04 10:46:34 -0700774 List<SubInfoRecord> slist = SubscriptionManager.getActiveSubInfoList();
Wink Saville08874612014-08-31 19:19:58 -0700775 for (SubInfoRecord subInfoRecord : slist) {
Stuart Scott48779712014-10-30 11:17:34 -0700776 if (subInfoRecord.getSubscriptionId() == subId) {
Wink Saville08874612014-08-31 19:19:58 -0700777 isValid = true;
778 break;
779 }
780 }
781 if (isValid == false) {
782 return;
783 }
784
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700785 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
Wink Saville36469e72014-06-11 15:17:00 -0700786 intent.putExtra(SUBSCRIPTION_KEY, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700787 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
788 mApp.startActivity(intent);
789 }
790
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700791 /**
792 * End a call based on call state
793 * @return true is a call was ended
794 */
795 public boolean endCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700796 return endCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700797 }
798
799 /**
800 * End a call based on the call state of the subId
801 * @return true is a call was ended
802 */
Wink Savilleb564aae2014-10-23 10:18:09 -0700803 public boolean endCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700804 enforceCallPermission();
Wink Saville9f73bda2014-11-05 08:13:36 -0800805 return (Boolean) sendRequest(CMD_END_CALL, new Integer(subId), null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700806 }
807
808 public void answerRingingCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700809 answerRingingCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700810 }
811
Wink Savilleb564aae2014-10-23 10:18:09 -0700812 public void answerRingingCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700813 if (DBG) log("answerRingingCall...");
814 // TODO: there should eventually be a separate "ANSWER_PHONE" permission,
815 // but that can probably wait till the big TelephonyManager API overhaul.
816 // For now, protect this call with the MODIFY_PHONE_STATE permission.
817 enforceModifyPermission();
Wink Saville9f73bda2014-11-05 08:13:36 -0800818 sendRequest(CMD_ANSWER_RINGING_CALL, new Integer(subId), null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700819 }
820
821 /**
822 * Make the actual telephony calls to implement answerRingingCall().
823 * This should only be called from the main thread of the Phone app.
824 * @see #answerRingingCall
825 *
826 * TODO: it would be nice to return true if we answered the call, or
827 * false if there wasn't actually a ringing incoming call, or some
828 * other error occurred. (In other words, pass back the return value
829 * from PhoneUtils.answerCall() or PhoneUtils.answerAndEndActive().)
830 * But that would require calling this method via sendRequest() rather
831 * than sendRequestAsync(), and right now we don't actually *need* that
832 * return value, so let's just return void for now.
833 */
Wink Savilleb564aae2014-10-23 10:18:09 -0700834 private void answerRingingCallInternal(int subId) {
Wink Saville08874612014-08-31 19:19:58 -0700835 final boolean hasRingingCall = !getPhone(subId).getRingingCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700836 if (hasRingingCall) {
Wink Saville08874612014-08-31 19:19:58 -0700837 final boolean hasActiveCall = !getPhone(subId).getForegroundCall().isIdle();
838 final boolean hasHoldingCall = !getPhone(subId).getBackgroundCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700839 if (hasActiveCall && hasHoldingCall) {
840 // Both lines are in use!
841 // TODO: provide a flag to let the caller specify what
842 // policy to use if both lines are in use. (The current
843 // behavior is hardwired to "answer incoming, end ongoing",
844 // which is how the CALL button is specced to behave.)
845 PhoneUtils.answerAndEndActive(mCM, mCM.getFirstActiveRingingCall());
846 return;
847 } else {
848 // answerCall() will automatically hold the current active
849 // call, if there is one.
850 PhoneUtils.answerCall(mCM.getFirstActiveRingingCall());
851 return;
852 }
853 } else {
854 // No call was ringing.
855 return;
856 }
857 }
858
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700859 /**
Santos Cordon5422a8d2014-09-12 04:20:56 -0700860 * This method is no longer used and can be removed once TelephonyManager stops referring to it.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700861 */
Santos Cordon5422a8d2014-09-12 04:20:56 -0700862 public void silenceRinger() {
863 Log.e(LOG_TAG, "silenseRinger not supported");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700864 }
865
866 public boolean isOffhook() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700867 return isOffhookForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700868 }
869
Wink Savilleb564aae2014-10-23 10:18:09 -0700870 public boolean isOffhookForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -0700871 return (getPhone(subId).getState() == PhoneConstants.State.OFFHOOK);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700872 }
873
874 public boolean isRinging() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700875 return (isRingingForSubscriber(getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -0700876 }
877
Wink Savilleb564aae2014-10-23 10:18:09 -0700878 public boolean isRingingForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -0700879 return (getPhone(subId).getState() == PhoneConstants.State.RINGING);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700880 }
881
882 public boolean isIdle() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700883 return isIdleForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700884 }
885
Wink Savilleb564aae2014-10-23 10:18:09 -0700886 public boolean isIdleForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -0700887 return (getPhone(subId).getState() == PhoneConstants.State.IDLE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700888 }
889
890 public boolean isSimPinEnabled() {
891 enforceReadPermission();
892 return (PhoneGlobals.getInstance().isSimPinEnabled());
893 }
894
895 public boolean supplyPin(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700896 return supplyPinForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -0700897 }
898
Wink Savilleb564aae2014-10-23 10:18:09 -0700899 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700900 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -0700901 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
902 }
903
904 public boolean supplyPuk(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700905 return supplyPukForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -0700906 }
907
Wink Savilleb564aae2014-10-23 10:18:09 -0700908 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700909 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -0700910 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
911 }
912
913 /** {@hide} */
914 public int[] supplyPinReportResult(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700915 return supplyPinReportResultForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -0700916 }
917
Wink Savilleb564aae2014-10-23 10:18:09 -0700918 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700919 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -0700920 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700921 checkSimPin.start();
922 return checkSimPin.unlockSim(null, pin);
923 }
924
Wink Saville9de0f752013-10-22 19:04:03 -0700925 /** {@hide} */
926 public int[] supplyPukReportResult(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700927 return supplyPukReportResultForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -0700928 }
929
Wink Savilleb564aae2014-10-23 10:18:09 -0700930 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700931 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -0700932 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700933 checkSimPuk.start();
934 return checkSimPuk.unlockSim(puk, pin);
935 }
936
937 /**
Wink Saville9de0f752013-10-22 19:04:03 -0700938 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700939 * a synchronous one.
940 */
941 private static class UnlockSim extends Thread {
942
943 private final IccCard mSimCard;
944
945 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -0700946 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
947 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700948
949 // For replies from SimCard interface
950 private Handler mHandler;
951
952 // For async handler to identify request type
953 private static final int SUPPLY_PIN_COMPLETE = 100;
954
955 public UnlockSim(IccCard simCard) {
956 mSimCard = simCard;
957 }
958
959 @Override
960 public void run() {
961 Looper.prepare();
962 synchronized (UnlockSim.this) {
963 mHandler = new Handler() {
964 @Override
965 public void handleMessage(Message msg) {
966 AsyncResult ar = (AsyncResult) msg.obj;
967 switch (msg.what) {
968 case SUPPLY_PIN_COMPLETE:
969 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
970 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -0700971 mRetryCount = msg.arg1;
972 if (ar.exception != null) {
973 if (ar.exception instanceof CommandException &&
974 ((CommandException)(ar.exception)).getCommandError()
975 == CommandException.Error.PASSWORD_INCORRECT) {
976 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
977 } else {
978 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
979 }
980 } else {
981 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
982 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700983 mDone = true;
984 UnlockSim.this.notifyAll();
985 }
986 break;
987 }
988 }
989 };
990 UnlockSim.this.notifyAll();
991 }
992 Looper.loop();
993 }
994
995 /*
996 * Use PIN or PUK to unlock SIM card
997 *
998 * If PUK is null, unlock SIM card with PIN
999 *
1000 * If PUK is not null, unlock SIM card with PUK and set PIN code
1001 */
Wink Saville9de0f752013-10-22 19:04:03 -07001002 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001003
1004 while (mHandler == null) {
1005 try {
1006 wait();
1007 } catch (InterruptedException e) {
1008 Thread.currentThread().interrupt();
1009 }
1010 }
1011 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
1012
1013 if (puk == null) {
1014 mSimCard.supplyPin(pin, callback);
1015 } else {
1016 mSimCard.supplyPuk(puk, pin, callback);
1017 }
1018
1019 while (!mDone) {
1020 try {
1021 Log.d(LOG_TAG, "wait for done");
1022 wait();
1023 } catch (InterruptedException e) {
1024 // Restore the interrupted status
1025 Thread.currentThread().interrupt();
1026 }
1027 }
1028 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07001029 int[] resultArray = new int[2];
1030 resultArray[0] = mResult;
1031 resultArray[1] = mRetryCount;
1032 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001033 }
1034 }
1035
1036 public void updateServiceLocation() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001037 updateServiceLocationForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001038
1039 }
1040
Wink Savilleb564aae2014-10-23 10:18:09 -07001041 public void updateServiceLocationForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001042 // No permission check needed here: this call is harmless, and it's
1043 // needed for the ServiceState.requestStateUpdate() call (which is
1044 // already intentionally exposed to 3rd parties.)
Wink Saville36469e72014-06-11 15:17:00 -07001045 getPhone(subId).updateServiceLocation();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001046 }
1047
1048 public boolean isRadioOn() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001049 return isRadioOnForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001050 }
1051
Wink Savilleb564aae2014-10-23 10:18:09 -07001052 public boolean isRadioOnForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001053 return getPhone(subId).getServiceState().getState() != ServiceState.STATE_POWER_OFF;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001054 }
1055
1056 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001057 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001058
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001059 }
Wink Saville36469e72014-06-11 15:17:00 -07001060
Wink Savilleb564aae2014-10-23 10:18:09 -07001061 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001062 enforceModifyPermission();
Wink Savilleadd7cc52014-09-08 14:23:09 -07001063 getPhone(subId).setRadioPower(!isRadioOnForSubscriber(subId));
Wink Saville36469e72014-06-11 15:17:00 -07001064 }
1065
1066 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001067 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001068 }
1069
Wink Savilleb564aae2014-10-23 10:18:09 -07001070 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07001071 enforceModifyPermission();
1072 if ((getPhone(subId).getServiceState().getState() !=
1073 ServiceState.STATE_POWER_OFF) != turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001074 toggleRadioOnOffForSubscriber(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001075 }
1076 return true;
1077 }
Wink Saville36469e72014-06-11 15:17:00 -07001078
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001079 public boolean needMobileRadioShutdown() {
1080 /*
1081 * If any of the Radios are available, it will need to be
1082 * shutdown. So return true if any Radio is available.
1083 */
1084 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1085 Phone phone = PhoneFactory.getPhone(i);
1086 if (phone != null && phone.isRadioAvailable()) return true;
1087 }
1088 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
1089 return false;
1090 }
1091
1092 public void shutdownMobileRadios() {
1093 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1094 logv("Shutting down Phone " + i);
1095 shutdownRadioUsingPhoneId(i);
1096 }
1097 }
1098
1099 private void shutdownRadioUsingPhoneId(int phoneId) {
1100 enforceModifyPermission();
1101 Phone phone = PhoneFactory.getPhone(phoneId);
1102 if (phone != null && phone.isRadioAvailable()) {
1103 phone.shutdownRadio();
1104 }
1105 }
1106
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001107 public boolean setRadioPower(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001108 return setRadioPowerForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001109 }
1110
Wink Savilleb564aae2014-10-23 10:18:09 -07001111 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001112 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001113 getPhone(subId).setRadioPower(turnOn);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001114 return true;
1115 }
1116
Wink Saville36469e72014-06-11 15:17:00 -07001117 // FIXME: subId version needed
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001118 public boolean enableDataConnectivity() {
1119 enforceModifyPermission();
Wink Savilleb564aae2014-10-23 10:18:09 -07001120 int subId = SubscriptionManager.getDefaultDataSubId();
Wink Saville36469e72014-06-11 15:17:00 -07001121 getPhone(subId).setDataEnabled(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001122 return true;
1123 }
1124
Wink Saville36469e72014-06-11 15:17:00 -07001125 // FIXME: subId version needed
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001126 public boolean disableDataConnectivity() {
1127 enforceModifyPermission();
Wink Savilleb564aae2014-10-23 10:18:09 -07001128 int subId = SubscriptionManager.getDefaultDataSubId();
Wink Saville36469e72014-06-11 15:17:00 -07001129 getPhone(subId).setDataEnabled(false);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001130 return true;
1131 }
1132
Wink Saville36469e72014-06-11 15:17:00 -07001133 // FIXME: subId version needed
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001134 public boolean isDataConnectivityPossible() {
Wink Savilleb564aae2014-10-23 10:18:09 -07001135 int subId = SubscriptionManager.getDefaultDataSubId();
Wink Saville36469e72014-06-11 15:17:00 -07001136 return getPhone(subId).isDataConnectivityPossible();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001137 }
1138
1139 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001140 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07001141 }
1142
Wink Savilleb564aae2014-10-23 10:18:09 -07001143 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001144 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001145 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001146 }
1147
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001148 public int getCallState() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001149 return getCallStateForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001150 }
1151
Wink Savilleb564aae2014-10-23 10:18:09 -07001152 public int getCallStateForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001153 return DefaultPhoneNotifier.convertCallState(getPhone(subId).getState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001154 }
1155
1156 public int getDataState() {
Wink Saville36469e72014-06-11 15:17:00 -07001157 Phone phone = getPhone(SubscriptionManager.getDefaultDataSubId());
1158 return DefaultPhoneNotifier.convertDataState(phone.getDataConnectionState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001159 }
1160
1161 public int getDataActivity() {
Wink Saville36469e72014-06-11 15:17:00 -07001162 Phone phone = getPhone(SubscriptionManager.getDefaultDataSubId());
1163 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001164 }
1165
1166 @Override
1167 public Bundle getCellLocation() {
1168 try {
1169 mApp.enforceCallingOrSelfPermission(
1170 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1171 } catch (SecurityException e) {
1172 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1173 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1174 // is the weaker precondition
1175 mApp.enforceCallingOrSelfPermission(
1176 android.Manifest.permission.ACCESS_COARSE_LOCATION, null);
1177 }
1178
Jake Hambye994d462014-02-03 13:10:13 -08001179 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001180 if (DBG_LOC) log("getCellLocation: is active user");
1181 Bundle data = new Bundle();
1182 mPhone.getCellLocation().fillInNotifierBundle(data);
1183 return data;
1184 } else {
1185 if (DBG_LOC) log("getCellLocation: suppress non-active user");
1186 return null;
1187 }
1188 }
1189
1190 @Override
1191 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001192 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001193 }
1194
Wink Savilleb564aae2014-10-23 10:18:09 -07001195 public void enableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001196 mApp.enforceCallingOrSelfPermission(
1197 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Wink Saville36469e72014-06-11 15:17:00 -07001198 getPhone(subId).enableLocationUpdates();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001199 }
1200
1201 @Override
1202 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001203 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001204 }
1205
Wink Savilleb564aae2014-10-23 10:18:09 -07001206 public void disableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001207 mApp.enforceCallingOrSelfPermission(
1208 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Wink Saville36469e72014-06-11 15:17:00 -07001209 getPhone(subId).disableLocationUpdates();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001210 }
1211
1212 @Override
1213 @SuppressWarnings("unchecked")
1214 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
1215 try {
1216 mApp.enforceCallingOrSelfPermission(
1217 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1218 } catch (SecurityException e) {
1219 // If we have ACCESS_FINE_LOCATION permission, skip the check
1220 // for ACCESS_COARSE_LOCATION
1221 // A failure should throw the SecurityException from
1222 // ACCESS_COARSE_LOCATION since this is the weaker precondition
1223 mApp.enforceCallingOrSelfPermission(
1224 android.Manifest.permission.ACCESS_COARSE_LOCATION, null);
1225 }
1226
1227 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
1228 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1229 return null;
1230 }
Jake Hambye994d462014-02-03 13:10:13 -08001231 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001232 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
1233
1234 ArrayList<NeighboringCellInfo> cells = null;
1235
1236 try {
1237 cells = (ArrayList<NeighboringCellInfo>) sendRequest(
Wink Saville36469e72014-06-11 15:17:00 -07001238 CMD_HANDLE_NEIGHBORING_CELL, null, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001239 } catch (RuntimeException e) {
Wink Saville36469e72014-06-11 15:17:00 -07001240 Log.e(LOG_TAG, "getNeighboringCellInfo " + e);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001241 }
1242 return cells;
1243 } else {
1244 if (DBG_LOC) log("getNeighboringCellInfo: suppress non-active user");
1245 return null;
1246 }
1247 }
1248
1249
1250 @Override
1251 public List<CellInfo> getAllCellInfo() {
1252 try {
1253 mApp.enforceCallingOrSelfPermission(
1254 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1255 } catch (SecurityException e) {
1256 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1257 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1258 // is the weaker precondition
1259 mApp.enforceCallingOrSelfPermission(
1260 android.Manifest.permission.ACCESS_COARSE_LOCATION, null);
1261 }
1262
Jake Hambye994d462014-02-03 13:10:13 -08001263 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001264 if (DBG_LOC) log("getAllCellInfo: is active user");
1265 return mPhone.getAllCellInfo();
1266 } else {
1267 if (DBG_LOC) log("getAllCellInfo: suppress non-active user");
1268 return null;
1269 }
1270 }
1271
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001272 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001273 public void setCellInfoListRate(int rateInMillis) {
1274 mPhone.setCellInfoListRate(rateInMillis);
1275 }
1276
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001277 //
1278 // Internal helper methods.
1279 //
1280
Jake Hambye994d462014-02-03 13:10:13 -08001281 private static boolean checkIfCallerIsSelfOrForegroundUser() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001282 boolean ok;
1283
1284 boolean self = Binder.getCallingUid() == Process.myUid();
1285 if (!self) {
1286 // Get the caller's user id then clear the calling identity
1287 // which will be restored in the finally clause.
1288 int callingUser = UserHandle.getCallingUserId();
1289 long ident = Binder.clearCallingIdentity();
1290
1291 try {
1292 // With calling identity cleared the current user is the foreground user.
1293 int foregroundUser = ActivityManager.getCurrentUser();
1294 ok = (foregroundUser == callingUser);
1295 if (DBG_LOC) {
1296 log("checkIfCallerIsSelfOrForegoundUser: foregroundUser=" + foregroundUser
1297 + " callingUser=" + callingUser + " ok=" + ok);
1298 }
1299 } catch (Exception ex) {
1300 if (DBG_LOC) loge("checkIfCallerIsSelfOrForegoundUser: Exception ex=" + ex);
1301 ok = false;
1302 } finally {
1303 Binder.restoreCallingIdentity(ident);
1304 }
1305 } else {
1306 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: is self");
1307 ok = true;
1308 }
1309 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: ret=" + ok);
1310 return ok;
1311 }
1312
1313 /**
1314 * Make sure the caller has the READ_PHONE_STATE permission.
1315 *
1316 * @throws SecurityException if the caller does not have the required permission
1317 */
1318 private void enforceReadPermission() {
1319 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE, null);
1320 }
1321
1322 /**
1323 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1324 *
1325 * @throws SecurityException if the caller does not have the required permission
1326 */
1327 private void enforceModifyPermission() {
1328 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1329 }
1330
1331 /**
Junda Liua2e36012014-07-09 18:30:01 -07001332 * Make sure either system app or the caller has carrier privilege.
1333 *
1334 * @throws SecurityException if the caller does not have the required permission/privilege
1335 */
1336 private void enforceModifyPermissionOrCarrierPrivilege() {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001337 int permission = mApp.checkCallingOrSelfPermission(
1338 android.Manifest.permission.MODIFY_PHONE_STATE);
1339 if (permission == PackageManager.PERMISSION_GRANTED) {
1340 return;
1341 }
1342
1343 log("No modify permission, check carrier privilege next.");
1344 if (hasCarrierPrivileges() != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
1345 loge("No Carrier Privilege.");
1346 throw new SecurityException("No modify permission or carrier privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001347 }
1348 }
1349
1350 /**
1351 * Make sure the caller has carrier privilege.
1352 *
1353 * @throws SecurityException if the caller does not have the required permission
1354 */
1355 private void enforceCarrierPrivilege() {
1356 if (hasCarrierPrivileges() != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001357 loge("No Carrier Privilege.");
1358 throw new SecurityException("No Carrier Privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001359 }
1360 }
1361
1362 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001363 * Make sure the caller has the CALL_PHONE permission.
1364 *
1365 * @throws SecurityException if the caller does not have the required permission
1366 */
1367 private void enforceCallPermission() {
1368 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1369 }
1370
Shishir Agrawal566b7612013-10-28 14:41:00 -07001371 /**
Gabriel Peal36ebb0d2014-03-20 09:20:43 -07001372 * Make sure the caller has the READ_PRIVILEGED_PHONE_STATE permission.
1373 *
1374 * @throws SecurityException if the caller does not have the required permission
1375 */
1376 private void enforcePrivilegedPhoneStatePermission() {
1377 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
1378 null);
1379 }
1380
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001381 private String createTelUrl(String number) {
1382 if (TextUtils.isEmpty(number)) {
1383 return null;
1384 }
1385
Jake Hambye994d462014-02-03 13:10:13 -08001386 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001387 }
1388
Ihab Awadf9e92732013-12-05 18:02:52 -08001389 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001390 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1391 }
1392
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001393 private static void logv(String msg) {
1394 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
1395 }
1396
Ihab Awadf9e92732013-12-05 18:02:52 -08001397 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001398 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1399 }
1400
1401 public int getActivePhoneType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001402 return getActivePhoneTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001403 }
1404
Wink Savilleb564aae2014-10-23 10:18:09 -07001405 public int getActivePhoneTypeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001406 return getPhone(subId).getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001407 }
1408
1409 /**
1410 * Returns the CDMA ERI icon index to display
1411 */
1412 public int getCdmaEriIconIndex() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001413 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001414
1415 }
1416
Wink Savilleb564aae2014-10-23 10:18:09 -07001417 public int getCdmaEriIconIndexForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001418 return getPhone(subId).getCdmaEriIconIndex();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001419 }
1420
1421 /**
1422 * Returns the CDMA ERI icon mode,
1423 * 0 - ON
1424 * 1 - FLASHING
1425 */
1426 public int getCdmaEriIconMode() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001427 return getCdmaEriIconModeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001428 }
1429
Wink Savilleb564aae2014-10-23 10:18:09 -07001430 public int getCdmaEriIconModeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001431 return getPhone(subId).getCdmaEriIconMode();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001432 }
1433
1434 /**
1435 * Returns the CDMA ERI text,
1436 */
1437 public String getCdmaEriText() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001438 return getCdmaEriTextForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001439 }
1440
Wink Savilleb564aae2014-10-23 10:18:09 -07001441 public String getCdmaEriTextForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001442 return getPhone(subId).getCdmaEriText();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001443 }
1444
1445 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07001446 * Returns the CDMA MDN.
1447 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001448 public String getCdmaMdn(int subId) {
Junda Liuca05d5d2014-08-14 22:36:34 -07001449 enforceModifyPermissionOrCarrierPrivilege();
1450 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1451 return getPhone(subId).getLine1Number();
1452 } else {
1453 return null;
1454 }
1455 }
1456
1457 /**
1458 * Returns the CDMA MIN.
1459 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001460 public String getCdmaMin(int subId) {
Junda Liuca05d5d2014-08-14 22:36:34 -07001461 enforceModifyPermissionOrCarrierPrivilege();
1462 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1463 return getPhone(subId).getCdmaMin();
1464 } else {
1465 return null;
1466 }
1467 }
1468
1469 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001470 * Returns true if CDMA provisioning needs to run.
1471 */
1472 public boolean needsOtaServiceProvisioning() {
1473 return mPhone.needsOtaServiceProvisioning();
1474 }
1475
1476 /**
1477 * Returns the unread count of voicemails
1478 */
1479 public int getVoiceMessageCount() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001480 return getVoiceMessageCountForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001481 }
1482
1483 /**
1484 * Returns the unread count of voicemails for a subId
1485 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001486 public int getVoiceMessageCountForSubscriber( int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001487 return getPhone(subId).getVoiceMessageCount();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001488 }
1489
1490 /**
1491 * Returns the data network type
1492 *
1493 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
1494 */
1495 @Override
1496 public int getNetworkType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001497 return getNetworkTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001498 }
1499
1500 /**
1501 * Returns the network type for a subId
1502 */
1503 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001504 public int getNetworkTypeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001505 return getPhone(subId).getServiceState().getDataNetworkType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001506 }
1507
1508 /**
1509 * Returns the data network type
1510 */
1511 @Override
1512 public int getDataNetworkType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001513 return getDataNetworkTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001514 }
1515
1516 /**
1517 * Returns the data network type for a subId
1518 */
1519 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001520 public int getDataNetworkTypeForSubscriber(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 getVoiceNetworkType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001529 return getVoiceNetworkTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001530 }
1531
1532 /**
1533 * Returns the Voice network type for a subId
1534 */
1535 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001536 public int getVoiceNetworkTypeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001537 return getPhone(subId).getServiceState().getVoiceNetworkType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001538 }
1539
1540 /**
1541 * @return true if a ICC card is present
1542 */
1543 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07001544 // FIXME Make changes to pass defaultSimId of type int
1545 return hasIccCardUsingSlotId(getDefaultSubscription());
1546 }
1547
1548 /**
1549 * @return true if a ICC card is present for a slotId
1550 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001551 public boolean hasIccCardUsingSlotId(int slotId) {
Wink Saville36469e72014-06-11 15:17:00 -07001552 return getPhone(slotId).getIccCard().hasIccCard();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001553 }
1554
1555 /**
1556 * Return if the current radio is LTE on CDMA. This
1557 * is a tri-state return value as for a period of time
1558 * the mode may be unknown.
1559 *
1560 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08001561 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001562 */
1563 public int getLteOnCdmaMode() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001564 return getLteOnCdmaModeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001565 }
1566
Wink Savilleb564aae2014-10-23 10:18:09 -07001567 public int getLteOnCdmaModeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001568 return getPhone(subId).getLteOnCdmaMode();
1569 }
1570
1571 public void setPhone(Phone phone) {
1572 mPhone = phone;
1573 }
1574
1575 /**
1576 * {@hide}
1577 * Returns Default subId, 0 in the case of single standby.
1578 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001579 private int getDefaultSubscription() {
Wink Saville36469e72014-06-11 15:17:00 -07001580 return SubscriptionManager.getDefaultSubId();
1581 }
1582
Wink Savilleb564aae2014-10-23 10:18:09 -07001583 private int getPreferredVoiceSubscription() {
Wink Saville36469e72014-06-11 15:17:00 -07001584 return SubscriptionManager.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001585 }
Ihab Awadf2177b72013-11-25 13:33:23 -08001586
1587 /**
1588 * @see android.telephony.TelephonyManager.WifiCallingChoices
1589 */
1590 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08001591 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
1592 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08001593 }
1594
1595 /**
1596 * @see android.telephony.TelephonyManager.WifiCallingChoices
1597 */
1598 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08001599 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
1600 Settings.System.putInt(mPhone.getContext().getContentResolver(),
1601 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08001602 }
1603
Sailesh Nepald1e68152013-12-12 19:08:02 -08001604 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07001605 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08001606 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08001607 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08001608
Shishir Agrawal566b7612013-10-28 14:41:00 -07001609 @Override
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001610 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(String AID) {
Junda Liua2e36012014-07-09 18:30:01 -07001611 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001612
1613 if (DBG) log("iccOpenLogicalChannel: " + AID);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001614 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
1615 CMD_OPEN_CHANNEL, AID);
1616 if (DBG) log("iccOpenLogicalChannel: " + response);
1617 return response;
Shishir Agrawal566b7612013-10-28 14:41:00 -07001618 }
1619
1620 @Override
1621 public boolean iccCloseLogicalChannel(int channel) {
Junda Liua2e36012014-07-09 18:30:01 -07001622 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001623
1624 if (DBG) log("iccCloseLogicalChannel: " + channel);
1625 if (channel < 0) {
1626 return false;
1627 }
Jake Hambye994d462014-02-03 13:10:13 -08001628 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07001629 if (DBG) log("iccCloseLogicalChannel: " + success);
1630 return success;
1631 }
1632
1633 @Override
1634 public String iccTransmitApduLogicalChannel(int channel, int cla,
1635 int command, int p1, int p2, int p3, String data) {
Junda Liua2e36012014-07-09 18:30:01 -07001636 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001637
1638 if (DBG) {
1639 log("iccTransmitApduLogicalChannel: chnl=" + channel + " cla=" + cla +
1640 " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
1641 " data=" + data);
1642 }
1643
1644 if (channel < 0) {
1645 return "";
1646 }
1647
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001648 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Shishir Agrawal566b7612013-10-28 14:41:00 -07001649 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data));
1650 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
1651
Shishir Agrawal566b7612013-10-28 14:41:00 -07001652 // Append the returned status code to the end of the response payload.
1653 String s = Integer.toHexString(
1654 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07001655 if (response.payload != null) {
1656 s = IccUtils.bytesToHexString(response.payload) + s;
1657 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07001658 return s;
1659 }
Jake Hambye994d462014-02-03 13:10:13 -08001660
Evan Charltonc66da362014-05-16 14:06:40 -07001661 @Override
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001662 public String iccTransmitApduBasicChannel(int cla, int command, int p1, int p2,
1663 int p3, String data) {
1664 enforceModifyPermissionOrCarrierPrivilege();
1665
1666 if (DBG) {
1667 log("iccTransmitApduBasicChannel: cla=" + cla + " cmd=" + command + " p1="
1668 + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
1669 }
1670
1671 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
1672 new IccAPDUArgument(0, cla, command, p1, p2, p3, data));
1673 if (DBG) log("iccTransmitApduBasicChannel: " + response);
1674
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001675 // Append the returned status code to the end of the response payload.
1676 String s = Integer.toHexString(
1677 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07001678 if (response.payload != null) {
1679 s = IccUtils.bytesToHexString(response.payload) + s;
1680 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001681 return s;
1682 }
1683
1684 @Override
1685 public byte[] iccExchangeSimIO(int fileID, int command, int p1, int p2, int p3,
1686 String filePath) {
1687 enforceModifyPermissionOrCarrierPrivilege();
1688
1689 if (DBG) {
1690 log("Exchange SIM_IO " + fileID + ":" + command + " " +
1691 p1 + " " + p2 + " " + p3 + ":" + filePath);
1692 }
1693
1694 IccIoResult response =
1695 (IccIoResult)sendRequest(CMD_EXCHANGE_SIM_IO,
Yong Jiang3edf3782014-10-03 13:23:28 -05001696 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath));
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001697
1698 if (DBG) {
1699 log("Exchange SIM_IO [R]" + response);
1700 }
1701
1702 byte[] result = null;
1703 int length = 2;
1704 if (response.payload != null) {
1705 length = 2 + response.payload.length;
1706 result = new byte[length];
1707 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
1708 } else {
1709 result = new byte[length];
1710 }
1711
1712 result[length - 1] = (byte) response.sw2;
1713 result[length - 2] = (byte) response.sw1;
1714 return result;
1715 }
1716
1717 @Override
Evan Charltonc66da362014-05-16 14:06:40 -07001718 public String sendEnvelopeWithStatus(String content) {
Junda Liua2e36012014-07-09 18:30:01 -07001719 enforceModifyPermissionOrCarrierPrivilege();
Evan Charltonc66da362014-05-16 14:06:40 -07001720
1721 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content);
1722 if (response.payload == null) {
1723 return "";
1724 }
1725
1726 // Append the returned status code to the end of the response payload.
1727 String s = Integer.toHexString(
1728 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
1729 s = IccUtils.bytesToHexString(response.payload) + s;
1730 return s;
1731 }
1732
Jake Hambye994d462014-02-03 13:10:13 -08001733 /**
1734 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
1735 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
1736 *
1737 * @param itemID the ID of the item to read
1738 * @return the NV item as a String, or null on error.
1739 */
1740 @Override
1741 public String nvReadItem(int itemID) {
Junda Liua2e36012014-07-09 18:30:01 -07001742 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001743 if (DBG) log("nvReadItem: item " + itemID);
1744 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
1745 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
1746 return value;
1747 }
1748
1749 /**
1750 * Write 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 * @param itemValue the value to write, as a String
1755 * @return true on success; false on any failure
1756 */
1757 @Override
1758 public boolean nvWriteItem(int itemID, String itemValue) {
Junda Liua2e36012014-07-09 18:30:01 -07001759 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001760 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
1761 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
1762 new Pair<Integer, String>(itemID, itemValue));
1763 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
1764 return success;
1765 }
1766
1767 /**
1768 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
1769 * Used for device configuration by some CDMA operators.
1770 *
1771 * @param preferredRoamingList byte array containing the new PRL
1772 * @return true on success; false on any failure
1773 */
1774 @Override
1775 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Junda Liua2e36012014-07-09 18:30:01 -07001776 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001777 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
1778 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
1779 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
1780 return success;
1781 }
1782
1783 /**
1784 * Perform the specified type of NV config reset.
1785 * Used for device configuration by some CDMA operators.
1786 *
1787 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
1788 * @return true on success; false on any failure
1789 */
1790 @Override
1791 public boolean nvResetConfig(int resetType) {
Junda Liua2e36012014-07-09 18:30:01 -07001792 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001793 if (DBG) log("nvResetConfig: type " + resetType);
1794 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
1795 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
1796 return success;
1797 }
Jake Hamby7c27be32014-03-03 13:25:59 -08001798
1799 /**
Wink Saville36469e72014-06-11 15:17:00 -07001800 * {@hide}
1801 * Returns Default sim, 0 in the case of single standby.
1802 */
1803 public int getDefaultSim() {
1804 //TODO Need to get it from Telephony Devcontroller
1805 return 0;
1806 }
1807
ram87fca6f2014-07-18 18:58:44 +05301808 public String[] getPcscfAddress(String apnType) {
Wink Saville36469e72014-06-11 15:17:00 -07001809 enforceReadPermission();
ram87fca6f2014-07-18 18:58:44 +05301810 return mPhone.getPcscfAddress(apnType);
Wink Saville36469e72014-06-11 15:17:00 -07001811 }
1812
1813 public void setImsRegistrationState(boolean registered) {
1814 enforceModifyPermission();
1815 mPhone.setImsRegistrationState(registered);
1816 }
1817
1818 /**
Junda Liu84d15a22014-07-02 11:21:04 -07001819 * Get the calculated preferred network type.
1820 * Used for debugging incorrect network type.
1821 *
1822 * @return the preferred network type, defined in RILConstants.java.
1823 */
1824 @Override
1825 public int getCalculatedPreferredNetworkType() {
1826 enforceReadPermission();
Wink Saville08874612014-08-31 19:19:58 -07001827 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext(), 0); // wink FIXME: need to get PhoneId from somewhere.
Junda Liu84d15a22014-07-02 11:21:04 -07001828 }
1829
1830 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08001831 * Get the preferred network type.
1832 * Used for device configuration by some CDMA operators.
1833 *
1834 * @return the preferred network type, defined in RILConstants.java.
1835 */
1836 @Override
1837 public int getPreferredNetworkType() {
Junda Liua2e36012014-07-09 18:30:01 -07001838 enforceModifyPermissionOrCarrierPrivilege();
Jake Hamby7c27be32014-03-03 13:25:59 -08001839 if (DBG) log("getPreferredNetworkType");
1840 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null);
1841 int networkType = (result != null ? result[0] : -1);
1842 if (DBG) log("getPreferredNetworkType: " + networkType);
1843 return networkType;
1844 }
1845
1846 /**
1847 * Set the preferred network type.
1848 * Used for device configuration by some CDMA operators.
1849 *
1850 * @param networkType the preferred network type, defined in RILConstants.java.
1851 * @return true on success; false on any failure.
1852 */
1853 @Override
1854 public boolean setPreferredNetworkType(int networkType) {
Junda Liua2e36012014-07-09 18:30:01 -07001855 enforceModifyPermissionOrCarrierPrivilege();
Jake Hamby7c27be32014-03-03 13:25:59 -08001856 if (DBG) log("setPreferredNetworkType: type " + networkType);
1857 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType);
1858 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
Junda Liu80bc0d12014-07-14 16:36:44 -07001859 if (success) {
1860 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
1861 Settings.Global.PREFERRED_NETWORK_MODE, networkType);
1862 }
Jake Hamby7c27be32014-03-03 13:25:59 -08001863 return success;
1864 }
Robert Greenwalted86e582014-05-21 20:03:20 -07001865
1866 /**
1867 * Set mobile data enabled
1868 * Used by the user through settings etc to turn on/off mobile data
1869 *
1870 * @param enable {@code true} turn turn data on, else {@code false}
1871 */
1872 @Override
1873 public void setDataEnabled(boolean enable) {
1874 enforceModifyPermission();
1875 mPhone.setDataEnabled(enable);
1876 }
1877
1878 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07001879 * Get whether mobile data is enabled.
1880 *
1881 * Note that this used to be available from ConnectivityService, gated by
1882 * ACCESS_NETWORK_STATE permission, so this will accept either that or
1883 * our MODIFY_PHONE_STATE.
Robert Greenwalted86e582014-05-21 20:03:20 -07001884 *
1885 * @return {@code true} if data is enabled else {@code false}
1886 */
1887 @Override
1888 public boolean getDataEnabled() {
Robert Greenwalt646120a2014-05-23 11:54:03 -07001889 try {
1890 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
1891 null);
1892 } catch (Exception e) {
1893 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE,
1894 null);
1895 }
Robert Greenwalted86e582014-05-21 20:03:20 -07001896 return mPhone.getDataEnabled();
1897 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07001898
1899 @Override
1900 public int hasCarrierPrivileges() {
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07001901 UiccCard card = UiccController.getInstance().getUiccCard();
1902 if (card == null) {
1903 loge("hasCarrierPrivileges: No UICC");
1904 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
1905 }
1906 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001907 mPhone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07001908 }
Junda Liu29340342014-07-10 15:23:27 -07001909
1910 @Override
Shishir Agrawal6d5a2852014-07-11 16:32:57 -07001911 public int checkCarrierPrivilegesForPackage(String pkgname) {
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07001912 UiccCard card = UiccController.getInstance().getUiccCard();
1913 if (card == null) {
1914 loge("checkCarrierPrivilegesForPackage: No UICC");
1915 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
1916 }
1917 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgname);
Junda Liu29340342014-07-10 15:23:27 -07001918 }
Derek Tan89e89d42014-07-08 17:00:10 -07001919
1920 @Override
Diego Pontorieroaf74c862014-08-28 11:51:16 -07001921 public List<String> getCarrierPackageNamesForIntent(Intent intent) {
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07001922 UiccCard card = UiccController.getInstance().getUiccCard();
1923 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07001924 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07001925 return null ;
1926 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07001927 return card.getCarrierPackageNamesForIntent(
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07001928 mPhone.getContext().getPackageManager(), intent);
1929 }
1930
Wink Savilleb564aae2014-10-23 10:18:09 -07001931 private String getIccId(int subId) {
Derek Tan97ebb422014-09-05 16:55:38 -07001932 UiccCard card = getPhone(subId).getUiccCard();
1933 if (card == null) {
1934 loge("getIccId: No UICC");
1935 return null;
1936 }
1937 String iccId = card.getIccId();
1938 if (TextUtils.isEmpty(iccId)) {
1939 loge("getIccId: ICC ID is null or empty.");
1940 return null;
1941 }
1942 return iccId;
1943 }
1944
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07001945 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001946 public void enableSimplifiedNetworkSettingsForSubscriber(int subId, boolean enable) {
Derek Tan352d8cd2014-07-12 12:57:11 -07001947 enforceModifyPermissionOrCarrierPrivilege();
Derek Tan97ebb422014-09-05 16:55:38 -07001948
1949 String iccId = getIccId(subId);
1950 if (iccId != null) {
1951 String snsPrefKey = PREF_CARRIERS_SIMPLIFIED_NETWORK_SETTINGS_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07001952 SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
Derek Tan97ebb422014-09-05 16:55:38 -07001953 if (enable) {
1954 editor.putBoolean(snsPrefKey, true);
1955 } else {
1956 editor.remove(snsPrefKey);
1957 }
1958 editor.commit();
Derek Tan89e89d42014-07-08 17:00:10 -07001959 }
1960 }
1961
1962 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001963 public boolean getSimplifiedNetworkSettingsEnabledForSubscriber(int subId) {
Derek Tan89e89d42014-07-08 17:00:10 -07001964 enforceReadPermission();
Derek Tan97ebb422014-09-05 16:55:38 -07001965 String iccId = getIccId(subId);
1966 if (iccId != null) {
1967 String snsPrefKey = PREF_CARRIERS_SIMPLIFIED_NETWORK_SETTINGS_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07001968 return mTelephonySharedPreferences.getBoolean(snsPrefKey, false);
Derek Tan97ebb422014-09-05 16:55:38 -07001969 }
1970 return false;
Derek Tan89e89d42014-07-08 17:00:10 -07001971 }
Derek Tan7226c842014-07-02 17:42:23 -07001972
1973 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001974 public void setLine1NumberForDisplayForSubscriber(int subId, String alphaTag, String number) {
Derek Tan352d8cd2014-07-12 12:57:11 -07001975 enforceModifyPermissionOrCarrierPrivilege();
Derek Tan97ebb422014-09-05 16:55:38 -07001976
1977 String iccId = getIccId(subId);
1978 if (iccId != null) {
1979 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07001980 SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
Derek Tan97ebb422014-09-05 16:55:38 -07001981 if (alphaTag == null) {
1982 editor.remove(alphaTagPrefKey);
1983 } else {
1984 editor.putString(alphaTagPrefKey, alphaTag);
1985 }
1986
1987 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
1988 if (number == null) {
1989 editor.remove(numberPrefKey);
1990 } else {
1991 editor.putString(numberPrefKey, number);
1992 }
1993 editor.commit();
1994 }
Derek Tan7226c842014-07-02 17:42:23 -07001995 }
1996
1997 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001998 public String getLine1NumberForDisplay(int subId) {
Derek Tan7226c842014-07-02 17:42:23 -07001999 enforceReadPermission();
Derek Tan97ebb422014-09-05 16:55:38 -07002000
2001 String iccId = getIccId(subId);
2002 if (iccId != null) {
2003 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002004 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002005 }
Derek Tan97ebb422014-09-05 16:55:38 -07002006 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002007 }
2008
2009 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002010 public String getLine1AlphaTagForDisplay(int subId) {
Derek Tan7226c842014-07-02 17:42:23 -07002011 enforceReadPermission();
Derek Tan97ebb422014-09-05 16:55:38 -07002012
2013 String iccId = getIccId(subId);
2014 if (iccId != null) {
2015 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002016 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002017 }
Derek Tan97ebb422014-09-05 16:55:38 -07002018 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002019 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002020
2021 @Override
Shishir Agrawala3dfd752014-09-04 13:25:42 -07002022 public boolean setOperatorBrandOverride(String brand) {
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002023 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawala3dfd752014-09-04 13:25:42 -07002024 return mPhone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002025 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05002026
2027 @Override
2028 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
2029 enforceModifyPermission();
2030
2031 int returnValue = 0;
2032 try {
2033 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
2034 if(result.exception == null) {
2035 if (result.result != null) {
2036 byte[] responseData = (byte[])(result.result);
2037 if(responseData.length > oemResp.length) {
2038 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
2039 responseData.length + "bytes. Buffer Size is " +
2040 oemResp.length + "bytes.");
2041 }
2042 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
2043 returnValue = responseData.length;
2044 }
2045 } else {
2046 CommandException ex = (CommandException) result.exception;
2047 returnValue = ex.getCommandError().ordinal();
2048 if(returnValue > 0) returnValue *= -1;
2049 }
2050 } catch (RuntimeException e) {
2051 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
2052 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
2053 if(returnValue > 0) returnValue *= -1;
2054 }
2055
2056 return returnValue;
2057 }
Wink Saville5d475dd2014-10-17 15:00:58 -07002058
2059 @Override
2060 public void setRadioCapability(RadioAccessFamily[] rafs) {
2061 try {
2062 ProxyController.getInstance().setRadioCapability(rafs);
2063 } catch (RuntimeException e) {
2064 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
2065 }
2066 }
2067
2068 @Override
2069 public int getRadioAccessFamily(int phoneId) {
2070 return ProxyController.getInstance().getRadioAccessFamily(phoneId);
2071 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002072
2073 @Override
2074 public void enableVideoCalling(boolean enable) {
2075 enforceModifyPermission();
2076 SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2077 editor.putBoolean(PREF_ENABLE_VIDEO_CALLING, enable);
2078 editor.commit();
2079 }
2080
2081 @Override
2082 public boolean isVideoCallingEnabled() {
2083 enforceReadPermission();
Andrew Lee77527ac2014-10-21 16:57:39 -07002084 // Check the user preference and the system-level IMS setting. Even if the user has
2085 // enabled video calling, if IMS is disabled we aren't able to support video calling.
2086 // In the long run, we may instead need to check if there exists a connection service
2087 // which can support video calling.
Andrew Lee312e8172014-10-23 17:01:36 -07002088 return ImsManager.isVtEnabledByPlatform(mPhone.getContext())
2089 && ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
2090 && mTelephonySharedPreferences.getBoolean(PREF_ENABLE_VIDEO_CALLING, true);
Andrew Leedf14ead2014-10-17 14:22:52 -07002091 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002092}