blob: 0539d787c2882daf5eac5bdcaa8933baef05a71b [file] [log] [blame]
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.phone;
18
19import android.app.ActivityManager;
20import android.app.AppOpsManager;
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -070021import android.content.ComponentName;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070022import android.content.Context;
23import android.content.Intent;
Derek Tan97ebb422014-09-05 16:55:38 -070024import android.content.SharedPreferences;
Shishir Agrawal60f9c952014-06-23 12:00:43 -070025import android.content.pm.PackageManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070026import android.net.Uri;
27import android.os.AsyncResult;
28import android.os.Binder;
29import android.os.Bundle;
30import android.os.Handler;
31import android.os.Looper;
32import android.os.Message;
33import android.os.Process;
34import android.os.ServiceManager;
35import android.os.UserHandle;
Stuart Scott981d8582015-04-21 14:09:50 -070036import android.os.UserManager;
Derek Tan97ebb422014-09-05 16:55:38 -070037import android.preference.PreferenceManager;
Ihab Awadf2177b72013-11-25 13:33:23 -080038import android.provider.Settings;
Santos Cordon7a1885b2015-02-03 11:15:19 -080039import android.telecom.PhoneAccount;
Nancy Chen31f9ba12016-01-06 11:42:12 -080040import android.telecom.PhoneAccountHandle;
Andrew Lee9431b832015-03-09 18:46:45 -070041import android.telecom.TelecomManager;
Junda Liu12f7d802015-05-01 12:06:44 -070042import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070043import android.telephony.CellInfo;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -070044import android.telephony.IccOpenLogicalChannelResponse;
Jake Hambye994d462014-02-03 13:10:13 -080045import android.telephony.NeighboringCellInfo;
Wink Saville5d475dd2014-10-17 15:00:58 -070046import android.telephony.RadioAccessFamily;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070047import android.telephony.ServiceState;
Wink Saville0f3b5fc2014-11-11 08:40:49 -080048import android.telephony.SubscriptionInfo;
Jeff Sharkey85190e62014-12-05 09:40:12 -080049import android.telephony.SubscriptionManager;
Ihab Awadf2177b72013-11-25 13:33:23 -080050import android.telephony.TelephonyManager;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -070051import android.telephony.ModemActivityInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070052import android.text.TextUtils;
Jeff Sharkey85190e62014-12-05 09:40:12 -080053import android.util.ArrayMap;
54import android.util.ArraySet;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070055import android.util.Log;
Jake Hambye994d462014-02-03 13:10:13 -080056import android.util.Pair;
Jeff Sharkey85190e62014-12-05 09:40:12 -080057import android.util.Slog;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070058
Andrew Lee312e8172014-10-23 17:01:36 -070059import com.android.ims.ImsManager;
Shishir Agrawal566b7612013-10-28 14:41:00 -070060import com.android.internal.telephony.CallManager;
Shishir Agrawal302c8692015-06-19 13:49:39 -070061import com.android.internal.telephony.CellNetworkScanResult;
Shishir Agrawal566b7612013-10-28 14:41:00 -070062import com.android.internal.telephony.CommandException;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070063import com.android.internal.telephony.DefaultPhoneNotifier;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070064import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -080065import com.android.internal.telephony.IccCard;
Narayan Kamath1c496c22015-04-16 14:40:19 +010066import com.android.internal.telephony.MccTable;
Shishir Agrawal302c8692015-06-19 13:49:39 -070067import com.android.internal.telephony.OperatorInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070068import com.android.internal.telephony.Phone;
Wink Saville36469e72014-06-11 15:17:00 -070069import com.android.internal.telephony.PhoneFactory;
Wink Saville5d475dd2014-10-17 15:00:58 -070070import com.android.internal.telephony.ProxyController;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070071import com.android.internal.telephony.PhoneConstants;
Svet Ganovb320e182015-04-16 12:30:10 -070072import com.android.internal.telephony.RILConstants;
Wink Savilleac1bdfd2014-11-20 23:04:44 -080073import com.android.internal.telephony.SubscriptionController;
Shishir Agrawal566b7612013-10-28 14:41:00 -070074import com.android.internal.telephony.uicc.IccIoResult;
75import com.android.internal.telephony.uicc.IccUtils;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -070076import com.android.internal.telephony.uicc.UiccCard;
Shishir Agrawal566b7612013-10-28 14:41:00 -070077import com.android.internal.telephony.uicc.UiccController;
Jake Hambye994d462014-02-03 13:10:13 -080078import com.android.internal.util.HexDump;
Nancy Chen31f9ba12016-01-06 11:42:12 -080079import com.android.phone.settings.VoicemailNotificationSettingsUtil;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070080
Wink Saville36469e72014-06-11 15:17:00 -070081import static com.android.internal.telephony.PhoneConstants.SUBSCRIPTION_KEY;
82
Santos Cordon7d4ddf62013-07-10 11:58:08 -070083import java.util.ArrayList;
Jeff Sharkey85190e62014-12-05 09:40:12 -080084import java.util.Arrays;
Jake Hambye994d462014-02-03 13:10:13 -080085import java.util.List;
Narayan Kamath1c496c22015-04-16 14:40:19 +010086import java.util.Locale;
Jeff Sharkey85190e62014-12-05 09:40:12 -080087import java.util.Map;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070088
89/**
90 * Implementation of the ITelephony interface.
91 */
Santos Cordon117fee72014-05-16 17:56:12 -070092public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -070093 private static final String LOG_TAG = "PhoneInterfaceManager";
94 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
95 private static final boolean DBG_LOC = false;
Jeff Sharkey85190e62014-12-05 09:40:12 -080096 private static final boolean DBG_MERGE = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070097
98 // Message codes used with mMainThreadHandler
99 private static final int CMD_HANDLE_PIN_MMI = 1;
100 private static final int CMD_HANDLE_NEIGHBORING_CELL = 2;
101 private static final int EVENT_NEIGHBORING_CELL_DONE = 3;
102 private static final int CMD_ANSWER_RINGING_CALL = 4;
103 private static final int CMD_END_CALL = 5; // not used yet
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700104 private static final int CMD_TRANSMIT_APDU_LOGICAL_CHANNEL = 7;
105 private static final int EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE = 8;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700106 private static final int CMD_OPEN_CHANNEL = 9;
107 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
108 private static final int CMD_CLOSE_CHANNEL = 11;
109 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -0800110 private static final int CMD_NV_READ_ITEM = 13;
111 private static final int EVENT_NV_READ_ITEM_DONE = 14;
112 private static final int CMD_NV_WRITE_ITEM = 15;
113 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
114 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
115 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
116 private static final int CMD_NV_RESET_CONFIG = 19;
117 private static final int EVENT_NV_RESET_CONFIG_DONE = 20;
Jake Hamby7c27be32014-03-03 13:25:59 -0800118 private static final int CMD_GET_PREFERRED_NETWORK_TYPE = 21;
119 private static final int EVENT_GET_PREFERRED_NETWORK_TYPE_DONE = 22;
120 private static final int CMD_SET_PREFERRED_NETWORK_TYPE = 23;
121 private static final int EVENT_SET_PREFERRED_NETWORK_TYPE_DONE = 24;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800122 private static final int CMD_SEND_ENVELOPE = 25;
123 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Derek Tan6b088ee2014-09-05 14:15:18 -0700124 private static final int CMD_INVOKE_OEM_RIL_REQUEST_RAW = 27;
125 private static final int EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE = 28;
126 private static final int CMD_TRANSMIT_APDU_BASIC_CHANNEL = 29;
127 private static final int EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE = 30;
128 private static final int CMD_EXCHANGE_SIM_IO = 31;
129 private static final int EVENT_EXCHANGE_SIM_IO_DONE = 32;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800130 private static final int CMD_SET_VOICEMAIL_NUMBER = 33;
131 private static final int EVENT_SET_VOICEMAIL_NUMBER_DONE = 34;
Stuart Scott54788802015-03-30 13:18:01 -0700132 private static final int CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC = 35;
133 private static final int EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE = 36;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700134 private static final int CMD_GET_MODEM_ACTIVITY_INFO = 37;
135 private static final int EVENT_GET_MODEM_ACTIVITY_INFO_DONE = 38;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700136 private static final int CMD_PERFORM_NETWORK_SCAN = 39;
137 private static final int EVENT_PERFORM_NETWORK_SCAN_DONE = 40;
138 private static final int CMD_SET_NETWORK_SELECTION_MODE_MANUAL = 41;
139 private static final int EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE = 42;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700140
141 /** The singleton instance. */
142 private static PhoneInterfaceManager sInstance;
143
Wink Saville3ab207e2014-11-20 13:07:20 -0800144 private PhoneGlobals mApp;
145 private Phone mPhone;
146 private CallManager mCM;
Stuart Scott981d8582015-04-21 14:09:50 -0700147 private UserManager mUserManager;
Wink Saville3ab207e2014-11-20 13:07:20 -0800148 private AppOpsManager mAppOps;
149 private MainThreadHandler mMainThreadHandler;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800150 private SubscriptionController mSubscriptionController;
Wink Saville3ab207e2014-11-20 13:07:20 -0800151 private SharedPreferences mTelephonySharedPreferences;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700152
Derek Tan97ebb422014-09-05 16:55:38 -0700153 private static final String PREF_CARRIERS_ALPHATAG_PREFIX = "carrier_alphtag_";
154 private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
Jeff Sharkey85190e62014-12-05 09:40:12 -0800155 private static final String PREF_CARRIERS_SUBSCRIBER_PREFIX = "carrier_subscriber_";
Derek Tan89e89d42014-07-08 17:00:10 -0700156
157 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700158 * A request object to use for transmitting data to an ICC.
159 */
160 private static final class IccAPDUArgument {
161 public int channel, cla, command, p1, p2, p3;
162 public String data;
163
164 public IccAPDUArgument(int channel, int cla, int command,
165 int p1, int p2, int p3, String data) {
166 this.channel = channel;
167 this.cla = cla;
168 this.command = command;
169 this.p1 = p1;
170 this.p2 = p2;
171 this.p3 = p3;
172 this.data = data;
173 }
174 }
175
176 /**
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700177 * A request object to use for transmitting data to an ICC.
178 */
179 private static final class ManualNetworkSelectionArgument {
180 public OperatorInfo operatorInfo;
181 public boolean persistSelection;
182
183 public ManualNetworkSelectionArgument(OperatorInfo operatorInfo, boolean persistSelection) {
184 this.operatorInfo = operatorInfo;
185 this.persistSelection = persistSelection;
186 }
187 }
188
189 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700190 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
191 * request after sending. The main thread will notify the request when it is complete.
192 */
193 private static final class MainThreadRequest {
194 /** The argument to use for the request */
195 public Object argument;
196 /** The result of the request that is run on the main thread */
197 public Object result;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800198 // The subscriber id that this request applies to. Defaults to
199 // SubscriptionManager.INVALID_SUBSCRIPTION_ID
200 public Integer subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700201
202 public MainThreadRequest(Object argument) {
203 this.argument = argument;
204 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800205
206 public MainThreadRequest(Object argument, Integer subId) {
207 this.argument = argument;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800208 if (subId != null) {
209 this.subId = subId;
210 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800211 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700212 }
213
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800214 private static final class IncomingThirdPartyCallArgs {
215 public final ComponentName component;
216 public final String callId;
217 public final String callerDisplayName;
218
219 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
220 String callerDisplayName) {
221 this.component = component;
222 this.callId = callId;
223 this.callerDisplayName = callerDisplayName;
224 }
225 }
226
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700227 /**
228 * A handler that processes messages on the main thread in the phone process. Since many
229 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
230 * inbound binder threads to the main thread in the phone process. The Binder thread
231 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
232 * on, which will be notified when the operation completes and will contain the result of the
233 * request.
234 *
235 * <p>If a MainThreadRequest object is provided in the msg.obj field,
236 * note that request.result must be set to something non-null for the calling thread to
237 * unblock.
238 */
239 private final class MainThreadHandler extends Handler {
240 @Override
241 public void handleMessage(Message msg) {
242 MainThreadRequest request;
243 Message onCompleted;
244 AsyncResult ar;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800245 UiccCard uiccCard;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700246 IccAPDUArgument iccArgument;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700247
248 switch (msg.what) {
Yorke Lee716f67e2015-06-17 15:39:16 -0700249 case CMD_HANDLE_PIN_MMI: {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700250 request = (MainThreadRequest) msg.obj;
Yorke Lee716f67e2015-06-17 15:39:16 -0700251 final Phone phone = getPhoneFromRequest(request);
252 request.result = phone != null ?
253 getPhoneFromRequest(request).handlePinMmi((String) request.argument)
254 : false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700255 // Wake up the requesting thread
256 synchronized (request) {
257 request.notifyAll();
258 }
259 break;
Yorke Lee716f67e2015-06-17 15:39:16 -0700260 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700261
262 case CMD_HANDLE_NEIGHBORING_CELL:
263 request = (MainThreadRequest) msg.obj;
264 onCompleted = obtainMessage(EVENT_NEIGHBORING_CELL_DONE,
265 request);
266 mPhone.getNeighboringCids(onCompleted);
267 break;
268
269 case EVENT_NEIGHBORING_CELL_DONE:
270 ar = (AsyncResult) msg.obj;
271 request = (MainThreadRequest) ar.userObj;
272 if (ar.exception == null && ar.result != null) {
273 request.result = ar.result;
274 } else {
275 // create an empty list to notify the waiting thread
Jake Hambye994d462014-02-03 13:10:13 -0800276 request.result = new ArrayList<NeighboringCellInfo>(0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700277 }
278 // Wake up the requesting thread
279 synchronized (request) {
280 request.notifyAll();
281 }
282 break;
283
284 case CMD_ANSWER_RINGING_CALL:
Wink Saville08874612014-08-31 19:19:58 -0700285 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800286 int answer_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700287 answerRingingCallInternal(answer_subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700288 break;
289
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700290 case CMD_END_CALL:
291 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800292 int end_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700293 final boolean hungUp;
Anthony Leeae4e36d2015-05-21 07:17:46 -0700294 Phone phone = getPhone(end_subId);
295 if (phone == null) {
296 if (DBG) log("CMD_END_CALL: no phone for id: " + end_subId);
297 break;
298 }
299 int phoneType = phone.getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700300 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
301 // CDMA: If the user presses the Power button we treat it as
302 // ending the complete call session
Wink Saville08874612014-08-31 19:19:58 -0700303 hungUp = PhoneUtils.hangupRingingAndActive(getPhone(end_subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700304 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
305 // GSM: End the call as per the Phone state
306 hungUp = PhoneUtils.hangup(mCM);
307 } else {
308 throw new IllegalStateException("Unexpected phone type: " + phoneType);
309 }
310 if (DBG) log("CMD_END_CALL: " + (hungUp ? "hung up!" : "no call to hang up"));
311 request.result = hungUp;
312 // Wake up the requesting thread
313 synchronized (request) {
314 request.notifyAll();
315 }
316 break;
317
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700318 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700319 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700320 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800321 uiccCard = getUiccCardFromRequest(request);
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;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800362 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700363 if (uiccCard == null) {
364 loge("iccTransmitApduBasicChannel: No UICC");
365 request.result = new IccIoResult(0x6F, 0, (byte[])null);
366 synchronized (request) {
367 request.notifyAll();
368 }
369 } else {
370 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
371 request);
372 uiccCard.iccTransmitApduBasicChannel(
373 iccArgument.cla, iccArgument.command, iccArgument.p1, iccArgument.p2,
374 iccArgument.p3, iccArgument.data, onCompleted);
375 }
376 break;
377
378 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
379 ar = (AsyncResult) msg.obj;
380 request = (MainThreadRequest) ar.userObj;
381 if (ar.exception == null && ar.result != null) {
382 request.result = ar.result;
383 } else {
384 request.result = new IccIoResult(0x6F, 0, (byte[])null);
385 if (ar.result == null) {
386 loge("iccTransmitApduBasicChannel: Empty response");
387 } else if (ar.exception instanceof CommandException) {
388 loge("iccTransmitApduBasicChannel: CommandException: " +
389 ar.exception);
390 } else {
391 loge("iccTransmitApduBasicChannel: Unknown exception");
392 }
393 }
394 synchronized (request) {
395 request.notifyAll();
396 }
397 break;
398
399 case CMD_EXCHANGE_SIM_IO:
400 request = (MainThreadRequest) msg.obj;
401 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800402 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700403 if (uiccCard == null) {
404 loge("iccExchangeSimIO: No UICC");
405 request.result = new IccIoResult(0x6F, 0, (byte[])null);
406 synchronized (request) {
407 request.notifyAll();
408 }
409 } else {
410 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
411 request);
412 uiccCard.iccExchangeSimIO(iccArgument.cla, /* fileID */
413 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
414 iccArgument.data, onCompleted);
415 }
416 break;
417
418 case EVENT_EXCHANGE_SIM_IO_DONE:
419 ar = (AsyncResult) msg.obj;
420 request = (MainThreadRequest) ar.userObj;
421 if (ar.exception == null && ar.result != null) {
422 request.result = ar.result;
423 } else {
424 request.result = new IccIoResult(0x6f, 0, (byte[])null);
425 }
426 synchronized (request) {
427 request.notifyAll();
428 }
429 break;
430
Derek Tan4d5e5c12014-02-04 11:54:58 -0800431 case CMD_SEND_ENVELOPE:
432 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800433 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700434 if (uiccCard == null) {
435 loge("sendEnvelopeWithStatus: No UICC");
436 request.result = new IccIoResult(0x6F, 0, (byte[])null);
437 synchronized (request) {
438 request.notifyAll();
439 }
440 } else {
441 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
442 uiccCard.sendEnvelopeWithStatus((String)request.argument, onCompleted);
443 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800444 break;
445
446 case EVENT_SEND_ENVELOPE_DONE:
447 ar = (AsyncResult) msg.obj;
448 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700449 if (ar.exception == null && ar.result != null) {
450 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800451 } else {
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700452 request.result = new IccIoResult(0x6F, 0, (byte[])null);
453 if (ar.result == null) {
454 loge("sendEnvelopeWithStatus: Empty response");
455 } else if (ar.exception instanceof CommandException) {
456 loge("sendEnvelopeWithStatus: CommandException: " +
457 ar.exception);
458 } else {
459 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
460 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800461 }
462 synchronized (request) {
463 request.notifyAll();
464 }
465 break;
466
Shishir Agrawal566b7612013-10-28 14:41:00 -0700467 case CMD_OPEN_CHANNEL:
468 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800469 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700470 if (uiccCard == null) {
471 loge("iccOpenLogicalChannel: No UICC");
Shishir Agrawalfc0492a2016-02-17 11:15:33 -0800472 request.result = new IccOpenLogicalChannelResponse(-1,
473 IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE, null);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700474 synchronized (request) {
475 request.notifyAll();
476 }
477 } else {
478 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
479 uiccCard.iccOpenLogicalChannel((String)request.argument, onCompleted);
480 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700481 break;
482
483 case EVENT_OPEN_CHANNEL_DONE:
484 ar = (AsyncResult) msg.obj;
485 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700486 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700487 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700488 int[] result = (int[]) ar.result;
489 int channelId = result[0];
490 byte[] selectResponse = null;
491 if (result.length > 1) {
492 selectResponse = new byte[result.length - 1];
493 for (int i = 1; i < result.length; ++i) {
494 selectResponse[i - 1] = (byte) result[i];
495 }
496 }
497 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700498 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700499 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700500 if (ar.result == null) {
501 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700502 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700503 if (ar.exception != null) {
504 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
505 }
506
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700507 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Junda Liua754ba12015-05-20 01:17:52 -0700508 if (ar.exception instanceof CommandException) {
509 CommandException.Error error =
510 ((CommandException) (ar.exception)).getCommandError();
511 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700512 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700513 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700514 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700515 }
516 }
517 openChannelResp = new IccOpenLogicalChannelResponse(
518 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700519 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700520 request.result = openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700521 synchronized (request) {
522 request.notifyAll();
523 }
524 break;
525
526 case CMD_CLOSE_CHANNEL:
527 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800528 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700529 if (uiccCard == null) {
530 loge("iccCloseLogicalChannel: No UICC");
531 request.result = new IccIoResult(0x6F, 0, (byte[])null);
532 synchronized (request) {
533 request.notifyAll();
534 }
535 } else {
536 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
537 uiccCard.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
538 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700539 break;
540
541 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800542 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
543 break;
544
545 case CMD_NV_READ_ITEM:
546 request = (MainThreadRequest) msg.obj;
547 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
548 mPhone.nvReadItem((Integer) request.argument, onCompleted);
549 break;
550
551 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700552 ar = (AsyncResult) msg.obj;
553 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800554 if (ar.exception == null && ar.result != null) {
555 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700556 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800557 request.result = "";
558 if (ar.result == null) {
559 loge("nvReadItem: Empty response");
560 } else if (ar.exception instanceof CommandException) {
561 loge("nvReadItem: CommandException: " +
562 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700563 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800564 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700565 }
566 }
567 synchronized (request) {
568 request.notifyAll();
569 }
570 break;
571
Jake Hambye994d462014-02-03 13:10:13 -0800572 case CMD_NV_WRITE_ITEM:
573 request = (MainThreadRequest) msg.obj;
574 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
575 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
576 mPhone.nvWriteItem(idValue.first, idValue.second, onCompleted);
577 break;
578
579 case EVENT_NV_WRITE_ITEM_DONE:
580 handleNullReturnEvent(msg, "nvWriteItem");
581 break;
582
583 case CMD_NV_WRITE_CDMA_PRL:
584 request = (MainThreadRequest) msg.obj;
585 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
586 mPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
587 break;
588
589 case EVENT_NV_WRITE_CDMA_PRL_DONE:
590 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
591 break;
592
593 case CMD_NV_RESET_CONFIG:
594 request = (MainThreadRequest) msg.obj;
595 onCompleted = obtainMessage(EVENT_NV_RESET_CONFIG_DONE, request);
596 mPhone.nvResetConfig((Integer) request.argument, onCompleted);
597 break;
598
599 case EVENT_NV_RESET_CONFIG_DONE:
600 handleNullReturnEvent(msg, "nvResetConfig");
601 break;
602
Jake Hamby7c27be32014-03-03 13:25:59 -0800603 case CMD_GET_PREFERRED_NETWORK_TYPE:
604 request = (MainThreadRequest) msg.obj;
605 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
Stuart Scott54788802015-03-30 13:18:01 -0700606 getPhoneFromRequest(request).getPreferredNetworkType(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800607 break;
608
609 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
610 ar = (AsyncResult) msg.obj;
611 request = (MainThreadRequest) ar.userObj;
612 if (ar.exception == null && ar.result != null) {
613 request.result = ar.result; // Integer
614 } else {
Sanket Padawecfc2d352016-01-05 19:52:14 -0800615 request.result = null;
Jake Hamby7c27be32014-03-03 13:25:59 -0800616 if (ar.result == null) {
617 loge("getPreferredNetworkType: Empty response");
618 } else if (ar.exception instanceof CommandException) {
619 loge("getPreferredNetworkType: CommandException: " +
620 ar.exception);
621 } else {
622 loge("getPreferredNetworkType: Unknown exception");
623 }
624 }
625 synchronized (request) {
626 request.notifyAll();
627 }
628 break;
629
630 case CMD_SET_PREFERRED_NETWORK_TYPE:
631 request = (MainThreadRequest) msg.obj;
632 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
633 int networkType = (Integer) request.argument;
Stuart Scott54788802015-03-30 13:18:01 -0700634 getPhoneFromRequest(request).setPreferredNetworkType(networkType, onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800635 break;
636
637 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
638 handleNullReturnEvent(msg, "setPreferredNetworkType");
639 break;
640
Steven Liu4bf01bc2014-07-17 11:05:29 -0500641 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
642 request = (MainThreadRequest)msg.obj;
643 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
644 mPhone.invokeOemRilRequestRaw((byte[])request.argument, onCompleted);
645 break;
646
647 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
648 ar = (AsyncResult)msg.obj;
649 request = (MainThreadRequest)ar.userObj;
650 request.result = ar;
651 synchronized (request) {
652 request.notifyAll();
653 }
654 break;
655
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800656 case CMD_SET_VOICEMAIL_NUMBER:
657 request = (MainThreadRequest) msg.obj;
658 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
659 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -0800660 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
661 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800662 break;
663
664 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
665 handleNullReturnEvent(msg, "setVoicemailNumber");
666 break;
667
Stuart Scott54788802015-03-30 13:18:01 -0700668 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
669 request = (MainThreadRequest) msg.obj;
670 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
671 request);
672 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
673 break;
674
675 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
676 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
677 break;
678
Shishir Agrawal302c8692015-06-19 13:49:39 -0700679 case CMD_PERFORM_NETWORK_SCAN:
680 request = (MainThreadRequest) msg.obj;
681 onCompleted = obtainMessage(EVENT_PERFORM_NETWORK_SCAN_DONE, request);
682 getPhoneFromRequest(request).getAvailableNetworks(onCompleted);
683 break;
684
685 case EVENT_PERFORM_NETWORK_SCAN_DONE:
686 ar = (AsyncResult) msg.obj;
687 request = (MainThreadRequest) ar.userObj;
688 CellNetworkScanResult cellScanResult;
689 if (ar.exception == null && ar.result != null) {
690 cellScanResult = new CellNetworkScanResult(
691 CellNetworkScanResult.STATUS_SUCCESS,
692 (List<OperatorInfo>) ar.result);
693 } else {
694 if (ar.result == null) {
695 loge("getCellNetworkScanResults: Empty response");
696 }
697 if (ar.exception != null) {
698 loge("getCellNetworkScanResults: Exception: " + ar.exception);
699 }
700 int errorCode = CellNetworkScanResult.STATUS_UNKNOWN_ERROR;
701 if (ar.exception instanceof CommandException) {
702 CommandException.Error error =
703 ((CommandException) (ar.exception)).getCommandError();
704 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
705 errorCode = CellNetworkScanResult.STATUS_RADIO_NOT_AVAILABLE;
706 } else if (error == CommandException.Error.GENERIC_FAILURE) {
707 errorCode = CellNetworkScanResult.STATUS_RADIO_GENERIC_FAILURE;
708 }
709 }
710 cellScanResult = new CellNetworkScanResult(errorCode, null);
711 }
712 request.result = cellScanResult;
713 synchronized (request) {
714 request.notifyAll();
715 }
716 break;
717
718 case CMD_SET_NETWORK_SELECTION_MODE_MANUAL:
719 request = (MainThreadRequest) msg.obj;
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700720 ManualNetworkSelectionArgument selArg =
721 (ManualNetworkSelectionArgument) request.argument;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700722 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE,
723 request);
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700724 getPhoneFromRequest(request).selectNetworkManually(selArg.operatorInfo,
725 selArg.persistSelection, onCompleted);
Shishir Agrawal302c8692015-06-19 13:49:39 -0700726 break;
727
728 case EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE:
729 handleNullReturnEvent(msg, "setNetworkSelectionModeManual");
730 break;
731
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700732 case CMD_GET_MODEM_ACTIVITY_INFO:
733 request = (MainThreadRequest) msg.obj;
734 onCompleted = obtainMessage(EVENT_GET_MODEM_ACTIVITY_INFO_DONE, request);
Prerepa Viswanadham61a60ad2015-06-08 18:07:51 -0700735 mPhone.getModemActivityInfo(onCompleted);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700736 break;
737
738 case EVENT_GET_MODEM_ACTIVITY_INFO_DONE:
739 ar = (AsyncResult) msg.obj;
740 request = (MainThreadRequest) ar.userObj;
741 if (ar.exception == null && ar.result != null) {
742 request.result = ar.result;
743 } else {
744 if (ar.result == null) {
745 loge("queryModemActivityInfo: Empty response");
746 } else if (ar.exception instanceof CommandException) {
747 loge("queryModemActivityInfo: CommandException: " +
748 ar.exception);
749 } else {
750 loge("queryModemActivityInfo: Unknown exception");
751 }
752 }
Amit Mahajand4766222016-01-28 15:28:28 -0800753 // Result cannot be null. Return ModemActivityInfo with all fields set to 0.
754 if (request.result == null) {
755 request.result = new ModemActivityInfo(0, 0, 0, null, 0, 0);
756 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700757 synchronized (request) {
758 request.notifyAll();
759 }
760 break;
761
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700762 default:
763 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
764 break;
765 }
766 }
Jake Hambye994d462014-02-03 13:10:13 -0800767
768 private void handleNullReturnEvent(Message msg, String command) {
769 AsyncResult ar = (AsyncResult) msg.obj;
770 MainThreadRequest request = (MainThreadRequest) ar.userObj;
771 if (ar.exception == null) {
772 request.result = true;
773 } else {
774 request.result = false;
775 if (ar.exception instanceof CommandException) {
776 loge(command + ": CommandException: " + ar.exception);
777 } else {
778 loge(command + ": Unknown exception");
779 }
780 }
781 synchronized (request) {
782 request.notifyAll();
783 }
784 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700785 }
786
787 /**
788 * Posts the specified command to be executed on the main thread,
789 * waits for the request to complete, and returns the result.
790 * @see #sendRequestAsync
791 */
792 private Object sendRequest(int command, Object argument) {
Sanket Padawe56e75a32016-02-08 12:18:19 -0800793 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Wink Saville36469e72014-06-11 15:17:00 -0700794 }
795
796 /**
797 * Posts the specified command to be executed on the main thread,
798 * waits for the request to complete, and returns the result.
799 * @see #sendRequestAsync
800 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800801 private Object sendRequest(int command, Object argument, Integer subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700802 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
803 throw new RuntimeException("This method will deadlock if called from the main thread.");
804 }
805
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800806 MainThreadRequest request = new MainThreadRequest(argument, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700807 Message msg = mMainThreadHandler.obtainMessage(command, request);
808 msg.sendToTarget();
809
810 // Wait for the request to complete
811 synchronized (request) {
812 while (request.result == null) {
813 try {
814 request.wait();
815 } catch (InterruptedException e) {
816 // Do nothing, go back and wait until the request is complete
817 }
818 }
819 }
820 return request.result;
821 }
822
823 /**
824 * Asynchronous ("fire and forget") version of sendRequest():
825 * Posts the specified command to be executed on the main thread, and
826 * returns immediately.
827 * @see #sendRequest
828 */
829 private void sendRequestAsync(int command) {
830 mMainThreadHandler.sendEmptyMessage(command);
831 }
832
833 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -0700834 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
835 * @see {@link #sendRequest(int,Object)}
836 */
837 private void sendRequestAsync(int command, Object argument) {
838 MainThreadRequest request = new MainThreadRequest(argument);
839 Message msg = mMainThreadHandler.obtainMessage(command, request);
840 msg.sendToTarget();
841 }
842
843 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700844 * Initialize the singleton PhoneInterfaceManager instance.
845 * This is only done once, at startup, from PhoneApp.onCreate().
846 */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700847 /* package */ static PhoneInterfaceManager init(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700848 synchronized (PhoneInterfaceManager.class) {
849 if (sInstance == null) {
Sailesh Nepal194161e2014-07-03 08:57:44 -0700850 sInstance = new PhoneInterfaceManager(app, phone);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700851 } else {
852 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
853 }
854 return sInstance;
855 }
856 }
857
858 /** Private constructor; @see init() */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700859 private PhoneInterfaceManager(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700860 mApp = app;
861 mPhone = phone;
862 mCM = PhoneGlobals.getInstance().mCM;
Stuart Scott981d8582015-04-21 14:09:50 -0700863 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700864 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
865 mMainThreadHandler = new MainThreadHandler();
Andrew Leedf14ead2014-10-17 14:22:52 -0700866 mTelephonySharedPreferences =
Derek Tan97ebb422014-09-05 16:55:38 -0700867 PreferenceManager.getDefaultSharedPreferences(mPhone.getContext());
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800868 mSubscriptionController = SubscriptionController.getInstance();
Wink Saville3ab207e2014-11-20 13:07:20 -0800869
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700870 publish();
871 }
872
873 private void publish() {
874 if (DBG) log("publish: " + this);
875
876 ServiceManager.addService("phone", this);
877 }
878
Stuart Scott584921c2015-01-15 17:10:34 -0800879 private Phone getPhoneFromRequest(MainThreadRequest request) {
Sanket Padawe56e75a32016-02-08 12:18:19 -0800880 return (request.subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID)
881 ? mPhone : getPhone(request.subId);
Stuart Scott584921c2015-01-15 17:10:34 -0800882 }
883
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800884 private UiccCard getUiccCardFromRequest(MainThreadRequest request) {
885 Phone phone = getPhoneFromRequest(request);
886 return phone == null ? null :
887 UiccController.getInstance().getUiccCard(phone.getPhoneId());
888 }
889
Wink Saville36469e72014-06-11 15:17:00 -0700890 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -0700891 private Phone getPhone(int subId) {
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800892 return PhoneFactory.getPhone(mSubscriptionController.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -0700893 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700894 //
895 // Implementation of the ITelephony interface.
896 //
897
898 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700899 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -0700900 }
901
Wink Savilleb564aae2014-10-23 10:18:09 -0700902 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700903 if (DBG) log("dial: " + number);
904 // No permission check needed here: This is just a wrapper around the
905 // ACTION_DIAL intent, which is available to any app since it puts up
906 // the UI before it does anything.
907
908 String url = createTelUrl(number);
909 if (url == null) {
910 return;
911 }
912
913 // PENDING: should we just silently fail if phone is offhook or ringing?
Wink Saville36469e72014-06-11 15:17:00 -0700914 PhoneConstants.State state = mCM.getState(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700915 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
916 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
917 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
918 mApp.startActivity(intent);
919 }
920 }
921
922 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700923 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -0700924 }
925
Wink Savilleb564aae2014-10-23 10:18:09 -0700926 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700927 if (DBG) log("call: " + number);
928
929 // This is just a wrapper around the ACTION_CALL intent, but we still
930 // need to do a permission check since we're calling startActivity()
931 // from the context of the phone app.
932 enforceCallPermission();
933
934 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
935 != AppOpsManager.MODE_ALLOWED) {
936 return;
937 }
938
939 String url = createTelUrl(number);
940 if (url == null) {
941 return;
942 }
943
Wink Saville08874612014-08-31 19:19:58 -0700944 boolean isValid = false;
Narayan Kamath1c496c22015-04-16 14:40:19 +0100945 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoList();
Wink Saville3ab207e2014-11-20 13:07:20 -0800946 if (slist != null) {
947 for (SubscriptionInfo subInfoRecord : slist) {
948 if (subInfoRecord.getSubscriptionId() == subId) {
949 isValid = true;
950 break;
951 }
Wink Saville08874612014-08-31 19:19:58 -0700952 }
953 }
954 if (isValid == false) {
955 return;
956 }
957
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700958 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
Wink Saville36469e72014-06-11 15:17:00 -0700959 intent.putExtra(SUBSCRIPTION_KEY, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700960 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
961 mApp.startActivity(intent);
962 }
963
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700964 /**
965 * End a call based on call state
966 * @return true is a call was ended
967 */
968 public boolean endCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700969 return endCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700970 }
971
972 /**
973 * End a call based on the call state of the subId
974 * @return true is a call was ended
975 */
Wink Savilleb564aae2014-10-23 10:18:09 -0700976 public boolean endCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700977 enforceCallPermission();
Stuart Scott584921c2015-01-15 17:10:34 -0800978 return (Boolean) sendRequest(CMD_END_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700979 }
980
981 public void answerRingingCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700982 answerRingingCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700983 }
984
Wink Savilleb564aae2014-10-23 10:18:09 -0700985 public void answerRingingCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700986 if (DBG) log("answerRingingCall...");
987 // TODO: there should eventually be a separate "ANSWER_PHONE" permission,
988 // but that can probably wait till the big TelephonyManager API overhaul.
989 // For now, protect this call with the MODIFY_PHONE_STATE permission.
990 enforceModifyPermission();
Stuart Scott584921c2015-01-15 17:10:34 -0800991 sendRequest(CMD_ANSWER_RINGING_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700992 }
993
994 /**
995 * Make the actual telephony calls to implement answerRingingCall().
996 * This should only be called from the main thread of the Phone app.
997 * @see #answerRingingCall
998 *
999 * TODO: it would be nice to return true if we answered the call, or
1000 * false if there wasn't actually a ringing incoming call, or some
1001 * other error occurred. (In other words, pass back the return value
1002 * from PhoneUtils.answerCall() or PhoneUtils.answerAndEndActive().)
1003 * But that would require calling this method via sendRequest() rather
1004 * than sendRequestAsync(), and right now we don't actually *need* that
1005 * return value, so let's just return void for now.
1006 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001007 private void answerRingingCallInternal(int subId) {
Wink Saville08874612014-08-31 19:19:58 -07001008 final boolean hasRingingCall = !getPhone(subId).getRingingCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001009 if (hasRingingCall) {
Wink Saville08874612014-08-31 19:19:58 -07001010 final boolean hasActiveCall = !getPhone(subId).getForegroundCall().isIdle();
1011 final boolean hasHoldingCall = !getPhone(subId).getBackgroundCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001012 if (hasActiveCall && hasHoldingCall) {
1013 // Both lines are in use!
1014 // TODO: provide a flag to let the caller specify what
1015 // policy to use if both lines are in use. (The current
1016 // behavior is hardwired to "answer incoming, end ongoing",
1017 // which is how the CALL button is specced to behave.)
1018 PhoneUtils.answerAndEndActive(mCM, mCM.getFirstActiveRingingCall());
1019 return;
1020 } else {
1021 // answerCall() will automatically hold the current active
1022 // call, if there is one.
1023 PhoneUtils.answerCall(mCM.getFirstActiveRingingCall());
1024 return;
1025 }
1026 } else {
1027 // No call was ringing.
1028 return;
1029 }
1030 }
1031
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001032 /**
Santos Cordon5422a8d2014-09-12 04:20:56 -07001033 * This method is no longer used and can be removed once TelephonyManager stops referring to it.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001034 */
Santos Cordon5422a8d2014-09-12 04:20:56 -07001035 public void silenceRinger() {
1036 Log.e(LOG_TAG, "silenseRinger not supported");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001037 }
1038
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001039 @Override
1040 public boolean isOffhook(String callingPackage) {
1041 return isOffhookForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001042 }
1043
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001044 @Override
1045 public boolean isOffhookForSubscriber(int subId, String callingPackage) {
1046 if (!canReadPhoneState(callingPackage, "isOffhookForSubscriber")) {
1047 return false;
1048 }
1049
Sanket Padawe356d7632015-06-22 14:03:32 -07001050 final Phone phone = getPhone(subId);
1051 if (phone != null) {
1052 return (phone.getState() == PhoneConstants.State.OFFHOOK);
1053 } else {
1054 return false;
1055 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001056 }
1057
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001058 @Override
1059 public boolean isRinging(String callingPackage) {
1060 return (isRingingForSubscriber(getDefaultSubscription(), callingPackage));
Wink Saville36469e72014-06-11 15:17:00 -07001061 }
1062
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001063 @Override
1064 public boolean isRingingForSubscriber(int subId, String callingPackage) {
1065 if (!canReadPhoneState(callingPackage, "isRingingForSubscriber")) {
1066 return false;
1067 }
1068
Sanket Padawe356d7632015-06-22 14:03:32 -07001069 final Phone phone = getPhone(subId);
1070 if (phone != null) {
1071 return (phone.getState() == PhoneConstants.State.RINGING);
1072 } else {
1073 return false;
1074 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001075 }
1076
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001077 @Override
1078 public boolean isIdle(String callingPackage) {
1079 return isIdleForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001080 }
1081
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001082 @Override
1083 public boolean isIdleForSubscriber(int subId, String callingPackage) {
1084 if (!canReadPhoneState(callingPackage, "isIdleForSubscriber")) {
1085 return false;
1086 }
1087
Sanket Padawe356d7632015-06-22 14:03:32 -07001088 final Phone phone = getPhone(subId);
1089 if (phone != null) {
1090 return (phone.getState() == PhoneConstants.State.IDLE);
1091 } else {
1092 return false;
1093 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001094 }
1095
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001096 public boolean supplyPin(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001097 return supplyPinForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001098 }
1099
Wink Savilleb564aae2014-10-23 10:18:09 -07001100 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001101 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001102 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1103 }
1104
1105 public boolean supplyPuk(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001106 return supplyPukForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001107 }
1108
Wink Savilleb564aae2014-10-23 10:18:09 -07001109 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001110 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001111 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1112 }
1113
1114 /** {@hide} */
1115 public int[] supplyPinReportResult(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001116 return supplyPinReportResultForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001117 }
1118
Wink Savilleb564aae2014-10-23 10:18:09 -07001119 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001120 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001121 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001122 checkSimPin.start();
1123 return checkSimPin.unlockSim(null, pin);
1124 }
1125
Wink Saville9de0f752013-10-22 19:04:03 -07001126 /** {@hide} */
1127 public int[] supplyPukReportResult(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001128 return supplyPukReportResultForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001129 }
1130
Wink Savilleb564aae2014-10-23 10:18:09 -07001131 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001132 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001133 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001134 checkSimPuk.start();
1135 return checkSimPuk.unlockSim(puk, pin);
1136 }
1137
1138 /**
Wink Saville9de0f752013-10-22 19:04:03 -07001139 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001140 * a synchronous one.
1141 */
1142 private static class UnlockSim extends Thread {
1143
1144 private final IccCard mSimCard;
1145
1146 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07001147 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1148 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001149
1150 // For replies from SimCard interface
1151 private Handler mHandler;
1152
1153 // For async handler to identify request type
1154 private static final int SUPPLY_PIN_COMPLETE = 100;
1155
1156 public UnlockSim(IccCard simCard) {
1157 mSimCard = simCard;
1158 }
1159
1160 @Override
1161 public void run() {
1162 Looper.prepare();
1163 synchronized (UnlockSim.this) {
1164 mHandler = new Handler() {
1165 @Override
1166 public void handleMessage(Message msg) {
1167 AsyncResult ar = (AsyncResult) msg.obj;
1168 switch (msg.what) {
1169 case SUPPLY_PIN_COMPLETE:
1170 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
1171 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07001172 mRetryCount = msg.arg1;
1173 if (ar.exception != null) {
1174 if (ar.exception instanceof CommandException &&
1175 ((CommandException)(ar.exception)).getCommandError()
1176 == CommandException.Error.PASSWORD_INCORRECT) {
1177 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
1178 } else {
1179 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1180 }
1181 } else {
1182 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
1183 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001184 mDone = true;
1185 UnlockSim.this.notifyAll();
1186 }
1187 break;
1188 }
1189 }
1190 };
1191 UnlockSim.this.notifyAll();
1192 }
1193 Looper.loop();
1194 }
1195
1196 /*
1197 * Use PIN or PUK to unlock SIM card
1198 *
1199 * If PUK is null, unlock SIM card with PIN
1200 *
1201 * If PUK is not null, unlock SIM card with PUK and set PIN code
1202 */
Wink Saville9de0f752013-10-22 19:04:03 -07001203 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001204
1205 while (mHandler == null) {
1206 try {
1207 wait();
1208 } catch (InterruptedException e) {
1209 Thread.currentThread().interrupt();
1210 }
1211 }
1212 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
1213
1214 if (puk == null) {
1215 mSimCard.supplyPin(pin, callback);
1216 } else {
1217 mSimCard.supplyPuk(puk, pin, callback);
1218 }
1219
1220 while (!mDone) {
1221 try {
1222 Log.d(LOG_TAG, "wait for done");
1223 wait();
1224 } catch (InterruptedException e) {
1225 // Restore the interrupted status
1226 Thread.currentThread().interrupt();
1227 }
1228 }
1229 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07001230 int[] resultArray = new int[2];
1231 resultArray[0] = mResult;
1232 resultArray[1] = mRetryCount;
1233 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001234 }
1235 }
1236
1237 public void updateServiceLocation() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001238 updateServiceLocationForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001239
1240 }
1241
Wink Savilleb564aae2014-10-23 10:18:09 -07001242 public void updateServiceLocationForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001243 // No permission check needed here: this call is harmless, and it's
1244 // needed for the ServiceState.requestStateUpdate() call (which is
1245 // already intentionally exposed to 3rd parties.)
Sanket Padawe356d7632015-06-22 14:03:32 -07001246 final Phone phone = getPhone(subId);
1247 if (phone != null) {
1248 phone.updateServiceLocation();
1249 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001250 }
1251
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001252 @Override
1253 public boolean isRadioOn(String callingPackage) {
1254 return isRadioOnForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001255 }
1256
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001257 @Override
1258 public boolean isRadioOnForSubscriber(int subId, String callingPackage) {
1259 if (!canReadPhoneState(callingPackage, "isRadioOnForSubscriber")) {
1260 return false;
1261 }
1262 return isRadioOnForSubscriber(subId);
1263 }
1264
1265 private boolean isRadioOnForSubscriber(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001266 final Phone phone = getPhone(subId);
1267 if (phone != null) {
1268 return phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF;
1269 } else {
1270 return false;
1271 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001272 }
1273
1274 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001275 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001276
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001277 }
Wink Saville36469e72014-06-11 15:17:00 -07001278
Wink Savilleb564aae2014-10-23 10:18:09 -07001279 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001280 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001281 final Phone phone = getPhone(subId);
1282 if (phone != null) {
1283 phone.setRadioPower(!isRadioOnForSubscriber(subId));
1284 }
Wink Saville36469e72014-06-11 15:17:00 -07001285 }
1286
1287 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001288 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001289 }
1290
Wink Savilleb564aae2014-10-23 10:18:09 -07001291 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07001292 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001293 final Phone phone = getPhone(subId);
1294 if (phone == null) {
1295 return false;
1296 }
1297 if ((phone.getServiceState().getState() !=
Wink Saville36469e72014-06-11 15:17:00 -07001298 ServiceState.STATE_POWER_OFF) != turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001299 toggleRadioOnOffForSubscriber(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001300 }
1301 return true;
1302 }
Wink Saville36469e72014-06-11 15:17:00 -07001303
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001304 public boolean needMobileRadioShutdown() {
1305 /*
1306 * If any of the Radios are available, it will need to be
1307 * shutdown. So return true if any Radio is available.
1308 */
1309 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1310 Phone phone = PhoneFactory.getPhone(i);
1311 if (phone != null && phone.isRadioAvailable()) return true;
1312 }
1313 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
1314 return false;
1315 }
1316
1317 public void shutdownMobileRadios() {
1318 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1319 logv("Shutting down Phone " + i);
1320 shutdownRadioUsingPhoneId(i);
1321 }
1322 }
1323
1324 private void shutdownRadioUsingPhoneId(int phoneId) {
1325 enforceModifyPermission();
1326 Phone phone = PhoneFactory.getPhone(phoneId);
1327 if (phone != null && phone.isRadioAvailable()) {
1328 phone.shutdownRadio();
1329 }
1330 }
1331
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001332 public boolean setRadioPower(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001333 return setRadioPowerForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001334 }
1335
Wink Savilleb564aae2014-10-23 10:18:09 -07001336 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001337 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001338 final Phone phone = getPhone(subId);
1339 if (phone != null) {
1340 phone.setRadioPower(turnOn);
1341 return true;
1342 } else {
1343 return false;
1344 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001345 }
1346
Wink Saville36469e72014-06-11 15:17:00 -07001347 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001348 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001349 public boolean enableDataConnectivity() {
1350 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001351 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001352 final Phone phone = getPhone(subId);
1353 if (phone != null) {
1354 phone.setDataEnabled(true);
1355 return true;
1356 } else {
1357 return false;
1358 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001359 }
1360
Wink Saville36469e72014-06-11 15:17:00 -07001361 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001362 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001363 public boolean disableDataConnectivity() {
1364 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001365 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001366 final Phone phone = getPhone(subId);
1367 if (phone != null) {
1368 phone.setDataEnabled(false);
1369 return true;
1370 } else {
1371 return false;
1372 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001373 }
1374
Wink Saville36469e72014-06-11 15:17:00 -07001375 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001376 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001377 public boolean isDataConnectivityPossible() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001378 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001379 final Phone phone = getPhone(subId);
1380 if (phone != null) {
1381 return phone.isDataConnectivityPossible();
1382 } else {
1383 return false;
1384 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001385 }
1386
1387 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001388 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07001389 }
1390
Wink Savilleb564aae2014-10-23 10:18:09 -07001391 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001392 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001393 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1394 return false;
1395 }
Wink Saville36469e72014-06-11 15:17:00 -07001396 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001397 }
1398
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001399 public int getCallState() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001400 return getCallStateForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001401 }
1402
Wink Savilleb564aae2014-10-23 10:18:09 -07001403 public int getCallStateForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001404 return DefaultPhoneNotifier.convertCallState(getPhone(subId).getState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001405 }
1406
Sanket Padawe356d7632015-06-22 14:03:32 -07001407 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001408 public int getDataState() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001409 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001410 if (phone != null) {
1411 return DefaultPhoneNotifier.convertDataState(phone.getDataConnectionState());
1412 } else {
1413 return DefaultPhoneNotifier.convertDataState(PhoneConstants.DataState.DISCONNECTED);
1414 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001415 }
1416
Sanket Padawe356d7632015-06-22 14:03:32 -07001417 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001418 public int getDataActivity() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001419 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001420 if (phone != null) {
1421 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
1422 } else {
1423 return TelephonyManager.DATA_ACTIVITY_NONE;
1424 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001425 }
1426
1427 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001428 public Bundle getCellLocation(String callingPackage) {
1429 enforceFineOrCoarseLocationPermission("getCellLocation");
1430
1431 // OP_COARSE_LOCATION controls both fine and coarse location.
1432 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1433 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001434 log("getCellLocation: returning null; mode != allowed");
Svetoslav64fad262015-04-14 14:35:21 -07001435 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001436 }
1437
Jake Hambye994d462014-02-03 13:10:13 -08001438 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001439 if (DBG_LOC) log("getCellLocation: is active user");
1440 Bundle data = new Bundle();
Legler Wu2c01cdf2014-12-08 19:00:59 +08001441 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001442 if (phone == null) {
1443 return null;
1444 }
Legler Wu2c01cdf2014-12-08 19:00:59 +08001445 phone.getCellLocation().fillInNotifierBundle(data);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001446 return data;
1447 } else {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001448 log("getCellLocation: suppress non-active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001449 return null;
1450 }
1451 }
1452
Svetoslav64fad262015-04-14 14:35:21 -07001453 private void enforceFineOrCoarseLocationPermission(String message) {
1454 try {
1455 mApp.enforceCallingOrSelfPermission(
1456 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1457 } catch (SecurityException e) {
1458 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1459 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1460 // is the weaker precondition
1461 mApp.enforceCallingOrSelfPermission(
1462 android.Manifest.permission.ACCESS_COARSE_LOCATION, message);
1463 }
1464 }
1465
1466
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001467 @Override
1468 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001469 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001470 }
1471
Sanket Padawe356d7632015-06-22 14:03:32 -07001472 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001473 public void enableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001474 mApp.enforceCallingOrSelfPermission(
1475 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001476 final Phone phone = getPhone(subId);
1477 if (phone != null) {
1478 phone.enableLocationUpdates();
1479 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001480 }
1481
1482 @Override
1483 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001484 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001485 }
1486
Sanket Padawe356d7632015-06-22 14:03:32 -07001487 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001488 public void disableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001489 mApp.enforceCallingOrSelfPermission(
1490 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001491 final Phone phone = getPhone(subId);
1492 if (phone != null) {
1493 phone.disableLocationUpdates();
1494 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001495 }
1496
1497 @Override
1498 @SuppressWarnings("unchecked")
1499 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
Svetoslav64fad262015-04-14 14:35:21 -07001500 enforceFineOrCoarseLocationPermission("getNeighboringCellInfo");
1501
1502 // OP_COARSE_LOCATION controls both fine and coarse location.
1503 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1504 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1505 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001506 }
1507
1508 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
1509 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1510 return null;
1511 }
Svetoslav64fad262015-04-14 14:35:21 -07001512
Jake Hambye994d462014-02-03 13:10:13 -08001513 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001514 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
1515
1516 ArrayList<NeighboringCellInfo> cells = null;
1517
1518 try {
1519 cells = (ArrayList<NeighboringCellInfo>) sendRequest(
Sanket Padawe56e75a32016-02-08 12:18:19 -08001520 CMD_HANDLE_NEIGHBORING_CELL, null,
1521 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001522 } catch (RuntimeException e) {
Wink Saville36469e72014-06-11 15:17:00 -07001523 Log.e(LOG_TAG, "getNeighboringCellInfo " + e);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001524 }
1525 return cells;
1526 } else {
1527 if (DBG_LOC) log("getNeighboringCellInfo: suppress non-active user");
1528 return null;
1529 }
1530 }
1531
1532
1533 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001534 public List<CellInfo> getAllCellInfo(String callingPackage) {
1535 enforceFineOrCoarseLocationPermission("getAllCellInfo");
1536
1537 // OP_COARSE_LOCATION controls both fine and coarse location.
1538 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1539 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1540 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001541 }
1542
Jake Hambye994d462014-02-03 13:10:13 -08001543 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001544 if (DBG_LOC) log("getAllCellInfo: is active user");
Legler Wu2c01cdf2014-12-08 19:00:59 +08001545 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
1546 for (Phone phone : PhoneFactory.getPhones()) {
Robert Greenwaltd9eb7da2015-08-19 12:18:12 -07001547 final List<CellInfo> info = phone.getAllCellInfo();
1548 if (info != null) cellInfos.addAll(phone.getAllCellInfo());
Legler Wu2c01cdf2014-12-08 19:00:59 +08001549 }
1550 return cellInfos;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001551 } else {
1552 if (DBG_LOC) log("getAllCellInfo: suppress non-active user");
1553 return null;
1554 }
1555 }
1556
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001557 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001558 public void setCellInfoListRate(int rateInMillis) {
1559 mPhone.setCellInfoListRate(rateInMillis);
1560 }
1561
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001562 //
1563 // Internal helper methods.
1564 //
1565
Jake Hambye994d462014-02-03 13:10:13 -08001566 private static boolean checkIfCallerIsSelfOrForegroundUser() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001567 boolean ok;
1568
1569 boolean self = Binder.getCallingUid() == Process.myUid();
1570 if (!self) {
1571 // Get the caller's user id then clear the calling identity
1572 // which will be restored in the finally clause.
1573 int callingUser = UserHandle.getCallingUserId();
1574 long ident = Binder.clearCallingIdentity();
1575
1576 try {
1577 // With calling identity cleared the current user is the foreground user.
1578 int foregroundUser = ActivityManager.getCurrentUser();
1579 ok = (foregroundUser == callingUser);
1580 if (DBG_LOC) {
1581 log("checkIfCallerIsSelfOrForegoundUser: foregroundUser=" + foregroundUser
1582 + " callingUser=" + callingUser + " ok=" + ok);
1583 }
1584 } catch (Exception ex) {
1585 if (DBG_LOC) loge("checkIfCallerIsSelfOrForegoundUser: Exception ex=" + ex);
1586 ok = false;
1587 } finally {
1588 Binder.restoreCallingIdentity(ident);
1589 }
1590 } else {
1591 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: is self");
1592 ok = true;
1593 }
1594 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: ret=" + ok);
1595 return ok;
1596 }
1597
1598 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001599 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1600 *
1601 * @throws SecurityException if the caller does not have the required permission
1602 */
1603 private void enforceModifyPermission() {
1604 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1605 }
1606
1607 /**
Junda Liua2e36012014-07-09 18:30:01 -07001608 * Make sure either system app or the caller has carrier privilege.
1609 *
1610 * @throws SecurityException if the caller does not have the required permission/privilege
1611 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001612 private void enforceModifyPermissionOrCarrierPrivilege(int subId) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001613 int permission = mApp.checkCallingOrSelfPermission(
1614 android.Manifest.permission.MODIFY_PHONE_STATE);
1615 if (permission == PackageManager.PERMISSION_GRANTED) {
1616 return;
1617 }
1618
1619 log("No modify permission, check carrier privilege next.");
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001620 enforceCarrierPrivilege(subId);
Junda Liua2e36012014-07-09 18:30:01 -07001621 }
1622
1623 /**
1624 * Make sure the caller has carrier privilege.
1625 *
1626 * @throws SecurityException if the caller does not have the required permission
1627 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001628 private void enforceCarrierPrivilege(int subId) {
1629 if (getCarrierPrivilegeStatus(subId) !=
1630 TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001631 loge("No Carrier Privilege.");
1632 throw new SecurityException("No Carrier Privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001633 }
1634 }
1635
1636 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001637 * Make sure the caller has the CALL_PHONE permission.
1638 *
1639 * @throws SecurityException if the caller does not have the required permission
1640 */
1641 private void enforceCallPermission() {
1642 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1643 }
1644
Stuart Scott8eef64f2015-04-08 15:13:54 -07001645 private void enforceConnectivityInternalPermission() {
1646 mApp.enforceCallingOrSelfPermission(
1647 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1648 "ConnectivityService");
1649 }
1650
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001651 private String createTelUrl(String number) {
1652 if (TextUtils.isEmpty(number)) {
1653 return null;
1654 }
1655
Jake Hambye994d462014-02-03 13:10:13 -08001656 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001657 }
1658
Ihab Awadf9e92732013-12-05 18:02:52 -08001659 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001660 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1661 }
1662
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001663 private static void logv(String msg) {
1664 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
1665 }
1666
Ihab Awadf9e92732013-12-05 18:02:52 -08001667 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001668 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1669 }
1670
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001671 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001672 public int getActivePhoneType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001673 return getActivePhoneTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001674 }
1675
Sanket Padawe356d7632015-06-22 14:03:32 -07001676 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001677 public int getActivePhoneTypeForSubscriber(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001678 final Phone phone = getPhone(subId);
1679 if (phone == null) {
1680 return PhoneConstants.PHONE_TYPE_NONE;
1681 } else {
1682 return getPhone(subId).getPhoneType();
1683 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001684 }
1685
1686 /**
1687 * Returns the CDMA ERI icon index to display
1688 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001689 @Override
1690 public int getCdmaEriIconIndex(String callingPackage) {
1691 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001692 }
1693
Sanket Padawe356d7632015-06-22 14:03:32 -07001694 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001695 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage) {
1696 if (!canReadPhoneState(callingPackage, "getCdmaEriIconIndexForSubscriber")) {
1697 return -1;
1698 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001699 final Phone phone = getPhone(subId);
1700 if (phone != null) {
1701 return phone.getCdmaEriIconIndex();
1702 } else {
1703 return -1;
1704 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001705 }
1706
1707 /**
1708 * Returns the CDMA ERI icon mode,
1709 * 0 - ON
1710 * 1 - FLASHING
1711 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001712 @Override
1713 public int getCdmaEriIconMode(String callingPackage) {
1714 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001715 }
1716
Sanket Padawe356d7632015-06-22 14:03:32 -07001717 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001718 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage) {
1719 if (!canReadPhoneState(callingPackage, "getCdmaEriIconModeForSubscriber")) {
1720 return -1;
1721 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001722 final Phone phone = getPhone(subId);
1723 if (phone != null) {
1724 return phone.getCdmaEriIconMode();
1725 } else {
1726 return -1;
1727 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001728 }
1729
1730 /**
1731 * Returns the CDMA ERI text,
1732 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001733 @Override
1734 public String getCdmaEriText(String callingPackage) {
1735 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001736 }
1737
Sanket Padawe356d7632015-06-22 14:03:32 -07001738 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001739 public String getCdmaEriTextForSubscriber(int subId, String callingPackage) {
1740 if (!canReadPhoneState(callingPackage, "getCdmaEriIconTextForSubscriber")) {
1741 return null;
1742 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001743 final Phone phone = getPhone(subId);
1744 if (phone != null) {
1745 return phone.getCdmaEriText();
1746 } else {
1747 return null;
1748 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001749 }
1750
1751 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07001752 * Returns the CDMA MDN.
1753 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001754 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001755 public String getCdmaMdn(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001756 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001757 final Phone phone = getPhone(subId);
1758 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA && phone != null) {
1759 return phone.getLine1Number();
Junda Liuca05d5d2014-08-14 22:36:34 -07001760 } else {
1761 return null;
1762 }
1763 }
1764
1765 /**
1766 * Returns the CDMA MIN.
1767 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001768 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001769 public String getCdmaMin(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001770 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001771 final Phone phone = getPhone(subId);
1772 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1773 return phone.getCdmaMin();
Junda Liuca05d5d2014-08-14 22:36:34 -07001774 } else {
1775 return null;
1776 }
1777 }
1778
1779 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001780 * Returns true if CDMA provisioning needs to run.
1781 */
1782 public boolean needsOtaServiceProvisioning() {
1783 return mPhone.needsOtaServiceProvisioning();
1784 }
1785
1786 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001787 * Sets the voice mail number of a given subId.
1788 */
1789 @Override
1790 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001791 enforceCarrierPrivilege(subId);
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001792 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
1793 new Pair<String, String>(alphaTag, number), new Integer(subId));
1794 return success;
1795 }
1796
1797 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001798 * Returns the unread count of voicemails
1799 */
1800 public int getVoiceMessageCount() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001801 return getVoiceMessageCountForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001802 }
1803
1804 /**
1805 * Returns the unread count of voicemails for a subId
1806 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001807 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001808 public int getVoiceMessageCountForSubscriber( int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001809 final Phone phone = getPhone(subId);
1810 if (phone != null) {
1811 return phone.getVoiceMessageCount();
1812 } else {
1813 return 0;
1814 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001815 }
1816
1817 /**
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001818 * Returns the data network type.
1819 * Legacy call, permission-free.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001820 *
1821 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
1822 */
1823 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001824 public int getNetworkType() {
1825 final Phone phone = getPhone(getDefaultSubscription());
1826 if (phone != null) {
1827 return phone.getServiceState().getDataNetworkType();
1828 } else {
1829 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1830 }
Wink Saville36469e72014-06-11 15:17:00 -07001831 }
1832
1833 /**
1834 * Returns the network type for a subId
1835 */
1836 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001837 public int getNetworkTypeForSubscriber(int subId, String callingPackage) {
1838 if (!canReadPhoneState(callingPackage, "getNetworkTypeForSubscriber")) {
1839 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1840 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001841
Sanket Padawe356d7632015-06-22 14:03:32 -07001842 final Phone phone = getPhone(subId);
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001843 if (phone != null) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001844 return phone.getServiceState().getDataNetworkType();
1845 } else {
1846 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1847 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001848 }
1849
1850 /**
1851 * Returns the data network type
1852 */
1853 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001854 public int getDataNetworkType(String callingPackage) {
1855 return getDataNetworkTypeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001856 }
1857
1858 /**
1859 * Returns the data network type for a subId
1860 */
1861 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001862 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage) {
1863 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
1864 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1865 }
1866
Sanket Padawe356d7632015-06-22 14:03:32 -07001867 final Phone phone = getPhone(subId);
1868 if (phone != null) {
1869 return phone.getServiceState().getDataNetworkType();
1870 } else {
1871 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1872 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001873 }
1874
1875 /**
Wink Saville36469e72014-06-11 15:17:00 -07001876 * Returns the Voice network type for a subId
1877 */
1878 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001879 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage) {
1880 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
1881 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1882 }
1883
Sanket Padawe356d7632015-06-22 14:03:32 -07001884 final Phone phone = getPhone(subId);
1885 if (phone != null) {
1886 return phone.getServiceState().getVoiceNetworkType();
1887 } else {
1888 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1889 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001890 }
1891
1892 /**
1893 * @return true if a ICC card is present
1894 */
1895 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07001896 // FIXME Make changes to pass defaultSimId of type int
Amit Mahajana6fc2a82015-01-06 11:53:51 -08001897 return hasIccCardUsingSlotId(mSubscriptionController.getSlotId(getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07001898 }
1899
1900 /**
1901 * @return true if a ICC card is present for a slotId
1902 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001903 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001904 public boolean hasIccCardUsingSlotId(int slotId) {
Amit Mahajana6fc2a82015-01-06 11:53:51 -08001905 int subId[] = mSubscriptionController.getSubIdUsingSlotId(slotId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001906 final Phone phone = getPhone(subId[0]);
1907 if (subId != null && phone != null) {
1908 return phone.getIccCard().hasIccCard();
Amit Mahajana6fc2a82015-01-06 11:53:51 -08001909 } else {
1910 return false;
1911 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001912 }
1913
1914 /**
1915 * Return if the current radio is LTE on CDMA. This
1916 * is a tri-state return value as for a period of time
1917 * the mode may be unknown.
1918 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001919 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001920 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08001921 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001922 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001923 @Override
1924 public int getLteOnCdmaMode(String callingPackage) {
1925 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001926 }
1927
Sanket Padawe356d7632015-06-22 14:03:32 -07001928 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001929 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage) {
1930 if (!canReadPhoneState(callingPackage, "getLteOnCdmaModeForSubscriber")) {
1931 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
1932 }
1933
Sanket Padawe356d7632015-06-22 14:03:32 -07001934 final Phone phone = getPhone(subId);
1935 if (phone == null) {
1936 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
1937 } else {
1938 return phone.getLteOnCdmaMode();
1939 }
Wink Saville36469e72014-06-11 15:17:00 -07001940 }
1941
1942 public void setPhone(Phone phone) {
1943 mPhone = phone;
1944 }
1945
1946 /**
1947 * {@hide}
1948 * Returns Default subId, 0 in the case of single standby.
1949 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001950 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001951 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07001952 }
1953
Wink Savilleb564aae2014-10-23 10:18:09 -07001954 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001955 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001956 }
Ihab Awadf2177b72013-11-25 13:33:23 -08001957
1958 /**
1959 * @see android.telephony.TelephonyManager.WifiCallingChoices
1960 */
1961 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08001962 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
1963 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08001964 }
1965
1966 /**
1967 * @see android.telephony.TelephonyManager.WifiCallingChoices
1968 */
1969 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08001970 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
1971 Settings.System.putInt(mPhone.getContext().getContentResolver(),
1972 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08001973 }
1974
Sailesh Nepald1e68152013-12-12 19:08:02 -08001975 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07001976 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08001977 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08001978 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08001979
Shishir Agrawal566b7612013-10-28 14:41:00 -07001980 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001981 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(int subId, String AID) {
1982 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07001983
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001984 if (DBG) log("iccOpenLogicalChannel: subId=" + subId + " aid=" + AID);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001985 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001986 CMD_OPEN_CHANNEL, AID, subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001987 if (DBG) log("iccOpenLogicalChannel: " + response);
1988 return response;
Shishir Agrawal566b7612013-10-28 14:41:00 -07001989 }
1990
1991 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001992 public boolean iccCloseLogicalChannel(int subId, int channel) {
1993 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07001994
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001995 if (DBG) log("iccCloseLogicalChannel: subId=" + subId + " chnl=" + channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07001996 if (channel < 0) {
1997 return false;
1998 }
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001999 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel, subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002000 if (DBG) log("iccCloseLogicalChannel: " + success);
2001 return success;
2002 }
2003
2004 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002005 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07002006 int command, int p1, int p2, int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002007 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002008
2009 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002010 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel +
2011 " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
Shishir Agrawal566b7612013-10-28 14:41:00 -07002012 " data=" + data);
2013 }
2014
2015 if (channel < 0) {
2016 return "";
2017 }
2018
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002019 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002020 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002021 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
2022
Shishir Agrawal566b7612013-10-28 14:41:00 -07002023 // Append the returned status code to the end of the response payload.
2024 String s = Integer.toHexString(
2025 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002026 if (response.payload != null) {
2027 s = IccUtils.bytesToHexString(response.payload) + s;
2028 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07002029 return s;
2030 }
Jake Hambye994d462014-02-03 13:10:13 -08002031
Evan Charltonc66da362014-05-16 14:06:40 -07002032 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002033 public String iccTransmitApduBasicChannel(int subId, int cla, int command, int p1, int p2,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002034 int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002035 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002036
2037 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002038 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd=" + command
2039 + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002040 }
2041
2042 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002043 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002044 if (DBG) log("iccTransmitApduBasicChannel: " + response);
2045
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002046 // Append the returned status code to the end of the response payload.
2047 String s = Integer.toHexString(
2048 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002049 if (response.payload != null) {
2050 s = IccUtils.bytesToHexString(response.payload) + s;
2051 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002052 return s;
2053 }
2054
2055 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002056 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002057 String filePath) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002058 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002059
2060 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002061 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " " +
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002062 p1 + " " + p2 + " " + p3 + ":" + filePath);
2063 }
2064
2065 IccIoResult response =
2066 (IccIoResult)sendRequest(CMD_EXCHANGE_SIM_IO,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002067 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
2068 subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002069
2070 if (DBG) {
2071 log("Exchange SIM_IO [R]" + response);
2072 }
2073
2074 byte[] result = null;
2075 int length = 2;
2076 if (response.payload != null) {
2077 length = 2 + response.payload.length;
2078 result = new byte[length];
2079 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
2080 } else {
2081 result = new byte[length];
2082 }
2083
2084 result[length - 1] = (byte) response.sw2;
2085 result[length - 2] = (byte) response.sw1;
2086 return result;
2087 }
2088
2089 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002090 public String sendEnvelopeWithStatus(int subId, String content) {
2091 enforceModifyPermissionOrCarrierPrivilege(subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002092
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002093 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content, subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002094 if (response.payload == null) {
2095 return "";
2096 }
2097
2098 // Append the returned status code to the end of the response payload.
2099 String s = Integer.toHexString(
2100 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
2101 s = IccUtils.bytesToHexString(response.payload) + s;
2102 return s;
2103 }
2104
Jake Hambye994d462014-02-03 13:10:13 -08002105 /**
2106 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2107 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2108 *
2109 * @param itemID the ID of the item to read
2110 * @return the NV item as a String, or null on error.
2111 */
2112 @Override
2113 public String nvReadItem(int itemID) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002114 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002115 if (DBG) log("nvReadItem: item " + itemID);
2116 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
2117 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
2118 return value;
2119 }
2120
2121 /**
2122 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2123 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2124 *
2125 * @param itemID the ID of the item to read
2126 * @param itemValue the value to write, as a String
2127 * @return true on success; false on any failure
2128 */
2129 @Override
2130 public boolean nvWriteItem(int itemID, String itemValue) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002131 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002132 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
2133 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
2134 new Pair<Integer, String>(itemID, itemValue));
2135 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
2136 return success;
2137 }
2138
2139 /**
2140 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
2141 * Used for device configuration by some CDMA operators.
2142 *
2143 * @param preferredRoamingList byte array containing the new PRL
2144 * @return true on success; false on any failure
2145 */
2146 @Override
2147 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002148 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002149 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
2150 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
2151 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
2152 return success;
2153 }
2154
2155 /**
2156 * Perform the specified type of NV config reset.
2157 * Used for device configuration by some CDMA operators.
2158 *
2159 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
2160 * @return true on success; false on any failure
2161 */
2162 @Override
2163 public boolean nvResetConfig(int resetType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002164 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002165 if (DBG) log("nvResetConfig: type " + resetType);
2166 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
2167 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
2168 return success;
2169 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002170
2171 /**
Wink Saville36469e72014-06-11 15:17:00 -07002172 * {@hide}
2173 * Returns Default sim, 0 in the case of single standby.
2174 */
2175 public int getDefaultSim() {
2176 //TODO Need to get it from Telephony Devcontroller
2177 return 0;
2178 }
2179
Svet Ganovb320e182015-04-16 12:30:10 -07002180 public String[] getPcscfAddress(String apnType, String callingPackage) {
2181 if (!canReadPhoneState(callingPackage, "getPcscfAddress")) {
2182 return new String[0];
2183 }
2184
2185
ram87fca6f2014-07-18 18:58:44 +05302186 return mPhone.getPcscfAddress(apnType);
Wink Saville36469e72014-06-11 15:17:00 -07002187 }
2188
2189 public void setImsRegistrationState(boolean registered) {
2190 enforceModifyPermission();
2191 mPhone.setImsRegistrationState(registered);
2192 }
2193
2194 /**
Stuart Scott54788802015-03-30 13:18:01 -07002195 * Set the network selection mode to automatic.
2196 *
2197 */
2198 @Override
2199 public void setNetworkSelectionModeAutomatic(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002200 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002201 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
2202 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
2203 }
2204
2205 /**
Shishir Agrawal302c8692015-06-19 13:49:39 -07002206 * Set the network selection mode to manual with the selected carrier.
2207 */
2208 @Override
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002209 public boolean setNetworkSelectionModeManual(int subId, OperatorInfo operator,
2210 boolean persistSelection) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002211 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002212 if (DBG) log("setNetworkSelectionModeManual: subId:" + subId + " operator:" + operator);
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002213 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operator,
2214 persistSelection);
2215 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002216 }
2217
2218 /**
2219 * Scans for available networks.
2220 */
2221 @Override
2222 public CellNetworkScanResult getCellNetworkScanResults(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002223 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002224 if (DBG) log("getCellNetworkScanResults: subId " + subId);
2225 CellNetworkScanResult result = (CellNetworkScanResult) sendRequest(
2226 CMD_PERFORM_NETWORK_SCAN, null, subId);
2227 return result;
2228 }
2229
2230 /**
Junda Liu84d15a22014-07-02 11:21:04 -07002231 * Get the calculated preferred network type.
2232 * Used for debugging incorrect network type.
2233 *
2234 * @return the preferred network type, defined in RILConstants.java.
2235 */
2236 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002237 public int getCalculatedPreferredNetworkType(String callingPackage) {
2238 if (!canReadPhoneState(callingPackage, "getCalculatedPreferredNetworkType")) {
2239 return RILConstants.PREFERRED_NETWORK_MODE;
2240 }
2241
Amit Mahajan43330e02014-11-18 11:54:45 -08002242 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext(), 0); // wink FIXME: need to get SubId from somewhere.
Junda Liu84d15a22014-07-02 11:21:04 -07002243 }
2244
2245 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08002246 * Get the preferred network type.
2247 * Used for device configuration by some CDMA operators.
2248 *
2249 * @return the preferred network type, defined in RILConstants.java.
2250 */
2251 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002252 public int getPreferredNetworkType(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002253 enforceModifyPermissionOrCarrierPrivilege(subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002254 if (DBG) log("getPreferredNetworkType");
Stuart Scott54788802015-03-30 13:18:01 -07002255 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002256 int networkType = (result != null ? result[0] : -1);
2257 if (DBG) log("getPreferredNetworkType: " + networkType);
2258 return networkType;
2259 }
2260
2261 /**
2262 * Set the preferred network type.
2263 * Used for device configuration by some CDMA operators.
2264 *
2265 * @param networkType the preferred network type, defined in RILConstants.java.
2266 * @return true on success; false on any failure.
2267 */
2268 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002269 public boolean setPreferredNetworkType(int subId, int networkType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002270 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002271 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
2272 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002273 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
Junda Liu80bc0d12014-07-14 16:36:44 -07002274 if (success) {
2275 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
Stuart Scott54788802015-03-30 13:18:01 -07002276 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
Junda Liu80bc0d12014-07-14 16:36:44 -07002277 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002278 return success;
2279 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002280
2281 /**
Junda Liu475951f2014-11-07 16:45:03 -08002282 * Check TETHER_DUN_REQUIRED and TETHER_DUN_APN settings, net.tethering.noprovisioning
2283 * SystemProperty, and config_tether_apndata to decide whether DUN APN is required for
2284 * tethering.
2285 *
2286 * @return 0: Not required. 1: required. 2: Not set.
2287 * @hide
2288 */
2289 @Override
2290 public int getTetherApnRequired() {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002291 enforceModifyPermission();
Junda Liu475951f2014-11-07 16:45:03 -08002292 int dunRequired = Settings.Global.getInt(mPhone.getContext().getContentResolver(),
2293 Settings.Global.TETHER_DUN_REQUIRED, 2);
2294 // If not set, check net.tethering.noprovisioning, TETHER_DUN_APN setting and
2295 // config_tether_apndata.
2296 if (dunRequired == 2 && mPhone.hasMatchedTetherApnSetting()) {
2297 dunRequired = 1;
2298 }
2299 return dunRequired;
2300 }
2301
2302 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07002303 * Set mobile data enabled
2304 * Used by the user through settings etc to turn on/off mobile data
2305 *
2306 * @param enable {@code true} turn turn data on, else {@code false}
2307 */
2308 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002309 public void setDataEnabled(int subId, boolean enable) {
Robert Greenwalted86e582014-05-21 20:03:20 -07002310 enforceModifyPermission();
Wink Savillee7353bb2014-12-05 14:21:41 -08002311 int phoneId = mSubscriptionController.getPhoneId(subId);
2312 log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
2313 Phone phone = PhoneFactory.getPhone(phoneId);
2314 if (phone != null) {
2315 log("setDataEnabled: subId=" + subId + " enable=" + enable);
2316 phone.setDataEnabled(enable);
2317 } else {
2318 loge("setDataEnabled: no phone for subId=" + subId);
2319 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002320 }
2321
2322 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07002323 * Get whether mobile data is enabled.
2324 *
2325 * Note that this used to be available from ConnectivityService, gated by
2326 * ACCESS_NETWORK_STATE permission, so this will accept either that or
2327 * our MODIFY_PHONE_STATE.
Robert Greenwalted86e582014-05-21 20:03:20 -07002328 *
2329 * @return {@code true} if data is enabled else {@code false}
2330 */
2331 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002332 public boolean getDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07002333 try {
2334 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
2335 null);
2336 } catch (Exception e) {
2337 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE,
2338 null);
2339 }
Wink Savillee7353bb2014-12-05 14:21:41 -08002340 int phoneId = mSubscriptionController.getPhoneId(subId);
2341 log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
2342 Phone phone = PhoneFactory.getPhone(phoneId);
2343 if (phone != null) {
2344 boolean retVal = phone.getDataEnabled();
2345 log("getDataEnabled: subId=" + subId + " retVal=" + retVal);
2346 return retVal;
2347 } else {
2348 loge("getDataEnabled: no phone subId=" + subId + " retVal=false");
2349 return false;
2350 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002351 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002352
2353 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002354 public int getCarrierPrivilegeStatus(int subId) {
2355 final Phone phone = getPhone(subId);
2356 if (phone == null) {
2357 loge("getCarrierPrivilegeStatus: Invalid subId");
2358 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
2359 }
2360 UiccCard card = UiccController.getInstance().getUiccCard(phone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002361 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002362 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002363 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2364 }
2365 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002366 phone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002367 }
Junda Liu29340342014-07-10 15:23:27 -07002368
2369 @Override
Zach Johnson50ecba32015-05-19 00:24:21 -07002370 public int checkCarrierPrivilegesForPackage(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002371 if (TextUtils.isEmpty(pkgName))
2372 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Shishir Agrawal21409252015-01-15 23:33:50 -08002373 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002374 if (card == null) {
2375 loge("checkCarrierPrivilegesForPackage: No UICC");
2376 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2377 }
Zach Johnson50ecba32015-05-19 00:24:21 -07002378 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgName);
2379 }
2380
2381 @Override
2382 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002383 if (TextUtils.isEmpty(pkgName))
2384 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Zach Johnson50ecba32015-05-19 00:24:21 -07002385 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2386 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2387 UiccCard card = UiccController.getInstance().getUiccCard(i);
2388 if (card == null) {
Jonathan Basseri7d320df2015-06-16 12:17:08 -07002389 // No UICC in that slot.
Zach Johnson50ecba32015-05-19 00:24:21 -07002390 continue;
2391 }
2392
2393 result = card.getCarrierPrivilegeStatus(
2394 mPhone.getContext().getPackageManager(), pkgName);
2395 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2396 break;
2397 }
2398 }
2399
2400 return result;
Junda Liu29340342014-07-10 15:23:27 -07002401 }
Derek Tan89e89d42014-07-08 17:00:10 -07002402
2403 @Override
Junda Liue64de782015-04-16 17:19:16 -07002404 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
2405 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
2406 loge("phoneId " + phoneId + " is not valid.");
2407 return null;
2408 }
2409 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002410 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002411 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002412 return null ;
2413 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002414 return card.getCarrierPackageNamesForIntent(
Svetoslav483aff72015-04-21 14:16:07 -07002415 mPhone.getContext().getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002416 }
2417
Wink Savilleb564aae2014-10-23 10:18:09 -07002418 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002419 final Phone phone = getPhone(subId);
2420 UiccCard card = phone == null ? null : phone.getUiccCard();
Derek Tan97ebb422014-09-05 16:55:38 -07002421 if (card == null) {
2422 loge("getIccId: No UICC");
2423 return null;
2424 }
2425 String iccId = card.getIccId();
2426 if (TextUtils.isEmpty(iccId)) {
2427 loge("getIccId: ICC ID is null or empty.");
2428 return null;
2429 }
2430 return iccId;
2431 }
2432
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002433 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002434 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
2435 String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002436 enforceCarrierPrivilege(subId);
Derek Tan97ebb422014-09-05 16:55:38 -07002437
Jeff Sharkey85190e62014-12-05 09:40:12 -08002438 final String iccId = getIccId(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07002439 final Phone phone = getPhone(subId);
2440 if (phone == null) {
2441 return false;
2442 }
2443 final String subscriberId = phone.getSubscriberId();
Jeff Sharkey85190e62014-12-05 09:40:12 -08002444
2445 if (DBG_MERGE) {
2446 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
2447 + subscriberId + " to " + number);
2448 }
2449
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002450 if (TextUtils.isEmpty(iccId)) {
2451 return false;
Derek Tan97ebb422014-09-05 16:55:38 -07002452 }
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002453
Jeff Sharkey85190e62014-12-05 09:40:12 -08002454 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2455
2456 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002457 if (alphaTag == null) {
2458 editor.remove(alphaTagPrefKey);
2459 } else {
2460 editor.putString(alphaTagPrefKey, alphaTag);
2461 }
2462
Jeff Sharkey85190e62014-12-05 09:40:12 -08002463 // Record both the line number and IMSI for this ICCID, since we need to
2464 // track all merged IMSIs based on line number
2465 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2466 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002467 if (number == null) {
2468 editor.remove(numberPrefKey);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002469 editor.remove(subscriberPrefKey);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002470 } else {
2471 editor.putString(numberPrefKey, number);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002472 editor.putString(subscriberPrefKey, subscriberId);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002473 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002474
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002475 editor.commit();
2476 return true;
Derek Tan7226c842014-07-02 17:42:23 -07002477 }
2478
2479 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002480 public String getLine1NumberForDisplay(int subId, String callingPackage) {
Makoto Onukifee69342015-06-29 14:44:50 -07002481 // This is open to apps with WRITE_SMS.
2482 if (!canReadPhoneNumber(callingPackage, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08002483 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07002484 return null;
2485 }
Derek Tan97ebb422014-09-05 16:55:38 -07002486
2487 String iccId = getIccId(subId);
2488 if (iccId != null) {
2489 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
Amit Mahajan9cf11512015-11-09 11:40:48 -08002490 if (DBG_MERGE) {
2491 log("getLine1NumberForDisplay returning " +
2492 mTelephonySharedPreferences.getString(numberPrefKey, null));
2493 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002494 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002495 }
Amit Mahajan9cf11512015-11-09 11:40:48 -08002496 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
Derek Tan97ebb422014-09-05 16:55:38 -07002497 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002498 }
2499
2500 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002501 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
2502 if (!canReadPhoneState(callingPackage, "getLine1AlphaTagForDisplay")) {
2503 return null;
2504 }
Derek Tan97ebb422014-09-05 16:55:38 -07002505
2506 String iccId = getIccId(subId);
2507 if (iccId != null) {
2508 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002509 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002510 }
Derek Tan97ebb422014-09-05 16:55:38 -07002511 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002512 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002513
2514 @Override
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002515 public String[] getMergedSubscriberIds(String callingPackage) {
2516 if (!canReadPhoneState(callingPackage, "getMergedSubscriberIds")) {
2517 return null;
2518 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002519 final Context context = mPhone.getContext();
2520 final TelephonyManager tele = TelephonyManager.from(context);
2521 final SubscriptionManager sub = SubscriptionManager.from(context);
2522
2523 // Figure out what subscribers are currently active
2524 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002525 // Clear calling identity, when calling TelephonyManager, because callerUid must be
2526 // the process, where TelephonyManager was instantiated. Otherwise AppOps check will fail.
2527 final long identity = Binder.clearCallingIdentity();
2528 try {
2529 final int[] subIds = sub.getActiveSubscriptionIdList();
2530 for (int subId : subIds) {
2531 activeSubscriberIds.add(tele.getSubscriberId(subId));
2532 }
2533 } finally {
2534 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002535 }
2536
2537 // First pass, find a number override for an active subscriber
2538 String mergeNumber = null;
2539 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
2540 for (String key : prefs.keySet()) {
2541 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
2542 final String subscriberId = (String) prefs.get(key);
2543 if (activeSubscriberIds.contains(subscriberId)) {
2544 final String iccId = key.substring(PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
2545 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2546 mergeNumber = (String) prefs.get(numberKey);
2547 if (DBG_MERGE) {
2548 Slog.d(LOG_TAG, "Found line number " + mergeNumber
2549 + " for active subscriber " + subscriberId);
2550 }
2551 if (!TextUtils.isEmpty(mergeNumber)) {
2552 break;
2553 }
2554 }
2555 }
2556 }
2557
2558 // Shortcut when no active merged subscribers
2559 if (TextUtils.isEmpty(mergeNumber)) {
2560 return null;
2561 }
2562
2563 // Second pass, find all subscribers under that line override
2564 final ArraySet<String> result = new ArraySet<>();
2565 for (String key : prefs.keySet()) {
2566 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
2567 final String number = (String) prefs.get(key);
2568 if (mergeNumber.equals(number)) {
2569 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
2570 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
2571 final String subscriberId = (String) prefs.get(subscriberKey);
2572 if (!TextUtils.isEmpty(subscriberId)) {
2573 result.add(subscriberId);
2574 }
2575 }
2576 }
2577 }
2578
2579 final String[] resultArray = result.toArray(new String[result.size()]);
2580 Arrays.sort(resultArray);
2581 if (DBG_MERGE) {
2582 Slog.d(LOG_TAG, "Found subscribers " + Arrays.toString(resultArray) + " after merge");
2583 }
2584 return resultArray;
2585 }
2586
2587 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002588 public boolean setOperatorBrandOverride(int subId, String brand) {
2589 enforceCarrierPrivilege(subId);
2590 final Phone phone = getPhone(subId);
2591 return phone == null ? false : phone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002592 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05002593
2594 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002595 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002596 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
2597 List<String> cdmaNonRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002598 enforceCarrierPrivilege(subId);
2599 final Phone phone = getPhone(subId);
2600 if (phone == null) {
2601 return false;
2602 }
2603 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002604 cdmaNonRoamingList);
2605 }
2606
2607 @Override
Steven Liu4bf01bc2014-07-17 11:05:29 -05002608 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
2609 enforceModifyPermission();
2610
2611 int returnValue = 0;
2612 try {
2613 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
2614 if(result.exception == null) {
2615 if (result.result != null) {
2616 byte[] responseData = (byte[])(result.result);
2617 if(responseData.length > oemResp.length) {
2618 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
2619 responseData.length + "bytes. Buffer Size is " +
2620 oemResp.length + "bytes.");
2621 }
2622 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
2623 returnValue = responseData.length;
2624 }
2625 } else {
2626 CommandException ex = (CommandException) result.exception;
2627 returnValue = ex.getCommandError().ordinal();
2628 if(returnValue > 0) returnValue *= -1;
2629 }
2630 } catch (RuntimeException e) {
2631 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
2632 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
2633 if(returnValue > 0) returnValue *= -1;
2634 }
2635
2636 return returnValue;
2637 }
Wink Saville5d475dd2014-10-17 15:00:58 -07002638
2639 @Override
2640 public void setRadioCapability(RadioAccessFamily[] rafs) {
2641 try {
2642 ProxyController.getInstance().setRadioCapability(rafs);
2643 } catch (RuntimeException e) {
2644 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
2645 }
2646 }
2647
2648 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002649 public int getRadioAccessFamily(int phoneId, String callingPackage) {
2650 if (!canReadPhoneState(callingPackage, "getRadioAccessFamily")) {
2651 return RadioAccessFamily.RAF_UNKNOWN;
2652 }
2653
Wink Saville5d475dd2014-10-17 15:00:58 -07002654 return ProxyController.getInstance().getRadioAccessFamily(phoneId);
2655 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002656
2657 @Override
2658 public void enableVideoCalling(boolean enable) {
2659 enforceModifyPermission();
Tyler Gunnfdd69de2015-12-04 21:24:38 -08002660 ImsManager.setVtSetting(mPhone.getContext(), enable);
Andrew Leedf14ead2014-10-17 14:22:52 -07002661 }
2662
2663 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002664 public boolean isVideoCallingEnabled(String callingPackage) {
2665 if (!canReadPhoneState(callingPackage, "isVideoCallingEnabled")) {
2666 return false;
2667 }
2668
Andrew Lee77527ac2014-10-21 16:57:39 -07002669 // Check the user preference and the system-level IMS setting. Even if the user has
2670 // enabled video calling, if IMS is disabled we aren't able to support video calling.
2671 // In the long run, we may instead need to check if there exists a connection service
2672 // which can support video calling.
Andrew Lee312e8172014-10-23 17:01:36 -07002673 return ImsManager.isVtEnabledByPlatform(mPhone.getContext())
2674 && ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
Tyler Gunnfdd69de2015-12-04 21:24:38 -08002675 && ImsManager.isVtEnabledByUser(mPhone.getContext());
Andrew Leedf14ead2014-10-17 14:22:52 -07002676 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002677
Andrew Leea1239f22015-03-02 17:44:07 -08002678 @Override
2679 public boolean canChangeDtmfToneLength() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07002680 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08002681 }
2682
2683 @Override
2684 public boolean isWorldPhone() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07002685 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08002686 }
2687
Andrew Lee9431b832015-03-09 18:46:45 -07002688 @Override
2689 public boolean isTtyModeSupported() {
2690 TelecomManager telecomManager = TelecomManager.from(mPhone.getContext());
2691 TelephonyManager telephonyManager =
2692 (TelephonyManager) mPhone.getContext().getSystemService(Context.TELEPHONY_SERVICE);
2693 return !telephonyManager.isMultiSimEnabled() && telecomManager.isTtySupported();
2694 }
2695
2696 @Override
2697 public boolean isHearingAidCompatibilitySupported() {
2698 return mPhone.getContext().getResources().getBoolean(R.bool.hac_enabled);
2699 }
2700
Sanket Padawe7310cc72015-01-14 09:53:20 -08002701 /**
2702 * Returns the unique device ID of phone, for example, the IMEI for
2703 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
2704 *
2705 * <p>Requires Permission:
2706 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
2707 */
2708 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002709 public String getDeviceId(String callingPackage) {
2710 if (!canReadPhoneState(callingPackage, "getDeviceId")) {
2711 return null;
2712 }
2713
Sanket Padawe7310cc72015-01-14 09:53:20 -08002714 final Phone phone = PhoneFactory.getPhone(0);
2715 if (phone != null) {
2716 return phone.getDeviceId();
2717 } else {
2718 return null;
2719 }
2720 }
2721
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002722 /*
2723 * {@hide}
2724 * Returns the IMS Registration Status
2725 */
Santos Cordon7a1885b2015-02-03 11:15:19 -08002726 @Override
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002727 public boolean isImsRegistered() {
2728 return mPhone.isImsRegistered();
2729 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08002730
2731 @Override
2732 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
2733 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
2734 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07002735
Nathan Haroldc55097a2015-03-11 18:14:50 -07002736 /*
2737 * {@hide}
2738 * Returns the IMS Registration Status
2739 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002740 public boolean isWifiCallingAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07002741 return mPhone.isWifiCallingEnabled();
2742 }
2743
2744 /*
2745 * {@hide}
2746 * Returns the IMS Registration Status
2747 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002748 public boolean isVolteAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07002749 return mPhone.isVolteEnabled();
2750 }
Svet Ganovb320e182015-04-16 12:30:10 -07002751
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002752 /*
2753 * {@hide} Returns the IMS Registration Status
2754 */
2755 public boolean isVideoTelephonyAvailable() {
2756 return mPhone.isVideoEnabled();
2757 }
2758
Svet Ganovb320e182015-04-16 12:30:10 -07002759 private boolean canReadPhoneState(String callingPackage, String message) {
Etan Cohen921655c2015-06-24 13:54:50 -07002760 try {
Amit Mahajan83ea23b2015-07-30 16:05:11 -07002761 mApp.enforceCallingOrSelfPermission(
Etan Cohen921655c2015-06-24 13:54:50 -07002762 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE, message);
2763
Amit Mahajan83ea23b2015-07-30 16:05:11 -07002764 // SKIP checking for run-time permission since caller or self has PRIVILEDGED permission
Etan Cohen921655c2015-06-24 13:54:50 -07002765 return true;
Amit Mahajan785783f2015-06-29 10:36:50 -07002766 } catch (SecurityException e) {
2767 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
2768 message);
Etan Cohen921655c2015-06-24 13:54:50 -07002769 }
Svet Ganovb320e182015-04-16 12:30:10 -07002770
2771 if (mAppOps.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(),
2772 callingPackage) != AppOpsManager.MODE_ALLOWED) {
2773 return false;
2774 }
2775
2776 return true;
2777 }
Stuart Scott8eef64f2015-04-08 15:13:54 -07002778
Makoto Onukifee69342015-06-29 14:44:50 -07002779 /**
Amit Mahajanb9b49782015-09-15 18:16:32 -07002780 * Besides READ_PHONE_STATE, WRITE_SMS and READ_SMS also allow apps to get phone numbers.
Makoto Onukifee69342015-06-29 14:44:50 -07002781 */
2782 private boolean canReadPhoneNumber(String callingPackage, String message) {
Makoto Onukie4072d12015-08-03 15:12:23 -07002783 // Default SMS app can always read it.
2784 if (mAppOps.noteOp(AppOpsManager.OP_WRITE_SMS,
2785 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED) {
2786 return true;
2787 }
2788 try {
2789 return canReadPhoneState(callingPackage, message);
Amit Mahajanb9b49782015-09-15 18:16:32 -07002790 } catch (SecurityException readPhoneStateSecurityException) {
2791 try {
2792 // Can be read with READ_SMS too.
2793 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_SMS, message);
2794 return mAppOps.noteOp(AppOpsManager.OP_READ_SMS,
2795 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED;
2796 } catch (SecurityException readSmsSecurityException) {
2797 // Throw exception with message including both READ_PHONE_STATE and READ_SMS
2798 // permissions
2799 throw new SecurityException(message + ": Neither user " + Binder.getCallingUid() +
2800 " nor current process has " + android.Manifest.permission.READ_PHONE_STATE +
2801 " or " + android.Manifest.permission.READ_SMS + ".");
2802 }
Makoto Onukie4072d12015-08-03 15:12:23 -07002803 }
Makoto Onukifee69342015-06-29 14:44:50 -07002804 }
2805
Stuart Scott8eef64f2015-04-08 15:13:54 -07002806 @Override
2807 public void factoryReset(int subId) {
2808 enforceConnectivityInternalPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07002809 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
2810 return;
2811 }
2812
Svet Ganovcc087f82015-05-12 20:35:54 -07002813 final long identity = Binder.clearCallingIdentity();
2814 try {
Stuart Scott981d8582015-04-21 14:09:50 -07002815 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
2816 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Svet Ganovcc087f82015-05-12 20:35:54 -07002817 // Enable data
2818 setDataEnabled(subId, true);
2819 // Set network selection mode to automatic
2820 setNetworkSelectionModeAutomatic(subId);
2821 // Set preferred mobile network type to the best available
2822 setPreferredNetworkType(subId, Phone.PREFERRED_NT_MODE);
2823 // Turn off roaming
2824 SubscriptionManager.from(mApp).setDataRoaming(0, subId);
2825 }
2826 } finally {
2827 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07002828 }
2829 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01002830
2831 @Override
2832 public String getLocaleFromDefaultSim() {
2833 // We query all subscriptions instead of just the active ones, because
2834 // this might be called early on in the provisioning flow when the
2835 // subscriptions potentially aren't active yet.
2836 final List<SubscriptionInfo> slist = getAllSubscriptionInfoList();
2837 if (slist == null || slist.isEmpty()) {
2838 return null;
2839 }
2840
2841 // This function may be called very early, say, from the setup wizard, at
2842 // which point we won't have a default subscription set. If that's the case
2843 // we just choose the first, which will be valid in "most cases".
2844 final int defaultSubId = getDefaultSubscription();
2845 SubscriptionInfo info = null;
2846 if (defaultSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
2847 info = slist.get(0);
2848 } else {
2849 for (SubscriptionInfo item : slist) {
2850 if (item.getSubscriptionId() == defaultSubId) {
2851 info = item;
2852 break;
2853 }
2854 }
2855
2856 if (info == null) {
2857 return null;
2858 }
2859 }
2860
2861 // Try and fetch the locale from the carrier properties or from the SIM language
2862 // preferences (EF-PL and EF-LI)...
Tony Hill183b2de2015-06-24 14:53:58 +01002863 final int mcc = info.getMcc();
Narayan Kamath1c496c22015-04-16 14:40:19 +01002864 final Phone defaultPhone = getPhone(info.getSubscriptionId());
Narayan Kamath011676f2015-07-29 12:04:08 +01002865 String simLanguage = null;
Narayan Kamath1c496c22015-04-16 14:40:19 +01002866 if (defaultPhone != null) {
2867 final Locale localeFromDefaultSim = defaultPhone.getLocaleFromSimAndCarrierPrefs();
2868 if (localeFromDefaultSim != null) {
Narayan Kamath011676f2015-07-29 12:04:08 +01002869 if (!localeFromDefaultSim.getCountry().isEmpty()) {
Tony Hill183b2de2015-06-24 14:53:58 +01002870 if (DBG) log("Using locale from default SIM:" + localeFromDefaultSim);
2871 return localeFromDefaultSim.toLanguageTag();
Narayan Kamath011676f2015-07-29 12:04:08 +01002872 } else {
2873 simLanguage = localeFromDefaultSim.getLanguage();
Tony Hill183b2de2015-06-24 14:53:58 +01002874 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01002875 }
2876 }
2877
Narayan Kamath011676f2015-07-29 12:04:08 +01002878 // The SIM language preferences only store a language (e.g. fr = French), not an
2879 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
2880 // the SIM and carrier preferences does not include a country we add the country
2881 // determined from the SIM MCC to provide an exact locale.
2882 final Locale mccLocale = MccTable.getLocaleFromMcc(mPhone.getContext(), mcc, simLanguage);
Tony Hill183b2de2015-06-24 14:53:58 +01002883 if (mccLocale != null) {
2884 if (DBG) log("No locale from default SIM, using mcc locale:" + mccLocale);
2885 return mccLocale.toLanguageTag();
Narayan Kamath1c496c22015-04-16 14:40:19 +01002886 }
2887
Tony Hill183b2de2015-06-24 14:53:58 +01002888 if (DBG) log("No locale found - returning null");
Narayan Kamath1c496c22015-04-16 14:40:19 +01002889 return null;
2890 }
2891
2892 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
2893 final long identity = Binder.clearCallingIdentity();
2894 try {
2895 return mSubscriptionController.getAllSubInfoList(
2896 mPhone.getContext().getOpPackageName());
2897 } finally {
2898 Binder.restoreCallingIdentity(identity);
2899 }
2900 }
2901
2902 private List<SubscriptionInfo> getActiveSubscriptionInfoList() {
2903 final long identity = Binder.clearCallingIdentity();
2904 try {
2905 return mSubscriptionController.getActiveSubscriptionInfoList(
2906 mPhone.getContext().getOpPackageName());
2907 } finally {
2908 Binder.restoreCallingIdentity(identity);
2909 }
2910 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07002911
2912 /**
2913 * {@hide}
2914 * Returns the modem stats
2915 */
2916 @Override
2917 public ModemActivityInfo getModemActivityInfo() {
2918 return (ModemActivityInfo) sendRequest(CMD_GET_MODEM_ACTIVITY_INFO, null);
2919 }
Jack Yu85bd38a2015-11-09 11:34:32 -08002920
2921 /**
2922 * {@hide}
2923 * Returns the service state information on specified subscription.
2924 */
2925 @Override
2926 public ServiceState getServiceStateForSubscriber(int subId, String callingPackage) {
2927
2928 if (!canReadPhoneState(callingPackage, "getServiceStateForSubscriber")) {
2929 return null;
2930 }
2931
2932 final Phone phone = getPhone(subId);
2933 if (phone == null) {
2934 return null;
2935 }
2936
2937 return phone.getServiceState();
2938 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08002939
2940 /**
2941 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
2942 *
2943 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
2944 * voicemail ringtone.
2945 * @return The URI for the ringtone to play when receiving a voicemail from a specific
2946 * PhoneAccount.
2947 */
2948 @Override
2949 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
2950 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
2951 if (phone == null) {
2952 return null;
2953 }
2954
2955 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone);
2956 }
2957
2958 /**
2959 * Returns whether vibration is set for voicemail notification in Phone settings.
2960 *
2961 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
2962 * voicemail vibration setting.
2963 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
2964 */
2965 @Override
2966 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
2967 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
2968 if (phone == null) {
2969 return false;
2970 }
2971
2972 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone);
2973 }
2974
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002975}