blob: b15e024fa28ed2ab1a43bef5f535bb942977cdb3 [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;
Amith Yamasani6e118872016-02-19 12:53:51 -080025import android.content.pm.PackageInfo;
Shishir Agrawal60f9c952014-06-23 12:00:43 -070026import android.content.pm.PackageManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070027import android.net.Uri;
28import android.os.AsyncResult;
29import android.os.Binder;
30import android.os.Bundle;
31import android.os.Handler;
32import android.os.Looper;
33import android.os.Message;
34import android.os.Process;
35import android.os.ServiceManager;
36import android.os.UserHandle;
Stuart Scott981d8582015-04-21 14:09:50 -070037import android.os.UserManager;
Derek Tan97ebb422014-09-05 16:55:38 -070038import android.preference.PreferenceManager;
Ihab Awadf2177b72013-11-25 13:33:23 -080039import android.provider.Settings;
Santos Cordon7a1885b2015-02-03 11:15:19 -080040import android.telecom.PhoneAccount;
Nancy Chen31f9ba12016-01-06 11:42:12 -080041import android.telecom.PhoneAccountHandle;
Andrew Lee9431b832015-03-09 18:46:45 -070042import android.telecom.TelecomManager;
Junda Liu12f7d802015-05-01 12:06:44 -070043import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070044import android.telephony.CellInfo;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -070045import android.telephony.IccOpenLogicalChannelResponse;
Jake Hambye994d462014-02-03 13:10:13 -080046import android.telephony.NeighboringCellInfo;
Wink Saville5d475dd2014-10-17 15:00:58 -070047import android.telephony.RadioAccessFamily;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070048import android.telephony.ServiceState;
Wink Saville0f3b5fc2014-11-11 08:40:49 -080049import android.telephony.SubscriptionInfo;
Jeff Sharkey85190e62014-12-05 09:40:12 -080050import android.telephony.SubscriptionManager;
Ihab Awadf2177b72013-11-25 13:33:23 -080051import android.telephony.TelephonyManager;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -070052import android.telephony.ModemActivityInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070053import android.text.TextUtils;
Jeff Sharkey85190e62014-12-05 09:40:12 -080054import android.util.ArrayMap;
55import android.util.ArraySet;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070056import android.util.Log;
Jake Hambye994d462014-02-03 13:10:13 -080057import android.util.Pair;
Jeff Sharkey85190e62014-12-05 09:40:12 -080058import android.util.Slog;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070059
Andrew Lee312e8172014-10-23 17:01:36 -070060import com.android.ims.ImsManager;
Shishir Agrawal566b7612013-10-28 14:41:00 -070061import com.android.internal.telephony.CallManager;
Shishir Agrawal302c8692015-06-19 13:49:39 -070062import com.android.internal.telephony.CellNetworkScanResult;
Shishir Agrawal566b7612013-10-28 14:41:00 -070063import com.android.internal.telephony.CommandException;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070064import com.android.internal.telephony.DefaultPhoneNotifier;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070065import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -080066import com.android.internal.telephony.IccCard;
Narayan Kamath1c496c22015-04-16 14:40:19 +010067import com.android.internal.telephony.MccTable;
Shishir Agrawal302c8692015-06-19 13:49:39 -070068import com.android.internal.telephony.OperatorInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070069import com.android.internal.telephony.Phone;
Wink Saville36469e72014-06-11 15:17:00 -070070import com.android.internal.telephony.PhoneFactory;
Wink Saville5d475dd2014-10-17 15:00:58 -070071import com.android.internal.telephony.ProxyController;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070072import com.android.internal.telephony.PhoneConstants;
Svet Ganovb320e182015-04-16 12:30:10 -070073import com.android.internal.telephony.RILConstants;
Wink Savilleac1bdfd2014-11-20 23:04:44 -080074import com.android.internal.telephony.SubscriptionController;
Shishir Agrawal566b7612013-10-28 14:41:00 -070075import com.android.internal.telephony.uicc.IccIoResult;
76import com.android.internal.telephony.uicc.IccUtils;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -070077import com.android.internal.telephony.uicc.UiccCard;
Shishir Agrawal566b7612013-10-28 14:41:00 -070078import com.android.internal.telephony.uicc.UiccController;
Jake Hambye994d462014-02-03 13:10:13 -080079import com.android.internal.util.HexDump;
Nancy Chen31f9ba12016-01-06 11:42:12 -080080import com.android.phone.settings.VoicemailNotificationSettingsUtil;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070081
Wink Saville36469e72014-06-11 15:17:00 -070082import static com.android.internal.telephony.PhoneConstants.SUBSCRIPTION_KEY;
83
Santos Cordon7d4ddf62013-07-10 11:58:08 -070084import java.util.ArrayList;
Jeff Sharkey85190e62014-12-05 09:40:12 -080085import java.util.Arrays;
Jake Hambye994d462014-02-03 13:10:13 -080086import java.util.List;
Narayan Kamath1c496c22015-04-16 14:40:19 +010087import java.util.Locale;
Jeff Sharkey85190e62014-12-05 09:40:12 -080088import java.util.Map;
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_";
Derek Tan89e89d42014-07-08 17:00:10 -0700157
158 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700159 * A request object to use for transmitting data to an ICC.
160 */
161 private static final class IccAPDUArgument {
162 public int channel, cla, command, p1, p2, p3;
163 public String data;
164
165 public IccAPDUArgument(int channel, int cla, int command,
166 int p1, int p2, int p3, String data) {
167 this.channel = channel;
168 this.cla = cla;
169 this.command = command;
170 this.p1 = p1;
171 this.p2 = p2;
172 this.p3 = p3;
173 this.data = data;
174 }
175 }
176
177 /**
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700178 * A request object to use for transmitting data to an ICC.
179 */
180 private static final class ManualNetworkSelectionArgument {
181 public OperatorInfo operatorInfo;
182 public boolean persistSelection;
183
184 public ManualNetworkSelectionArgument(OperatorInfo operatorInfo, boolean persistSelection) {
185 this.operatorInfo = operatorInfo;
186 this.persistSelection = persistSelection;
187 }
188 }
189
190 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700191 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
192 * request after sending. The main thread will notify the request when it is complete.
193 */
194 private static final class MainThreadRequest {
195 /** The argument to use for the request */
196 public Object argument;
197 /** The result of the request that is run on the main thread */
198 public Object result;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800199 // The subscriber id that this request applies to. Defaults to
200 // SubscriptionManager.INVALID_SUBSCRIPTION_ID
201 public Integer subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700202
203 public MainThreadRequest(Object argument) {
204 this.argument = argument;
205 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800206
207 public MainThreadRequest(Object argument, Integer subId) {
208 this.argument = argument;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800209 if (subId != null) {
210 this.subId = subId;
211 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800212 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700213 }
214
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800215 private static final class IncomingThirdPartyCallArgs {
216 public final ComponentName component;
217 public final String callId;
218 public final String callerDisplayName;
219
220 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
221 String callerDisplayName) {
222 this.component = component;
223 this.callId = callId;
224 this.callerDisplayName = callerDisplayName;
225 }
226 }
227
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700228 /**
229 * A handler that processes messages on the main thread in the phone process. Since many
230 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
231 * inbound binder threads to the main thread in the phone process. The Binder thread
232 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
233 * on, which will be notified when the operation completes and will contain the result of the
234 * request.
235 *
236 * <p>If a MainThreadRequest object is provided in the msg.obj field,
237 * note that request.result must be set to something non-null for the calling thread to
238 * unblock.
239 */
240 private final class MainThreadHandler extends Handler {
241 @Override
242 public void handleMessage(Message msg) {
243 MainThreadRequest request;
244 Message onCompleted;
245 AsyncResult ar;
Shishir Agrawal21409252015-01-15 23:33:50 -0800246 UiccCard uiccCard = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700247 IccAPDUArgument iccArgument;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700248
249 switch (msg.what) {
Yorke Lee716f67e2015-06-17 15:39:16 -0700250 case CMD_HANDLE_PIN_MMI: {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700251 request = (MainThreadRequest) msg.obj;
Yorke Lee716f67e2015-06-17 15:39:16 -0700252 final Phone phone = getPhoneFromRequest(request);
253 request.result = phone != null ?
254 getPhoneFromRequest(request).handlePinMmi((String) request.argument)
255 : false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700256 // Wake up the requesting thread
257 synchronized (request) {
258 request.notifyAll();
259 }
260 break;
Yorke Lee716f67e2015-06-17 15:39:16 -0700261 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700262
263 case CMD_HANDLE_NEIGHBORING_CELL:
264 request = (MainThreadRequest) msg.obj;
265 onCompleted = obtainMessage(EVENT_NEIGHBORING_CELL_DONE,
266 request);
267 mPhone.getNeighboringCids(onCompleted);
268 break;
269
270 case EVENT_NEIGHBORING_CELL_DONE:
271 ar = (AsyncResult) msg.obj;
272 request = (MainThreadRequest) ar.userObj;
273 if (ar.exception == null && ar.result != null) {
274 request.result = ar.result;
275 } else {
276 // create an empty list to notify the waiting thread
Jake Hambye994d462014-02-03 13:10:13 -0800277 request.result = new ArrayList<NeighboringCellInfo>(0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700278 }
279 // Wake up the requesting thread
280 synchronized (request) {
281 request.notifyAll();
282 }
283 break;
284
285 case CMD_ANSWER_RINGING_CALL:
Wink Saville08874612014-08-31 19:19:58 -0700286 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800287 int answer_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700288 answerRingingCallInternal(answer_subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700289 break;
290
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700291 case CMD_END_CALL:
292 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800293 int end_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700294 final boolean hungUp;
Anthony Leeae4e36d2015-05-21 07:17:46 -0700295 Phone phone = getPhone(end_subId);
296 if (phone == null) {
297 if (DBG) log("CMD_END_CALL: no phone for id: " + end_subId);
298 break;
299 }
300 int phoneType = phone.getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700301 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
302 // CDMA: If the user presses the Power button we treat it as
303 // ending the complete call session
Wink Saville08874612014-08-31 19:19:58 -0700304 hungUp = PhoneUtils.hangupRingingAndActive(getPhone(end_subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700305 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
306 // GSM: End the call as per the Phone state
307 hungUp = PhoneUtils.hangup(mCM);
308 } else {
309 throw new IllegalStateException("Unexpected phone type: " + phoneType);
310 }
311 if (DBG) log("CMD_END_CALL: " + (hungUp ? "hung up!" : "no call to hang up"));
312 request.result = hungUp;
313 // Wake up the requesting thread
314 synchronized (request) {
315 request.notifyAll();
316 }
317 break;
318
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700319 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700320 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700321 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700322 if (uiccCard == null) {
323 loge("iccTransmitApduLogicalChannel: No UICC");
324 request.result = new IccIoResult(0x6F, 0, (byte[])null);
325 synchronized (request) {
326 request.notifyAll();
327 }
328 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700329 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
330 request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700331 uiccCard.iccTransmitApduLogicalChannel(
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700332 iccArgument.channel, iccArgument.cla, iccArgument.command,
333 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
Shishir Agrawal566b7612013-10-28 14:41:00 -0700334 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700335 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700336 break;
337
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700338 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700339 ar = (AsyncResult) msg.obj;
340 request = (MainThreadRequest) ar.userObj;
341 if (ar.exception == null && ar.result != null) {
342 request.result = ar.result;
343 } else {
344 request.result = new IccIoResult(0x6F, 0, (byte[])null);
345 if (ar.result == null) {
346 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800347 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700348 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800349 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700350 } else {
351 loge("iccTransmitApduLogicalChannel: Unknown exception");
352 }
353 }
354 synchronized (request) {
355 request.notifyAll();
356 }
357 break;
358
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700359 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
360 request = (MainThreadRequest) msg.obj;
361 iccArgument = (IccAPDUArgument) request.argument;
362 if (uiccCard == null) {
363 loge("iccTransmitApduBasicChannel: No UICC");
364 request.result = new IccIoResult(0x6F, 0, (byte[])null);
365 synchronized (request) {
366 request.notifyAll();
367 }
368 } else {
369 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
370 request);
371 uiccCard.iccTransmitApduBasicChannel(
372 iccArgument.cla, iccArgument.command, iccArgument.p1, iccArgument.p2,
373 iccArgument.p3, iccArgument.data, onCompleted);
374 }
375 break;
376
377 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
378 ar = (AsyncResult) msg.obj;
379 request = (MainThreadRequest) ar.userObj;
380 if (ar.exception == null && ar.result != null) {
381 request.result = ar.result;
382 } else {
383 request.result = new IccIoResult(0x6F, 0, (byte[])null);
384 if (ar.result == null) {
385 loge("iccTransmitApduBasicChannel: Empty response");
386 } else if (ar.exception instanceof CommandException) {
387 loge("iccTransmitApduBasicChannel: CommandException: " +
388 ar.exception);
389 } else {
390 loge("iccTransmitApduBasicChannel: Unknown exception");
391 }
392 }
393 synchronized (request) {
394 request.notifyAll();
395 }
396 break;
397
398 case CMD_EXCHANGE_SIM_IO:
399 request = (MainThreadRequest) msg.obj;
400 iccArgument = (IccAPDUArgument) request.argument;
401 if (uiccCard == null) {
402 loge("iccExchangeSimIO: No UICC");
403 request.result = new IccIoResult(0x6F, 0, (byte[])null);
404 synchronized (request) {
405 request.notifyAll();
406 }
407 } else {
408 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
409 request);
410 uiccCard.iccExchangeSimIO(iccArgument.cla, /* fileID */
411 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
412 iccArgument.data, onCompleted);
413 }
414 break;
415
416 case EVENT_EXCHANGE_SIM_IO_DONE:
417 ar = (AsyncResult) msg.obj;
418 request = (MainThreadRequest) ar.userObj;
419 if (ar.exception == null && ar.result != null) {
420 request.result = ar.result;
421 } else {
422 request.result = new IccIoResult(0x6f, 0, (byte[])null);
423 }
424 synchronized (request) {
425 request.notifyAll();
426 }
427 break;
428
Derek Tan4d5e5c12014-02-04 11:54:58 -0800429 case CMD_SEND_ENVELOPE:
430 request = (MainThreadRequest) msg.obj;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700431 if (uiccCard == null) {
432 loge("sendEnvelopeWithStatus: No UICC");
433 request.result = new IccIoResult(0x6F, 0, (byte[])null);
434 synchronized (request) {
435 request.notifyAll();
436 }
437 } else {
438 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
439 uiccCard.sendEnvelopeWithStatus((String)request.argument, onCompleted);
440 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800441 break;
442
443 case EVENT_SEND_ENVELOPE_DONE:
444 ar = (AsyncResult) msg.obj;
445 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700446 if (ar.exception == null && ar.result != null) {
447 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800448 } else {
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700449 request.result = new IccIoResult(0x6F, 0, (byte[])null);
450 if (ar.result == null) {
451 loge("sendEnvelopeWithStatus: Empty response");
452 } else if (ar.exception instanceof CommandException) {
453 loge("sendEnvelopeWithStatus: CommandException: " +
454 ar.exception);
455 } else {
456 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
457 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800458 }
459 synchronized (request) {
460 request.notifyAll();
461 }
462 break;
463
Shishir Agrawal566b7612013-10-28 14:41:00 -0700464 case CMD_OPEN_CHANNEL:
465 request = (MainThreadRequest) msg.obj;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700466 if (uiccCard == null) {
467 loge("iccOpenLogicalChannel: No UICC");
Shishir Agrawalfc0492a2016-02-17 11:15:33 -0800468 request.result = new IccOpenLogicalChannelResponse(-1,
469 IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE, null);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700470 synchronized (request) {
471 request.notifyAll();
472 }
473 } else {
474 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
475 uiccCard.iccOpenLogicalChannel((String)request.argument, onCompleted);
476 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700477 break;
478
479 case EVENT_OPEN_CHANNEL_DONE:
480 ar = (AsyncResult) msg.obj;
481 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700482 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700483 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700484 int[] result = (int[]) ar.result;
485 int channelId = result[0];
486 byte[] selectResponse = null;
487 if (result.length > 1) {
488 selectResponse = new byte[result.length - 1];
489 for (int i = 1; i < result.length; ++i) {
490 selectResponse[i - 1] = (byte) result[i];
491 }
492 }
493 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700494 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700495 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700496 if (ar.result == null) {
497 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700498 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700499 if (ar.exception != null) {
500 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
501 }
502
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700503 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Junda Liua754ba12015-05-20 01:17:52 -0700504 if (ar.exception instanceof CommandException) {
505 CommandException.Error error =
506 ((CommandException) (ar.exception)).getCommandError();
507 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700508 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700509 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700510 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700511 }
512 }
513 openChannelResp = new IccOpenLogicalChannelResponse(
514 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700515 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700516 request.result = openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700517 synchronized (request) {
518 request.notifyAll();
519 }
520 break;
521
522 case CMD_CLOSE_CHANNEL:
523 request = (MainThreadRequest) msg.obj;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700524 if (uiccCard == null) {
525 loge("iccCloseLogicalChannel: No UICC");
526 request.result = new IccIoResult(0x6F, 0, (byte[])null);
527 synchronized (request) {
528 request.notifyAll();
529 }
530 } else {
531 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
532 uiccCard.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
533 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700534 break;
535
536 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800537 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
538 break;
539
540 case CMD_NV_READ_ITEM:
541 request = (MainThreadRequest) msg.obj;
542 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
543 mPhone.nvReadItem((Integer) request.argument, onCompleted);
544 break;
545
546 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700547 ar = (AsyncResult) msg.obj;
548 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800549 if (ar.exception == null && ar.result != null) {
550 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700551 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800552 request.result = "";
553 if (ar.result == null) {
554 loge("nvReadItem: Empty response");
555 } else if (ar.exception instanceof CommandException) {
556 loge("nvReadItem: CommandException: " +
557 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700558 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800559 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700560 }
561 }
562 synchronized (request) {
563 request.notifyAll();
564 }
565 break;
566
Jake Hambye994d462014-02-03 13:10:13 -0800567 case CMD_NV_WRITE_ITEM:
568 request = (MainThreadRequest) msg.obj;
569 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
570 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
571 mPhone.nvWriteItem(idValue.first, idValue.second, onCompleted);
572 break;
573
574 case EVENT_NV_WRITE_ITEM_DONE:
575 handleNullReturnEvent(msg, "nvWriteItem");
576 break;
577
578 case CMD_NV_WRITE_CDMA_PRL:
579 request = (MainThreadRequest) msg.obj;
580 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
581 mPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
582 break;
583
584 case EVENT_NV_WRITE_CDMA_PRL_DONE:
585 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
586 break;
587
588 case CMD_NV_RESET_CONFIG:
589 request = (MainThreadRequest) msg.obj;
590 onCompleted = obtainMessage(EVENT_NV_RESET_CONFIG_DONE, request);
591 mPhone.nvResetConfig((Integer) request.argument, onCompleted);
592 break;
593
594 case EVENT_NV_RESET_CONFIG_DONE:
595 handleNullReturnEvent(msg, "nvResetConfig");
596 break;
597
Jake Hamby7c27be32014-03-03 13:25:59 -0800598 case CMD_GET_PREFERRED_NETWORK_TYPE:
599 request = (MainThreadRequest) msg.obj;
600 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
Stuart Scott54788802015-03-30 13:18:01 -0700601 getPhoneFromRequest(request).getPreferredNetworkType(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800602 break;
603
604 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
605 ar = (AsyncResult) msg.obj;
606 request = (MainThreadRequest) ar.userObj;
607 if (ar.exception == null && ar.result != null) {
608 request.result = ar.result; // Integer
609 } else {
Sanket Padawecfc2d352016-01-05 19:52:14 -0800610 request.result = null;
Jake Hamby7c27be32014-03-03 13:25:59 -0800611 if (ar.result == null) {
612 loge("getPreferredNetworkType: Empty response");
613 } else if (ar.exception instanceof CommandException) {
614 loge("getPreferredNetworkType: CommandException: " +
615 ar.exception);
616 } else {
617 loge("getPreferredNetworkType: Unknown exception");
618 }
619 }
620 synchronized (request) {
621 request.notifyAll();
622 }
623 break;
624
625 case CMD_SET_PREFERRED_NETWORK_TYPE:
626 request = (MainThreadRequest) msg.obj;
627 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
628 int networkType = (Integer) request.argument;
Stuart Scott54788802015-03-30 13:18:01 -0700629 getPhoneFromRequest(request).setPreferredNetworkType(networkType, onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800630 break;
631
632 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
633 handleNullReturnEvent(msg, "setPreferredNetworkType");
634 break;
635
Steven Liu4bf01bc2014-07-17 11:05:29 -0500636 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
637 request = (MainThreadRequest)msg.obj;
638 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
639 mPhone.invokeOemRilRequestRaw((byte[])request.argument, onCompleted);
640 break;
641
642 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
643 ar = (AsyncResult)msg.obj;
644 request = (MainThreadRequest)ar.userObj;
645 request.result = ar;
646 synchronized (request) {
647 request.notifyAll();
648 }
649 break;
650
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800651 case CMD_SET_VOICEMAIL_NUMBER:
652 request = (MainThreadRequest) msg.obj;
653 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
654 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -0800655 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
656 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800657 break;
658
659 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
660 handleNullReturnEvent(msg, "setVoicemailNumber");
661 break;
662
Stuart Scott54788802015-03-30 13:18:01 -0700663 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
664 request = (MainThreadRequest) msg.obj;
665 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
666 request);
667 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
668 break;
669
670 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
671 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
672 break;
673
Shishir Agrawal302c8692015-06-19 13:49:39 -0700674 case CMD_PERFORM_NETWORK_SCAN:
675 request = (MainThreadRequest) msg.obj;
676 onCompleted = obtainMessage(EVENT_PERFORM_NETWORK_SCAN_DONE, request);
677 getPhoneFromRequest(request).getAvailableNetworks(onCompleted);
678 break;
679
680 case EVENT_PERFORM_NETWORK_SCAN_DONE:
681 ar = (AsyncResult) msg.obj;
682 request = (MainThreadRequest) ar.userObj;
683 CellNetworkScanResult cellScanResult;
684 if (ar.exception == null && ar.result != null) {
685 cellScanResult = new CellNetworkScanResult(
686 CellNetworkScanResult.STATUS_SUCCESS,
687 (List<OperatorInfo>) ar.result);
688 } else {
689 if (ar.result == null) {
690 loge("getCellNetworkScanResults: Empty response");
691 }
692 if (ar.exception != null) {
693 loge("getCellNetworkScanResults: Exception: " + ar.exception);
694 }
695 int errorCode = CellNetworkScanResult.STATUS_UNKNOWN_ERROR;
696 if (ar.exception instanceof CommandException) {
697 CommandException.Error error =
698 ((CommandException) (ar.exception)).getCommandError();
699 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
700 errorCode = CellNetworkScanResult.STATUS_RADIO_NOT_AVAILABLE;
701 } else if (error == CommandException.Error.GENERIC_FAILURE) {
702 errorCode = CellNetworkScanResult.STATUS_RADIO_GENERIC_FAILURE;
703 }
704 }
705 cellScanResult = new CellNetworkScanResult(errorCode, null);
706 }
707 request.result = cellScanResult;
708 synchronized (request) {
709 request.notifyAll();
710 }
711 break;
712
713 case CMD_SET_NETWORK_SELECTION_MODE_MANUAL:
714 request = (MainThreadRequest) msg.obj;
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700715 ManualNetworkSelectionArgument selArg =
716 (ManualNetworkSelectionArgument) request.argument;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700717 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE,
718 request);
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700719 getPhoneFromRequest(request).selectNetworkManually(selArg.operatorInfo,
720 selArg.persistSelection, onCompleted);
Shishir Agrawal302c8692015-06-19 13:49:39 -0700721 break;
722
723 case EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE:
724 handleNullReturnEvent(msg, "setNetworkSelectionModeManual");
725 break;
726
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700727 case CMD_GET_MODEM_ACTIVITY_INFO:
728 request = (MainThreadRequest) msg.obj;
729 onCompleted = obtainMessage(EVENT_GET_MODEM_ACTIVITY_INFO_DONE, request);
Prerepa Viswanadham61a60ad2015-06-08 18:07:51 -0700730 mPhone.getModemActivityInfo(onCompleted);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700731 break;
732
733 case EVENT_GET_MODEM_ACTIVITY_INFO_DONE:
734 ar = (AsyncResult) msg.obj;
735 request = (MainThreadRequest) ar.userObj;
736 if (ar.exception == null && ar.result != null) {
737 request.result = ar.result;
738 } else {
739 if (ar.result == null) {
740 loge("queryModemActivityInfo: Empty response");
741 } else if (ar.exception instanceof CommandException) {
742 loge("queryModemActivityInfo: CommandException: " +
743 ar.exception);
744 } else {
745 loge("queryModemActivityInfo: Unknown exception");
746 }
747 }
Amit Mahajand4766222016-01-28 15:28:28 -0800748 // Result cannot be null. Return ModemActivityInfo with all fields set to 0.
749 if (request.result == null) {
750 request.result = new ModemActivityInfo(0, 0, 0, null, 0, 0);
751 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700752 synchronized (request) {
753 request.notifyAll();
754 }
755 break;
756
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700757 default:
758 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
759 break;
760 }
761 }
Jake Hambye994d462014-02-03 13:10:13 -0800762
763 private void handleNullReturnEvent(Message msg, String command) {
764 AsyncResult ar = (AsyncResult) msg.obj;
765 MainThreadRequest request = (MainThreadRequest) ar.userObj;
766 if (ar.exception == null) {
767 request.result = true;
768 } else {
769 request.result = false;
770 if (ar.exception instanceof CommandException) {
771 loge(command + ": CommandException: " + ar.exception);
772 } else {
773 loge(command + ": Unknown exception");
774 }
775 }
776 synchronized (request) {
777 request.notifyAll();
778 }
779 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700780 }
781
782 /**
783 * Posts the specified command to be executed on the main thread,
784 * waits for the request to complete, and returns the result.
785 * @see #sendRequestAsync
786 */
787 private Object sendRequest(int command, Object argument) {
Sanket Padawe56e75a32016-02-08 12:18:19 -0800788 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Wink Saville36469e72014-06-11 15:17:00 -0700789 }
790
791 /**
792 * Posts the specified command to be executed on the main thread,
793 * waits for the request to complete, and returns the result.
794 * @see #sendRequestAsync
795 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800796 private Object sendRequest(int command, Object argument, Integer subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700797 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
798 throw new RuntimeException("This method will deadlock if called from the main thread.");
799 }
800
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800801 MainThreadRequest request = new MainThreadRequest(argument, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700802 Message msg = mMainThreadHandler.obtainMessage(command, request);
803 msg.sendToTarget();
804
805 // Wait for the request to complete
806 synchronized (request) {
807 while (request.result == null) {
808 try {
809 request.wait();
810 } catch (InterruptedException e) {
811 // Do nothing, go back and wait until the request is complete
812 }
813 }
814 }
815 return request.result;
816 }
817
818 /**
819 * Asynchronous ("fire and forget") version of sendRequest():
820 * Posts the specified command to be executed on the main thread, and
821 * returns immediately.
822 * @see #sendRequest
823 */
824 private void sendRequestAsync(int command) {
825 mMainThreadHandler.sendEmptyMessage(command);
826 }
827
828 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -0700829 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
830 * @see {@link #sendRequest(int,Object)}
831 */
832 private void sendRequestAsync(int command, Object argument) {
833 MainThreadRequest request = new MainThreadRequest(argument);
834 Message msg = mMainThreadHandler.obtainMessage(command, request);
835 msg.sendToTarget();
836 }
837
838 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700839 * Initialize the singleton PhoneInterfaceManager instance.
840 * This is only done once, at startup, from PhoneApp.onCreate().
841 */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700842 /* package */ static PhoneInterfaceManager init(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700843 synchronized (PhoneInterfaceManager.class) {
844 if (sInstance == null) {
Sailesh Nepal194161e2014-07-03 08:57:44 -0700845 sInstance = new PhoneInterfaceManager(app, phone);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700846 } else {
847 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
848 }
849 return sInstance;
850 }
851 }
852
853 /** Private constructor; @see init() */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700854 private PhoneInterfaceManager(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700855 mApp = app;
856 mPhone = phone;
857 mCM = PhoneGlobals.getInstance().mCM;
Stuart Scott981d8582015-04-21 14:09:50 -0700858 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700859 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
860 mMainThreadHandler = new MainThreadHandler();
Andrew Leedf14ead2014-10-17 14:22:52 -0700861 mTelephonySharedPreferences =
Derek Tan97ebb422014-09-05 16:55:38 -0700862 PreferenceManager.getDefaultSharedPreferences(mPhone.getContext());
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800863 mSubscriptionController = SubscriptionController.getInstance();
Wink Saville3ab207e2014-11-20 13:07:20 -0800864
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700865 publish();
866 }
867
868 private void publish() {
869 if (DBG) log("publish: " + this);
870
871 ServiceManager.addService("phone", this);
872 }
873
Stuart Scott584921c2015-01-15 17:10:34 -0800874 private Phone getPhoneFromRequest(MainThreadRequest request) {
Sanket Padawe56e75a32016-02-08 12:18:19 -0800875 return (request.subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID)
876 ? mPhone : getPhone(request.subId);
Stuart Scott584921c2015-01-15 17:10:34 -0800877 }
878
Wink Saville36469e72014-06-11 15:17:00 -0700879 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -0700880 private Phone getPhone(int subId) {
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800881 return PhoneFactory.getPhone(mSubscriptionController.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -0700882 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700883 //
884 // Implementation of the ITelephony interface.
885 //
886
887 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700888 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -0700889 }
890
Wink Savilleb564aae2014-10-23 10:18:09 -0700891 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700892 if (DBG) log("dial: " + number);
893 // No permission check needed here: This is just a wrapper around the
894 // ACTION_DIAL intent, which is available to any app since it puts up
895 // the UI before it does anything.
896
897 String url = createTelUrl(number);
898 if (url == null) {
899 return;
900 }
901
902 // PENDING: should we just silently fail if phone is offhook or ringing?
Wink Saville36469e72014-06-11 15:17:00 -0700903 PhoneConstants.State state = mCM.getState(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700904 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
905 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
906 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
907 mApp.startActivity(intent);
908 }
909 }
910
911 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700912 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -0700913 }
914
Wink Savilleb564aae2014-10-23 10:18:09 -0700915 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700916 if (DBG) log("call: " + number);
917
918 // This is just a wrapper around the ACTION_CALL intent, but we still
919 // need to do a permission check since we're calling startActivity()
920 // from the context of the phone app.
921 enforceCallPermission();
922
923 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
924 != AppOpsManager.MODE_ALLOWED) {
925 return;
926 }
927
928 String url = createTelUrl(number);
929 if (url == null) {
930 return;
931 }
932
Wink Saville08874612014-08-31 19:19:58 -0700933 boolean isValid = false;
Narayan Kamath1c496c22015-04-16 14:40:19 +0100934 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoList();
Wink Saville3ab207e2014-11-20 13:07:20 -0800935 if (slist != null) {
936 for (SubscriptionInfo subInfoRecord : slist) {
937 if (subInfoRecord.getSubscriptionId() == subId) {
938 isValid = true;
939 break;
940 }
Wink Saville08874612014-08-31 19:19:58 -0700941 }
942 }
943 if (isValid == false) {
944 return;
945 }
946
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700947 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
Wink Saville36469e72014-06-11 15:17:00 -0700948 intent.putExtra(SUBSCRIPTION_KEY, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700949 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
950 mApp.startActivity(intent);
951 }
952
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700953 /**
954 * End a call based on call state
955 * @return true is a call was ended
956 */
957 public boolean endCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700958 return endCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700959 }
960
961 /**
962 * End a call based on the call state of the subId
963 * @return true is a call was ended
964 */
Wink Savilleb564aae2014-10-23 10:18:09 -0700965 public boolean endCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700966 enforceCallPermission();
Stuart Scott584921c2015-01-15 17:10:34 -0800967 return (Boolean) sendRequest(CMD_END_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700968 }
969
970 public void answerRingingCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700971 answerRingingCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700972 }
973
Wink Savilleb564aae2014-10-23 10:18:09 -0700974 public void answerRingingCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700975 if (DBG) log("answerRingingCall...");
976 // TODO: there should eventually be a separate "ANSWER_PHONE" permission,
977 // but that can probably wait till the big TelephonyManager API overhaul.
978 // For now, protect this call with the MODIFY_PHONE_STATE permission.
979 enforceModifyPermission();
Stuart Scott584921c2015-01-15 17:10:34 -0800980 sendRequest(CMD_ANSWER_RINGING_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700981 }
982
983 /**
984 * Make the actual telephony calls to implement answerRingingCall().
985 * This should only be called from the main thread of the Phone app.
986 * @see #answerRingingCall
987 *
988 * TODO: it would be nice to return true if we answered the call, or
989 * false if there wasn't actually a ringing incoming call, or some
990 * other error occurred. (In other words, pass back the return value
991 * from PhoneUtils.answerCall() or PhoneUtils.answerAndEndActive().)
992 * But that would require calling this method via sendRequest() rather
993 * than sendRequestAsync(), and right now we don't actually *need* that
994 * return value, so let's just return void for now.
995 */
Wink Savilleb564aae2014-10-23 10:18:09 -0700996 private void answerRingingCallInternal(int subId) {
Wink Saville08874612014-08-31 19:19:58 -0700997 final boolean hasRingingCall = !getPhone(subId).getRingingCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700998 if (hasRingingCall) {
Wink Saville08874612014-08-31 19:19:58 -0700999 final boolean hasActiveCall = !getPhone(subId).getForegroundCall().isIdle();
1000 final boolean hasHoldingCall = !getPhone(subId).getBackgroundCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001001 if (hasActiveCall && hasHoldingCall) {
1002 // Both lines are in use!
1003 // TODO: provide a flag to let the caller specify what
1004 // policy to use if both lines are in use. (The current
1005 // behavior is hardwired to "answer incoming, end ongoing",
1006 // which is how the CALL button is specced to behave.)
1007 PhoneUtils.answerAndEndActive(mCM, mCM.getFirstActiveRingingCall());
1008 return;
1009 } else {
1010 // answerCall() will automatically hold the current active
1011 // call, if there is one.
1012 PhoneUtils.answerCall(mCM.getFirstActiveRingingCall());
1013 return;
1014 }
1015 } else {
1016 // No call was ringing.
1017 return;
1018 }
1019 }
1020
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001021 /**
Santos Cordon5422a8d2014-09-12 04:20:56 -07001022 * This method is no longer used and can be removed once TelephonyManager stops referring to it.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001023 */
Santos Cordon5422a8d2014-09-12 04:20:56 -07001024 public void silenceRinger() {
1025 Log.e(LOG_TAG, "silenseRinger not supported");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001026 }
1027
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001028 @Override
1029 public boolean isOffhook(String callingPackage) {
1030 return isOffhookForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001031 }
1032
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001033 @Override
1034 public boolean isOffhookForSubscriber(int subId, String callingPackage) {
1035 if (!canReadPhoneState(callingPackage, "isOffhookForSubscriber")) {
1036 return false;
1037 }
1038
Sanket Padawe356d7632015-06-22 14:03:32 -07001039 final Phone phone = getPhone(subId);
1040 if (phone != null) {
1041 return (phone.getState() == PhoneConstants.State.OFFHOOK);
1042 } else {
1043 return false;
1044 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001045 }
1046
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001047 @Override
1048 public boolean isRinging(String callingPackage) {
1049 return (isRingingForSubscriber(getDefaultSubscription(), callingPackage));
Wink Saville36469e72014-06-11 15:17:00 -07001050 }
1051
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001052 @Override
1053 public boolean isRingingForSubscriber(int subId, String callingPackage) {
1054 if (!canReadPhoneState(callingPackage, "isRingingForSubscriber")) {
1055 return false;
1056 }
1057
Sanket Padawe356d7632015-06-22 14:03:32 -07001058 final Phone phone = getPhone(subId);
1059 if (phone != null) {
1060 return (phone.getState() == PhoneConstants.State.RINGING);
1061 } else {
1062 return false;
1063 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001064 }
1065
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001066 @Override
1067 public boolean isIdle(String callingPackage) {
1068 return isIdleForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001069 }
1070
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001071 @Override
1072 public boolean isIdleForSubscriber(int subId, String callingPackage) {
1073 if (!canReadPhoneState(callingPackage, "isIdleForSubscriber")) {
1074 return false;
1075 }
1076
Sanket Padawe356d7632015-06-22 14:03:32 -07001077 final Phone phone = getPhone(subId);
1078 if (phone != null) {
1079 return (phone.getState() == PhoneConstants.State.IDLE);
1080 } else {
1081 return false;
1082 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001083 }
1084
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001085 public boolean supplyPin(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001086 return supplyPinForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001087 }
1088
Wink Savilleb564aae2014-10-23 10:18:09 -07001089 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001090 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001091 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1092 }
1093
1094 public boolean supplyPuk(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001095 return supplyPukForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001096 }
1097
Wink Savilleb564aae2014-10-23 10:18:09 -07001098 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001099 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001100 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1101 }
1102
1103 /** {@hide} */
1104 public int[] supplyPinReportResult(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001105 return supplyPinReportResultForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001106 }
1107
Wink Savilleb564aae2014-10-23 10:18:09 -07001108 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001109 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001110 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001111 checkSimPin.start();
1112 return checkSimPin.unlockSim(null, pin);
1113 }
1114
Wink Saville9de0f752013-10-22 19:04:03 -07001115 /** {@hide} */
1116 public int[] supplyPukReportResult(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001117 return supplyPukReportResultForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001118 }
1119
Wink Savilleb564aae2014-10-23 10:18:09 -07001120 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001121 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001122 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001123 checkSimPuk.start();
1124 return checkSimPuk.unlockSim(puk, pin);
1125 }
1126
1127 /**
Wink Saville9de0f752013-10-22 19:04:03 -07001128 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001129 * a synchronous one.
1130 */
1131 private static class UnlockSim extends Thread {
1132
1133 private final IccCard mSimCard;
1134
1135 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07001136 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1137 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001138
1139 // For replies from SimCard interface
1140 private Handler mHandler;
1141
1142 // For async handler to identify request type
1143 private static final int SUPPLY_PIN_COMPLETE = 100;
1144
1145 public UnlockSim(IccCard simCard) {
1146 mSimCard = simCard;
1147 }
1148
1149 @Override
1150 public void run() {
1151 Looper.prepare();
1152 synchronized (UnlockSim.this) {
1153 mHandler = new Handler() {
1154 @Override
1155 public void handleMessage(Message msg) {
1156 AsyncResult ar = (AsyncResult) msg.obj;
1157 switch (msg.what) {
1158 case SUPPLY_PIN_COMPLETE:
1159 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
1160 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07001161 mRetryCount = msg.arg1;
1162 if (ar.exception != null) {
1163 if (ar.exception instanceof CommandException &&
1164 ((CommandException)(ar.exception)).getCommandError()
1165 == CommandException.Error.PASSWORD_INCORRECT) {
1166 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
1167 } else {
1168 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1169 }
1170 } else {
1171 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
1172 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001173 mDone = true;
1174 UnlockSim.this.notifyAll();
1175 }
1176 break;
1177 }
1178 }
1179 };
1180 UnlockSim.this.notifyAll();
1181 }
1182 Looper.loop();
1183 }
1184
1185 /*
1186 * Use PIN or PUK to unlock SIM card
1187 *
1188 * If PUK is null, unlock SIM card with PIN
1189 *
1190 * If PUK is not null, unlock SIM card with PUK and set PIN code
1191 */
Wink Saville9de0f752013-10-22 19:04:03 -07001192 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001193
1194 while (mHandler == null) {
1195 try {
1196 wait();
1197 } catch (InterruptedException e) {
1198 Thread.currentThread().interrupt();
1199 }
1200 }
1201 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
1202
1203 if (puk == null) {
1204 mSimCard.supplyPin(pin, callback);
1205 } else {
1206 mSimCard.supplyPuk(puk, pin, callback);
1207 }
1208
1209 while (!mDone) {
1210 try {
1211 Log.d(LOG_TAG, "wait for done");
1212 wait();
1213 } catch (InterruptedException e) {
1214 // Restore the interrupted status
1215 Thread.currentThread().interrupt();
1216 }
1217 }
1218 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07001219 int[] resultArray = new int[2];
1220 resultArray[0] = mResult;
1221 resultArray[1] = mRetryCount;
1222 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001223 }
1224 }
1225
1226 public void updateServiceLocation() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001227 updateServiceLocationForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001228
1229 }
1230
Wink Savilleb564aae2014-10-23 10:18:09 -07001231 public void updateServiceLocationForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001232 // No permission check needed here: this call is harmless, and it's
1233 // needed for the ServiceState.requestStateUpdate() call (which is
1234 // already intentionally exposed to 3rd parties.)
Sanket Padawe356d7632015-06-22 14:03:32 -07001235 final Phone phone = getPhone(subId);
1236 if (phone != null) {
1237 phone.updateServiceLocation();
1238 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001239 }
1240
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001241 @Override
1242 public boolean isRadioOn(String callingPackage) {
1243 return isRadioOnForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001244 }
1245
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001246 @Override
1247 public boolean isRadioOnForSubscriber(int subId, String callingPackage) {
1248 if (!canReadPhoneState(callingPackage, "isRadioOnForSubscriber")) {
1249 return false;
1250 }
1251 return isRadioOnForSubscriber(subId);
1252 }
1253
1254 private boolean isRadioOnForSubscriber(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001255 final Phone phone = getPhone(subId);
1256 if (phone != null) {
1257 return phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF;
1258 } else {
1259 return false;
1260 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001261 }
1262
1263 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001264 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001265
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001266 }
Wink Saville36469e72014-06-11 15:17:00 -07001267
Wink Savilleb564aae2014-10-23 10:18:09 -07001268 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001269 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001270 final Phone phone = getPhone(subId);
1271 if (phone != null) {
1272 phone.setRadioPower(!isRadioOnForSubscriber(subId));
1273 }
Wink Saville36469e72014-06-11 15:17:00 -07001274 }
1275
1276 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001277 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001278 }
1279
Wink Savilleb564aae2014-10-23 10:18:09 -07001280 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07001281 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001282 final Phone phone = getPhone(subId);
1283 if (phone == null) {
1284 return false;
1285 }
1286 if ((phone.getServiceState().getState() !=
Wink Saville36469e72014-06-11 15:17:00 -07001287 ServiceState.STATE_POWER_OFF) != turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001288 toggleRadioOnOffForSubscriber(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001289 }
1290 return true;
1291 }
Wink Saville36469e72014-06-11 15:17:00 -07001292
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001293 public boolean needMobileRadioShutdown() {
1294 /*
1295 * If any of the Radios are available, it will need to be
1296 * shutdown. So return true if any Radio is available.
1297 */
1298 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1299 Phone phone = PhoneFactory.getPhone(i);
1300 if (phone != null && phone.isRadioAvailable()) return true;
1301 }
1302 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
1303 return false;
1304 }
1305
1306 public void shutdownMobileRadios() {
1307 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1308 logv("Shutting down Phone " + i);
1309 shutdownRadioUsingPhoneId(i);
1310 }
1311 }
1312
1313 private void shutdownRadioUsingPhoneId(int phoneId) {
1314 enforceModifyPermission();
1315 Phone phone = PhoneFactory.getPhone(phoneId);
1316 if (phone != null && phone.isRadioAvailable()) {
1317 phone.shutdownRadio();
1318 }
1319 }
1320
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001321 public boolean setRadioPower(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001322 return setRadioPowerForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001323 }
1324
Wink Savilleb564aae2014-10-23 10:18:09 -07001325 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001326 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001327 final Phone phone = getPhone(subId);
1328 if (phone != null) {
1329 phone.setRadioPower(turnOn);
1330 return true;
1331 } else {
1332 return false;
1333 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001334 }
1335
Wink Saville36469e72014-06-11 15:17:00 -07001336 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001337 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001338 public boolean enableDataConnectivity() {
1339 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001340 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001341 final Phone phone = getPhone(subId);
1342 if (phone != null) {
1343 phone.setDataEnabled(true);
1344 return true;
1345 } else {
1346 return false;
1347 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001348 }
1349
Wink Saville36469e72014-06-11 15:17:00 -07001350 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001351 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001352 public boolean disableDataConnectivity() {
1353 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001354 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001355 final Phone phone = getPhone(subId);
1356 if (phone != null) {
1357 phone.setDataEnabled(false);
1358 return true;
1359 } else {
1360 return false;
1361 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001362 }
1363
Wink Saville36469e72014-06-11 15:17:00 -07001364 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001365 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001366 public boolean isDataConnectivityPossible() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001367 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001368 final Phone phone = getPhone(subId);
1369 if (phone != null) {
1370 return phone.isDataConnectivityPossible();
1371 } else {
1372 return false;
1373 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001374 }
1375
1376 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001377 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07001378 }
1379
Wink Savilleb564aae2014-10-23 10:18:09 -07001380 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001381 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001382 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1383 return false;
1384 }
Wink Saville36469e72014-06-11 15:17:00 -07001385 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001386 }
1387
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001388 public int getCallState() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001389 return getCallStateForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001390 }
1391
Wink Savilleb564aae2014-10-23 10:18:09 -07001392 public int getCallStateForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001393 return DefaultPhoneNotifier.convertCallState(getPhone(subId).getState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001394 }
1395
Sanket Padawe356d7632015-06-22 14:03:32 -07001396 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001397 public int getDataState() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001398 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001399 if (phone != null) {
1400 return DefaultPhoneNotifier.convertDataState(phone.getDataConnectionState());
1401 } else {
1402 return DefaultPhoneNotifier.convertDataState(PhoneConstants.DataState.DISCONNECTED);
1403 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001404 }
1405
Sanket Padawe356d7632015-06-22 14:03:32 -07001406 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001407 public int getDataActivity() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001408 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001409 if (phone != null) {
1410 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
1411 } else {
1412 return TelephonyManager.DATA_ACTIVITY_NONE;
1413 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001414 }
1415
1416 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001417 public Bundle getCellLocation(String callingPackage) {
1418 enforceFineOrCoarseLocationPermission("getCellLocation");
1419
1420 // OP_COARSE_LOCATION controls both fine and coarse location.
1421 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1422 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001423 log("getCellLocation: returning null; mode != allowed");
Svetoslav64fad262015-04-14 14:35:21 -07001424 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001425 }
1426
Jake Hambye994d462014-02-03 13:10:13 -08001427 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001428 if (DBG_LOC) log("getCellLocation: is active user");
1429 Bundle data = new Bundle();
Legler Wu2c01cdf2014-12-08 19:00:59 +08001430 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001431 if (phone == null) {
1432 return null;
1433 }
Legler Wu2c01cdf2014-12-08 19:00:59 +08001434 phone.getCellLocation().fillInNotifierBundle(data);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001435 return data;
1436 } else {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001437 log("getCellLocation: suppress non-active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001438 return null;
1439 }
1440 }
1441
Svetoslav64fad262015-04-14 14:35:21 -07001442 private void enforceFineOrCoarseLocationPermission(String message) {
1443 try {
1444 mApp.enforceCallingOrSelfPermission(
1445 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1446 } catch (SecurityException e) {
1447 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1448 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1449 // is the weaker precondition
1450 mApp.enforceCallingOrSelfPermission(
1451 android.Manifest.permission.ACCESS_COARSE_LOCATION, message);
1452 }
1453 }
1454
1455
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001456 @Override
1457 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001458 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001459 }
1460
Sanket Padawe356d7632015-06-22 14:03:32 -07001461 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001462 public void enableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001463 mApp.enforceCallingOrSelfPermission(
1464 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001465 final Phone phone = getPhone(subId);
1466 if (phone != null) {
1467 phone.enableLocationUpdates();
1468 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001469 }
1470
1471 @Override
1472 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001473 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001474 }
1475
Sanket Padawe356d7632015-06-22 14:03:32 -07001476 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001477 public void disableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001478 mApp.enforceCallingOrSelfPermission(
1479 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001480 final Phone phone = getPhone(subId);
1481 if (phone != null) {
1482 phone.disableLocationUpdates();
1483 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001484 }
1485
1486 @Override
1487 @SuppressWarnings("unchecked")
1488 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
Svetoslav64fad262015-04-14 14:35:21 -07001489 enforceFineOrCoarseLocationPermission("getNeighboringCellInfo");
1490
1491 // OP_COARSE_LOCATION controls both fine and coarse location.
1492 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1493 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1494 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001495 }
1496
1497 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
1498 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1499 return null;
1500 }
Svetoslav64fad262015-04-14 14:35:21 -07001501
Jake Hambye994d462014-02-03 13:10:13 -08001502 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001503 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
1504
1505 ArrayList<NeighboringCellInfo> cells = null;
1506
1507 try {
1508 cells = (ArrayList<NeighboringCellInfo>) sendRequest(
Sanket Padawe56e75a32016-02-08 12:18:19 -08001509 CMD_HANDLE_NEIGHBORING_CELL, null,
1510 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001511 } catch (RuntimeException e) {
Wink Saville36469e72014-06-11 15:17:00 -07001512 Log.e(LOG_TAG, "getNeighboringCellInfo " + e);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001513 }
1514 return cells;
1515 } else {
1516 if (DBG_LOC) log("getNeighboringCellInfo: suppress non-active user");
1517 return null;
1518 }
1519 }
1520
1521
1522 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001523 public List<CellInfo> getAllCellInfo(String callingPackage) {
1524 enforceFineOrCoarseLocationPermission("getAllCellInfo");
1525
1526 // OP_COARSE_LOCATION controls both fine and coarse location.
1527 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1528 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1529 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001530 }
1531
Jake Hambye994d462014-02-03 13:10:13 -08001532 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001533 if (DBG_LOC) log("getAllCellInfo: is active user");
Legler Wu2c01cdf2014-12-08 19:00:59 +08001534 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
1535 for (Phone phone : PhoneFactory.getPhones()) {
Robert Greenwaltd9eb7da2015-08-19 12:18:12 -07001536 final List<CellInfo> info = phone.getAllCellInfo();
1537 if (info != null) cellInfos.addAll(phone.getAllCellInfo());
Legler Wu2c01cdf2014-12-08 19:00:59 +08001538 }
1539 return cellInfos;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001540 } else {
1541 if (DBG_LOC) log("getAllCellInfo: suppress non-active user");
1542 return null;
1543 }
1544 }
1545
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001546 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001547 public void setCellInfoListRate(int rateInMillis) {
1548 mPhone.setCellInfoListRate(rateInMillis);
1549 }
1550
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001551 //
1552 // Internal helper methods.
1553 //
1554
Jake Hambye994d462014-02-03 13:10:13 -08001555 private static boolean checkIfCallerIsSelfOrForegroundUser() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001556 boolean ok;
1557
1558 boolean self = Binder.getCallingUid() == Process.myUid();
1559 if (!self) {
1560 // Get the caller's user id then clear the calling identity
1561 // which will be restored in the finally clause.
1562 int callingUser = UserHandle.getCallingUserId();
1563 long ident = Binder.clearCallingIdentity();
1564
1565 try {
1566 // With calling identity cleared the current user is the foreground user.
1567 int foregroundUser = ActivityManager.getCurrentUser();
1568 ok = (foregroundUser == callingUser);
1569 if (DBG_LOC) {
1570 log("checkIfCallerIsSelfOrForegoundUser: foregroundUser=" + foregroundUser
1571 + " callingUser=" + callingUser + " ok=" + ok);
1572 }
1573 } catch (Exception ex) {
1574 if (DBG_LOC) loge("checkIfCallerIsSelfOrForegoundUser: Exception ex=" + ex);
1575 ok = false;
1576 } finally {
1577 Binder.restoreCallingIdentity(ident);
1578 }
1579 } else {
1580 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: is self");
1581 ok = true;
1582 }
1583 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: ret=" + ok);
1584 return ok;
1585 }
1586
1587 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001588 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1589 *
1590 * @throws SecurityException if the caller does not have the required permission
1591 */
1592 private void enforceModifyPermission() {
1593 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1594 }
1595
1596 /**
Junda Liua2e36012014-07-09 18:30:01 -07001597 * Make sure either system app or the caller has carrier privilege.
1598 *
1599 * @throws SecurityException if the caller does not have the required permission/privilege
1600 */
1601 private void enforceModifyPermissionOrCarrierPrivilege() {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001602 int permission = mApp.checkCallingOrSelfPermission(
1603 android.Manifest.permission.MODIFY_PHONE_STATE);
1604 if (permission == PackageManager.PERMISSION_GRANTED) {
1605 return;
1606 }
1607
1608 log("No modify permission, check carrier privilege next.");
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08001609 if (getCarrierPrivilegeStatus() != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001610 loge("No Carrier Privilege.");
1611 throw new SecurityException("No modify permission or carrier privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001612 }
1613 }
1614
1615 /**
1616 * Make sure the caller has carrier privilege.
1617 *
1618 * @throws SecurityException if the caller does not have the required permission
1619 */
1620 private void enforceCarrierPrivilege() {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08001621 if (getCarrierPrivilegeStatus() != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001622 loge("No Carrier Privilege.");
1623 throw new SecurityException("No Carrier Privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001624 }
1625 }
1626
1627 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001628 * Make sure the caller has the CALL_PHONE permission.
1629 *
1630 * @throws SecurityException if the caller does not have the required permission
1631 */
1632 private void enforceCallPermission() {
1633 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1634 }
1635
Stuart Scott8eef64f2015-04-08 15:13:54 -07001636 private void enforceConnectivityInternalPermission() {
1637 mApp.enforceCallingOrSelfPermission(
1638 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1639 "ConnectivityService");
1640 }
1641
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001642 private String createTelUrl(String number) {
1643 if (TextUtils.isEmpty(number)) {
1644 return null;
1645 }
1646
Jake Hambye994d462014-02-03 13:10:13 -08001647 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001648 }
1649
Ihab Awadf9e92732013-12-05 18:02:52 -08001650 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001651 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1652 }
1653
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001654 private static void logv(String msg) {
1655 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
1656 }
1657
Ihab Awadf9e92732013-12-05 18:02:52 -08001658 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001659 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1660 }
1661
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001662 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001663 public int getActivePhoneType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001664 return getActivePhoneTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001665 }
1666
Sanket Padawe356d7632015-06-22 14:03:32 -07001667 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001668 public int getActivePhoneTypeForSubscriber(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001669 final Phone phone = getPhone(subId);
1670 if (phone == null) {
1671 return PhoneConstants.PHONE_TYPE_NONE;
1672 } else {
1673 return getPhone(subId).getPhoneType();
1674 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001675 }
1676
1677 /**
1678 * Returns the CDMA ERI icon index to display
1679 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001680 @Override
1681 public int getCdmaEriIconIndex(String callingPackage) {
1682 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001683 }
1684
Sanket Padawe356d7632015-06-22 14:03:32 -07001685 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001686 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage) {
1687 if (!canReadPhoneState(callingPackage, "getCdmaEriIconIndexForSubscriber")) {
1688 return -1;
1689 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001690 final Phone phone = getPhone(subId);
1691 if (phone != null) {
1692 return phone.getCdmaEriIconIndex();
1693 } else {
1694 return -1;
1695 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001696 }
1697
1698 /**
1699 * Returns the CDMA ERI icon mode,
1700 * 0 - ON
1701 * 1 - FLASHING
1702 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001703 @Override
1704 public int getCdmaEriIconMode(String callingPackage) {
1705 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001706 }
1707
Sanket Padawe356d7632015-06-22 14:03:32 -07001708 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001709 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage) {
1710 if (!canReadPhoneState(callingPackage, "getCdmaEriIconModeForSubscriber")) {
1711 return -1;
1712 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001713 final Phone phone = getPhone(subId);
1714 if (phone != null) {
1715 return phone.getCdmaEriIconMode();
1716 } else {
1717 return -1;
1718 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001719 }
1720
1721 /**
1722 * Returns the CDMA ERI text,
1723 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001724 @Override
1725 public String getCdmaEriText(String callingPackage) {
1726 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001727 }
1728
Sanket Padawe356d7632015-06-22 14:03:32 -07001729 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001730 public String getCdmaEriTextForSubscriber(int subId, String callingPackage) {
1731 if (!canReadPhoneState(callingPackage, "getCdmaEriIconTextForSubscriber")) {
1732 return null;
1733 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001734 final Phone phone = getPhone(subId);
1735 if (phone != null) {
1736 return phone.getCdmaEriText();
1737 } else {
1738 return null;
1739 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001740 }
1741
1742 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07001743 * Returns the CDMA MDN.
1744 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001745 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001746 public String getCdmaMdn(int subId) {
Junda Liuca05d5d2014-08-14 22:36:34 -07001747 enforceModifyPermissionOrCarrierPrivilege();
Sanket Padawe356d7632015-06-22 14:03:32 -07001748 final Phone phone = getPhone(subId);
1749 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA && phone != null) {
1750 return phone.getLine1Number();
Junda Liuca05d5d2014-08-14 22:36:34 -07001751 } else {
1752 return null;
1753 }
1754 }
1755
1756 /**
1757 * Returns the CDMA MIN.
1758 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001759 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001760 public String getCdmaMin(int subId) {
Junda Liuca05d5d2014-08-14 22:36:34 -07001761 enforceModifyPermissionOrCarrierPrivilege();
Sanket Padawe356d7632015-06-22 14:03:32 -07001762 final Phone phone = getPhone(subId);
1763 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1764 return phone.getCdmaMin();
Junda Liuca05d5d2014-08-14 22:36:34 -07001765 } else {
1766 return null;
1767 }
1768 }
1769
1770 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001771 * Returns true if CDMA provisioning needs to run.
1772 */
1773 public boolean needsOtaServiceProvisioning() {
1774 return mPhone.needsOtaServiceProvisioning();
1775 }
1776
1777 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001778 * Sets the voice mail number of a given subId.
1779 */
1780 @Override
1781 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08001782 enforceCarrierPrivilege();
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001783 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
1784 new Pair<String, String>(alphaTag, number), new Integer(subId));
1785 return success;
1786 }
1787
1788 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001789 * Returns the unread count of voicemails
1790 */
1791 public int getVoiceMessageCount() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001792 return getVoiceMessageCountForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001793 }
1794
1795 /**
1796 * Returns the unread count of voicemails for a subId
1797 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001798 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001799 public int getVoiceMessageCountForSubscriber( int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001800 final Phone phone = getPhone(subId);
1801 if (phone != null) {
1802 return phone.getVoiceMessageCount();
1803 } else {
1804 return 0;
1805 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001806 }
1807
1808 /**
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001809 * Returns the data network type.
1810 * Legacy call, permission-free.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001811 *
1812 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
1813 */
1814 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001815 public int getNetworkType() {
1816 final Phone phone = getPhone(getDefaultSubscription());
1817 if (phone != null) {
1818 return phone.getServiceState().getDataNetworkType();
1819 } else {
1820 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1821 }
Wink Saville36469e72014-06-11 15:17:00 -07001822 }
1823
1824 /**
1825 * Returns the network type for a subId
1826 */
1827 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001828 public int getNetworkTypeForSubscriber(int subId, String callingPackage) {
1829 if (!canReadPhoneState(callingPackage, "getNetworkTypeForSubscriber")) {
1830 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1831 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001832
Sanket Padawe356d7632015-06-22 14:03:32 -07001833 final Phone phone = getPhone(subId);
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001834 if (phone != null) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001835 return phone.getServiceState().getDataNetworkType();
1836 } else {
1837 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1838 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001839 }
1840
1841 /**
1842 * Returns the data network type
1843 */
1844 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001845 public int getDataNetworkType(String callingPackage) {
1846 return getDataNetworkTypeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001847 }
1848
1849 /**
1850 * Returns the data network type for a subId
1851 */
1852 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001853 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage) {
1854 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
1855 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1856 }
1857
Sanket Padawe356d7632015-06-22 14:03:32 -07001858 final Phone phone = getPhone(subId);
1859 if (phone != null) {
1860 return phone.getServiceState().getDataNetworkType();
1861 } else {
1862 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1863 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001864 }
1865
1866 /**
Wink Saville36469e72014-06-11 15:17:00 -07001867 * Returns the Voice network type for a subId
1868 */
1869 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001870 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage) {
1871 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
1872 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1873 }
1874
Sanket Padawe356d7632015-06-22 14:03:32 -07001875 final Phone phone = getPhone(subId);
1876 if (phone != null) {
1877 return phone.getServiceState().getVoiceNetworkType();
1878 } else {
1879 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1880 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001881 }
1882
1883 /**
1884 * @return true if a ICC card is present
1885 */
1886 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07001887 // FIXME Make changes to pass defaultSimId of type int
Amit Mahajana6fc2a82015-01-06 11:53:51 -08001888 return hasIccCardUsingSlotId(mSubscriptionController.getSlotId(getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07001889 }
1890
1891 /**
1892 * @return true if a ICC card is present for a slotId
1893 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001894 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001895 public boolean hasIccCardUsingSlotId(int slotId) {
Amit Mahajana6fc2a82015-01-06 11:53:51 -08001896 int subId[] = mSubscriptionController.getSubIdUsingSlotId(slotId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001897 final Phone phone = getPhone(subId[0]);
1898 if (subId != null && phone != null) {
1899 return phone.getIccCard().hasIccCard();
Amit Mahajana6fc2a82015-01-06 11:53:51 -08001900 } else {
1901 return false;
1902 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001903 }
1904
1905 /**
1906 * Return if the current radio is LTE on CDMA. This
1907 * is a tri-state return value as for a period of time
1908 * the mode may be unknown.
1909 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001910 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001911 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08001912 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001913 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001914 @Override
1915 public int getLteOnCdmaMode(String callingPackage) {
1916 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001917 }
1918
Sanket Padawe356d7632015-06-22 14:03:32 -07001919 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001920 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage) {
1921 if (!canReadPhoneState(callingPackage, "getLteOnCdmaModeForSubscriber")) {
1922 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
1923 }
1924
Sanket Padawe356d7632015-06-22 14:03:32 -07001925 final Phone phone = getPhone(subId);
1926 if (phone == null) {
1927 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
1928 } else {
1929 return phone.getLteOnCdmaMode();
1930 }
Wink Saville36469e72014-06-11 15:17:00 -07001931 }
1932
1933 public void setPhone(Phone phone) {
1934 mPhone = phone;
1935 }
1936
1937 /**
1938 * {@hide}
1939 * Returns Default subId, 0 in the case of single standby.
1940 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001941 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001942 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07001943 }
1944
Wink Savilleb564aae2014-10-23 10:18:09 -07001945 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001946 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001947 }
Ihab Awadf2177b72013-11-25 13:33:23 -08001948
1949 /**
1950 * @see android.telephony.TelephonyManager.WifiCallingChoices
1951 */
1952 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08001953 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
1954 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08001955 }
1956
1957 /**
1958 * @see android.telephony.TelephonyManager.WifiCallingChoices
1959 */
1960 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08001961 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
1962 Settings.System.putInt(mPhone.getContext().getContentResolver(),
1963 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08001964 }
1965
Sailesh Nepald1e68152013-12-12 19:08:02 -08001966 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07001967 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08001968 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08001969 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08001970
Shishir Agrawal566b7612013-10-28 14:41:00 -07001971 @Override
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001972 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(String AID) {
Junda Liua2e36012014-07-09 18:30:01 -07001973 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001974
1975 if (DBG) log("iccOpenLogicalChannel: " + AID);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001976 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
1977 CMD_OPEN_CHANNEL, AID);
1978 if (DBG) log("iccOpenLogicalChannel: " + response);
1979 return response;
Shishir Agrawal566b7612013-10-28 14:41:00 -07001980 }
1981
1982 @Override
1983 public boolean iccCloseLogicalChannel(int channel) {
Junda Liua2e36012014-07-09 18:30:01 -07001984 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001985
1986 if (DBG) log("iccCloseLogicalChannel: " + channel);
1987 if (channel < 0) {
1988 return false;
1989 }
Jake Hambye994d462014-02-03 13:10:13 -08001990 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07001991 if (DBG) log("iccCloseLogicalChannel: " + success);
1992 return success;
1993 }
1994
1995 @Override
1996 public String iccTransmitApduLogicalChannel(int channel, int cla,
1997 int command, int p1, int p2, int p3, String data) {
Junda Liua2e36012014-07-09 18:30:01 -07001998 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001999
2000 if (DBG) {
2001 log("iccTransmitApduLogicalChannel: chnl=" + channel + " cla=" + cla +
2002 " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
2003 " data=" + data);
2004 }
2005
2006 if (channel < 0) {
2007 return "";
2008 }
2009
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002010 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Shishir Agrawal566b7612013-10-28 14:41:00 -07002011 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data));
2012 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
2013
Shishir Agrawal566b7612013-10-28 14:41:00 -07002014 // Append the returned status code to the end of the response payload.
2015 String s = Integer.toHexString(
2016 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002017 if (response.payload != null) {
2018 s = IccUtils.bytesToHexString(response.payload) + s;
2019 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07002020 return s;
2021 }
Jake Hambye994d462014-02-03 13:10:13 -08002022
Evan Charltonc66da362014-05-16 14:06:40 -07002023 @Override
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002024 public String iccTransmitApduBasicChannel(int cla, int command, int p1, int p2,
2025 int p3, String data) {
2026 enforceModifyPermissionOrCarrierPrivilege();
2027
2028 if (DBG) {
2029 log("iccTransmitApduBasicChannel: cla=" + cla + " cmd=" + command + " p1="
2030 + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
2031 }
2032
2033 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
2034 new IccAPDUArgument(0, cla, command, p1, p2, p3, data));
2035 if (DBG) log("iccTransmitApduBasicChannel: " + response);
2036
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002037 // Append the returned status code to the end of the response payload.
2038 String s = Integer.toHexString(
2039 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002040 if (response.payload != null) {
2041 s = IccUtils.bytesToHexString(response.payload) + s;
2042 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002043 return s;
2044 }
2045
2046 @Override
2047 public byte[] iccExchangeSimIO(int fileID, int command, int p1, int p2, int p3,
2048 String filePath) {
2049 enforceModifyPermissionOrCarrierPrivilege();
2050
2051 if (DBG) {
2052 log("Exchange SIM_IO " + fileID + ":" + command + " " +
2053 p1 + " " + p2 + " " + p3 + ":" + filePath);
2054 }
2055
2056 IccIoResult response =
2057 (IccIoResult)sendRequest(CMD_EXCHANGE_SIM_IO,
Yong Jiang3edf3782014-10-03 13:23:28 -05002058 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath));
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002059
2060 if (DBG) {
2061 log("Exchange SIM_IO [R]" + response);
2062 }
2063
2064 byte[] result = null;
2065 int length = 2;
2066 if (response.payload != null) {
2067 length = 2 + response.payload.length;
2068 result = new byte[length];
2069 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
2070 } else {
2071 result = new byte[length];
2072 }
2073
2074 result[length - 1] = (byte) response.sw2;
2075 result[length - 2] = (byte) response.sw1;
2076 return result;
2077 }
2078
2079 @Override
Evan Charltonc66da362014-05-16 14:06:40 -07002080 public String sendEnvelopeWithStatus(String content) {
Junda Liua2e36012014-07-09 18:30:01 -07002081 enforceModifyPermissionOrCarrierPrivilege();
Evan Charltonc66da362014-05-16 14:06:40 -07002082
2083 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content);
2084 if (response.payload == null) {
2085 return "";
2086 }
2087
2088 // Append the returned status code to the end of the response payload.
2089 String s = Integer.toHexString(
2090 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
2091 s = IccUtils.bytesToHexString(response.payload) + s;
2092 return s;
2093 }
2094
Jake Hambye994d462014-02-03 13:10:13 -08002095 /**
2096 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2097 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2098 *
2099 * @param itemID the ID of the item to read
2100 * @return the NV item as a String, or null on error.
2101 */
2102 @Override
2103 public String nvReadItem(int itemID) {
Junda Liua2e36012014-07-09 18:30:01 -07002104 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08002105 if (DBG) log("nvReadItem: item " + itemID);
2106 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
2107 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
2108 return value;
2109 }
2110
2111 /**
2112 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2113 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2114 *
2115 * @param itemID the ID of the item to read
2116 * @param itemValue the value to write, as a String
2117 * @return true on success; false on any failure
2118 */
2119 @Override
2120 public boolean nvWriteItem(int itemID, String itemValue) {
Junda Liua2e36012014-07-09 18:30:01 -07002121 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08002122 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
2123 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
2124 new Pair<Integer, String>(itemID, itemValue));
2125 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
2126 return success;
2127 }
2128
2129 /**
2130 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
2131 * Used for device configuration by some CDMA operators.
2132 *
2133 * @param preferredRoamingList byte array containing the new PRL
2134 * @return true on success; false on any failure
2135 */
2136 @Override
2137 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Junda Liua2e36012014-07-09 18:30:01 -07002138 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08002139 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
2140 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
2141 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
2142 return success;
2143 }
2144
2145 /**
2146 * Perform the specified type of NV config reset.
2147 * Used for device configuration by some CDMA operators.
2148 *
2149 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
2150 * @return true on success; false on any failure
2151 */
2152 @Override
2153 public boolean nvResetConfig(int resetType) {
Junda Liua2e36012014-07-09 18:30:01 -07002154 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08002155 if (DBG) log("nvResetConfig: type " + resetType);
2156 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
2157 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
2158 return success;
2159 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002160
2161 /**
Wink Saville36469e72014-06-11 15:17:00 -07002162 * {@hide}
2163 * Returns Default sim, 0 in the case of single standby.
2164 */
2165 public int getDefaultSim() {
2166 //TODO Need to get it from Telephony Devcontroller
2167 return 0;
2168 }
2169
Svet Ganovb320e182015-04-16 12:30:10 -07002170 public String[] getPcscfAddress(String apnType, String callingPackage) {
2171 if (!canReadPhoneState(callingPackage, "getPcscfAddress")) {
2172 return new String[0];
2173 }
2174
2175
ram87fca6f2014-07-18 18:58:44 +05302176 return mPhone.getPcscfAddress(apnType);
Wink Saville36469e72014-06-11 15:17:00 -07002177 }
2178
2179 public void setImsRegistrationState(boolean registered) {
2180 enforceModifyPermission();
2181 mPhone.setImsRegistrationState(registered);
2182 }
2183
2184 /**
Stuart Scott54788802015-03-30 13:18:01 -07002185 * Set the network selection mode to automatic.
2186 *
2187 */
2188 @Override
2189 public void setNetworkSelectionModeAutomatic(int subId) {
2190 enforceModifyPermissionOrCarrierPrivilege();
2191 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
2192 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
2193 }
2194
2195 /**
Shishir Agrawal302c8692015-06-19 13:49:39 -07002196 * Set the network selection mode to manual with the selected carrier.
2197 */
2198 @Override
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002199 public boolean setNetworkSelectionModeManual(int subId, OperatorInfo operator,
2200 boolean persistSelection) {
Shishir Agrawal302c8692015-06-19 13:49:39 -07002201 enforceModifyPermissionOrCarrierPrivilege();
2202 if (DBG) log("setNetworkSelectionModeManual: subId:" + subId + " operator:" + operator);
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002203 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operator,
2204 persistSelection);
2205 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002206 }
2207
2208 /**
2209 * Scans for available networks.
2210 */
2211 @Override
2212 public CellNetworkScanResult getCellNetworkScanResults(int subId) {
2213 enforceModifyPermissionOrCarrierPrivilege();
2214 if (DBG) log("getCellNetworkScanResults: subId " + subId);
2215 CellNetworkScanResult result = (CellNetworkScanResult) sendRequest(
2216 CMD_PERFORM_NETWORK_SCAN, null, subId);
2217 return result;
2218 }
2219
2220 /**
Junda Liu84d15a22014-07-02 11:21:04 -07002221 * Get the calculated preferred network type.
2222 * Used for debugging incorrect network type.
2223 *
2224 * @return the preferred network type, defined in RILConstants.java.
2225 */
2226 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002227 public int getCalculatedPreferredNetworkType(String callingPackage) {
2228 if (!canReadPhoneState(callingPackage, "getCalculatedPreferredNetworkType")) {
2229 return RILConstants.PREFERRED_NETWORK_MODE;
2230 }
2231
Amit Mahajan43330e02014-11-18 11:54:45 -08002232 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext(), 0); // wink FIXME: need to get SubId from somewhere.
Junda Liu84d15a22014-07-02 11:21:04 -07002233 }
2234
2235 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08002236 * Get the preferred network type.
2237 * Used for device configuration by some CDMA operators.
2238 *
2239 * @return the preferred network type, defined in RILConstants.java.
2240 */
2241 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002242 public int getPreferredNetworkType(int subId) {
Junda Liua2e36012014-07-09 18:30:01 -07002243 enforceModifyPermissionOrCarrierPrivilege();
Jake Hamby7c27be32014-03-03 13:25:59 -08002244 if (DBG) log("getPreferredNetworkType");
Stuart Scott54788802015-03-30 13:18:01 -07002245 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002246 int networkType = (result != null ? result[0] : -1);
2247 if (DBG) log("getPreferredNetworkType: " + networkType);
2248 return networkType;
2249 }
2250
2251 /**
2252 * Set the preferred network type.
2253 * Used for device configuration by some CDMA operators.
2254 *
2255 * @param networkType the preferred network type, defined in RILConstants.java.
2256 * @return true on success; false on any failure.
2257 */
2258 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002259 public boolean setPreferredNetworkType(int subId, int networkType) {
Junda Liua2e36012014-07-09 18:30:01 -07002260 enforceModifyPermissionOrCarrierPrivilege();
Stuart Scott54788802015-03-30 13:18:01 -07002261 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
2262 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002263 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
Junda Liu80bc0d12014-07-14 16:36:44 -07002264 if (success) {
2265 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
Stuart Scott54788802015-03-30 13:18:01 -07002266 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
Junda Liu80bc0d12014-07-14 16:36:44 -07002267 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002268 return success;
2269 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002270
2271 /**
Junda Liu475951f2014-11-07 16:45:03 -08002272 * Check TETHER_DUN_REQUIRED and TETHER_DUN_APN settings, net.tethering.noprovisioning
2273 * SystemProperty, and config_tether_apndata to decide whether DUN APN is required for
2274 * tethering.
2275 *
2276 * @return 0: Not required. 1: required. 2: Not set.
2277 * @hide
2278 */
2279 @Override
2280 public int getTetherApnRequired() {
2281 enforceModifyPermissionOrCarrierPrivilege();
2282 int dunRequired = Settings.Global.getInt(mPhone.getContext().getContentResolver(),
2283 Settings.Global.TETHER_DUN_REQUIRED, 2);
2284 // If not set, check net.tethering.noprovisioning, TETHER_DUN_APN setting and
2285 // config_tether_apndata.
2286 if (dunRequired == 2 && mPhone.hasMatchedTetherApnSetting()) {
2287 dunRequired = 1;
2288 }
2289 return dunRequired;
2290 }
2291
2292 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07002293 * Set mobile data enabled
2294 * Used by the user through settings etc to turn on/off mobile data
2295 *
2296 * @param enable {@code true} turn turn data on, else {@code false}
2297 */
2298 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002299 public void setDataEnabled(int subId, boolean enable) {
Robert Greenwalted86e582014-05-21 20:03:20 -07002300 enforceModifyPermission();
Wink Savillee7353bb2014-12-05 14:21:41 -08002301 int phoneId = mSubscriptionController.getPhoneId(subId);
2302 log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
2303 Phone phone = PhoneFactory.getPhone(phoneId);
2304 if (phone != null) {
2305 log("setDataEnabled: subId=" + subId + " enable=" + enable);
2306 phone.setDataEnabled(enable);
2307 } else {
2308 loge("setDataEnabled: no phone for subId=" + subId);
2309 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002310 }
2311
2312 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07002313 * Get whether mobile data is enabled.
2314 *
2315 * Note that this used to be available from ConnectivityService, gated by
2316 * ACCESS_NETWORK_STATE permission, so this will accept either that or
2317 * our MODIFY_PHONE_STATE.
Robert Greenwalted86e582014-05-21 20:03:20 -07002318 *
2319 * @return {@code true} if data is enabled else {@code false}
2320 */
2321 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002322 public boolean getDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07002323 try {
2324 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
2325 null);
2326 } catch (Exception e) {
2327 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE,
2328 null);
2329 }
Wink Savillee7353bb2014-12-05 14:21:41 -08002330 int phoneId = mSubscriptionController.getPhoneId(subId);
2331 log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
2332 Phone phone = PhoneFactory.getPhone(phoneId);
2333 if (phone != null) {
2334 boolean retVal = phone.getDataEnabled();
2335 log("getDataEnabled: subId=" + subId + " retVal=" + retVal);
2336 return retVal;
2337 } else {
2338 loge("getDataEnabled: no phone subId=" + subId + " retVal=false");
2339 return false;
2340 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002341 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002342
2343 @Override
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002344 public int getCarrierPrivilegeStatus() {
Shishir Agrawal21409252015-01-15 23:33:50 -08002345 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002346 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002347 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002348 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2349 }
2350 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07002351 mPhone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002352 }
Junda Liu29340342014-07-10 15:23:27 -07002353
2354 @Override
Zach Johnson50ecba32015-05-19 00:24:21 -07002355 public int checkCarrierPrivilegesForPackage(String pkgName) {
Shishir Agrawal21409252015-01-15 23:33:50 -08002356 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002357 if (card == null) {
2358 loge("checkCarrierPrivilegesForPackage: No UICC");
2359 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2360 }
Zach Johnson50ecba32015-05-19 00:24:21 -07002361 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgName);
2362 }
2363
2364 @Override
2365 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
2366 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2367 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2368 UiccCard card = UiccController.getInstance().getUiccCard(i);
2369 if (card == null) {
Jonathan Basseri7d320df2015-06-16 12:17:08 -07002370 // No UICC in that slot.
Zach Johnson50ecba32015-05-19 00:24:21 -07002371 continue;
2372 }
2373
2374 result = card.getCarrierPrivilegeStatus(
2375 mPhone.getContext().getPackageManager(), pkgName);
2376 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2377 break;
2378 }
2379 }
2380
2381 return result;
Junda Liu29340342014-07-10 15:23:27 -07002382 }
Derek Tan89e89d42014-07-08 17:00:10 -07002383
2384 @Override
Junda Liue64de782015-04-16 17:19:16 -07002385 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
2386 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
2387 loge("phoneId " + phoneId + " is not valid.");
2388 return null;
2389 }
2390 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002391 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002392 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002393 return null ;
2394 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002395 return card.getCarrierPackageNamesForIntent(
Svetoslav483aff72015-04-21 14:16:07 -07002396 mPhone.getContext().getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002397 }
2398
Amith Yamasani6e118872016-02-19 12:53:51 -08002399 @Override
2400 public List<String> getPackagesWithCarrierPrivileges() {
2401 PackageManager pm = mPhone.getContext().getPackageManager();
2402 List<String> privilegedPackages = new ArrayList<>();
2403 List<PackageInfo> packages = null;
2404 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2405 UiccCard card = UiccController.getInstance().getUiccCard(i);
2406 if (card == null) {
2407 // No UICC in that slot.
2408 continue;
2409 }
2410 if (card.hasCarrierPrivilegeRules()) {
2411 if (packages == null) {
2412 // Only check packages in user 0 for now
2413 packages = pm.getInstalledPackagesAsUser(
2414 PackageManager.MATCH_DISABLED_COMPONENTS
2415 | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS
2416 | PackageManager.GET_SIGNATURES, UserHandle.USER_SYSTEM);
2417 }
2418 for (int p = packages.size() - 1; p >= 0; p--) {
2419 PackageInfo pkgInfo = packages.get(p);
2420 if (pkgInfo != null && pkgInfo.packageName != null
2421 && card.getCarrierPrivilegeStatus(pkgInfo)
2422 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2423 privilegedPackages.add(pkgInfo.packageName);
2424 }
2425 }
2426 }
2427 }
2428 return privilegedPackages;
2429 }
2430
Wink Savilleb564aae2014-10-23 10:18:09 -07002431 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002432 final Phone phone = getPhone(subId);
2433 UiccCard card = phone == null ? null : phone.getUiccCard();
Derek Tan97ebb422014-09-05 16:55:38 -07002434 if (card == null) {
2435 loge("getIccId: No UICC");
2436 return null;
2437 }
2438 String iccId = card.getIccId();
2439 if (TextUtils.isEmpty(iccId)) {
2440 loge("getIccId: ICC ID is null or empty.");
2441 return null;
2442 }
2443 return iccId;
2444 }
2445
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002446 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002447 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
2448 String number) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002449 enforceCarrierPrivilege();
Derek Tan97ebb422014-09-05 16:55:38 -07002450
Jeff Sharkey85190e62014-12-05 09:40:12 -08002451 final String iccId = getIccId(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07002452 final Phone phone = getPhone(subId);
2453 if (phone == null) {
2454 return false;
2455 }
2456 final String subscriberId = phone.getSubscriberId();
Jeff Sharkey85190e62014-12-05 09:40:12 -08002457
2458 if (DBG_MERGE) {
2459 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
2460 + subscriberId + " to " + number);
2461 }
2462
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002463 if (TextUtils.isEmpty(iccId)) {
2464 return false;
Derek Tan97ebb422014-09-05 16:55:38 -07002465 }
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002466
Jeff Sharkey85190e62014-12-05 09:40:12 -08002467 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2468
2469 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002470 if (alphaTag == null) {
2471 editor.remove(alphaTagPrefKey);
2472 } else {
2473 editor.putString(alphaTagPrefKey, alphaTag);
2474 }
2475
Jeff Sharkey85190e62014-12-05 09:40:12 -08002476 // Record both the line number and IMSI for this ICCID, since we need to
2477 // track all merged IMSIs based on line number
2478 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2479 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002480 if (number == null) {
2481 editor.remove(numberPrefKey);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002482 editor.remove(subscriberPrefKey);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002483 } else {
2484 editor.putString(numberPrefKey, number);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002485 editor.putString(subscriberPrefKey, subscriberId);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002486 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002487
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002488 editor.commit();
2489 return true;
Derek Tan7226c842014-07-02 17:42:23 -07002490 }
2491
2492 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002493 public String getLine1NumberForDisplay(int subId, String callingPackage) {
Makoto Onukifee69342015-06-29 14:44:50 -07002494 // This is open to apps with WRITE_SMS.
2495 if (!canReadPhoneNumber(callingPackage, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08002496 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07002497 return null;
2498 }
Derek Tan97ebb422014-09-05 16:55:38 -07002499
2500 String iccId = getIccId(subId);
2501 if (iccId != null) {
2502 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
Amit Mahajan9cf11512015-11-09 11:40:48 -08002503 if (DBG_MERGE) {
2504 log("getLine1NumberForDisplay returning " +
2505 mTelephonySharedPreferences.getString(numberPrefKey, null));
2506 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002507 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002508 }
Amit Mahajan9cf11512015-11-09 11:40:48 -08002509 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
Derek Tan97ebb422014-09-05 16:55:38 -07002510 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002511 }
2512
2513 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002514 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
2515 if (!canReadPhoneState(callingPackage, "getLine1AlphaTagForDisplay")) {
2516 return null;
2517 }
Derek Tan97ebb422014-09-05 16:55:38 -07002518
2519 String iccId = getIccId(subId);
2520 if (iccId != null) {
2521 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002522 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002523 }
Derek Tan97ebb422014-09-05 16:55:38 -07002524 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002525 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002526
2527 @Override
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002528 public String[] getMergedSubscriberIds(String callingPackage) {
2529 if (!canReadPhoneState(callingPackage, "getMergedSubscriberIds")) {
2530 return null;
2531 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002532 final Context context = mPhone.getContext();
2533 final TelephonyManager tele = TelephonyManager.from(context);
2534 final SubscriptionManager sub = SubscriptionManager.from(context);
2535
2536 // Figure out what subscribers are currently active
2537 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002538 // Clear calling identity, when calling TelephonyManager, because callerUid must be
2539 // the process, where TelephonyManager was instantiated. Otherwise AppOps check will fail.
2540 final long identity = Binder.clearCallingIdentity();
2541 try {
2542 final int[] subIds = sub.getActiveSubscriptionIdList();
2543 for (int subId : subIds) {
2544 activeSubscriberIds.add(tele.getSubscriberId(subId));
2545 }
2546 } finally {
2547 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002548 }
2549
2550 // First pass, find a number override for an active subscriber
2551 String mergeNumber = null;
2552 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
2553 for (String key : prefs.keySet()) {
2554 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
2555 final String subscriberId = (String) prefs.get(key);
2556 if (activeSubscriberIds.contains(subscriberId)) {
2557 final String iccId = key.substring(PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
2558 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2559 mergeNumber = (String) prefs.get(numberKey);
2560 if (DBG_MERGE) {
2561 Slog.d(LOG_TAG, "Found line number " + mergeNumber
2562 + " for active subscriber " + subscriberId);
2563 }
2564 if (!TextUtils.isEmpty(mergeNumber)) {
2565 break;
2566 }
2567 }
2568 }
2569 }
2570
2571 // Shortcut when no active merged subscribers
2572 if (TextUtils.isEmpty(mergeNumber)) {
2573 return null;
2574 }
2575
2576 // Second pass, find all subscribers under that line override
2577 final ArraySet<String> result = new ArraySet<>();
2578 for (String key : prefs.keySet()) {
2579 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
2580 final String number = (String) prefs.get(key);
2581 if (mergeNumber.equals(number)) {
2582 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
2583 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
2584 final String subscriberId = (String) prefs.get(subscriberKey);
2585 if (!TextUtils.isEmpty(subscriberId)) {
2586 result.add(subscriberId);
2587 }
2588 }
2589 }
2590 }
2591
2592 final String[] resultArray = result.toArray(new String[result.size()]);
2593 Arrays.sort(resultArray);
2594 if (DBG_MERGE) {
2595 Slog.d(LOG_TAG, "Found subscribers " + Arrays.toString(resultArray) + " after merge");
2596 }
2597 return resultArray;
2598 }
2599
2600 @Override
Shishir Agrawala3dfd752014-09-04 13:25:42 -07002601 public boolean setOperatorBrandOverride(String brand) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002602 enforceCarrierPrivilege();
Shishir Agrawala3dfd752014-09-04 13:25:42 -07002603 return mPhone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002604 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05002605
2606 @Override
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002607 public boolean setRoamingOverride(List<String> gsmRoamingList,
2608 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
2609 List<String> cdmaNonRoamingList) {
2610 enforceCarrierPrivilege();
2611 return mPhone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
2612 cdmaNonRoamingList);
2613 }
2614
2615 @Override
Steven Liu4bf01bc2014-07-17 11:05:29 -05002616 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
2617 enforceModifyPermission();
2618
2619 int returnValue = 0;
2620 try {
2621 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
2622 if(result.exception == null) {
2623 if (result.result != null) {
2624 byte[] responseData = (byte[])(result.result);
2625 if(responseData.length > oemResp.length) {
2626 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
2627 responseData.length + "bytes. Buffer Size is " +
2628 oemResp.length + "bytes.");
2629 }
2630 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
2631 returnValue = responseData.length;
2632 }
2633 } else {
2634 CommandException ex = (CommandException) result.exception;
2635 returnValue = ex.getCommandError().ordinal();
2636 if(returnValue > 0) returnValue *= -1;
2637 }
2638 } catch (RuntimeException e) {
2639 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
2640 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
2641 if(returnValue > 0) returnValue *= -1;
2642 }
2643
2644 return returnValue;
2645 }
Wink Saville5d475dd2014-10-17 15:00:58 -07002646
2647 @Override
2648 public void setRadioCapability(RadioAccessFamily[] rafs) {
2649 try {
2650 ProxyController.getInstance().setRadioCapability(rafs);
2651 } catch (RuntimeException e) {
2652 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
2653 }
2654 }
2655
2656 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002657 public int getRadioAccessFamily(int phoneId, String callingPackage) {
2658 if (!canReadPhoneState(callingPackage, "getRadioAccessFamily")) {
2659 return RadioAccessFamily.RAF_UNKNOWN;
2660 }
2661
Wink Saville5d475dd2014-10-17 15:00:58 -07002662 return ProxyController.getInstance().getRadioAccessFamily(phoneId);
2663 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002664
2665 @Override
2666 public void enableVideoCalling(boolean enable) {
2667 enforceModifyPermission();
Tyler Gunnfdd69de2015-12-04 21:24:38 -08002668 ImsManager.setVtSetting(mPhone.getContext(), enable);
Andrew Leedf14ead2014-10-17 14:22:52 -07002669 }
2670
2671 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002672 public boolean isVideoCallingEnabled(String callingPackage) {
2673 if (!canReadPhoneState(callingPackage, "isVideoCallingEnabled")) {
2674 return false;
2675 }
2676
Andrew Lee77527ac2014-10-21 16:57:39 -07002677 // Check the user preference and the system-level IMS setting. Even if the user has
2678 // enabled video calling, if IMS is disabled we aren't able to support video calling.
2679 // In the long run, we may instead need to check if there exists a connection service
2680 // which can support video calling.
Andrew Lee312e8172014-10-23 17:01:36 -07002681 return ImsManager.isVtEnabledByPlatform(mPhone.getContext())
2682 && ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
Tyler Gunnfdd69de2015-12-04 21:24:38 -08002683 && ImsManager.isVtEnabledByUser(mPhone.getContext());
Andrew Leedf14ead2014-10-17 14:22:52 -07002684 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002685
Andrew Leea1239f22015-03-02 17:44:07 -08002686 @Override
2687 public boolean canChangeDtmfToneLength() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07002688 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08002689 }
2690
2691 @Override
2692 public boolean isWorldPhone() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07002693 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08002694 }
2695
Andrew Lee9431b832015-03-09 18:46:45 -07002696 @Override
2697 public boolean isTtyModeSupported() {
2698 TelecomManager telecomManager = TelecomManager.from(mPhone.getContext());
2699 TelephonyManager telephonyManager =
2700 (TelephonyManager) mPhone.getContext().getSystemService(Context.TELEPHONY_SERVICE);
2701 return !telephonyManager.isMultiSimEnabled() && telecomManager.isTtySupported();
2702 }
2703
2704 @Override
2705 public boolean isHearingAidCompatibilitySupported() {
2706 return mPhone.getContext().getResources().getBoolean(R.bool.hac_enabled);
2707 }
2708
Sanket Padawe7310cc72015-01-14 09:53:20 -08002709 /**
2710 * Returns the unique device ID of phone, for example, the IMEI for
2711 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
2712 *
2713 * <p>Requires Permission:
2714 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
2715 */
2716 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002717 public String getDeviceId(String callingPackage) {
2718 if (!canReadPhoneState(callingPackage, "getDeviceId")) {
2719 return null;
2720 }
2721
Sanket Padawe7310cc72015-01-14 09:53:20 -08002722 final Phone phone = PhoneFactory.getPhone(0);
2723 if (phone != null) {
2724 return phone.getDeviceId();
2725 } else {
2726 return null;
2727 }
2728 }
2729
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002730 /*
2731 * {@hide}
2732 * Returns the IMS Registration Status
2733 */
Santos Cordon7a1885b2015-02-03 11:15:19 -08002734 @Override
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002735 public boolean isImsRegistered() {
2736 return mPhone.isImsRegistered();
2737 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08002738
2739 @Override
2740 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
2741 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
2742 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07002743
Nathan Haroldc55097a2015-03-11 18:14:50 -07002744 /*
2745 * {@hide}
2746 * Returns the IMS Registration Status
2747 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002748 public boolean isWifiCallingAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07002749 return mPhone.isWifiCallingEnabled();
2750 }
2751
2752 /*
2753 * {@hide}
2754 * Returns the IMS Registration Status
2755 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002756 public boolean isVolteAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07002757 return mPhone.isVolteEnabled();
2758 }
Svet Ganovb320e182015-04-16 12:30:10 -07002759
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002760 /*
2761 * {@hide} Returns the IMS Registration Status
2762 */
2763 public boolean isVideoTelephonyAvailable() {
2764 return mPhone.isVideoEnabled();
2765 }
2766
Svet Ganovb320e182015-04-16 12:30:10 -07002767 private boolean canReadPhoneState(String callingPackage, String message) {
Etan Cohen921655c2015-06-24 13:54:50 -07002768 try {
Amit Mahajan83ea23b2015-07-30 16:05:11 -07002769 mApp.enforceCallingOrSelfPermission(
Etan Cohen921655c2015-06-24 13:54:50 -07002770 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE, message);
2771
Amit Mahajan83ea23b2015-07-30 16:05:11 -07002772 // SKIP checking for run-time permission since caller or self has PRIVILEDGED permission
Etan Cohen921655c2015-06-24 13:54:50 -07002773 return true;
Amit Mahajan785783f2015-06-29 10:36:50 -07002774 } catch (SecurityException e) {
2775 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
2776 message);
Etan Cohen921655c2015-06-24 13:54:50 -07002777 }
Svet Ganovb320e182015-04-16 12:30:10 -07002778
2779 if (mAppOps.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(),
2780 callingPackage) != AppOpsManager.MODE_ALLOWED) {
2781 return false;
2782 }
2783
2784 return true;
2785 }
Stuart Scott8eef64f2015-04-08 15:13:54 -07002786
Makoto Onukifee69342015-06-29 14:44:50 -07002787 /**
Amit Mahajanb9b49782015-09-15 18:16:32 -07002788 * Besides READ_PHONE_STATE, WRITE_SMS and READ_SMS also allow apps to get phone numbers.
Makoto Onukifee69342015-06-29 14:44:50 -07002789 */
2790 private boolean canReadPhoneNumber(String callingPackage, String message) {
Makoto Onukie4072d12015-08-03 15:12:23 -07002791 // Default SMS app can always read it.
2792 if (mAppOps.noteOp(AppOpsManager.OP_WRITE_SMS,
2793 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED) {
2794 return true;
2795 }
2796 try {
2797 return canReadPhoneState(callingPackage, message);
Amit Mahajanb9b49782015-09-15 18:16:32 -07002798 } catch (SecurityException readPhoneStateSecurityException) {
2799 try {
2800 // Can be read with READ_SMS too.
2801 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_SMS, message);
2802 return mAppOps.noteOp(AppOpsManager.OP_READ_SMS,
2803 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED;
2804 } catch (SecurityException readSmsSecurityException) {
2805 // Throw exception with message including both READ_PHONE_STATE and READ_SMS
2806 // permissions
2807 throw new SecurityException(message + ": Neither user " + Binder.getCallingUid() +
2808 " nor current process has " + android.Manifest.permission.READ_PHONE_STATE +
2809 " or " + android.Manifest.permission.READ_SMS + ".");
2810 }
Makoto Onukie4072d12015-08-03 15:12:23 -07002811 }
Makoto Onukifee69342015-06-29 14:44:50 -07002812 }
2813
Stuart Scott8eef64f2015-04-08 15:13:54 -07002814 @Override
2815 public void factoryReset(int subId) {
2816 enforceConnectivityInternalPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07002817 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
2818 return;
2819 }
2820
Svet Ganovcc087f82015-05-12 20:35:54 -07002821 final long identity = Binder.clearCallingIdentity();
2822 try {
Stuart Scott981d8582015-04-21 14:09:50 -07002823 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
2824 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Svet Ganovcc087f82015-05-12 20:35:54 -07002825 // Enable data
2826 setDataEnabled(subId, true);
2827 // Set network selection mode to automatic
2828 setNetworkSelectionModeAutomatic(subId);
2829 // Set preferred mobile network type to the best available
2830 setPreferredNetworkType(subId, Phone.PREFERRED_NT_MODE);
2831 // Turn off roaming
2832 SubscriptionManager.from(mApp).setDataRoaming(0, subId);
2833 }
2834 } finally {
2835 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07002836 }
2837 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01002838
2839 @Override
2840 public String getLocaleFromDefaultSim() {
2841 // We query all subscriptions instead of just the active ones, because
2842 // this might be called early on in the provisioning flow when the
2843 // subscriptions potentially aren't active yet.
2844 final List<SubscriptionInfo> slist = getAllSubscriptionInfoList();
2845 if (slist == null || slist.isEmpty()) {
2846 return null;
2847 }
2848
2849 // This function may be called very early, say, from the setup wizard, at
2850 // which point we won't have a default subscription set. If that's the case
2851 // we just choose the first, which will be valid in "most cases".
2852 final int defaultSubId = getDefaultSubscription();
2853 SubscriptionInfo info = null;
2854 if (defaultSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
2855 info = slist.get(0);
2856 } else {
2857 for (SubscriptionInfo item : slist) {
2858 if (item.getSubscriptionId() == defaultSubId) {
2859 info = item;
2860 break;
2861 }
2862 }
2863
2864 if (info == null) {
2865 return null;
2866 }
2867 }
2868
2869 // Try and fetch the locale from the carrier properties or from the SIM language
2870 // preferences (EF-PL and EF-LI)...
Tony Hill183b2de2015-06-24 14:53:58 +01002871 final int mcc = info.getMcc();
Narayan Kamath1c496c22015-04-16 14:40:19 +01002872 final Phone defaultPhone = getPhone(info.getSubscriptionId());
Narayan Kamath011676f2015-07-29 12:04:08 +01002873 String simLanguage = null;
Narayan Kamath1c496c22015-04-16 14:40:19 +01002874 if (defaultPhone != null) {
2875 final Locale localeFromDefaultSim = defaultPhone.getLocaleFromSimAndCarrierPrefs();
2876 if (localeFromDefaultSim != null) {
Narayan Kamath011676f2015-07-29 12:04:08 +01002877 if (!localeFromDefaultSim.getCountry().isEmpty()) {
Tony Hill183b2de2015-06-24 14:53:58 +01002878 if (DBG) log("Using locale from default SIM:" + localeFromDefaultSim);
2879 return localeFromDefaultSim.toLanguageTag();
Narayan Kamath011676f2015-07-29 12:04:08 +01002880 } else {
2881 simLanguage = localeFromDefaultSim.getLanguage();
Tony Hill183b2de2015-06-24 14:53:58 +01002882 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01002883 }
2884 }
2885
Narayan Kamath011676f2015-07-29 12:04:08 +01002886 // The SIM language preferences only store a language (e.g. fr = French), not an
2887 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
2888 // the SIM and carrier preferences does not include a country we add the country
2889 // determined from the SIM MCC to provide an exact locale.
2890 final Locale mccLocale = MccTable.getLocaleFromMcc(mPhone.getContext(), mcc, simLanguage);
Tony Hill183b2de2015-06-24 14:53:58 +01002891 if (mccLocale != null) {
2892 if (DBG) log("No locale from default SIM, using mcc locale:" + mccLocale);
2893 return mccLocale.toLanguageTag();
Narayan Kamath1c496c22015-04-16 14:40:19 +01002894 }
2895
Tony Hill183b2de2015-06-24 14:53:58 +01002896 if (DBG) log("No locale found - returning null");
Narayan Kamath1c496c22015-04-16 14:40:19 +01002897 return null;
2898 }
2899
2900 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
2901 final long identity = Binder.clearCallingIdentity();
2902 try {
2903 return mSubscriptionController.getAllSubInfoList(
2904 mPhone.getContext().getOpPackageName());
2905 } finally {
2906 Binder.restoreCallingIdentity(identity);
2907 }
2908 }
2909
2910 private List<SubscriptionInfo> getActiveSubscriptionInfoList() {
2911 final long identity = Binder.clearCallingIdentity();
2912 try {
2913 return mSubscriptionController.getActiveSubscriptionInfoList(
2914 mPhone.getContext().getOpPackageName());
2915 } finally {
2916 Binder.restoreCallingIdentity(identity);
2917 }
2918 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07002919
2920 /**
2921 * {@hide}
2922 * Returns the modem stats
2923 */
2924 @Override
2925 public ModemActivityInfo getModemActivityInfo() {
2926 return (ModemActivityInfo) sendRequest(CMD_GET_MODEM_ACTIVITY_INFO, null);
2927 }
Jack Yu85bd38a2015-11-09 11:34:32 -08002928
2929 /**
2930 * {@hide}
2931 * Returns the service state information on specified subscription.
2932 */
2933 @Override
2934 public ServiceState getServiceStateForSubscriber(int subId, String callingPackage) {
2935
2936 if (!canReadPhoneState(callingPackage, "getServiceStateForSubscriber")) {
2937 return null;
2938 }
2939
2940 final Phone phone = getPhone(subId);
2941 if (phone == null) {
2942 return null;
2943 }
2944
2945 return phone.getServiceState();
2946 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08002947
2948 /**
2949 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
2950 *
2951 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
2952 * voicemail ringtone.
2953 * @return The URI for the ringtone to play when receiving a voicemail from a specific
2954 * PhoneAccount.
2955 */
2956 @Override
2957 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
2958 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
2959 if (phone == null) {
2960 return null;
2961 }
2962
2963 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone);
2964 }
2965
2966 /**
2967 * Returns whether vibration is set for voicemail notification in Phone settings.
2968 *
2969 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
2970 * voicemail vibration setting.
2971 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
2972 */
2973 @Override
2974 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
2975 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
2976 if (phone == null) {
2977 return false;
2978 }
2979
2980 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone);
2981 }
2982
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002983}