blob: f92e7e4ac40e74ae93b9826a98671caab064dab3 [file] [log] [blame]
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.phone;
18
19import android.app.ActivityManager;
20import android.app.AppOpsManager;
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -070021import android.content.ComponentName;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070022import android.content.Context;
23import android.content.Intent;
Derek Tan97ebb422014-09-05 16:55:38 -070024import android.content.SharedPreferences;
Shishir Agrawal60f9c952014-06-23 12:00:43 -070025import android.content.pm.PackageInfo;
26import android.content.pm.PackageManager;
27import android.content.pm.Signature;
Wink Saville36469e72014-06-11 15:17:00 -070028import android.net.ConnectivityManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070029import android.net.Uri;
30import android.os.AsyncResult;
31import android.os.Binder;
32import android.os.Bundle;
33import android.os.Handler;
Gabriel Peal36ebb0d2014-03-20 09:20:43 -070034import android.os.IBinder;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070035import android.os.Looper;
36import android.os.Message;
37import android.os.Process;
Gabriel Peal36ebb0d2014-03-20 09:20:43 -070038import android.os.RemoteException;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070039import android.os.ServiceManager;
Wink Saville08874612014-08-31 19:19:58 -070040import android.os.SystemProperties;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070041import android.os.UserHandle;
Derek Tan97ebb422014-09-05 16:55:38 -070042import android.preference.PreferenceManager;
Ihab Awadf2177b72013-11-25 13:33:23 -080043import android.provider.Settings;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070044import android.telephony.CellInfo;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -070045import android.telephony.IccOpenLogicalChannelResponse;
Jake Hambye994d462014-02-03 13:10:13 -080046import android.telephony.NeighboringCellInfo;
Wink Saville5d475dd2014-10-17 15:00:58 -070047import android.telephony.RadioAccessFamily;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070048import android.telephony.ServiceState;
Wink Saville36469e72014-06-11 15:17:00 -070049import android.telephony.SubscriptionManager;
Wink Saville0f3b5fc2014-11-11 08:40:49 -080050import android.telephony.SubscriptionInfo;
Ihab Awadf2177b72013-11-25 13:33:23 -080051import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070052import android.text.TextUtils;
53import android.util.Log;
Jake Hambye994d462014-02-03 13:10:13 -080054import android.util.Pair;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070055
Andrew Lee312e8172014-10-23 17:01:36 -070056import com.android.ims.ImsManager;
Shishir Agrawal566b7612013-10-28 14:41:00 -070057import com.android.internal.telephony.CallManager;
58import com.android.internal.telephony.CommandException;
Gabriel Peal36ebb0d2014-03-20 09:20:43 -070059import com.android.internal.telephony.Connection;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070060import com.android.internal.telephony.DefaultPhoneNotifier;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070061import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -080062import com.android.internal.telephony.IccCard;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070063import com.android.internal.telephony.Phone;
Wink Saville36469e72014-06-11 15:17:00 -070064import com.android.internal.telephony.PhoneFactory;
Wink Saville5d475dd2014-10-17 15:00:58 -070065import com.android.internal.telephony.PhoneProxy;
66import com.android.internal.telephony.ProxyController;
Wink Saville36469e72014-06-11 15:17:00 -070067import com.android.internal.telephony.CallManager;
68import com.android.internal.telephony.CommandException;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070069import com.android.internal.telephony.PhoneConstants;
Wink Saville08874612014-08-31 19:19:58 -070070import com.android.internal.telephony.TelephonyIntents;
Wink Saville36469e72014-06-11 15:17:00 -070071import com.android.internal.telephony.dataconnection.DctController;
Derek Tan7226c842014-07-02 17:42:23 -070072import com.android.internal.telephony.uicc.AdnRecord;
Shishir Agrawal566b7612013-10-28 14:41:00 -070073import com.android.internal.telephony.uicc.IccIoResult;
74import com.android.internal.telephony.uicc.IccUtils;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -070075import com.android.internal.telephony.uicc.UiccCard;
Shishir Agrawal60f9c952014-06-23 12:00:43 -070076import com.android.internal.telephony.uicc.UiccCarrierPrivilegeRules;
Shishir Agrawal566b7612013-10-28 14:41:00 -070077import com.android.internal.telephony.uicc.UiccController;
Jake Hambye994d462014-02-03 13:10:13 -080078import com.android.internal.util.HexDump;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070079
Wink Saville36469e72014-06-11 15:17:00 -070080import static com.android.internal.telephony.PhoneConstants.SUBSCRIPTION_KEY;
81
Santos Cordon7d4ddf62013-07-10 11:58:08 -070082import java.util.ArrayList;
Gabriel Peal36ebb0d2014-03-20 09:20:43 -070083import java.util.HashMap;
Derek Tan89e89d42014-07-08 17:00:10 -070084import java.util.HashSet;
Gabriel Peal36ebb0d2014-03-20 09:20:43 -070085import java.util.Iterator;
Jake Hambye994d462014-02-03 13:10:13 -080086import java.util.List;
Gabriel Peal36ebb0d2014-03-20 09:20:43 -070087import java.util.Map;
Derek Tan89e89d42014-07-08 17:00:10 -070088import java.util.Set;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070089
90/**
91 * Implementation of the ITelephony interface.
92 */
Santos Cordon117fee72014-05-16 17:56:12 -070093public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -070094 private static final String LOG_TAG = "PhoneInterfaceManager";
95 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
96 private static final boolean DBG_LOC = false;
97
98 // Message codes used with mMainThreadHandler
99 private static final int CMD_HANDLE_PIN_MMI = 1;
100 private static final int CMD_HANDLE_NEIGHBORING_CELL = 2;
101 private static final int EVENT_NEIGHBORING_CELL_DONE = 3;
102 private static final int CMD_ANSWER_RINGING_CALL = 4;
103 private static final int CMD_END_CALL = 5; // not used yet
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700104 private static final int CMD_TRANSMIT_APDU_LOGICAL_CHANNEL = 7;
105 private static final int EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE = 8;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700106 private static final int CMD_OPEN_CHANNEL = 9;
107 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
108 private static final int CMD_CLOSE_CHANNEL = 11;
109 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -0800110 private static final int CMD_NV_READ_ITEM = 13;
111 private static final int EVENT_NV_READ_ITEM_DONE = 14;
112 private static final int CMD_NV_WRITE_ITEM = 15;
113 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
114 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
115 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
116 private static final int CMD_NV_RESET_CONFIG = 19;
117 private static final int EVENT_NV_RESET_CONFIG_DONE = 20;
Jake Hamby7c27be32014-03-03 13:25:59 -0800118 private static final int CMD_GET_PREFERRED_NETWORK_TYPE = 21;
119 private static final int EVENT_GET_PREFERRED_NETWORK_TYPE_DONE = 22;
120 private static final int CMD_SET_PREFERRED_NETWORK_TYPE = 23;
121 private static final int EVENT_SET_PREFERRED_NETWORK_TYPE_DONE = 24;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800122 private static final int CMD_SEND_ENVELOPE = 25;
123 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Derek Tan6b088ee2014-09-05 14:15:18 -0700124 private static final int CMD_INVOKE_OEM_RIL_REQUEST_RAW = 27;
125 private static final int EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE = 28;
126 private static final int CMD_TRANSMIT_APDU_BASIC_CHANNEL = 29;
127 private static final int EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE = 30;
128 private static final int CMD_EXCHANGE_SIM_IO = 31;
129 private static final int EVENT_EXCHANGE_SIM_IO_DONE = 32;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800130 private static final int CMD_SET_VOICEMAIL_NUMBER = 33;
131 private static final int EVENT_SET_VOICEMAIL_NUMBER_DONE = 34;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700132
133 /** The singleton instance. */
134 private static PhoneInterfaceManager sInstance;
135
136 PhoneGlobals mApp;
137 Phone mPhone;
138 CallManager mCM;
139 AppOpsManager mAppOps;
140 MainThreadHandler mMainThreadHandler;
141
Andrew Leedf14ead2014-10-17 14:22:52 -0700142 SharedPreferences mTelephonySharedPreferences;
Derek Tan97ebb422014-09-05 16:55:38 -0700143 private static final String PREF_CARRIERS_ALPHATAG_PREFIX = "carrier_alphtag_";
144 private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
Andrew Leedf14ead2014-10-17 14:22:52 -0700145 private static final String PREF_ENABLE_VIDEO_CALLING = "enable_video_calling";
Derek Tan89e89d42014-07-08 17:00:10 -0700146
147 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700148 * A request object to use for transmitting data to an ICC.
149 */
150 private static final class IccAPDUArgument {
151 public int channel, cla, command, p1, p2, p3;
152 public String data;
153
154 public IccAPDUArgument(int channel, int cla, int command,
155 int p1, int p2, int p3, String data) {
156 this.channel = channel;
157 this.cla = cla;
158 this.command = command;
159 this.p1 = p1;
160 this.p2 = p2;
161 this.p3 = p3;
162 this.data = data;
163 }
164 }
165
166 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700167 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
168 * request after sending. The main thread will notify the request when it is complete.
169 */
170 private static final class MainThreadRequest {
171 /** The argument to use for the request */
172 public Object argument;
173 /** The result of the request that is run on the main thread */
174 public Object result;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800175 /** The subscriber id that this request applies to. Null if default. */
176 public Integer subId;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700177
178 public MainThreadRequest(Object argument) {
179 this.argument = argument;
180 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800181
182 public MainThreadRequest(Object argument, Integer subId) {
183 this.argument = argument;
184 this.subId = subId;
185 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700186 }
187
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800188 private static final class IncomingThirdPartyCallArgs {
189 public final ComponentName component;
190 public final String callId;
191 public final String callerDisplayName;
192
193 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
194 String callerDisplayName) {
195 this.component = component;
196 this.callId = callId;
197 this.callerDisplayName = callerDisplayName;
198 }
199 }
200
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700201 /**
202 * A handler that processes messages on the main thread in the phone process. Since many
203 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
204 * inbound binder threads to the main thread in the phone process. The Binder thread
205 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
206 * on, which will be notified when the operation completes and will contain the result of the
207 * request.
208 *
209 * <p>If a MainThreadRequest object is provided in the msg.obj field,
210 * note that request.result must be set to something non-null for the calling thread to
211 * unblock.
212 */
213 private final class MainThreadHandler extends Handler {
214 @Override
215 public void handleMessage(Message msg) {
216 MainThreadRequest request;
217 Message onCompleted;
218 AsyncResult ar;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700219 UiccCard uiccCard = UiccController.getInstance().getUiccCard();
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700220 IccAPDUArgument iccArgument;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700221
222 switch (msg.what) {
223 case CMD_HANDLE_PIN_MMI:
224 request = (MainThreadRequest) msg.obj;
Jake Hambye994d462014-02-03 13:10:13 -0800225 request.result = mPhone.handlePinMmi((String) request.argument);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700226 // Wake up the requesting thread
227 synchronized (request) {
228 request.notifyAll();
229 }
230 break;
231
232 case CMD_HANDLE_NEIGHBORING_CELL:
233 request = (MainThreadRequest) msg.obj;
234 onCompleted = obtainMessage(EVENT_NEIGHBORING_CELL_DONE,
235 request);
236 mPhone.getNeighboringCids(onCompleted);
237 break;
238
239 case EVENT_NEIGHBORING_CELL_DONE:
240 ar = (AsyncResult) msg.obj;
241 request = (MainThreadRequest) ar.userObj;
242 if (ar.exception == null && ar.result != null) {
243 request.result = ar.result;
244 } else {
245 // create an empty list to notify the waiting thread
Jake Hambye994d462014-02-03 13:10:13 -0800246 request.result = new ArrayList<NeighboringCellInfo>(0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700247 }
248 // Wake up the requesting thread
249 synchronized (request) {
250 request.notifyAll();
251 }
252 break;
253
254 case CMD_ANSWER_RINGING_CALL:
Wink Saville08874612014-08-31 19:19:58 -0700255 request = (MainThreadRequest) msg.obj;
Wink Savilleb564aae2014-10-23 10:18:09 -0700256 int answer_subId = ((Integer)request.argument).intValue();
Wink Saville08874612014-08-31 19:19:58 -0700257 answerRingingCallInternal(answer_subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700258 break;
259
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700260 case CMD_END_CALL:
261 request = (MainThreadRequest) msg.obj;
Wink Savilleb564aae2014-10-23 10:18:09 -0700262 int end_subId = ((Integer)request.argument).intValue();
Wink Saville08874612014-08-31 19:19:58 -0700263 final boolean hungUp;
264 int phoneType = getPhone(end_subId).getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700265 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
266 // CDMA: If the user presses the Power button we treat it as
267 // ending the complete call session
Wink Saville08874612014-08-31 19:19:58 -0700268 hungUp = PhoneUtils.hangupRingingAndActive(getPhone(end_subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700269 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
270 // GSM: End the call as per the Phone state
271 hungUp = PhoneUtils.hangup(mCM);
272 } else {
273 throw new IllegalStateException("Unexpected phone type: " + phoneType);
274 }
275 if (DBG) log("CMD_END_CALL: " + (hungUp ? "hung up!" : "no call to hang up"));
276 request.result = hungUp;
277 // Wake up the requesting thread
278 synchronized (request) {
279 request.notifyAll();
280 }
281 break;
282
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700283 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700284 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700285 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700286 if (uiccCard == null) {
287 loge("iccTransmitApduLogicalChannel: No UICC");
288 request.result = new IccIoResult(0x6F, 0, (byte[])null);
289 synchronized (request) {
290 request.notifyAll();
291 }
292 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700293 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
294 request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700295 uiccCard.iccTransmitApduLogicalChannel(
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700296 iccArgument.channel, iccArgument.cla, iccArgument.command,
297 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
Shishir Agrawal566b7612013-10-28 14:41:00 -0700298 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700299 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700300 break;
301
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700302 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700303 ar = (AsyncResult) msg.obj;
304 request = (MainThreadRequest) ar.userObj;
305 if (ar.exception == null && ar.result != null) {
306 request.result = ar.result;
307 } else {
308 request.result = new IccIoResult(0x6F, 0, (byte[])null);
309 if (ar.result == null) {
310 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800311 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700312 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800313 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700314 } else {
315 loge("iccTransmitApduLogicalChannel: Unknown exception");
316 }
317 }
318 synchronized (request) {
319 request.notifyAll();
320 }
321 break;
322
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700323 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
324 request = (MainThreadRequest) msg.obj;
325 iccArgument = (IccAPDUArgument) request.argument;
326 if (uiccCard == null) {
327 loge("iccTransmitApduBasicChannel: No UICC");
328 request.result = new IccIoResult(0x6F, 0, (byte[])null);
329 synchronized (request) {
330 request.notifyAll();
331 }
332 } else {
333 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
334 request);
335 uiccCard.iccTransmitApduBasicChannel(
336 iccArgument.cla, iccArgument.command, iccArgument.p1, iccArgument.p2,
337 iccArgument.p3, iccArgument.data, onCompleted);
338 }
339 break;
340
341 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
342 ar = (AsyncResult) msg.obj;
343 request = (MainThreadRequest) ar.userObj;
344 if (ar.exception == null && ar.result != null) {
345 request.result = ar.result;
346 } else {
347 request.result = new IccIoResult(0x6F, 0, (byte[])null);
348 if (ar.result == null) {
349 loge("iccTransmitApduBasicChannel: Empty response");
350 } else if (ar.exception instanceof CommandException) {
351 loge("iccTransmitApduBasicChannel: CommandException: " +
352 ar.exception);
353 } else {
354 loge("iccTransmitApduBasicChannel: Unknown exception");
355 }
356 }
357 synchronized (request) {
358 request.notifyAll();
359 }
360 break;
361
362 case CMD_EXCHANGE_SIM_IO:
363 request = (MainThreadRequest) msg.obj;
364 iccArgument = (IccAPDUArgument) request.argument;
365 if (uiccCard == null) {
366 loge("iccExchangeSimIO: No UICC");
367 request.result = new IccIoResult(0x6F, 0, (byte[])null);
368 synchronized (request) {
369 request.notifyAll();
370 }
371 } else {
372 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
373 request);
374 uiccCard.iccExchangeSimIO(iccArgument.cla, /* fileID */
375 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
376 iccArgument.data, onCompleted);
377 }
378 break;
379
380 case EVENT_EXCHANGE_SIM_IO_DONE:
381 ar = (AsyncResult) msg.obj;
382 request = (MainThreadRequest) ar.userObj;
383 if (ar.exception == null && ar.result != null) {
384 request.result = ar.result;
385 } else {
386 request.result = new IccIoResult(0x6f, 0, (byte[])null);
387 }
388 synchronized (request) {
389 request.notifyAll();
390 }
391 break;
392
Derek Tan4d5e5c12014-02-04 11:54:58 -0800393 case CMD_SEND_ENVELOPE:
394 request = (MainThreadRequest) msg.obj;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700395 if (uiccCard == null) {
396 loge("sendEnvelopeWithStatus: No UICC");
397 request.result = new IccIoResult(0x6F, 0, (byte[])null);
398 synchronized (request) {
399 request.notifyAll();
400 }
401 } else {
402 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
403 uiccCard.sendEnvelopeWithStatus((String)request.argument, onCompleted);
404 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800405 break;
406
407 case EVENT_SEND_ENVELOPE_DONE:
408 ar = (AsyncResult) msg.obj;
409 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700410 if (ar.exception == null && ar.result != null) {
411 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800412 } else {
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700413 request.result = new IccIoResult(0x6F, 0, (byte[])null);
414 if (ar.result == null) {
415 loge("sendEnvelopeWithStatus: Empty response");
416 } else if (ar.exception instanceof CommandException) {
417 loge("sendEnvelopeWithStatus: CommandException: " +
418 ar.exception);
419 } else {
420 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
421 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800422 }
423 synchronized (request) {
424 request.notifyAll();
425 }
426 break;
427
Shishir Agrawal566b7612013-10-28 14:41:00 -0700428 case CMD_OPEN_CHANNEL:
429 request = (MainThreadRequest) msg.obj;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700430 if (uiccCard == null) {
431 loge("iccOpenLogicalChannel: No UICC");
432 request.result = new IccIoResult(0x6F, 0, (byte[])null);
433 synchronized (request) {
434 request.notifyAll();
435 }
436 } else {
437 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
438 uiccCard.iccOpenLogicalChannel((String)request.argument, onCompleted);
439 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700440 break;
441
442 case EVENT_OPEN_CHANNEL_DONE:
443 ar = (AsyncResult) msg.obj;
444 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700445 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700446 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700447 int[] result = (int[]) ar.result;
448 int channelId = result[0];
449 byte[] selectResponse = null;
450 if (result.length > 1) {
451 selectResponse = new byte[result.length - 1];
452 for (int i = 1; i < result.length; ++i) {
453 selectResponse[i - 1] = (byte) result[i];
454 }
455 }
456 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700457 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700458 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700459 if (ar.result == null) {
460 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700461 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700462 if (ar.exception != null) {
463 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
464 }
465
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700466 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700467 if ((ar.exception != null) && (ar.exception instanceof CommandException)) {
468 if (ar.exception.getMessage().compareTo("MISSING_RESOURCE") == 0) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700469 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700470 } else if (ar.exception.getMessage().compareTo("NO_SUCH_ELEMENT") == 0) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700471 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700472 }
473 }
474 openChannelResp = new IccOpenLogicalChannelResponse(
475 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700476 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700477 request.result = openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700478 synchronized (request) {
479 request.notifyAll();
480 }
481 break;
482
483 case CMD_CLOSE_CHANNEL:
484 request = (MainThreadRequest) msg.obj;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700485 if (uiccCard == null) {
486 loge("iccCloseLogicalChannel: No UICC");
487 request.result = new IccIoResult(0x6F, 0, (byte[])null);
488 synchronized (request) {
489 request.notifyAll();
490 }
491 } else {
492 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
493 uiccCard.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
494 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700495 break;
496
497 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800498 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
499 break;
500
501 case CMD_NV_READ_ITEM:
502 request = (MainThreadRequest) msg.obj;
503 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
504 mPhone.nvReadItem((Integer) request.argument, onCompleted);
505 break;
506
507 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700508 ar = (AsyncResult) msg.obj;
509 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800510 if (ar.exception == null && ar.result != null) {
511 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700512 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800513 request.result = "";
514 if (ar.result == null) {
515 loge("nvReadItem: Empty response");
516 } else if (ar.exception instanceof CommandException) {
517 loge("nvReadItem: CommandException: " +
518 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700519 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800520 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700521 }
522 }
523 synchronized (request) {
524 request.notifyAll();
525 }
526 break;
527
Jake Hambye994d462014-02-03 13:10:13 -0800528 case CMD_NV_WRITE_ITEM:
529 request = (MainThreadRequest) msg.obj;
530 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
531 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
532 mPhone.nvWriteItem(idValue.first, idValue.second, onCompleted);
533 break;
534
535 case EVENT_NV_WRITE_ITEM_DONE:
536 handleNullReturnEvent(msg, "nvWriteItem");
537 break;
538
539 case CMD_NV_WRITE_CDMA_PRL:
540 request = (MainThreadRequest) msg.obj;
541 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
542 mPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
543 break;
544
545 case EVENT_NV_WRITE_CDMA_PRL_DONE:
546 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
547 break;
548
549 case CMD_NV_RESET_CONFIG:
550 request = (MainThreadRequest) msg.obj;
551 onCompleted = obtainMessage(EVENT_NV_RESET_CONFIG_DONE, request);
552 mPhone.nvResetConfig((Integer) request.argument, onCompleted);
553 break;
554
555 case EVENT_NV_RESET_CONFIG_DONE:
556 handleNullReturnEvent(msg, "nvResetConfig");
557 break;
558
Jake Hamby7c27be32014-03-03 13:25:59 -0800559 case CMD_GET_PREFERRED_NETWORK_TYPE:
560 request = (MainThreadRequest) msg.obj;
561 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
562 mPhone.getPreferredNetworkType(onCompleted);
563 break;
564
565 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
566 ar = (AsyncResult) msg.obj;
567 request = (MainThreadRequest) ar.userObj;
568 if (ar.exception == null && ar.result != null) {
569 request.result = ar.result; // Integer
570 } else {
571 request.result = -1;
572 if (ar.result == null) {
573 loge("getPreferredNetworkType: Empty response");
574 } else if (ar.exception instanceof CommandException) {
575 loge("getPreferredNetworkType: CommandException: " +
576 ar.exception);
577 } else {
578 loge("getPreferredNetworkType: Unknown exception");
579 }
580 }
581 synchronized (request) {
582 request.notifyAll();
583 }
584 break;
585
586 case CMD_SET_PREFERRED_NETWORK_TYPE:
587 request = (MainThreadRequest) msg.obj;
588 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
589 int networkType = (Integer) request.argument;
590 mPhone.setPreferredNetworkType(networkType, onCompleted);
591 break;
592
593 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
594 handleNullReturnEvent(msg, "setPreferredNetworkType");
595 break;
596
Steven Liu4bf01bc2014-07-17 11:05:29 -0500597 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
598 request = (MainThreadRequest)msg.obj;
599 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
600 mPhone.invokeOemRilRequestRaw((byte[])request.argument, onCompleted);
601 break;
602
603 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
604 ar = (AsyncResult)msg.obj;
605 request = (MainThreadRequest)ar.userObj;
606 request.result = ar;
607 synchronized (request) {
608 request.notifyAll();
609 }
610 break;
611
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800612 case CMD_SET_VOICEMAIL_NUMBER:
613 request = (MainThreadRequest) msg.obj;
614 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
615 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
616 Phone phone = (request.subId == null) ? mPhone : getPhone(request.subId);
617 phone.setVoiceMailNumber(tagNum.first, tagNum.second, onCompleted);
618 break;
619
620 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
621 handleNullReturnEvent(msg, "setVoicemailNumber");
622 break;
623
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700624 default:
625 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
626 break;
627 }
628 }
Jake Hambye994d462014-02-03 13:10:13 -0800629
630 private void handleNullReturnEvent(Message msg, String command) {
631 AsyncResult ar = (AsyncResult) msg.obj;
632 MainThreadRequest request = (MainThreadRequest) ar.userObj;
633 if (ar.exception == null) {
634 request.result = true;
635 } else {
636 request.result = false;
637 if (ar.exception instanceof CommandException) {
638 loge(command + ": CommandException: " + ar.exception);
639 } else {
640 loge(command + ": Unknown exception");
641 }
642 }
643 synchronized (request) {
644 request.notifyAll();
645 }
646 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700647 }
648
649 /**
650 * Posts the specified command to be executed on the main thread,
651 * waits for the request to complete, and returns the result.
652 * @see #sendRequestAsync
653 */
654 private Object sendRequest(int command, Object argument) {
Santos Cordon500b0e02014-06-17 10:33:33 -0700655 return sendRequest(command, argument, null);
Wink Saville36469e72014-06-11 15:17:00 -0700656 }
657
658 /**
659 * Posts the specified command to be executed on the main thread,
660 * waits for the request to complete, and returns the result.
661 * @see #sendRequestAsync
662 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800663 private Object sendRequest(int command, Object argument, Integer subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700664 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
665 throw new RuntimeException("This method will deadlock if called from the main thread.");
666 }
667
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800668 MainThreadRequest request = new MainThreadRequest(argument, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700669 Message msg = mMainThreadHandler.obtainMessage(command, request);
670 msg.sendToTarget();
671
672 // Wait for the request to complete
673 synchronized (request) {
674 while (request.result == null) {
675 try {
676 request.wait();
677 } catch (InterruptedException e) {
678 // Do nothing, go back and wait until the request is complete
679 }
680 }
681 }
682 return request.result;
683 }
684
685 /**
686 * Asynchronous ("fire and forget") version of sendRequest():
687 * Posts the specified command to be executed on the main thread, and
688 * returns immediately.
689 * @see #sendRequest
690 */
691 private void sendRequestAsync(int command) {
692 mMainThreadHandler.sendEmptyMessage(command);
693 }
694
695 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -0700696 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
697 * @see {@link #sendRequest(int,Object)}
698 */
699 private void sendRequestAsync(int command, Object argument) {
700 MainThreadRequest request = new MainThreadRequest(argument);
701 Message msg = mMainThreadHandler.obtainMessage(command, request);
702 msg.sendToTarget();
703 }
704
705 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700706 * Initialize the singleton PhoneInterfaceManager instance.
707 * This is only done once, at startup, from PhoneApp.onCreate().
708 */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700709 /* package */ static PhoneInterfaceManager init(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700710 synchronized (PhoneInterfaceManager.class) {
711 if (sInstance == null) {
Sailesh Nepal194161e2014-07-03 08:57:44 -0700712 sInstance = new PhoneInterfaceManager(app, phone);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700713 } else {
714 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
715 }
716 return sInstance;
717 }
718 }
719
720 /** Private constructor; @see init() */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700721 private PhoneInterfaceManager(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700722 mApp = app;
723 mPhone = phone;
724 mCM = PhoneGlobals.getInstance().mCM;
725 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
726 mMainThreadHandler = new MainThreadHandler();
Andrew Leedf14ead2014-10-17 14:22:52 -0700727 mTelephonySharedPreferences =
Derek Tan97ebb422014-09-05 16:55:38 -0700728 PreferenceManager.getDefaultSharedPreferences(mPhone.getContext());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700729 publish();
730 }
731
732 private void publish() {
733 if (DBG) log("publish: " + this);
734
735 ServiceManager.addService("phone", this);
736 }
737
Wink Saville36469e72014-06-11 15:17:00 -0700738 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -0700739 private Phone getPhone(int subId) {
Legler Wuf8733b42014-09-25 11:00:54 +0800740 return PhoneFactory.getPhone(SubscriptionManager.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -0700741 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700742 //
743 // Implementation of the ITelephony interface.
744 //
745
746 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700747 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -0700748 }
749
Wink Savilleb564aae2014-10-23 10:18:09 -0700750 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700751 if (DBG) log("dial: " + number);
752 // No permission check needed here: This is just a wrapper around the
753 // ACTION_DIAL intent, which is available to any app since it puts up
754 // the UI before it does anything.
755
756 String url = createTelUrl(number);
757 if (url == null) {
758 return;
759 }
760
761 // PENDING: should we just silently fail if phone is offhook or ringing?
Wink Saville36469e72014-06-11 15:17:00 -0700762 PhoneConstants.State state = mCM.getState(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700763 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
764 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
765 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
766 mApp.startActivity(intent);
767 }
768 }
769
770 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700771 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -0700772 }
773
Wink Savilleb564aae2014-10-23 10:18:09 -0700774 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700775 if (DBG) log("call: " + number);
776
777 // This is just a wrapper around the ACTION_CALL intent, but we still
778 // need to do a permission check since we're calling startActivity()
779 // from the context of the phone app.
780 enforceCallPermission();
781
782 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
783 != AppOpsManager.MODE_ALLOWED) {
784 return;
785 }
786
787 String url = createTelUrl(number);
788 if (url == null) {
789 return;
790 }
791
Wink Saville08874612014-08-31 19:19:58 -0700792 boolean isValid = false;
Wink Saville0f3b5fc2014-11-11 08:40:49 -0800793 List<SubscriptionInfo> slist = SubscriptionManager.getActiveSubscriptionInfoList();
794 for (SubscriptionInfo subInfoRecord : slist) {
Stuart Scott48779712014-10-30 11:17:34 -0700795 if (subInfoRecord.getSubscriptionId() == subId) {
Wink Saville08874612014-08-31 19:19:58 -0700796 isValid = true;
797 break;
798 }
799 }
800 if (isValid == false) {
801 return;
802 }
803
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700804 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
Wink Saville36469e72014-06-11 15:17:00 -0700805 intent.putExtra(SUBSCRIPTION_KEY, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700806 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
807 mApp.startActivity(intent);
808 }
809
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700810 /**
811 * End a call based on call state
812 * @return true is a call was ended
813 */
814 public boolean endCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700815 return endCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700816 }
817
818 /**
819 * End a call based on the call state of the subId
820 * @return true is a call was ended
821 */
Wink Savilleb564aae2014-10-23 10:18:09 -0700822 public boolean endCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700823 enforceCallPermission();
Wink Saville9f73bda2014-11-05 08:13:36 -0800824 return (Boolean) sendRequest(CMD_END_CALL, new Integer(subId), null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700825 }
826
827 public void answerRingingCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700828 answerRingingCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700829 }
830
Wink Savilleb564aae2014-10-23 10:18:09 -0700831 public void answerRingingCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700832 if (DBG) log("answerRingingCall...");
833 // TODO: there should eventually be a separate "ANSWER_PHONE" permission,
834 // but that can probably wait till the big TelephonyManager API overhaul.
835 // For now, protect this call with the MODIFY_PHONE_STATE permission.
836 enforceModifyPermission();
Wink Saville9f73bda2014-11-05 08:13:36 -0800837 sendRequest(CMD_ANSWER_RINGING_CALL, new Integer(subId), null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700838 }
839
840 /**
841 * Make the actual telephony calls to implement answerRingingCall().
842 * This should only be called from the main thread of the Phone app.
843 * @see #answerRingingCall
844 *
845 * TODO: it would be nice to return true if we answered the call, or
846 * false if there wasn't actually a ringing incoming call, or some
847 * other error occurred. (In other words, pass back the return value
848 * from PhoneUtils.answerCall() or PhoneUtils.answerAndEndActive().)
849 * But that would require calling this method via sendRequest() rather
850 * than sendRequestAsync(), and right now we don't actually *need* that
851 * return value, so let's just return void for now.
852 */
Wink Savilleb564aae2014-10-23 10:18:09 -0700853 private void answerRingingCallInternal(int subId) {
Wink Saville08874612014-08-31 19:19:58 -0700854 final boolean hasRingingCall = !getPhone(subId).getRingingCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700855 if (hasRingingCall) {
Wink Saville08874612014-08-31 19:19:58 -0700856 final boolean hasActiveCall = !getPhone(subId).getForegroundCall().isIdle();
857 final boolean hasHoldingCall = !getPhone(subId).getBackgroundCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700858 if (hasActiveCall && hasHoldingCall) {
859 // Both lines are in use!
860 // TODO: provide a flag to let the caller specify what
861 // policy to use if both lines are in use. (The current
862 // behavior is hardwired to "answer incoming, end ongoing",
863 // which is how the CALL button is specced to behave.)
864 PhoneUtils.answerAndEndActive(mCM, mCM.getFirstActiveRingingCall());
865 return;
866 } else {
867 // answerCall() will automatically hold the current active
868 // call, if there is one.
869 PhoneUtils.answerCall(mCM.getFirstActiveRingingCall());
870 return;
871 }
872 } else {
873 // No call was ringing.
874 return;
875 }
876 }
877
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700878 /**
Santos Cordon5422a8d2014-09-12 04:20:56 -0700879 * This method is no longer used and can be removed once TelephonyManager stops referring to it.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700880 */
Santos Cordon5422a8d2014-09-12 04:20:56 -0700881 public void silenceRinger() {
882 Log.e(LOG_TAG, "silenseRinger not supported");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700883 }
884
885 public boolean isOffhook() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700886 return isOffhookForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700887 }
888
Wink Savilleb564aae2014-10-23 10:18:09 -0700889 public boolean isOffhookForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -0700890 return (getPhone(subId).getState() == PhoneConstants.State.OFFHOOK);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700891 }
892
893 public boolean isRinging() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700894 return (isRingingForSubscriber(getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -0700895 }
896
Wink Savilleb564aae2014-10-23 10:18:09 -0700897 public boolean isRingingForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -0700898 return (getPhone(subId).getState() == PhoneConstants.State.RINGING);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700899 }
900
901 public boolean isIdle() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700902 return isIdleForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700903 }
904
Wink Savilleb564aae2014-10-23 10:18:09 -0700905 public boolean isIdleForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -0700906 return (getPhone(subId).getState() == PhoneConstants.State.IDLE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700907 }
908
909 public boolean isSimPinEnabled() {
910 enforceReadPermission();
911 return (PhoneGlobals.getInstance().isSimPinEnabled());
912 }
913
914 public boolean supplyPin(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700915 return supplyPinForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -0700916 }
917
Wink Savilleb564aae2014-10-23 10:18:09 -0700918 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700919 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -0700920 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
921 }
922
923 public boolean supplyPuk(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700924 return supplyPukForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -0700925 }
926
Wink Savilleb564aae2014-10-23 10:18:09 -0700927 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700928 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -0700929 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
930 }
931
932 /** {@hide} */
933 public int[] supplyPinReportResult(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700934 return supplyPinReportResultForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -0700935 }
936
Wink Savilleb564aae2014-10-23 10:18:09 -0700937 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700938 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -0700939 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700940 checkSimPin.start();
941 return checkSimPin.unlockSim(null, pin);
942 }
943
Wink Saville9de0f752013-10-22 19:04:03 -0700944 /** {@hide} */
945 public int[] supplyPukReportResult(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700946 return supplyPukReportResultForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -0700947 }
948
Wink Savilleb564aae2014-10-23 10:18:09 -0700949 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700950 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -0700951 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700952 checkSimPuk.start();
953 return checkSimPuk.unlockSim(puk, pin);
954 }
955
956 /**
Wink Saville9de0f752013-10-22 19:04:03 -0700957 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700958 * a synchronous one.
959 */
960 private static class UnlockSim extends Thread {
961
962 private final IccCard mSimCard;
963
964 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -0700965 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
966 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700967
968 // For replies from SimCard interface
969 private Handler mHandler;
970
971 // For async handler to identify request type
972 private static final int SUPPLY_PIN_COMPLETE = 100;
973
974 public UnlockSim(IccCard simCard) {
975 mSimCard = simCard;
976 }
977
978 @Override
979 public void run() {
980 Looper.prepare();
981 synchronized (UnlockSim.this) {
982 mHandler = new Handler() {
983 @Override
984 public void handleMessage(Message msg) {
985 AsyncResult ar = (AsyncResult) msg.obj;
986 switch (msg.what) {
987 case SUPPLY_PIN_COMPLETE:
988 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
989 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -0700990 mRetryCount = msg.arg1;
991 if (ar.exception != null) {
992 if (ar.exception instanceof CommandException &&
993 ((CommandException)(ar.exception)).getCommandError()
994 == CommandException.Error.PASSWORD_INCORRECT) {
995 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
996 } else {
997 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
998 }
999 } else {
1000 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
1001 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001002 mDone = true;
1003 UnlockSim.this.notifyAll();
1004 }
1005 break;
1006 }
1007 }
1008 };
1009 UnlockSim.this.notifyAll();
1010 }
1011 Looper.loop();
1012 }
1013
1014 /*
1015 * Use PIN or PUK to unlock SIM card
1016 *
1017 * If PUK is null, unlock SIM card with PIN
1018 *
1019 * If PUK is not null, unlock SIM card with PUK and set PIN code
1020 */
Wink Saville9de0f752013-10-22 19:04:03 -07001021 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001022
1023 while (mHandler == null) {
1024 try {
1025 wait();
1026 } catch (InterruptedException e) {
1027 Thread.currentThread().interrupt();
1028 }
1029 }
1030 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
1031
1032 if (puk == null) {
1033 mSimCard.supplyPin(pin, callback);
1034 } else {
1035 mSimCard.supplyPuk(puk, pin, callback);
1036 }
1037
1038 while (!mDone) {
1039 try {
1040 Log.d(LOG_TAG, "wait for done");
1041 wait();
1042 } catch (InterruptedException e) {
1043 // Restore the interrupted status
1044 Thread.currentThread().interrupt();
1045 }
1046 }
1047 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07001048 int[] resultArray = new int[2];
1049 resultArray[0] = mResult;
1050 resultArray[1] = mRetryCount;
1051 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001052 }
1053 }
1054
1055 public void updateServiceLocation() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001056 updateServiceLocationForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001057
1058 }
1059
Wink Savilleb564aae2014-10-23 10:18:09 -07001060 public void updateServiceLocationForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001061 // No permission check needed here: this call is harmless, and it's
1062 // needed for the ServiceState.requestStateUpdate() call (which is
1063 // already intentionally exposed to 3rd parties.)
Wink Saville36469e72014-06-11 15:17:00 -07001064 getPhone(subId).updateServiceLocation();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001065 }
1066
1067 public boolean isRadioOn() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001068 return isRadioOnForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001069 }
1070
Wink Savilleb564aae2014-10-23 10:18:09 -07001071 public boolean isRadioOnForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001072 return getPhone(subId).getServiceState().getState() != ServiceState.STATE_POWER_OFF;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001073 }
1074
1075 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001076 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001077
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001078 }
Wink Saville36469e72014-06-11 15:17:00 -07001079
Wink Savilleb564aae2014-10-23 10:18:09 -07001080 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001081 enforceModifyPermission();
Wink Savilleadd7cc52014-09-08 14:23:09 -07001082 getPhone(subId).setRadioPower(!isRadioOnForSubscriber(subId));
Wink Saville36469e72014-06-11 15:17:00 -07001083 }
1084
1085 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001086 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001087 }
1088
Wink Savilleb564aae2014-10-23 10:18:09 -07001089 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07001090 enforceModifyPermission();
1091 if ((getPhone(subId).getServiceState().getState() !=
1092 ServiceState.STATE_POWER_OFF) != turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001093 toggleRadioOnOffForSubscriber(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001094 }
1095 return true;
1096 }
Wink Saville36469e72014-06-11 15:17:00 -07001097
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001098 public boolean needMobileRadioShutdown() {
1099 /*
1100 * If any of the Radios are available, it will need to be
1101 * shutdown. So return true if any Radio is available.
1102 */
1103 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1104 Phone phone = PhoneFactory.getPhone(i);
1105 if (phone != null && phone.isRadioAvailable()) return true;
1106 }
1107 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
1108 return false;
1109 }
1110
1111 public void shutdownMobileRadios() {
1112 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1113 logv("Shutting down Phone " + i);
1114 shutdownRadioUsingPhoneId(i);
1115 }
1116 }
1117
1118 private void shutdownRadioUsingPhoneId(int phoneId) {
1119 enforceModifyPermission();
1120 Phone phone = PhoneFactory.getPhone(phoneId);
1121 if (phone != null && phone.isRadioAvailable()) {
1122 phone.shutdownRadio();
1123 }
1124 }
1125
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001126 public boolean setRadioPower(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001127 return setRadioPowerForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001128 }
1129
Wink Savilleb564aae2014-10-23 10:18:09 -07001130 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001131 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001132 getPhone(subId).setRadioPower(turnOn);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001133 return true;
1134 }
1135
Wink Saville36469e72014-06-11 15:17:00 -07001136 // FIXME: subId version needed
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001137 public boolean enableDataConnectivity() {
1138 enforceModifyPermission();
Wink Savilleb564aae2014-10-23 10:18:09 -07001139 int subId = SubscriptionManager.getDefaultDataSubId();
Wink Saville36469e72014-06-11 15:17:00 -07001140 getPhone(subId).setDataEnabled(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001141 return true;
1142 }
1143
Wink Saville36469e72014-06-11 15:17:00 -07001144 // FIXME: subId version needed
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001145 public boolean disableDataConnectivity() {
1146 enforceModifyPermission();
Wink Savilleb564aae2014-10-23 10:18:09 -07001147 int subId = SubscriptionManager.getDefaultDataSubId();
Wink Saville36469e72014-06-11 15:17:00 -07001148 getPhone(subId).setDataEnabled(false);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001149 return true;
1150 }
1151
Wink Saville36469e72014-06-11 15:17:00 -07001152 // FIXME: subId version needed
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001153 public boolean isDataConnectivityPossible() {
Wink Savilleb564aae2014-10-23 10:18:09 -07001154 int subId = SubscriptionManager.getDefaultDataSubId();
Wink Saville36469e72014-06-11 15:17:00 -07001155 return getPhone(subId).isDataConnectivityPossible();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001156 }
1157
1158 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001159 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07001160 }
1161
Wink Savilleb564aae2014-10-23 10:18:09 -07001162 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001163 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001164 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001165 }
1166
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001167 public int getCallState() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001168 return getCallStateForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001169 }
1170
Wink Savilleb564aae2014-10-23 10:18:09 -07001171 public int getCallStateForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001172 return DefaultPhoneNotifier.convertCallState(getPhone(subId).getState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001173 }
1174
1175 public int getDataState() {
Wink Saville36469e72014-06-11 15:17:00 -07001176 Phone phone = getPhone(SubscriptionManager.getDefaultDataSubId());
1177 return DefaultPhoneNotifier.convertDataState(phone.getDataConnectionState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001178 }
1179
1180 public int getDataActivity() {
Wink Saville36469e72014-06-11 15:17:00 -07001181 Phone phone = getPhone(SubscriptionManager.getDefaultDataSubId());
1182 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001183 }
1184
1185 @Override
1186 public Bundle getCellLocation() {
1187 try {
1188 mApp.enforceCallingOrSelfPermission(
1189 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1190 } catch (SecurityException e) {
1191 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1192 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1193 // is the weaker precondition
1194 mApp.enforceCallingOrSelfPermission(
1195 android.Manifest.permission.ACCESS_COARSE_LOCATION, null);
1196 }
1197
Jake Hambye994d462014-02-03 13:10:13 -08001198 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001199 if (DBG_LOC) log("getCellLocation: is active user");
1200 Bundle data = new Bundle();
1201 mPhone.getCellLocation().fillInNotifierBundle(data);
1202 return data;
1203 } else {
1204 if (DBG_LOC) log("getCellLocation: suppress non-active user");
1205 return null;
1206 }
1207 }
1208
1209 @Override
1210 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001211 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001212 }
1213
Wink Savilleb564aae2014-10-23 10:18:09 -07001214 public void enableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001215 mApp.enforceCallingOrSelfPermission(
1216 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Wink Saville36469e72014-06-11 15:17:00 -07001217 getPhone(subId).enableLocationUpdates();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001218 }
1219
1220 @Override
1221 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001222 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001223 }
1224
Wink Savilleb564aae2014-10-23 10:18:09 -07001225 public void disableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001226 mApp.enforceCallingOrSelfPermission(
1227 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Wink Saville36469e72014-06-11 15:17:00 -07001228 getPhone(subId).disableLocationUpdates();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001229 }
1230
1231 @Override
1232 @SuppressWarnings("unchecked")
1233 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
1234 try {
1235 mApp.enforceCallingOrSelfPermission(
1236 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1237 } catch (SecurityException e) {
1238 // If we have ACCESS_FINE_LOCATION permission, skip the check
1239 // for ACCESS_COARSE_LOCATION
1240 // A failure should throw the SecurityException from
1241 // ACCESS_COARSE_LOCATION since this is the weaker precondition
1242 mApp.enforceCallingOrSelfPermission(
1243 android.Manifest.permission.ACCESS_COARSE_LOCATION, null);
1244 }
1245
1246 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
1247 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1248 return null;
1249 }
Jake Hambye994d462014-02-03 13:10:13 -08001250 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001251 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
1252
1253 ArrayList<NeighboringCellInfo> cells = null;
1254
1255 try {
1256 cells = (ArrayList<NeighboringCellInfo>) sendRequest(
Wink Saville36469e72014-06-11 15:17:00 -07001257 CMD_HANDLE_NEIGHBORING_CELL, null, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001258 } catch (RuntimeException e) {
Wink Saville36469e72014-06-11 15:17:00 -07001259 Log.e(LOG_TAG, "getNeighboringCellInfo " + e);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001260 }
1261 return cells;
1262 } else {
1263 if (DBG_LOC) log("getNeighboringCellInfo: suppress non-active user");
1264 return null;
1265 }
1266 }
1267
1268
1269 @Override
1270 public List<CellInfo> getAllCellInfo() {
1271 try {
1272 mApp.enforceCallingOrSelfPermission(
1273 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1274 } catch (SecurityException e) {
1275 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1276 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1277 // is the weaker precondition
1278 mApp.enforceCallingOrSelfPermission(
1279 android.Manifest.permission.ACCESS_COARSE_LOCATION, null);
1280 }
1281
Jake Hambye994d462014-02-03 13:10:13 -08001282 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001283 if (DBG_LOC) log("getAllCellInfo: is active user");
1284 return mPhone.getAllCellInfo();
1285 } else {
1286 if (DBG_LOC) log("getAllCellInfo: suppress non-active user");
1287 return null;
1288 }
1289 }
1290
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001291 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001292 public void setCellInfoListRate(int rateInMillis) {
1293 mPhone.setCellInfoListRate(rateInMillis);
1294 }
1295
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001296 //
1297 // Internal helper methods.
1298 //
1299
Jake Hambye994d462014-02-03 13:10:13 -08001300 private static boolean checkIfCallerIsSelfOrForegroundUser() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001301 boolean ok;
1302
1303 boolean self = Binder.getCallingUid() == Process.myUid();
1304 if (!self) {
1305 // Get the caller's user id then clear the calling identity
1306 // which will be restored in the finally clause.
1307 int callingUser = UserHandle.getCallingUserId();
1308 long ident = Binder.clearCallingIdentity();
1309
1310 try {
1311 // With calling identity cleared the current user is the foreground user.
1312 int foregroundUser = ActivityManager.getCurrentUser();
1313 ok = (foregroundUser == callingUser);
1314 if (DBG_LOC) {
1315 log("checkIfCallerIsSelfOrForegoundUser: foregroundUser=" + foregroundUser
1316 + " callingUser=" + callingUser + " ok=" + ok);
1317 }
1318 } catch (Exception ex) {
1319 if (DBG_LOC) loge("checkIfCallerIsSelfOrForegoundUser: Exception ex=" + ex);
1320 ok = false;
1321 } finally {
1322 Binder.restoreCallingIdentity(ident);
1323 }
1324 } else {
1325 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: is self");
1326 ok = true;
1327 }
1328 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: ret=" + ok);
1329 return ok;
1330 }
1331
1332 /**
1333 * Make sure the caller has the READ_PHONE_STATE permission.
1334 *
1335 * @throws SecurityException if the caller does not have the required permission
1336 */
1337 private void enforceReadPermission() {
1338 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE, null);
1339 }
1340
1341 /**
1342 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1343 *
1344 * @throws SecurityException if the caller does not have the required permission
1345 */
1346 private void enforceModifyPermission() {
1347 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1348 }
1349
1350 /**
Junda Liua2e36012014-07-09 18:30:01 -07001351 * Make sure either system app or the caller has carrier privilege.
1352 *
1353 * @throws SecurityException if the caller does not have the required permission/privilege
1354 */
1355 private void enforceModifyPermissionOrCarrierPrivilege() {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001356 int permission = mApp.checkCallingOrSelfPermission(
1357 android.Manifest.permission.MODIFY_PHONE_STATE);
1358 if (permission == PackageManager.PERMISSION_GRANTED) {
1359 return;
1360 }
1361
1362 log("No modify permission, check carrier privilege next.");
1363 if (hasCarrierPrivileges() != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
1364 loge("No Carrier Privilege.");
1365 throw new SecurityException("No modify permission or carrier privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001366 }
1367 }
1368
1369 /**
1370 * Make sure the caller has carrier privilege.
1371 *
1372 * @throws SecurityException if the caller does not have the required permission
1373 */
1374 private void enforceCarrierPrivilege() {
1375 if (hasCarrierPrivileges() != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001376 loge("No Carrier Privilege.");
1377 throw new SecurityException("No Carrier Privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001378 }
1379 }
1380
1381 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001382 * Make sure the caller has the CALL_PHONE permission.
1383 *
1384 * @throws SecurityException if the caller does not have the required permission
1385 */
1386 private void enforceCallPermission() {
1387 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1388 }
1389
Shishir Agrawal566b7612013-10-28 14:41:00 -07001390 /**
Gabriel Peal36ebb0d2014-03-20 09:20:43 -07001391 * Make sure the caller has the READ_PRIVILEGED_PHONE_STATE permission.
1392 *
1393 * @throws SecurityException if the caller does not have the required permission
1394 */
1395 private void enforcePrivilegedPhoneStatePermission() {
1396 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
1397 null);
1398 }
1399
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001400 private String createTelUrl(String number) {
1401 if (TextUtils.isEmpty(number)) {
1402 return null;
1403 }
1404
Jake Hambye994d462014-02-03 13:10:13 -08001405 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001406 }
1407
Ihab Awadf9e92732013-12-05 18:02:52 -08001408 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001409 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1410 }
1411
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001412 private static void logv(String msg) {
1413 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
1414 }
1415
Ihab Awadf9e92732013-12-05 18:02:52 -08001416 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001417 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1418 }
1419
1420 public int getActivePhoneType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001421 return getActivePhoneTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001422 }
1423
Wink Savilleb564aae2014-10-23 10:18:09 -07001424 public int getActivePhoneTypeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001425 return getPhone(subId).getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001426 }
1427
1428 /**
1429 * Returns the CDMA ERI icon index to display
1430 */
1431 public int getCdmaEriIconIndex() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001432 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001433
1434 }
1435
Wink Savilleb564aae2014-10-23 10:18:09 -07001436 public int getCdmaEriIconIndexForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001437 return getPhone(subId).getCdmaEriIconIndex();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001438 }
1439
1440 /**
1441 * Returns the CDMA ERI icon mode,
1442 * 0 - ON
1443 * 1 - FLASHING
1444 */
1445 public int getCdmaEriIconMode() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001446 return getCdmaEriIconModeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001447 }
1448
Wink Savilleb564aae2014-10-23 10:18:09 -07001449 public int getCdmaEriIconModeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001450 return getPhone(subId).getCdmaEriIconMode();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001451 }
1452
1453 /**
1454 * Returns the CDMA ERI text,
1455 */
1456 public String getCdmaEriText() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001457 return getCdmaEriTextForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001458 }
1459
Wink Savilleb564aae2014-10-23 10:18:09 -07001460 public String getCdmaEriTextForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001461 return getPhone(subId).getCdmaEriText();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001462 }
1463
1464 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07001465 * Returns the CDMA MDN.
1466 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001467 public String getCdmaMdn(int subId) {
Junda Liuca05d5d2014-08-14 22:36:34 -07001468 enforceModifyPermissionOrCarrierPrivilege();
1469 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1470 return getPhone(subId).getLine1Number();
1471 } else {
1472 return null;
1473 }
1474 }
1475
1476 /**
1477 * Returns the CDMA MIN.
1478 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001479 public String getCdmaMin(int subId) {
Junda Liuca05d5d2014-08-14 22:36:34 -07001480 enforceModifyPermissionOrCarrierPrivilege();
1481 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1482 return getPhone(subId).getCdmaMin();
1483 } else {
1484 return null;
1485 }
1486 }
1487
1488 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001489 * Returns true if CDMA provisioning needs to run.
1490 */
1491 public boolean needsOtaServiceProvisioning() {
1492 return mPhone.needsOtaServiceProvisioning();
1493 }
1494
1495 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001496 * Sets the voice mail number of a given subId.
1497 */
1498 @Override
1499 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
1500 enforceModifyPermissionOrCarrierPrivilege();
1501 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
1502 new Pair<String, String>(alphaTag, number), new Integer(subId));
1503 return success;
1504 }
1505
1506 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001507 * Returns the unread count of voicemails
1508 */
1509 public int getVoiceMessageCount() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001510 return getVoiceMessageCountForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001511 }
1512
1513 /**
1514 * Returns the unread count of voicemails for a subId
1515 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001516 public int getVoiceMessageCountForSubscriber( int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001517 return getPhone(subId).getVoiceMessageCount();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001518 }
1519
1520 /**
1521 * Returns the data network type
1522 *
1523 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
1524 */
1525 @Override
1526 public int getNetworkType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001527 return getNetworkTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001528 }
1529
1530 /**
1531 * Returns the network type for a subId
1532 */
1533 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001534 public int getNetworkTypeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001535 return getPhone(subId).getServiceState().getDataNetworkType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001536 }
1537
1538 /**
1539 * Returns the data network type
1540 */
1541 @Override
1542 public int getDataNetworkType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001543 return getDataNetworkTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001544 }
1545
1546 /**
1547 * Returns the data network type for a subId
1548 */
1549 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001550 public int getDataNetworkTypeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001551 return getPhone(subId).getServiceState().getDataNetworkType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001552 }
1553
1554 /**
1555 * Returns the data network type
1556 */
1557 @Override
1558 public int getVoiceNetworkType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001559 return getVoiceNetworkTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001560 }
1561
1562 /**
1563 * Returns the Voice network type for a subId
1564 */
1565 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001566 public int getVoiceNetworkTypeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001567 return getPhone(subId).getServiceState().getVoiceNetworkType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001568 }
1569
1570 /**
1571 * @return true if a ICC card is present
1572 */
1573 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07001574 // FIXME Make changes to pass defaultSimId of type int
1575 return hasIccCardUsingSlotId(getDefaultSubscription());
1576 }
1577
1578 /**
1579 * @return true if a ICC card is present for a slotId
1580 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001581 public boolean hasIccCardUsingSlotId(int slotId) {
Wink Saville36469e72014-06-11 15:17:00 -07001582 return getPhone(slotId).getIccCard().hasIccCard();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001583 }
1584
1585 /**
1586 * Return if the current radio is LTE on CDMA. This
1587 * is a tri-state return value as for a period of time
1588 * the mode may be unknown.
1589 *
1590 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08001591 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001592 */
1593 public int getLteOnCdmaMode() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001594 return getLteOnCdmaModeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001595 }
1596
Wink Savilleb564aae2014-10-23 10:18:09 -07001597 public int getLteOnCdmaModeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001598 return getPhone(subId).getLteOnCdmaMode();
1599 }
1600
1601 public void setPhone(Phone phone) {
1602 mPhone = phone;
1603 }
1604
1605 /**
1606 * {@hide}
1607 * Returns Default subId, 0 in the case of single standby.
1608 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001609 private int getDefaultSubscription() {
Wink Saville36469e72014-06-11 15:17:00 -07001610 return SubscriptionManager.getDefaultSubId();
1611 }
1612
Wink Savilleb564aae2014-10-23 10:18:09 -07001613 private int getPreferredVoiceSubscription() {
Wink Saville36469e72014-06-11 15:17:00 -07001614 return SubscriptionManager.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001615 }
Ihab Awadf2177b72013-11-25 13:33:23 -08001616
1617 /**
1618 * @see android.telephony.TelephonyManager.WifiCallingChoices
1619 */
1620 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08001621 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
1622 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08001623 }
1624
1625 /**
1626 * @see android.telephony.TelephonyManager.WifiCallingChoices
1627 */
1628 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08001629 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
1630 Settings.System.putInt(mPhone.getContext().getContentResolver(),
1631 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08001632 }
1633
Sailesh Nepald1e68152013-12-12 19:08:02 -08001634 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07001635 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08001636 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08001637 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08001638
Shishir Agrawal566b7612013-10-28 14:41:00 -07001639 @Override
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001640 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(String AID) {
Junda Liua2e36012014-07-09 18:30:01 -07001641 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001642
1643 if (DBG) log("iccOpenLogicalChannel: " + AID);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001644 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
1645 CMD_OPEN_CHANNEL, AID);
1646 if (DBG) log("iccOpenLogicalChannel: " + response);
1647 return response;
Shishir Agrawal566b7612013-10-28 14:41:00 -07001648 }
1649
1650 @Override
1651 public boolean iccCloseLogicalChannel(int channel) {
Junda Liua2e36012014-07-09 18:30:01 -07001652 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001653
1654 if (DBG) log("iccCloseLogicalChannel: " + channel);
1655 if (channel < 0) {
1656 return false;
1657 }
Jake Hambye994d462014-02-03 13:10:13 -08001658 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07001659 if (DBG) log("iccCloseLogicalChannel: " + success);
1660 return success;
1661 }
1662
1663 @Override
1664 public String iccTransmitApduLogicalChannel(int channel, int cla,
1665 int command, int p1, int p2, int p3, String data) {
Junda Liua2e36012014-07-09 18:30:01 -07001666 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001667
1668 if (DBG) {
1669 log("iccTransmitApduLogicalChannel: chnl=" + channel + " cla=" + cla +
1670 " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
1671 " data=" + data);
1672 }
1673
1674 if (channel < 0) {
1675 return "";
1676 }
1677
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001678 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Shishir Agrawal566b7612013-10-28 14:41:00 -07001679 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data));
1680 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
1681
Shishir Agrawal566b7612013-10-28 14:41:00 -07001682 // Append the returned status code to the end of the response payload.
1683 String s = Integer.toHexString(
1684 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07001685 if (response.payload != null) {
1686 s = IccUtils.bytesToHexString(response.payload) + s;
1687 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07001688 return s;
1689 }
Jake Hambye994d462014-02-03 13:10:13 -08001690
Evan Charltonc66da362014-05-16 14:06:40 -07001691 @Override
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001692 public String iccTransmitApduBasicChannel(int cla, int command, int p1, int p2,
1693 int p3, String data) {
1694 enforceModifyPermissionOrCarrierPrivilege();
1695
1696 if (DBG) {
1697 log("iccTransmitApduBasicChannel: cla=" + cla + " cmd=" + command + " p1="
1698 + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
1699 }
1700
1701 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
1702 new IccAPDUArgument(0, cla, command, p1, p2, p3, data));
1703 if (DBG) log("iccTransmitApduBasicChannel: " + response);
1704
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001705 // Append the returned status code to the end of the response payload.
1706 String s = Integer.toHexString(
1707 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07001708 if (response.payload != null) {
1709 s = IccUtils.bytesToHexString(response.payload) + s;
1710 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001711 return s;
1712 }
1713
1714 @Override
1715 public byte[] iccExchangeSimIO(int fileID, int command, int p1, int p2, int p3,
1716 String filePath) {
1717 enforceModifyPermissionOrCarrierPrivilege();
1718
1719 if (DBG) {
1720 log("Exchange SIM_IO " + fileID + ":" + command + " " +
1721 p1 + " " + p2 + " " + p3 + ":" + filePath);
1722 }
1723
1724 IccIoResult response =
1725 (IccIoResult)sendRequest(CMD_EXCHANGE_SIM_IO,
Yong Jiang3edf3782014-10-03 13:23:28 -05001726 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath));
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001727
1728 if (DBG) {
1729 log("Exchange SIM_IO [R]" + response);
1730 }
1731
1732 byte[] result = null;
1733 int length = 2;
1734 if (response.payload != null) {
1735 length = 2 + response.payload.length;
1736 result = new byte[length];
1737 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
1738 } else {
1739 result = new byte[length];
1740 }
1741
1742 result[length - 1] = (byte) response.sw2;
1743 result[length - 2] = (byte) response.sw1;
1744 return result;
1745 }
1746
1747 @Override
Evan Charltonc66da362014-05-16 14:06:40 -07001748 public String sendEnvelopeWithStatus(String content) {
Junda Liua2e36012014-07-09 18:30:01 -07001749 enforceModifyPermissionOrCarrierPrivilege();
Evan Charltonc66da362014-05-16 14:06:40 -07001750
1751 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content);
1752 if (response.payload == null) {
1753 return "";
1754 }
1755
1756 // Append the returned status code to the end of the response payload.
1757 String s = Integer.toHexString(
1758 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
1759 s = IccUtils.bytesToHexString(response.payload) + s;
1760 return s;
1761 }
1762
Jake Hambye994d462014-02-03 13:10:13 -08001763 /**
1764 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
1765 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
1766 *
1767 * @param itemID the ID of the item to read
1768 * @return the NV item as a String, or null on error.
1769 */
1770 @Override
1771 public String nvReadItem(int itemID) {
Junda Liua2e36012014-07-09 18:30:01 -07001772 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001773 if (DBG) log("nvReadItem: item " + itemID);
1774 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
1775 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
1776 return value;
1777 }
1778
1779 /**
1780 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
1781 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
1782 *
1783 * @param itemID the ID of the item to read
1784 * @param itemValue the value to write, as a String
1785 * @return true on success; false on any failure
1786 */
1787 @Override
1788 public boolean nvWriteItem(int itemID, String itemValue) {
Junda Liua2e36012014-07-09 18:30:01 -07001789 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001790 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
1791 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
1792 new Pair<Integer, String>(itemID, itemValue));
1793 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
1794 return success;
1795 }
1796
1797 /**
1798 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
1799 * Used for device configuration by some CDMA operators.
1800 *
1801 * @param preferredRoamingList byte array containing the new PRL
1802 * @return true on success; false on any failure
1803 */
1804 @Override
1805 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Junda Liua2e36012014-07-09 18:30:01 -07001806 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001807 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
1808 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
1809 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
1810 return success;
1811 }
1812
1813 /**
1814 * Perform the specified type of NV config reset.
1815 * Used for device configuration by some CDMA operators.
1816 *
1817 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
1818 * @return true on success; false on any failure
1819 */
1820 @Override
1821 public boolean nvResetConfig(int resetType) {
Junda Liua2e36012014-07-09 18:30:01 -07001822 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001823 if (DBG) log("nvResetConfig: type " + resetType);
1824 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
1825 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
1826 return success;
1827 }
Jake Hamby7c27be32014-03-03 13:25:59 -08001828
1829 /**
Wink Saville36469e72014-06-11 15:17:00 -07001830 * {@hide}
1831 * Returns Default sim, 0 in the case of single standby.
1832 */
1833 public int getDefaultSim() {
1834 //TODO Need to get it from Telephony Devcontroller
1835 return 0;
1836 }
1837
ram87fca6f2014-07-18 18:58:44 +05301838 public String[] getPcscfAddress(String apnType) {
Wink Saville36469e72014-06-11 15:17:00 -07001839 enforceReadPermission();
ram87fca6f2014-07-18 18:58:44 +05301840 return mPhone.getPcscfAddress(apnType);
Wink Saville36469e72014-06-11 15:17:00 -07001841 }
1842
1843 public void setImsRegistrationState(boolean registered) {
1844 enforceModifyPermission();
1845 mPhone.setImsRegistrationState(registered);
1846 }
1847
1848 /**
Junda Liu84d15a22014-07-02 11:21:04 -07001849 * Get the calculated preferred network type.
1850 * Used for debugging incorrect network type.
1851 *
1852 * @return the preferred network type, defined in RILConstants.java.
1853 */
1854 @Override
1855 public int getCalculatedPreferredNetworkType() {
1856 enforceReadPermission();
Wink Saville08874612014-08-31 19:19:58 -07001857 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext(), 0); // wink FIXME: need to get PhoneId from somewhere.
Junda Liu84d15a22014-07-02 11:21:04 -07001858 }
1859
1860 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08001861 * Get the preferred network type.
1862 * Used for device configuration by some CDMA operators.
1863 *
1864 * @return the preferred network type, defined in RILConstants.java.
1865 */
1866 @Override
1867 public int getPreferredNetworkType() {
Junda Liua2e36012014-07-09 18:30:01 -07001868 enforceModifyPermissionOrCarrierPrivilege();
Jake Hamby7c27be32014-03-03 13:25:59 -08001869 if (DBG) log("getPreferredNetworkType");
1870 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null);
1871 int networkType = (result != null ? result[0] : -1);
1872 if (DBG) log("getPreferredNetworkType: " + networkType);
1873 return networkType;
1874 }
1875
1876 /**
1877 * Set the preferred network type.
1878 * Used for device configuration by some CDMA operators.
1879 *
1880 * @param networkType the preferred network type, defined in RILConstants.java.
1881 * @return true on success; false on any failure.
1882 */
1883 @Override
1884 public boolean setPreferredNetworkType(int networkType) {
Junda Liua2e36012014-07-09 18:30:01 -07001885 enforceModifyPermissionOrCarrierPrivilege();
Jake Hamby7c27be32014-03-03 13:25:59 -08001886 if (DBG) log("setPreferredNetworkType: type " + networkType);
1887 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType);
1888 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
Junda Liu80bc0d12014-07-14 16:36:44 -07001889 if (success) {
1890 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
1891 Settings.Global.PREFERRED_NETWORK_MODE, networkType);
1892 }
Jake Hamby7c27be32014-03-03 13:25:59 -08001893 return success;
1894 }
Robert Greenwalted86e582014-05-21 20:03:20 -07001895
1896 /**
1897 * Set mobile data enabled
1898 * Used by the user through settings etc to turn on/off mobile data
1899 *
1900 * @param enable {@code true} turn turn data on, else {@code false}
1901 */
1902 @Override
1903 public void setDataEnabled(boolean enable) {
1904 enforceModifyPermission();
1905 mPhone.setDataEnabled(enable);
1906 }
1907
1908 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07001909 * Get whether mobile data is enabled.
1910 *
1911 * Note that this used to be available from ConnectivityService, gated by
1912 * ACCESS_NETWORK_STATE permission, so this will accept either that or
1913 * our MODIFY_PHONE_STATE.
Robert Greenwalted86e582014-05-21 20:03:20 -07001914 *
1915 * @return {@code true} if data is enabled else {@code false}
1916 */
1917 @Override
1918 public boolean getDataEnabled() {
Robert Greenwalt646120a2014-05-23 11:54:03 -07001919 try {
1920 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
1921 null);
1922 } catch (Exception e) {
1923 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE,
1924 null);
1925 }
Robert Greenwalted86e582014-05-21 20:03:20 -07001926 return mPhone.getDataEnabled();
1927 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07001928
1929 @Override
1930 public int hasCarrierPrivileges() {
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07001931 UiccCard card = UiccController.getInstance().getUiccCard();
1932 if (card == null) {
1933 loge("hasCarrierPrivileges: No UICC");
1934 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
1935 }
1936 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001937 mPhone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07001938 }
Junda Liu29340342014-07-10 15:23:27 -07001939
1940 @Override
Shishir Agrawal6d5a2852014-07-11 16:32:57 -07001941 public int checkCarrierPrivilegesForPackage(String pkgname) {
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07001942 UiccCard card = UiccController.getInstance().getUiccCard();
1943 if (card == null) {
1944 loge("checkCarrierPrivilegesForPackage: No UICC");
1945 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
1946 }
1947 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgname);
Junda Liu29340342014-07-10 15:23:27 -07001948 }
Derek Tan89e89d42014-07-08 17:00:10 -07001949
1950 @Override
Diego Pontorieroaf74c862014-08-28 11:51:16 -07001951 public List<String> getCarrierPackageNamesForIntent(Intent intent) {
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07001952 UiccCard card = UiccController.getInstance().getUiccCard();
1953 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07001954 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07001955 return null ;
1956 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07001957 return card.getCarrierPackageNamesForIntent(
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07001958 mPhone.getContext().getPackageManager(), intent);
1959 }
1960
Wink Savilleb564aae2014-10-23 10:18:09 -07001961 private String getIccId(int subId) {
Derek Tan97ebb422014-09-05 16:55:38 -07001962 UiccCard card = getPhone(subId).getUiccCard();
1963 if (card == null) {
1964 loge("getIccId: No UICC");
1965 return null;
1966 }
1967 String iccId = card.getIccId();
1968 if (TextUtils.isEmpty(iccId)) {
1969 loge("getIccId: ICC ID is null or empty.");
1970 return null;
1971 }
1972 return iccId;
1973 }
1974
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07001975 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001976 public void setLine1NumberForDisplayForSubscriber(int subId, String alphaTag, String number) {
Derek Tan352d8cd2014-07-12 12:57:11 -07001977 enforceModifyPermissionOrCarrierPrivilege();
Derek Tan97ebb422014-09-05 16:55:38 -07001978
1979 String iccId = getIccId(subId);
1980 if (iccId != null) {
1981 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07001982 SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
Derek Tan97ebb422014-09-05 16:55:38 -07001983 if (alphaTag == null) {
1984 editor.remove(alphaTagPrefKey);
1985 } else {
1986 editor.putString(alphaTagPrefKey, alphaTag);
1987 }
1988
1989 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
1990 if (number == null) {
1991 editor.remove(numberPrefKey);
1992 } else {
1993 editor.putString(numberPrefKey, number);
1994 }
1995 editor.commit();
1996 }
Derek Tan7226c842014-07-02 17:42:23 -07001997 }
1998
1999 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002000 public String getLine1NumberForDisplay(int subId) {
Derek Tan7226c842014-07-02 17:42:23 -07002001 enforceReadPermission();
Derek Tan97ebb422014-09-05 16:55:38 -07002002
2003 String iccId = getIccId(subId);
2004 if (iccId != null) {
2005 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002006 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002007 }
Derek Tan97ebb422014-09-05 16:55:38 -07002008 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002009 }
2010
2011 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002012 public String getLine1AlphaTagForDisplay(int subId) {
Derek Tan7226c842014-07-02 17:42:23 -07002013 enforceReadPermission();
Derek Tan97ebb422014-09-05 16:55:38 -07002014
2015 String iccId = getIccId(subId);
2016 if (iccId != null) {
2017 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002018 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002019 }
Derek Tan97ebb422014-09-05 16:55:38 -07002020 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002021 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002022
2023 @Override
Shishir Agrawala3dfd752014-09-04 13:25:42 -07002024 public boolean setOperatorBrandOverride(String brand) {
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002025 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawala3dfd752014-09-04 13:25:42 -07002026 return mPhone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002027 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05002028
2029 @Override
2030 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
2031 enforceModifyPermission();
2032
2033 int returnValue = 0;
2034 try {
2035 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
2036 if(result.exception == null) {
2037 if (result.result != null) {
2038 byte[] responseData = (byte[])(result.result);
2039 if(responseData.length > oemResp.length) {
2040 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
2041 responseData.length + "bytes. Buffer Size is " +
2042 oemResp.length + "bytes.");
2043 }
2044 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
2045 returnValue = responseData.length;
2046 }
2047 } else {
2048 CommandException ex = (CommandException) result.exception;
2049 returnValue = ex.getCommandError().ordinal();
2050 if(returnValue > 0) returnValue *= -1;
2051 }
2052 } catch (RuntimeException e) {
2053 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
2054 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
2055 if(returnValue > 0) returnValue *= -1;
2056 }
2057
2058 return returnValue;
2059 }
Wink Saville5d475dd2014-10-17 15:00:58 -07002060
2061 @Override
2062 public void setRadioCapability(RadioAccessFamily[] rafs) {
2063 try {
2064 ProxyController.getInstance().setRadioCapability(rafs);
2065 } catch (RuntimeException e) {
2066 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
2067 }
2068 }
2069
2070 @Override
2071 public int getRadioAccessFamily(int phoneId) {
2072 return ProxyController.getInstance().getRadioAccessFamily(phoneId);
2073 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002074
2075 @Override
2076 public void enableVideoCalling(boolean enable) {
2077 enforceModifyPermission();
2078 SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2079 editor.putBoolean(PREF_ENABLE_VIDEO_CALLING, enable);
2080 editor.commit();
2081 }
2082
2083 @Override
2084 public boolean isVideoCallingEnabled() {
2085 enforceReadPermission();
Andrew Lee77527ac2014-10-21 16:57:39 -07002086 // Check the user preference and the system-level IMS setting. Even if the user has
2087 // enabled video calling, if IMS is disabled we aren't able to support video calling.
2088 // In the long run, we may instead need to check if there exists a connection service
2089 // which can support video calling.
Andrew Lee312e8172014-10-23 17:01:36 -07002090 return ImsManager.isVtEnabledByPlatform(mPhone.getContext())
2091 && ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
2092 && mTelephonySharedPreferences.getBoolean(PREF_ENABLE_VIDEO_CALLING, true);
Andrew Leedf14ead2014-10-17 14:22:52 -07002093 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002094}