blob: f002765a0403d461ad1fd764f0e73be253e965e8 [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 Sasindran22882212016-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 Sasindran22882212016-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;
Brad Ebinger76681002017-01-23 13:50:20 -080067import com.android.ims.internal.IImsServiceController;
68import com.android.ims.internal.IImsServiceFeatureListener;
Shishir Agrawal566b7612013-10-28 14:41:00 -070069import com.android.internal.telephony.CallManager;
Shishir Agrawal302c8692015-06-19 13:49:39 -070070import com.android.internal.telephony.CellNetworkScanResult;
Shishir Agrawal566b7612013-10-28 14:41:00 -070071import com.android.internal.telephony.CommandException;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070072import com.android.internal.telephony.DefaultPhoneNotifier;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070073import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -080074import com.android.internal.telephony.IccCard;
Narayan Kamath1c496c22015-04-16 14:40:19 +010075import com.android.internal.telephony.MccTable;
Shishir Agrawal302c8692015-06-19 13:49:39 -070076import com.android.internal.telephony.OperatorInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070077import com.android.internal.telephony.Phone;
Ta-wei Yen87c49842016-05-13 21:19:52 -070078import com.android.internal.telephony.PhoneConstants;
Wink Saville36469e72014-06-11 15:17:00 -070079import com.android.internal.telephony.PhoneFactory;
Wink Saville5d475dd2014-10-17 15:00:58 -070080import com.android.internal.telephony.ProxyController;
Sanket Padawe99ef1e32016-05-18 16:12:33 -070081import com.android.internal.telephony.RIL;
Svet Ganovb320e182015-04-16 12:30:10 -070082import com.android.internal.telephony.RILConstants;
Wink Savilleac1bdfd2014-11-20 23:04:44 -080083import com.android.internal.telephony.SubscriptionController;
Shishir Agrawal566b7612013-10-28 14:41:00 -070084import com.android.internal.telephony.uicc.IccIoResult;
85import com.android.internal.telephony.uicc.IccUtils;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -070086import com.android.internal.telephony.uicc.UiccCard;
Shishir Agrawal566b7612013-10-28 14:41:00 -070087import com.android.internal.telephony.uicc.UiccController;
Jake Hambye994d462014-02-03 13:10:13 -080088import com.android.internal.util.HexDump;
Ta-wei Yenb0f695b2016-08-08 17:33:11 -070089import com.android.phone.settings.VisualVoicemailSettingsUtil;
Nancy Chen31f9ba12016-01-06 11:42:12 -080090import com.android.phone.settings.VoicemailNotificationSettingsUtil;
Ta-wei Yenc236d6b2016-06-21 13:33:12 -070091import java.io.FileDescriptor;
92import java.io.PrintWriter;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070093import java.util.ArrayList;
Jeff Sharkey85190e62014-12-05 09:40:12 -080094import java.util.Arrays;
Jake Hambye994d462014-02-03 13:10:13 -080095import java.util.List;
Narayan Kamath1c496c22015-04-16 14:40:19 +010096import java.util.Locale;
Jeff Sharkey85190e62014-12-05 09:40:12 -080097import java.util.Map;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070098
99/**
100 * Implementation of the ITelephony interface.
101 */
Santos Cordon117fee72014-05-16 17:56:12 -0700102public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700103 private static final String LOG_TAG = "PhoneInterfaceManager";
104 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
105 private static final boolean DBG_LOC = false;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800106 private static final boolean DBG_MERGE = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700107
108 // Message codes used with mMainThreadHandler
109 private static final int CMD_HANDLE_PIN_MMI = 1;
110 private static final int CMD_HANDLE_NEIGHBORING_CELL = 2;
111 private static final int EVENT_NEIGHBORING_CELL_DONE = 3;
112 private static final int CMD_ANSWER_RINGING_CALL = 4;
113 private static final int CMD_END_CALL = 5; // not used yet
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700114 private static final int CMD_TRANSMIT_APDU_LOGICAL_CHANNEL = 7;
115 private static final int EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE = 8;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700116 private static final int CMD_OPEN_CHANNEL = 9;
117 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
118 private static final int CMD_CLOSE_CHANNEL = 11;
119 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -0800120 private static final int CMD_NV_READ_ITEM = 13;
121 private static final int EVENT_NV_READ_ITEM_DONE = 14;
122 private static final int CMD_NV_WRITE_ITEM = 15;
123 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
124 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
125 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
126 private static final int CMD_NV_RESET_CONFIG = 19;
127 private static final int EVENT_NV_RESET_CONFIG_DONE = 20;
Jake Hamby7c27be32014-03-03 13:25:59 -0800128 private static final int CMD_GET_PREFERRED_NETWORK_TYPE = 21;
129 private static final int EVENT_GET_PREFERRED_NETWORK_TYPE_DONE = 22;
130 private static final int CMD_SET_PREFERRED_NETWORK_TYPE = 23;
131 private static final int EVENT_SET_PREFERRED_NETWORK_TYPE_DONE = 24;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800132 private static final int CMD_SEND_ENVELOPE = 25;
133 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Derek Tan6b088ee2014-09-05 14:15:18 -0700134 private static final int CMD_INVOKE_OEM_RIL_REQUEST_RAW = 27;
135 private static final int EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE = 28;
136 private static final int CMD_TRANSMIT_APDU_BASIC_CHANNEL = 29;
137 private static final int EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE = 30;
138 private static final int CMD_EXCHANGE_SIM_IO = 31;
139 private static final int EVENT_EXCHANGE_SIM_IO_DONE = 32;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800140 private static final int CMD_SET_VOICEMAIL_NUMBER = 33;
141 private static final int EVENT_SET_VOICEMAIL_NUMBER_DONE = 34;
Stuart Scott54788802015-03-30 13:18:01 -0700142 private static final int CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC = 35;
143 private static final int EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE = 36;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700144 private static final int CMD_GET_MODEM_ACTIVITY_INFO = 37;
145 private static final int EVENT_GET_MODEM_ACTIVITY_INFO_DONE = 38;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700146 private static final int CMD_PERFORM_NETWORK_SCAN = 39;
147 private static final int EVENT_PERFORM_NETWORK_SCAN_DONE = 40;
148 private static final int CMD_SET_NETWORK_SELECTION_MODE_MANUAL = 41;
149 private static final int EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE = 42;
Meng Wang1a7c35a2016-05-05 20:56:15 -0700150 private static final int CMD_SET_ALLOWED_CARRIERS = 43;
151 private static final int EVENT_SET_ALLOWED_CARRIERS_DONE = 44;
152 private static final int CMD_GET_ALLOWED_CARRIERS = 45;
153 private static final int EVENT_GET_ALLOWED_CARRIERS_DONE = 46;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700154
155 /** The singleton instance. */
156 private static PhoneInterfaceManager sInstance;
157
Wink Saville3ab207e2014-11-20 13:07:20 -0800158 private PhoneGlobals mApp;
159 private Phone mPhone;
160 private CallManager mCM;
Stuart Scott981d8582015-04-21 14:09:50 -0700161 private UserManager mUserManager;
Wink Saville3ab207e2014-11-20 13:07:20 -0800162 private AppOpsManager mAppOps;
163 private MainThreadHandler mMainThreadHandler;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800164 private SubscriptionController mSubscriptionController;
Wink Saville3ab207e2014-11-20 13:07:20 -0800165 private SharedPreferences mTelephonySharedPreferences;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700166
Derek Tan97ebb422014-09-05 16:55:38 -0700167 private static final String PREF_CARRIERS_ALPHATAG_PREFIX = "carrier_alphtag_";
168 private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
Jeff Sharkey85190e62014-12-05 09:40:12 -0800169 private static final String PREF_CARRIERS_SUBSCRIBER_PREFIX = "carrier_subscriber_";
Derek Tan89e89d42014-07-08 17:00:10 -0700170
171 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700172 * A request object to use for transmitting data to an ICC.
173 */
174 private static final class IccAPDUArgument {
175 public int channel, cla, command, p1, p2, p3;
176 public String data;
177
178 public IccAPDUArgument(int channel, int cla, int command,
179 int p1, int p2, int p3, String data) {
180 this.channel = channel;
181 this.cla = cla;
182 this.command = command;
183 this.p1 = p1;
184 this.p2 = p2;
185 this.p3 = p3;
186 this.data = data;
187 }
188 }
189
190 /**
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700191 * A request object to use for transmitting data to an ICC.
192 */
193 private static final class ManualNetworkSelectionArgument {
194 public OperatorInfo operatorInfo;
195 public boolean persistSelection;
196
197 public ManualNetworkSelectionArgument(OperatorInfo operatorInfo, boolean persistSelection) {
198 this.operatorInfo = operatorInfo;
199 this.persistSelection = persistSelection;
200 }
201 }
202
203 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700204 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
205 * request after sending. The main thread will notify the request when it is complete.
206 */
207 private static final class MainThreadRequest {
208 /** The argument to use for the request */
209 public Object argument;
210 /** The result of the request that is run on the main thread */
211 public Object result;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800212 // The subscriber id that this request applies to. Defaults to
213 // SubscriptionManager.INVALID_SUBSCRIPTION_ID
214 public Integer subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700215
216 public MainThreadRequest(Object argument) {
217 this.argument = argument;
218 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800219
220 public MainThreadRequest(Object argument, Integer subId) {
221 this.argument = argument;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800222 if (subId != null) {
223 this.subId = subId;
224 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800225 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700226 }
227
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800228 private static final class IncomingThirdPartyCallArgs {
229 public final ComponentName component;
230 public final String callId;
231 public final String callerDisplayName;
232
233 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
234 String callerDisplayName) {
235 this.component = component;
236 this.callId = callId;
237 this.callerDisplayName = callerDisplayName;
238 }
239 }
240
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700241 /**
242 * A handler that processes messages on the main thread in the phone process. Since many
243 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
244 * inbound binder threads to the main thread in the phone process. The Binder thread
245 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
246 * on, which will be notified when the operation completes and will contain the result of the
247 * request.
248 *
249 * <p>If a MainThreadRequest object is provided in the msg.obj field,
250 * note that request.result must be set to something non-null for the calling thread to
251 * unblock.
252 */
253 private final class MainThreadHandler extends Handler {
254 @Override
255 public void handleMessage(Message msg) {
256 MainThreadRequest request;
257 Message onCompleted;
258 AsyncResult ar;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800259 UiccCard uiccCard;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700260 IccAPDUArgument iccArgument;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700261
262 switch (msg.what) {
Yorke Lee716f67e2015-06-17 15:39:16 -0700263 case CMD_HANDLE_PIN_MMI: {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700264 request = (MainThreadRequest) msg.obj;
Yorke Lee716f67e2015-06-17 15:39:16 -0700265 final Phone phone = getPhoneFromRequest(request);
266 request.result = phone != null ?
267 getPhoneFromRequest(request).handlePinMmi((String) request.argument)
268 : false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700269 // Wake up the requesting thread
270 synchronized (request) {
271 request.notifyAll();
272 }
273 break;
Yorke Lee716f67e2015-06-17 15:39:16 -0700274 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700275
276 case CMD_HANDLE_NEIGHBORING_CELL:
277 request = (MainThreadRequest) msg.obj;
278 onCompleted = obtainMessage(EVENT_NEIGHBORING_CELL_DONE,
279 request);
Sooraj Sasindran22882212016-07-18 11:57:25 -0700280 mPhone.getNeighboringCids(onCompleted, (WorkSource)request.argument);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700281 break;
282
283 case EVENT_NEIGHBORING_CELL_DONE:
284 ar = (AsyncResult) msg.obj;
285 request = (MainThreadRequest) ar.userObj;
286 if (ar.exception == null && ar.result != null) {
287 request.result = ar.result;
288 } else {
289 // create an empty list to notify the waiting thread
Jake Hambye994d462014-02-03 13:10:13 -0800290 request.result = new ArrayList<NeighboringCellInfo>(0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700291 }
292 // Wake up the requesting thread
293 synchronized (request) {
294 request.notifyAll();
295 }
296 break;
297
298 case CMD_ANSWER_RINGING_CALL:
Wink Saville08874612014-08-31 19:19:58 -0700299 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800300 int answer_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700301 answerRingingCallInternal(answer_subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700302 break;
303
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700304 case CMD_END_CALL:
305 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800306 int end_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700307 final boolean hungUp;
Anthony Leeae4e36d2015-05-21 07:17:46 -0700308 Phone phone = getPhone(end_subId);
309 if (phone == null) {
310 if (DBG) log("CMD_END_CALL: no phone for id: " + end_subId);
311 break;
312 }
313 int phoneType = phone.getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700314 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
315 // CDMA: If the user presses the Power button we treat it as
316 // ending the complete call session
Wink Saville08874612014-08-31 19:19:58 -0700317 hungUp = PhoneUtils.hangupRingingAndActive(getPhone(end_subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700318 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
319 // GSM: End the call as per the Phone state
320 hungUp = PhoneUtils.hangup(mCM);
321 } else {
322 throw new IllegalStateException("Unexpected phone type: " + phoneType);
323 }
324 if (DBG) log("CMD_END_CALL: " + (hungUp ? "hung up!" : "no call to hang up"));
325 request.result = hungUp;
326 // Wake up the requesting thread
327 synchronized (request) {
328 request.notifyAll();
329 }
330 break;
331
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700332 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700333 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700334 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800335 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700336 if (uiccCard == null) {
337 loge("iccTransmitApduLogicalChannel: No UICC");
338 request.result = new IccIoResult(0x6F, 0, (byte[])null);
339 synchronized (request) {
340 request.notifyAll();
341 }
342 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700343 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
344 request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700345 uiccCard.iccTransmitApduLogicalChannel(
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700346 iccArgument.channel, iccArgument.cla, iccArgument.command,
347 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
Shishir Agrawal566b7612013-10-28 14:41:00 -0700348 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700349 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700350 break;
351
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700352 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700353 ar = (AsyncResult) msg.obj;
354 request = (MainThreadRequest) ar.userObj;
355 if (ar.exception == null && ar.result != null) {
356 request.result = ar.result;
357 } else {
358 request.result = new IccIoResult(0x6F, 0, (byte[])null);
359 if (ar.result == null) {
360 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800361 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700362 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800363 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700364 } else {
365 loge("iccTransmitApduLogicalChannel: Unknown exception");
366 }
367 }
368 synchronized (request) {
369 request.notifyAll();
370 }
371 break;
372
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700373 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
374 request = (MainThreadRequest) msg.obj;
375 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800376 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700377 if (uiccCard == null) {
378 loge("iccTransmitApduBasicChannel: No UICC");
379 request.result = new IccIoResult(0x6F, 0, (byte[])null);
380 synchronized (request) {
381 request.notifyAll();
382 }
383 } else {
384 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
385 request);
386 uiccCard.iccTransmitApduBasicChannel(
387 iccArgument.cla, iccArgument.command, iccArgument.p1, iccArgument.p2,
388 iccArgument.p3, iccArgument.data, onCompleted);
389 }
390 break;
391
392 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
393 ar = (AsyncResult) msg.obj;
394 request = (MainThreadRequest) ar.userObj;
395 if (ar.exception == null && ar.result != null) {
396 request.result = ar.result;
397 } else {
398 request.result = new IccIoResult(0x6F, 0, (byte[])null);
399 if (ar.result == null) {
400 loge("iccTransmitApduBasicChannel: Empty response");
401 } else if (ar.exception instanceof CommandException) {
402 loge("iccTransmitApduBasicChannel: CommandException: " +
403 ar.exception);
404 } else {
405 loge("iccTransmitApduBasicChannel: Unknown exception");
406 }
407 }
408 synchronized (request) {
409 request.notifyAll();
410 }
411 break;
412
413 case CMD_EXCHANGE_SIM_IO:
414 request = (MainThreadRequest) msg.obj;
415 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800416 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700417 if (uiccCard == null) {
418 loge("iccExchangeSimIO: No UICC");
419 request.result = new IccIoResult(0x6F, 0, (byte[])null);
420 synchronized (request) {
421 request.notifyAll();
422 }
423 } else {
424 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
425 request);
426 uiccCard.iccExchangeSimIO(iccArgument.cla, /* fileID */
427 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
428 iccArgument.data, onCompleted);
429 }
430 break;
431
432 case EVENT_EXCHANGE_SIM_IO_DONE:
433 ar = (AsyncResult) msg.obj;
434 request = (MainThreadRequest) ar.userObj;
435 if (ar.exception == null && ar.result != null) {
436 request.result = ar.result;
437 } else {
438 request.result = new IccIoResult(0x6f, 0, (byte[])null);
439 }
440 synchronized (request) {
441 request.notifyAll();
442 }
443 break;
444
Derek Tan4d5e5c12014-02-04 11:54:58 -0800445 case CMD_SEND_ENVELOPE:
446 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800447 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700448 if (uiccCard == null) {
449 loge("sendEnvelopeWithStatus: No UICC");
450 request.result = new IccIoResult(0x6F, 0, (byte[])null);
451 synchronized (request) {
452 request.notifyAll();
453 }
454 } else {
455 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
456 uiccCard.sendEnvelopeWithStatus((String)request.argument, onCompleted);
457 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800458 break;
459
460 case EVENT_SEND_ENVELOPE_DONE:
461 ar = (AsyncResult) msg.obj;
462 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700463 if (ar.exception == null && ar.result != null) {
464 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800465 } else {
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700466 request.result = new IccIoResult(0x6F, 0, (byte[])null);
467 if (ar.result == null) {
468 loge("sendEnvelopeWithStatus: Empty response");
469 } else if (ar.exception instanceof CommandException) {
470 loge("sendEnvelopeWithStatus: CommandException: " +
471 ar.exception);
472 } else {
473 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
474 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800475 }
476 synchronized (request) {
477 request.notifyAll();
478 }
479 break;
480
Shishir Agrawal566b7612013-10-28 14:41:00 -0700481 case CMD_OPEN_CHANNEL:
482 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800483 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700484 if (uiccCard == null) {
485 loge("iccOpenLogicalChannel: No UICC");
Shishir Agrawalfc0492a2016-02-17 11:15:33 -0800486 request.result = new IccOpenLogicalChannelResponse(-1,
487 IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE, null);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700488 synchronized (request) {
489 request.notifyAll();
490 }
491 } else {
492 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
493 uiccCard.iccOpenLogicalChannel((String)request.argument, onCompleted);
494 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700495 break;
496
497 case EVENT_OPEN_CHANNEL_DONE:
498 ar = (AsyncResult) msg.obj;
499 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700500 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700501 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700502 int[] result = (int[]) ar.result;
503 int channelId = result[0];
504 byte[] selectResponse = null;
505 if (result.length > 1) {
506 selectResponse = new byte[result.length - 1];
507 for (int i = 1; i < result.length; ++i) {
508 selectResponse[i - 1] = (byte) result[i];
509 }
510 }
511 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700512 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700513 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700514 if (ar.result == null) {
515 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700516 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700517 if (ar.exception != null) {
518 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
519 }
520
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700521 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Junda Liua754ba12015-05-20 01:17:52 -0700522 if (ar.exception instanceof CommandException) {
523 CommandException.Error error =
524 ((CommandException) (ar.exception)).getCommandError();
525 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700526 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700527 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700528 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700529 }
530 }
531 openChannelResp = new IccOpenLogicalChannelResponse(
532 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700533 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700534 request.result = openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700535 synchronized (request) {
536 request.notifyAll();
537 }
538 break;
539
540 case CMD_CLOSE_CHANNEL:
541 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800542 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700543 if (uiccCard == null) {
544 loge("iccCloseLogicalChannel: No UICC");
545 request.result = new IccIoResult(0x6F, 0, (byte[])null);
546 synchronized (request) {
547 request.notifyAll();
548 }
549 } else {
550 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
551 uiccCard.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
552 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700553 break;
554
555 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800556 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
557 break;
558
559 case CMD_NV_READ_ITEM:
560 request = (MainThreadRequest) msg.obj;
561 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
562 mPhone.nvReadItem((Integer) request.argument, onCompleted);
563 break;
564
565 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700566 ar = (AsyncResult) msg.obj;
567 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800568 if (ar.exception == null && ar.result != null) {
569 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700570 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800571 request.result = "";
572 if (ar.result == null) {
573 loge("nvReadItem: Empty response");
574 } else if (ar.exception instanceof CommandException) {
575 loge("nvReadItem: CommandException: " +
576 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700577 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800578 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700579 }
580 }
581 synchronized (request) {
582 request.notifyAll();
583 }
584 break;
585
Jake Hambye994d462014-02-03 13:10:13 -0800586 case CMD_NV_WRITE_ITEM:
587 request = (MainThreadRequest) msg.obj;
588 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
589 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
590 mPhone.nvWriteItem(idValue.first, idValue.second, onCompleted);
591 break;
592
593 case EVENT_NV_WRITE_ITEM_DONE:
594 handleNullReturnEvent(msg, "nvWriteItem");
595 break;
596
597 case CMD_NV_WRITE_CDMA_PRL:
598 request = (MainThreadRequest) msg.obj;
599 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
600 mPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
601 break;
602
603 case EVENT_NV_WRITE_CDMA_PRL_DONE:
604 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
605 break;
606
607 case CMD_NV_RESET_CONFIG:
608 request = (MainThreadRequest) msg.obj;
609 onCompleted = obtainMessage(EVENT_NV_RESET_CONFIG_DONE, request);
610 mPhone.nvResetConfig((Integer) request.argument, onCompleted);
611 break;
612
613 case EVENT_NV_RESET_CONFIG_DONE:
614 handleNullReturnEvent(msg, "nvResetConfig");
615 break;
616
Jake Hamby7c27be32014-03-03 13:25:59 -0800617 case CMD_GET_PREFERRED_NETWORK_TYPE:
618 request = (MainThreadRequest) msg.obj;
619 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
Stuart Scott54788802015-03-30 13:18:01 -0700620 getPhoneFromRequest(request).getPreferredNetworkType(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800621 break;
622
623 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
624 ar = (AsyncResult) msg.obj;
625 request = (MainThreadRequest) ar.userObj;
626 if (ar.exception == null && ar.result != null) {
627 request.result = ar.result; // Integer
628 } else {
Sanket Padawecfc2d352016-01-05 19:52:14 -0800629 request.result = null;
Jake Hamby7c27be32014-03-03 13:25:59 -0800630 if (ar.result == null) {
631 loge("getPreferredNetworkType: Empty response");
632 } else if (ar.exception instanceof CommandException) {
633 loge("getPreferredNetworkType: CommandException: " +
634 ar.exception);
635 } else {
636 loge("getPreferredNetworkType: Unknown exception");
637 }
638 }
639 synchronized (request) {
640 request.notifyAll();
641 }
642 break;
643
644 case CMD_SET_PREFERRED_NETWORK_TYPE:
645 request = (MainThreadRequest) msg.obj;
646 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
647 int networkType = (Integer) request.argument;
Stuart Scott54788802015-03-30 13:18:01 -0700648 getPhoneFromRequest(request).setPreferredNetworkType(networkType, onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800649 break;
650
651 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
652 handleNullReturnEvent(msg, "setPreferredNetworkType");
653 break;
654
Steven Liu4bf01bc2014-07-17 11:05:29 -0500655 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
656 request = (MainThreadRequest)msg.obj;
657 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
658 mPhone.invokeOemRilRequestRaw((byte[])request.argument, onCompleted);
659 break;
660
661 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
662 ar = (AsyncResult)msg.obj;
663 request = (MainThreadRequest)ar.userObj;
664 request.result = ar;
665 synchronized (request) {
666 request.notifyAll();
667 }
668 break;
669
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800670 case CMD_SET_VOICEMAIL_NUMBER:
671 request = (MainThreadRequest) msg.obj;
672 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
673 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -0800674 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
675 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800676 break;
677
678 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
679 handleNullReturnEvent(msg, "setVoicemailNumber");
680 break;
681
Stuart Scott54788802015-03-30 13:18:01 -0700682 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
683 request = (MainThreadRequest) msg.obj;
684 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
685 request);
686 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
687 break;
688
689 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
690 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
691 break;
692
Shishir Agrawal302c8692015-06-19 13:49:39 -0700693 case CMD_PERFORM_NETWORK_SCAN:
694 request = (MainThreadRequest) msg.obj;
695 onCompleted = obtainMessage(EVENT_PERFORM_NETWORK_SCAN_DONE, request);
696 getPhoneFromRequest(request).getAvailableNetworks(onCompleted);
697 break;
698
699 case EVENT_PERFORM_NETWORK_SCAN_DONE:
700 ar = (AsyncResult) msg.obj;
701 request = (MainThreadRequest) ar.userObj;
702 CellNetworkScanResult cellScanResult;
703 if (ar.exception == null && ar.result != null) {
704 cellScanResult = new CellNetworkScanResult(
705 CellNetworkScanResult.STATUS_SUCCESS,
706 (List<OperatorInfo>) ar.result);
707 } else {
708 if (ar.result == null) {
709 loge("getCellNetworkScanResults: Empty response");
710 }
711 if (ar.exception != null) {
712 loge("getCellNetworkScanResults: Exception: " + ar.exception);
713 }
714 int errorCode = CellNetworkScanResult.STATUS_UNKNOWN_ERROR;
715 if (ar.exception instanceof CommandException) {
716 CommandException.Error error =
717 ((CommandException) (ar.exception)).getCommandError();
718 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
719 errorCode = CellNetworkScanResult.STATUS_RADIO_NOT_AVAILABLE;
720 } else if (error == CommandException.Error.GENERIC_FAILURE) {
721 errorCode = CellNetworkScanResult.STATUS_RADIO_GENERIC_FAILURE;
722 }
723 }
724 cellScanResult = new CellNetworkScanResult(errorCode, null);
725 }
726 request.result = cellScanResult;
727 synchronized (request) {
728 request.notifyAll();
729 }
730 break;
731
732 case CMD_SET_NETWORK_SELECTION_MODE_MANUAL:
733 request = (MainThreadRequest) msg.obj;
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700734 ManualNetworkSelectionArgument selArg =
735 (ManualNetworkSelectionArgument) request.argument;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700736 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE,
737 request);
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700738 getPhoneFromRequest(request).selectNetworkManually(selArg.operatorInfo,
739 selArg.persistSelection, onCompleted);
Shishir Agrawal302c8692015-06-19 13:49:39 -0700740 break;
741
742 case EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE:
743 handleNullReturnEvent(msg, "setNetworkSelectionModeManual");
744 break;
745
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700746 case CMD_GET_MODEM_ACTIVITY_INFO:
747 request = (MainThreadRequest) msg.obj;
748 onCompleted = obtainMessage(EVENT_GET_MODEM_ACTIVITY_INFO_DONE, request);
Prerepa Viswanadham61a60ad2015-06-08 18:07:51 -0700749 mPhone.getModemActivityInfo(onCompleted);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700750 break;
751
752 case EVENT_GET_MODEM_ACTIVITY_INFO_DONE:
753 ar = (AsyncResult) msg.obj;
754 request = (MainThreadRequest) ar.userObj;
755 if (ar.exception == null && ar.result != null) {
756 request.result = ar.result;
757 } else {
758 if (ar.result == null) {
759 loge("queryModemActivityInfo: Empty response");
760 } else if (ar.exception instanceof CommandException) {
761 loge("queryModemActivityInfo: CommandException: " +
762 ar.exception);
763 } else {
764 loge("queryModemActivityInfo: Unknown exception");
765 }
766 }
Amit Mahajand4766222016-01-28 15:28:28 -0800767 // Result cannot be null. Return ModemActivityInfo with all fields set to 0.
768 if (request.result == null) {
769 request.result = new ModemActivityInfo(0, 0, 0, null, 0, 0);
770 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700771 synchronized (request) {
772 request.notifyAll();
773 }
774 break;
775
Meng Wang1a7c35a2016-05-05 20:56:15 -0700776 case CMD_SET_ALLOWED_CARRIERS:
777 request = (MainThreadRequest) msg.obj;
778 onCompleted = obtainMessage(EVENT_SET_ALLOWED_CARRIERS_DONE, request);
779 mPhone.setAllowedCarriers(
780 (List<CarrierIdentifier>) request.argument,
781 onCompleted);
782 break;
783
784 case EVENT_SET_ALLOWED_CARRIERS_DONE:
785 ar = (AsyncResult) msg.obj;
786 request = (MainThreadRequest) ar.userObj;
787 if (ar.exception == null && ar.result != null) {
788 request.result = ar.result;
789 } else {
790 if (ar.result == null) {
791 loge("setAllowedCarriers: Empty response");
792 } else if (ar.exception instanceof CommandException) {
793 loge("setAllowedCarriers: CommandException: " +
794 ar.exception);
795 } else {
796 loge("setAllowedCarriers: Unknown exception");
797 }
798 }
799 // Result cannot be null. Return -1 on error.
800 if (request.result == null) {
801 request.result = new int[]{-1};
802 }
803 synchronized (request) {
804 request.notifyAll();
805 }
806 break;
807
808 case CMD_GET_ALLOWED_CARRIERS:
809 request = (MainThreadRequest) msg.obj;
810 onCompleted = obtainMessage(EVENT_GET_ALLOWED_CARRIERS_DONE, request);
811 mPhone.getAllowedCarriers(onCompleted);
812 break;
813
814 case EVENT_GET_ALLOWED_CARRIERS_DONE:
815 ar = (AsyncResult) msg.obj;
816 request = (MainThreadRequest) ar.userObj;
817 if (ar.exception == null && ar.result != null) {
818 request.result = ar.result;
819 } else {
820 if (ar.result == null) {
821 loge("getAllowedCarriers: Empty response");
822 } else if (ar.exception instanceof CommandException) {
823 loge("getAllowedCarriers: CommandException: " +
824 ar.exception);
825 } else {
826 loge("getAllowedCarriers: Unknown exception");
827 }
828 }
829 // Result cannot be null. Return empty list of CarrierIdentifier.
830 if (request.result == null) {
831 request.result = new ArrayList<CarrierIdentifier>(0);
832 }
833 synchronized (request) {
834 request.notifyAll();
835 }
836 break;
837
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700838 default:
839 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
840 break;
841 }
842 }
Jake Hambye994d462014-02-03 13:10:13 -0800843
844 private void handleNullReturnEvent(Message msg, String command) {
845 AsyncResult ar = (AsyncResult) msg.obj;
846 MainThreadRequest request = (MainThreadRequest) ar.userObj;
847 if (ar.exception == null) {
848 request.result = true;
849 } else {
850 request.result = false;
851 if (ar.exception instanceof CommandException) {
852 loge(command + ": CommandException: " + ar.exception);
853 } else {
854 loge(command + ": Unknown exception");
855 }
856 }
857 synchronized (request) {
858 request.notifyAll();
859 }
860 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700861 }
862
863 /**
864 * Posts the specified command to be executed on the main thread,
865 * waits for the request to complete, and returns the result.
866 * @see #sendRequestAsync
867 */
868 private Object sendRequest(int command, Object argument) {
Sanket Padawe56e75a32016-02-08 12:18:19 -0800869 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Wink Saville36469e72014-06-11 15:17:00 -0700870 }
871
872 /**
873 * Posts the specified command to be executed on the main thread,
874 * waits for the request to complete, and returns the result.
875 * @see #sendRequestAsync
876 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800877 private Object sendRequest(int command, Object argument, Integer subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700878 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
879 throw new RuntimeException("This method will deadlock if called from the main thread.");
880 }
881
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800882 MainThreadRequest request = new MainThreadRequest(argument, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700883 Message msg = mMainThreadHandler.obtainMessage(command, request);
884 msg.sendToTarget();
885
886 // Wait for the request to complete
887 synchronized (request) {
888 while (request.result == null) {
889 try {
890 request.wait();
891 } catch (InterruptedException e) {
892 // Do nothing, go back and wait until the request is complete
893 }
894 }
895 }
896 return request.result;
897 }
898
899 /**
900 * Asynchronous ("fire and forget") version of sendRequest():
901 * Posts the specified command to be executed on the main thread, and
902 * returns immediately.
903 * @see #sendRequest
904 */
905 private void sendRequestAsync(int command) {
906 mMainThreadHandler.sendEmptyMessage(command);
907 }
908
909 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -0700910 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
911 * @see {@link #sendRequest(int,Object)}
912 */
913 private void sendRequestAsync(int command, Object argument) {
914 MainThreadRequest request = new MainThreadRequest(argument);
915 Message msg = mMainThreadHandler.obtainMessage(command, request);
916 msg.sendToTarget();
917 }
918
919 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700920 * Initialize the singleton PhoneInterfaceManager instance.
921 * This is only done once, at startup, from PhoneApp.onCreate().
922 */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700923 /* package */ static PhoneInterfaceManager init(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700924 synchronized (PhoneInterfaceManager.class) {
925 if (sInstance == null) {
Sailesh Nepal194161e2014-07-03 08:57:44 -0700926 sInstance = new PhoneInterfaceManager(app, phone);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700927 } else {
928 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
929 }
930 return sInstance;
931 }
932 }
933
934 /** Private constructor; @see init() */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700935 private PhoneInterfaceManager(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700936 mApp = app;
937 mPhone = phone;
938 mCM = PhoneGlobals.getInstance().mCM;
Stuart Scott981d8582015-04-21 14:09:50 -0700939 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700940 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
941 mMainThreadHandler = new MainThreadHandler();
Andrew Leedf14ead2014-10-17 14:22:52 -0700942 mTelephonySharedPreferences =
Derek Tan97ebb422014-09-05 16:55:38 -0700943 PreferenceManager.getDefaultSharedPreferences(mPhone.getContext());
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800944 mSubscriptionController = SubscriptionController.getInstance();
Wink Saville3ab207e2014-11-20 13:07:20 -0800945
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700946 publish();
947 }
948
949 private void publish() {
950 if (DBG) log("publish: " + this);
951
952 ServiceManager.addService("phone", this);
953 }
954
Stuart Scott584921c2015-01-15 17:10:34 -0800955 private Phone getPhoneFromRequest(MainThreadRequest request) {
Sanket Padawe56e75a32016-02-08 12:18:19 -0800956 return (request.subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID)
957 ? mPhone : getPhone(request.subId);
Stuart Scott584921c2015-01-15 17:10:34 -0800958 }
959
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800960 private UiccCard getUiccCardFromRequest(MainThreadRequest request) {
961 Phone phone = getPhoneFromRequest(request);
962 return phone == null ? null :
963 UiccController.getInstance().getUiccCard(phone.getPhoneId());
964 }
965
Wink Saville36469e72014-06-11 15:17:00 -0700966 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -0700967 private Phone getPhone(int subId) {
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800968 return PhoneFactory.getPhone(mSubscriptionController.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -0700969 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700970 //
971 // Implementation of the ITelephony interface.
972 //
973
974 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700975 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -0700976 }
977
Wink Savilleb564aae2014-10-23 10:18:09 -0700978 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700979 if (DBG) log("dial: " + number);
980 // No permission check needed here: This is just a wrapper around the
981 // ACTION_DIAL intent, which is available to any app since it puts up
982 // the UI before it does anything.
983
984 String url = createTelUrl(number);
985 if (url == null) {
986 return;
987 }
988
989 // PENDING: should we just silently fail if phone is offhook or ringing?
Wink Saville36469e72014-06-11 15:17:00 -0700990 PhoneConstants.State state = mCM.getState(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700991 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
992 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
993 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
994 mApp.startActivity(intent);
995 }
996 }
997
998 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700999 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -07001000 }
1001
Wink Savilleb564aae2014-10-23 10:18:09 -07001002 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001003 if (DBG) log("call: " + number);
1004
1005 // This is just a wrapper around the ACTION_CALL intent, but we still
1006 // need to do a permission check since we're calling startActivity()
1007 // from the context of the phone app.
1008 enforceCallPermission();
1009
1010 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
1011 != AppOpsManager.MODE_ALLOWED) {
1012 return;
1013 }
1014
1015 String url = createTelUrl(number);
1016 if (url == null) {
1017 return;
1018 }
1019
Wink Saville08874612014-08-31 19:19:58 -07001020 boolean isValid = false;
Narayan Kamath1c496c22015-04-16 14:40:19 +01001021 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoList();
Wink Saville3ab207e2014-11-20 13:07:20 -08001022 if (slist != null) {
1023 for (SubscriptionInfo subInfoRecord : slist) {
1024 if (subInfoRecord.getSubscriptionId() == subId) {
1025 isValid = true;
1026 break;
1027 }
Wink Saville08874612014-08-31 19:19:58 -07001028 }
1029 }
1030 if (isValid == false) {
1031 return;
1032 }
1033
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001034 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
Wink Saville36469e72014-06-11 15:17:00 -07001035 intent.putExtra(SUBSCRIPTION_KEY, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001036 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1037 mApp.startActivity(intent);
1038 }
1039
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001040 /**
1041 * End a call based on call state
1042 * @return true is a call was ended
1043 */
1044 public boolean endCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001045 return endCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001046 }
1047
1048 /**
1049 * End a call based on the call state of the subId
1050 * @return true is a call was ended
1051 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001052 public boolean endCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001053 enforceCallPermission();
Stuart Scott584921c2015-01-15 17:10:34 -08001054 return (Boolean) sendRequest(CMD_END_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001055 }
1056
1057 public void answerRingingCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001058 answerRingingCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001059 }
1060
Wink Savilleb564aae2014-10-23 10:18:09 -07001061 public void answerRingingCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001062 if (DBG) log("answerRingingCall...");
1063 // TODO: there should eventually be a separate "ANSWER_PHONE" permission,
1064 // but that can probably wait till the big TelephonyManager API overhaul.
1065 // For now, protect this call with the MODIFY_PHONE_STATE permission.
1066 enforceModifyPermission();
Stuart Scott584921c2015-01-15 17:10:34 -08001067 sendRequest(CMD_ANSWER_RINGING_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001068 }
1069
1070 /**
1071 * Make the actual telephony calls to implement answerRingingCall().
1072 * This should only be called from the main thread of the Phone app.
1073 * @see #answerRingingCall
1074 *
1075 * TODO: it would be nice to return true if we answered the call, or
1076 * false if there wasn't actually a ringing incoming call, or some
1077 * other error occurred. (In other words, pass back the return value
1078 * from PhoneUtils.answerCall() or PhoneUtils.answerAndEndActive().)
1079 * But that would require calling this method via sendRequest() rather
1080 * than sendRequestAsync(), and right now we don't actually *need* that
1081 * return value, so let's just return void for now.
1082 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001083 private void answerRingingCallInternal(int subId) {
Wink Saville08874612014-08-31 19:19:58 -07001084 final boolean hasRingingCall = !getPhone(subId).getRingingCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001085 if (hasRingingCall) {
Wink Saville08874612014-08-31 19:19:58 -07001086 final boolean hasActiveCall = !getPhone(subId).getForegroundCall().isIdle();
1087 final boolean hasHoldingCall = !getPhone(subId).getBackgroundCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001088 if (hasActiveCall && hasHoldingCall) {
1089 // Both lines are in use!
1090 // TODO: provide a flag to let the caller specify what
1091 // policy to use if both lines are in use. (The current
1092 // behavior is hardwired to "answer incoming, end ongoing",
1093 // which is how the CALL button is specced to behave.)
1094 PhoneUtils.answerAndEndActive(mCM, mCM.getFirstActiveRingingCall());
1095 return;
1096 } else {
1097 // answerCall() will automatically hold the current active
1098 // call, if there is one.
1099 PhoneUtils.answerCall(mCM.getFirstActiveRingingCall());
1100 return;
1101 }
1102 } else {
1103 // No call was ringing.
1104 return;
1105 }
1106 }
1107
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001108 /**
Santos Cordon5422a8d2014-09-12 04:20:56 -07001109 * This method is no longer used and can be removed once TelephonyManager stops referring to it.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001110 */
Santos Cordon5422a8d2014-09-12 04:20:56 -07001111 public void silenceRinger() {
1112 Log.e(LOG_TAG, "silenseRinger not supported");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001113 }
1114
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001115 @Override
1116 public boolean isOffhook(String callingPackage) {
1117 return isOffhookForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001118 }
1119
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001120 @Override
1121 public boolean isOffhookForSubscriber(int subId, String callingPackage) {
1122 if (!canReadPhoneState(callingPackage, "isOffhookForSubscriber")) {
1123 return false;
1124 }
1125
Sanket Padawe356d7632015-06-22 14:03:32 -07001126 final Phone phone = getPhone(subId);
1127 if (phone != null) {
1128 return (phone.getState() == PhoneConstants.State.OFFHOOK);
1129 } else {
1130 return false;
1131 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001132 }
1133
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001134 @Override
1135 public boolean isRinging(String callingPackage) {
1136 return (isRingingForSubscriber(getDefaultSubscription(), callingPackage));
Wink Saville36469e72014-06-11 15:17:00 -07001137 }
1138
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001139 @Override
1140 public boolean isRingingForSubscriber(int subId, String callingPackage) {
1141 if (!canReadPhoneState(callingPackage, "isRingingForSubscriber")) {
1142 return false;
1143 }
1144
Sanket Padawe356d7632015-06-22 14:03:32 -07001145 final Phone phone = getPhone(subId);
1146 if (phone != null) {
1147 return (phone.getState() == PhoneConstants.State.RINGING);
1148 } else {
1149 return false;
1150 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001151 }
1152
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001153 @Override
1154 public boolean isIdle(String callingPackage) {
1155 return isIdleForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001156 }
1157
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001158 @Override
1159 public boolean isIdleForSubscriber(int subId, String callingPackage) {
1160 if (!canReadPhoneState(callingPackage, "isIdleForSubscriber")) {
1161 return false;
1162 }
1163
Sanket Padawe356d7632015-06-22 14:03:32 -07001164 final Phone phone = getPhone(subId);
1165 if (phone != null) {
1166 return (phone.getState() == PhoneConstants.State.IDLE);
1167 } else {
1168 return false;
1169 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001170 }
1171
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001172 public boolean supplyPin(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001173 return supplyPinForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001174 }
1175
Wink Savilleb564aae2014-10-23 10:18:09 -07001176 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001177 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001178 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1179 }
1180
1181 public boolean supplyPuk(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001182 return supplyPukForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001183 }
1184
Wink Savilleb564aae2014-10-23 10:18:09 -07001185 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001186 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001187 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1188 }
1189
1190 /** {@hide} */
1191 public int[] supplyPinReportResult(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001192 return supplyPinReportResultForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001193 }
1194
Wink Savilleb564aae2014-10-23 10:18:09 -07001195 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001196 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001197 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001198 checkSimPin.start();
1199 return checkSimPin.unlockSim(null, pin);
1200 }
1201
Wink Saville9de0f752013-10-22 19:04:03 -07001202 /** {@hide} */
1203 public int[] supplyPukReportResult(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001204 return supplyPukReportResultForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001205 }
1206
Wink Savilleb564aae2014-10-23 10:18:09 -07001207 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001208 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001209 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001210 checkSimPuk.start();
1211 return checkSimPuk.unlockSim(puk, pin);
1212 }
1213
1214 /**
Wink Saville9de0f752013-10-22 19:04:03 -07001215 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001216 * a synchronous one.
1217 */
1218 private static class UnlockSim extends Thread {
1219
1220 private final IccCard mSimCard;
1221
1222 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07001223 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1224 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001225
1226 // For replies from SimCard interface
1227 private Handler mHandler;
1228
1229 // For async handler to identify request type
1230 private static final int SUPPLY_PIN_COMPLETE = 100;
1231
1232 public UnlockSim(IccCard simCard) {
1233 mSimCard = simCard;
1234 }
1235
1236 @Override
1237 public void run() {
1238 Looper.prepare();
1239 synchronized (UnlockSim.this) {
1240 mHandler = new Handler() {
1241 @Override
1242 public void handleMessage(Message msg) {
1243 AsyncResult ar = (AsyncResult) msg.obj;
1244 switch (msg.what) {
1245 case SUPPLY_PIN_COMPLETE:
1246 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
1247 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07001248 mRetryCount = msg.arg1;
1249 if (ar.exception != null) {
1250 if (ar.exception instanceof CommandException &&
1251 ((CommandException)(ar.exception)).getCommandError()
1252 == CommandException.Error.PASSWORD_INCORRECT) {
1253 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
1254 } else {
1255 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1256 }
1257 } else {
1258 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
1259 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001260 mDone = true;
1261 UnlockSim.this.notifyAll();
1262 }
1263 break;
1264 }
1265 }
1266 };
1267 UnlockSim.this.notifyAll();
1268 }
1269 Looper.loop();
1270 }
1271
1272 /*
1273 * Use PIN or PUK to unlock SIM card
1274 *
1275 * If PUK is null, unlock SIM card with PIN
1276 *
1277 * If PUK is not null, unlock SIM card with PUK and set PIN code
1278 */
Wink Saville9de0f752013-10-22 19:04:03 -07001279 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001280
1281 while (mHandler == null) {
1282 try {
1283 wait();
1284 } catch (InterruptedException e) {
1285 Thread.currentThread().interrupt();
1286 }
1287 }
1288 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
1289
1290 if (puk == null) {
1291 mSimCard.supplyPin(pin, callback);
1292 } else {
1293 mSimCard.supplyPuk(puk, pin, callback);
1294 }
1295
1296 while (!mDone) {
1297 try {
1298 Log.d(LOG_TAG, "wait for done");
1299 wait();
1300 } catch (InterruptedException e) {
1301 // Restore the interrupted status
1302 Thread.currentThread().interrupt();
1303 }
1304 }
1305 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07001306 int[] resultArray = new int[2];
1307 resultArray[0] = mResult;
1308 resultArray[1] = mRetryCount;
1309 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001310 }
1311 }
1312
1313 public void updateServiceLocation() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001314 updateServiceLocationForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001315
1316 }
1317
Wink Savilleb564aae2014-10-23 10:18:09 -07001318 public void updateServiceLocationForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001319 // No permission check needed here: this call is harmless, and it's
1320 // needed for the ServiceState.requestStateUpdate() call (which is
1321 // already intentionally exposed to 3rd parties.)
Sanket Padawe356d7632015-06-22 14:03:32 -07001322 final Phone phone = getPhone(subId);
1323 if (phone != null) {
1324 phone.updateServiceLocation();
1325 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001326 }
1327
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001328 @Override
1329 public boolean isRadioOn(String callingPackage) {
1330 return isRadioOnForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001331 }
1332
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001333 @Override
1334 public boolean isRadioOnForSubscriber(int subId, String callingPackage) {
1335 if (!canReadPhoneState(callingPackage, "isRadioOnForSubscriber")) {
1336 return false;
1337 }
1338 return isRadioOnForSubscriber(subId);
1339 }
1340
1341 private boolean isRadioOnForSubscriber(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001342 final Phone phone = getPhone(subId);
1343 if (phone != null) {
1344 return phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF;
1345 } else {
1346 return false;
1347 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001348 }
1349
1350 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001351 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001352
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001353 }
Wink Saville36469e72014-06-11 15:17:00 -07001354
Wink Savilleb564aae2014-10-23 10:18:09 -07001355 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001356 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001357 final Phone phone = getPhone(subId);
1358 if (phone != null) {
1359 phone.setRadioPower(!isRadioOnForSubscriber(subId));
1360 }
Wink Saville36469e72014-06-11 15:17:00 -07001361 }
1362
1363 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001364 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001365 }
1366
Wink Savilleb564aae2014-10-23 10:18:09 -07001367 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07001368 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001369 final Phone phone = getPhone(subId);
1370 if (phone == null) {
1371 return false;
1372 }
1373 if ((phone.getServiceState().getState() !=
Wink Saville36469e72014-06-11 15:17:00 -07001374 ServiceState.STATE_POWER_OFF) != turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001375 toggleRadioOnOffForSubscriber(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001376 }
1377 return true;
1378 }
Wink Saville36469e72014-06-11 15:17:00 -07001379
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001380 public boolean needMobileRadioShutdown() {
1381 /*
1382 * If any of the Radios are available, it will need to be
1383 * shutdown. So return true if any Radio is available.
1384 */
1385 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1386 Phone phone = PhoneFactory.getPhone(i);
1387 if (phone != null && phone.isRadioAvailable()) return true;
1388 }
1389 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
1390 return false;
1391 }
1392
1393 public void shutdownMobileRadios() {
1394 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1395 logv("Shutting down Phone " + i);
1396 shutdownRadioUsingPhoneId(i);
1397 }
1398 }
1399
1400 private void shutdownRadioUsingPhoneId(int phoneId) {
1401 enforceModifyPermission();
1402 Phone phone = PhoneFactory.getPhone(phoneId);
1403 if (phone != null && phone.isRadioAvailable()) {
1404 phone.shutdownRadio();
1405 }
1406 }
1407
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001408 public boolean setRadioPower(boolean turnOn) {
Wei Liu9ae2a062016-08-08 11:09:34 -07001409 final Phone defaultPhone = PhoneFactory.getDefaultPhone();
1410 if (defaultPhone != null) {
1411 defaultPhone.setRadioPower(turnOn);
1412 return true;
1413 } else {
1414 loge("There's no default phone.");
1415 return false;
1416 }
Wink Saville36469e72014-06-11 15:17:00 -07001417 }
1418
Wink Savilleb564aae2014-10-23 10:18:09 -07001419 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001420 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001421 final Phone phone = getPhone(subId);
1422 if (phone != null) {
1423 phone.setRadioPower(turnOn);
1424 return true;
1425 } else {
1426 return false;
1427 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001428 }
1429
Wink Saville36469e72014-06-11 15:17:00 -07001430 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001431 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001432 public boolean enableDataConnectivity() {
1433 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001434 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001435 final Phone phone = getPhone(subId);
1436 if (phone != null) {
1437 phone.setDataEnabled(true);
1438 return true;
1439 } else {
1440 return false;
1441 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001442 }
1443
Wink Saville36469e72014-06-11 15:17:00 -07001444 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001445 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001446 public boolean disableDataConnectivity() {
1447 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001448 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001449 final Phone phone = getPhone(subId);
1450 if (phone != null) {
1451 phone.setDataEnabled(false);
1452 return true;
1453 } else {
1454 return false;
1455 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001456 }
1457
Wink Saville36469e72014-06-11 15:17:00 -07001458 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001459 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001460 public boolean isDataConnectivityPossible() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001461 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001462 final Phone phone = getPhone(subId);
1463 if (phone != null) {
1464 return phone.isDataConnectivityPossible();
1465 } else {
1466 return false;
1467 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001468 }
1469
1470 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001471 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07001472 }
1473
Wink Savilleb564aae2014-10-23 10:18:09 -07001474 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001475 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001476 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1477 return false;
1478 }
Wink Saville36469e72014-06-11 15:17:00 -07001479 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001480 }
1481
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001482 public int getCallState() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001483 return getCallStateForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001484 }
1485
Shishir Agrawala9f32182016-04-12 12:00:16 -07001486 public int getCallStateForSlot(int slotId) {
1487 Phone phone = PhoneFactory.getPhone(slotId);
1488 return phone == null ? TelephonyManager.CALL_STATE_IDLE :
1489 DefaultPhoneNotifier.convertCallState(phone.getState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001490 }
1491
Sanket Padawe356d7632015-06-22 14:03:32 -07001492 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001493 public int getDataState() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001494 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001495 if (phone != null) {
1496 return DefaultPhoneNotifier.convertDataState(phone.getDataConnectionState());
1497 } else {
1498 return DefaultPhoneNotifier.convertDataState(PhoneConstants.DataState.DISCONNECTED);
1499 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001500 }
1501
Sanket Padawe356d7632015-06-22 14:03:32 -07001502 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001503 public int getDataActivity() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001504 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001505 if (phone != null) {
1506 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
1507 } else {
1508 return TelephonyManager.DATA_ACTIVITY_NONE;
1509 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001510 }
1511
1512 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001513 public Bundle getCellLocation(String callingPackage) {
1514 enforceFineOrCoarseLocationPermission("getCellLocation");
1515
1516 // OP_COARSE_LOCATION controls both fine and coarse location.
1517 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1518 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001519 log("getCellLocation: returning null; mode != allowed");
Svetoslav64fad262015-04-14 14:35:21 -07001520 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001521 }
1522
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001523 if (checkIfCallerIsSelfOrForegroundUser() ||
1524 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001525 if (DBG_LOC) log("getCellLocation: is active user");
1526 Bundle data = new Bundle();
Legler Wu2c01cdf2014-12-08 19:00:59 +08001527 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001528 if (phone == null) {
1529 return null;
1530 }
Sooraj Sasindran22882212016-07-18 11:57:25 -07001531
1532 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
1533 phone.getCellLocation(workSource).fillInNotifierBundle(data);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001534 return data;
1535 } else {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001536 log("getCellLocation: suppress non-active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001537 return null;
1538 }
1539 }
1540
Svetoslav64fad262015-04-14 14:35:21 -07001541 private void enforceFineOrCoarseLocationPermission(String message) {
1542 try {
1543 mApp.enforceCallingOrSelfPermission(
1544 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1545 } catch (SecurityException e) {
1546 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1547 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1548 // is the weaker precondition
1549 mApp.enforceCallingOrSelfPermission(
1550 android.Manifest.permission.ACCESS_COARSE_LOCATION, message);
1551 }
1552 }
1553
1554
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001555 @Override
1556 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001557 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001558 }
1559
Sanket Padawe356d7632015-06-22 14:03:32 -07001560 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001561 public void enableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001562 mApp.enforceCallingOrSelfPermission(
1563 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001564 final Phone phone = getPhone(subId);
1565 if (phone != null) {
1566 phone.enableLocationUpdates();
1567 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001568 }
1569
1570 @Override
1571 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001572 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001573 }
1574
Sanket Padawe356d7632015-06-22 14:03:32 -07001575 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001576 public void disableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001577 mApp.enforceCallingOrSelfPermission(
1578 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001579 final Phone phone = getPhone(subId);
1580 if (phone != null) {
1581 phone.disableLocationUpdates();
1582 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001583 }
1584
1585 @Override
1586 @SuppressWarnings("unchecked")
1587 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
Svetoslav64fad262015-04-14 14:35:21 -07001588 enforceFineOrCoarseLocationPermission("getNeighboringCellInfo");
1589
1590 // OP_COARSE_LOCATION controls both fine and coarse location.
1591 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1592 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1593 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001594 }
1595
1596 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
1597 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1598 return null;
1599 }
Svetoslav64fad262015-04-14 14:35:21 -07001600
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001601 if (checkIfCallerIsSelfOrForegroundUser() ||
1602 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001603 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
1604
1605 ArrayList<NeighboringCellInfo> cells = null;
1606
Sooraj Sasindran22882212016-07-18 11:57:25 -07001607 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001608 try {
1609 cells = (ArrayList<NeighboringCellInfo>) sendRequest(
Sooraj Sasindran22882212016-07-18 11:57:25 -07001610 CMD_HANDLE_NEIGHBORING_CELL, workSource,
Sanket Padawe56e75a32016-02-08 12:18:19 -08001611 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001612 } catch (RuntimeException e) {
Wink Saville36469e72014-06-11 15:17:00 -07001613 Log.e(LOG_TAG, "getNeighboringCellInfo " + e);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001614 }
1615 return cells;
1616 } else {
1617 if (DBG_LOC) log("getNeighboringCellInfo: suppress non-active user");
1618 return null;
1619 }
1620 }
1621
1622
1623 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001624 public List<CellInfo> getAllCellInfo(String callingPackage) {
1625 enforceFineOrCoarseLocationPermission("getAllCellInfo");
1626
1627 // OP_COARSE_LOCATION controls both fine and coarse location.
1628 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1629 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1630 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001631 }
1632
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001633 if (checkIfCallerIsSelfOrForegroundUser() ||
1634 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001635 if (DBG_LOC) log("getAllCellInfo: is active user");
Sooraj Sasindran22882212016-07-18 11:57:25 -07001636 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
Legler Wu2c01cdf2014-12-08 19:00:59 +08001637 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
1638 for (Phone phone : PhoneFactory.getPhones()) {
Sooraj Sasindran22882212016-07-18 11:57:25 -07001639 final List<CellInfo> info = phone.getAllCellInfo(workSource);
1640 if (info != null) cellInfos.addAll(info);
Legler Wu2c01cdf2014-12-08 19:00:59 +08001641 }
1642 return cellInfos;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001643 } else {
1644 if (DBG_LOC) log("getAllCellInfo: suppress non-active user");
1645 return null;
1646 }
1647 }
1648
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001649 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001650 public void setCellInfoListRate(int rateInMillis) {
Jack Yu3128d9e2017-01-16 10:15:34 -08001651 enforceModifyPermission();
Sooraj Sasindran22882212016-07-18 11:57:25 -07001652 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
1653 mPhone.setCellInfoListRate(rateInMillis, workSource);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001654 }
1655
Shishir Agrawala9f32182016-04-12 12:00:16 -07001656 @Override
1657 public String getImeiForSlot(int slotId, String callingPackage) {
1658 if (!canReadPhoneState(callingPackage, "getImeiForSlot")) {
1659 return null;
1660 }
1661 Phone phone = PhoneFactory.getPhone(slotId);
1662 return phone == null ? null : phone.getImei();
1663 }
1664
1665 @Override
1666 public String getDeviceSoftwareVersionForSlot(int slotId, String callingPackage) {
1667 if (!canReadPhoneState(callingPackage, "getDeviceSoftwareVersionForSlot")) {
1668 return null;
1669 }
1670 Phone phone = PhoneFactory.getPhone(slotId);
1671 return phone == null ? null : phone.getDeviceSvn();
1672 }
1673
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001674 //
1675 // Internal helper methods.
1676 //
1677
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001678 /**
1679 * Returns true if the caller holds INTERACT_ACROSS_USERS_FULL.
1680 */
1681 private boolean checkCallerInteractAcrossUsersFull() {
1682 return mPhone.getContext().checkCallingOrSelfPermission(
1683 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1684 == PackageManager.PERMISSION_GRANTED;
1685 }
1686
Jake Hambye994d462014-02-03 13:10:13 -08001687 private static boolean checkIfCallerIsSelfOrForegroundUser() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001688 boolean ok;
1689
1690 boolean self = Binder.getCallingUid() == Process.myUid();
1691 if (!self) {
1692 // Get the caller's user id then clear the calling identity
1693 // which will be restored in the finally clause.
1694 int callingUser = UserHandle.getCallingUserId();
1695 long ident = Binder.clearCallingIdentity();
1696
1697 try {
1698 // With calling identity cleared the current user is the foreground user.
1699 int foregroundUser = ActivityManager.getCurrentUser();
1700 ok = (foregroundUser == callingUser);
1701 if (DBG_LOC) {
1702 log("checkIfCallerIsSelfOrForegoundUser: foregroundUser=" + foregroundUser
1703 + " callingUser=" + callingUser + " ok=" + ok);
1704 }
1705 } catch (Exception ex) {
1706 if (DBG_LOC) loge("checkIfCallerIsSelfOrForegoundUser: Exception ex=" + ex);
1707 ok = false;
1708 } finally {
1709 Binder.restoreCallingIdentity(ident);
1710 }
1711 } else {
1712 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: is self");
1713 ok = true;
1714 }
1715 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: ret=" + ok);
1716 return ok;
1717 }
1718
1719 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001720 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1721 *
1722 * @throws SecurityException if the caller does not have the required permission
1723 */
1724 private void enforceModifyPermission() {
1725 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1726 }
1727
1728 /**
Junda Liua2e36012014-07-09 18:30:01 -07001729 * Make sure either system app or the caller has carrier privilege.
1730 *
1731 * @throws SecurityException if the caller does not have the required permission/privilege
1732 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001733 private void enforceModifyPermissionOrCarrierPrivilege(int subId) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001734 int permission = mApp.checkCallingOrSelfPermission(
1735 android.Manifest.permission.MODIFY_PHONE_STATE);
1736 if (permission == PackageManager.PERMISSION_GRANTED) {
1737 return;
1738 }
1739
1740 log("No modify permission, check carrier privilege next.");
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001741 enforceCarrierPrivilege(subId);
Junda Liua2e36012014-07-09 18:30:01 -07001742 }
1743
1744 /**
1745 * Make sure the caller has carrier privilege.
1746 *
1747 * @throws SecurityException if the caller does not have the required permission
1748 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001749 private void enforceCarrierPrivilege(int subId) {
1750 if (getCarrierPrivilegeStatus(subId) !=
1751 TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001752 loge("No Carrier Privilege.");
1753 throw new SecurityException("No Carrier Privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001754 }
1755 }
1756
1757 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001758 * Make sure the caller has the CALL_PHONE permission.
1759 *
1760 * @throws SecurityException if the caller does not have the required permission
1761 */
1762 private void enforceCallPermission() {
1763 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1764 }
1765
Stuart Scott8eef64f2015-04-08 15:13:54 -07001766 private void enforceConnectivityInternalPermission() {
1767 mApp.enforceCallingOrSelfPermission(
1768 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1769 "ConnectivityService");
1770 }
1771
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001772 private String createTelUrl(String number) {
1773 if (TextUtils.isEmpty(number)) {
1774 return null;
1775 }
1776
Jake Hambye994d462014-02-03 13:10:13 -08001777 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001778 }
1779
Ihab Awadf9e92732013-12-05 18:02:52 -08001780 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001781 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1782 }
1783
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001784 private static void logv(String msg) {
1785 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
1786 }
1787
Ihab Awadf9e92732013-12-05 18:02:52 -08001788 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001789 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1790 }
1791
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001792 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001793 public int getActivePhoneType() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001794 return getActivePhoneTypeForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001795 }
1796
Sanket Padawe356d7632015-06-22 14:03:32 -07001797 @Override
Shishir Agrawala9f32182016-04-12 12:00:16 -07001798 public int getActivePhoneTypeForSlot(int slotId) {
1799 final Phone phone = PhoneFactory.getPhone(slotId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001800 if (phone == null) {
1801 return PhoneConstants.PHONE_TYPE_NONE;
1802 } else {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001803 return phone.getPhoneType();
Sanket Padawe356d7632015-06-22 14:03:32 -07001804 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001805 }
1806
1807 /**
1808 * Returns the CDMA ERI icon index to display
1809 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001810 @Override
1811 public int getCdmaEriIconIndex(String callingPackage) {
1812 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001813 }
1814
Sanket Padawe356d7632015-06-22 14:03:32 -07001815 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001816 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage) {
1817 if (!canReadPhoneState(callingPackage, "getCdmaEriIconIndexForSubscriber")) {
1818 return -1;
1819 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001820 final Phone phone = getPhone(subId);
1821 if (phone != null) {
1822 return phone.getCdmaEriIconIndex();
1823 } else {
1824 return -1;
1825 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001826 }
1827
1828 /**
1829 * Returns the CDMA ERI icon mode,
1830 * 0 - ON
1831 * 1 - FLASHING
1832 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001833 @Override
1834 public int getCdmaEriIconMode(String callingPackage) {
1835 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001836 }
1837
Sanket Padawe356d7632015-06-22 14:03:32 -07001838 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001839 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage) {
1840 if (!canReadPhoneState(callingPackage, "getCdmaEriIconModeForSubscriber")) {
1841 return -1;
1842 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001843 final Phone phone = getPhone(subId);
1844 if (phone != null) {
1845 return phone.getCdmaEriIconMode();
1846 } else {
1847 return -1;
1848 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001849 }
1850
1851 /**
1852 * Returns the CDMA ERI text,
1853 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001854 @Override
1855 public String getCdmaEriText(String callingPackage) {
1856 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001857 }
1858
Sanket Padawe356d7632015-06-22 14:03:32 -07001859 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001860 public String getCdmaEriTextForSubscriber(int subId, String callingPackage) {
1861 if (!canReadPhoneState(callingPackage, "getCdmaEriIconTextForSubscriber")) {
1862 return null;
1863 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001864 final Phone phone = getPhone(subId);
1865 if (phone != null) {
1866 return phone.getCdmaEriText();
1867 } else {
1868 return null;
1869 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001870 }
1871
1872 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07001873 * Returns the CDMA MDN.
1874 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001875 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001876 public String getCdmaMdn(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001877 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001878 final Phone phone = getPhone(subId);
1879 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA && phone != null) {
1880 return phone.getLine1Number();
Junda Liuca05d5d2014-08-14 22:36:34 -07001881 } else {
1882 return null;
1883 }
1884 }
1885
1886 /**
1887 * Returns the CDMA MIN.
1888 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001889 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001890 public String getCdmaMin(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001891 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001892 final Phone phone = getPhone(subId);
1893 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1894 return phone.getCdmaMin();
Junda Liuca05d5d2014-08-14 22:36:34 -07001895 } else {
1896 return null;
1897 }
1898 }
1899
1900 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001901 * Returns true if CDMA provisioning needs to run.
1902 */
1903 public boolean needsOtaServiceProvisioning() {
1904 return mPhone.needsOtaServiceProvisioning();
1905 }
1906
1907 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001908 * Sets the voice mail number of a given subId.
1909 */
1910 @Override
1911 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001912 enforceCarrierPrivilege(subId);
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001913 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
1914 new Pair<String, String>(alphaTag, number), new Integer(subId));
1915 return success;
1916 }
1917
Ta-wei Yen87c49842016-05-13 21:19:52 -07001918 @Override
Ta-wei Yenb0f695b2016-08-08 17:33:11 -07001919 public void setVisualVoicemailEnabled(String callingPackage,
1920 PhoneAccountHandle phoneAccountHandle, boolean enabled) {
1921 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
1922 if (!TextUtils.equals(callingPackage,
1923 TelecomManager.from(mPhone.getContext()).getDefaultDialerPackage())) {
1924 enforceModifyPermissionOrCarrierPrivilege(
1925 PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle));
1926 }
1927 VisualVoicemailSettingsUtil.setEnabled(mPhone.getContext(), phoneAccountHandle, enabled);
1928 }
1929
1930 @Override
1931 public boolean isVisualVoicemailEnabled(String callingPackage,
1932 PhoneAccountHandle phoneAccountHandle) {
1933 if (!canReadPhoneState(callingPackage, "isVisualVoicemailEnabled")) {
1934 return false;
1935 }
1936 return VisualVoicemailSettingsUtil.isEnabled(mPhone.getContext(), phoneAccountHandle);
1937 }
1938
1939 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07001940 public void enableVisualVoicemailSmsFilter(String callingPackage, int subId,
1941 VisualVoicemailSmsFilterSettings settings) {
1942 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001943 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07001944 .enableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId,
1945 settings);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001946 }
1947
1948 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07001949 public void disableVisualVoicemailSmsFilter(String callingPackage, int subId) {
1950 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001951 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07001952 .disableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001953 }
1954
1955 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07001956 public VisualVoicemailSmsFilterSettings getVisualVoicemailSmsFilterSettings(
1957 String callingPackage, int subId) {
1958 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001959 return VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07001960 .getVisualVoicemailSmsFilterSettings(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001961 }
1962
1963 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07001964 public VisualVoicemailSmsFilterSettings getSystemVisualVoicemailSmsFilterSettings(
1965 String packageName, int subId) {
1966 enforceReadPrivilegedPermission();
Ta-wei Yen87c49842016-05-13 21:19:52 -07001967 return VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07001968 .getVisualVoicemailSmsFilterSettings(mPhone.getContext(), packageName, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001969 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001970 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001971 * Returns the unread count of voicemails
1972 */
1973 public int getVoiceMessageCount() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001974 return getVoiceMessageCountForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001975 }
1976
1977 /**
1978 * Returns the unread count of voicemails for a subId
1979 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001980 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001981 public int getVoiceMessageCountForSubscriber( int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001982 final Phone phone = getPhone(subId);
1983 if (phone != null) {
1984 return phone.getVoiceMessageCount();
1985 } else {
1986 return 0;
1987 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001988 }
1989
1990 /**
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001991 * Returns the data network type.
1992 * Legacy call, permission-free.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001993 *
1994 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
1995 */
1996 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001997 public int getNetworkType() {
1998 final Phone phone = getPhone(getDefaultSubscription());
1999 if (phone != null) {
2000 return phone.getServiceState().getDataNetworkType();
2001 } else {
2002 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2003 }
Wink Saville36469e72014-06-11 15:17:00 -07002004 }
2005
2006 /**
2007 * Returns the network type for a subId
2008 */
2009 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002010 public int getNetworkTypeForSubscriber(int subId, String callingPackage) {
2011 if (!canReadPhoneState(callingPackage, "getNetworkTypeForSubscriber")) {
2012 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2013 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002014
Sanket Padawe356d7632015-06-22 14:03:32 -07002015 final Phone phone = getPhone(subId);
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002016 if (phone != null) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002017 return phone.getServiceState().getDataNetworkType();
2018 } else {
2019 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2020 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002021 }
2022
2023 /**
2024 * Returns the data network type
2025 */
2026 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002027 public int getDataNetworkType(String callingPackage) {
2028 return getDataNetworkTypeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002029 }
2030
2031 /**
2032 * Returns the data network type for a subId
2033 */
2034 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002035 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage) {
2036 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2037 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2038 }
2039
Sanket Padawe356d7632015-06-22 14:03:32 -07002040 final Phone phone = getPhone(subId);
2041 if (phone != null) {
2042 return phone.getServiceState().getDataNetworkType();
2043 } else {
2044 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2045 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002046 }
2047
2048 /**
Wink Saville36469e72014-06-11 15:17:00 -07002049 * Returns the Voice network type for a subId
2050 */
2051 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002052 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage) {
2053 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2054 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2055 }
2056
Sanket Padawe356d7632015-06-22 14:03:32 -07002057 final Phone phone = getPhone(subId);
2058 if (phone != null) {
2059 return phone.getServiceState().getVoiceNetworkType();
2060 } else {
2061 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2062 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002063 }
2064
2065 /**
2066 * @return true if a ICC card is present
2067 */
2068 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07002069 // FIXME Make changes to pass defaultSimId of type int
Amit Mahajana6fc2a82015-01-06 11:53:51 -08002070 return hasIccCardUsingSlotId(mSubscriptionController.getSlotId(getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07002071 }
2072
2073 /**
2074 * @return true if a ICC card is present for a slotId
2075 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002076 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002077 public boolean hasIccCardUsingSlotId(int slotId) {
Amit Mahajana6fc2a82015-01-06 11:53:51 -08002078 int subId[] = mSubscriptionController.getSubIdUsingSlotId(slotId);
Sanket Padawe356d7632015-06-22 14:03:32 -07002079 final Phone phone = getPhone(subId[0]);
2080 if (subId != null && phone != null) {
2081 return phone.getIccCard().hasIccCard();
Amit Mahajana6fc2a82015-01-06 11:53:51 -08002082 } else {
2083 return false;
2084 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002085 }
2086
2087 /**
2088 * Return if the current radio is LTE on CDMA. This
2089 * is a tri-state return value as for a period of time
2090 * the mode may be unknown.
2091 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002092 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002093 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08002094 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002095 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002096 @Override
2097 public int getLteOnCdmaMode(String callingPackage) {
2098 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002099 }
2100
Sanket Padawe356d7632015-06-22 14:03:32 -07002101 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002102 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage) {
2103 if (!canReadPhoneState(callingPackage, "getLteOnCdmaModeForSubscriber")) {
2104 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2105 }
2106
Sanket Padawe356d7632015-06-22 14:03:32 -07002107 final Phone phone = getPhone(subId);
2108 if (phone == null) {
2109 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2110 } else {
2111 return phone.getLteOnCdmaMode();
2112 }
Wink Saville36469e72014-06-11 15:17:00 -07002113 }
2114
2115 public void setPhone(Phone phone) {
2116 mPhone = phone;
2117 }
2118
2119 /**
2120 * {@hide}
2121 * Returns Default subId, 0 in the case of single standby.
2122 */
Wink Savilleb564aae2014-10-23 10:18:09 -07002123 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002124 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07002125 }
2126
Shishir Agrawala9f32182016-04-12 12:00:16 -07002127 private int getSlotForDefaultSubscription() {
2128 return mSubscriptionController.getPhoneId(getDefaultSubscription());
2129 }
2130
Wink Savilleb564aae2014-10-23 10:18:09 -07002131 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002132 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002133 }
Ihab Awadf2177b72013-11-25 13:33:23 -08002134
2135 /**
2136 * @see android.telephony.TelephonyManager.WifiCallingChoices
2137 */
2138 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002139 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
2140 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08002141 }
2142
2143 /**
2144 * @see android.telephony.TelephonyManager.WifiCallingChoices
2145 */
2146 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002147 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
2148 Settings.System.putInt(mPhone.getContext().getContentResolver(),
2149 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08002150 }
2151
Sailesh Nepald1e68152013-12-12 19:08:02 -08002152 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07002153 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08002154 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08002155 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08002156
Shishir Agrawal566b7612013-10-28 14:41:00 -07002157 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002158 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(int subId, String AID) {
2159 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002160
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002161 if (DBG) log("iccOpenLogicalChannel: subId=" + subId + " aid=" + AID);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002162 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002163 CMD_OPEN_CHANNEL, AID, subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002164 if (DBG) log("iccOpenLogicalChannel: " + response);
2165 return response;
Shishir Agrawal566b7612013-10-28 14:41:00 -07002166 }
2167
2168 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002169 public boolean iccCloseLogicalChannel(int subId, int channel) {
2170 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002171
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002172 if (DBG) log("iccCloseLogicalChannel: subId=" + subId + " chnl=" + channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002173 if (channel < 0) {
2174 return false;
2175 }
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002176 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel, subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002177 if (DBG) log("iccCloseLogicalChannel: " + success);
2178 return success;
2179 }
2180
2181 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002182 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07002183 int command, int p1, int p2, int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002184 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002185
2186 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002187 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel +
2188 " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
Shishir Agrawal566b7612013-10-28 14:41:00 -07002189 " data=" + data);
2190 }
2191
2192 if (channel < 0) {
2193 return "";
2194 }
2195
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002196 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002197 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002198 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
2199
Shishir Agrawal566b7612013-10-28 14:41:00 -07002200 // Append the returned status code to the end of the response payload.
2201 String s = Integer.toHexString(
2202 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002203 if (response.payload != null) {
2204 s = IccUtils.bytesToHexString(response.payload) + s;
2205 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07002206 return s;
2207 }
Jake Hambye994d462014-02-03 13:10:13 -08002208
Evan Charltonc66da362014-05-16 14:06:40 -07002209 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002210 public String iccTransmitApduBasicChannel(int subId, int cla, int command, int p1, int p2,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002211 int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002212 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002213
2214 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002215 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd=" + command
2216 + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002217 }
2218
2219 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002220 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002221 if (DBG) log("iccTransmitApduBasicChannel: " + response);
2222
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002223 // Append the returned status code to the end of the response payload.
2224 String s = Integer.toHexString(
2225 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002226 if (response.payload != null) {
2227 s = IccUtils.bytesToHexString(response.payload) + s;
2228 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002229 return s;
2230 }
2231
2232 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002233 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002234 String filePath) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002235 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002236
2237 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002238 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " " +
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002239 p1 + " " + p2 + " " + p3 + ":" + filePath);
2240 }
2241
2242 IccIoResult response =
2243 (IccIoResult)sendRequest(CMD_EXCHANGE_SIM_IO,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002244 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
2245 subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002246
2247 if (DBG) {
2248 log("Exchange SIM_IO [R]" + response);
2249 }
2250
2251 byte[] result = null;
2252 int length = 2;
2253 if (response.payload != null) {
2254 length = 2 + response.payload.length;
2255 result = new byte[length];
2256 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
2257 } else {
2258 result = new byte[length];
2259 }
2260
2261 result[length - 1] = (byte) response.sw2;
2262 result[length - 2] = (byte) response.sw1;
2263 return result;
2264 }
2265
2266 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002267 public String sendEnvelopeWithStatus(int subId, String content) {
2268 enforceModifyPermissionOrCarrierPrivilege(subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002269
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002270 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content, subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002271 if (response.payload == null) {
2272 return "";
2273 }
2274
2275 // Append the returned status code to the end of the response payload.
2276 String s = Integer.toHexString(
2277 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
2278 s = IccUtils.bytesToHexString(response.payload) + s;
2279 return s;
2280 }
2281
Jake Hambye994d462014-02-03 13:10:13 -08002282 /**
2283 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2284 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2285 *
2286 * @param itemID the ID of the item to read
2287 * @return the NV item as a String, or null on error.
2288 */
2289 @Override
2290 public String nvReadItem(int itemID) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002291 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002292 if (DBG) log("nvReadItem: item " + itemID);
2293 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
2294 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
2295 return value;
2296 }
2297
2298 /**
2299 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2300 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2301 *
2302 * @param itemID the ID of the item to read
2303 * @param itemValue the value to write, as a String
2304 * @return true on success; false on any failure
2305 */
2306 @Override
2307 public boolean nvWriteItem(int itemID, String itemValue) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002308 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002309 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
2310 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
2311 new Pair<Integer, String>(itemID, itemValue));
2312 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
2313 return success;
2314 }
2315
2316 /**
2317 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
2318 * Used for device configuration by some CDMA operators.
2319 *
2320 * @param preferredRoamingList byte array containing the new PRL
2321 * @return true on success; false on any failure
2322 */
2323 @Override
2324 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002325 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002326 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
2327 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
2328 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
2329 return success;
2330 }
2331
2332 /**
2333 * Perform the specified type of NV config reset.
2334 * Used for device configuration by some CDMA operators.
2335 *
2336 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
2337 * @return true on success; false on any failure
2338 */
2339 @Override
2340 public boolean nvResetConfig(int resetType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002341 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002342 if (DBG) log("nvResetConfig: type " + resetType);
2343 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
2344 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
2345 return success;
2346 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002347
2348 /**
Wink Saville36469e72014-06-11 15:17:00 -07002349 * {@hide}
2350 * Returns Default sim, 0 in the case of single standby.
2351 */
2352 public int getDefaultSim() {
2353 //TODO Need to get it from Telephony Devcontroller
2354 return 0;
2355 }
2356
Svet Ganovb320e182015-04-16 12:30:10 -07002357 public String[] getPcscfAddress(String apnType, String callingPackage) {
2358 if (!canReadPhoneState(callingPackage, "getPcscfAddress")) {
2359 return new String[0];
2360 }
2361
2362
ram87fca6f2014-07-18 18:58:44 +05302363 return mPhone.getPcscfAddress(apnType);
Wink Saville36469e72014-06-11 15:17:00 -07002364 }
2365
Brad Ebinger76681002017-01-23 13:50:20 -08002366 /**
2367 * Returns the {@link IImsServiceController} that corresponds to the given slot Id and IMS
2368 * feature or {@link null} if the service is not available. If an ImsServiceController is
2369 * available, the {@link IImsServiceFeatureListener} callback is registered as a listener for
2370 * feature updates.
2371 */
2372 public IImsServiceController getImsServiceControllerAndListen(int slotId, int feature,
2373 IImsServiceFeatureListener callback) {
2374 enforceModifyPermission();
2375 return PhoneFactory.getImsResolver().getImsServiceControllerAndListen(slotId, feature,
2376 callback);
2377 }
2378
Wink Saville36469e72014-06-11 15:17:00 -07002379 public void setImsRegistrationState(boolean registered) {
2380 enforceModifyPermission();
2381 mPhone.setImsRegistrationState(registered);
2382 }
2383
2384 /**
Stuart Scott54788802015-03-30 13:18:01 -07002385 * Set the network selection mode to automatic.
2386 *
2387 */
2388 @Override
2389 public void setNetworkSelectionModeAutomatic(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002390 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002391 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
2392 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
2393 }
2394
2395 /**
Shishir Agrawal302c8692015-06-19 13:49:39 -07002396 * Set the network selection mode to manual with the selected carrier.
2397 */
2398 @Override
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002399 public boolean setNetworkSelectionModeManual(int subId, OperatorInfo operator,
2400 boolean persistSelection) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002401 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002402 if (DBG) log("setNetworkSelectionModeManual: subId:" + subId + " operator:" + operator);
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002403 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operator,
2404 persistSelection);
2405 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002406 }
2407
2408 /**
2409 * Scans for available networks.
2410 */
2411 @Override
2412 public CellNetworkScanResult getCellNetworkScanResults(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002413 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002414 if (DBG) log("getCellNetworkScanResults: subId " + subId);
2415 CellNetworkScanResult result = (CellNetworkScanResult) sendRequest(
2416 CMD_PERFORM_NETWORK_SCAN, null, subId);
2417 return result;
2418 }
2419
2420 /**
Junda Liu84d15a22014-07-02 11:21:04 -07002421 * Get the calculated preferred network type.
2422 * Used for debugging incorrect network type.
2423 *
2424 * @return the preferred network type, defined in RILConstants.java.
2425 */
2426 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002427 public int getCalculatedPreferredNetworkType(String callingPackage) {
2428 if (!canReadPhoneState(callingPackage, "getCalculatedPreferredNetworkType")) {
2429 return RILConstants.PREFERRED_NETWORK_MODE;
2430 }
2431
Amit Mahajan43330e02014-11-18 11:54:45 -08002432 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext(), 0); // wink FIXME: need to get SubId from somewhere.
Junda Liu84d15a22014-07-02 11:21:04 -07002433 }
2434
2435 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08002436 * Get the preferred network type.
2437 * Used for device configuration by some CDMA operators.
2438 *
2439 * @return the preferred network type, defined in RILConstants.java.
2440 */
2441 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002442 public int getPreferredNetworkType(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002443 enforceModifyPermissionOrCarrierPrivilege(subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002444 if (DBG) log("getPreferredNetworkType");
Stuart Scott54788802015-03-30 13:18:01 -07002445 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002446 int networkType = (result != null ? result[0] : -1);
2447 if (DBG) log("getPreferredNetworkType: " + networkType);
2448 return networkType;
2449 }
2450
2451 /**
2452 * Set the preferred network type.
2453 * Used for device configuration by some CDMA operators.
2454 *
2455 * @param networkType the preferred network type, defined in RILConstants.java.
2456 * @return true on success; false on any failure.
2457 */
2458 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002459 public boolean setPreferredNetworkType(int subId, int networkType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002460 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002461 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
2462 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002463 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
Junda Liu80bc0d12014-07-14 16:36:44 -07002464 if (success) {
2465 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
Stuart Scott54788802015-03-30 13:18:01 -07002466 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
Junda Liu80bc0d12014-07-14 16:36:44 -07002467 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002468 return success;
2469 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002470
2471 /**
Junda Liu475951f2014-11-07 16:45:03 -08002472 * Check TETHER_DUN_REQUIRED and TETHER_DUN_APN settings, net.tethering.noprovisioning
2473 * SystemProperty, and config_tether_apndata to decide whether DUN APN is required for
2474 * tethering.
2475 *
2476 * @return 0: Not required. 1: required. 2: Not set.
2477 * @hide
2478 */
2479 @Override
2480 public int getTetherApnRequired() {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002481 enforceModifyPermission();
Junda Liu475951f2014-11-07 16:45:03 -08002482 int dunRequired = Settings.Global.getInt(mPhone.getContext().getContentResolver(),
2483 Settings.Global.TETHER_DUN_REQUIRED, 2);
2484 // If not set, check net.tethering.noprovisioning, TETHER_DUN_APN setting and
2485 // config_tether_apndata.
2486 if (dunRequired == 2 && mPhone.hasMatchedTetherApnSetting()) {
2487 dunRequired = 1;
2488 }
2489 return dunRequired;
2490 }
2491
2492 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07002493 * Set mobile data enabled
2494 * Used by the user through settings etc to turn on/off mobile data
2495 *
2496 * @param enable {@code true} turn turn data on, else {@code false}
2497 */
2498 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002499 public void setDataEnabled(int subId, boolean enable) {
Jeff Davidsonf7eecf02016-11-18 17:05:56 -08002500 enforceModifyPermissionOrCarrierPrivilege(subId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002501 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002502 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002503 Phone phone = PhoneFactory.getPhone(phoneId);
2504 if (phone != null) {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002505 if (DBG) log("setDataEnabled: subId=" + subId + " enable=" + enable);
Wink Savillee7353bb2014-12-05 14:21:41 -08002506 phone.setDataEnabled(enable);
2507 } else {
2508 loge("setDataEnabled: no phone for subId=" + subId);
2509 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002510 }
2511
2512 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07002513 * Get whether mobile data is enabled.
2514 *
Jeff Davidsonf7eecf02016-11-18 17:05:56 -08002515 * Accepts either ACCESS_NETWORK_STATE, MODIFY_PHONE_STATE or carrier privileges.
Robert Greenwalted86e582014-05-21 20:03:20 -07002516 *
2517 * @return {@code true} if data is enabled else {@code false}
2518 */
2519 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002520 public boolean getDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07002521 try {
2522 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
2523 null);
2524 } catch (Exception e) {
Jeff Davidsonf7eecf02016-11-18 17:05:56 -08002525 enforceModifyPermissionOrCarrierPrivilege(subId);
Robert Greenwalt646120a2014-05-23 11:54:03 -07002526 }
Wink Savillee7353bb2014-12-05 14:21:41 -08002527 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002528 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002529 Phone phone = PhoneFactory.getPhone(phoneId);
2530 if (phone != null) {
2531 boolean retVal = phone.getDataEnabled();
Joe Onoratoa601dd22016-02-23 13:03:53 -08002532 if (DBG) log("getDataEnabled: subId=" + subId + " retVal=" + retVal);
Wink Savillee7353bb2014-12-05 14:21:41 -08002533 return retVal;
2534 } else {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002535 if (DBG) loge("getDataEnabled: no phone subId=" + subId + " retVal=false");
Wink Savillee7353bb2014-12-05 14:21:41 -08002536 return false;
2537 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002538 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002539
2540 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002541 public int getCarrierPrivilegeStatus(int subId) {
2542 final Phone phone = getPhone(subId);
2543 if (phone == null) {
2544 loge("getCarrierPrivilegeStatus: Invalid subId");
2545 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
2546 }
2547 UiccCard card = UiccController.getInstance().getUiccCard(phone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002548 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002549 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002550 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2551 }
2552 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002553 phone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002554 }
Junda Liu29340342014-07-10 15:23:27 -07002555
2556 @Override
Zach Johnson50ecba32015-05-19 00:24:21 -07002557 public int checkCarrierPrivilegesForPackage(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002558 if (TextUtils.isEmpty(pkgName))
2559 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Shishir Agrawal21409252015-01-15 23:33:50 -08002560 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002561 if (card == null) {
2562 loge("checkCarrierPrivilegesForPackage: No UICC");
2563 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2564 }
Zach Johnson50ecba32015-05-19 00:24:21 -07002565 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgName);
2566 }
2567
2568 @Override
2569 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002570 if (TextUtils.isEmpty(pkgName))
2571 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Zach Johnson50ecba32015-05-19 00:24:21 -07002572 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2573 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2574 UiccCard card = UiccController.getInstance().getUiccCard(i);
2575 if (card == null) {
Jonathan Basseri7d320df2015-06-16 12:17:08 -07002576 // No UICC in that slot.
Zach Johnson50ecba32015-05-19 00:24:21 -07002577 continue;
2578 }
2579
2580 result = card.getCarrierPrivilegeStatus(
2581 mPhone.getContext().getPackageManager(), pkgName);
2582 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2583 break;
2584 }
2585 }
2586
2587 return result;
Junda Liu29340342014-07-10 15:23:27 -07002588 }
Derek Tan89e89d42014-07-08 17:00:10 -07002589
2590 @Override
Junda Liue64de782015-04-16 17:19:16 -07002591 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
2592 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
2593 loge("phoneId " + phoneId + " is not valid.");
2594 return null;
2595 }
2596 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002597 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002598 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002599 return null ;
2600 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002601 return card.getCarrierPackageNamesForIntent(
Svetoslav483aff72015-04-21 14:16:07 -07002602 mPhone.getContext().getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002603 }
2604
Amith Yamasani6e118872016-02-19 12:53:51 -08002605 @Override
2606 public List<String> getPackagesWithCarrierPrivileges() {
2607 PackageManager pm = mPhone.getContext().getPackageManager();
2608 List<String> privilegedPackages = new ArrayList<>();
2609 List<PackageInfo> packages = null;
2610 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2611 UiccCard card = UiccController.getInstance().getUiccCard(i);
2612 if (card == null) {
2613 // No UICC in that slot.
2614 continue;
2615 }
2616 if (card.hasCarrierPrivilegeRules()) {
2617 if (packages == null) {
2618 // Only check packages in user 0 for now
2619 packages = pm.getInstalledPackagesAsUser(
2620 PackageManager.MATCH_DISABLED_COMPONENTS
2621 | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS
2622 | PackageManager.GET_SIGNATURES, UserHandle.USER_SYSTEM);
2623 }
2624 for (int p = packages.size() - 1; p >= 0; p--) {
2625 PackageInfo pkgInfo = packages.get(p);
2626 if (pkgInfo != null && pkgInfo.packageName != null
2627 && card.getCarrierPrivilegeStatus(pkgInfo)
2628 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2629 privilegedPackages.add(pkgInfo.packageName);
2630 }
2631 }
2632 }
2633 }
2634 return privilegedPackages;
2635 }
2636
Wink Savilleb564aae2014-10-23 10:18:09 -07002637 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002638 final Phone phone = getPhone(subId);
2639 UiccCard card = phone == null ? null : phone.getUiccCard();
Derek Tan97ebb422014-09-05 16:55:38 -07002640 if (card == null) {
2641 loge("getIccId: No UICC");
2642 return null;
2643 }
2644 String iccId = card.getIccId();
2645 if (TextUtils.isEmpty(iccId)) {
2646 loge("getIccId: ICC ID is null or empty.");
2647 return null;
2648 }
2649 return iccId;
2650 }
2651
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002652 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002653 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
2654 String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002655 enforceCarrierPrivilege(subId);
Derek Tan97ebb422014-09-05 16:55:38 -07002656
Jeff Sharkey85190e62014-12-05 09:40:12 -08002657 final String iccId = getIccId(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07002658 final Phone phone = getPhone(subId);
2659 if (phone == null) {
2660 return false;
2661 }
2662 final String subscriberId = phone.getSubscriberId();
Jeff Sharkey85190e62014-12-05 09:40:12 -08002663
2664 if (DBG_MERGE) {
2665 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
2666 + subscriberId + " to " + number);
2667 }
2668
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002669 if (TextUtils.isEmpty(iccId)) {
2670 return false;
Derek Tan97ebb422014-09-05 16:55:38 -07002671 }
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002672
Jeff Sharkey85190e62014-12-05 09:40:12 -08002673 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2674
2675 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002676 if (alphaTag == null) {
2677 editor.remove(alphaTagPrefKey);
2678 } else {
2679 editor.putString(alphaTagPrefKey, alphaTag);
2680 }
2681
Jeff Sharkey85190e62014-12-05 09:40:12 -08002682 // Record both the line number and IMSI for this ICCID, since we need to
2683 // track all merged IMSIs based on line number
2684 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2685 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002686 if (number == null) {
2687 editor.remove(numberPrefKey);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002688 editor.remove(subscriberPrefKey);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002689 } else {
2690 editor.putString(numberPrefKey, number);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002691 editor.putString(subscriberPrefKey, subscriberId);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002692 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002693
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002694 editor.commit();
2695 return true;
Derek Tan7226c842014-07-02 17:42:23 -07002696 }
2697
2698 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002699 public String getLine1NumberForDisplay(int subId, String callingPackage) {
Makoto Onukifee69342015-06-29 14:44:50 -07002700 // This is open to apps with WRITE_SMS.
2701 if (!canReadPhoneNumber(callingPackage, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08002702 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07002703 return null;
2704 }
Derek Tan97ebb422014-09-05 16:55:38 -07002705
2706 String iccId = getIccId(subId);
2707 if (iccId != null) {
2708 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
Amit Mahajan9cf11512015-11-09 11:40:48 -08002709 if (DBG_MERGE) {
2710 log("getLine1NumberForDisplay returning " +
2711 mTelephonySharedPreferences.getString(numberPrefKey, null));
2712 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002713 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002714 }
Amit Mahajan9cf11512015-11-09 11:40:48 -08002715 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
Derek Tan97ebb422014-09-05 16:55:38 -07002716 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002717 }
2718
2719 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002720 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
2721 if (!canReadPhoneState(callingPackage, "getLine1AlphaTagForDisplay")) {
2722 return null;
2723 }
Derek Tan97ebb422014-09-05 16:55:38 -07002724
2725 String iccId = getIccId(subId);
2726 if (iccId != null) {
2727 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002728 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002729 }
Derek Tan97ebb422014-09-05 16:55:38 -07002730 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002731 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002732
2733 @Override
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002734 public String[] getMergedSubscriberIds(String callingPackage) {
2735 if (!canReadPhoneState(callingPackage, "getMergedSubscriberIds")) {
2736 return null;
2737 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002738 final Context context = mPhone.getContext();
2739 final TelephonyManager tele = TelephonyManager.from(context);
2740 final SubscriptionManager sub = SubscriptionManager.from(context);
2741
2742 // Figure out what subscribers are currently active
2743 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002744 // Clear calling identity, when calling TelephonyManager, because callerUid must be
2745 // the process, where TelephonyManager was instantiated. Otherwise AppOps check will fail.
2746 final long identity = Binder.clearCallingIdentity();
2747 try {
2748 final int[] subIds = sub.getActiveSubscriptionIdList();
2749 for (int subId : subIds) {
2750 activeSubscriberIds.add(tele.getSubscriberId(subId));
2751 }
2752 } finally {
2753 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002754 }
2755
2756 // First pass, find a number override for an active subscriber
2757 String mergeNumber = null;
2758 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
2759 for (String key : prefs.keySet()) {
2760 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
2761 final String subscriberId = (String) prefs.get(key);
2762 if (activeSubscriberIds.contains(subscriberId)) {
2763 final String iccId = key.substring(PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
2764 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2765 mergeNumber = (String) prefs.get(numberKey);
2766 if (DBG_MERGE) {
2767 Slog.d(LOG_TAG, "Found line number " + mergeNumber
2768 + " for active subscriber " + subscriberId);
2769 }
2770 if (!TextUtils.isEmpty(mergeNumber)) {
2771 break;
2772 }
2773 }
2774 }
2775 }
2776
2777 // Shortcut when no active merged subscribers
2778 if (TextUtils.isEmpty(mergeNumber)) {
2779 return null;
2780 }
2781
2782 // Second pass, find all subscribers under that line override
2783 final ArraySet<String> result = new ArraySet<>();
2784 for (String key : prefs.keySet()) {
2785 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
2786 final String number = (String) prefs.get(key);
2787 if (mergeNumber.equals(number)) {
2788 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
2789 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
2790 final String subscriberId = (String) prefs.get(subscriberKey);
2791 if (!TextUtils.isEmpty(subscriberId)) {
2792 result.add(subscriberId);
2793 }
2794 }
2795 }
2796 }
2797
2798 final String[] resultArray = result.toArray(new String[result.size()]);
2799 Arrays.sort(resultArray);
2800 if (DBG_MERGE) {
2801 Slog.d(LOG_TAG, "Found subscribers " + Arrays.toString(resultArray) + " after merge");
2802 }
2803 return resultArray;
2804 }
2805
2806 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002807 public boolean setOperatorBrandOverride(int subId, String brand) {
2808 enforceCarrierPrivilege(subId);
2809 final Phone phone = getPhone(subId);
2810 return phone == null ? false : phone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002811 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05002812
2813 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002814 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002815 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
2816 List<String> cdmaNonRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002817 enforceCarrierPrivilege(subId);
2818 final Phone phone = getPhone(subId);
2819 if (phone == null) {
2820 return false;
2821 }
2822 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002823 cdmaNonRoamingList);
2824 }
2825
2826 @Override
Steven Liu4bf01bc2014-07-17 11:05:29 -05002827 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
2828 enforceModifyPermission();
2829
2830 int returnValue = 0;
2831 try {
2832 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
2833 if(result.exception == null) {
2834 if (result.result != null) {
2835 byte[] responseData = (byte[])(result.result);
2836 if(responseData.length > oemResp.length) {
2837 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
2838 responseData.length + "bytes. Buffer Size is " +
2839 oemResp.length + "bytes.");
2840 }
2841 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
2842 returnValue = responseData.length;
2843 }
2844 } else {
2845 CommandException ex = (CommandException) result.exception;
2846 returnValue = ex.getCommandError().ordinal();
2847 if(returnValue > 0) returnValue *= -1;
2848 }
2849 } catch (RuntimeException e) {
2850 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
2851 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
2852 if(returnValue > 0) returnValue *= -1;
2853 }
2854
2855 return returnValue;
2856 }
Wink Saville5d475dd2014-10-17 15:00:58 -07002857
2858 @Override
2859 public void setRadioCapability(RadioAccessFamily[] rafs) {
2860 try {
2861 ProxyController.getInstance().setRadioCapability(rafs);
2862 } catch (RuntimeException e) {
2863 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
2864 }
2865 }
2866
2867 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002868 public int getRadioAccessFamily(int phoneId, String callingPackage) {
2869 if (!canReadPhoneState(callingPackage, "getRadioAccessFamily")) {
2870 return RadioAccessFamily.RAF_UNKNOWN;
2871 }
2872
Wink Saville5d475dd2014-10-17 15:00:58 -07002873 return ProxyController.getInstance().getRadioAccessFamily(phoneId);
2874 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002875
2876 @Override
2877 public void enableVideoCalling(boolean enable) {
2878 enforceModifyPermission();
Tyler Gunnfdd69de2015-12-04 21:24:38 -08002879 ImsManager.setVtSetting(mPhone.getContext(), enable);
Andrew Leedf14ead2014-10-17 14:22:52 -07002880 }
2881
2882 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002883 public boolean isVideoCallingEnabled(String callingPackage) {
2884 if (!canReadPhoneState(callingPackage, "isVideoCallingEnabled")) {
2885 return false;
2886 }
2887
Andrew Lee77527ac2014-10-21 16:57:39 -07002888 // Check the user preference and the system-level IMS setting. Even if the user has
2889 // enabled video calling, if IMS is disabled we aren't able to support video calling.
2890 // In the long run, we may instead need to check if there exists a connection service
2891 // which can support video calling.
Andrew Lee312e8172014-10-23 17:01:36 -07002892 return ImsManager.isVtEnabledByPlatform(mPhone.getContext())
2893 && ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
Tyler Gunnfdd69de2015-12-04 21:24:38 -08002894 && ImsManager.isVtEnabledByUser(mPhone.getContext());
Andrew Leedf14ead2014-10-17 14:22:52 -07002895 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002896
Andrew Leea1239f22015-03-02 17:44:07 -08002897 @Override
2898 public boolean canChangeDtmfToneLength() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07002899 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08002900 }
2901
2902 @Override
2903 public boolean isWorldPhone() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07002904 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08002905 }
2906
Andrew Lee9431b832015-03-09 18:46:45 -07002907 @Override
2908 public boolean isTtyModeSupported() {
2909 TelecomManager telecomManager = TelecomManager.from(mPhone.getContext());
2910 TelephonyManager telephonyManager =
2911 (TelephonyManager) mPhone.getContext().getSystemService(Context.TELEPHONY_SERVICE);
Wooki Wu1f82f7a2016-02-15 15:59:58 +08002912 return telecomManager.isTtySupported();
Andrew Lee9431b832015-03-09 18:46:45 -07002913 }
2914
2915 @Override
2916 public boolean isHearingAidCompatibilitySupported() {
2917 return mPhone.getContext().getResources().getBoolean(R.bool.hac_enabled);
2918 }
2919
Sanket Padawe7310cc72015-01-14 09:53:20 -08002920 /**
2921 * Returns the unique device ID of phone, for example, the IMEI for
2922 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
2923 *
2924 * <p>Requires Permission:
2925 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
2926 */
2927 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002928 public String getDeviceId(String callingPackage) {
2929 if (!canReadPhoneState(callingPackage, "getDeviceId")) {
2930 return null;
2931 }
2932
Sanket Padawe7310cc72015-01-14 09:53:20 -08002933 final Phone phone = PhoneFactory.getPhone(0);
2934 if (phone != null) {
2935 return phone.getDeviceId();
2936 } else {
2937 return null;
2938 }
2939 }
2940
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002941 /*
2942 * {@hide}
2943 * Returns the IMS Registration Status
2944 */
Santos Cordon7a1885b2015-02-03 11:15:19 -08002945 @Override
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002946 public boolean isImsRegistered() {
2947 return mPhone.isImsRegistered();
2948 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08002949
2950 @Override
2951 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
2952 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
2953 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07002954
Nathan Haroldc55097a2015-03-11 18:14:50 -07002955 /*
2956 * {@hide}
2957 * Returns the IMS Registration Status
2958 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002959 public boolean isWifiCallingAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07002960 return mPhone.isWifiCallingEnabled();
2961 }
2962
2963 /*
2964 * {@hide}
2965 * Returns the IMS Registration Status
2966 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002967 public boolean isVolteAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07002968 return mPhone.isVolteEnabled();
2969 }
Svet Ganovb320e182015-04-16 12:30:10 -07002970
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002971 /*
2972 * {@hide} Returns the IMS Registration Status
2973 */
2974 public boolean isVideoTelephonyAvailable() {
2975 return mPhone.isVideoEnabled();
2976 }
2977
Svet Ganovb320e182015-04-16 12:30:10 -07002978 private boolean canReadPhoneState(String callingPackage, String message) {
Etan Cohen921655c2015-06-24 13:54:50 -07002979 try {
Amit Mahajan83ea23b2015-07-30 16:05:11 -07002980 mApp.enforceCallingOrSelfPermission(
Etan Cohen921655c2015-06-24 13:54:50 -07002981 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE, message);
2982
Amit Mahajan83ea23b2015-07-30 16:05:11 -07002983 // SKIP checking for run-time permission since caller or self has PRIVILEDGED permission
Etan Cohen921655c2015-06-24 13:54:50 -07002984 return true;
Amit Mahajan785783f2015-06-29 10:36:50 -07002985 } catch (SecurityException e) {
2986 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
2987 message);
Etan Cohen921655c2015-06-24 13:54:50 -07002988 }
Svet Ganovb320e182015-04-16 12:30:10 -07002989
2990 if (mAppOps.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(),
2991 callingPackage) != AppOpsManager.MODE_ALLOWED) {
2992 return false;
2993 }
2994
2995 return true;
2996 }
Stuart Scott8eef64f2015-04-08 15:13:54 -07002997
Makoto Onukifee69342015-06-29 14:44:50 -07002998 /**
Amit Mahajanb9b49782015-09-15 18:16:32 -07002999 * Besides READ_PHONE_STATE, WRITE_SMS and READ_SMS also allow apps to get phone numbers.
Makoto Onukifee69342015-06-29 14:44:50 -07003000 */
3001 private boolean canReadPhoneNumber(String callingPackage, String message) {
Makoto Onukie4072d12015-08-03 15:12:23 -07003002 // Default SMS app can always read it.
3003 if (mAppOps.noteOp(AppOpsManager.OP_WRITE_SMS,
3004 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED) {
3005 return true;
3006 }
3007 try {
3008 return canReadPhoneState(callingPackage, message);
Amit Mahajanb9b49782015-09-15 18:16:32 -07003009 } catch (SecurityException readPhoneStateSecurityException) {
3010 try {
3011 // Can be read with READ_SMS too.
3012 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_SMS, message);
3013 return mAppOps.noteOp(AppOpsManager.OP_READ_SMS,
3014 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED;
3015 } catch (SecurityException readSmsSecurityException) {
3016 // Throw exception with message including both READ_PHONE_STATE and READ_SMS
3017 // permissions
3018 throw new SecurityException(message + ": Neither user " + Binder.getCallingUid() +
3019 " nor current process has " + android.Manifest.permission.READ_PHONE_STATE +
3020 " or " + android.Manifest.permission.READ_SMS + ".");
3021 }
Makoto Onukie4072d12015-08-03 15:12:23 -07003022 }
Makoto Onukifee69342015-06-29 14:44:50 -07003023 }
3024
Stuart Scott8eef64f2015-04-08 15:13:54 -07003025 @Override
3026 public void factoryReset(int subId) {
3027 enforceConnectivityInternalPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07003028 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
3029 return;
3030 }
3031
Svet Ganovcc087f82015-05-12 20:35:54 -07003032 final long identity = Binder.clearCallingIdentity();
3033 try {
Stuart Scott981d8582015-04-21 14:09:50 -07003034 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
3035 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Svet Ganovcc087f82015-05-12 20:35:54 -07003036 // Enable data
3037 setDataEnabled(subId, true);
3038 // Set network selection mode to automatic
3039 setNetworkSelectionModeAutomatic(subId);
3040 // Set preferred mobile network type to the best available
3041 setPreferredNetworkType(subId, Phone.PREFERRED_NT_MODE);
3042 // Turn off roaming
3043 SubscriptionManager.from(mApp).setDataRoaming(0, subId);
3044 }
3045 } finally {
3046 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07003047 }
3048 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003049
3050 @Override
3051 public String getLocaleFromDefaultSim() {
3052 // We query all subscriptions instead of just the active ones, because
3053 // this might be called early on in the provisioning flow when the
3054 // subscriptions potentially aren't active yet.
3055 final List<SubscriptionInfo> slist = getAllSubscriptionInfoList();
3056 if (slist == null || slist.isEmpty()) {
3057 return null;
3058 }
3059
3060 // This function may be called very early, say, from the setup wizard, at
3061 // which point we won't have a default subscription set. If that's the case
3062 // we just choose the first, which will be valid in "most cases".
3063 final int defaultSubId = getDefaultSubscription();
3064 SubscriptionInfo info = null;
3065 if (defaultSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
3066 info = slist.get(0);
3067 } else {
3068 for (SubscriptionInfo item : slist) {
3069 if (item.getSubscriptionId() == defaultSubId) {
3070 info = item;
3071 break;
3072 }
3073 }
3074
3075 if (info == null) {
3076 return null;
3077 }
3078 }
3079
3080 // Try and fetch the locale from the carrier properties or from the SIM language
3081 // preferences (EF-PL and EF-LI)...
Tony Hill183b2de2015-06-24 14:53:58 +01003082 final int mcc = info.getMcc();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003083 final Phone defaultPhone = getPhone(info.getSubscriptionId());
Narayan Kamath011676f2015-07-29 12:04:08 +01003084 String simLanguage = null;
Narayan Kamath1c496c22015-04-16 14:40:19 +01003085 if (defaultPhone != null) {
3086 final Locale localeFromDefaultSim = defaultPhone.getLocaleFromSimAndCarrierPrefs();
3087 if (localeFromDefaultSim != null) {
Narayan Kamath011676f2015-07-29 12:04:08 +01003088 if (!localeFromDefaultSim.getCountry().isEmpty()) {
Tony Hill183b2de2015-06-24 14:53:58 +01003089 if (DBG) log("Using locale from default SIM:" + localeFromDefaultSim);
3090 return localeFromDefaultSim.toLanguageTag();
Narayan Kamath011676f2015-07-29 12:04:08 +01003091 } else {
3092 simLanguage = localeFromDefaultSim.getLanguage();
Tony Hill183b2de2015-06-24 14:53:58 +01003093 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003094 }
3095 }
3096
Narayan Kamath011676f2015-07-29 12:04:08 +01003097 // The SIM language preferences only store a language (e.g. fr = French), not an
3098 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
3099 // the SIM and carrier preferences does not include a country we add the country
3100 // determined from the SIM MCC to provide an exact locale.
3101 final Locale mccLocale = MccTable.getLocaleFromMcc(mPhone.getContext(), mcc, simLanguage);
Tony Hill183b2de2015-06-24 14:53:58 +01003102 if (mccLocale != null) {
3103 if (DBG) log("No locale from default SIM, using mcc locale:" + mccLocale);
3104 return mccLocale.toLanguageTag();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003105 }
3106
Tony Hill183b2de2015-06-24 14:53:58 +01003107 if (DBG) log("No locale found - returning null");
Narayan Kamath1c496c22015-04-16 14:40:19 +01003108 return null;
3109 }
3110
3111 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
3112 final long identity = Binder.clearCallingIdentity();
3113 try {
3114 return mSubscriptionController.getAllSubInfoList(
3115 mPhone.getContext().getOpPackageName());
3116 } finally {
3117 Binder.restoreCallingIdentity(identity);
3118 }
3119 }
3120
3121 private List<SubscriptionInfo> getActiveSubscriptionInfoList() {
3122 final long identity = Binder.clearCallingIdentity();
3123 try {
3124 return mSubscriptionController.getActiveSubscriptionInfoList(
3125 mPhone.getContext().getOpPackageName());
3126 } finally {
3127 Binder.restoreCallingIdentity(identity);
3128 }
3129 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003130
3131 /**
Adam Lesinski903a54c2016-04-11 14:49:52 -07003132 * Responds to the ResultReceiver with the {@link android.telephony.ModemActivityInfo} object
3133 * representing the state of the modem.
3134 *
3135 * NOTE: This clears the modem state, so there should only every be one caller.
3136 * @hide
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003137 */
3138 @Override
Adam Lesinski903a54c2016-04-11 14:49:52 -07003139 public void requestModemActivityInfo(ResultReceiver result) {
3140 enforceModifyPermission();
3141
3142 ModemActivityInfo info = (ModemActivityInfo) sendRequest(CMD_GET_MODEM_ACTIVITY_INFO, null);
3143 Bundle bundle = new Bundle();
3144 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY, info);
3145 result.send(0, bundle);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003146 }
Jack Yu85bd38a2015-11-09 11:34:32 -08003147
3148 /**
3149 * {@hide}
3150 * Returns the service state information on specified subscription.
3151 */
3152 @Override
3153 public ServiceState getServiceStateForSubscriber(int subId, String callingPackage) {
3154
3155 if (!canReadPhoneState(callingPackage, "getServiceStateForSubscriber")) {
3156 return null;
3157 }
3158
3159 final Phone phone = getPhone(subId);
3160 if (phone == null) {
3161 return null;
3162 }
3163
3164 return phone.getServiceState();
3165 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08003166
3167 /**
3168 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
3169 *
3170 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3171 * voicemail ringtone.
3172 * @return The URI for the ringtone to play when receiving a voicemail from a specific
3173 * PhoneAccount.
3174 */
3175 @Override
3176 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
3177 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
3178 if (phone == null) {
3179 return null;
3180 }
3181
3182 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone);
3183 }
3184
3185 /**
3186 * Returns whether vibration is set for voicemail notification in Phone settings.
3187 *
3188 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3189 * voicemail vibration setting.
3190 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
3191 */
3192 @Override
3193 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
3194 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
3195 if (phone == null) {
3196 return false;
3197 }
3198
3199 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone);
3200 }
3201
Youhan Wange64578a2016-05-02 15:32:42 -07003202 /**
3203 * Make sure either called from same process as self (phone) or IPC caller has read privilege.
3204 *
3205 * @throws SecurityException if the caller does not have the required permission
3206 */
3207 private void enforceReadPrivilegedPermission() {
3208 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
3209 null);
3210 }
3211
3212 /**
3213 * Return the application ID for the app type.
3214 *
3215 * @param subId the subscription ID that this request applies to.
3216 * @param appType the uicc app type.
3217 * @return Application ID for specificied app type, or null if no uicc.
3218 */
3219 @Override
3220 public String getAidForAppType(int subId, int appType) {
3221 enforceReadPrivilegedPermission();
3222 Phone phone = getPhone(subId);
3223 if (phone == null) {
3224 return null;
3225 }
3226 String aid = null;
3227 try {
3228 aid = UiccController.getInstance().getUiccCard(phone.getPhoneId())
3229 .getApplicationByType(appType).getAid();
3230 } catch (Exception e) {
3231 Log.e(LOG_TAG, "Not getting aid. Exception ex=" + e);
3232 }
3233 return aid;
3234 }
3235
Youhan Wang4001d252016-05-11 10:29:41 -07003236 /**
3237 * Return the Electronic Serial Number.
3238 *
3239 * @param subId the subscription ID that this request applies to.
3240 * @return ESN or null if error.
3241 */
3242 @Override
3243 public String getEsn(int subId) {
3244 enforceReadPrivilegedPermission();
3245 Phone phone = getPhone(subId);
3246 if (phone == null) {
3247 return null;
3248 }
3249 String esn = null;
3250 try {
3251 esn = phone.getEsn();
3252 } catch (Exception e) {
3253 Log.e(LOG_TAG, "Not getting ESN. Exception ex=" + e);
3254 }
3255 return esn;
3256 }
3257
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003258 /**
Youhan Wang66ad5d72016-07-18 17:56:58 -07003259 * Return the Preferred Roaming List Version.
3260 *
3261 * @param subId the subscription ID that this request applies to.
3262 * @return PRLVersion or null if error.
3263 */
3264 @Override
3265 public String getCdmaPrlVersion(int subId) {
3266 enforceReadPrivilegedPermission();
3267 Phone phone = getPhone(subId);
3268 if (phone == null) {
3269 return null;
3270 }
3271 String cdmaPrlVersion = null;
3272 try {
3273 cdmaPrlVersion = phone.getCdmaPrlVersion();
3274 } catch (Exception e) {
3275 Log.e(LOG_TAG, "Not getting PRLVersion", e);
3276 }
3277 return cdmaPrlVersion;
3278 }
3279
3280 /**
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003281 * Get snapshot of Telephony histograms
3282 * @return List of Telephony histograms
3283 * @hide
3284 */
3285 @Override
3286 public List<TelephonyHistogram> getTelephonyHistograms() {
3287 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
3288 return RIL.getTelephonyRILTimingHistograms();
3289 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07003290
3291 /**
3292 * {@hide}
3293 * Set the allowed carrier list for slotId
3294 * Require system privileges. In the future we may add this to carrier APIs.
3295 *
3296 * @return The number of carriers set successfully, should match length of carriers
3297 */
3298 @Override
3299 public int setAllowedCarriers(int slotId, List<CarrierIdentifier> carriers) {
3300 enforceModifyPermission();
3301 int subId = SubscriptionManager.getSubId(slotId)[0];
3302 int[] retVal = (int[]) sendRequest(CMD_SET_ALLOWED_CARRIERS, carriers, subId);
3303 return retVal[0];
3304 }
3305
3306 /**
3307 * {@hide}
3308 * Get the allowed carrier list for slotId.
3309 * Require system privileges. In the future we may add this to carrier APIs.
3310 *
3311 * @return List of {@link android.service.telephony.CarrierIdentifier}; empty list
3312 * means all carriers are allowed.
3313 */
3314 @Override
3315 public List<CarrierIdentifier> getAllowedCarriers(int slotId) {
3316 enforceReadPrivilegedPermission();
3317 int subId = SubscriptionManager.getSubId(slotId)[0];
3318 return (List<CarrierIdentifier>) sendRequest(CMD_GET_ALLOWED_CARRIERS, null, subId);
3319 }
3320
fionaxu59545b42016-05-25 15:53:37 -07003321 /**
3322 * Action set from carrier signalling broadcast receivers to enable/disable metered apns
3323 * @param subId the subscription ID that this action applies to.
3324 * @param enabled control enable or disable metered apns.
3325 * {@hide}
3326 */
3327 @Override
3328 public void carrierActionSetMeteredApnsEnabled(int subId, boolean enabled) {
3329 enforceModifyPermission();
3330 final Phone phone = getPhone(subId);
3331 if (phone == null) {
3332 loge("carrierAction: SetMeteredApnsEnabled fails with invalid subId: " + subId);
3333 return;
3334 }
3335 try {
3336 phone.carrierActionSetMeteredApnsEnabled(enabled);
3337 } catch (Exception e) {
3338 Log.e(LOG_TAG, "carrierAction: SetMeteredApnsEnabled fails. Exception ex=" + e);
3339 }
3340 }
3341
3342 /**
3343 * Action set from carrier signalling broadcast receivers to enable/disable radio
3344 * @param subId the subscription ID that this action applies to.
3345 * @param enabled control enable or disable radio.
3346 * {@hide}
3347 */
3348 @Override
3349 public void carrierActionSetRadioEnabled(int subId, boolean enabled) {
3350 enforceModifyPermission();
3351 final Phone phone = getPhone(subId);
3352 if (phone == null) {
3353 loge("carrierAction: SetRadioEnabled fails with invalid sibId: " + subId);
3354 return;
3355 }
3356 try {
3357 phone.carrierActionSetRadioEnabled(enabled);
3358 } catch (Exception e) {
3359 Log.e(LOG_TAG, "carrierAction: SetRadioEnabled fails. Exception ex=" + e);
3360 }
3361 }
3362
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003363 /**
3364 * Called when "adb shell dumpsys phone" is invoked. Dump is also automatically invoked when a
3365 * bug report is being generated.
3366 */
3367 @Override
Ta-wei Yen99282e02016-06-21 18:19:35 -07003368 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
dcashman22b950d2016-06-27 11:39:02 -07003369 if (mPhone.getContext().checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3370 != PackageManager.PERMISSION_GRANTED) {
3371 writer.println("Permission Denial: can't dump Phone from pid="
3372 + Binder.getCallingPid()
3373 + ", uid=" + Binder.getCallingUid()
3374 + "without permission "
3375 + android.Manifest.permission.DUMP);
3376 return;
3377 }
Ta-wei Yen99282e02016-06-21 18:19:35 -07003378 DumpsysHandler.dump(mPhone.getContext(), fd, writer, args);
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003379 }
Jack Yueb89b242016-06-22 13:27:47 -07003380
3381 /**
3382 * Get aggregated video call data usage from all subscriptions since boot.
3383 * @return total data usage in bytes
3384 * {@hide}
3385 */
3386 @Override
3387 public long getVtDataUsage() {
3388 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_NETWORK_USAGE_HISTORY,
3389 null);
3390
3391 // NetworkStatsService keeps tracking the active network interface and identity. It will
3392 // record the delta with the corresponding network identity. What we need to do here is
3393 // returning total video call data usage from all subscriptions since boot.
3394
3395 // TODO: Add sub id support in the future. We'll need it when we support DSDA and
3396 // simultaneous VT calls.
3397 final Phone[] phones = PhoneFactory.getPhones();
3398 long total = 0;
3399 for (Phone phone : phones) {
3400 total += phone.getVtDataUsage();
3401 }
3402 return total;
3403 }
Jack Yu75ab2952016-07-08 14:29:33 -07003404
3405 /**
3406 * Policy control of data connection. Usually used when data limit is passed.
3407 * @param enabled True if enabling the data, otherwise disabling.
3408 * @param subId Subscription index
3409 * {@hide}
3410 */
3411 @Override
3412 public void setPolicyDataEnabled(boolean enabled, int subId) {
3413 enforceModifyPermission();
3414 Phone phone = getPhone(subId);
3415 if (phone != null) {
3416 phone.setPolicyDataEnabled(enabled);
3417 }
3418 }
Sooraj Sasindran22882212016-07-18 11:57:25 -07003419
3420 /**
3421 * Get Client request stats
3422 * @return List of Client Request Stats
3423 * @hide
3424 */
3425 @Override
3426 public List<ClientRequestStats> getClientRequestStats(String callingPackage, int subId) {
3427 if (!canReadPhoneState(callingPackage, "getClientRequestStats")) {
3428 return null;
3429 }
3430
3431 Phone phone = getPhone(subId);
3432 if (phone != null) {
3433 return phone.getClientRequestStats();
3434 }
3435
3436 return null;
3437 }
3438
3439 private WorkSource getWorkSource(WorkSource workSource, int uid) {
3440 if (workSource != null) {
3441 return workSource;
3442 }
3443
3444 String packageName = mPhone.getContext().getPackageManager().getNameForUid(uid);
3445 workSource = new WorkSource(uid, packageName);
3446 return workSource;
3447 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003448}