blob: 2162c36924f77958d2b78468b6e23e3a2a165f07 [file] [log] [blame]
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.phone;
18
19import android.app.ActivityManager;
20import android.app.AppOpsManager;
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -070021import android.content.ComponentName;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070022import android.content.Context;
23import android.content.Intent;
Derek Tan97ebb422014-09-05 16:55:38 -070024import android.content.SharedPreferences;
Shishir Agrawal60f9c952014-06-23 12:00:43 -070025import android.content.pm.PackageManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070026import android.net.Uri;
27import android.os.AsyncResult;
28import android.os.Binder;
29import android.os.Bundle;
30import android.os.Handler;
31import android.os.Looper;
32import android.os.Message;
33import android.os.Process;
34import android.os.ServiceManager;
35import android.os.UserHandle;
Derek Tan97ebb422014-09-05 16:55:38 -070036import android.preference.PreferenceManager;
Ihab Awadf2177b72013-11-25 13:33:23 -080037import android.provider.Settings;
Santos Cordon7a1885b2015-02-03 11:15:19 -080038import android.telecom.PhoneAccount;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070039import android.telephony.CellInfo;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -070040import android.telephony.IccOpenLogicalChannelResponse;
Jake Hambye994d462014-02-03 13:10:13 -080041import android.telephony.NeighboringCellInfo;
Wink Saville5d475dd2014-10-17 15:00:58 -070042import android.telephony.RadioAccessFamily;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070043import android.telephony.ServiceState;
Wink Saville0f3b5fc2014-11-11 08:40:49 -080044import android.telephony.SubscriptionInfo;
Jeff Sharkey85190e62014-12-05 09:40:12 -080045import android.telephony.SubscriptionManager;
Ihab Awadf2177b72013-11-25 13:33:23 -080046import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070047import android.text.TextUtils;
Jeff Sharkey85190e62014-12-05 09:40:12 -080048import android.util.ArrayMap;
49import android.util.ArraySet;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070050import android.util.Log;
Jake Hambye994d462014-02-03 13:10:13 -080051import android.util.Pair;
Jeff Sharkey85190e62014-12-05 09:40:12 -080052import android.util.Slog;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070053
Andrew Lee312e8172014-10-23 17:01:36 -070054import com.android.ims.ImsManager;
Shishir Agrawal566b7612013-10-28 14:41:00 -070055import com.android.internal.telephony.CallManager;
56import com.android.internal.telephony.CommandException;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070057import com.android.internal.telephony.DefaultPhoneNotifier;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070058import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -080059import com.android.internal.telephony.IccCard;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070060import com.android.internal.telephony.Phone;
Wink Saville36469e72014-06-11 15:17:00 -070061import com.android.internal.telephony.PhoneFactory;
Wink Saville5d475dd2014-10-17 15:00:58 -070062import com.android.internal.telephony.ProxyController;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070063import com.android.internal.telephony.PhoneConstants;
Wink Savilleac1bdfd2014-11-20 23:04:44 -080064import com.android.internal.telephony.SubscriptionController;
Shishir Agrawal566b7612013-10-28 14:41:00 -070065import com.android.internal.telephony.uicc.IccIoResult;
66import com.android.internal.telephony.uicc.IccUtils;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -070067import com.android.internal.telephony.uicc.UiccCard;
Shishir Agrawal566b7612013-10-28 14:41:00 -070068import com.android.internal.telephony.uicc.UiccController;
Jake Hambye994d462014-02-03 13:10:13 -080069import com.android.internal.util.HexDump;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070070
Wink Saville36469e72014-06-11 15:17:00 -070071import static com.android.internal.telephony.PhoneConstants.SUBSCRIPTION_KEY;
72
Santos Cordon7d4ddf62013-07-10 11:58:08 -070073import java.util.ArrayList;
Jeff Sharkey85190e62014-12-05 09:40:12 -080074import java.util.Arrays;
Shishir Agrawal621a47c2014-12-01 10:25:09 -080075import java.util.HashMap;
76import java.util.Iterator;
Jake Hambye994d462014-02-03 13:10:13 -080077import java.util.List;
Jeff Sharkey85190e62014-12-05 09:40:12 -080078import java.util.Map;
79import java.util.Objects;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070080
81/**
82 * Implementation of the ITelephony interface.
83 */
Santos Cordon117fee72014-05-16 17:56:12 -070084public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -070085 private static final String LOG_TAG = "PhoneInterfaceManager";
86 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
87 private static final boolean DBG_LOC = false;
Jeff Sharkey85190e62014-12-05 09:40:12 -080088 private static final boolean DBG_MERGE = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070089
90 // Message codes used with mMainThreadHandler
91 private static final int CMD_HANDLE_PIN_MMI = 1;
92 private static final int CMD_HANDLE_NEIGHBORING_CELL = 2;
93 private static final int EVENT_NEIGHBORING_CELL_DONE = 3;
94 private static final int CMD_ANSWER_RINGING_CALL = 4;
95 private static final int CMD_END_CALL = 5; // not used yet
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -070096 private static final int CMD_TRANSMIT_APDU_LOGICAL_CHANNEL = 7;
97 private static final int EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE = 8;
Shishir Agrawal566b7612013-10-28 14:41:00 -070098 private static final int CMD_OPEN_CHANNEL = 9;
99 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
100 private static final int CMD_CLOSE_CHANNEL = 11;
101 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -0800102 private static final int CMD_NV_READ_ITEM = 13;
103 private static final int EVENT_NV_READ_ITEM_DONE = 14;
104 private static final int CMD_NV_WRITE_ITEM = 15;
105 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
106 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
107 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
108 private static final int CMD_NV_RESET_CONFIG = 19;
109 private static final int EVENT_NV_RESET_CONFIG_DONE = 20;
Jake Hamby7c27be32014-03-03 13:25:59 -0800110 private static final int CMD_GET_PREFERRED_NETWORK_TYPE = 21;
111 private static final int EVENT_GET_PREFERRED_NETWORK_TYPE_DONE = 22;
112 private static final int CMD_SET_PREFERRED_NETWORK_TYPE = 23;
113 private static final int EVENT_SET_PREFERRED_NETWORK_TYPE_DONE = 24;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800114 private static final int CMD_SEND_ENVELOPE = 25;
115 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Derek Tan6b088ee2014-09-05 14:15:18 -0700116 private static final int CMD_INVOKE_OEM_RIL_REQUEST_RAW = 27;
117 private static final int EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE = 28;
118 private static final int CMD_TRANSMIT_APDU_BASIC_CHANNEL = 29;
119 private static final int EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE = 30;
120 private static final int CMD_EXCHANGE_SIM_IO = 31;
121 private static final int EVENT_EXCHANGE_SIM_IO_DONE = 32;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800122 private static final int CMD_SET_VOICEMAIL_NUMBER = 33;
123 private static final int EVENT_SET_VOICEMAIL_NUMBER_DONE = 34;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700124
125 /** The singleton instance. */
126 private static PhoneInterfaceManager sInstance;
127
Wink Saville3ab207e2014-11-20 13:07:20 -0800128 private PhoneGlobals mApp;
129 private Phone mPhone;
130 private CallManager mCM;
131 private AppOpsManager mAppOps;
132 private MainThreadHandler mMainThreadHandler;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800133 private SubscriptionController mSubscriptionController;
Wink Saville3ab207e2014-11-20 13:07:20 -0800134 private SharedPreferences mTelephonySharedPreferences;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700135
Derek Tan97ebb422014-09-05 16:55:38 -0700136 private static final String PREF_CARRIERS_ALPHATAG_PREFIX = "carrier_alphtag_";
137 private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
Jeff Sharkey85190e62014-12-05 09:40:12 -0800138 private static final String PREF_CARRIERS_SUBSCRIBER_PREFIX = "carrier_subscriber_";
Andrew Leedf14ead2014-10-17 14:22:52 -0700139 private static final String PREF_ENABLE_VIDEO_CALLING = "enable_video_calling";
Derek Tan89e89d42014-07-08 17:00:10 -0700140
141 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700142 * A request object to use for transmitting data to an ICC.
143 */
144 private static final class IccAPDUArgument {
145 public int channel, cla, command, p1, p2, p3;
146 public String data;
147
148 public IccAPDUArgument(int channel, int cla, int command,
149 int p1, int p2, int p3, String data) {
150 this.channel = channel;
151 this.cla = cla;
152 this.command = command;
153 this.p1 = p1;
154 this.p2 = p2;
155 this.p3 = p3;
156 this.data = data;
157 }
158 }
159
160 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700161 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
162 * request after sending. The main thread will notify the request when it is complete.
163 */
164 private static final class MainThreadRequest {
165 /** The argument to use for the request */
166 public Object argument;
167 /** The result of the request that is run on the main thread */
168 public Object result;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800169 /** The subscriber id that this request applies to. Null if default. */
170 public Integer subId;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700171
172 public MainThreadRequest(Object argument) {
173 this.argument = argument;
174 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800175
176 public MainThreadRequest(Object argument, Integer subId) {
177 this.argument = argument;
178 this.subId = subId;
179 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700180 }
181
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800182 private static final class IncomingThirdPartyCallArgs {
183 public final ComponentName component;
184 public final String callId;
185 public final String callerDisplayName;
186
187 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
188 String callerDisplayName) {
189 this.component = component;
190 this.callId = callId;
191 this.callerDisplayName = callerDisplayName;
192 }
193 }
194
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700195 /**
196 * A handler that processes messages on the main thread in the phone process. Since many
197 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
198 * inbound binder threads to the main thread in the phone process. The Binder thread
199 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
200 * on, which will be notified when the operation completes and will contain the result of the
201 * request.
202 *
203 * <p>If a MainThreadRequest object is provided in the msg.obj field,
204 * note that request.result must be set to something non-null for the calling thread to
205 * unblock.
206 */
207 private final class MainThreadHandler extends Handler {
208 @Override
209 public void handleMessage(Message msg) {
210 MainThreadRequest request;
211 Message onCompleted;
212 AsyncResult ar;
Shishir Agrawal21409252015-01-15 23:33:50 -0800213 UiccCard uiccCard = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700214 IccAPDUArgument iccArgument;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700215
216 switch (msg.what) {
217 case CMD_HANDLE_PIN_MMI:
218 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800219 request.result = getPhoneFromRequest(request).handlePinMmi(
220 (String) request.argument);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700221 // Wake up the requesting thread
222 synchronized (request) {
223 request.notifyAll();
224 }
225 break;
226
227 case CMD_HANDLE_NEIGHBORING_CELL:
228 request = (MainThreadRequest) msg.obj;
229 onCompleted = obtainMessage(EVENT_NEIGHBORING_CELL_DONE,
230 request);
231 mPhone.getNeighboringCids(onCompleted);
232 break;
233
234 case EVENT_NEIGHBORING_CELL_DONE:
235 ar = (AsyncResult) msg.obj;
236 request = (MainThreadRequest) ar.userObj;
237 if (ar.exception == null && ar.result != null) {
238 request.result = ar.result;
239 } else {
240 // create an empty list to notify the waiting thread
Jake Hambye994d462014-02-03 13:10:13 -0800241 request.result = new ArrayList<NeighboringCellInfo>(0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700242 }
243 // Wake up the requesting thread
244 synchronized (request) {
245 request.notifyAll();
246 }
247 break;
248
249 case CMD_ANSWER_RINGING_CALL:
Wink Saville08874612014-08-31 19:19:58 -0700250 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800251 int answer_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700252 answerRingingCallInternal(answer_subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700253 break;
254
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700255 case CMD_END_CALL:
256 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800257 int end_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700258 final boolean hungUp;
259 int phoneType = getPhone(end_subId).getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700260 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
261 // CDMA: If the user presses the Power button we treat it as
262 // ending the complete call session
Wink Saville08874612014-08-31 19:19:58 -0700263 hungUp = PhoneUtils.hangupRingingAndActive(getPhone(end_subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700264 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
265 // GSM: End the call as per the Phone state
266 hungUp = PhoneUtils.hangup(mCM);
267 } else {
268 throw new IllegalStateException("Unexpected phone type: " + phoneType);
269 }
270 if (DBG) log("CMD_END_CALL: " + (hungUp ? "hung up!" : "no call to hang up"));
271 request.result = hungUp;
272 // Wake up the requesting thread
273 synchronized (request) {
274 request.notifyAll();
275 }
276 break;
277
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700278 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700279 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700280 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700281 if (uiccCard == null) {
282 loge("iccTransmitApduLogicalChannel: No UICC");
283 request.result = new IccIoResult(0x6F, 0, (byte[])null);
284 synchronized (request) {
285 request.notifyAll();
286 }
287 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700288 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
289 request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700290 uiccCard.iccTransmitApduLogicalChannel(
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700291 iccArgument.channel, iccArgument.cla, iccArgument.command,
292 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
Shishir Agrawal566b7612013-10-28 14:41:00 -0700293 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700294 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700295 break;
296
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700297 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700298 ar = (AsyncResult) msg.obj;
299 request = (MainThreadRequest) ar.userObj;
300 if (ar.exception == null && ar.result != null) {
301 request.result = ar.result;
302 } else {
303 request.result = new IccIoResult(0x6F, 0, (byte[])null);
304 if (ar.result == null) {
305 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800306 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700307 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800308 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700309 } else {
310 loge("iccTransmitApduLogicalChannel: Unknown exception");
311 }
312 }
313 synchronized (request) {
314 request.notifyAll();
315 }
316 break;
317
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700318 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
319 request = (MainThreadRequest) msg.obj;
320 iccArgument = (IccAPDUArgument) request.argument;
321 if (uiccCard == null) {
322 loge("iccTransmitApduBasicChannel: No UICC");
323 request.result = new IccIoResult(0x6F, 0, (byte[])null);
324 synchronized (request) {
325 request.notifyAll();
326 }
327 } else {
328 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
329 request);
330 uiccCard.iccTransmitApduBasicChannel(
331 iccArgument.cla, iccArgument.command, iccArgument.p1, iccArgument.p2,
332 iccArgument.p3, iccArgument.data, onCompleted);
333 }
334 break;
335
336 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
337 ar = (AsyncResult) msg.obj;
338 request = (MainThreadRequest) ar.userObj;
339 if (ar.exception == null && ar.result != null) {
340 request.result = ar.result;
341 } else {
342 request.result = new IccIoResult(0x6F, 0, (byte[])null);
343 if (ar.result == null) {
344 loge("iccTransmitApduBasicChannel: Empty response");
345 } else if (ar.exception instanceof CommandException) {
346 loge("iccTransmitApduBasicChannel: CommandException: " +
347 ar.exception);
348 } else {
349 loge("iccTransmitApduBasicChannel: Unknown exception");
350 }
351 }
352 synchronized (request) {
353 request.notifyAll();
354 }
355 break;
356
357 case CMD_EXCHANGE_SIM_IO:
358 request = (MainThreadRequest) msg.obj;
359 iccArgument = (IccAPDUArgument) request.argument;
360 if (uiccCard == null) {
361 loge("iccExchangeSimIO: No UICC");
362 request.result = new IccIoResult(0x6F, 0, (byte[])null);
363 synchronized (request) {
364 request.notifyAll();
365 }
366 } else {
367 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
368 request);
369 uiccCard.iccExchangeSimIO(iccArgument.cla, /* fileID */
370 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
371 iccArgument.data, onCompleted);
372 }
373 break;
374
375 case EVENT_EXCHANGE_SIM_IO_DONE:
376 ar = (AsyncResult) msg.obj;
377 request = (MainThreadRequest) ar.userObj;
378 if (ar.exception == null && ar.result != null) {
379 request.result = ar.result;
380 } else {
381 request.result = new IccIoResult(0x6f, 0, (byte[])null);
382 }
383 synchronized (request) {
384 request.notifyAll();
385 }
386 break;
387
Derek Tan4d5e5c12014-02-04 11:54:58 -0800388 case CMD_SEND_ENVELOPE:
389 request = (MainThreadRequest) msg.obj;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700390 if (uiccCard == null) {
391 loge("sendEnvelopeWithStatus: No UICC");
392 request.result = new IccIoResult(0x6F, 0, (byte[])null);
393 synchronized (request) {
394 request.notifyAll();
395 }
396 } else {
397 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
398 uiccCard.sendEnvelopeWithStatus((String)request.argument, onCompleted);
399 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800400 break;
401
402 case EVENT_SEND_ENVELOPE_DONE:
403 ar = (AsyncResult) msg.obj;
404 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700405 if (ar.exception == null && ar.result != null) {
406 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800407 } else {
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700408 request.result = new IccIoResult(0x6F, 0, (byte[])null);
409 if (ar.result == null) {
410 loge("sendEnvelopeWithStatus: Empty response");
411 } else if (ar.exception instanceof CommandException) {
412 loge("sendEnvelopeWithStatus: CommandException: " +
413 ar.exception);
414 } else {
415 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
416 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800417 }
418 synchronized (request) {
419 request.notifyAll();
420 }
421 break;
422
Shishir Agrawal566b7612013-10-28 14:41:00 -0700423 case CMD_OPEN_CHANNEL:
424 request = (MainThreadRequest) msg.obj;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700425 if (uiccCard == null) {
426 loge("iccOpenLogicalChannel: No UICC");
427 request.result = new IccIoResult(0x6F, 0, (byte[])null);
428 synchronized (request) {
429 request.notifyAll();
430 }
431 } else {
432 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
433 uiccCard.iccOpenLogicalChannel((String)request.argument, onCompleted);
434 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700435 break;
436
437 case EVENT_OPEN_CHANNEL_DONE:
438 ar = (AsyncResult) msg.obj;
439 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700440 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700441 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700442 int[] result = (int[]) ar.result;
443 int channelId = result[0];
444 byte[] selectResponse = null;
445 if (result.length > 1) {
446 selectResponse = new byte[result.length - 1];
447 for (int i = 1; i < result.length; ++i) {
448 selectResponse[i - 1] = (byte) result[i];
449 }
450 }
451 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700452 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700453 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700454 if (ar.result == null) {
455 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700456 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700457 if (ar.exception != null) {
458 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
459 }
460
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700461 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700462 if ((ar.exception != null) && (ar.exception instanceof CommandException)) {
463 if (ar.exception.getMessage().compareTo("MISSING_RESOURCE") == 0) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700464 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700465 } else if (ar.exception.getMessage().compareTo("NO_SUCH_ELEMENT") == 0) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700466 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700467 }
468 }
469 openChannelResp = new IccOpenLogicalChannelResponse(
470 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700471 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700472 request.result = openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700473 synchronized (request) {
474 request.notifyAll();
475 }
476 break;
477
478 case CMD_CLOSE_CHANNEL:
479 request = (MainThreadRequest) msg.obj;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700480 if (uiccCard == null) {
481 loge("iccCloseLogicalChannel: No UICC");
482 request.result = new IccIoResult(0x6F, 0, (byte[])null);
483 synchronized (request) {
484 request.notifyAll();
485 }
486 } else {
487 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
488 uiccCard.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
489 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700490 break;
491
492 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800493 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
494 break;
495
496 case CMD_NV_READ_ITEM:
497 request = (MainThreadRequest) msg.obj;
498 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
499 mPhone.nvReadItem((Integer) request.argument, onCompleted);
500 break;
501
502 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700503 ar = (AsyncResult) msg.obj;
504 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800505 if (ar.exception == null && ar.result != null) {
506 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700507 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800508 request.result = "";
509 if (ar.result == null) {
510 loge("nvReadItem: Empty response");
511 } else if (ar.exception instanceof CommandException) {
512 loge("nvReadItem: CommandException: " +
513 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700514 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800515 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700516 }
517 }
518 synchronized (request) {
519 request.notifyAll();
520 }
521 break;
522
Jake Hambye994d462014-02-03 13:10:13 -0800523 case CMD_NV_WRITE_ITEM:
524 request = (MainThreadRequest) msg.obj;
525 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
526 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
527 mPhone.nvWriteItem(idValue.first, idValue.second, onCompleted);
528 break;
529
530 case EVENT_NV_WRITE_ITEM_DONE:
531 handleNullReturnEvent(msg, "nvWriteItem");
532 break;
533
534 case CMD_NV_WRITE_CDMA_PRL:
535 request = (MainThreadRequest) msg.obj;
536 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
537 mPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
538 break;
539
540 case EVENT_NV_WRITE_CDMA_PRL_DONE:
541 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
542 break;
543
544 case CMD_NV_RESET_CONFIG:
545 request = (MainThreadRequest) msg.obj;
546 onCompleted = obtainMessage(EVENT_NV_RESET_CONFIG_DONE, request);
547 mPhone.nvResetConfig((Integer) request.argument, onCompleted);
548 break;
549
550 case EVENT_NV_RESET_CONFIG_DONE:
551 handleNullReturnEvent(msg, "nvResetConfig");
552 break;
553
Jake Hamby7c27be32014-03-03 13:25:59 -0800554 case CMD_GET_PREFERRED_NETWORK_TYPE:
555 request = (MainThreadRequest) msg.obj;
556 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
557 mPhone.getPreferredNetworkType(onCompleted);
558 break;
559
560 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
561 ar = (AsyncResult) msg.obj;
562 request = (MainThreadRequest) ar.userObj;
563 if (ar.exception == null && ar.result != null) {
564 request.result = ar.result; // Integer
565 } else {
566 request.result = -1;
567 if (ar.result == null) {
568 loge("getPreferredNetworkType: Empty response");
569 } else if (ar.exception instanceof CommandException) {
570 loge("getPreferredNetworkType: CommandException: " +
571 ar.exception);
572 } else {
573 loge("getPreferredNetworkType: Unknown exception");
574 }
575 }
576 synchronized (request) {
577 request.notifyAll();
578 }
579 break;
580
581 case CMD_SET_PREFERRED_NETWORK_TYPE:
582 request = (MainThreadRequest) msg.obj;
583 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
584 int networkType = (Integer) request.argument;
585 mPhone.setPreferredNetworkType(networkType, onCompleted);
586 break;
587
588 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
589 handleNullReturnEvent(msg, "setPreferredNetworkType");
590 break;
591
Steven Liu4bf01bc2014-07-17 11:05:29 -0500592 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
593 request = (MainThreadRequest)msg.obj;
594 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
595 mPhone.invokeOemRilRequestRaw((byte[])request.argument, onCompleted);
596 break;
597
598 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
599 ar = (AsyncResult)msg.obj;
600 request = (MainThreadRequest)ar.userObj;
601 request.result = ar;
602 synchronized (request) {
603 request.notifyAll();
604 }
605 break;
606
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800607 case CMD_SET_VOICEMAIL_NUMBER:
608 request = (MainThreadRequest) msg.obj;
609 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
610 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -0800611 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
612 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800613 break;
614
615 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
616 handleNullReturnEvent(msg, "setVoicemailNumber");
617 break;
618
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700619 default:
620 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
621 break;
622 }
623 }
Jake Hambye994d462014-02-03 13:10:13 -0800624
625 private void handleNullReturnEvent(Message msg, String command) {
626 AsyncResult ar = (AsyncResult) msg.obj;
627 MainThreadRequest request = (MainThreadRequest) ar.userObj;
628 if (ar.exception == null) {
629 request.result = true;
630 } else {
631 request.result = false;
632 if (ar.exception instanceof CommandException) {
633 loge(command + ": CommandException: " + ar.exception);
634 } else {
635 loge(command + ": Unknown exception");
636 }
637 }
638 synchronized (request) {
639 request.notifyAll();
640 }
641 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700642 }
643
644 /**
645 * Posts the specified command to be executed on the main thread,
646 * waits for the request to complete, and returns the result.
647 * @see #sendRequestAsync
648 */
649 private Object sendRequest(int command, Object argument) {
Santos Cordon500b0e02014-06-17 10:33:33 -0700650 return sendRequest(command, argument, null);
Wink Saville36469e72014-06-11 15:17:00 -0700651 }
652
653 /**
654 * Posts the specified command to be executed on the main thread,
655 * waits for the request to complete, and returns the result.
656 * @see #sendRequestAsync
657 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800658 private Object sendRequest(int command, Object argument, Integer subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700659 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
660 throw new RuntimeException("This method will deadlock if called from the main thread.");
661 }
662
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800663 MainThreadRequest request = new MainThreadRequest(argument, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700664 Message msg = mMainThreadHandler.obtainMessage(command, request);
665 msg.sendToTarget();
666
667 // Wait for the request to complete
668 synchronized (request) {
669 while (request.result == null) {
670 try {
671 request.wait();
672 } catch (InterruptedException e) {
673 // Do nothing, go back and wait until the request is complete
674 }
675 }
676 }
677 return request.result;
678 }
679
680 /**
681 * Asynchronous ("fire and forget") version of sendRequest():
682 * Posts the specified command to be executed on the main thread, and
683 * returns immediately.
684 * @see #sendRequest
685 */
686 private void sendRequestAsync(int command) {
687 mMainThreadHandler.sendEmptyMessage(command);
688 }
689
690 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -0700691 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
692 * @see {@link #sendRequest(int,Object)}
693 */
694 private void sendRequestAsync(int command, Object argument) {
695 MainThreadRequest request = new MainThreadRequest(argument);
696 Message msg = mMainThreadHandler.obtainMessage(command, request);
697 msg.sendToTarget();
698 }
699
700 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700701 * Initialize the singleton PhoneInterfaceManager instance.
702 * This is only done once, at startup, from PhoneApp.onCreate().
703 */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700704 /* package */ static PhoneInterfaceManager init(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700705 synchronized (PhoneInterfaceManager.class) {
706 if (sInstance == null) {
Sailesh Nepal194161e2014-07-03 08:57:44 -0700707 sInstance = new PhoneInterfaceManager(app, phone);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700708 } else {
709 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
710 }
711 return sInstance;
712 }
713 }
714
715 /** Private constructor; @see init() */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700716 private PhoneInterfaceManager(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700717 mApp = app;
718 mPhone = phone;
719 mCM = PhoneGlobals.getInstance().mCM;
720 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
721 mMainThreadHandler = new MainThreadHandler();
Andrew Leedf14ead2014-10-17 14:22:52 -0700722 mTelephonySharedPreferences =
Derek Tan97ebb422014-09-05 16:55:38 -0700723 PreferenceManager.getDefaultSharedPreferences(mPhone.getContext());
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800724 mSubscriptionController = SubscriptionController.getInstance();
Wink Saville3ab207e2014-11-20 13:07:20 -0800725
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700726 publish();
727 }
728
729 private void publish() {
730 if (DBG) log("publish: " + this);
731
732 ServiceManager.addService("phone", this);
733 }
734
Stuart Scott584921c2015-01-15 17:10:34 -0800735 private Phone getPhoneFromRequest(MainThreadRequest request) {
736 return (request.subId == null) ? mPhone : getPhone(request.subId);
737 }
738
Wink Saville36469e72014-06-11 15:17:00 -0700739 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -0700740 private Phone getPhone(int subId) {
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800741 return PhoneFactory.getPhone(mSubscriptionController.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -0700742 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700743 //
744 // Implementation of the ITelephony interface.
745 //
746
747 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700748 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -0700749 }
750
Wink Savilleb564aae2014-10-23 10:18:09 -0700751 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700752 if (DBG) log("dial: " + number);
753 // No permission check needed here: This is just a wrapper around the
754 // ACTION_DIAL intent, which is available to any app since it puts up
755 // the UI before it does anything.
756
757 String url = createTelUrl(number);
758 if (url == null) {
759 return;
760 }
761
762 // PENDING: should we just silently fail if phone is offhook or ringing?
Wink Saville36469e72014-06-11 15:17:00 -0700763 PhoneConstants.State state = mCM.getState(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700764 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
765 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
766 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
767 mApp.startActivity(intent);
768 }
769 }
770
771 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700772 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -0700773 }
774
Wink Savilleb564aae2014-10-23 10:18:09 -0700775 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700776 if (DBG) log("call: " + number);
777
778 // This is just a wrapper around the ACTION_CALL intent, but we still
779 // need to do a permission check since we're calling startActivity()
780 // from the context of the phone app.
781 enforceCallPermission();
782
783 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
784 != AppOpsManager.MODE_ALLOWED) {
785 return;
786 }
787
788 String url = createTelUrl(number);
789 if (url == null) {
790 return;
791 }
792
Wink Saville08874612014-08-31 19:19:58 -0700793 boolean isValid = false;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800794 List<SubscriptionInfo> slist = mSubscriptionController.getActiveSubscriptionInfoList();
Wink Saville3ab207e2014-11-20 13:07:20 -0800795 if (slist != null) {
796 for (SubscriptionInfo subInfoRecord : slist) {
797 if (subInfoRecord.getSubscriptionId() == subId) {
798 isValid = true;
799 break;
800 }
Wink Saville08874612014-08-31 19:19:58 -0700801 }
802 }
803 if (isValid == false) {
804 return;
805 }
806
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700807 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
Wink Saville36469e72014-06-11 15:17:00 -0700808 intent.putExtra(SUBSCRIPTION_KEY, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700809 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
810 mApp.startActivity(intent);
811 }
812
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700813 /**
814 * End a call based on call state
815 * @return true is a call was ended
816 */
817 public boolean endCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700818 return endCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700819 }
820
821 /**
822 * End a call based on the call state of the subId
823 * @return true is a call was ended
824 */
Wink Savilleb564aae2014-10-23 10:18:09 -0700825 public boolean endCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700826 enforceCallPermission();
Stuart Scott584921c2015-01-15 17:10:34 -0800827 return (Boolean) sendRequest(CMD_END_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700828 }
829
830 public void answerRingingCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700831 answerRingingCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700832 }
833
Wink Savilleb564aae2014-10-23 10:18:09 -0700834 public void answerRingingCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700835 if (DBG) log("answerRingingCall...");
836 // TODO: there should eventually be a separate "ANSWER_PHONE" permission,
837 // but that can probably wait till the big TelephonyManager API overhaul.
838 // For now, protect this call with the MODIFY_PHONE_STATE permission.
839 enforceModifyPermission();
Stuart Scott584921c2015-01-15 17:10:34 -0800840 sendRequest(CMD_ANSWER_RINGING_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700841 }
842
843 /**
844 * Make the actual telephony calls to implement answerRingingCall().
845 * This should only be called from the main thread of the Phone app.
846 * @see #answerRingingCall
847 *
848 * TODO: it would be nice to return true if we answered the call, or
849 * false if there wasn't actually a ringing incoming call, or some
850 * other error occurred. (In other words, pass back the return value
851 * from PhoneUtils.answerCall() or PhoneUtils.answerAndEndActive().)
852 * But that would require calling this method via sendRequest() rather
853 * than sendRequestAsync(), and right now we don't actually *need* that
854 * return value, so let's just return void for now.
855 */
Wink Savilleb564aae2014-10-23 10:18:09 -0700856 private void answerRingingCallInternal(int subId) {
Wink Saville08874612014-08-31 19:19:58 -0700857 final boolean hasRingingCall = !getPhone(subId).getRingingCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700858 if (hasRingingCall) {
Wink Saville08874612014-08-31 19:19:58 -0700859 final boolean hasActiveCall = !getPhone(subId).getForegroundCall().isIdle();
860 final boolean hasHoldingCall = !getPhone(subId).getBackgroundCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700861 if (hasActiveCall && hasHoldingCall) {
862 // Both lines are in use!
863 // TODO: provide a flag to let the caller specify what
864 // policy to use if both lines are in use. (The current
865 // behavior is hardwired to "answer incoming, end ongoing",
866 // which is how the CALL button is specced to behave.)
867 PhoneUtils.answerAndEndActive(mCM, mCM.getFirstActiveRingingCall());
868 return;
869 } else {
870 // answerCall() will automatically hold the current active
871 // call, if there is one.
872 PhoneUtils.answerCall(mCM.getFirstActiveRingingCall());
873 return;
874 }
875 } else {
876 // No call was ringing.
877 return;
878 }
879 }
880
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700881 /**
Santos Cordon5422a8d2014-09-12 04:20:56 -0700882 * This method is no longer used and can be removed once TelephonyManager stops referring to it.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700883 */
Santos Cordon5422a8d2014-09-12 04:20:56 -0700884 public void silenceRinger() {
885 Log.e(LOG_TAG, "silenseRinger not supported");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700886 }
887
888 public boolean isOffhook() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700889 return isOffhookForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700890 }
891
Wink Savilleb564aae2014-10-23 10:18:09 -0700892 public boolean isOffhookForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -0700893 return (getPhone(subId).getState() == PhoneConstants.State.OFFHOOK);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700894 }
895
896 public boolean isRinging() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700897 return (isRingingForSubscriber(getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -0700898 }
899
Wink Savilleb564aae2014-10-23 10:18:09 -0700900 public boolean isRingingForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -0700901 return (getPhone(subId).getState() == PhoneConstants.State.RINGING);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700902 }
903
904 public boolean isIdle() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700905 return isIdleForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700906 }
907
Wink Savilleb564aae2014-10-23 10:18:09 -0700908 public boolean isIdleForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -0700909 return (getPhone(subId).getState() == PhoneConstants.State.IDLE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700910 }
911
912 public boolean isSimPinEnabled() {
913 enforceReadPermission();
914 return (PhoneGlobals.getInstance().isSimPinEnabled());
915 }
916
917 public boolean supplyPin(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700918 return supplyPinForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -0700919 }
920
Wink Savilleb564aae2014-10-23 10:18:09 -0700921 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700922 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -0700923 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
924 }
925
926 public boolean supplyPuk(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700927 return supplyPukForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -0700928 }
929
Wink Savilleb564aae2014-10-23 10:18:09 -0700930 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700931 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -0700932 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
933 }
934
935 /** {@hide} */
936 public int[] supplyPinReportResult(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700937 return supplyPinReportResultForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -0700938 }
939
Wink Savilleb564aae2014-10-23 10:18:09 -0700940 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700941 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -0700942 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700943 checkSimPin.start();
944 return checkSimPin.unlockSim(null, pin);
945 }
946
Wink Saville9de0f752013-10-22 19:04:03 -0700947 /** {@hide} */
948 public int[] supplyPukReportResult(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700949 return supplyPukReportResultForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -0700950 }
951
Wink Savilleb564aae2014-10-23 10:18:09 -0700952 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700953 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -0700954 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700955 checkSimPuk.start();
956 return checkSimPuk.unlockSim(puk, pin);
957 }
958
959 /**
Wink Saville9de0f752013-10-22 19:04:03 -0700960 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700961 * a synchronous one.
962 */
963 private static class UnlockSim extends Thread {
964
965 private final IccCard mSimCard;
966
967 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -0700968 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
969 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700970
971 // For replies from SimCard interface
972 private Handler mHandler;
973
974 // For async handler to identify request type
975 private static final int SUPPLY_PIN_COMPLETE = 100;
976
977 public UnlockSim(IccCard simCard) {
978 mSimCard = simCard;
979 }
980
981 @Override
982 public void run() {
983 Looper.prepare();
984 synchronized (UnlockSim.this) {
985 mHandler = new Handler() {
986 @Override
987 public void handleMessage(Message msg) {
988 AsyncResult ar = (AsyncResult) msg.obj;
989 switch (msg.what) {
990 case SUPPLY_PIN_COMPLETE:
991 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
992 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -0700993 mRetryCount = msg.arg1;
994 if (ar.exception != null) {
995 if (ar.exception instanceof CommandException &&
996 ((CommandException)(ar.exception)).getCommandError()
997 == CommandException.Error.PASSWORD_INCORRECT) {
998 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
999 } else {
1000 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1001 }
1002 } else {
1003 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
1004 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001005 mDone = true;
1006 UnlockSim.this.notifyAll();
1007 }
1008 break;
1009 }
1010 }
1011 };
1012 UnlockSim.this.notifyAll();
1013 }
1014 Looper.loop();
1015 }
1016
1017 /*
1018 * Use PIN or PUK to unlock SIM card
1019 *
1020 * If PUK is null, unlock SIM card with PIN
1021 *
1022 * If PUK is not null, unlock SIM card with PUK and set PIN code
1023 */
Wink Saville9de0f752013-10-22 19:04:03 -07001024 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001025
1026 while (mHandler == null) {
1027 try {
1028 wait();
1029 } catch (InterruptedException e) {
1030 Thread.currentThread().interrupt();
1031 }
1032 }
1033 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
1034
1035 if (puk == null) {
1036 mSimCard.supplyPin(pin, callback);
1037 } else {
1038 mSimCard.supplyPuk(puk, pin, callback);
1039 }
1040
1041 while (!mDone) {
1042 try {
1043 Log.d(LOG_TAG, "wait for done");
1044 wait();
1045 } catch (InterruptedException e) {
1046 // Restore the interrupted status
1047 Thread.currentThread().interrupt();
1048 }
1049 }
1050 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07001051 int[] resultArray = new int[2];
1052 resultArray[0] = mResult;
1053 resultArray[1] = mRetryCount;
1054 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001055 }
1056 }
1057
1058 public void updateServiceLocation() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001059 updateServiceLocationForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001060
1061 }
1062
Wink Savilleb564aae2014-10-23 10:18:09 -07001063 public void updateServiceLocationForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001064 // No permission check needed here: this call is harmless, and it's
1065 // needed for the ServiceState.requestStateUpdate() call (which is
1066 // already intentionally exposed to 3rd parties.)
Wink Saville36469e72014-06-11 15:17:00 -07001067 getPhone(subId).updateServiceLocation();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001068 }
1069
1070 public boolean isRadioOn() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001071 return isRadioOnForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001072 }
1073
Wink Savilleb564aae2014-10-23 10:18:09 -07001074 public boolean isRadioOnForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001075 return getPhone(subId).getServiceState().getState() != ServiceState.STATE_POWER_OFF;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001076 }
1077
1078 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001079 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001080
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001081 }
Wink Saville36469e72014-06-11 15:17:00 -07001082
Wink Savilleb564aae2014-10-23 10:18:09 -07001083 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001084 enforceModifyPermission();
Wink Savilleadd7cc52014-09-08 14:23:09 -07001085 getPhone(subId).setRadioPower(!isRadioOnForSubscriber(subId));
Wink Saville36469e72014-06-11 15:17:00 -07001086 }
1087
1088 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001089 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001090 }
1091
Wink Savilleb564aae2014-10-23 10:18:09 -07001092 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07001093 enforceModifyPermission();
1094 if ((getPhone(subId).getServiceState().getState() !=
1095 ServiceState.STATE_POWER_OFF) != turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001096 toggleRadioOnOffForSubscriber(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001097 }
1098 return true;
1099 }
Wink Saville36469e72014-06-11 15:17:00 -07001100
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001101 public boolean needMobileRadioShutdown() {
1102 /*
1103 * If any of the Radios are available, it will need to be
1104 * shutdown. So return true if any Radio is available.
1105 */
1106 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1107 Phone phone = PhoneFactory.getPhone(i);
1108 if (phone != null && phone.isRadioAvailable()) return true;
1109 }
1110 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
1111 return false;
1112 }
1113
1114 public void shutdownMobileRadios() {
1115 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1116 logv("Shutting down Phone " + i);
1117 shutdownRadioUsingPhoneId(i);
1118 }
1119 }
1120
1121 private void shutdownRadioUsingPhoneId(int phoneId) {
1122 enforceModifyPermission();
1123 Phone phone = PhoneFactory.getPhone(phoneId);
1124 if (phone != null && phone.isRadioAvailable()) {
1125 phone.shutdownRadio();
1126 }
1127 }
1128
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001129 public boolean setRadioPower(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001130 return setRadioPowerForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001131 }
1132
Wink Savilleb564aae2014-10-23 10:18:09 -07001133 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001134 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001135 getPhone(subId).setRadioPower(turnOn);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001136 return true;
1137 }
1138
Wink Saville36469e72014-06-11 15:17:00 -07001139 // FIXME: subId version needed
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001140 public boolean enableDataConnectivity() {
1141 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001142 int subId = mSubscriptionController.getDefaultDataSubId();
Wink Saville36469e72014-06-11 15:17:00 -07001143 getPhone(subId).setDataEnabled(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001144 return true;
1145 }
1146
Wink Saville36469e72014-06-11 15:17:00 -07001147 // FIXME: subId version needed
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001148 public boolean disableDataConnectivity() {
1149 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001150 int subId = mSubscriptionController.getDefaultDataSubId();
Wink Saville36469e72014-06-11 15:17:00 -07001151 getPhone(subId).setDataEnabled(false);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001152 return true;
1153 }
1154
Wink Saville36469e72014-06-11 15:17:00 -07001155 // FIXME: subId version needed
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001156 public boolean isDataConnectivityPossible() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001157 int subId = mSubscriptionController.getDefaultDataSubId();
Wink Saville36469e72014-06-11 15:17:00 -07001158 return getPhone(subId).isDataConnectivityPossible();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001159 }
1160
1161 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001162 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07001163 }
1164
Wink Savilleb564aae2014-10-23 10:18:09 -07001165 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001166 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001167 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001168 }
1169
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001170 public int getCallState() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001171 return getCallStateForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001172 }
1173
Wink Savilleb564aae2014-10-23 10:18:09 -07001174 public int getCallStateForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001175 return DefaultPhoneNotifier.convertCallState(getPhone(subId).getState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001176 }
1177
1178 public int getDataState() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001179 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Wink Saville36469e72014-06-11 15:17:00 -07001180 return DefaultPhoneNotifier.convertDataState(phone.getDataConnectionState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001181 }
1182
1183 public int getDataActivity() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001184 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Wink Saville36469e72014-06-11 15:17:00 -07001185 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001186 }
1187
1188 @Override
1189 public Bundle getCellLocation() {
1190 try {
1191 mApp.enforceCallingOrSelfPermission(
1192 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1193 } catch (SecurityException e) {
1194 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1195 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1196 // is the weaker precondition
1197 mApp.enforceCallingOrSelfPermission(
1198 android.Manifest.permission.ACCESS_COARSE_LOCATION, null);
1199 }
1200
Jake Hambye994d462014-02-03 13:10:13 -08001201 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001202 if (DBG_LOC) log("getCellLocation: is active user");
1203 Bundle data = new Bundle();
Legler Wu2c01cdf2014-12-08 19:00:59 +08001204 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
1205 phone.getCellLocation().fillInNotifierBundle(data);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001206 return data;
1207 } else {
1208 if (DBG_LOC) log("getCellLocation: suppress non-active user");
1209 return null;
1210 }
1211 }
1212
1213 @Override
1214 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001215 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001216 }
1217
Wink Savilleb564aae2014-10-23 10:18:09 -07001218 public void enableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001219 mApp.enforceCallingOrSelfPermission(
1220 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Wink Saville36469e72014-06-11 15:17:00 -07001221 getPhone(subId).enableLocationUpdates();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001222 }
1223
1224 @Override
1225 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001226 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001227 }
1228
Wink Savilleb564aae2014-10-23 10:18:09 -07001229 public void disableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001230 mApp.enforceCallingOrSelfPermission(
1231 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Wink Saville36469e72014-06-11 15:17:00 -07001232 getPhone(subId).disableLocationUpdates();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001233 }
1234
1235 @Override
1236 @SuppressWarnings("unchecked")
1237 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
1238 try {
1239 mApp.enforceCallingOrSelfPermission(
1240 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1241 } catch (SecurityException e) {
1242 // If we have ACCESS_FINE_LOCATION permission, skip the check
1243 // for ACCESS_COARSE_LOCATION
1244 // A failure should throw the SecurityException from
1245 // ACCESS_COARSE_LOCATION since this is the weaker precondition
1246 mApp.enforceCallingOrSelfPermission(
1247 android.Manifest.permission.ACCESS_COARSE_LOCATION, null);
1248 }
1249
1250 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
1251 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1252 return null;
1253 }
Jake Hambye994d462014-02-03 13:10:13 -08001254 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001255 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
1256
1257 ArrayList<NeighboringCellInfo> cells = null;
1258
1259 try {
1260 cells = (ArrayList<NeighboringCellInfo>) sendRequest(
Wink Saville36469e72014-06-11 15:17:00 -07001261 CMD_HANDLE_NEIGHBORING_CELL, null, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001262 } catch (RuntimeException e) {
Wink Saville36469e72014-06-11 15:17:00 -07001263 Log.e(LOG_TAG, "getNeighboringCellInfo " + e);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001264 }
1265 return cells;
1266 } else {
1267 if (DBG_LOC) log("getNeighboringCellInfo: suppress non-active user");
1268 return null;
1269 }
1270 }
1271
1272
1273 @Override
1274 public List<CellInfo> getAllCellInfo() {
1275 try {
1276 mApp.enforceCallingOrSelfPermission(
1277 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1278 } catch (SecurityException e) {
1279 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1280 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1281 // is the weaker precondition
1282 mApp.enforceCallingOrSelfPermission(
1283 android.Manifest.permission.ACCESS_COARSE_LOCATION, null);
1284 }
1285
Jake Hambye994d462014-02-03 13:10:13 -08001286 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001287 if (DBG_LOC) log("getAllCellInfo: is active user");
Legler Wu2c01cdf2014-12-08 19:00:59 +08001288 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
1289 for (Phone phone : PhoneFactory.getPhones()) {
1290 cellInfos.addAll(phone.getAllCellInfo());
1291 }
1292 return cellInfos;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001293 } else {
1294 if (DBG_LOC) log("getAllCellInfo: suppress non-active user");
1295 return null;
1296 }
1297 }
1298
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001299 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001300 public void setCellInfoListRate(int rateInMillis) {
1301 mPhone.setCellInfoListRate(rateInMillis);
1302 }
1303
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001304 //
1305 // Internal helper methods.
1306 //
1307
Jake Hambye994d462014-02-03 13:10:13 -08001308 private static boolean checkIfCallerIsSelfOrForegroundUser() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001309 boolean ok;
1310
1311 boolean self = Binder.getCallingUid() == Process.myUid();
1312 if (!self) {
1313 // Get the caller's user id then clear the calling identity
1314 // which will be restored in the finally clause.
1315 int callingUser = UserHandle.getCallingUserId();
1316 long ident = Binder.clearCallingIdentity();
1317
1318 try {
1319 // With calling identity cleared the current user is the foreground user.
1320 int foregroundUser = ActivityManager.getCurrentUser();
1321 ok = (foregroundUser == callingUser);
1322 if (DBG_LOC) {
1323 log("checkIfCallerIsSelfOrForegoundUser: foregroundUser=" + foregroundUser
1324 + " callingUser=" + callingUser + " ok=" + ok);
1325 }
1326 } catch (Exception ex) {
1327 if (DBG_LOC) loge("checkIfCallerIsSelfOrForegoundUser: Exception ex=" + ex);
1328 ok = false;
1329 } finally {
1330 Binder.restoreCallingIdentity(ident);
1331 }
1332 } else {
1333 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: is self");
1334 ok = true;
1335 }
1336 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: ret=" + ok);
1337 return ok;
1338 }
1339
1340 /**
1341 * Make sure the caller has the READ_PHONE_STATE permission.
1342 *
1343 * @throws SecurityException if the caller does not have the required permission
1344 */
1345 private void enforceReadPermission() {
1346 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE, null);
1347 }
1348
1349 /**
1350 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1351 *
1352 * @throws SecurityException if the caller does not have the required permission
1353 */
1354 private void enforceModifyPermission() {
1355 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1356 }
1357
1358 /**
Junda Liua2e36012014-07-09 18:30:01 -07001359 * Make sure either system app or the caller has carrier privilege.
1360 *
1361 * @throws SecurityException if the caller does not have the required permission/privilege
1362 */
1363 private void enforceModifyPermissionOrCarrierPrivilege() {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001364 int permission = mApp.checkCallingOrSelfPermission(
1365 android.Manifest.permission.MODIFY_PHONE_STATE);
1366 if (permission == PackageManager.PERMISSION_GRANTED) {
1367 return;
1368 }
1369
1370 log("No modify permission, check carrier privilege next.");
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08001371 if (getCarrierPrivilegeStatus() != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001372 loge("No Carrier Privilege.");
1373 throw new SecurityException("No modify permission or carrier privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001374 }
1375 }
1376
1377 /**
1378 * Make sure the caller has carrier privilege.
1379 *
1380 * @throws SecurityException if the caller does not have the required permission
1381 */
1382 private void enforceCarrierPrivilege() {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08001383 if (getCarrierPrivilegeStatus() != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001384 loge("No Carrier Privilege.");
1385 throw new SecurityException("No Carrier Privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001386 }
1387 }
1388
1389 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001390 * Make sure the caller has the CALL_PHONE permission.
1391 *
1392 * @throws SecurityException if the caller does not have the required permission
1393 */
1394 private void enforceCallPermission() {
1395 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1396 }
1397
Shishir Agrawal566b7612013-10-28 14:41:00 -07001398 /**
Gabriel Peal36ebb0d2014-03-20 09:20:43 -07001399 * Make sure the caller has the READ_PRIVILEGED_PHONE_STATE permission.
1400 *
1401 * @throws SecurityException if the caller does not have the required permission
1402 */
1403 private void enforcePrivilegedPhoneStatePermission() {
1404 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
1405 null);
1406 }
1407
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001408 private String createTelUrl(String number) {
1409 if (TextUtils.isEmpty(number)) {
1410 return null;
1411 }
1412
Jake Hambye994d462014-02-03 13:10:13 -08001413 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001414 }
1415
Ihab Awadf9e92732013-12-05 18:02:52 -08001416 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001417 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1418 }
1419
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001420 private static void logv(String msg) {
1421 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
1422 }
1423
Ihab Awadf9e92732013-12-05 18:02:52 -08001424 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001425 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1426 }
1427
1428 public int getActivePhoneType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001429 return getActivePhoneTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001430 }
1431
Wink Savilleb564aae2014-10-23 10:18:09 -07001432 public int getActivePhoneTypeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001433 return getPhone(subId).getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001434 }
1435
1436 /**
1437 * Returns the CDMA ERI icon index to display
1438 */
1439 public int getCdmaEriIconIndex() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001440 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001441
1442 }
1443
Wink Savilleb564aae2014-10-23 10:18:09 -07001444 public int getCdmaEriIconIndexForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001445 return getPhone(subId).getCdmaEriIconIndex();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001446 }
1447
1448 /**
1449 * Returns the CDMA ERI icon mode,
1450 * 0 - ON
1451 * 1 - FLASHING
1452 */
1453 public int getCdmaEriIconMode() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001454 return getCdmaEriIconModeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001455 }
1456
Wink Savilleb564aae2014-10-23 10:18:09 -07001457 public int getCdmaEriIconModeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001458 return getPhone(subId).getCdmaEriIconMode();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001459 }
1460
1461 /**
1462 * Returns the CDMA ERI text,
1463 */
1464 public String getCdmaEriText() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001465 return getCdmaEriTextForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001466 }
1467
Wink Savilleb564aae2014-10-23 10:18:09 -07001468 public String getCdmaEriTextForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001469 return getPhone(subId).getCdmaEriText();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001470 }
1471
1472 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07001473 * Returns the CDMA MDN.
1474 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001475 public String getCdmaMdn(int subId) {
Junda Liuca05d5d2014-08-14 22:36:34 -07001476 enforceModifyPermissionOrCarrierPrivilege();
1477 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1478 return getPhone(subId).getLine1Number();
1479 } else {
1480 return null;
1481 }
1482 }
1483
1484 /**
1485 * Returns the CDMA MIN.
1486 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001487 public String getCdmaMin(int subId) {
Junda Liuca05d5d2014-08-14 22:36:34 -07001488 enforceModifyPermissionOrCarrierPrivilege();
1489 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1490 return getPhone(subId).getCdmaMin();
1491 } else {
1492 return null;
1493 }
1494 }
1495
1496 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001497 * Returns true if CDMA provisioning needs to run.
1498 */
1499 public boolean needsOtaServiceProvisioning() {
1500 return mPhone.needsOtaServiceProvisioning();
1501 }
1502
1503 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001504 * Sets the voice mail number of a given subId.
1505 */
1506 @Override
1507 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08001508 enforceCarrierPrivilege();
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001509 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
1510 new Pair<String, String>(alphaTag, number), new Integer(subId));
1511 return success;
1512 }
1513
1514 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001515 * Returns the unread count of voicemails
1516 */
1517 public int getVoiceMessageCount() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001518 return getVoiceMessageCountForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001519 }
1520
1521 /**
1522 * Returns the unread count of voicemails for a subId
1523 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001524 public int getVoiceMessageCountForSubscriber( int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001525 return getPhone(subId).getVoiceMessageCount();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001526 }
1527
1528 /**
1529 * Returns the data network type
1530 *
1531 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
1532 */
1533 @Override
1534 public int getNetworkType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001535 return getNetworkTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001536 }
1537
1538 /**
1539 * Returns the network type for a subId
1540 */
1541 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001542 public int getNetworkTypeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001543 return getPhone(subId).getServiceState().getDataNetworkType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001544 }
1545
1546 /**
1547 * Returns the data network type
1548 */
1549 @Override
1550 public int getDataNetworkType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001551 return getDataNetworkTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001552 }
1553
1554 /**
1555 * Returns the data network type for a subId
1556 */
1557 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001558 public int getDataNetworkTypeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001559 return getPhone(subId).getServiceState().getDataNetworkType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001560 }
1561
1562 /**
1563 * Returns the data network type
1564 */
1565 @Override
1566 public int getVoiceNetworkType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001567 return getVoiceNetworkTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001568 }
1569
1570 /**
1571 * Returns the Voice network type for a subId
1572 */
1573 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001574 public int getVoiceNetworkTypeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001575 return getPhone(subId).getServiceState().getVoiceNetworkType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001576 }
1577
1578 /**
1579 * @return true if a ICC card is present
1580 */
1581 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07001582 // FIXME Make changes to pass defaultSimId of type int
Amit Mahajana6fc2a82015-01-06 11:53:51 -08001583 return hasIccCardUsingSlotId(mSubscriptionController.getSlotId(getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07001584 }
1585
1586 /**
1587 * @return true if a ICC card is present for a slotId
1588 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001589 public boolean hasIccCardUsingSlotId(int slotId) {
Amit Mahajana6fc2a82015-01-06 11:53:51 -08001590 int subId[] = mSubscriptionController.getSubIdUsingSlotId(slotId);
1591 if (subId != null) {
1592 return getPhone(subId[0]).getIccCard().hasIccCard();
1593 } else {
1594 return false;
1595 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001596 }
1597
1598 /**
1599 * Return if the current radio is LTE on CDMA. This
1600 * is a tri-state return value as for a period of time
1601 * the mode may be unknown.
1602 *
1603 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08001604 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001605 */
1606 public int getLteOnCdmaMode() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001607 return getLteOnCdmaModeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001608 }
1609
Wink Savilleb564aae2014-10-23 10:18:09 -07001610 public int getLteOnCdmaModeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001611 return getPhone(subId).getLteOnCdmaMode();
1612 }
1613
1614 public void setPhone(Phone phone) {
1615 mPhone = phone;
1616 }
1617
1618 /**
1619 * {@hide}
1620 * Returns Default subId, 0 in the case of single standby.
1621 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001622 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001623 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07001624 }
1625
Wink Savilleb564aae2014-10-23 10:18:09 -07001626 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001627 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001628 }
Ihab Awadf2177b72013-11-25 13:33:23 -08001629
1630 /**
1631 * @see android.telephony.TelephonyManager.WifiCallingChoices
1632 */
1633 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08001634 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
1635 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08001636 }
1637
1638 /**
1639 * @see android.telephony.TelephonyManager.WifiCallingChoices
1640 */
1641 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08001642 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
1643 Settings.System.putInt(mPhone.getContext().getContentResolver(),
1644 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08001645 }
1646
Sailesh Nepald1e68152013-12-12 19:08:02 -08001647 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07001648 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08001649 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08001650 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08001651
Shishir Agrawal566b7612013-10-28 14:41:00 -07001652 @Override
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001653 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(String AID) {
Junda Liua2e36012014-07-09 18:30:01 -07001654 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001655
1656 if (DBG) log("iccOpenLogicalChannel: " + AID);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001657 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
1658 CMD_OPEN_CHANNEL, AID);
1659 if (DBG) log("iccOpenLogicalChannel: " + response);
1660 return response;
Shishir Agrawal566b7612013-10-28 14:41:00 -07001661 }
1662
1663 @Override
1664 public boolean iccCloseLogicalChannel(int channel) {
Junda Liua2e36012014-07-09 18:30:01 -07001665 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001666
1667 if (DBG) log("iccCloseLogicalChannel: " + channel);
1668 if (channel < 0) {
1669 return false;
1670 }
Jake Hambye994d462014-02-03 13:10:13 -08001671 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07001672 if (DBG) log("iccCloseLogicalChannel: " + success);
1673 return success;
1674 }
1675
1676 @Override
1677 public String iccTransmitApduLogicalChannel(int channel, int cla,
1678 int command, int p1, int p2, int p3, String data) {
Junda Liua2e36012014-07-09 18:30:01 -07001679 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001680
1681 if (DBG) {
1682 log("iccTransmitApduLogicalChannel: chnl=" + channel + " cla=" + cla +
1683 " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
1684 " data=" + data);
1685 }
1686
1687 if (channel < 0) {
1688 return "";
1689 }
1690
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001691 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Shishir Agrawal566b7612013-10-28 14:41:00 -07001692 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data));
1693 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
1694
Shishir Agrawal566b7612013-10-28 14:41:00 -07001695 // Append the returned status code to the end of the response payload.
1696 String s = Integer.toHexString(
1697 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07001698 if (response.payload != null) {
1699 s = IccUtils.bytesToHexString(response.payload) + s;
1700 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07001701 return s;
1702 }
Jake Hambye994d462014-02-03 13:10:13 -08001703
Evan Charltonc66da362014-05-16 14:06:40 -07001704 @Override
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001705 public String iccTransmitApduBasicChannel(int cla, int command, int p1, int p2,
1706 int p3, String data) {
1707 enforceModifyPermissionOrCarrierPrivilege();
1708
1709 if (DBG) {
1710 log("iccTransmitApduBasicChannel: cla=" + cla + " cmd=" + command + " p1="
1711 + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
1712 }
1713
1714 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
1715 new IccAPDUArgument(0, cla, command, p1, p2, p3, data));
1716 if (DBG) log("iccTransmitApduBasicChannel: " + response);
1717
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001718 // Append the returned status code to the end of the response payload.
1719 String s = Integer.toHexString(
1720 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07001721 if (response.payload != null) {
1722 s = IccUtils.bytesToHexString(response.payload) + s;
1723 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001724 return s;
1725 }
1726
1727 @Override
1728 public byte[] iccExchangeSimIO(int fileID, int command, int p1, int p2, int p3,
1729 String filePath) {
1730 enforceModifyPermissionOrCarrierPrivilege();
1731
1732 if (DBG) {
1733 log("Exchange SIM_IO " + fileID + ":" + command + " " +
1734 p1 + " " + p2 + " " + p3 + ":" + filePath);
1735 }
1736
1737 IccIoResult response =
1738 (IccIoResult)sendRequest(CMD_EXCHANGE_SIM_IO,
Yong Jiang3edf3782014-10-03 13:23:28 -05001739 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath));
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001740
1741 if (DBG) {
1742 log("Exchange SIM_IO [R]" + response);
1743 }
1744
1745 byte[] result = null;
1746 int length = 2;
1747 if (response.payload != null) {
1748 length = 2 + response.payload.length;
1749 result = new byte[length];
1750 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
1751 } else {
1752 result = new byte[length];
1753 }
1754
1755 result[length - 1] = (byte) response.sw2;
1756 result[length - 2] = (byte) response.sw1;
1757 return result;
1758 }
1759
1760 @Override
Evan Charltonc66da362014-05-16 14:06:40 -07001761 public String sendEnvelopeWithStatus(String content) {
Junda Liua2e36012014-07-09 18:30:01 -07001762 enforceModifyPermissionOrCarrierPrivilege();
Evan Charltonc66da362014-05-16 14:06:40 -07001763
1764 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content);
1765 if (response.payload == null) {
1766 return "";
1767 }
1768
1769 // Append the returned status code to the end of the response payload.
1770 String s = Integer.toHexString(
1771 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
1772 s = IccUtils.bytesToHexString(response.payload) + s;
1773 return s;
1774 }
1775
Jake Hambye994d462014-02-03 13:10:13 -08001776 /**
1777 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
1778 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
1779 *
1780 * @param itemID the ID of the item to read
1781 * @return the NV item as a String, or null on error.
1782 */
1783 @Override
1784 public String nvReadItem(int itemID) {
Junda Liua2e36012014-07-09 18:30:01 -07001785 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001786 if (DBG) log("nvReadItem: item " + itemID);
1787 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
1788 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
1789 return value;
1790 }
1791
1792 /**
1793 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
1794 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
1795 *
1796 * @param itemID the ID of the item to read
1797 * @param itemValue the value to write, as a String
1798 * @return true on success; false on any failure
1799 */
1800 @Override
1801 public boolean nvWriteItem(int itemID, String itemValue) {
Junda Liua2e36012014-07-09 18:30:01 -07001802 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001803 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
1804 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
1805 new Pair<Integer, String>(itemID, itemValue));
1806 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
1807 return success;
1808 }
1809
1810 /**
1811 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
1812 * Used for device configuration by some CDMA operators.
1813 *
1814 * @param preferredRoamingList byte array containing the new PRL
1815 * @return true on success; false on any failure
1816 */
1817 @Override
1818 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Junda Liua2e36012014-07-09 18:30:01 -07001819 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001820 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
1821 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
1822 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
1823 return success;
1824 }
1825
1826 /**
1827 * Perform the specified type of NV config reset.
1828 * Used for device configuration by some CDMA operators.
1829 *
1830 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
1831 * @return true on success; false on any failure
1832 */
1833 @Override
1834 public boolean nvResetConfig(int resetType) {
Junda Liua2e36012014-07-09 18:30:01 -07001835 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001836 if (DBG) log("nvResetConfig: type " + resetType);
1837 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
1838 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
1839 return success;
1840 }
Jake Hamby7c27be32014-03-03 13:25:59 -08001841
1842 /**
Wink Saville36469e72014-06-11 15:17:00 -07001843 * {@hide}
1844 * Returns Default sim, 0 in the case of single standby.
1845 */
1846 public int getDefaultSim() {
1847 //TODO Need to get it from Telephony Devcontroller
1848 return 0;
1849 }
1850
ram87fca6f2014-07-18 18:58:44 +05301851 public String[] getPcscfAddress(String apnType) {
Wink Saville36469e72014-06-11 15:17:00 -07001852 enforceReadPermission();
ram87fca6f2014-07-18 18:58:44 +05301853 return mPhone.getPcscfAddress(apnType);
Wink Saville36469e72014-06-11 15:17:00 -07001854 }
1855
1856 public void setImsRegistrationState(boolean registered) {
1857 enforceModifyPermission();
1858 mPhone.setImsRegistrationState(registered);
1859 }
1860
1861 /**
Junda Liu84d15a22014-07-02 11:21:04 -07001862 * Get the calculated preferred network type.
1863 * Used for debugging incorrect network type.
1864 *
1865 * @return the preferred network type, defined in RILConstants.java.
1866 */
1867 @Override
1868 public int getCalculatedPreferredNetworkType() {
1869 enforceReadPermission();
Amit Mahajan43330e02014-11-18 11:54:45 -08001870 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext(), 0); // wink FIXME: need to get SubId from somewhere.
Junda Liu84d15a22014-07-02 11:21:04 -07001871 }
1872
1873 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08001874 * Get the preferred network type.
1875 * Used for device configuration by some CDMA operators.
1876 *
1877 * @return the preferred network type, defined in RILConstants.java.
1878 */
1879 @Override
1880 public int getPreferredNetworkType() {
Junda Liua2e36012014-07-09 18:30:01 -07001881 enforceModifyPermissionOrCarrierPrivilege();
Jake Hamby7c27be32014-03-03 13:25:59 -08001882 if (DBG) log("getPreferredNetworkType");
1883 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null);
1884 int networkType = (result != null ? result[0] : -1);
1885 if (DBG) log("getPreferredNetworkType: " + networkType);
1886 return networkType;
1887 }
1888
1889 /**
1890 * Set the preferred network type.
1891 * Used for device configuration by some CDMA operators.
1892 *
1893 * @param networkType the preferred network type, defined in RILConstants.java.
1894 * @return true on success; false on any failure.
1895 */
1896 @Override
1897 public boolean setPreferredNetworkType(int networkType) {
Junda Liua2e36012014-07-09 18:30:01 -07001898 enforceModifyPermissionOrCarrierPrivilege();
PauloftheWest3c6ce5e2014-11-03 07:09:47 -08001899 final int phoneSubId = mPhone.getSubId();
Jake Hamby7c27be32014-03-03 13:25:59 -08001900 if (DBG) log("setPreferredNetworkType: type " + networkType);
1901 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType);
1902 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
Junda Liu80bc0d12014-07-14 16:36:44 -07001903 if (success) {
1904 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
PauloftheWest3c6ce5e2014-11-03 07:09:47 -08001905 Settings.Global.PREFERRED_NETWORK_MODE + phoneSubId, networkType);
Junda Liu80bc0d12014-07-14 16:36:44 -07001906 }
Jake Hamby7c27be32014-03-03 13:25:59 -08001907 return success;
1908 }
Robert Greenwalted86e582014-05-21 20:03:20 -07001909
1910 /**
Junda Liu475951f2014-11-07 16:45:03 -08001911 * Check TETHER_DUN_REQUIRED and TETHER_DUN_APN settings, net.tethering.noprovisioning
1912 * SystemProperty, and config_tether_apndata to decide whether DUN APN is required for
1913 * tethering.
1914 *
1915 * @return 0: Not required. 1: required. 2: Not set.
1916 * @hide
1917 */
1918 @Override
1919 public int getTetherApnRequired() {
1920 enforceModifyPermissionOrCarrierPrivilege();
1921 int dunRequired = Settings.Global.getInt(mPhone.getContext().getContentResolver(),
1922 Settings.Global.TETHER_DUN_REQUIRED, 2);
1923 // If not set, check net.tethering.noprovisioning, TETHER_DUN_APN setting and
1924 // config_tether_apndata.
1925 if (dunRequired == 2 && mPhone.hasMatchedTetherApnSetting()) {
1926 dunRequired = 1;
1927 }
1928 return dunRequired;
1929 }
1930
1931 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07001932 * Set mobile data enabled
1933 * Used by the user through settings etc to turn on/off mobile data
1934 *
1935 * @param enable {@code true} turn turn data on, else {@code false}
1936 */
1937 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08001938 public void setDataEnabled(int subId, boolean enable) {
Robert Greenwalted86e582014-05-21 20:03:20 -07001939 enforceModifyPermission();
Wink Savillee7353bb2014-12-05 14:21:41 -08001940 int phoneId = mSubscriptionController.getPhoneId(subId);
1941 log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
1942 Phone phone = PhoneFactory.getPhone(phoneId);
1943 if (phone != null) {
1944 log("setDataEnabled: subId=" + subId + " enable=" + enable);
1945 phone.setDataEnabled(enable);
1946 } else {
1947 loge("setDataEnabled: no phone for subId=" + subId);
1948 }
Robert Greenwalted86e582014-05-21 20:03:20 -07001949 }
1950
1951 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07001952 * Get whether mobile data is enabled.
1953 *
1954 * Note that this used to be available from ConnectivityService, gated by
1955 * ACCESS_NETWORK_STATE permission, so this will accept either that or
1956 * our MODIFY_PHONE_STATE.
Robert Greenwalted86e582014-05-21 20:03:20 -07001957 *
1958 * @return {@code true} if data is enabled else {@code false}
1959 */
1960 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08001961 public boolean getDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07001962 try {
1963 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
1964 null);
1965 } catch (Exception e) {
1966 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE,
1967 null);
1968 }
Wink Savillee7353bb2014-12-05 14:21:41 -08001969 int phoneId = mSubscriptionController.getPhoneId(subId);
1970 log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
1971 Phone phone = PhoneFactory.getPhone(phoneId);
1972 if (phone != null) {
1973 boolean retVal = phone.getDataEnabled();
1974 log("getDataEnabled: subId=" + subId + " retVal=" + retVal);
1975 return retVal;
1976 } else {
1977 loge("getDataEnabled: no phone subId=" + subId + " retVal=false");
1978 return false;
1979 }
Robert Greenwalted86e582014-05-21 20:03:20 -07001980 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07001981
1982 @Override
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08001983 public int getCarrierPrivilegeStatus() {
Shishir Agrawal21409252015-01-15 23:33:50 -08001984 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07001985 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08001986 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07001987 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
1988 }
1989 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001990 mPhone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07001991 }
Junda Liu29340342014-07-10 15:23:27 -07001992
1993 @Override
Shishir Agrawal6d5a2852014-07-11 16:32:57 -07001994 public int checkCarrierPrivilegesForPackage(String pkgname) {
Shishir Agrawal21409252015-01-15 23:33:50 -08001995 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07001996 if (card == null) {
1997 loge("checkCarrierPrivilegesForPackage: No UICC");
1998 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
1999 }
2000 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgname);
Junda Liu29340342014-07-10 15:23:27 -07002001 }
Derek Tan89e89d42014-07-08 17:00:10 -07002002
2003 @Override
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002004 public List<String> getCarrierPackageNamesForIntent(Intent intent) {
Shishir Agrawal21409252015-01-15 23:33:50 -08002005 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002006 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002007 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002008 return null ;
2009 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002010 return card.getCarrierPackageNamesForIntent(
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002011 mPhone.getContext().getPackageManager(), intent);
2012 }
2013
Wink Savilleb564aae2014-10-23 10:18:09 -07002014 private String getIccId(int subId) {
Derek Tan97ebb422014-09-05 16:55:38 -07002015 UiccCard card = getPhone(subId).getUiccCard();
2016 if (card == null) {
2017 loge("getIccId: No UICC");
2018 return null;
2019 }
2020 String iccId = card.getIccId();
2021 if (TextUtils.isEmpty(iccId)) {
2022 loge("getIccId: ICC ID is null or empty.");
2023 return null;
2024 }
2025 return iccId;
2026 }
2027
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002028 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002029 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
2030 String number) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002031 enforceCarrierPrivilege();
Derek Tan97ebb422014-09-05 16:55:38 -07002032
Jeff Sharkey85190e62014-12-05 09:40:12 -08002033 final String iccId = getIccId(subId);
2034 final String subscriberId = getPhone(subId).getSubscriberId();
2035
2036 if (DBG_MERGE) {
2037 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
2038 + subscriberId + " to " + number);
2039 }
2040
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002041 if (TextUtils.isEmpty(iccId)) {
2042 return false;
Derek Tan97ebb422014-09-05 16:55:38 -07002043 }
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002044
Jeff Sharkey85190e62014-12-05 09:40:12 -08002045 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2046
2047 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002048 if (alphaTag == null) {
2049 editor.remove(alphaTagPrefKey);
2050 } else {
2051 editor.putString(alphaTagPrefKey, alphaTag);
2052 }
2053
Jeff Sharkey85190e62014-12-05 09:40:12 -08002054 // Record both the line number and IMSI for this ICCID, since we need to
2055 // track all merged IMSIs based on line number
2056 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2057 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002058 if (number == null) {
2059 editor.remove(numberPrefKey);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002060 editor.remove(subscriberPrefKey);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002061 } else {
2062 editor.putString(numberPrefKey, number);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002063 editor.putString(subscriberPrefKey, subscriberId);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002064 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002065
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002066 editor.commit();
2067 return true;
Derek Tan7226c842014-07-02 17:42:23 -07002068 }
2069
2070 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002071 public String getLine1NumberForDisplay(int subId) {
Derek Tan7226c842014-07-02 17:42:23 -07002072 enforceReadPermission();
Derek Tan97ebb422014-09-05 16:55:38 -07002073
2074 String iccId = getIccId(subId);
2075 if (iccId != null) {
2076 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002077 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002078 }
Derek Tan97ebb422014-09-05 16:55:38 -07002079 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002080 }
2081
2082 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002083 public String getLine1AlphaTagForDisplay(int subId) {
Derek Tan7226c842014-07-02 17:42:23 -07002084 enforceReadPermission();
Derek Tan97ebb422014-09-05 16:55:38 -07002085
2086 String iccId = getIccId(subId);
2087 if (iccId != null) {
2088 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002089 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002090 }
Derek Tan97ebb422014-09-05 16:55:38 -07002091 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002092 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002093
2094 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002095 public String[] getMergedSubscriberIds() {
2096 final Context context = mPhone.getContext();
2097 final TelephonyManager tele = TelephonyManager.from(context);
2098 final SubscriptionManager sub = SubscriptionManager.from(context);
2099
2100 // Figure out what subscribers are currently active
2101 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
2102 final int[] subIds = sub.getActiveSubscriptionIdList();
2103 for (int subId : subIds) {
2104 activeSubscriberIds.add(tele.getSubscriberId(subId));
2105 }
2106
2107 // First pass, find a number override for an active subscriber
2108 String mergeNumber = null;
2109 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
2110 for (String key : prefs.keySet()) {
2111 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
2112 final String subscriberId = (String) prefs.get(key);
2113 if (activeSubscriberIds.contains(subscriberId)) {
2114 final String iccId = key.substring(PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
2115 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2116 mergeNumber = (String) prefs.get(numberKey);
2117 if (DBG_MERGE) {
2118 Slog.d(LOG_TAG, "Found line number " + mergeNumber
2119 + " for active subscriber " + subscriberId);
2120 }
2121 if (!TextUtils.isEmpty(mergeNumber)) {
2122 break;
2123 }
2124 }
2125 }
2126 }
2127
2128 // Shortcut when no active merged subscribers
2129 if (TextUtils.isEmpty(mergeNumber)) {
2130 return null;
2131 }
2132
2133 // Second pass, find all subscribers under that line override
2134 final ArraySet<String> result = new ArraySet<>();
2135 for (String key : prefs.keySet()) {
2136 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
2137 final String number = (String) prefs.get(key);
2138 if (mergeNumber.equals(number)) {
2139 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
2140 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
2141 final String subscriberId = (String) prefs.get(subscriberKey);
2142 if (!TextUtils.isEmpty(subscriberId)) {
2143 result.add(subscriberId);
2144 }
2145 }
2146 }
2147 }
2148
2149 final String[] resultArray = result.toArray(new String[result.size()]);
2150 Arrays.sort(resultArray);
2151 if (DBG_MERGE) {
2152 Slog.d(LOG_TAG, "Found subscribers " + Arrays.toString(resultArray) + " after merge");
2153 }
2154 return resultArray;
2155 }
2156
2157 @Override
Shishir Agrawala3dfd752014-09-04 13:25:42 -07002158 public boolean setOperatorBrandOverride(String brand) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002159 enforceCarrierPrivilege();
Shishir Agrawala3dfd752014-09-04 13:25:42 -07002160 return mPhone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002161 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05002162
2163 @Override
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002164 public boolean setRoamingOverride(List<String> gsmRoamingList,
2165 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
2166 List<String> cdmaNonRoamingList) {
2167 enforceCarrierPrivilege();
2168 return mPhone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
2169 cdmaNonRoamingList);
2170 }
2171
2172 @Override
Steven Liu4bf01bc2014-07-17 11:05:29 -05002173 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
2174 enforceModifyPermission();
2175
2176 int returnValue = 0;
2177 try {
2178 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
2179 if(result.exception == null) {
2180 if (result.result != null) {
2181 byte[] responseData = (byte[])(result.result);
2182 if(responseData.length > oemResp.length) {
2183 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
2184 responseData.length + "bytes. Buffer Size is " +
2185 oemResp.length + "bytes.");
2186 }
2187 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
2188 returnValue = responseData.length;
2189 }
2190 } else {
2191 CommandException ex = (CommandException) result.exception;
2192 returnValue = ex.getCommandError().ordinal();
2193 if(returnValue > 0) returnValue *= -1;
2194 }
2195 } catch (RuntimeException e) {
2196 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
2197 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
2198 if(returnValue > 0) returnValue *= -1;
2199 }
2200
2201 return returnValue;
2202 }
Wink Saville5d475dd2014-10-17 15:00:58 -07002203
2204 @Override
2205 public void setRadioCapability(RadioAccessFamily[] rafs) {
2206 try {
2207 ProxyController.getInstance().setRadioCapability(rafs);
2208 } catch (RuntimeException e) {
2209 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
2210 }
2211 }
2212
2213 @Override
2214 public int getRadioAccessFamily(int phoneId) {
2215 return ProxyController.getInstance().getRadioAccessFamily(phoneId);
2216 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002217
2218 @Override
2219 public void enableVideoCalling(boolean enable) {
2220 enforceModifyPermission();
2221 SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2222 editor.putBoolean(PREF_ENABLE_VIDEO_CALLING, enable);
2223 editor.commit();
2224 }
2225
2226 @Override
2227 public boolean isVideoCallingEnabled() {
2228 enforceReadPermission();
Andrew Lee77527ac2014-10-21 16:57:39 -07002229 // Check the user preference and the system-level IMS setting. Even if the user has
2230 // enabled video calling, if IMS is disabled we aren't able to support video calling.
2231 // In the long run, we may instead need to check if there exists a connection service
2232 // which can support video calling.
Andrew Lee312e8172014-10-23 17:01:36 -07002233 return ImsManager.isVtEnabledByPlatform(mPhone.getContext())
2234 && ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
2235 && mTelephonySharedPreferences.getBoolean(PREF_ENABLE_VIDEO_CALLING, true);
Andrew Leedf14ead2014-10-17 14:22:52 -07002236 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002237
Andrew Leea1239f22015-03-02 17:44:07 -08002238 @Override
2239 public boolean canChangeDtmfToneLength() {
2240 return mPhone.getContext().getResources().getBoolean(R.bool.dtmf_type_enabled);
2241 }
2242
2243 @Override
2244 public boolean isWorldPhone() {
2245 return mPhone.getContext().getResources().getBoolean(R.bool.world_phone);
2246 }
2247
Sanket Padawe7310cc72015-01-14 09:53:20 -08002248 /**
2249 * Returns the unique device ID of phone, for example, the IMEI for
2250 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
2251 *
2252 * <p>Requires Permission:
2253 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
2254 */
2255 @Override
2256 public String getDeviceId() {
2257 enforceReadPermission();
2258 final Phone phone = PhoneFactory.getPhone(0);
2259 if (phone != null) {
2260 return phone.getDeviceId();
2261 } else {
2262 return null;
2263 }
2264 }
2265
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002266 /*
2267 * {@hide}
2268 * Returns the IMS Registration Status
2269 */
Santos Cordon7a1885b2015-02-03 11:15:19 -08002270 @Override
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002271 public boolean isImsRegistered() {
2272 return mPhone.isImsRegistered();
2273 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08002274
2275 @Override
2276 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
2277 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
2278 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002279}