blob: e82201409c7edc38a194088b0b38e59913fd9da4 [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;
Sooraj Sasindran9a909312016-07-18 11:57:25 -070041import android.os.WorkSource;
Derek Tan97ebb422014-09-05 16:55:38 -070042import android.preference.PreferenceManager;
Ihab Awadf2177b72013-11-25 13:33:23 -080043import android.provider.Settings;
Meng Wang1a7c35a2016-05-05 20:56:15 -070044import android.service.carrier.CarrierIdentifier;
Santos Cordon7a1885b2015-02-03 11:15:19 -080045import android.telecom.PhoneAccount;
Nancy Chen31f9ba12016-01-06 11:42:12 -080046import android.telecom.PhoneAccountHandle;
Andrew Lee9431b832015-03-09 18:46:45 -070047import android.telecom.TelecomManager;
Junda Liu12f7d802015-05-01 12:06:44 -070048import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070049import android.telephony.CellInfo;
Sooraj Sasindran9a909312016-07-18 11:57:25 -070050import android.telephony.ClientRequestStats;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -070051import android.telephony.IccOpenLogicalChannelResponse;
Ta-wei Yen87c49842016-05-13 21:19:52 -070052import android.telephony.ModemActivityInfo;
Jake Hambye994d462014-02-03 13:10:13 -080053import android.telephony.NeighboringCellInfo;
Wink Saville5d475dd2014-10-17 15:00:58 -070054import android.telephony.RadioAccessFamily;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070055import android.telephony.ServiceState;
Wink Saville0f3b5fc2014-11-11 08:40:49 -080056import android.telephony.SubscriptionInfo;
Jeff Sharkey85190e62014-12-05 09:40:12 -080057import android.telephony.SubscriptionManager;
Sanket Padawe99ef1e32016-05-18 16:12:33 -070058import android.telephony.TelephonyHistogram;
Ta-wei Yenb6929602016-05-24 15:48:27 -070059import android.telephony.TelephonyManager;
60import android.telephony.VisualVoicemailSmsFilterSettings;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070061import android.text.TextUtils;
Jeff Sharkey85190e62014-12-05 09:40:12 -080062import android.util.ArraySet;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070063import android.util.Log;
Jake Hambye994d462014-02-03 13:10:13 -080064import android.util.Pair;
Jeff Sharkey85190e62014-12-05 09:40:12 -080065import android.util.Slog;
Andrew Lee312e8172014-10-23 17:01:36 -070066import com.android.ims.ImsManager;
Shishir Agrawal566b7612013-10-28 14:41:00 -070067import com.android.internal.telephony.CallManager;
Shishir Agrawal302c8692015-06-19 13:49:39 -070068import com.android.internal.telephony.CellNetworkScanResult;
Shishir Agrawal566b7612013-10-28 14:41:00 -070069import com.android.internal.telephony.CommandException;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070070import com.android.internal.telephony.DefaultPhoneNotifier;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070071import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -080072import com.android.internal.telephony.IccCard;
Narayan Kamath1c496c22015-04-16 14:40:19 +010073import com.android.internal.telephony.MccTable;
Shishir Agrawal302c8692015-06-19 13:49:39 -070074import com.android.internal.telephony.OperatorInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070075import com.android.internal.telephony.Phone;
Ta-wei Yen87c49842016-05-13 21:19:52 -070076import com.android.internal.telephony.PhoneConstants;
Wink Saville36469e72014-06-11 15:17:00 -070077import com.android.internal.telephony.PhoneFactory;
Wink Saville5d475dd2014-10-17 15:00:58 -070078import com.android.internal.telephony.ProxyController;
Sanket Padawe99ef1e32016-05-18 16:12:33 -070079import com.android.internal.telephony.RIL;
Svet Ganovb320e182015-04-16 12:30:10 -070080import com.android.internal.telephony.RILConstants;
Wink Savilleac1bdfd2014-11-20 23:04:44 -080081import com.android.internal.telephony.SubscriptionController;
Shishir Agrawal566b7612013-10-28 14:41:00 -070082import com.android.internal.telephony.uicc.IccIoResult;
83import com.android.internal.telephony.uicc.IccUtils;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -070084import com.android.internal.telephony.uicc.UiccCard;
Shishir Agrawal566b7612013-10-28 14:41:00 -070085import com.android.internal.telephony.uicc.UiccController;
Jake Hambye994d462014-02-03 13:10:13 -080086import com.android.internal.util.HexDump;
Ta-wei Yenb0f695b2016-08-08 17:33:11 -070087import com.android.phone.settings.VisualVoicemailSettingsUtil;
Nancy Chen31f9ba12016-01-06 11:42:12 -080088import com.android.phone.settings.VoicemailNotificationSettingsUtil;
Ta-wei Yenc236d6b2016-06-21 13:33:12 -070089import java.io.FileDescriptor;
90import java.io.PrintWriter;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070091import java.util.ArrayList;
Jeff Sharkey85190e62014-12-05 09:40:12 -080092import java.util.Arrays;
Jake Hambye994d462014-02-03 13:10:13 -080093import java.util.List;
Narayan Kamath1c496c22015-04-16 14:40:19 +010094import java.util.Locale;
Jeff Sharkey85190e62014-12-05 09:40:12 -080095import java.util.Map;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070096
97/**
98 * Implementation of the ITelephony interface.
99 */
Santos Cordon117fee72014-05-16 17:56:12 -0700100public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700101 private static final String LOG_TAG = "PhoneInterfaceManager";
102 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
103 private static final boolean DBG_LOC = false;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800104 private static final boolean DBG_MERGE = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700105
106 // Message codes used with mMainThreadHandler
107 private static final int CMD_HANDLE_PIN_MMI = 1;
108 private static final int CMD_HANDLE_NEIGHBORING_CELL = 2;
109 private static final int EVENT_NEIGHBORING_CELL_DONE = 3;
110 private static final int CMD_ANSWER_RINGING_CALL = 4;
111 private static final int CMD_END_CALL = 5; // not used yet
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700112 private static final int CMD_TRANSMIT_APDU_LOGICAL_CHANNEL = 7;
113 private static final int EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE = 8;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700114 private static final int CMD_OPEN_CHANNEL = 9;
115 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
116 private static final int CMD_CLOSE_CHANNEL = 11;
117 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -0800118 private static final int CMD_NV_READ_ITEM = 13;
119 private static final int EVENT_NV_READ_ITEM_DONE = 14;
120 private static final int CMD_NV_WRITE_ITEM = 15;
121 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
122 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
123 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
124 private static final int CMD_NV_RESET_CONFIG = 19;
125 private static final int EVENT_NV_RESET_CONFIG_DONE = 20;
Jake Hamby7c27be32014-03-03 13:25:59 -0800126 private static final int CMD_GET_PREFERRED_NETWORK_TYPE = 21;
127 private static final int EVENT_GET_PREFERRED_NETWORK_TYPE_DONE = 22;
128 private static final int CMD_SET_PREFERRED_NETWORK_TYPE = 23;
129 private static final int EVENT_SET_PREFERRED_NETWORK_TYPE_DONE = 24;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800130 private static final int CMD_SEND_ENVELOPE = 25;
131 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Derek Tan6b088ee2014-09-05 14:15:18 -0700132 private static final int CMD_INVOKE_OEM_RIL_REQUEST_RAW = 27;
133 private static final int EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE = 28;
134 private static final int CMD_TRANSMIT_APDU_BASIC_CHANNEL = 29;
135 private static final int EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE = 30;
136 private static final int CMD_EXCHANGE_SIM_IO = 31;
137 private static final int EVENT_EXCHANGE_SIM_IO_DONE = 32;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800138 private static final int CMD_SET_VOICEMAIL_NUMBER = 33;
139 private static final int EVENT_SET_VOICEMAIL_NUMBER_DONE = 34;
Stuart Scott54788802015-03-30 13:18:01 -0700140 private static final int CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC = 35;
141 private static final int EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE = 36;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700142 private static final int CMD_GET_MODEM_ACTIVITY_INFO = 37;
143 private static final int EVENT_GET_MODEM_ACTIVITY_INFO_DONE = 38;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700144 private static final int CMD_PERFORM_NETWORK_SCAN = 39;
145 private static final int EVENT_PERFORM_NETWORK_SCAN_DONE = 40;
146 private static final int CMD_SET_NETWORK_SELECTION_MODE_MANUAL = 41;
147 private static final int EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE = 42;
Meng Wang1a7c35a2016-05-05 20:56:15 -0700148 private static final int CMD_SET_ALLOWED_CARRIERS = 43;
149 private static final int EVENT_SET_ALLOWED_CARRIERS_DONE = 44;
150 private static final int CMD_GET_ALLOWED_CARRIERS = 45;
151 private static final int EVENT_GET_ALLOWED_CARRIERS_DONE = 46;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700152
153 /** The singleton instance. */
154 private static PhoneInterfaceManager sInstance;
155
Wink Saville3ab207e2014-11-20 13:07:20 -0800156 private PhoneGlobals mApp;
157 private Phone mPhone;
158 private CallManager mCM;
Stuart Scott981d8582015-04-21 14:09:50 -0700159 private UserManager mUserManager;
Wink Saville3ab207e2014-11-20 13:07:20 -0800160 private AppOpsManager mAppOps;
161 private MainThreadHandler mMainThreadHandler;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800162 private SubscriptionController mSubscriptionController;
Wink Saville3ab207e2014-11-20 13:07:20 -0800163 private SharedPreferences mTelephonySharedPreferences;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700164
Derek Tan97ebb422014-09-05 16:55:38 -0700165 private static final String PREF_CARRIERS_ALPHATAG_PREFIX = "carrier_alphtag_";
166 private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
Jeff Sharkey85190e62014-12-05 09:40:12 -0800167 private static final String PREF_CARRIERS_SUBSCRIBER_PREFIX = "carrier_subscriber_";
Derek Tan89e89d42014-07-08 17:00:10 -0700168
169 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700170 * A request object to use for transmitting data to an ICC.
171 */
172 private static final class IccAPDUArgument {
173 public int channel, cla, command, p1, p2, p3;
174 public String data;
175
176 public IccAPDUArgument(int channel, int cla, int command,
177 int p1, int p2, int p3, String data) {
178 this.channel = channel;
179 this.cla = cla;
180 this.command = command;
181 this.p1 = p1;
182 this.p2 = p2;
183 this.p3 = p3;
184 this.data = data;
185 }
186 }
187
188 /**
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700189 * A request object to use for transmitting data to an ICC.
190 */
191 private static final class ManualNetworkSelectionArgument {
192 public OperatorInfo operatorInfo;
193 public boolean persistSelection;
194
195 public ManualNetworkSelectionArgument(OperatorInfo operatorInfo, boolean persistSelection) {
196 this.operatorInfo = operatorInfo;
197 this.persistSelection = persistSelection;
198 }
199 }
200
201 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700202 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
203 * request after sending. The main thread will notify the request when it is complete.
204 */
205 private static final class MainThreadRequest {
206 /** The argument to use for the request */
207 public Object argument;
208 /** The result of the request that is run on the main thread */
209 public Object result;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800210 // The subscriber id that this request applies to. Defaults to
211 // SubscriptionManager.INVALID_SUBSCRIPTION_ID
212 public Integer subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700213
214 public MainThreadRequest(Object argument) {
215 this.argument = argument;
216 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800217
218 public MainThreadRequest(Object argument, Integer subId) {
219 this.argument = argument;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800220 if (subId != null) {
221 this.subId = subId;
222 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800223 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700224 }
225
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800226 private static final class IncomingThirdPartyCallArgs {
227 public final ComponentName component;
228 public final String callId;
229 public final String callerDisplayName;
230
231 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
232 String callerDisplayName) {
233 this.component = component;
234 this.callId = callId;
235 this.callerDisplayName = callerDisplayName;
236 }
237 }
238
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700239 /**
240 * A handler that processes messages on the main thread in the phone process. Since many
241 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
242 * inbound binder threads to the main thread in the phone process. The Binder thread
243 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
244 * on, which will be notified when the operation completes and will contain the result of the
245 * request.
246 *
247 * <p>If a MainThreadRequest object is provided in the msg.obj field,
248 * note that request.result must be set to something non-null for the calling thread to
249 * unblock.
250 */
251 private final class MainThreadHandler extends Handler {
252 @Override
253 public void handleMessage(Message msg) {
254 MainThreadRequest request;
255 Message onCompleted;
256 AsyncResult ar;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800257 UiccCard uiccCard;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700258 IccAPDUArgument iccArgument;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700259
260 switch (msg.what) {
Yorke Lee716f67e2015-06-17 15:39:16 -0700261 case CMD_HANDLE_PIN_MMI: {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700262 request = (MainThreadRequest) msg.obj;
Yorke Lee716f67e2015-06-17 15:39:16 -0700263 final Phone phone = getPhoneFromRequest(request);
264 request.result = phone != null ?
265 getPhoneFromRequest(request).handlePinMmi((String) request.argument)
266 : false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700267 // Wake up the requesting thread
268 synchronized (request) {
269 request.notifyAll();
270 }
271 break;
Yorke Lee716f67e2015-06-17 15:39:16 -0700272 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700273
274 case CMD_HANDLE_NEIGHBORING_CELL:
275 request = (MainThreadRequest) msg.obj;
276 onCompleted = obtainMessage(EVENT_NEIGHBORING_CELL_DONE,
277 request);
Sooraj Sasindran9a909312016-07-18 11:57:25 -0700278 mPhone.getNeighboringCids(onCompleted, (WorkSource)request.argument);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700279 break;
280
281 case EVENT_NEIGHBORING_CELL_DONE:
282 ar = (AsyncResult) msg.obj;
283 request = (MainThreadRequest) ar.userObj;
284 if (ar.exception == null && ar.result != null) {
285 request.result = ar.result;
286 } else {
287 // create an empty list to notify the waiting thread
Jake Hambye994d462014-02-03 13:10:13 -0800288 request.result = new ArrayList<NeighboringCellInfo>(0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700289 }
290 // Wake up the requesting thread
291 synchronized (request) {
292 request.notifyAll();
293 }
294 break;
295
296 case CMD_ANSWER_RINGING_CALL:
Wink Saville08874612014-08-31 19:19:58 -0700297 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800298 int answer_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700299 answerRingingCallInternal(answer_subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700300 break;
301
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700302 case CMD_END_CALL:
303 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800304 int end_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700305 final boolean hungUp;
Anthony Leeae4e36d2015-05-21 07:17:46 -0700306 Phone phone = getPhone(end_subId);
307 if (phone == null) {
308 if (DBG) log("CMD_END_CALL: no phone for id: " + end_subId);
309 break;
310 }
311 int phoneType = phone.getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700312 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
313 // CDMA: If the user presses the Power button we treat it as
314 // ending the complete call session
Wink Saville08874612014-08-31 19:19:58 -0700315 hungUp = PhoneUtils.hangupRingingAndActive(getPhone(end_subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700316 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
317 // GSM: End the call as per the Phone state
318 hungUp = PhoneUtils.hangup(mCM);
319 } else {
320 throw new IllegalStateException("Unexpected phone type: " + phoneType);
321 }
322 if (DBG) log("CMD_END_CALL: " + (hungUp ? "hung up!" : "no call to hang up"));
323 request.result = hungUp;
324 // Wake up the requesting thread
325 synchronized (request) {
326 request.notifyAll();
327 }
328 break;
329
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700330 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700331 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700332 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800333 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700334 if (uiccCard == null) {
335 loge("iccTransmitApduLogicalChannel: No UICC");
336 request.result = new IccIoResult(0x6F, 0, (byte[])null);
337 synchronized (request) {
338 request.notifyAll();
339 }
340 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700341 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
342 request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700343 uiccCard.iccTransmitApduLogicalChannel(
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700344 iccArgument.channel, iccArgument.cla, iccArgument.command,
345 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
Shishir Agrawal566b7612013-10-28 14:41:00 -0700346 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700347 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700348 break;
349
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700350 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700351 ar = (AsyncResult) msg.obj;
352 request = (MainThreadRequest) ar.userObj;
353 if (ar.exception == null && ar.result != null) {
354 request.result = ar.result;
355 } else {
356 request.result = new IccIoResult(0x6F, 0, (byte[])null);
357 if (ar.result == null) {
358 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800359 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700360 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800361 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700362 } else {
363 loge("iccTransmitApduLogicalChannel: Unknown exception");
364 }
365 }
366 synchronized (request) {
367 request.notifyAll();
368 }
369 break;
370
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700371 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
372 request = (MainThreadRequest) msg.obj;
373 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800374 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700375 if (uiccCard == null) {
376 loge("iccTransmitApduBasicChannel: No UICC");
377 request.result = new IccIoResult(0x6F, 0, (byte[])null);
378 synchronized (request) {
379 request.notifyAll();
380 }
381 } else {
382 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
383 request);
384 uiccCard.iccTransmitApduBasicChannel(
385 iccArgument.cla, iccArgument.command, iccArgument.p1, iccArgument.p2,
386 iccArgument.p3, iccArgument.data, onCompleted);
387 }
388 break;
389
390 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
391 ar = (AsyncResult) msg.obj;
392 request = (MainThreadRequest) ar.userObj;
393 if (ar.exception == null && ar.result != null) {
394 request.result = ar.result;
395 } else {
396 request.result = new IccIoResult(0x6F, 0, (byte[])null);
397 if (ar.result == null) {
398 loge("iccTransmitApduBasicChannel: Empty response");
399 } else if (ar.exception instanceof CommandException) {
400 loge("iccTransmitApduBasicChannel: CommandException: " +
401 ar.exception);
402 } else {
403 loge("iccTransmitApduBasicChannel: Unknown exception");
404 }
405 }
406 synchronized (request) {
407 request.notifyAll();
408 }
409 break;
410
411 case CMD_EXCHANGE_SIM_IO:
412 request = (MainThreadRequest) msg.obj;
413 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800414 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700415 if (uiccCard == null) {
416 loge("iccExchangeSimIO: No UICC");
417 request.result = new IccIoResult(0x6F, 0, (byte[])null);
418 synchronized (request) {
419 request.notifyAll();
420 }
421 } else {
422 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
423 request);
424 uiccCard.iccExchangeSimIO(iccArgument.cla, /* fileID */
425 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
426 iccArgument.data, onCompleted);
427 }
428 break;
429
430 case EVENT_EXCHANGE_SIM_IO_DONE:
431 ar = (AsyncResult) msg.obj;
432 request = (MainThreadRequest) ar.userObj;
433 if (ar.exception == null && ar.result != null) {
434 request.result = ar.result;
435 } else {
436 request.result = new IccIoResult(0x6f, 0, (byte[])null);
437 }
438 synchronized (request) {
439 request.notifyAll();
440 }
441 break;
442
Derek Tan4d5e5c12014-02-04 11:54:58 -0800443 case CMD_SEND_ENVELOPE:
444 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800445 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700446 if (uiccCard == null) {
447 loge("sendEnvelopeWithStatus: No UICC");
448 request.result = new IccIoResult(0x6F, 0, (byte[])null);
449 synchronized (request) {
450 request.notifyAll();
451 }
452 } else {
453 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
454 uiccCard.sendEnvelopeWithStatus((String)request.argument, onCompleted);
455 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800456 break;
457
458 case EVENT_SEND_ENVELOPE_DONE:
459 ar = (AsyncResult) msg.obj;
460 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700461 if (ar.exception == null && ar.result != null) {
462 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800463 } else {
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700464 request.result = new IccIoResult(0x6F, 0, (byte[])null);
465 if (ar.result == null) {
466 loge("sendEnvelopeWithStatus: Empty response");
467 } else if (ar.exception instanceof CommandException) {
468 loge("sendEnvelopeWithStatus: CommandException: " +
469 ar.exception);
470 } else {
471 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
472 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800473 }
474 synchronized (request) {
475 request.notifyAll();
476 }
477 break;
478
Shishir Agrawal566b7612013-10-28 14:41:00 -0700479 case CMD_OPEN_CHANNEL:
480 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800481 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700482 if (uiccCard == null) {
483 loge("iccOpenLogicalChannel: No UICC");
Shishir Agrawalfc0492a2016-02-17 11:15:33 -0800484 request.result = new IccOpenLogicalChannelResponse(-1,
485 IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE, null);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700486 synchronized (request) {
487 request.notifyAll();
488 }
489 } else {
490 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
491 uiccCard.iccOpenLogicalChannel((String)request.argument, onCompleted);
492 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700493 break;
494
495 case EVENT_OPEN_CHANNEL_DONE:
496 ar = (AsyncResult) msg.obj;
497 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700498 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700499 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700500 int[] result = (int[]) ar.result;
501 int channelId = result[0];
502 byte[] selectResponse = null;
503 if (result.length > 1) {
504 selectResponse = new byte[result.length - 1];
505 for (int i = 1; i < result.length; ++i) {
506 selectResponse[i - 1] = (byte) result[i];
507 }
508 }
509 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700510 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700511 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700512 if (ar.result == null) {
513 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700514 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700515 if (ar.exception != null) {
516 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
517 }
518
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700519 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Junda Liua754ba12015-05-20 01:17:52 -0700520 if (ar.exception instanceof CommandException) {
521 CommandException.Error error =
522 ((CommandException) (ar.exception)).getCommandError();
523 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700524 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700525 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700526 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700527 }
528 }
529 openChannelResp = new IccOpenLogicalChannelResponse(
530 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700531 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700532 request.result = openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700533 synchronized (request) {
534 request.notifyAll();
535 }
536 break;
537
538 case CMD_CLOSE_CHANNEL:
539 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800540 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700541 if (uiccCard == null) {
542 loge("iccCloseLogicalChannel: No UICC");
543 request.result = new IccIoResult(0x6F, 0, (byte[])null);
544 synchronized (request) {
545 request.notifyAll();
546 }
547 } else {
548 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
549 uiccCard.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
550 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700551 break;
552
553 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800554 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
555 break;
556
557 case CMD_NV_READ_ITEM:
558 request = (MainThreadRequest) msg.obj;
559 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
560 mPhone.nvReadItem((Integer) request.argument, onCompleted);
561 break;
562
563 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700564 ar = (AsyncResult) msg.obj;
565 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800566 if (ar.exception == null && ar.result != null) {
567 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700568 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800569 request.result = "";
570 if (ar.result == null) {
571 loge("nvReadItem: Empty response");
572 } else if (ar.exception instanceof CommandException) {
573 loge("nvReadItem: CommandException: " +
574 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700575 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800576 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700577 }
578 }
579 synchronized (request) {
580 request.notifyAll();
581 }
582 break;
583
Jake Hambye994d462014-02-03 13:10:13 -0800584 case CMD_NV_WRITE_ITEM:
585 request = (MainThreadRequest) msg.obj;
586 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
587 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
588 mPhone.nvWriteItem(idValue.first, idValue.second, onCompleted);
589 break;
590
591 case EVENT_NV_WRITE_ITEM_DONE:
592 handleNullReturnEvent(msg, "nvWriteItem");
593 break;
594
595 case CMD_NV_WRITE_CDMA_PRL:
596 request = (MainThreadRequest) msg.obj;
597 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
598 mPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
599 break;
600
601 case EVENT_NV_WRITE_CDMA_PRL_DONE:
602 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
603 break;
604
605 case CMD_NV_RESET_CONFIG:
606 request = (MainThreadRequest) msg.obj;
607 onCompleted = obtainMessage(EVENT_NV_RESET_CONFIG_DONE, request);
608 mPhone.nvResetConfig((Integer) request.argument, onCompleted);
609 break;
610
611 case EVENT_NV_RESET_CONFIG_DONE:
612 handleNullReturnEvent(msg, "nvResetConfig");
613 break;
614
Jake Hamby7c27be32014-03-03 13:25:59 -0800615 case CMD_GET_PREFERRED_NETWORK_TYPE:
616 request = (MainThreadRequest) msg.obj;
617 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
Stuart Scott54788802015-03-30 13:18:01 -0700618 getPhoneFromRequest(request).getPreferredNetworkType(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800619 break;
620
621 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
622 ar = (AsyncResult) msg.obj;
623 request = (MainThreadRequest) ar.userObj;
624 if (ar.exception == null && ar.result != null) {
625 request.result = ar.result; // Integer
626 } else {
Sanket Padawecfc2d352016-01-05 19:52:14 -0800627 request.result = null;
Jake Hamby7c27be32014-03-03 13:25:59 -0800628 if (ar.result == null) {
629 loge("getPreferredNetworkType: Empty response");
630 } else if (ar.exception instanceof CommandException) {
631 loge("getPreferredNetworkType: CommandException: " +
632 ar.exception);
633 } else {
634 loge("getPreferredNetworkType: Unknown exception");
635 }
636 }
637 synchronized (request) {
638 request.notifyAll();
639 }
640 break;
641
642 case CMD_SET_PREFERRED_NETWORK_TYPE:
643 request = (MainThreadRequest) msg.obj;
644 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
645 int networkType = (Integer) request.argument;
Stuart Scott54788802015-03-30 13:18:01 -0700646 getPhoneFromRequest(request).setPreferredNetworkType(networkType, onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800647 break;
648
649 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
650 handleNullReturnEvent(msg, "setPreferredNetworkType");
651 break;
652
Steven Liu4bf01bc2014-07-17 11:05:29 -0500653 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
654 request = (MainThreadRequest)msg.obj;
655 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
656 mPhone.invokeOemRilRequestRaw((byte[])request.argument, onCompleted);
657 break;
658
659 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
660 ar = (AsyncResult)msg.obj;
661 request = (MainThreadRequest)ar.userObj;
662 request.result = ar;
663 synchronized (request) {
664 request.notifyAll();
665 }
666 break;
667
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800668 case CMD_SET_VOICEMAIL_NUMBER:
669 request = (MainThreadRequest) msg.obj;
670 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
671 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -0800672 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
673 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800674 break;
675
676 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
677 handleNullReturnEvent(msg, "setVoicemailNumber");
678 break;
679
Stuart Scott54788802015-03-30 13:18:01 -0700680 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
681 request = (MainThreadRequest) msg.obj;
682 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
683 request);
684 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
685 break;
686
687 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
688 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
689 break;
690
Shishir Agrawal302c8692015-06-19 13:49:39 -0700691 case CMD_PERFORM_NETWORK_SCAN:
692 request = (MainThreadRequest) msg.obj;
693 onCompleted = obtainMessage(EVENT_PERFORM_NETWORK_SCAN_DONE, request);
694 getPhoneFromRequest(request).getAvailableNetworks(onCompleted);
695 break;
696
697 case EVENT_PERFORM_NETWORK_SCAN_DONE:
698 ar = (AsyncResult) msg.obj;
699 request = (MainThreadRequest) ar.userObj;
700 CellNetworkScanResult cellScanResult;
701 if (ar.exception == null && ar.result != null) {
702 cellScanResult = new CellNetworkScanResult(
703 CellNetworkScanResult.STATUS_SUCCESS,
704 (List<OperatorInfo>) ar.result);
705 } else {
706 if (ar.result == null) {
707 loge("getCellNetworkScanResults: Empty response");
708 }
709 if (ar.exception != null) {
710 loge("getCellNetworkScanResults: Exception: " + ar.exception);
711 }
712 int errorCode = CellNetworkScanResult.STATUS_UNKNOWN_ERROR;
713 if (ar.exception instanceof CommandException) {
714 CommandException.Error error =
715 ((CommandException) (ar.exception)).getCommandError();
716 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
717 errorCode = CellNetworkScanResult.STATUS_RADIO_NOT_AVAILABLE;
718 } else if (error == CommandException.Error.GENERIC_FAILURE) {
719 errorCode = CellNetworkScanResult.STATUS_RADIO_GENERIC_FAILURE;
720 }
721 }
722 cellScanResult = new CellNetworkScanResult(errorCode, null);
723 }
724 request.result = cellScanResult;
725 synchronized (request) {
726 request.notifyAll();
727 }
728 break;
729
730 case CMD_SET_NETWORK_SELECTION_MODE_MANUAL:
731 request = (MainThreadRequest) msg.obj;
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700732 ManualNetworkSelectionArgument selArg =
733 (ManualNetworkSelectionArgument) request.argument;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700734 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE,
735 request);
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700736 getPhoneFromRequest(request).selectNetworkManually(selArg.operatorInfo,
737 selArg.persistSelection, onCompleted);
Shishir Agrawal302c8692015-06-19 13:49:39 -0700738 break;
739
740 case EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE:
741 handleNullReturnEvent(msg, "setNetworkSelectionModeManual");
742 break;
743
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700744 case CMD_GET_MODEM_ACTIVITY_INFO:
745 request = (MainThreadRequest) msg.obj;
746 onCompleted = obtainMessage(EVENT_GET_MODEM_ACTIVITY_INFO_DONE, request);
Prerepa Viswanadham61a60ad2015-06-08 18:07:51 -0700747 mPhone.getModemActivityInfo(onCompleted);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700748 break;
749
750 case EVENT_GET_MODEM_ACTIVITY_INFO_DONE:
751 ar = (AsyncResult) msg.obj;
752 request = (MainThreadRequest) ar.userObj;
753 if (ar.exception == null && ar.result != null) {
754 request.result = ar.result;
755 } else {
756 if (ar.result == null) {
757 loge("queryModemActivityInfo: Empty response");
758 } else if (ar.exception instanceof CommandException) {
759 loge("queryModemActivityInfo: CommandException: " +
760 ar.exception);
761 } else {
762 loge("queryModemActivityInfo: Unknown exception");
763 }
764 }
Amit Mahajand4766222016-01-28 15:28:28 -0800765 // Result cannot be null. Return ModemActivityInfo with all fields set to 0.
766 if (request.result == null) {
767 request.result = new ModemActivityInfo(0, 0, 0, null, 0, 0);
768 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700769 synchronized (request) {
770 request.notifyAll();
771 }
772 break;
773
Meng Wang1a7c35a2016-05-05 20:56:15 -0700774 case CMD_SET_ALLOWED_CARRIERS:
775 request = (MainThreadRequest) msg.obj;
776 onCompleted = obtainMessage(EVENT_SET_ALLOWED_CARRIERS_DONE, request);
777 mPhone.setAllowedCarriers(
778 (List<CarrierIdentifier>) request.argument,
779 onCompleted);
780 break;
781
782 case EVENT_SET_ALLOWED_CARRIERS_DONE:
783 ar = (AsyncResult) msg.obj;
784 request = (MainThreadRequest) ar.userObj;
785 if (ar.exception == null && ar.result != null) {
786 request.result = ar.result;
787 } else {
788 if (ar.result == null) {
789 loge("setAllowedCarriers: Empty response");
790 } else if (ar.exception instanceof CommandException) {
791 loge("setAllowedCarriers: CommandException: " +
792 ar.exception);
793 } else {
794 loge("setAllowedCarriers: Unknown exception");
795 }
796 }
797 // Result cannot be null. Return -1 on error.
798 if (request.result == null) {
799 request.result = new int[]{-1};
800 }
801 synchronized (request) {
802 request.notifyAll();
803 }
804 break;
805
806 case CMD_GET_ALLOWED_CARRIERS:
807 request = (MainThreadRequest) msg.obj;
808 onCompleted = obtainMessage(EVENT_GET_ALLOWED_CARRIERS_DONE, request);
809 mPhone.getAllowedCarriers(onCompleted);
810 break;
811
812 case EVENT_GET_ALLOWED_CARRIERS_DONE:
813 ar = (AsyncResult) msg.obj;
814 request = (MainThreadRequest) ar.userObj;
815 if (ar.exception == null && ar.result != null) {
816 request.result = ar.result;
817 } else {
818 if (ar.result == null) {
819 loge("getAllowedCarriers: Empty response");
820 } else if (ar.exception instanceof CommandException) {
821 loge("getAllowedCarriers: CommandException: " +
822 ar.exception);
823 } else {
824 loge("getAllowedCarriers: Unknown exception");
825 }
826 }
827 // Result cannot be null. Return empty list of CarrierIdentifier.
828 if (request.result == null) {
829 request.result = new ArrayList<CarrierIdentifier>(0);
830 }
831 synchronized (request) {
832 request.notifyAll();
833 }
834 break;
835
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700836 default:
837 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
838 break;
839 }
840 }
Jake Hambye994d462014-02-03 13:10:13 -0800841
842 private void handleNullReturnEvent(Message msg, String command) {
843 AsyncResult ar = (AsyncResult) msg.obj;
844 MainThreadRequest request = (MainThreadRequest) ar.userObj;
845 if (ar.exception == null) {
846 request.result = true;
847 } else {
848 request.result = false;
849 if (ar.exception instanceof CommandException) {
850 loge(command + ": CommandException: " + ar.exception);
851 } else {
852 loge(command + ": Unknown exception");
853 }
854 }
855 synchronized (request) {
856 request.notifyAll();
857 }
858 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700859 }
860
861 /**
862 * Posts the specified command to be executed on the main thread,
863 * waits for the request to complete, and returns the result.
864 * @see #sendRequestAsync
865 */
866 private Object sendRequest(int command, Object argument) {
Sanket Padawe56e75a32016-02-08 12:18:19 -0800867 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Wink Saville36469e72014-06-11 15:17:00 -0700868 }
869
870 /**
871 * Posts the specified command to be executed on the main thread,
872 * waits for the request to complete, and returns the result.
873 * @see #sendRequestAsync
874 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800875 private Object sendRequest(int command, Object argument, Integer subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700876 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
877 throw new RuntimeException("This method will deadlock if called from the main thread.");
878 }
879
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800880 MainThreadRequest request = new MainThreadRequest(argument, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700881 Message msg = mMainThreadHandler.obtainMessage(command, request);
882 msg.sendToTarget();
883
884 // Wait for the request to complete
885 synchronized (request) {
886 while (request.result == null) {
887 try {
888 request.wait();
889 } catch (InterruptedException e) {
890 // Do nothing, go back and wait until the request is complete
891 }
892 }
893 }
894 return request.result;
895 }
896
897 /**
898 * Asynchronous ("fire and forget") version of sendRequest():
899 * Posts the specified command to be executed on the main thread, and
900 * returns immediately.
901 * @see #sendRequest
902 */
903 private void sendRequestAsync(int command) {
904 mMainThreadHandler.sendEmptyMessage(command);
905 }
906
907 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -0700908 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
909 * @see {@link #sendRequest(int,Object)}
910 */
911 private void sendRequestAsync(int command, Object argument) {
912 MainThreadRequest request = new MainThreadRequest(argument);
913 Message msg = mMainThreadHandler.obtainMessage(command, request);
914 msg.sendToTarget();
915 }
916
917 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700918 * Initialize the singleton PhoneInterfaceManager instance.
919 * This is only done once, at startup, from PhoneApp.onCreate().
920 */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700921 /* package */ static PhoneInterfaceManager init(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700922 synchronized (PhoneInterfaceManager.class) {
923 if (sInstance == null) {
Sailesh Nepal194161e2014-07-03 08:57:44 -0700924 sInstance = new PhoneInterfaceManager(app, phone);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700925 } else {
926 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
927 }
928 return sInstance;
929 }
930 }
931
932 /** Private constructor; @see init() */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700933 private PhoneInterfaceManager(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700934 mApp = app;
935 mPhone = phone;
936 mCM = PhoneGlobals.getInstance().mCM;
Stuart Scott981d8582015-04-21 14:09:50 -0700937 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700938 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
939 mMainThreadHandler = new MainThreadHandler();
Andrew Leedf14ead2014-10-17 14:22:52 -0700940 mTelephonySharedPreferences =
Derek Tan97ebb422014-09-05 16:55:38 -0700941 PreferenceManager.getDefaultSharedPreferences(mPhone.getContext());
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800942 mSubscriptionController = SubscriptionController.getInstance();
Wink Saville3ab207e2014-11-20 13:07:20 -0800943
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700944 publish();
945 }
946
947 private void publish() {
948 if (DBG) log("publish: " + this);
949
950 ServiceManager.addService("phone", this);
951 }
952
Stuart Scott584921c2015-01-15 17:10:34 -0800953 private Phone getPhoneFromRequest(MainThreadRequest request) {
Sanket Padawe56e75a32016-02-08 12:18:19 -0800954 return (request.subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID)
955 ? mPhone : getPhone(request.subId);
Stuart Scott584921c2015-01-15 17:10:34 -0800956 }
957
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800958 private UiccCard getUiccCardFromRequest(MainThreadRequest request) {
959 Phone phone = getPhoneFromRequest(request);
960 return phone == null ? null :
961 UiccController.getInstance().getUiccCard(phone.getPhoneId());
962 }
963
Wink Saville36469e72014-06-11 15:17:00 -0700964 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -0700965 private Phone getPhone(int subId) {
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800966 return PhoneFactory.getPhone(mSubscriptionController.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -0700967 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700968 //
969 // Implementation of the ITelephony interface.
970 //
971
972 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700973 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -0700974 }
975
Wink Savilleb564aae2014-10-23 10:18:09 -0700976 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700977 if (DBG) log("dial: " + number);
978 // No permission check needed here: This is just a wrapper around the
979 // ACTION_DIAL intent, which is available to any app since it puts up
980 // the UI before it does anything.
981
982 String url = createTelUrl(number);
983 if (url == null) {
984 return;
985 }
986
987 // PENDING: should we just silently fail if phone is offhook or ringing?
Wink Saville36469e72014-06-11 15:17:00 -0700988 PhoneConstants.State state = mCM.getState(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700989 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
990 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
991 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
992 mApp.startActivity(intent);
993 }
994 }
995
996 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700997 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -0700998 }
999
Wink Savilleb564aae2014-10-23 10:18:09 -07001000 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001001 if (DBG) log("call: " + number);
1002
1003 // This is just a wrapper around the ACTION_CALL intent, but we still
1004 // need to do a permission check since we're calling startActivity()
1005 // from the context of the phone app.
1006 enforceCallPermission();
1007
1008 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
1009 != AppOpsManager.MODE_ALLOWED) {
1010 return;
1011 }
1012
1013 String url = createTelUrl(number);
1014 if (url == null) {
1015 return;
1016 }
1017
Wink Saville08874612014-08-31 19:19:58 -07001018 boolean isValid = false;
Narayan Kamath1c496c22015-04-16 14:40:19 +01001019 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoList();
Wink Saville3ab207e2014-11-20 13:07:20 -08001020 if (slist != null) {
1021 for (SubscriptionInfo subInfoRecord : slist) {
1022 if (subInfoRecord.getSubscriptionId() == subId) {
1023 isValid = true;
1024 break;
1025 }
Wink Saville08874612014-08-31 19:19:58 -07001026 }
1027 }
1028 if (isValid == false) {
1029 return;
1030 }
1031
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001032 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
Wink Saville36469e72014-06-11 15:17:00 -07001033 intent.putExtra(SUBSCRIPTION_KEY, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001034 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1035 mApp.startActivity(intent);
1036 }
1037
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001038 /**
1039 * End a call based on call state
1040 * @return true is a call was ended
1041 */
1042 public boolean endCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001043 return endCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001044 }
1045
1046 /**
1047 * End a call based on the call state of the subId
1048 * @return true is a call was ended
1049 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001050 public boolean endCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001051 enforceCallPermission();
Stuart Scott584921c2015-01-15 17:10:34 -08001052 return (Boolean) sendRequest(CMD_END_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001053 }
1054
1055 public void answerRingingCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001056 answerRingingCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001057 }
1058
Wink Savilleb564aae2014-10-23 10:18:09 -07001059 public void answerRingingCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001060 if (DBG) log("answerRingingCall...");
1061 // TODO: there should eventually be a separate "ANSWER_PHONE" permission,
1062 // but that can probably wait till the big TelephonyManager API overhaul.
1063 // For now, protect this call with the MODIFY_PHONE_STATE permission.
1064 enforceModifyPermission();
Stuart Scott584921c2015-01-15 17:10:34 -08001065 sendRequest(CMD_ANSWER_RINGING_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001066 }
1067
1068 /**
1069 * Make the actual telephony calls to implement answerRingingCall().
1070 * This should only be called from the main thread of the Phone app.
1071 * @see #answerRingingCall
1072 *
1073 * TODO: it would be nice to return true if we answered the call, or
1074 * false if there wasn't actually a ringing incoming call, or some
1075 * other error occurred. (In other words, pass back the return value
1076 * from PhoneUtils.answerCall() or PhoneUtils.answerAndEndActive().)
1077 * But that would require calling this method via sendRequest() rather
1078 * than sendRequestAsync(), and right now we don't actually *need* that
1079 * return value, so let's just return void for now.
1080 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001081 private void answerRingingCallInternal(int subId) {
Wink Saville08874612014-08-31 19:19:58 -07001082 final boolean hasRingingCall = !getPhone(subId).getRingingCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001083 if (hasRingingCall) {
Wink Saville08874612014-08-31 19:19:58 -07001084 final boolean hasActiveCall = !getPhone(subId).getForegroundCall().isIdle();
1085 final boolean hasHoldingCall = !getPhone(subId).getBackgroundCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001086 if (hasActiveCall && hasHoldingCall) {
1087 // Both lines are in use!
1088 // TODO: provide a flag to let the caller specify what
1089 // policy to use if both lines are in use. (The current
1090 // behavior is hardwired to "answer incoming, end ongoing",
1091 // which is how the CALL button is specced to behave.)
1092 PhoneUtils.answerAndEndActive(mCM, mCM.getFirstActiveRingingCall());
1093 return;
1094 } else {
1095 // answerCall() will automatically hold the current active
1096 // call, if there is one.
1097 PhoneUtils.answerCall(mCM.getFirstActiveRingingCall());
1098 return;
1099 }
1100 } else {
1101 // No call was ringing.
1102 return;
1103 }
1104 }
1105
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001106 /**
Santos Cordon5422a8d2014-09-12 04:20:56 -07001107 * This method is no longer used and can be removed once TelephonyManager stops referring to it.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001108 */
Santos Cordon5422a8d2014-09-12 04:20:56 -07001109 public void silenceRinger() {
1110 Log.e(LOG_TAG, "silenseRinger not supported");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001111 }
1112
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001113 @Override
1114 public boolean isOffhook(String callingPackage) {
1115 return isOffhookForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001116 }
1117
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001118 @Override
1119 public boolean isOffhookForSubscriber(int subId, String callingPackage) {
1120 if (!canReadPhoneState(callingPackage, "isOffhookForSubscriber")) {
1121 return false;
1122 }
1123
Sanket Padawe356d7632015-06-22 14:03:32 -07001124 final Phone phone = getPhone(subId);
1125 if (phone != null) {
1126 return (phone.getState() == PhoneConstants.State.OFFHOOK);
1127 } else {
1128 return false;
1129 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001130 }
1131
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001132 @Override
1133 public boolean isRinging(String callingPackage) {
1134 return (isRingingForSubscriber(getDefaultSubscription(), callingPackage));
Wink Saville36469e72014-06-11 15:17:00 -07001135 }
1136
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001137 @Override
1138 public boolean isRingingForSubscriber(int subId, String callingPackage) {
1139 if (!canReadPhoneState(callingPackage, "isRingingForSubscriber")) {
1140 return false;
1141 }
1142
Sanket Padawe356d7632015-06-22 14:03:32 -07001143 final Phone phone = getPhone(subId);
1144 if (phone != null) {
1145 return (phone.getState() == PhoneConstants.State.RINGING);
1146 } else {
1147 return false;
1148 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001149 }
1150
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001151 @Override
1152 public boolean isIdle(String callingPackage) {
1153 return isIdleForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001154 }
1155
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001156 @Override
1157 public boolean isIdleForSubscriber(int subId, String callingPackage) {
1158 if (!canReadPhoneState(callingPackage, "isIdleForSubscriber")) {
1159 return false;
1160 }
1161
Sanket Padawe356d7632015-06-22 14:03:32 -07001162 final Phone phone = getPhone(subId);
1163 if (phone != null) {
1164 return (phone.getState() == PhoneConstants.State.IDLE);
1165 } else {
1166 return false;
1167 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001168 }
1169
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001170 public boolean supplyPin(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001171 return supplyPinForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001172 }
1173
Wink Savilleb564aae2014-10-23 10:18:09 -07001174 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001175 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001176 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1177 }
1178
1179 public boolean supplyPuk(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001180 return supplyPukForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001181 }
1182
Wink Savilleb564aae2014-10-23 10:18:09 -07001183 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001184 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001185 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1186 }
1187
1188 /** {@hide} */
1189 public int[] supplyPinReportResult(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001190 return supplyPinReportResultForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001191 }
1192
Wink Savilleb564aae2014-10-23 10:18:09 -07001193 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001194 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001195 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001196 checkSimPin.start();
1197 return checkSimPin.unlockSim(null, pin);
1198 }
1199
Wink Saville9de0f752013-10-22 19:04:03 -07001200 /** {@hide} */
1201 public int[] supplyPukReportResult(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001202 return supplyPukReportResultForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001203 }
1204
Wink Savilleb564aae2014-10-23 10:18:09 -07001205 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001206 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001207 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001208 checkSimPuk.start();
1209 return checkSimPuk.unlockSim(puk, pin);
1210 }
1211
1212 /**
Wink Saville9de0f752013-10-22 19:04:03 -07001213 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001214 * a synchronous one.
1215 */
1216 private static class UnlockSim extends Thread {
1217
1218 private final IccCard mSimCard;
1219
1220 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07001221 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1222 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001223
1224 // For replies from SimCard interface
1225 private Handler mHandler;
1226
1227 // For async handler to identify request type
1228 private static final int SUPPLY_PIN_COMPLETE = 100;
1229
1230 public UnlockSim(IccCard simCard) {
1231 mSimCard = simCard;
1232 }
1233
1234 @Override
1235 public void run() {
1236 Looper.prepare();
1237 synchronized (UnlockSim.this) {
1238 mHandler = new Handler() {
1239 @Override
1240 public void handleMessage(Message msg) {
1241 AsyncResult ar = (AsyncResult) msg.obj;
1242 switch (msg.what) {
1243 case SUPPLY_PIN_COMPLETE:
1244 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
1245 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07001246 mRetryCount = msg.arg1;
1247 if (ar.exception != null) {
1248 if (ar.exception instanceof CommandException &&
1249 ((CommandException)(ar.exception)).getCommandError()
1250 == CommandException.Error.PASSWORD_INCORRECT) {
1251 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
1252 } else {
1253 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1254 }
1255 } else {
1256 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
1257 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001258 mDone = true;
1259 UnlockSim.this.notifyAll();
1260 }
1261 break;
1262 }
1263 }
1264 };
1265 UnlockSim.this.notifyAll();
1266 }
1267 Looper.loop();
1268 }
1269
1270 /*
1271 * Use PIN or PUK to unlock SIM card
1272 *
1273 * If PUK is null, unlock SIM card with PIN
1274 *
1275 * If PUK is not null, unlock SIM card with PUK and set PIN code
1276 */
Wink Saville9de0f752013-10-22 19:04:03 -07001277 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001278
1279 while (mHandler == null) {
1280 try {
1281 wait();
1282 } catch (InterruptedException e) {
1283 Thread.currentThread().interrupt();
1284 }
1285 }
1286 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
1287
1288 if (puk == null) {
1289 mSimCard.supplyPin(pin, callback);
1290 } else {
1291 mSimCard.supplyPuk(puk, pin, callback);
1292 }
1293
1294 while (!mDone) {
1295 try {
1296 Log.d(LOG_TAG, "wait for done");
1297 wait();
1298 } catch (InterruptedException e) {
1299 // Restore the interrupted status
1300 Thread.currentThread().interrupt();
1301 }
1302 }
1303 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07001304 int[] resultArray = new int[2];
1305 resultArray[0] = mResult;
1306 resultArray[1] = mRetryCount;
1307 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001308 }
1309 }
1310
1311 public void updateServiceLocation() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001312 updateServiceLocationForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001313
1314 }
1315
Wink Savilleb564aae2014-10-23 10:18:09 -07001316 public void updateServiceLocationForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001317 // No permission check needed here: this call is harmless, and it's
1318 // needed for the ServiceState.requestStateUpdate() call (which is
1319 // already intentionally exposed to 3rd parties.)
Sanket Padawe356d7632015-06-22 14:03:32 -07001320 final Phone phone = getPhone(subId);
1321 if (phone != null) {
1322 phone.updateServiceLocation();
1323 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001324 }
1325
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001326 @Override
1327 public boolean isRadioOn(String callingPackage) {
1328 return isRadioOnForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001329 }
1330
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001331 @Override
1332 public boolean isRadioOnForSubscriber(int subId, String callingPackage) {
1333 if (!canReadPhoneState(callingPackage, "isRadioOnForSubscriber")) {
1334 return false;
1335 }
1336 return isRadioOnForSubscriber(subId);
1337 }
1338
1339 private boolean isRadioOnForSubscriber(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001340 final Phone phone = getPhone(subId);
1341 if (phone != null) {
1342 return phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF;
1343 } else {
1344 return false;
1345 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001346 }
1347
1348 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001349 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001350
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001351 }
Wink Saville36469e72014-06-11 15:17:00 -07001352
Wink Savilleb564aae2014-10-23 10:18:09 -07001353 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001354 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001355 final Phone phone = getPhone(subId);
1356 if (phone != null) {
1357 phone.setRadioPower(!isRadioOnForSubscriber(subId));
1358 }
Wink Saville36469e72014-06-11 15:17:00 -07001359 }
1360
1361 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001362 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001363 }
1364
Wink Savilleb564aae2014-10-23 10:18:09 -07001365 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07001366 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001367 final Phone phone = getPhone(subId);
1368 if (phone == null) {
1369 return false;
1370 }
1371 if ((phone.getServiceState().getState() !=
Wink Saville36469e72014-06-11 15:17:00 -07001372 ServiceState.STATE_POWER_OFF) != turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001373 toggleRadioOnOffForSubscriber(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001374 }
1375 return true;
1376 }
Wink Saville36469e72014-06-11 15:17:00 -07001377
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001378 public boolean needMobileRadioShutdown() {
1379 /*
1380 * If any of the Radios are available, it will need to be
1381 * shutdown. So return true if any Radio is available.
1382 */
1383 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1384 Phone phone = PhoneFactory.getPhone(i);
1385 if (phone != null && phone.isRadioAvailable()) return true;
1386 }
1387 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
1388 return false;
1389 }
1390
1391 public void shutdownMobileRadios() {
1392 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1393 logv("Shutting down Phone " + i);
1394 shutdownRadioUsingPhoneId(i);
1395 }
1396 }
1397
1398 private void shutdownRadioUsingPhoneId(int phoneId) {
1399 enforceModifyPermission();
1400 Phone phone = PhoneFactory.getPhone(phoneId);
1401 if (phone != null && phone.isRadioAvailable()) {
1402 phone.shutdownRadio();
1403 }
1404 }
1405
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001406 public boolean setRadioPower(boolean turnOn) {
Wei Liu9ae2a062016-08-08 11:09:34 -07001407 final Phone defaultPhone = PhoneFactory.getDefaultPhone();
1408 if (defaultPhone != null) {
1409 defaultPhone.setRadioPower(turnOn);
1410 return true;
1411 } else {
1412 loge("There's no default phone.");
1413 return false;
1414 }
Wink Saville36469e72014-06-11 15:17:00 -07001415 }
1416
Wink Savilleb564aae2014-10-23 10:18:09 -07001417 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001418 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001419 final Phone phone = getPhone(subId);
1420 if (phone != null) {
1421 phone.setRadioPower(turnOn);
1422 return true;
1423 } else {
1424 return false;
1425 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001426 }
1427
Wink Saville36469e72014-06-11 15:17:00 -07001428 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001429 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001430 public boolean enableDataConnectivity() {
1431 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001432 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001433 final Phone phone = getPhone(subId);
1434 if (phone != null) {
1435 phone.setDataEnabled(true);
1436 return true;
1437 } else {
1438 return false;
1439 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001440 }
1441
Wink Saville36469e72014-06-11 15:17:00 -07001442 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001443 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001444 public boolean disableDataConnectivity() {
1445 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001446 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001447 final Phone phone = getPhone(subId);
1448 if (phone != null) {
1449 phone.setDataEnabled(false);
1450 return true;
1451 } else {
1452 return false;
1453 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001454 }
1455
Wink Saville36469e72014-06-11 15:17:00 -07001456 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001457 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001458 public boolean isDataConnectivityPossible() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001459 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001460 final Phone phone = getPhone(subId);
1461 if (phone != null) {
1462 return phone.isDataConnectivityPossible();
1463 } else {
1464 return false;
1465 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001466 }
1467
1468 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001469 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07001470 }
1471
Wink Savilleb564aae2014-10-23 10:18:09 -07001472 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001473 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001474 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1475 return false;
1476 }
Wink Saville36469e72014-06-11 15:17:00 -07001477 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001478 }
1479
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001480 public int getCallState() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001481 return getCallStateForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001482 }
1483
Shishir Agrawala9f32182016-04-12 12:00:16 -07001484 public int getCallStateForSlot(int slotId) {
1485 Phone phone = PhoneFactory.getPhone(slotId);
1486 return phone == null ? TelephonyManager.CALL_STATE_IDLE :
1487 DefaultPhoneNotifier.convertCallState(phone.getState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001488 }
1489
Sanket Padawe356d7632015-06-22 14:03:32 -07001490 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001491 public int getDataState() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001492 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001493 if (phone != null) {
1494 return DefaultPhoneNotifier.convertDataState(phone.getDataConnectionState());
1495 } else {
1496 return DefaultPhoneNotifier.convertDataState(PhoneConstants.DataState.DISCONNECTED);
1497 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001498 }
1499
Sanket Padawe356d7632015-06-22 14:03:32 -07001500 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001501 public int getDataActivity() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001502 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001503 if (phone != null) {
1504 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
1505 } else {
1506 return TelephonyManager.DATA_ACTIVITY_NONE;
1507 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001508 }
1509
1510 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001511 public Bundle getCellLocation(String callingPackage) {
1512 enforceFineOrCoarseLocationPermission("getCellLocation");
1513
1514 // OP_COARSE_LOCATION controls both fine and coarse location.
1515 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1516 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001517 log("getCellLocation: returning null; mode != allowed");
Svetoslav64fad262015-04-14 14:35:21 -07001518 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001519 }
1520
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001521 if (checkIfCallerIsSelfOrForegroundUser() ||
1522 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001523 if (DBG_LOC) log("getCellLocation: is active user");
1524 Bundle data = new Bundle();
Legler Wu2c01cdf2014-12-08 19:00:59 +08001525 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001526 if (phone == null) {
1527 return null;
1528 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001529
1530 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
1531 phone.getCellLocation(workSource).fillInNotifierBundle(data);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001532 return data;
1533 } else {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001534 log("getCellLocation: suppress non-active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001535 return null;
1536 }
1537 }
1538
Svetoslav64fad262015-04-14 14:35:21 -07001539 private void enforceFineOrCoarseLocationPermission(String message) {
1540 try {
1541 mApp.enforceCallingOrSelfPermission(
1542 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1543 } catch (SecurityException e) {
1544 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1545 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1546 // is the weaker precondition
1547 mApp.enforceCallingOrSelfPermission(
1548 android.Manifest.permission.ACCESS_COARSE_LOCATION, message);
1549 }
1550 }
1551
1552
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001553 @Override
1554 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001555 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001556 }
1557
Sanket Padawe356d7632015-06-22 14:03:32 -07001558 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001559 public void enableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001560 mApp.enforceCallingOrSelfPermission(
1561 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001562 final Phone phone = getPhone(subId);
1563 if (phone != null) {
1564 phone.enableLocationUpdates();
1565 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001566 }
1567
1568 @Override
1569 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001570 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001571 }
1572
Sanket Padawe356d7632015-06-22 14:03:32 -07001573 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001574 public void disableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001575 mApp.enforceCallingOrSelfPermission(
1576 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001577 final Phone phone = getPhone(subId);
1578 if (phone != null) {
1579 phone.disableLocationUpdates();
1580 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001581 }
1582
1583 @Override
1584 @SuppressWarnings("unchecked")
1585 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
Svetoslav64fad262015-04-14 14:35:21 -07001586 enforceFineOrCoarseLocationPermission("getNeighboringCellInfo");
1587
1588 // OP_COARSE_LOCATION controls both fine and coarse location.
1589 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1590 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1591 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001592 }
1593
1594 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
1595 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1596 return null;
1597 }
Svetoslav64fad262015-04-14 14:35:21 -07001598
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001599 if (checkIfCallerIsSelfOrForegroundUser() ||
1600 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001601 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
1602
1603 ArrayList<NeighboringCellInfo> cells = null;
1604
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001605 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001606 try {
1607 cells = (ArrayList<NeighboringCellInfo>) sendRequest(
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001608 CMD_HANDLE_NEIGHBORING_CELL, workSource,
Sanket Padawe56e75a32016-02-08 12:18:19 -08001609 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001610 } catch (RuntimeException e) {
Wink Saville36469e72014-06-11 15:17:00 -07001611 Log.e(LOG_TAG, "getNeighboringCellInfo " + e);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001612 }
1613 return cells;
1614 } else {
1615 if (DBG_LOC) log("getNeighboringCellInfo: suppress non-active user");
1616 return null;
1617 }
1618 }
1619
1620
1621 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001622 public List<CellInfo> getAllCellInfo(String callingPackage) {
1623 enforceFineOrCoarseLocationPermission("getAllCellInfo");
1624
1625 // OP_COARSE_LOCATION controls both fine and coarse location.
1626 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1627 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1628 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001629 }
1630
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001631 if (checkIfCallerIsSelfOrForegroundUser() ||
1632 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001633 if (DBG_LOC) log("getAllCellInfo: is active user");
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001634 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
Legler Wu2c01cdf2014-12-08 19:00:59 +08001635 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
1636 for (Phone phone : PhoneFactory.getPhones()) {
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001637 final List<CellInfo> info = phone.getAllCellInfo(workSource);
1638 if (info != null) cellInfos.addAll(info);
Legler Wu2c01cdf2014-12-08 19:00:59 +08001639 }
1640 return cellInfos;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001641 } else {
1642 if (DBG_LOC) log("getAllCellInfo: suppress non-active user");
1643 return null;
1644 }
1645 }
1646
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001647 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001648 public void setCellInfoListRate(int rateInMillis) {
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001649 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
1650 mPhone.setCellInfoListRate(rateInMillis, workSource);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001651 }
1652
Shishir Agrawala9f32182016-04-12 12:00:16 -07001653 @Override
1654 public String getImeiForSlot(int slotId, String callingPackage) {
1655 if (!canReadPhoneState(callingPackage, "getImeiForSlot")) {
1656 return null;
1657 }
1658 Phone phone = PhoneFactory.getPhone(slotId);
1659 return phone == null ? null : phone.getImei();
1660 }
1661
1662 @Override
1663 public String getDeviceSoftwareVersionForSlot(int slotId, String callingPackage) {
1664 if (!canReadPhoneState(callingPackage, "getDeviceSoftwareVersionForSlot")) {
1665 return null;
1666 }
1667 Phone phone = PhoneFactory.getPhone(slotId);
1668 return phone == null ? null : phone.getDeviceSvn();
1669 }
1670
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001671 //
1672 // Internal helper methods.
1673 //
1674
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001675 /**
1676 * Returns true if the caller holds INTERACT_ACROSS_USERS_FULL.
1677 */
1678 private boolean checkCallerInteractAcrossUsersFull() {
1679 return mPhone.getContext().checkCallingOrSelfPermission(
1680 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1681 == PackageManager.PERMISSION_GRANTED;
1682 }
1683
Jake Hambye994d462014-02-03 13:10:13 -08001684 private static boolean checkIfCallerIsSelfOrForegroundUser() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001685 boolean ok;
1686
1687 boolean self = Binder.getCallingUid() == Process.myUid();
1688 if (!self) {
1689 // Get the caller's user id then clear the calling identity
1690 // which will be restored in the finally clause.
1691 int callingUser = UserHandle.getCallingUserId();
1692 long ident = Binder.clearCallingIdentity();
1693
1694 try {
1695 // With calling identity cleared the current user is the foreground user.
1696 int foregroundUser = ActivityManager.getCurrentUser();
1697 ok = (foregroundUser == callingUser);
1698 if (DBG_LOC) {
1699 log("checkIfCallerIsSelfOrForegoundUser: foregroundUser=" + foregroundUser
1700 + " callingUser=" + callingUser + " ok=" + ok);
1701 }
1702 } catch (Exception ex) {
1703 if (DBG_LOC) loge("checkIfCallerIsSelfOrForegoundUser: Exception ex=" + ex);
1704 ok = false;
1705 } finally {
1706 Binder.restoreCallingIdentity(ident);
1707 }
1708 } else {
1709 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: is self");
1710 ok = true;
1711 }
1712 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: ret=" + ok);
1713 return ok;
1714 }
1715
1716 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001717 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1718 *
1719 * @throws SecurityException if the caller does not have the required permission
1720 */
1721 private void enforceModifyPermission() {
1722 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1723 }
1724
1725 /**
Junda Liua2e36012014-07-09 18:30:01 -07001726 * Make sure either system app or the caller has carrier privilege.
1727 *
1728 * @throws SecurityException if the caller does not have the required permission/privilege
1729 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001730 private void enforceModifyPermissionOrCarrierPrivilege(int subId) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001731 int permission = mApp.checkCallingOrSelfPermission(
1732 android.Manifest.permission.MODIFY_PHONE_STATE);
1733 if (permission == PackageManager.PERMISSION_GRANTED) {
1734 return;
1735 }
1736
1737 log("No modify permission, check carrier privilege next.");
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001738 enforceCarrierPrivilege(subId);
Junda Liua2e36012014-07-09 18:30:01 -07001739 }
1740
1741 /**
1742 * Make sure the caller has carrier privilege.
1743 *
1744 * @throws SecurityException if the caller does not have the required permission
1745 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001746 private void enforceCarrierPrivilege(int subId) {
1747 if (getCarrierPrivilegeStatus(subId) !=
1748 TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001749 loge("No Carrier Privilege.");
1750 throw new SecurityException("No Carrier Privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001751 }
1752 }
1753
1754 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001755 * Make sure the caller has the CALL_PHONE permission.
1756 *
1757 * @throws SecurityException if the caller does not have the required permission
1758 */
1759 private void enforceCallPermission() {
1760 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1761 }
1762
Stuart Scott8eef64f2015-04-08 15:13:54 -07001763 private void enforceConnectivityInternalPermission() {
1764 mApp.enforceCallingOrSelfPermission(
1765 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1766 "ConnectivityService");
1767 }
1768
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001769 private String createTelUrl(String number) {
1770 if (TextUtils.isEmpty(number)) {
1771 return null;
1772 }
1773
Jake Hambye994d462014-02-03 13:10:13 -08001774 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001775 }
1776
Ihab Awadf9e92732013-12-05 18:02:52 -08001777 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001778 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1779 }
1780
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001781 private static void logv(String msg) {
1782 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
1783 }
1784
Ihab Awadf9e92732013-12-05 18:02:52 -08001785 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001786 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1787 }
1788
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001789 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001790 public int getActivePhoneType() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001791 return getActivePhoneTypeForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001792 }
1793
Sanket Padawe356d7632015-06-22 14:03:32 -07001794 @Override
Shishir Agrawala9f32182016-04-12 12:00:16 -07001795 public int getActivePhoneTypeForSlot(int slotId) {
1796 final Phone phone = PhoneFactory.getPhone(slotId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001797 if (phone == null) {
1798 return PhoneConstants.PHONE_TYPE_NONE;
1799 } else {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001800 return phone.getPhoneType();
Sanket Padawe356d7632015-06-22 14:03:32 -07001801 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001802 }
1803
1804 /**
1805 * Returns the CDMA ERI icon index to display
1806 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001807 @Override
1808 public int getCdmaEriIconIndex(String callingPackage) {
1809 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001810 }
1811
Sanket Padawe356d7632015-06-22 14:03:32 -07001812 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001813 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage) {
1814 if (!canReadPhoneState(callingPackage, "getCdmaEriIconIndexForSubscriber")) {
1815 return -1;
1816 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001817 final Phone phone = getPhone(subId);
1818 if (phone != null) {
1819 return phone.getCdmaEriIconIndex();
1820 } else {
1821 return -1;
1822 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001823 }
1824
1825 /**
1826 * Returns the CDMA ERI icon mode,
1827 * 0 - ON
1828 * 1 - FLASHING
1829 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001830 @Override
1831 public int getCdmaEriIconMode(String callingPackage) {
1832 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001833 }
1834
Sanket Padawe356d7632015-06-22 14:03:32 -07001835 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001836 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage) {
1837 if (!canReadPhoneState(callingPackage, "getCdmaEriIconModeForSubscriber")) {
1838 return -1;
1839 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001840 final Phone phone = getPhone(subId);
1841 if (phone != null) {
1842 return phone.getCdmaEriIconMode();
1843 } else {
1844 return -1;
1845 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001846 }
1847
1848 /**
1849 * Returns the CDMA ERI text,
1850 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001851 @Override
1852 public String getCdmaEriText(String callingPackage) {
1853 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001854 }
1855
Sanket Padawe356d7632015-06-22 14:03:32 -07001856 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001857 public String getCdmaEriTextForSubscriber(int subId, String callingPackage) {
1858 if (!canReadPhoneState(callingPackage, "getCdmaEriIconTextForSubscriber")) {
1859 return null;
1860 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001861 final Phone phone = getPhone(subId);
1862 if (phone != null) {
1863 return phone.getCdmaEriText();
1864 } else {
1865 return null;
1866 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001867 }
1868
1869 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07001870 * Returns the CDMA MDN.
1871 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001872 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001873 public String getCdmaMdn(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001874 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001875 final Phone phone = getPhone(subId);
1876 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA && phone != null) {
1877 return phone.getLine1Number();
Junda Liuca05d5d2014-08-14 22:36:34 -07001878 } else {
1879 return null;
1880 }
1881 }
1882
1883 /**
1884 * Returns the CDMA MIN.
1885 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001886 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001887 public String getCdmaMin(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001888 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001889 final Phone phone = getPhone(subId);
1890 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1891 return phone.getCdmaMin();
Junda Liuca05d5d2014-08-14 22:36:34 -07001892 } else {
1893 return null;
1894 }
1895 }
1896
1897 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001898 * Returns true if CDMA provisioning needs to run.
1899 */
1900 public boolean needsOtaServiceProvisioning() {
1901 return mPhone.needsOtaServiceProvisioning();
1902 }
1903
1904 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001905 * Sets the voice mail number of a given subId.
1906 */
1907 @Override
1908 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001909 enforceCarrierPrivilege(subId);
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001910 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
1911 new Pair<String, String>(alphaTag, number), new Integer(subId));
1912 return success;
1913 }
1914
Ta-wei Yen87c49842016-05-13 21:19:52 -07001915 @Override
Ta-wei Yenb0f695b2016-08-08 17:33:11 -07001916 public void setVisualVoicemailEnabled(String callingPackage,
1917 PhoneAccountHandle phoneAccountHandle, boolean enabled) {
1918 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
1919 if (!TextUtils.equals(callingPackage,
1920 TelecomManager.from(mPhone.getContext()).getDefaultDialerPackage())) {
1921 enforceModifyPermissionOrCarrierPrivilege(
1922 PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle));
1923 }
1924 VisualVoicemailSettingsUtil.setEnabled(mPhone.getContext(), phoneAccountHandle, enabled);
1925 }
1926
1927 @Override
1928 public boolean isVisualVoicemailEnabled(String callingPackage,
1929 PhoneAccountHandle phoneAccountHandle) {
1930 if (!canReadPhoneState(callingPackage, "isVisualVoicemailEnabled")) {
1931 return false;
1932 }
1933 return VisualVoicemailSettingsUtil.isEnabled(mPhone.getContext(), phoneAccountHandle);
1934 }
1935
1936 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07001937 public void enableVisualVoicemailSmsFilter(String callingPackage, int subId,
1938 VisualVoicemailSmsFilterSettings settings) {
1939 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001940 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07001941 .enableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId,
1942 settings);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001943 }
1944
1945 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07001946 public void disableVisualVoicemailSmsFilter(String callingPackage, int subId) {
1947 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001948 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07001949 .disableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001950 }
1951
1952 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07001953 public VisualVoicemailSmsFilterSettings getVisualVoicemailSmsFilterSettings(
1954 String callingPackage, int subId) {
1955 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001956 return VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07001957 .getVisualVoicemailSmsFilterSettings(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001958 }
1959
1960 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07001961 public VisualVoicemailSmsFilterSettings getSystemVisualVoicemailSmsFilterSettings(
1962 String packageName, int subId) {
1963 enforceReadPrivilegedPermission();
Ta-wei Yen87c49842016-05-13 21:19:52 -07001964 return VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07001965 .getVisualVoicemailSmsFilterSettings(mPhone.getContext(), packageName, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001966 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001967 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001968 * Returns the unread count of voicemails
1969 */
1970 public int getVoiceMessageCount() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001971 return getVoiceMessageCountForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001972 }
1973
1974 /**
1975 * Returns the unread count of voicemails for a subId
1976 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001977 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001978 public int getVoiceMessageCountForSubscriber( int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001979 final Phone phone = getPhone(subId);
1980 if (phone != null) {
1981 return phone.getVoiceMessageCount();
1982 } else {
1983 return 0;
1984 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001985 }
1986
1987 /**
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001988 * Returns the data network type.
1989 * Legacy call, permission-free.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001990 *
1991 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
1992 */
1993 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001994 public int getNetworkType() {
1995 final Phone phone = getPhone(getDefaultSubscription());
1996 if (phone != null) {
1997 return phone.getServiceState().getDataNetworkType();
1998 } else {
1999 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2000 }
Wink Saville36469e72014-06-11 15:17:00 -07002001 }
2002
2003 /**
2004 * Returns the network type for a subId
2005 */
2006 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002007 public int getNetworkTypeForSubscriber(int subId, String callingPackage) {
2008 if (!canReadPhoneState(callingPackage, "getNetworkTypeForSubscriber")) {
2009 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2010 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002011
Sanket Padawe356d7632015-06-22 14:03:32 -07002012 final Phone phone = getPhone(subId);
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002013 if (phone != null) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002014 return phone.getServiceState().getDataNetworkType();
2015 } else {
2016 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2017 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002018 }
2019
2020 /**
2021 * Returns the data network type
2022 */
2023 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002024 public int getDataNetworkType(String callingPackage) {
2025 return getDataNetworkTypeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002026 }
2027
2028 /**
2029 * Returns the data network type for a subId
2030 */
2031 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002032 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage) {
2033 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2034 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2035 }
2036
Sanket Padawe356d7632015-06-22 14:03:32 -07002037 final Phone phone = getPhone(subId);
2038 if (phone != null) {
2039 return phone.getServiceState().getDataNetworkType();
2040 } else {
2041 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2042 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002043 }
2044
2045 /**
Wink Saville36469e72014-06-11 15:17:00 -07002046 * Returns the Voice network type for a subId
2047 */
2048 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002049 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage) {
2050 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2051 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2052 }
2053
Sanket Padawe356d7632015-06-22 14:03:32 -07002054 final Phone phone = getPhone(subId);
2055 if (phone != null) {
2056 return phone.getServiceState().getVoiceNetworkType();
2057 } else {
2058 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2059 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002060 }
2061
2062 /**
2063 * @return true if a ICC card is present
2064 */
2065 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07002066 // FIXME Make changes to pass defaultSimId of type int
Amit Mahajana6fc2a82015-01-06 11:53:51 -08002067 return hasIccCardUsingSlotId(mSubscriptionController.getSlotId(getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07002068 }
2069
2070 /**
2071 * @return true if a ICC card is present for a slotId
2072 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002073 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002074 public boolean hasIccCardUsingSlotId(int slotId) {
fionaxu6e6c68b2016-06-23 13:31:32 -07002075 final Phone phone = PhoneFactory.getPhone(slotId);
2076 if (phone != null) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002077 return phone.getIccCard().hasIccCard();
Amit Mahajana6fc2a82015-01-06 11:53:51 -08002078 } else {
2079 return false;
2080 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002081 }
2082
2083 /**
2084 * Return if the current radio is LTE on CDMA. This
2085 * is a tri-state return value as for a period of time
2086 * the mode may be unknown.
2087 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002088 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002089 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08002090 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002091 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002092 @Override
2093 public int getLteOnCdmaMode(String callingPackage) {
2094 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002095 }
2096
Sanket Padawe356d7632015-06-22 14:03:32 -07002097 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002098 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage) {
2099 if (!canReadPhoneState(callingPackage, "getLteOnCdmaModeForSubscriber")) {
2100 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2101 }
2102
Sanket Padawe356d7632015-06-22 14:03:32 -07002103 final Phone phone = getPhone(subId);
2104 if (phone == null) {
2105 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2106 } else {
2107 return phone.getLteOnCdmaMode();
2108 }
Wink Saville36469e72014-06-11 15:17:00 -07002109 }
2110
2111 public void setPhone(Phone phone) {
2112 mPhone = phone;
2113 }
2114
2115 /**
2116 * {@hide}
2117 * Returns Default subId, 0 in the case of single standby.
2118 */
Wink Savilleb564aae2014-10-23 10:18:09 -07002119 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002120 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07002121 }
2122
Shishir Agrawala9f32182016-04-12 12:00:16 -07002123 private int getSlotForDefaultSubscription() {
2124 return mSubscriptionController.getPhoneId(getDefaultSubscription());
2125 }
2126
Wink Savilleb564aae2014-10-23 10:18:09 -07002127 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002128 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002129 }
Ihab Awadf2177b72013-11-25 13:33:23 -08002130
2131 /**
2132 * @see android.telephony.TelephonyManager.WifiCallingChoices
2133 */
2134 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002135 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
2136 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08002137 }
2138
2139 /**
2140 * @see android.telephony.TelephonyManager.WifiCallingChoices
2141 */
2142 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002143 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
2144 Settings.System.putInt(mPhone.getContext().getContentResolver(),
2145 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08002146 }
2147
Sailesh Nepald1e68152013-12-12 19:08:02 -08002148 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07002149 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08002150 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08002151 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08002152
Shishir Agrawal566b7612013-10-28 14:41:00 -07002153 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002154 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(int subId, String AID) {
2155 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002156
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002157 if (DBG) log("iccOpenLogicalChannel: subId=" + subId + " aid=" + AID);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002158 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002159 CMD_OPEN_CHANNEL, AID, subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002160 if (DBG) log("iccOpenLogicalChannel: " + response);
2161 return response;
Shishir Agrawal566b7612013-10-28 14:41:00 -07002162 }
2163
2164 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002165 public boolean iccCloseLogicalChannel(int subId, int channel) {
2166 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002167
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002168 if (DBG) log("iccCloseLogicalChannel: subId=" + subId + " chnl=" + channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002169 if (channel < 0) {
2170 return false;
2171 }
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002172 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel, subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002173 if (DBG) log("iccCloseLogicalChannel: " + success);
2174 return success;
2175 }
2176
2177 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002178 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07002179 int command, int p1, int p2, int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002180 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002181
2182 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002183 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel +
2184 " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
Shishir Agrawal566b7612013-10-28 14:41:00 -07002185 " data=" + data);
2186 }
2187
2188 if (channel < 0) {
2189 return "";
2190 }
2191
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002192 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002193 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002194 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
2195
Shishir Agrawal566b7612013-10-28 14:41:00 -07002196 // Append the returned status code to the end of the response payload.
2197 String s = Integer.toHexString(
2198 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002199 if (response.payload != null) {
2200 s = IccUtils.bytesToHexString(response.payload) + s;
2201 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07002202 return s;
2203 }
Jake Hambye994d462014-02-03 13:10:13 -08002204
Evan Charltonc66da362014-05-16 14:06:40 -07002205 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002206 public String iccTransmitApduBasicChannel(int subId, int cla, int command, int p1, int p2,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002207 int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002208 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002209
2210 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002211 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd=" + command
2212 + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002213 }
2214
2215 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002216 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002217 if (DBG) log("iccTransmitApduBasicChannel: " + response);
2218
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002219 // Append the returned status code to the end of the response payload.
2220 String s = Integer.toHexString(
2221 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002222 if (response.payload != null) {
2223 s = IccUtils.bytesToHexString(response.payload) + s;
2224 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002225 return s;
2226 }
2227
2228 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002229 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002230 String filePath) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002231 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002232
2233 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002234 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " " +
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002235 p1 + " " + p2 + " " + p3 + ":" + filePath);
2236 }
2237
2238 IccIoResult response =
2239 (IccIoResult)sendRequest(CMD_EXCHANGE_SIM_IO,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002240 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
2241 subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002242
2243 if (DBG) {
2244 log("Exchange SIM_IO [R]" + response);
2245 }
2246
2247 byte[] result = null;
2248 int length = 2;
2249 if (response.payload != null) {
2250 length = 2 + response.payload.length;
2251 result = new byte[length];
2252 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
2253 } else {
2254 result = new byte[length];
2255 }
2256
2257 result[length - 1] = (byte) response.sw2;
2258 result[length - 2] = (byte) response.sw1;
2259 return result;
2260 }
2261
2262 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002263 public String sendEnvelopeWithStatus(int subId, String content) {
2264 enforceModifyPermissionOrCarrierPrivilege(subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002265
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002266 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content, subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002267 if (response.payload == null) {
2268 return "";
2269 }
2270
2271 // Append the returned status code to the end of the response payload.
2272 String s = Integer.toHexString(
2273 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
2274 s = IccUtils.bytesToHexString(response.payload) + s;
2275 return s;
2276 }
2277
Jake Hambye994d462014-02-03 13:10:13 -08002278 /**
2279 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2280 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2281 *
2282 * @param itemID the ID of the item to read
2283 * @return the NV item as a String, or null on error.
2284 */
2285 @Override
2286 public String nvReadItem(int itemID) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002287 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002288 if (DBG) log("nvReadItem: item " + itemID);
2289 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
2290 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
2291 return value;
2292 }
2293
2294 /**
2295 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2296 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2297 *
2298 * @param itemID the ID of the item to read
2299 * @param itemValue the value to write, as a String
2300 * @return true on success; false on any failure
2301 */
2302 @Override
2303 public boolean nvWriteItem(int itemID, String itemValue) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002304 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002305 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
2306 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
2307 new Pair<Integer, String>(itemID, itemValue));
2308 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
2309 return success;
2310 }
2311
2312 /**
2313 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
2314 * Used for device configuration by some CDMA operators.
2315 *
2316 * @param preferredRoamingList byte array containing the new PRL
2317 * @return true on success; false on any failure
2318 */
2319 @Override
2320 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002321 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002322 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
2323 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
2324 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
2325 return success;
2326 }
2327
2328 /**
2329 * Perform the specified type of NV config reset.
2330 * Used for device configuration by some CDMA operators.
2331 *
2332 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
2333 * @return true on success; false on any failure
2334 */
2335 @Override
2336 public boolean nvResetConfig(int resetType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002337 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002338 if (DBG) log("nvResetConfig: type " + resetType);
2339 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
2340 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
2341 return success;
2342 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002343
2344 /**
Wink Saville36469e72014-06-11 15:17:00 -07002345 * {@hide}
2346 * Returns Default sim, 0 in the case of single standby.
2347 */
2348 public int getDefaultSim() {
2349 //TODO Need to get it from Telephony Devcontroller
2350 return 0;
2351 }
2352
Svet Ganovb320e182015-04-16 12:30:10 -07002353 public String[] getPcscfAddress(String apnType, String callingPackage) {
2354 if (!canReadPhoneState(callingPackage, "getPcscfAddress")) {
2355 return new String[0];
2356 }
2357
2358
ram87fca6f2014-07-18 18:58:44 +05302359 return mPhone.getPcscfAddress(apnType);
Wink Saville36469e72014-06-11 15:17:00 -07002360 }
2361
2362 public void setImsRegistrationState(boolean registered) {
2363 enforceModifyPermission();
2364 mPhone.setImsRegistrationState(registered);
2365 }
2366
2367 /**
Stuart Scott54788802015-03-30 13:18:01 -07002368 * Set the network selection mode to automatic.
2369 *
2370 */
2371 @Override
2372 public void setNetworkSelectionModeAutomatic(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002373 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002374 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
2375 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
2376 }
2377
2378 /**
Shishir Agrawal302c8692015-06-19 13:49:39 -07002379 * Set the network selection mode to manual with the selected carrier.
2380 */
2381 @Override
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002382 public boolean setNetworkSelectionModeManual(int subId, OperatorInfo operator,
2383 boolean persistSelection) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002384 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002385 if (DBG) log("setNetworkSelectionModeManual: subId:" + subId + " operator:" + operator);
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002386 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operator,
2387 persistSelection);
2388 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002389 }
2390
2391 /**
2392 * Scans for available networks.
2393 */
2394 @Override
2395 public CellNetworkScanResult getCellNetworkScanResults(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002396 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002397 if (DBG) log("getCellNetworkScanResults: subId " + subId);
2398 CellNetworkScanResult result = (CellNetworkScanResult) sendRequest(
2399 CMD_PERFORM_NETWORK_SCAN, null, subId);
2400 return result;
2401 }
2402
2403 /**
Junda Liu84d15a22014-07-02 11:21:04 -07002404 * Get the calculated preferred network type.
2405 * Used for debugging incorrect network type.
2406 *
2407 * @return the preferred network type, defined in RILConstants.java.
2408 */
2409 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002410 public int getCalculatedPreferredNetworkType(String callingPackage) {
2411 if (!canReadPhoneState(callingPackage, "getCalculatedPreferredNetworkType")) {
2412 return RILConstants.PREFERRED_NETWORK_MODE;
2413 }
2414
Amit Mahajan43330e02014-11-18 11:54:45 -08002415 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext(), 0); // wink FIXME: need to get SubId from somewhere.
Junda Liu84d15a22014-07-02 11:21:04 -07002416 }
2417
2418 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08002419 * Get the preferred network type.
2420 * Used for device configuration by some CDMA operators.
2421 *
2422 * @return the preferred network type, defined in RILConstants.java.
2423 */
2424 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002425 public int getPreferredNetworkType(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002426 enforceModifyPermissionOrCarrierPrivilege(subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002427 if (DBG) log("getPreferredNetworkType");
Stuart Scott54788802015-03-30 13:18:01 -07002428 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002429 int networkType = (result != null ? result[0] : -1);
2430 if (DBG) log("getPreferredNetworkType: " + networkType);
2431 return networkType;
2432 }
2433
2434 /**
2435 * Set the preferred network type.
2436 * Used for device configuration by some CDMA operators.
2437 *
2438 * @param networkType the preferred network type, defined in RILConstants.java.
2439 * @return true on success; false on any failure.
2440 */
2441 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002442 public boolean setPreferredNetworkType(int subId, int networkType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002443 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002444 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
2445 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002446 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
Junda Liu80bc0d12014-07-14 16:36:44 -07002447 if (success) {
2448 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
Stuart Scott54788802015-03-30 13:18:01 -07002449 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
Junda Liu80bc0d12014-07-14 16:36:44 -07002450 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002451 return success;
2452 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002453
2454 /**
Junda Liu475951f2014-11-07 16:45:03 -08002455 * Check TETHER_DUN_REQUIRED and TETHER_DUN_APN settings, net.tethering.noprovisioning
2456 * SystemProperty, and config_tether_apndata to decide whether DUN APN is required for
2457 * tethering.
2458 *
2459 * @return 0: Not required. 1: required. 2: Not set.
2460 * @hide
2461 */
2462 @Override
2463 public int getTetherApnRequired() {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002464 enforceModifyPermission();
Junda Liu475951f2014-11-07 16:45:03 -08002465 int dunRequired = Settings.Global.getInt(mPhone.getContext().getContentResolver(),
2466 Settings.Global.TETHER_DUN_REQUIRED, 2);
2467 // If not set, check net.tethering.noprovisioning, TETHER_DUN_APN setting and
2468 // config_tether_apndata.
2469 if (dunRequired == 2 && mPhone.hasMatchedTetherApnSetting()) {
2470 dunRequired = 1;
2471 }
2472 return dunRequired;
2473 }
2474
2475 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07002476 * Set mobile data enabled
2477 * Used by the user through settings etc to turn on/off mobile data
2478 *
2479 * @param enable {@code true} turn turn data on, else {@code false}
2480 */
2481 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002482 public void setDataEnabled(int subId, boolean enable) {
Robert Greenwalted86e582014-05-21 20:03:20 -07002483 enforceModifyPermission();
Wink Savillee7353bb2014-12-05 14:21:41 -08002484 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002485 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002486 Phone phone = PhoneFactory.getPhone(phoneId);
2487 if (phone != null) {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002488 if (DBG) log("setDataEnabled: subId=" + subId + " enable=" + enable);
Wink Savillee7353bb2014-12-05 14:21:41 -08002489 phone.setDataEnabled(enable);
2490 } else {
2491 loge("setDataEnabled: no phone for subId=" + subId);
2492 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002493 }
2494
2495 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07002496 * Get whether mobile data is enabled.
2497 *
2498 * Note that this used to be available from ConnectivityService, gated by
2499 * ACCESS_NETWORK_STATE permission, so this will accept either that or
2500 * our MODIFY_PHONE_STATE.
Robert Greenwalted86e582014-05-21 20:03:20 -07002501 *
2502 * @return {@code true} if data is enabled else {@code false}
2503 */
2504 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002505 public boolean getDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07002506 try {
2507 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
2508 null);
2509 } catch (Exception e) {
2510 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE,
2511 null);
2512 }
Wink Savillee7353bb2014-12-05 14:21:41 -08002513 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002514 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002515 Phone phone = PhoneFactory.getPhone(phoneId);
2516 if (phone != null) {
2517 boolean retVal = phone.getDataEnabled();
Joe Onoratoa601dd22016-02-23 13:03:53 -08002518 if (DBG) log("getDataEnabled: subId=" + subId + " retVal=" + retVal);
Wink Savillee7353bb2014-12-05 14:21:41 -08002519 return retVal;
2520 } else {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002521 if (DBG) loge("getDataEnabled: no phone subId=" + subId + " retVal=false");
Wink Savillee7353bb2014-12-05 14:21:41 -08002522 return false;
2523 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002524 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002525
2526 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002527 public int getCarrierPrivilegeStatus(int subId) {
2528 final Phone phone = getPhone(subId);
2529 if (phone == null) {
2530 loge("getCarrierPrivilegeStatus: Invalid subId");
2531 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
2532 }
2533 UiccCard card = UiccController.getInstance().getUiccCard(phone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002534 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002535 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002536 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2537 }
2538 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002539 phone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002540 }
Junda Liu29340342014-07-10 15:23:27 -07002541
2542 @Override
Zach Johnson50ecba32015-05-19 00:24:21 -07002543 public int checkCarrierPrivilegesForPackage(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002544 if (TextUtils.isEmpty(pkgName))
2545 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Shishir Agrawal21409252015-01-15 23:33:50 -08002546 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002547 if (card == null) {
2548 loge("checkCarrierPrivilegesForPackage: No UICC");
2549 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2550 }
Zach Johnson50ecba32015-05-19 00:24:21 -07002551 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgName);
2552 }
2553
2554 @Override
2555 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002556 if (TextUtils.isEmpty(pkgName))
2557 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Zach Johnson50ecba32015-05-19 00:24:21 -07002558 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2559 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2560 UiccCard card = UiccController.getInstance().getUiccCard(i);
2561 if (card == null) {
Jonathan Basseri7d320df2015-06-16 12:17:08 -07002562 // No UICC in that slot.
Zach Johnson50ecba32015-05-19 00:24:21 -07002563 continue;
2564 }
2565
2566 result = card.getCarrierPrivilegeStatus(
2567 mPhone.getContext().getPackageManager(), pkgName);
2568 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2569 break;
2570 }
2571 }
2572
2573 return result;
Junda Liu29340342014-07-10 15:23:27 -07002574 }
Derek Tan89e89d42014-07-08 17:00:10 -07002575
2576 @Override
Junda Liue64de782015-04-16 17:19:16 -07002577 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
2578 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
2579 loge("phoneId " + phoneId + " is not valid.");
2580 return null;
2581 }
2582 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002583 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002584 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002585 return null ;
2586 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002587 return card.getCarrierPackageNamesForIntent(
Svetoslav483aff72015-04-21 14:16:07 -07002588 mPhone.getContext().getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002589 }
2590
Amith Yamasani6e118872016-02-19 12:53:51 -08002591 @Override
2592 public List<String> getPackagesWithCarrierPrivileges() {
2593 PackageManager pm = mPhone.getContext().getPackageManager();
2594 List<String> privilegedPackages = new ArrayList<>();
2595 List<PackageInfo> packages = null;
2596 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2597 UiccCard card = UiccController.getInstance().getUiccCard(i);
2598 if (card == null) {
2599 // No UICC in that slot.
2600 continue;
2601 }
2602 if (card.hasCarrierPrivilegeRules()) {
2603 if (packages == null) {
2604 // Only check packages in user 0 for now
2605 packages = pm.getInstalledPackagesAsUser(
2606 PackageManager.MATCH_DISABLED_COMPONENTS
2607 | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS
2608 | PackageManager.GET_SIGNATURES, UserHandle.USER_SYSTEM);
2609 }
2610 for (int p = packages.size() - 1; p >= 0; p--) {
2611 PackageInfo pkgInfo = packages.get(p);
2612 if (pkgInfo != null && pkgInfo.packageName != null
2613 && card.getCarrierPrivilegeStatus(pkgInfo)
2614 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2615 privilegedPackages.add(pkgInfo.packageName);
2616 }
2617 }
2618 }
2619 }
2620 return privilegedPackages;
2621 }
2622
Wink Savilleb564aae2014-10-23 10:18:09 -07002623 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002624 final Phone phone = getPhone(subId);
2625 UiccCard card = phone == null ? null : phone.getUiccCard();
Derek Tan97ebb422014-09-05 16:55:38 -07002626 if (card == null) {
2627 loge("getIccId: No UICC");
2628 return null;
2629 }
2630 String iccId = card.getIccId();
2631 if (TextUtils.isEmpty(iccId)) {
2632 loge("getIccId: ICC ID is null or empty.");
2633 return null;
2634 }
2635 return iccId;
2636 }
2637
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002638 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002639 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
2640 String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002641 enforceCarrierPrivilege(subId);
Derek Tan97ebb422014-09-05 16:55:38 -07002642
Jeff Sharkey85190e62014-12-05 09:40:12 -08002643 final String iccId = getIccId(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07002644 final Phone phone = getPhone(subId);
2645 if (phone == null) {
2646 return false;
2647 }
2648 final String subscriberId = phone.getSubscriberId();
Jeff Sharkey85190e62014-12-05 09:40:12 -08002649
2650 if (DBG_MERGE) {
2651 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
2652 + subscriberId + " to " + number);
2653 }
2654
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002655 if (TextUtils.isEmpty(iccId)) {
2656 return false;
Derek Tan97ebb422014-09-05 16:55:38 -07002657 }
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002658
Jeff Sharkey85190e62014-12-05 09:40:12 -08002659 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2660
2661 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002662 if (alphaTag == null) {
2663 editor.remove(alphaTagPrefKey);
2664 } else {
2665 editor.putString(alphaTagPrefKey, alphaTag);
2666 }
2667
Jeff Sharkey85190e62014-12-05 09:40:12 -08002668 // Record both the line number and IMSI for this ICCID, since we need to
2669 // track all merged IMSIs based on line number
2670 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2671 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002672 if (number == null) {
2673 editor.remove(numberPrefKey);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002674 editor.remove(subscriberPrefKey);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002675 } else {
2676 editor.putString(numberPrefKey, number);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002677 editor.putString(subscriberPrefKey, subscriberId);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002678 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002679
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002680 editor.commit();
2681 return true;
Derek Tan7226c842014-07-02 17:42:23 -07002682 }
2683
2684 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002685 public String getLine1NumberForDisplay(int subId, String callingPackage) {
Makoto Onukifee69342015-06-29 14:44:50 -07002686 // This is open to apps with WRITE_SMS.
2687 if (!canReadPhoneNumber(callingPackage, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08002688 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07002689 return null;
2690 }
Derek Tan97ebb422014-09-05 16:55:38 -07002691
2692 String iccId = getIccId(subId);
2693 if (iccId != null) {
2694 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
Amit Mahajan9cf11512015-11-09 11:40:48 -08002695 if (DBG_MERGE) {
2696 log("getLine1NumberForDisplay returning " +
2697 mTelephonySharedPreferences.getString(numberPrefKey, null));
2698 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002699 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002700 }
Amit Mahajan9cf11512015-11-09 11:40:48 -08002701 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
Derek Tan97ebb422014-09-05 16:55:38 -07002702 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002703 }
2704
2705 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002706 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
2707 if (!canReadPhoneState(callingPackage, "getLine1AlphaTagForDisplay")) {
2708 return null;
2709 }
Derek Tan97ebb422014-09-05 16:55:38 -07002710
2711 String iccId = getIccId(subId);
2712 if (iccId != null) {
2713 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002714 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002715 }
Derek Tan97ebb422014-09-05 16:55:38 -07002716 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002717 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002718
2719 @Override
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002720 public String[] getMergedSubscriberIds(String callingPackage) {
2721 if (!canReadPhoneState(callingPackage, "getMergedSubscriberIds")) {
2722 return null;
2723 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002724 final Context context = mPhone.getContext();
2725 final TelephonyManager tele = TelephonyManager.from(context);
2726 final SubscriptionManager sub = SubscriptionManager.from(context);
2727
2728 // Figure out what subscribers are currently active
2729 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002730 // Clear calling identity, when calling TelephonyManager, because callerUid must be
2731 // the process, where TelephonyManager was instantiated. Otherwise AppOps check will fail.
2732 final long identity = Binder.clearCallingIdentity();
2733 try {
2734 final int[] subIds = sub.getActiveSubscriptionIdList();
2735 for (int subId : subIds) {
2736 activeSubscriberIds.add(tele.getSubscriberId(subId));
2737 }
2738 } finally {
2739 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002740 }
2741
2742 // First pass, find a number override for an active subscriber
2743 String mergeNumber = null;
2744 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
2745 for (String key : prefs.keySet()) {
2746 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
2747 final String subscriberId = (String) prefs.get(key);
2748 if (activeSubscriberIds.contains(subscriberId)) {
2749 final String iccId = key.substring(PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
2750 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2751 mergeNumber = (String) prefs.get(numberKey);
2752 if (DBG_MERGE) {
2753 Slog.d(LOG_TAG, "Found line number " + mergeNumber
2754 + " for active subscriber " + subscriberId);
2755 }
2756 if (!TextUtils.isEmpty(mergeNumber)) {
2757 break;
2758 }
2759 }
2760 }
2761 }
2762
2763 // Shortcut when no active merged subscribers
2764 if (TextUtils.isEmpty(mergeNumber)) {
2765 return null;
2766 }
2767
2768 // Second pass, find all subscribers under that line override
2769 final ArraySet<String> result = new ArraySet<>();
2770 for (String key : prefs.keySet()) {
2771 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
2772 final String number = (String) prefs.get(key);
2773 if (mergeNumber.equals(number)) {
2774 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
2775 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
2776 final String subscriberId = (String) prefs.get(subscriberKey);
2777 if (!TextUtils.isEmpty(subscriberId)) {
2778 result.add(subscriberId);
2779 }
2780 }
2781 }
2782 }
2783
2784 final String[] resultArray = result.toArray(new String[result.size()]);
2785 Arrays.sort(resultArray);
2786 if (DBG_MERGE) {
2787 Slog.d(LOG_TAG, "Found subscribers " + Arrays.toString(resultArray) + " after merge");
2788 }
2789 return resultArray;
2790 }
2791
2792 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002793 public boolean setOperatorBrandOverride(int subId, String brand) {
2794 enforceCarrierPrivilege(subId);
2795 final Phone phone = getPhone(subId);
2796 return phone == null ? false : phone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002797 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05002798
2799 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002800 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002801 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
2802 List<String> cdmaNonRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002803 enforceCarrierPrivilege(subId);
2804 final Phone phone = getPhone(subId);
2805 if (phone == null) {
2806 return false;
2807 }
2808 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002809 cdmaNonRoamingList);
2810 }
2811
2812 @Override
Steven Liu4bf01bc2014-07-17 11:05:29 -05002813 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
2814 enforceModifyPermission();
2815
2816 int returnValue = 0;
2817 try {
2818 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
2819 if(result.exception == null) {
2820 if (result.result != null) {
2821 byte[] responseData = (byte[])(result.result);
2822 if(responseData.length > oemResp.length) {
2823 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
2824 responseData.length + "bytes. Buffer Size is " +
2825 oemResp.length + "bytes.");
2826 }
2827 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
2828 returnValue = responseData.length;
2829 }
2830 } else {
2831 CommandException ex = (CommandException) result.exception;
2832 returnValue = ex.getCommandError().ordinal();
2833 if(returnValue > 0) returnValue *= -1;
2834 }
2835 } catch (RuntimeException e) {
2836 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
2837 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
2838 if(returnValue > 0) returnValue *= -1;
2839 }
2840
2841 return returnValue;
2842 }
Wink Saville5d475dd2014-10-17 15:00:58 -07002843
2844 @Override
2845 public void setRadioCapability(RadioAccessFamily[] rafs) {
2846 try {
2847 ProxyController.getInstance().setRadioCapability(rafs);
2848 } catch (RuntimeException e) {
2849 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
2850 }
2851 }
2852
2853 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002854 public int getRadioAccessFamily(int phoneId, String callingPackage) {
2855 if (!canReadPhoneState(callingPackage, "getRadioAccessFamily")) {
2856 return RadioAccessFamily.RAF_UNKNOWN;
2857 }
2858
Wink Saville5d475dd2014-10-17 15:00:58 -07002859 return ProxyController.getInstance().getRadioAccessFamily(phoneId);
2860 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002861
2862 @Override
2863 public void enableVideoCalling(boolean enable) {
2864 enforceModifyPermission();
Tyler Gunnfdd69de2015-12-04 21:24:38 -08002865 ImsManager.setVtSetting(mPhone.getContext(), enable);
Andrew Leedf14ead2014-10-17 14:22:52 -07002866 }
2867
2868 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002869 public boolean isVideoCallingEnabled(String callingPackage) {
2870 if (!canReadPhoneState(callingPackage, "isVideoCallingEnabled")) {
2871 return false;
2872 }
2873
Andrew Lee77527ac2014-10-21 16:57:39 -07002874 // Check the user preference and the system-level IMS setting. Even if the user has
2875 // enabled video calling, if IMS is disabled we aren't able to support video calling.
2876 // In the long run, we may instead need to check if there exists a connection service
2877 // which can support video calling.
Andrew Lee312e8172014-10-23 17:01:36 -07002878 return ImsManager.isVtEnabledByPlatform(mPhone.getContext())
2879 && ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
Tyler Gunnfdd69de2015-12-04 21:24:38 -08002880 && ImsManager.isVtEnabledByUser(mPhone.getContext());
Andrew Leedf14ead2014-10-17 14:22:52 -07002881 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002882
Andrew Leea1239f22015-03-02 17:44:07 -08002883 @Override
2884 public boolean canChangeDtmfToneLength() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07002885 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08002886 }
2887
2888 @Override
2889 public boolean isWorldPhone() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07002890 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08002891 }
2892
Andrew Lee9431b832015-03-09 18:46:45 -07002893 @Override
2894 public boolean isTtyModeSupported() {
2895 TelecomManager telecomManager = TelecomManager.from(mPhone.getContext());
2896 TelephonyManager telephonyManager =
2897 (TelephonyManager) mPhone.getContext().getSystemService(Context.TELEPHONY_SERVICE);
Wooki Wu1f82f7a2016-02-15 15:59:58 +08002898 return telecomManager.isTtySupported();
Andrew Lee9431b832015-03-09 18:46:45 -07002899 }
2900
2901 @Override
2902 public boolean isHearingAidCompatibilitySupported() {
2903 return mPhone.getContext().getResources().getBoolean(R.bool.hac_enabled);
2904 }
2905
Sanket Padawe7310cc72015-01-14 09:53:20 -08002906 /**
2907 * Returns the unique device ID of phone, for example, the IMEI for
2908 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
2909 *
2910 * <p>Requires Permission:
2911 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
2912 */
2913 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002914 public String getDeviceId(String callingPackage) {
2915 if (!canReadPhoneState(callingPackage, "getDeviceId")) {
2916 return null;
2917 }
2918
Sanket Padawe7310cc72015-01-14 09:53:20 -08002919 final Phone phone = PhoneFactory.getPhone(0);
2920 if (phone != null) {
2921 return phone.getDeviceId();
2922 } else {
2923 return null;
2924 }
2925 }
2926
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002927 /*
2928 * {@hide}
2929 * Returns the IMS Registration Status
2930 */
Santos Cordon7a1885b2015-02-03 11:15:19 -08002931 @Override
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002932 public boolean isImsRegistered() {
2933 return mPhone.isImsRegistered();
2934 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08002935
2936 @Override
2937 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
2938 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
2939 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07002940
Nathan Haroldc55097a2015-03-11 18:14:50 -07002941 /*
2942 * {@hide}
2943 * Returns the IMS Registration Status
2944 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002945 public boolean isWifiCallingAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07002946 return mPhone.isWifiCallingEnabled();
2947 }
2948
2949 /*
2950 * {@hide}
2951 * Returns the IMS Registration Status
2952 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002953 public boolean isVolteAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07002954 return mPhone.isVolteEnabled();
2955 }
Svet Ganovb320e182015-04-16 12:30:10 -07002956
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002957 /*
2958 * {@hide} Returns the IMS Registration Status
2959 */
2960 public boolean isVideoTelephonyAvailable() {
2961 return mPhone.isVideoEnabled();
2962 }
2963
Svet Ganovb320e182015-04-16 12:30:10 -07002964 private boolean canReadPhoneState(String callingPackage, String message) {
Etan Cohen921655c2015-06-24 13:54:50 -07002965 try {
Amit Mahajan83ea23b2015-07-30 16:05:11 -07002966 mApp.enforceCallingOrSelfPermission(
Etan Cohen921655c2015-06-24 13:54:50 -07002967 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE, message);
2968
Amit Mahajan83ea23b2015-07-30 16:05:11 -07002969 // SKIP checking for run-time permission since caller or self has PRIVILEDGED permission
Etan Cohen921655c2015-06-24 13:54:50 -07002970 return true;
Amit Mahajan785783f2015-06-29 10:36:50 -07002971 } catch (SecurityException e) {
2972 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
2973 message);
Etan Cohen921655c2015-06-24 13:54:50 -07002974 }
Svet Ganovb320e182015-04-16 12:30:10 -07002975
2976 if (mAppOps.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(),
2977 callingPackage) != AppOpsManager.MODE_ALLOWED) {
2978 return false;
2979 }
2980
2981 return true;
2982 }
Stuart Scott8eef64f2015-04-08 15:13:54 -07002983
Makoto Onukifee69342015-06-29 14:44:50 -07002984 /**
Amit Mahajanb9b49782015-09-15 18:16:32 -07002985 * Besides READ_PHONE_STATE, WRITE_SMS and READ_SMS also allow apps to get phone numbers.
Makoto Onukifee69342015-06-29 14:44:50 -07002986 */
2987 private boolean canReadPhoneNumber(String callingPackage, String message) {
Makoto Onukie4072d12015-08-03 15:12:23 -07002988 // Default SMS app can always read it.
2989 if (mAppOps.noteOp(AppOpsManager.OP_WRITE_SMS,
2990 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED) {
2991 return true;
2992 }
2993 try {
2994 return canReadPhoneState(callingPackage, message);
Amit Mahajanb9b49782015-09-15 18:16:32 -07002995 } catch (SecurityException readPhoneStateSecurityException) {
2996 try {
2997 // Can be read with READ_SMS too.
2998 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_SMS, message);
2999 return mAppOps.noteOp(AppOpsManager.OP_READ_SMS,
3000 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED;
3001 } catch (SecurityException readSmsSecurityException) {
3002 // Throw exception with message including both READ_PHONE_STATE and READ_SMS
3003 // permissions
3004 throw new SecurityException(message + ": Neither user " + Binder.getCallingUid() +
3005 " nor current process has " + android.Manifest.permission.READ_PHONE_STATE +
3006 " or " + android.Manifest.permission.READ_SMS + ".");
3007 }
Makoto Onukie4072d12015-08-03 15:12:23 -07003008 }
Makoto Onukifee69342015-06-29 14:44:50 -07003009 }
3010
Stuart Scott8eef64f2015-04-08 15:13:54 -07003011 @Override
3012 public void factoryReset(int subId) {
3013 enforceConnectivityInternalPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07003014 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
3015 return;
3016 }
3017
Svet Ganovcc087f82015-05-12 20:35:54 -07003018 final long identity = Binder.clearCallingIdentity();
3019 try {
Stuart Scott981d8582015-04-21 14:09:50 -07003020 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
3021 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Svet Ganovcc087f82015-05-12 20:35:54 -07003022 // Enable data
3023 setDataEnabled(subId, true);
3024 // Set network selection mode to automatic
3025 setNetworkSelectionModeAutomatic(subId);
3026 // Set preferred mobile network type to the best available
3027 setPreferredNetworkType(subId, Phone.PREFERRED_NT_MODE);
3028 // Turn off roaming
3029 SubscriptionManager.from(mApp).setDataRoaming(0, subId);
3030 }
3031 } finally {
3032 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07003033 }
3034 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003035
3036 @Override
3037 public String getLocaleFromDefaultSim() {
3038 // We query all subscriptions instead of just the active ones, because
3039 // this might be called early on in the provisioning flow when the
3040 // subscriptions potentially aren't active yet.
3041 final List<SubscriptionInfo> slist = getAllSubscriptionInfoList();
3042 if (slist == null || slist.isEmpty()) {
3043 return null;
3044 }
3045
3046 // This function may be called very early, say, from the setup wizard, at
3047 // which point we won't have a default subscription set. If that's the case
3048 // we just choose the first, which will be valid in "most cases".
3049 final int defaultSubId = getDefaultSubscription();
3050 SubscriptionInfo info = null;
3051 if (defaultSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
3052 info = slist.get(0);
3053 } else {
3054 for (SubscriptionInfo item : slist) {
3055 if (item.getSubscriptionId() == defaultSubId) {
3056 info = item;
3057 break;
3058 }
3059 }
3060
3061 if (info == null) {
3062 return null;
3063 }
3064 }
3065
3066 // Try and fetch the locale from the carrier properties or from the SIM language
3067 // preferences (EF-PL and EF-LI)...
Tony Hill183b2de2015-06-24 14:53:58 +01003068 final int mcc = info.getMcc();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003069 final Phone defaultPhone = getPhone(info.getSubscriptionId());
Narayan Kamath011676f2015-07-29 12:04:08 +01003070 String simLanguage = null;
Narayan Kamath1c496c22015-04-16 14:40:19 +01003071 if (defaultPhone != null) {
3072 final Locale localeFromDefaultSim = defaultPhone.getLocaleFromSimAndCarrierPrefs();
3073 if (localeFromDefaultSim != null) {
Narayan Kamath011676f2015-07-29 12:04:08 +01003074 if (!localeFromDefaultSim.getCountry().isEmpty()) {
Tony Hill183b2de2015-06-24 14:53:58 +01003075 if (DBG) log("Using locale from default SIM:" + localeFromDefaultSim);
3076 return localeFromDefaultSim.toLanguageTag();
Narayan Kamath011676f2015-07-29 12:04:08 +01003077 } else {
3078 simLanguage = localeFromDefaultSim.getLanguage();
Tony Hill183b2de2015-06-24 14:53:58 +01003079 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003080 }
3081 }
3082
Narayan Kamath011676f2015-07-29 12:04:08 +01003083 // The SIM language preferences only store a language (e.g. fr = French), not an
3084 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
3085 // the SIM and carrier preferences does not include a country we add the country
3086 // determined from the SIM MCC to provide an exact locale.
3087 final Locale mccLocale = MccTable.getLocaleFromMcc(mPhone.getContext(), mcc, simLanguage);
Tony Hill183b2de2015-06-24 14:53:58 +01003088 if (mccLocale != null) {
3089 if (DBG) log("No locale from default SIM, using mcc locale:" + mccLocale);
3090 return mccLocale.toLanguageTag();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003091 }
3092
Tony Hill183b2de2015-06-24 14:53:58 +01003093 if (DBG) log("No locale found - returning null");
Narayan Kamath1c496c22015-04-16 14:40:19 +01003094 return null;
3095 }
3096
3097 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
3098 final long identity = Binder.clearCallingIdentity();
3099 try {
3100 return mSubscriptionController.getAllSubInfoList(
3101 mPhone.getContext().getOpPackageName());
3102 } finally {
3103 Binder.restoreCallingIdentity(identity);
3104 }
3105 }
3106
3107 private List<SubscriptionInfo> getActiveSubscriptionInfoList() {
3108 final long identity = Binder.clearCallingIdentity();
3109 try {
3110 return mSubscriptionController.getActiveSubscriptionInfoList(
3111 mPhone.getContext().getOpPackageName());
3112 } finally {
3113 Binder.restoreCallingIdentity(identity);
3114 }
3115 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003116
3117 /**
Adam Lesinski903a54c2016-04-11 14:49:52 -07003118 * Responds to the ResultReceiver with the {@link android.telephony.ModemActivityInfo} object
3119 * representing the state of the modem.
3120 *
3121 * NOTE: This clears the modem state, so there should only every be one caller.
3122 * @hide
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003123 */
3124 @Override
Adam Lesinski903a54c2016-04-11 14:49:52 -07003125 public void requestModemActivityInfo(ResultReceiver result) {
3126 enforceModifyPermission();
3127
3128 ModemActivityInfo info = (ModemActivityInfo) sendRequest(CMD_GET_MODEM_ACTIVITY_INFO, null);
3129 Bundle bundle = new Bundle();
3130 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY, info);
3131 result.send(0, bundle);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003132 }
Jack Yu85bd38a2015-11-09 11:34:32 -08003133
3134 /**
3135 * {@hide}
3136 * Returns the service state information on specified subscription.
3137 */
3138 @Override
3139 public ServiceState getServiceStateForSubscriber(int subId, String callingPackage) {
3140
3141 if (!canReadPhoneState(callingPackage, "getServiceStateForSubscriber")) {
3142 return null;
3143 }
3144
3145 final Phone phone = getPhone(subId);
3146 if (phone == null) {
3147 return null;
3148 }
3149
3150 return phone.getServiceState();
3151 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08003152
3153 /**
3154 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
3155 *
3156 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3157 * voicemail ringtone.
3158 * @return The URI for the ringtone to play when receiving a voicemail from a specific
3159 * PhoneAccount.
3160 */
3161 @Override
3162 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
3163 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
3164 if (phone == null) {
3165 return null;
3166 }
3167
3168 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone);
3169 }
3170
3171 /**
3172 * Returns whether vibration is set for voicemail notification in Phone settings.
3173 *
3174 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3175 * voicemail vibration setting.
3176 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
3177 */
3178 @Override
3179 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
3180 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
3181 if (phone == null) {
3182 return false;
3183 }
3184
3185 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone);
3186 }
3187
Youhan Wange64578a2016-05-02 15:32:42 -07003188 /**
3189 * Make sure either called from same process as self (phone) or IPC caller has read privilege.
3190 *
3191 * @throws SecurityException if the caller does not have the required permission
3192 */
3193 private void enforceReadPrivilegedPermission() {
3194 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
3195 null);
3196 }
3197
3198 /**
3199 * Return the application ID for the app type.
3200 *
3201 * @param subId the subscription ID that this request applies to.
3202 * @param appType the uicc app type.
3203 * @return Application ID for specificied app type, or null if no uicc.
3204 */
3205 @Override
3206 public String getAidForAppType(int subId, int appType) {
3207 enforceReadPrivilegedPermission();
3208 Phone phone = getPhone(subId);
3209 if (phone == null) {
3210 return null;
3211 }
3212 String aid = null;
3213 try {
3214 aid = UiccController.getInstance().getUiccCard(phone.getPhoneId())
3215 .getApplicationByType(appType).getAid();
3216 } catch (Exception e) {
3217 Log.e(LOG_TAG, "Not getting aid. Exception ex=" + e);
3218 }
3219 return aid;
3220 }
3221
Youhan Wang4001d252016-05-11 10:29:41 -07003222 /**
3223 * Return the Electronic Serial Number.
3224 *
3225 * @param subId the subscription ID that this request applies to.
3226 * @return ESN or null if error.
3227 */
3228 @Override
3229 public String getEsn(int subId) {
3230 enforceReadPrivilegedPermission();
3231 Phone phone = getPhone(subId);
3232 if (phone == null) {
3233 return null;
3234 }
3235 String esn = null;
3236 try {
3237 esn = phone.getEsn();
3238 } catch (Exception e) {
3239 Log.e(LOG_TAG, "Not getting ESN. Exception ex=" + e);
3240 }
3241 return esn;
3242 }
3243
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003244 /**
Youhan Wang66ad5d72016-07-18 17:56:58 -07003245 * Return the Preferred Roaming List Version.
3246 *
3247 * @param subId the subscription ID that this request applies to.
3248 * @return PRLVersion or null if error.
3249 */
3250 @Override
3251 public String getCdmaPrlVersion(int subId) {
3252 enforceReadPrivilegedPermission();
3253 Phone phone = getPhone(subId);
3254 if (phone == null) {
3255 return null;
3256 }
3257 String cdmaPrlVersion = null;
3258 try {
3259 cdmaPrlVersion = phone.getCdmaPrlVersion();
3260 } catch (Exception e) {
3261 Log.e(LOG_TAG, "Not getting PRLVersion", e);
3262 }
3263 return cdmaPrlVersion;
3264 }
3265
3266 /**
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003267 * Get snapshot of Telephony histograms
3268 * @return List of Telephony histograms
3269 * @hide
3270 */
3271 @Override
3272 public List<TelephonyHistogram> getTelephonyHistograms() {
3273 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
3274 return RIL.getTelephonyRILTimingHistograms();
3275 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07003276
3277 /**
3278 * {@hide}
3279 * Set the allowed carrier list for slotId
3280 * Require system privileges. In the future we may add this to carrier APIs.
3281 *
3282 * @return The number of carriers set successfully, should match length of carriers
3283 */
3284 @Override
3285 public int setAllowedCarriers(int slotId, List<CarrierIdentifier> carriers) {
3286 enforceModifyPermission();
3287 int subId = SubscriptionManager.getSubId(slotId)[0];
3288 int[] retVal = (int[]) sendRequest(CMD_SET_ALLOWED_CARRIERS, carriers, subId);
3289 return retVal[0];
3290 }
3291
3292 /**
3293 * {@hide}
3294 * Get the allowed carrier list for slotId.
3295 * Require system privileges. In the future we may add this to carrier APIs.
3296 *
3297 * @return List of {@link android.service.telephony.CarrierIdentifier}; empty list
3298 * means all carriers are allowed.
3299 */
3300 @Override
3301 public List<CarrierIdentifier> getAllowedCarriers(int slotId) {
3302 enforceReadPrivilegedPermission();
3303 int subId = SubscriptionManager.getSubId(slotId)[0];
3304 return (List<CarrierIdentifier>) sendRequest(CMD_GET_ALLOWED_CARRIERS, null, subId);
3305 }
3306
fionaxu59545b42016-05-25 15:53:37 -07003307 /**
3308 * Action set from carrier signalling broadcast receivers to enable/disable metered apns
3309 * @param subId the subscription ID that this action applies to.
3310 * @param enabled control enable or disable metered apns.
3311 * {@hide}
3312 */
3313 @Override
3314 public void carrierActionSetMeteredApnsEnabled(int subId, boolean enabled) {
3315 enforceModifyPermission();
3316 final Phone phone = getPhone(subId);
3317 if (phone == null) {
3318 loge("carrierAction: SetMeteredApnsEnabled fails with invalid subId: " + subId);
3319 return;
3320 }
3321 try {
3322 phone.carrierActionSetMeteredApnsEnabled(enabled);
3323 } catch (Exception e) {
3324 Log.e(LOG_TAG, "carrierAction: SetMeteredApnsEnabled fails. Exception ex=" + e);
3325 }
3326 }
3327
3328 /**
3329 * Action set from carrier signalling broadcast receivers to enable/disable radio
3330 * @param subId the subscription ID that this action applies to.
3331 * @param enabled control enable or disable radio.
3332 * {@hide}
3333 */
3334 @Override
3335 public void carrierActionSetRadioEnabled(int subId, boolean enabled) {
3336 enforceModifyPermission();
3337 final Phone phone = getPhone(subId);
3338 if (phone == null) {
3339 loge("carrierAction: SetRadioEnabled fails with invalid sibId: " + subId);
3340 return;
3341 }
3342 try {
3343 phone.carrierActionSetRadioEnabled(enabled);
3344 } catch (Exception e) {
3345 Log.e(LOG_TAG, "carrierAction: SetRadioEnabled fails. Exception ex=" + e);
3346 }
3347 }
3348
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003349 /**
3350 * Called when "adb shell dumpsys phone" is invoked. Dump is also automatically invoked when a
3351 * bug report is being generated.
3352 */
3353 @Override
Ta-wei Yen99282e02016-06-21 18:19:35 -07003354 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
dcashman22b950d2016-06-27 11:39:02 -07003355 if (mPhone.getContext().checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3356 != PackageManager.PERMISSION_GRANTED) {
3357 writer.println("Permission Denial: can't dump Phone from pid="
3358 + Binder.getCallingPid()
3359 + ", uid=" + Binder.getCallingUid()
3360 + "without permission "
3361 + android.Manifest.permission.DUMP);
3362 return;
3363 }
Ta-wei Yen99282e02016-06-21 18:19:35 -07003364 DumpsysHandler.dump(mPhone.getContext(), fd, writer, args);
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003365 }
Jack Yueb89b242016-06-22 13:27:47 -07003366
3367 /**
3368 * Get aggregated video call data usage from all subscriptions since boot.
3369 * @return total data usage in bytes
3370 * {@hide}
3371 */
3372 @Override
3373 public long getVtDataUsage() {
3374 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_NETWORK_USAGE_HISTORY,
3375 null);
3376
3377 // NetworkStatsService keeps tracking the active network interface and identity. It will
3378 // record the delta with the corresponding network identity. What we need to do here is
3379 // returning total video call data usage from all subscriptions since boot.
3380
3381 // TODO: Add sub id support in the future. We'll need it when we support DSDA and
3382 // simultaneous VT calls.
3383 final Phone[] phones = PhoneFactory.getPhones();
3384 long total = 0;
3385 for (Phone phone : phones) {
3386 total += phone.getVtDataUsage();
3387 }
3388 return total;
3389 }
Jack Yu75ab2952016-07-08 14:29:33 -07003390
3391 /**
3392 * Policy control of data connection. Usually used when data limit is passed.
3393 * @param enabled True if enabling the data, otherwise disabling.
3394 * @param subId Subscription index
3395 * {@hide}
3396 */
3397 @Override
3398 public void setPolicyDataEnabled(boolean enabled, int subId) {
3399 enforceModifyPermission();
3400 Phone phone = getPhone(subId);
3401 if (phone != null) {
3402 phone.setPolicyDataEnabled(enabled);
3403 }
3404 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07003405
3406 /**
3407 * Get Client request stats
3408 * @return List of Client Request Stats
3409 * @hide
3410 */
3411 @Override
3412 public List<ClientRequestStats> getClientRequestStats(String callingPackage, int subId) {
3413 if (!canReadPhoneState(callingPackage, "getClientRequestStats")) {
3414 return null;
3415 }
3416
3417 Phone phone = getPhone(subId);
3418 if (phone != null) {
3419 return phone.getClientRequestStats();
3420 }
3421
3422 return null;
3423 }
3424
3425 private WorkSource getWorkSource(WorkSource workSource, int uid) {
3426 if (workSource != null) {
3427 return workSource;
3428 }
3429
3430 String packageName = mPhone.getContext().getPackageManager().getNameForUid(uid);
3431 workSource = new WorkSource(uid, packageName);
3432 return workSource;
3433 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003434}