blob: d1219527b13963471ab772e8897d5f1a19ba5de3 [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
Ta-wei Yen30a69c82016-12-27 14:52:32 -080021import android.Manifest.permission;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070022import android.app.ActivityManager;
23import android.app.AppOpsManager;
Ta-wei Yen30a69c82016-12-27 14:52:32 -080024import android.app.PendingIntent;
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -070025import android.content.ComponentName;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070026import android.content.Context;
27import android.content.Intent;
Derek Tan97ebb422014-09-05 16:55:38 -070028import android.content.SharedPreferences;
Amith Yamasani6e118872016-02-19 12:53:51 -080029import android.content.pm.PackageInfo;
Shishir Agrawal60f9c952014-06-23 12:00:43 -070030import android.content.pm.PackageManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070031import android.net.Uri;
32import android.os.AsyncResult;
33import android.os.Binder;
34import android.os.Bundle;
35import android.os.Handler;
yinxu504e1392017-04-12 16:03:22 -070036import android.os.IBinder;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070037import android.os.Looper;
38import android.os.Message;
yinxu504e1392017-04-12 16:03:22 -070039import android.os.Messenger;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070040import android.os.Process;
Adam Lesinski903a54c2016-04-11 14:49:52 -070041import android.os.ResultReceiver;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070042import android.os.ServiceManager;
43import android.os.UserHandle;
Stuart Scott981d8582015-04-21 14:09:50 -070044import android.os.UserManager;
Sooraj Sasindran9a909312016-07-18 11:57:25 -070045import android.os.WorkSource;
Derek Tan97ebb422014-09-05 16:55:38 -070046import android.preference.PreferenceManager;
Ihab Awadf2177b72013-11-25 13:33:23 -080047import android.provider.Settings;
Meng Wang1a7c35a2016-05-05 20:56:15 -070048import android.service.carrier.CarrierIdentifier;
Santos Cordon7a1885b2015-02-03 11:15:19 -080049import android.telecom.PhoneAccount;
Nancy Chen31f9ba12016-01-06 11:42:12 -080050import android.telecom.PhoneAccountHandle;
Andrew Lee9431b832015-03-09 18:46:45 -070051import android.telecom.TelecomManager;
Junda Liu12f7d802015-05-01 12:06:44 -070052import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070053import android.telephony.CellInfo;
Sooraj Sasindran9a909312016-07-18 11:57:25 -070054import android.telephony.ClientRequestStats;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -070055import android.telephony.IccOpenLogicalChannelResponse;
Ta-wei Yen87c49842016-05-13 21:19:52 -070056import android.telephony.ModemActivityInfo;
Jake Hambye994d462014-02-03 13:10:13 -080057import android.telephony.NeighboringCellInfo;
yinxu504e1392017-04-12 16:03:22 -070058import android.telephony.NetworkScanRequest;
Wink Saville5d475dd2014-10-17 15:00:58 -070059import android.telephony.RadioAccessFamily;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070060import android.telephony.ServiceState;
Ta-wei Yen30a69c82016-12-27 14:52:32 -080061import android.telephony.SmsManager;
Wink Saville0f3b5fc2014-11-11 08:40:49 -080062import android.telephony.SubscriptionInfo;
Jeff Sharkey85190e62014-12-05 09:40:12 -080063import android.telephony.SubscriptionManager;
Sanket Padawe99ef1e32016-05-18 16:12:33 -070064import android.telephony.TelephonyHistogram;
Ta-wei Yenb6929602016-05-24 15:48:27 -070065import android.telephony.TelephonyManager;
66import android.telephony.VisualVoicemailSmsFilterSettings;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070067import android.text.TextUtils;
Jeff Sharkey85190e62014-12-05 09:40:12 -080068import android.util.ArraySet;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070069import android.util.Log;
Jake Hambye994d462014-02-03 13:10:13 -080070import android.util.Pair;
Jeff Sharkey85190e62014-12-05 09:40:12 -080071import android.util.Slog;
Ta-wei Yen30a69c82016-12-27 14:52:32 -080072
Andrew Lee312e8172014-10-23 17:01:36 -070073import com.android.ims.ImsManager;
Brad Ebinger51f743a2017-01-23 13:50:20 -080074import com.android.ims.internal.IImsServiceController;
75import com.android.ims.internal.IImsServiceFeatureListener;
Shishir Agrawal566b7612013-10-28 14:41:00 -070076import com.android.internal.telephony.CallManager;
Tyler Gunn52dcf772017-04-26 11:30:31 -070077import com.android.internal.telephony.CallStateException;
Shishir Agrawal302c8692015-06-19 13:49:39 -070078import com.android.internal.telephony.CellNetworkScanResult;
Shishir Agrawal566b7612013-10-28 14:41:00 -070079import com.android.internal.telephony.CommandException;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070080import com.android.internal.telephony.DefaultPhoneNotifier;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070081import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -080082import com.android.internal.telephony.IccCard;
Narayan Kamath1c496c22015-04-16 14:40:19 +010083import com.android.internal.telephony.MccTable;
Shishir Agrawal302c8692015-06-19 13:49:39 -070084import com.android.internal.telephony.OperatorInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070085import com.android.internal.telephony.Phone;
Nathan Harolda667c152016-12-14 11:27:20 -080086import com.android.internal.telephony.PhoneConstantConversions;
Ta-wei Yen87c49842016-05-13 21:19:52 -070087import com.android.internal.telephony.PhoneConstants;
Wink Saville36469e72014-06-11 15:17:00 -070088import com.android.internal.telephony.PhoneFactory;
Wink Saville5d475dd2014-10-17 15:00:58 -070089import com.android.internal.telephony.ProxyController;
Sanket Padawe99ef1e32016-05-18 16:12:33 -070090import com.android.internal.telephony.RIL;
Svet Ganovb320e182015-04-16 12:30:10 -070091import com.android.internal.telephony.RILConstants;
Wink Savilleac1bdfd2014-11-20 23:04:44 -080092import com.android.internal.telephony.SubscriptionController;
Shishir Agrawal566b7612013-10-28 14:41:00 -070093import com.android.internal.telephony.uicc.IccIoResult;
94import com.android.internal.telephony.uicc.IccUtils;
Nathan Haroldb3014052017-01-25 15:57:32 -080095import com.android.internal.telephony.uicc.SIMRecords;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -070096import com.android.internal.telephony.uicc.UiccCard;
Nathan Haroldb3014052017-01-25 15:57:32 -080097import com.android.internal.telephony.uicc.UiccCardApplication;
Shishir Agrawal566b7612013-10-28 14:41:00 -070098import com.android.internal.telephony.uicc.UiccController;
Jake Hambye994d462014-02-03 13:10:13 -080099import com.android.internal.util.HexDump;
Nancy Chen31f9ba12016-01-06 11:42:12 -0800100import com.android.phone.settings.VoicemailNotificationSettingsUtil;
Ta-wei Yenc9df0432017-04-17 17:09:07 -0700101import com.android.phone.vvm.PhoneAccountHandleConverter;
Ta-wei Yen527a9c02017-01-06 15:29:25 -0800102import com.android.phone.vvm.RemoteVvmTaskManager;
Ta-wei Yenc9df0432017-04-17 17:09:07 -0700103import com.android.phone.vvm.VisualVoicemailPreferences;
104import com.android.phone.vvm.VisualVoicemailSettingsUtil;
Ta-wei Yenc8905312017-03-28 11:14:45 -0700105import com.android.phone.vvm.VisualVoicemailSmsFilterConfig;
Ta-wei Yen30a69c82016-12-27 14:52:32 -0800106
Ta-wei Yenc236d6b2016-06-21 13:33:12 -0700107import java.io.FileDescriptor;
108import java.io.PrintWriter;
Ta-wei Yen30a69c82016-12-27 14:52:32 -0800109import java.nio.charset.StandardCharsets;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700110import java.util.ArrayList;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800111import java.util.Arrays;
Jake Hambye994d462014-02-03 13:10:13 -0800112import java.util.List;
Narayan Kamath1c496c22015-04-16 14:40:19 +0100113import java.util.Locale;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800114import java.util.Map;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700115
116/**
117 * Implementation of the ITelephony interface.
118 */
Santos Cordon117fee72014-05-16 17:56:12 -0700119public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700120 private static final String LOG_TAG = "PhoneInterfaceManager";
121 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
122 private static final boolean DBG_LOC = false;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800123 private static final boolean DBG_MERGE = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700124
125 // Message codes used with mMainThreadHandler
126 private static final int CMD_HANDLE_PIN_MMI = 1;
127 private static final int CMD_HANDLE_NEIGHBORING_CELL = 2;
128 private static final int EVENT_NEIGHBORING_CELL_DONE = 3;
129 private static final int CMD_ANSWER_RINGING_CALL = 4;
130 private static final int CMD_END_CALL = 5; // not used yet
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700131 private static final int CMD_TRANSMIT_APDU_LOGICAL_CHANNEL = 7;
132 private static final int EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE = 8;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700133 private static final int CMD_OPEN_CHANNEL = 9;
134 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
135 private static final int CMD_CLOSE_CHANNEL = 11;
136 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -0800137 private static final int CMD_NV_READ_ITEM = 13;
138 private static final int EVENT_NV_READ_ITEM_DONE = 14;
139 private static final int CMD_NV_WRITE_ITEM = 15;
140 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
141 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
142 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
143 private static final int CMD_NV_RESET_CONFIG = 19;
144 private static final int EVENT_NV_RESET_CONFIG_DONE = 20;
Jake Hamby7c27be32014-03-03 13:25:59 -0800145 private static final int CMD_GET_PREFERRED_NETWORK_TYPE = 21;
146 private static final int EVENT_GET_PREFERRED_NETWORK_TYPE_DONE = 22;
147 private static final int CMD_SET_PREFERRED_NETWORK_TYPE = 23;
148 private static final int EVENT_SET_PREFERRED_NETWORK_TYPE_DONE = 24;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800149 private static final int CMD_SEND_ENVELOPE = 25;
150 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Derek Tan6b088ee2014-09-05 14:15:18 -0700151 private static final int CMD_INVOKE_OEM_RIL_REQUEST_RAW = 27;
152 private static final int EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE = 28;
153 private static final int CMD_TRANSMIT_APDU_BASIC_CHANNEL = 29;
154 private static final int EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE = 30;
155 private static final int CMD_EXCHANGE_SIM_IO = 31;
156 private static final int EVENT_EXCHANGE_SIM_IO_DONE = 32;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800157 private static final int CMD_SET_VOICEMAIL_NUMBER = 33;
158 private static final int EVENT_SET_VOICEMAIL_NUMBER_DONE = 34;
Stuart Scott54788802015-03-30 13:18:01 -0700159 private static final int CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC = 35;
160 private static final int EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE = 36;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700161 private static final int CMD_GET_MODEM_ACTIVITY_INFO = 37;
162 private static final int EVENT_GET_MODEM_ACTIVITY_INFO_DONE = 38;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700163 private static final int CMD_PERFORM_NETWORK_SCAN = 39;
164 private static final int EVENT_PERFORM_NETWORK_SCAN_DONE = 40;
165 private static final int CMD_SET_NETWORK_SELECTION_MODE_MANUAL = 41;
166 private static final int EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE = 42;
Meng Wang1a7c35a2016-05-05 20:56:15 -0700167 private static final int CMD_SET_ALLOWED_CARRIERS = 43;
168 private static final int EVENT_SET_ALLOWED_CARRIERS_DONE = 44;
169 private static final int CMD_GET_ALLOWED_CARRIERS = 45;
170 private static final int EVENT_GET_ALLOWED_CARRIERS_DONE = 46;
pkanwar32d516d2016-10-14 19:37:38 -0700171 private static final int CMD_HANDLE_USSD_REQUEST = 47;
Nathan Haroldb3014052017-01-25 15:57:32 -0800172 private static final int CMD_GET_FORBIDDEN_PLMNS = 48;
173 private static final int EVENT_GET_FORBIDDEN_PLMNS_DONE = 49;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700174
175 /** The singleton instance. */
176 private static PhoneInterfaceManager sInstance;
177
Wink Saville3ab207e2014-11-20 13:07:20 -0800178 private PhoneGlobals mApp;
179 private Phone mPhone;
180 private CallManager mCM;
Stuart Scott981d8582015-04-21 14:09:50 -0700181 private UserManager mUserManager;
Wink Saville3ab207e2014-11-20 13:07:20 -0800182 private AppOpsManager mAppOps;
183 private MainThreadHandler mMainThreadHandler;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800184 private SubscriptionController mSubscriptionController;
Wink Saville3ab207e2014-11-20 13:07:20 -0800185 private SharedPreferences mTelephonySharedPreferences;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700186
Derek Tan97ebb422014-09-05 16:55:38 -0700187 private static final String PREF_CARRIERS_ALPHATAG_PREFIX = "carrier_alphtag_";
188 private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
Jeff Sharkey85190e62014-12-05 09:40:12 -0800189 private static final String PREF_CARRIERS_SUBSCRIBER_PREFIX = "carrier_subscriber_";
Derek Tan89e89d42014-07-08 17:00:10 -0700190
191 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700192 * A request object to use for transmitting data to an ICC.
193 */
194 private static final class IccAPDUArgument {
195 public int channel, cla, command, p1, p2, p3;
196 public String data;
197
198 public IccAPDUArgument(int channel, int cla, int command,
199 int p1, int p2, int p3, String data) {
200 this.channel = channel;
201 this.cla = cla;
202 this.command = command;
203 this.p1 = p1;
204 this.p2 = p2;
205 this.p3 = p3;
206 this.data = data;
207 }
208 }
209
210 /**
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700211 * A request object to use for transmitting data to an ICC.
212 */
213 private static final class ManualNetworkSelectionArgument {
214 public OperatorInfo operatorInfo;
215 public boolean persistSelection;
216
217 public ManualNetworkSelectionArgument(OperatorInfo operatorInfo, boolean persistSelection) {
218 this.operatorInfo = operatorInfo;
219 this.persistSelection = persistSelection;
220 }
221 }
222
223 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700224 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
225 * request after sending. The main thread will notify the request when it is complete.
226 */
227 private static final class MainThreadRequest {
228 /** The argument to use for the request */
229 public Object argument;
230 /** The result of the request that is run on the main thread */
231 public Object result;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800232 // The subscriber id that this request applies to. Defaults to
233 // SubscriptionManager.INVALID_SUBSCRIPTION_ID
234 public Integer subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700235
236 public MainThreadRequest(Object argument) {
237 this.argument = argument;
238 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800239
240 public MainThreadRequest(Object argument, Integer subId) {
241 this.argument = argument;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800242 if (subId != null) {
243 this.subId = subId;
244 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800245 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700246 }
247
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800248 private static final class IncomingThirdPartyCallArgs {
249 public final ComponentName component;
250 public final String callId;
251 public final String callerDisplayName;
252
253 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
254 String callerDisplayName) {
255 this.component = component;
256 this.callId = callId;
257 this.callerDisplayName = callerDisplayName;
258 }
259 }
260
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700261 /**
262 * A handler that processes messages on the main thread in the phone process. Since many
263 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
264 * inbound binder threads to the main thread in the phone process. The Binder thread
265 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
266 * on, which will be notified when the operation completes and will contain the result of the
267 * request.
268 *
269 * <p>If a MainThreadRequest object is provided in the msg.obj field,
270 * note that request.result must be set to something non-null for the calling thread to
271 * unblock.
272 */
273 private final class MainThreadHandler extends Handler {
274 @Override
275 public void handleMessage(Message msg) {
276 MainThreadRequest request;
277 Message onCompleted;
278 AsyncResult ar;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800279 UiccCard uiccCard;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700280 IccAPDUArgument iccArgument;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700281
282 switch (msg.what) {
pkanwar32d516d2016-10-14 19:37:38 -0700283 case CMD_HANDLE_USSD_REQUEST: {
284 request = (MainThreadRequest) msg.obj;
285 final Phone phone = getPhoneFromRequest(request);
286 Pair<String, ResultReceiver> ussdObject = (Pair) request.argument;
287 String ussdRequest = ussdObject.first;
288 ResultReceiver wrappedCallback = ussdObject.second;
Tyler Gunn52dcf772017-04-26 11:30:31 -0700289 try {
290 request.result = phone != null ?
291 phone.handleUssdRequest(ussdRequest, wrappedCallback)
292 : false;
293 } catch (CallStateException cse) {
294 request.result = false;
295 }
pkanwar32d516d2016-10-14 19:37:38 -0700296 // Wake up the requesting thread
297 synchronized (request) {
298 request.notifyAll();
299 }
300 break;
301 }
302
Yorke Lee716f67e2015-06-17 15:39:16 -0700303 case CMD_HANDLE_PIN_MMI: {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700304 request = (MainThreadRequest) msg.obj;
Yorke Lee716f67e2015-06-17 15:39:16 -0700305 final Phone phone = getPhoneFromRequest(request);
306 request.result = phone != null ?
307 getPhoneFromRequest(request).handlePinMmi((String) request.argument)
308 : false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700309 // Wake up the requesting thread
310 synchronized (request) {
311 request.notifyAll();
312 }
313 break;
Yorke Lee716f67e2015-06-17 15:39:16 -0700314 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700315
316 case CMD_HANDLE_NEIGHBORING_CELL:
317 request = (MainThreadRequest) msg.obj;
318 onCompleted = obtainMessage(EVENT_NEIGHBORING_CELL_DONE,
319 request);
Sooraj Sasindran9a909312016-07-18 11:57:25 -0700320 mPhone.getNeighboringCids(onCompleted, (WorkSource)request.argument);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700321 break;
322
323 case EVENT_NEIGHBORING_CELL_DONE:
324 ar = (AsyncResult) msg.obj;
325 request = (MainThreadRequest) ar.userObj;
326 if (ar.exception == null && ar.result != null) {
327 request.result = ar.result;
328 } else {
329 // create an empty list to notify the waiting thread
Jake Hambye994d462014-02-03 13:10:13 -0800330 request.result = new ArrayList<NeighboringCellInfo>(0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700331 }
332 // Wake up the requesting thread
333 synchronized (request) {
334 request.notifyAll();
335 }
336 break;
337
338 case CMD_ANSWER_RINGING_CALL:
Wink Saville08874612014-08-31 19:19:58 -0700339 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800340 int answer_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700341 answerRingingCallInternal(answer_subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700342 break;
343
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700344 case CMD_END_CALL:
345 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800346 int end_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700347 final boolean hungUp;
Anthony Leeae4e36d2015-05-21 07:17:46 -0700348 Phone phone = getPhone(end_subId);
349 if (phone == null) {
350 if (DBG) log("CMD_END_CALL: no phone for id: " + end_subId);
351 break;
352 }
353 int phoneType = phone.getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700354 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
355 // CDMA: If the user presses the Power button we treat it as
356 // ending the complete call session
Wink Saville08874612014-08-31 19:19:58 -0700357 hungUp = PhoneUtils.hangupRingingAndActive(getPhone(end_subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700358 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
359 // GSM: End the call as per the Phone state
360 hungUp = PhoneUtils.hangup(mCM);
361 } else {
362 throw new IllegalStateException("Unexpected phone type: " + phoneType);
363 }
364 if (DBG) log("CMD_END_CALL: " + (hungUp ? "hung up!" : "no call to hang up"));
365 request.result = hungUp;
366 // Wake up the requesting thread
367 synchronized (request) {
368 request.notifyAll();
369 }
370 break;
371
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700372 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700373 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700374 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800375 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700376 if (uiccCard == null) {
377 loge("iccTransmitApduLogicalChannel: No UICC");
378 request.result = new IccIoResult(0x6F, 0, (byte[])null);
379 synchronized (request) {
380 request.notifyAll();
381 }
382 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700383 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
384 request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700385 uiccCard.iccTransmitApduLogicalChannel(
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700386 iccArgument.channel, iccArgument.cla, iccArgument.command,
387 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
Shishir Agrawal566b7612013-10-28 14:41:00 -0700388 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700389 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700390 break;
391
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700392 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700393 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("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800401 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700402 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800403 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700404 } else {
405 loge("iccTransmitApduLogicalChannel: Unknown exception");
406 }
407 }
408 synchronized (request) {
409 request.notifyAll();
410 }
411 break;
412
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700413 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
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("iccTransmitApduBasicChannel: No UICC");
419 request.result = new IccIoResult(0x6F, 0, (byte[])null);
420 synchronized (request) {
421 request.notifyAll();
422 }
423 } else {
424 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
425 request);
426 uiccCard.iccTransmitApduBasicChannel(
427 iccArgument.cla, iccArgument.command, iccArgument.p1, iccArgument.p2,
428 iccArgument.p3, iccArgument.data, onCompleted);
429 }
430 break;
431
432 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_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 if (ar.result == null) {
440 loge("iccTransmitApduBasicChannel: Empty response");
441 } else if (ar.exception instanceof CommandException) {
442 loge("iccTransmitApduBasicChannel: CommandException: " +
443 ar.exception);
444 } else {
445 loge("iccTransmitApduBasicChannel: Unknown exception");
446 }
447 }
448 synchronized (request) {
449 request.notifyAll();
450 }
451 break;
452
453 case CMD_EXCHANGE_SIM_IO:
454 request = (MainThreadRequest) msg.obj;
455 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800456 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700457 if (uiccCard == null) {
458 loge("iccExchangeSimIO: No UICC");
459 request.result = new IccIoResult(0x6F, 0, (byte[])null);
460 synchronized (request) {
461 request.notifyAll();
462 }
463 } else {
464 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
465 request);
466 uiccCard.iccExchangeSimIO(iccArgument.cla, /* fileID */
467 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
468 iccArgument.data, onCompleted);
469 }
470 break;
471
472 case EVENT_EXCHANGE_SIM_IO_DONE:
473 ar = (AsyncResult) msg.obj;
474 request = (MainThreadRequest) ar.userObj;
475 if (ar.exception == null && ar.result != null) {
476 request.result = ar.result;
477 } else {
478 request.result = new IccIoResult(0x6f, 0, (byte[])null);
479 }
480 synchronized (request) {
481 request.notifyAll();
482 }
483 break;
484
Derek Tan4d5e5c12014-02-04 11:54:58 -0800485 case CMD_SEND_ENVELOPE:
486 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800487 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700488 if (uiccCard == null) {
489 loge("sendEnvelopeWithStatus: No UICC");
490 request.result = new IccIoResult(0x6F, 0, (byte[])null);
491 synchronized (request) {
492 request.notifyAll();
493 }
494 } else {
495 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
496 uiccCard.sendEnvelopeWithStatus((String)request.argument, onCompleted);
497 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800498 break;
499
500 case EVENT_SEND_ENVELOPE_DONE:
501 ar = (AsyncResult) msg.obj;
502 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700503 if (ar.exception == null && ar.result != null) {
504 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800505 } else {
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700506 request.result = new IccIoResult(0x6F, 0, (byte[])null);
507 if (ar.result == null) {
508 loge("sendEnvelopeWithStatus: Empty response");
509 } else if (ar.exception instanceof CommandException) {
510 loge("sendEnvelopeWithStatus: CommandException: " +
511 ar.exception);
512 } else {
513 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
514 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800515 }
516 synchronized (request) {
517 request.notifyAll();
518 }
519 break;
520
Shishir Agrawal566b7612013-10-28 14:41:00 -0700521 case CMD_OPEN_CHANNEL:
522 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800523 uiccCard = getUiccCardFromRequest(request);
Ajay Nambid7454d32015-12-03 13:50:00 -0800524 Pair<String, Integer> openChannelArgs = (Pair<String, Integer>) request.argument;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700525 if (uiccCard == null) {
526 loge("iccOpenLogicalChannel: No UICC");
Shishir Agrawalfc0492a2016-02-17 11:15:33 -0800527 request.result = new IccOpenLogicalChannelResponse(-1,
528 IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE, null);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700529 synchronized (request) {
530 request.notifyAll();
531 }
532 } else {
533 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
Ajay Nambid7454d32015-12-03 13:50:00 -0800534 uiccCard.iccOpenLogicalChannel(openChannelArgs.first,
535 openChannelArgs.second, onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700536 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700537 break;
538
539 case EVENT_OPEN_CHANNEL_DONE:
540 ar = (AsyncResult) msg.obj;
541 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700542 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700543 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700544 int[] result = (int[]) ar.result;
545 int channelId = result[0];
546 byte[] selectResponse = null;
547 if (result.length > 1) {
548 selectResponse = new byte[result.length - 1];
549 for (int i = 1; i < result.length; ++i) {
550 selectResponse[i - 1] = (byte) result[i];
551 }
552 }
553 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700554 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700555 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700556 if (ar.result == null) {
557 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700558 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700559 if (ar.exception != null) {
560 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
561 }
562
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700563 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Junda Liua754ba12015-05-20 01:17:52 -0700564 if (ar.exception instanceof CommandException) {
565 CommandException.Error error =
566 ((CommandException) (ar.exception)).getCommandError();
567 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700568 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700569 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700570 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700571 }
572 }
573 openChannelResp = new IccOpenLogicalChannelResponse(
574 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700575 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700576 request.result = openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700577 synchronized (request) {
578 request.notifyAll();
579 }
580 break;
581
582 case CMD_CLOSE_CHANNEL:
583 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800584 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700585 if (uiccCard == null) {
586 loge("iccCloseLogicalChannel: No UICC");
587 request.result = new IccIoResult(0x6F, 0, (byte[])null);
588 synchronized (request) {
589 request.notifyAll();
590 }
591 } else {
592 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
593 uiccCard.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
594 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700595 break;
596
597 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800598 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
599 break;
600
601 case CMD_NV_READ_ITEM:
602 request = (MainThreadRequest) msg.obj;
603 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
604 mPhone.nvReadItem((Integer) request.argument, onCompleted);
605 break;
606
607 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700608 ar = (AsyncResult) msg.obj;
609 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800610 if (ar.exception == null && ar.result != null) {
611 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700612 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800613 request.result = "";
614 if (ar.result == null) {
615 loge("nvReadItem: Empty response");
616 } else if (ar.exception instanceof CommandException) {
617 loge("nvReadItem: CommandException: " +
618 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700619 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800620 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700621 }
622 }
623 synchronized (request) {
624 request.notifyAll();
625 }
626 break;
627
Jake Hambye994d462014-02-03 13:10:13 -0800628 case CMD_NV_WRITE_ITEM:
629 request = (MainThreadRequest) msg.obj;
630 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
631 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
632 mPhone.nvWriteItem(idValue.first, idValue.second, onCompleted);
633 break;
634
635 case EVENT_NV_WRITE_ITEM_DONE:
636 handleNullReturnEvent(msg, "nvWriteItem");
637 break;
638
639 case CMD_NV_WRITE_CDMA_PRL:
640 request = (MainThreadRequest) msg.obj;
641 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
642 mPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
643 break;
644
645 case EVENT_NV_WRITE_CDMA_PRL_DONE:
646 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
647 break;
648
649 case CMD_NV_RESET_CONFIG:
650 request = (MainThreadRequest) msg.obj;
651 onCompleted = obtainMessage(EVENT_NV_RESET_CONFIG_DONE, request);
652 mPhone.nvResetConfig((Integer) request.argument, onCompleted);
653 break;
654
655 case EVENT_NV_RESET_CONFIG_DONE:
656 handleNullReturnEvent(msg, "nvResetConfig");
657 break;
658
Jake Hamby7c27be32014-03-03 13:25:59 -0800659 case CMD_GET_PREFERRED_NETWORK_TYPE:
660 request = (MainThreadRequest) msg.obj;
661 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
Stuart Scott54788802015-03-30 13:18:01 -0700662 getPhoneFromRequest(request).getPreferredNetworkType(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800663 break;
664
665 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
666 ar = (AsyncResult) msg.obj;
667 request = (MainThreadRequest) ar.userObj;
668 if (ar.exception == null && ar.result != null) {
669 request.result = ar.result; // Integer
670 } else {
Sanket Padawecfc2d352016-01-05 19:52:14 -0800671 request.result = null;
Jake Hamby7c27be32014-03-03 13:25:59 -0800672 if (ar.result == null) {
673 loge("getPreferredNetworkType: Empty response");
674 } else if (ar.exception instanceof CommandException) {
675 loge("getPreferredNetworkType: CommandException: " +
676 ar.exception);
677 } else {
678 loge("getPreferredNetworkType: Unknown exception");
679 }
680 }
681 synchronized (request) {
682 request.notifyAll();
683 }
684 break;
685
686 case CMD_SET_PREFERRED_NETWORK_TYPE:
687 request = (MainThreadRequest) msg.obj;
688 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
689 int networkType = (Integer) request.argument;
Stuart Scott54788802015-03-30 13:18:01 -0700690 getPhoneFromRequest(request).setPreferredNetworkType(networkType, onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800691 break;
692
693 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
694 handleNullReturnEvent(msg, "setPreferredNetworkType");
695 break;
696
Steven Liu4bf01bc2014-07-17 11:05:29 -0500697 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
698 request = (MainThreadRequest)msg.obj;
699 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
700 mPhone.invokeOemRilRequestRaw((byte[])request.argument, onCompleted);
701 break;
702
703 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
704 ar = (AsyncResult)msg.obj;
705 request = (MainThreadRequest)ar.userObj;
706 request.result = ar;
707 synchronized (request) {
708 request.notifyAll();
709 }
710 break;
711
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800712 case CMD_SET_VOICEMAIL_NUMBER:
713 request = (MainThreadRequest) msg.obj;
714 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
715 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -0800716 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
717 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800718 break;
719
720 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
721 handleNullReturnEvent(msg, "setVoicemailNumber");
722 break;
723
Stuart Scott54788802015-03-30 13:18:01 -0700724 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
725 request = (MainThreadRequest) msg.obj;
726 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
727 request);
728 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
729 break;
730
731 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
732 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
733 break;
734
Shishir Agrawal302c8692015-06-19 13:49:39 -0700735 case CMD_PERFORM_NETWORK_SCAN:
736 request = (MainThreadRequest) msg.obj;
737 onCompleted = obtainMessage(EVENT_PERFORM_NETWORK_SCAN_DONE, request);
738 getPhoneFromRequest(request).getAvailableNetworks(onCompleted);
739 break;
740
741 case EVENT_PERFORM_NETWORK_SCAN_DONE:
742 ar = (AsyncResult) msg.obj;
743 request = (MainThreadRequest) ar.userObj;
744 CellNetworkScanResult cellScanResult;
745 if (ar.exception == null && ar.result != null) {
746 cellScanResult = new CellNetworkScanResult(
747 CellNetworkScanResult.STATUS_SUCCESS,
748 (List<OperatorInfo>) ar.result);
749 } else {
750 if (ar.result == null) {
751 loge("getCellNetworkScanResults: Empty response");
752 }
753 if (ar.exception != null) {
754 loge("getCellNetworkScanResults: Exception: " + ar.exception);
755 }
756 int errorCode = CellNetworkScanResult.STATUS_UNKNOWN_ERROR;
757 if (ar.exception instanceof CommandException) {
758 CommandException.Error error =
759 ((CommandException) (ar.exception)).getCommandError();
760 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
761 errorCode = CellNetworkScanResult.STATUS_RADIO_NOT_AVAILABLE;
762 } else if (error == CommandException.Error.GENERIC_FAILURE) {
763 errorCode = CellNetworkScanResult.STATUS_RADIO_GENERIC_FAILURE;
764 }
765 }
766 cellScanResult = new CellNetworkScanResult(errorCode, null);
767 }
768 request.result = cellScanResult;
769 synchronized (request) {
770 request.notifyAll();
771 }
772 break;
773
774 case CMD_SET_NETWORK_SELECTION_MODE_MANUAL:
775 request = (MainThreadRequest) msg.obj;
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700776 ManualNetworkSelectionArgument selArg =
777 (ManualNetworkSelectionArgument) request.argument;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700778 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE,
779 request);
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700780 getPhoneFromRequest(request).selectNetworkManually(selArg.operatorInfo,
781 selArg.persistSelection, onCompleted);
Shishir Agrawal302c8692015-06-19 13:49:39 -0700782 break;
783
784 case EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE:
785 handleNullReturnEvent(msg, "setNetworkSelectionModeManual");
786 break;
787
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700788 case CMD_GET_MODEM_ACTIVITY_INFO:
789 request = (MainThreadRequest) msg.obj;
790 onCompleted = obtainMessage(EVENT_GET_MODEM_ACTIVITY_INFO_DONE, request);
Prerepa Viswanadham61a60ad2015-06-08 18:07:51 -0700791 mPhone.getModemActivityInfo(onCompleted);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700792 break;
793
794 case EVENT_GET_MODEM_ACTIVITY_INFO_DONE:
795 ar = (AsyncResult) msg.obj;
796 request = (MainThreadRequest) ar.userObj;
797 if (ar.exception == null && ar.result != null) {
798 request.result = ar.result;
799 } else {
800 if (ar.result == null) {
801 loge("queryModemActivityInfo: Empty response");
802 } else if (ar.exception instanceof CommandException) {
803 loge("queryModemActivityInfo: CommandException: " +
804 ar.exception);
805 } else {
806 loge("queryModemActivityInfo: Unknown exception");
807 }
808 }
Amit Mahajand4766222016-01-28 15:28:28 -0800809 // Result cannot be null. Return ModemActivityInfo with all fields set to 0.
810 if (request.result == null) {
811 request.result = new ModemActivityInfo(0, 0, 0, null, 0, 0);
812 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700813 synchronized (request) {
814 request.notifyAll();
815 }
816 break;
817
Meng Wang1a7c35a2016-05-05 20:56:15 -0700818 case CMD_SET_ALLOWED_CARRIERS:
819 request = (MainThreadRequest) msg.obj;
820 onCompleted = obtainMessage(EVENT_SET_ALLOWED_CARRIERS_DONE, request);
821 mPhone.setAllowedCarriers(
822 (List<CarrierIdentifier>) request.argument,
823 onCompleted);
824 break;
825
826 case EVENT_SET_ALLOWED_CARRIERS_DONE:
827 ar = (AsyncResult) msg.obj;
828 request = (MainThreadRequest) ar.userObj;
829 if (ar.exception == null && ar.result != null) {
830 request.result = ar.result;
831 } else {
832 if (ar.result == null) {
833 loge("setAllowedCarriers: Empty response");
834 } else if (ar.exception instanceof CommandException) {
835 loge("setAllowedCarriers: CommandException: " +
836 ar.exception);
837 } else {
838 loge("setAllowedCarriers: Unknown exception");
839 }
840 }
841 // Result cannot be null. Return -1 on error.
842 if (request.result == null) {
843 request.result = new int[]{-1};
844 }
845 synchronized (request) {
846 request.notifyAll();
847 }
848 break;
849
850 case CMD_GET_ALLOWED_CARRIERS:
851 request = (MainThreadRequest) msg.obj;
852 onCompleted = obtainMessage(EVENT_GET_ALLOWED_CARRIERS_DONE, request);
853 mPhone.getAllowedCarriers(onCompleted);
854 break;
855
856 case EVENT_GET_ALLOWED_CARRIERS_DONE:
857 ar = (AsyncResult) msg.obj;
858 request = (MainThreadRequest) ar.userObj;
859 if (ar.exception == null && ar.result != null) {
860 request.result = ar.result;
861 } else {
862 if (ar.result == null) {
863 loge("getAllowedCarriers: Empty response");
864 } else if (ar.exception instanceof CommandException) {
865 loge("getAllowedCarriers: CommandException: " +
866 ar.exception);
867 } else {
868 loge("getAllowedCarriers: Unknown exception");
869 }
870 }
871 // Result cannot be null. Return empty list of CarrierIdentifier.
872 if (request.result == null) {
873 request.result = new ArrayList<CarrierIdentifier>(0);
874 }
875 synchronized (request) {
876 request.notifyAll();
877 }
878 break;
879
Nathan Haroldb3014052017-01-25 15:57:32 -0800880 case EVENT_GET_FORBIDDEN_PLMNS_DONE:
881 ar = (AsyncResult) msg.obj;
882 request = (MainThreadRequest) ar.userObj;
883 if (ar.exception == null && ar.result != null) {
884 request.result = ar.result;
885 } else {
886 request.result = new IllegalArgumentException(
887 "Failed to retrieve Forbidden Plmns");
888 if (ar.result == null) {
889 loge("getForbiddenPlmns: Empty response");
890 } else {
891 loge("getForbiddenPlmns: Unknown exception");
892 }
893 }
894 synchronized (request) {
895 request.notifyAll();
896 }
897 break;
898
899 case CMD_GET_FORBIDDEN_PLMNS:
900 request = (MainThreadRequest) msg.obj;
901 uiccCard = getUiccCardFromRequest(request);
902 if (uiccCard == null) {
903 loge("getForbiddenPlmns() UiccCard is null");
904 request.result = new IllegalArgumentException(
905 "getForbiddenPlmns() UiccCard is null");
906 synchronized (request) {
907 request.notifyAll();
908 }
909 break;
910 }
911 Integer appType = (Integer) request.argument;
912 UiccCardApplication uiccApp = uiccCard.getApplicationByType(appType);
913 if (uiccApp == null) {
914 loge("getForbiddenPlmns() no app with specified type -- "
915 + appType);
916 request.result = new IllegalArgumentException("Failed to get UICC App");
917 synchronized (request) {
918 request.notifyAll();
919 }
920 break;
921 } else {
922 if (DBG) logv("getForbiddenPlmns() found app " + uiccApp.getAid()
923 + " specified type -- " + appType);
924 }
925 onCompleted = obtainMessage(EVENT_GET_FORBIDDEN_PLMNS_DONE, request);
926 ((SIMRecords) uiccApp.getIccRecords()).getForbiddenPlmns(
927 onCompleted);
928 break;
929
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700930 default:
931 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
932 break;
933 }
934 }
Jake Hambye994d462014-02-03 13:10:13 -0800935
936 private void handleNullReturnEvent(Message msg, String command) {
937 AsyncResult ar = (AsyncResult) msg.obj;
938 MainThreadRequest request = (MainThreadRequest) ar.userObj;
939 if (ar.exception == null) {
940 request.result = true;
941 } else {
942 request.result = false;
943 if (ar.exception instanceof CommandException) {
944 loge(command + ": CommandException: " + ar.exception);
945 } else {
946 loge(command + ": Unknown exception");
947 }
948 }
949 synchronized (request) {
950 request.notifyAll();
951 }
952 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700953 }
954
955 /**
956 * Posts the specified command to be executed on the main thread,
957 * waits for the request to complete, and returns the result.
958 * @see #sendRequestAsync
959 */
960 private Object sendRequest(int command, Object argument) {
Sanket Padawe56e75a32016-02-08 12:18:19 -0800961 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Wink Saville36469e72014-06-11 15:17:00 -0700962 }
963
964 /**
965 * Posts the specified command to be executed on the main thread,
966 * waits for the request to complete, and returns the result.
967 * @see #sendRequestAsync
968 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800969 private Object sendRequest(int command, Object argument, Integer subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700970 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
971 throw new RuntimeException("This method will deadlock if called from the main thread.");
972 }
973
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800974 MainThreadRequest request = new MainThreadRequest(argument, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700975 Message msg = mMainThreadHandler.obtainMessage(command, request);
976 msg.sendToTarget();
977
978 // Wait for the request to complete
979 synchronized (request) {
980 while (request.result == null) {
981 try {
982 request.wait();
983 } catch (InterruptedException e) {
984 // Do nothing, go back and wait until the request is complete
985 }
986 }
987 }
988 return request.result;
989 }
990
991 /**
992 * Asynchronous ("fire and forget") version of sendRequest():
993 * Posts the specified command to be executed on the main thread, and
994 * returns immediately.
995 * @see #sendRequest
996 */
997 private void sendRequestAsync(int command) {
998 mMainThreadHandler.sendEmptyMessage(command);
999 }
1000
1001 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001002 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
1003 * @see {@link #sendRequest(int,Object)}
1004 */
1005 private void sendRequestAsync(int command, Object argument) {
1006 MainThreadRequest request = new MainThreadRequest(argument);
1007 Message msg = mMainThreadHandler.obtainMessage(command, request);
1008 msg.sendToTarget();
1009 }
1010
1011 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001012 * Initialize the singleton PhoneInterfaceManager instance.
1013 * This is only done once, at startup, from PhoneApp.onCreate().
1014 */
Sailesh Nepal194161e2014-07-03 08:57:44 -07001015 /* package */ static PhoneInterfaceManager init(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001016 synchronized (PhoneInterfaceManager.class) {
1017 if (sInstance == null) {
Sailesh Nepal194161e2014-07-03 08:57:44 -07001018 sInstance = new PhoneInterfaceManager(app, phone);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001019 } else {
1020 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
1021 }
1022 return sInstance;
1023 }
1024 }
1025
1026 /** Private constructor; @see init() */
Sailesh Nepal194161e2014-07-03 08:57:44 -07001027 private PhoneInterfaceManager(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001028 mApp = app;
1029 mPhone = phone;
1030 mCM = PhoneGlobals.getInstance().mCM;
Stuart Scott981d8582015-04-21 14:09:50 -07001031 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001032 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
1033 mMainThreadHandler = new MainThreadHandler();
Andrew Leedf14ead2014-10-17 14:22:52 -07001034 mTelephonySharedPreferences =
Derek Tan97ebb422014-09-05 16:55:38 -07001035 PreferenceManager.getDefaultSharedPreferences(mPhone.getContext());
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001036 mSubscriptionController = SubscriptionController.getInstance();
Wink Saville3ab207e2014-11-20 13:07:20 -08001037
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001038 publish();
1039 }
1040
1041 private void publish() {
1042 if (DBG) log("publish: " + this);
1043
1044 ServiceManager.addService("phone", this);
1045 }
1046
Stuart Scott584921c2015-01-15 17:10:34 -08001047 private Phone getPhoneFromRequest(MainThreadRequest request) {
Sanket Padawe56e75a32016-02-08 12:18:19 -08001048 return (request.subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID)
1049 ? mPhone : getPhone(request.subId);
Stuart Scott584921c2015-01-15 17:10:34 -08001050 }
1051
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001052 private UiccCard getUiccCardFromRequest(MainThreadRequest request) {
1053 Phone phone = getPhoneFromRequest(request);
1054 return phone == null ? null :
1055 UiccController.getInstance().getUiccCard(phone.getPhoneId());
1056 }
1057
Wink Saville36469e72014-06-11 15:17:00 -07001058 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -07001059 private Phone getPhone(int subId) {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001060 return PhoneFactory.getPhone(mSubscriptionController.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -07001061 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001062 //
1063 // Implementation of the ITelephony interface.
1064 //
1065
1066 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001067 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -07001068 }
1069
Wink Savilleb564aae2014-10-23 10:18:09 -07001070 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001071 if (DBG) log("dial: " + number);
1072 // No permission check needed here: This is just a wrapper around the
1073 // ACTION_DIAL intent, which is available to any app since it puts up
1074 // the UI before it does anything.
1075
1076 String url = createTelUrl(number);
1077 if (url == null) {
1078 return;
1079 }
1080
1081 // PENDING: should we just silently fail if phone is offhook or ringing?
Wink Saville36469e72014-06-11 15:17:00 -07001082 PhoneConstants.State state = mCM.getState(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001083 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
1084 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
1085 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1086 mApp.startActivity(intent);
1087 }
1088 }
1089
1090 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001091 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -07001092 }
1093
Wink Savilleb564aae2014-10-23 10:18:09 -07001094 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001095 if (DBG) log("call: " + number);
1096
1097 // This is just a wrapper around the ACTION_CALL intent, but we still
1098 // need to do a permission check since we're calling startActivity()
1099 // from the context of the phone app.
1100 enforceCallPermission();
1101
1102 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
1103 != AppOpsManager.MODE_ALLOWED) {
1104 return;
1105 }
1106
1107 String url = createTelUrl(number);
1108 if (url == null) {
1109 return;
1110 }
1111
Wink Saville08874612014-08-31 19:19:58 -07001112 boolean isValid = false;
Narayan Kamath1c496c22015-04-16 14:40:19 +01001113 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoList();
Wink Saville3ab207e2014-11-20 13:07:20 -08001114 if (slist != null) {
1115 for (SubscriptionInfo subInfoRecord : slist) {
1116 if (subInfoRecord.getSubscriptionId() == subId) {
1117 isValid = true;
1118 break;
1119 }
Wink Saville08874612014-08-31 19:19:58 -07001120 }
1121 }
1122 if (isValid == false) {
1123 return;
1124 }
1125
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001126 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
Wink Saville36469e72014-06-11 15:17:00 -07001127 intent.putExtra(SUBSCRIPTION_KEY, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001128 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1129 mApp.startActivity(intent);
1130 }
1131
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001132 /**
1133 * End a call based on call state
1134 * @return true is a call was ended
1135 */
1136 public boolean endCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001137 return endCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001138 }
1139
1140 /**
1141 * End a call based on the call state of the subId
1142 * @return true is a call was ended
1143 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001144 public boolean endCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001145 enforceCallPermission();
Stuart Scott584921c2015-01-15 17:10:34 -08001146 return (Boolean) sendRequest(CMD_END_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001147 }
1148
1149 public void answerRingingCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001150 answerRingingCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001151 }
1152
Wink Savilleb564aae2014-10-23 10:18:09 -07001153 public void answerRingingCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001154 if (DBG) log("answerRingingCall...");
1155 // TODO: there should eventually be a separate "ANSWER_PHONE" permission,
1156 // but that can probably wait till the big TelephonyManager API overhaul.
1157 // For now, protect this call with the MODIFY_PHONE_STATE permission.
1158 enforceModifyPermission();
Stuart Scott584921c2015-01-15 17:10:34 -08001159 sendRequest(CMD_ANSWER_RINGING_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001160 }
1161
1162 /**
1163 * Make the actual telephony calls to implement answerRingingCall().
1164 * This should only be called from the main thread of the Phone app.
1165 * @see #answerRingingCall
1166 *
1167 * TODO: it would be nice to return true if we answered the call, or
1168 * false if there wasn't actually a ringing incoming call, or some
1169 * other error occurred. (In other words, pass back the return value
1170 * from PhoneUtils.answerCall() or PhoneUtils.answerAndEndActive().)
1171 * But that would require calling this method via sendRequest() rather
1172 * than sendRequestAsync(), and right now we don't actually *need* that
1173 * return value, so let's just return void for now.
1174 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001175 private void answerRingingCallInternal(int subId) {
Wink Saville08874612014-08-31 19:19:58 -07001176 final boolean hasRingingCall = !getPhone(subId).getRingingCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001177 if (hasRingingCall) {
Wink Saville08874612014-08-31 19:19:58 -07001178 final boolean hasActiveCall = !getPhone(subId).getForegroundCall().isIdle();
1179 final boolean hasHoldingCall = !getPhone(subId).getBackgroundCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001180 if (hasActiveCall && hasHoldingCall) {
1181 // Both lines are in use!
1182 // TODO: provide a flag to let the caller specify what
1183 // policy to use if both lines are in use. (The current
1184 // behavior is hardwired to "answer incoming, end ongoing",
1185 // which is how the CALL button is specced to behave.)
1186 PhoneUtils.answerAndEndActive(mCM, mCM.getFirstActiveRingingCall());
1187 return;
1188 } else {
1189 // answerCall() will automatically hold the current active
1190 // call, if there is one.
1191 PhoneUtils.answerCall(mCM.getFirstActiveRingingCall());
1192 return;
1193 }
1194 } else {
1195 // No call was ringing.
1196 return;
1197 }
1198 }
1199
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001200 /**
Santos Cordon5422a8d2014-09-12 04:20:56 -07001201 * This method is no longer used and can be removed once TelephonyManager stops referring to it.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001202 */
Santos Cordon5422a8d2014-09-12 04:20:56 -07001203 public void silenceRinger() {
1204 Log.e(LOG_TAG, "silenseRinger not supported");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001205 }
1206
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001207 @Override
1208 public boolean isOffhook(String callingPackage) {
1209 return isOffhookForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001210 }
1211
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001212 @Override
1213 public boolean isOffhookForSubscriber(int subId, String callingPackage) {
1214 if (!canReadPhoneState(callingPackage, "isOffhookForSubscriber")) {
1215 return false;
1216 }
1217
Sanket Padawe356d7632015-06-22 14:03:32 -07001218 final Phone phone = getPhone(subId);
1219 if (phone != null) {
1220 return (phone.getState() == PhoneConstants.State.OFFHOOK);
1221 } else {
1222 return false;
1223 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001224 }
1225
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001226 @Override
1227 public boolean isRinging(String callingPackage) {
1228 return (isRingingForSubscriber(getDefaultSubscription(), callingPackage));
Wink Saville36469e72014-06-11 15:17:00 -07001229 }
1230
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001231 @Override
1232 public boolean isRingingForSubscriber(int subId, String callingPackage) {
1233 if (!canReadPhoneState(callingPackage, "isRingingForSubscriber")) {
1234 return false;
1235 }
1236
Sanket Padawe356d7632015-06-22 14:03:32 -07001237 final Phone phone = getPhone(subId);
1238 if (phone != null) {
1239 return (phone.getState() == PhoneConstants.State.RINGING);
1240 } else {
1241 return false;
1242 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001243 }
1244
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001245 @Override
1246 public boolean isIdle(String callingPackage) {
1247 return isIdleForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001248 }
1249
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001250 @Override
1251 public boolean isIdleForSubscriber(int subId, String callingPackage) {
1252 if (!canReadPhoneState(callingPackage, "isIdleForSubscriber")) {
1253 return false;
1254 }
1255
Sanket Padawe356d7632015-06-22 14:03:32 -07001256 final Phone phone = getPhone(subId);
1257 if (phone != null) {
1258 return (phone.getState() == PhoneConstants.State.IDLE);
1259 } else {
1260 return false;
1261 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001262 }
1263
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001264 public boolean supplyPin(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001265 return supplyPinForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001266 }
1267
Wink Savilleb564aae2014-10-23 10:18:09 -07001268 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001269 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001270 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1271 }
1272
1273 public boolean supplyPuk(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001274 return supplyPukForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001275 }
1276
Wink Savilleb564aae2014-10-23 10:18:09 -07001277 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001278 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001279 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1280 }
1281
1282 /** {@hide} */
1283 public int[] supplyPinReportResult(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001284 return supplyPinReportResultForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001285 }
1286
Wink Savilleb564aae2014-10-23 10:18:09 -07001287 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001288 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001289 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001290 checkSimPin.start();
1291 return checkSimPin.unlockSim(null, pin);
1292 }
1293
Wink Saville9de0f752013-10-22 19:04:03 -07001294 /** {@hide} */
1295 public int[] supplyPukReportResult(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001296 return supplyPukReportResultForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001297 }
1298
Wink Savilleb564aae2014-10-23 10:18:09 -07001299 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001300 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001301 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001302 checkSimPuk.start();
1303 return checkSimPuk.unlockSim(puk, pin);
1304 }
1305
1306 /**
Wink Saville9de0f752013-10-22 19:04:03 -07001307 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001308 * a synchronous one.
1309 */
1310 private static class UnlockSim extends Thread {
1311
1312 private final IccCard mSimCard;
1313
1314 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07001315 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1316 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001317
1318 // For replies from SimCard interface
1319 private Handler mHandler;
1320
1321 // For async handler to identify request type
1322 private static final int SUPPLY_PIN_COMPLETE = 100;
1323
1324 public UnlockSim(IccCard simCard) {
1325 mSimCard = simCard;
1326 }
1327
1328 @Override
1329 public void run() {
1330 Looper.prepare();
1331 synchronized (UnlockSim.this) {
1332 mHandler = new Handler() {
1333 @Override
1334 public void handleMessage(Message msg) {
1335 AsyncResult ar = (AsyncResult) msg.obj;
1336 switch (msg.what) {
1337 case SUPPLY_PIN_COMPLETE:
1338 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
1339 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07001340 mRetryCount = msg.arg1;
1341 if (ar.exception != null) {
1342 if (ar.exception instanceof CommandException &&
1343 ((CommandException)(ar.exception)).getCommandError()
1344 == CommandException.Error.PASSWORD_INCORRECT) {
1345 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
1346 } else {
1347 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1348 }
1349 } else {
1350 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
1351 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001352 mDone = true;
1353 UnlockSim.this.notifyAll();
1354 }
1355 break;
1356 }
1357 }
1358 };
1359 UnlockSim.this.notifyAll();
1360 }
1361 Looper.loop();
1362 }
1363
1364 /*
1365 * Use PIN or PUK to unlock SIM card
1366 *
1367 * If PUK is null, unlock SIM card with PIN
1368 *
1369 * If PUK is not null, unlock SIM card with PUK and set PIN code
1370 */
Wink Saville9de0f752013-10-22 19:04:03 -07001371 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001372
1373 while (mHandler == null) {
1374 try {
1375 wait();
1376 } catch (InterruptedException e) {
1377 Thread.currentThread().interrupt();
1378 }
1379 }
1380 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
1381
1382 if (puk == null) {
1383 mSimCard.supplyPin(pin, callback);
1384 } else {
1385 mSimCard.supplyPuk(puk, pin, callback);
1386 }
1387
1388 while (!mDone) {
1389 try {
1390 Log.d(LOG_TAG, "wait for done");
1391 wait();
1392 } catch (InterruptedException e) {
1393 // Restore the interrupted status
1394 Thread.currentThread().interrupt();
1395 }
1396 }
1397 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07001398 int[] resultArray = new int[2];
1399 resultArray[0] = mResult;
1400 resultArray[1] = mRetryCount;
1401 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001402 }
1403 }
1404
1405 public void updateServiceLocation() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001406 updateServiceLocationForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001407
1408 }
1409
Wink Savilleb564aae2014-10-23 10:18:09 -07001410 public void updateServiceLocationForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001411 // No permission check needed here: this call is harmless, and it's
1412 // needed for the ServiceState.requestStateUpdate() call (which is
1413 // already intentionally exposed to 3rd parties.)
Sanket Padawe356d7632015-06-22 14:03:32 -07001414 final Phone phone = getPhone(subId);
1415 if (phone != null) {
1416 phone.updateServiceLocation();
1417 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001418 }
1419
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001420 @Override
1421 public boolean isRadioOn(String callingPackage) {
1422 return isRadioOnForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001423 }
1424
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001425 @Override
1426 public boolean isRadioOnForSubscriber(int subId, String callingPackage) {
1427 if (!canReadPhoneState(callingPackage, "isRadioOnForSubscriber")) {
1428 return false;
1429 }
1430 return isRadioOnForSubscriber(subId);
1431 }
1432
1433 private boolean isRadioOnForSubscriber(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001434 final Phone phone = getPhone(subId);
1435 if (phone != null) {
1436 return phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF;
1437 } else {
1438 return false;
1439 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001440 }
1441
1442 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001443 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001444
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001445 }
Wink Saville36469e72014-06-11 15:17:00 -07001446
Wink Savilleb564aae2014-10-23 10:18:09 -07001447 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001448 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001449 final Phone phone = getPhone(subId);
1450 if (phone != null) {
1451 phone.setRadioPower(!isRadioOnForSubscriber(subId));
1452 }
Wink Saville36469e72014-06-11 15:17:00 -07001453 }
1454
1455 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001456 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001457 }
1458
Wink Savilleb564aae2014-10-23 10:18:09 -07001459 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07001460 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001461 final Phone phone = getPhone(subId);
1462 if (phone == null) {
1463 return false;
1464 }
1465 if ((phone.getServiceState().getState() !=
Wink Saville36469e72014-06-11 15:17:00 -07001466 ServiceState.STATE_POWER_OFF) != turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001467 toggleRadioOnOffForSubscriber(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001468 }
1469 return true;
1470 }
Wink Saville36469e72014-06-11 15:17:00 -07001471
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001472 public boolean needMobileRadioShutdown() {
1473 /*
1474 * If any of the Radios are available, it will need to be
1475 * shutdown. So return true if any Radio is available.
1476 */
1477 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1478 Phone phone = PhoneFactory.getPhone(i);
1479 if (phone != null && phone.isRadioAvailable()) return true;
1480 }
1481 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
1482 return false;
1483 }
1484
1485 public void shutdownMobileRadios() {
1486 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1487 logv("Shutting down Phone " + i);
1488 shutdownRadioUsingPhoneId(i);
1489 }
1490 }
1491
1492 private void shutdownRadioUsingPhoneId(int phoneId) {
1493 enforceModifyPermission();
1494 Phone phone = PhoneFactory.getPhone(phoneId);
1495 if (phone != null && phone.isRadioAvailable()) {
1496 phone.shutdownRadio();
1497 }
1498 }
1499
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001500 public boolean setRadioPower(boolean turnOn) {
Wei Liu9ae2a062016-08-08 11:09:34 -07001501 final Phone defaultPhone = PhoneFactory.getDefaultPhone();
1502 if (defaultPhone != null) {
1503 defaultPhone.setRadioPower(turnOn);
1504 return true;
1505 } else {
1506 loge("There's no default phone.");
1507 return false;
1508 }
Wink Saville36469e72014-06-11 15:17:00 -07001509 }
1510
Wink Savilleb564aae2014-10-23 10:18:09 -07001511 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001512 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001513 final Phone phone = getPhone(subId);
1514 if (phone != null) {
1515 phone.setRadioPower(turnOn);
1516 return true;
1517 } else {
1518 return false;
1519 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001520 }
1521
Wink Saville36469e72014-06-11 15:17:00 -07001522 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001523 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001524 public boolean enableDataConnectivity() {
1525 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001526 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001527 final Phone phone = getPhone(subId);
1528 if (phone != null) {
1529 phone.setDataEnabled(true);
1530 return true;
1531 } else {
1532 return false;
1533 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001534 }
1535
Wink Saville36469e72014-06-11 15:17:00 -07001536 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001537 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001538 public boolean disableDataConnectivity() {
1539 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001540 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001541 final Phone phone = getPhone(subId);
1542 if (phone != null) {
1543 phone.setDataEnabled(false);
1544 return true;
1545 } else {
1546 return false;
1547 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001548 }
1549
Sanket Padawe356d7632015-06-22 14:03:32 -07001550 @Override
Jack Yuacf8a132017-05-01 17:00:48 -07001551 public boolean isDataConnectivityPossible(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001552 final Phone phone = getPhone(subId);
1553 if (phone != null) {
Jack Yuacf8a132017-05-01 17:00:48 -07001554 return phone.isDataAllowed();
Sanket Padawe356d7632015-06-22 14:03:32 -07001555 } else {
1556 return false;
1557 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001558 }
1559
1560 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001561 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07001562 }
1563
pkanwarae03a6b2016-11-06 20:37:09 -08001564 public void handleUssdRequest(int subId, String ussdRequest, ResultReceiver wrappedCallback) {
pkanwar32d516d2016-10-14 19:37:38 -07001565 enforceCallPermission();
pkanwar32d516d2016-10-14 19:37:38 -07001566 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1567 return;
1568 }
1569 Pair<String, ResultReceiver> ussdObject = new Pair(ussdRequest, wrappedCallback);
1570 sendRequest(CMD_HANDLE_USSD_REQUEST, ussdObject, subId);
1571 };
1572
Wink Savilleb564aae2014-10-23 10:18:09 -07001573 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001574 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001575 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1576 return false;
1577 }
Wink Saville36469e72014-06-11 15:17:00 -07001578 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001579 }
1580
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001581 public int getCallState() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001582 return getCallStateForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001583 }
1584
Sanket Padawe13bac7b2017-03-20 15:04:47 -07001585 public int getCallStateForSlot(int slotIndex) {
1586 Phone phone = PhoneFactory.getPhone(slotIndex);
Shishir Agrawala9f32182016-04-12 12:00:16 -07001587 return phone == null ? TelephonyManager.CALL_STATE_IDLE :
Nathan Harolda667c152016-12-14 11:27:20 -08001588 PhoneConstantConversions.convertCallState(phone.getState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001589 }
1590
Sanket Padawe356d7632015-06-22 14:03:32 -07001591 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001592 public int getDataState() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001593 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001594 if (phone != null) {
Nathan Harolda667c152016-12-14 11:27:20 -08001595 return PhoneConstantConversions.convertDataState(phone.getDataConnectionState());
Sanket Padawe356d7632015-06-22 14:03:32 -07001596 } else {
Nathan Harolda667c152016-12-14 11:27:20 -08001597 return PhoneConstantConversions.convertDataState(PhoneConstants.DataState.DISCONNECTED);
Sanket Padawe356d7632015-06-22 14:03:32 -07001598 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001599 }
1600
Sanket Padawe356d7632015-06-22 14:03:32 -07001601 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001602 public int getDataActivity() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001603 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001604 if (phone != null) {
1605 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
1606 } else {
1607 return TelephonyManager.DATA_ACTIVITY_NONE;
1608 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001609 }
1610
1611 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001612 public Bundle getCellLocation(String callingPackage) {
1613 enforceFineOrCoarseLocationPermission("getCellLocation");
1614
1615 // OP_COARSE_LOCATION controls both fine and coarse location.
1616 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1617 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001618 log("getCellLocation: returning null; mode != allowed");
Svetoslav64fad262015-04-14 14:35:21 -07001619 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001620 }
1621
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001622 if (checkIfCallerIsSelfOrForegroundUser() ||
1623 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001624 if (DBG_LOC) log("getCellLocation: is active user");
1625 Bundle data = new Bundle();
Legler Wu2c01cdf2014-12-08 19:00:59 +08001626 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001627 if (phone == null) {
1628 return null;
1629 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001630
1631 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
1632 phone.getCellLocation(workSource).fillInNotifierBundle(data);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001633 return data;
1634 } else {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001635 log("getCellLocation: suppress non-active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001636 return null;
1637 }
1638 }
1639
Svetoslav64fad262015-04-14 14:35:21 -07001640 private void enforceFineOrCoarseLocationPermission(String message) {
1641 try {
1642 mApp.enforceCallingOrSelfPermission(
1643 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1644 } catch (SecurityException e) {
1645 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1646 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1647 // is the weaker precondition
1648 mApp.enforceCallingOrSelfPermission(
1649 android.Manifest.permission.ACCESS_COARSE_LOCATION, message);
1650 }
1651 }
1652
1653
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001654 @Override
1655 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001656 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001657 }
1658
Sanket Padawe356d7632015-06-22 14:03:32 -07001659 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001660 public void enableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001661 mApp.enforceCallingOrSelfPermission(
1662 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001663 final Phone phone = getPhone(subId);
1664 if (phone != null) {
1665 phone.enableLocationUpdates();
1666 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001667 }
1668
1669 @Override
1670 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001671 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001672 }
1673
Sanket Padawe356d7632015-06-22 14:03:32 -07001674 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001675 public void disableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001676 mApp.enforceCallingOrSelfPermission(
1677 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001678 final Phone phone = getPhone(subId);
1679 if (phone != null) {
1680 phone.disableLocationUpdates();
1681 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001682 }
1683
1684 @Override
1685 @SuppressWarnings("unchecked")
1686 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
Svetoslav64fad262015-04-14 14:35:21 -07001687 enforceFineOrCoarseLocationPermission("getNeighboringCellInfo");
1688
1689 // OP_COARSE_LOCATION controls both fine and coarse location.
1690 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1691 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1692 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001693 }
1694
1695 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
1696 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1697 return null;
1698 }
Svetoslav64fad262015-04-14 14:35:21 -07001699
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001700 if (checkIfCallerIsSelfOrForegroundUser() ||
1701 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001702 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
1703
1704 ArrayList<NeighboringCellInfo> cells = null;
1705
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001706 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001707 try {
1708 cells = (ArrayList<NeighboringCellInfo>) sendRequest(
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001709 CMD_HANDLE_NEIGHBORING_CELL, workSource,
Sanket Padawe56e75a32016-02-08 12:18:19 -08001710 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001711 } catch (RuntimeException e) {
Wink Saville36469e72014-06-11 15:17:00 -07001712 Log.e(LOG_TAG, "getNeighboringCellInfo " + e);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001713 }
1714 return cells;
1715 } else {
1716 if (DBG_LOC) log("getNeighboringCellInfo: suppress non-active user");
1717 return null;
1718 }
1719 }
1720
1721
1722 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001723 public List<CellInfo> getAllCellInfo(String callingPackage) {
1724 enforceFineOrCoarseLocationPermission("getAllCellInfo");
1725
1726 // OP_COARSE_LOCATION controls both fine and coarse location.
1727 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1728 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1729 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001730 }
1731
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001732 if (checkIfCallerIsSelfOrForegroundUser() ||
1733 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001734 if (DBG_LOC) log("getAllCellInfo: is active user");
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001735 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
Legler Wu2c01cdf2014-12-08 19:00:59 +08001736 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
1737 for (Phone phone : PhoneFactory.getPhones()) {
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001738 final List<CellInfo> info = phone.getAllCellInfo(workSource);
1739 if (info != null) cellInfos.addAll(info);
Legler Wu2c01cdf2014-12-08 19:00:59 +08001740 }
1741 return cellInfos;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001742 } else {
1743 if (DBG_LOC) log("getAllCellInfo: suppress non-active user");
1744 return null;
1745 }
1746 }
1747
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001748 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001749 public void setCellInfoListRate(int rateInMillis) {
Jack Yua8d8cb82017-01-16 10:15:34 -08001750 enforceModifyPermission();
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001751 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
1752 mPhone.setCellInfoListRate(rateInMillis, workSource);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001753 }
1754
Shishir Agrawala9f32182016-04-12 12:00:16 -07001755 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07001756 public String getImeiForSlot(int slotIndex, String callingPackage) {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001757 if (!canReadPhoneState(callingPackage, "getImeiForSlot")) {
1758 return null;
1759 }
Sanket Padawe13bac7b2017-03-20 15:04:47 -07001760 Phone phone = PhoneFactory.getPhone(slotIndex);
Shishir Agrawala9f32182016-04-12 12:00:16 -07001761 return phone == null ? null : phone.getImei();
1762 }
1763
1764 @Override
Jack Yu2af8d712017-03-15 17:14:14 -07001765 public String getMeidForSlot(int slotIndex, String callingPackage) {
1766 if (!canReadPhoneState(callingPackage, "getMeidForSlot")) {
1767 return null;
1768 }
1769 Phone phone = PhoneFactory.getPhone(slotIndex);
1770 return phone == null ? null : phone.getMeid();
1771 }
1772
1773 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07001774 public String getDeviceSoftwareVersionForSlot(int slotIndex, String callingPackage) {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001775 if (!canReadPhoneState(callingPackage, "getDeviceSoftwareVersionForSlot")) {
1776 return null;
1777 }
Sanket Padawe13bac7b2017-03-20 15:04:47 -07001778 Phone phone = PhoneFactory.getPhone(slotIndex);
Shishir Agrawala9f32182016-04-12 12:00:16 -07001779 return phone == null ? null : phone.getDeviceSvn();
1780 }
1781
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001782 //
1783 // Internal helper methods.
1784 //
1785
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001786 /**
1787 * Returns true if the caller holds INTERACT_ACROSS_USERS_FULL.
1788 */
1789 private boolean checkCallerInteractAcrossUsersFull() {
1790 return mPhone.getContext().checkCallingOrSelfPermission(
1791 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1792 == PackageManager.PERMISSION_GRANTED;
1793 }
1794
Jake Hambye994d462014-02-03 13:10:13 -08001795 private static boolean checkIfCallerIsSelfOrForegroundUser() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001796 boolean ok;
1797
1798 boolean self = Binder.getCallingUid() == Process.myUid();
1799 if (!self) {
1800 // Get the caller's user id then clear the calling identity
1801 // which will be restored in the finally clause.
1802 int callingUser = UserHandle.getCallingUserId();
1803 long ident = Binder.clearCallingIdentity();
1804
1805 try {
1806 // With calling identity cleared the current user is the foreground user.
1807 int foregroundUser = ActivityManager.getCurrentUser();
1808 ok = (foregroundUser == callingUser);
1809 if (DBG_LOC) {
1810 log("checkIfCallerIsSelfOrForegoundUser: foregroundUser=" + foregroundUser
1811 + " callingUser=" + callingUser + " ok=" + ok);
1812 }
1813 } catch (Exception ex) {
1814 if (DBG_LOC) loge("checkIfCallerIsSelfOrForegoundUser: Exception ex=" + ex);
1815 ok = false;
1816 } finally {
1817 Binder.restoreCallingIdentity(ident);
1818 }
1819 } else {
1820 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: is self");
1821 ok = true;
1822 }
1823 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: ret=" + ok);
1824 return ok;
1825 }
1826
1827 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001828 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1829 *
1830 * @throws SecurityException if the caller does not have the required permission
1831 */
1832 private void enforceModifyPermission() {
1833 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1834 }
1835
1836 /**
Junda Liua2e36012014-07-09 18:30:01 -07001837 * Make sure either system app or the caller has carrier privilege.
1838 *
1839 * @throws SecurityException if the caller does not have the required permission/privilege
1840 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001841 private void enforceModifyPermissionOrCarrierPrivilege(int subId) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001842 int permission = mApp.checkCallingOrSelfPermission(
1843 android.Manifest.permission.MODIFY_PHONE_STATE);
1844 if (permission == PackageManager.PERMISSION_GRANTED) {
1845 return;
1846 }
1847
1848 log("No modify permission, check carrier privilege next.");
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001849 enforceCarrierPrivilege(subId);
Junda Liua2e36012014-07-09 18:30:01 -07001850 }
1851
1852 /**
1853 * Make sure the caller has carrier privilege.
1854 *
1855 * @throws SecurityException if the caller does not have the required permission
1856 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001857 private void enforceCarrierPrivilege(int subId) {
1858 if (getCarrierPrivilegeStatus(subId) !=
1859 TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001860 loge("No Carrier Privilege.");
1861 throw new SecurityException("No Carrier Privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001862 }
1863 }
1864
1865 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001866 * Make sure the caller has the CALL_PHONE permission.
1867 *
1868 * @throws SecurityException if the caller does not have the required permission
1869 */
1870 private void enforceCallPermission() {
1871 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1872 }
1873
Stuart Scott8eef64f2015-04-08 15:13:54 -07001874 private void enforceConnectivityInternalPermission() {
1875 mApp.enforceCallingOrSelfPermission(
1876 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1877 "ConnectivityService");
1878 }
1879
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001880 private String createTelUrl(String number) {
1881 if (TextUtils.isEmpty(number)) {
1882 return null;
1883 }
1884
Jake Hambye994d462014-02-03 13:10:13 -08001885 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001886 }
1887
Ihab Awadf9e92732013-12-05 18:02:52 -08001888 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001889 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1890 }
1891
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001892 private static void logv(String msg) {
1893 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
1894 }
1895
Ihab Awadf9e92732013-12-05 18:02:52 -08001896 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001897 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1898 }
1899
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001900 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001901 public int getActivePhoneType() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001902 return getActivePhoneTypeForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001903 }
1904
Sanket Padawe356d7632015-06-22 14:03:32 -07001905 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07001906 public int getActivePhoneTypeForSlot(int slotIndex) {
1907 final Phone phone = PhoneFactory.getPhone(slotIndex);
Sanket Padawe356d7632015-06-22 14:03:32 -07001908 if (phone == null) {
1909 return PhoneConstants.PHONE_TYPE_NONE;
1910 } else {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001911 return phone.getPhoneType();
Sanket Padawe356d7632015-06-22 14:03:32 -07001912 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001913 }
1914
1915 /**
1916 * Returns the CDMA ERI icon index to display
1917 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001918 @Override
1919 public int getCdmaEriIconIndex(String callingPackage) {
1920 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001921 }
1922
Sanket Padawe356d7632015-06-22 14:03:32 -07001923 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001924 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage) {
1925 if (!canReadPhoneState(callingPackage, "getCdmaEriIconIndexForSubscriber")) {
1926 return -1;
1927 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001928 final Phone phone = getPhone(subId);
1929 if (phone != null) {
1930 return phone.getCdmaEriIconIndex();
1931 } else {
1932 return -1;
1933 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001934 }
1935
1936 /**
1937 * Returns the CDMA ERI icon mode,
1938 * 0 - ON
1939 * 1 - FLASHING
1940 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001941 @Override
1942 public int getCdmaEriIconMode(String callingPackage) {
1943 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001944 }
1945
Sanket Padawe356d7632015-06-22 14:03:32 -07001946 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001947 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage) {
1948 if (!canReadPhoneState(callingPackage, "getCdmaEriIconModeForSubscriber")) {
1949 return -1;
1950 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001951 final Phone phone = getPhone(subId);
1952 if (phone != null) {
1953 return phone.getCdmaEriIconMode();
1954 } else {
1955 return -1;
1956 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001957 }
1958
1959 /**
1960 * Returns the CDMA ERI text,
1961 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001962 @Override
1963 public String getCdmaEriText(String callingPackage) {
1964 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001965 }
1966
Sanket Padawe356d7632015-06-22 14:03:32 -07001967 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001968 public String getCdmaEriTextForSubscriber(int subId, String callingPackage) {
1969 if (!canReadPhoneState(callingPackage, "getCdmaEriIconTextForSubscriber")) {
1970 return null;
1971 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001972 final Phone phone = getPhone(subId);
1973 if (phone != null) {
1974 return phone.getCdmaEriText();
1975 } else {
1976 return null;
1977 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001978 }
1979
1980 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07001981 * Returns the CDMA MDN.
1982 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001983 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001984 public String getCdmaMdn(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001985 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001986 final Phone phone = getPhone(subId);
1987 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA && phone != null) {
1988 return phone.getLine1Number();
Junda Liuca05d5d2014-08-14 22:36:34 -07001989 } else {
1990 return null;
1991 }
1992 }
1993
1994 /**
1995 * Returns the CDMA MIN.
1996 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001997 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001998 public String getCdmaMin(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001999 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07002000 final Phone phone = getPhone(subId);
2001 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
2002 return phone.getCdmaMin();
Junda Liuca05d5d2014-08-14 22:36:34 -07002003 } else {
2004 return null;
2005 }
2006 }
2007
2008 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002009 * Returns true if CDMA provisioning needs to run.
2010 */
2011 public boolean needsOtaServiceProvisioning() {
2012 return mPhone.needsOtaServiceProvisioning();
2013 }
2014
2015 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002016 * Sets the voice mail number of a given subId.
2017 */
2018 @Override
2019 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002020 enforceCarrierPrivilege(subId);
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002021 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
2022 new Pair<String, String>(alphaTag, number), new Integer(subId));
2023 return success;
2024 }
2025
Ta-wei Yen87c49842016-05-13 21:19:52 -07002026 @Override
Ta-wei Yenc9df0432017-04-17 17:09:07 -07002027 public Bundle getVisualVoicemailSettings(String callingPackage, int subId) {
2028 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
2029 String systemDialer = TelecomManager.from(mPhone.getContext()).getSystemDialerPackage();
2030 if (!TextUtils.equals(callingPackage, systemDialer)) {
2031 throw new SecurityException("caller must be system dialer");
2032 }
2033 PhoneAccountHandle phoneAccountHandle = PhoneAccountHandleConverter.fromSubId(subId);
2034 if (phoneAccountHandle == null){
2035 return null;
2036 }
2037 return VisualVoicemailSettingsUtil.dump(mPhone.getContext(), phoneAccountHandle);
2038 }
2039
2040 @Override
Ta-wei Yen409ac562017-03-06 16:00:44 -08002041 public String getVisualVoicemailPackageName(String callingPackage, int subId) {
Ta-wei Yendca928f2017-01-10 16:17:08 -08002042 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
2043 if (!canReadPhoneState(callingPackage, "getVisualVoicemailPackageName")) {
2044 return null;
2045 }
Ta-wei Yendca928f2017-01-10 16:17:08 -08002046 return RemoteVvmTaskManager.getRemotePackage(mPhone.getContext(), subId).getPackageName();
2047 }
2048
2049 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002050 public void enableVisualVoicemailSmsFilter(String callingPackage, int subId,
2051 VisualVoicemailSmsFilterSettings settings) {
2052 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002053 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002054 .enableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId,
2055 settings);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002056 }
2057
2058 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002059 public void disableVisualVoicemailSmsFilter(String callingPackage, int subId) {
2060 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002061 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002062 .disableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002063 }
2064
2065 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002066 public VisualVoicemailSmsFilterSettings getVisualVoicemailSmsFilterSettings(
2067 String callingPackage, int subId) {
2068 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002069 return VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002070 .getVisualVoicemailSmsFilterSettings(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002071 }
2072
2073 @Override
Ta-wei Yen30a69c82016-12-27 14:52:32 -08002074 public VisualVoicemailSmsFilterSettings getActiveVisualVoicemailSmsFilterSettings(int subId) {
Ta-wei Yenb6929602016-05-24 15:48:27 -07002075 enforceReadPrivilegedPermission();
Ta-wei Yen87c49842016-05-13 21:19:52 -07002076 return VisualVoicemailSmsFilterConfig
Ta-wei Yen30a69c82016-12-27 14:52:32 -08002077 .getActiveVisualVoicemailSmsFilterSettings(mPhone.getContext(), subId);
2078 }
2079
2080 @Override
2081 public void sendVisualVoicemailSmsForSubscriber(String callingPackage, int subId,
2082 String number, int port, String text, PendingIntent sentIntent) {
2083 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen527a9c02017-01-06 15:29:25 -08002084 enforceVisualVoicemailPackage(callingPackage, subId);
Ta-wei Yen30a69c82016-12-27 14:52:32 -08002085 enforceSendSmsPermission();
2086 // Make the calls as the phone process.
2087 final long identity = Binder.clearCallingIdentity();
2088 try {
2089 SmsManager smsManager = SmsManager.getSmsManagerForSubscriptionId(subId);
2090 if (port == 0) {
2091 smsManager.sendTextMessageWithSelfPermissions(number, null, text,
2092 sentIntent, null, false);
2093 } else {
2094 byte[] data = text.getBytes(StandardCharsets.UTF_8);
2095 smsManager.sendDataMessageWithSelfPermissions(number, null,
2096 (short) port, data, sentIntent, null);
2097 }
2098 } finally {
2099 Binder.restoreCallingIdentity(identity);
2100 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07002101 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002102 /**
fionaxu0152e512016-11-14 13:36:14 -08002103 * Sets the voice activation state of a given subId.
2104 */
2105 @Override
2106 public void setVoiceActivationState(int subId, int activationState) {
2107 enforceModifyPermissionOrCarrierPrivilege(subId);
2108 final Phone phone = getPhone(subId);
2109 if (phone != null) {
2110 phone.setVoiceActivationState(activationState);
2111 } else {
2112 loge("setVoiceActivationState fails with invalid subId: " + subId);
2113 }
2114 }
2115
2116 /**
2117 * Sets the data activation state of a given subId.
2118 */
2119 @Override
2120 public void setDataActivationState(int subId, int activationState) {
2121 enforceModifyPermissionOrCarrierPrivilege(subId);
2122 final Phone phone = getPhone(subId);
2123 if (phone != null) {
2124 phone.setDataActivationState(activationState);
2125 } else {
2126 loge("setVoiceActivationState fails with invalid subId: " + subId);
2127 }
2128 }
2129
2130 /**
2131 * Returns the voice activation state of a given subId.
2132 */
2133 @Override
2134 public int getVoiceActivationState(int subId, String callingPackage) {
2135 if (!canReadPhoneState(callingPackage, "getVoiceActivationStateForSubscriber")) {
2136 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2137 }
2138 final Phone phone = getPhone(subId);
2139 if (phone != null) {
2140 return phone.getVoiceActivationState();
2141 } else {
2142 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2143 }
2144 }
2145
2146 /**
2147 * Returns the data activation state of a given subId.
2148 */
2149 @Override
2150 public int getDataActivationState(int subId, String callingPackage) {
2151 if (!canReadPhoneState(callingPackage, "getDataActivationStateForSubscriber")) {
2152 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2153 }
2154 final Phone phone = getPhone(subId);
2155 if (phone != null) {
2156 return phone.getDataActivationState();
2157 } else {
2158 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2159 }
2160 }
2161
2162 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002163 * Returns the unread count of voicemails
2164 */
2165 public int getVoiceMessageCount() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002166 return getVoiceMessageCountForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07002167 }
2168
2169 /**
2170 * Returns the unread count of voicemails for a subId
2171 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002172 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002173 public int getVoiceMessageCountForSubscriber( int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002174 final Phone phone = getPhone(subId);
2175 if (phone != null) {
2176 return phone.getVoiceMessageCount();
2177 } else {
2178 return 0;
2179 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002180 }
2181
2182 /**
pkanwar8a4dcfb2017-01-19 13:43:16 -08002183 * returns true, if the device is in a state where both voice and data
2184 * are supported simultaneously. This can change based on location or network condition.
2185 */
2186 @Override
2187 public boolean isConcurrentVoiceAndDataAllowed(int subId) {
2188 final Phone phone = getPhone(subId);
2189 return (phone == null ? false : phone.isConcurrentVoiceAndDataAllowed());
2190 }
2191
2192 /**
fionaxu235cc5e2017-03-06 22:25:57 -08002193 * Send the dialer code if called from the current default dialer or the caller has
2194 * carrier privilege.
2195 * @param inputCode The dialer code to send
2196 */
2197 @Override
2198 public void sendDialerSpecialCode(String callingPackage, String inputCode) {
2199 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
2200 String defaultDialer = TelecomManager.from(mPhone.getContext()).getDefaultDialerPackage();
2201 if (!TextUtils.equals(callingPackage, defaultDialer)) {
2202 enforceCarrierPrivilege(getDefaultSubscription());
2203 }
2204 mPhone.sendDialerSpecialCode(inputCode);
2205 }
2206
2207 /**
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002208 * Returns the data network type.
2209 * Legacy call, permission-free.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002210 *
2211 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
2212 */
2213 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002214 public int getNetworkType() {
2215 final Phone phone = getPhone(getDefaultSubscription());
2216 if (phone != null) {
2217 return phone.getServiceState().getDataNetworkType();
2218 } else {
2219 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2220 }
Wink Saville36469e72014-06-11 15:17:00 -07002221 }
2222
2223 /**
2224 * Returns the network type for a subId
2225 */
2226 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002227 public int getNetworkTypeForSubscriber(int subId, String callingPackage) {
2228 if (!canReadPhoneState(callingPackage, "getNetworkTypeForSubscriber")) {
2229 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2230 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002231
Sanket Padawe356d7632015-06-22 14:03:32 -07002232 final Phone phone = getPhone(subId);
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002233 if (phone != null) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002234 return phone.getServiceState().getDataNetworkType();
2235 } else {
2236 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2237 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002238 }
2239
2240 /**
2241 * Returns the data network type
2242 */
2243 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002244 public int getDataNetworkType(String callingPackage) {
2245 return getDataNetworkTypeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002246 }
2247
2248 /**
2249 * Returns the data network type for a subId
2250 */
2251 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002252 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage) {
2253 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2254 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2255 }
2256
Sanket Padawe356d7632015-06-22 14:03:32 -07002257 final Phone phone = getPhone(subId);
2258 if (phone != null) {
2259 return phone.getServiceState().getDataNetworkType();
2260 } else {
2261 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2262 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002263 }
2264
2265 /**
Wink Saville36469e72014-06-11 15:17:00 -07002266 * Returns the Voice network type for a subId
2267 */
2268 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002269 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage) {
2270 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2271 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2272 }
2273
Sanket Padawe356d7632015-06-22 14:03:32 -07002274 final Phone phone = getPhone(subId);
2275 if (phone != null) {
2276 return phone.getServiceState().getVoiceNetworkType();
2277 } else {
2278 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2279 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002280 }
2281
2282 /**
2283 * @return true if a ICC card is present
2284 */
2285 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07002286 // FIXME Make changes to pass defaultSimId of type int
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002287 return hasIccCardUsingSlotIndex(mSubscriptionController.getSlotIndex(
2288 getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07002289 }
2290
2291 /**
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002292 * @return true if a ICC card is present for a slotIndex
Wink Saville36469e72014-06-11 15:17:00 -07002293 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002294 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002295 public boolean hasIccCardUsingSlotIndex(int slotIndex) {
2296 final Phone phone = PhoneFactory.getPhone(slotIndex);
fionaxu6e6c68b2016-06-23 13:31:32 -07002297 if (phone != null) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002298 return phone.getIccCard().hasIccCard();
Amit Mahajana6fc2a82015-01-06 11:53:51 -08002299 } else {
2300 return false;
2301 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002302 }
2303
2304 /**
2305 * Return if the current radio is LTE on CDMA. This
2306 * is a tri-state return value as for a period of time
2307 * the mode may be unknown.
2308 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002309 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002310 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08002311 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002312 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002313 @Override
2314 public int getLteOnCdmaMode(String callingPackage) {
2315 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002316 }
2317
Sanket Padawe356d7632015-06-22 14:03:32 -07002318 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002319 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage) {
2320 if (!canReadPhoneState(callingPackage, "getLteOnCdmaModeForSubscriber")) {
2321 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2322 }
2323
Sanket Padawe356d7632015-06-22 14:03:32 -07002324 final Phone phone = getPhone(subId);
2325 if (phone == null) {
2326 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2327 } else {
2328 return phone.getLteOnCdmaMode();
2329 }
Wink Saville36469e72014-06-11 15:17:00 -07002330 }
2331
2332 public void setPhone(Phone phone) {
2333 mPhone = phone;
2334 }
2335
2336 /**
2337 * {@hide}
2338 * Returns Default subId, 0 in the case of single standby.
2339 */
Wink Savilleb564aae2014-10-23 10:18:09 -07002340 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002341 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07002342 }
2343
Shishir Agrawala9f32182016-04-12 12:00:16 -07002344 private int getSlotForDefaultSubscription() {
2345 return mSubscriptionController.getPhoneId(getDefaultSubscription());
2346 }
2347
Wink Savilleb564aae2014-10-23 10:18:09 -07002348 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002349 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002350 }
Ihab Awadf2177b72013-11-25 13:33:23 -08002351
2352 /**
2353 * @see android.telephony.TelephonyManager.WifiCallingChoices
2354 */
2355 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002356 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
2357 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08002358 }
2359
2360 /**
2361 * @see android.telephony.TelephonyManager.WifiCallingChoices
2362 */
2363 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002364 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
2365 Settings.System.putInt(mPhone.getContext().getContentResolver(),
2366 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08002367 }
2368
Sailesh Nepald1e68152013-12-12 19:08:02 -08002369 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07002370 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08002371 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08002372 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08002373
Shishir Agrawal566b7612013-10-28 14:41:00 -07002374 @Override
Ajay Nambid7454d32015-12-03 13:50:00 -08002375 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(int subId, String AID, int p2) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002376 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002377
Ajay Nambid7454d32015-12-03 13:50:00 -08002378 if (DBG) log("iccOpenLogicalChannel: subId=" + subId + " aid=" + AID + " p2=" + p2);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002379 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
Ajay Nambid7454d32015-12-03 13:50:00 -08002380 CMD_OPEN_CHANNEL, new Pair<String, Integer>(AID, p2), subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002381 if (DBG) log("iccOpenLogicalChannel: " + response);
2382 return response;
Shishir Agrawal566b7612013-10-28 14:41:00 -07002383 }
2384
2385 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002386 public boolean iccCloseLogicalChannel(int subId, int channel) {
2387 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002388
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002389 if (DBG) log("iccCloseLogicalChannel: subId=" + subId + " chnl=" + channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002390 if (channel < 0) {
2391 return false;
2392 }
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002393 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel, subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002394 if (DBG) log("iccCloseLogicalChannel: " + success);
2395 return success;
2396 }
2397
2398 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002399 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07002400 int command, int p1, int p2, int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002401 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002402
2403 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002404 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel +
2405 " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
Shishir Agrawal566b7612013-10-28 14:41:00 -07002406 " data=" + data);
2407 }
2408
2409 if (channel < 0) {
2410 return "";
2411 }
2412
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002413 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002414 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002415 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
2416
Shishir Agrawal566b7612013-10-28 14:41:00 -07002417 // Append the returned status code to the end of the response payload.
2418 String s = Integer.toHexString(
2419 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002420 if (response.payload != null) {
2421 s = IccUtils.bytesToHexString(response.payload) + s;
2422 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07002423 return s;
2424 }
Jake Hambye994d462014-02-03 13:10:13 -08002425
Evan Charltonc66da362014-05-16 14:06:40 -07002426 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002427 public String iccTransmitApduBasicChannel(int subId, int cla, int command, int p1, int p2,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002428 int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002429 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002430
2431 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002432 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd=" + command
2433 + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002434 }
2435
2436 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002437 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002438 if (DBG) log("iccTransmitApduBasicChannel: " + response);
2439
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002440 // Append the returned status code to the end of the response payload.
2441 String s = Integer.toHexString(
2442 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002443 if (response.payload != null) {
2444 s = IccUtils.bytesToHexString(response.payload) + s;
2445 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002446 return s;
2447 }
2448
2449 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002450 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002451 String filePath) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002452 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002453
2454 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002455 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " " +
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002456 p1 + " " + p2 + " " + p3 + ":" + filePath);
2457 }
2458
2459 IccIoResult response =
2460 (IccIoResult)sendRequest(CMD_EXCHANGE_SIM_IO,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002461 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
2462 subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002463
2464 if (DBG) {
2465 log("Exchange SIM_IO [R]" + response);
2466 }
2467
2468 byte[] result = null;
2469 int length = 2;
2470 if (response.payload != null) {
2471 length = 2 + response.payload.length;
2472 result = new byte[length];
2473 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
2474 } else {
2475 result = new byte[length];
2476 }
2477
2478 result[length - 1] = (byte) response.sw2;
2479 result[length - 2] = (byte) response.sw1;
2480 return result;
2481 }
2482
Nathan Haroldb3014052017-01-25 15:57:32 -08002483 /**
2484 * Get the forbidden PLMN List from the given app type (ex APPTYPE_USIM)
2485 * on a particular subscription
2486 */
2487 public String[] getForbiddenPlmns(int subId, int appType) {
Nathan Harold892104e2017-04-13 18:19:05 -07002488 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
2489 "Requires READ_PHONE_STATE");
Nathan Haroldb3014052017-01-25 15:57:32 -08002490 if (appType != TelephonyManager.APPTYPE_USIM && appType != TelephonyManager.APPTYPE_SIM) {
2491 loge("getForbiddenPlmnList(): App Type must be USIM or SIM");
2492 return null;
2493 }
2494 Object response = sendRequest(
2495 CMD_GET_FORBIDDEN_PLMNS, new Integer(appType), subId);
2496 if (response instanceof String[]) {
2497 return (String[]) response;
2498 }
2499 // Response is an Exception of some kind, which is signalled to the user as a NULL retval
2500 return null;
2501 }
2502
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002503 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002504 public String sendEnvelopeWithStatus(int subId, String content) {
2505 enforceModifyPermissionOrCarrierPrivilege(subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002506
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002507 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content, subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002508 if (response.payload == null) {
2509 return "";
2510 }
2511
2512 // Append the returned status code to the end of the response payload.
2513 String s = Integer.toHexString(
2514 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
2515 s = IccUtils.bytesToHexString(response.payload) + s;
2516 return s;
2517 }
2518
Jake Hambye994d462014-02-03 13:10:13 -08002519 /**
2520 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2521 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2522 *
2523 * @param itemID the ID of the item to read
2524 * @return the NV item as a String, or null on error.
2525 */
2526 @Override
2527 public String nvReadItem(int itemID) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002528 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002529 if (DBG) log("nvReadItem: item " + itemID);
2530 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
2531 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
2532 return value;
2533 }
2534
2535 /**
2536 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2537 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2538 *
2539 * @param itemID the ID of the item to read
2540 * @param itemValue the value to write, as a String
2541 * @return true on success; false on any failure
2542 */
2543 @Override
2544 public boolean nvWriteItem(int itemID, String itemValue) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002545 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002546 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
2547 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
2548 new Pair<Integer, String>(itemID, itemValue));
2549 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
2550 return success;
2551 }
2552
2553 /**
2554 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
2555 * Used for device configuration by some CDMA operators.
2556 *
2557 * @param preferredRoamingList byte array containing the new PRL
2558 * @return true on success; false on any failure
2559 */
2560 @Override
2561 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002562 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002563 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
2564 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
2565 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
2566 return success;
2567 }
2568
2569 /**
2570 * Perform the specified type of NV config reset.
2571 * Used for device configuration by some CDMA operators.
2572 *
2573 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
2574 * @return true on success; false on any failure
2575 */
2576 @Override
2577 public boolean nvResetConfig(int resetType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002578 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002579 if (DBG) log("nvResetConfig: type " + resetType);
2580 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
2581 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
2582 return success;
2583 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002584
2585 /**
Wink Saville36469e72014-06-11 15:17:00 -07002586 * {@hide}
2587 * Returns Default sim, 0 in the case of single standby.
2588 */
2589 public int getDefaultSim() {
2590 //TODO Need to get it from Telephony Devcontroller
2591 return 0;
2592 }
2593
Svet Ganovb320e182015-04-16 12:30:10 -07002594 public String[] getPcscfAddress(String apnType, String callingPackage) {
2595 if (!canReadPhoneState(callingPackage, "getPcscfAddress")) {
2596 return new String[0];
2597 }
2598
2599
ram87fca6f2014-07-18 18:58:44 +05302600 return mPhone.getPcscfAddress(apnType);
Wink Saville36469e72014-06-11 15:17:00 -07002601 }
2602
Brad Ebinger51f743a2017-01-23 13:50:20 -08002603 /**
2604 * Returns the {@link IImsServiceController} that corresponds to the given slot Id and IMS
2605 * feature or {@link null} if the service is not available. If an ImsServiceController is
2606 * available, the {@link IImsServiceFeatureListener} callback is registered as a listener for
2607 * feature updates.
2608 */
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002609 public IImsServiceController getImsServiceControllerAndListen(int slotIndex, int feature,
Brad Ebinger51f743a2017-01-23 13:50:20 -08002610 IImsServiceFeatureListener callback) {
2611 enforceModifyPermission();
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002612 return PhoneFactory.getImsResolver().getImsServiceControllerAndListen(slotIndex, feature,
Brad Ebinger51f743a2017-01-23 13:50:20 -08002613 callback);
2614 }
2615
Wink Saville36469e72014-06-11 15:17:00 -07002616 public void setImsRegistrationState(boolean registered) {
2617 enforceModifyPermission();
2618 mPhone.setImsRegistrationState(registered);
2619 }
2620
2621 /**
Stuart Scott54788802015-03-30 13:18:01 -07002622 * Set the network selection mode to automatic.
2623 *
2624 */
2625 @Override
2626 public void setNetworkSelectionModeAutomatic(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002627 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002628 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
2629 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
2630 }
2631
2632 /**
Shishir Agrawal302c8692015-06-19 13:49:39 -07002633 * Set the network selection mode to manual with the selected carrier.
2634 */
2635 @Override
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002636 public boolean setNetworkSelectionModeManual(int subId, OperatorInfo operator,
2637 boolean persistSelection) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002638 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002639 if (DBG) log("setNetworkSelectionModeManual: subId:" + subId + " operator:" + operator);
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002640 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operator,
2641 persistSelection);
2642 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002643 }
2644
2645 /**
2646 * Scans for available networks.
2647 */
2648 @Override
2649 public CellNetworkScanResult getCellNetworkScanResults(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002650 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002651 if (DBG) log("getCellNetworkScanResults: subId " + subId);
2652 CellNetworkScanResult result = (CellNetworkScanResult) sendRequest(
2653 CMD_PERFORM_NETWORK_SCAN, null, subId);
2654 return result;
2655 }
2656
2657 /**
yinxu504e1392017-04-12 16:03:22 -07002658 * Performs a new network scan and returns the id of this scan.
2659 *
2660 * @return the id of the requested scan which can be used to stop the scan.
2661 */
2662 @Override
2663 public int requestNetworkScan(int subId, NetworkScanRequest request, Messenger messenger,
2664 IBinder binder) {
2665 // TODO(yinxu): Implement this method.
2666 throw new UnsupportedOperationException("To be implemented...");
2667 }
2668
2669 /**
2670 * Stops an existing network scan with the given scanId.
2671 */
2672 @Override
2673 public void stopNetworkScan(int subId, int scanId) {
2674 // TODO(yinxu): Implement this method.
2675 throw new UnsupportedOperationException("To be implemented...");
2676 }
2677
2678 /**
Junda Liu84d15a22014-07-02 11:21:04 -07002679 * Get the calculated preferred network type.
2680 * Used for debugging incorrect network type.
2681 *
2682 * @return the preferred network type, defined in RILConstants.java.
2683 */
2684 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002685 public int getCalculatedPreferredNetworkType(String callingPackage) {
2686 if (!canReadPhoneState(callingPackage, "getCalculatedPreferredNetworkType")) {
2687 return RILConstants.PREFERRED_NETWORK_MODE;
2688 }
2689
Amit Mahajan43330e02014-11-18 11:54:45 -08002690 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext(), 0); // wink FIXME: need to get SubId from somewhere.
Junda Liu84d15a22014-07-02 11:21:04 -07002691 }
2692
2693 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08002694 * Get the preferred network type.
2695 * Used for device configuration by some CDMA operators.
2696 *
2697 * @return the preferred network type, defined in RILConstants.java.
2698 */
2699 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002700 public int getPreferredNetworkType(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002701 enforceModifyPermissionOrCarrierPrivilege(subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002702 if (DBG) log("getPreferredNetworkType");
Stuart Scott54788802015-03-30 13:18:01 -07002703 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002704 int networkType = (result != null ? result[0] : -1);
2705 if (DBG) log("getPreferredNetworkType: " + networkType);
2706 return networkType;
2707 }
2708
2709 /**
2710 * Set the preferred network type.
2711 * Used for device configuration by some CDMA operators.
2712 *
2713 * @param networkType the preferred network type, defined in RILConstants.java.
2714 * @return true on success; false on any failure.
2715 */
2716 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002717 public boolean setPreferredNetworkType(int subId, int networkType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002718 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002719 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
2720 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002721 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
Junda Liu80bc0d12014-07-14 16:36:44 -07002722 if (success) {
2723 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
Stuart Scott54788802015-03-30 13:18:01 -07002724 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
Junda Liu80bc0d12014-07-14 16:36:44 -07002725 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002726 return success;
2727 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002728
2729 /**
Junda Liu475951f2014-11-07 16:45:03 -08002730 * Check TETHER_DUN_REQUIRED and TETHER_DUN_APN settings, net.tethering.noprovisioning
2731 * SystemProperty, and config_tether_apndata to decide whether DUN APN is required for
2732 * tethering.
2733 *
2734 * @return 0: Not required. 1: required. 2: Not set.
2735 * @hide
2736 */
2737 @Override
2738 public int getTetherApnRequired() {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002739 enforceModifyPermission();
Junda Liu475951f2014-11-07 16:45:03 -08002740 int dunRequired = Settings.Global.getInt(mPhone.getContext().getContentResolver(),
2741 Settings.Global.TETHER_DUN_REQUIRED, 2);
2742 // If not set, check net.tethering.noprovisioning, TETHER_DUN_APN setting and
2743 // config_tether_apndata.
2744 if (dunRequired == 2 && mPhone.hasMatchedTetherApnSetting()) {
2745 dunRequired = 1;
2746 }
2747 return dunRequired;
2748 }
2749
2750 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07002751 * Set mobile data enabled
2752 * Used by the user through settings etc to turn on/off mobile data
2753 *
2754 * @param enable {@code true} turn turn data on, else {@code false}
2755 */
2756 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002757 public void setDataEnabled(int subId, boolean enable) {
Jeff Davidsona1920712016-11-18 17:05:56 -08002758 enforceModifyPermissionOrCarrierPrivilege(subId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002759 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002760 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002761 Phone phone = PhoneFactory.getPhone(phoneId);
2762 if (phone != null) {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002763 if (DBG) log("setDataEnabled: subId=" + subId + " enable=" + enable);
Wink Savillee7353bb2014-12-05 14:21:41 -08002764 phone.setDataEnabled(enable);
2765 } else {
2766 loge("setDataEnabled: no phone for subId=" + subId);
2767 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002768 }
2769
2770 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07002771 * Get whether mobile data is enabled.
2772 *
Jeff Davidsona1920712016-11-18 17:05:56 -08002773 * Accepts either ACCESS_NETWORK_STATE, MODIFY_PHONE_STATE or carrier privileges.
Robert Greenwalted86e582014-05-21 20:03:20 -07002774 *
2775 * @return {@code true} if data is enabled else {@code false}
2776 */
2777 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002778 public boolean getDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07002779 try {
2780 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
2781 null);
2782 } catch (Exception e) {
Jeff Davidsona1920712016-11-18 17:05:56 -08002783 enforceModifyPermissionOrCarrierPrivilege(subId);
Robert Greenwalt646120a2014-05-23 11:54:03 -07002784 }
Wink Savillee7353bb2014-12-05 14:21:41 -08002785 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002786 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002787 Phone phone = PhoneFactory.getPhone(phoneId);
2788 if (phone != null) {
2789 boolean retVal = phone.getDataEnabled();
Joe Onoratoa601dd22016-02-23 13:03:53 -08002790 if (DBG) log("getDataEnabled: subId=" + subId + " retVal=" + retVal);
Wink Savillee7353bb2014-12-05 14:21:41 -08002791 return retVal;
2792 } else {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002793 if (DBG) loge("getDataEnabled: no phone subId=" + subId + " retVal=false");
Wink Savillee7353bb2014-12-05 14:21:41 -08002794 return false;
2795 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002796 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002797
2798 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002799 public int getCarrierPrivilegeStatus(int subId) {
2800 final Phone phone = getPhone(subId);
2801 if (phone == null) {
2802 loge("getCarrierPrivilegeStatus: Invalid subId");
2803 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
2804 }
2805 UiccCard card = UiccController.getInstance().getUiccCard(phone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002806 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002807 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002808 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2809 }
2810 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002811 phone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002812 }
Junda Liu29340342014-07-10 15:23:27 -07002813
2814 @Override
Zach Johnson50ecba32015-05-19 00:24:21 -07002815 public int checkCarrierPrivilegesForPackage(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002816 if (TextUtils.isEmpty(pkgName))
2817 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Shishir Agrawal21409252015-01-15 23:33:50 -08002818 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002819 if (card == null) {
2820 loge("checkCarrierPrivilegesForPackage: No UICC");
2821 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2822 }
Zach Johnson50ecba32015-05-19 00:24:21 -07002823 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgName);
2824 }
2825
2826 @Override
2827 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002828 if (TextUtils.isEmpty(pkgName))
2829 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Zach Johnson50ecba32015-05-19 00:24:21 -07002830 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2831 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2832 UiccCard card = UiccController.getInstance().getUiccCard(i);
2833 if (card == null) {
Jonathan Basseri7d320df2015-06-16 12:17:08 -07002834 // No UICC in that slot.
Zach Johnson50ecba32015-05-19 00:24:21 -07002835 continue;
2836 }
2837
2838 result = card.getCarrierPrivilegeStatus(
2839 mPhone.getContext().getPackageManager(), pkgName);
2840 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2841 break;
2842 }
2843 }
2844
2845 return result;
Junda Liu29340342014-07-10 15:23:27 -07002846 }
Derek Tan89e89d42014-07-08 17:00:10 -07002847
2848 @Override
Junda Liue64de782015-04-16 17:19:16 -07002849 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
2850 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
2851 loge("phoneId " + phoneId + " is not valid.");
2852 return null;
2853 }
2854 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002855 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002856 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002857 return null ;
2858 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002859 return card.getCarrierPackageNamesForIntent(
Svetoslav483aff72015-04-21 14:16:07 -07002860 mPhone.getContext().getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002861 }
2862
Amith Yamasani6e118872016-02-19 12:53:51 -08002863 @Override
2864 public List<String> getPackagesWithCarrierPrivileges() {
2865 PackageManager pm = mPhone.getContext().getPackageManager();
2866 List<String> privilegedPackages = new ArrayList<>();
2867 List<PackageInfo> packages = null;
2868 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2869 UiccCard card = UiccController.getInstance().getUiccCard(i);
2870 if (card == null) {
2871 // No UICC in that slot.
2872 continue;
2873 }
2874 if (card.hasCarrierPrivilegeRules()) {
2875 if (packages == null) {
2876 // Only check packages in user 0 for now
2877 packages = pm.getInstalledPackagesAsUser(
2878 PackageManager.MATCH_DISABLED_COMPONENTS
2879 | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS
2880 | PackageManager.GET_SIGNATURES, UserHandle.USER_SYSTEM);
2881 }
2882 for (int p = packages.size() - 1; p >= 0; p--) {
2883 PackageInfo pkgInfo = packages.get(p);
2884 if (pkgInfo != null && pkgInfo.packageName != null
2885 && card.getCarrierPrivilegeStatus(pkgInfo)
2886 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2887 privilegedPackages.add(pkgInfo.packageName);
2888 }
2889 }
2890 }
2891 }
2892 return privilegedPackages;
2893 }
2894
Wink Savilleb564aae2014-10-23 10:18:09 -07002895 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002896 final Phone phone = getPhone(subId);
2897 UiccCard card = phone == null ? null : phone.getUiccCard();
Derek Tan97ebb422014-09-05 16:55:38 -07002898 if (card == null) {
2899 loge("getIccId: No UICC");
2900 return null;
2901 }
2902 String iccId = card.getIccId();
2903 if (TextUtils.isEmpty(iccId)) {
2904 loge("getIccId: ICC ID is null or empty.");
2905 return null;
2906 }
2907 return iccId;
2908 }
2909
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002910 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002911 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
2912 String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002913 enforceCarrierPrivilege(subId);
Derek Tan97ebb422014-09-05 16:55:38 -07002914
Jeff Sharkey85190e62014-12-05 09:40:12 -08002915 final String iccId = getIccId(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07002916 final Phone phone = getPhone(subId);
2917 if (phone == null) {
2918 return false;
2919 }
2920 final String subscriberId = phone.getSubscriberId();
Jeff Sharkey85190e62014-12-05 09:40:12 -08002921
2922 if (DBG_MERGE) {
2923 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
2924 + subscriberId + " to " + number);
2925 }
2926
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002927 if (TextUtils.isEmpty(iccId)) {
2928 return false;
Derek Tan97ebb422014-09-05 16:55:38 -07002929 }
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002930
Jeff Sharkey85190e62014-12-05 09:40:12 -08002931 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2932
2933 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002934 if (alphaTag == null) {
2935 editor.remove(alphaTagPrefKey);
2936 } else {
2937 editor.putString(alphaTagPrefKey, alphaTag);
2938 }
2939
Jeff Sharkey85190e62014-12-05 09:40:12 -08002940 // Record both the line number and IMSI for this ICCID, since we need to
2941 // track all merged IMSIs based on line number
2942 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2943 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002944 if (number == null) {
2945 editor.remove(numberPrefKey);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002946 editor.remove(subscriberPrefKey);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002947 } else {
2948 editor.putString(numberPrefKey, number);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002949 editor.putString(subscriberPrefKey, subscriberId);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002950 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002951
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002952 editor.commit();
2953 return true;
Derek Tan7226c842014-07-02 17:42:23 -07002954 }
2955
2956 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002957 public String getLine1NumberForDisplay(int subId, String callingPackage) {
Makoto Onukifee69342015-06-29 14:44:50 -07002958 // This is open to apps with WRITE_SMS.
2959 if (!canReadPhoneNumber(callingPackage, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08002960 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07002961 return null;
2962 }
Derek Tan97ebb422014-09-05 16:55:38 -07002963
2964 String iccId = getIccId(subId);
2965 if (iccId != null) {
2966 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
Amit Mahajan9cf11512015-11-09 11:40:48 -08002967 if (DBG_MERGE) {
2968 log("getLine1NumberForDisplay returning " +
2969 mTelephonySharedPreferences.getString(numberPrefKey, null));
2970 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002971 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002972 }
Amit Mahajan9cf11512015-11-09 11:40:48 -08002973 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
Derek Tan97ebb422014-09-05 16:55:38 -07002974 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002975 }
2976
2977 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002978 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
2979 if (!canReadPhoneState(callingPackage, "getLine1AlphaTagForDisplay")) {
2980 return null;
2981 }
Derek Tan97ebb422014-09-05 16:55:38 -07002982
2983 String iccId = getIccId(subId);
2984 if (iccId != null) {
2985 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002986 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002987 }
Derek Tan97ebb422014-09-05 16:55:38 -07002988 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002989 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002990
2991 @Override
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002992 public String[] getMergedSubscriberIds(String callingPackage) {
2993 if (!canReadPhoneState(callingPackage, "getMergedSubscriberIds")) {
2994 return null;
2995 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002996 final Context context = mPhone.getContext();
2997 final TelephonyManager tele = TelephonyManager.from(context);
2998 final SubscriptionManager sub = SubscriptionManager.from(context);
2999
3000 // Figure out what subscribers are currently active
3001 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07003002 // Clear calling identity, when calling TelephonyManager, because callerUid must be
3003 // the process, where TelephonyManager was instantiated. Otherwise AppOps check will fail.
3004 final long identity = Binder.clearCallingIdentity();
3005 try {
3006 final int[] subIds = sub.getActiveSubscriptionIdList();
3007 for (int subId : subIds) {
3008 activeSubscriberIds.add(tele.getSubscriberId(subId));
3009 }
3010 } finally {
3011 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08003012 }
3013
3014 // First pass, find a number override for an active subscriber
3015 String mergeNumber = null;
3016 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
3017 for (String key : prefs.keySet()) {
3018 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
3019 final String subscriberId = (String) prefs.get(key);
3020 if (activeSubscriberIds.contains(subscriberId)) {
3021 final String iccId = key.substring(PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
3022 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
3023 mergeNumber = (String) prefs.get(numberKey);
3024 if (DBG_MERGE) {
3025 Slog.d(LOG_TAG, "Found line number " + mergeNumber
3026 + " for active subscriber " + subscriberId);
3027 }
3028 if (!TextUtils.isEmpty(mergeNumber)) {
3029 break;
3030 }
3031 }
3032 }
3033 }
3034
3035 // Shortcut when no active merged subscribers
3036 if (TextUtils.isEmpty(mergeNumber)) {
3037 return null;
3038 }
3039
3040 // Second pass, find all subscribers under that line override
3041 final ArraySet<String> result = new ArraySet<>();
3042 for (String key : prefs.keySet()) {
3043 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
3044 final String number = (String) prefs.get(key);
3045 if (mergeNumber.equals(number)) {
3046 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
3047 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
3048 final String subscriberId = (String) prefs.get(subscriberKey);
3049 if (!TextUtils.isEmpty(subscriberId)) {
3050 result.add(subscriberId);
3051 }
3052 }
3053 }
3054 }
3055
3056 final String[] resultArray = result.toArray(new String[result.size()]);
3057 Arrays.sort(resultArray);
3058 if (DBG_MERGE) {
3059 Slog.d(LOG_TAG, "Found subscribers " + Arrays.toString(resultArray) + " after merge");
3060 }
3061 return resultArray;
3062 }
3063
3064 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003065 public boolean setOperatorBrandOverride(int subId, String brand) {
3066 enforceCarrierPrivilege(subId);
3067 final Phone phone = getPhone(subId);
3068 return phone == null ? false : phone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07003069 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05003070
3071 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003072 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08003073 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
3074 List<String> cdmaNonRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003075 enforceCarrierPrivilege(subId);
3076 final Phone phone = getPhone(subId);
3077 if (phone == null) {
3078 return false;
3079 }
3080 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08003081 cdmaNonRoamingList);
3082 }
3083
3084 @Override
Amit Mahajanf43fe462017-02-23 12:08:31 -08003085 @Deprecated
Steven Liu4bf01bc2014-07-17 11:05:29 -05003086 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
3087 enforceModifyPermission();
3088
3089 int returnValue = 0;
3090 try {
3091 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
3092 if(result.exception == null) {
3093 if (result.result != null) {
3094 byte[] responseData = (byte[])(result.result);
3095 if(responseData.length > oemResp.length) {
3096 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
3097 responseData.length + "bytes. Buffer Size is " +
3098 oemResp.length + "bytes.");
3099 }
3100 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
3101 returnValue = responseData.length;
3102 }
3103 } else {
3104 CommandException ex = (CommandException) result.exception;
3105 returnValue = ex.getCommandError().ordinal();
3106 if(returnValue > 0) returnValue *= -1;
3107 }
3108 } catch (RuntimeException e) {
3109 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
3110 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
3111 if(returnValue > 0) returnValue *= -1;
3112 }
3113
3114 return returnValue;
3115 }
Wink Saville5d475dd2014-10-17 15:00:58 -07003116
3117 @Override
3118 public void setRadioCapability(RadioAccessFamily[] rafs) {
3119 try {
3120 ProxyController.getInstance().setRadioCapability(rafs);
3121 } catch (RuntimeException e) {
3122 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
3123 }
3124 }
3125
3126 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003127 public int getRadioAccessFamily(int phoneId, String callingPackage) {
3128 if (!canReadPhoneState(callingPackage, "getRadioAccessFamily")) {
3129 return RadioAccessFamily.RAF_UNKNOWN;
3130 }
3131
Wink Saville5d475dd2014-10-17 15:00:58 -07003132 return ProxyController.getInstance().getRadioAccessFamily(phoneId);
3133 }
Andrew Leedf14ead2014-10-17 14:22:52 -07003134
3135 @Override
3136 public void enableVideoCalling(boolean enable) {
3137 enforceModifyPermission();
Tyler Gunnfdd69de2015-12-04 21:24:38 -08003138 ImsManager.setVtSetting(mPhone.getContext(), enable);
Andrew Leedf14ead2014-10-17 14:22:52 -07003139 }
3140
3141 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07003142 public boolean isVideoCallingEnabled(String callingPackage) {
3143 if (!canReadPhoneState(callingPackage, "isVideoCallingEnabled")) {
3144 return false;
3145 }
3146
Andrew Lee77527ac2014-10-21 16:57:39 -07003147 // Check the user preference and the system-level IMS setting. Even if the user has
3148 // enabled video calling, if IMS is disabled we aren't able to support video calling.
3149 // In the long run, we may instead need to check if there exists a connection service
3150 // which can support video calling.
Andrew Lee312e8172014-10-23 17:01:36 -07003151 return ImsManager.isVtEnabledByPlatform(mPhone.getContext())
3152 && ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
Tyler Gunnfdd69de2015-12-04 21:24:38 -08003153 && ImsManager.isVtEnabledByUser(mPhone.getContext());
Andrew Leedf14ead2014-10-17 14:22:52 -07003154 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003155
Andrew Leea1239f22015-03-02 17:44:07 -08003156 @Override
3157 public boolean canChangeDtmfToneLength() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07003158 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08003159 }
3160
3161 @Override
3162 public boolean isWorldPhone() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07003163 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08003164 }
3165
Andrew Lee9431b832015-03-09 18:46:45 -07003166 @Override
3167 public boolean isTtyModeSupported() {
3168 TelecomManager telecomManager = TelecomManager.from(mPhone.getContext());
3169 TelephonyManager telephonyManager =
3170 (TelephonyManager) mPhone.getContext().getSystemService(Context.TELEPHONY_SERVICE);
Wooki Wu1f82f7a2016-02-15 15:59:58 +08003171 return telecomManager.isTtySupported();
Andrew Lee9431b832015-03-09 18:46:45 -07003172 }
3173
3174 @Override
3175 public boolean isHearingAidCompatibilitySupported() {
3176 return mPhone.getContext().getResources().getBoolean(R.bool.hac_enabled);
3177 }
3178
Sanket Padawe7310cc72015-01-14 09:53:20 -08003179 /**
3180 * Returns the unique device ID of phone, for example, the IMEI for
3181 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
3182 *
3183 * <p>Requires Permission:
3184 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
3185 */
3186 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07003187 public String getDeviceId(String callingPackage) {
3188 if (!canReadPhoneState(callingPackage, "getDeviceId")) {
3189 return null;
3190 }
3191
Sanket Padawe7310cc72015-01-14 09:53:20 -08003192 final Phone phone = PhoneFactory.getPhone(0);
3193 if (phone != null) {
3194 return phone.getDeviceId();
3195 } else {
3196 return null;
3197 }
3198 }
3199
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003200 /*
3201 * {@hide}
3202 * Returns the IMS Registration Status
3203 */
Santos Cordon7a1885b2015-02-03 11:15:19 -08003204 @Override
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003205 public boolean isImsRegistered() {
3206 return mPhone.isImsRegistered();
3207 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08003208
3209 @Override
3210 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
3211 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
3212 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07003213
Nathan Haroldc55097a2015-03-11 18:14:50 -07003214 /*
3215 * {@hide}
3216 * Returns the IMS Registration Status
3217 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003218 public boolean isWifiCallingAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07003219 return mPhone.isWifiCallingEnabled();
3220 }
3221
3222 /*
3223 * {@hide}
3224 * Returns the IMS Registration Status
3225 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003226 public boolean isVolteAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07003227 return mPhone.isVolteEnabled();
3228 }
Svet Ganovb320e182015-04-16 12:30:10 -07003229
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003230 /*
3231 * {@hide} Returns the IMS Registration Status
3232 */
3233 public boolean isVideoTelephonyAvailable() {
3234 return mPhone.isVideoEnabled();
3235 }
3236
Svet Ganovb320e182015-04-16 12:30:10 -07003237 private boolean canReadPhoneState(String callingPackage, String message) {
Etan Cohen921655c2015-06-24 13:54:50 -07003238 try {
Amit Mahajan83ea23b2015-07-30 16:05:11 -07003239 mApp.enforceCallingOrSelfPermission(
Etan Cohen921655c2015-06-24 13:54:50 -07003240 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE, message);
3241
Amit Mahajan83ea23b2015-07-30 16:05:11 -07003242 // SKIP checking for run-time permission since caller or self has PRIVILEDGED permission
Etan Cohen921655c2015-06-24 13:54:50 -07003243 return true;
Amit Mahajan785783f2015-06-29 10:36:50 -07003244 } catch (SecurityException e) {
3245 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
3246 message);
Etan Cohen921655c2015-06-24 13:54:50 -07003247 }
Svet Ganovb320e182015-04-16 12:30:10 -07003248
3249 if (mAppOps.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(),
3250 callingPackage) != AppOpsManager.MODE_ALLOWED) {
3251 return false;
3252 }
3253
3254 return true;
3255 }
Stuart Scott8eef64f2015-04-08 15:13:54 -07003256
Makoto Onukifee69342015-06-29 14:44:50 -07003257 /**
Amit Mahajanb9b49782015-09-15 18:16:32 -07003258 * Besides READ_PHONE_STATE, WRITE_SMS and READ_SMS also allow apps to get phone numbers.
Makoto Onukifee69342015-06-29 14:44:50 -07003259 */
3260 private boolean canReadPhoneNumber(String callingPackage, String message) {
Makoto Onukie4072d12015-08-03 15:12:23 -07003261 // Default SMS app can always read it.
3262 if (mAppOps.noteOp(AppOpsManager.OP_WRITE_SMS,
3263 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED) {
3264 return true;
3265 }
Chad Brubaker7a8fba22016-11-10 11:23:18 -08003266
Makoto Onukie4072d12015-08-03 15:12:23 -07003267 try {
3268 return canReadPhoneState(callingPackage, message);
Amit Mahajanb9b49782015-09-15 18:16:32 -07003269 } catch (SecurityException readPhoneStateSecurityException) {
Makoto Onukie4072d12015-08-03 15:12:23 -07003270 }
Chad Brubaker7a8fba22016-11-10 11:23:18 -08003271 // Can be read with READ_SMS too.
3272 try {
3273 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_SMS, message);
3274 int opCode = mAppOps.permissionToOpCode(android.Manifest.permission.READ_SMS);
3275 if (opCode != AppOpsManager.OP_NONE) {
3276 return mAppOps.noteOp(opCode, Binder.getCallingUid(), callingPackage)
3277 == AppOpsManager.MODE_ALLOWED;
3278 } else {
3279 return true;
3280 }
3281 } catch (SecurityException readSmsSecurityException) {
3282 }
Chad Brubakerf342a982017-03-30 16:27:34 -07003283 // Can be read with READ_PHONE_NUMBERS too.
Chad Brubaker7a8fba22016-11-10 11:23:18 -08003284 try {
Chad Brubakerf342a982017-03-30 16:27:34 -07003285 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_NUMBERS,
Chad Brubaker7a8fba22016-11-10 11:23:18 -08003286 message);
Chad Brubakerf342a982017-03-30 16:27:34 -07003287 int opCode = mAppOps.permissionToOpCode(android.Manifest.permission.READ_PHONE_NUMBERS);
Chad Brubaker7a8fba22016-11-10 11:23:18 -08003288 if (opCode != AppOpsManager.OP_NONE) {
3289 return mAppOps.noteOp(opCode, Binder.getCallingUid(), callingPackage)
3290 == AppOpsManager.MODE_ALLOWED;
3291 } else {
3292 return true;
3293 }
3294 } catch (SecurityException readPhoneNumberSecurityException) {
3295 }
3296
3297 throw new SecurityException(message + ": Neither user " + Binder.getCallingUid() +
3298 " nor current process has" + android.Manifest.permission.READ_PHONE_STATE +
3299 ", " + android.Manifest.permission.READ_SMS + ", or " +
Chad Brubakerf342a982017-03-30 16:27:34 -07003300 android.Manifest.permission.READ_PHONE_NUMBERS);
Makoto Onukifee69342015-06-29 14:44:50 -07003301 }
3302
Stuart Scott8eef64f2015-04-08 15:13:54 -07003303 @Override
3304 public void factoryReset(int subId) {
3305 enforceConnectivityInternalPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07003306 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
3307 return;
3308 }
3309
Svet Ganovcc087f82015-05-12 20:35:54 -07003310 final long identity = Binder.clearCallingIdentity();
3311 try {
Stuart Scott981d8582015-04-21 14:09:50 -07003312 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
3313 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Svet Ganovcc087f82015-05-12 20:35:54 -07003314 // Enable data
3315 setDataEnabled(subId, true);
3316 // Set network selection mode to automatic
3317 setNetworkSelectionModeAutomatic(subId);
3318 // Set preferred mobile network type to the best available
3319 setPreferredNetworkType(subId, Phone.PREFERRED_NT_MODE);
3320 // Turn off roaming
Malcolm Chenf6b97f42017-04-19 16:03:24 -07003321 mPhone.setDataRoamingEnabled(false);
Svet Ganovcc087f82015-05-12 20:35:54 -07003322 }
3323 } finally {
3324 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07003325 }
3326 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003327
3328 @Override
3329 public String getLocaleFromDefaultSim() {
3330 // We query all subscriptions instead of just the active ones, because
3331 // this might be called early on in the provisioning flow when the
3332 // subscriptions potentially aren't active yet.
3333 final List<SubscriptionInfo> slist = getAllSubscriptionInfoList();
3334 if (slist == null || slist.isEmpty()) {
3335 return null;
3336 }
3337
3338 // This function may be called very early, say, from the setup wizard, at
3339 // which point we won't have a default subscription set. If that's the case
3340 // we just choose the first, which will be valid in "most cases".
3341 final int defaultSubId = getDefaultSubscription();
3342 SubscriptionInfo info = null;
3343 if (defaultSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
3344 info = slist.get(0);
3345 } else {
3346 for (SubscriptionInfo item : slist) {
3347 if (item.getSubscriptionId() == defaultSubId) {
3348 info = item;
3349 break;
3350 }
3351 }
3352
3353 if (info == null) {
3354 return null;
3355 }
3356 }
3357
3358 // Try and fetch the locale from the carrier properties or from the SIM language
3359 // preferences (EF-PL and EF-LI)...
Tony Hill183b2de2015-06-24 14:53:58 +01003360 final int mcc = info.getMcc();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003361 final Phone defaultPhone = getPhone(info.getSubscriptionId());
Narayan Kamath011676f2015-07-29 12:04:08 +01003362 String simLanguage = null;
Narayan Kamath1c496c22015-04-16 14:40:19 +01003363 if (defaultPhone != null) {
3364 final Locale localeFromDefaultSim = defaultPhone.getLocaleFromSimAndCarrierPrefs();
3365 if (localeFromDefaultSim != null) {
Narayan Kamath011676f2015-07-29 12:04:08 +01003366 if (!localeFromDefaultSim.getCountry().isEmpty()) {
Tony Hill183b2de2015-06-24 14:53:58 +01003367 if (DBG) log("Using locale from default SIM:" + localeFromDefaultSim);
3368 return localeFromDefaultSim.toLanguageTag();
Narayan Kamath011676f2015-07-29 12:04:08 +01003369 } else {
3370 simLanguage = localeFromDefaultSim.getLanguage();
Tony Hill183b2de2015-06-24 14:53:58 +01003371 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003372 }
3373 }
3374
Narayan Kamath011676f2015-07-29 12:04:08 +01003375 // The SIM language preferences only store a language (e.g. fr = French), not an
3376 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
3377 // the SIM and carrier preferences does not include a country we add the country
3378 // determined from the SIM MCC to provide an exact locale.
3379 final Locale mccLocale = MccTable.getLocaleFromMcc(mPhone.getContext(), mcc, simLanguage);
Tony Hill183b2de2015-06-24 14:53:58 +01003380 if (mccLocale != null) {
3381 if (DBG) log("No locale from default SIM, using mcc locale:" + mccLocale);
3382 return mccLocale.toLanguageTag();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003383 }
3384
Tony Hill183b2de2015-06-24 14:53:58 +01003385 if (DBG) log("No locale found - returning null");
Narayan Kamath1c496c22015-04-16 14:40:19 +01003386 return null;
3387 }
3388
3389 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
3390 final long identity = Binder.clearCallingIdentity();
3391 try {
3392 return mSubscriptionController.getAllSubInfoList(
3393 mPhone.getContext().getOpPackageName());
3394 } finally {
3395 Binder.restoreCallingIdentity(identity);
3396 }
3397 }
3398
3399 private List<SubscriptionInfo> getActiveSubscriptionInfoList() {
3400 final long identity = Binder.clearCallingIdentity();
3401 try {
3402 return mSubscriptionController.getActiveSubscriptionInfoList(
3403 mPhone.getContext().getOpPackageName());
3404 } finally {
3405 Binder.restoreCallingIdentity(identity);
3406 }
3407 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003408
3409 /**
Adam Lesinski903a54c2016-04-11 14:49:52 -07003410 * Responds to the ResultReceiver with the {@link android.telephony.ModemActivityInfo} object
3411 * representing the state of the modem.
3412 *
3413 * NOTE: This clears the modem state, so there should only every be one caller.
3414 * @hide
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003415 */
3416 @Override
Adam Lesinski903a54c2016-04-11 14:49:52 -07003417 public void requestModemActivityInfo(ResultReceiver result) {
3418 enforceModifyPermission();
3419
3420 ModemActivityInfo info = (ModemActivityInfo) sendRequest(CMD_GET_MODEM_ACTIVITY_INFO, null);
3421 Bundle bundle = new Bundle();
3422 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY, info);
3423 result.send(0, bundle);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003424 }
Jack Yu85bd38a2015-11-09 11:34:32 -08003425
3426 /**
3427 * {@hide}
3428 * Returns the service state information on specified subscription.
3429 */
3430 @Override
3431 public ServiceState getServiceStateForSubscriber(int subId, String callingPackage) {
3432
3433 if (!canReadPhoneState(callingPackage, "getServiceStateForSubscriber")) {
3434 return null;
3435 }
3436
3437 final Phone phone = getPhone(subId);
3438 if (phone == null) {
3439 return null;
3440 }
3441
3442 return phone.getServiceState();
3443 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08003444
3445 /**
3446 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
3447 *
3448 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3449 * voicemail ringtone.
3450 * @return The URI for the ringtone to play when receiving a voicemail from a specific
3451 * PhoneAccount.
3452 */
3453 @Override
3454 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
Ta-wei Yen00b4ab52017-04-11 13:24:26 -07003455 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
Nancy Chen31f9ba12016-01-06 11:42:12 -08003456 if (phone == null) {
Ta-wei Yen00b4ab52017-04-11 13:24:26 -07003457 phone = mPhone;
Nancy Chen31f9ba12016-01-06 11:42:12 -08003458 }
3459
3460 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone);
3461 }
3462
3463 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08003464 * Sets the per-account voicemail ringtone.
3465 *
3466 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
3467 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
3468 *
3469 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
3470 * voicemail ringtone.
3471 * @param uri The URI for the ringtone to play when receiving a voicemail from a specific
3472 * PhoneAccount.
3473 */
3474 @Override
3475 public void setVoicemailRingtoneUri(String callingPackage,
3476 PhoneAccountHandle phoneAccountHandle, Uri uri) {
3477 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
3478 if (!TextUtils.equals(callingPackage,
3479 TelecomManager.from(mPhone.getContext()).getDefaultDialerPackage())) {
3480 enforceModifyPermissionOrCarrierPrivilege(
3481 PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle));
3482 }
3483 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
3484 if (phone == null){
Ta-wei Yen00b4ab52017-04-11 13:24:26 -07003485 phone = mPhone;
Ta-wei Yenc33877d2017-01-23 18:11:21 -08003486 }
3487 VoicemailNotificationSettingsUtil.setRingtoneUri(phone, uri);
3488 }
3489
3490 /**
Nancy Chen31f9ba12016-01-06 11:42:12 -08003491 * Returns whether vibration is set for voicemail notification in Phone settings.
3492 *
3493 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3494 * voicemail vibration setting.
3495 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
3496 */
3497 @Override
3498 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
Ta-wei Yen00b4ab52017-04-11 13:24:26 -07003499 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
Nancy Chen31f9ba12016-01-06 11:42:12 -08003500 if (phone == null) {
Ta-wei Yen00b4ab52017-04-11 13:24:26 -07003501 phone = mPhone;
Nancy Chen31f9ba12016-01-06 11:42:12 -08003502 }
3503
3504 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone);
3505 }
3506
Youhan Wange64578a2016-05-02 15:32:42 -07003507 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08003508 * Sets the per-account voicemail vibration.
3509 *
3510 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
3511 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
3512 *
3513 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
3514 * voicemail vibration setting.
3515 * @param enabled Whether to enable or disable vibration for voicemail notifications from a
3516 * specific PhoneAccount.
3517 */
3518 @Override
3519 public void setVoicemailVibrationEnabled(String callingPackage,
3520 PhoneAccountHandle phoneAccountHandle, boolean enabled) {
3521 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
3522 if (!TextUtils.equals(callingPackage,
3523 TelecomManager.from(mPhone.getContext()).getDefaultDialerPackage())) {
3524 enforceModifyPermissionOrCarrierPrivilege(
3525 PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle));
3526 }
3527
3528 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
3529 if (phone == null){
Ta-wei Yen00b4ab52017-04-11 13:24:26 -07003530 phone = mPhone;
Ta-wei Yenc33877d2017-01-23 18:11:21 -08003531 }
3532 VoicemailNotificationSettingsUtil.setVibrationEnabled(phone, enabled);
3533 }
3534
3535 /**
Youhan Wange64578a2016-05-02 15:32:42 -07003536 * Make sure either called from same process as self (phone) or IPC caller has read privilege.
3537 *
3538 * @throws SecurityException if the caller does not have the required permission
3539 */
3540 private void enforceReadPrivilegedPermission() {
3541 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
3542 null);
3543 }
3544
3545 /**
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003546 * Make sure either called from same process as self (phone) or IPC caller has send SMS
3547 * permission.
3548 *
3549 * @throws SecurityException if the caller does not have the required permission
3550 */
3551 private void enforceSendSmsPermission() {
3552 mApp.enforceCallingOrSelfPermission(permission.SEND_SMS, null);
3553 }
3554
3555 /**
Ta-wei Yen527a9c02017-01-06 15:29:25 -08003556 * Make sure called from the package in charge of visual voicemail.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003557 *
Ta-wei Yen527a9c02017-01-06 15:29:25 -08003558 * @throws SecurityException if the caller is not the visual voicemail package.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003559 */
Ta-wei Yen527a9c02017-01-06 15:29:25 -08003560 private void enforceVisualVoicemailPackage(String callingPackage, int subId) {
Ta-wei Yen5d81b0c2017-03-03 16:32:26 -08003561 ComponentName componentName =
3562 RemoteVvmTaskManager.getRemotePackage(mPhone.getContext(), subId);
3563 if(componentName == null) {
3564 throw new SecurityException("Caller not current active visual voicemail package[null]");
3565 }
3566 String vvmPackage = componentName.getPackageName();
Ta-wei Yen527a9c02017-01-06 15:29:25 -08003567 if (!callingPackage.equals(vvmPackage)) {
3568 throw new SecurityException("Caller not current active visual voicemail package[" +
3569 vvmPackage + "]");
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003570 }
3571 }
3572
3573 /**
Youhan Wange64578a2016-05-02 15:32:42 -07003574 * Return the application ID for the app type.
3575 *
3576 * @param subId the subscription ID that this request applies to.
3577 * @param appType the uicc app type.
3578 * @return Application ID for specificied app type, or null if no uicc.
3579 */
3580 @Override
3581 public String getAidForAppType(int subId, int appType) {
3582 enforceReadPrivilegedPermission();
3583 Phone phone = getPhone(subId);
3584 if (phone == null) {
3585 return null;
3586 }
3587 String aid = null;
3588 try {
3589 aid = UiccController.getInstance().getUiccCard(phone.getPhoneId())
3590 .getApplicationByType(appType).getAid();
3591 } catch (Exception e) {
3592 Log.e(LOG_TAG, "Not getting aid. Exception ex=" + e);
3593 }
3594 return aid;
3595 }
3596
Youhan Wang4001d252016-05-11 10:29:41 -07003597 /**
3598 * Return the Electronic Serial Number.
3599 *
3600 * @param subId the subscription ID that this request applies to.
3601 * @return ESN or null if error.
3602 */
3603 @Override
3604 public String getEsn(int subId) {
3605 enforceReadPrivilegedPermission();
3606 Phone phone = getPhone(subId);
3607 if (phone == null) {
3608 return null;
3609 }
3610 String esn = null;
3611 try {
3612 esn = phone.getEsn();
3613 } catch (Exception e) {
3614 Log.e(LOG_TAG, "Not getting ESN. Exception ex=" + e);
3615 }
3616 return esn;
3617 }
3618
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003619 /**
Youhan Wang66ad5d72016-07-18 17:56:58 -07003620 * Return the Preferred Roaming List Version.
3621 *
3622 * @param subId the subscription ID that this request applies to.
3623 * @return PRLVersion or null if error.
3624 */
3625 @Override
3626 public String getCdmaPrlVersion(int subId) {
3627 enforceReadPrivilegedPermission();
3628 Phone phone = getPhone(subId);
3629 if (phone == null) {
3630 return null;
3631 }
3632 String cdmaPrlVersion = null;
3633 try {
3634 cdmaPrlVersion = phone.getCdmaPrlVersion();
3635 } catch (Exception e) {
3636 Log.e(LOG_TAG, "Not getting PRLVersion", e);
3637 }
3638 return cdmaPrlVersion;
3639 }
3640
3641 /**
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003642 * Get snapshot of Telephony histograms
3643 * @return List of Telephony histograms
3644 * @hide
3645 */
3646 @Override
3647 public List<TelephonyHistogram> getTelephonyHistograms() {
3648 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
3649 return RIL.getTelephonyRILTimingHistograms();
3650 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07003651
3652 /**
3653 * {@hide}
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003654 * Set the allowed carrier list for slotIndex
Meng Wang1a7c35a2016-05-05 20:56:15 -07003655 * Require system privileges. In the future we may add this to carrier APIs.
3656 *
3657 * @return The number of carriers set successfully, should match length of carriers
3658 */
3659 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003660 public int setAllowedCarriers(int slotIndex, List<CarrierIdentifier> carriers) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07003661 enforceModifyPermission();
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003662
Meng Wang9b7c4e92017-02-17 11:41:27 -08003663 if (carriers == null) {
3664 throw new NullPointerException("carriers cannot be null");
3665 }
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003666
3667 int subId = SubscriptionManager.getSubId(slotIndex)[0];
Meng Wang1a7c35a2016-05-05 20:56:15 -07003668 int[] retVal = (int[]) sendRequest(CMD_SET_ALLOWED_CARRIERS, carriers, subId);
3669 return retVal[0];
3670 }
3671
3672 /**
3673 * {@hide}
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003674 * Get the allowed carrier list for slotIndex.
Meng Wang1a7c35a2016-05-05 20:56:15 -07003675 * Require system privileges. In the future we may add this to carrier APIs.
3676 *
3677 * @return List of {@link android.service.telephony.CarrierIdentifier}; empty list
3678 * means all carriers are allowed.
3679 */
3680 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003681 public List<CarrierIdentifier> getAllowedCarriers(int slotIndex) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07003682 enforceReadPrivilegedPermission();
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003683 int subId = SubscriptionManager.getSubId(slotIndex)[0];
Meng Wang1a7c35a2016-05-05 20:56:15 -07003684 return (List<CarrierIdentifier>) sendRequest(CMD_GET_ALLOWED_CARRIERS, null, subId);
3685 }
3686
fionaxu59545b42016-05-25 15:53:37 -07003687 /**
3688 * Action set from carrier signalling broadcast receivers to enable/disable metered apns
3689 * @param subId the subscription ID that this action applies to.
3690 * @param enabled control enable or disable metered apns.
3691 * {@hide}
3692 */
3693 @Override
3694 public void carrierActionSetMeteredApnsEnabled(int subId, boolean enabled) {
3695 enforceModifyPermission();
3696 final Phone phone = getPhone(subId);
3697 if (phone == null) {
3698 loge("carrierAction: SetMeteredApnsEnabled fails with invalid subId: " + subId);
3699 return;
3700 }
3701 try {
3702 phone.carrierActionSetMeteredApnsEnabled(enabled);
3703 } catch (Exception e) {
3704 Log.e(LOG_TAG, "carrierAction: SetMeteredApnsEnabled fails. Exception ex=" + e);
3705 }
3706 }
3707
3708 /**
3709 * Action set from carrier signalling broadcast receivers to enable/disable radio
3710 * @param subId the subscription ID that this action applies to.
3711 * @param enabled control enable or disable radio.
3712 * {@hide}
3713 */
3714 @Override
3715 public void carrierActionSetRadioEnabled(int subId, boolean enabled) {
3716 enforceModifyPermission();
3717 final Phone phone = getPhone(subId);
3718 if (phone == null) {
3719 loge("carrierAction: SetRadioEnabled fails with invalid sibId: " + subId);
3720 return;
3721 }
3722 try {
3723 phone.carrierActionSetRadioEnabled(enabled);
3724 } catch (Exception e) {
3725 Log.e(LOG_TAG, "carrierAction: SetRadioEnabled fails. Exception ex=" + e);
3726 }
3727 }
3728
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003729 /**
3730 * Called when "adb shell dumpsys phone" is invoked. Dump is also automatically invoked when a
3731 * bug report is being generated.
3732 */
3733 @Override
Ta-wei Yen99282e02016-06-21 18:19:35 -07003734 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
dcashman22b950d2016-06-27 11:39:02 -07003735 if (mPhone.getContext().checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3736 != PackageManager.PERMISSION_GRANTED) {
3737 writer.println("Permission Denial: can't dump Phone from pid="
3738 + Binder.getCallingPid()
3739 + ", uid=" + Binder.getCallingUid()
3740 + "without permission "
3741 + android.Manifest.permission.DUMP);
3742 return;
3743 }
Ta-wei Yen99282e02016-06-21 18:19:35 -07003744 DumpsysHandler.dump(mPhone.getContext(), fd, writer, args);
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003745 }
Jack Yueb89b242016-06-22 13:27:47 -07003746
3747 /**
3748 * Get aggregated video call data usage from all subscriptions since boot.
3749 * @return total data usage in bytes
3750 * {@hide}
3751 */
3752 @Override
3753 public long getVtDataUsage() {
3754 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_NETWORK_USAGE_HISTORY,
3755 null);
3756
3757 // NetworkStatsService keeps tracking the active network interface and identity. It will
3758 // record the delta with the corresponding network identity. What we need to do here is
3759 // returning total video call data usage from all subscriptions since boot.
3760
3761 // TODO: Add sub id support in the future. We'll need it when we support DSDA and
3762 // simultaneous VT calls.
3763 final Phone[] phones = PhoneFactory.getPhones();
3764 long total = 0;
3765 for (Phone phone : phones) {
3766 total += phone.getVtDataUsage();
3767 }
3768 return total;
3769 }
Jack Yu75ab2952016-07-08 14:29:33 -07003770
3771 /**
3772 * Policy control of data connection. Usually used when data limit is passed.
3773 * @param enabled True if enabling the data, otherwise disabling.
3774 * @param subId Subscription index
3775 * {@hide}
3776 */
3777 @Override
3778 public void setPolicyDataEnabled(boolean enabled, int subId) {
3779 enforceModifyPermission();
3780 Phone phone = getPhone(subId);
3781 if (phone != null) {
3782 phone.setPolicyDataEnabled(enabled);
3783 }
3784 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07003785
3786 /**
3787 * Get Client request stats
3788 * @return List of Client Request Stats
3789 * @hide
3790 */
3791 @Override
3792 public List<ClientRequestStats> getClientRequestStats(String callingPackage, int subId) {
3793 if (!canReadPhoneState(callingPackage, "getClientRequestStats")) {
3794 return null;
3795 }
3796
3797 Phone phone = getPhone(subId);
3798 if (phone != null) {
3799 return phone.getClientRequestStats();
3800 }
3801
3802 return null;
3803 }
3804
3805 private WorkSource getWorkSource(WorkSource workSource, int uid) {
3806 if (workSource != null) {
3807 return workSource;
3808 }
3809
3810 String packageName = mPhone.getContext().getPackageManager().getNameForUid(uid);
3811 workSource = new WorkSource(uid, packageName);
3812 return workSource;
3813 }
Jack Yueb4124c2017-02-16 15:32:43 -08003814
3815 /**
Grace Chen70990072017-03-24 17:21:30 -07003816 * Set SIM card power state.
Jack Yueb4124c2017-02-16 15:32:43 -08003817 *
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003818 * @param slotIndex SIM slot id.
Grace Chen70990072017-03-24 17:21:30 -07003819 * @param state State of SIM (power down, power up, pass through)
3820 * - {@link android.telephony.TelephonyManager#CARD_POWER_DOWN}
3821 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP}
3822 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP_PASS_THROUGH}
Jack Yueb4124c2017-02-16 15:32:43 -08003823 *
3824 **/
3825 @Override
Grace Chen70990072017-03-24 17:21:30 -07003826 public void setSimPowerStateForSlot(int slotIndex, int state) {
Jack Yueb4124c2017-02-16 15:32:43 -08003827 enforceModifyPermission();
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003828 Phone phone = PhoneFactory.getPhone(slotIndex);
3829
Jack Yueb4124c2017-02-16 15:32:43 -08003830 if (phone != null) {
Grace Chen70990072017-03-24 17:21:30 -07003831 phone.setSimPowerState(state);
Jack Yueb4124c2017-02-16 15:32:43 -08003832 }
3833 }
Shuo Qiandd210312017-04-12 22:11:33 +00003834
3835 /**
3836 * Check if phone is in emergency callback mode
3837 * @return true if phone is in emergency callback mode
3838 * @param subId sub id
3839 */
3840 public boolean getEmergencyCallbackMode(int subId) {
3841 final Phone phone = getPhone(subId);
3842 if (phone != null) {
3843 return phone.isInEcm();
3844 } else {
3845 return false;
3846 }
3847 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003848}