blob: 9992edf689cdbb63a826a7e4f8300ef888b65e90 [file] [log] [blame]
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.phone;
18
19import android.app.ActivityManager;
20import android.app.AppOpsManager;
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -070021import android.content.ComponentName;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070022import android.content.Context;
23import android.content.Intent;
Derek Tan97ebb422014-09-05 16:55:38 -070024import android.content.SharedPreferences;
Shishir Agrawal60f9c952014-06-23 12:00:43 -070025import android.content.pm.PackageManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070026import android.net.Uri;
27import android.os.AsyncResult;
28import android.os.Binder;
29import android.os.Bundle;
30import android.os.Handler;
31import android.os.Looper;
32import android.os.Message;
33import android.os.Process;
34import android.os.ServiceManager;
35import android.os.UserHandle;
Derek Tan97ebb422014-09-05 16:55:38 -070036import android.preference.PreferenceManager;
Ihab Awadf2177b72013-11-25 13:33:23 -080037import android.provider.Settings;
Santos Cordon7a1885b2015-02-03 11:15:19 -080038import android.telecom.PhoneAccount;
Andrew Lee9431b832015-03-09 18:46:45 -070039import android.telecom.TelecomManager;
Junda Liu12f7d802015-05-01 12:06:44 -070040import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070041import android.telephony.CellInfo;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -070042import android.telephony.IccOpenLogicalChannelResponse;
Jake Hambye994d462014-02-03 13:10:13 -080043import android.telephony.NeighboringCellInfo;
Wink Saville5d475dd2014-10-17 15:00:58 -070044import android.telephony.RadioAccessFamily;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070045import android.telephony.ServiceState;
Wink Saville0f3b5fc2014-11-11 08:40:49 -080046import android.telephony.SubscriptionInfo;
Jeff Sharkey85190e62014-12-05 09:40:12 -080047import android.telephony.SubscriptionManager;
Ihab Awadf2177b72013-11-25 13:33:23 -080048import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070049import android.text.TextUtils;
Jeff Sharkey85190e62014-12-05 09:40:12 -080050import android.util.ArrayMap;
51import android.util.ArraySet;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070052import android.util.Log;
Jake Hambye994d462014-02-03 13:10:13 -080053import android.util.Pair;
Jeff Sharkey85190e62014-12-05 09:40:12 -080054import android.util.Slog;
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;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070059import com.android.internal.telephony.DefaultPhoneNotifier;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070060import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -080061import com.android.internal.telephony.IccCard;
Narayan Kamath1c496c22015-04-16 14:40:19 +010062import com.android.internal.telephony.MccTable;
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.ProxyController;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070066import com.android.internal.telephony.PhoneConstants;
Svet Ganovb320e182015-04-16 12:30:10 -070067import com.android.internal.telephony.RILConstants;
Wink Savilleac1bdfd2014-11-20 23:04:44 -080068import com.android.internal.telephony.SubscriptionController;
Shishir Agrawal566b7612013-10-28 14:41:00 -070069import com.android.internal.telephony.uicc.IccIoResult;
70import com.android.internal.telephony.uicc.IccUtils;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -070071import com.android.internal.telephony.uicc.UiccCard;
Shishir Agrawal566b7612013-10-28 14:41:00 -070072import com.android.internal.telephony.uicc.UiccController;
Jake Hambye994d462014-02-03 13:10:13 -080073import com.android.internal.util.HexDump;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070074
Wink Saville36469e72014-06-11 15:17:00 -070075import static com.android.internal.telephony.PhoneConstants.SUBSCRIPTION_KEY;
76
Santos Cordon7d4ddf62013-07-10 11:58:08 -070077import java.util.ArrayList;
Jeff Sharkey85190e62014-12-05 09:40:12 -080078import java.util.Arrays;
Shishir Agrawal621a47c2014-12-01 10:25:09 -080079import java.util.HashMap;
80import java.util.Iterator;
Jake Hambye994d462014-02-03 13:10:13 -080081import java.util.List;
Narayan Kamath1c496c22015-04-16 14:40:19 +010082import java.util.Locale;
Jeff Sharkey85190e62014-12-05 09:40:12 -080083import java.util.Map;
84import java.util.Objects;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070085
86/**
87 * Implementation of the ITelephony interface.
88 */
Santos Cordon117fee72014-05-16 17:56:12 -070089public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -070090 private static final String LOG_TAG = "PhoneInterfaceManager";
91 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
92 private static final boolean DBG_LOC = false;
Jeff Sharkey85190e62014-12-05 09:40:12 -080093 private static final boolean DBG_MERGE = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070094
95 // Message codes used with mMainThreadHandler
96 private static final int CMD_HANDLE_PIN_MMI = 1;
97 private static final int CMD_HANDLE_NEIGHBORING_CELL = 2;
98 private static final int EVENT_NEIGHBORING_CELL_DONE = 3;
99 private static final int CMD_ANSWER_RINGING_CALL = 4;
100 private static final int CMD_END_CALL = 5; // not used yet
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700101 private static final int CMD_TRANSMIT_APDU_LOGICAL_CHANNEL = 7;
102 private static final int EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE = 8;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700103 private static final int CMD_OPEN_CHANNEL = 9;
104 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
105 private static final int CMD_CLOSE_CHANNEL = 11;
106 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -0800107 private static final int CMD_NV_READ_ITEM = 13;
108 private static final int EVENT_NV_READ_ITEM_DONE = 14;
109 private static final int CMD_NV_WRITE_ITEM = 15;
110 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
111 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
112 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
113 private static final int CMD_NV_RESET_CONFIG = 19;
114 private static final int EVENT_NV_RESET_CONFIG_DONE = 20;
Jake Hamby7c27be32014-03-03 13:25:59 -0800115 private static final int CMD_GET_PREFERRED_NETWORK_TYPE = 21;
116 private static final int EVENT_GET_PREFERRED_NETWORK_TYPE_DONE = 22;
117 private static final int CMD_SET_PREFERRED_NETWORK_TYPE = 23;
118 private static final int EVENT_SET_PREFERRED_NETWORK_TYPE_DONE = 24;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800119 private static final int CMD_SEND_ENVELOPE = 25;
120 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Derek Tan6b088ee2014-09-05 14:15:18 -0700121 private static final int CMD_INVOKE_OEM_RIL_REQUEST_RAW = 27;
122 private static final int EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE = 28;
123 private static final int CMD_TRANSMIT_APDU_BASIC_CHANNEL = 29;
124 private static final int EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE = 30;
125 private static final int CMD_EXCHANGE_SIM_IO = 31;
126 private static final int EVENT_EXCHANGE_SIM_IO_DONE = 32;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800127 private static final int CMD_SET_VOICEMAIL_NUMBER = 33;
128 private static final int EVENT_SET_VOICEMAIL_NUMBER_DONE = 34;
Stuart Scott54788802015-03-30 13:18:01 -0700129 private static final int CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC = 35;
130 private static final int EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE = 36;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700131
132 /** The singleton instance. */
133 private static PhoneInterfaceManager sInstance;
134
Wink Saville3ab207e2014-11-20 13:07:20 -0800135 private PhoneGlobals mApp;
136 private Phone mPhone;
137 private CallManager mCM;
138 private AppOpsManager mAppOps;
139 private MainThreadHandler mMainThreadHandler;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800140 private SubscriptionController mSubscriptionController;
Wink Saville3ab207e2014-11-20 13:07:20 -0800141 private SharedPreferences mTelephonySharedPreferences;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700142
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_";
Jeff Sharkey85190e62014-12-05 09:40:12 -0800145 private static final String PREF_CARRIERS_SUBSCRIBER_PREFIX = "carrier_subscriber_";
Andrew Leedf14ead2014-10-17 14:22:52 -0700146 private static final String PREF_ENABLE_VIDEO_CALLING = "enable_video_calling";
Derek Tan89e89d42014-07-08 17:00:10 -0700147
148 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700149 * A request object to use for transmitting data to an ICC.
150 */
151 private static final class IccAPDUArgument {
152 public int channel, cla, command, p1, p2, p3;
153 public String data;
154
155 public IccAPDUArgument(int channel, int cla, int command,
156 int p1, int p2, int p3, String data) {
157 this.channel = channel;
158 this.cla = cla;
159 this.command = command;
160 this.p1 = p1;
161 this.p2 = p2;
162 this.p3 = p3;
163 this.data = data;
164 }
165 }
166
167 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700168 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
169 * request after sending. The main thread will notify the request when it is complete.
170 */
171 private static final class MainThreadRequest {
172 /** The argument to use for the request */
173 public Object argument;
174 /** The result of the request that is run on the main thread */
175 public Object result;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800176 /** The subscriber id that this request applies to. Null if default. */
177 public Integer subId;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700178
179 public MainThreadRequest(Object argument) {
180 this.argument = argument;
181 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800182
183 public MainThreadRequest(Object argument, Integer subId) {
184 this.argument = argument;
185 this.subId = subId;
186 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700187 }
188
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800189 private static final class IncomingThirdPartyCallArgs {
190 public final ComponentName component;
191 public final String callId;
192 public final String callerDisplayName;
193
194 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
195 String callerDisplayName) {
196 this.component = component;
197 this.callId = callId;
198 this.callerDisplayName = callerDisplayName;
199 }
200 }
201
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700202 /**
203 * A handler that processes messages on the main thread in the phone process. Since many
204 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
205 * inbound binder threads to the main thread in the phone process. The Binder thread
206 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
207 * on, which will be notified when the operation completes and will contain the result of the
208 * request.
209 *
210 * <p>If a MainThreadRequest object is provided in the msg.obj field,
211 * note that request.result must be set to something non-null for the calling thread to
212 * unblock.
213 */
214 private final class MainThreadHandler extends Handler {
215 @Override
216 public void handleMessage(Message msg) {
217 MainThreadRequest request;
218 Message onCompleted;
219 AsyncResult ar;
Shishir Agrawal21409252015-01-15 23:33:50 -0800220 UiccCard uiccCard = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700221 IccAPDUArgument iccArgument;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700222
223 switch (msg.what) {
224 case CMD_HANDLE_PIN_MMI:
225 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800226 request.result = getPhoneFromRequest(request).handlePinMmi(
227 (String) request.argument);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700228 // Wake up the requesting thread
229 synchronized (request) {
230 request.notifyAll();
231 }
232 break;
233
234 case CMD_HANDLE_NEIGHBORING_CELL:
235 request = (MainThreadRequest) msg.obj;
236 onCompleted = obtainMessage(EVENT_NEIGHBORING_CELL_DONE,
237 request);
238 mPhone.getNeighboringCids(onCompleted);
239 break;
240
241 case EVENT_NEIGHBORING_CELL_DONE:
242 ar = (AsyncResult) msg.obj;
243 request = (MainThreadRequest) ar.userObj;
244 if (ar.exception == null && ar.result != null) {
245 request.result = ar.result;
246 } else {
247 // create an empty list to notify the waiting thread
Jake Hambye994d462014-02-03 13:10:13 -0800248 request.result = new ArrayList<NeighboringCellInfo>(0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700249 }
250 // Wake up the requesting thread
251 synchronized (request) {
252 request.notifyAll();
253 }
254 break;
255
256 case CMD_ANSWER_RINGING_CALL:
Wink Saville08874612014-08-31 19:19:58 -0700257 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800258 int answer_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700259 answerRingingCallInternal(answer_subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700260 break;
261
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700262 case CMD_END_CALL:
263 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800264 int end_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700265 final boolean hungUp;
Anthony Leeae4e36d2015-05-21 07:17:46 -0700266 Phone phone = getPhone(end_subId);
267 if (phone == null) {
268 if (DBG) log("CMD_END_CALL: no phone for id: " + end_subId);
269 break;
270 }
271 int phoneType = phone.getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700272 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
273 // CDMA: If the user presses the Power button we treat it as
274 // ending the complete call session
Wink Saville08874612014-08-31 19:19:58 -0700275 hungUp = PhoneUtils.hangupRingingAndActive(getPhone(end_subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700276 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
277 // GSM: End the call as per the Phone state
278 hungUp = PhoneUtils.hangup(mCM);
279 } else {
280 throw new IllegalStateException("Unexpected phone type: " + phoneType);
281 }
282 if (DBG) log("CMD_END_CALL: " + (hungUp ? "hung up!" : "no call to hang up"));
283 request.result = hungUp;
284 // Wake up the requesting thread
285 synchronized (request) {
286 request.notifyAll();
287 }
288 break;
289
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700290 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700291 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700292 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700293 if (uiccCard == null) {
294 loge("iccTransmitApduLogicalChannel: No UICC");
295 request.result = new IccIoResult(0x6F, 0, (byte[])null);
296 synchronized (request) {
297 request.notifyAll();
298 }
299 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700300 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
301 request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700302 uiccCard.iccTransmitApduLogicalChannel(
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700303 iccArgument.channel, iccArgument.cla, iccArgument.command,
304 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
Shishir Agrawal566b7612013-10-28 14:41:00 -0700305 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700306 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700307 break;
308
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700309 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700310 ar = (AsyncResult) msg.obj;
311 request = (MainThreadRequest) ar.userObj;
312 if (ar.exception == null && ar.result != null) {
313 request.result = ar.result;
314 } else {
315 request.result = new IccIoResult(0x6F, 0, (byte[])null);
316 if (ar.result == null) {
317 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800318 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700319 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800320 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700321 } else {
322 loge("iccTransmitApduLogicalChannel: Unknown exception");
323 }
324 }
325 synchronized (request) {
326 request.notifyAll();
327 }
328 break;
329
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700330 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
331 request = (MainThreadRequest) msg.obj;
332 iccArgument = (IccAPDUArgument) request.argument;
333 if (uiccCard == null) {
334 loge("iccTransmitApduBasicChannel: No UICC");
335 request.result = new IccIoResult(0x6F, 0, (byte[])null);
336 synchronized (request) {
337 request.notifyAll();
338 }
339 } else {
340 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
341 request);
342 uiccCard.iccTransmitApduBasicChannel(
343 iccArgument.cla, iccArgument.command, iccArgument.p1, iccArgument.p2,
344 iccArgument.p3, iccArgument.data, onCompleted);
345 }
346 break;
347
348 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
349 ar = (AsyncResult) msg.obj;
350 request = (MainThreadRequest) ar.userObj;
351 if (ar.exception == null && ar.result != null) {
352 request.result = ar.result;
353 } else {
354 request.result = new IccIoResult(0x6F, 0, (byte[])null);
355 if (ar.result == null) {
356 loge("iccTransmitApduBasicChannel: Empty response");
357 } else if (ar.exception instanceof CommandException) {
358 loge("iccTransmitApduBasicChannel: CommandException: " +
359 ar.exception);
360 } else {
361 loge("iccTransmitApduBasicChannel: Unknown exception");
362 }
363 }
364 synchronized (request) {
365 request.notifyAll();
366 }
367 break;
368
369 case CMD_EXCHANGE_SIM_IO:
370 request = (MainThreadRequest) msg.obj;
371 iccArgument = (IccAPDUArgument) request.argument;
372 if (uiccCard == null) {
373 loge("iccExchangeSimIO: No UICC");
374 request.result = new IccIoResult(0x6F, 0, (byte[])null);
375 synchronized (request) {
376 request.notifyAll();
377 }
378 } else {
379 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
380 request);
381 uiccCard.iccExchangeSimIO(iccArgument.cla, /* fileID */
382 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
383 iccArgument.data, onCompleted);
384 }
385 break;
386
387 case EVENT_EXCHANGE_SIM_IO_DONE:
388 ar = (AsyncResult) msg.obj;
389 request = (MainThreadRequest) ar.userObj;
390 if (ar.exception == null && ar.result != null) {
391 request.result = ar.result;
392 } else {
393 request.result = new IccIoResult(0x6f, 0, (byte[])null);
394 }
395 synchronized (request) {
396 request.notifyAll();
397 }
398 break;
399
Derek Tan4d5e5c12014-02-04 11:54:58 -0800400 case CMD_SEND_ENVELOPE:
401 request = (MainThreadRequest) msg.obj;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700402 if (uiccCard == null) {
403 loge("sendEnvelopeWithStatus: No UICC");
404 request.result = new IccIoResult(0x6F, 0, (byte[])null);
405 synchronized (request) {
406 request.notifyAll();
407 }
408 } else {
409 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
410 uiccCard.sendEnvelopeWithStatus((String)request.argument, onCompleted);
411 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800412 break;
413
414 case EVENT_SEND_ENVELOPE_DONE:
415 ar = (AsyncResult) msg.obj;
416 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700417 if (ar.exception == null && ar.result != null) {
418 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800419 } else {
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700420 request.result = new IccIoResult(0x6F, 0, (byte[])null);
421 if (ar.result == null) {
422 loge("sendEnvelopeWithStatus: Empty response");
423 } else if (ar.exception instanceof CommandException) {
424 loge("sendEnvelopeWithStatus: CommandException: " +
425 ar.exception);
426 } else {
427 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
428 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800429 }
430 synchronized (request) {
431 request.notifyAll();
432 }
433 break;
434
Shishir Agrawal566b7612013-10-28 14:41:00 -0700435 case CMD_OPEN_CHANNEL:
436 request = (MainThreadRequest) msg.obj;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700437 if (uiccCard == null) {
438 loge("iccOpenLogicalChannel: No UICC");
439 request.result = new IccIoResult(0x6F, 0, (byte[])null);
440 synchronized (request) {
441 request.notifyAll();
442 }
443 } else {
444 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
445 uiccCard.iccOpenLogicalChannel((String)request.argument, onCompleted);
446 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700447 break;
448
449 case EVENT_OPEN_CHANNEL_DONE:
450 ar = (AsyncResult) msg.obj;
451 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700452 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700453 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700454 int[] result = (int[]) ar.result;
455 int channelId = result[0];
456 byte[] selectResponse = null;
457 if (result.length > 1) {
458 selectResponse = new byte[result.length - 1];
459 for (int i = 1; i < result.length; ++i) {
460 selectResponse[i - 1] = (byte) result[i];
461 }
462 }
463 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700464 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700465 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700466 if (ar.result == null) {
467 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700468 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700469 if (ar.exception != null) {
470 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
471 }
472
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700473 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Junda Liua754ba12015-05-20 01:17:52 -0700474 if (ar.exception instanceof CommandException) {
475 CommandException.Error error =
476 ((CommandException) (ar.exception)).getCommandError();
477 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700478 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700479 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700480 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700481 }
482 }
483 openChannelResp = new IccOpenLogicalChannelResponse(
484 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700485 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700486 request.result = openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700487 synchronized (request) {
488 request.notifyAll();
489 }
490 break;
491
492 case CMD_CLOSE_CHANNEL:
493 request = (MainThreadRequest) msg.obj;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700494 if (uiccCard == null) {
495 loge("iccCloseLogicalChannel: No UICC");
496 request.result = new IccIoResult(0x6F, 0, (byte[])null);
497 synchronized (request) {
498 request.notifyAll();
499 }
500 } else {
501 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
502 uiccCard.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
503 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700504 break;
505
506 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800507 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
508 break;
509
510 case CMD_NV_READ_ITEM:
511 request = (MainThreadRequest) msg.obj;
512 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
513 mPhone.nvReadItem((Integer) request.argument, onCompleted);
514 break;
515
516 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700517 ar = (AsyncResult) msg.obj;
518 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800519 if (ar.exception == null && ar.result != null) {
520 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700521 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800522 request.result = "";
523 if (ar.result == null) {
524 loge("nvReadItem: Empty response");
525 } else if (ar.exception instanceof CommandException) {
526 loge("nvReadItem: CommandException: " +
527 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700528 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800529 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700530 }
531 }
532 synchronized (request) {
533 request.notifyAll();
534 }
535 break;
536
Jake Hambye994d462014-02-03 13:10:13 -0800537 case CMD_NV_WRITE_ITEM:
538 request = (MainThreadRequest) msg.obj;
539 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
540 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
541 mPhone.nvWriteItem(idValue.first, idValue.second, onCompleted);
542 break;
543
544 case EVENT_NV_WRITE_ITEM_DONE:
545 handleNullReturnEvent(msg, "nvWriteItem");
546 break;
547
548 case CMD_NV_WRITE_CDMA_PRL:
549 request = (MainThreadRequest) msg.obj;
550 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
551 mPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
552 break;
553
554 case EVENT_NV_WRITE_CDMA_PRL_DONE:
555 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
556 break;
557
558 case CMD_NV_RESET_CONFIG:
559 request = (MainThreadRequest) msg.obj;
560 onCompleted = obtainMessage(EVENT_NV_RESET_CONFIG_DONE, request);
561 mPhone.nvResetConfig((Integer) request.argument, onCompleted);
562 break;
563
564 case EVENT_NV_RESET_CONFIG_DONE:
565 handleNullReturnEvent(msg, "nvResetConfig");
566 break;
567
Jake Hamby7c27be32014-03-03 13:25:59 -0800568 case CMD_GET_PREFERRED_NETWORK_TYPE:
569 request = (MainThreadRequest) msg.obj;
570 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
Stuart Scott54788802015-03-30 13:18:01 -0700571 getPhoneFromRequest(request).getPreferredNetworkType(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800572 break;
573
574 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
575 ar = (AsyncResult) msg.obj;
576 request = (MainThreadRequest) ar.userObj;
577 if (ar.exception == null && ar.result != null) {
578 request.result = ar.result; // Integer
579 } else {
580 request.result = -1;
581 if (ar.result == null) {
582 loge("getPreferredNetworkType: Empty response");
583 } else if (ar.exception instanceof CommandException) {
584 loge("getPreferredNetworkType: CommandException: " +
585 ar.exception);
586 } else {
587 loge("getPreferredNetworkType: Unknown exception");
588 }
589 }
590 synchronized (request) {
591 request.notifyAll();
592 }
593 break;
594
595 case CMD_SET_PREFERRED_NETWORK_TYPE:
596 request = (MainThreadRequest) msg.obj;
597 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
598 int networkType = (Integer) request.argument;
Stuart Scott54788802015-03-30 13:18:01 -0700599 getPhoneFromRequest(request).setPreferredNetworkType(networkType, onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800600 break;
601
602 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
603 handleNullReturnEvent(msg, "setPreferredNetworkType");
604 break;
605
Steven Liu4bf01bc2014-07-17 11:05:29 -0500606 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
607 request = (MainThreadRequest)msg.obj;
608 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
609 mPhone.invokeOemRilRequestRaw((byte[])request.argument, onCompleted);
610 break;
611
612 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
613 ar = (AsyncResult)msg.obj;
614 request = (MainThreadRequest)ar.userObj;
615 request.result = ar;
616 synchronized (request) {
617 request.notifyAll();
618 }
619 break;
620
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800621 case CMD_SET_VOICEMAIL_NUMBER:
622 request = (MainThreadRequest) msg.obj;
623 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
624 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -0800625 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
626 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800627 break;
628
629 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
630 handleNullReturnEvent(msg, "setVoicemailNumber");
631 break;
632
Stuart Scott54788802015-03-30 13:18:01 -0700633 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
634 request = (MainThreadRequest) msg.obj;
635 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
636 request);
637 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
638 break;
639
640 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
641 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
642 break;
643
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700644 default:
645 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
646 break;
647 }
648 }
Jake Hambye994d462014-02-03 13:10:13 -0800649
650 private void handleNullReturnEvent(Message msg, String command) {
651 AsyncResult ar = (AsyncResult) msg.obj;
652 MainThreadRequest request = (MainThreadRequest) ar.userObj;
653 if (ar.exception == null) {
654 request.result = true;
655 } else {
656 request.result = false;
657 if (ar.exception instanceof CommandException) {
658 loge(command + ": CommandException: " + ar.exception);
659 } else {
660 loge(command + ": Unknown exception");
661 }
662 }
663 synchronized (request) {
664 request.notifyAll();
665 }
666 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700667 }
668
669 /**
670 * Posts the specified command to be executed on the main thread,
671 * waits for the request to complete, and returns the result.
672 * @see #sendRequestAsync
673 */
674 private Object sendRequest(int command, Object argument) {
Santos Cordon500b0e02014-06-17 10:33:33 -0700675 return sendRequest(command, argument, null);
Wink Saville36469e72014-06-11 15:17:00 -0700676 }
677
678 /**
679 * Posts the specified command to be executed on the main thread,
680 * waits for the request to complete, and returns the result.
681 * @see #sendRequestAsync
682 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800683 private Object sendRequest(int command, Object argument, Integer subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700684 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
685 throw new RuntimeException("This method will deadlock if called from the main thread.");
686 }
687
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800688 MainThreadRequest request = new MainThreadRequest(argument, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700689 Message msg = mMainThreadHandler.obtainMessage(command, request);
690 msg.sendToTarget();
691
692 // Wait for the request to complete
693 synchronized (request) {
694 while (request.result == null) {
695 try {
696 request.wait();
697 } catch (InterruptedException e) {
698 // Do nothing, go back and wait until the request is complete
699 }
700 }
701 }
702 return request.result;
703 }
704
705 /**
706 * Asynchronous ("fire and forget") version of sendRequest():
707 * Posts the specified command to be executed on the main thread, and
708 * returns immediately.
709 * @see #sendRequest
710 */
711 private void sendRequestAsync(int command) {
712 mMainThreadHandler.sendEmptyMessage(command);
713 }
714
715 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -0700716 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
717 * @see {@link #sendRequest(int,Object)}
718 */
719 private void sendRequestAsync(int command, Object argument) {
720 MainThreadRequest request = new MainThreadRequest(argument);
721 Message msg = mMainThreadHandler.obtainMessage(command, request);
722 msg.sendToTarget();
723 }
724
725 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700726 * Initialize the singleton PhoneInterfaceManager instance.
727 * This is only done once, at startup, from PhoneApp.onCreate().
728 */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700729 /* package */ static PhoneInterfaceManager init(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700730 synchronized (PhoneInterfaceManager.class) {
731 if (sInstance == null) {
Sailesh Nepal194161e2014-07-03 08:57:44 -0700732 sInstance = new PhoneInterfaceManager(app, phone);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700733 } else {
734 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
735 }
736 return sInstance;
737 }
738 }
739
740 /** Private constructor; @see init() */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700741 private PhoneInterfaceManager(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700742 mApp = app;
743 mPhone = phone;
744 mCM = PhoneGlobals.getInstance().mCM;
745 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
746 mMainThreadHandler = new MainThreadHandler();
Andrew Leedf14ead2014-10-17 14:22:52 -0700747 mTelephonySharedPreferences =
Derek Tan97ebb422014-09-05 16:55:38 -0700748 PreferenceManager.getDefaultSharedPreferences(mPhone.getContext());
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800749 mSubscriptionController = SubscriptionController.getInstance();
Wink Saville3ab207e2014-11-20 13:07:20 -0800750
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700751 publish();
752 }
753
754 private void publish() {
755 if (DBG) log("publish: " + this);
756
757 ServiceManager.addService("phone", this);
758 }
759
Stuart Scott584921c2015-01-15 17:10:34 -0800760 private Phone getPhoneFromRequest(MainThreadRequest request) {
761 return (request.subId == null) ? mPhone : getPhone(request.subId);
762 }
763
Wink Saville36469e72014-06-11 15:17:00 -0700764 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -0700765 private Phone getPhone(int subId) {
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800766 return PhoneFactory.getPhone(mSubscriptionController.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -0700767 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700768 //
769 // Implementation of the ITelephony interface.
770 //
771
772 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700773 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -0700774 }
775
Wink Savilleb564aae2014-10-23 10:18:09 -0700776 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700777 if (DBG) log("dial: " + number);
778 // No permission check needed here: This is just a wrapper around the
779 // ACTION_DIAL intent, which is available to any app since it puts up
780 // the UI before it does anything.
781
782 String url = createTelUrl(number);
783 if (url == null) {
784 return;
785 }
786
787 // PENDING: should we just silently fail if phone is offhook or ringing?
Wink Saville36469e72014-06-11 15:17:00 -0700788 PhoneConstants.State state = mCM.getState(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700789 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
790 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
791 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
792 mApp.startActivity(intent);
793 }
794 }
795
796 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700797 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -0700798 }
799
Wink Savilleb564aae2014-10-23 10:18:09 -0700800 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700801 if (DBG) log("call: " + number);
802
803 // This is just a wrapper around the ACTION_CALL intent, but we still
804 // need to do a permission check since we're calling startActivity()
805 // from the context of the phone app.
806 enforceCallPermission();
807
808 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
809 != AppOpsManager.MODE_ALLOWED) {
810 return;
811 }
812
813 String url = createTelUrl(number);
814 if (url == null) {
815 return;
816 }
817
Wink Saville08874612014-08-31 19:19:58 -0700818 boolean isValid = false;
Narayan Kamath1c496c22015-04-16 14:40:19 +0100819 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoList();
Wink Saville3ab207e2014-11-20 13:07:20 -0800820 if (slist != null) {
821 for (SubscriptionInfo subInfoRecord : slist) {
822 if (subInfoRecord.getSubscriptionId() == subId) {
823 isValid = true;
824 break;
825 }
Wink Saville08874612014-08-31 19:19:58 -0700826 }
827 }
828 if (isValid == false) {
829 return;
830 }
831
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700832 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
Wink Saville36469e72014-06-11 15:17:00 -0700833 intent.putExtra(SUBSCRIPTION_KEY, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700834 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
835 mApp.startActivity(intent);
836 }
837
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700838 /**
839 * End a call based on call state
840 * @return true is a call was ended
841 */
842 public boolean endCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700843 return endCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700844 }
845
846 /**
847 * End a call based on the call state of the subId
848 * @return true is a call was ended
849 */
Wink Savilleb564aae2014-10-23 10:18:09 -0700850 public boolean endCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700851 enforceCallPermission();
Stuart Scott584921c2015-01-15 17:10:34 -0800852 return (Boolean) sendRequest(CMD_END_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700853 }
854
855 public void answerRingingCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700856 answerRingingCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700857 }
858
Wink Savilleb564aae2014-10-23 10:18:09 -0700859 public void answerRingingCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700860 if (DBG) log("answerRingingCall...");
861 // TODO: there should eventually be a separate "ANSWER_PHONE" permission,
862 // but that can probably wait till the big TelephonyManager API overhaul.
863 // For now, protect this call with the MODIFY_PHONE_STATE permission.
864 enforceModifyPermission();
Stuart Scott584921c2015-01-15 17:10:34 -0800865 sendRequest(CMD_ANSWER_RINGING_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700866 }
867
868 /**
869 * Make the actual telephony calls to implement answerRingingCall().
870 * This should only be called from the main thread of the Phone app.
871 * @see #answerRingingCall
872 *
873 * TODO: it would be nice to return true if we answered the call, or
874 * false if there wasn't actually a ringing incoming call, or some
875 * other error occurred. (In other words, pass back the return value
876 * from PhoneUtils.answerCall() or PhoneUtils.answerAndEndActive().)
877 * But that would require calling this method via sendRequest() rather
878 * than sendRequestAsync(), and right now we don't actually *need* that
879 * return value, so let's just return void for now.
880 */
Wink Savilleb564aae2014-10-23 10:18:09 -0700881 private void answerRingingCallInternal(int subId) {
Wink Saville08874612014-08-31 19:19:58 -0700882 final boolean hasRingingCall = !getPhone(subId).getRingingCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700883 if (hasRingingCall) {
Wink Saville08874612014-08-31 19:19:58 -0700884 final boolean hasActiveCall = !getPhone(subId).getForegroundCall().isIdle();
885 final boolean hasHoldingCall = !getPhone(subId).getBackgroundCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700886 if (hasActiveCall && hasHoldingCall) {
887 // Both lines are in use!
888 // TODO: provide a flag to let the caller specify what
889 // policy to use if both lines are in use. (The current
890 // behavior is hardwired to "answer incoming, end ongoing",
891 // which is how the CALL button is specced to behave.)
892 PhoneUtils.answerAndEndActive(mCM, mCM.getFirstActiveRingingCall());
893 return;
894 } else {
895 // answerCall() will automatically hold the current active
896 // call, if there is one.
897 PhoneUtils.answerCall(mCM.getFirstActiveRingingCall());
898 return;
899 }
900 } else {
901 // No call was ringing.
902 return;
903 }
904 }
905
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700906 /**
Santos Cordon5422a8d2014-09-12 04:20:56 -0700907 * This method is no longer used and can be removed once TelephonyManager stops referring to it.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700908 */
Santos Cordon5422a8d2014-09-12 04:20:56 -0700909 public void silenceRinger() {
910 Log.e(LOG_TAG, "silenseRinger not supported");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700911 }
912
913 public boolean isOffhook() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700914 return isOffhookForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700915 }
916
Wink Savilleb564aae2014-10-23 10:18:09 -0700917 public boolean isOffhookForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -0700918 return (getPhone(subId).getState() == PhoneConstants.State.OFFHOOK);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700919 }
920
921 public boolean isRinging() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700922 return (isRingingForSubscriber(getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -0700923 }
924
Wink Savilleb564aae2014-10-23 10:18:09 -0700925 public boolean isRingingForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -0700926 return (getPhone(subId).getState() == PhoneConstants.State.RINGING);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700927 }
928
929 public boolean isIdle() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700930 return isIdleForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700931 }
932
Wink Savilleb564aae2014-10-23 10:18:09 -0700933 public boolean isIdleForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -0700934 return (getPhone(subId).getState() == PhoneConstants.State.IDLE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700935 }
936
Svet Ganovb320e182015-04-16 12:30:10 -0700937 public boolean isSimPinEnabled(String callingPackage) {
938 if (!canReadPhoneState(callingPackage, "isSimPinEnabled")) {
939 return false;
940 }
941
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700942 return (PhoneGlobals.getInstance().isSimPinEnabled());
943 }
944
945 public boolean supplyPin(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700946 return supplyPinForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -0700947 }
948
Wink Savilleb564aae2014-10-23 10:18:09 -0700949 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700950 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -0700951 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
952 }
953
954 public boolean supplyPuk(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700955 return supplyPukForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -0700956 }
957
Wink Savilleb564aae2014-10-23 10:18:09 -0700958 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700959 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -0700960 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
961 }
962
963 /** {@hide} */
964 public int[] supplyPinReportResult(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700965 return supplyPinReportResultForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -0700966 }
967
Wink Savilleb564aae2014-10-23 10:18:09 -0700968 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700969 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -0700970 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700971 checkSimPin.start();
972 return checkSimPin.unlockSim(null, pin);
973 }
974
Wink Saville9de0f752013-10-22 19:04:03 -0700975 /** {@hide} */
976 public int[] supplyPukReportResult(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700977 return supplyPukReportResultForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -0700978 }
979
Wink Savilleb564aae2014-10-23 10:18:09 -0700980 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700981 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -0700982 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700983 checkSimPuk.start();
984 return checkSimPuk.unlockSim(puk, pin);
985 }
986
987 /**
Wink Saville9de0f752013-10-22 19:04:03 -0700988 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700989 * a synchronous one.
990 */
991 private static class UnlockSim extends Thread {
992
993 private final IccCard mSimCard;
994
995 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -0700996 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
997 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700998
999 // For replies from SimCard interface
1000 private Handler mHandler;
1001
1002 // For async handler to identify request type
1003 private static final int SUPPLY_PIN_COMPLETE = 100;
1004
1005 public UnlockSim(IccCard simCard) {
1006 mSimCard = simCard;
1007 }
1008
1009 @Override
1010 public void run() {
1011 Looper.prepare();
1012 synchronized (UnlockSim.this) {
1013 mHandler = new Handler() {
1014 @Override
1015 public void handleMessage(Message msg) {
1016 AsyncResult ar = (AsyncResult) msg.obj;
1017 switch (msg.what) {
1018 case SUPPLY_PIN_COMPLETE:
1019 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
1020 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07001021 mRetryCount = msg.arg1;
1022 if (ar.exception != null) {
1023 if (ar.exception instanceof CommandException &&
1024 ((CommandException)(ar.exception)).getCommandError()
1025 == CommandException.Error.PASSWORD_INCORRECT) {
1026 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
1027 } else {
1028 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1029 }
1030 } else {
1031 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
1032 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001033 mDone = true;
1034 UnlockSim.this.notifyAll();
1035 }
1036 break;
1037 }
1038 }
1039 };
1040 UnlockSim.this.notifyAll();
1041 }
1042 Looper.loop();
1043 }
1044
1045 /*
1046 * Use PIN or PUK to unlock SIM card
1047 *
1048 * If PUK is null, unlock SIM card with PIN
1049 *
1050 * If PUK is not null, unlock SIM card with PUK and set PIN code
1051 */
Wink Saville9de0f752013-10-22 19:04:03 -07001052 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001053
1054 while (mHandler == null) {
1055 try {
1056 wait();
1057 } catch (InterruptedException e) {
1058 Thread.currentThread().interrupt();
1059 }
1060 }
1061 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
1062
1063 if (puk == null) {
1064 mSimCard.supplyPin(pin, callback);
1065 } else {
1066 mSimCard.supplyPuk(puk, pin, callback);
1067 }
1068
1069 while (!mDone) {
1070 try {
1071 Log.d(LOG_TAG, "wait for done");
1072 wait();
1073 } catch (InterruptedException e) {
1074 // Restore the interrupted status
1075 Thread.currentThread().interrupt();
1076 }
1077 }
1078 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07001079 int[] resultArray = new int[2];
1080 resultArray[0] = mResult;
1081 resultArray[1] = mRetryCount;
1082 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001083 }
1084 }
1085
1086 public void updateServiceLocation() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001087 updateServiceLocationForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001088
1089 }
1090
Wink Savilleb564aae2014-10-23 10:18:09 -07001091 public void updateServiceLocationForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001092 // No permission check needed here: this call is harmless, and it's
1093 // needed for the ServiceState.requestStateUpdate() call (which is
1094 // already intentionally exposed to 3rd parties.)
Wink Saville36469e72014-06-11 15:17:00 -07001095 getPhone(subId).updateServiceLocation();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001096 }
1097
1098 public boolean isRadioOn() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001099 return isRadioOnForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001100 }
1101
Wink Savilleb564aae2014-10-23 10:18:09 -07001102 public boolean isRadioOnForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001103 return getPhone(subId).getServiceState().getState() != ServiceState.STATE_POWER_OFF;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001104 }
1105
1106 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001107 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001108
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001109 }
Wink Saville36469e72014-06-11 15:17:00 -07001110
Wink Savilleb564aae2014-10-23 10:18:09 -07001111 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001112 enforceModifyPermission();
Wink Savilleadd7cc52014-09-08 14:23:09 -07001113 getPhone(subId).setRadioPower(!isRadioOnForSubscriber(subId));
Wink Saville36469e72014-06-11 15:17:00 -07001114 }
1115
1116 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001117 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001118 }
1119
Wink Savilleb564aae2014-10-23 10:18:09 -07001120 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07001121 enforceModifyPermission();
1122 if ((getPhone(subId).getServiceState().getState() !=
1123 ServiceState.STATE_POWER_OFF) != turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001124 toggleRadioOnOffForSubscriber(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001125 }
1126 return true;
1127 }
Wink Saville36469e72014-06-11 15:17:00 -07001128
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001129 public boolean needMobileRadioShutdown() {
1130 /*
1131 * If any of the Radios are available, it will need to be
1132 * shutdown. So return true if any Radio is available.
1133 */
1134 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1135 Phone phone = PhoneFactory.getPhone(i);
1136 if (phone != null && phone.isRadioAvailable()) return true;
1137 }
1138 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
1139 return false;
1140 }
1141
1142 public void shutdownMobileRadios() {
1143 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1144 logv("Shutting down Phone " + i);
1145 shutdownRadioUsingPhoneId(i);
1146 }
1147 }
1148
1149 private void shutdownRadioUsingPhoneId(int phoneId) {
1150 enforceModifyPermission();
1151 Phone phone = PhoneFactory.getPhone(phoneId);
1152 if (phone != null && phone.isRadioAvailable()) {
1153 phone.shutdownRadio();
1154 }
1155 }
1156
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001157 public boolean setRadioPower(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001158 return setRadioPowerForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001159 }
1160
Wink Savilleb564aae2014-10-23 10:18:09 -07001161 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001162 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001163 getPhone(subId).setRadioPower(turnOn);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001164 return true;
1165 }
1166
Wink Saville36469e72014-06-11 15:17:00 -07001167 // FIXME: subId version needed
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001168 public boolean enableDataConnectivity() {
1169 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001170 int subId = mSubscriptionController.getDefaultDataSubId();
Wink Saville36469e72014-06-11 15:17:00 -07001171 getPhone(subId).setDataEnabled(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001172 return true;
1173 }
1174
Wink Saville36469e72014-06-11 15:17:00 -07001175 // FIXME: subId version needed
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001176 public boolean disableDataConnectivity() {
1177 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001178 int subId = mSubscriptionController.getDefaultDataSubId();
Wink Saville36469e72014-06-11 15:17:00 -07001179 getPhone(subId).setDataEnabled(false);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001180 return true;
1181 }
1182
Wink Saville36469e72014-06-11 15:17:00 -07001183 // FIXME: subId version needed
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001184 public boolean isDataConnectivityPossible() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001185 int subId = mSubscriptionController.getDefaultDataSubId();
Wink Saville36469e72014-06-11 15:17:00 -07001186 return getPhone(subId).isDataConnectivityPossible();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001187 }
1188
1189 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001190 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07001191 }
1192
Wink Savilleb564aae2014-10-23 10:18:09 -07001193 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001194 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001195 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001196 }
1197
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001198 public int getCallState() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001199 return getCallStateForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001200 }
1201
Wink Savilleb564aae2014-10-23 10:18:09 -07001202 public int getCallStateForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001203 return DefaultPhoneNotifier.convertCallState(getPhone(subId).getState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001204 }
1205
1206 public int getDataState() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001207 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Wink Saville36469e72014-06-11 15:17:00 -07001208 return DefaultPhoneNotifier.convertDataState(phone.getDataConnectionState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001209 }
1210
1211 public int getDataActivity() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001212 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Wink Saville36469e72014-06-11 15:17:00 -07001213 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001214 }
1215
1216 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001217 public Bundle getCellLocation(String callingPackage) {
1218 enforceFineOrCoarseLocationPermission("getCellLocation");
1219
1220 // OP_COARSE_LOCATION controls both fine and coarse location.
1221 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1222 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1223 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001224 }
1225
Jake Hambye994d462014-02-03 13:10:13 -08001226 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001227 if (DBG_LOC) log("getCellLocation: is active user");
1228 Bundle data = new Bundle();
Legler Wu2c01cdf2014-12-08 19:00:59 +08001229 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
1230 phone.getCellLocation().fillInNotifierBundle(data);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001231 return data;
1232 } else {
1233 if (DBG_LOC) log("getCellLocation: suppress non-active user");
1234 return null;
1235 }
1236 }
1237
Svetoslav64fad262015-04-14 14:35:21 -07001238 private void enforceFineOrCoarseLocationPermission(String message) {
1239 try {
1240 mApp.enforceCallingOrSelfPermission(
1241 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1242 } catch (SecurityException e) {
1243 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1244 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1245 // is the weaker precondition
1246 mApp.enforceCallingOrSelfPermission(
1247 android.Manifest.permission.ACCESS_COARSE_LOCATION, message);
1248 }
1249 }
1250
1251
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001252 @Override
1253 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001254 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001255 }
1256
Wink Savilleb564aae2014-10-23 10:18:09 -07001257 public void enableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001258 mApp.enforceCallingOrSelfPermission(
1259 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Wink Saville36469e72014-06-11 15:17:00 -07001260 getPhone(subId).enableLocationUpdates();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001261 }
1262
1263 @Override
1264 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001265 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001266 }
1267
Wink Savilleb564aae2014-10-23 10:18:09 -07001268 public void disableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001269 mApp.enforceCallingOrSelfPermission(
1270 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Wink Saville36469e72014-06-11 15:17:00 -07001271 getPhone(subId).disableLocationUpdates();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001272 }
1273
1274 @Override
1275 @SuppressWarnings("unchecked")
1276 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
Svetoslav64fad262015-04-14 14:35:21 -07001277 enforceFineOrCoarseLocationPermission("getNeighboringCellInfo");
1278
1279 // OP_COARSE_LOCATION controls both fine and coarse location.
1280 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1281 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1282 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001283 }
1284
1285 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
1286 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1287 return null;
1288 }
Svetoslav64fad262015-04-14 14:35:21 -07001289
Jake Hambye994d462014-02-03 13:10:13 -08001290 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001291 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
1292
1293 ArrayList<NeighboringCellInfo> cells = null;
1294
1295 try {
1296 cells = (ArrayList<NeighboringCellInfo>) sendRequest(
Wink Saville36469e72014-06-11 15:17:00 -07001297 CMD_HANDLE_NEIGHBORING_CELL, null, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001298 } catch (RuntimeException e) {
Wink Saville36469e72014-06-11 15:17:00 -07001299 Log.e(LOG_TAG, "getNeighboringCellInfo " + e);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001300 }
1301 return cells;
1302 } else {
1303 if (DBG_LOC) log("getNeighboringCellInfo: suppress non-active user");
1304 return null;
1305 }
1306 }
1307
1308
1309 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001310 public List<CellInfo> getAllCellInfo(String callingPackage) {
1311 enforceFineOrCoarseLocationPermission("getAllCellInfo");
1312
1313 // OP_COARSE_LOCATION controls both fine and coarse location.
1314 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1315 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1316 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001317 }
1318
Jake Hambye994d462014-02-03 13:10:13 -08001319 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001320 if (DBG_LOC) log("getAllCellInfo: is active user");
Legler Wu2c01cdf2014-12-08 19:00:59 +08001321 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
1322 for (Phone phone : PhoneFactory.getPhones()) {
1323 cellInfos.addAll(phone.getAllCellInfo());
1324 }
1325 return cellInfos;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001326 } else {
1327 if (DBG_LOC) log("getAllCellInfo: suppress non-active user");
1328 return null;
1329 }
1330 }
1331
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001332 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001333 public void setCellInfoListRate(int rateInMillis) {
1334 mPhone.setCellInfoListRate(rateInMillis);
1335 }
1336
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001337 //
1338 // Internal helper methods.
1339 //
1340
Jake Hambye994d462014-02-03 13:10:13 -08001341 private static boolean checkIfCallerIsSelfOrForegroundUser() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001342 boolean ok;
1343
1344 boolean self = Binder.getCallingUid() == Process.myUid();
1345 if (!self) {
1346 // Get the caller's user id then clear the calling identity
1347 // which will be restored in the finally clause.
1348 int callingUser = UserHandle.getCallingUserId();
1349 long ident = Binder.clearCallingIdentity();
1350
1351 try {
1352 // With calling identity cleared the current user is the foreground user.
1353 int foregroundUser = ActivityManager.getCurrentUser();
1354 ok = (foregroundUser == callingUser);
1355 if (DBG_LOC) {
1356 log("checkIfCallerIsSelfOrForegoundUser: foregroundUser=" + foregroundUser
1357 + " callingUser=" + callingUser + " ok=" + ok);
1358 }
1359 } catch (Exception ex) {
1360 if (DBG_LOC) loge("checkIfCallerIsSelfOrForegoundUser: Exception ex=" + ex);
1361 ok = false;
1362 } finally {
1363 Binder.restoreCallingIdentity(ident);
1364 }
1365 } else {
1366 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: is self");
1367 ok = true;
1368 }
1369 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: ret=" + ok);
1370 return ok;
1371 }
1372
1373 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001374 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1375 *
1376 * @throws SecurityException if the caller does not have the required permission
1377 */
1378 private void enforceModifyPermission() {
1379 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1380 }
1381
1382 /**
Junda Liua2e36012014-07-09 18:30:01 -07001383 * Make sure either system app or the caller has carrier privilege.
1384 *
1385 * @throws SecurityException if the caller does not have the required permission/privilege
1386 */
1387 private void enforceModifyPermissionOrCarrierPrivilege() {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001388 int permission = mApp.checkCallingOrSelfPermission(
1389 android.Manifest.permission.MODIFY_PHONE_STATE);
1390 if (permission == PackageManager.PERMISSION_GRANTED) {
1391 return;
1392 }
1393
1394 log("No modify permission, check carrier privilege next.");
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08001395 if (getCarrierPrivilegeStatus() != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001396 loge("No Carrier Privilege.");
1397 throw new SecurityException("No modify permission or carrier privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001398 }
1399 }
1400
1401 /**
1402 * Make sure the caller has carrier privilege.
1403 *
1404 * @throws SecurityException if the caller does not have the required permission
1405 */
1406 private void enforceCarrierPrivilege() {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08001407 if (getCarrierPrivilegeStatus() != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001408 loge("No Carrier Privilege.");
1409 throw new SecurityException("No Carrier Privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001410 }
1411 }
1412
1413 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001414 * Make sure the caller has the CALL_PHONE permission.
1415 *
1416 * @throws SecurityException if the caller does not have the required permission
1417 */
1418 private void enforceCallPermission() {
1419 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1420 }
1421
Shishir Agrawal566b7612013-10-28 14:41:00 -07001422 /**
Gabriel Peal36ebb0d2014-03-20 09:20:43 -07001423 * Make sure the caller has the READ_PRIVILEGED_PHONE_STATE permission.
1424 *
1425 * @throws SecurityException if the caller does not have the required permission
1426 */
1427 private void enforcePrivilegedPhoneStatePermission() {
1428 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
1429 null);
1430 }
1431
Stuart Scott8eef64f2015-04-08 15:13:54 -07001432 private void enforceConnectivityInternalPermission() {
1433 mApp.enforceCallingOrSelfPermission(
1434 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1435 "ConnectivityService");
1436 }
1437
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001438 private String createTelUrl(String number) {
1439 if (TextUtils.isEmpty(number)) {
1440 return null;
1441 }
1442
Jake Hambye994d462014-02-03 13:10:13 -08001443 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001444 }
1445
Ihab Awadf9e92732013-12-05 18:02:52 -08001446 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001447 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1448 }
1449
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001450 private static void logv(String msg) {
1451 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
1452 }
1453
Ihab Awadf9e92732013-12-05 18:02:52 -08001454 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001455 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1456 }
1457
1458 public int getActivePhoneType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001459 return getActivePhoneTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001460 }
1461
Wink Savilleb564aae2014-10-23 10:18:09 -07001462 public int getActivePhoneTypeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001463 return getPhone(subId).getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001464 }
1465
1466 /**
1467 * Returns the CDMA ERI icon index to display
1468 */
1469 public int getCdmaEriIconIndex() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001470 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001471
1472 }
1473
Wink Savilleb564aae2014-10-23 10:18:09 -07001474 public int getCdmaEriIconIndexForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001475 return getPhone(subId).getCdmaEriIconIndex();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001476 }
1477
1478 /**
1479 * Returns the CDMA ERI icon mode,
1480 * 0 - ON
1481 * 1 - FLASHING
1482 */
1483 public int getCdmaEriIconMode() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001484 return getCdmaEriIconModeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001485 }
1486
Wink Savilleb564aae2014-10-23 10:18:09 -07001487 public int getCdmaEriIconModeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001488 return getPhone(subId).getCdmaEriIconMode();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001489 }
1490
1491 /**
1492 * Returns the CDMA ERI text,
1493 */
1494 public String getCdmaEriText() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001495 return getCdmaEriTextForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001496 }
1497
Wink Savilleb564aae2014-10-23 10:18:09 -07001498 public String getCdmaEriTextForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001499 return getPhone(subId).getCdmaEriText();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001500 }
1501
1502 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07001503 * Returns the CDMA MDN.
1504 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001505 public String getCdmaMdn(int subId) {
Junda Liuca05d5d2014-08-14 22:36:34 -07001506 enforceModifyPermissionOrCarrierPrivilege();
1507 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1508 return getPhone(subId).getLine1Number();
1509 } else {
1510 return null;
1511 }
1512 }
1513
1514 /**
1515 * Returns the CDMA MIN.
1516 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001517 public String getCdmaMin(int subId) {
Junda Liuca05d5d2014-08-14 22:36:34 -07001518 enforceModifyPermissionOrCarrierPrivilege();
1519 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1520 return getPhone(subId).getCdmaMin();
1521 } else {
1522 return null;
1523 }
1524 }
1525
1526 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001527 * Returns true if CDMA provisioning needs to run.
1528 */
1529 public boolean needsOtaServiceProvisioning() {
1530 return mPhone.needsOtaServiceProvisioning();
1531 }
1532
1533 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001534 * Sets the voice mail number of a given subId.
1535 */
1536 @Override
1537 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08001538 enforceCarrierPrivilege();
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001539 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
1540 new Pair<String, String>(alphaTag, number), new Integer(subId));
1541 return success;
1542 }
1543
1544 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001545 * Returns the unread count of voicemails
1546 */
1547 public int getVoiceMessageCount() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001548 return getVoiceMessageCountForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001549 }
1550
1551 /**
1552 * Returns the unread count of voicemails for a subId
1553 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001554 public int getVoiceMessageCountForSubscriber( int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001555 return getPhone(subId).getVoiceMessageCount();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001556 }
1557
1558 /**
1559 * Returns the data network type
1560 *
1561 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
1562 */
1563 @Override
1564 public int getNetworkType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001565 return getNetworkTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001566 }
1567
1568 /**
1569 * Returns the network type for a subId
1570 */
1571 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001572 public int getNetworkTypeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001573 return getPhone(subId).getServiceState().getDataNetworkType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001574 }
1575
1576 /**
1577 * Returns the data network type
1578 */
1579 @Override
1580 public int getDataNetworkType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001581 return getDataNetworkTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001582 }
1583
1584 /**
1585 * Returns the data network type for a subId
1586 */
1587 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001588 public int getDataNetworkTypeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001589 return getPhone(subId).getServiceState().getDataNetworkType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001590 }
1591
1592 /**
1593 * Returns the data network type
1594 */
1595 @Override
1596 public int getVoiceNetworkType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001597 return getVoiceNetworkTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001598 }
1599
1600 /**
1601 * Returns the Voice network type for a subId
1602 */
1603 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001604 public int getVoiceNetworkTypeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001605 return getPhone(subId).getServiceState().getVoiceNetworkType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001606 }
1607
1608 /**
1609 * @return true if a ICC card is present
1610 */
1611 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07001612 // FIXME Make changes to pass defaultSimId of type int
Amit Mahajana6fc2a82015-01-06 11:53:51 -08001613 return hasIccCardUsingSlotId(mSubscriptionController.getSlotId(getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07001614 }
1615
1616 /**
1617 * @return true if a ICC card is present for a slotId
1618 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001619 public boolean hasIccCardUsingSlotId(int slotId) {
Amit Mahajana6fc2a82015-01-06 11:53:51 -08001620 int subId[] = mSubscriptionController.getSubIdUsingSlotId(slotId);
1621 if (subId != null) {
1622 return getPhone(subId[0]).getIccCard().hasIccCard();
1623 } else {
1624 return false;
1625 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001626 }
1627
1628 /**
1629 * Return if the current radio is LTE on CDMA. This
1630 * is a tri-state return value as for a period of time
1631 * the mode may be unknown.
1632 *
1633 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08001634 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001635 */
1636 public int getLteOnCdmaMode() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001637 return getLteOnCdmaModeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001638 }
1639
Wink Savilleb564aae2014-10-23 10:18:09 -07001640 public int getLteOnCdmaModeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001641 return getPhone(subId).getLteOnCdmaMode();
1642 }
1643
1644 public void setPhone(Phone phone) {
1645 mPhone = phone;
1646 }
1647
1648 /**
1649 * {@hide}
1650 * Returns Default subId, 0 in the case of single standby.
1651 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001652 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001653 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07001654 }
1655
Wink Savilleb564aae2014-10-23 10:18:09 -07001656 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001657 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001658 }
Ihab Awadf2177b72013-11-25 13:33:23 -08001659
1660 /**
1661 * @see android.telephony.TelephonyManager.WifiCallingChoices
1662 */
1663 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08001664 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
1665 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08001666 }
1667
1668 /**
1669 * @see android.telephony.TelephonyManager.WifiCallingChoices
1670 */
1671 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08001672 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
1673 Settings.System.putInt(mPhone.getContext().getContentResolver(),
1674 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08001675 }
1676
Sailesh Nepald1e68152013-12-12 19:08:02 -08001677 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07001678 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08001679 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08001680 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08001681
Shishir Agrawal566b7612013-10-28 14:41:00 -07001682 @Override
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001683 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(String AID) {
Junda Liua2e36012014-07-09 18:30:01 -07001684 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001685
1686 if (DBG) log("iccOpenLogicalChannel: " + AID);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001687 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
1688 CMD_OPEN_CHANNEL, AID);
1689 if (DBG) log("iccOpenLogicalChannel: " + response);
1690 return response;
Shishir Agrawal566b7612013-10-28 14:41:00 -07001691 }
1692
1693 @Override
1694 public boolean iccCloseLogicalChannel(int channel) {
Junda Liua2e36012014-07-09 18:30:01 -07001695 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001696
1697 if (DBG) log("iccCloseLogicalChannel: " + channel);
1698 if (channel < 0) {
1699 return false;
1700 }
Jake Hambye994d462014-02-03 13:10:13 -08001701 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07001702 if (DBG) log("iccCloseLogicalChannel: " + success);
1703 return success;
1704 }
1705
1706 @Override
1707 public String iccTransmitApduLogicalChannel(int channel, int cla,
1708 int command, int p1, int p2, int p3, String data) {
Junda Liua2e36012014-07-09 18:30:01 -07001709 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001710
1711 if (DBG) {
1712 log("iccTransmitApduLogicalChannel: chnl=" + channel + " cla=" + cla +
1713 " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
1714 " data=" + data);
1715 }
1716
1717 if (channel < 0) {
1718 return "";
1719 }
1720
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001721 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Shishir Agrawal566b7612013-10-28 14:41:00 -07001722 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data));
1723 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
1724
Shishir Agrawal566b7612013-10-28 14:41:00 -07001725 // Append the returned status code to the end of the response payload.
1726 String s = Integer.toHexString(
1727 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07001728 if (response.payload != null) {
1729 s = IccUtils.bytesToHexString(response.payload) + s;
1730 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07001731 return s;
1732 }
Jake Hambye994d462014-02-03 13:10:13 -08001733
Evan Charltonc66da362014-05-16 14:06:40 -07001734 @Override
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001735 public String iccTransmitApduBasicChannel(int cla, int command, int p1, int p2,
1736 int p3, String data) {
1737 enforceModifyPermissionOrCarrierPrivilege();
1738
1739 if (DBG) {
1740 log("iccTransmitApduBasicChannel: cla=" + cla + " cmd=" + command + " p1="
1741 + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
1742 }
1743
1744 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
1745 new IccAPDUArgument(0, cla, command, p1, p2, p3, data));
1746 if (DBG) log("iccTransmitApduBasicChannel: " + response);
1747
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001748 // Append the returned status code to the end of the response payload.
1749 String s = Integer.toHexString(
1750 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07001751 if (response.payload != null) {
1752 s = IccUtils.bytesToHexString(response.payload) + s;
1753 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001754 return s;
1755 }
1756
1757 @Override
1758 public byte[] iccExchangeSimIO(int fileID, int command, int p1, int p2, int p3,
1759 String filePath) {
1760 enforceModifyPermissionOrCarrierPrivilege();
1761
1762 if (DBG) {
1763 log("Exchange SIM_IO " + fileID + ":" + command + " " +
1764 p1 + " " + p2 + " " + p3 + ":" + filePath);
1765 }
1766
1767 IccIoResult response =
1768 (IccIoResult)sendRequest(CMD_EXCHANGE_SIM_IO,
Yong Jiang3edf3782014-10-03 13:23:28 -05001769 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath));
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001770
1771 if (DBG) {
1772 log("Exchange SIM_IO [R]" + response);
1773 }
1774
1775 byte[] result = null;
1776 int length = 2;
1777 if (response.payload != null) {
1778 length = 2 + response.payload.length;
1779 result = new byte[length];
1780 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
1781 } else {
1782 result = new byte[length];
1783 }
1784
1785 result[length - 1] = (byte) response.sw2;
1786 result[length - 2] = (byte) response.sw1;
1787 return result;
1788 }
1789
1790 @Override
Evan Charltonc66da362014-05-16 14:06:40 -07001791 public String sendEnvelopeWithStatus(String content) {
Junda Liua2e36012014-07-09 18:30:01 -07001792 enforceModifyPermissionOrCarrierPrivilege();
Evan Charltonc66da362014-05-16 14:06:40 -07001793
1794 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content);
1795 if (response.payload == null) {
1796 return "";
1797 }
1798
1799 // Append the returned status code to the end of the response payload.
1800 String s = Integer.toHexString(
1801 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
1802 s = IccUtils.bytesToHexString(response.payload) + s;
1803 return s;
1804 }
1805
Jake Hambye994d462014-02-03 13:10:13 -08001806 /**
1807 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
1808 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
1809 *
1810 * @param itemID the ID of the item to read
1811 * @return the NV item as a String, or null on error.
1812 */
1813 @Override
1814 public String nvReadItem(int itemID) {
Junda Liua2e36012014-07-09 18:30:01 -07001815 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001816 if (DBG) log("nvReadItem: item " + itemID);
1817 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
1818 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
1819 return value;
1820 }
1821
1822 /**
1823 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
1824 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
1825 *
1826 * @param itemID the ID of the item to read
1827 * @param itemValue the value to write, as a String
1828 * @return true on success; false on any failure
1829 */
1830 @Override
1831 public boolean nvWriteItem(int itemID, String itemValue) {
Junda Liua2e36012014-07-09 18:30:01 -07001832 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001833 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
1834 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
1835 new Pair<Integer, String>(itemID, itemValue));
1836 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
1837 return success;
1838 }
1839
1840 /**
1841 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
1842 * Used for device configuration by some CDMA operators.
1843 *
1844 * @param preferredRoamingList byte array containing the new PRL
1845 * @return true on success; false on any failure
1846 */
1847 @Override
1848 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Junda Liua2e36012014-07-09 18:30:01 -07001849 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001850 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
1851 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
1852 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
1853 return success;
1854 }
1855
1856 /**
1857 * Perform the specified type of NV config reset.
1858 * Used for device configuration by some CDMA operators.
1859 *
1860 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
1861 * @return true on success; false on any failure
1862 */
1863 @Override
1864 public boolean nvResetConfig(int resetType) {
Junda Liua2e36012014-07-09 18:30:01 -07001865 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001866 if (DBG) log("nvResetConfig: type " + resetType);
1867 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
1868 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
1869 return success;
1870 }
Jake Hamby7c27be32014-03-03 13:25:59 -08001871
1872 /**
Wink Saville36469e72014-06-11 15:17:00 -07001873 * {@hide}
1874 * Returns Default sim, 0 in the case of single standby.
1875 */
1876 public int getDefaultSim() {
1877 //TODO Need to get it from Telephony Devcontroller
1878 return 0;
1879 }
1880
Svet Ganovb320e182015-04-16 12:30:10 -07001881 public String[] getPcscfAddress(String apnType, String callingPackage) {
1882 if (!canReadPhoneState(callingPackage, "getPcscfAddress")) {
1883 return new String[0];
1884 }
1885
1886
ram87fca6f2014-07-18 18:58:44 +05301887 return mPhone.getPcscfAddress(apnType);
Wink Saville36469e72014-06-11 15:17:00 -07001888 }
1889
1890 public void setImsRegistrationState(boolean registered) {
1891 enforceModifyPermission();
1892 mPhone.setImsRegistrationState(registered);
1893 }
1894
1895 /**
Stuart Scott54788802015-03-30 13:18:01 -07001896 * Set the network selection mode to automatic.
1897 *
1898 */
1899 @Override
1900 public void setNetworkSelectionModeAutomatic(int subId) {
1901 enforceModifyPermissionOrCarrierPrivilege();
1902 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
1903 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
1904 }
1905
1906 /**
Junda Liu84d15a22014-07-02 11:21:04 -07001907 * Get the calculated preferred network type.
1908 * Used for debugging incorrect network type.
1909 *
1910 * @return the preferred network type, defined in RILConstants.java.
1911 */
1912 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07001913 public int getCalculatedPreferredNetworkType(String callingPackage) {
1914 if (!canReadPhoneState(callingPackage, "getCalculatedPreferredNetworkType")) {
1915 return RILConstants.PREFERRED_NETWORK_MODE;
1916 }
1917
Amit Mahajan43330e02014-11-18 11:54:45 -08001918 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext(), 0); // wink FIXME: need to get SubId from somewhere.
Junda Liu84d15a22014-07-02 11:21:04 -07001919 }
1920
1921 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08001922 * Get the preferred network type.
1923 * Used for device configuration by some CDMA operators.
1924 *
1925 * @return the preferred network type, defined in RILConstants.java.
1926 */
1927 @Override
Stuart Scott54788802015-03-30 13:18:01 -07001928 public int getPreferredNetworkType(int subId) {
Junda Liua2e36012014-07-09 18:30:01 -07001929 enforceModifyPermissionOrCarrierPrivilege();
Jake Hamby7c27be32014-03-03 13:25:59 -08001930 if (DBG) log("getPreferredNetworkType");
Stuart Scott54788802015-03-30 13:18:01 -07001931 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08001932 int networkType = (result != null ? result[0] : -1);
1933 if (DBG) log("getPreferredNetworkType: " + networkType);
1934 return networkType;
1935 }
1936
1937 /**
1938 * Set the preferred network type.
1939 * Used for device configuration by some CDMA operators.
1940 *
1941 * @param networkType the preferred network type, defined in RILConstants.java.
1942 * @return true on success; false on any failure.
1943 */
1944 @Override
Stuart Scott54788802015-03-30 13:18:01 -07001945 public boolean setPreferredNetworkType(int subId, int networkType) {
Junda Liua2e36012014-07-09 18:30:01 -07001946 enforceModifyPermissionOrCarrierPrivilege();
Stuart Scott54788802015-03-30 13:18:01 -07001947 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
1948 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08001949 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
Junda Liu80bc0d12014-07-14 16:36:44 -07001950 if (success) {
1951 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
Stuart Scott54788802015-03-30 13:18:01 -07001952 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
Junda Liu80bc0d12014-07-14 16:36:44 -07001953 }
Jake Hamby7c27be32014-03-03 13:25:59 -08001954 return success;
1955 }
Robert Greenwalted86e582014-05-21 20:03:20 -07001956
1957 /**
Junda Liu475951f2014-11-07 16:45:03 -08001958 * Check TETHER_DUN_REQUIRED and TETHER_DUN_APN settings, net.tethering.noprovisioning
1959 * SystemProperty, and config_tether_apndata to decide whether DUN APN is required for
1960 * tethering.
1961 *
1962 * @return 0: Not required. 1: required. 2: Not set.
1963 * @hide
1964 */
1965 @Override
1966 public int getTetherApnRequired() {
1967 enforceModifyPermissionOrCarrierPrivilege();
1968 int dunRequired = Settings.Global.getInt(mPhone.getContext().getContentResolver(),
1969 Settings.Global.TETHER_DUN_REQUIRED, 2);
1970 // If not set, check net.tethering.noprovisioning, TETHER_DUN_APN setting and
1971 // config_tether_apndata.
1972 if (dunRequired == 2 && mPhone.hasMatchedTetherApnSetting()) {
1973 dunRequired = 1;
1974 }
1975 return dunRequired;
1976 }
1977
1978 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07001979 * Set mobile data enabled
1980 * Used by the user through settings etc to turn on/off mobile data
1981 *
1982 * @param enable {@code true} turn turn data on, else {@code false}
1983 */
1984 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08001985 public void setDataEnabled(int subId, boolean enable) {
Robert Greenwalted86e582014-05-21 20:03:20 -07001986 enforceModifyPermission();
Wink Savillee7353bb2014-12-05 14:21:41 -08001987 int phoneId = mSubscriptionController.getPhoneId(subId);
1988 log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
1989 Phone phone = PhoneFactory.getPhone(phoneId);
1990 if (phone != null) {
1991 log("setDataEnabled: subId=" + subId + " enable=" + enable);
1992 phone.setDataEnabled(enable);
1993 } else {
1994 loge("setDataEnabled: no phone for subId=" + subId);
1995 }
Robert Greenwalted86e582014-05-21 20:03:20 -07001996 }
1997
1998 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07001999 * Get whether mobile data is enabled.
2000 *
2001 * Note that this used to be available from ConnectivityService, gated by
2002 * ACCESS_NETWORK_STATE permission, so this will accept either that or
2003 * our MODIFY_PHONE_STATE.
Robert Greenwalted86e582014-05-21 20:03:20 -07002004 *
2005 * @return {@code true} if data is enabled else {@code false}
2006 */
2007 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002008 public boolean getDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07002009 try {
2010 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
2011 null);
2012 } catch (Exception e) {
2013 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE,
2014 null);
2015 }
Wink Savillee7353bb2014-12-05 14:21:41 -08002016 int phoneId = mSubscriptionController.getPhoneId(subId);
2017 log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
2018 Phone phone = PhoneFactory.getPhone(phoneId);
2019 if (phone != null) {
2020 boolean retVal = phone.getDataEnabled();
2021 log("getDataEnabled: subId=" + subId + " retVal=" + retVal);
2022 return retVal;
2023 } else {
2024 loge("getDataEnabled: no phone subId=" + subId + " retVal=false");
2025 return false;
2026 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002027 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002028
2029 @Override
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002030 public int getCarrierPrivilegeStatus() {
Shishir Agrawal21409252015-01-15 23:33:50 -08002031 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002032 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002033 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002034 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2035 }
2036 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07002037 mPhone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002038 }
Junda Liu29340342014-07-10 15:23:27 -07002039
2040 @Override
Shishir Agrawal6d5a2852014-07-11 16:32:57 -07002041 public int checkCarrierPrivilegesForPackage(String pkgname) {
Shishir Agrawal21409252015-01-15 23:33:50 -08002042 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002043 if (card == null) {
2044 loge("checkCarrierPrivilegesForPackage: No UICC");
2045 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2046 }
2047 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgname);
Junda Liu29340342014-07-10 15:23:27 -07002048 }
Derek Tan89e89d42014-07-08 17:00:10 -07002049
2050 @Override
Junda Liue64de782015-04-16 17:19:16 -07002051 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
2052 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
2053 loge("phoneId " + phoneId + " is not valid.");
2054 return null;
2055 }
2056 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002057 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002058 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002059 return null ;
2060 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002061 return card.getCarrierPackageNamesForIntent(
Svetoslav483aff72015-04-21 14:16:07 -07002062 mPhone.getContext().getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002063 }
2064
Wink Savilleb564aae2014-10-23 10:18:09 -07002065 private String getIccId(int subId) {
Derek Tan97ebb422014-09-05 16:55:38 -07002066 UiccCard card = getPhone(subId).getUiccCard();
2067 if (card == null) {
2068 loge("getIccId: No UICC");
2069 return null;
2070 }
2071 String iccId = card.getIccId();
2072 if (TextUtils.isEmpty(iccId)) {
2073 loge("getIccId: ICC ID is null or empty.");
2074 return null;
2075 }
2076 return iccId;
2077 }
2078
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002079 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002080 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
2081 String number) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002082 enforceCarrierPrivilege();
Derek Tan97ebb422014-09-05 16:55:38 -07002083
Jeff Sharkey85190e62014-12-05 09:40:12 -08002084 final String iccId = getIccId(subId);
2085 final String subscriberId = getPhone(subId).getSubscriberId();
2086
2087 if (DBG_MERGE) {
2088 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
2089 + subscriberId + " to " + number);
2090 }
2091
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002092 if (TextUtils.isEmpty(iccId)) {
2093 return false;
Derek Tan97ebb422014-09-05 16:55:38 -07002094 }
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002095
Jeff Sharkey85190e62014-12-05 09:40:12 -08002096 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2097
2098 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002099 if (alphaTag == null) {
2100 editor.remove(alphaTagPrefKey);
2101 } else {
2102 editor.putString(alphaTagPrefKey, alphaTag);
2103 }
2104
Jeff Sharkey85190e62014-12-05 09:40:12 -08002105 // Record both the line number and IMSI for this ICCID, since we need to
2106 // track all merged IMSIs based on line number
2107 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2108 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002109 if (number == null) {
2110 editor.remove(numberPrefKey);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002111 editor.remove(subscriberPrefKey);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002112 } else {
2113 editor.putString(numberPrefKey, number);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002114 editor.putString(subscriberPrefKey, subscriberId);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002115 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002116
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002117 editor.commit();
2118 return true;
Derek Tan7226c842014-07-02 17:42:23 -07002119 }
2120
2121 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002122 public String getLine1NumberForDisplay(int subId, String callingPackage) {
2123 if (!canReadPhoneState(callingPackage, "getLine1NumberForDisplay")) {
2124 return null;
2125 }
Derek Tan97ebb422014-09-05 16:55:38 -07002126
2127 String iccId = getIccId(subId);
2128 if (iccId != null) {
2129 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002130 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002131 }
Derek Tan97ebb422014-09-05 16:55:38 -07002132 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002133 }
2134
2135 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002136 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
2137 if (!canReadPhoneState(callingPackage, "getLine1AlphaTagForDisplay")) {
2138 return null;
2139 }
Derek Tan97ebb422014-09-05 16:55:38 -07002140
2141 String iccId = getIccId(subId);
2142 if (iccId != null) {
2143 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002144 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002145 }
Derek Tan97ebb422014-09-05 16:55:38 -07002146 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002147 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002148
2149 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002150 public String[] getMergedSubscriberIds() {
2151 final Context context = mPhone.getContext();
2152 final TelephonyManager tele = TelephonyManager.from(context);
2153 final SubscriptionManager sub = SubscriptionManager.from(context);
2154
2155 // Figure out what subscribers are currently active
2156 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
2157 final int[] subIds = sub.getActiveSubscriptionIdList();
2158 for (int subId : subIds) {
2159 activeSubscriberIds.add(tele.getSubscriberId(subId));
2160 }
2161
2162 // First pass, find a number override for an active subscriber
2163 String mergeNumber = null;
2164 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
2165 for (String key : prefs.keySet()) {
2166 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
2167 final String subscriberId = (String) prefs.get(key);
2168 if (activeSubscriberIds.contains(subscriberId)) {
2169 final String iccId = key.substring(PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
2170 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2171 mergeNumber = (String) prefs.get(numberKey);
2172 if (DBG_MERGE) {
2173 Slog.d(LOG_TAG, "Found line number " + mergeNumber
2174 + " for active subscriber " + subscriberId);
2175 }
2176 if (!TextUtils.isEmpty(mergeNumber)) {
2177 break;
2178 }
2179 }
2180 }
2181 }
2182
2183 // Shortcut when no active merged subscribers
2184 if (TextUtils.isEmpty(mergeNumber)) {
2185 return null;
2186 }
2187
2188 // Second pass, find all subscribers under that line override
2189 final ArraySet<String> result = new ArraySet<>();
2190 for (String key : prefs.keySet()) {
2191 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
2192 final String number = (String) prefs.get(key);
2193 if (mergeNumber.equals(number)) {
2194 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
2195 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
2196 final String subscriberId = (String) prefs.get(subscriberKey);
2197 if (!TextUtils.isEmpty(subscriberId)) {
2198 result.add(subscriberId);
2199 }
2200 }
2201 }
2202 }
2203
2204 final String[] resultArray = result.toArray(new String[result.size()]);
2205 Arrays.sort(resultArray);
2206 if (DBG_MERGE) {
2207 Slog.d(LOG_TAG, "Found subscribers " + Arrays.toString(resultArray) + " after merge");
2208 }
2209 return resultArray;
2210 }
2211
2212 @Override
Shishir Agrawala3dfd752014-09-04 13:25:42 -07002213 public boolean setOperatorBrandOverride(String brand) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002214 enforceCarrierPrivilege();
Shishir Agrawala3dfd752014-09-04 13:25:42 -07002215 return mPhone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002216 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05002217
2218 @Override
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002219 public boolean setRoamingOverride(List<String> gsmRoamingList,
2220 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
2221 List<String> cdmaNonRoamingList) {
2222 enforceCarrierPrivilege();
2223 return mPhone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
2224 cdmaNonRoamingList);
2225 }
2226
2227 @Override
Steven Liu4bf01bc2014-07-17 11:05:29 -05002228 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
2229 enforceModifyPermission();
2230
2231 int returnValue = 0;
2232 try {
2233 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
2234 if(result.exception == null) {
2235 if (result.result != null) {
2236 byte[] responseData = (byte[])(result.result);
2237 if(responseData.length > oemResp.length) {
2238 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
2239 responseData.length + "bytes. Buffer Size is " +
2240 oemResp.length + "bytes.");
2241 }
2242 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
2243 returnValue = responseData.length;
2244 }
2245 } else {
2246 CommandException ex = (CommandException) result.exception;
2247 returnValue = ex.getCommandError().ordinal();
2248 if(returnValue > 0) returnValue *= -1;
2249 }
2250 } catch (RuntimeException e) {
2251 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
2252 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
2253 if(returnValue > 0) returnValue *= -1;
2254 }
2255
2256 return returnValue;
2257 }
Wink Saville5d475dd2014-10-17 15:00:58 -07002258
2259 @Override
2260 public void setRadioCapability(RadioAccessFamily[] rafs) {
2261 try {
2262 ProxyController.getInstance().setRadioCapability(rafs);
2263 } catch (RuntimeException e) {
2264 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
2265 }
2266 }
2267
2268 @Override
2269 public int getRadioAccessFamily(int phoneId) {
2270 return ProxyController.getInstance().getRadioAccessFamily(phoneId);
2271 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002272
2273 @Override
2274 public void enableVideoCalling(boolean enable) {
2275 enforceModifyPermission();
2276 SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2277 editor.putBoolean(PREF_ENABLE_VIDEO_CALLING, enable);
2278 editor.commit();
2279 }
2280
2281 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002282 public boolean isVideoCallingEnabled(String callingPackage) {
2283 if (!canReadPhoneState(callingPackage, "isVideoCallingEnabled")) {
2284 return false;
2285 }
2286
Andrew Lee77527ac2014-10-21 16:57:39 -07002287 // Check the user preference and the system-level IMS setting. Even if the user has
2288 // enabled video calling, if IMS is disabled we aren't able to support video calling.
2289 // In the long run, we may instead need to check if there exists a connection service
2290 // which can support video calling.
Andrew Lee312e8172014-10-23 17:01:36 -07002291 return ImsManager.isVtEnabledByPlatform(mPhone.getContext())
2292 && ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
2293 && mTelephonySharedPreferences.getBoolean(PREF_ENABLE_VIDEO_CALLING, true);
Andrew Leedf14ead2014-10-17 14:22:52 -07002294 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002295
Andrew Leea1239f22015-03-02 17:44:07 -08002296 @Override
2297 public boolean canChangeDtmfToneLength() {
Junda Liu12f7d802015-05-01 12:06:44 -07002298 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.BOOL_DTMF_TYPE_ENABLED);
Andrew Leea1239f22015-03-02 17:44:07 -08002299 }
2300
2301 @Override
2302 public boolean isWorldPhone() {
Junda Liu12f7d802015-05-01 12:06:44 -07002303 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.BOOL_WORLD_PHONE);
Andrew Leea1239f22015-03-02 17:44:07 -08002304 }
2305
Andrew Lee9431b832015-03-09 18:46:45 -07002306 @Override
2307 public boolean isTtyModeSupported() {
2308 TelecomManager telecomManager = TelecomManager.from(mPhone.getContext());
2309 TelephonyManager telephonyManager =
2310 (TelephonyManager) mPhone.getContext().getSystemService(Context.TELEPHONY_SERVICE);
2311 return !telephonyManager.isMultiSimEnabled() && telecomManager.isTtySupported();
2312 }
2313
2314 @Override
2315 public boolean isHearingAidCompatibilitySupported() {
2316 return mPhone.getContext().getResources().getBoolean(R.bool.hac_enabled);
2317 }
2318
Sanket Padawe7310cc72015-01-14 09:53:20 -08002319 /**
2320 * Returns the unique device ID of phone, for example, the IMEI for
2321 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
2322 *
2323 * <p>Requires Permission:
2324 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
2325 */
2326 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002327 public String getDeviceId(String callingPackage) {
2328 if (!canReadPhoneState(callingPackage, "getDeviceId")) {
2329 return null;
2330 }
2331
Sanket Padawe7310cc72015-01-14 09:53:20 -08002332 final Phone phone = PhoneFactory.getPhone(0);
2333 if (phone != null) {
2334 return phone.getDeviceId();
2335 } else {
2336 return null;
2337 }
2338 }
2339
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002340 /*
2341 * {@hide}
2342 * Returns the IMS Registration Status
2343 */
Santos Cordon7a1885b2015-02-03 11:15:19 -08002344 @Override
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002345 public boolean isImsRegistered() {
2346 return mPhone.isImsRegistered();
2347 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08002348
2349 @Override
2350 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
2351 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
2352 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07002353
Nathan Haroldc55097a2015-03-11 18:14:50 -07002354 /*
2355 * {@hide}
2356 * Returns the IMS Registration Status
2357 */
2358 public boolean isWifiCallingEnabled() {
2359 return mPhone.isWifiCallingEnabled();
2360 }
2361
2362 /*
2363 * {@hide}
2364 * Returns the IMS Registration Status
2365 */
2366 public boolean isVolteEnabled() {
2367 return mPhone.isVolteEnabled();
2368 }
Svet Ganovb320e182015-04-16 12:30:10 -07002369
2370 private boolean canReadPhoneState(String callingPackage, String message) {
2371 mApp.enforceCallingOrSelfPermission(
2372 android.Manifest.permission.READ_PHONE_STATE, message);
2373
2374 if (mAppOps.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(),
2375 callingPackage) != AppOpsManager.MODE_ALLOWED) {
2376 return false;
2377 }
2378
2379 return true;
2380 }
Stuart Scott8eef64f2015-04-08 15:13:54 -07002381
2382 @Override
2383 public void factoryReset(int subId) {
2384 enforceConnectivityInternalPermission();
Svet Ganovcc087f82015-05-12 20:35:54 -07002385 final long identity = Binder.clearCallingIdentity();
2386 try {
2387 if (SubscriptionManager.isUsableSubIdValue(subId)) {
2388 // Enable data
2389 setDataEnabled(subId, true);
2390 // Set network selection mode to automatic
2391 setNetworkSelectionModeAutomatic(subId);
2392 // Set preferred mobile network type to the best available
2393 setPreferredNetworkType(subId, Phone.PREFERRED_NT_MODE);
2394 // Turn off roaming
2395 SubscriptionManager.from(mApp).setDataRoaming(0, subId);
2396 }
2397 } finally {
2398 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07002399 }
2400 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01002401
2402 @Override
2403 public String getLocaleFromDefaultSim() {
2404 // We query all subscriptions instead of just the active ones, because
2405 // this might be called early on in the provisioning flow when the
2406 // subscriptions potentially aren't active yet.
2407 final List<SubscriptionInfo> slist = getAllSubscriptionInfoList();
2408 if (slist == null || slist.isEmpty()) {
2409 return null;
2410 }
2411
2412 // This function may be called very early, say, from the setup wizard, at
2413 // which point we won't have a default subscription set. If that's the case
2414 // we just choose the first, which will be valid in "most cases".
2415 final int defaultSubId = getDefaultSubscription();
2416 SubscriptionInfo info = null;
2417 if (defaultSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
2418 info = slist.get(0);
2419 } else {
2420 for (SubscriptionInfo item : slist) {
2421 if (item.getSubscriptionId() == defaultSubId) {
2422 info = item;
2423 break;
2424 }
2425 }
2426
2427 if (info == null) {
2428 return null;
2429 }
2430 }
2431
2432 // Try and fetch the locale from the carrier properties or from the SIM language
2433 // preferences (EF-PL and EF-LI)...
2434 final Phone defaultPhone = getPhone(info.getSubscriptionId());
2435 if (defaultPhone != null) {
2436 final Locale localeFromDefaultSim = defaultPhone.getLocaleFromSimAndCarrierPrefs();
2437 if (localeFromDefaultSim != null) {
2438 return localeFromDefaultSim.toLanguageTag();
2439 }
2440 }
2441
2442 // .. if that doesn't work, try and guess the language from the sim MCC.
2443 final int mcc = info.getMcc();
2444 final Locale locale = MccTable.getLocaleFromMcc(mPhone.getContext(), mcc);
2445 if (locale != null) {
2446 return locale.toLanguageTag();
2447 }
2448
2449 return null;
2450 }
2451
2452 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
2453 final long identity = Binder.clearCallingIdentity();
2454 try {
2455 return mSubscriptionController.getAllSubInfoList(
2456 mPhone.getContext().getOpPackageName());
2457 } finally {
2458 Binder.restoreCallingIdentity(identity);
2459 }
2460 }
2461
2462 private List<SubscriptionInfo> getActiveSubscriptionInfoList() {
2463 final long identity = Binder.clearCallingIdentity();
2464 try {
2465 return mSubscriptionController.getActiveSubscriptionInfoList(
2466 mPhone.getContext().getOpPackageName());
2467 } finally {
2468 Binder.restoreCallingIdentity(identity);
2469 }
2470 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002471}