blob: 4c9700daa1ce3842aff9eee39f47f342e779a920 [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
Ta-wei Yen87c49842016-05-13 21:19:52 -070019import static com.android.internal.telephony.PhoneConstants.SUBSCRIPTION_KEY;
20
Santos Cordon7d4ddf62013-07-10 11:58:08 -070021import android.app.ActivityManager;
22import android.app.AppOpsManager;
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -070023import android.content.ComponentName;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070024import android.content.Context;
25import android.content.Intent;
Derek Tan97ebb422014-09-05 16:55:38 -070026import android.content.SharedPreferences;
Amith Yamasani6e118872016-02-19 12:53:51 -080027import android.content.pm.PackageInfo;
Shishir Agrawal60f9c952014-06-23 12:00:43 -070028import android.content.pm.PackageManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070029import android.net.Uri;
30import android.os.AsyncResult;
31import android.os.Binder;
32import android.os.Bundle;
33import android.os.Handler;
34import android.os.Looper;
35import android.os.Message;
36import android.os.Process;
Adam Lesinski903a54c2016-04-11 14:49:52 -070037import android.os.ResultReceiver;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070038import android.os.ServiceManager;
39import android.os.UserHandle;
Stuart Scott981d8582015-04-21 14:09:50 -070040import android.os.UserManager;
Derek Tan97ebb422014-09-05 16:55:38 -070041import android.preference.PreferenceManager;
Ihab Awadf2177b72013-11-25 13:33:23 -080042import android.provider.Settings;
Santos Cordon7a1885b2015-02-03 11:15:19 -080043import android.telecom.PhoneAccount;
Nancy Chen31f9ba12016-01-06 11:42:12 -080044import android.telecom.PhoneAccountHandle;
Andrew Lee9431b832015-03-09 18:46:45 -070045import android.telecom.TelecomManager;
Junda Liu12f7d802015-05-01 12:06:44 -070046import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070047import android.telephony.CellInfo;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -070048import android.telephony.IccOpenLogicalChannelResponse;
Ta-wei Yen87c49842016-05-13 21:19:52 -070049import android.telephony.ModemActivityInfo;
Jake Hambye994d462014-02-03 13:10:13 -080050import android.telephony.NeighboringCellInfo;
Wink Saville5d475dd2014-10-17 15:00:58 -070051import android.telephony.RadioAccessFamily;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070052import android.telephony.ServiceState;
Wink Saville0f3b5fc2014-11-11 08:40:49 -080053import android.telephony.SubscriptionInfo;
Jeff Sharkey85190e62014-12-05 09:40:12 -080054import android.telephony.SubscriptionManager;
Sanket Padawe99ef1e32016-05-18 16:12:33 -070055import android.telephony.TelephonyHistogram;
Ta-wei Yenb6929602016-05-24 15:48:27 -070056import android.telephony.TelephonyManager;
57import android.telephony.VisualVoicemailSmsFilterSettings;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070058import android.text.TextUtils;
Jeff Sharkey85190e62014-12-05 09:40:12 -080059import android.util.ArraySet;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070060import android.util.Log;
Jake Hambye994d462014-02-03 13:10:13 -080061import android.util.Pair;
Jeff Sharkey85190e62014-12-05 09:40:12 -080062import android.util.Slog;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070063
Andrew Lee312e8172014-10-23 17:01:36 -070064import com.android.ims.ImsManager;
Shishir Agrawal566b7612013-10-28 14:41:00 -070065import com.android.internal.telephony.CallManager;
Shishir Agrawal302c8692015-06-19 13:49:39 -070066import com.android.internal.telephony.CellNetworkScanResult;
Shishir Agrawal566b7612013-10-28 14:41:00 -070067import com.android.internal.telephony.CommandException;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070068import com.android.internal.telephony.DefaultPhoneNotifier;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070069import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -080070import com.android.internal.telephony.IccCard;
Narayan Kamath1c496c22015-04-16 14:40:19 +010071import com.android.internal.telephony.MccTable;
Shishir Agrawal302c8692015-06-19 13:49:39 -070072import com.android.internal.telephony.OperatorInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070073import com.android.internal.telephony.Phone;
Ta-wei Yen87c49842016-05-13 21:19:52 -070074import com.android.internal.telephony.PhoneConstants;
Wink Saville36469e72014-06-11 15:17:00 -070075import com.android.internal.telephony.PhoneFactory;
Wink Saville5d475dd2014-10-17 15:00:58 -070076import com.android.internal.telephony.ProxyController;
Sanket Padawe99ef1e32016-05-18 16:12:33 -070077import com.android.internal.telephony.RIL;
Svet Ganovb320e182015-04-16 12:30:10 -070078import com.android.internal.telephony.RILConstants;
Wink Savilleac1bdfd2014-11-20 23:04:44 -080079import com.android.internal.telephony.SubscriptionController;
Shishir Agrawal566b7612013-10-28 14:41:00 -070080import com.android.internal.telephony.uicc.IccIoResult;
81import com.android.internal.telephony.uicc.IccUtils;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -070082import com.android.internal.telephony.uicc.UiccCard;
Shishir Agrawal566b7612013-10-28 14:41:00 -070083import com.android.internal.telephony.uicc.UiccController;
Jake Hambye994d462014-02-03 13:10:13 -080084import com.android.internal.util.HexDump;
Nancy Chen31f9ba12016-01-06 11:42:12 -080085import com.android.phone.settings.VoicemailNotificationSettingsUtil;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070086
Santos Cordon7d4ddf62013-07-10 11:58:08 -070087import java.util.ArrayList;
Jeff Sharkey85190e62014-12-05 09:40:12 -080088import java.util.Arrays;
Jake Hambye994d462014-02-03 13:10:13 -080089import java.util.List;
Narayan Kamath1c496c22015-04-16 14:40:19 +010090import java.util.Locale;
Jeff Sharkey85190e62014-12-05 09:40:12 -080091import java.util.Map;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070092
93/**
94 * Implementation of the ITelephony interface.
95 */
Santos Cordon117fee72014-05-16 17:56:12 -070096public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -070097 private static final String LOG_TAG = "PhoneInterfaceManager";
98 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
99 private static final boolean DBG_LOC = false;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800100 private static final boolean DBG_MERGE = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700101
102 // Message codes used with mMainThreadHandler
103 private static final int CMD_HANDLE_PIN_MMI = 1;
104 private static final int CMD_HANDLE_NEIGHBORING_CELL = 2;
105 private static final int EVENT_NEIGHBORING_CELL_DONE = 3;
106 private static final int CMD_ANSWER_RINGING_CALL = 4;
107 private static final int CMD_END_CALL = 5; // not used yet
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700108 private static final int CMD_TRANSMIT_APDU_LOGICAL_CHANNEL = 7;
109 private static final int EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE = 8;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700110 private static final int CMD_OPEN_CHANNEL = 9;
111 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
112 private static final int CMD_CLOSE_CHANNEL = 11;
113 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -0800114 private static final int CMD_NV_READ_ITEM = 13;
115 private static final int EVENT_NV_READ_ITEM_DONE = 14;
116 private static final int CMD_NV_WRITE_ITEM = 15;
117 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
118 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
119 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
120 private static final int CMD_NV_RESET_CONFIG = 19;
121 private static final int EVENT_NV_RESET_CONFIG_DONE = 20;
Jake Hamby7c27be32014-03-03 13:25:59 -0800122 private static final int CMD_GET_PREFERRED_NETWORK_TYPE = 21;
123 private static final int EVENT_GET_PREFERRED_NETWORK_TYPE_DONE = 22;
124 private static final int CMD_SET_PREFERRED_NETWORK_TYPE = 23;
125 private static final int EVENT_SET_PREFERRED_NETWORK_TYPE_DONE = 24;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800126 private static final int CMD_SEND_ENVELOPE = 25;
127 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Derek Tan6b088ee2014-09-05 14:15:18 -0700128 private static final int CMD_INVOKE_OEM_RIL_REQUEST_RAW = 27;
129 private static final int EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE = 28;
130 private static final int CMD_TRANSMIT_APDU_BASIC_CHANNEL = 29;
131 private static final int EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE = 30;
132 private static final int CMD_EXCHANGE_SIM_IO = 31;
133 private static final int EVENT_EXCHANGE_SIM_IO_DONE = 32;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800134 private static final int CMD_SET_VOICEMAIL_NUMBER = 33;
135 private static final int EVENT_SET_VOICEMAIL_NUMBER_DONE = 34;
Stuart Scott54788802015-03-30 13:18:01 -0700136 private static final int CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC = 35;
137 private static final int EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE = 36;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700138 private static final int CMD_GET_MODEM_ACTIVITY_INFO = 37;
139 private static final int EVENT_GET_MODEM_ACTIVITY_INFO_DONE = 38;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700140 private static final int CMD_PERFORM_NETWORK_SCAN = 39;
141 private static final int EVENT_PERFORM_NETWORK_SCAN_DONE = 40;
142 private static final int CMD_SET_NETWORK_SELECTION_MODE_MANUAL = 41;
143 private static final int EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE = 42;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700144
145 /** The singleton instance. */
146 private static PhoneInterfaceManager sInstance;
147
Wink Saville3ab207e2014-11-20 13:07:20 -0800148 private PhoneGlobals mApp;
149 private Phone mPhone;
150 private CallManager mCM;
Stuart Scott981d8582015-04-21 14:09:50 -0700151 private UserManager mUserManager;
Wink Saville3ab207e2014-11-20 13:07:20 -0800152 private AppOpsManager mAppOps;
153 private MainThreadHandler mMainThreadHandler;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800154 private SubscriptionController mSubscriptionController;
Wink Saville3ab207e2014-11-20 13:07:20 -0800155 private SharedPreferences mTelephonySharedPreferences;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700156
Derek Tan97ebb422014-09-05 16:55:38 -0700157 private static final String PREF_CARRIERS_ALPHATAG_PREFIX = "carrier_alphtag_";
158 private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
Jeff Sharkey85190e62014-12-05 09:40:12 -0800159 private static final String PREF_CARRIERS_SUBSCRIBER_PREFIX = "carrier_subscriber_";
Derek Tan89e89d42014-07-08 17:00:10 -0700160
161 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700162 * A request object to use for transmitting data to an ICC.
163 */
164 private static final class IccAPDUArgument {
165 public int channel, cla, command, p1, p2, p3;
166 public String data;
167
168 public IccAPDUArgument(int channel, int cla, int command,
169 int p1, int p2, int p3, String data) {
170 this.channel = channel;
171 this.cla = cla;
172 this.command = command;
173 this.p1 = p1;
174 this.p2 = p2;
175 this.p3 = p3;
176 this.data = data;
177 }
178 }
179
180 /**
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700181 * A request object to use for transmitting data to an ICC.
182 */
183 private static final class ManualNetworkSelectionArgument {
184 public OperatorInfo operatorInfo;
185 public boolean persistSelection;
186
187 public ManualNetworkSelectionArgument(OperatorInfo operatorInfo, boolean persistSelection) {
188 this.operatorInfo = operatorInfo;
189 this.persistSelection = persistSelection;
190 }
191 }
192
193 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700194 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
195 * request after sending. The main thread will notify the request when it is complete.
196 */
197 private static final class MainThreadRequest {
198 /** The argument to use for the request */
199 public Object argument;
200 /** The result of the request that is run on the main thread */
201 public Object result;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800202 // The subscriber id that this request applies to. Defaults to
203 // SubscriptionManager.INVALID_SUBSCRIPTION_ID
204 public Integer subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700205
206 public MainThreadRequest(Object argument) {
207 this.argument = argument;
208 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800209
210 public MainThreadRequest(Object argument, Integer subId) {
211 this.argument = argument;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800212 if (subId != null) {
213 this.subId = subId;
214 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800215 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700216 }
217
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800218 private static final class IncomingThirdPartyCallArgs {
219 public final ComponentName component;
220 public final String callId;
221 public final String callerDisplayName;
222
223 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
224 String callerDisplayName) {
225 this.component = component;
226 this.callId = callId;
227 this.callerDisplayName = callerDisplayName;
228 }
229 }
230
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700231 /**
232 * A handler that processes messages on the main thread in the phone process. Since many
233 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
234 * inbound binder threads to the main thread in the phone process. The Binder thread
235 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
236 * on, which will be notified when the operation completes and will contain the result of the
237 * request.
238 *
239 * <p>If a MainThreadRequest object is provided in the msg.obj field,
240 * note that request.result must be set to something non-null for the calling thread to
241 * unblock.
242 */
243 private final class MainThreadHandler extends Handler {
244 @Override
245 public void handleMessage(Message msg) {
246 MainThreadRequest request;
247 Message onCompleted;
248 AsyncResult ar;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800249 UiccCard uiccCard;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700250 IccAPDUArgument iccArgument;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700251
252 switch (msg.what) {
Yorke Lee716f67e2015-06-17 15:39:16 -0700253 case CMD_HANDLE_PIN_MMI: {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700254 request = (MainThreadRequest) msg.obj;
Yorke Lee716f67e2015-06-17 15:39:16 -0700255 final Phone phone = getPhoneFromRequest(request);
256 request.result = phone != null ?
257 getPhoneFromRequest(request).handlePinMmi((String) request.argument)
258 : false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700259 // Wake up the requesting thread
260 synchronized (request) {
261 request.notifyAll();
262 }
263 break;
Yorke Lee716f67e2015-06-17 15:39:16 -0700264 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700265
266 case CMD_HANDLE_NEIGHBORING_CELL:
267 request = (MainThreadRequest) msg.obj;
268 onCompleted = obtainMessage(EVENT_NEIGHBORING_CELL_DONE,
269 request);
270 mPhone.getNeighboringCids(onCompleted);
271 break;
272
273 case EVENT_NEIGHBORING_CELL_DONE:
274 ar = (AsyncResult) msg.obj;
275 request = (MainThreadRequest) ar.userObj;
276 if (ar.exception == null && ar.result != null) {
277 request.result = ar.result;
278 } else {
279 // create an empty list to notify the waiting thread
Jake Hambye994d462014-02-03 13:10:13 -0800280 request.result = new ArrayList<NeighboringCellInfo>(0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700281 }
282 // Wake up the requesting thread
283 synchronized (request) {
284 request.notifyAll();
285 }
286 break;
287
288 case CMD_ANSWER_RINGING_CALL:
Wink Saville08874612014-08-31 19:19:58 -0700289 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800290 int answer_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700291 answerRingingCallInternal(answer_subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700292 break;
293
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700294 case CMD_END_CALL:
295 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800296 int end_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700297 final boolean hungUp;
Anthony Leeae4e36d2015-05-21 07:17:46 -0700298 Phone phone = getPhone(end_subId);
299 if (phone == null) {
300 if (DBG) log("CMD_END_CALL: no phone for id: " + end_subId);
301 break;
302 }
303 int phoneType = phone.getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700304 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
305 // CDMA: If the user presses the Power button we treat it as
306 // ending the complete call session
Wink Saville08874612014-08-31 19:19:58 -0700307 hungUp = PhoneUtils.hangupRingingAndActive(getPhone(end_subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700308 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
309 // GSM: End the call as per the Phone state
310 hungUp = PhoneUtils.hangup(mCM);
311 } else {
312 throw new IllegalStateException("Unexpected phone type: " + phoneType);
313 }
314 if (DBG) log("CMD_END_CALL: " + (hungUp ? "hung up!" : "no call to hang up"));
315 request.result = hungUp;
316 // Wake up the requesting thread
317 synchronized (request) {
318 request.notifyAll();
319 }
320 break;
321
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700322 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700323 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700324 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800325 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700326 if (uiccCard == null) {
327 loge("iccTransmitApduLogicalChannel: No UICC");
328 request.result = new IccIoResult(0x6F, 0, (byte[])null);
329 synchronized (request) {
330 request.notifyAll();
331 }
332 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700333 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
334 request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700335 uiccCard.iccTransmitApduLogicalChannel(
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700336 iccArgument.channel, iccArgument.cla, iccArgument.command,
337 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
Shishir Agrawal566b7612013-10-28 14:41:00 -0700338 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700339 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700340 break;
341
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700342 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700343 ar = (AsyncResult) msg.obj;
344 request = (MainThreadRequest) ar.userObj;
345 if (ar.exception == null && ar.result != null) {
346 request.result = ar.result;
347 } else {
348 request.result = new IccIoResult(0x6F, 0, (byte[])null);
349 if (ar.result == null) {
350 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800351 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700352 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800353 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700354 } else {
355 loge("iccTransmitApduLogicalChannel: Unknown exception");
356 }
357 }
358 synchronized (request) {
359 request.notifyAll();
360 }
361 break;
362
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700363 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
364 request = (MainThreadRequest) msg.obj;
365 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800366 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700367 if (uiccCard == null) {
368 loge("iccTransmitApduBasicChannel: No UICC");
369 request.result = new IccIoResult(0x6F, 0, (byte[])null);
370 synchronized (request) {
371 request.notifyAll();
372 }
373 } else {
374 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
375 request);
376 uiccCard.iccTransmitApduBasicChannel(
377 iccArgument.cla, iccArgument.command, iccArgument.p1, iccArgument.p2,
378 iccArgument.p3, iccArgument.data, onCompleted);
379 }
380 break;
381
382 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
383 ar = (AsyncResult) msg.obj;
384 request = (MainThreadRequest) ar.userObj;
385 if (ar.exception == null && ar.result != null) {
386 request.result = ar.result;
387 } else {
388 request.result = new IccIoResult(0x6F, 0, (byte[])null);
389 if (ar.result == null) {
390 loge("iccTransmitApduBasicChannel: Empty response");
391 } else if (ar.exception instanceof CommandException) {
392 loge("iccTransmitApduBasicChannel: CommandException: " +
393 ar.exception);
394 } else {
395 loge("iccTransmitApduBasicChannel: Unknown exception");
396 }
397 }
398 synchronized (request) {
399 request.notifyAll();
400 }
401 break;
402
403 case CMD_EXCHANGE_SIM_IO:
404 request = (MainThreadRequest) msg.obj;
405 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800406 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700407 if (uiccCard == null) {
408 loge("iccExchangeSimIO: No UICC");
409 request.result = new IccIoResult(0x6F, 0, (byte[])null);
410 synchronized (request) {
411 request.notifyAll();
412 }
413 } else {
414 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
415 request);
416 uiccCard.iccExchangeSimIO(iccArgument.cla, /* fileID */
417 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
418 iccArgument.data, onCompleted);
419 }
420 break;
421
422 case EVENT_EXCHANGE_SIM_IO_DONE:
423 ar = (AsyncResult) msg.obj;
424 request = (MainThreadRequest) ar.userObj;
425 if (ar.exception == null && ar.result != null) {
426 request.result = ar.result;
427 } else {
428 request.result = new IccIoResult(0x6f, 0, (byte[])null);
429 }
430 synchronized (request) {
431 request.notifyAll();
432 }
433 break;
434
Derek Tan4d5e5c12014-02-04 11:54:58 -0800435 case CMD_SEND_ENVELOPE:
436 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800437 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700438 if (uiccCard == null) {
439 loge("sendEnvelopeWithStatus: No UICC");
440 request.result = new IccIoResult(0x6F, 0, (byte[])null);
441 synchronized (request) {
442 request.notifyAll();
443 }
444 } else {
445 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
446 uiccCard.sendEnvelopeWithStatus((String)request.argument, onCompleted);
447 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800448 break;
449
450 case EVENT_SEND_ENVELOPE_DONE:
451 ar = (AsyncResult) msg.obj;
452 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700453 if (ar.exception == null && ar.result != null) {
454 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800455 } else {
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700456 request.result = new IccIoResult(0x6F, 0, (byte[])null);
457 if (ar.result == null) {
458 loge("sendEnvelopeWithStatus: Empty response");
459 } else if (ar.exception instanceof CommandException) {
460 loge("sendEnvelopeWithStatus: CommandException: " +
461 ar.exception);
462 } else {
463 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
464 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800465 }
466 synchronized (request) {
467 request.notifyAll();
468 }
469 break;
470
Shishir Agrawal566b7612013-10-28 14:41:00 -0700471 case CMD_OPEN_CHANNEL:
472 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800473 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700474 if (uiccCard == null) {
475 loge("iccOpenLogicalChannel: No UICC");
Shishir Agrawalfc0492a2016-02-17 11:15:33 -0800476 request.result = new IccOpenLogicalChannelResponse(-1,
477 IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE, null);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700478 synchronized (request) {
479 request.notifyAll();
480 }
481 } else {
482 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
483 uiccCard.iccOpenLogicalChannel((String)request.argument, onCompleted);
484 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700485 break;
486
487 case EVENT_OPEN_CHANNEL_DONE:
488 ar = (AsyncResult) msg.obj;
489 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700490 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700491 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700492 int[] result = (int[]) ar.result;
493 int channelId = result[0];
494 byte[] selectResponse = null;
495 if (result.length > 1) {
496 selectResponse = new byte[result.length - 1];
497 for (int i = 1; i < result.length; ++i) {
498 selectResponse[i - 1] = (byte) result[i];
499 }
500 }
501 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700502 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700503 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700504 if (ar.result == null) {
505 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700506 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700507 if (ar.exception != null) {
508 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
509 }
510
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700511 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Junda Liua754ba12015-05-20 01:17:52 -0700512 if (ar.exception instanceof CommandException) {
513 CommandException.Error error =
514 ((CommandException) (ar.exception)).getCommandError();
515 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700516 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700517 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700518 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700519 }
520 }
521 openChannelResp = new IccOpenLogicalChannelResponse(
522 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700523 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700524 request.result = openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700525 synchronized (request) {
526 request.notifyAll();
527 }
528 break;
529
530 case CMD_CLOSE_CHANNEL:
531 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800532 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700533 if (uiccCard == null) {
534 loge("iccCloseLogicalChannel: No UICC");
535 request.result = new IccIoResult(0x6F, 0, (byte[])null);
536 synchronized (request) {
537 request.notifyAll();
538 }
539 } else {
540 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
541 uiccCard.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
542 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700543 break;
544
545 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800546 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
547 break;
548
549 case CMD_NV_READ_ITEM:
550 request = (MainThreadRequest) msg.obj;
551 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
552 mPhone.nvReadItem((Integer) request.argument, onCompleted);
553 break;
554
555 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700556 ar = (AsyncResult) msg.obj;
557 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800558 if (ar.exception == null && ar.result != null) {
559 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700560 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800561 request.result = "";
562 if (ar.result == null) {
563 loge("nvReadItem: Empty response");
564 } else if (ar.exception instanceof CommandException) {
565 loge("nvReadItem: CommandException: " +
566 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700567 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800568 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700569 }
570 }
571 synchronized (request) {
572 request.notifyAll();
573 }
574 break;
575
Jake Hambye994d462014-02-03 13:10:13 -0800576 case CMD_NV_WRITE_ITEM:
577 request = (MainThreadRequest) msg.obj;
578 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
579 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
580 mPhone.nvWriteItem(idValue.first, idValue.second, onCompleted);
581 break;
582
583 case EVENT_NV_WRITE_ITEM_DONE:
584 handleNullReturnEvent(msg, "nvWriteItem");
585 break;
586
587 case CMD_NV_WRITE_CDMA_PRL:
588 request = (MainThreadRequest) msg.obj;
589 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
590 mPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
591 break;
592
593 case EVENT_NV_WRITE_CDMA_PRL_DONE:
594 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
595 break;
596
597 case CMD_NV_RESET_CONFIG:
598 request = (MainThreadRequest) msg.obj;
599 onCompleted = obtainMessage(EVENT_NV_RESET_CONFIG_DONE, request);
600 mPhone.nvResetConfig((Integer) request.argument, onCompleted);
601 break;
602
603 case EVENT_NV_RESET_CONFIG_DONE:
604 handleNullReturnEvent(msg, "nvResetConfig");
605 break;
606
Jake Hamby7c27be32014-03-03 13:25:59 -0800607 case CMD_GET_PREFERRED_NETWORK_TYPE:
608 request = (MainThreadRequest) msg.obj;
609 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
Stuart Scott54788802015-03-30 13:18:01 -0700610 getPhoneFromRequest(request).getPreferredNetworkType(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800611 break;
612
613 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
614 ar = (AsyncResult) msg.obj;
615 request = (MainThreadRequest) ar.userObj;
616 if (ar.exception == null && ar.result != null) {
617 request.result = ar.result; // Integer
618 } else {
Sanket Padawecfc2d352016-01-05 19:52:14 -0800619 request.result = null;
Jake Hamby7c27be32014-03-03 13:25:59 -0800620 if (ar.result == null) {
621 loge("getPreferredNetworkType: Empty response");
622 } else if (ar.exception instanceof CommandException) {
623 loge("getPreferredNetworkType: CommandException: " +
624 ar.exception);
625 } else {
626 loge("getPreferredNetworkType: Unknown exception");
627 }
628 }
629 synchronized (request) {
630 request.notifyAll();
631 }
632 break;
633
634 case CMD_SET_PREFERRED_NETWORK_TYPE:
635 request = (MainThreadRequest) msg.obj;
636 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
637 int networkType = (Integer) request.argument;
Stuart Scott54788802015-03-30 13:18:01 -0700638 getPhoneFromRequest(request).setPreferredNetworkType(networkType, onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800639 break;
640
641 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
642 handleNullReturnEvent(msg, "setPreferredNetworkType");
643 break;
644
Steven Liu4bf01bc2014-07-17 11:05:29 -0500645 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
646 request = (MainThreadRequest)msg.obj;
647 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
648 mPhone.invokeOemRilRequestRaw((byte[])request.argument, onCompleted);
649 break;
650
651 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
652 ar = (AsyncResult)msg.obj;
653 request = (MainThreadRequest)ar.userObj;
654 request.result = ar;
655 synchronized (request) {
656 request.notifyAll();
657 }
658 break;
659
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800660 case CMD_SET_VOICEMAIL_NUMBER:
661 request = (MainThreadRequest) msg.obj;
662 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
663 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -0800664 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
665 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800666 break;
667
668 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
669 handleNullReturnEvent(msg, "setVoicemailNumber");
670 break;
671
Stuart Scott54788802015-03-30 13:18:01 -0700672 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
673 request = (MainThreadRequest) msg.obj;
674 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
675 request);
676 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
677 break;
678
679 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
680 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
681 break;
682
Shishir Agrawal302c8692015-06-19 13:49:39 -0700683 case CMD_PERFORM_NETWORK_SCAN:
684 request = (MainThreadRequest) msg.obj;
685 onCompleted = obtainMessage(EVENT_PERFORM_NETWORK_SCAN_DONE, request);
686 getPhoneFromRequest(request).getAvailableNetworks(onCompleted);
687 break;
688
689 case EVENT_PERFORM_NETWORK_SCAN_DONE:
690 ar = (AsyncResult) msg.obj;
691 request = (MainThreadRequest) ar.userObj;
692 CellNetworkScanResult cellScanResult;
693 if (ar.exception == null && ar.result != null) {
694 cellScanResult = new CellNetworkScanResult(
695 CellNetworkScanResult.STATUS_SUCCESS,
696 (List<OperatorInfo>) ar.result);
697 } else {
698 if (ar.result == null) {
699 loge("getCellNetworkScanResults: Empty response");
700 }
701 if (ar.exception != null) {
702 loge("getCellNetworkScanResults: Exception: " + ar.exception);
703 }
704 int errorCode = CellNetworkScanResult.STATUS_UNKNOWN_ERROR;
705 if (ar.exception instanceof CommandException) {
706 CommandException.Error error =
707 ((CommandException) (ar.exception)).getCommandError();
708 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
709 errorCode = CellNetworkScanResult.STATUS_RADIO_NOT_AVAILABLE;
710 } else if (error == CommandException.Error.GENERIC_FAILURE) {
711 errorCode = CellNetworkScanResult.STATUS_RADIO_GENERIC_FAILURE;
712 }
713 }
714 cellScanResult = new CellNetworkScanResult(errorCode, null);
715 }
716 request.result = cellScanResult;
717 synchronized (request) {
718 request.notifyAll();
719 }
720 break;
721
722 case CMD_SET_NETWORK_SELECTION_MODE_MANUAL:
723 request = (MainThreadRequest) msg.obj;
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700724 ManualNetworkSelectionArgument selArg =
725 (ManualNetworkSelectionArgument) request.argument;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700726 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE,
727 request);
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700728 getPhoneFromRequest(request).selectNetworkManually(selArg.operatorInfo,
729 selArg.persistSelection, onCompleted);
Shishir Agrawal302c8692015-06-19 13:49:39 -0700730 break;
731
732 case EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE:
733 handleNullReturnEvent(msg, "setNetworkSelectionModeManual");
734 break;
735
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700736 case CMD_GET_MODEM_ACTIVITY_INFO:
737 request = (MainThreadRequest) msg.obj;
738 onCompleted = obtainMessage(EVENT_GET_MODEM_ACTIVITY_INFO_DONE, request);
Prerepa Viswanadham61a60ad2015-06-08 18:07:51 -0700739 mPhone.getModemActivityInfo(onCompleted);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700740 break;
741
742 case EVENT_GET_MODEM_ACTIVITY_INFO_DONE:
743 ar = (AsyncResult) msg.obj;
744 request = (MainThreadRequest) ar.userObj;
745 if (ar.exception == null && ar.result != null) {
746 request.result = ar.result;
747 } else {
748 if (ar.result == null) {
749 loge("queryModemActivityInfo: Empty response");
750 } else if (ar.exception instanceof CommandException) {
751 loge("queryModemActivityInfo: CommandException: " +
752 ar.exception);
753 } else {
754 loge("queryModemActivityInfo: Unknown exception");
755 }
756 }
Amit Mahajand4766222016-01-28 15:28:28 -0800757 // Result cannot be null. Return ModemActivityInfo with all fields set to 0.
758 if (request.result == null) {
759 request.result = new ModemActivityInfo(0, 0, 0, null, 0, 0);
760 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700761 synchronized (request) {
762 request.notifyAll();
763 }
764 break;
765
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700766 default:
767 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
768 break;
769 }
770 }
Jake Hambye994d462014-02-03 13:10:13 -0800771
772 private void handleNullReturnEvent(Message msg, String command) {
773 AsyncResult ar = (AsyncResult) msg.obj;
774 MainThreadRequest request = (MainThreadRequest) ar.userObj;
775 if (ar.exception == null) {
776 request.result = true;
777 } else {
778 request.result = false;
779 if (ar.exception instanceof CommandException) {
780 loge(command + ": CommandException: " + ar.exception);
781 } else {
782 loge(command + ": Unknown exception");
783 }
784 }
785 synchronized (request) {
786 request.notifyAll();
787 }
788 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700789 }
790
791 /**
792 * Posts the specified command to be executed on the main thread,
793 * waits for the request to complete, and returns the result.
794 * @see #sendRequestAsync
795 */
796 private Object sendRequest(int command, Object argument) {
Sanket Padawe56e75a32016-02-08 12:18:19 -0800797 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Wink Saville36469e72014-06-11 15:17:00 -0700798 }
799
800 /**
801 * Posts the specified command to be executed on the main thread,
802 * waits for the request to complete, and returns the result.
803 * @see #sendRequestAsync
804 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800805 private Object sendRequest(int command, Object argument, Integer subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700806 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
807 throw new RuntimeException("This method will deadlock if called from the main thread.");
808 }
809
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800810 MainThreadRequest request = new MainThreadRequest(argument, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700811 Message msg = mMainThreadHandler.obtainMessage(command, request);
812 msg.sendToTarget();
813
814 // Wait for the request to complete
815 synchronized (request) {
816 while (request.result == null) {
817 try {
818 request.wait();
819 } catch (InterruptedException e) {
820 // Do nothing, go back and wait until the request is complete
821 }
822 }
823 }
824 return request.result;
825 }
826
827 /**
828 * Asynchronous ("fire and forget") version of sendRequest():
829 * Posts the specified command to be executed on the main thread, and
830 * returns immediately.
831 * @see #sendRequest
832 */
833 private void sendRequestAsync(int command) {
834 mMainThreadHandler.sendEmptyMessage(command);
835 }
836
837 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -0700838 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
839 * @see {@link #sendRequest(int,Object)}
840 */
841 private void sendRequestAsync(int command, Object argument) {
842 MainThreadRequest request = new MainThreadRequest(argument);
843 Message msg = mMainThreadHandler.obtainMessage(command, request);
844 msg.sendToTarget();
845 }
846
847 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700848 * Initialize the singleton PhoneInterfaceManager instance.
849 * This is only done once, at startup, from PhoneApp.onCreate().
850 */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700851 /* package */ static PhoneInterfaceManager init(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700852 synchronized (PhoneInterfaceManager.class) {
853 if (sInstance == null) {
Sailesh Nepal194161e2014-07-03 08:57:44 -0700854 sInstance = new PhoneInterfaceManager(app, phone);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700855 } else {
856 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
857 }
858 return sInstance;
859 }
860 }
861
862 /** Private constructor; @see init() */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700863 private PhoneInterfaceManager(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700864 mApp = app;
865 mPhone = phone;
866 mCM = PhoneGlobals.getInstance().mCM;
Stuart Scott981d8582015-04-21 14:09:50 -0700867 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700868 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
869 mMainThreadHandler = new MainThreadHandler();
Andrew Leedf14ead2014-10-17 14:22:52 -0700870 mTelephonySharedPreferences =
Derek Tan97ebb422014-09-05 16:55:38 -0700871 PreferenceManager.getDefaultSharedPreferences(mPhone.getContext());
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800872 mSubscriptionController = SubscriptionController.getInstance();
Wink Saville3ab207e2014-11-20 13:07:20 -0800873
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700874 publish();
875 }
876
877 private void publish() {
878 if (DBG) log("publish: " + this);
879
880 ServiceManager.addService("phone", this);
881 }
882
Stuart Scott584921c2015-01-15 17:10:34 -0800883 private Phone getPhoneFromRequest(MainThreadRequest request) {
Sanket Padawe56e75a32016-02-08 12:18:19 -0800884 return (request.subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID)
885 ? mPhone : getPhone(request.subId);
Stuart Scott584921c2015-01-15 17:10:34 -0800886 }
887
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800888 private UiccCard getUiccCardFromRequest(MainThreadRequest request) {
889 Phone phone = getPhoneFromRequest(request);
890 return phone == null ? null :
891 UiccController.getInstance().getUiccCard(phone.getPhoneId());
892 }
893
Wink Saville36469e72014-06-11 15:17:00 -0700894 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -0700895 private Phone getPhone(int subId) {
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800896 return PhoneFactory.getPhone(mSubscriptionController.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -0700897 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700898 //
899 // Implementation of the ITelephony interface.
900 //
901
902 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700903 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -0700904 }
905
Wink Savilleb564aae2014-10-23 10:18:09 -0700906 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700907 if (DBG) log("dial: " + number);
908 // No permission check needed here: This is just a wrapper around the
909 // ACTION_DIAL intent, which is available to any app since it puts up
910 // the UI before it does anything.
911
912 String url = createTelUrl(number);
913 if (url == null) {
914 return;
915 }
916
917 // PENDING: should we just silently fail if phone is offhook or ringing?
Wink Saville36469e72014-06-11 15:17:00 -0700918 PhoneConstants.State state = mCM.getState(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700919 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
920 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
921 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
922 mApp.startActivity(intent);
923 }
924 }
925
926 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700927 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -0700928 }
929
Wink Savilleb564aae2014-10-23 10:18:09 -0700930 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700931 if (DBG) log("call: " + number);
932
933 // This is just a wrapper around the ACTION_CALL intent, but we still
934 // need to do a permission check since we're calling startActivity()
935 // from the context of the phone app.
936 enforceCallPermission();
937
938 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
939 != AppOpsManager.MODE_ALLOWED) {
940 return;
941 }
942
943 String url = createTelUrl(number);
944 if (url == null) {
945 return;
946 }
947
Wink Saville08874612014-08-31 19:19:58 -0700948 boolean isValid = false;
Narayan Kamath1c496c22015-04-16 14:40:19 +0100949 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoList();
Wink Saville3ab207e2014-11-20 13:07:20 -0800950 if (slist != null) {
951 for (SubscriptionInfo subInfoRecord : slist) {
952 if (subInfoRecord.getSubscriptionId() == subId) {
953 isValid = true;
954 break;
955 }
Wink Saville08874612014-08-31 19:19:58 -0700956 }
957 }
958 if (isValid == false) {
959 return;
960 }
961
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700962 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
Wink Saville36469e72014-06-11 15:17:00 -0700963 intent.putExtra(SUBSCRIPTION_KEY, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700964 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
965 mApp.startActivity(intent);
966 }
967
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700968 /**
969 * End a call based on call state
970 * @return true is a call was ended
971 */
972 public boolean endCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700973 return endCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700974 }
975
976 /**
977 * End a call based on the call state of the subId
978 * @return true is a call was ended
979 */
Wink Savilleb564aae2014-10-23 10:18:09 -0700980 public boolean endCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700981 enforceCallPermission();
Stuart Scott584921c2015-01-15 17:10:34 -0800982 return (Boolean) sendRequest(CMD_END_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700983 }
984
985 public void answerRingingCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700986 answerRingingCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700987 }
988
Wink Savilleb564aae2014-10-23 10:18:09 -0700989 public void answerRingingCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700990 if (DBG) log("answerRingingCall...");
991 // TODO: there should eventually be a separate "ANSWER_PHONE" permission,
992 // but that can probably wait till the big TelephonyManager API overhaul.
993 // For now, protect this call with the MODIFY_PHONE_STATE permission.
994 enforceModifyPermission();
Stuart Scott584921c2015-01-15 17:10:34 -0800995 sendRequest(CMD_ANSWER_RINGING_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700996 }
997
998 /**
999 * Make the actual telephony calls to implement answerRingingCall().
1000 * This should only be called from the main thread of the Phone app.
1001 * @see #answerRingingCall
1002 *
1003 * TODO: it would be nice to return true if we answered the call, or
1004 * false if there wasn't actually a ringing incoming call, or some
1005 * other error occurred. (In other words, pass back the return value
1006 * from PhoneUtils.answerCall() or PhoneUtils.answerAndEndActive().)
1007 * But that would require calling this method via sendRequest() rather
1008 * than sendRequestAsync(), and right now we don't actually *need* that
1009 * return value, so let's just return void for now.
1010 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001011 private void answerRingingCallInternal(int subId) {
Wink Saville08874612014-08-31 19:19:58 -07001012 final boolean hasRingingCall = !getPhone(subId).getRingingCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001013 if (hasRingingCall) {
Wink Saville08874612014-08-31 19:19:58 -07001014 final boolean hasActiveCall = !getPhone(subId).getForegroundCall().isIdle();
1015 final boolean hasHoldingCall = !getPhone(subId).getBackgroundCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001016 if (hasActiveCall && hasHoldingCall) {
1017 // Both lines are in use!
1018 // TODO: provide a flag to let the caller specify what
1019 // policy to use if both lines are in use. (The current
1020 // behavior is hardwired to "answer incoming, end ongoing",
1021 // which is how the CALL button is specced to behave.)
1022 PhoneUtils.answerAndEndActive(mCM, mCM.getFirstActiveRingingCall());
1023 return;
1024 } else {
1025 // answerCall() will automatically hold the current active
1026 // call, if there is one.
1027 PhoneUtils.answerCall(mCM.getFirstActiveRingingCall());
1028 return;
1029 }
1030 } else {
1031 // No call was ringing.
1032 return;
1033 }
1034 }
1035
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001036 /**
Santos Cordon5422a8d2014-09-12 04:20:56 -07001037 * This method is no longer used and can be removed once TelephonyManager stops referring to it.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001038 */
Santos Cordon5422a8d2014-09-12 04:20:56 -07001039 public void silenceRinger() {
1040 Log.e(LOG_TAG, "silenseRinger not supported");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001041 }
1042
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001043 @Override
1044 public boolean isOffhook(String callingPackage) {
1045 return isOffhookForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001046 }
1047
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001048 @Override
1049 public boolean isOffhookForSubscriber(int subId, String callingPackage) {
1050 if (!canReadPhoneState(callingPackage, "isOffhookForSubscriber")) {
1051 return false;
1052 }
1053
Sanket Padawe356d7632015-06-22 14:03:32 -07001054 final Phone phone = getPhone(subId);
1055 if (phone != null) {
1056 return (phone.getState() == PhoneConstants.State.OFFHOOK);
1057 } else {
1058 return false;
1059 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001060 }
1061
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001062 @Override
1063 public boolean isRinging(String callingPackage) {
1064 return (isRingingForSubscriber(getDefaultSubscription(), callingPackage));
Wink Saville36469e72014-06-11 15:17:00 -07001065 }
1066
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001067 @Override
1068 public boolean isRingingForSubscriber(int subId, String callingPackage) {
1069 if (!canReadPhoneState(callingPackage, "isRingingForSubscriber")) {
1070 return false;
1071 }
1072
Sanket Padawe356d7632015-06-22 14:03:32 -07001073 final Phone phone = getPhone(subId);
1074 if (phone != null) {
1075 return (phone.getState() == PhoneConstants.State.RINGING);
1076 } else {
1077 return false;
1078 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001079 }
1080
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001081 @Override
1082 public boolean isIdle(String callingPackage) {
1083 return isIdleForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001084 }
1085
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001086 @Override
1087 public boolean isIdleForSubscriber(int subId, String callingPackage) {
1088 if (!canReadPhoneState(callingPackage, "isIdleForSubscriber")) {
1089 return false;
1090 }
1091
Sanket Padawe356d7632015-06-22 14:03:32 -07001092 final Phone phone = getPhone(subId);
1093 if (phone != null) {
1094 return (phone.getState() == PhoneConstants.State.IDLE);
1095 } else {
1096 return false;
1097 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001098 }
1099
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001100 public boolean supplyPin(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001101 return supplyPinForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001102 }
1103
Wink Savilleb564aae2014-10-23 10:18:09 -07001104 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001105 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001106 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1107 }
1108
1109 public boolean supplyPuk(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001110 return supplyPukForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001111 }
1112
Wink Savilleb564aae2014-10-23 10:18:09 -07001113 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001114 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001115 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1116 }
1117
1118 /** {@hide} */
1119 public int[] supplyPinReportResult(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001120 return supplyPinReportResultForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001121 }
1122
Wink Savilleb564aae2014-10-23 10:18:09 -07001123 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001124 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001125 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001126 checkSimPin.start();
1127 return checkSimPin.unlockSim(null, pin);
1128 }
1129
Wink Saville9de0f752013-10-22 19:04:03 -07001130 /** {@hide} */
1131 public int[] supplyPukReportResult(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001132 return supplyPukReportResultForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001133 }
1134
Wink Savilleb564aae2014-10-23 10:18:09 -07001135 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001136 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001137 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001138 checkSimPuk.start();
1139 return checkSimPuk.unlockSim(puk, pin);
1140 }
1141
1142 /**
Wink Saville9de0f752013-10-22 19:04:03 -07001143 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001144 * a synchronous one.
1145 */
1146 private static class UnlockSim extends Thread {
1147
1148 private final IccCard mSimCard;
1149
1150 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07001151 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1152 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001153
1154 // For replies from SimCard interface
1155 private Handler mHandler;
1156
1157 // For async handler to identify request type
1158 private static final int SUPPLY_PIN_COMPLETE = 100;
1159
1160 public UnlockSim(IccCard simCard) {
1161 mSimCard = simCard;
1162 }
1163
1164 @Override
1165 public void run() {
1166 Looper.prepare();
1167 synchronized (UnlockSim.this) {
1168 mHandler = new Handler() {
1169 @Override
1170 public void handleMessage(Message msg) {
1171 AsyncResult ar = (AsyncResult) msg.obj;
1172 switch (msg.what) {
1173 case SUPPLY_PIN_COMPLETE:
1174 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
1175 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07001176 mRetryCount = msg.arg1;
1177 if (ar.exception != null) {
1178 if (ar.exception instanceof CommandException &&
1179 ((CommandException)(ar.exception)).getCommandError()
1180 == CommandException.Error.PASSWORD_INCORRECT) {
1181 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
1182 } else {
1183 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1184 }
1185 } else {
1186 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
1187 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001188 mDone = true;
1189 UnlockSim.this.notifyAll();
1190 }
1191 break;
1192 }
1193 }
1194 };
1195 UnlockSim.this.notifyAll();
1196 }
1197 Looper.loop();
1198 }
1199
1200 /*
1201 * Use PIN or PUK to unlock SIM card
1202 *
1203 * If PUK is null, unlock SIM card with PIN
1204 *
1205 * If PUK is not null, unlock SIM card with PUK and set PIN code
1206 */
Wink Saville9de0f752013-10-22 19:04:03 -07001207 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001208
1209 while (mHandler == null) {
1210 try {
1211 wait();
1212 } catch (InterruptedException e) {
1213 Thread.currentThread().interrupt();
1214 }
1215 }
1216 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
1217
1218 if (puk == null) {
1219 mSimCard.supplyPin(pin, callback);
1220 } else {
1221 mSimCard.supplyPuk(puk, pin, callback);
1222 }
1223
1224 while (!mDone) {
1225 try {
1226 Log.d(LOG_TAG, "wait for done");
1227 wait();
1228 } catch (InterruptedException e) {
1229 // Restore the interrupted status
1230 Thread.currentThread().interrupt();
1231 }
1232 }
1233 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07001234 int[] resultArray = new int[2];
1235 resultArray[0] = mResult;
1236 resultArray[1] = mRetryCount;
1237 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001238 }
1239 }
1240
1241 public void updateServiceLocation() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001242 updateServiceLocationForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001243
1244 }
1245
Wink Savilleb564aae2014-10-23 10:18:09 -07001246 public void updateServiceLocationForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001247 // No permission check needed here: this call is harmless, and it's
1248 // needed for the ServiceState.requestStateUpdate() call (which is
1249 // already intentionally exposed to 3rd parties.)
Sanket Padawe356d7632015-06-22 14:03:32 -07001250 final Phone phone = getPhone(subId);
1251 if (phone != null) {
1252 phone.updateServiceLocation();
1253 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001254 }
1255
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001256 @Override
1257 public boolean isRadioOn(String callingPackage) {
1258 return isRadioOnForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001259 }
1260
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001261 @Override
1262 public boolean isRadioOnForSubscriber(int subId, String callingPackage) {
1263 if (!canReadPhoneState(callingPackage, "isRadioOnForSubscriber")) {
1264 return false;
1265 }
1266 return isRadioOnForSubscriber(subId);
1267 }
1268
1269 private boolean isRadioOnForSubscriber(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001270 final Phone phone = getPhone(subId);
1271 if (phone != null) {
1272 return phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF;
1273 } else {
1274 return false;
1275 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001276 }
1277
1278 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001279 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001280
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001281 }
Wink Saville36469e72014-06-11 15:17:00 -07001282
Wink Savilleb564aae2014-10-23 10:18:09 -07001283 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001284 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001285 final Phone phone = getPhone(subId);
1286 if (phone != null) {
1287 phone.setRadioPower(!isRadioOnForSubscriber(subId));
1288 }
Wink Saville36469e72014-06-11 15:17:00 -07001289 }
1290
1291 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001292 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001293 }
1294
Wink Savilleb564aae2014-10-23 10:18:09 -07001295 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07001296 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001297 final Phone phone = getPhone(subId);
1298 if (phone == null) {
1299 return false;
1300 }
1301 if ((phone.getServiceState().getState() !=
Wink Saville36469e72014-06-11 15:17:00 -07001302 ServiceState.STATE_POWER_OFF) != turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001303 toggleRadioOnOffForSubscriber(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001304 }
1305 return true;
1306 }
Wink Saville36469e72014-06-11 15:17:00 -07001307
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001308 public boolean needMobileRadioShutdown() {
1309 /*
1310 * If any of the Radios are available, it will need to be
1311 * shutdown. So return true if any Radio is available.
1312 */
1313 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1314 Phone phone = PhoneFactory.getPhone(i);
1315 if (phone != null && phone.isRadioAvailable()) return true;
1316 }
1317 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
1318 return false;
1319 }
1320
1321 public void shutdownMobileRadios() {
1322 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1323 logv("Shutting down Phone " + i);
1324 shutdownRadioUsingPhoneId(i);
1325 }
1326 }
1327
1328 private void shutdownRadioUsingPhoneId(int phoneId) {
1329 enforceModifyPermission();
1330 Phone phone = PhoneFactory.getPhone(phoneId);
1331 if (phone != null && phone.isRadioAvailable()) {
1332 phone.shutdownRadio();
1333 }
1334 }
1335
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001336 public boolean setRadioPower(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001337 return setRadioPowerForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001338 }
1339
Wink Savilleb564aae2014-10-23 10:18:09 -07001340 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001341 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001342 final Phone phone = getPhone(subId);
1343 if (phone != null) {
1344 phone.setRadioPower(turnOn);
1345 return true;
1346 } else {
1347 return false;
1348 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001349 }
1350
Wink Saville36469e72014-06-11 15:17:00 -07001351 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001352 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001353 public boolean enableDataConnectivity() {
1354 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001355 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001356 final Phone phone = getPhone(subId);
1357 if (phone != null) {
1358 phone.setDataEnabled(true);
1359 return true;
1360 } else {
1361 return false;
1362 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001363 }
1364
Wink Saville36469e72014-06-11 15:17:00 -07001365 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001366 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001367 public boolean disableDataConnectivity() {
1368 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001369 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001370 final Phone phone = getPhone(subId);
1371 if (phone != null) {
1372 phone.setDataEnabled(false);
1373 return true;
1374 } else {
1375 return false;
1376 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001377 }
1378
Wink Saville36469e72014-06-11 15:17:00 -07001379 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001380 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001381 public boolean isDataConnectivityPossible() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001382 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001383 final Phone phone = getPhone(subId);
1384 if (phone != null) {
1385 return phone.isDataConnectivityPossible();
1386 } else {
1387 return false;
1388 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001389 }
1390
1391 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001392 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07001393 }
1394
Wink Savilleb564aae2014-10-23 10:18:09 -07001395 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001396 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001397 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1398 return false;
1399 }
Wink Saville36469e72014-06-11 15:17:00 -07001400 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001401 }
1402
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001403 public int getCallState() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001404 return getCallStateForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001405 }
1406
Shishir Agrawala9f32182016-04-12 12:00:16 -07001407 public int getCallStateForSlot(int slotId) {
1408 Phone phone = PhoneFactory.getPhone(slotId);
1409 return phone == null ? TelephonyManager.CALL_STATE_IDLE :
1410 DefaultPhoneNotifier.convertCallState(phone.getState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001411 }
1412
Sanket Padawe356d7632015-06-22 14:03:32 -07001413 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001414 public int getDataState() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001415 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001416 if (phone != null) {
1417 return DefaultPhoneNotifier.convertDataState(phone.getDataConnectionState());
1418 } else {
1419 return DefaultPhoneNotifier.convertDataState(PhoneConstants.DataState.DISCONNECTED);
1420 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001421 }
1422
Sanket Padawe356d7632015-06-22 14:03:32 -07001423 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001424 public int getDataActivity() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001425 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001426 if (phone != null) {
1427 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
1428 } else {
1429 return TelephonyManager.DATA_ACTIVITY_NONE;
1430 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001431 }
1432
1433 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001434 public Bundle getCellLocation(String callingPackage) {
1435 enforceFineOrCoarseLocationPermission("getCellLocation");
1436
1437 // OP_COARSE_LOCATION controls both fine and coarse location.
1438 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1439 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001440 log("getCellLocation: returning null; mode != allowed");
Svetoslav64fad262015-04-14 14:35:21 -07001441 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001442 }
1443
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001444 if (checkIfCallerIsSelfOrForegroundUser() ||
1445 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001446 if (DBG_LOC) log("getCellLocation: is active user");
1447 Bundle data = new Bundle();
Legler Wu2c01cdf2014-12-08 19:00:59 +08001448 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001449 if (phone == null) {
1450 return null;
1451 }
Legler Wu2c01cdf2014-12-08 19:00:59 +08001452 phone.getCellLocation().fillInNotifierBundle(data);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001453 return data;
1454 } else {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001455 log("getCellLocation: suppress non-active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001456 return null;
1457 }
1458 }
1459
Svetoslav64fad262015-04-14 14:35:21 -07001460 private void enforceFineOrCoarseLocationPermission(String message) {
1461 try {
1462 mApp.enforceCallingOrSelfPermission(
1463 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1464 } catch (SecurityException e) {
1465 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1466 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1467 // is the weaker precondition
1468 mApp.enforceCallingOrSelfPermission(
1469 android.Manifest.permission.ACCESS_COARSE_LOCATION, message);
1470 }
1471 }
1472
1473
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001474 @Override
1475 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001476 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001477 }
1478
Sanket Padawe356d7632015-06-22 14:03:32 -07001479 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001480 public void enableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001481 mApp.enforceCallingOrSelfPermission(
1482 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001483 final Phone phone = getPhone(subId);
1484 if (phone != null) {
1485 phone.enableLocationUpdates();
1486 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001487 }
1488
1489 @Override
1490 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001491 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001492 }
1493
Sanket Padawe356d7632015-06-22 14:03:32 -07001494 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001495 public void disableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001496 mApp.enforceCallingOrSelfPermission(
1497 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001498 final Phone phone = getPhone(subId);
1499 if (phone != null) {
1500 phone.disableLocationUpdates();
1501 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001502 }
1503
1504 @Override
1505 @SuppressWarnings("unchecked")
1506 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
Svetoslav64fad262015-04-14 14:35:21 -07001507 enforceFineOrCoarseLocationPermission("getNeighboringCellInfo");
1508
1509 // OP_COARSE_LOCATION controls both fine and coarse location.
1510 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1511 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1512 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001513 }
1514
1515 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
1516 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1517 return null;
1518 }
Svetoslav64fad262015-04-14 14:35:21 -07001519
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001520 if (checkIfCallerIsSelfOrForegroundUser() ||
1521 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001522 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
1523
1524 ArrayList<NeighboringCellInfo> cells = null;
1525
1526 try {
1527 cells = (ArrayList<NeighboringCellInfo>) sendRequest(
Sanket Padawe56e75a32016-02-08 12:18:19 -08001528 CMD_HANDLE_NEIGHBORING_CELL, null,
1529 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001530 } catch (RuntimeException e) {
Wink Saville36469e72014-06-11 15:17:00 -07001531 Log.e(LOG_TAG, "getNeighboringCellInfo " + e);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001532 }
1533 return cells;
1534 } else {
1535 if (DBG_LOC) log("getNeighboringCellInfo: suppress non-active user");
1536 return null;
1537 }
1538 }
1539
1540
1541 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001542 public List<CellInfo> getAllCellInfo(String callingPackage) {
1543 enforceFineOrCoarseLocationPermission("getAllCellInfo");
1544
1545 // OP_COARSE_LOCATION controls both fine and coarse location.
1546 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1547 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1548 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001549 }
1550
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001551 if (checkIfCallerIsSelfOrForegroundUser() ||
1552 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001553 if (DBG_LOC) log("getAllCellInfo: is active user");
Legler Wu2c01cdf2014-12-08 19:00:59 +08001554 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
1555 for (Phone phone : PhoneFactory.getPhones()) {
Robert Greenwaltd9eb7da2015-08-19 12:18:12 -07001556 final List<CellInfo> info = phone.getAllCellInfo();
1557 if (info != null) cellInfos.addAll(phone.getAllCellInfo());
Legler Wu2c01cdf2014-12-08 19:00:59 +08001558 }
1559 return cellInfos;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001560 } else {
1561 if (DBG_LOC) log("getAllCellInfo: suppress non-active user");
1562 return null;
1563 }
1564 }
1565
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001566 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001567 public void setCellInfoListRate(int rateInMillis) {
1568 mPhone.setCellInfoListRate(rateInMillis);
1569 }
1570
Shishir Agrawala9f32182016-04-12 12:00:16 -07001571 @Override
1572 public String getImeiForSlot(int slotId, String callingPackage) {
1573 if (!canReadPhoneState(callingPackage, "getImeiForSlot")) {
1574 return null;
1575 }
1576 Phone phone = PhoneFactory.getPhone(slotId);
1577 return phone == null ? null : phone.getImei();
1578 }
1579
1580 @Override
1581 public String getDeviceSoftwareVersionForSlot(int slotId, String callingPackage) {
1582 if (!canReadPhoneState(callingPackage, "getDeviceSoftwareVersionForSlot")) {
1583 return null;
1584 }
1585 Phone phone = PhoneFactory.getPhone(slotId);
1586 return phone == null ? null : phone.getDeviceSvn();
1587 }
1588
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001589 //
1590 // Internal helper methods.
1591 //
1592
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001593 /**
1594 * Returns true if the caller holds INTERACT_ACROSS_USERS_FULL.
1595 */
1596 private boolean checkCallerInteractAcrossUsersFull() {
1597 return mPhone.getContext().checkCallingOrSelfPermission(
1598 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1599 == PackageManager.PERMISSION_GRANTED;
1600 }
1601
Jake Hambye994d462014-02-03 13:10:13 -08001602 private static boolean checkIfCallerIsSelfOrForegroundUser() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001603 boolean ok;
1604
1605 boolean self = Binder.getCallingUid() == Process.myUid();
1606 if (!self) {
1607 // Get the caller's user id then clear the calling identity
1608 // which will be restored in the finally clause.
1609 int callingUser = UserHandle.getCallingUserId();
1610 long ident = Binder.clearCallingIdentity();
1611
1612 try {
1613 // With calling identity cleared the current user is the foreground user.
1614 int foregroundUser = ActivityManager.getCurrentUser();
1615 ok = (foregroundUser == callingUser);
1616 if (DBG_LOC) {
1617 log("checkIfCallerIsSelfOrForegoundUser: foregroundUser=" + foregroundUser
1618 + " callingUser=" + callingUser + " ok=" + ok);
1619 }
1620 } catch (Exception ex) {
1621 if (DBG_LOC) loge("checkIfCallerIsSelfOrForegoundUser: Exception ex=" + ex);
1622 ok = false;
1623 } finally {
1624 Binder.restoreCallingIdentity(ident);
1625 }
1626 } else {
1627 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: is self");
1628 ok = true;
1629 }
1630 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: ret=" + ok);
1631 return ok;
1632 }
1633
1634 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001635 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1636 *
1637 * @throws SecurityException if the caller does not have the required permission
1638 */
1639 private void enforceModifyPermission() {
1640 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1641 }
1642
1643 /**
Junda Liua2e36012014-07-09 18:30:01 -07001644 * Make sure either system app or the caller has carrier privilege.
1645 *
1646 * @throws SecurityException if the caller does not have the required permission/privilege
1647 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001648 private void enforceModifyPermissionOrCarrierPrivilege(int subId) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001649 int permission = mApp.checkCallingOrSelfPermission(
1650 android.Manifest.permission.MODIFY_PHONE_STATE);
1651 if (permission == PackageManager.PERMISSION_GRANTED) {
1652 return;
1653 }
1654
1655 log("No modify permission, check carrier privilege next.");
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001656 enforceCarrierPrivilege(subId);
Junda Liua2e36012014-07-09 18:30:01 -07001657 }
1658
1659 /**
1660 * Make sure the caller has carrier privilege.
1661 *
1662 * @throws SecurityException if the caller does not have the required permission
1663 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001664 private void enforceCarrierPrivilege(int subId) {
1665 if (getCarrierPrivilegeStatus(subId) !=
1666 TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001667 loge("No Carrier Privilege.");
1668 throw new SecurityException("No Carrier Privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001669 }
1670 }
1671
1672 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001673 * Make sure the caller has the CALL_PHONE permission.
1674 *
1675 * @throws SecurityException if the caller does not have the required permission
1676 */
1677 private void enforceCallPermission() {
1678 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1679 }
1680
Stuart Scott8eef64f2015-04-08 15:13:54 -07001681 private void enforceConnectivityInternalPermission() {
1682 mApp.enforceCallingOrSelfPermission(
1683 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1684 "ConnectivityService");
1685 }
1686
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001687 private String createTelUrl(String number) {
1688 if (TextUtils.isEmpty(number)) {
1689 return null;
1690 }
1691
Jake Hambye994d462014-02-03 13:10:13 -08001692 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001693 }
1694
Ihab Awadf9e92732013-12-05 18:02:52 -08001695 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001696 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1697 }
1698
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001699 private static void logv(String msg) {
1700 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
1701 }
1702
Ihab Awadf9e92732013-12-05 18:02:52 -08001703 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001704 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1705 }
1706
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001707 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001708 public int getActivePhoneType() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001709 return getActivePhoneTypeForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001710 }
1711
Sanket Padawe356d7632015-06-22 14:03:32 -07001712 @Override
Shishir Agrawala9f32182016-04-12 12:00:16 -07001713 public int getActivePhoneTypeForSlot(int slotId) {
1714 final Phone phone = PhoneFactory.getPhone(slotId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001715 if (phone == null) {
1716 return PhoneConstants.PHONE_TYPE_NONE;
1717 } else {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001718 return phone.getPhoneType();
Sanket Padawe356d7632015-06-22 14:03:32 -07001719 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001720 }
1721
1722 /**
1723 * Returns the CDMA ERI icon index to display
1724 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001725 @Override
1726 public int getCdmaEriIconIndex(String callingPackage) {
1727 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001728 }
1729
Sanket Padawe356d7632015-06-22 14:03:32 -07001730 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001731 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage) {
1732 if (!canReadPhoneState(callingPackage, "getCdmaEriIconIndexForSubscriber")) {
1733 return -1;
1734 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001735 final Phone phone = getPhone(subId);
1736 if (phone != null) {
1737 return phone.getCdmaEriIconIndex();
1738 } else {
1739 return -1;
1740 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001741 }
1742
1743 /**
1744 * Returns the CDMA ERI icon mode,
1745 * 0 - ON
1746 * 1 - FLASHING
1747 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001748 @Override
1749 public int getCdmaEriIconMode(String callingPackage) {
1750 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001751 }
1752
Sanket Padawe356d7632015-06-22 14:03:32 -07001753 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001754 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage) {
1755 if (!canReadPhoneState(callingPackage, "getCdmaEriIconModeForSubscriber")) {
1756 return -1;
1757 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001758 final Phone phone = getPhone(subId);
1759 if (phone != null) {
1760 return phone.getCdmaEriIconMode();
1761 } else {
1762 return -1;
1763 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001764 }
1765
1766 /**
1767 * Returns the CDMA ERI text,
1768 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001769 @Override
1770 public String getCdmaEriText(String callingPackage) {
1771 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001772 }
1773
Sanket Padawe356d7632015-06-22 14:03:32 -07001774 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001775 public String getCdmaEriTextForSubscriber(int subId, String callingPackage) {
1776 if (!canReadPhoneState(callingPackage, "getCdmaEriIconTextForSubscriber")) {
1777 return null;
1778 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001779 final Phone phone = getPhone(subId);
1780 if (phone != null) {
1781 return phone.getCdmaEriText();
1782 } else {
1783 return null;
1784 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001785 }
1786
1787 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07001788 * Returns the CDMA MDN.
1789 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001790 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001791 public String getCdmaMdn(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001792 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001793 final Phone phone = getPhone(subId);
1794 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA && phone != null) {
1795 return phone.getLine1Number();
Junda Liuca05d5d2014-08-14 22:36:34 -07001796 } else {
1797 return null;
1798 }
1799 }
1800
1801 /**
1802 * Returns the CDMA MIN.
1803 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001804 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001805 public String getCdmaMin(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001806 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001807 final Phone phone = getPhone(subId);
1808 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1809 return phone.getCdmaMin();
Junda Liuca05d5d2014-08-14 22:36:34 -07001810 } else {
1811 return null;
1812 }
1813 }
1814
1815 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001816 * Returns true if CDMA provisioning needs to run.
1817 */
1818 public boolean needsOtaServiceProvisioning() {
1819 return mPhone.needsOtaServiceProvisioning();
1820 }
1821
1822 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001823 * Sets the voice mail number of a given subId.
1824 */
1825 @Override
1826 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001827 enforceCarrierPrivilege(subId);
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001828 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
1829 new Pair<String, String>(alphaTag, number), new Integer(subId));
1830 return success;
1831 }
1832
Ta-wei Yen87c49842016-05-13 21:19:52 -07001833 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07001834 public void enableVisualVoicemailSmsFilter(String callingPackage, int subId,
1835 VisualVoicemailSmsFilterSettings settings) {
1836 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001837 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07001838 .enableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId,
1839 settings);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001840 }
1841
1842 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07001843 public void disableVisualVoicemailSmsFilter(String callingPackage, int subId) {
1844 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001845 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07001846 .disableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001847 }
1848
1849 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07001850 public VisualVoicemailSmsFilterSettings getVisualVoicemailSmsFilterSettings(
1851 String callingPackage, int subId) {
1852 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001853 return VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07001854 .getVisualVoicemailSmsFilterSettings(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001855 }
1856
1857 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07001858 public VisualVoicemailSmsFilterSettings getSystemVisualVoicemailSmsFilterSettings(
1859 String packageName, int subId) {
1860 enforceReadPrivilegedPermission();
Ta-wei Yen87c49842016-05-13 21:19:52 -07001861 return VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07001862 .getVisualVoicemailSmsFilterSettings(mPhone.getContext(), packageName, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001863 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001864 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001865 * Returns the unread count of voicemails
1866 */
1867 public int getVoiceMessageCount() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001868 return getVoiceMessageCountForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001869 }
1870
1871 /**
1872 * Returns the unread count of voicemails for a subId
1873 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001874 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001875 public int getVoiceMessageCountForSubscriber( int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001876 final Phone phone = getPhone(subId);
1877 if (phone != null) {
1878 return phone.getVoiceMessageCount();
1879 } else {
1880 return 0;
1881 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001882 }
1883
1884 /**
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001885 * Returns the data network type.
1886 * Legacy call, permission-free.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001887 *
1888 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
1889 */
1890 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001891 public int getNetworkType() {
1892 final Phone phone = getPhone(getDefaultSubscription());
1893 if (phone != null) {
1894 return phone.getServiceState().getDataNetworkType();
1895 } else {
1896 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1897 }
Wink Saville36469e72014-06-11 15:17:00 -07001898 }
1899
1900 /**
1901 * Returns the network type for a subId
1902 */
1903 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001904 public int getNetworkTypeForSubscriber(int subId, String callingPackage) {
1905 if (!canReadPhoneState(callingPackage, "getNetworkTypeForSubscriber")) {
1906 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1907 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001908
Sanket Padawe356d7632015-06-22 14:03:32 -07001909 final Phone phone = getPhone(subId);
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001910 if (phone != null) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001911 return phone.getServiceState().getDataNetworkType();
1912 } else {
1913 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1914 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001915 }
1916
1917 /**
1918 * Returns the data network type
1919 */
1920 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001921 public int getDataNetworkType(String callingPackage) {
1922 return getDataNetworkTypeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001923 }
1924
1925 /**
1926 * Returns the data network type for a subId
1927 */
1928 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001929 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage) {
1930 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
1931 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1932 }
1933
Sanket Padawe356d7632015-06-22 14:03:32 -07001934 final Phone phone = getPhone(subId);
1935 if (phone != null) {
1936 return phone.getServiceState().getDataNetworkType();
1937 } else {
1938 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1939 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001940 }
1941
1942 /**
Wink Saville36469e72014-06-11 15:17:00 -07001943 * Returns the Voice network type for a subId
1944 */
1945 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001946 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage) {
1947 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
1948 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1949 }
1950
Sanket Padawe356d7632015-06-22 14:03:32 -07001951 final Phone phone = getPhone(subId);
1952 if (phone != null) {
1953 return phone.getServiceState().getVoiceNetworkType();
1954 } else {
1955 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1956 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001957 }
1958
1959 /**
1960 * @return true if a ICC card is present
1961 */
1962 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07001963 // FIXME Make changes to pass defaultSimId of type int
Amit Mahajana6fc2a82015-01-06 11:53:51 -08001964 return hasIccCardUsingSlotId(mSubscriptionController.getSlotId(getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07001965 }
1966
1967 /**
1968 * @return true if a ICC card is present for a slotId
1969 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001970 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001971 public boolean hasIccCardUsingSlotId(int slotId) {
Amit Mahajana6fc2a82015-01-06 11:53:51 -08001972 int subId[] = mSubscriptionController.getSubIdUsingSlotId(slotId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001973 final Phone phone = getPhone(subId[0]);
1974 if (subId != null && phone != null) {
1975 return phone.getIccCard().hasIccCard();
Amit Mahajana6fc2a82015-01-06 11:53:51 -08001976 } else {
1977 return false;
1978 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001979 }
1980
1981 /**
1982 * Return if the current radio is LTE on CDMA. This
1983 * is a tri-state return value as for a period of time
1984 * the mode may be unknown.
1985 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001986 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001987 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08001988 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001989 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001990 @Override
1991 public int getLteOnCdmaMode(String callingPackage) {
1992 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001993 }
1994
Sanket Padawe356d7632015-06-22 14:03:32 -07001995 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001996 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage) {
1997 if (!canReadPhoneState(callingPackage, "getLteOnCdmaModeForSubscriber")) {
1998 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
1999 }
2000
Sanket Padawe356d7632015-06-22 14:03:32 -07002001 final Phone phone = getPhone(subId);
2002 if (phone == null) {
2003 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2004 } else {
2005 return phone.getLteOnCdmaMode();
2006 }
Wink Saville36469e72014-06-11 15:17:00 -07002007 }
2008
2009 public void setPhone(Phone phone) {
2010 mPhone = phone;
2011 }
2012
2013 /**
2014 * {@hide}
2015 * Returns Default subId, 0 in the case of single standby.
2016 */
Wink Savilleb564aae2014-10-23 10:18:09 -07002017 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002018 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07002019 }
2020
Shishir Agrawala9f32182016-04-12 12:00:16 -07002021 private int getSlotForDefaultSubscription() {
2022 return mSubscriptionController.getPhoneId(getDefaultSubscription());
2023 }
2024
Wink Savilleb564aae2014-10-23 10:18:09 -07002025 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002026 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002027 }
Ihab Awadf2177b72013-11-25 13:33:23 -08002028
2029 /**
2030 * @see android.telephony.TelephonyManager.WifiCallingChoices
2031 */
2032 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002033 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
2034 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08002035 }
2036
2037 /**
2038 * @see android.telephony.TelephonyManager.WifiCallingChoices
2039 */
2040 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002041 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
2042 Settings.System.putInt(mPhone.getContext().getContentResolver(),
2043 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08002044 }
2045
Sailesh Nepald1e68152013-12-12 19:08:02 -08002046 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07002047 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08002048 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08002049 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08002050
Shishir Agrawal566b7612013-10-28 14:41:00 -07002051 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002052 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(int subId, String AID) {
2053 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002054
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002055 if (DBG) log("iccOpenLogicalChannel: subId=" + subId + " aid=" + AID);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002056 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002057 CMD_OPEN_CHANNEL, AID, subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002058 if (DBG) log("iccOpenLogicalChannel: " + response);
2059 return response;
Shishir Agrawal566b7612013-10-28 14:41:00 -07002060 }
2061
2062 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002063 public boolean iccCloseLogicalChannel(int subId, int channel) {
2064 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002065
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002066 if (DBG) log("iccCloseLogicalChannel: subId=" + subId + " chnl=" + channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002067 if (channel < 0) {
2068 return false;
2069 }
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002070 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel, subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002071 if (DBG) log("iccCloseLogicalChannel: " + success);
2072 return success;
2073 }
2074
2075 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002076 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07002077 int command, int p1, int p2, int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002078 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002079
2080 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002081 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel +
2082 " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
Shishir Agrawal566b7612013-10-28 14:41:00 -07002083 " data=" + data);
2084 }
2085
2086 if (channel < 0) {
2087 return "";
2088 }
2089
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002090 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002091 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002092 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
2093
Shishir Agrawal566b7612013-10-28 14:41:00 -07002094 // Append the returned status code to the end of the response payload.
2095 String s = Integer.toHexString(
2096 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002097 if (response.payload != null) {
2098 s = IccUtils.bytesToHexString(response.payload) + s;
2099 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07002100 return s;
2101 }
Jake Hambye994d462014-02-03 13:10:13 -08002102
Evan Charltonc66da362014-05-16 14:06:40 -07002103 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002104 public String iccTransmitApduBasicChannel(int subId, int cla, int command, int p1, int p2,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002105 int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002106 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002107
2108 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002109 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd=" + command
2110 + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002111 }
2112
2113 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002114 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002115 if (DBG) log("iccTransmitApduBasicChannel: " + response);
2116
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002117 // Append the returned status code to the end of the response payload.
2118 String s = Integer.toHexString(
2119 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002120 if (response.payload != null) {
2121 s = IccUtils.bytesToHexString(response.payload) + s;
2122 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002123 return s;
2124 }
2125
2126 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002127 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002128 String filePath) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002129 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002130
2131 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002132 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " " +
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002133 p1 + " " + p2 + " " + p3 + ":" + filePath);
2134 }
2135
2136 IccIoResult response =
2137 (IccIoResult)sendRequest(CMD_EXCHANGE_SIM_IO,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002138 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
2139 subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002140
2141 if (DBG) {
2142 log("Exchange SIM_IO [R]" + response);
2143 }
2144
2145 byte[] result = null;
2146 int length = 2;
2147 if (response.payload != null) {
2148 length = 2 + response.payload.length;
2149 result = new byte[length];
2150 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
2151 } else {
2152 result = new byte[length];
2153 }
2154
2155 result[length - 1] = (byte) response.sw2;
2156 result[length - 2] = (byte) response.sw1;
2157 return result;
2158 }
2159
2160 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002161 public String sendEnvelopeWithStatus(int subId, String content) {
2162 enforceModifyPermissionOrCarrierPrivilege(subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002163
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002164 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content, subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002165 if (response.payload == null) {
2166 return "";
2167 }
2168
2169 // Append the returned status code to the end of the response payload.
2170 String s = Integer.toHexString(
2171 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
2172 s = IccUtils.bytesToHexString(response.payload) + s;
2173 return s;
2174 }
2175
Jake Hambye994d462014-02-03 13:10:13 -08002176 /**
2177 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2178 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2179 *
2180 * @param itemID the ID of the item to read
2181 * @return the NV item as a String, or null on error.
2182 */
2183 @Override
2184 public String nvReadItem(int itemID) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002185 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002186 if (DBG) log("nvReadItem: item " + itemID);
2187 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
2188 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
2189 return value;
2190 }
2191
2192 /**
2193 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2194 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2195 *
2196 * @param itemID the ID of the item to read
2197 * @param itemValue the value to write, as a String
2198 * @return true on success; false on any failure
2199 */
2200 @Override
2201 public boolean nvWriteItem(int itemID, String itemValue) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002202 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002203 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
2204 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
2205 new Pair<Integer, String>(itemID, itemValue));
2206 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
2207 return success;
2208 }
2209
2210 /**
2211 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
2212 * Used for device configuration by some CDMA operators.
2213 *
2214 * @param preferredRoamingList byte array containing the new PRL
2215 * @return true on success; false on any failure
2216 */
2217 @Override
2218 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002219 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002220 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
2221 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
2222 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
2223 return success;
2224 }
2225
2226 /**
2227 * Perform the specified type of NV config reset.
2228 * Used for device configuration by some CDMA operators.
2229 *
2230 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
2231 * @return true on success; false on any failure
2232 */
2233 @Override
2234 public boolean nvResetConfig(int resetType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002235 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002236 if (DBG) log("nvResetConfig: type " + resetType);
2237 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
2238 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
2239 return success;
2240 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002241
2242 /**
Wink Saville36469e72014-06-11 15:17:00 -07002243 * {@hide}
2244 * Returns Default sim, 0 in the case of single standby.
2245 */
2246 public int getDefaultSim() {
2247 //TODO Need to get it from Telephony Devcontroller
2248 return 0;
2249 }
2250
Svet Ganovb320e182015-04-16 12:30:10 -07002251 public String[] getPcscfAddress(String apnType, String callingPackage) {
2252 if (!canReadPhoneState(callingPackage, "getPcscfAddress")) {
2253 return new String[0];
2254 }
2255
2256
ram87fca6f2014-07-18 18:58:44 +05302257 return mPhone.getPcscfAddress(apnType);
Wink Saville36469e72014-06-11 15:17:00 -07002258 }
2259
2260 public void setImsRegistrationState(boolean registered) {
2261 enforceModifyPermission();
2262 mPhone.setImsRegistrationState(registered);
2263 }
2264
2265 /**
Stuart Scott54788802015-03-30 13:18:01 -07002266 * Set the network selection mode to automatic.
2267 *
2268 */
2269 @Override
2270 public void setNetworkSelectionModeAutomatic(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002271 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002272 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
2273 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
2274 }
2275
2276 /**
Shishir Agrawal302c8692015-06-19 13:49:39 -07002277 * Set the network selection mode to manual with the selected carrier.
2278 */
2279 @Override
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002280 public boolean setNetworkSelectionModeManual(int subId, OperatorInfo operator,
2281 boolean persistSelection) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002282 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002283 if (DBG) log("setNetworkSelectionModeManual: subId:" + subId + " operator:" + operator);
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002284 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operator,
2285 persistSelection);
2286 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002287 }
2288
2289 /**
2290 * Scans for available networks.
2291 */
2292 @Override
2293 public CellNetworkScanResult getCellNetworkScanResults(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002294 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002295 if (DBG) log("getCellNetworkScanResults: subId " + subId);
2296 CellNetworkScanResult result = (CellNetworkScanResult) sendRequest(
2297 CMD_PERFORM_NETWORK_SCAN, null, subId);
2298 return result;
2299 }
2300
2301 /**
Junda Liu84d15a22014-07-02 11:21:04 -07002302 * Get the calculated preferred network type.
2303 * Used for debugging incorrect network type.
2304 *
2305 * @return the preferred network type, defined in RILConstants.java.
2306 */
2307 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002308 public int getCalculatedPreferredNetworkType(String callingPackage) {
2309 if (!canReadPhoneState(callingPackage, "getCalculatedPreferredNetworkType")) {
2310 return RILConstants.PREFERRED_NETWORK_MODE;
2311 }
2312
Amit Mahajan43330e02014-11-18 11:54:45 -08002313 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext(), 0); // wink FIXME: need to get SubId from somewhere.
Junda Liu84d15a22014-07-02 11:21:04 -07002314 }
2315
2316 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08002317 * Get the preferred network type.
2318 * Used for device configuration by some CDMA operators.
2319 *
2320 * @return the preferred network type, defined in RILConstants.java.
2321 */
2322 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002323 public int getPreferredNetworkType(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002324 enforceModifyPermissionOrCarrierPrivilege(subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002325 if (DBG) log("getPreferredNetworkType");
Stuart Scott54788802015-03-30 13:18:01 -07002326 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002327 int networkType = (result != null ? result[0] : -1);
2328 if (DBG) log("getPreferredNetworkType: " + networkType);
2329 return networkType;
2330 }
2331
2332 /**
2333 * Set the preferred network type.
2334 * Used for device configuration by some CDMA operators.
2335 *
2336 * @param networkType the preferred network type, defined in RILConstants.java.
2337 * @return true on success; false on any failure.
2338 */
2339 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002340 public boolean setPreferredNetworkType(int subId, int networkType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002341 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002342 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
2343 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002344 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
Junda Liu80bc0d12014-07-14 16:36:44 -07002345 if (success) {
2346 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
Stuart Scott54788802015-03-30 13:18:01 -07002347 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
Junda Liu80bc0d12014-07-14 16:36:44 -07002348 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002349 return success;
2350 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002351
2352 /**
Junda Liu475951f2014-11-07 16:45:03 -08002353 * Check TETHER_DUN_REQUIRED and TETHER_DUN_APN settings, net.tethering.noprovisioning
2354 * SystemProperty, and config_tether_apndata to decide whether DUN APN is required for
2355 * tethering.
2356 *
2357 * @return 0: Not required. 1: required. 2: Not set.
2358 * @hide
2359 */
2360 @Override
2361 public int getTetherApnRequired() {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002362 enforceModifyPermission();
Junda Liu475951f2014-11-07 16:45:03 -08002363 int dunRequired = Settings.Global.getInt(mPhone.getContext().getContentResolver(),
2364 Settings.Global.TETHER_DUN_REQUIRED, 2);
2365 // If not set, check net.tethering.noprovisioning, TETHER_DUN_APN setting and
2366 // config_tether_apndata.
2367 if (dunRequired == 2 && mPhone.hasMatchedTetherApnSetting()) {
2368 dunRequired = 1;
2369 }
2370 return dunRequired;
2371 }
2372
2373 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07002374 * Set mobile data enabled
2375 * Used by the user through settings etc to turn on/off mobile data
2376 *
2377 * @param enable {@code true} turn turn data on, else {@code false}
2378 */
2379 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002380 public void setDataEnabled(int subId, boolean enable) {
Robert Greenwalted86e582014-05-21 20:03:20 -07002381 enforceModifyPermission();
Wink Savillee7353bb2014-12-05 14:21:41 -08002382 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002383 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002384 Phone phone = PhoneFactory.getPhone(phoneId);
2385 if (phone != null) {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002386 if (DBG) log("setDataEnabled: subId=" + subId + " enable=" + enable);
Wink Savillee7353bb2014-12-05 14:21:41 -08002387 phone.setDataEnabled(enable);
2388 } else {
2389 loge("setDataEnabled: no phone for subId=" + subId);
2390 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002391 }
2392
2393 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07002394 * Get whether mobile data is enabled.
2395 *
2396 * Note that this used to be available from ConnectivityService, gated by
2397 * ACCESS_NETWORK_STATE permission, so this will accept either that or
2398 * our MODIFY_PHONE_STATE.
Robert Greenwalted86e582014-05-21 20:03:20 -07002399 *
2400 * @return {@code true} if data is enabled else {@code false}
2401 */
2402 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002403 public boolean getDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07002404 try {
2405 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
2406 null);
2407 } catch (Exception e) {
2408 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE,
2409 null);
2410 }
Wink Savillee7353bb2014-12-05 14:21:41 -08002411 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002412 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002413 Phone phone = PhoneFactory.getPhone(phoneId);
2414 if (phone != null) {
2415 boolean retVal = phone.getDataEnabled();
Joe Onoratoa601dd22016-02-23 13:03:53 -08002416 if (DBG) log("getDataEnabled: subId=" + subId + " retVal=" + retVal);
Wink Savillee7353bb2014-12-05 14:21:41 -08002417 return retVal;
2418 } else {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002419 if (DBG) loge("getDataEnabled: no phone subId=" + subId + " retVal=false");
Wink Savillee7353bb2014-12-05 14:21:41 -08002420 return false;
2421 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002422 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002423
2424 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002425 public int getCarrierPrivilegeStatus(int subId) {
2426 final Phone phone = getPhone(subId);
2427 if (phone == null) {
2428 loge("getCarrierPrivilegeStatus: Invalid subId");
2429 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
2430 }
2431 UiccCard card = UiccController.getInstance().getUiccCard(phone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002432 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002433 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002434 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2435 }
2436 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002437 phone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002438 }
Junda Liu29340342014-07-10 15:23:27 -07002439
2440 @Override
Zach Johnson50ecba32015-05-19 00:24:21 -07002441 public int checkCarrierPrivilegesForPackage(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002442 if (TextUtils.isEmpty(pkgName))
2443 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Shishir Agrawal21409252015-01-15 23:33:50 -08002444 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002445 if (card == null) {
2446 loge("checkCarrierPrivilegesForPackage: No UICC");
2447 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2448 }
Zach Johnson50ecba32015-05-19 00:24:21 -07002449 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgName);
2450 }
2451
2452 @Override
2453 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002454 if (TextUtils.isEmpty(pkgName))
2455 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Zach Johnson50ecba32015-05-19 00:24:21 -07002456 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2457 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2458 UiccCard card = UiccController.getInstance().getUiccCard(i);
2459 if (card == null) {
Jonathan Basseri7d320df2015-06-16 12:17:08 -07002460 // No UICC in that slot.
Zach Johnson50ecba32015-05-19 00:24:21 -07002461 continue;
2462 }
2463
2464 result = card.getCarrierPrivilegeStatus(
2465 mPhone.getContext().getPackageManager(), pkgName);
2466 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2467 break;
2468 }
2469 }
2470
2471 return result;
Junda Liu29340342014-07-10 15:23:27 -07002472 }
Derek Tan89e89d42014-07-08 17:00:10 -07002473
2474 @Override
Junda Liue64de782015-04-16 17:19:16 -07002475 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
2476 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
2477 loge("phoneId " + phoneId + " is not valid.");
2478 return null;
2479 }
2480 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002481 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002482 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002483 return null ;
2484 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002485 return card.getCarrierPackageNamesForIntent(
Svetoslav483aff72015-04-21 14:16:07 -07002486 mPhone.getContext().getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002487 }
2488
Amith Yamasani6e118872016-02-19 12:53:51 -08002489 @Override
2490 public List<String> getPackagesWithCarrierPrivileges() {
2491 PackageManager pm = mPhone.getContext().getPackageManager();
2492 List<String> privilegedPackages = new ArrayList<>();
2493 List<PackageInfo> packages = null;
2494 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2495 UiccCard card = UiccController.getInstance().getUiccCard(i);
2496 if (card == null) {
2497 // No UICC in that slot.
2498 continue;
2499 }
2500 if (card.hasCarrierPrivilegeRules()) {
2501 if (packages == null) {
2502 // Only check packages in user 0 for now
2503 packages = pm.getInstalledPackagesAsUser(
2504 PackageManager.MATCH_DISABLED_COMPONENTS
2505 | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS
2506 | PackageManager.GET_SIGNATURES, UserHandle.USER_SYSTEM);
2507 }
2508 for (int p = packages.size() - 1; p >= 0; p--) {
2509 PackageInfo pkgInfo = packages.get(p);
2510 if (pkgInfo != null && pkgInfo.packageName != null
2511 && card.getCarrierPrivilegeStatus(pkgInfo)
2512 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2513 privilegedPackages.add(pkgInfo.packageName);
2514 }
2515 }
2516 }
2517 }
2518 return privilegedPackages;
2519 }
2520
Wink Savilleb564aae2014-10-23 10:18:09 -07002521 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002522 final Phone phone = getPhone(subId);
2523 UiccCard card = phone == null ? null : phone.getUiccCard();
Derek Tan97ebb422014-09-05 16:55:38 -07002524 if (card == null) {
2525 loge("getIccId: No UICC");
2526 return null;
2527 }
2528 String iccId = card.getIccId();
2529 if (TextUtils.isEmpty(iccId)) {
2530 loge("getIccId: ICC ID is null or empty.");
2531 return null;
2532 }
2533 return iccId;
2534 }
2535
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002536 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002537 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
2538 String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002539 enforceCarrierPrivilege(subId);
Derek Tan97ebb422014-09-05 16:55:38 -07002540
Jeff Sharkey85190e62014-12-05 09:40:12 -08002541 final String iccId = getIccId(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07002542 final Phone phone = getPhone(subId);
2543 if (phone == null) {
2544 return false;
2545 }
2546 final String subscriberId = phone.getSubscriberId();
Jeff Sharkey85190e62014-12-05 09:40:12 -08002547
2548 if (DBG_MERGE) {
2549 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
2550 + subscriberId + " to " + number);
2551 }
2552
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002553 if (TextUtils.isEmpty(iccId)) {
2554 return false;
Derek Tan97ebb422014-09-05 16:55:38 -07002555 }
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002556
Jeff Sharkey85190e62014-12-05 09:40:12 -08002557 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2558
2559 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002560 if (alphaTag == null) {
2561 editor.remove(alphaTagPrefKey);
2562 } else {
2563 editor.putString(alphaTagPrefKey, alphaTag);
2564 }
2565
Jeff Sharkey85190e62014-12-05 09:40:12 -08002566 // Record both the line number and IMSI for this ICCID, since we need to
2567 // track all merged IMSIs based on line number
2568 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2569 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002570 if (number == null) {
2571 editor.remove(numberPrefKey);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002572 editor.remove(subscriberPrefKey);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002573 } else {
2574 editor.putString(numberPrefKey, number);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002575 editor.putString(subscriberPrefKey, subscriberId);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002576 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002577
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002578 editor.commit();
2579 return true;
Derek Tan7226c842014-07-02 17:42:23 -07002580 }
2581
2582 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002583 public String getLine1NumberForDisplay(int subId, String callingPackage) {
Makoto Onukifee69342015-06-29 14:44:50 -07002584 // This is open to apps with WRITE_SMS.
2585 if (!canReadPhoneNumber(callingPackage, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08002586 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07002587 return null;
2588 }
Derek Tan97ebb422014-09-05 16:55:38 -07002589
2590 String iccId = getIccId(subId);
2591 if (iccId != null) {
2592 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
Amit Mahajan9cf11512015-11-09 11:40:48 -08002593 if (DBG_MERGE) {
2594 log("getLine1NumberForDisplay returning " +
2595 mTelephonySharedPreferences.getString(numberPrefKey, null));
2596 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002597 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002598 }
Amit Mahajan9cf11512015-11-09 11:40:48 -08002599 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
Derek Tan97ebb422014-09-05 16:55:38 -07002600 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002601 }
2602
2603 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002604 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
2605 if (!canReadPhoneState(callingPackage, "getLine1AlphaTagForDisplay")) {
2606 return null;
2607 }
Derek Tan97ebb422014-09-05 16:55:38 -07002608
2609 String iccId = getIccId(subId);
2610 if (iccId != null) {
2611 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002612 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002613 }
Derek Tan97ebb422014-09-05 16:55:38 -07002614 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002615 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002616
2617 @Override
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002618 public String[] getMergedSubscriberIds(String callingPackage) {
2619 if (!canReadPhoneState(callingPackage, "getMergedSubscriberIds")) {
2620 return null;
2621 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002622 final Context context = mPhone.getContext();
2623 final TelephonyManager tele = TelephonyManager.from(context);
2624 final SubscriptionManager sub = SubscriptionManager.from(context);
2625
2626 // Figure out what subscribers are currently active
2627 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002628 // Clear calling identity, when calling TelephonyManager, because callerUid must be
2629 // the process, where TelephonyManager was instantiated. Otherwise AppOps check will fail.
2630 final long identity = Binder.clearCallingIdentity();
2631 try {
2632 final int[] subIds = sub.getActiveSubscriptionIdList();
2633 for (int subId : subIds) {
2634 activeSubscriberIds.add(tele.getSubscriberId(subId));
2635 }
2636 } finally {
2637 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002638 }
2639
2640 // First pass, find a number override for an active subscriber
2641 String mergeNumber = null;
2642 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
2643 for (String key : prefs.keySet()) {
2644 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
2645 final String subscriberId = (String) prefs.get(key);
2646 if (activeSubscriberIds.contains(subscriberId)) {
2647 final String iccId = key.substring(PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
2648 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2649 mergeNumber = (String) prefs.get(numberKey);
2650 if (DBG_MERGE) {
2651 Slog.d(LOG_TAG, "Found line number " + mergeNumber
2652 + " for active subscriber " + subscriberId);
2653 }
2654 if (!TextUtils.isEmpty(mergeNumber)) {
2655 break;
2656 }
2657 }
2658 }
2659 }
2660
2661 // Shortcut when no active merged subscribers
2662 if (TextUtils.isEmpty(mergeNumber)) {
2663 return null;
2664 }
2665
2666 // Second pass, find all subscribers under that line override
2667 final ArraySet<String> result = new ArraySet<>();
2668 for (String key : prefs.keySet()) {
2669 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
2670 final String number = (String) prefs.get(key);
2671 if (mergeNumber.equals(number)) {
2672 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
2673 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
2674 final String subscriberId = (String) prefs.get(subscriberKey);
2675 if (!TextUtils.isEmpty(subscriberId)) {
2676 result.add(subscriberId);
2677 }
2678 }
2679 }
2680 }
2681
2682 final String[] resultArray = result.toArray(new String[result.size()]);
2683 Arrays.sort(resultArray);
2684 if (DBG_MERGE) {
2685 Slog.d(LOG_TAG, "Found subscribers " + Arrays.toString(resultArray) + " after merge");
2686 }
2687 return resultArray;
2688 }
2689
2690 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002691 public boolean setOperatorBrandOverride(int subId, String brand) {
2692 enforceCarrierPrivilege(subId);
2693 final Phone phone = getPhone(subId);
2694 return phone == null ? false : phone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002695 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05002696
2697 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002698 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002699 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
2700 List<String> cdmaNonRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002701 enforceCarrierPrivilege(subId);
2702 final Phone phone = getPhone(subId);
2703 if (phone == null) {
2704 return false;
2705 }
2706 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002707 cdmaNonRoamingList);
2708 }
2709
2710 @Override
Steven Liu4bf01bc2014-07-17 11:05:29 -05002711 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
2712 enforceModifyPermission();
2713
2714 int returnValue = 0;
2715 try {
2716 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
2717 if(result.exception == null) {
2718 if (result.result != null) {
2719 byte[] responseData = (byte[])(result.result);
2720 if(responseData.length > oemResp.length) {
2721 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
2722 responseData.length + "bytes. Buffer Size is " +
2723 oemResp.length + "bytes.");
2724 }
2725 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
2726 returnValue = responseData.length;
2727 }
2728 } else {
2729 CommandException ex = (CommandException) result.exception;
2730 returnValue = ex.getCommandError().ordinal();
2731 if(returnValue > 0) returnValue *= -1;
2732 }
2733 } catch (RuntimeException e) {
2734 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
2735 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
2736 if(returnValue > 0) returnValue *= -1;
2737 }
2738
2739 return returnValue;
2740 }
Wink Saville5d475dd2014-10-17 15:00:58 -07002741
2742 @Override
2743 public void setRadioCapability(RadioAccessFamily[] rafs) {
2744 try {
2745 ProxyController.getInstance().setRadioCapability(rafs);
2746 } catch (RuntimeException e) {
2747 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
2748 }
2749 }
2750
2751 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002752 public int getRadioAccessFamily(int phoneId, String callingPackage) {
2753 if (!canReadPhoneState(callingPackage, "getRadioAccessFamily")) {
2754 return RadioAccessFamily.RAF_UNKNOWN;
2755 }
2756
Wink Saville5d475dd2014-10-17 15:00:58 -07002757 return ProxyController.getInstance().getRadioAccessFamily(phoneId);
2758 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002759
2760 @Override
2761 public void enableVideoCalling(boolean enable) {
2762 enforceModifyPermission();
Tyler Gunnfdd69de2015-12-04 21:24:38 -08002763 ImsManager.setVtSetting(mPhone.getContext(), enable);
Andrew Leedf14ead2014-10-17 14:22:52 -07002764 }
2765
2766 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002767 public boolean isVideoCallingEnabled(String callingPackage) {
2768 if (!canReadPhoneState(callingPackage, "isVideoCallingEnabled")) {
2769 return false;
2770 }
2771
Andrew Lee77527ac2014-10-21 16:57:39 -07002772 // Check the user preference and the system-level IMS setting. Even if the user has
2773 // enabled video calling, if IMS is disabled we aren't able to support video calling.
2774 // In the long run, we may instead need to check if there exists a connection service
2775 // which can support video calling.
Andrew Lee312e8172014-10-23 17:01:36 -07002776 return ImsManager.isVtEnabledByPlatform(mPhone.getContext())
2777 && ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
Tyler Gunnfdd69de2015-12-04 21:24:38 -08002778 && ImsManager.isVtEnabledByUser(mPhone.getContext());
Andrew Leedf14ead2014-10-17 14:22:52 -07002779 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002780
Andrew Leea1239f22015-03-02 17:44:07 -08002781 @Override
2782 public boolean canChangeDtmfToneLength() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07002783 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08002784 }
2785
2786 @Override
2787 public boolean isWorldPhone() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07002788 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08002789 }
2790
Andrew Lee9431b832015-03-09 18:46:45 -07002791 @Override
2792 public boolean isTtyModeSupported() {
2793 TelecomManager telecomManager = TelecomManager.from(mPhone.getContext());
2794 TelephonyManager telephonyManager =
2795 (TelephonyManager) mPhone.getContext().getSystemService(Context.TELEPHONY_SERVICE);
2796 return !telephonyManager.isMultiSimEnabled() && telecomManager.isTtySupported();
2797 }
2798
2799 @Override
2800 public boolean isHearingAidCompatibilitySupported() {
2801 return mPhone.getContext().getResources().getBoolean(R.bool.hac_enabled);
2802 }
2803
Sanket Padawe7310cc72015-01-14 09:53:20 -08002804 /**
2805 * Returns the unique device ID of phone, for example, the IMEI for
2806 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
2807 *
2808 * <p>Requires Permission:
2809 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
2810 */
2811 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002812 public String getDeviceId(String callingPackage) {
2813 if (!canReadPhoneState(callingPackage, "getDeviceId")) {
2814 return null;
2815 }
2816
Sanket Padawe7310cc72015-01-14 09:53:20 -08002817 final Phone phone = PhoneFactory.getPhone(0);
2818 if (phone != null) {
2819 return phone.getDeviceId();
2820 } else {
2821 return null;
2822 }
2823 }
2824
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002825 /*
2826 * {@hide}
2827 * Returns the IMS Registration Status
2828 */
Santos Cordon7a1885b2015-02-03 11:15:19 -08002829 @Override
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002830 public boolean isImsRegistered() {
2831 return mPhone.isImsRegistered();
2832 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08002833
2834 @Override
2835 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
2836 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
2837 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07002838
Nathan Haroldc55097a2015-03-11 18:14:50 -07002839 /*
2840 * {@hide}
2841 * Returns the IMS Registration Status
2842 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002843 public boolean isWifiCallingAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07002844 return mPhone.isWifiCallingEnabled();
2845 }
2846
2847 /*
2848 * {@hide}
2849 * Returns the IMS Registration Status
2850 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002851 public boolean isVolteAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07002852 return mPhone.isVolteEnabled();
2853 }
Svet Ganovb320e182015-04-16 12:30:10 -07002854
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002855 /*
2856 * {@hide} Returns the IMS Registration Status
2857 */
2858 public boolean isVideoTelephonyAvailable() {
2859 return mPhone.isVideoEnabled();
2860 }
2861
Svet Ganovb320e182015-04-16 12:30:10 -07002862 private boolean canReadPhoneState(String callingPackage, String message) {
Etan Cohen921655c2015-06-24 13:54:50 -07002863 try {
Amit Mahajan83ea23b2015-07-30 16:05:11 -07002864 mApp.enforceCallingOrSelfPermission(
Etan Cohen921655c2015-06-24 13:54:50 -07002865 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE, message);
2866
Amit Mahajan83ea23b2015-07-30 16:05:11 -07002867 // SKIP checking for run-time permission since caller or self has PRIVILEDGED permission
Etan Cohen921655c2015-06-24 13:54:50 -07002868 return true;
Amit Mahajan785783f2015-06-29 10:36:50 -07002869 } catch (SecurityException e) {
2870 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
2871 message);
Etan Cohen921655c2015-06-24 13:54:50 -07002872 }
Svet Ganovb320e182015-04-16 12:30:10 -07002873
2874 if (mAppOps.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(),
2875 callingPackage) != AppOpsManager.MODE_ALLOWED) {
2876 return false;
2877 }
2878
2879 return true;
2880 }
Stuart Scott8eef64f2015-04-08 15:13:54 -07002881
Makoto Onukifee69342015-06-29 14:44:50 -07002882 /**
Amit Mahajanb9b49782015-09-15 18:16:32 -07002883 * Besides READ_PHONE_STATE, WRITE_SMS and READ_SMS also allow apps to get phone numbers.
Makoto Onukifee69342015-06-29 14:44:50 -07002884 */
2885 private boolean canReadPhoneNumber(String callingPackage, String message) {
Makoto Onukie4072d12015-08-03 15:12:23 -07002886 // Default SMS app can always read it.
2887 if (mAppOps.noteOp(AppOpsManager.OP_WRITE_SMS,
2888 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED) {
2889 return true;
2890 }
2891 try {
2892 return canReadPhoneState(callingPackage, message);
Amit Mahajanb9b49782015-09-15 18:16:32 -07002893 } catch (SecurityException readPhoneStateSecurityException) {
2894 try {
2895 // Can be read with READ_SMS too.
2896 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_SMS, message);
2897 return mAppOps.noteOp(AppOpsManager.OP_READ_SMS,
2898 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED;
2899 } catch (SecurityException readSmsSecurityException) {
2900 // Throw exception with message including both READ_PHONE_STATE and READ_SMS
2901 // permissions
2902 throw new SecurityException(message + ": Neither user " + Binder.getCallingUid() +
2903 " nor current process has " + android.Manifest.permission.READ_PHONE_STATE +
2904 " or " + android.Manifest.permission.READ_SMS + ".");
2905 }
Makoto Onukie4072d12015-08-03 15:12:23 -07002906 }
Makoto Onukifee69342015-06-29 14:44:50 -07002907 }
2908
Stuart Scott8eef64f2015-04-08 15:13:54 -07002909 @Override
2910 public void factoryReset(int subId) {
2911 enforceConnectivityInternalPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07002912 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
2913 return;
2914 }
2915
Svet Ganovcc087f82015-05-12 20:35:54 -07002916 final long identity = Binder.clearCallingIdentity();
2917 try {
Stuart Scott981d8582015-04-21 14:09:50 -07002918 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
2919 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Svet Ganovcc087f82015-05-12 20:35:54 -07002920 // Enable data
2921 setDataEnabled(subId, true);
2922 // Set network selection mode to automatic
2923 setNetworkSelectionModeAutomatic(subId);
2924 // Set preferred mobile network type to the best available
2925 setPreferredNetworkType(subId, Phone.PREFERRED_NT_MODE);
2926 // Turn off roaming
2927 SubscriptionManager.from(mApp).setDataRoaming(0, subId);
2928 }
2929 } finally {
2930 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07002931 }
2932 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01002933
2934 @Override
2935 public String getLocaleFromDefaultSim() {
2936 // We query all subscriptions instead of just the active ones, because
2937 // this might be called early on in the provisioning flow when the
2938 // subscriptions potentially aren't active yet.
2939 final List<SubscriptionInfo> slist = getAllSubscriptionInfoList();
2940 if (slist == null || slist.isEmpty()) {
2941 return null;
2942 }
2943
2944 // This function may be called very early, say, from the setup wizard, at
2945 // which point we won't have a default subscription set. If that's the case
2946 // we just choose the first, which will be valid in "most cases".
2947 final int defaultSubId = getDefaultSubscription();
2948 SubscriptionInfo info = null;
2949 if (defaultSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
2950 info = slist.get(0);
2951 } else {
2952 for (SubscriptionInfo item : slist) {
2953 if (item.getSubscriptionId() == defaultSubId) {
2954 info = item;
2955 break;
2956 }
2957 }
2958
2959 if (info == null) {
2960 return null;
2961 }
2962 }
2963
2964 // Try and fetch the locale from the carrier properties or from the SIM language
2965 // preferences (EF-PL and EF-LI)...
Tony Hill183b2de2015-06-24 14:53:58 +01002966 final int mcc = info.getMcc();
Narayan Kamath1c496c22015-04-16 14:40:19 +01002967 final Phone defaultPhone = getPhone(info.getSubscriptionId());
Narayan Kamath011676f2015-07-29 12:04:08 +01002968 String simLanguage = null;
Narayan Kamath1c496c22015-04-16 14:40:19 +01002969 if (defaultPhone != null) {
2970 final Locale localeFromDefaultSim = defaultPhone.getLocaleFromSimAndCarrierPrefs();
2971 if (localeFromDefaultSim != null) {
Narayan Kamath011676f2015-07-29 12:04:08 +01002972 if (!localeFromDefaultSim.getCountry().isEmpty()) {
Tony Hill183b2de2015-06-24 14:53:58 +01002973 if (DBG) log("Using locale from default SIM:" + localeFromDefaultSim);
2974 return localeFromDefaultSim.toLanguageTag();
Narayan Kamath011676f2015-07-29 12:04:08 +01002975 } else {
2976 simLanguage = localeFromDefaultSim.getLanguage();
Tony Hill183b2de2015-06-24 14:53:58 +01002977 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01002978 }
2979 }
2980
Narayan Kamath011676f2015-07-29 12:04:08 +01002981 // The SIM language preferences only store a language (e.g. fr = French), not an
2982 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
2983 // the SIM and carrier preferences does not include a country we add the country
2984 // determined from the SIM MCC to provide an exact locale.
2985 final Locale mccLocale = MccTable.getLocaleFromMcc(mPhone.getContext(), mcc, simLanguage);
Tony Hill183b2de2015-06-24 14:53:58 +01002986 if (mccLocale != null) {
2987 if (DBG) log("No locale from default SIM, using mcc locale:" + mccLocale);
2988 return mccLocale.toLanguageTag();
Narayan Kamath1c496c22015-04-16 14:40:19 +01002989 }
2990
Tony Hill183b2de2015-06-24 14:53:58 +01002991 if (DBG) log("No locale found - returning null");
Narayan Kamath1c496c22015-04-16 14:40:19 +01002992 return null;
2993 }
2994
2995 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
2996 final long identity = Binder.clearCallingIdentity();
2997 try {
2998 return mSubscriptionController.getAllSubInfoList(
2999 mPhone.getContext().getOpPackageName());
3000 } finally {
3001 Binder.restoreCallingIdentity(identity);
3002 }
3003 }
3004
3005 private List<SubscriptionInfo> getActiveSubscriptionInfoList() {
3006 final long identity = Binder.clearCallingIdentity();
3007 try {
3008 return mSubscriptionController.getActiveSubscriptionInfoList(
3009 mPhone.getContext().getOpPackageName());
3010 } finally {
3011 Binder.restoreCallingIdentity(identity);
3012 }
3013 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003014
3015 /**
Adam Lesinski903a54c2016-04-11 14:49:52 -07003016 * Responds to the ResultReceiver with the {@link android.telephony.ModemActivityInfo} object
3017 * representing the state of the modem.
3018 *
3019 * NOTE: This clears the modem state, so there should only every be one caller.
3020 * @hide
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003021 */
3022 @Override
Adam Lesinski903a54c2016-04-11 14:49:52 -07003023 public void requestModemActivityInfo(ResultReceiver result) {
3024 enforceModifyPermission();
3025
3026 ModemActivityInfo info = (ModemActivityInfo) sendRequest(CMD_GET_MODEM_ACTIVITY_INFO, null);
3027 Bundle bundle = new Bundle();
3028 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY, info);
3029 result.send(0, bundle);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003030 }
Jack Yu85bd38a2015-11-09 11:34:32 -08003031
3032 /**
3033 * {@hide}
3034 * Returns the service state information on specified subscription.
3035 */
3036 @Override
3037 public ServiceState getServiceStateForSubscriber(int subId, String callingPackage) {
3038
3039 if (!canReadPhoneState(callingPackage, "getServiceStateForSubscriber")) {
3040 return null;
3041 }
3042
3043 final Phone phone = getPhone(subId);
3044 if (phone == null) {
3045 return null;
3046 }
3047
3048 return phone.getServiceState();
3049 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08003050
3051 /**
3052 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
3053 *
3054 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3055 * voicemail ringtone.
3056 * @return The URI for the ringtone to play when receiving a voicemail from a specific
3057 * PhoneAccount.
3058 */
3059 @Override
3060 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
3061 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
3062 if (phone == null) {
3063 return null;
3064 }
3065
3066 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone);
3067 }
3068
3069 /**
3070 * Returns whether vibration is set for voicemail notification in Phone settings.
3071 *
3072 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3073 * voicemail vibration setting.
3074 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
3075 */
3076 @Override
3077 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
3078 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
3079 if (phone == null) {
3080 return false;
3081 }
3082
3083 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone);
3084 }
3085
Youhan Wange64578a2016-05-02 15:32:42 -07003086 /**
3087 * Make sure either called from same process as self (phone) or IPC caller has read privilege.
3088 *
3089 * @throws SecurityException if the caller does not have the required permission
3090 */
3091 private void enforceReadPrivilegedPermission() {
3092 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
3093 null);
3094 }
3095
3096 /**
3097 * Return the application ID for the app type.
3098 *
3099 * @param subId the subscription ID that this request applies to.
3100 * @param appType the uicc app type.
3101 * @return Application ID for specificied app type, or null if no uicc.
3102 */
3103 @Override
3104 public String getAidForAppType(int subId, int appType) {
3105 enforceReadPrivilegedPermission();
3106 Phone phone = getPhone(subId);
3107 if (phone == null) {
3108 return null;
3109 }
3110 String aid = null;
3111 try {
3112 aid = UiccController.getInstance().getUiccCard(phone.getPhoneId())
3113 .getApplicationByType(appType).getAid();
3114 } catch (Exception e) {
3115 Log.e(LOG_TAG, "Not getting aid. Exception ex=" + e);
3116 }
3117 return aid;
3118 }
3119
Youhan Wang4001d252016-05-11 10:29:41 -07003120
3121 /**
3122 * Return the Electronic Serial Number.
3123 *
3124 * @param subId the subscription ID that this request applies to.
3125 * @return ESN or null if error.
3126 */
3127 @Override
3128 public String getEsn(int subId) {
3129 enforceReadPrivilegedPermission();
3130 Phone phone = getPhone(subId);
3131 if (phone == null) {
3132 return null;
3133 }
3134 String esn = null;
3135 try {
3136 esn = phone.getEsn();
3137 } catch (Exception e) {
3138 Log.e(LOG_TAG, "Not getting ESN. Exception ex=" + e);
3139 }
3140 return esn;
3141 }
3142
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003143 /**
3144 * Get snapshot of Telephony histograms
3145 * @return List of Telephony histograms
3146 * @hide
3147 */
3148 @Override
3149 public List<TelephonyHistogram> getTelephonyHistograms() {
3150 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
3151 return RIL.getTelephonyRILTimingHistograms();
3152 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003153}