blob: 4bc94b34c304a9c19107cd3a112929521556df8d [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 Sasindran600a37a2016-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 Sasindran600a37a2016-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 Sasindran600a37a2016-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 Sasindran600a37a2016-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 Sasindran600a37a2016-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 Sasindran600a37a2016-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 Sasindran600a37a2016-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 Sasindran600a37a2016-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) {
Sooraj Sasindran600a37a2016-07-18 11:57:25 -07001651 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
1652 mPhone.setCellInfoListRate(rateInMillis, workSource);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001653 }
1654
Shishir Agrawala9f32182016-04-12 12:00:16 -07001655 @Override
1656 public String getImeiForSlot(int slotId, String callingPackage) {
1657 if (!canReadPhoneState(callingPackage, "getImeiForSlot")) {
1658 return null;
1659 }
1660 Phone phone = PhoneFactory.getPhone(slotId);
1661 return phone == null ? null : phone.getImei();
1662 }
1663
1664 @Override
1665 public String getDeviceSoftwareVersionForSlot(int slotId, String callingPackage) {
1666 if (!canReadPhoneState(callingPackage, "getDeviceSoftwareVersionForSlot")) {
1667 return null;
1668 }
1669 Phone phone = PhoneFactory.getPhone(slotId);
1670 return phone == null ? null : phone.getDeviceSvn();
1671 }
1672
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001673 //
1674 // Internal helper methods.
1675 //
1676
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001677 /**
1678 * Returns true if the caller holds INTERACT_ACROSS_USERS_FULL.
1679 */
1680 private boolean checkCallerInteractAcrossUsersFull() {
1681 return mPhone.getContext().checkCallingOrSelfPermission(
1682 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1683 == PackageManager.PERMISSION_GRANTED;
1684 }
1685
Jake Hambye994d462014-02-03 13:10:13 -08001686 private static boolean checkIfCallerIsSelfOrForegroundUser() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001687 boolean ok;
1688
1689 boolean self = Binder.getCallingUid() == Process.myUid();
1690 if (!self) {
1691 // Get the caller's user id then clear the calling identity
1692 // which will be restored in the finally clause.
1693 int callingUser = UserHandle.getCallingUserId();
1694 long ident = Binder.clearCallingIdentity();
1695
1696 try {
1697 // With calling identity cleared the current user is the foreground user.
1698 int foregroundUser = ActivityManager.getCurrentUser();
1699 ok = (foregroundUser == callingUser);
1700 if (DBG_LOC) {
1701 log("checkIfCallerIsSelfOrForegoundUser: foregroundUser=" + foregroundUser
1702 + " callingUser=" + callingUser + " ok=" + ok);
1703 }
1704 } catch (Exception ex) {
1705 if (DBG_LOC) loge("checkIfCallerIsSelfOrForegoundUser: Exception ex=" + ex);
1706 ok = false;
1707 } finally {
1708 Binder.restoreCallingIdentity(ident);
1709 }
1710 } else {
1711 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: is self");
1712 ok = true;
1713 }
1714 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: ret=" + ok);
1715 return ok;
1716 }
1717
1718 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001719 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1720 *
1721 * @throws SecurityException if the caller does not have the required permission
1722 */
1723 private void enforceModifyPermission() {
1724 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1725 }
1726
1727 /**
Junda Liua2e36012014-07-09 18:30:01 -07001728 * Make sure either system app or the caller has carrier privilege.
1729 *
1730 * @throws SecurityException if the caller does not have the required permission/privilege
1731 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001732 private void enforceModifyPermissionOrCarrierPrivilege(int subId) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001733 int permission = mApp.checkCallingOrSelfPermission(
1734 android.Manifest.permission.MODIFY_PHONE_STATE);
1735 if (permission == PackageManager.PERMISSION_GRANTED) {
1736 return;
1737 }
1738
1739 log("No modify permission, check carrier privilege next.");
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001740 enforceCarrierPrivilege(subId);
Junda Liua2e36012014-07-09 18:30:01 -07001741 }
1742
1743 /**
1744 * Make sure the caller has carrier privilege.
1745 *
1746 * @throws SecurityException if the caller does not have the required permission
1747 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001748 private void enforceCarrierPrivilege(int subId) {
1749 if (getCarrierPrivilegeStatus(subId) !=
1750 TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001751 loge("No Carrier Privilege.");
1752 throw new SecurityException("No Carrier Privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001753 }
1754 }
1755
1756 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001757 * Make sure the caller has the CALL_PHONE permission.
1758 *
1759 * @throws SecurityException if the caller does not have the required permission
1760 */
1761 private void enforceCallPermission() {
1762 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1763 }
1764
Stuart Scott8eef64f2015-04-08 15:13:54 -07001765 private void enforceConnectivityInternalPermission() {
1766 mApp.enforceCallingOrSelfPermission(
1767 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1768 "ConnectivityService");
1769 }
1770
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001771 private String createTelUrl(String number) {
1772 if (TextUtils.isEmpty(number)) {
1773 return null;
1774 }
1775
Jake Hambye994d462014-02-03 13:10:13 -08001776 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001777 }
1778
Ihab Awadf9e92732013-12-05 18:02:52 -08001779 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001780 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1781 }
1782
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001783 private static void logv(String msg) {
1784 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
1785 }
1786
Ihab Awadf9e92732013-12-05 18:02:52 -08001787 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001788 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1789 }
1790
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001791 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001792 public int getActivePhoneType() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001793 return getActivePhoneTypeForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001794 }
1795
Sanket Padawe356d7632015-06-22 14:03:32 -07001796 @Override
Shishir Agrawala9f32182016-04-12 12:00:16 -07001797 public int getActivePhoneTypeForSlot(int slotId) {
1798 final Phone phone = PhoneFactory.getPhone(slotId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001799 if (phone == null) {
1800 return PhoneConstants.PHONE_TYPE_NONE;
1801 } else {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001802 return phone.getPhoneType();
Sanket Padawe356d7632015-06-22 14:03:32 -07001803 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001804 }
1805
1806 /**
1807 * Returns the CDMA ERI icon index to display
1808 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001809 @Override
1810 public int getCdmaEriIconIndex(String callingPackage) {
1811 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001812 }
1813
Sanket Padawe356d7632015-06-22 14:03:32 -07001814 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001815 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage) {
1816 if (!canReadPhoneState(callingPackage, "getCdmaEriIconIndexForSubscriber")) {
1817 return -1;
1818 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001819 final Phone phone = getPhone(subId);
1820 if (phone != null) {
1821 return phone.getCdmaEriIconIndex();
1822 } else {
1823 return -1;
1824 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001825 }
1826
1827 /**
1828 * Returns the CDMA ERI icon mode,
1829 * 0 - ON
1830 * 1 - FLASHING
1831 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001832 @Override
1833 public int getCdmaEriIconMode(String callingPackage) {
1834 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001835 }
1836
Sanket Padawe356d7632015-06-22 14:03:32 -07001837 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001838 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage) {
1839 if (!canReadPhoneState(callingPackage, "getCdmaEriIconModeForSubscriber")) {
1840 return -1;
1841 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001842 final Phone phone = getPhone(subId);
1843 if (phone != null) {
1844 return phone.getCdmaEriIconMode();
1845 } else {
1846 return -1;
1847 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001848 }
1849
1850 /**
1851 * Returns the CDMA ERI text,
1852 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001853 @Override
1854 public String getCdmaEriText(String callingPackage) {
1855 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001856 }
1857
Sanket Padawe356d7632015-06-22 14:03:32 -07001858 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001859 public String getCdmaEriTextForSubscriber(int subId, String callingPackage) {
1860 if (!canReadPhoneState(callingPackage, "getCdmaEriIconTextForSubscriber")) {
1861 return null;
1862 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001863 final Phone phone = getPhone(subId);
1864 if (phone != null) {
1865 return phone.getCdmaEriText();
1866 } else {
1867 return null;
1868 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001869 }
1870
1871 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07001872 * Returns the CDMA MDN.
1873 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001874 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001875 public String getCdmaMdn(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001876 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001877 final Phone phone = getPhone(subId);
1878 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA && phone != null) {
1879 return phone.getLine1Number();
Junda Liuca05d5d2014-08-14 22:36:34 -07001880 } else {
1881 return null;
1882 }
1883 }
1884
1885 /**
1886 * Returns the CDMA MIN.
1887 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001888 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001889 public String getCdmaMin(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001890 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001891 final Phone phone = getPhone(subId);
1892 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1893 return phone.getCdmaMin();
Junda Liuca05d5d2014-08-14 22:36:34 -07001894 } else {
1895 return null;
1896 }
1897 }
1898
1899 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001900 * Returns true if CDMA provisioning needs to run.
1901 */
1902 public boolean needsOtaServiceProvisioning() {
1903 return mPhone.needsOtaServiceProvisioning();
1904 }
1905
1906 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001907 * Sets the voice mail number of a given subId.
1908 */
1909 @Override
1910 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001911 enforceCarrierPrivilege(subId);
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001912 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
1913 new Pair<String, String>(alphaTag, number), new Integer(subId));
1914 return success;
1915 }
1916
Ta-wei Yen87c49842016-05-13 21:19:52 -07001917 @Override
Ta-wei Yenb0f695b2016-08-08 17:33:11 -07001918 public void setVisualVoicemailEnabled(String callingPackage,
1919 PhoneAccountHandle phoneAccountHandle, boolean enabled) {
1920 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
1921 if (!TextUtils.equals(callingPackage,
1922 TelecomManager.from(mPhone.getContext()).getDefaultDialerPackage())) {
1923 enforceModifyPermissionOrCarrierPrivilege(
1924 PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle));
1925 }
1926 VisualVoicemailSettingsUtil.setEnabled(mPhone.getContext(), phoneAccountHandle, enabled);
1927 }
1928
1929 @Override
1930 public boolean isVisualVoicemailEnabled(String callingPackage,
1931 PhoneAccountHandle phoneAccountHandle) {
1932 if (!canReadPhoneState(callingPackage, "isVisualVoicemailEnabled")) {
1933 return false;
1934 }
1935 return VisualVoicemailSettingsUtil.isEnabled(mPhone.getContext(), phoneAccountHandle);
1936 }
1937
1938 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07001939 public void enableVisualVoicemailSmsFilter(String callingPackage, int subId,
1940 VisualVoicemailSmsFilterSettings settings) {
1941 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001942 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07001943 .enableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId,
1944 settings);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001945 }
1946
1947 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07001948 public void disableVisualVoicemailSmsFilter(String callingPackage, int subId) {
1949 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001950 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07001951 .disableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001952 }
1953
1954 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07001955 public VisualVoicemailSmsFilterSettings getVisualVoicemailSmsFilterSettings(
1956 String callingPackage, int subId) {
1957 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001958 return VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07001959 .getVisualVoicemailSmsFilterSettings(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001960 }
1961
1962 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07001963 public VisualVoicemailSmsFilterSettings getSystemVisualVoicemailSmsFilterSettings(
1964 String packageName, int subId) {
1965 enforceReadPrivilegedPermission();
Ta-wei Yen87c49842016-05-13 21:19:52 -07001966 return VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07001967 .getVisualVoicemailSmsFilterSettings(mPhone.getContext(), packageName, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001968 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001969 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001970 * Returns the unread count of voicemails
1971 */
1972 public int getVoiceMessageCount() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001973 return getVoiceMessageCountForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001974 }
1975
1976 /**
1977 * Returns the unread count of voicemails for a subId
1978 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001979 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001980 public int getVoiceMessageCountForSubscriber( int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001981 final Phone phone = getPhone(subId);
1982 if (phone != null) {
1983 return phone.getVoiceMessageCount();
1984 } else {
1985 return 0;
1986 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001987 }
1988
1989 /**
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001990 * Returns the data network type.
1991 * Legacy call, permission-free.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001992 *
1993 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
1994 */
1995 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001996 public int getNetworkType() {
1997 final Phone phone = getPhone(getDefaultSubscription());
1998 if (phone != null) {
1999 return phone.getServiceState().getDataNetworkType();
2000 } else {
2001 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2002 }
Wink Saville36469e72014-06-11 15:17:00 -07002003 }
2004
2005 /**
2006 * Returns the network type for a subId
2007 */
2008 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002009 public int getNetworkTypeForSubscriber(int subId, String callingPackage) {
2010 if (!canReadPhoneState(callingPackage, "getNetworkTypeForSubscriber")) {
2011 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2012 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002013
Sanket Padawe356d7632015-06-22 14:03:32 -07002014 final Phone phone = getPhone(subId);
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002015 if (phone != null) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002016 return phone.getServiceState().getDataNetworkType();
2017 } else {
2018 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2019 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002020 }
2021
2022 /**
2023 * Returns the data network type
2024 */
2025 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002026 public int getDataNetworkType(String callingPackage) {
2027 return getDataNetworkTypeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002028 }
2029
2030 /**
2031 * Returns the data network type for a subId
2032 */
2033 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002034 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage) {
2035 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2036 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2037 }
2038
Sanket Padawe356d7632015-06-22 14:03:32 -07002039 final Phone phone = getPhone(subId);
2040 if (phone != null) {
2041 return phone.getServiceState().getDataNetworkType();
2042 } else {
2043 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2044 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002045 }
2046
2047 /**
Wink Saville36469e72014-06-11 15:17:00 -07002048 * Returns the Voice network type for a subId
2049 */
2050 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002051 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage) {
2052 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2053 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2054 }
2055
Sanket Padawe356d7632015-06-22 14:03:32 -07002056 final Phone phone = getPhone(subId);
2057 if (phone != null) {
2058 return phone.getServiceState().getVoiceNetworkType();
2059 } else {
2060 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2061 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002062 }
2063
2064 /**
2065 * @return true if a ICC card is present
2066 */
2067 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07002068 // FIXME Make changes to pass defaultSimId of type int
Amit Mahajana6fc2a82015-01-06 11:53:51 -08002069 return hasIccCardUsingSlotId(mSubscriptionController.getSlotId(getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07002070 }
2071
2072 /**
2073 * @return true if a ICC card is present for a slotId
2074 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002075 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002076 public boolean hasIccCardUsingSlotId(int slotId) {
Amit Mahajana6fc2a82015-01-06 11:53:51 -08002077 int subId[] = mSubscriptionController.getSubIdUsingSlotId(slotId);
Sanket Padawe356d7632015-06-22 14:03:32 -07002078 final Phone phone = getPhone(subId[0]);
2079 if (subId != null && phone != null) {
2080 return phone.getIccCard().hasIccCard();
Amit Mahajana6fc2a82015-01-06 11:53:51 -08002081 } else {
2082 return false;
2083 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002084 }
2085
2086 /**
2087 * Return if the current radio is LTE on CDMA. This
2088 * is a tri-state return value as for a period of time
2089 * the mode may be unknown.
2090 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002091 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002092 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08002093 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002094 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002095 @Override
2096 public int getLteOnCdmaMode(String callingPackage) {
2097 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002098 }
2099
Sanket Padawe356d7632015-06-22 14:03:32 -07002100 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002101 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage) {
2102 if (!canReadPhoneState(callingPackage, "getLteOnCdmaModeForSubscriber")) {
2103 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2104 }
2105
Sanket Padawe356d7632015-06-22 14:03:32 -07002106 final Phone phone = getPhone(subId);
2107 if (phone == null) {
2108 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2109 } else {
2110 return phone.getLteOnCdmaMode();
2111 }
Wink Saville36469e72014-06-11 15:17:00 -07002112 }
2113
2114 public void setPhone(Phone phone) {
2115 mPhone = phone;
2116 }
2117
2118 /**
2119 * {@hide}
2120 * Returns Default subId, 0 in the case of single standby.
2121 */
Wink Savilleb564aae2014-10-23 10:18:09 -07002122 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002123 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07002124 }
2125
Shishir Agrawala9f32182016-04-12 12:00:16 -07002126 private int getSlotForDefaultSubscription() {
2127 return mSubscriptionController.getPhoneId(getDefaultSubscription());
2128 }
2129
Wink Savilleb564aae2014-10-23 10:18:09 -07002130 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002131 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002132 }
Ihab Awadf2177b72013-11-25 13:33:23 -08002133
2134 /**
2135 * @see android.telephony.TelephonyManager.WifiCallingChoices
2136 */
2137 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002138 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
2139 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08002140 }
2141
2142 /**
2143 * @see android.telephony.TelephonyManager.WifiCallingChoices
2144 */
2145 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002146 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
2147 Settings.System.putInt(mPhone.getContext().getContentResolver(),
2148 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08002149 }
2150
Sailesh Nepald1e68152013-12-12 19:08:02 -08002151 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07002152 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08002153 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08002154 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08002155
Shishir Agrawal566b7612013-10-28 14:41:00 -07002156 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002157 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(int subId, String AID) {
2158 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002159
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002160 if (DBG) log("iccOpenLogicalChannel: subId=" + subId + " aid=" + AID);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002161 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002162 CMD_OPEN_CHANNEL, AID, subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002163 if (DBG) log("iccOpenLogicalChannel: " + response);
2164 return response;
Shishir Agrawal566b7612013-10-28 14:41:00 -07002165 }
2166
2167 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002168 public boolean iccCloseLogicalChannel(int subId, int channel) {
2169 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002170
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002171 if (DBG) log("iccCloseLogicalChannel: subId=" + subId + " chnl=" + channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002172 if (channel < 0) {
2173 return false;
2174 }
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002175 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel, subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002176 if (DBG) log("iccCloseLogicalChannel: " + success);
2177 return success;
2178 }
2179
2180 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002181 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07002182 int command, int p1, int p2, int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002183 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002184
2185 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002186 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel +
2187 " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
Shishir Agrawal566b7612013-10-28 14:41:00 -07002188 " data=" + data);
2189 }
2190
2191 if (channel < 0) {
2192 return "";
2193 }
2194
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002195 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002196 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002197 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
2198
Shishir Agrawal566b7612013-10-28 14:41:00 -07002199 // Append the returned status code to the end of the response payload.
2200 String s = Integer.toHexString(
2201 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002202 if (response.payload != null) {
2203 s = IccUtils.bytesToHexString(response.payload) + s;
2204 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07002205 return s;
2206 }
Jake Hambye994d462014-02-03 13:10:13 -08002207
Evan Charltonc66da362014-05-16 14:06:40 -07002208 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002209 public String iccTransmitApduBasicChannel(int subId, int cla, int command, int p1, int p2,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002210 int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002211 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002212
2213 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002214 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd=" + command
2215 + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002216 }
2217
2218 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002219 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002220 if (DBG) log("iccTransmitApduBasicChannel: " + response);
2221
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002222 // Append the returned status code to the end of the response payload.
2223 String s = Integer.toHexString(
2224 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002225 if (response.payload != null) {
2226 s = IccUtils.bytesToHexString(response.payload) + s;
2227 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002228 return s;
2229 }
2230
2231 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002232 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002233 String filePath) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002234 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002235
2236 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002237 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " " +
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002238 p1 + " " + p2 + " " + p3 + ":" + filePath);
2239 }
2240
2241 IccIoResult response =
2242 (IccIoResult)sendRequest(CMD_EXCHANGE_SIM_IO,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002243 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
2244 subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002245
2246 if (DBG) {
2247 log("Exchange SIM_IO [R]" + response);
2248 }
2249
2250 byte[] result = null;
2251 int length = 2;
2252 if (response.payload != null) {
2253 length = 2 + response.payload.length;
2254 result = new byte[length];
2255 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
2256 } else {
2257 result = new byte[length];
2258 }
2259
2260 result[length - 1] = (byte) response.sw2;
2261 result[length - 2] = (byte) response.sw1;
2262 return result;
2263 }
2264
2265 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002266 public String sendEnvelopeWithStatus(int subId, String content) {
2267 enforceModifyPermissionOrCarrierPrivilege(subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002268
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002269 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content, subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002270 if (response.payload == null) {
2271 return "";
2272 }
2273
2274 // Append the returned status code to the end of the response payload.
2275 String s = Integer.toHexString(
2276 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
2277 s = IccUtils.bytesToHexString(response.payload) + s;
2278 return s;
2279 }
2280
Jake Hambye994d462014-02-03 13:10:13 -08002281 /**
2282 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2283 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2284 *
2285 * @param itemID the ID of the item to read
2286 * @return the NV item as a String, or null on error.
2287 */
2288 @Override
2289 public String nvReadItem(int itemID) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002290 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002291 if (DBG) log("nvReadItem: item " + itemID);
2292 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
2293 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
2294 return value;
2295 }
2296
2297 /**
2298 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2299 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2300 *
2301 * @param itemID the ID of the item to read
2302 * @param itemValue the value to write, as a String
2303 * @return true on success; false on any failure
2304 */
2305 @Override
2306 public boolean nvWriteItem(int itemID, String itemValue) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002307 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002308 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
2309 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
2310 new Pair<Integer, String>(itemID, itemValue));
2311 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
2312 return success;
2313 }
2314
2315 /**
2316 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
2317 * Used for device configuration by some CDMA operators.
2318 *
2319 * @param preferredRoamingList byte array containing the new PRL
2320 * @return true on success; false on any failure
2321 */
2322 @Override
2323 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002324 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002325 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
2326 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
2327 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
2328 return success;
2329 }
2330
2331 /**
2332 * Perform the specified type of NV config reset.
2333 * Used for device configuration by some CDMA operators.
2334 *
2335 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
2336 * @return true on success; false on any failure
2337 */
2338 @Override
2339 public boolean nvResetConfig(int resetType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002340 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002341 if (DBG) log("nvResetConfig: type " + resetType);
2342 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
2343 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
2344 return success;
2345 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002346
2347 /**
Wink Saville36469e72014-06-11 15:17:00 -07002348 * {@hide}
2349 * Returns Default sim, 0 in the case of single standby.
2350 */
2351 public int getDefaultSim() {
2352 //TODO Need to get it from Telephony Devcontroller
2353 return 0;
2354 }
2355
Svet Ganovb320e182015-04-16 12:30:10 -07002356 public String[] getPcscfAddress(String apnType, String callingPackage) {
2357 if (!canReadPhoneState(callingPackage, "getPcscfAddress")) {
2358 return new String[0];
2359 }
2360
2361
ram87fca6f2014-07-18 18:58:44 +05302362 return mPhone.getPcscfAddress(apnType);
Wink Saville36469e72014-06-11 15:17:00 -07002363 }
2364
Brad Ebinger76681002017-01-23 13:50:20 -08002365 /**
2366 * Returns the {@link IImsServiceController} that corresponds to the given slot Id and IMS
2367 * feature or {@link null} if the service is not available. If an ImsServiceController is
2368 * available, the {@link IImsServiceFeatureListener} callback is registered as a listener for
2369 * feature updates.
2370 */
2371 public IImsServiceController getImsServiceControllerAndListen(int slotId, int feature,
2372 IImsServiceFeatureListener callback) {
2373 enforceModifyPermission();
2374 return PhoneFactory.getImsResolver().getImsServiceControllerAndListen(slotId, feature,
2375 callback);
2376 }
2377
Wink Saville36469e72014-06-11 15:17:00 -07002378 public void setImsRegistrationState(boolean registered) {
2379 enforceModifyPermission();
2380 mPhone.setImsRegistrationState(registered);
2381 }
2382
2383 /**
Stuart Scott54788802015-03-30 13:18:01 -07002384 * Set the network selection mode to automatic.
2385 *
2386 */
2387 @Override
2388 public void setNetworkSelectionModeAutomatic(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002389 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002390 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
2391 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
2392 }
2393
2394 /**
Shishir Agrawal302c8692015-06-19 13:49:39 -07002395 * Set the network selection mode to manual with the selected carrier.
2396 */
2397 @Override
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002398 public boolean setNetworkSelectionModeManual(int subId, OperatorInfo operator,
2399 boolean persistSelection) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002400 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002401 if (DBG) log("setNetworkSelectionModeManual: subId:" + subId + " operator:" + operator);
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002402 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operator,
2403 persistSelection);
2404 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002405 }
2406
2407 /**
2408 * Scans for available networks.
2409 */
2410 @Override
2411 public CellNetworkScanResult getCellNetworkScanResults(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002412 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002413 if (DBG) log("getCellNetworkScanResults: subId " + subId);
2414 CellNetworkScanResult result = (CellNetworkScanResult) sendRequest(
2415 CMD_PERFORM_NETWORK_SCAN, null, subId);
2416 return result;
2417 }
2418
2419 /**
Junda Liu84d15a22014-07-02 11:21:04 -07002420 * Get the calculated preferred network type.
2421 * Used for debugging incorrect network type.
2422 *
2423 * @return the preferred network type, defined in RILConstants.java.
2424 */
2425 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002426 public int getCalculatedPreferredNetworkType(String callingPackage) {
2427 if (!canReadPhoneState(callingPackage, "getCalculatedPreferredNetworkType")) {
2428 return RILConstants.PREFERRED_NETWORK_MODE;
2429 }
2430
Amit Mahajan43330e02014-11-18 11:54:45 -08002431 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext(), 0); // wink FIXME: need to get SubId from somewhere.
Junda Liu84d15a22014-07-02 11:21:04 -07002432 }
2433
2434 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08002435 * Get the preferred network type.
2436 * Used for device configuration by some CDMA operators.
2437 *
2438 * @return the preferred network type, defined in RILConstants.java.
2439 */
2440 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002441 public int getPreferredNetworkType(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002442 enforceModifyPermissionOrCarrierPrivilege(subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002443 if (DBG) log("getPreferredNetworkType");
Stuart Scott54788802015-03-30 13:18:01 -07002444 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002445 int networkType = (result != null ? result[0] : -1);
2446 if (DBG) log("getPreferredNetworkType: " + networkType);
2447 return networkType;
2448 }
2449
2450 /**
2451 * Set the preferred network type.
2452 * Used for device configuration by some CDMA operators.
2453 *
2454 * @param networkType the preferred network type, defined in RILConstants.java.
2455 * @return true on success; false on any failure.
2456 */
2457 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002458 public boolean setPreferredNetworkType(int subId, int networkType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002459 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002460 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
2461 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002462 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
Junda Liu80bc0d12014-07-14 16:36:44 -07002463 if (success) {
2464 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
Stuart Scott54788802015-03-30 13:18:01 -07002465 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
Junda Liu80bc0d12014-07-14 16:36:44 -07002466 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002467 return success;
2468 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002469
2470 /**
Junda Liu475951f2014-11-07 16:45:03 -08002471 * Check TETHER_DUN_REQUIRED and TETHER_DUN_APN settings, net.tethering.noprovisioning
2472 * SystemProperty, and config_tether_apndata to decide whether DUN APN is required for
2473 * tethering.
2474 *
2475 * @return 0: Not required. 1: required. 2: Not set.
2476 * @hide
2477 */
2478 @Override
2479 public int getTetherApnRequired() {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002480 enforceModifyPermission();
Junda Liu475951f2014-11-07 16:45:03 -08002481 int dunRequired = Settings.Global.getInt(mPhone.getContext().getContentResolver(),
2482 Settings.Global.TETHER_DUN_REQUIRED, 2);
2483 // If not set, check net.tethering.noprovisioning, TETHER_DUN_APN setting and
2484 // config_tether_apndata.
2485 if (dunRequired == 2 && mPhone.hasMatchedTetherApnSetting()) {
2486 dunRequired = 1;
2487 }
2488 return dunRequired;
2489 }
2490
2491 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07002492 * Set mobile data enabled
2493 * Used by the user through settings etc to turn on/off mobile data
2494 *
2495 * @param enable {@code true} turn turn data on, else {@code false}
2496 */
2497 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002498 public void setDataEnabled(int subId, boolean enable) {
Jeff Davidsonf7eecf02016-11-18 17:05:56 -08002499 enforceModifyPermissionOrCarrierPrivilege(subId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002500 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002501 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002502 Phone phone = PhoneFactory.getPhone(phoneId);
2503 if (phone != null) {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002504 if (DBG) log("setDataEnabled: subId=" + subId + " enable=" + enable);
Wink Savillee7353bb2014-12-05 14:21:41 -08002505 phone.setDataEnabled(enable);
2506 } else {
2507 loge("setDataEnabled: no phone for subId=" + subId);
2508 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002509 }
2510
2511 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07002512 * Get whether mobile data is enabled.
2513 *
Jeff Davidsonf7eecf02016-11-18 17:05:56 -08002514 * Accepts either ACCESS_NETWORK_STATE, MODIFY_PHONE_STATE or carrier privileges.
Robert Greenwalted86e582014-05-21 20:03:20 -07002515 *
2516 * @return {@code true} if data is enabled else {@code false}
2517 */
2518 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002519 public boolean getDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07002520 try {
2521 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
2522 null);
2523 } catch (Exception e) {
Jeff Davidsonf7eecf02016-11-18 17:05:56 -08002524 enforceModifyPermissionOrCarrierPrivilege(subId);
Robert Greenwalt646120a2014-05-23 11:54:03 -07002525 }
Wink Savillee7353bb2014-12-05 14:21:41 -08002526 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002527 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002528 Phone phone = PhoneFactory.getPhone(phoneId);
2529 if (phone != null) {
2530 boolean retVal = phone.getDataEnabled();
Joe Onoratoa601dd22016-02-23 13:03:53 -08002531 if (DBG) log("getDataEnabled: subId=" + subId + " retVal=" + retVal);
Wink Savillee7353bb2014-12-05 14:21:41 -08002532 return retVal;
2533 } else {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002534 if (DBG) loge("getDataEnabled: no phone subId=" + subId + " retVal=false");
Wink Savillee7353bb2014-12-05 14:21:41 -08002535 return false;
2536 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002537 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002538
2539 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002540 public int getCarrierPrivilegeStatus(int subId) {
2541 final Phone phone = getPhone(subId);
2542 if (phone == null) {
2543 loge("getCarrierPrivilegeStatus: Invalid subId");
2544 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
2545 }
2546 UiccCard card = UiccController.getInstance().getUiccCard(phone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002547 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002548 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002549 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2550 }
2551 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002552 phone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002553 }
Junda Liu29340342014-07-10 15:23:27 -07002554
2555 @Override
Zach Johnson50ecba32015-05-19 00:24:21 -07002556 public int checkCarrierPrivilegesForPackage(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002557 if (TextUtils.isEmpty(pkgName))
2558 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Shishir Agrawal21409252015-01-15 23:33:50 -08002559 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002560 if (card == null) {
2561 loge("checkCarrierPrivilegesForPackage: No UICC");
2562 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2563 }
Zach Johnson50ecba32015-05-19 00:24:21 -07002564 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgName);
2565 }
2566
2567 @Override
2568 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002569 if (TextUtils.isEmpty(pkgName))
2570 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Zach Johnson50ecba32015-05-19 00:24:21 -07002571 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2572 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2573 UiccCard card = UiccController.getInstance().getUiccCard(i);
2574 if (card == null) {
Jonathan Basseri7d320df2015-06-16 12:17:08 -07002575 // No UICC in that slot.
Zach Johnson50ecba32015-05-19 00:24:21 -07002576 continue;
2577 }
2578
2579 result = card.getCarrierPrivilegeStatus(
2580 mPhone.getContext().getPackageManager(), pkgName);
2581 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2582 break;
2583 }
2584 }
2585
2586 return result;
Junda Liu29340342014-07-10 15:23:27 -07002587 }
Derek Tan89e89d42014-07-08 17:00:10 -07002588
2589 @Override
Junda Liue64de782015-04-16 17:19:16 -07002590 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
2591 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
2592 loge("phoneId " + phoneId + " is not valid.");
2593 return null;
2594 }
2595 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002596 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002597 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002598 return null ;
2599 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002600 return card.getCarrierPackageNamesForIntent(
Svetoslav483aff72015-04-21 14:16:07 -07002601 mPhone.getContext().getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002602 }
2603
Amith Yamasani6e118872016-02-19 12:53:51 -08002604 @Override
2605 public List<String> getPackagesWithCarrierPrivileges() {
2606 PackageManager pm = mPhone.getContext().getPackageManager();
2607 List<String> privilegedPackages = new ArrayList<>();
2608 List<PackageInfo> packages = null;
2609 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2610 UiccCard card = UiccController.getInstance().getUiccCard(i);
2611 if (card == null) {
2612 // No UICC in that slot.
2613 continue;
2614 }
2615 if (card.hasCarrierPrivilegeRules()) {
2616 if (packages == null) {
2617 // Only check packages in user 0 for now
2618 packages = pm.getInstalledPackagesAsUser(
2619 PackageManager.MATCH_DISABLED_COMPONENTS
2620 | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS
2621 | PackageManager.GET_SIGNATURES, UserHandle.USER_SYSTEM);
2622 }
2623 for (int p = packages.size() - 1; p >= 0; p--) {
2624 PackageInfo pkgInfo = packages.get(p);
2625 if (pkgInfo != null && pkgInfo.packageName != null
2626 && card.getCarrierPrivilegeStatus(pkgInfo)
2627 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2628 privilegedPackages.add(pkgInfo.packageName);
2629 }
2630 }
2631 }
2632 }
2633 return privilegedPackages;
2634 }
2635
Wink Savilleb564aae2014-10-23 10:18:09 -07002636 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002637 final Phone phone = getPhone(subId);
2638 UiccCard card = phone == null ? null : phone.getUiccCard();
Derek Tan97ebb422014-09-05 16:55:38 -07002639 if (card == null) {
2640 loge("getIccId: No UICC");
2641 return null;
2642 }
2643 String iccId = card.getIccId();
2644 if (TextUtils.isEmpty(iccId)) {
2645 loge("getIccId: ICC ID is null or empty.");
2646 return null;
2647 }
2648 return iccId;
2649 }
2650
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002651 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002652 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
2653 String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002654 enforceCarrierPrivilege(subId);
Derek Tan97ebb422014-09-05 16:55:38 -07002655
Jeff Sharkey85190e62014-12-05 09:40:12 -08002656 final String iccId = getIccId(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07002657 final Phone phone = getPhone(subId);
2658 if (phone == null) {
2659 return false;
2660 }
2661 final String subscriberId = phone.getSubscriberId();
Jeff Sharkey85190e62014-12-05 09:40:12 -08002662
2663 if (DBG_MERGE) {
2664 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
2665 + subscriberId + " to " + number);
2666 }
2667
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002668 if (TextUtils.isEmpty(iccId)) {
2669 return false;
Derek Tan97ebb422014-09-05 16:55:38 -07002670 }
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002671
Jeff Sharkey85190e62014-12-05 09:40:12 -08002672 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2673
2674 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002675 if (alphaTag == null) {
2676 editor.remove(alphaTagPrefKey);
2677 } else {
2678 editor.putString(alphaTagPrefKey, alphaTag);
2679 }
2680
Jeff Sharkey85190e62014-12-05 09:40:12 -08002681 // Record both the line number and IMSI for this ICCID, since we need to
2682 // track all merged IMSIs based on line number
2683 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2684 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002685 if (number == null) {
2686 editor.remove(numberPrefKey);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002687 editor.remove(subscriberPrefKey);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002688 } else {
2689 editor.putString(numberPrefKey, number);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002690 editor.putString(subscriberPrefKey, subscriberId);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002691 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002692
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002693 editor.commit();
2694 return true;
Derek Tan7226c842014-07-02 17:42:23 -07002695 }
2696
2697 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002698 public String getLine1NumberForDisplay(int subId, String callingPackage) {
Makoto Onukifee69342015-06-29 14:44:50 -07002699 // This is open to apps with WRITE_SMS.
2700 if (!canReadPhoneNumber(callingPackage, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08002701 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07002702 return null;
2703 }
Derek Tan97ebb422014-09-05 16:55:38 -07002704
2705 String iccId = getIccId(subId);
2706 if (iccId != null) {
2707 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
Amit Mahajan9cf11512015-11-09 11:40:48 -08002708 if (DBG_MERGE) {
2709 log("getLine1NumberForDisplay returning " +
2710 mTelephonySharedPreferences.getString(numberPrefKey, null));
2711 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002712 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002713 }
Amit Mahajan9cf11512015-11-09 11:40:48 -08002714 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
Derek Tan97ebb422014-09-05 16:55:38 -07002715 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002716 }
2717
2718 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002719 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
2720 if (!canReadPhoneState(callingPackage, "getLine1AlphaTagForDisplay")) {
2721 return null;
2722 }
Derek Tan97ebb422014-09-05 16:55:38 -07002723
2724 String iccId = getIccId(subId);
2725 if (iccId != null) {
2726 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002727 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002728 }
Derek Tan97ebb422014-09-05 16:55:38 -07002729 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002730 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002731
2732 @Override
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002733 public String[] getMergedSubscriberIds(String callingPackage) {
2734 if (!canReadPhoneState(callingPackage, "getMergedSubscriberIds")) {
2735 return null;
2736 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002737 final Context context = mPhone.getContext();
2738 final TelephonyManager tele = TelephonyManager.from(context);
2739 final SubscriptionManager sub = SubscriptionManager.from(context);
2740
2741 // Figure out what subscribers are currently active
2742 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002743 // Clear calling identity, when calling TelephonyManager, because callerUid must be
2744 // the process, where TelephonyManager was instantiated. Otherwise AppOps check will fail.
2745 final long identity = Binder.clearCallingIdentity();
2746 try {
2747 final int[] subIds = sub.getActiveSubscriptionIdList();
2748 for (int subId : subIds) {
2749 activeSubscriberIds.add(tele.getSubscriberId(subId));
2750 }
2751 } finally {
2752 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002753 }
2754
2755 // First pass, find a number override for an active subscriber
2756 String mergeNumber = null;
2757 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
2758 for (String key : prefs.keySet()) {
2759 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
2760 final String subscriberId = (String) prefs.get(key);
2761 if (activeSubscriberIds.contains(subscriberId)) {
2762 final String iccId = key.substring(PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
2763 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2764 mergeNumber = (String) prefs.get(numberKey);
2765 if (DBG_MERGE) {
2766 Slog.d(LOG_TAG, "Found line number " + mergeNumber
2767 + " for active subscriber " + subscriberId);
2768 }
2769 if (!TextUtils.isEmpty(mergeNumber)) {
2770 break;
2771 }
2772 }
2773 }
2774 }
2775
2776 // Shortcut when no active merged subscribers
2777 if (TextUtils.isEmpty(mergeNumber)) {
2778 return null;
2779 }
2780
2781 // Second pass, find all subscribers under that line override
2782 final ArraySet<String> result = new ArraySet<>();
2783 for (String key : prefs.keySet()) {
2784 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
2785 final String number = (String) prefs.get(key);
2786 if (mergeNumber.equals(number)) {
2787 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
2788 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
2789 final String subscriberId = (String) prefs.get(subscriberKey);
2790 if (!TextUtils.isEmpty(subscriberId)) {
2791 result.add(subscriberId);
2792 }
2793 }
2794 }
2795 }
2796
2797 final String[] resultArray = result.toArray(new String[result.size()]);
2798 Arrays.sort(resultArray);
2799 if (DBG_MERGE) {
2800 Slog.d(LOG_TAG, "Found subscribers " + Arrays.toString(resultArray) + " after merge");
2801 }
2802 return resultArray;
2803 }
2804
2805 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002806 public boolean setOperatorBrandOverride(int subId, String brand) {
2807 enforceCarrierPrivilege(subId);
2808 final Phone phone = getPhone(subId);
2809 return phone == null ? false : phone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002810 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05002811
2812 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002813 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002814 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
2815 List<String> cdmaNonRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002816 enforceCarrierPrivilege(subId);
2817 final Phone phone = getPhone(subId);
2818 if (phone == null) {
2819 return false;
2820 }
2821 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002822 cdmaNonRoamingList);
2823 }
2824
2825 @Override
Steven Liu4bf01bc2014-07-17 11:05:29 -05002826 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
2827 enforceModifyPermission();
2828
2829 int returnValue = 0;
2830 try {
2831 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
2832 if(result.exception == null) {
2833 if (result.result != null) {
2834 byte[] responseData = (byte[])(result.result);
2835 if(responseData.length > oemResp.length) {
2836 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
2837 responseData.length + "bytes. Buffer Size is " +
2838 oemResp.length + "bytes.");
2839 }
2840 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
2841 returnValue = responseData.length;
2842 }
2843 } else {
2844 CommandException ex = (CommandException) result.exception;
2845 returnValue = ex.getCommandError().ordinal();
2846 if(returnValue > 0) returnValue *= -1;
2847 }
2848 } catch (RuntimeException e) {
2849 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
2850 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
2851 if(returnValue > 0) returnValue *= -1;
2852 }
2853
2854 return returnValue;
2855 }
Wink Saville5d475dd2014-10-17 15:00:58 -07002856
2857 @Override
2858 public void setRadioCapability(RadioAccessFamily[] rafs) {
2859 try {
2860 ProxyController.getInstance().setRadioCapability(rafs);
2861 } catch (RuntimeException e) {
2862 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
2863 }
2864 }
2865
2866 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002867 public int getRadioAccessFamily(int phoneId, String callingPackage) {
2868 if (!canReadPhoneState(callingPackage, "getRadioAccessFamily")) {
2869 return RadioAccessFamily.RAF_UNKNOWN;
2870 }
2871
Wink Saville5d475dd2014-10-17 15:00:58 -07002872 return ProxyController.getInstance().getRadioAccessFamily(phoneId);
2873 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002874
2875 @Override
2876 public void enableVideoCalling(boolean enable) {
2877 enforceModifyPermission();
Tyler Gunnfdd69de2015-12-04 21:24:38 -08002878 ImsManager.setVtSetting(mPhone.getContext(), enable);
Andrew Leedf14ead2014-10-17 14:22:52 -07002879 }
2880
2881 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002882 public boolean isVideoCallingEnabled(String callingPackage) {
2883 if (!canReadPhoneState(callingPackage, "isVideoCallingEnabled")) {
2884 return false;
2885 }
2886
Andrew Lee77527ac2014-10-21 16:57:39 -07002887 // Check the user preference and the system-level IMS setting. Even if the user has
2888 // enabled video calling, if IMS is disabled we aren't able to support video calling.
2889 // In the long run, we may instead need to check if there exists a connection service
2890 // which can support video calling.
Andrew Lee312e8172014-10-23 17:01:36 -07002891 return ImsManager.isVtEnabledByPlatform(mPhone.getContext())
2892 && ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
Tyler Gunnfdd69de2015-12-04 21:24:38 -08002893 && ImsManager.isVtEnabledByUser(mPhone.getContext());
Andrew Leedf14ead2014-10-17 14:22:52 -07002894 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002895
Andrew Leea1239f22015-03-02 17:44:07 -08002896 @Override
2897 public boolean canChangeDtmfToneLength() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07002898 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08002899 }
2900
2901 @Override
2902 public boolean isWorldPhone() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07002903 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08002904 }
2905
Andrew Lee9431b832015-03-09 18:46:45 -07002906 @Override
2907 public boolean isTtyModeSupported() {
2908 TelecomManager telecomManager = TelecomManager.from(mPhone.getContext());
2909 TelephonyManager telephonyManager =
2910 (TelephonyManager) mPhone.getContext().getSystemService(Context.TELEPHONY_SERVICE);
Wooki Wu1f82f7a2016-02-15 15:59:58 +08002911 return telecomManager.isTtySupported();
Andrew Lee9431b832015-03-09 18:46:45 -07002912 }
2913
2914 @Override
2915 public boolean isHearingAidCompatibilitySupported() {
2916 return mPhone.getContext().getResources().getBoolean(R.bool.hac_enabled);
2917 }
2918
Sanket Padawe7310cc72015-01-14 09:53:20 -08002919 /**
2920 * Returns the unique device ID of phone, for example, the IMEI for
2921 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
2922 *
2923 * <p>Requires Permission:
2924 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
2925 */
2926 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002927 public String getDeviceId(String callingPackage) {
2928 if (!canReadPhoneState(callingPackage, "getDeviceId")) {
2929 return null;
2930 }
2931
Sanket Padawe7310cc72015-01-14 09:53:20 -08002932 final Phone phone = PhoneFactory.getPhone(0);
2933 if (phone != null) {
2934 return phone.getDeviceId();
2935 } else {
2936 return null;
2937 }
2938 }
2939
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002940 /*
2941 * {@hide}
2942 * Returns the IMS Registration Status
2943 */
Santos Cordon7a1885b2015-02-03 11:15:19 -08002944 @Override
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002945 public boolean isImsRegistered() {
2946 return mPhone.isImsRegistered();
2947 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08002948
2949 @Override
2950 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
2951 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
2952 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07002953
Nathan Haroldc55097a2015-03-11 18:14:50 -07002954 /*
2955 * {@hide}
2956 * Returns the IMS Registration Status
2957 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002958 public boolean isWifiCallingAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07002959 return mPhone.isWifiCallingEnabled();
2960 }
2961
2962 /*
2963 * {@hide}
2964 * Returns the IMS Registration Status
2965 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002966 public boolean isVolteAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07002967 return mPhone.isVolteEnabled();
2968 }
Svet Ganovb320e182015-04-16 12:30:10 -07002969
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002970 /*
2971 * {@hide} Returns the IMS Registration Status
2972 */
2973 public boolean isVideoTelephonyAvailable() {
2974 return mPhone.isVideoEnabled();
2975 }
2976
Svet Ganovb320e182015-04-16 12:30:10 -07002977 private boolean canReadPhoneState(String callingPackage, String message) {
Etan Cohen921655c2015-06-24 13:54:50 -07002978 try {
Amit Mahajan83ea23b2015-07-30 16:05:11 -07002979 mApp.enforceCallingOrSelfPermission(
Etan Cohen921655c2015-06-24 13:54:50 -07002980 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE, message);
2981
Amit Mahajan83ea23b2015-07-30 16:05:11 -07002982 // SKIP checking for run-time permission since caller or self has PRIVILEDGED permission
Etan Cohen921655c2015-06-24 13:54:50 -07002983 return true;
Amit Mahajan785783f2015-06-29 10:36:50 -07002984 } catch (SecurityException e) {
2985 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
2986 message);
Etan Cohen921655c2015-06-24 13:54:50 -07002987 }
Svet Ganovb320e182015-04-16 12:30:10 -07002988
2989 if (mAppOps.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(),
2990 callingPackage) != AppOpsManager.MODE_ALLOWED) {
2991 return false;
2992 }
2993
2994 return true;
2995 }
Stuart Scott8eef64f2015-04-08 15:13:54 -07002996
Makoto Onukifee69342015-06-29 14:44:50 -07002997 /**
Amit Mahajanb9b49782015-09-15 18:16:32 -07002998 * Besides READ_PHONE_STATE, WRITE_SMS and READ_SMS also allow apps to get phone numbers.
Makoto Onukifee69342015-06-29 14:44:50 -07002999 */
3000 private boolean canReadPhoneNumber(String callingPackage, String message) {
Makoto Onukie4072d12015-08-03 15:12:23 -07003001 // Default SMS app can always read it.
3002 if (mAppOps.noteOp(AppOpsManager.OP_WRITE_SMS,
3003 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED) {
3004 return true;
3005 }
3006 try {
3007 return canReadPhoneState(callingPackage, message);
Amit Mahajanb9b49782015-09-15 18:16:32 -07003008 } catch (SecurityException readPhoneStateSecurityException) {
3009 try {
3010 // Can be read with READ_SMS too.
3011 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_SMS, message);
3012 return mAppOps.noteOp(AppOpsManager.OP_READ_SMS,
3013 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED;
3014 } catch (SecurityException readSmsSecurityException) {
3015 // Throw exception with message including both READ_PHONE_STATE and READ_SMS
3016 // permissions
3017 throw new SecurityException(message + ": Neither user " + Binder.getCallingUid() +
3018 " nor current process has " + android.Manifest.permission.READ_PHONE_STATE +
3019 " or " + android.Manifest.permission.READ_SMS + ".");
3020 }
Makoto Onukie4072d12015-08-03 15:12:23 -07003021 }
Makoto Onukifee69342015-06-29 14:44:50 -07003022 }
3023
Stuart Scott8eef64f2015-04-08 15:13:54 -07003024 @Override
3025 public void factoryReset(int subId) {
3026 enforceConnectivityInternalPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07003027 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
3028 return;
3029 }
3030
Svet Ganovcc087f82015-05-12 20:35:54 -07003031 final long identity = Binder.clearCallingIdentity();
3032 try {
Stuart Scott981d8582015-04-21 14:09:50 -07003033 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
3034 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Svet Ganovcc087f82015-05-12 20:35:54 -07003035 // Enable data
3036 setDataEnabled(subId, true);
3037 // Set network selection mode to automatic
3038 setNetworkSelectionModeAutomatic(subId);
3039 // Set preferred mobile network type to the best available
3040 setPreferredNetworkType(subId, Phone.PREFERRED_NT_MODE);
3041 // Turn off roaming
3042 SubscriptionManager.from(mApp).setDataRoaming(0, subId);
3043 }
3044 } finally {
3045 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07003046 }
3047 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003048
3049 @Override
3050 public String getLocaleFromDefaultSim() {
3051 // We query all subscriptions instead of just the active ones, because
3052 // this might be called early on in the provisioning flow when the
3053 // subscriptions potentially aren't active yet.
3054 final List<SubscriptionInfo> slist = getAllSubscriptionInfoList();
3055 if (slist == null || slist.isEmpty()) {
3056 return null;
3057 }
3058
3059 // This function may be called very early, say, from the setup wizard, at
3060 // which point we won't have a default subscription set. If that's the case
3061 // we just choose the first, which will be valid in "most cases".
3062 final int defaultSubId = getDefaultSubscription();
3063 SubscriptionInfo info = null;
3064 if (defaultSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
3065 info = slist.get(0);
3066 } else {
3067 for (SubscriptionInfo item : slist) {
3068 if (item.getSubscriptionId() == defaultSubId) {
3069 info = item;
3070 break;
3071 }
3072 }
3073
3074 if (info == null) {
3075 return null;
3076 }
3077 }
3078
3079 // Try and fetch the locale from the carrier properties or from the SIM language
3080 // preferences (EF-PL and EF-LI)...
Tony Hill183b2de2015-06-24 14:53:58 +01003081 final int mcc = info.getMcc();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003082 final Phone defaultPhone = getPhone(info.getSubscriptionId());
Narayan Kamath011676f2015-07-29 12:04:08 +01003083 String simLanguage = null;
Narayan Kamath1c496c22015-04-16 14:40:19 +01003084 if (defaultPhone != null) {
3085 final Locale localeFromDefaultSim = defaultPhone.getLocaleFromSimAndCarrierPrefs();
3086 if (localeFromDefaultSim != null) {
Narayan Kamath011676f2015-07-29 12:04:08 +01003087 if (!localeFromDefaultSim.getCountry().isEmpty()) {
Tony Hill183b2de2015-06-24 14:53:58 +01003088 if (DBG) log("Using locale from default SIM:" + localeFromDefaultSim);
3089 return localeFromDefaultSim.toLanguageTag();
Narayan Kamath011676f2015-07-29 12:04:08 +01003090 } else {
3091 simLanguage = localeFromDefaultSim.getLanguage();
Tony Hill183b2de2015-06-24 14:53:58 +01003092 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003093 }
3094 }
3095
Narayan Kamath011676f2015-07-29 12:04:08 +01003096 // The SIM language preferences only store a language (e.g. fr = French), not an
3097 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
3098 // the SIM and carrier preferences does not include a country we add the country
3099 // determined from the SIM MCC to provide an exact locale.
3100 final Locale mccLocale = MccTable.getLocaleFromMcc(mPhone.getContext(), mcc, simLanguage);
Tony Hill183b2de2015-06-24 14:53:58 +01003101 if (mccLocale != null) {
3102 if (DBG) log("No locale from default SIM, using mcc locale:" + mccLocale);
3103 return mccLocale.toLanguageTag();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003104 }
3105
Tony Hill183b2de2015-06-24 14:53:58 +01003106 if (DBG) log("No locale found - returning null");
Narayan Kamath1c496c22015-04-16 14:40:19 +01003107 return null;
3108 }
3109
3110 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
3111 final long identity = Binder.clearCallingIdentity();
3112 try {
3113 return mSubscriptionController.getAllSubInfoList(
3114 mPhone.getContext().getOpPackageName());
3115 } finally {
3116 Binder.restoreCallingIdentity(identity);
3117 }
3118 }
3119
3120 private List<SubscriptionInfo> getActiveSubscriptionInfoList() {
3121 final long identity = Binder.clearCallingIdentity();
3122 try {
3123 return mSubscriptionController.getActiveSubscriptionInfoList(
3124 mPhone.getContext().getOpPackageName());
3125 } finally {
3126 Binder.restoreCallingIdentity(identity);
3127 }
3128 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003129
3130 /**
Adam Lesinski903a54c2016-04-11 14:49:52 -07003131 * Responds to the ResultReceiver with the {@link android.telephony.ModemActivityInfo} object
3132 * representing the state of the modem.
3133 *
3134 * NOTE: This clears the modem state, so there should only every be one caller.
3135 * @hide
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003136 */
3137 @Override
Adam Lesinski903a54c2016-04-11 14:49:52 -07003138 public void requestModemActivityInfo(ResultReceiver result) {
3139 enforceModifyPermission();
3140
3141 ModemActivityInfo info = (ModemActivityInfo) sendRequest(CMD_GET_MODEM_ACTIVITY_INFO, null);
3142 Bundle bundle = new Bundle();
3143 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY, info);
3144 result.send(0, bundle);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003145 }
Jack Yu85bd38a2015-11-09 11:34:32 -08003146
3147 /**
3148 * {@hide}
3149 * Returns the service state information on specified subscription.
3150 */
3151 @Override
3152 public ServiceState getServiceStateForSubscriber(int subId, String callingPackage) {
3153
3154 if (!canReadPhoneState(callingPackage, "getServiceStateForSubscriber")) {
3155 return null;
3156 }
3157
3158 final Phone phone = getPhone(subId);
3159 if (phone == null) {
3160 return null;
3161 }
3162
3163 return phone.getServiceState();
3164 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08003165
3166 /**
3167 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
3168 *
3169 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3170 * voicemail ringtone.
3171 * @return The URI for the ringtone to play when receiving a voicemail from a specific
3172 * PhoneAccount.
3173 */
3174 @Override
3175 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
3176 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
3177 if (phone == null) {
3178 return null;
3179 }
3180
3181 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone);
3182 }
3183
3184 /**
3185 * Returns whether vibration is set for voicemail notification in Phone settings.
3186 *
3187 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3188 * voicemail vibration setting.
3189 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
3190 */
3191 @Override
3192 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
3193 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
3194 if (phone == null) {
3195 return false;
3196 }
3197
3198 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone);
3199 }
3200
Youhan Wange64578a2016-05-02 15:32:42 -07003201 /**
3202 * Make sure either called from same process as self (phone) or IPC caller has read privilege.
3203 *
3204 * @throws SecurityException if the caller does not have the required permission
3205 */
3206 private void enforceReadPrivilegedPermission() {
3207 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
3208 null);
3209 }
3210
3211 /**
3212 * Return the application ID for the app type.
3213 *
3214 * @param subId the subscription ID that this request applies to.
3215 * @param appType the uicc app type.
3216 * @return Application ID for specificied app type, or null if no uicc.
3217 */
3218 @Override
3219 public String getAidForAppType(int subId, int appType) {
3220 enforceReadPrivilegedPermission();
3221 Phone phone = getPhone(subId);
3222 if (phone == null) {
3223 return null;
3224 }
3225 String aid = null;
3226 try {
3227 aid = UiccController.getInstance().getUiccCard(phone.getPhoneId())
3228 .getApplicationByType(appType).getAid();
3229 } catch (Exception e) {
3230 Log.e(LOG_TAG, "Not getting aid. Exception ex=" + e);
3231 }
3232 return aid;
3233 }
3234
Youhan Wang4001d252016-05-11 10:29:41 -07003235 /**
3236 * Return the Electronic Serial Number.
3237 *
3238 * @param subId the subscription ID that this request applies to.
3239 * @return ESN or null if error.
3240 */
3241 @Override
3242 public String getEsn(int subId) {
3243 enforceReadPrivilegedPermission();
3244 Phone phone = getPhone(subId);
3245 if (phone == null) {
3246 return null;
3247 }
3248 String esn = null;
3249 try {
3250 esn = phone.getEsn();
3251 } catch (Exception e) {
3252 Log.e(LOG_TAG, "Not getting ESN. Exception ex=" + e);
3253 }
3254 return esn;
3255 }
3256
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003257 /**
Youhan Wang66ad5d72016-07-18 17:56:58 -07003258 * Return the Preferred Roaming List Version.
3259 *
3260 * @param subId the subscription ID that this request applies to.
3261 * @return PRLVersion or null if error.
3262 */
3263 @Override
3264 public String getCdmaPrlVersion(int subId) {
3265 enforceReadPrivilegedPermission();
3266 Phone phone = getPhone(subId);
3267 if (phone == null) {
3268 return null;
3269 }
3270 String cdmaPrlVersion = null;
3271 try {
3272 cdmaPrlVersion = phone.getCdmaPrlVersion();
3273 } catch (Exception e) {
3274 Log.e(LOG_TAG, "Not getting PRLVersion", e);
3275 }
3276 return cdmaPrlVersion;
3277 }
3278
3279 /**
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003280 * Get snapshot of Telephony histograms
3281 * @return List of Telephony histograms
3282 * @hide
3283 */
3284 @Override
3285 public List<TelephonyHistogram> getTelephonyHistograms() {
3286 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
3287 return RIL.getTelephonyRILTimingHistograms();
3288 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07003289
3290 /**
3291 * {@hide}
3292 * Set the allowed carrier list for slotId
3293 * Require system privileges. In the future we may add this to carrier APIs.
3294 *
3295 * @return The number of carriers set successfully, should match length of carriers
3296 */
3297 @Override
3298 public int setAllowedCarriers(int slotId, List<CarrierIdentifier> carriers) {
3299 enforceModifyPermission();
3300 int subId = SubscriptionManager.getSubId(slotId)[0];
3301 int[] retVal = (int[]) sendRequest(CMD_SET_ALLOWED_CARRIERS, carriers, subId);
3302 return retVal[0];
3303 }
3304
3305 /**
3306 * {@hide}
3307 * Get the allowed carrier list for slotId.
3308 * Require system privileges. In the future we may add this to carrier APIs.
3309 *
3310 * @return List of {@link android.service.telephony.CarrierIdentifier}; empty list
3311 * means all carriers are allowed.
3312 */
3313 @Override
3314 public List<CarrierIdentifier> getAllowedCarriers(int slotId) {
3315 enforceReadPrivilegedPermission();
3316 int subId = SubscriptionManager.getSubId(slotId)[0];
3317 return (List<CarrierIdentifier>) sendRequest(CMD_GET_ALLOWED_CARRIERS, null, subId);
3318 }
3319
fionaxu59545b42016-05-25 15:53:37 -07003320 /**
3321 * Action set from carrier signalling broadcast receivers to enable/disable metered apns
3322 * @param subId the subscription ID that this action applies to.
3323 * @param enabled control enable or disable metered apns.
3324 * {@hide}
3325 */
3326 @Override
3327 public void carrierActionSetMeteredApnsEnabled(int subId, boolean enabled) {
3328 enforceModifyPermission();
3329 final Phone phone = getPhone(subId);
3330 if (phone == null) {
3331 loge("carrierAction: SetMeteredApnsEnabled fails with invalid subId: " + subId);
3332 return;
3333 }
3334 try {
3335 phone.carrierActionSetMeteredApnsEnabled(enabled);
3336 } catch (Exception e) {
3337 Log.e(LOG_TAG, "carrierAction: SetMeteredApnsEnabled fails. Exception ex=" + e);
3338 }
3339 }
3340
3341 /**
3342 * Action set from carrier signalling broadcast receivers to enable/disable radio
3343 * @param subId the subscription ID that this action applies to.
3344 * @param enabled control enable or disable radio.
3345 * {@hide}
3346 */
3347 @Override
3348 public void carrierActionSetRadioEnabled(int subId, boolean enabled) {
3349 enforceModifyPermission();
3350 final Phone phone = getPhone(subId);
3351 if (phone == null) {
3352 loge("carrierAction: SetRadioEnabled fails with invalid sibId: " + subId);
3353 return;
3354 }
3355 try {
3356 phone.carrierActionSetRadioEnabled(enabled);
3357 } catch (Exception e) {
3358 Log.e(LOG_TAG, "carrierAction: SetRadioEnabled fails. Exception ex=" + e);
3359 }
3360 }
3361
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003362 /**
3363 * Called when "adb shell dumpsys phone" is invoked. Dump is also automatically invoked when a
3364 * bug report is being generated.
3365 */
3366 @Override
Ta-wei Yen99282e02016-06-21 18:19:35 -07003367 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
dcashman22b950d2016-06-27 11:39:02 -07003368 if (mPhone.getContext().checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3369 != PackageManager.PERMISSION_GRANTED) {
3370 writer.println("Permission Denial: can't dump Phone from pid="
3371 + Binder.getCallingPid()
3372 + ", uid=" + Binder.getCallingUid()
3373 + "without permission "
3374 + android.Manifest.permission.DUMP);
3375 return;
3376 }
Ta-wei Yen99282e02016-06-21 18:19:35 -07003377 DumpsysHandler.dump(mPhone.getContext(), fd, writer, args);
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003378 }
Jack Yueb89b242016-06-22 13:27:47 -07003379
3380 /**
3381 * Get aggregated video call data usage from all subscriptions since boot.
3382 * @return total data usage in bytes
3383 * {@hide}
3384 */
3385 @Override
3386 public long getVtDataUsage() {
3387 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_NETWORK_USAGE_HISTORY,
3388 null);
3389
3390 // NetworkStatsService keeps tracking the active network interface and identity. It will
3391 // record the delta with the corresponding network identity. What we need to do here is
3392 // returning total video call data usage from all subscriptions since boot.
3393
3394 // TODO: Add sub id support in the future. We'll need it when we support DSDA and
3395 // simultaneous VT calls.
3396 final Phone[] phones = PhoneFactory.getPhones();
3397 long total = 0;
3398 for (Phone phone : phones) {
3399 total += phone.getVtDataUsage();
3400 }
3401 return total;
3402 }
Jack Yu75ab2952016-07-08 14:29:33 -07003403
3404 /**
3405 * Policy control of data connection. Usually used when data limit is passed.
3406 * @param enabled True if enabling the data, otherwise disabling.
3407 * @param subId Subscription index
3408 * {@hide}
3409 */
3410 @Override
3411 public void setPolicyDataEnabled(boolean enabled, int subId) {
3412 enforceModifyPermission();
3413 Phone phone = getPhone(subId);
3414 if (phone != null) {
3415 phone.setPolicyDataEnabled(enabled);
3416 }
3417 }
Sooraj Sasindran600a37a2016-07-18 11:57:25 -07003418
3419 /**
3420 * Get Client request stats
3421 * @return List of Client Request Stats
3422 * @hide
3423 */
3424 @Override
3425 public List<ClientRequestStats> getClientRequestStats(String callingPackage, int subId) {
3426 if (!canReadPhoneState(callingPackage, "getClientRequestStats")) {
3427 return null;
3428 }
3429
3430 Phone phone = getPhone(subId);
3431 if (phone != null) {
3432 return phone.getClientRequestStats();
3433 }
3434
3435 return null;
3436 }
3437
3438 private WorkSource getWorkSource(WorkSource workSource, int uid) {
3439 if (workSource != null) {
3440 return workSource;
3441 }
3442
3443 String packageName = mPhone.getContext().getPackageManager().getNameForUid(uid);
3444 workSource = new WorkSource(uid, packageName);
3445 return workSource;
3446 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003447}