blob: 2816e72afcf5db6d6d32e1c3caf8539312dca14e [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;
Nathan Harold46b42aa2017-03-10 19:38:22 -080062import android.telephony.SignalStrength;
Wink Saville0f3b5fc2014-11-11 08:40:49 -080063import android.telephony.SubscriptionInfo;
Jeff Sharkey85190e62014-12-05 09:40:12 -080064import android.telephony.SubscriptionManager;
Sanket Padawe99ef1e32016-05-18 16:12:33 -070065import android.telephony.TelephonyHistogram;
Ta-wei Yenb6929602016-05-24 15:48:27 -070066import android.telephony.TelephonyManager;
67import android.telephony.VisualVoicemailSmsFilterSettings;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070068import android.text.TextUtils;
Jeff Sharkey85190e62014-12-05 09:40:12 -080069import android.util.ArraySet;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070070import android.util.Log;
Jake Hambye994d462014-02-03 13:10:13 -080071import android.util.Pair;
Jeff Sharkey85190e62014-12-05 09:40:12 -080072import android.util.Slog;
Ta-wei Yen30a69c82016-12-27 14:52:32 -080073
Andrew Lee312e8172014-10-23 17:01:36 -070074import com.android.ims.ImsManager;
Brad Ebinger51f743a2017-01-23 13:50:20 -080075import com.android.ims.internal.IImsServiceController;
76import com.android.ims.internal.IImsServiceFeatureListener;
Shishir Agrawal566b7612013-10-28 14:41:00 -070077import com.android.internal.telephony.CallManager;
Tyler Gunn52dcf772017-04-26 11:30:31 -070078import com.android.internal.telephony.CallStateException;
Shishir Agrawal302c8692015-06-19 13:49:39 -070079import com.android.internal.telephony.CellNetworkScanResult;
Shishir Agrawal566b7612013-10-28 14:41:00 -070080import com.android.internal.telephony.CommandException;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070081import com.android.internal.telephony.DefaultPhoneNotifier;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070082import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -080083import com.android.internal.telephony.IccCard;
Narayan Kamath1c496c22015-04-16 14:40:19 +010084import com.android.internal.telephony.MccTable;
Shishir Agrawal302c8692015-06-19 13:49:39 -070085import com.android.internal.telephony.OperatorInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070086import com.android.internal.telephony.Phone;
Nathan Harolda667c152016-12-14 11:27:20 -080087import com.android.internal.telephony.PhoneConstantConversions;
Ta-wei Yen87c49842016-05-13 21:19:52 -070088import com.android.internal.telephony.PhoneConstants;
Wink Saville36469e72014-06-11 15:17:00 -070089import com.android.internal.telephony.PhoneFactory;
Wink Saville5d475dd2014-10-17 15:00:58 -070090import com.android.internal.telephony.ProxyController;
Sanket Padawe99ef1e32016-05-18 16:12:33 -070091import com.android.internal.telephony.RIL;
Svet Ganovb320e182015-04-16 12:30:10 -070092import com.android.internal.telephony.RILConstants;
Wink Savilleac1bdfd2014-11-20 23:04:44 -080093import com.android.internal.telephony.SubscriptionController;
Shishir Agrawal566b7612013-10-28 14:41:00 -070094import com.android.internal.telephony.uicc.IccIoResult;
95import com.android.internal.telephony.uicc.IccUtils;
Nathan Haroldb3014052017-01-25 15:57:32 -080096import com.android.internal.telephony.uicc.SIMRecords;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -070097import com.android.internal.telephony.uicc.UiccCard;
Nathan Haroldb3014052017-01-25 15:57:32 -080098import com.android.internal.telephony.uicc.UiccCardApplication;
Shishir Agrawal566b7612013-10-28 14:41:00 -070099import com.android.internal.telephony.uicc.UiccController;
Jake Hambye994d462014-02-03 13:10:13 -0800100import com.android.internal.util.HexDump;
Nancy Chen31f9ba12016-01-06 11:42:12 -0800101import com.android.phone.settings.VoicemailNotificationSettingsUtil;
Ta-wei Yenc9df0432017-04-17 17:09:07 -0700102import com.android.phone.vvm.PhoneAccountHandleConverter;
Ta-wei Yen527a9c02017-01-06 15:29:25 -0800103import com.android.phone.vvm.RemoteVvmTaskManager;
Ta-wei Yenc9df0432017-04-17 17:09:07 -0700104import com.android.phone.vvm.VisualVoicemailPreferences;
105import com.android.phone.vvm.VisualVoicemailSettingsUtil;
Ta-wei Yenc8905312017-03-28 11:14:45 -0700106import com.android.phone.vvm.VisualVoicemailSmsFilterConfig;
Ta-wei Yen30a69c82016-12-27 14:52:32 -0800107
Ta-wei Yenc236d6b2016-06-21 13:33:12 -0700108import java.io.FileDescriptor;
109import java.io.PrintWriter;
Ta-wei Yen30a69c82016-12-27 14:52:32 -0800110import java.nio.charset.StandardCharsets;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700111import java.util.ArrayList;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800112import java.util.Arrays;
Jake Hambye994d462014-02-03 13:10:13 -0800113import java.util.List;
Narayan Kamath1c496c22015-04-16 14:40:19 +0100114import java.util.Locale;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800115import java.util.Map;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700116
117/**
118 * Implementation of the ITelephony interface.
119 */
Santos Cordon117fee72014-05-16 17:56:12 -0700120public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700121 private static final String LOG_TAG = "PhoneInterfaceManager";
122 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
123 private static final boolean DBG_LOC = false;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800124 private static final boolean DBG_MERGE = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700125
126 // Message codes used with mMainThreadHandler
127 private static final int CMD_HANDLE_PIN_MMI = 1;
128 private static final int CMD_HANDLE_NEIGHBORING_CELL = 2;
129 private static final int EVENT_NEIGHBORING_CELL_DONE = 3;
130 private static final int CMD_ANSWER_RINGING_CALL = 4;
131 private static final int CMD_END_CALL = 5; // not used yet
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700132 private static final int CMD_TRANSMIT_APDU_LOGICAL_CHANNEL = 7;
133 private static final int EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE = 8;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700134 private static final int CMD_OPEN_CHANNEL = 9;
135 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
136 private static final int CMD_CLOSE_CHANNEL = 11;
137 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -0800138 private static final int CMD_NV_READ_ITEM = 13;
139 private static final int EVENT_NV_READ_ITEM_DONE = 14;
140 private static final int CMD_NV_WRITE_ITEM = 15;
141 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
142 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
143 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
144 private static final int CMD_NV_RESET_CONFIG = 19;
145 private static final int EVENT_NV_RESET_CONFIG_DONE = 20;
Jake Hamby7c27be32014-03-03 13:25:59 -0800146 private static final int CMD_GET_PREFERRED_NETWORK_TYPE = 21;
147 private static final int EVENT_GET_PREFERRED_NETWORK_TYPE_DONE = 22;
148 private static final int CMD_SET_PREFERRED_NETWORK_TYPE = 23;
149 private static final int EVENT_SET_PREFERRED_NETWORK_TYPE_DONE = 24;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800150 private static final int CMD_SEND_ENVELOPE = 25;
151 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Derek Tan6b088ee2014-09-05 14:15:18 -0700152 private static final int CMD_INVOKE_OEM_RIL_REQUEST_RAW = 27;
153 private static final int EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE = 28;
154 private static final int CMD_TRANSMIT_APDU_BASIC_CHANNEL = 29;
155 private static final int EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE = 30;
156 private static final int CMD_EXCHANGE_SIM_IO = 31;
157 private static final int EVENT_EXCHANGE_SIM_IO_DONE = 32;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800158 private static final int CMD_SET_VOICEMAIL_NUMBER = 33;
159 private static final int EVENT_SET_VOICEMAIL_NUMBER_DONE = 34;
Stuart Scott54788802015-03-30 13:18:01 -0700160 private static final int CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC = 35;
161 private static final int EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE = 36;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700162 private static final int CMD_GET_MODEM_ACTIVITY_INFO = 37;
163 private static final int EVENT_GET_MODEM_ACTIVITY_INFO_DONE = 38;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700164 private static final int CMD_PERFORM_NETWORK_SCAN = 39;
165 private static final int EVENT_PERFORM_NETWORK_SCAN_DONE = 40;
166 private static final int CMD_SET_NETWORK_SELECTION_MODE_MANUAL = 41;
167 private static final int EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE = 42;
Meng Wang1a7c35a2016-05-05 20:56:15 -0700168 private static final int CMD_SET_ALLOWED_CARRIERS = 43;
169 private static final int EVENT_SET_ALLOWED_CARRIERS_DONE = 44;
170 private static final int CMD_GET_ALLOWED_CARRIERS = 45;
171 private static final int EVENT_GET_ALLOWED_CARRIERS_DONE = 46;
pkanwar32d516d2016-10-14 19:37:38 -0700172 private static final int CMD_HANDLE_USSD_REQUEST = 47;
Nathan Haroldb3014052017-01-25 15:57:32 -0800173 private static final int CMD_GET_FORBIDDEN_PLMNS = 48;
174 private static final int EVENT_GET_FORBIDDEN_PLMNS_DONE = 49;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700175
176 /** The singleton instance. */
177 private static PhoneInterfaceManager sInstance;
178
Wink Saville3ab207e2014-11-20 13:07:20 -0800179 private PhoneGlobals mApp;
180 private Phone mPhone;
181 private CallManager mCM;
Stuart Scott981d8582015-04-21 14:09:50 -0700182 private UserManager mUserManager;
Wink Saville3ab207e2014-11-20 13:07:20 -0800183 private AppOpsManager mAppOps;
184 private MainThreadHandler mMainThreadHandler;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800185 private SubscriptionController mSubscriptionController;
Wink Saville3ab207e2014-11-20 13:07:20 -0800186 private SharedPreferences mTelephonySharedPreferences;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700187
Derek Tan97ebb422014-09-05 16:55:38 -0700188 private static final String PREF_CARRIERS_ALPHATAG_PREFIX = "carrier_alphtag_";
189 private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
Jeff Sharkey85190e62014-12-05 09:40:12 -0800190 private static final String PREF_CARRIERS_SUBSCRIBER_PREFIX = "carrier_subscriber_";
Derek Tan89e89d42014-07-08 17:00:10 -0700191
192 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700193 * A request object to use for transmitting data to an ICC.
194 */
195 private static final class IccAPDUArgument {
196 public int channel, cla, command, p1, p2, p3;
197 public String data;
198
199 public IccAPDUArgument(int channel, int cla, int command,
200 int p1, int p2, int p3, String data) {
201 this.channel = channel;
202 this.cla = cla;
203 this.command = command;
204 this.p1 = p1;
205 this.p2 = p2;
206 this.p3 = p3;
207 this.data = data;
208 }
209 }
210
211 /**
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700212 * A request object to use for transmitting data to an ICC.
213 */
214 private static final class ManualNetworkSelectionArgument {
215 public OperatorInfo operatorInfo;
216 public boolean persistSelection;
217
218 public ManualNetworkSelectionArgument(OperatorInfo operatorInfo, boolean persistSelection) {
219 this.operatorInfo = operatorInfo;
220 this.persistSelection = persistSelection;
221 }
222 }
223
224 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700225 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
226 * request after sending. The main thread will notify the request when it is complete.
227 */
228 private static final class MainThreadRequest {
229 /** The argument to use for the request */
230 public Object argument;
231 /** The result of the request that is run on the main thread */
232 public Object result;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800233 // The subscriber id that this request applies to. Defaults to
234 // SubscriptionManager.INVALID_SUBSCRIPTION_ID
235 public Integer subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700236
237 public MainThreadRequest(Object argument) {
238 this.argument = argument;
239 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800240
241 public MainThreadRequest(Object argument, Integer subId) {
242 this.argument = argument;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800243 if (subId != null) {
244 this.subId = subId;
245 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800246 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700247 }
248
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800249 private static final class IncomingThirdPartyCallArgs {
250 public final ComponentName component;
251 public final String callId;
252 public final String callerDisplayName;
253
254 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
255 String callerDisplayName) {
256 this.component = component;
257 this.callId = callId;
258 this.callerDisplayName = callerDisplayName;
259 }
260 }
261
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700262 /**
263 * A handler that processes messages on the main thread in the phone process. Since many
264 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
265 * inbound binder threads to the main thread in the phone process. The Binder thread
266 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
267 * on, which will be notified when the operation completes and will contain the result of the
268 * request.
269 *
270 * <p>If a MainThreadRequest object is provided in the msg.obj field,
271 * note that request.result must be set to something non-null for the calling thread to
272 * unblock.
273 */
274 private final class MainThreadHandler extends Handler {
275 @Override
276 public void handleMessage(Message msg) {
277 MainThreadRequest request;
278 Message onCompleted;
279 AsyncResult ar;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800280 UiccCard uiccCard;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700281 IccAPDUArgument iccArgument;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700282
283 switch (msg.what) {
pkanwar32d516d2016-10-14 19:37:38 -0700284 case CMD_HANDLE_USSD_REQUEST: {
285 request = (MainThreadRequest) msg.obj;
286 final Phone phone = getPhoneFromRequest(request);
287 Pair<String, ResultReceiver> ussdObject = (Pair) request.argument;
288 String ussdRequest = ussdObject.first;
289 ResultReceiver wrappedCallback = ussdObject.second;
Tyler Gunn52dcf772017-04-26 11:30:31 -0700290 try {
291 request.result = phone != null ?
292 phone.handleUssdRequest(ussdRequest, wrappedCallback)
293 : false;
294 } catch (CallStateException cse) {
295 request.result = false;
296 }
pkanwar32d516d2016-10-14 19:37:38 -0700297 // Wake up the requesting thread
298 synchronized (request) {
299 request.notifyAll();
300 }
301 break;
302 }
303
Yorke Lee716f67e2015-06-17 15:39:16 -0700304 case CMD_HANDLE_PIN_MMI: {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700305 request = (MainThreadRequest) msg.obj;
Yorke Lee716f67e2015-06-17 15:39:16 -0700306 final Phone phone = getPhoneFromRequest(request);
307 request.result = phone != null ?
308 getPhoneFromRequest(request).handlePinMmi((String) request.argument)
309 : false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700310 // Wake up the requesting thread
311 synchronized (request) {
312 request.notifyAll();
313 }
314 break;
Yorke Lee716f67e2015-06-17 15:39:16 -0700315 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700316
317 case CMD_HANDLE_NEIGHBORING_CELL:
318 request = (MainThreadRequest) msg.obj;
319 onCompleted = obtainMessage(EVENT_NEIGHBORING_CELL_DONE,
320 request);
Sooraj Sasindran9a909312016-07-18 11:57:25 -0700321 mPhone.getNeighboringCids(onCompleted, (WorkSource)request.argument);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700322 break;
323
324 case EVENT_NEIGHBORING_CELL_DONE:
325 ar = (AsyncResult) msg.obj;
326 request = (MainThreadRequest) ar.userObj;
327 if (ar.exception == null && ar.result != null) {
328 request.result = ar.result;
329 } else {
330 // create an empty list to notify the waiting thread
Jake Hambye994d462014-02-03 13:10:13 -0800331 request.result = new ArrayList<NeighboringCellInfo>(0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700332 }
333 // Wake up the requesting thread
334 synchronized (request) {
335 request.notifyAll();
336 }
337 break;
338
339 case CMD_ANSWER_RINGING_CALL:
Wink Saville08874612014-08-31 19:19:58 -0700340 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800341 int answer_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700342 answerRingingCallInternal(answer_subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700343 break;
344
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700345 case CMD_END_CALL:
346 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800347 int end_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700348 final boolean hungUp;
Anthony Leeae4e36d2015-05-21 07:17:46 -0700349 Phone phone = getPhone(end_subId);
350 if (phone == null) {
351 if (DBG) log("CMD_END_CALL: no phone for id: " + end_subId);
352 break;
353 }
354 int phoneType = phone.getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700355 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
356 // CDMA: If the user presses the Power button we treat it as
357 // ending the complete call session
Wink Saville08874612014-08-31 19:19:58 -0700358 hungUp = PhoneUtils.hangupRingingAndActive(getPhone(end_subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700359 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
360 // GSM: End the call as per the Phone state
361 hungUp = PhoneUtils.hangup(mCM);
362 } else {
363 throw new IllegalStateException("Unexpected phone type: " + phoneType);
364 }
365 if (DBG) log("CMD_END_CALL: " + (hungUp ? "hung up!" : "no call to hang up"));
366 request.result = hungUp;
367 // Wake up the requesting thread
368 synchronized (request) {
369 request.notifyAll();
370 }
371 break;
372
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700373 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700374 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700375 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800376 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700377 if (uiccCard == null) {
378 loge("iccTransmitApduLogicalChannel: No UICC");
379 request.result = new IccIoResult(0x6F, 0, (byte[])null);
380 synchronized (request) {
381 request.notifyAll();
382 }
383 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700384 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
385 request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700386 uiccCard.iccTransmitApduLogicalChannel(
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700387 iccArgument.channel, iccArgument.cla, iccArgument.command,
388 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
Shishir Agrawal566b7612013-10-28 14:41:00 -0700389 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700390 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700391 break;
392
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700393 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700394 ar = (AsyncResult) msg.obj;
395 request = (MainThreadRequest) ar.userObj;
396 if (ar.exception == null && ar.result != null) {
397 request.result = ar.result;
398 } else {
399 request.result = new IccIoResult(0x6F, 0, (byte[])null);
400 if (ar.result == null) {
401 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800402 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700403 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800404 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700405 } else {
406 loge("iccTransmitApduLogicalChannel: Unknown exception");
407 }
408 }
409 synchronized (request) {
410 request.notifyAll();
411 }
412 break;
413
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700414 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
415 request = (MainThreadRequest) msg.obj;
416 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800417 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700418 if (uiccCard == null) {
419 loge("iccTransmitApduBasicChannel: No UICC");
420 request.result = new IccIoResult(0x6F, 0, (byte[])null);
421 synchronized (request) {
422 request.notifyAll();
423 }
424 } else {
425 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
426 request);
427 uiccCard.iccTransmitApduBasicChannel(
428 iccArgument.cla, iccArgument.command, iccArgument.p1, iccArgument.p2,
429 iccArgument.p3, iccArgument.data, onCompleted);
430 }
431 break;
432
433 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
434 ar = (AsyncResult) msg.obj;
435 request = (MainThreadRequest) ar.userObj;
436 if (ar.exception == null && ar.result != null) {
437 request.result = ar.result;
438 } else {
439 request.result = new IccIoResult(0x6F, 0, (byte[])null);
440 if (ar.result == null) {
441 loge("iccTransmitApduBasicChannel: Empty response");
442 } else if (ar.exception instanceof CommandException) {
443 loge("iccTransmitApduBasicChannel: CommandException: " +
444 ar.exception);
445 } else {
446 loge("iccTransmitApduBasicChannel: Unknown exception");
447 }
448 }
449 synchronized (request) {
450 request.notifyAll();
451 }
452 break;
453
454 case CMD_EXCHANGE_SIM_IO:
455 request = (MainThreadRequest) msg.obj;
456 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800457 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700458 if (uiccCard == null) {
459 loge("iccExchangeSimIO: No UICC");
460 request.result = new IccIoResult(0x6F, 0, (byte[])null);
461 synchronized (request) {
462 request.notifyAll();
463 }
464 } else {
465 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
466 request);
467 uiccCard.iccExchangeSimIO(iccArgument.cla, /* fileID */
468 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
469 iccArgument.data, onCompleted);
470 }
471 break;
472
473 case EVENT_EXCHANGE_SIM_IO_DONE:
474 ar = (AsyncResult) msg.obj;
475 request = (MainThreadRequest) ar.userObj;
476 if (ar.exception == null && ar.result != null) {
477 request.result = ar.result;
478 } else {
479 request.result = new IccIoResult(0x6f, 0, (byte[])null);
480 }
481 synchronized (request) {
482 request.notifyAll();
483 }
484 break;
485
Derek Tan4d5e5c12014-02-04 11:54:58 -0800486 case CMD_SEND_ENVELOPE:
487 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800488 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700489 if (uiccCard == null) {
490 loge("sendEnvelopeWithStatus: No UICC");
491 request.result = new IccIoResult(0x6F, 0, (byte[])null);
492 synchronized (request) {
493 request.notifyAll();
494 }
495 } else {
496 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
497 uiccCard.sendEnvelopeWithStatus((String)request.argument, onCompleted);
498 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800499 break;
500
501 case EVENT_SEND_ENVELOPE_DONE:
502 ar = (AsyncResult) msg.obj;
503 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700504 if (ar.exception == null && ar.result != null) {
505 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800506 } else {
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700507 request.result = new IccIoResult(0x6F, 0, (byte[])null);
508 if (ar.result == null) {
509 loge("sendEnvelopeWithStatus: Empty response");
510 } else if (ar.exception instanceof CommandException) {
511 loge("sendEnvelopeWithStatus: CommandException: " +
512 ar.exception);
513 } else {
514 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
515 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800516 }
517 synchronized (request) {
518 request.notifyAll();
519 }
520 break;
521
Shishir Agrawal566b7612013-10-28 14:41:00 -0700522 case CMD_OPEN_CHANNEL:
523 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800524 uiccCard = getUiccCardFromRequest(request);
Ajay Nambid7454d32015-12-03 13:50:00 -0800525 Pair<String, Integer> openChannelArgs = (Pair<String, Integer>) request.argument;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700526 if (uiccCard == null) {
527 loge("iccOpenLogicalChannel: No UICC");
Shishir Agrawalfc0492a2016-02-17 11:15:33 -0800528 request.result = new IccOpenLogicalChannelResponse(-1,
529 IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE, null);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700530 synchronized (request) {
531 request.notifyAll();
532 }
533 } else {
534 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
Ajay Nambid7454d32015-12-03 13:50:00 -0800535 uiccCard.iccOpenLogicalChannel(openChannelArgs.first,
536 openChannelArgs.second, onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700537 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700538 break;
539
540 case EVENT_OPEN_CHANNEL_DONE:
541 ar = (AsyncResult) msg.obj;
542 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700543 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700544 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700545 int[] result = (int[]) ar.result;
546 int channelId = result[0];
547 byte[] selectResponse = null;
548 if (result.length > 1) {
549 selectResponse = new byte[result.length - 1];
550 for (int i = 1; i < result.length; ++i) {
551 selectResponse[i - 1] = (byte) result[i];
552 }
553 }
554 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700555 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700556 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700557 if (ar.result == null) {
558 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700559 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700560 if (ar.exception != null) {
561 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
562 }
563
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700564 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Junda Liua754ba12015-05-20 01:17:52 -0700565 if (ar.exception instanceof CommandException) {
566 CommandException.Error error =
567 ((CommandException) (ar.exception)).getCommandError();
568 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700569 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700570 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700571 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700572 }
573 }
574 openChannelResp = new IccOpenLogicalChannelResponse(
575 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700576 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700577 request.result = openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700578 synchronized (request) {
579 request.notifyAll();
580 }
581 break;
582
583 case CMD_CLOSE_CHANNEL:
584 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800585 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700586 if (uiccCard == null) {
587 loge("iccCloseLogicalChannel: No UICC");
588 request.result = new IccIoResult(0x6F, 0, (byte[])null);
589 synchronized (request) {
590 request.notifyAll();
591 }
592 } else {
593 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
594 uiccCard.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
595 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700596 break;
597
598 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800599 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
600 break;
601
602 case CMD_NV_READ_ITEM:
603 request = (MainThreadRequest) msg.obj;
604 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
605 mPhone.nvReadItem((Integer) request.argument, onCompleted);
606 break;
607
608 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700609 ar = (AsyncResult) msg.obj;
610 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800611 if (ar.exception == null && ar.result != null) {
612 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700613 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800614 request.result = "";
615 if (ar.result == null) {
616 loge("nvReadItem: Empty response");
617 } else if (ar.exception instanceof CommandException) {
618 loge("nvReadItem: CommandException: " +
619 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700620 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800621 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700622 }
623 }
624 synchronized (request) {
625 request.notifyAll();
626 }
627 break;
628
Jake Hambye994d462014-02-03 13:10:13 -0800629 case CMD_NV_WRITE_ITEM:
630 request = (MainThreadRequest) msg.obj;
631 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
632 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
633 mPhone.nvWriteItem(idValue.first, idValue.second, onCompleted);
634 break;
635
636 case EVENT_NV_WRITE_ITEM_DONE:
637 handleNullReturnEvent(msg, "nvWriteItem");
638 break;
639
640 case CMD_NV_WRITE_CDMA_PRL:
641 request = (MainThreadRequest) msg.obj;
642 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
643 mPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
644 break;
645
646 case EVENT_NV_WRITE_CDMA_PRL_DONE:
647 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
648 break;
649
650 case CMD_NV_RESET_CONFIG:
651 request = (MainThreadRequest) msg.obj;
652 onCompleted = obtainMessage(EVENT_NV_RESET_CONFIG_DONE, request);
653 mPhone.nvResetConfig((Integer) request.argument, onCompleted);
654 break;
655
656 case EVENT_NV_RESET_CONFIG_DONE:
657 handleNullReturnEvent(msg, "nvResetConfig");
658 break;
659
Jake Hamby7c27be32014-03-03 13:25:59 -0800660 case CMD_GET_PREFERRED_NETWORK_TYPE:
661 request = (MainThreadRequest) msg.obj;
662 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
Stuart Scott54788802015-03-30 13:18:01 -0700663 getPhoneFromRequest(request).getPreferredNetworkType(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800664 break;
665
666 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
667 ar = (AsyncResult) msg.obj;
668 request = (MainThreadRequest) ar.userObj;
669 if (ar.exception == null && ar.result != null) {
670 request.result = ar.result; // Integer
671 } else {
Sanket Padawecfc2d352016-01-05 19:52:14 -0800672 request.result = null;
Jake Hamby7c27be32014-03-03 13:25:59 -0800673 if (ar.result == null) {
674 loge("getPreferredNetworkType: Empty response");
675 } else if (ar.exception instanceof CommandException) {
676 loge("getPreferredNetworkType: CommandException: " +
677 ar.exception);
678 } else {
679 loge("getPreferredNetworkType: Unknown exception");
680 }
681 }
682 synchronized (request) {
683 request.notifyAll();
684 }
685 break;
686
687 case CMD_SET_PREFERRED_NETWORK_TYPE:
688 request = (MainThreadRequest) msg.obj;
689 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
690 int networkType = (Integer) request.argument;
Stuart Scott54788802015-03-30 13:18:01 -0700691 getPhoneFromRequest(request).setPreferredNetworkType(networkType, onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800692 break;
693
694 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
695 handleNullReturnEvent(msg, "setPreferredNetworkType");
696 break;
697
Steven Liu4bf01bc2014-07-17 11:05:29 -0500698 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
699 request = (MainThreadRequest)msg.obj;
700 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
701 mPhone.invokeOemRilRequestRaw((byte[])request.argument, onCompleted);
702 break;
703
704 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
705 ar = (AsyncResult)msg.obj;
706 request = (MainThreadRequest)ar.userObj;
707 request.result = ar;
708 synchronized (request) {
709 request.notifyAll();
710 }
711 break;
712
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800713 case CMD_SET_VOICEMAIL_NUMBER:
714 request = (MainThreadRequest) msg.obj;
715 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
716 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -0800717 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
718 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800719 break;
720
721 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
722 handleNullReturnEvent(msg, "setVoicemailNumber");
723 break;
724
Stuart Scott54788802015-03-30 13:18:01 -0700725 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
726 request = (MainThreadRequest) msg.obj;
727 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
728 request);
729 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
730 break;
731
732 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
733 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
734 break;
735
Shishir Agrawal302c8692015-06-19 13:49:39 -0700736 case CMD_PERFORM_NETWORK_SCAN:
737 request = (MainThreadRequest) msg.obj;
738 onCompleted = obtainMessage(EVENT_PERFORM_NETWORK_SCAN_DONE, request);
739 getPhoneFromRequest(request).getAvailableNetworks(onCompleted);
740 break;
741
742 case EVENT_PERFORM_NETWORK_SCAN_DONE:
743 ar = (AsyncResult) msg.obj;
744 request = (MainThreadRequest) ar.userObj;
745 CellNetworkScanResult cellScanResult;
746 if (ar.exception == null && ar.result != null) {
747 cellScanResult = new CellNetworkScanResult(
748 CellNetworkScanResult.STATUS_SUCCESS,
749 (List<OperatorInfo>) ar.result);
750 } else {
751 if (ar.result == null) {
752 loge("getCellNetworkScanResults: Empty response");
753 }
754 if (ar.exception != null) {
755 loge("getCellNetworkScanResults: Exception: " + ar.exception);
756 }
757 int errorCode = CellNetworkScanResult.STATUS_UNKNOWN_ERROR;
758 if (ar.exception instanceof CommandException) {
759 CommandException.Error error =
760 ((CommandException) (ar.exception)).getCommandError();
761 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
762 errorCode = CellNetworkScanResult.STATUS_RADIO_NOT_AVAILABLE;
763 } else if (error == CommandException.Error.GENERIC_FAILURE) {
764 errorCode = CellNetworkScanResult.STATUS_RADIO_GENERIC_FAILURE;
765 }
766 }
767 cellScanResult = new CellNetworkScanResult(errorCode, null);
768 }
769 request.result = cellScanResult;
770 synchronized (request) {
771 request.notifyAll();
772 }
773 break;
774
775 case CMD_SET_NETWORK_SELECTION_MODE_MANUAL:
776 request = (MainThreadRequest) msg.obj;
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700777 ManualNetworkSelectionArgument selArg =
778 (ManualNetworkSelectionArgument) request.argument;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700779 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE,
780 request);
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700781 getPhoneFromRequest(request).selectNetworkManually(selArg.operatorInfo,
782 selArg.persistSelection, onCompleted);
Shishir Agrawal302c8692015-06-19 13:49:39 -0700783 break;
784
785 case EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE:
786 handleNullReturnEvent(msg, "setNetworkSelectionModeManual");
787 break;
788
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700789 case CMD_GET_MODEM_ACTIVITY_INFO:
790 request = (MainThreadRequest) msg.obj;
791 onCompleted = obtainMessage(EVENT_GET_MODEM_ACTIVITY_INFO_DONE, request);
Prerepa Viswanadham61a60ad2015-06-08 18:07:51 -0700792 mPhone.getModemActivityInfo(onCompleted);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700793 break;
794
795 case EVENT_GET_MODEM_ACTIVITY_INFO_DONE:
796 ar = (AsyncResult) msg.obj;
797 request = (MainThreadRequest) ar.userObj;
798 if (ar.exception == null && ar.result != null) {
799 request.result = ar.result;
800 } else {
801 if (ar.result == null) {
802 loge("queryModemActivityInfo: Empty response");
803 } else if (ar.exception instanceof CommandException) {
804 loge("queryModemActivityInfo: CommandException: " +
805 ar.exception);
806 } else {
807 loge("queryModemActivityInfo: Unknown exception");
808 }
809 }
Amit Mahajand4766222016-01-28 15:28:28 -0800810 // Result cannot be null. Return ModemActivityInfo with all fields set to 0.
811 if (request.result == null) {
812 request.result = new ModemActivityInfo(0, 0, 0, null, 0, 0);
813 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700814 synchronized (request) {
815 request.notifyAll();
816 }
817 break;
818
Meng Wang1a7c35a2016-05-05 20:56:15 -0700819 case CMD_SET_ALLOWED_CARRIERS:
820 request = (MainThreadRequest) msg.obj;
821 onCompleted = obtainMessage(EVENT_SET_ALLOWED_CARRIERS_DONE, request);
822 mPhone.setAllowedCarriers(
823 (List<CarrierIdentifier>) request.argument,
824 onCompleted);
825 break;
826
827 case EVENT_SET_ALLOWED_CARRIERS_DONE:
828 ar = (AsyncResult) msg.obj;
829 request = (MainThreadRequest) ar.userObj;
830 if (ar.exception == null && ar.result != null) {
831 request.result = ar.result;
832 } else {
833 if (ar.result == null) {
834 loge("setAllowedCarriers: Empty response");
835 } else if (ar.exception instanceof CommandException) {
836 loge("setAllowedCarriers: CommandException: " +
837 ar.exception);
838 } else {
839 loge("setAllowedCarriers: Unknown exception");
840 }
841 }
842 // Result cannot be null. Return -1 on error.
843 if (request.result == null) {
844 request.result = new int[]{-1};
845 }
846 synchronized (request) {
847 request.notifyAll();
848 }
849 break;
850
851 case CMD_GET_ALLOWED_CARRIERS:
852 request = (MainThreadRequest) msg.obj;
853 onCompleted = obtainMessage(EVENT_GET_ALLOWED_CARRIERS_DONE, request);
854 mPhone.getAllowedCarriers(onCompleted);
855 break;
856
857 case EVENT_GET_ALLOWED_CARRIERS_DONE:
858 ar = (AsyncResult) msg.obj;
859 request = (MainThreadRequest) ar.userObj;
860 if (ar.exception == null && ar.result != null) {
861 request.result = ar.result;
862 } else {
863 if (ar.result == null) {
864 loge("getAllowedCarriers: Empty response");
865 } else if (ar.exception instanceof CommandException) {
866 loge("getAllowedCarriers: CommandException: " +
867 ar.exception);
868 } else {
869 loge("getAllowedCarriers: Unknown exception");
870 }
871 }
872 // Result cannot be null. Return empty list of CarrierIdentifier.
873 if (request.result == null) {
874 request.result = new ArrayList<CarrierIdentifier>(0);
875 }
876 synchronized (request) {
877 request.notifyAll();
878 }
879 break;
880
Nathan Haroldb3014052017-01-25 15:57:32 -0800881 case EVENT_GET_FORBIDDEN_PLMNS_DONE:
882 ar = (AsyncResult) msg.obj;
883 request = (MainThreadRequest) ar.userObj;
884 if (ar.exception == null && ar.result != null) {
885 request.result = ar.result;
886 } else {
887 request.result = new IllegalArgumentException(
888 "Failed to retrieve Forbidden Plmns");
889 if (ar.result == null) {
890 loge("getForbiddenPlmns: Empty response");
891 } else {
892 loge("getForbiddenPlmns: Unknown exception");
893 }
894 }
895 synchronized (request) {
896 request.notifyAll();
897 }
898 break;
899
900 case CMD_GET_FORBIDDEN_PLMNS:
901 request = (MainThreadRequest) msg.obj;
902 uiccCard = getUiccCardFromRequest(request);
903 if (uiccCard == null) {
904 loge("getForbiddenPlmns() UiccCard is null");
905 request.result = new IllegalArgumentException(
906 "getForbiddenPlmns() UiccCard is null");
907 synchronized (request) {
908 request.notifyAll();
909 }
910 break;
911 }
912 Integer appType = (Integer) request.argument;
913 UiccCardApplication uiccApp = uiccCard.getApplicationByType(appType);
914 if (uiccApp == null) {
915 loge("getForbiddenPlmns() no app with specified type -- "
916 + appType);
917 request.result = new IllegalArgumentException("Failed to get UICC App");
918 synchronized (request) {
919 request.notifyAll();
920 }
921 break;
922 } else {
923 if (DBG) logv("getForbiddenPlmns() found app " + uiccApp.getAid()
924 + " specified type -- " + appType);
925 }
926 onCompleted = obtainMessage(EVENT_GET_FORBIDDEN_PLMNS_DONE, request);
927 ((SIMRecords) uiccApp.getIccRecords()).getForbiddenPlmns(
928 onCompleted);
929 break;
930
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700931 default:
932 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
933 break;
934 }
935 }
Jake Hambye994d462014-02-03 13:10:13 -0800936
937 private void handleNullReturnEvent(Message msg, String command) {
938 AsyncResult ar = (AsyncResult) msg.obj;
939 MainThreadRequest request = (MainThreadRequest) ar.userObj;
940 if (ar.exception == null) {
941 request.result = true;
942 } else {
943 request.result = false;
944 if (ar.exception instanceof CommandException) {
945 loge(command + ": CommandException: " + ar.exception);
946 } else {
947 loge(command + ": Unknown exception");
948 }
949 }
950 synchronized (request) {
951 request.notifyAll();
952 }
953 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700954 }
955
956 /**
957 * Posts the specified command to be executed on the main thread,
958 * waits for the request to complete, and returns the result.
959 * @see #sendRequestAsync
960 */
961 private Object sendRequest(int command, Object argument) {
Sanket Padawe56e75a32016-02-08 12:18:19 -0800962 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Wink Saville36469e72014-06-11 15:17:00 -0700963 }
964
965 /**
966 * Posts the specified command to be executed on the main thread,
967 * waits for the request to complete, and returns the result.
968 * @see #sendRequestAsync
969 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800970 private Object sendRequest(int command, Object argument, Integer subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700971 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
972 throw new RuntimeException("This method will deadlock if called from the main thread.");
973 }
974
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800975 MainThreadRequest request = new MainThreadRequest(argument, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700976 Message msg = mMainThreadHandler.obtainMessage(command, request);
977 msg.sendToTarget();
978
979 // Wait for the request to complete
980 synchronized (request) {
981 while (request.result == null) {
982 try {
983 request.wait();
984 } catch (InterruptedException e) {
985 // Do nothing, go back and wait until the request is complete
986 }
987 }
988 }
989 return request.result;
990 }
991
992 /**
993 * Asynchronous ("fire and forget") version of sendRequest():
994 * Posts the specified command to be executed on the main thread, and
995 * returns immediately.
996 * @see #sendRequest
997 */
998 private void sendRequestAsync(int command) {
999 mMainThreadHandler.sendEmptyMessage(command);
1000 }
1001
1002 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001003 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
1004 * @see {@link #sendRequest(int,Object)}
1005 */
1006 private void sendRequestAsync(int command, Object argument) {
1007 MainThreadRequest request = new MainThreadRequest(argument);
1008 Message msg = mMainThreadHandler.obtainMessage(command, request);
1009 msg.sendToTarget();
1010 }
1011
1012 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001013 * Initialize the singleton PhoneInterfaceManager instance.
1014 * This is only done once, at startup, from PhoneApp.onCreate().
1015 */
Sailesh Nepal194161e2014-07-03 08:57:44 -07001016 /* package */ static PhoneInterfaceManager init(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001017 synchronized (PhoneInterfaceManager.class) {
1018 if (sInstance == null) {
Sailesh Nepal194161e2014-07-03 08:57:44 -07001019 sInstance = new PhoneInterfaceManager(app, phone);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001020 } else {
1021 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
1022 }
1023 return sInstance;
1024 }
1025 }
1026
1027 /** Private constructor; @see init() */
Sailesh Nepal194161e2014-07-03 08:57:44 -07001028 private PhoneInterfaceManager(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001029 mApp = app;
1030 mPhone = phone;
1031 mCM = PhoneGlobals.getInstance().mCM;
Stuart Scott981d8582015-04-21 14:09:50 -07001032 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001033 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
1034 mMainThreadHandler = new MainThreadHandler();
Andrew Leedf14ead2014-10-17 14:22:52 -07001035 mTelephonySharedPreferences =
Derek Tan97ebb422014-09-05 16:55:38 -07001036 PreferenceManager.getDefaultSharedPreferences(mPhone.getContext());
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001037 mSubscriptionController = SubscriptionController.getInstance();
Wink Saville3ab207e2014-11-20 13:07:20 -08001038
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001039 publish();
1040 }
1041
1042 private void publish() {
1043 if (DBG) log("publish: " + this);
1044
1045 ServiceManager.addService("phone", this);
1046 }
1047
Stuart Scott584921c2015-01-15 17:10:34 -08001048 private Phone getPhoneFromRequest(MainThreadRequest request) {
Sanket Padawe56e75a32016-02-08 12:18:19 -08001049 return (request.subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID)
1050 ? mPhone : getPhone(request.subId);
Stuart Scott584921c2015-01-15 17:10:34 -08001051 }
1052
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001053 private UiccCard getUiccCardFromRequest(MainThreadRequest request) {
1054 Phone phone = getPhoneFromRequest(request);
1055 return phone == null ? null :
1056 UiccController.getInstance().getUiccCard(phone.getPhoneId());
1057 }
1058
Wink Saville36469e72014-06-11 15:17:00 -07001059 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -07001060 private Phone getPhone(int subId) {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001061 return PhoneFactory.getPhone(mSubscriptionController.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -07001062 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001063 //
1064 // Implementation of the ITelephony interface.
1065 //
1066
1067 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001068 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -07001069 }
1070
Wink Savilleb564aae2014-10-23 10:18:09 -07001071 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001072 if (DBG) log("dial: " + number);
1073 // No permission check needed here: This is just a wrapper around the
1074 // ACTION_DIAL intent, which is available to any app since it puts up
1075 // the UI before it does anything.
1076
1077 String url = createTelUrl(number);
1078 if (url == null) {
1079 return;
1080 }
1081
1082 // PENDING: should we just silently fail if phone is offhook or ringing?
Wink Saville36469e72014-06-11 15:17:00 -07001083 PhoneConstants.State state = mCM.getState(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001084 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
1085 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
1086 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1087 mApp.startActivity(intent);
1088 }
1089 }
1090
1091 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001092 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -07001093 }
1094
Wink Savilleb564aae2014-10-23 10:18:09 -07001095 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001096 if (DBG) log("call: " + number);
1097
1098 // This is just a wrapper around the ACTION_CALL intent, but we still
1099 // need to do a permission check since we're calling startActivity()
1100 // from the context of the phone app.
1101 enforceCallPermission();
1102
1103 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
1104 != AppOpsManager.MODE_ALLOWED) {
1105 return;
1106 }
1107
1108 String url = createTelUrl(number);
1109 if (url == null) {
1110 return;
1111 }
1112
Wink Saville08874612014-08-31 19:19:58 -07001113 boolean isValid = false;
Narayan Kamath1c496c22015-04-16 14:40:19 +01001114 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoList();
Wink Saville3ab207e2014-11-20 13:07:20 -08001115 if (slist != null) {
1116 for (SubscriptionInfo subInfoRecord : slist) {
1117 if (subInfoRecord.getSubscriptionId() == subId) {
1118 isValid = true;
1119 break;
1120 }
Wink Saville08874612014-08-31 19:19:58 -07001121 }
1122 }
1123 if (isValid == false) {
1124 return;
1125 }
1126
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001127 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
Wink Saville36469e72014-06-11 15:17:00 -07001128 intent.putExtra(SUBSCRIPTION_KEY, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001129 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1130 mApp.startActivity(intent);
1131 }
1132
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001133 /**
1134 * End a call based on call state
1135 * @return true is a call was ended
1136 */
1137 public boolean endCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001138 return endCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001139 }
1140
1141 /**
1142 * End a call based on the call state of the subId
1143 * @return true is a call was ended
1144 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001145 public boolean endCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001146 enforceCallPermission();
Stuart Scott584921c2015-01-15 17:10:34 -08001147 return (Boolean) sendRequest(CMD_END_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001148 }
1149
1150 public void answerRingingCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001151 answerRingingCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001152 }
1153
Wink Savilleb564aae2014-10-23 10:18:09 -07001154 public void answerRingingCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001155 if (DBG) log("answerRingingCall...");
1156 // TODO: there should eventually be a separate "ANSWER_PHONE" permission,
1157 // but that can probably wait till the big TelephonyManager API overhaul.
1158 // For now, protect this call with the MODIFY_PHONE_STATE permission.
1159 enforceModifyPermission();
Stuart Scott584921c2015-01-15 17:10:34 -08001160 sendRequest(CMD_ANSWER_RINGING_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001161 }
1162
1163 /**
1164 * Make the actual telephony calls to implement answerRingingCall().
1165 * This should only be called from the main thread of the Phone app.
1166 * @see #answerRingingCall
1167 *
1168 * TODO: it would be nice to return true if we answered the call, or
1169 * false if there wasn't actually a ringing incoming call, or some
1170 * other error occurred. (In other words, pass back the return value
1171 * from PhoneUtils.answerCall() or PhoneUtils.answerAndEndActive().)
1172 * But that would require calling this method via sendRequest() rather
1173 * than sendRequestAsync(), and right now we don't actually *need* that
1174 * return value, so let's just return void for now.
1175 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001176 private void answerRingingCallInternal(int subId) {
Wink Saville08874612014-08-31 19:19:58 -07001177 final boolean hasRingingCall = !getPhone(subId).getRingingCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001178 if (hasRingingCall) {
Wink Saville08874612014-08-31 19:19:58 -07001179 final boolean hasActiveCall = !getPhone(subId).getForegroundCall().isIdle();
1180 final boolean hasHoldingCall = !getPhone(subId).getBackgroundCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001181 if (hasActiveCall && hasHoldingCall) {
1182 // Both lines are in use!
1183 // TODO: provide a flag to let the caller specify what
1184 // policy to use if both lines are in use. (The current
1185 // behavior is hardwired to "answer incoming, end ongoing",
1186 // which is how the CALL button is specced to behave.)
1187 PhoneUtils.answerAndEndActive(mCM, mCM.getFirstActiveRingingCall());
1188 return;
1189 } else {
1190 // answerCall() will automatically hold the current active
1191 // call, if there is one.
1192 PhoneUtils.answerCall(mCM.getFirstActiveRingingCall());
1193 return;
1194 }
1195 } else {
1196 // No call was ringing.
1197 return;
1198 }
1199 }
1200
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001201 /**
Santos Cordon5422a8d2014-09-12 04:20:56 -07001202 * This method is no longer used and can be removed once TelephonyManager stops referring to it.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001203 */
Santos Cordon5422a8d2014-09-12 04:20:56 -07001204 public void silenceRinger() {
1205 Log.e(LOG_TAG, "silenseRinger not supported");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001206 }
1207
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001208 @Override
1209 public boolean isOffhook(String callingPackage) {
1210 return isOffhookForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001211 }
1212
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001213 @Override
1214 public boolean isOffhookForSubscriber(int subId, String callingPackage) {
1215 if (!canReadPhoneState(callingPackage, "isOffhookForSubscriber")) {
1216 return false;
1217 }
1218
Sanket Padawe356d7632015-06-22 14:03:32 -07001219 final Phone phone = getPhone(subId);
1220 if (phone != null) {
1221 return (phone.getState() == PhoneConstants.State.OFFHOOK);
1222 } else {
1223 return false;
1224 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001225 }
1226
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001227 @Override
1228 public boolean isRinging(String callingPackage) {
1229 return (isRingingForSubscriber(getDefaultSubscription(), callingPackage));
Wink Saville36469e72014-06-11 15:17:00 -07001230 }
1231
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001232 @Override
1233 public boolean isRingingForSubscriber(int subId, String callingPackage) {
1234 if (!canReadPhoneState(callingPackage, "isRingingForSubscriber")) {
1235 return false;
1236 }
1237
Sanket Padawe356d7632015-06-22 14:03:32 -07001238 final Phone phone = getPhone(subId);
1239 if (phone != null) {
1240 return (phone.getState() == PhoneConstants.State.RINGING);
1241 } else {
1242 return false;
1243 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001244 }
1245
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001246 @Override
1247 public boolean isIdle(String callingPackage) {
1248 return isIdleForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001249 }
1250
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001251 @Override
1252 public boolean isIdleForSubscriber(int subId, String callingPackage) {
1253 if (!canReadPhoneState(callingPackage, "isIdleForSubscriber")) {
1254 return false;
1255 }
1256
Sanket Padawe356d7632015-06-22 14:03:32 -07001257 final Phone phone = getPhone(subId);
1258 if (phone != null) {
1259 return (phone.getState() == PhoneConstants.State.IDLE);
1260 } else {
1261 return false;
1262 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001263 }
1264
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001265 public boolean supplyPin(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001266 return supplyPinForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001267 }
1268
Wink Savilleb564aae2014-10-23 10:18:09 -07001269 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001270 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001271 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1272 }
1273
1274 public boolean supplyPuk(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001275 return supplyPukForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001276 }
1277
Wink Savilleb564aae2014-10-23 10:18:09 -07001278 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001279 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001280 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1281 }
1282
1283 /** {@hide} */
1284 public int[] supplyPinReportResult(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001285 return supplyPinReportResultForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001286 }
1287
Wink Savilleb564aae2014-10-23 10:18:09 -07001288 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001289 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001290 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001291 checkSimPin.start();
1292 return checkSimPin.unlockSim(null, pin);
1293 }
1294
Wink Saville9de0f752013-10-22 19:04:03 -07001295 /** {@hide} */
1296 public int[] supplyPukReportResult(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001297 return supplyPukReportResultForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001298 }
1299
Wink Savilleb564aae2014-10-23 10:18:09 -07001300 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001301 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001302 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001303 checkSimPuk.start();
1304 return checkSimPuk.unlockSim(puk, pin);
1305 }
1306
1307 /**
Wink Saville9de0f752013-10-22 19:04:03 -07001308 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001309 * a synchronous one.
1310 */
1311 private static class UnlockSim extends Thread {
1312
1313 private final IccCard mSimCard;
1314
1315 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07001316 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1317 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001318
1319 // For replies from SimCard interface
1320 private Handler mHandler;
1321
1322 // For async handler to identify request type
1323 private static final int SUPPLY_PIN_COMPLETE = 100;
1324
1325 public UnlockSim(IccCard simCard) {
1326 mSimCard = simCard;
1327 }
1328
1329 @Override
1330 public void run() {
1331 Looper.prepare();
1332 synchronized (UnlockSim.this) {
1333 mHandler = new Handler() {
1334 @Override
1335 public void handleMessage(Message msg) {
1336 AsyncResult ar = (AsyncResult) msg.obj;
1337 switch (msg.what) {
1338 case SUPPLY_PIN_COMPLETE:
1339 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
1340 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07001341 mRetryCount = msg.arg1;
1342 if (ar.exception != null) {
1343 if (ar.exception instanceof CommandException &&
1344 ((CommandException)(ar.exception)).getCommandError()
1345 == CommandException.Error.PASSWORD_INCORRECT) {
1346 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
1347 } else {
1348 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1349 }
1350 } else {
1351 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
1352 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001353 mDone = true;
1354 UnlockSim.this.notifyAll();
1355 }
1356 break;
1357 }
1358 }
1359 };
1360 UnlockSim.this.notifyAll();
1361 }
1362 Looper.loop();
1363 }
1364
1365 /*
1366 * Use PIN or PUK to unlock SIM card
1367 *
1368 * If PUK is null, unlock SIM card with PIN
1369 *
1370 * If PUK is not null, unlock SIM card with PUK and set PIN code
1371 */
Wink Saville9de0f752013-10-22 19:04:03 -07001372 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001373
1374 while (mHandler == null) {
1375 try {
1376 wait();
1377 } catch (InterruptedException e) {
1378 Thread.currentThread().interrupt();
1379 }
1380 }
1381 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
1382
1383 if (puk == null) {
1384 mSimCard.supplyPin(pin, callback);
1385 } else {
1386 mSimCard.supplyPuk(puk, pin, callback);
1387 }
1388
1389 while (!mDone) {
1390 try {
1391 Log.d(LOG_TAG, "wait for done");
1392 wait();
1393 } catch (InterruptedException e) {
1394 // Restore the interrupted status
1395 Thread.currentThread().interrupt();
1396 }
1397 }
1398 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07001399 int[] resultArray = new int[2];
1400 resultArray[0] = mResult;
1401 resultArray[1] = mRetryCount;
1402 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001403 }
1404 }
1405
1406 public void updateServiceLocation() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001407 updateServiceLocationForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001408
1409 }
1410
Wink Savilleb564aae2014-10-23 10:18:09 -07001411 public void updateServiceLocationForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001412 // No permission check needed here: this call is harmless, and it's
1413 // needed for the ServiceState.requestStateUpdate() call (which is
1414 // already intentionally exposed to 3rd parties.)
Sanket Padawe356d7632015-06-22 14:03:32 -07001415 final Phone phone = getPhone(subId);
1416 if (phone != null) {
1417 phone.updateServiceLocation();
1418 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001419 }
1420
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001421 @Override
1422 public boolean isRadioOn(String callingPackage) {
1423 return isRadioOnForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001424 }
1425
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001426 @Override
1427 public boolean isRadioOnForSubscriber(int subId, String callingPackage) {
1428 if (!canReadPhoneState(callingPackage, "isRadioOnForSubscriber")) {
1429 return false;
1430 }
1431 return isRadioOnForSubscriber(subId);
1432 }
1433
1434 private boolean isRadioOnForSubscriber(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001435 final Phone phone = getPhone(subId);
1436 if (phone != null) {
1437 return phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF;
1438 } else {
1439 return false;
1440 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001441 }
1442
1443 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001444 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001445
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001446 }
Wink Saville36469e72014-06-11 15:17:00 -07001447
Wink Savilleb564aae2014-10-23 10:18:09 -07001448 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001449 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001450 final Phone phone = getPhone(subId);
1451 if (phone != null) {
1452 phone.setRadioPower(!isRadioOnForSubscriber(subId));
1453 }
Wink Saville36469e72014-06-11 15:17:00 -07001454 }
1455
1456 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001457 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001458 }
1459
Wink Savilleb564aae2014-10-23 10:18:09 -07001460 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07001461 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001462 final Phone phone = getPhone(subId);
1463 if (phone == null) {
1464 return false;
1465 }
1466 if ((phone.getServiceState().getState() !=
Wink Saville36469e72014-06-11 15:17:00 -07001467 ServiceState.STATE_POWER_OFF) != turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001468 toggleRadioOnOffForSubscriber(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001469 }
1470 return true;
1471 }
Wink Saville36469e72014-06-11 15:17:00 -07001472
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001473 public boolean needMobileRadioShutdown() {
1474 /*
1475 * If any of the Radios are available, it will need to be
1476 * shutdown. So return true if any Radio is available.
1477 */
1478 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1479 Phone phone = PhoneFactory.getPhone(i);
1480 if (phone != null && phone.isRadioAvailable()) return true;
1481 }
1482 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
1483 return false;
1484 }
1485
1486 public void shutdownMobileRadios() {
1487 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1488 logv("Shutting down Phone " + i);
1489 shutdownRadioUsingPhoneId(i);
1490 }
1491 }
1492
1493 private void shutdownRadioUsingPhoneId(int phoneId) {
1494 enforceModifyPermission();
1495 Phone phone = PhoneFactory.getPhone(phoneId);
1496 if (phone != null && phone.isRadioAvailable()) {
1497 phone.shutdownRadio();
1498 }
1499 }
1500
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001501 public boolean setRadioPower(boolean turnOn) {
Jack Yub4e16162017-05-15 12:48:40 -07001502 enforceModifyPermission();
Wei Liu9ae2a062016-08-08 11:09:34 -07001503 final Phone defaultPhone = PhoneFactory.getDefaultPhone();
1504 if (defaultPhone != null) {
1505 defaultPhone.setRadioPower(turnOn);
1506 return true;
1507 } else {
1508 loge("There's no default phone.");
1509 return false;
1510 }
Wink Saville36469e72014-06-11 15:17:00 -07001511 }
1512
Wink Savilleb564aae2014-10-23 10:18:09 -07001513 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001514 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001515 final Phone phone = getPhone(subId);
1516 if (phone != null) {
1517 phone.setRadioPower(turnOn);
1518 return true;
1519 } else {
1520 return false;
1521 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001522 }
1523
Wink Saville36469e72014-06-11 15:17:00 -07001524 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001525 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001526 public boolean enableDataConnectivity() {
1527 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001528 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001529 final Phone phone = getPhone(subId);
1530 if (phone != null) {
1531 phone.setDataEnabled(true);
1532 return true;
1533 } else {
1534 return false;
1535 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001536 }
1537
Wink Saville36469e72014-06-11 15:17:00 -07001538 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001539 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001540 public boolean disableDataConnectivity() {
1541 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001542 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001543 final Phone phone = getPhone(subId);
1544 if (phone != null) {
1545 phone.setDataEnabled(false);
1546 return true;
1547 } else {
1548 return false;
1549 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001550 }
1551
Sanket Padawe356d7632015-06-22 14:03:32 -07001552 @Override
Jack Yuacf8a132017-05-01 17:00:48 -07001553 public boolean isDataConnectivityPossible(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001554 final Phone phone = getPhone(subId);
1555 if (phone != null) {
Jack Yuacf8a132017-05-01 17:00:48 -07001556 return phone.isDataAllowed();
Sanket Padawe356d7632015-06-22 14:03:32 -07001557 } else {
1558 return false;
1559 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001560 }
1561
1562 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001563 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07001564 }
1565
pkanwarae03a6b2016-11-06 20:37:09 -08001566 public void handleUssdRequest(int subId, String ussdRequest, ResultReceiver wrappedCallback) {
pkanwar32d516d2016-10-14 19:37:38 -07001567 enforceCallPermission();
pkanwar32d516d2016-10-14 19:37:38 -07001568 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1569 return;
1570 }
1571 Pair<String, ResultReceiver> ussdObject = new Pair(ussdRequest, wrappedCallback);
1572 sendRequest(CMD_HANDLE_USSD_REQUEST, ussdObject, subId);
1573 };
1574
Wink Savilleb564aae2014-10-23 10:18:09 -07001575 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001576 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001577 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1578 return false;
1579 }
Wink Saville36469e72014-06-11 15:17:00 -07001580 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001581 }
1582
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001583 public int getCallState() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001584 return getCallStateForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001585 }
1586
Sanket Padawe13bac7b2017-03-20 15:04:47 -07001587 public int getCallStateForSlot(int slotIndex) {
1588 Phone phone = PhoneFactory.getPhone(slotIndex);
Shishir Agrawala9f32182016-04-12 12:00:16 -07001589 return phone == null ? TelephonyManager.CALL_STATE_IDLE :
Nathan Harolda667c152016-12-14 11:27:20 -08001590 PhoneConstantConversions.convertCallState(phone.getState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001591 }
1592
Sanket Padawe356d7632015-06-22 14:03:32 -07001593 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001594 public int getDataState() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001595 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001596 if (phone != null) {
Nathan Harolda667c152016-12-14 11:27:20 -08001597 return PhoneConstantConversions.convertDataState(phone.getDataConnectionState());
Sanket Padawe356d7632015-06-22 14:03:32 -07001598 } else {
Nathan Harolda667c152016-12-14 11:27:20 -08001599 return PhoneConstantConversions.convertDataState(PhoneConstants.DataState.DISCONNECTED);
Sanket Padawe356d7632015-06-22 14:03:32 -07001600 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001601 }
1602
Sanket Padawe356d7632015-06-22 14:03:32 -07001603 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001604 public int getDataActivity() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001605 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001606 if (phone != null) {
1607 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
1608 } else {
1609 return TelephonyManager.DATA_ACTIVITY_NONE;
1610 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001611 }
1612
1613 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001614 public Bundle getCellLocation(String callingPackage) {
1615 enforceFineOrCoarseLocationPermission("getCellLocation");
1616
1617 // OP_COARSE_LOCATION controls both fine and coarse location.
1618 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1619 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001620 log("getCellLocation: returning null; mode != allowed");
Svetoslav64fad262015-04-14 14:35:21 -07001621 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001622 }
1623
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001624 if (checkIfCallerIsSelfOrForegroundUser() ||
1625 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001626 if (DBG_LOC) log("getCellLocation: is active user");
1627 Bundle data = new Bundle();
Legler Wu2c01cdf2014-12-08 19:00:59 +08001628 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001629 if (phone == null) {
1630 return null;
1631 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001632
1633 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
1634 phone.getCellLocation(workSource).fillInNotifierBundle(data);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001635 return data;
1636 } else {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001637 log("getCellLocation: suppress non-active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001638 return null;
1639 }
1640 }
1641
Svetoslav64fad262015-04-14 14:35:21 -07001642 private void enforceFineOrCoarseLocationPermission(String message) {
1643 try {
1644 mApp.enforceCallingOrSelfPermission(
1645 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1646 } catch (SecurityException e) {
1647 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1648 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1649 // is the weaker precondition
1650 mApp.enforceCallingOrSelfPermission(
1651 android.Manifest.permission.ACCESS_COARSE_LOCATION, message);
1652 }
1653 }
1654
1655
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001656 @Override
1657 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001658 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001659 }
1660
Sanket Padawe356d7632015-06-22 14:03:32 -07001661 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001662 public void enableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001663 mApp.enforceCallingOrSelfPermission(
1664 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001665 final Phone phone = getPhone(subId);
1666 if (phone != null) {
1667 phone.enableLocationUpdates();
1668 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001669 }
1670
1671 @Override
1672 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001673 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001674 }
1675
Sanket Padawe356d7632015-06-22 14:03:32 -07001676 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001677 public void disableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001678 mApp.enforceCallingOrSelfPermission(
1679 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001680 final Phone phone = getPhone(subId);
1681 if (phone != null) {
1682 phone.disableLocationUpdates();
1683 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001684 }
1685
1686 @Override
1687 @SuppressWarnings("unchecked")
1688 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
Svetoslav64fad262015-04-14 14:35:21 -07001689 enforceFineOrCoarseLocationPermission("getNeighboringCellInfo");
1690
1691 // OP_COARSE_LOCATION controls both fine and coarse location.
1692 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1693 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1694 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001695 }
1696
1697 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
1698 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1699 return null;
1700 }
Svetoslav64fad262015-04-14 14:35:21 -07001701
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001702 if (checkIfCallerIsSelfOrForegroundUser() ||
1703 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001704 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
1705
1706 ArrayList<NeighboringCellInfo> cells = null;
1707
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001708 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001709 try {
1710 cells = (ArrayList<NeighboringCellInfo>) sendRequest(
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001711 CMD_HANDLE_NEIGHBORING_CELL, workSource,
Sanket Padawe56e75a32016-02-08 12:18:19 -08001712 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001713 } catch (RuntimeException e) {
Wink Saville36469e72014-06-11 15:17:00 -07001714 Log.e(LOG_TAG, "getNeighboringCellInfo " + e);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001715 }
1716 return cells;
1717 } else {
1718 if (DBG_LOC) log("getNeighboringCellInfo: suppress non-active user");
1719 return null;
1720 }
1721 }
1722
1723
1724 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001725 public List<CellInfo> getAllCellInfo(String callingPackage) {
1726 enforceFineOrCoarseLocationPermission("getAllCellInfo");
1727
1728 // OP_COARSE_LOCATION controls both fine and coarse location.
1729 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1730 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1731 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001732 }
1733
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001734 if (checkIfCallerIsSelfOrForegroundUser() ||
1735 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001736 if (DBG_LOC) log("getAllCellInfo: is active user");
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001737 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
Legler Wu2c01cdf2014-12-08 19:00:59 +08001738 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
1739 for (Phone phone : PhoneFactory.getPhones()) {
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001740 final List<CellInfo> info = phone.getAllCellInfo(workSource);
1741 if (info != null) cellInfos.addAll(info);
Legler Wu2c01cdf2014-12-08 19:00:59 +08001742 }
1743 return cellInfos;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001744 } else {
1745 if (DBG_LOC) log("getAllCellInfo: suppress non-active user");
1746 return null;
1747 }
1748 }
1749
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001750 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001751 public void setCellInfoListRate(int rateInMillis) {
Jack Yua8d8cb82017-01-16 10:15:34 -08001752 enforceModifyPermission();
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001753 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
1754 mPhone.setCellInfoListRate(rateInMillis, workSource);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001755 }
1756
Shishir Agrawala9f32182016-04-12 12:00:16 -07001757 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07001758 public String getImeiForSlot(int slotIndex, String callingPackage) {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001759 if (!canReadPhoneState(callingPackage, "getImeiForSlot")) {
1760 return null;
1761 }
Sanket Padawe13bac7b2017-03-20 15:04:47 -07001762 Phone phone = PhoneFactory.getPhone(slotIndex);
Shishir Agrawala9f32182016-04-12 12:00:16 -07001763 return phone == null ? null : phone.getImei();
1764 }
1765
1766 @Override
Jack Yu2af8d712017-03-15 17:14:14 -07001767 public String getMeidForSlot(int slotIndex, String callingPackage) {
1768 if (!canReadPhoneState(callingPackage, "getMeidForSlot")) {
1769 return null;
1770 }
1771 Phone phone = PhoneFactory.getPhone(slotIndex);
1772 return phone == null ? null : phone.getMeid();
1773 }
1774
1775 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07001776 public String getDeviceSoftwareVersionForSlot(int slotIndex, String callingPackage) {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001777 if (!canReadPhoneState(callingPackage, "getDeviceSoftwareVersionForSlot")) {
1778 return null;
1779 }
Sanket Padawe13bac7b2017-03-20 15:04:47 -07001780 Phone phone = PhoneFactory.getPhone(slotIndex);
Shishir Agrawala9f32182016-04-12 12:00:16 -07001781 return phone == null ? null : phone.getDeviceSvn();
1782 }
1783
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001784 //
1785 // Internal helper methods.
1786 //
1787
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001788 /**
1789 * Returns true if the caller holds INTERACT_ACROSS_USERS_FULL.
1790 */
1791 private boolean checkCallerInteractAcrossUsersFull() {
1792 return mPhone.getContext().checkCallingOrSelfPermission(
1793 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1794 == PackageManager.PERMISSION_GRANTED;
1795 }
1796
Jake Hambye994d462014-02-03 13:10:13 -08001797 private static boolean checkIfCallerIsSelfOrForegroundUser() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001798 boolean ok;
1799
1800 boolean self = Binder.getCallingUid() == Process.myUid();
1801 if (!self) {
1802 // Get the caller's user id then clear the calling identity
1803 // which will be restored in the finally clause.
1804 int callingUser = UserHandle.getCallingUserId();
1805 long ident = Binder.clearCallingIdentity();
1806
1807 try {
1808 // With calling identity cleared the current user is the foreground user.
1809 int foregroundUser = ActivityManager.getCurrentUser();
1810 ok = (foregroundUser == callingUser);
1811 if (DBG_LOC) {
1812 log("checkIfCallerIsSelfOrForegoundUser: foregroundUser=" + foregroundUser
1813 + " callingUser=" + callingUser + " ok=" + ok);
1814 }
1815 } catch (Exception ex) {
1816 if (DBG_LOC) loge("checkIfCallerIsSelfOrForegoundUser: Exception ex=" + ex);
1817 ok = false;
1818 } finally {
1819 Binder.restoreCallingIdentity(ident);
1820 }
1821 } else {
1822 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: is self");
1823 ok = true;
1824 }
1825 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: ret=" + ok);
1826 return ok;
1827 }
1828
1829 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001830 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1831 *
1832 * @throws SecurityException if the caller does not have the required permission
1833 */
1834 private void enforceModifyPermission() {
1835 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1836 }
1837
1838 /**
Junda Liua2e36012014-07-09 18:30:01 -07001839 * Make sure either system app or the caller has carrier privilege.
1840 *
1841 * @throws SecurityException if the caller does not have the required permission/privilege
1842 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001843 private void enforceModifyPermissionOrCarrierPrivilege(int subId) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001844 int permission = mApp.checkCallingOrSelfPermission(
1845 android.Manifest.permission.MODIFY_PHONE_STATE);
1846 if (permission == PackageManager.PERMISSION_GRANTED) {
1847 return;
1848 }
1849
1850 log("No modify permission, check carrier privilege next.");
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001851 enforceCarrierPrivilege(subId);
Junda Liua2e36012014-07-09 18:30:01 -07001852 }
1853
1854 /**
1855 * Make sure the caller has carrier privilege.
1856 *
1857 * @throws SecurityException if the caller does not have the required permission
1858 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001859 private void enforceCarrierPrivilege(int subId) {
1860 if (getCarrierPrivilegeStatus(subId) !=
1861 TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001862 loge("No Carrier Privilege.");
1863 throw new SecurityException("No Carrier Privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001864 }
1865 }
1866
1867 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001868 * Make sure the caller has the CALL_PHONE permission.
1869 *
1870 * @throws SecurityException if the caller does not have the required permission
1871 */
1872 private void enforceCallPermission() {
1873 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1874 }
1875
Stuart Scott8eef64f2015-04-08 15:13:54 -07001876 private void enforceConnectivityInternalPermission() {
1877 mApp.enforceCallingOrSelfPermission(
1878 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1879 "ConnectivityService");
1880 }
1881
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001882 private String createTelUrl(String number) {
1883 if (TextUtils.isEmpty(number)) {
1884 return null;
1885 }
1886
Jake Hambye994d462014-02-03 13:10:13 -08001887 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001888 }
1889
Ihab Awadf9e92732013-12-05 18:02:52 -08001890 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001891 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1892 }
1893
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001894 private static void logv(String msg) {
1895 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
1896 }
1897
Ihab Awadf9e92732013-12-05 18:02:52 -08001898 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001899 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1900 }
1901
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001902 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001903 public int getActivePhoneType() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001904 return getActivePhoneTypeForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001905 }
1906
Sanket Padawe356d7632015-06-22 14:03:32 -07001907 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07001908 public int getActivePhoneTypeForSlot(int slotIndex) {
1909 final Phone phone = PhoneFactory.getPhone(slotIndex);
Sanket Padawe356d7632015-06-22 14:03:32 -07001910 if (phone == null) {
1911 return PhoneConstants.PHONE_TYPE_NONE;
1912 } else {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001913 return phone.getPhoneType();
Sanket Padawe356d7632015-06-22 14:03:32 -07001914 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001915 }
1916
1917 /**
1918 * Returns the CDMA ERI icon index to display
1919 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001920 @Override
1921 public int getCdmaEriIconIndex(String callingPackage) {
1922 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001923 }
1924
Sanket Padawe356d7632015-06-22 14:03:32 -07001925 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001926 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage) {
1927 if (!canReadPhoneState(callingPackage, "getCdmaEriIconIndexForSubscriber")) {
1928 return -1;
1929 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001930 final Phone phone = getPhone(subId);
1931 if (phone != null) {
1932 return phone.getCdmaEriIconIndex();
1933 } else {
1934 return -1;
1935 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001936 }
1937
1938 /**
1939 * Returns the CDMA ERI icon mode,
1940 * 0 - ON
1941 * 1 - FLASHING
1942 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001943 @Override
1944 public int getCdmaEriIconMode(String callingPackage) {
1945 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001946 }
1947
Sanket Padawe356d7632015-06-22 14:03:32 -07001948 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001949 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage) {
1950 if (!canReadPhoneState(callingPackage, "getCdmaEriIconModeForSubscriber")) {
1951 return -1;
1952 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001953 final Phone phone = getPhone(subId);
1954 if (phone != null) {
1955 return phone.getCdmaEriIconMode();
1956 } else {
1957 return -1;
1958 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001959 }
1960
1961 /**
1962 * Returns the CDMA ERI text,
1963 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001964 @Override
1965 public String getCdmaEriText(String callingPackage) {
1966 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001967 }
1968
Sanket Padawe356d7632015-06-22 14:03:32 -07001969 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001970 public String getCdmaEriTextForSubscriber(int subId, String callingPackage) {
1971 if (!canReadPhoneState(callingPackage, "getCdmaEriIconTextForSubscriber")) {
1972 return null;
1973 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001974 final Phone phone = getPhone(subId);
1975 if (phone != null) {
1976 return phone.getCdmaEriText();
1977 } else {
1978 return null;
1979 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001980 }
1981
1982 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07001983 * Returns the CDMA MDN.
1984 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001985 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001986 public String getCdmaMdn(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001987 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001988 final Phone phone = getPhone(subId);
1989 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA && phone != null) {
1990 return phone.getLine1Number();
Junda Liuca05d5d2014-08-14 22:36:34 -07001991 } else {
1992 return null;
1993 }
1994 }
1995
1996 /**
1997 * Returns the CDMA MIN.
1998 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001999 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002000 public String getCdmaMin(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002001 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07002002 final Phone phone = getPhone(subId);
2003 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
2004 return phone.getCdmaMin();
Junda Liuca05d5d2014-08-14 22:36:34 -07002005 } else {
2006 return null;
2007 }
2008 }
2009
2010 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002011 * Returns true if CDMA provisioning needs to run.
2012 */
2013 public boolean needsOtaServiceProvisioning() {
2014 return mPhone.needsOtaServiceProvisioning();
2015 }
2016
2017 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002018 * Sets the voice mail number of a given subId.
2019 */
2020 @Override
2021 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002022 enforceCarrierPrivilege(subId);
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002023 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
2024 new Pair<String, String>(alphaTag, number), new Integer(subId));
2025 return success;
2026 }
2027
Ta-wei Yen87c49842016-05-13 21:19:52 -07002028 @Override
Ta-wei Yenc9df0432017-04-17 17:09:07 -07002029 public Bundle getVisualVoicemailSettings(String callingPackage, int subId) {
2030 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
2031 String systemDialer = TelecomManager.from(mPhone.getContext()).getSystemDialerPackage();
2032 if (!TextUtils.equals(callingPackage, systemDialer)) {
2033 throw new SecurityException("caller must be system dialer");
2034 }
2035 PhoneAccountHandle phoneAccountHandle = PhoneAccountHandleConverter.fromSubId(subId);
2036 if (phoneAccountHandle == null){
2037 return null;
2038 }
2039 return VisualVoicemailSettingsUtil.dump(mPhone.getContext(), phoneAccountHandle);
2040 }
2041
2042 @Override
Ta-wei Yen409ac562017-03-06 16:00:44 -08002043 public String getVisualVoicemailPackageName(String callingPackage, int subId) {
Ta-wei Yendca928f2017-01-10 16:17:08 -08002044 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
2045 if (!canReadPhoneState(callingPackage, "getVisualVoicemailPackageName")) {
2046 return null;
2047 }
Ta-wei Yendca928f2017-01-10 16:17:08 -08002048 return RemoteVvmTaskManager.getRemotePackage(mPhone.getContext(), subId).getPackageName();
2049 }
2050
2051 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002052 public void enableVisualVoicemailSmsFilter(String callingPackage, int subId,
2053 VisualVoicemailSmsFilterSettings settings) {
2054 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002055 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002056 .enableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId,
2057 settings);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002058 }
2059
2060 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002061 public void disableVisualVoicemailSmsFilter(String callingPackage, int subId) {
2062 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002063 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002064 .disableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002065 }
2066
2067 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002068 public VisualVoicemailSmsFilterSettings getVisualVoicemailSmsFilterSettings(
2069 String callingPackage, int subId) {
2070 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002071 return VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002072 .getVisualVoicemailSmsFilterSettings(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002073 }
2074
2075 @Override
Ta-wei Yen30a69c82016-12-27 14:52:32 -08002076 public VisualVoicemailSmsFilterSettings getActiveVisualVoicemailSmsFilterSettings(int subId) {
Ta-wei Yenb6929602016-05-24 15:48:27 -07002077 enforceReadPrivilegedPermission();
Ta-wei Yen87c49842016-05-13 21:19:52 -07002078 return VisualVoicemailSmsFilterConfig
Ta-wei Yen30a69c82016-12-27 14:52:32 -08002079 .getActiveVisualVoicemailSmsFilterSettings(mPhone.getContext(), subId);
2080 }
2081
2082 @Override
2083 public void sendVisualVoicemailSmsForSubscriber(String callingPackage, int subId,
2084 String number, int port, String text, PendingIntent sentIntent) {
2085 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen527a9c02017-01-06 15:29:25 -08002086 enforceVisualVoicemailPackage(callingPackage, subId);
Ta-wei Yen30a69c82016-12-27 14:52:32 -08002087 enforceSendSmsPermission();
2088 // Make the calls as the phone process.
2089 final long identity = Binder.clearCallingIdentity();
2090 try {
2091 SmsManager smsManager = SmsManager.getSmsManagerForSubscriptionId(subId);
2092 if (port == 0) {
2093 smsManager.sendTextMessageWithSelfPermissions(number, null, text,
2094 sentIntent, null, false);
2095 } else {
2096 byte[] data = text.getBytes(StandardCharsets.UTF_8);
2097 smsManager.sendDataMessageWithSelfPermissions(number, null,
2098 (short) port, data, sentIntent, null);
2099 }
2100 } finally {
2101 Binder.restoreCallingIdentity(identity);
2102 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07002103 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002104 /**
fionaxu0152e512016-11-14 13:36:14 -08002105 * Sets the voice activation state of a given subId.
2106 */
2107 @Override
2108 public void setVoiceActivationState(int subId, int activationState) {
2109 enforceModifyPermissionOrCarrierPrivilege(subId);
2110 final Phone phone = getPhone(subId);
2111 if (phone != null) {
2112 phone.setVoiceActivationState(activationState);
2113 } else {
2114 loge("setVoiceActivationState fails with invalid subId: " + subId);
2115 }
2116 }
2117
2118 /**
2119 * Sets the data activation state of a given subId.
2120 */
2121 @Override
2122 public void setDataActivationState(int subId, int activationState) {
2123 enforceModifyPermissionOrCarrierPrivilege(subId);
2124 final Phone phone = getPhone(subId);
2125 if (phone != null) {
2126 phone.setDataActivationState(activationState);
2127 } else {
2128 loge("setVoiceActivationState fails with invalid subId: " + subId);
2129 }
2130 }
2131
2132 /**
2133 * Returns the voice activation state of a given subId.
2134 */
2135 @Override
2136 public int getVoiceActivationState(int subId, String callingPackage) {
2137 if (!canReadPhoneState(callingPackage, "getVoiceActivationStateForSubscriber")) {
2138 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2139 }
2140 final Phone phone = getPhone(subId);
2141 if (phone != null) {
2142 return phone.getVoiceActivationState();
2143 } else {
2144 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2145 }
2146 }
2147
2148 /**
2149 * Returns the data activation state of a given subId.
2150 */
2151 @Override
2152 public int getDataActivationState(int subId, String callingPackage) {
2153 if (!canReadPhoneState(callingPackage, "getDataActivationStateForSubscriber")) {
2154 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2155 }
2156 final Phone phone = getPhone(subId);
2157 if (phone != null) {
2158 return phone.getDataActivationState();
2159 } else {
2160 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2161 }
2162 }
2163
2164 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002165 * Returns the unread count of voicemails
2166 */
2167 public int getVoiceMessageCount() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002168 return getVoiceMessageCountForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07002169 }
2170
2171 /**
2172 * Returns the unread count of voicemails for a subId
2173 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002174 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002175 public int getVoiceMessageCountForSubscriber( int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002176 final Phone phone = getPhone(subId);
2177 if (phone != null) {
2178 return phone.getVoiceMessageCount();
2179 } else {
2180 return 0;
2181 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002182 }
2183
2184 /**
pkanwar8a4dcfb2017-01-19 13:43:16 -08002185 * returns true, if the device is in a state where both voice and data
2186 * are supported simultaneously. This can change based on location or network condition.
2187 */
2188 @Override
2189 public boolean isConcurrentVoiceAndDataAllowed(int subId) {
2190 final Phone phone = getPhone(subId);
2191 return (phone == null ? false : phone.isConcurrentVoiceAndDataAllowed());
2192 }
2193
2194 /**
fionaxu235cc5e2017-03-06 22:25:57 -08002195 * Send the dialer code if called from the current default dialer or the caller has
2196 * carrier privilege.
2197 * @param inputCode The dialer code to send
2198 */
2199 @Override
2200 public void sendDialerSpecialCode(String callingPackage, String inputCode) {
2201 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
2202 String defaultDialer = TelecomManager.from(mPhone.getContext()).getDefaultDialerPackage();
2203 if (!TextUtils.equals(callingPackage, defaultDialer)) {
2204 enforceCarrierPrivilege(getDefaultSubscription());
2205 }
2206 mPhone.sendDialerSpecialCode(inputCode);
2207 }
2208
2209 /**
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002210 * Returns the data network type.
2211 * Legacy call, permission-free.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002212 *
2213 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
2214 */
2215 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002216 public int getNetworkType() {
2217 final Phone phone = getPhone(getDefaultSubscription());
2218 if (phone != null) {
2219 return phone.getServiceState().getDataNetworkType();
2220 } else {
2221 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2222 }
Wink Saville36469e72014-06-11 15:17:00 -07002223 }
2224
2225 /**
2226 * Returns the network type for a subId
2227 */
2228 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002229 public int getNetworkTypeForSubscriber(int subId, String callingPackage) {
2230 if (!canReadPhoneState(callingPackage, "getNetworkTypeForSubscriber")) {
2231 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2232 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002233
Sanket Padawe356d7632015-06-22 14:03:32 -07002234 final Phone phone = getPhone(subId);
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002235 if (phone != null) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002236 return phone.getServiceState().getDataNetworkType();
2237 } else {
2238 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2239 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002240 }
2241
2242 /**
2243 * Returns the data network type
2244 */
2245 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002246 public int getDataNetworkType(String callingPackage) {
2247 return getDataNetworkTypeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002248 }
2249
2250 /**
2251 * Returns the data network type for a subId
2252 */
2253 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002254 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage) {
2255 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2256 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2257 }
2258
Sanket Padawe356d7632015-06-22 14:03:32 -07002259 final Phone phone = getPhone(subId);
2260 if (phone != null) {
2261 return phone.getServiceState().getDataNetworkType();
2262 } else {
2263 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2264 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002265 }
2266
2267 /**
Wink Saville36469e72014-06-11 15:17:00 -07002268 * Returns the Voice network type for a subId
2269 */
2270 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002271 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage) {
2272 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2273 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2274 }
2275
Sanket Padawe356d7632015-06-22 14:03:32 -07002276 final Phone phone = getPhone(subId);
2277 if (phone != null) {
2278 return phone.getServiceState().getVoiceNetworkType();
2279 } else {
2280 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2281 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002282 }
2283
2284 /**
2285 * @return true if a ICC card is present
2286 */
2287 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07002288 // FIXME Make changes to pass defaultSimId of type int
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002289 return hasIccCardUsingSlotIndex(mSubscriptionController.getSlotIndex(
2290 getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07002291 }
2292
2293 /**
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002294 * @return true if a ICC card is present for a slotIndex
Wink Saville36469e72014-06-11 15:17:00 -07002295 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002296 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002297 public boolean hasIccCardUsingSlotIndex(int slotIndex) {
2298 final Phone phone = PhoneFactory.getPhone(slotIndex);
fionaxu6e6c68b2016-06-23 13:31:32 -07002299 if (phone != null) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002300 return phone.getIccCard().hasIccCard();
Amit Mahajana6fc2a82015-01-06 11:53:51 -08002301 } else {
2302 return false;
2303 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002304 }
2305
2306 /**
2307 * Return if the current radio is LTE on CDMA. This
2308 * is a tri-state return value as for a period of time
2309 * the mode may be unknown.
2310 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002311 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002312 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08002313 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002314 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002315 @Override
2316 public int getLteOnCdmaMode(String callingPackage) {
2317 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002318 }
2319
Sanket Padawe356d7632015-06-22 14:03:32 -07002320 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002321 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage) {
2322 if (!canReadPhoneState(callingPackage, "getLteOnCdmaModeForSubscriber")) {
2323 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2324 }
2325
Sanket Padawe356d7632015-06-22 14:03:32 -07002326 final Phone phone = getPhone(subId);
2327 if (phone == null) {
2328 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2329 } else {
2330 return phone.getLteOnCdmaMode();
2331 }
Wink Saville36469e72014-06-11 15:17:00 -07002332 }
2333
2334 public void setPhone(Phone phone) {
2335 mPhone = phone;
2336 }
2337
2338 /**
2339 * {@hide}
2340 * Returns Default subId, 0 in the case of single standby.
2341 */
Wink Savilleb564aae2014-10-23 10:18:09 -07002342 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002343 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07002344 }
2345
Shishir Agrawala9f32182016-04-12 12:00:16 -07002346 private int getSlotForDefaultSubscription() {
2347 return mSubscriptionController.getPhoneId(getDefaultSubscription());
2348 }
2349
Wink Savilleb564aae2014-10-23 10:18:09 -07002350 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002351 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002352 }
Ihab Awadf2177b72013-11-25 13:33:23 -08002353
2354 /**
2355 * @see android.telephony.TelephonyManager.WifiCallingChoices
2356 */
2357 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002358 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
2359 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08002360 }
2361
2362 /**
2363 * @see android.telephony.TelephonyManager.WifiCallingChoices
2364 */
2365 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002366 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
2367 Settings.System.putInt(mPhone.getContext().getContentResolver(),
2368 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08002369 }
2370
Sailesh Nepald1e68152013-12-12 19:08:02 -08002371 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07002372 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08002373 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08002374 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08002375
Shishir Agrawal566b7612013-10-28 14:41:00 -07002376 @Override
Ajay Nambid7454d32015-12-03 13:50:00 -08002377 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(int subId, String AID, int p2) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002378 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002379
Ajay Nambid7454d32015-12-03 13:50:00 -08002380 if (DBG) log("iccOpenLogicalChannel: subId=" + subId + " aid=" + AID + " p2=" + p2);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002381 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
Ajay Nambid7454d32015-12-03 13:50:00 -08002382 CMD_OPEN_CHANNEL, new Pair<String, Integer>(AID, p2), subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002383 if (DBG) log("iccOpenLogicalChannel: " + response);
2384 return response;
Shishir Agrawal566b7612013-10-28 14:41:00 -07002385 }
2386
2387 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002388 public boolean iccCloseLogicalChannel(int subId, int channel) {
2389 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002390
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002391 if (DBG) log("iccCloseLogicalChannel: subId=" + subId + " chnl=" + channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002392 if (channel < 0) {
2393 return false;
2394 }
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002395 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel, subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002396 if (DBG) log("iccCloseLogicalChannel: " + success);
2397 return success;
2398 }
2399
2400 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002401 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07002402 int command, int p1, int p2, int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002403 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002404
2405 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002406 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel +
2407 " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
Shishir Agrawal566b7612013-10-28 14:41:00 -07002408 " data=" + data);
2409 }
2410
2411 if (channel < 0) {
2412 return "";
2413 }
2414
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002415 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002416 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002417 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
2418
Shishir Agrawal566b7612013-10-28 14:41:00 -07002419 // Append the returned status code to the end of the response payload.
2420 String s = Integer.toHexString(
2421 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002422 if (response.payload != null) {
2423 s = IccUtils.bytesToHexString(response.payload) + s;
2424 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07002425 return s;
2426 }
Jake Hambye994d462014-02-03 13:10:13 -08002427
Evan Charltonc66da362014-05-16 14:06:40 -07002428 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002429 public String iccTransmitApduBasicChannel(int subId, int cla, int command, int p1, int p2,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002430 int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002431 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002432
2433 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002434 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd=" + command
2435 + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002436 }
2437
2438 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002439 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002440 if (DBG) log("iccTransmitApduBasicChannel: " + response);
2441
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002442 // Append the returned status code to the end of the response payload.
2443 String s = Integer.toHexString(
2444 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002445 if (response.payload != null) {
2446 s = IccUtils.bytesToHexString(response.payload) + s;
2447 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002448 return s;
2449 }
2450
2451 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002452 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002453 String filePath) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002454 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002455
2456 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002457 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " " +
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002458 p1 + " " + p2 + " " + p3 + ":" + filePath);
2459 }
2460
2461 IccIoResult response =
2462 (IccIoResult)sendRequest(CMD_EXCHANGE_SIM_IO,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002463 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
2464 subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002465
2466 if (DBG) {
2467 log("Exchange SIM_IO [R]" + response);
2468 }
2469
2470 byte[] result = null;
2471 int length = 2;
2472 if (response.payload != null) {
2473 length = 2 + response.payload.length;
2474 result = new byte[length];
2475 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
2476 } else {
2477 result = new byte[length];
2478 }
2479
2480 result[length - 1] = (byte) response.sw2;
2481 result[length - 2] = (byte) response.sw1;
2482 return result;
2483 }
2484
Nathan Haroldb3014052017-01-25 15:57:32 -08002485 /**
2486 * Get the forbidden PLMN List from the given app type (ex APPTYPE_USIM)
2487 * on a particular subscription
2488 */
2489 public String[] getForbiddenPlmns(int subId, int appType) {
Nathan Harold892104e2017-04-13 18:19:05 -07002490 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
2491 "Requires READ_PHONE_STATE");
Nathan Haroldb3014052017-01-25 15:57:32 -08002492 if (appType != TelephonyManager.APPTYPE_USIM && appType != TelephonyManager.APPTYPE_SIM) {
2493 loge("getForbiddenPlmnList(): App Type must be USIM or SIM");
2494 return null;
2495 }
2496 Object response = sendRequest(
2497 CMD_GET_FORBIDDEN_PLMNS, new Integer(appType), subId);
2498 if (response instanceof String[]) {
2499 return (String[]) response;
2500 }
2501 // Response is an Exception of some kind, which is signalled to the user as a NULL retval
2502 return null;
2503 }
2504
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002505 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002506 public String sendEnvelopeWithStatus(int subId, String content) {
2507 enforceModifyPermissionOrCarrierPrivilege(subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002508
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002509 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content, subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002510 if (response.payload == null) {
2511 return "";
2512 }
2513
2514 // Append the returned status code to the end of the response payload.
2515 String s = Integer.toHexString(
2516 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
2517 s = IccUtils.bytesToHexString(response.payload) + s;
2518 return s;
2519 }
2520
Jake Hambye994d462014-02-03 13:10:13 -08002521 /**
2522 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2523 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2524 *
2525 * @param itemID the ID of the item to read
2526 * @return the NV item as a String, or null on error.
2527 */
2528 @Override
2529 public String nvReadItem(int itemID) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002530 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002531 if (DBG) log("nvReadItem: item " + itemID);
2532 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
2533 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
2534 return value;
2535 }
2536
2537 /**
2538 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2539 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2540 *
2541 * @param itemID the ID of the item to read
2542 * @param itemValue the value to write, as a String
2543 * @return true on success; false on any failure
2544 */
2545 @Override
2546 public boolean nvWriteItem(int itemID, String itemValue) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002547 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002548 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
2549 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
2550 new Pair<Integer, String>(itemID, itemValue));
2551 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
2552 return success;
2553 }
2554
2555 /**
2556 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
2557 * Used for device configuration by some CDMA operators.
2558 *
2559 * @param preferredRoamingList byte array containing the new PRL
2560 * @return true on success; false on any failure
2561 */
2562 @Override
2563 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002564 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002565 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
2566 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
2567 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
2568 return success;
2569 }
2570
2571 /**
2572 * Perform the specified type of NV config reset.
2573 * Used for device configuration by some CDMA operators.
2574 *
2575 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
2576 * @return true on success; false on any failure
2577 */
2578 @Override
2579 public boolean nvResetConfig(int resetType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002580 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002581 if (DBG) log("nvResetConfig: type " + resetType);
2582 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
2583 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
2584 return success;
2585 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002586
2587 /**
Wink Saville36469e72014-06-11 15:17:00 -07002588 * {@hide}
2589 * Returns Default sim, 0 in the case of single standby.
2590 */
2591 public int getDefaultSim() {
2592 //TODO Need to get it from Telephony Devcontroller
2593 return 0;
2594 }
2595
Svet Ganovb320e182015-04-16 12:30:10 -07002596 public String[] getPcscfAddress(String apnType, String callingPackage) {
2597 if (!canReadPhoneState(callingPackage, "getPcscfAddress")) {
2598 return new String[0];
2599 }
2600
2601
ram87fca6f2014-07-18 18:58:44 +05302602 return mPhone.getPcscfAddress(apnType);
Wink Saville36469e72014-06-11 15:17:00 -07002603 }
2604
Brad Ebinger51f743a2017-01-23 13:50:20 -08002605 /**
2606 * Returns the {@link IImsServiceController} that corresponds to the given slot Id and IMS
2607 * feature or {@link null} if the service is not available. If an ImsServiceController is
2608 * available, the {@link IImsServiceFeatureListener} callback is registered as a listener for
2609 * feature updates.
2610 */
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002611 public IImsServiceController getImsServiceControllerAndListen(int slotIndex, int feature,
Brad Ebinger51f743a2017-01-23 13:50:20 -08002612 IImsServiceFeatureListener callback) {
2613 enforceModifyPermission();
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002614 return PhoneFactory.getImsResolver().getImsServiceControllerAndListen(slotIndex, feature,
Brad Ebinger51f743a2017-01-23 13:50:20 -08002615 callback);
2616 }
2617
Wink Saville36469e72014-06-11 15:17:00 -07002618 public void setImsRegistrationState(boolean registered) {
2619 enforceModifyPermission();
2620 mPhone.setImsRegistrationState(registered);
2621 }
2622
2623 /**
Stuart Scott54788802015-03-30 13:18:01 -07002624 * Set the network selection mode to automatic.
2625 *
2626 */
2627 @Override
2628 public void setNetworkSelectionModeAutomatic(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002629 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002630 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
2631 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
2632 }
2633
2634 /**
Shishir Agrawal302c8692015-06-19 13:49:39 -07002635 * Set the network selection mode to manual with the selected carrier.
2636 */
2637 @Override
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002638 public boolean setNetworkSelectionModeManual(int subId, OperatorInfo operator,
2639 boolean persistSelection) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002640 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002641 if (DBG) log("setNetworkSelectionModeManual: subId:" + subId + " operator:" + operator);
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002642 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operator,
2643 persistSelection);
2644 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002645 }
2646
2647 /**
2648 * Scans for available networks.
2649 */
2650 @Override
2651 public CellNetworkScanResult getCellNetworkScanResults(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002652 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002653 if (DBG) log("getCellNetworkScanResults: subId " + subId);
2654 CellNetworkScanResult result = (CellNetworkScanResult) sendRequest(
2655 CMD_PERFORM_NETWORK_SCAN, null, subId);
2656 return result;
2657 }
2658
2659 /**
yinxu504e1392017-04-12 16:03:22 -07002660 * Performs a new network scan and returns the id of this scan.
2661 *
2662 * @return the id of the requested scan which can be used to stop the scan.
2663 */
2664 @Override
2665 public int requestNetworkScan(int subId, NetworkScanRequest request, Messenger messenger,
2666 IBinder binder) {
2667 // TODO(yinxu): Implement this method.
2668 throw new UnsupportedOperationException("To be implemented...");
2669 }
2670
2671 /**
2672 * Stops an existing network scan with the given scanId.
2673 */
2674 @Override
2675 public void stopNetworkScan(int subId, int scanId) {
2676 // TODO(yinxu): Implement this method.
2677 throw new UnsupportedOperationException("To be implemented...");
2678 }
2679
2680 /**
Junda Liu84d15a22014-07-02 11:21:04 -07002681 * Get the calculated preferred network type.
2682 * Used for debugging incorrect network type.
2683 *
2684 * @return the preferred network type, defined in RILConstants.java.
2685 */
2686 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002687 public int getCalculatedPreferredNetworkType(String callingPackage) {
2688 if (!canReadPhoneState(callingPackage, "getCalculatedPreferredNetworkType")) {
2689 return RILConstants.PREFERRED_NETWORK_MODE;
2690 }
2691
Amit Mahajan43330e02014-11-18 11:54:45 -08002692 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext(), 0); // wink FIXME: need to get SubId from somewhere.
Junda Liu84d15a22014-07-02 11:21:04 -07002693 }
2694
2695 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08002696 * Get the preferred network type.
2697 * Used for device configuration by some CDMA operators.
2698 *
2699 * @return the preferred network type, defined in RILConstants.java.
2700 */
2701 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002702 public int getPreferredNetworkType(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002703 enforceModifyPermissionOrCarrierPrivilege(subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002704 if (DBG) log("getPreferredNetworkType");
Stuart Scott54788802015-03-30 13:18:01 -07002705 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002706 int networkType = (result != null ? result[0] : -1);
2707 if (DBG) log("getPreferredNetworkType: " + networkType);
2708 return networkType;
2709 }
2710
2711 /**
2712 * Set the preferred network type.
2713 * Used for device configuration by some CDMA operators.
2714 *
2715 * @param networkType the preferred network type, defined in RILConstants.java.
2716 * @return true on success; false on any failure.
2717 */
2718 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002719 public boolean setPreferredNetworkType(int subId, int networkType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002720 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002721 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
2722 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002723 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
Junda Liu80bc0d12014-07-14 16:36:44 -07002724 if (success) {
2725 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
Stuart Scott54788802015-03-30 13:18:01 -07002726 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
Junda Liu80bc0d12014-07-14 16:36:44 -07002727 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002728 return success;
2729 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002730
2731 /**
Junda Liu475951f2014-11-07 16:45:03 -08002732 * Check TETHER_DUN_REQUIRED and TETHER_DUN_APN settings, net.tethering.noprovisioning
2733 * SystemProperty, and config_tether_apndata to decide whether DUN APN is required for
2734 * tethering.
2735 *
2736 * @return 0: Not required. 1: required. 2: Not set.
2737 * @hide
2738 */
2739 @Override
2740 public int getTetherApnRequired() {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002741 enforceModifyPermission();
Junda Liu475951f2014-11-07 16:45:03 -08002742 int dunRequired = Settings.Global.getInt(mPhone.getContext().getContentResolver(),
2743 Settings.Global.TETHER_DUN_REQUIRED, 2);
2744 // If not set, check net.tethering.noprovisioning, TETHER_DUN_APN setting and
2745 // config_tether_apndata.
2746 if (dunRequired == 2 && mPhone.hasMatchedTetherApnSetting()) {
2747 dunRequired = 1;
2748 }
2749 return dunRequired;
2750 }
2751
2752 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07002753 * Set mobile data enabled
2754 * Used by the user through settings etc to turn on/off mobile data
2755 *
2756 * @param enable {@code true} turn turn data on, else {@code false}
2757 */
2758 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002759 public void setDataEnabled(int subId, boolean enable) {
Jeff Davidsona1920712016-11-18 17:05:56 -08002760 enforceModifyPermissionOrCarrierPrivilege(subId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002761 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002762 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002763 Phone phone = PhoneFactory.getPhone(phoneId);
2764 if (phone != null) {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002765 if (DBG) log("setDataEnabled: subId=" + subId + " enable=" + enable);
Wink Savillee7353bb2014-12-05 14:21:41 -08002766 phone.setDataEnabled(enable);
2767 } else {
2768 loge("setDataEnabled: no phone for subId=" + subId);
2769 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002770 }
2771
2772 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07002773 * Get whether mobile data is enabled.
2774 *
Jeff Davidsona1920712016-11-18 17:05:56 -08002775 * Accepts either ACCESS_NETWORK_STATE, MODIFY_PHONE_STATE or carrier privileges.
Robert Greenwalted86e582014-05-21 20:03:20 -07002776 *
2777 * @return {@code true} if data is enabled else {@code false}
2778 */
2779 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002780 public boolean getDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07002781 try {
2782 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
2783 null);
2784 } catch (Exception e) {
Jeff Davidsona1920712016-11-18 17:05:56 -08002785 enforceModifyPermissionOrCarrierPrivilege(subId);
Robert Greenwalt646120a2014-05-23 11:54:03 -07002786 }
Wink Savillee7353bb2014-12-05 14:21:41 -08002787 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002788 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002789 Phone phone = PhoneFactory.getPhone(phoneId);
2790 if (phone != null) {
2791 boolean retVal = phone.getDataEnabled();
Joe Onoratoa601dd22016-02-23 13:03:53 -08002792 if (DBG) log("getDataEnabled: subId=" + subId + " retVal=" + retVal);
Wink Savillee7353bb2014-12-05 14:21:41 -08002793 return retVal;
2794 } else {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002795 if (DBG) loge("getDataEnabled: no phone subId=" + subId + " retVal=false");
Wink Savillee7353bb2014-12-05 14:21:41 -08002796 return false;
2797 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002798 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002799
2800 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002801 public int getCarrierPrivilegeStatus(int subId) {
2802 final Phone phone = getPhone(subId);
2803 if (phone == null) {
2804 loge("getCarrierPrivilegeStatus: Invalid subId");
2805 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
2806 }
2807 UiccCard card = UiccController.getInstance().getUiccCard(phone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002808 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002809 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002810 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2811 }
2812 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002813 phone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002814 }
Junda Liu29340342014-07-10 15:23:27 -07002815
2816 @Override
Zach Johnson50ecba32015-05-19 00:24:21 -07002817 public int checkCarrierPrivilegesForPackage(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002818 if (TextUtils.isEmpty(pkgName))
2819 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Shishir Agrawal21409252015-01-15 23:33:50 -08002820 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002821 if (card == null) {
2822 loge("checkCarrierPrivilegesForPackage: No UICC");
2823 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2824 }
Zach Johnson50ecba32015-05-19 00:24:21 -07002825 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgName);
2826 }
2827
2828 @Override
2829 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002830 if (TextUtils.isEmpty(pkgName))
2831 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Zach Johnson50ecba32015-05-19 00:24:21 -07002832 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2833 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2834 UiccCard card = UiccController.getInstance().getUiccCard(i);
2835 if (card == null) {
Jonathan Basseri7d320df2015-06-16 12:17:08 -07002836 // No UICC in that slot.
Zach Johnson50ecba32015-05-19 00:24:21 -07002837 continue;
2838 }
2839
2840 result = card.getCarrierPrivilegeStatus(
2841 mPhone.getContext().getPackageManager(), pkgName);
2842 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2843 break;
2844 }
2845 }
2846
2847 return result;
Junda Liu29340342014-07-10 15:23:27 -07002848 }
Derek Tan89e89d42014-07-08 17:00:10 -07002849
2850 @Override
Junda Liue64de782015-04-16 17:19:16 -07002851 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
2852 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
2853 loge("phoneId " + phoneId + " is not valid.");
2854 return null;
2855 }
2856 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002857 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002858 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002859 return null ;
2860 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002861 return card.getCarrierPackageNamesForIntent(
Svetoslav483aff72015-04-21 14:16:07 -07002862 mPhone.getContext().getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002863 }
2864
Amith Yamasani6e118872016-02-19 12:53:51 -08002865 @Override
2866 public List<String> getPackagesWithCarrierPrivileges() {
2867 PackageManager pm = mPhone.getContext().getPackageManager();
2868 List<String> privilegedPackages = new ArrayList<>();
2869 List<PackageInfo> packages = null;
2870 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2871 UiccCard card = UiccController.getInstance().getUiccCard(i);
2872 if (card == null) {
2873 // No UICC in that slot.
2874 continue;
2875 }
2876 if (card.hasCarrierPrivilegeRules()) {
2877 if (packages == null) {
2878 // Only check packages in user 0 for now
2879 packages = pm.getInstalledPackagesAsUser(
2880 PackageManager.MATCH_DISABLED_COMPONENTS
2881 | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS
2882 | PackageManager.GET_SIGNATURES, UserHandle.USER_SYSTEM);
2883 }
2884 for (int p = packages.size() - 1; p >= 0; p--) {
2885 PackageInfo pkgInfo = packages.get(p);
2886 if (pkgInfo != null && pkgInfo.packageName != null
2887 && card.getCarrierPrivilegeStatus(pkgInfo)
2888 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2889 privilegedPackages.add(pkgInfo.packageName);
2890 }
2891 }
2892 }
2893 }
2894 return privilegedPackages;
2895 }
2896
Wink Savilleb564aae2014-10-23 10:18:09 -07002897 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002898 final Phone phone = getPhone(subId);
2899 UiccCard card = phone == null ? null : phone.getUiccCard();
Derek Tan97ebb422014-09-05 16:55:38 -07002900 if (card == null) {
2901 loge("getIccId: No UICC");
2902 return null;
2903 }
2904 String iccId = card.getIccId();
2905 if (TextUtils.isEmpty(iccId)) {
2906 loge("getIccId: ICC ID is null or empty.");
2907 return null;
2908 }
2909 return iccId;
2910 }
2911
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002912 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002913 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
2914 String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002915 enforceCarrierPrivilege(subId);
Derek Tan97ebb422014-09-05 16:55:38 -07002916
Jeff Sharkey85190e62014-12-05 09:40:12 -08002917 final String iccId = getIccId(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07002918 final Phone phone = getPhone(subId);
2919 if (phone == null) {
2920 return false;
2921 }
2922 final String subscriberId = phone.getSubscriberId();
Jeff Sharkey85190e62014-12-05 09:40:12 -08002923
2924 if (DBG_MERGE) {
2925 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
2926 + subscriberId + " to " + number);
2927 }
2928
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002929 if (TextUtils.isEmpty(iccId)) {
2930 return false;
Derek Tan97ebb422014-09-05 16:55:38 -07002931 }
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002932
Jeff Sharkey85190e62014-12-05 09:40:12 -08002933 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2934
2935 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002936 if (alphaTag == null) {
2937 editor.remove(alphaTagPrefKey);
2938 } else {
2939 editor.putString(alphaTagPrefKey, alphaTag);
2940 }
2941
Jeff Sharkey85190e62014-12-05 09:40:12 -08002942 // Record both the line number and IMSI for this ICCID, since we need to
2943 // track all merged IMSIs based on line number
2944 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2945 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002946 if (number == null) {
2947 editor.remove(numberPrefKey);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002948 editor.remove(subscriberPrefKey);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002949 } else {
2950 editor.putString(numberPrefKey, number);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002951 editor.putString(subscriberPrefKey, subscriberId);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002952 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002953
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002954 editor.commit();
2955 return true;
Derek Tan7226c842014-07-02 17:42:23 -07002956 }
2957
2958 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002959 public String getLine1NumberForDisplay(int subId, String callingPackage) {
Makoto Onukifee69342015-06-29 14:44:50 -07002960 // This is open to apps with WRITE_SMS.
2961 if (!canReadPhoneNumber(callingPackage, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08002962 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07002963 return null;
2964 }
Derek Tan97ebb422014-09-05 16:55:38 -07002965
2966 String iccId = getIccId(subId);
2967 if (iccId != null) {
2968 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
Amit Mahajan9cf11512015-11-09 11:40:48 -08002969 if (DBG_MERGE) {
2970 log("getLine1NumberForDisplay returning " +
2971 mTelephonySharedPreferences.getString(numberPrefKey, null));
2972 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002973 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002974 }
Amit Mahajan9cf11512015-11-09 11:40:48 -08002975 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
Derek Tan97ebb422014-09-05 16:55:38 -07002976 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002977 }
2978
2979 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002980 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
2981 if (!canReadPhoneState(callingPackage, "getLine1AlphaTagForDisplay")) {
2982 return null;
2983 }
Derek Tan97ebb422014-09-05 16:55:38 -07002984
2985 String iccId = getIccId(subId);
2986 if (iccId != null) {
2987 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002988 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002989 }
Derek Tan97ebb422014-09-05 16:55:38 -07002990 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002991 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002992
2993 @Override
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002994 public String[] getMergedSubscriberIds(String callingPackage) {
2995 if (!canReadPhoneState(callingPackage, "getMergedSubscriberIds")) {
2996 return null;
2997 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002998 final Context context = mPhone.getContext();
2999 final TelephonyManager tele = TelephonyManager.from(context);
3000 final SubscriptionManager sub = SubscriptionManager.from(context);
3001
3002 // Figure out what subscribers are currently active
3003 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07003004 // Clear calling identity, when calling TelephonyManager, because callerUid must be
3005 // the process, where TelephonyManager was instantiated. Otherwise AppOps check will fail.
3006 final long identity = Binder.clearCallingIdentity();
3007 try {
3008 final int[] subIds = sub.getActiveSubscriptionIdList();
3009 for (int subId : subIds) {
3010 activeSubscriberIds.add(tele.getSubscriberId(subId));
3011 }
3012 } finally {
3013 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08003014 }
3015
3016 // First pass, find a number override for an active subscriber
3017 String mergeNumber = null;
3018 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
3019 for (String key : prefs.keySet()) {
3020 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
3021 final String subscriberId = (String) prefs.get(key);
3022 if (activeSubscriberIds.contains(subscriberId)) {
3023 final String iccId = key.substring(PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
3024 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
3025 mergeNumber = (String) prefs.get(numberKey);
3026 if (DBG_MERGE) {
3027 Slog.d(LOG_TAG, "Found line number " + mergeNumber
3028 + " for active subscriber " + subscriberId);
3029 }
3030 if (!TextUtils.isEmpty(mergeNumber)) {
3031 break;
3032 }
3033 }
3034 }
3035 }
3036
3037 // Shortcut when no active merged subscribers
3038 if (TextUtils.isEmpty(mergeNumber)) {
3039 return null;
3040 }
3041
3042 // Second pass, find all subscribers under that line override
3043 final ArraySet<String> result = new ArraySet<>();
3044 for (String key : prefs.keySet()) {
3045 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
3046 final String number = (String) prefs.get(key);
3047 if (mergeNumber.equals(number)) {
3048 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
3049 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
3050 final String subscriberId = (String) prefs.get(subscriberKey);
3051 if (!TextUtils.isEmpty(subscriberId)) {
3052 result.add(subscriberId);
3053 }
3054 }
3055 }
3056 }
3057
3058 final String[] resultArray = result.toArray(new String[result.size()]);
3059 Arrays.sort(resultArray);
3060 if (DBG_MERGE) {
3061 Slog.d(LOG_TAG, "Found subscribers " + Arrays.toString(resultArray) + " after merge");
3062 }
3063 return resultArray;
3064 }
3065
3066 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003067 public boolean setOperatorBrandOverride(int subId, String brand) {
3068 enforceCarrierPrivilege(subId);
3069 final Phone phone = getPhone(subId);
3070 return phone == null ? false : phone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07003071 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05003072
3073 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003074 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08003075 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
3076 List<String> cdmaNonRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003077 enforceCarrierPrivilege(subId);
3078 final Phone phone = getPhone(subId);
3079 if (phone == null) {
3080 return false;
3081 }
3082 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08003083 cdmaNonRoamingList);
3084 }
3085
3086 @Override
Amit Mahajanf43fe462017-02-23 12:08:31 -08003087 @Deprecated
Steven Liu4bf01bc2014-07-17 11:05:29 -05003088 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
3089 enforceModifyPermission();
3090
3091 int returnValue = 0;
3092 try {
3093 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
3094 if(result.exception == null) {
3095 if (result.result != null) {
3096 byte[] responseData = (byte[])(result.result);
3097 if(responseData.length > oemResp.length) {
3098 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
3099 responseData.length + "bytes. Buffer Size is " +
3100 oemResp.length + "bytes.");
3101 }
3102 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
3103 returnValue = responseData.length;
3104 }
3105 } else {
3106 CommandException ex = (CommandException) result.exception;
3107 returnValue = ex.getCommandError().ordinal();
3108 if(returnValue > 0) returnValue *= -1;
3109 }
3110 } catch (RuntimeException e) {
3111 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
3112 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
3113 if(returnValue > 0) returnValue *= -1;
3114 }
3115
3116 return returnValue;
3117 }
Wink Saville5d475dd2014-10-17 15:00:58 -07003118
3119 @Override
3120 public void setRadioCapability(RadioAccessFamily[] rafs) {
3121 try {
3122 ProxyController.getInstance().setRadioCapability(rafs);
3123 } catch (RuntimeException e) {
3124 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
3125 }
3126 }
3127
3128 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003129 public int getRadioAccessFamily(int phoneId, String callingPackage) {
3130 if (!canReadPhoneState(callingPackage, "getRadioAccessFamily")) {
3131 return RadioAccessFamily.RAF_UNKNOWN;
3132 }
3133
Wink Saville5d475dd2014-10-17 15:00:58 -07003134 return ProxyController.getInstance().getRadioAccessFamily(phoneId);
3135 }
Andrew Leedf14ead2014-10-17 14:22:52 -07003136
3137 @Override
3138 public void enableVideoCalling(boolean enable) {
3139 enforceModifyPermission();
Tyler Gunnfdd69de2015-12-04 21:24:38 -08003140 ImsManager.setVtSetting(mPhone.getContext(), enable);
Andrew Leedf14ead2014-10-17 14:22:52 -07003141 }
3142
3143 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07003144 public boolean isVideoCallingEnabled(String callingPackage) {
3145 if (!canReadPhoneState(callingPackage, "isVideoCallingEnabled")) {
3146 return false;
3147 }
3148
Andrew Lee77527ac2014-10-21 16:57:39 -07003149 // Check the user preference and the system-level IMS setting. Even if the user has
3150 // enabled video calling, if IMS is disabled we aren't able to support video calling.
3151 // In the long run, we may instead need to check if there exists a connection service
3152 // which can support video calling.
Andrew Lee312e8172014-10-23 17:01:36 -07003153 return ImsManager.isVtEnabledByPlatform(mPhone.getContext())
3154 && ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
Tyler Gunnfdd69de2015-12-04 21:24:38 -08003155 && ImsManager.isVtEnabledByUser(mPhone.getContext());
Andrew Leedf14ead2014-10-17 14:22:52 -07003156 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003157
Andrew Leea1239f22015-03-02 17:44:07 -08003158 @Override
3159 public boolean canChangeDtmfToneLength() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07003160 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08003161 }
3162
3163 @Override
3164 public boolean isWorldPhone() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07003165 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08003166 }
3167
Andrew Lee9431b832015-03-09 18:46:45 -07003168 @Override
3169 public boolean isTtyModeSupported() {
3170 TelecomManager telecomManager = TelecomManager.from(mPhone.getContext());
3171 TelephonyManager telephonyManager =
3172 (TelephonyManager) mPhone.getContext().getSystemService(Context.TELEPHONY_SERVICE);
Wooki Wu1f82f7a2016-02-15 15:59:58 +08003173 return telecomManager.isTtySupported();
Andrew Lee9431b832015-03-09 18:46:45 -07003174 }
3175
3176 @Override
3177 public boolean isHearingAidCompatibilitySupported() {
3178 return mPhone.getContext().getResources().getBoolean(R.bool.hac_enabled);
3179 }
3180
Sanket Padawe7310cc72015-01-14 09:53:20 -08003181 /**
3182 * Returns the unique device ID of phone, for example, the IMEI for
3183 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
3184 *
3185 * <p>Requires Permission:
3186 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
3187 */
3188 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07003189 public String getDeviceId(String callingPackage) {
3190 if (!canReadPhoneState(callingPackage, "getDeviceId")) {
3191 return null;
3192 }
3193
Sanket Padawe7310cc72015-01-14 09:53:20 -08003194 final Phone phone = PhoneFactory.getPhone(0);
3195 if (phone != null) {
3196 return phone.getDeviceId();
3197 } else {
3198 return null;
3199 }
3200 }
3201
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003202 /*
3203 * {@hide}
3204 * Returns the IMS Registration Status
3205 */
Santos Cordon7a1885b2015-02-03 11:15:19 -08003206 @Override
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003207 public boolean isImsRegistered() {
3208 return mPhone.isImsRegistered();
3209 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08003210
3211 @Override
3212 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
3213 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
3214 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07003215
Nathan Haroldc55097a2015-03-11 18:14:50 -07003216 /*
3217 * {@hide}
3218 * Returns the IMS Registration Status
3219 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003220 public boolean isWifiCallingAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07003221 return mPhone.isWifiCallingEnabled();
3222 }
3223
3224 /*
3225 * {@hide}
3226 * Returns the IMS Registration Status
3227 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003228 public boolean isVolteAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07003229 return mPhone.isVolteEnabled();
3230 }
Svet Ganovb320e182015-04-16 12:30:10 -07003231
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003232 /*
3233 * {@hide} Returns the IMS Registration Status
3234 */
3235 public boolean isVideoTelephonyAvailable() {
3236 return mPhone.isVideoEnabled();
3237 }
3238
Svet Ganovb320e182015-04-16 12:30:10 -07003239 private boolean canReadPhoneState(String callingPackage, String message) {
Etan Cohen921655c2015-06-24 13:54:50 -07003240 try {
Amit Mahajan83ea23b2015-07-30 16:05:11 -07003241 mApp.enforceCallingOrSelfPermission(
Etan Cohen921655c2015-06-24 13:54:50 -07003242 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE, message);
3243
Amit Mahajan83ea23b2015-07-30 16:05:11 -07003244 // SKIP checking for run-time permission since caller or self has PRIVILEDGED permission
Etan Cohen921655c2015-06-24 13:54:50 -07003245 return true;
Amit Mahajan785783f2015-06-29 10:36:50 -07003246 } catch (SecurityException e) {
3247 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
3248 message);
Etan Cohen921655c2015-06-24 13:54:50 -07003249 }
Svet Ganovb320e182015-04-16 12:30:10 -07003250
3251 if (mAppOps.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(),
3252 callingPackage) != AppOpsManager.MODE_ALLOWED) {
3253 return false;
3254 }
3255
3256 return true;
3257 }
Stuart Scott8eef64f2015-04-08 15:13:54 -07003258
Makoto Onukifee69342015-06-29 14:44:50 -07003259 /**
Amit Mahajanb9b49782015-09-15 18:16:32 -07003260 * Besides READ_PHONE_STATE, WRITE_SMS and READ_SMS also allow apps to get phone numbers.
Makoto Onukifee69342015-06-29 14:44:50 -07003261 */
3262 private boolean canReadPhoneNumber(String callingPackage, String message) {
Makoto Onukie4072d12015-08-03 15:12:23 -07003263 // Default SMS app can always read it.
3264 if (mAppOps.noteOp(AppOpsManager.OP_WRITE_SMS,
3265 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED) {
3266 return true;
3267 }
Chad Brubaker7a8fba22016-11-10 11:23:18 -08003268
Makoto Onukie4072d12015-08-03 15:12:23 -07003269 try {
3270 return canReadPhoneState(callingPackage, message);
Amit Mahajanb9b49782015-09-15 18:16:32 -07003271 } catch (SecurityException readPhoneStateSecurityException) {
Makoto Onukie4072d12015-08-03 15:12:23 -07003272 }
Chad Brubaker7a8fba22016-11-10 11:23:18 -08003273 // Can be read with READ_SMS too.
3274 try {
3275 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_SMS, message);
3276 int opCode = mAppOps.permissionToOpCode(android.Manifest.permission.READ_SMS);
3277 if (opCode != AppOpsManager.OP_NONE) {
3278 return mAppOps.noteOp(opCode, Binder.getCallingUid(), callingPackage)
3279 == AppOpsManager.MODE_ALLOWED;
3280 } else {
3281 return true;
3282 }
3283 } catch (SecurityException readSmsSecurityException) {
3284 }
Chad Brubakerf342a982017-03-30 16:27:34 -07003285 // Can be read with READ_PHONE_NUMBERS too.
Chad Brubaker7a8fba22016-11-10 11:23:18 -08003286 try {
Chad Brubakerf342a982017-03-30 16:27:34 -07003287 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_NUMBERS,
Chad Brubaker7a8fba22016-11-10 11:23:18 -08003288 message);
Chad Brubakerf342a982017-03-30 16:27:34 -07003289 int opCode = mAppOps.permissionToOpCode(android.Manifest.permission.READ_PHONE_NUMBERS);
Chad Brubaker7a8fba22016-11-10 11:23:18 -08003290 if (opCode != AppOpsManager.OP_NONE) {
3291 return mAppOps.noteOp(opCode, Binder.getCallingUid(), callingPackage)
3292 == AppOpsManager.MODE_ALLOWED;
3293 } else {
3294 return true;
3295 }
3296 } catch (SecurityException readPhoneNumberSecurityException) {
3297 }
3298
3299 throw new SecurityException(message + ": Neither user " + Binder.getCallingUid() +
3300 " nor current process has" + android.Manifest.permission.READ_PHONE_STATE +
3301 ", " + android.Manifest.permission.READ_SMS + ", or " +
Chad Brubakerf342a982017-03-30 16:27:34 -07003302 android.Manifest.permission.READ_PHONE_NUMBERS);
Makoto Onukifee69342015-06-29 14:44:50 -07003303 }
3304
Stuart Scott8eef64f2015-04-08 15:13:54 -07003305 @Override
3306 public void factoryReset(int subId) {
3307 enforceConnectivityInternalPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07003308 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
3309 return;
3310 }
3311
Svet Ganovcc087f82015-05-12 20:35:54 -07003312 final long identity = Binder.clearCallingIdentity();
3313 try {
Stuart Scott981d8582015-04-21 14:09:50 -07003314 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
3315 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Svet Ganovcc087f82015-05-12 20:35:54 -07003316 // Enable data
3317 setDataEnabled(subId, true);
3318 // Set network selection mode to automatic
3319 setNetworkSelectionModeAutomatic(subId);
3320 // Set preferred mobile network type to the best available
3321 setPreferredNetworkType(subId, Phone.PREFERRED_NT_MODE);
3322 // Turn off roaming
Malcolm Chenf6b97f42017-04-19 16:03:24 -07003323 mPhone.setDataRoamingEnabled(false);
Svet Ganovcc087f82015-05-12 20:35:54 -07003324 }
3325 } finally {
3326 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07003327 }
3328 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003329
3330 @Override
3331 public String getLocaleFromDefaultSim() {
3332 // We query all subscriptions instead of just the active ones, because
3333 // this might be called early on in the provisioning flow when the
3334 // subscriptions potentially aren't active yet.
3335 final List<SubscriptionInfo> slist = getAllSubscriptionInfoList();
3336 if (slist == null || slist.isEmpty()) {
3337 return null;
3338 }
3339
3340 // This function may be called very early, say, from the setup wizard, at
3341 // which point we won't have a default subscription set. If that's the case
3342 // we just choose the first, which will be valid in "most cases".
3343 final int defaultSubId = getDefaultSubscription();
3344 SubscriptionInfo info = null;
3345 if (defaultSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
3346 info = slist.get(0);
3347 } else {
3348 for (SubscriptionInfo item : slist) {
3349 if (item.getSubscriptionId() == defaultSubId) {
3350 info = item;
3351 break;
3352 }
3353 }
3354
3355 if (info == null) {
3356 return null;
3357 }
3358 }
3359
3360 // Try and fetch the locale from the carrier properties or from the SIM language
3361 // preferences (EF-PL and EF-LI)...
Tony Hill183b2de2015-06-24 14:53:58 +01003362 final int mcc = info.getMcc();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003363 final Phone defaultPhone = getPhone(info.getSubscriptionId());
Narayan Kamath011676f2015-07-29 12:04:08 +01003364 String simLanguage = null;
Narayan Kamath1c496c22015-04-16 14:40:19 +01003365 if (defaultPhone != null) {
3366 final Locale localeFromDefaultSim = defaultPhone.getLocaleFromSimAndCarrierPrefs();
3367 if (localeFromDefaultSim != null) {
Narayan Kamath011676f2015-07-29 12:04:08 +01003368 if (!localeFromDefaultSim.getCountry().isEmpty()) {
Tony Hill183b2de2015-06-24 14:53:58 +01003369 if (DBG) log("Using locale from default SIM:" + localeFromDefaultSim);
3370 return localeFromDefaultSim.toLanguageTag();
Narayan Kamath011676f2015-07-29 12:04:08 +01003371 } else {
3372 simLanguage = localeFromDefaultSim.getLanguage();
Tony Hill183b2de2015-06-24 14:53:58 +01003373 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003374 }
3375 }
3376
Narayan Kamath011676f2015-07-29 12:04:08 +01003377 // The SIM language preferences only store a language (e.g. fr = French), not an
3378 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
3379 // the SIM and carrier preferences does not include a country we add the country
3380 // determined from the SIM MCC to provide an exact locale.
3381 final Locale mccLocale = MccTable.getLocaleFromMcc(mPhone.getContext(), mcc, simLanguage);
Tony Hill183b2de2015-06-24 14:53:58 +01003382 if (mccLocale != null) {
3383 if (DBG) log("No locale from default SIM, using mcc locale:" + mccLocale);
3384 return mccLocale.toLanguageTag();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003385 }
3386
Tony Hill183b2de2015-06-24 14:53:58 +01003387 if (DBG) log("No locale found - returning null");
Narayan Kamath1c496c22015-04-16 14:40:19 +01003388 return null;
3389 }
3390
3391 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
3392 final long identity = Binder.clearCallingIdentity();
3393 try {
3394 return mSubscriptionController.getAllSubInfoList(
3395 mPhone.getContext().getOpPackageName());
3396 } finally {
3397 Binder.restoreCallingIdentity(identity);
3398 }
3399 }
3400
3401 private List<SubscriptionInfo> getActiveSubscriptionInfoList() {
3402 final long identity = Binder.clearCallingIdentity();
3403 try {
3404 return mSubscriptionController.getActiveSubscriptionInfoList(
3405 mPhone.getContext().getOpPackageName());
3406 } finally {
3407 Binder.restoreCallingIdentity(identity);
3408 }
3409 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003410
3411 /**
Adam Lesinski903a54c2016-04-11 14:49:52 -07003412 * Responds to the ResultReceiver with the {@link android.telephony.ModemActivityInfo} object
3413 * representing the state of the modem.
3414 *
3415 * NOTE: This clears the modem state, so there should only every be one caller.
3416 * @hide
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003417 */
3418 @Override
Adam Lesinski903a54c2016-04-11 14:49:52 -07003419 public void requestModemActivityInfo(ResultReceiver result) {
3420 enforceModifyPermission();
3421
3422 ModemActivityInfo info = (ModemActivityInfo) sendRequest(CMD_GET_MODEM_ACTIVITY_INFO, null);
3423 Bundle bundle = new Bundle();
3424 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY, info);
3425 result.send(0, bundle);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003426 }
Jack Yu85bd38a2015-11-09 11:34:32 -08003427
3428 /**
3429 * {@hide}
3430 * Returns the service state information on specified subscription.
3431 */
3432 @Override
3433 public ServiceState getServiceStateForSubscriber(int subId, String callingPackage) {
3434
3435 if (!canReadPhoneState(callingPackage, "getServiceStateForSubscriber")) {
3436 return null;
3437 }
3438
3439 final Phone phone = getPhone(subId);
3440 if (phone == null) {
3441 return null;
3442 }
3443
3444 return phone.getServiceState();
3445 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08003446
3447 /**
3448 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
3449 *
3450 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3451 * voicemail ringtone.
3452 * @return The URI for the ringtone to play when receiving a voicemail from a specific
3453 * PhoneAccount.
3454 */
3455 @Override
3456 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
Ta-wei Yen00b4ab52017-04-11 13:24:26 -07003457 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
Nancy Chen31f9ba12016-01-06 11:42:12 -08003458 if (phone == null) {
Ta-wei Yen00b4ab52017-04-11 13:24:26 -07003459 phone = mPhone;
Nancy Chen31f9ba12016-01-06 11:42:12 -08003460 }
3461
3462 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone);
3463 }
3464
3465 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08003466 * Sets the per-account voicemail ringtone.
3467 *
3468 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
3469 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
3470 *
3471 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
3472 * voicemail ringtone.
3473 * @param uri The URI for the ringtone to play when receiving a voicemail from a specific
3474 * PhoneAccount.
3475 */
3476 @Override
3477 public void setVoicemailRingtoneUri(String callingPackage,
3478 PhoneAccountHandle phoneAccountHandle, Uri uri) {
3479 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
3480 if (!TextUtils.equals(callingPackage,
3481 TelecomManager.from(mPhone.getContext()).getDefaultDialerPackage())) {
3482 enforceModifyPermissionOrCarrierPrivilege(
3483 PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle));
3484 }
3485 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
3486 if (phone == null){
Ta-wei Yen00b4ab52017-04-11 13:24:26 -07003487 phone = mPhone;
Ta-wei Yenc33877d2017-01-23 18:11:21 -08003488 }
3489 VoicemailNotificationSettingsUtil.setRingtoneUri(phone, uri);
3490 }
3491
3492 /**
Nancy Chen31f9ba12016-01-06 11:42:12 -08003493 * Returns whether vibration is set for voicemail notification in Phone settings.
3494 *
3495 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3496 * voicemail vibration setting.
3497 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
3498 */
3499 @Override
3500 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
Ta-wei Yen00b4ab52017-04-11 13:24:26 -07003501 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
Nancy Chen31f9ba12016-01-06 11:42:12 -08003502 if (phone == null) {
Ta-wei Yen00b4ab52017-04-11 13:24:26 -07003503 phone = mPhone;
Nancy Chen31f9ba12016-01-06 11:42:12 -08003504 }
3505
3506 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone);
3507 }
3508
Youhan Wange64578a2016-05-02 15:32:42 -07003509 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08003510 * Sets the per-account voicemail vibration.
3511 *
3512 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
3513 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
3514 *
3515 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
3516 * voicemail vibration setting.
3517 * @param enabled Whether to enable or disable vibration for voicemail notifications from a
3518 * specific PhoneAccount.
3519 */
3520 @Override
3521 public void setVoicemailVibrationEnabled(String callingPackage,
3522 PhoneAccountHandle phoneAccountHandle, boolean enabled) {
3523 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
3524 if (!TextUtils.equals(callingPackage,
3525 TelecomManager.from(mPhone.getContext()).getDefaultDialerPackage())) {
3526 enforceModifyPermissionOrCarrierPrivilege(
3527 PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle));
3528 }
3529
3530 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
3531 if (phone == null){
Ta-wei Yen00b4ab52017-04-11 13:24:26 -07003532 phone = mPhone;
Ta-wei Yenc33877d2017-01-23 18:11:21 -08003533 }
3534 VoicemailNotificationSettingsUtil.setVibrationEnabled(phone, enabled);
3535 }
3536
3537 /**
Youhan Wange64578a2016-05-02 15:32:42 -07003538 * Make sure either called from same process as self (phone) or IPC caller has read privilege.
3539 *
3540 * @throws SecurityException if the caller does not have the required permission
3541 */
3542 private void enforceReadPrivilegedPermission() {
3543 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
3544 null);
3545 }
3546
3547 /**
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003548 * Make sure either called from same process as self (phone) or IPC caller has send SMS
3549 * permission.
3550 *
3551 * @throws SecurityException if the caller does not have the required permission
3552 */
3553 private void enforceSendSmsPermission() {
3554 mApp.enforceCallingOrSelfPermission(permission.SEND_SMS, null);
3555 }
3556
3557 /**
Ta-wei Yen527a9c02017-01-06 15:29:25 -08003558 * Make sure called from the package in charge of visual voicemail.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003559 *
Ta-wei Yen527a9c02017-01-06 15:29:25 -08003560 * @throws SecurityException if the caller is not the visual voicemail package.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003561 */
Ta-wei Yen527a9c02017-01-06 15:29:25 -08003562 private void enforceVisualVoicemailPackage(String callingPackage, int subId) {
Ta-wei Yen5d81b0c2017-03-03 16:32:26 -08003563 ComponentName componentName =
3564 RemoteVvmTaskManager.getRemotePackage(mPhone.getContext(), subId);
3565 if(componentName == null) {
3566 throw new SecurityException("Caller not current active visual voicemail package[null]");
3567 }
3568 String vvmPackage = componentName.getPackageName();
Ta-wei Yen527a9c02017-01-06 15:29:25 -08003569 if (!callingPackage.equals(vvmPackage)) {
3570 throw new SecurityException("Caller not current active visual voicemail package[" +
3571 vvmPackage + "]");
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003572 }
3573 }
3574
3575 /**
Youhan Wange64578a2016-05-02 15:32:42 -07003576 * Return the application ID for the app type.
3577 *
3578 * @param subId the subscription ID that this request applies to.
3579 * @param appType the uicc app type.
3580 * @return Application ID for specificied app type, or null if no uicc.
3581 */
3582 @Override
3583 public String getAidForAppType(int subId, int appType) {
3584 enforceReadPrivilegedPermission();
3585 Phone phone = getPhone(subId);
3586 if (phone == null) {
3587 return null;
3588 }
3589 String aid = null;
3590 try {
3591 aid = UiccController.getInstance().getUiccCard(phone.getPhoneId())
3592 .getApplicationByType(appType).getAid();
3593 } catch (Exception e) {
3594 Log.e(LOG_TAG, "Not getting aid. Exception ex=" + e);
3595 }
3596 return aid;
3597 }
3598
Youhan Wang4001d252016-05-11 10:29:41 -07003599 /**
3600 * Return the Electronic Serial Number.
3601 *
3602 * @param subId the subscription ID that this request applies to.
3603 * @return ESN or null if error.
3604 */
3605 @Override
3606 public String getEsn(int subId) {
3607 enforceReadPrivilegedPermission();
3608 Phone phone = getPhone(subId);
3609 if (phone == null) {
3610 return null;
3611 }
3612 String esn = null;
3613 try {
3614 esn = phone.getEsn();
3615 } catch (Exception e) {
3616 Log.e(LOG_TAG, "Not getting ESN. Exception ex=" + e);
3617 }
3618 return esn;
3619 }
3620
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003621 /**
Youhan Wang66ad5d72016-07-18 17:56:58 -07003622 * Return the Preferred Roaming List Version.
3623 *
3624 * @param subId the subscription ID that this request applies to.
3625 * @return PRLVersion or null if error.
3626 */
3627 @Override
3628 public String getCdmaPrlVersion(int subId) {
3629 enforceReadPrivilegedPermission();
3630 Phone phone = getPhone(subId);
3631 if (phone == null) {
3632 return null;
3633 }
3634 String cdmaPrlVersion = null;
3635 try {
3636 cdmaPrlVersion = phone.getCdmaPrlVersion();
3637 } catch (Exception e) {
3638 Log.e(LOG_TAG, "Not getting PRLVersion", e);
3639 }
3640 return cdmaPrlVersion;
3641 }
3642
3643 /**
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003644 * Get snapshot of Telephony histograms
3645 * @return List of Telephony histograms
3646 * @hide
3647 */
3648 @Override
3649 public List<TelephonyHistogram> getTelephonyHistograms() {
3650 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
3651 return RIL.getTelephonyRILTimingHistograms();
3652 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07003653
3654 /**
3655 * {@hide}
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003656 * Set the allowed carrier list for slotIndex
Meng Wang1a7c35a2016-05-05 20:56:15 -07003657 * Require system privileges. In the future we may add this to carrier APIs.
3658 *
3659 * @return The number of carriers set successfully, should match length of carriers
3660 */
3661 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003662 public int setAllowedCarriers(int slotIndex, List<CarrierIdentifier> carriers) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07003663 enforceModifyPermission();
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003664
Meng Wang9b7c4e92017-02-17 11:41:27 -08003665 if (carriers == null) {
3666 throw new NullPointerException("carriers cannot be null");
3667 }
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003668
3669 int subId = SubscriptionManager.getSubId(slotIndex)[0];
Meng Wang1a7c35a2016-05-05 20:56:15 -07003670 int[] retVal = (int[]) sendRequest(CMD_SET_ALLOWED_CARRIERS, carriers, subId);
3671 return retVal[0];
3672 }
3673
3674 /**
3675 * {@hide}
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003676 * Get the allowed carrier list for slotIndex.
Meng Wang1a7c35a2016-05-05 20:56:15 -07003677 * Require system privileges. In the future we may add this to carrier APIs.
3678 *
3679 * @return List of {@link android.service.telephony.CarrierIdentifier}; empty list
3680 * means all carriers are allowed.
3681 */
3682 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003683 public List<CarrierIdentifier> getAllowedCarriers(int slotIndex) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07003684 enforceReadPrivilegedPermission();
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003685 int subId = SubscriptionManager.getSubId(slotIndex)[0];
Meng Wang1a7c35a2016-05-05 20:56:15 -07003686 return (List<CarrierIdentifier>) sendRequest(CMD_GET_ALLOWED_CARRIERS, null, subId);
3687 }
3688
fionaxu59545b42016-05-25 15:53:37 -07003689 /**
3690 * Action set from carrier signalling broadcast receivers to enable/disable metered apns
3691 * @param subId the subscription ID that this action applies to.
3692 * @param enabled control enable or disable metered apns.
3693 * {@hide}
3694 */
3695 @Override
3696 public void carrierActionSetMeteredApnsEnabled(int subId, boolean enabled) {
3697 enforceModifyPermission();
3698 final Phone phone = getPhone(subId);
3699 if (phone == null) {
3700 loge("carrierAction: SetMeteredApnsEnabled fails with invalid subId: " + subId);
3701 return;
3702 }
3703 try {
3704 phone.carrierActionSetMeteredApnsEnabled(enabled);
3705 } catch (Exception e) {
3706 Log.e(LOG_TAG, "carrierAction: SetMeteredApnsEnabled fails. Exception ex=" + e);
3707 }
3708 }
3709
3710 /**
3711 * Action set from carrier signalling broadcast receivers to enable/disable radio
3712 * @param subId the subscription ID that this action applies to.
3713 * @param enabled control enable or disable radio.
3714 * {@hide}
3715 */
3716 @Override
3717 public void carrierActionSetRadioEnabled(int subId, boolean enabled) {
3718 enforceModifyPermission();
3719 final Phone phone = getPhone(subId);
3720 if (phone == null) {
3721 loge("carrierAction: SetRadioEnabled fails with invalid sibId: " + subId);
3722 return;
3723 }
3724 try {
3725 phone.carrierActionSetRadioEnabled(enabled);
3726 } catch (Exception e) {
3727 Log.e(LOG_TAG, "carrierAction: SetRadioEnabled fails. Exception ex=" + e);
3728 }
3729 }
3730
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003731 /**
3732 * Called when "adb shell dumpsys phone" is invoked. Dump is also automatically invoked when a
3733 * bug report is being generated.
3734 */
3735 @Override
Ta-wei Yen99282e02016-06-21 18:19:35 -07003736 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
dcashman22b950d2016-06-27 11:39:02 -07003737 if (mPhone.getContext().checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3738 != PackageManager.PERMISSION_GRANTED) {
3739 writer.println("Permission Denial: can't dump Phone from pid="
3740 + Binder.getCallingPid()
3741 + ", uid=" + Binder.getCallingUid()
3742 + "without permission "
3743 + android.Manifest.permission.DUMP);
3744 return;
3745 }
Ta-wei Yen99282e02016-06-21 18:19:35 -07003746 DumpsysHandler.dump(mPhone.getContext(), fd, writer, args);
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003747 }
Jack Yueb89b242016-06-22 13:27:47 -07003748
3749 /**
3750 * Get aggregated video call data usage from all subscriptions since boot.
3751 * @return total data usage in bytes
3752 * {@hide}
3753 */
3754 @Override
3755 public long getVtDataUsage() {
3756 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_NETWORK_USAGE_HISTORY,
3757 null);
3758
3759 // NetworkStatsService keeps tracking the active network interface and identity. It will
3760 // record the delta with the corresponding network identity. What we need to do here is
3761 // returning total video call data usage from all subscriptions since boot.
3762
3763 // TODO: Add sub id support in the future. We'll need it when we support DSDA and
3764 // simultaneous VT calls.
3765 final Phone[] phones = PhoneFactory.getPhones();
3766 long total = 0;
3767 for (Phone phone : phones) {
3768 total += phone.getVtDataUsage();
3769 }
3770 return total;
3771 }
Jack Yu75ab2952016-07-08 14:29:33 -07003772
3773 /**
3774 * Policy control of data connection. Usually used when data limit is passed.
3775 * @param enabled True if enabling the data, otherwise disabling.
3776 * @param subId Subscription index
3777 * {@hide}
3778 */
3779 @Override
3780 public void setPolicyDataEnabled(boolean enabled, int subId) {
3781 enforceModifyPermission();
3782 Phone phone = getPhone(subId);
3783 if (phone != null) {
3784 phone.setPolicyDataEnabled(enabled);
3785 }
3786 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07003787
3788 /**
3789 * Get Client request stats
3790 * @return List of Client Request Stats
3791 * @hide
3792 */
3793 @Override
3794 public List<ClientRequestStats> getClientRequestStats(String callingPackage, int subId) {
3795 if (!canReadPhoneState(callingPackage, "getClientRequestStats")) {
3796 return null;
3797 }
3798
3799 Phone phone = getPhone(subId);
3800 if (phone != null) {
3801 return phone.getClientRequestStats();
3802 }
3803
3804 return null;
3805 }
3806
3807 private WorkSource getWorkSource(WorkSource workSource, int uid) {
3808 if (workSource != null) {
3809 return workSource;
3810 }
3811
3812 String packageName = mPhone.getContext().getPackageManager().getNameForUid(uid);
3813 workSource = new WorkSource(uid, packageName);
3814 return workSource;
3815 }
Jack Yueb4124c2017-02-16 15:32:43 -08003816
3817 /**
Grace Chen70990072017-03-24 17:21:30 -07003818 * Set SIM card power state.
Jack Yueb4124c2017-02-16 15:32:43 -08003819 *
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003820 * @param slotIndex SIM slot id.
Grace Chen70990072017-03-24 17:21:30 -07003821 * @param state State of SIM (power down, power up, pass through)
3822 * - {@link android.telephony.TelephonyManager#CARD_POWER_DOWN}
3823 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP}
3824 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP_PASS_THROUGH}
Jack Yueb4124c2017-02-16 15:32:43 -08003825 *
3826 **/
3827 @Override
Grace Chen70990072017-03-24 17:21:30 -07003828 public void setSimPowerStateForSlot(int slotIndex, int state) {
Jack Yueb4124c2017-02-16 15:32:43 -08003829 enforceModifyPermission();
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003830 Phone phone = PhoneFactory.getPhone(slotIndex);
3831
Jack Yueb4124c2017-02-16 15:32:43 -08003832 if (phone != null) {
Grace Chen70990072017-03-24 17:21:30 -07003833 phone.setSimPowerState(state);
Jack Yueb4124c2017-02-16 15:32:43 -08003834 }
3835 }
Shuo Qiandd210312017-04-12 22:11:33 +00003836
3837 /**
3838 * Check if phone is in emergency callback mode
3839 * @return true if phone is in emergency callback mode
3840 * @param subId sub id
3841 */
3842 public boolean getEmergencyCallbackMode(int subId) {
3843 final Phone phone = getPhone(subId);
3844 if (phone != null) {
3845 return phone.isInEcm();
3846 } else {
3847 return false;
3848 }
3849 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08003850
3851 /**
3852 * Get the current signal strength information for the given subscription.
3853 * Because this information is not updated when the device is in a low power state
3854 * it should not be relied-upon to be current.
3855 * @param subId Subscription index
3856 * @return the most recent cached signal strength info from the modem
3857 */
3858 @Override
3859 public SignalStrength getSignalStrength(int subId) {
3860 Phone p = getPhone(subId);
3861 if (p == null) {
3862 return null;
3863 }
3864
3865 return p.getSignalStrength();
3866 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003867}