blob: 5b44f5a0ac213ef8a5bb858ace59e6c06779839a [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;
Stuart Scott981d8582015-04-21 14:09:50 -070036import android.os.UserManager;
Derek Tan97ebb422014-09-05 16:55:38 -070037import android.preference.PreferenceManager;
Ihab Awadf2177b72013-11-25 13:33:23 -080038import android.provider.Settings;
Santos Cordon7a1885b2015-02-03 11:15:19 -080039import android.telecom.PhoneAccount;
Andrew Lee9431b832015-03-09 18:46:45 -070040import android.telecom.TelecomManager;
Junda Liu12f7d802015-05-01 12:06:44 -070041import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070042import android.telephony.CellInfo;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -070043import android.telephony.IccOpenLogicalChannelResponse;
Jake Hambye994d462014-02-03 13:10:13 -080044import android.telephony.NeighboringCellInfo;
Wink Saville5d475dd2014-10-17 15:00:58 -070045import android.telephony.RadioAccessFamily;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070046import android.telephony.ServiceState;
Wink Saville0f3b5fc2014-11-11 08:40:49 -080047import android.telephony.SubscriptionInfo;
Jeff Sharkey85190e62014-12-05 09:40:12 -080048import android.telephony.SubscriptionManager;
Ihab Awadf2177b72013-11-25 13:33:23 -080049import android.telephony.TelephonyManager;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -070050import android.telephony.ModemActivityInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070051import android.text.TextUtils;
Jeff Sharkey85190e62014-12-05 09:40:12 -080052import android.util.ArrayMap;
53import android.util.ArraySet;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070054import android.util.Log;
Jake Hambye994d462014-02-03 13:10:13 -080055import android.util.Pair;
Jeff Sharkey85190e62014-12-05 09:40:12 -080056import android.util.Slog;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070057
Andrew Lee312e8172014-10-23 17:01:36 -070058import com.android.ims.ImsManager;
Shishir Agrawal566b7612013-10-28 14:41:00 -070059import com.android.internal.telephony.CallManager;
Shishir Agrawal302c8692015-06-19 13:49:39 -070060import com.android.internal.telephony.CellNetworkScanResult;
Shishir Agrawal566b7612013-10-28 14:41:00 -070061import com.android.internal.telephony.CommandException;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070062import com.android.internal.telephony.DefaultPhoneNotifier;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070063import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -080064import com.android.internal.telephony.IccCard;
Narayan Kamath1c496c22015-04-16 14:40:19 +010065import com.android.internal.telephony.MccTable;
Shishir Agrawal302c8692015-06-19 13:49:39 -070066import com.android.internal.telephony.OperatorInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070067import com.android.internal.telephony.Phone;
Wink Saville36469e72014-06-11 15:17:00 -070068import com.android.internal.telephony.PhoneFactory;
Wink Saville5d475dd2014-10-17 15:00:58 -070069import com.android.internal.telephony.ProxyController;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070070import com.android.internal.telephony.PhoneConstants;
Svet Ganovb320e182015-04-16 12:30:10 -070071import com.android.internal.telephony.RILConstants;
Wink Savilleac1bdfd2014-11-20 23:04:44 -080072import com.android.internal.telephony.SubscriptionController;
Shishir Agrawal566b7612013-10-28 14:41:00 -070073import com.android.internal.telephony.uicc.IccIoResult;
74import com.android.internal.telephony.uicc.IccUtils;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -070075import com.android.internal.telephony.uicc.UiccCard;
Shishir Agrawal566b7612013-10-28 14:41:00 -070076import com.android.internal.telephony.uicc.UiccController;
Jake Hambye994d462014-02-03 13:10:13 -080077import com.android.internal.util.HexDump;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070078
Wink Saville36469e72014-06-11 15:17:00 -070079import static com.android.internal.telephony.PhoneConstants.SUBSCRIPTION_KEY;
80
Santos Cordon7d4ddf62013-07-10 11:58:08 -070081import java.util.ArrayList;
Jeff Sharkey85190e62014-12-05 09:40:12 -080082import java.util.Arrays;
Shishir Agrawal621a47c2014-12-01 10:25:09 -080083import java.util.HashMap;
84import java.util.Iterator;
Jake Hambye994d462014-02-03 13:10:13 -080085import java.util.List;
Narayan Kamath1c496c22015-04-16 14:40:19 +010086import java.util.Locale;
Jeff Sharkey85190e62014-12-05 09:40:12 -080087import java.util.Map;
88import java.util.Objects;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070089
90/**
91 * Implementation of the ITelephony interface.
92 */
Santos Cordon117fee72014-05-16 17:56:12 -070093public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -070094 private static final String LOG_TAG = "PhoneInterfaceManager";
95 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
96 private static final boolean DBG_LOC = false;
Jeff Sharkey85190e62014-12-05 09:40:12 -080097 private static final boolean DBG_MERGE = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070098
99 // Message codes used with mMainThreadHandler
100 private static final int CMD_HANDLE_PIN_MMI = 1;
101 private static final int CMD_HANDLE_NEIGHBORING_CELL = 2;
102 private static final int EVENT_NEIGHBORING_CELL_DONE = 3;
103 private static final int CMD_ANSWER_RINGING_CALL = 4;
104 private static final int CMD_END_CALL = 5; // not used yet
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700105 private static final int CMD_TRANSMIT_APDU_LOGICAL_CHANNEL = 7;
106 private static final int EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE = 8;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700107 private static final int CMD_OPEN_CHANNEL = 9;
108 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
109 private static final int CMD_CLOSE_CHANNEL = 11;
110 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -0800111 private static final int CMD_NV_READ_ITEM = 13;
112 private static final int EVENT_NV_READ_ITEM_DONE = 14;
113 private static final int CMD_NV_WRITE_ITEM = 15;
114 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
115 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
116 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
117 private static final int CMD_NV_RESET_CONFIG = 19;
118 private static final int EVENT_NV_RESET_CONFIG_DONE = 20;
Jake Hamby7c27be32014-03-03 13:25:59 -0800119 private static final int CMD_GET_PREFERRED_NETWORK_TYPE = 21;
120 private static final int EVENT_GET_PREFERRED_NETWORK_TYPE_DONE = 22;
121 private static final int CMD_SET_PREFERRED_NETWORK_TYPE = 23;
122 private static final int EVENT_SET_PREFERRED_NETWORK_TYPE_DONE = 24;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800123 private static final int CMD_SEND_ENVELOPE = 25;
124 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Derek Tan6b088ee2014-09-05 14:15:18 -0700125 private static final int CMD_INVOKE_OEM_RIL_REQUEST_RAW = 27;
126 private static final int EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE = 28;
127 private static final int CMD_TRANSMIT_APDU_BASIC_CHANNEL = 29;
128 private static final int EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE = 30;
129 private static final int CMD_EXCHANGE_SIM_IO = 31;
130 private static final int EVENT_EXCHANGE_SIM_IO_DONE = 32;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800131 private static final int CMD_SET_VOICEMAIL_NUMBER = 33;
132 private static final int EVENT_SET_VOICEMAIL_NUMBER_DONE = 34;
Stuart Scott54788802015-03-30 13:18:01 -0700133 private static final int CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC = 35;
134 private static final int EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE = 36;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700135 private static final int CMD_GET_MODEM_ACTIVITY_INFO = 37;
136 private static final int EVENT_GET_MODEM_ACTIVITY_INFO_DONE = 38;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700137 private static final int CMD_PERFORM_NETWORK_SCAN = 39;
138 private static final int EVENT_PERFORM_NETWORK_SCAN_DONE = 40;
139 private static final int CMD_SET_NETWORK_SELECTION_MODE_MANUAL = 41;
140 private static final int EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE = 42;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700141
142 /** The singleton instance. */
143 private static PhoneInterfaceManager sInstance;
144
Wink Saville3ab207e2014-11-20 13:07:20 -0800145 private PhoneGlobals mApp;
146 private Phone mPhone;
147 private CallManager mCM;
Stuart Scott981d8582015-04-21 14:09:50 -0700148 private UserManager mUserManager;
Wink Saville3ab207e2014-11-20 13:07:20 -0800149 private AppOpsManager mAppOps;
150 private MainThreadHandler mMainThreadHandler;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800151 private SubscriptionController mSubscriptionController;
Wink Saville3ab207e2014-11-20 13:07:20 -0800152 private SharedPreferences mTelephonySharedPreferences;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700153
Derek Tan97ebb422014-09-05 16:55:38 -0700154 private static final String PREF_CARRIERS_ALPHATAG_PREFIX = "carrier_alphtag_";
155 private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
Jeff Sharkey85190e62014-12-05 09:40:12 -0800156 private static final String PREF_CARRIERS_SUBSCRIBER_PREFIX = "carrier_subscriber_";
Andrew Leedf14ead2014-10-17 14:22:52 -0700157 private static final String PREF_ENABLE_VIDEO_CALLING = "enable_video_calling";
Derek Tan89e89d42014-07-08 17:00:10 -0700158
159 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700160 * A request object to use for transmitting data to an ICC.
161 */
162 private static final class IccAPDUArgument {
163 public int channel, cla, command, p1, p2, p3;
164 public String data;
165
166 public IccAPDUArgument(int channel, int cla, int command,
167 int p1, int p2, int p3, String data) {
168 this.channel = channel;
169 this.cla = cla;
170 this.command = command;
171 this.p1 = p1;
172 this.p2 = p2;
173 this.p3 = p3;
174 this.data = data;
175 }
176 }
177
178 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700179 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
180 * request after sending. The main thread will notify the request when it is complete.
181 */
182 private static final class MainThreadRequest {
183 /** The argument to use for the request */
184 public Object argument;
185 /** The result of the request that is run on the main thread */
186 public Object result;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800187 /** The subscriber id that this request applies to. Null if default. */
188 public Integer subId;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700189
190 public MainThreadRequest(Object argument) {
191 this.argument = argument;
192 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800193
194 public MainThreadRequest(Object argument, Integer subId) {
195 this.argument = argument;
196 this.subId = subId;
197 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700198 }
199
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800200 private static final class IncomingThirdPartyCallArgs {
201 public final ComponentName component;
202 public final String callId;
203 public final String callerDisplayName;
204
205 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
206 String callerDisplayName) {
207 this.component = component;
208 this.callId = callId;
209 this.callerDisplayName = callerDisplayName;
210 }
211 }
212
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700213 /**
214 * A handler that processes messages on the main thread in the phone process. Since many
215 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
216 * inbound binder threads to the main thread in the phone process. The Binder thread
217 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
218 * on, which will be notified when the operation completes and will contain the result of the
219 * request.
220 *
221 * <p>If a MainThreadRequest object is provided in the msg.obj field,
222 * note that request.result must be set to something non-null for the calling thread to
223 * unblock.
224 */
225 private final class MainThreadHandler extends Handler {
226 @Override
227 public void handleMessage(Message msg) {
228 MainThreadRequest request;
229 Message onCompleted;
230 AsyncResult ar;
Shishir Agrawal21409252015-01-15 23:33:50 -0800231 UiccCard uiccCard = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700232 IccAPDUArgument iccArgument;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700233
234 switch (msg.what) {
235 case CMD_HANDLE_PIN_MMI:
236 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800237 request.result = getPhoneFromRequest(request).handlePinMmi(
238 (String) request.argument);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700239 // Wake up the requesting thread
240 synchronized (request) {
241 request.notifyAll();
242 }
243 break;
244
245 case CMD_HANDLE_NEIGHBORING_CELL:
246 request = (MainThreadRequest) msg.obj;
247 onCompleted = obtainMessage(EVENT_NEIGHBORING_CELL_DONE,
248 request);
249 mPhone.getNeighboringCids(onCompleted);
250 break;
251
252 case EVENT_NEIGHBORING_CELL_DONE:
253 ar = (AsyncResult) msg.obj;
254 request = (MainThreadRequest) ar.userObj;
255 if (ar.exception == null && ar.result != null) {
256 request.result = ar.result;
257 } else {
258 // create an empty list to notify the waiting thread
Jake Hambye994d462014-02-03 13:10:13 -0800259 request.result = new ArrayList<NeighboringCellInfo>(0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700260 }
261 // Wake up the requesting thread
262 synchronized (request) {
263 request.notifyAll();
264 }
265 break;
266
267 case CMD_ANSWER_RINGING_CALL:
Wink Saville08874612014-08-31 19:19:58 -0700268 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800269 int answer_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700270 answerRingingCallInternal(answer_subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700271 break;
272
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700273 case CMD_END_CALL:
274 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800275 int end_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700276 final boolean hungUp;
Anthony Leeae4e36d2015-05-21 07:17:46 -0700277 Phone phone = getPhone(end_subId);
278 if (phone == null) {
279 if (DBG) log("CMD_END_CALL: no phone for id: " + end_subId);
280 break;
281 }
282 int phoneType = phone.getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700283 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
284 // CDMA: If the user presses the Power button we treat it as
285 // ending the complete call session
Wink Saville08874612014-08-31 19:19:58 -0700286 hungUp = PhoneUtils.hangupRingingAndActive(getPhone(end_subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700287 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
288 // GSM: End the call as per the Phone state
289 hungUp = PhoneUtils.hangup(mCM);
290 } else {
291 throw new IllegalStateException("Unexpected phone type: " + phoneType);
292 }
293 if (DBG) log("CMD_END_CALL: " + (hungUp ? "hung up!" : "no call to hang up"));
294 request.result = hungUp;
295 // Wake up the requesting thread
296 synchronized (request) {
297 request.notifyAll();
298 }
299 break;
300
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700301 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700302 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700303 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700304 if (uiccCard == null) {
305 loge("iccTransmitApduLogicalChannel: No UICC");
306 request.result = new IccIoResult(0x6F, 0, (byte[])null);
307 synchronized (request) {
308 request.notifyAll();
309 }
310 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700311 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
312 request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700313 uiccCard.iccTransmitApduLogicalChannel(
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700314 iccArgument.channel, iccArgument.cla, iccArgument.command,
315 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
Shishir Agrawal566b7612013-10-28 14:41:00 -0700316 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700317 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700318 break;
319
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700320 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700321 ar = (AsyncResult) msg.obj;
322 request = (MainThreadRequest) ar.userObj;
323 if (ar.exception == null && ar.result != null) {
324 request.result = ar.result;
325 } else {
326 request.result = new IccIoResult(0x6F, 0, (byte[])null);
327 if (ar.result == null) {
328 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800329 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700330 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800331 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700332 } else {
333 loge("iccTransmitApduLogicalChannel: Unknown exception");
334 }
335 }
336 synchronized (request) {
337 request.notifyAll();
338 }
339 break;
340
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700341 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
342 request = (MainThreadRequest) msg.obj;
343 iccArgument = (IccAPDUArgument) request.argument;
344 if (uiccCard == null) {
345 loge("iccTransmitApduBasicChannel: No UICC");
346 request.result = new IccIoResult(0x6F, 0, (byte[])null);
347 synchronized (request) {
348 request.notifyAll();
349 }
350 } else {
351 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
352 request);
353 uiccCard.iccTransmitApduBasicChannel(
354 iccArgument.cla, iccArgument.command, iccArgument.p1, iccArgument.p2,
355 iccArgument.p3, iccArgument.data, onCompleted);
356 }
357 break;
358
359 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
360 ar = (AsyncResult) msg.obj;
361 request = (MainThreadRequest) ar.userObj;
362 if (ar.exception == null && ar.result != null) {
363 request.result = ar.result;
364 } else {
365 request.result = new IccIoResult(0x6F, 0, (byte[])null);
366 if (ar.result == null) {
367 loge("iccTransmitApduBasicChannel: Empty response");
368 } else if (ar.exception instanceof CommandException) {
369 loge("iccTransmitApduBasicChannel: CommandException: " +
370 ar.exception);
371 } else {
372 loge("iccTransmitApduBasicChannel: Unknown exception");
373 }
374 }
375 synchronized (request) {
376 request.notifyAll();
377 }
378 break;
379
380 case CMD_EXCHANGE_SIM_IO:
381 request = (MainThreadRequest) msg.obj;
382 iccArgument = (IccAPDUArgument) request.argument;
383 if (uiccCard == null) {
384 loge("iccExchangeSimIO: No UICC");
385 request.result = new IccIoResult(0x6F, 0, (byte[])null);
386 synchronized (request) {
387 request.notifyAll();
388 }
389 } else {
390 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
391 request);
392 uiccCard.iccExchangeSimIO(iccArgument.cla, /* fileID */
393 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
394 iccArgument.data, onCompleted);
395 }
396 break;
397
398 case EVENT_EXCHANGE_SIM_IO_DONE:
399 ar = (AsyncResult) msg.obj;
400 request = (MainThreadRequest) ar.userObj;
401 if (ar.exception == null && ar.result != null) {
402 request.result = ar.result;
403 } else {
404 request.result = new IccIoResult(0x6f, 0, (byte[])null);
405 }
406 synchronized (request) {
407 request.notifyAll();
408 }
409 break;
410
Derek Tan4d5e5c12014-02-04 11:54:58 -0800411 case CMD_SEND_ENVELOPE:
412 request = (MainThreadRequest) msg.obj;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700413 if (uiccCard == null) {
414 loge("sendEnvelopeWithStatus: No UICC");
415 request.result = new IccIoResult(0x6F, 0, (byte[])null);
416 synchronized (request) {
417 request.notifyAll();
418 }
419 } else {
420 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
421 uiccCard.sendEnvelopeWithStatus((String)request.argument, onCompleted);
422 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800423 break;
424
425 case EVENT_SEND_ENVELOPE_DONE:
426 ar = (AsyncResult) msg.obj;
427 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700428 if (ar.exception == null && ar.result != null) {
429 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800430 } else {
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700431 request.result = new IccIoResult(0x6F, 0, (byte[])null);
432 if (ar.result == null) {
433 loge("sendEnvelopeWithStatus: Empty response");
434 } else if (ar.exception instanceof CommandException) {
435 loge("sendEnvelopeWithStatus: CommandException: " +
436 ar.exception);
437 } else {
438 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
439 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800440 }
441 synchronized (request) {
442 request.notifyAll();
443 }
444 break;
445
Shishir Agrawal566b7612013-10-28 14:41:00 -0700446 case CMD_OPEN_CHANNEL:
447 request = (MainThreadRequest) msg.obj;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700448 if (uiccCard == null) {
449 loge("iccOpenLogicalChannel: No UICC");
450 request.result = new IccIoResult(0x6F, 0, (byte[])null);
451 synchronized (request) {
452 request.notifyAll();
453 }
454 } else {
455 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
456 uiccCard.iccOpenLogicalChannel((String)request.argument, onCompleted);
457 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700458 break;
459
460 case EVENT_OPEN_CHANNEL_DONE:
461 ar = (AsyncResult) msg.obj;
462 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700463 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700464 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700465 int[] result = (int[]) ar.result;
466 int channelId = result[0];
467 byte[] selectResponse = null;
468 if (result.length > 1) {
469 selectResponse = new byte[result.length - 1];
470 for (int i = 1; i < result.length; ++i) {
471 selectResponse[i - 1] = (byte) result[i];
472 }
473 }
474 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700475 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700476 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700477 if (ar.result == null) {
478 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700479 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700480 if (ar.exception != null) {
481 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
482 }
483
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700484 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Junda Liua754ba12015-05-20 01:17:52 -0700485 if (ar.exception instanceof CommandException) {
486 CommandException.Error error =
487 ((CommandException) (ar.exception)).getCommandError();
488 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700489 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700490 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700491 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700492 }
493 }
494 openChannelResp = new IccOpenLogicalChannelResponse(
495 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700496 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700497 request.result = openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700498 synchronized (request) {
499 request.notifyAll();
500 }
501 break;
502
503 case CMD_CLOSE_CHANNEL:
504 request = (MainThreadRequest) msg.obj;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700505 if (uiccCard == null) {
506 loge("iccCloseLogicalChannel: No UICC");
507 request.result = new IccIoResult(0x6F, 0, (byte[])null);
508 synchronized (request) {
509 request.notifyAll();
510 }
511 } else {
512 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
513 uiccCard.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
514 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700515 break;
516
517 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800518 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
519 break;
520
521 case CMD_NV_READ_ITEM:
522 request = (MainThreadRequest) msg.obj;
523 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
524 mPhone.nvReadItem((Integer) request.argument, onCompleted);
525 break;
526
527 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700528 ar = (AsyncResult) msg.obj;
529 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800530 if (ar.exception == null && ar.result != null) {
531 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700532 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800533 request.result = "";
534 if (ar.result == null) {
535 loge("nvReadItem: Empty response");
536 } else if (ar.exception instanceof CommandException) {
537 loge("nvReadItem: CommandException: " +
538 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700539 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800540 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700541 }
542 }
543 synchronized (request) {
544 request.notifyAll();
545 }
546 break;
547
Jake Hambye994d462014-02-03 13:10:13 -0800548 case CMD_NV_WRITE_ITEM:
549 request = (MainThreadRequest) msg.obj;
550 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
551 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
552 mPhone.nvWriteItem(idValue.first, idValue.second, onCompleted);
553 break;
554
555 case EVENT_NV_WRITE_ITEM_DONE:
556 handleNullReturnEvent(msg, "nvWriteItem");
557 break;
558
559 case CMD_NV_WRITE_CDMA_PRL:
560 request = (MainThreadRequest) msg.obj;
561 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
562 mPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
563 break;
564
565 case EVENT_NV_WRITE_CDMA_PRL_DONE:
566 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
567 break;
568
569 case CMD_NV_RESET_CONFIG:
570 request = (MainThreadRequest) msg.obj;
571 onCompleted = obtainMessage(EVENT_NV_RESET_CONFIG_DONE, request);
572 mPhone.nvResetConfig((Integer) request.argument, onCompleted);
573 break;
574
575 case EVENT_NV_RESET_CONFIG_DONE:
576 handleNullReturnEvent(msg, "nvResetConfig");
577 break;
578
Jake Hamby7c27be32014-03-03 13:25:59 -0800579 case CMD_GET_PREFERRED_NETWORK_TYPE:
580 request = (MainThreadRequest) msg.obj;
581 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
Stuart Scott54788802015-03-30 13:18:01 -0700582 getPhoneFromRequest(request).getPreferredNetworkType(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800583 break;
584
585 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
586 ar = (AsyncResult) msg.obj;
587 request = (MainThreadRequest) ar.userObj;
588 if (ar.exception == null && ar.result != null) {
589 request.result = ar.result; // Integer
590 } else {
591 request.result = -1;
592 if (ar.result == null) {
593 loge("getPreferredNetworkType: Empty response");
594 } else if (ar.exception instanceof CommandException) {
595 loge("getPreferredNetworkType: CommandException: " +
596 ar.exception);
597 } else {
598 loge("getPreferredNetworkType: Unknown exception");
599 }
600 }
601 synchronized (request) {
602 request.notifyAll();
603 }
604 break;
605
606 case CMD_SET_PREFERRED_NETWORK_TYPE:
607 request = (MainThreadRequest) msg.obj;
608 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
609 int networkType = (Integer) request.argument;
Stuart Scott54788802015-03-30 13:18:01 -0700610 getPhoneFromRequest(request).setPreferredNetworkType(networkType, onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800611 break;
612
613 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
614 handleNullReturnEvent(msg, "setPreferredNetworkType");
615 break;
616
Steven Liu4bf01bc2014-07-17 11:05:29 -0500617 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
618 request = (MainThreadRequest)msg.obj;
619 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
620 mPhone.invokeOemRilRequestRaw((byte[])request.argument, onCompleted);
621 break;
622
623 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
624 ar = (AsyncResult)msg.obj;
625 request = (MainThreadRequest)ar.userObj;
626 request.result = ar;
627 synchronized (request) {
628 request.notifyAll();
629 }
630 break;
631
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800632 case CMD_SET_VOICEMAIL_NUMBER:
633 request = (MainThreadRequest) msg.obj;
634 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
635 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -0800636 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
637 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800638 break;
639
640 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
641 handleNullReturnEvent(msg, "setVoicemailNumber");
642 break;
643
Stuart Scott54788802015-03-30 13:18:01 -0700644 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
645 request = (MainThreadRequest) msg.obj;
646 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
647 request);
648 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
649 break;
650
651 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
652 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
653 break;
654
Shishir Agrawal302c8692015-06-19 13:49:39 -0700655 case CMD_PERFORM_NETWORK_SCAN:
656 request = (MainThreadRequest) msg.obj;
657 onCompleted = obtainMessage(EVENT_PERFORM_NETWORK_SCAN_DONE, request);
658 getPhoneFromRequest(request).getAvailableNetworks(onCompleted);
659 break;
660
661 case EVENT_PERFORM_NETWORK_SCAN_DONE:
662 ar = (AsyncResult) msg.obj;
663 request = (MainThreadRequest) ar.userObj;
664 CellNetworkScanResult cellScanResult;
665 if (ar.exception == null && ar.result != null) {
666 cellScanResult = new CellNetworkScanResult(
667 CellNetworkScanResult.STATUS_SUCCESS,
668 (List<OperatorInfo>) ar.result);
669 } else {
670 if (ar.result == null) {
671 loge("getCellNetworkScanResults: Empty response");
672 }
673 if (ar.exception != null) {
674 loge("getCellNetworkScanResults: Exception: " + ar.exception);
675 }
676 int errorCode = CellNetworkScanResult.STATUS_UNKNOWN_ERROR;
677 if (ar.exception instanceof CommandException) {
678 CommandException.Error error =
679 ((CommandException) (ar.exception)).getCommandError();
680 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
681 errorCode = CellNetworkScanResult.STATUS_RADIO_NOT_AVAILABLE;
682 } else if (error == CommandException.Error.GENERIC_FAILURE) {
683 errorCode = CellNetworkScanResult.STATUS_RADIO_GENERIC_FAILURE;
684 }
685 }
686 cellScanResult = new CellNetworkScanResult(errorCode, null);
687 }
688 request.result = cellScanResult;
689 synchronized (request) {
690 request.notifyAll();
691 }
692 break;
693
694 case CMD_SET_NETWORK_SELECTION_MODE_MANUAL:
695 request = (MainThreadRequest) msg.obj;
696 OperatorInfo operator = (OperatorInfo) request.argument;
697 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE,
698 request);
699 getPhoneFromRequest(request).selectNetworkManually(operator, onCompleted);
700 break;
701
702 case EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE:
703 handleNullReturnEvent(msg, "setNetworkSelectionModeManual");
704 break;
705
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700706 case CMD_GET_MODEM_ACTIVITY_INFO:
707 request = (MainThreadRequest) msg.obj;
708 onCompleted = obtainMessage(EVENT_GET_MODEM_ACTIVITY_INFO_DONE, request);
Prerepa Viswanadham61a60ad2015-06-08 18:07:51 -0700709 mPhone.getModemActivityInfo(onCompleted);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700710 break;
711
712 case EVENT_GET_MODEM_ACTIVITY_INFO_DONE:
713 ar = (AsyncResult) msg.obj;
714 request = (MainThreadRequest) ar.userObj;
715 if (ar.exception == null && ar.result != null) {
716 request.result = ar.result;
717 } else {
718 if (ar.result == null) {
719 loge("queryModemActivityInfo: Empty response");
720 } else if (ar.exception instanceof CommandException) {
721 loge("queryModemActivityInfo: CommandException: " +
722 ar.exception);
723 } else {
724 loge("queryModemActivityInfo: Unknown exception");
725 }
726 }
727 synchronized (request) {
728 request.notifyAll();
729 }
730 break;
731
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700732 default:
733 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
734 break;
735 }
736 }
Jake Hambye994d462014-02-03 13:10:13 -0800737
738 private void handleNullReturnEvent(Message msg, String command) {
739 AsyncResult ar = (AsyncResult) msg.obj;
740 MainThreadRequest request = (MainThreadRequest) ar.userObj;
741 if (ar.exception == null) {
742 request.result = true;
743 } else {
744 request.result = false;
745 if (ar.exception instanceof CommandException) {
746 loge(command + ": CommandException: " + ar.exception);
747 } else {
748 loge(command + ": Unknown exception");
749 }
750 }
751 synchronized (request) {
752 request.notifyAll();
753 }
754 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700755 }
756
757 /**
758 * Posts the specified command to be executed on the main thread,
759 * waits for the request to complete, and returns the result.
760 * @see #sendRequestAsync
761 */
762 private Object sendRequest(int command, Object argument) {
Santos Cordon500b0e02014-06-17 10:33:33 -0700763 return sendRequest(command, argument, null);
Wink Saville36469e72014-06-11 15:17:00 -0700764 }
765
766 /**
767 * Posts the specified command to be executed on the main thread,
768 * waits for the request to complete, and returns the result.
769 * @see #sendRequestAsync
770 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800771 private Object sendRequest(int command, Object argument, Integer subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700772 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
773 throw new RuntimeException("This method will deadlock if called from the main thread.");
774 }
775
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800776 MainThreadRequest request = new MainThreadRequest(argument, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700777 Message msg = mMainThreadHandler.obtainMessage(command, request);
778 msg.sendToTarget();
779
780 // Wait for the request to complete
781 synchronized (request) {
782 while (request.result == null) {
783 try {
784 request.wait();
785 } catch (InterruptedException e) {
786 // Do nothing, go back and wait until the request is complete
787 }
788 }
789 }
790 return request.result;
791 }
792
793 /**
794 * Asynchronous ("fire and forget") version of sendRequest():
795 * Posts the specified command to be executed on the main thread, and
796 * returns immediately.
797 * @see #sendRequest
798 */
799 private void sendRequestAsync(int command) {
800 mMainThreadHandler.sendEmptyMessage(command);
801 }
802
803 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -0700804 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
805 * @see {@link #sendRequest(int,Object)}
806 */
807 private void sendRequestAsync(int command, Object argument) {
808 MainThreadRequest request = new MainThreadRequest(argument);
809 Message msg = mMainThreadHandler.obtainMessage(command, request);
810 msg.sendToTarget();
811 }
812
813 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700814 * Initialize the singleton PhoneInterfaceManager instance.
815 * This is only done once, at startup, from PhoneApp.onCreate().
816 */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700817 /* package */ static PhoneInterfaceManager init(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700818 synchronized (PhoneInterfaceManager.class) {
819 if (sInstance == null) {
Sailesh Nepal194161e2014-07-03 08:57:44 -0700820 sInstance = new PhoneInterfaceManager(app, phone);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700821 } else {
822 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
823 }
824 return sInstance;
825 }
826 }
827
828 /** Private constructor; @see init() */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700829 private PhoneInterfaceManager(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700830 mApp = app;
831 mPhone = phone;
832 mCM = PhoneGlobals.getInstance().mCM;
Stuart Scott981d8582015-04-21 14:09:50 -0700833 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700834 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
835 mMainThreadHandler = new MainThreadHandler();
Andrew Leedf14ead2014-10-17 14:22:52 -0700836 mTelephonySharedPreferences =
Derek Tan97ebb422014-09-05 16:55:38 -0700837 PreferenceManager.getDefaultSharedPreferences(mPhone.getContext());
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800838 mSubscriptionController = SubscriptionController.getInstance();
Wink Saville3ab207e2014-11-20 13:07:20 -0800839
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700840 publish();
841 }
842
843 private void publish() {
844 if (DBG) log("publish: " + this);
845
846 ServiceManager.addService("phone", this);
847 }
848
Stuart Scott584921c2015-01-15 17:10:34 -0800849 private Phone getPhoneFromRequest(MainThreadRequest request) {
850 return (request.subId == null) ? mPhone : getPhone(request.subId);
851 }
852
Wink Saville36469e72014-06-11 15:17:00 -0700853 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -0700854 private Phone getPhone(int subId) {
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800855 return PhoneFactory.getPhone(mSubscriptionController.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -0700856 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700857 //
858 // Implementation of the ITelephony interface.
859 //
860
861 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700862 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -0700863 }
864
Wink Savilleb564aae2014-10-23 10:18:09 -0700865 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700866 if (DBG) log("dial: " + number);
867 // No permission check needed here: This is just a wrapper around the
868 // ACTION_DIAL intent, which is available to any app since it puts up
869 // the UI before it does anything.
870
871 String url = createTelUrl(number);
872 if (url == null) {
873 return;
874 }
875
876 // PENDING: should we just silently fail if phone is offhook or ringing?
Wink Saville36469e72014-06-11 15:17:00 -0700877 PhoneConstants.State state = mCM.getState(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700878 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
879 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
880 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
881 mApp.startActivity(intent);
882 }
883 }
884
885 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700886 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -0700887 }
888
Wink Savilleb564aae2014-10-23 10:18:09 -0700889 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700890 if (DBG) log("call: " + number);
891
892 // This is just a wrapper around the ACTION_CALL intent, but we still
893 // need to do a permission check since we're calling startActivity()
894 // from the context of the phone app.
895 enforceCallPermission();
896
897 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
898 != AppOpsManager.MODE_ALLOWED) {
899 return;
900 }
901
902 String url = createTelUrl(number);
903 if (url == null) {
904 return;
905 }
906
Wink Saville08874612014-08-31 19:19:58 -0700907 boolean isValid = false;
Narayan Kamath1c496c22015-04-16 14:40:19 +0100908 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoList();
Wink Saville3ab207e2014-11-20 13:07:20 -0800909 if (slist != null) {
910 for (SubscriptionInfo subInfoRecord : slist) {
911 if (subInfoRecord.getSubscriptionId() == subId) {
912 isValid = true;
913 break;
914 }
Wink Saville08874612014-08-31 19:19:58 -0700915 }
916 }
917 if (isValid == false) {
918 return;
919 }
920
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700921 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
Wink Saville36469e72014-06-11 15:17:00 -0700922 intent.putExtra(SUBSCRIPTION_KEY, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700923 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
924 mApp.startActivity(intent);
925 }
926
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700927 /**
928 * End a call based on call state
929 * @return true is a call was ended
930 */
931 public boolean endCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700932 return endCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700933 }
934
935 /**
936 * End a call based on the call state of the subId
937 * @return true is a call was ended
938 */
Wink Savilleb564aae2014-10-23 10:18:09 -0700939 public boolean endCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700940 enforceCallPermission();
Stuart Scott584921c2015-01-15 17:10:34 -0800941 return (Boolean) sendRequest(CMD_END_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700942 }
943
944 public void answerRingingCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700945 answerRingingCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700946 }
947
Wink Savilleb564aae2014-10-23 10:18:09 -0700948 public void answerRingingCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700949 if (DBG) log("answerRingingCall...");
950 // TODO: there should eventually be a separate "ANSWER_PHONE" permission,
951 // but that can probably wait till the big TelephonyManager API overhaul.
952 // For now, protect this call with the MODIFY_PHONE_STATE permission.
953 enforceModifyPermission();
Stuart Scott584921c2015-01-15 17:10:34 -0800954 sendRequest(CMD_ANSWER_RINGING_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700955 }
956
957 /**
958 * Make the actual telephony calls to implement answerRingingCall().
959 * This should only be called from the main thread of the Phone app.
960 * @see #answerRingingCall
961 *
962 * TODO: it would be nice to return true if we answered the call, or
963 * false if there wasn't actually a ringing incoming call, or some
964 * other error occurred. (In other words, pass back the return value
965 * from PhoneUtils.answerCall() or PhoneUtils.answerAndEndActive().)
966 * But that would require calling this method via sendRequest() rather
967 * than sendRequestAsync(), and right now we don't actually *need* that
968 * return value, so let's just return void for now.
969 */
Wink Savilleb564aae2014-10-23 10:18:09 -0700970 private void answerRingingCallInternal(int subId) {
Wink Saville08874612014-08-31 19:19:58 -0700971 final boolean hasRingingCall = !getPhone(subId).getRingingCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700972 if (hasRingingCall) {
Wink Saville08874612014-08-31 19:19:58 -0700973 final boolean hasActiveCall = !getPhone(subId).getForegroundCall().isIdle();
974 final boolean hasHoldingCall = !getPhone(subId).getBackgroundCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700975 if (hasActiveCall && hasHoldingCall) {
976 // Both lines are in use!
977 // TODO: provide a flag to let the caller specify what
978 // policy to use if both lines are in use. (The current
979 // behavior is hardwired to "answer incoming, end ongoing",
980 // which is how the CALL button is specced to behave.)
981 PhoneUtils.answerAndEndActive(mCM, mCM.getFirstActiveRingingCall());
982 return;
983 } else {
984 // answerCall() will automatically hold the current active
985 // call, if there is one.
986 PhoneUtils.answerCall(mCM.getFirstActiveRingingCall());
987 return;
988 }
989 } else {
990 // No call was ringing.
991 return;
992 }
993 }
994
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700995 /**
Santos Cordon5422a8d2014-09-12 04:20:56 -0700996 * This method is no longer used and can be removed once TelephonyManager stops referring to it.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700997 */
Santos Cordon5422a8d2014-09-12 04:20:56 -0700998 public void silenceRinger() {
999 Log.e(LOG_TAG, "silenseRinger not supported");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001000 }
1001
1002 public boolean isOffhook() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001003 return isOffhookForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001004 }
1005
Wink Savilleb564aae2014-10-23 10:18:09 -07001006 public boolean isOffhookForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001007 return (getPhone(subId).getState() == PhoneConstants.State.OFFHOOK);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001008 }
1009
1010 public boolean isRinging() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001011 return (isRingingForSubscriber(getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07001012 }
1013
Wink Savilleb564aae2014-10-23 10:18:09 -07001014 public boolean isRingingForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001015 return (getPhone(subId).getState() == PhoneConstants.State.RINGING);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001016 }
1017
1018 public boolean isIdle() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001019 return isIdleForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001020 }
1021
Wink Savilleb564aae2014-10-23 10:18:09 -07001022 public boolean isIdleForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001023 return (getPhone(subId).getState() == PhoneConstants.State.IDLE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001024 }
1025
Svet Ganovb320e182015-04-16 12:30:10 -07001026 public boolean isSimPinEnabled(String callingPackage) {
1027 if (!canReadPhoneState(callingPackage, "isSimPinEnabled")) {
1028 return false;
1029 }
1030
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001031 return (PhoneGlobals.getInstance().isSimPinEnabled());
1032 }
1033
1034 public boolean supplyPin(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001035 return supplyPinForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001036 }
1037
Wink Savilleb564aae2014-10-23 10:18:09 -07001038 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001039 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001040 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1041 }
1042
1043 public boolean supplyPuk(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001044 return supplyPukForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001045 }
1046
Wink Savilleb564aae2014-10-23 10:18:09 -07001047 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001048 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001049 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1050 }
1051
1052 /** {@hide} */
1053 public int[] supplyPinReportResult(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001054 return supplyPinReportResultForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001055 }
1056
Wink Savilleb564aae2014-10-23 10:18:09 -07001057 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001058 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001059 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001060 checkSimPin.start();
1061 return checkSimPin.unlockSim(null, pin);
1062 }
1063
Wink Saville9de0f752013-10-22 19:04:03 -07001064 /** {@hide} */
1065 public int[] supplyPukReportResult(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001066 return supplyPukReportResultForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001067 }
1068
Wink Savilleb564aae2014-10-23 10:18:09 -07001069 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001070 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001071 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001072 checkSimPuk.start();
1073 return checkSimPuk.unlockSim(puk, pin);
1074 }
1075
1076 /**
Wink Saville9de0f752013-10-22 19:04:03 -07001077 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001078 * a synchronous one.
1079 */
1080 private static class UnlockSim extends Thread {
1081
1082 private final IccCard mSimCard;
1083
1084 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07001085 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1086 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001087
1088 // For replies from SimCard interface
1089 private Handler mHandler;
1090
1091 // For async handler to identify request type
1092 private static final int SUPPLY_PIN_COMPLETE = 100;
1093
1094 public UnlockSim(IccCard simCard) {
1095 mSimCard = simCard;
1096 }
1097
1098 @Override
1099 public void run() {
1100 Looper.prepare();
1101 synchronized (UnlockSim.this) {
1102 mHandler = new Handler() {
1103 @Override
1104 public void handleMessage(Message msg) {
1105 AsyncResult ar = (AsyncResult) msg.obj;
1106 switch (msg.what) {
1107 case SUPPLY_PIN_COMPLETE:
1108 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
1109 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07001110 mRetryCount = msg.arg1;
1111 if (ar.exception != null) {
1112 if (ar.exception instanceof CommandException &&
1113 ((CommandException)(ar.exception)).getCommandError()
1114 == CommandException.Error.PASSWORD_INCORRECT) {
1115 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
1116 } else {
1117 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1118 }
1119 } else {
1120 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
1121 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001122 mDone = true;
1123 UnlockSim.this.notifyAll();
1124 }
1125 break;
1126 }
1127 }
1128 };
1129 UnlockSim.this.notifyAll();
1130 }
1131 Looper.loop();
1132 }
1133
1134 /*
1135 * Use PIN or PUK to unlock SIM card
1136 *
1137 * If PUK is null, unlock SIM card with PIN
1138 *
1139 * If PUK is not null, unlock SIM card with PUK and set PIN code
1140 */
Wink Saville9de0f752013-10-22 19:04:03 -07001141 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001142
1143 while (mHandler == null) {
1144 try {
1145 wait();
1146 } catch (InterruptedException e) {
1147 Thread.currentThread().interrupt();
1148 }
1149 }
1150 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
1151
1152 if (puk == null) {
1153 mSimCard.supplyPin(pin, callback);
1154 } else {
1155 mSimCard.supplyPuk(puk, pin, callback);
1156 }
1157
1158 while (!mDone) {
1159 try {
1160 Log.d(LOG_TAG, "wait for done");
1161 wait();
1162 } catch (InterruptedException e) {
1163 // Restore the interrupted status
1164 Thread.currentThread().interrupt();
1165 }
1166 }
1167 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07001168 int[] resultArray = new int[2];
1169 resultArray[0] = mResult;
1170 resultArray[1] = mRetryCount;
1171 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001172 }
1173 }
1174
1175 public void updateServiceLocation() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001176 updateServiceLocationForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001177
1178 }
1179
Wink Savilleb564aae2014-10-23 10:18:09 -07001180 public void updateServiceLocationForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001181 // No permission check needed here: this call is harmless, and it's
1182 // needed for the ServiceState.requestStateUpdate() call (which is
1183 // already intentionally exposed to 3rd parties.)
Wink Saville36469e72014-06-11 15:17:00 -07001184 getPhone(subId).updateServiceLocation();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001185 }
1186
1187 public boolean isRadioOn() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001188 return isRadioOnForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001189 }
1190
Wink Savilleb564aae2014-10-23 10:18:09 -07001191 public boolean isRadioOnForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001192 return getPhone(subId).getServiceState().getState() != ServiceState.STATE_POWER_OFF;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001193 }
1194
1195 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001196 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001197
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001198 }
Wink Saville36469e72014-06-11 15:17:00 -07001199
Wink Savilleb564aae2014-10-23 10:18:09 -07001200 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001201 enforceModifyPermission();
Wink Savilleadd7cc52014-09-08 14:23:09 -07001202 getPhone(subId).setRadioPower(!isRadioOnForSubscriber(subId));
Wink Saville36469e72014-06-11 15:17:00 -07001203 }
1204
1205 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001206 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001207 }
1208
Wink Savilleb564aae2014-10-23 10:18:09 -07001209 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07001210 enforceModifyPermission();
1211 if ((getPhone(subId).getServiceState().getState() !=
1212 ServiceState.STATE_POWER_OFF) != turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001213 toggleRadioOnOffForSubscriber(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001214 }
1215 return true;
1216 }
Wink Saville36469e72014-06-11 15:17:00 -07001217
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001218 public boolean needMobileRadioShutdown() {
1219 /*
1220 * If any of the Radios are available, it will need to be
1221 * shutdown. So return true if any Radio is available.
1222 */
1223 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1224 Phone phone = PhoneFactory.getPhone(i);
1225 if (phone != null && phone.isRadioAvailable()) return true;
1226 }
1227 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
1228 return false;
1229 }
1230
1231 public void shutdownMobileRadios() {
1232 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1233 logv("Shutting down Phone " + i);
1234 shutdownRadioUsingPhoneId(i);
1235 }
1236 }
1237
1238 private void shutdownRadioUsingPhoneId(int phoneId) {
1239 enforceModifyPermission();
1240 Phone phone = PhoneFactory.getPhone(phoneId);
1241 if (phone != null && phone.isRadioAvailable()) {
1242 phone.shutdownRadio();
1243 }
1244 }
1245
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001246 public boolean setRadioPower(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001247 return setRadioPowerForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001248 }
1249
Wink Savilleb564aae2014-10-23 10:18:09 -07001250 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001251 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001252 getPhone(subId).setRadioPower(turnOn);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001253 return true;
1254 }
1255
Wink Saville36469e72014-06-11 15:17:00 -07001256 // FIXME: subId version needed
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001257 public boolean enableDataConnectivity() {
1258 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001259 int subId = mSubscriptionController.getDefaultDataSubId();
Wink Saville36469e72014-06-11 15:17:00 -07001260 getPhone(subId).setDataEnabled(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001261 return true;
1262 }
1263
Wink Saville36469e72014-06-11 15:17:00 -07001264 // FIXME: subId version needed
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001265 public boolean disableDataConnectivity() {
1266 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001267 int subId = mSubscriptionController.getDefaultDataSubId();
Wink Saville36469e72014-06-11 15:17:00 -07001268 getPhone(subId).setDataEnabled(false);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001269 return true;
1270 }
1271
Wink Saville36469e72014-06-11 15:17:00 -07001272 // FIXME: subId version needed
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001273 public boolean isDataConnectivityPossible() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001274 int subId = mSubscriptionController.getDefaultDataSubId();
Wink Saville36469e72014-06-11 15:17:00 -07001275 return getPhone(subId).isDataConnectivityPossible();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001276 }
1277
1278 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001279 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07001280 }
1281
Wink Savilleb564aae2014-10-23 10:18:09 -07001282 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001283 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001284 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001285 }
1286
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001287 public int getCallState() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001288 return getCallStateForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001289 }
1290
Wink Savilleb564aae2014-10-23 10:18:09 -07001291 public int getCallStateForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001292 return DefaultPhoneNotifier.convertCallState(getPhone(subId).getState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001293 }
1294
1295 public int getDataState() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001296 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Wink Saville36469e72014-06-11 15:17:00 -07001297 return DefaultPhoneNotifier.convertDataState(phone.getDataConnectionState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001298 }
1299
1300 public int getDataActivity() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001301 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Wink Saville36469e72014-06-11 15:17:00 -07001302 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001303 }
1304
1305 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001306 public Bundle getCellLocation(String callingPackage) {
1307 enforceFineOrCoarseLocationPermission("getCellLocation");
1308
1309 // OP_COARSE_LOCATION controls both fine and coarse location.
1310 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1311 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001312 log("getCellLocation: returning null; mode != allowed");
Svetoslav64fad262015-04-14 14:35:21 -07001313 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001314 }
1315
Jake Hambye994d462014-02-03 13:10:13 -08001316 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001317 if (DBG_LOC) log("getCellLocation: is active user");
1318 Bundle data = new Bundle();
Legler Wu2c01cdf2014-12-08 19:00:59 +08001319 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
1320 phone.getCellLocation().fillInNotifierBundle(data);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001321 return data;
1322 } else {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001323 log("getCellLocation: suppress non-active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001324 return null;
1325 }
1326 }
1327
Svetoslav64fad262015-04-14 14:35:21 -07001328 private void enforceFineOrCoarseLocationPermission(String message) {
1329 try {
1330 mApp.enforceCallingOrSelfPermission(
1331 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1332 } catch (SecurityException e) {
1333 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1334 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1335 // is the weaker precondition
1336 mApp.enforceCallingOrSelfPermission(
1337 android.Manifest.permission.ACCESS_COARSE_LOCATION, message);
1338 }
1339 }
1340
1341
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001342 @Override
1343 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001344 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001345 }
1346
Wink Savilleb564aae2014-10-23 10:18:09 -07001347 public void enableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001348 mApp.enforceCallingOrSelfPermission(
1349 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Wink Saville36469e72014-06-11 15:17:00 -07001350 getPhone(subId).enableLocationUpdates();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001351 }
1352
1353 @Override
1354 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001355 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001356 }
1357
Wink Savilleb564aae2014-10-23 10:18:09 -07001358 public void disableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001359 mApp.enforceCallingOrSelfPermission(
1360 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Wink Saville36469e72014-06-11 15:17:00 -07001361 getPhone(subId).disableLocationUpdates();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001362 }
1363
1364 @Override
1365 @SuppressWarnings("unchecked")
1366 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
Svetoslav64fad262015-04-14 14:35:21 -07001367 enforceFineOrCoarseLocationPermission("getNeighboringCellInfo");
1368
1369 // OP_COARSE_LOCATION controls both fine and coarse location.
1370 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1371 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1372 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001373 }
1374
1375 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
1376 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1377 return null;
1378 }
Svetoslav64fad262015-04-14 14:35:21 -07001379
Jake Hambye994d462014-02-03 13:10:13 -08001380 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001381 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
1382
1383 ArrayList<NeighboringCellInfo> cells = null;
1384
1385 try {
1386 cells = (ArrayList<NeighboringCellInfo>) sendRequest(
Wink Saville36469e72014-06-11 15:17:00 -07001387 CMD_HANDLE_NEIGHBORING_CELL, null, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001388 } catch (RuntimeException e) {
Wink Saville36469e72014-06-11 15:17:00 -07001389 Log.e(LOG_TAG, "getNeighboringCellInfo " + e);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001390 }
1391 return cells;
1392 } else {
1393 if (DBG_LOC) log("getNeighboringCellInfo: suppress non-active user");
1394 return null;
1395 }
1396 }
1397
1398
1399 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001400 public List<CellInfo> getAllCellInfo(String callingPackage) {
1401 enforceFineOrCoarseLocationPermission("getAllCellInfo");
1402
1403 // OP_COARSE_LOCATION controls both fine and coarse location.
1404 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1405 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1406 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001407 }
1408
Jake Hambye994d462014-02-03 13:10:13 -08001409 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001410 if (DBG_LOC) log("getAllCellInfo: is active user");
Legler Wu2c01cdf2014-12-08 19:00:59 +08001411 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
1412 for (Phone phone : PhoneFactory.getPhones()) {
1413 cellInfos.addAll(phone.getAllCellInfo());
1414 }
1415 return cellInfos;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001416 } else {
1417 if (DBG_LOC) log("getAllCellInfo: suppress non-active user");
1418 return null;
1419 }
1420 }
1421
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001422 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001423 public void setCellInfoListRate(int rateInMillis) {
1424 mPhone.setCellInfoListRate(rateInMillis);
1425 }
1426
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001427 //
1428 // Internal helper methods.
1429 //
1430
Jake Hambye994d462014-02-03 13:10:13 -08001431 private static boolean checkIfCallerIsSelfOrForegroundUser() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001432 boolean ok;
1433
1434 boolean self = Binder.getCallingUid() == Process.myUid();
1435 if (!self) {
1436 // Get the caller's user id then clear the calling identity
1437 // which will be restored in the finally clause.
1438 int callingUser = UserHandle.getCallingUserId();
1439 long ident = Binder.clearCallingIdentity();
1440
1441 try {
1442 // With calling identity cleared the current user is the foreground user.
1443 int foregroundUser = ActivityManager.getCurrentUser();
1444 ok = (foregroundUser == callingUser);
1445 if (DBG_LOC) {
1446 log("checkIfCallerIsSelfOrForegoundUser: foregroundUser=" + foregroundUser
1447 + " callingUser=" + callingUser + " ok=" + ok);
1448 }
1449 } catch (Exception ex) {
1450 if (DBG_LOC) loge("checkIfCallerIsSelfOrForegoundUser: Exception ex=" + ex);
1451 ok = false;
1452 } finally {
1453 Binder.restoreCallingIdentity(ident);
1454 }
1455 } else {
1456 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: is self");
1457 ok = true;
1458 }
1459 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: ret=" + ok);
1460 return ok;
1461 }
1462
1463 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001464 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1465 *
1466 * @throws SecurityException if the caller does not have the required permission
1467 */
1468 private void enforceModifyPermission() {
1469 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1470 }
1471
1472 /**
Junda Liua2e36012014-07-09 18:30:01 -07001473 * Make sure either system app or the caller has carrier privilege.
1474 *
1475 * @throws SecurityException if the caller does not have the required permission/privilege
1476 */
1477 private void enforceModifyPermissionOrCarrierPrivilege() {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001478 int permission = mApp.checkCallingOrSelfPermission(
1479 android.Manifest.permission.MODIFY_PHONE_STATE);
1480 if (permission == PackageManager.PERMISSION_GRANTED) {
1481 return;
1482 }
1483
1484 log("No modify permission, check carrier privilege next.");
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08001485 if (getCarrierPrivilegeStatus() != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001486 loge("No Carrier Privilege.");
1487 throw new SecurityException("No modify permission or carrier privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001488 }
1489 }
1490
1491 /**
1492 * Make sure the caller has carrier privilege.
1493 *
1494 * @throws SecurityException if the caller does not have the required permission
1495 */
1496 private void enforceCarrierPrivilege() {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08001497 if (getCarrierPrivilegeStatus() != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001498 loge("No Carrier Privilege.");
1499 throw new SecurityException("No Carrier Privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001500 }
1501 }
1502
1503 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001504 * Make sure the caller has the CALL_PHONE permission.
1505 *
1506 * @throws SecurityException if the caller does not have the required permission
1507 */
1508 private void enforceCallPermission() {
1509 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1510 }
1511
Shishir Agrawal566b7612013-10-28 14:41:00 -07001512 /**
Gabriel Peal36ebb0d2014-03-20 09:20:43 -07001513 * Make sure the caller has the READ_PRIVILEGED_PHONE_STATE permission.
1514 *
1515 * @throws SecurityException if the caller does not have the required permission
1516 */
1517 private void enforcePrivilegedPhoneStatePermission() {
1518 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
1519 null);
1520 }
1521
Stuart Scott8eef64f2015-04-08 15:13:54 -07001522 private void enforceConnectivityInternalPermission() {
1523 mApp.enforceCallingOrSelfPermission(
1524 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1525 "ConnectivityService");
1526 }
1527
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001528 private String createTelUrl(String number) {
1529 if (TextUtils.isEmpty(number)) {
1530 return null;
1531 }
1532
Jake Hambye994d462014-02-03 13:10:13 -08001533 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001534 }
1535
Ihab Awadf9e92732013-12-05 18:02:52 -08001536 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001537 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1538 }
1539
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001540 private static void logv(String msg) {
1541 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
1542 }
1543
Ihab Awadf9e92732013-12-05 18:02:52 -08001544 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001545 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1546 }
1547
1548 public int getActivePhoneType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001549 return getActivePhoneTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001550 }
1551
Wink Savilleb564aae2014-10-23 10:18:09 -07001552 public int getActivePhoneTypeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001553 return getPhone(subId).getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001554 }
1555
1556 /**
1557 * Returns the CDMA ERI icon index to display
1558 */
1559 public int getCdmaEriIconIndex() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001560 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001561
1562 }
1563
Wink Savilleb564aae2014-10-23 10:18:09 -07001564 public int getCdmaEriIconIndexForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001565 return getPhone(subId).getCdmaEriIconIndex();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001566 }
1567
1568 /**
1569 * Returns the CDMA ERI icon mode,
1570 * 0 - ON
1571 * 1 - FLASHING
1572 */
1573 public int getCdmaEriIconMode() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001574 return getCdmaEriIconModeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001575 }
1576
Wink Savilleb564aae2014-10-23 10:18:09 -07001577 public int getCdmaEriIconModeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001578 return getPhone(subId).getCdmaEriIconMode();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001579 }
1580
1581 /**
1582 * Returns the CDMA ERI text,
1583 */
1584 public String getCdmaEriText() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001585 return getCdmaEriTextForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001586 }
1587
Wink Savilleb564aae2014-10-23 10:18:09 -07001588 public String getCdmaEriTextForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001589 return getPhone(subId).getCdmaEriText();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001590 }
1591
1592 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07001593 * Returns the CDMA MDN.
1594 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001595 public String getCdmaMdn(int subId) {
Junda Liuca05d5d2014-08-14 22:36:34 -07001596 enforceModifyPermissionOrCarrierPrivilege();
1597 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1598 return getPhone(subId).getLine1Number();
1599 } else {
1600 return null;
1601 }
1602 }
1603
1604 /**
1605 * Returns the CDMA MIN.
1606 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001607 public String getCdmaMin(int subId) {
Junda Liuca05d5d2014-08-14 22:36:34 -07001608 enforceModifyPermissionOrCarrierPrivilege();
1609 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1610 return getPhone(subId).getCdmaMin();
1611 } else {
1612 return null;
1613 }
1614 }
1615
1616 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001617 * Returns true if CDMA provisioning needs to run.
1618 */
1619 public boolean needsOtaServiceProvisioning() {
1620 return mPhone.needsOtaServiceProvisioning();
1621 }
1622
1623 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001624 * Sets the voice mail number of a given subId.
1625 */
1626 @Override
1627 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08001628 enforceCarrierPrivilege();
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001629 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
1630 new Pair<String, String>(alphaTag, number), new Integer(subId));
1631 return success;
1632 }
1633
1634 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001635 * Returns the unread count of voicemails
1636 */
1637 public int getVoiceMessageCount() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001638 return getVoiceMessageCountForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001639 }
1640
1641 /**
1642 * Returns the unread count of voicemails for a subId
1643 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001644 public int getVoiceMessageCountForSubscriber( int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001645 return getPhone(subId).getVoiceMessageCount();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001646 }
1647
1648 /**
1649 * Returns the data network type
1650 *
1651 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
1652 */
1653 @Override
1654 public int getNetworkType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001655 return getNetworkTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001656 }
1657
1658 /**
1659 * Returns the network type for a subId
1660 */
1661 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001662 public int getNetworkTypeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001663 return getPhone(subId).getServiceState().getDataNetworkType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001664 }
1665
1666 /**
1667 * Returns the data network type
1668 */
1669 @Override
1670 public int getDataNetworkType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001671 return getDataNetworkTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001672 }
1673
1674 /**
1675 * Returns the data network type for a subId
1676 */
1677 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001678 public int getDataNetworkTypeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001679 return getPhone(subId).getServiceState().getDataNetworkType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001680 }
1681
1682 /**
1683 * Returns the data network type
1684 */
1685 @Override
1686 public int getVoiceNetworkType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001687 return getVoiceNetworkTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001688 }
1689
1690 /**
1691 * Returns the Voice network type for a subId
1692 */
1693 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001694 public int getVoiceNetworkTypeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001695 return getPhone(subId).getServiceState().getVoiceNetworkType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001696 }
1697
1698 /**
1699 * @return true if a ICC card is present
1700 */
1701 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07001702 // FIXME Make changes to pass defaultSimId of type int
Amit Mahajana6fc2a82015-01-06 11:53:51 -08001703 return hasIccCardUsingSlotId(mSubscriptionController.getSlotId(getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07001704 }
1705
1706 /**
1707 * @return true if a ICC card is present for a slotId
1708 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001709 public boolean hasIccCardUsingSlotId(int slotId) {
Amit Mahajana6fc2a82015-01-06 11:53:51 -08001710 int subId[] = mSubscriptionController.getSubIdUsingSlotId(slotId);
1711 if (subId != null) {
1712 return getPhone(subId[0]).getIccCard().hasIccCard();
1713 } else {
1714 return false;
1715 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001716 }
1717
1718 /**
1719 * Return if the current radio is LTE on CDMA. This
1720 * is a tri-state return value as for a period of time
1721 * the mode may be unknown.
1722 *
1723 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08001724 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001725 */
1726 public int getLteOnCdmaMode() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001727 return getLteOnCdmaModeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001728 }
1729
Wink Savilleb564aae2014-10-23 10:18:09 -07001730 public int getLteOnCdmaModeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001731 return getPhone(subId).getLteOnCdmaMode();
1732 }
1733
1734 public void setPhone(Phone phone) {
1735 mPhone = phone;
1736 }
1737
1738 /**
1739 * {@hide}
1740 * Returns Default subId, 0 in the case of single standby.
1741 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001742 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001743 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07001744 }
1745
Wink Savilleb564aae2014-10-23 10:18:09 -07001746 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001747 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001748 }
Ihab Awadf2177b72013-11-25 13:33:23 -08001749
1750 /**
1751 * @see android.telephony.TelephonyManager.WifiCallingChoices
1752 */
1753 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08001754 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
1755 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08001756 }
1757
1758 /**
1759 * @see android.telephony.TelephonyManager.WifiCallingChoices
1760 */
1761 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08001762 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
1763 Settings.System.putInt(mPhone.getContext().getContentResolver(),
1764 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08001765 }
1766
Sailesh Nepald1e68152013-12-12 19:08:02 -08001767 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07001768 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08001769 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08001770 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08001771
Shishir Agrawal566b7612013-10-28 14:41:00 -07001772 @Override
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001773 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(String AID) {
Junda Liua2e36012014-07-09 18:30:01 -07001774 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001775
1776 if (DBG) log("iccOpenLogicalChannel: " + AID);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001777 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
1778 CMD_OPEN_CHANNEL, AID);
1779 if (DBG) log("iccOpenLogicalChannel: " + response);
1780 return response;
Shishir Agrawal566b7612013-10-28 14:41:00 -07001781 }
1782
1783 @Override
1784 public boolean iccCloseLogicalChannel(int channel) {
Junda Liua2e36012014-07-09 18:30:01 -07001785 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001786
1787 if (DBG) log("iccCloseLogicalChannel: " + channel);
1788 if (channel < 0) {
1789 return false;
1790 }
Jake Hambye994d462014-02-03 13:10:13 -08001791 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07001792 if (DBG) log("iccCloseLogicalChannel: " + success);
1793 return success;
1794 }
1795
1796 @Override
1797 public String iccTransmitApduLogicalChannel(int channel, int cla,
1798 int command, int p1, int p2, int p3, String data) {
Junda Liua2e36012014-07-09 18:30:01 -07001799 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001800
1801 if (DBG) {
1802 log("iccTransmitApduLogicalChannel: chnl=" + channel + " cla=" + cla +
1803 " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
1804 " data=" + data);
1805 }
1806
1807 if (channel < 0) {
1808 return "";
1809 }
1810
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001811 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Shishir Agrawal566b7612013-10-28 14:41:00 -07001812 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data));
1813 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
1814
Shishir Agrawal566b7612013-10-28 14:41:00 -07001815 // Append the returned status code to the end of the response payload.
1816 String s = Integer.toHexString(
1817 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07001818 if (response.payload != null) {
1819 s = IccUtils.bytesToHexString(response.payload) + s;
1820 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07001821 return s;
1822 }
Jake Hambye994d462014-02-03 13:10:13 -08001823
Evan Charltonc66da362014-05-16 14:06:40 -07001824 @Override
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001825 public String iccTransmitApduBasicChannel(int cla, int command, int p1, int p2,
1826 int p3, String data) {
1827 enforceModifyPermissionOrCarrierPrivilege();
1828
1829 if (DBG) {
1830 log("iccTransmitApduBasicChannel: cla=" + cla + " cmd=" + command + " p1="
1831 + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
1832 }
1833
1834 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
1835 new IccAPDUArgument(0, cla, command, p1, p2, p3, data));
1836 if (DBG) log("iccTransmitApduBasicChannel: " + response);
1837
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001838 // Append the returned status code to the end of the response payload.
1839 String s = Integer.toHexString(
1840 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07001841 if (response.payload != null) {
1842 s = IccUtils.bytesToHexString(response.payload) + s;
1843 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001844 return s;
1845 }
1846
1847 @Override
1848 public byte[] iccExchangeSimIO(int fileID, int command, int p1, int p2, int p3,
1849 String filePath) {
1850 enforceModifyPermissionOrCarrierPrivilege();
1851
1852 if (DBG) {
1853 log("Exchange SIM_IO " + fileID + ":" + command + " " +
1854 p1 + " " + p2 + " " + p3 + ":" + filePath);
1855 }
1856
1857 IccIoResult response =
1858 (IccIoResult)sendRequest(CMD_EXCHANGE_SIM_IO,
Yong Jiang3edf3782014-10-03 13:23:28 -05001859 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath));
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001860
1861 if (DBG) {
1862 log("Exchange SIM_IO [R]" + response);
1863 }
1864
1865 byte[] result = null;
1866 int length = 2;
1867 if (response.payload != null) {
1868 length = 2 + response.payload.length;
1869 result = new byte[length];
1870 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
1871 } else {
1872 result = new byte[length];
1873 }
1874
1875 result[length - 1] = (byte) response.sw2;
1876 result[length - 2] = (byte) response.sw1;
1877 return result;
1878 }
1879
1880 @Override
Evan Charltonc66da362014-05-16 14:06:40 -07001881 public String sendEnvelopeWithStatus(String content) {
Junda Liua2e36012014-07-09 18:30:01 -07001882 enforceModifyPermissionOrCarrierPrivilege();
Evan Charltonc66da362014-05-16 14:06:40 -07001883
1884 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content);
1885 if (response.payload == null) {
1886 return "";
1887 }
1888
1889 // Append the returned status code to the end of the response payload.
1890 String s = Integer.toHexString(
1891 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
1892 s = IccUtils.bytesToHexString(response.payload) + s;
1893 return s;
1894 }
1895
Jake Hambye994d462014-02-03 13:10:13 -08001896 /**
1897 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
1898 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
1899 *
1900 * @param itemID the ID of the item to read
1901 * @return the NV item as a String, or null on error.
1902 */
1903 @Override
1904 public String nvReadItem(int itemID) {
Junda Liua2e36012014-07-09 18:30:01 -07001905 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001906 if (DBG) log("nvReadItem: item " + itemID);
1907 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
1908 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
1909 return value;
1910 }
1911
1912 /**
1913 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
1914 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
1915 *
1916 * @param itemID the ID of the item to read
1917 * @param itemValue the value to write, as a String
1918 * @return true on success; false on any failure
1919 */
1920 @Override
1921 public boolean nvWriteItem(int itemID, String itemValue) {
Junda Liua2e36012014-07-09 18:30:01 -07001922 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001923 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
1924 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
1925 new Pair<Integer, String>(itemID, itemValue));
1926 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
1927 return success;
1928 }
1929
1930 /**
1931 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
1932 * Used for device configuration by some CDMA operators.
1933 *
1934 * @param preferredRoamingList byte array containing the new PRL
1935 * @return true on success; false on any failure
1936 */
1937 @Override
1938 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Junda Liua2e36012014-07-09 18:30:01 -07001939 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001940 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
1941 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
1942 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
1943 return success;
1944 }
1945
1946 /**
1947 * Perform the specified type of NV config reset.
1948 * Used for device configuration by some CDMA operators.
1949 *
1950 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
1951 * @return true on success; false on any failure
1952 */
1953 @Override
1954 public boolean nvResetConfig(int resetType) {
Junda Liua2e36012014-07-09 18:30:01 -07001955 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001956 if (DBG) log("nvResetConfig: type " + resetType);
1957 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
1958 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
1959 return success;
1960 }
Jake Hamby7c27be32014-03-03 13:25:59 -08001961
1962 /**
Wink Saville36469e72014-06-11 15:17:00 -07001963 * {@hide}
1964 * Returns Default sim, 0 in the case of single standby.
1965 */
1966 public int getDefaultSim() {
1967 //TODO Need to get it from Telephony Devcontroller
1968 return 0;
1969 }
1970
Svet Ganovb320e182015-04-16 12:30:10 -07001971 public String[] getPcscfAddress(String apnType, String callingPackage) {
1972 if (!canReadPhoneState(callingPackage, "getPcscfAddress")) {
1973 return new String[0];
1974 }
1975
1976
ram87fca6f2014-07-18 18:58:44 +05301977 return mPhone.getPcscfAddress(apnType);
Wink Saville36469e72014-06-11 15:17:00 -07001978 }
1979
1980 public void setImsRegistrationState(boolean registered) {
1981 enforceModifyPermission();
1982 mPhone.setImsRegistrationState(registered);
1983 }
1984
1985 /**
Stuart Scott54788802015-03-30 13:18:01 -07001986 * Set the network selection mode to automatic.
1987 *
1988 */
1989 @Override
1990 public void setNetworkSelectionModeAutomatic(int subId) {
1991 enforceModifyPermissionOrCarrierPrivilege();
1992 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
1993 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
1994 }
1995
1996 /**
Shishir Agrawal302c8692015-06-19 13:49:39 -07001997 * Set the network selection mode to manual with the selected carrier.
1998 */
1999 @Override
2000 public boolean setNetworkSelectionModeManual(int subId, OperatorInfo operator) {
2001 enforceModifyPermissionOrCarrierPrivilege();
2002 if (DBG) log("setNetworkSelectionModeManual: subId:" + subId + " operator:" + operator);
2003 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, operator, subId);
2004 }
2005
2006 /**
2007 * Scans for available networks.
2008 */
2009 @Override
2010 public CellNetworkScanResult getCellNetworkScanResults(int subId) {
2011 enforceModifyPermissionOrCarrierPrivilege();
2012 if (DBG) log("getCellNetworkScanResults: subId " + subId);
2013 CellNetworkScanResult result = (CellNetworkScanResult) sendRequest(
2014 CMD_PERFORM_NETWORK_SCAN, null, subId);
2015 return result;
2016 }
2017
2018 /**
Junda Liu84d15a22014-07-02 11:21:04 -07002019 * Get the calculated preferred network type.
2020 * Used for debugging incorrect network type.
2021 *
2022 * @return the preferred network type, defined in RILConstants.java.
2023 */
2024 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002025 public int getCalculatedPreferredNetworkType(String callingPackage) {
2026 if (!canReadPhoneState(callingPackage, "getCalculatedPreferredNetworkType")) {
2027 return RILConstants.PREFERRED_NETWORK_MODE;
2028 }
2029
Amit Mahajan43330e02014-11-18 11:54:45 -08002030 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext(), 0); // wink FIXME: need to get SubId from somewhere.
Junda Liu84d15a22014-07-02 11:21:04 -07002031 }
2032
2033 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08002034 * Get the preferred network type.
2035 * Used for device configuration by some CDMA operators.
2036 *
2037 * @return the preferred network type, defined in RILConstants.java.
2038 */
2039 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002040 public int getPreferredNetworkType(int subId) {
Junda Liua2e36012014-07-09 18:30:01 -07002041 enforceModifyPermissionOrCarrierPrivilege();
Jake Hamby7c27be32014-03-03 13:25:59 -08002042 if (DBG) log("getPreferredNetworkType");
Stuart Scott54788802015-03-30 13:18:01 -07002043 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002044 int networkType = (result != null ? result[0] : -1);
2045 if (DBG) log("getPreferredNetworkType: " + networkType);
2046 return networkType;
2047 }
2048
2049 /**
2050 * Set the preferred network type.
2051 * Used for device configuration by some CDMA operators.
2052 *
2053 * @param networkType the preferred network type, defined in RILConstants.java.
2054 * @return true on success; false on any failure.
2055 */
2056 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002057 public boolean setPreferredNetworkType(int subId, int networkType) {
Junda Liua2e36012014-07-09 18:30:01 -07002058 enforceModifyPermissionOrCarrierPrivilege();
Stuart Scott54788802015-03-30 13:18:01 -07002059 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
2060 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002061 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
Junda Liu80bc0d12014-07-14 16:36:44 -07002062 if (success) {
2063 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
Stuart Scott54788802015-03-30 13:18:01 -07002064 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
Junda Liu80bc0d12014-07-14 16:36:44 -07002065 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002066 return success;
2067 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002068
2069 /**
Junda Liu475951f2014-11-07 16:45:03 -08002070 * Check TETHER_DUN_REQUIRED and TETHER_DUN_APN settings, net.tethering.noprovisioning
2071 * SystemProperty, and config_tether_apndata to decide whether DUN APN is required for
2072 * tethering.
2073 *
2074 * @return 0: Not required. 1: required. 2: Not set.
2075 * @hide
2076 */
2077 @Override
2078 public int getTetherApnRequired() {
2079 enforceModifyPermissionOrCarrierPrivilege();
2080 int dunRequired = Settings.Global.getInt(mPhone.getContext().getContentResolver(),
2081 Settings.Global.TETHER_DUN_REQUIRED, 2);
2082 // If not set, check net.tethering.noprovisioning, TETHER_DUN_APN setting and
2083 // config_tether_apndata.
2084 if (dunRequired == 2 && mPhone.hasMatchedTetherApnSetting()) {
2085 dunRequired = 1;
2086 }
2087 return dunRequired;
2088 }
2089
2090 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07002091 * Set mobile data enabled
2092 * Used by the user through settings etc to turn on/off mobile data
2093 *
2094 * @param enable {@code true} turn turn data on, else {@code false}
2095 */
2096 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002097 public void setDataEnabled(int subId, boolean enable) {
Robert Greenwalted86e582014-05-21 20:03:20 -07002098 enforceModifyPermission();
Wink Savillee7353bb2014-12-05 14:21:41 -08002099 int phoneId = mSubscriptionController.getPhoneId(subId);
2100 log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
2101 Phone phone = PhoneFactory.getPhone(phoneId);
2102 if (phone != null) {
2103 log("setDataEnabled: subId=" + subId + " enable=" + enable);
2104 phone.setDataEnabled(enable);
2105 } else {
2106 loge("setDataEnabled: no phone for subId=" + subId);
2107 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002108 }
2109
2110 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07002111 * Get whether mobile data is enabled.
2112 *
2113 * Note that this used to be available from ConnectivityService, gated by
2114 * ACCESS_NETWORK_STATE permission, so this will accept either that or
2115 * our MODIFY_PHONE_STATE.
Robert Greenwalted86e582014-05-21 20:03:20 -07002116 *
2117 * @return {@code true} if data is enabled else {@code false}
2118 */
2119 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002120 public boolean getDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07002121 try {
2122 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
2123 null);
2124 } catch (Exception e) {
2125 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE,
2126 null);
2127 }
Wink Savillee7353bb2014-12-05 14:21:41 -08002128 int phoneId = mSubscriptionController.getPhoneId(subId);
2129 log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
2130 Phone phone = PhoneFactory.getPhone(phoneId);
2131 if (phone != null) {
2132 boolean retVal = phone.getDataEnabled();
2133 log("getDataEnabled: subId=" + subId + " retVal=" + retVal);
2134 return retVal;
2135 } else {
2136 loge("getDataEnabled: no phone subId=" + subId + " retVal=false");
2137 return false;
2138 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002139 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002140
2141 @Override
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002142 public int getCarrierPrivilegeStatus() {
Shishir Agrawal21409252015-01-15 23:33:50 -08002143 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002144 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002145 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002146 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2147 }
2148 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07002149 mPhone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002150 }
Junda Liu29340342014-07-10 15:23:27 -07002151
2152 @Override
Zach Johnson50ecba32015-05-19 00:24:21 -07002153 public int checkCarrierPrivilegesForPackage(String pkgName) {
Shishir Agrawal21409252015-01-15 23:33:50 -08002154 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002155 if (card == null) {
2156 loge("checkCarrierPrivilegesForPackage: No UICC");
2157 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2158 }
Zach Johnson50ecba32015-05-19 00:24:21 -07002159 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgName);
2160 }
2161
2162 @Override
2163 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
2164 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2165 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2166 UiccCard card = UiccController.getInstance().getUiccCard(i);
2167 if (card == null) {
Jonathan Basseri7d320df2015-06-16 12:17:08 -07002168 // No UICC in that slot.
Zach Johnson50ecba32015-05-19 00:24:21 -07002169 continue;
2170 }
2171
2172 result = card.getCarrierPrivilegeStatus(
2173 mPhone.getContext().getPackageManager(), pkgName);
2174 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2175 break;
2176 }
2177 }
2178
2179 return result;
Junda Liu29340342014-07-10 15:23:27 -07002180 }
Derek Tan89e89d42014-07-08 17:00:10 -07002181
2182 @Override
Junda Liue64de782015-04-16 17:19:16 -07002183 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
2184 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
2185 loge("phoneId " + phoneId + " is not valid.");
2186 return null;
2187 }
2188 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002189 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002190 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002191 return null ;
2192 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002193 return card.getCarrierPackageNamesForIntent(
Svetoslav483aff72015-04-21 14:16:07 -07002194 mPhone.getContext().getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002195 }
2196
Wink Savilleb564aae2014-10-23 10:18:09 -07002197 private String getIccId(int subId) {
Derek Tan97ebb422014-09-05 16:55:38 -07002198 UiccCard card = getPhone(subId).getUiccCard();
2199 if (card == null) {
2200 loge("getIccId: No UICC");
2201 return null;
2202 }
2203 String iccId = card.getIccId();
2204 if (TextUtils.isEmpty(iccId)) {
2205 loge("getIccId: ICC ID is null or empty.");
2206 return null;
2207 }
2208 return iccId;
2209 }
2210
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002211 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002212 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
2213 String number) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002214 enforceCarrierPrivilege();
Derek Tan97ebb422014-09-05 16:55:38 -07002215
Jeff Sharkey85190e62014-12-05 09:40:12 -08002216 final String iccId = getIccId(subId);
2217 final String subscriberId = getPhone(subId).getSubscriberId();
2218
2219 if (DBG_MERGE) {
2220 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
2221 + subscriberId + " to " + number);
2222 }
2223
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002224 if (TextUtils.isEmpty(iccId)) {
2225 return false;
Derek Tan97ebb422014-09-05 16:55:38 -07002226 }
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002227
Jeff Sharkey85190e62014-12-05 09:40:12 -08002228 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2229
2230 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002231 if (alphaTag == null) {
2232 editor.remove(alphaTagPrefKey);
2233 } else {
2234 editor.putString(alphaTagPrefKey, alphaTag);
2235 }
2236
Jeff Sharkey85190e62014-12-05 09:40:12 -08002237 // Record both the line number and IMSI for this ICCID, since we need to
2238 // track all merged IMSIs based on line number
2239 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2240 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002241 if (number == null) {
2242 editor.remove(numberPrefKey);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002243 editor.remove(subscriberPrefKey);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002244 } else {
2245 editor.putString(numberPrefKey, number);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002246 editor.putString(subscriberPrefKey, subscriberId);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002247 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002248
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002249 editor.commit();
2250 return true;
Derek Tan7226c842014-07-02 17:42:23 -07002251 }
2252
2253 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002254 public String getLine1NumberForDisplay(int subId, String callingPackage) {
2255 if (!canReadPhoneState(callingPackage, "getLine1NumberForDisplay")) {
2256 return null;
2257 }
Derek Tan97ebb422014-09-05 16:55:38 -07002258
2259 String iccId = getIccId(subId);
2260 if (iccId != null) {
2261 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002262 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002263 }
Derek Tan97ebb422014-09-05 16:55:38 -07002264 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002265 }
2266
2267 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002268 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
2269 if (!canReadPhoneState(callingPackage, "getLine1AlphaTagForDisplay")) {
2270 return null;
2271 }
Derek Tan97ebb422014-09-05 16:55:38 -07002272
2273 String iccId = getIccId(subId);
2274 if (iccId != null) {
2275 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002276 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002277 }
Derek Tan97ebb422014-09-05 16:55:38 -07002278 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002279 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002280
2281 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002282 public String[] getMergedSubscriberIds() {
2283 final Context context = mPhone.getContext();
2284 final TelephonyManager tele = TelephonyManager.from(context);
2285 final SubscriptionManager sub = SubscriptionManager.from(context);
2286
2287 // Figure out what subscribers are currently active
2288 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
2289 final int[] subIds = sub.getActiveSubscriptionIdList();
2290 for (int subId : subIds) {
2291 activeSubscriberIds.add(tele.getSubscriberId(subId));
2292 }
2293
2294 // First pass, find a number override for an active subscriber
2295 String mergeNumber = null;
2296 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
2297 for (String key : prefs.keySet()) {
2298 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
2299 final String subscriberId = (String) prefs.get(key);
2300 if (activeSubscriberIds.contains(subscriberId)) {
2301 final String iccId = key.substring(PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
2302 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2303 mergeNumber = (String) prefs.get(numberKey);
2304 if (DBG_MERGE) {
2305 Slog.d(LOG_TAG, "Found line number " + mergeNumber
2306 + " for active subscriber " + subscriberId);
2307 }
2308 if (!TextUtils.isEmpty(mergeNumber)) {
2309 break;
2310 }
2311 }
2312 }
2313 }
2314
2315 // Shortcut when no active merged subscribers
2316 if (TextUtils.isEmpty(mergeNumber)) {
2317 return null;
2318 }
2319
2320 // Second pass, find all subscribers under that line override
2321 final ArraySet<String> result = new ArraySet<>();
2322 for (String key : prefs.keySet()) {
2323 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
2324 final String number = (String) prefs.get(key);
2325 if (mergeNumber.equals(number)) {
2326 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
2327 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
2328 final String subscriberId = (String) prefs.get(subscriberKey);
2329 if (!TextUtils.isEmpty(subscriberId)) {
2330 result.add(subscriberId);
2331 }
2332 }
2333 }
2334 }
2335
2336 final String[] resultArray = result.toArray(new String[result.size()]);
2337 Arrays.sort(resultArray);
2338 if (DBG_MERGE) {
2339 Slog.d(LOG_TAG, "Found subscribers " + Arrays.toString(resultArray) + " after merge");
2340 }
2341 return resultArray;
2342 }
2343
2344 @Override
Shishir Agrawala3dfd752014-09-04 13:25:42 -07002345 public boolean setOperatorBrandOverride(String brand) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002346 enforceCarrierPrivilege();
Shishir Agrawala3dfd752014-09-04 13:25:42 -07002347 return mPhone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002348 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05002349
2350 @Override
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002351 public boolean setRoamingOverride(List<String> gsmRoamingList,
2352 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
2353 List<String> cdmaNonRoamingList) {
2354 enforceCarrierPrivilege();
2355 return mPhone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
2356 cdmaNonRoamingList);
2357 }
2358
2359 @Override
Steven Liu4bf01bc2014-07-17 11:05:29 -05002360 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
2361 enforceModifyPermission();
2362
2363 int returnValue = 0;
2364 try {
2365 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
2366 if(result.exception == null) {
2367 if (result.result != null) {
2368 byte[] responseData = (byte[])(result.result);
2369 if(responseData.length > oemResp.length) {
2370 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
2371 responseData.length + "bytes. Buffer Size is " +
2372 oemResp.length + "bytes.");
2373 }
2374 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
2375 returnValue = responseData.length;
2376 }
2377 } else {
2378 CommandException ex = (CommandException) result.exception;
2379 returnValue = ex.getCommandError().ordinal();
2380 if(returnValue > 0) returnValue *= -1;
2381 }
2382 } catch (RuntimeException e) {
2383 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
2384 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
2385 if(returnValue > 0) returnValue *= -1;
2386 }
2387
2388 return returnValue;
2389 }
Wink Saville5d475dd2014-10-17 15:00:58 -07002390
2391 @Override
2392 public void setRadioCapability(RadioAccessFamily[] rafs) {
2393 try {
2394 ProxyController.getInstance().setRadioCapability(rafs);
2395 } catch (RuntimeException e) {
2396 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
2397 }
2398 }
2399
2400 @Override
2401 public int getRadioAccessFamily(int phoneId) {
2402 return ProxyController.getInstance().getRadioAccessFamily(phoneId);
2403 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002404
2405 @Override
2406 public void enableVideoCalling(boolean enable) {
2407 enforceModifyPermission();
2408 SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2409 editor.putBoolean(PREF_ENABLE_VIDEO_CALLING, enable);
2410 editor.commit();
2411 }
2412
2413 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002414 public boolean isVideoCallingEnabled(String callingPackage) {
2415 if (!canReadPhoneState(callingPackage, "isVideoCallingEnabled")) {
2416 return false;
2417 }
2418
Andrew Lee77527ac2014-10-21 16:57:39 -07002419 // Check the user preference and the system-level IMS setting. Even if the user has
2420 // enabled video calling, if IMS is disabled we aren't able to support video calling.
2421 // In the long run, we may instead need to check if there exists a connection service
2422 // which can support video calling.
Andrew Lee312e8172014-10-23 17:01:36 -07002423 return ImsManager.isVtEnabledByPlatform(mPhone.getContext())
2424 && ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
2425 && mTelephonySharedPreferences.getBoolean(PREF_ENABLE_VIDEO_CALLING, true);
Andrew Leedf14ead2014-10-17 14:22:52 -07002426 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002427
Andrew Leea1239f22015-03-02 17:44:07 -08002428 @Override
2429 public boolean canChangeDtmfToneLength() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07002430 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08002431 }
2432
2433 @Override
2434 public boolean isWorldPhone() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07002435 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08002436 }
2437
Andrew Lee9431b832015-03-09 18:46:45 -07002438 @Override
2439 public boolean isTtyModeSupported() {
2440 TelecomManager telecomManager = TelecomManager.from(mPhone.getContext());
2441 TelephonyManager telephonyManager =
2442 (TelephonyManager) mPhone.getContext().getSystemService(Context.TELEPHONY_SERVICE);
2443 return !telephonyManager.isMultiSimEnabled() && telecomManager.isTtySupported();
2444 }
2445
2446 @Override
2447 public boolean isHearingAidCompatibilitySupported() {
2448 return mPhone.getContext().getResources().getBoolean(R.bool.hac_enabled);
2449 }
2450
Sanket Padawe7310cc72015-01-14 09:53:20 -08002451 /**
2452 * Returns the unique device ID of phone, for example, the IMEI for
2453 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
2454 *
2455 * <p>Requires Permission:
2456 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
2457 */
2458 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002459 public String getDeviceId(String callingPackage) {
2460 if (!canReadPhoneState(callingPackage, "getDeviceId")) {
2461 return null;
2462 }
2463
Sanket Padawe7310cc72015-01-14 09:53:20 -08002464 final Phone phone = PhoneFactory.getPhone(0);
2465 if (phone != null) {
2466 return phone.getDeviceId();
2467 } else {
2468 return null;
2469 }
2470 }
2471
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002472 /*
2473 * {@hide}
2474 * Returns the IMS Registration Status
2475 */
Santos Cordon7a1885b2015-02-03 11:15:19 -08002476 @Override
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002477 public boolean isImsRegistered() {
2478 return mPhone.isImsRegistered();
2479 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08002480
2481 @Override
2482 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
2483 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
2484 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07002485
Nathan Haroldc55097a2015-03-11 18:14:50 -07002486 /*
2487 * {@hide}
2488 * Returns the IMS Registration Status
2489 */
2490 public boolean isWifiCallingEnabled() {
2491 return mPhone.isWifiCallingEnabled();
2492 }
2493
2494 /*
2495 * {@hide}
2496 * Returns the IMS Registration Status
2497 */
2498 public boolean isVolteEnabled() {
2499 return mPhone.isVolteEnabled();
2500 }
Svet Ganovb320e182015-04-16 12:30:10 -07002501
2502 private boolean canReadPhoneState(String callingPackage, String message) {
2503 mApp.enforceCallingOrSelfPermission(
2504 android.Manifest.permission.READ_PHONE_STATE, message);
2505
2506 if (mAppOps.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(),
2507 callingPackage) != AppOpsManager.MODE_ALLOWED) {
2508 return false;
2509 }
2510
2511 return true;
2512 }
Stuart Scott8eef64f2015-04-08 15:13:54 -07002513
2514 @Override
2515 public void factoryReset(int subId) {
2516 enforceConnectivityInternalPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07002517 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
2518 return;
2519 }
2520
Svet Ganovcc087f82015-05-12 20:35:54 -07002521 final long identity = Binder.clearCallingIdentity();
2522 try {
Stuart Scott981d8582015-04-21 14:09:50 -07002523 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
2524 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Svet Ganovcc087f82015-05-12 20:35:54 -07002525 // Enable data
2526 setDataEnabled(subId, true);
2527 // Set network selection mode to automatic
2528 setNetworkSelectionModeAutomatic(subId);
2529 // Set preferred mobile network type to the best available
2530 setPreferredNetworkType(subId, Phone.PREFERRED_NT_MODE);
2531 // Turn off roaming
2532 SubscriptionManager.from(mApp).setDataRoaming(0, subId);
2533 }
2534 } finally {
2535 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07002536 }
2537 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01002538
2539 @Override
2540 public String getLocaleFromDefaultSim() {
2541 // We query all subscriptions instead of just the active ones, because
2542 // this might be called early on in the provisioning flow when the
2543 // subscriptions potentially aren't active yet.
2544 final List<SubscriptionInfo> slist = getAllSubscriptionInfoList();
2545 if (slist == null || slist.isEmpty()) {
2546 return null;
2547 }
2548
2549 // This function may be called very early, say, from the setup wizard, at
2550 // which point we won't have a default subscription set. If that's the case
2551 // we just choose the first, which will be valid in "most cases".
2552 final int defaultSubId = getDefaultSubscription();
2553 SubscriptionInfo info = null;
2554 if (defaultSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
2555 info = slist.get(0);
2556 } else {
2557 for (SubscriptionInfo item : slist) {
2558 if (item.getSubscriptionId() == defaultSubId) {
2559 info = item;
2560 break;
2561 }
2562 }
2563
2564 if (info == null) {
2565 return null;
2566 }
2567 }
2568
2569 // Try and fetch the locale from the carrier properties or from the SIM language
2570 // preferences (EF-PL and EF-LI)...
2571 final Phone defaultPhone = getPhone(info.getSubscriptionId());
2572 if (defaultPhone != null) {
2573 final Locale localeFromDefaultSim = defaultPhone.getLocaleFromSimAndCarrierPrefs();
2574 if (localeFromDefaultSim != null) {
2575 return localeFromDefaultSim.toLanguageTag();
2576 }
2577 }
2578
2579 // .. if that doesn't work, try and guess the language from the sim MCC.
2580 final int mcc = info.getMcc();
2581 final Locale locale = MccTable.getLocaleFromMcc(mPhone.getContext(), mcc);
2582 if (locale != null) {
2583 return locale.toLanguageTag();
2584 }
2585
2586 return null;
2587 }
2588
2589 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
2590 final long identity = Binder.clearCallingIdentity();
2591 try {
2592 return mSubscriptionController.getAllSubInfoList(
2593 mPhone.getContext().getOpPackageName());
2594 } finally {
2595 Binder.restoreCallingIdentity(identity);
2596 }
2597 }
2598
2599 private List<SubscriptionInfo> getActiveSubscriptionInfoList() {
2600 final long identity = Binder.clearCallingIdentity();
2601 try {
2602 return mSubscriptionController.getActiveSubscriptionInfoList(
2603 mPhone.getContext().getOpPackageName());
2604 } finally {
2605 Binder.restoreCallingIdentity(identity);
2606 }
2607 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07002608
2609 /**
2610 * {@hide}
2611 * Returns the modem stats
2612 */
2613 @Override
2614 public ModemActivityInfo getModemActivityInfo() {
2615 return (ModemActivityInfo) sendRequest(CMD_GET_MODEM_ACTIVITY_INFO, null);
2616 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002617}