blob: 2ace8e10730c22d1c1daaf41043fa4e035082456 [file] [log] [blame]
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.phone;
18
Ta-wei Yen87c49842016-05-13 21:19:52 -070019import static com.android.internal.telephony.PhoneConstants.SUBSCRIPTION_KEY;
20
Santos Cordon7d4ddf62013-07-10 11:58:08 -070021import android.app.ActivityManager;
22import android.app.AppOpsManager;
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -070023import android.content.ComponentName;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070024import android.content.Context;
25import android.content.Intent;
Derek Tan97ebb422014-09-05 16:55:38 -070026import android.content.SharedPreferences;
Amith Yamasani6e118872016-02-19 12:53:51 -080027import android.content.pm.PackageInfo;
Shishir Agrawal60f9c952014-06-23 12:00:43 -070028import android.content.pm.PackageManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070029import android.net.Uri;
30import android.os.AsyncResult;
31import android.os.Binder;
32import android.os.Bundle;
33import android.os.Handler;
34import android.os.Looper;
35import android.os.Message;
36import android.os.Process;
Adam Lesinski903a54c2016-04-11 14:49:52 -070037import android.os.ResultReceiver;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070038import android.os.ServiceManager;
39import android.os.UserHandle;
Stuart Scott981d8582015-04-21 14:09:50 -070040import android.os.UserManager;
Derek Tan97ebb422014-09-05 16:55:38 -070041import android.preference.PreferenceManager;
Ihab Awadf2177b72013-11-25 13:33:23 -080042import android.provider.Settings;
Meng Wang1a7c35a2016-05-05 20:56:15 -070043import android.service.carrier.CarrierIdentifier;
Santos Cordon7a1885b2015-02-03 11:15:19 -080044import android.telecom.PhoneAccount;
Nancy Chen31f9ba12016-01-06 11:42:12 -080045import android.telecom.PhoneAccountHandle;
Andrew Lee9431b832015-03-09 18:46:45 -070046import android.telecom.TelecomManager;
Junda Liu12f7d802015-05-01 12:06:44 -070047import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070048import android.telephony.CellInfo;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -070049import android.telephony.IccOpenLogicalChannelResponse;
Ta-wei Yen87c49842016-05-13 21:19:52 -070050import android.telephony.ModemActivityInfo;
Jake Hambye994d462014-02-03 13:10:13 -080051import android.telephony.NeighboringCellInfo;
Wink Saville5d475dd2014-10-17 15:00:58 -070052import android.telephony.RadioAccessFamily;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070053import android.telephony.ServiceState;
Wink Saville0f3b5fc2014-11-11 08:40:49 -080054import android.telephony.SubscriptionInfo;
Jeff Sharkey85190e62014-12-05 09:40:12 -080055import android.telephony.SubscriptionManager;
Sanket Padawe99ef1e32016-05-18 16:12:33 -070056import android.telephony.TelephonyHistogram;
Ta-wei Yenb6929602016-05-24 15:48:27 -070057import android.telephony.TelephonyManager;
58import android.telephony.VisualVoicemailSmsFilterSettings;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070059import android.text.TextUtils;
Jeff Sharkey85190e62014-12-05 09:40:12 -080060import android.util.ArraySet;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070061import android.util.Log;
Jake Hambye994d462014-02-03 13:10:13 -080062import android.util.Pair;
Jeff Sharkey85190e62014-12-05 09:40:12 -080063import android.util.Slog;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070064
Andrew Lee312e8172014-10-23 17:01:36 -070065import com.android.ims.ImsManager;
Shishir Agrawal566b7612013-10-28 14:41:00 -070066import com.android.internal.telephony.CallManager;
Shishir Agrawal302c8692015-06-19 13:49:39 -070067import com.android.internal.telephony.CellNetworkScanResult;
Shishir Agrawal566b7612013-10-28 14:41:00 -070068import com.android.internal.telephony.CommandException;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070069import com.android.internal.telephony.DefaultPhoneNotifier;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070070import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -080071import com.android.internal.telephony.IccCard;
Narayan Kamath1c496c22015-04-16 14:40:19 +010072import com.android.internal.telephony.MccTable;
Shishir Agrawal302c8692015-06-19 13:49:39 -070073import com.android.internal.telephony.OperatorInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070074import com.android.internal.telephony.Phone;
Ta-wei Yen87c49842016-05-13 21:19:52 -070075import com.android.internal.telephony.PhoneConstants;
Wink Saville36469e72014-06-11 15:17:00 -070076import com.android.internal.telephony.PhoneFactory;
Wink Saville5d475dd2014-10-17 15:00:58 -070077import com.android.internal.telephony.ProxyController;
Sanket Padawe99ef1e32016-05-18 16:12:33 -070078import com.android.internal.telephony.RIL;
Svet Ganovb320e182015-04-16 12:30:10 -070079import com.android.internal.telephony.RILConstants;
Wink Savilleac1bdfd2014-11-20 23:04:44 -080080import com.android.internal.telephony.SubscriptionController;
Shishir Agrawal566b7612013-10-28 14:41:00 -070081import com.android.internal.telephony.uicc.IccIoResult;
82import com.android.internal.telephony.uicc.IccUtils;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -070083import com.android.internal.telephony.uicc.UiccCard;
Shishir Agrawal566b7612013-10-28 14:41:00 -070084import com.android.internal.telephony.uicc.UiccController;
Jake Hambye994d462014-02-03 13:10:13 -080085import com.android.internal.util.HexDump;
Nancy Chen31f9ba12016-01-06 11:42:12 -080086import com.android.phone.settings.VoicemailNotificationSettingsUtil;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070087
Santos Cordon7d4ddf62013-07-10 11:58:08 -070088import java.util.ArrayList;
Jeff Sharkey85190e62014-12-05 09:40:12 -080089import java.util.Arrays;
Jake Hambye994d462014-02-03 13:10:13 -080090import java.util.List;
Narayan Kamath1c496c22015-04-16 14:40:19 +010091import java.util.Locale;
Jeff Sharkey85190e62014-12-05 09:40:12 -080092import java.util.Map;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070093
94/**
95 * Implementation of the ITelephony interface.
96 */
Santos Cordon117fee72014-05-16 17:56:12 -070097public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -070098 private static final String LOG_TAG = "PhoneInterfaceManager";
99 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
100 private static final boolean DBG_LOC = false;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800101 private static final boolean DBG_MERGE = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700102
103 // Message codes used with mMainThreadHandler
104 private static final int CMD_HANDLE_PIN_MMI = 1;
105 private static final int CMD_HANDLE_NEIGHBORING_CELL = 2;
106 private static final int EVENT_NEIGHBORING_CELL_DONE = 3;
107 private static final int CMD_ANSWER_RINGING_CALL = 4;
108 private static final int CMD_END_CALL = 5; // not used yet
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700109 private static final int CMD_TRANSMIT_APDU_LOGICAL_CHANNEL = 7;
110 private static final int EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE = 8;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700111 private static final int CMD_OPEN_CHANNEL = 9;
112 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
113 private static final int CMD_CLOSE_CHANNEL = 11;
114 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -0800115 private static final int CMD_NV_READ_ITEM = 13;
116 private static final int EVENT_NV_READ_ITEM_DONE = 14;
117 private static final int CMD_NV_WRITE_ITEM = 15;
118 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
119 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
120 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
121 private static final int CMD_NV_RESET_CONFIG = 19;
122 private static final int EVENT_NV_RESET_CONFIG_DONE = 20;
Jake Hamby7c27be32014-03-03 13:25:59 -0800123 private static final int CMD_GET_PREFERRED_NETWORK_TYPE = 21;
124 private static final int EVENT_GET_PREFERRED_NETWORK_TYPE_DONE = 22;
125 private static final int CMD_SET_PREFERRED_NETWORK_TYPE = 23;
126 private static final int EVENT_SET_PREFERRED_NETWORK_TYPE_DONE = 24;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800127 private static final int CMD_SEND_ENVELOPE = 25;
128 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Derek Tan6b088ee2014-09-05 14:15:18 -0700129 private static final int CMD_INVOKE_OEM_RIL_REQUEST_RAW = 27;
130 private static final int EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE = 28;
131 private static final int CMD_TRANSMIT_APDU_BASIC_CHANNEL = 29;
132 private static final int EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE = 30;
133 private static final int CMD_EXCHANGE_SIM_IO = 31;
134 private static final int EVENT_EXCHANGE_SIM_IO_DONE = 32;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800135 private static final int CMD_SET_VOICEMAIL_NUMBER = 33;
136 private static final int EVENT_SET_VOICEMAIL_NUMBER_DONE = 34;
Stuart Scott54788802015-03-30 13:18:01 -0700137 private static final int CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC = 35;
138 private static final int EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE = 36;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700139 private static final int CMD_GET_MODEM_ACTIVITY_INFO = 37;
140 private static final int EVENT_GET_MODEM_ACTIVITY_INFO_DONE = 38;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700141 private static final int CMD_PERFORM_NETWORK_SCAN = 39;
142 private static final int EVENT_PERFORM_NETWORK_SCAN_DONE = 40;
143 private static final int CMD_SET_NETWORK_SELECTION_MODE_MANUAL = 41;
144 private static final int EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE = 42;
Meng Wang1a7c35a2016-05-05 20:56:15 -0700145 private static final int CMD_SET_ALLOWED_CARRIERS = 43;
146 private static final int EVENT_SET_ALLOWED_CARRIERS_DONE = 44;
147 private static final int CMD_GET_ALLOWED_CARRIERS = 45;
148 private static final int EVENT_GET_ALLOWED_CARRIERS_DONE = 46;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700149
150 /** The singleton instance. */
151 private static PhoneInterfaceManager sInstance;
152
Wink Saville3ab207e2014-11-20 13:07:20 -0800153 private PhoneGlobals mApp;
154 private Phone mPhone;
155 private CallManager mCM;
Stuart Scott981d8582015-04-21 14:09:50 -0700156 private UserManager mUserManager;
Wink Saville3ab207e2014-11-20 13:07:20 -0800157 private AppOpsManager mAppOps;
158 private MainThreadHandler mMainThreadHandler;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800159 private SubscriptionController mSubscriptionController;
Wink Saville3ab207e2014-11-20 13:07:20 -0800160 private SharedPreferences mTelephonySharedPreferences;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700161
Derek Tan97ebb422014-09-05 16:55:38 -0700162 private static final String PREF_CARRIERS_ALPHATAG_PREFIX = "carrier_alphtag_";
163 private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
Jeff Sharkey85190e62014-12-05 09:40:12 -0800164 private static final String PREF_CARRIERS_SUBSCRIBER_PREFIX = "carrier_subscriber_";
Derek Tan89e89d42014-07-08 17:00:10 -0700165
166 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700167 * A request object to use for transmitting data to an ICC.
168 */
169 private static final class IccAPDUArgument {
170 public int channel, cla, command, p1, p2, p3;
171 public String data;
172
173 public IccAPDUArgument(int channel, int cla, int command,
174 int p1, int p2, int p3, String data) {
175 this.channel = channel;
176 this.cla = cla;
177 this.command = command;
178 this.p1 = p1;
179 this.p2 = p2;
180 this.p3 = p3;
181 this.data = data;
182 }
183 }
184
185 /**
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700186 * A request object to use for transmitting data to an ICC.
187 */
188 private static final class ManualNetworkSelectionArgument {
189 public OperatorInfo operatorInfo;
190 public boolean persistSelection;
191
192 public ManualNetworkSelectionArgument(OperatorInfo operatorInfo, boolean persistSelection) {
193 this.operatorInfo = operatorInfo;
194 this.persistSelection = persistSelection;
195 }
196 }
197
198 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700199 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
200 * request after sending. The main thread will notify the request when it is complete.
201 */
202 private static final class MainThreadRequest {
203 /** The argument to use for the request */
204 public Object argument;
205 /** The result of the request that is run on the main thread */
206 public Object result;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800207 // The subscriber id that this request applies to. Defaults to
208 // SubscriptionManager.INVALID_SUBSCRIPTION_ID
209 public Integer subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700210
211 public MainThreadRequest(Object argument) {
212 this.argument = argument;
213 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800214
215 public MainThreadRequest(Object argument, Integer subId) {
216 this.argument = argument;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800217 if (subId != null) {
218 this.subId = subId;
219 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800220 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700221 }
222
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800223 private static final class IncomingThirdPartyCallArgs {
224 public final ComponentName component;
225 public final String callId;
226 public final String callerDisplayName;
227
228 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
229 String callerDisplayName) {
230 this.component = component;
231 this.callId = callId;
232 this.callerDisplayName = callerDisplayName;
233 }
234 }
235
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700236 /**
237 * A handler that processes messages on the main thread in the phone process. Since many
238 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
239 * inbound binder threads to the main thread in the phone process. The Binder thread
240 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
241 * on, which will be notified when the operation completes and will contain the result of the
242 * request.
243 *
244 * <p>If a MainThreadRequest object is provided in the msg.obj field,
245 * note that request.result must be set to something non-null for the calling thread to
246 * unblock.
247 */
248 private final class MainThreadHandler extends Handler {
249 @Override
250 public void handleMessage(Message msg) {
251 MainThreadRequest request;
252 Message onCompleted;
253 AsyncResult ar;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800254 UiccCard uiccCard;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700255 IccAPDUArgument iccArgument;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700256
257 switch (msg.what) {
Yorke Lee716f67e2015-06-17 15:39:16 -0700258 case CMD_HANDLE_PIN_MMI: {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700259 request = (MainThreadRequest) msg.obj;
Yorke Lee716f67e2015-06-17 15:39:16 -0700260 final Phone phone = getPhoneFromRequest(request);
261 request.result = phone != null ?
262 getPhoneFromRequest(request).handlePinMmi((String) request.argument)
263 : false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700264 // Wake up the requesting thread
265 synchronized (request) {
266 request.notifyAll();
267 }
268 break;
Yorke Lee716f67e2015-06-17 15:39:16 -0700269 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700270
271 case CMD_HANDLE_NEIGHBORING_CELL:
272 request = (MainThreadRequest) msg.obj;
273 onCompleted = obtainMessage(EVENT_NEIGHBORING_CELL_DONE,
274 request);
275 mPhone.getNeighboringCids(onCompleted);
276 break;
277
278 case EVENT_NEIGHBORING_CELL_DONE:
279 ar = (AsyncResult) msg.obj;
280 request = (MainThreadRequest) ar.userObj;
281 if (ar.exception == null && ar.result != null) {
282 request.result = ar.result;
283 } else {
284 // create an empty list to notify the waiting thread
Jake Hambye994d462014-02-03 13:10:13 -0800285 request.result = new ArrayList<NeighboringCellInfo>(0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700286 }
287 // Wake up the requesting thread
288 synchronized (request) {
289 request.notifyAll();
290 }
291 break;
292
293 case CMD_ANSWER_RINGING_CALL:
Wink Saville08874612014-08-31 19:19:58 -0700294 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800295 int answer_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700296 answerRingingCallInternal(answer_subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700297 break;
298
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700299 case CMD_END_CALL:
300 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800301 int end_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700302 final boolean hungUp;
Anthony Leeae4e36d2015-05-21 07:17:46 -0700303 Phone phone = getPhone(end_subId);
304 if (phone == null) {
305 if (DBG) log("CMD_END_CALL: no phone for id: " + end_subId);
306 break;
307 }
308 int phoneType = phone.getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700309 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
310 // CDMA: If the user presses the Power button we treat it as
311 // ending the complete call session
Wink Saville08874612014-08-31 19:19:58 -0700312 hungUp = PhoneUtils.hangupRingingAndActive(getPhone(end_subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700313 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
314 // GSM: End the call as per the Phone state
315 hungUp = PhoneUtils.hangup(mCM);
316 } else {
317 throw new IllegalStateException("Unexpected phone type: " + phoneType);
318 }
319 if (DBG) log("CMD_END_CALL: " + (hungUp ? "hung up!" : "no call to hang up"));
320 request.result = hungUp;
321 // Wake up the requesting thread
322 synchronized (request) {
323 request.notifyAll();
324 }
325 break;
326
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700327 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700328 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700329 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800330 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700331 if (uiccCard == null) {
332 loge("iccTransmitApduLogicalChannel: No UICC");
333 request.result = new IccIoResult(0x6F, 0, (byte[])null);
334 synchronized (request) {
335 request.notifyAll();
336 }
337 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700338 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
339 request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700340 uiccCard.iccTransmitApduLogicalChannel(
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700341 iccArgument.channel, iccArgument.cla, iccArgument.command,
342 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
Shishir Agrawal566b7612013-10-28 14:41:00 -0700343 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700344 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700345 break;
346
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700347 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700348 ar = (AsyncResult) msg.obj;
349 request = (MainThreadRequest) ar.userObj;
350 if (ar.exception == null && ar.result != null) {
351 request.result = ar.result;
352 } else {
353 request.result = new IccIoResult(0x6F, 0, (byte[])null);
354 if (ar.result == null) {
355 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800356 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700357 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800358 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700359 } else {
360 loge("iccTransmitApduLogicalChannel: Unknown exception");
361 }
362 }
363 synchronized (request) {
364 request.notifyAll();
365 }
366 break;
367
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700368 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
369 request = (MainThreadRequest) msg.obj;
370 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800371 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700372 if (uiccCard == null) {
373 loge("iccTransmitApduBasicChannel: No UICC");
374 request.result = new IccIoResult(0x6F, 0, (byte[])null);
375 synchronized (request) {
376 request.notifyAll();
377 }
378 } else {
379 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
380 request);
381 uiccCard.iccTransmitApduBasicChannel(
382 iccArgument.cla, iccArgument.command, iccArgument.p1, iccArgument.p2,
383 iccArgument.p3, iccArgument.data, onCompleted);
384 }
385 break;
386
387 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
388 ar = (AsyncResult) msg.obj;
389 request = (MainThreadRequest) ar.userObj;
390 if (ar.exception == null && ar.result != null) {
391 request.result = ar.result;
392 } else {
393 request.result = new IccIoResult(0x6F, 0, (byte[])null);
394 if (ar.result == null) {
395 loge("iccTransmitApduBasicChannel: Empty response");
396 } else if (ar.exception instanceof CommandException) {
397 loge("iccTransmitApduBasicChannel: CommandException: " +
398 ar.exception);
399 } else {
400 loge("iccTransmitApduBasicChannel: Unknown exception");
401 }
402 }
403 synchronized (request) {
404 request.notifyAll();
405 }
406 break;
407
408 case CMD_EXCHANGE_SIM_IO:
409 request = (MainThreadRequest) msg.obj;
410 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800411 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700412 if (uiccCard == null) {
413 loge("iccExchangeSimIO: No UICC");
414 request.result = new IccIoResult(0x6F, 0, (byte[])null);
415 synchronized (request) {
416 request.notifyAll();
417 }
418 } else {
419 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
420 request);
421 uiccCard.iccExchangeSimIO(iccArgument.cla, /* fileID */
422 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
423 iccArgument.data, onCompleted);
424 }
425 break;
426
427 case EVENT_EXCHANGE_SIM_IO_DONE:
428 ar = (AsyncResult) msg.obj;
429 request = (MainThreadRequest) ar.userObj;
430 if (ar.exception == null && ar.result != null) {
431 request.result = ar.result;
432 } else {
433 request.result = new IccIoResult(0x6f, 0, (byte[])null);
434 }
435 synchronized (request) {
436 request.notifyAll();
437 }
438 break;
439
Derek Tan4d5e5c12014-02-04 11:54:58 -0800440 case CMD_SEND_ENVELOPE:
441 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800442 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700443 if (uiccCard == null) {
444 loge("sendEnvelopeWithStatus: No UICC");
445 request.result = new IccIoResult(0x6F, 0, (byte[])null);
446 synchronized (request) {
447 request.notifyAll();
448 }
449 } else {
450 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
451 uiccCard.sendEnvelopeWithStatus((String)request.argument, onCompleted);
452 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800453 break;
454
455 case EVENT_SEND_ENVELOPE_DONE:
456 ar = (AsyncResult) msg.obj;
457 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700458 if (ar.exception == null && ar.result != null) {
459 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800460 } else {
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700461 request.result = new IccIoResult(0x6F, 0, (byte[])null);
462 if (ar.result == null) {
463 loge("sendEnvelopeWithStatus: Empty response");
464 } else if (ar.exception instanceof CommandException) {
465 loge("sendEnvelopeWithStatus: CommandException: " +
466 ar.exception);
467 } else {
468 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
469 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800470 }
471 synchronized (request) {
472 request.notifyAll();
473 }
474 break;
475
Shishir Agrawal566b7612013-10-28 14:41:00 -0700476 case CMD_OPEN_CHANNEL:
477 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800478 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700479 if (uiccCard == null) {
480 loge("iccOpenLogicalChannel: No UICC");
Shishir Agrawalfc0492a2016-02-17 11:15:33 -0800481 request.result = new IccOpenLogicalChannelResponse(-1,
482 IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE, null);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700483 synchronized (request) {
484 request.notifyAll();
485 }
486 } else {
487 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
488 uiccCard.iccOpenLogicalChannel((String)request.argument, onCompleted);
489 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700490 break;
491
492 case EVENT_OPEN_CHANNEL_DONE:
493 ar = (AsyncResult) msg.obj;
494 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700495 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700496 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700497 int[] result = (int[]) ar.result;
498 int channelId = result[0];
499 byte[] selectResponse = null;
500 if (result.length > 1) {
501 selectResponse = new byte[result.length - 1];
502 for (int i = 1; i < result.length; ++i) {
503 selectResponse[i - 1] = (byte) result[i];
504 }
505 }
506 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700507 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700508 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700509 if (ar.result == null) {
510 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700511 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700512 if (ar.exception != null) {
513 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
514 }
515
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700516 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Junda Liua754ba12015-05-20 01:17:52 -0700517 if (ar.exception instanceof CommandException) {
518 CommandException.Error error =
519 ((CommandException) (ar.exception)).getCommandError();
520 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700521 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700522 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700523 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700524 }
525 }
526 openChannelResp = new IccOpenLogicalChannelResponse(
527 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700528 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700529 request.result = openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700530 synchronized (request) {
531 request.notifyAll();
532 }
533 break;
534
535 case CMD_CLOSE_CHANNEL:
536 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800537 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700538 if (uiccCard == null) {
539 loge("iccCloseLogicalChannel: No UICC");
540 request.result = new IccIoResult(0x6F, 0, (byte[])null);
541 synchronized (request) {
542 request.notifyAll();
543 }
544 } else {
545 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
546 uiccCard.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
547 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700548 break;
549
550 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800551 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
552 break;
553
554 case CMD_NV_READ_ITEM:
555 request = (MainThreadRequest) msg.obj;
556 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
557 mPhone.nvReadItem((Integer) request.argument, onCompleted);
558 break;
559
560 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700561 ar = (AsyncResult) msg.obj;
562 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800563 if (ar.exception == null && ar.result != null) {
564 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700565 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800566 request.result = "";
567 if (ar.result == null) {
568 loge("nvReadItem: Empty response");
569 } else if (ar.exception instanceof CommandException) {
570 loge("nvReadItem: CommandException: " +
571 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700572 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800573 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700574 }
575 }
576 synchronized (request) {
577 request.notifyAll();
578 }
579 break;
580
Jake Hambye994d462014-02-03 13:10:13 -0800581 case CMD_NV_WRITE_ITEM:
582 request = (MainThreadRequest) msg.obj;
583 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
584 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
585 mPhone.nvWriteItem(idValue.first, idValue.second, onCompleted);
586 break;
587
588 case EVENT_NV_WRITE_ITEM_DONE:
589 handleNullReturnEvent(msg, "nvWriteItem");
590 break;
591
592 case CMD_NV_WRITE_CDMA_PRL:
593 request = (MainThreadRequest) msg.obj;
594 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
595 mPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
596 break;
597
598 case EVENT_NV_WRITE_CDMA_PRL_DONE:
599 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
600 break;
601
602 case CMD_NV_RESET_CONFIG:
603 request = (MainThreadRequest) msg.obj;
604 onCompleted = obtainMessage(EVENT_NV_RESET_CONFIG_DONE, request);
605 mPhone.nvResetConfig((Integer) request.argument, onCompleted);
606 break;
607
608 case EVENT_NV_RESET_CONFIG_DONE:
609 handleNullReturnEvent(msg, "nvResetConfig");
610 break;
611
Jake Hamby7c27be32014-03-03 13:25:59 -0800612 case CMD_GET_PREFERRED_NETWORK_TYPE:
613 request = (MainThreadRequest) msg.obj;
614 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
Stuart Scott54788802015-03-30 13:18:01 -0700615 getPhoneFromRequest(request).getPreferredNetworkType(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800616 break;
617
618 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
619 ar = (AsyncResult) msg.obj;
620 request = (MainThreadRequest) ar.userObj;
621 if (ar.exception == null && ar.result != null) {
622 request.result = ar.result; // Integer
623 } else {
Sanket Padawecfc2d352016-01-05 19:52:14 -0800624 request.result = null;
Jake Hamby7c27be32014-03-03 13:25:59 -0800625 if (ar.result == null) {
626 loge("getPreferredNetworkType: Empty response");
627 } else if (ar.exception instanceof CommandException) {
628 loge("getPreferredNetworkType: CommandException: " +
629 ar.exception);
630 } else {
631 loge("getPreferredNetworkType: Unknown exception");
632 }
633 }
634 synchronized (request) {
635 request.notifyAll();
636 }
637 break;
638
639 case CMD_SET_PREFERRED_NETWORK_TYPE:
640 request = (MainThreadRequest) msg.obj;
641 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
642 int networkType = (Integer) request.argument;
Stuart Scott54788802015-03-30 13:18:01 -0700643 getPhoneFromRequest(request).setPreferredNetworkType(networkType, onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800644 break;
645
646 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
647 handleNullReturnEvent(msg, "setPreferredNetworkType");
648 break;
649
Steven Liu4bf01bc2014-07-17 11:05:29 -0500650 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
651 request = (MainThreadRequest)msg.obj;
652 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
653 mPhone.invokeOemRilRequestRaw((byte[])request.argument, onCompleted);
654 break;
655
656 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
657 ar = (AsyncResult)msg.obj;
658 request = (MainThreadRequest)ar.userObj;
659 request.result = ar;
660 synchronized (request) {
661 request.notifyAll();
662 }
663 break;
664
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800665 case CMD_SET_VOICEMAIL_NUMBER:
666 request = (MainThreadRequest) msg.obj;
667 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
668 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -0800669 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
670 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800671 break;
672
673 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
674 handleNullReturnEvent(msg, "setVoicemailNumber");
675 break;
676
Stuart Scott54788802015-03-30 13:18:01 -0700677 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
678 request = (MainThreadRequest) msg.obj;
679 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
680 request);
681 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
682 break;
683
684 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
685 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
686 break;
687
Shishir Agrawal302c8692015-06-19 13:49:39 -0700688 case CMD_PERFORM_NETWORK_SCAN:
689 request = (MainThreadRequest) msg.obj;
690 onCompleted = obtainMessage(EVENT_PERFORM_NETWORK_SCAN_DONE, request);
691 getPhoneFromRequest(request).getAvailableNetworks(onCompleted);
692 break;
693
694 case EVENT_PERFORM_NETWORK_SCAN_DONE:
695 ar = (AsyncResult) msg.obj;
696 request = (MainThreadRequest) ar.userObj;
697 CellNetworkScanResult cellScanResult;
698 if (ar.exception == null && ar.result != null) {
699 cellScanResult = new CellNetworkScanResult(
700 CellNetworkScanResult.STATUS_SUCCESS,
701 (List<OperatorInfo>) ar.result);
702 } else {
703 if (ar.result == null) {
704 loge("getCellNetworkScanResults: Empty response");
705 }
706 if (ar.exception != null) {
707 loge("getCellNetworkScanResults: Exception: " + ar.exception);
708 }
709 int errorCode = CellNetworkScanResult.STATUS_UNKNOWN_ERROR;
710 if (ar.exception instanceof CommandException) {
711 CommandException.Error error =
712 ((CommandException) (ar.exception)).getCommandError();
713 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
714 errorCode = CellNetworkScanResult.STATUS_RADIO_NOT_AVAILABLE;
715 } else if (error == CommandException.Error.GENERIC_FAILURE) {
716 errorCode = CellNetworkScanResult.STATUS_RADIO_GENERIC_FAILURE;
717 }
718 }
719 cellScanResult = new CellNetworkScanResult(errorCode, null);
720 }
721 request.result = cellScanResult;
722 synchronized (request) {
723 request.notifyAll();
724 }
725 break;
726
727 case CMD_SET_NETWORK_SELECTION_MODE_MANUAL:
728 request = (MainThreadRequest) msg.obj;
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700729 ManualNetworkSelectionArgument selArg =
730 (ManualNetworkSelectionArgument) request.argument;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700731 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE,
732 request);
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700733 getPhoneFromRequest(request).selectNetworkManually(selArg.operatorInfo,
734 selArg.persistSelection, onCompleted);
Shishir Agrawal302c8692015-06-19 13:49:39 -0700735 break;
736
737 case EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE:
738 handleNullReturnEvent(msg, "setNetworkSelectionModeManual");
739 break;
740
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700741 case CMD_GET_MODEM_ACTIVITY_INFO:
742 request = (MainThreadRequest) msg.obj;
743 onCompleted = obtainMessage(EVENT_GET_MODEM_ACTIVITY_INFO_DONE, request);
Prerepa Viswanadham61a60ad2015-06-08 18:07:51 -0700744 mPhone.getModemActivityInfo(onCompleted);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700745 break;
746
747 case EVENT_GET_MODEM_ACTIVITY_INFO_DONE:
748 ar = (AsyncResult) msg.obj;
749 request = (MainThreadRequest) ar.userObj;
750 if (ar.exception == null && ar.result != null) {
751 request.result = ar.result;
752 } else {
753 if (ar.result == null) {
754 loge("queryModemActivityInfo: Empty response");
755 } else if (ar.exception instanceof CommandException) {
756 loge("queryModemActivityInfo: CommandException: " +
757 ar.exception);
758 } else {
759 loge("queryModemActivityInfo: Unknown exception");
760 }
761 }
Amit Mahajand4766222016-01-28 15:28:28 -0800762 // Result cannot be null. Return ModemActivityInfo with all fields set to 0.
763 if (request.result == null) {
764 request.result = new ModemActivityInfo(0, 0, 0, null, 0, 0);
765 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700766 synchronized (request) {
767 request.notifyAll();
768 }
769 break;
770
Meng Wang1a7c35a2016-05-05 20:56:15 -0700771 case CMD_SET_ALLOWED_CARRIERS:
772 request = (MainThreadRequest) msg.obj;
773 onCompleted = obtainMessage(EVENT_SET_ALLOWED_CARRIERS_DONE, request);
774 mPhone.setAllowedCarriers(
775 (List<CarrierIdentifier>) request.argument,
776 onCompleted);
777 break;
778
779 case EVENT_SET_ALLOWED_CARRIERS_DONE:
780 ar = (AsyncResult) msg.obj;
781 request = (MainThreadRequest) ar.userObj;
782 if (ar.exception == null && ar.result != null) {
783 request.result = ar.result;
784 } else {
785 if (ar.result == null) {
786 loge("setAllowedCarriers: Empty response");
787 } else if (ar.exception instanceof CommandException) {
788 loge("setAllowedCarriers: CommandException: " +
789 ar.exception);
790 } else {
791 loge("setAllowedCarriers: Unknown exception");
792 }
793 }
794 // Result cannot be null. Return -1 on error.
795 if (request.result == null) {
796 request.result = new int[]{-1};
797 }
798 synchronized (request) {
799 request.notifyAll();
800 }
801 break;
802
803 case CMD_GET_ALLOWED_CARRIERS:
804 request = (MainThreadRequest) msg.obj;
805 onCompleted = obtainMessage(EVENT_GET_ALLOWED_CARRIERS_DONE, request);
806 mPhone.getAllowedCarriers(onCompleted);
807 break;
808
809 case EVENT_GET_ALLOWED_CARRIERS_DONE:
810 ar = (AsyncResult) msg.obj;
811 request = (MainThreadRequest) ar.userObj;
812 if (ar.exception == null && ar.result != null) {
813 request.result = ar.result;
814 } else {
815 if (ar.result == null) {
816 loge("getAllowedCarriers: Empty response");
817 } else if (ar.exception instanceof CommandException) {
818 loge("getAllowedCarriers: CommandException: " +
819 ar.exception);
820 } else {
821 loge("getAllowedCarriers: Unknown exception");
822 }
823 }
824 // Result cannot be null. Return empty list of CarrierIdentifier.
825 if (request.result == null) {
826 request.result = new ArrayList<CarrierIdentifier>(0);
827 }
828 synchronized (request) {
829 request.notifyAll();
830 }
831 break;
832
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700833 default:
834 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
835 break;
836 }
837 }
Jake Hambye994d462014-02-03 13:10:13 -0800838
839 private void handleNullReturnEvent(Message msg, String command) {
840 AsyncResult ar = (AsyncResult) msg.obj;
841 MainThreadRequest request = (MainThreadRequest) ar.userObj;
842 if (ar.exception == null) {
843 request.result = true;
844 } else {
845 request.result = false;
846 if (ar.exception instanceof CommandException) {
847 loge(command + ": CommandException: " + ar.exception);
848 } else {
849 loge(command + ": Unknown exception");
850 }
851 }
852 synchronized (request) {
853 request.notifyAll();
854 }
855 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700856 }
857
858 /**
859 * Posts the specified command to be executed on the main thread,
860 * waits for the request to complete, and returns the result.
861 * @see #sendRequestAsync
862 */
863 private Object sendRequest(int command, Object argument) {
Sanket Padawe56e75a32016-02-08 12:18:19 -0800864 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Wink Saville36469e72014-06-11 15:17:00 -0700865 }
866
867 /**
868 * Posts the specified command to be executed on the main thread,
869 * waits for the request to complete, and returns the result.
870 * @see #sendRequestAsync
871 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800872 private Object sendRequest(int command, Object argument, Integer subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700873 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
874 throw new RuntimeException("This method will deadlock if called from the main thread.");
875 }
876
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800877 MainThreadRequest request = new MainThreadRequest(argument, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700878 Message msg = mMainThreadHandler.obtainMessage(command, request);
879 msg.sendToTarget();
880
881 // Wait for the request to complete
882 synchronized (request) {
883 while (request.result == null) {
884 try {
885 request.wait();
886 } catch (InterruptedException e) {
887 // Do nothing, go back and wait until the request is complete
888 }
889 }
890 }
891 return request.result;
892 }
893
894 /**
895 * Asynchronous ("fire and forget") version of sendRequest():
896 * Posts the specified command to be executed on the main thread, and
897 * returns immediately.
898 * @see #sendRequest
899 */
900 private void sendRequestAsync(int command) {
901 mMainThreadHandler.sendEmptyMessage(command);
902 }
903
904 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -0700905 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
906 * @see {@link #sendRequest(int,Object)}
907 */
908 private void sendRequestAsync(int command, Object argument) {
909 MainThreadRequest request = new MainThreadRequest(argument);
910 Message msg = mMainThreadHandler.obtainMessage(command, request);
911 msg.sendToTarget();
912 }
913
914 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700915 * Initialize the singleton PhoneInterfaceManager instance.
916 * This is only done once, at startup, from PhoneApp.onCreate().
917 */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700918 /* package */ static PhoneInterfaceManager init(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700919 synchronized (PhoneInterfaceManager.class) {
920 if (sInstance == null) {
Sailesh Nepal194161e2014-07-03 08:57:44 -0700921 sInstance = new PhoneInterfaceManager(app, phone);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700922 } else {
923 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
924 }
925 return sInstance;
926 }
927 }
928
929 /** Private constructor; @see init() */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700930 private PhoneInterfaceManager(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700931 mApp = app;
932 mPhone = phone;
933 mCM = PhoneGlobals.getInstance().mCM;
Stuart Scott981d8582015-04-21 14:09:50 -0700934 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700935 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
936 mMainThreadHandler = new MainThreadHandler();
Andrew Leedf14ead2014-10-17 14:22:52 -0700937 mTelephonySharedPreferences =
Derek Tan97ebb422014-09-05 16:55:38 -0700938 PreferenceManager.getDefaultSharedPreferences(mPhone.getContext());
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800939 mSubscriptionController = SubscriptionController.getInstance();
Wink Saville3ab207e2014-11-20 13:07:20 -0800940
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700941 publish();
942 }
943
944 private void publish() {
945 if (DBG) log("publish: " + this);
946
947 ServiceManager.addService("phone", this);
948 }
949
Stuart Scott584921c2015-01-15 17:10:34 -0800950 private Phone getPhoneFromRequest(MainThreadRequest request) {
Sanket Padawe56e75a32016-02-08 12:18:19 -0800951 return (request.subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID)
952 ? mPhone : getPhone(request.subId);
Stuart Scott584921c2015-01-15 17:10:34 -0800953 }
954
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800955 private UiccCard getUiccCardFromRequest(MainThreadRequest request) {
956 Phone phone = getPhoneFromRequest(request);
957 return phone == null ? null :
958 UiccController.getInstance().getUiccCard(phone.getPhoneId());
959 }
960
Wink Saville36469e72014-06-11 15:17:00 -0700961 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -0700962 private Phone getPhone(int subId) {
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800963 return PhoneFactory.getPhone(mSubscriptionController.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -0700964 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700965 //
966 // Implementation of the ITelephony interface.
967 //
968
969 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700970 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -0700971 }
972
Wink Savilleb564aae2014-10-23 10:18:09 -0700973 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700974 if (DBG) log("dial: " + number);
975 // No permission check needed here: This is just a wrapper around the
976 // ACTION_DIAL intent, which is available to any app since it puts up
977 // the UI before it does anything.
978
979 String url = createTelUrl(number);
980 if (url == null) {
981 return;
982 }
983
984 // PENDING: should we just silently fail if phone is offhook or ringing?
Wink Saville36469e72014-06-11 15:17:00 -0700985 PhoneConstants.State state = mCM.getState(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700986 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
987 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
988 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
989 mApp.startActivity(intent);
990 }
991 }
992
993 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700994 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -0700995 }
996
Wink Savilleb564aae2014-10-23 10:18:09 -0700997 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700998 if (DBG) log("call: " + number);
999
1000 // This is just a wrapper around the ACTION_CALL intent, but we still
1001 // need to do a permission check since we're calling startActivity()
1002 // from the context of the phone app.
1003 enforceCallPermission();
1004
1005 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
1006 != AppOpsManager.MODE_ALLOWED) {
1007 return;
1008 }
1009
1010 String url = createTelUrl(number);
1011 if (url == null) {
1012 return;
1013 }
1014
Wink Saville08874612014-08-31 19:19:58 -07001015 boolean isValid = false;
Narayan Kamath1c496c22015-04-16 14:40:19 +01001016 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoList();
Wink Saville3ab207e2014-11-20 13:07:20 -08001017 if (slist != null) {
1018 for (SubscriptionInfo subInfoRecord : slist) {
1019 if (subInfoRecord.getSubscriptionId() == subId) {
1020 isValid = true;
1021 break;
1022 }
Wink Saville08874612014-08-31 19:19:58 -07001023 }
1024 }
1025 if (isValid == false) {
1026 return;
1027 }
1028
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001029 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
Wink Saville36469e72014-06-11 15:17:00 -07001030 intent.putExtra(SUBSCRIPTION_KEY, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001031 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1032 mApp.startActivity(intent);
1033 }
1034
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001035 /**
1036 * End a call based on call state
1037 * @return true is a call was ended
1038 */
1039 public boolean endCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001040 return endCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001041 }
1042
1043 /**
1044 * End a call based on the call state of the subId
1045 * @return true is a call was ended
1046 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001047 public boolean endCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001048 enforceCallPermission();
Stuart Scott584921c2015-01-15 17:10:34 -08001049 return (Boolean) sendRequest(CMD_END_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001050 }
1051
1052 public void answerRingingCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001053 answerRingingCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001054 }
1055
Wink Savilleb564aae2014-10-23 10:18:09 -07001056 public void answerRingingCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001057 if (DBG) log("answerRingingCall...");
1058 // TODO: there should eventually be a separate "ANSWER_PHONE" permission,
1059 // but that can probably wait till the big TelephonyManager API overhaul.
1060 // For now, protect this call with the MODIFY_PHONE_STATE permission.
1061 enforceModifyPermission();
Stuart Scott584921c2015-01-15 17:10:34 -08001062 sendRequest(CMD_ANSWER_RINGING_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001063 }
1064
1065 /**
1066 * Make the actual telephony calls to implement answerRingingCall().
1067 * This should only be called from the main thread of the Phone app.
1068 * @see #answerRingingCall
1069 *
1070 * TODO: it would be nice to return true if we answered the call, or
1071 * false if there wasn't actually a ringing incoming call, or some
1072 * other error occurred. (In other words, pass back the return value
1073 * from PhoneUtils.answerCall() or PhoneUtils.answerAndEndActive().)
1074 * But that would require calling this method via sendRequest() rather
1075 * than sendRequestAsync(), and right now we don't actually *need* that
1076 * return value, so let's just return void for now.
1077 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001078 private void answerRingingCallInternal(int subId) {
Wink Saville08874612014-08-31 19:19:58 -07001079 final boolean hasRingingCall = !getPhone(subId).getRingingCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001080 if (hasRingingCall) {
Wink Saville08874612014-08-31 19:19:58 -07001081 final boolean hasActiveCall = !getPhone(subId).getForegroundCall().isIdle();
1082 final boolean hasHoldingCall = !getPhone(subId).getBackgroundCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001083 if (hasActiveCall && hasHoldingCall) {
1084 // Both lines are in use!
1085 // TODO: provide a flag to let the caller specify what
1086 // policy to use if both lines are in use. (The current
1087 // behavior is hardwired to "answer incoming, end ongoing",
1088 // which is how the CALL button is specced to behave.)
1089 PhoneUtils.answerAndEndActive(mCM, mCM.getFirstActiveRingingCall());
1090 return;
1091 } else {
1092 // answerCall() will automatically hold the current active
1093 // call, if there is one.
1094 PhoneUtils.answerCall(mCM.getFirstActiveRingingCall());
1095 return;
1096 }
1097 } else {
1098 // No call was ringing.
1099 return;
1100 }
1101 }
1102
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001103 /**
Santos Cordon5422a8d2014-09-12 04:20:56 -07001104 * This method is no longer used and can be removed once TelephonyManager stops referring to it.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001105 */
Santos Cordon5422a8d2014-09-12 04:20:56 -07001106 public void silenceRinger() {
1107 Log.e(LOG_TAG, "silenseRinger not supported");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001108 }
1109
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001110 @Override
1111 public boolean isOffhook(String callingPackage) {
1112 return isOffhookForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001113 }
1114
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001115 @Override
1116 public boolean isOffhookForSubscriber(int subId, String callingPackage) {
1117 if (!canReadPhoneState(callingPackage, "isOffhookForSubscriber")) {
1118 return false;
1119 }
1120
Sanket Padawe356d7632015-06-22 14:03:32 -07001121 final Phone phone = getPhone(subId);
1122 if (phone != null) {
1123 return (phone.getState() == PhoneConstants.State.OFFHOOK);
1124 } else {
1125 return false;
1126 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001127 }
1128
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001129 @Override
1130 public boolean isRinging(String callingPackage) {
1131 return (isRingingForSubscriber(getDefaultSubscription(), callingPackage));
Wink Saville36469e72014-06-11 15:17:00 -07001132 }
1133
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001134 @Override
1135 public boolean isRingingForSubscriber(int subId, String callingPackage) {
1136 if (!canReadPhoneState(callingPackage, "isRingingForSubscriber")) {
1137 return false;
1138 }
1139
Sanket Padawe356d7632015-06-22 14:03:32 -07001140 final Phone phone = getPhone(subId);
1141 if (phone != null) {
1142 return (phone.getState() == PhoneConstants.State.RINGING);
1143 } else {
1144 return false;
1145 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001146 }
1147
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001148 @Override
1149 public boolean isIdle(String callingPackage) {
1150 return isIdleForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001151 }
1152
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001153 @Override
1154 public boolean isIdleForSubscriber(int subId, String callingPackage) {
1155 if (!canReadPhoneState(callingPackage, "isIdleForSubscriber")) {
1156 return false;
1157 }
1158
Sanket Padawe356d7632015-06-22 14:03:32 -07001159 final Phone phone = getPhone(subId);
1160 if (phone != null) {
1161 return (phone.getState() == PhoneConstants.State.IDLE);
1162 } else {
1163 return false;
1164 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001165 }
1166
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001167 public boolean supplyPin(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001168 return supplyPinForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001169 }
1170
Wink Savilleb564aae2014-10-23 10:18:09 -07001171 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001172 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001173 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1174 }
1175
1176 public boolean supplyPuk(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001177 return supplyPukForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001178 }
1179
Wink Savilleb564aae2014-10-23 10:18:09 -07001180 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001181 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001182 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1183 }
1184
1185 /** {@hide} */
1186 public int[] supplyPinReportResult(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001187 return supplyPinReportResultForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001188 }
1189
Wink Savilleb564aae2014-10-23 10:18:09 -07001190 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001191 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001192 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001193 checkSimPin.start();
1194 return checkSimPin.unlockSim(null, pin);
1195 }
1196
Wink Saville9de0f752013-10-22 19:04:03 -07001197 /** {@hide} */
1198 public int[] supplyPukReportResult(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001199 return supplyPukReportResultForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001200 }
1201
Wink Savilleb564aae2014-10-23 10:18:09 -07001202 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001203 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001204 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001205 checkSimPuk.start();
1206 return checkSimPuk.unlockSim(puk, pin);
1207 }
1208
1209 /**
Wink Saville9de0f752013-10-22 19:04:03 -07001210 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001211 * a synchronous one.
1212 */
1213 private static class UnlockSim extends Thread {
1214
1215 private final IccCard mSimCard;
1216
1217 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07001218 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1219 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001220
1221 // For replies from SimCard interface
1222 private Handler mHandler;
1223
1224 // For async handler to identify request type
1225 private static final int SUPPLY_PIN_COMPLETE = 100;
1226
1227 public UnlockSim(IccCard simCard) {
1228 mSimCard = simCard;
1229 }
1230
1231 @Override
1232 public void run() {
1233 Looper.prepare();
1234 synchronized (UnlockSim.this) {
1235 mHandler = new Handler() {
1236 @Override
1237 public void handleMessage(Message msg) {
1238 AsyncResult ar = (AsyncResult) msg.obj;
1239 switch (msg.what) {
1240 case SUPPLY_PIN_COMPLETE:
1241 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
1242 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07001243 mRetryCount = msg.arg1;
1244 if (ar.exception != null) {
1245 if (ar.exception instanceof CommandException &&
1246 ((CommandException)(ar.exception)).getCommandError()
1247 == CommandException.Error.PASSWORD_INCORRECT) {
1248 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
1249 } else {
1250 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1251 }
1252 } else {
1253 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
1254 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001255 mDone = true;
1256 UnlockSim.this.notifyAll();
1257 }
1258 break;
1259 }
1260 }
1261 };
1262 UnlockSim.this.notifyAll();
1263 }
1264 Looper.loop();
1265 }
1266
1267 /*
1268 * Use PIN or PUK to unlock SIM card
1269 *
1270 * If PUK is null, unlock SIM card with PIN
1271 *
1272 * If PUK is not null, unlock SIM card with PUK and set PIN code
1273 */
Wink Saville9de0f752013-10-22 19:04:03 -07001274 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001275
1276 while (mHandler == null) {
1277 try {
1278 wait();
1279 } catch (InterruptedException e) {
1280 Thread.currentThread().interrupt();
1281 }
1282 }
1283 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
1284
1285 if (puk == null) {
1286 mSimCard.supplyPin(pin, callback);
1287 } else {
1288 mSimCard.supplyPuk(puk, pin, callback);
1289 }
1290
1291 while (!mDone) {
1292 try {
1293 Log.d(LOG_TAG, "wait for done");
1294 wait();
1295 } catch (InterruptedException e) {
1296 // Restore the interrupted status
1297 Thread.currentThread().interrupt();
1298 }
1299 }
1300 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07001301 int[] resultArray = new int[2];
1302 resultArray[0] = mResult;
1303 resultArray[1] = mRetryCount;
1304 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001305 }
1306 }
1307
1308 public void updateServiceLocation() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001309 updateServiceLocationForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001310
1311 }
1312
Wink Savilleb564aae2014-10-23 10:18:09 -07001313 public void updateServiceLocationForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001314 // No permission check needed here: this call is harmless, and it's
1315 // needed for the ServiceState.requestStateUpdate() call (which is
1316 // already intentionally exposed to 3rd parties.)
Sanket Padawe356d7632015-06-22 14:03:32 -07001317 final Phone phone = getPhone(subId);
1318 if (phone != null) {
1319 phone.updateServiceLocation();
1320 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001321 }
1322
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001323 @Override
1324 public boolean isRadioOn(String callingPackage) {
1325 return isRadioOnForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001326 }
1327
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001328 @Override
1329 public boolean isRadioOnForSubscriber(int subId, String callingPackage) {
1330 if (!canReadPhoneState(callingPackage, "isRadioOnForSubscriber")) {
1331 return false;
1332 }
1333 return isRadioOnForSubscriber(subId);
1334 }
1335
1336 private boolean isRadioOnForSubscriber(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001337 final Phone phone = getPhone(subId);
1338 if (phone != null) {
1339 return phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF;
1340 } else {
1341 return false;
1342 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001343 }
1344
1345 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001346 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001347
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001348 }
Wink Saville36469e72014-06-11 15:17:00 -07001349
Wink Savilleb564aae2014-10-23 10:18:09 -07001350 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001351 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001352 final Phone phone = getPhone(subId);
1353 if (phone != null) {
1354 phone.setRadioPower(!isRadioOnForSubscriber(subId));
1355 }
Wink Saville36469e72014-06-11 15:17:00 -07001356 }
1357
1358 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001359 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001360 }
1361
Wink Savilleb564aae2014-10-23 10:18:09 -07001362 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07001363 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001364 final Phone phone = getPhone(subId);
1365 if (phone == null) {
1366 return false;
1367 }
1368 if ((phone.getServiceState().getState() !=
Wink Saville36469e72014-06-11 15:17:00 -07001369 ServiceState.STATE_POWER_OFF) != turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001370 toggleRadioOnOffForSubscriber(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001371 }
1372 return true;
1373 }
Wink Saville36469e72014-06-11 15:17:00 -07001374
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001375 public boolean needMobileRadioShutdown() {
1376 /*
1377 * If any of the Radios are available, it will need to be
1378 * shutdown. So return true if any Radio is available.
1379 */
1380 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1381 Phone phone = PhoneFactory.getPhone(i);
1382 if (phone != null && phone.isRadioAvailable()) return true;
1383 }
1384 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
1385 return false;
1386 }
1387
1388 public void shutdownMobileRadios() {
1389 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1390 logv("Shutting down Phone " + i);
1391 shutdownRadioUsingPhoneId(i);
1392 }
1393 }
1394
1395 private void shutdownRadioUsingPhoneId(int phoneId) {
1396 enforceModifyPermission();
1397 Phone phone = PhoneFactory.getPhone(phoneId);
1398 if (phone != null && phone.isRadioAvailable()) {
1399 phone.shutdownRadio();
1400 }
1401 }
1402
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001403 public boolean setRadioPower(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001404 return setRadioPowerForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001405 }
1406
Wink Savilleb564aae2014-10-23 10:18:09 -07001407 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001408 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001409 final Phone phone = getPhone(subId);
1410 if (phone != null) {
1411 phone.setRadioPower(turnOn);
1412 return true;
1413 } else {
1414 return false;
1415 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001416 }
1417
Wink Saville36469e72014-06-11 15:17:00 -07001418 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001419 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001420 public boolean enableDataConnectivity() {
1421 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001422 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001423 final Phone phone = getPhone(subId);
1424 if (phone != null) {
1425 phone.setDataEnabled(true);
1426 return true;
1427 } else {
1428 return false;
1429 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001430 }
1431
Wink Saville36469e72014-06-11 15:17:00 -07001432 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001433 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001434 public boolean disableDataConnectivity() {
1435 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001436 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001437 final Phone phone = getPhone(subId);
1438 if (phone != null) {
1439 phone.setDataEnabled(false);
1440 return true;
1441 } else {
1442 return false;
1443 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001444 }
1445
Wink Saville36469e72014-06-11 15:17:00 -07001446 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001447 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001448 public boolean isDataConnectivityPossible() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001449 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001450 final Phone phone = getPhone(subId);
1451 if (phone != null) {
1452 return phone.isDataConnectivityPossible();
1453 } else {
1454 return false;
1455 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001456 }
1457
1458 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001459 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07001460 }
1461
Wink Savilleb564aae2014-10-23 10:18:09 -07001462 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001463 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001464 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1465 return false;
1466 }
Wink Saville36469e72014-06-11 15:17:00 -07001467 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001468 }
1469
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001470 public int getCallState() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001471 return getCallStateForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001472 }
1473
Shishir Agrawala9f32182016-04-12 12:00:16 -07001474 public int getCallStateForSlot(int slotId) {
1475 Phone phone = PhoneFactory.getPhone(slotId);
1476 return phone == null ? TelephonyManager.CALL_STATE_IDLE :
1477 DefaultPhoneNotifier.convertCallState(phone.getState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001478 }
1479
Sanket Padawe356d7632015-06-22 14:03:32 -07001480 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001481 public int getDataState() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001482 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001483 if (phone != null) {
1484 return DefaultPhoneNotifier.convertDataState(phone.getDataConnectionState());
1485 } else {
1486 return DefaultPhoneNotifier.convertDataState(PhoneConstants.DataState.DISCONNECTED);
1487 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001488 }
1489
Sanket Padawe356d7632015-06-22 14:03:32 -07001490 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001491 public int getDataActivity() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001492 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001493 if (phone != null) {
1494 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
1495 } else {
1496 return TelephonyManager.DATA_ACTIVITY_NONE;
1497 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001498 }
1499
1500 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001501 public Bundle getCellLocation(String callingPackage) {
1502 enforceFineOrCoarseLocationPermission("getCellLocation");
1503
1504 // OP_COARSE_LOCATION controls both fine and coarse location.
1505 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1506 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001507 log("getCellLocation: returning null; mode != allowed");
Svetoslav64fad262015-04-14 14:35:21 -07001508 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001509 }
1510
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001511 if (checkIfCallerIsSelfOrForegroundUser() ||
1512 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001513 if (DBG_LOC) log("getCellLocation: is active user");
1514 Bundle data = new Bundle();
Legler Wu2c01cdf2014-12-08 19:00:59 +08001515 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001516 if (phone == null) {
1517 return null;
1518 }
Legler Wu2c01cdf2014-12-08 19:00:59 +08001519 phone.getCellLocation().fillInNotifierBundle(data);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001520 return data;
1521 } else {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001522 log("getCellLocation: suppress non-active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001523 return null;
1524 }
1525 }
1526
Svetoslav64fad262015-04-14 14:35:21 -07001527 private void enforceFineOrCoarseLocationPermission(String message) {
1528 try {
1529 mApp.enforceCallingOrSelfPermission(
1530 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1531 } catch (SecurityException e) {
1532 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1533 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1534 // is the weaker precondition
1535 mApp.enforceCallingOrSelfPermission(
1536 android.Manifest.permission.ACCESS_COARSE_LOCATION, message);
1537 }
1538 }
1539
1540
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001541 @Override
1542 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001543 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001544 }
1545
Sanket Padawe356d7632015-06-22 14:03:32 -07001546 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001547 public void enableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001548 mApp.enforceCallingOrSelfPermission(
1549 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001550 final Phone phone = getPhone(subId);
1551 if (phone != null) {
1552 phone.enableLocationUpdates();
1553 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001554 }
1555
1556 @Override
1557 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001558 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001559 }
1560
Sanket Padawe356d7632015-06-22 14:03:32 -07001561 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001562 public void disableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001563 mApp.enforceCallingOrSelfPermission(
1564 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001565 final Phone phone = getPhone(subId);
1566 if (phone != null) {
1567 phone.disableLocationUpdates();
1568 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001569 }
1570
1571 @Override
1572 @SuppressWarnings("unchecked")
1573 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
Svetoslav64fad262015-04-14 14:35:21 -07001574 enforceFineOrCoarseLocationPermission("getNeighboringCellInfo");
1575
1576 // OP_COARSE_LOCATION controls both fine and coarse location.
1577 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1578 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1579 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001580 }
1581
1582 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
1583 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1584 return null;
1585 }
Svetoslav64fad262015-04-14 14:35:21 -07001586
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001587 if (checkIfCallerIsSelfOrForegroundUser() ||
1588 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001589 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
1590
1591 ArrayList<NeighboringCellInfo> cells = null;
1592
1593 try {
1594 cells = (ArrayList<NeighboringCellInfo>) sendRequest(
Sanket Padawe56e75a32016-02-08 12:18:19 -08001595 CMD_HANDLE_NEIGHBORING_CELL, null,
1596 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001597 } catch (RuntimeException e) {
Wink Saville36469e72014-06-11 15:17:00 -07001598 Log.e(LOG_TAG, "getNeighboringCellInfo " + e);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001599 }
1600 return cells;
1601 } else {
1602 if (DBG_LOC) log("getNeighboringCellInfo: suppress non-active user");
1603 return null;
1604 }
1605 }
1606
1607
1608 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001609 public List<CellInfo> getAllCellInfo(String callingPackage) {
1610 enforceFineOrCoarseLocationPermission("getAllCellInfo");
1611
1612 // OP_COARSE_LOCATION controls both fine and coarse location.
1613 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1614 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1615 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001616 }
1617
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001618 if (checkIfCallerIsSelfOrForegroundUser() ||
1619 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001620 if (DBG_LOC) log("getAllCellInfo: is active user");
Legler Wu2c01cdf2014-12-08 19:00:59 +08001621 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
1622 for (Phone phone : PhoneFactory.getPhones()) {
Robert Greenwaltd9eb7da2015-08-19 12:18:12 -07001623 final List<CellInfo> info = phone.getAllCellInfo();
1624 if (info != null) cellInfos.addAll(phone.getAllCellInfo());
Legler Wu2c01cdf2014-12-08 19:00:59 +08001625 }
1626 return cellInfos;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001627 } else {
1628 if (DBG_LOC) log("getAllCellInfo: suppress non-active user");
1629 return null;
1630 }
1631 }
1632
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001633 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001634 public void setCellInfoListRate(int rateInMillis) {
1635 mPhone.setCellInfoListRate(rateInMillis);
1636 }
1637
Shishir Agrawala9f32182016-04-12 12:00:16 -07001638 @Override
1639 public String getImeiForSlot(int slotId, String callingPackage) {
1640 if (!canReadPhoneState(callingPackage, "getImeiForSlot")) {
1641 return null;
1642 }
1643 Phone phone = PhoneFactory.getPhone(slotId);
1644 return phone == null ? null : phone.getImei();
1645 }
1646
1647 @Override
1648 public String getDeviceSoftwareVersionForSlot(int slotId, String callingPackage) {
1649 if (!canReadPhoneState(callingPackage, "getDeviceSoftwareVersionForSlot")) {
1650 return null;
1651 }
1652 Phone phone = PhoneFactory.getPhone(slotId);
1653 return phone == null ? null : phone.getDeviceSvn();
1654 }
1655
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001656 //
1657 // Internal helper methods.
1658 //
1659
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001660 /**
1661 * Returns true if the caller holds INTERACT_ACROSS_USERS_FULL.
1662 */
1663 private boolean checkCallerInteractAcrossUsersFull() {
1664 return mPhone.getContext().checkCallingOrSelfPermission(
1665 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1666 == PackageManager.PERMISSION_GRANTED;
1667 }
1668
Jake Hambye994d462014-02-03 13:10:13 -08001669 private static boolean checkIfCallerIsSelfOrForegroundUser() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001670 boolean ok;
1671
1672 boolean self = Binder.getCallingUid() == Process.myUid();
1673 if (!self) {
1674 // Get the caller's user id then clear the calling identity
1675 // which will be restored in the finally clause.
1676 int callingUser = UserHandle.getCallingUserId();
1677 long ident = Binder.clearCallingIdentity();
1678
1679 try {
1680 // With calling identity cleared the current user is the foreground user.
1681 int foregroundUser = ActivityManager.getCurrentUser();
1682 ok = (foregroundUser == callingUser);
1683 if (DBG_LOC) {
1684 log("checkIfCallerIsSelfOrForegoundUser: foregroundUser=" + foregroundUser
1685 + " callingUser=" + callingUser + " ok=" + ok);
1686 }
1687 } catch (Exception ex) {
1688 if (DBG_LOC) loge("checkIfCallerIsSelfOrForegoundUser: Exception ex=" + ex);
1689 ok = false;
1690 } finally {
1691 Binder.restoreCallingIdentity(ident);
1692 }
1693 } else {
1694 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: is self");
1695 ok = true;
1696 }
1697 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: ret=" + ok);
1698 return ok;
1699 }
1700
1701 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001702 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1703 *
1704 * @throws SecurityException if the caller does not have the required permission
1705 */
1706 private void enforceModifyPermission() {
1707 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1708 }
1709
1710 /**
Junda Liua2e36012014-07-09 18:30:01 -07001711 * Make sure either system app or the caller has carrier privilege.
1712 *
1713 * @throws SecurityException if the caller does not have the required permission/privilege
1714 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001715 private void enforceModifyPermissionOrCarrierPrivilege(int subId) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001716 int permission = mApp.checkCallingOrSelfPermission(
1717 android.Manifest.permission.MODIFY_PHONE_STATE);
1718 if (permission == PackageManager.PERMISSION_GRANTED) {
1719 return;
1720 }
1721
1722 log("No modify permission, check carrier privilege next.");
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001723 enforceCarrierPrivilege(subId);
Junda Liua2e36012014-07-09 18:30:01 -07001724 }
1725
1726 /**
1727 * Make sure the caller has carrier privilege.
1728 *
1729 * @throws SecurityException if the caller does not have the required permission
1730 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001731 private void enforceCarrierPrivilege(int subId) {
1732 if (getCarrierPrivilegeStatus(subId) !=
1733 TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001734 loge("No Carrier Privilege.");
1735 throw new SecurityException("No Carrier Privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001736 }
1737 }
1738
1739 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001740 * Make sure the caller has the CALL_PHONE permission.
1741 *
1742 * @throws SecurityException if the caller does not have the required permission
1743 */
1744 private void enforceCallPermission() {
1745 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1746 }
1747
Stuart Scott8eef64f2015-04-08 15:13:54 -07001748 private void enforceConnectivityInternalPermission() {
1749 mApp.enforceCallingOrSelfPermission(
1750 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1751 "ConnectivityService");
1752 }
1753
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001754 private String createTelUrl(String number) {
1755 if (TextUtils.isEmpty(number)) {
1756 return null;
1757 }
1758
Jake Hambye994d462014-02-03 13:10:13 -08001759 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001760 }
1761
Ihab Awadf9e92732013-12-05 18:02:52 -08001762 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001763 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1764 }
1765
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001766 private static void logv(String msg) {
1767 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
1768 }
1769
Ihab Awadf9e92732013-12-05 18:02:52 -08001770 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001771 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1772 }
1773
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001774 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001775 public int getActivePhoneType() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001776 return getActivePhoneTypeForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001777 }
1778
Sanket Padawe356d7632015-06-22 14:03:32 -07001779 @Override
Shishir Agrawala9f32182016-04-12 12:00:16 -07001780 public int getActivePhoneTypeForSlot(int slotId) {
1781 final Phone phone = PhoneFactory.getPhone(slotId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001782 if (phone == null) {
1783 return PhoneConstants.PHONE_TYPE_NONE;
1784 } else {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001785 return phone.getPhoneType();
Sanket Padawe356d7632015-06-22 14:03:32 -07001786 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001787 }
1788
1789 /**
1790 * Returns the CDMA ERI icon index to display
1791 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001792 @Override
1793 public int getCdmaEriIconIndex(String callingPackage) {
1794 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001795 }
1796
Sanket Padawe356d7632015-06-22 14:03:32 -07001797 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001798 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage) {
1799 if (!canReadPhoneState(callingPackage, "getCdmaEriIconIndexForSubscriber")) {
1800 return -1;
1801 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001802 final Phone phone = getPhone(subId);
1803 if (phone != null) {
1804 return phone.getCdmaEriIconIndex();
1805 } else {
1806 return -1;
1807 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001808 }
1809
1810 /**
1811 * Returns the CDMA ERI icon mode,
1812 * 0 - ON
1813 * 1 - FLASHING
1814 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001815 @Override
1816 public int getCdmaEriIconMode(String callingPackage) {
1817 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001818 }
1819
Sanket Padawe356d7632015-06-22 14:03:32 -07001820 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001821 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage) {
1822 if (!canReadPhoneState(callingPackage, "getCdmaEriIconModeForSubscriber")) {
1823 return -1;
1824 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001825 final Phone phone = getPhone(subId);
1826 if (phone != null) {
1827 return phone.getCdmaEriIconMode();
1828 } else {
1829 return -1;
1830 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001831 }
1832
1833 /**
1834 * Returns the CDMA ERI text,
1835 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001836 @Override
1837 public String getCdmaEriText(String callingPackage) {
1838 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001839 }
1840
Sanket Padawe356d7632015-06-22 14:03:32 -07001841 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001842 public String getCdmaEriTextForSubscriber(int subId, String callingPackage) {
1843 if (!canReadPhoneState(callingPackage, "getCdmaEriIconTextForSubscriber")) {
1844 return null;
1845 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001846 final Phone phone = getPhone(subId);
1847 if (phone != null) {
1848 return phone.getCdmaEriText();
1849 } else {
1850 return null;
1851 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001852 }
1853
1854 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07001855 * Returns the CDMA MDN.
1856 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001857 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001858 public String getCdmaMdn(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001859 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001860 final Phone phone = getPhone(subId);
1861 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA && phone != null) {
1862 return phone.getLine1Number();
Junda Liuca05d5d2014-08-14 22:36:34 -07001863 } else {
1864 return null;
1865 }
1866 }
1867
1868 /**
1869 * Returns the CDMA MIN.
1870 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001871 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001872 public String getCdmaMin(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001873 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001874 final Phone phone = getPhone(subId);
1875 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1876 return phone.getCdmaMin();
Junda Liuca05d5d2014-08-14 22:36:34 -07001877 } else {
1878 return null;
1879 }
1880 }
1881
1882 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001883 * Returns true if CDMA provisioning needs to run.
1884 */
1885 public boolean needsOtaServiceProvisioning() {
1886 return mPhone.needsOtaServiceProvisioning();
1887 }
1888
1889 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001890 * Sets the voice mail number of a given subId.
1891 */
1892 @Override
1893 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001894 enforceCarrierPrivilege(subId);
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001895 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
1896 new Pair<String, String>(alphaTag, number), new Integer(subId));
1897 return success;
1898 }
1899
Ta-wei Yen87c49842016-05-13 21:19:52 -07001900 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07001901 public void enableVisualVoicemailSmsFilter(String callingPackage, int subId,
1902 VisualVoicemailSmsFilterSettings settings) {
1903 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001904 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07001905 .enableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId,
1906 settings);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001907 }
1908
1909 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07001910 public void disableVisualVoicemailSmsFilter(String callingPackage, int subId) {
1911 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001912 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07001913 .disableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001914 }
1915
1916 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07001917 public VisualVoicemailSmsFilterSettings getVisualVoicemailSmsFilterSettings(
1918 String callingPackage, int subId) {
1919 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001920 return VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07001921 .getVisualVoicemailSmsFilterSettings(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001922 }
1923
1924 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07001925 public VisualVoicemailSmsFilterSettings getSystemVisualVoicemailSmsFilterSettings(
1926 String packageName, int subId) {
1927 enforceReadPrivilegedPermission();
Ta-wei Yen87c49842016-05-13 21:19:52 -07001928 return VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07001929 .getVisualVoicemailSmsFilterSettings(mPhone.getContext(), packageName, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001930 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001931 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001932 * Returns the unread count of voicemails
1933 */
1934 public int getVoiceMessageCount() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001935 return getVoiceMessageCountForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001936 }
1937
1938 /**
1939 * Returns the unread count of voicemails for a subId
1940 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001941 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001942 public int getVoiceMessageCountForSubscriber( int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001943 final Phone phone = getPhone(subId);
1944 if (phone != null) {
1945 return phone.getVoiceMessageCount();
1946 } else {
1947 return 0;
1948 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001949 }
1950
1951 /**
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001952 * Returns the data network type.
1953 * Legacy call, permission-free.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001954 *
1955 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
1956 */
1957 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001958 public int getNetworkType() {
1959 final Phone phone = getPhone(getDefaultSubscription());
1960 if (phone != null) {
1961 return phone.getServiceState().getDataNetworkType();
1962 } else {
1963 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1964 }
Wink Saville36469e72014-06-11 15:17:00 -07001965 }
1966
1967 /**
1968 * Returns the network type for a subId
1969 */
1970 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001971 public int getNetworkTypeForSubscriber(int subId, String callingPackage) {
1972 if (!canReadPhoneState(callingPackage, "getNetworkTypeForSubscriber")) {
1973 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1974 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001975
Sanket Padawe356d7632015-06-22 14:03:32 -07001976 final Phone phone = getPhone(subId);
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001977 if (phone != null) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001978 return phone.getServiceState().getDataNetworkType();
1979 } else {
1980 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1981 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001982 }
1983
1984 /**
1985 * Returns the data network type
1986 */
1987 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001988 public int getDataNetworkType(String callingPackage) {
1989 return getDataNetworkTypeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001990 }
1991
1992 /**
1993 * Returns the data network type for a subId
1994 */
1995 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001996 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage) {
1997 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
1998 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1999 }
2000
Sanket Padawe356d7632015-06-22 14:03:32 -07002001 final Phone phone = getPhone(subId);
2002 if (phone != null) {
2003 return phone.getServiceState().getDataNetworkType();
2004 } else {
2005 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2006 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002007 }
2008
2009 /**
Wink Saville36469e72014-06-11 15:17:00 -07002010 * Returns the Voice network type for a subId
2011 */
2012 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002013 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage) {
2014 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2015 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2016 }
2017
Sanket Padawe356d7632015-06-22 14:03:32 -07002018 final Phone phone = getPhone(subId);
2019 if (phone != null) {
2020 return phone.getServiceState().getVoiceNetworkType();
2021 } else {
2022 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2023 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002024 }
2025
2026 /**
2027 * @return true if a ICC card is present
2028 */
2029 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07002030 // FIXME Make changes to pass defaultSimId of type int
Amit Mahajana6fc2a82015-01-06 11:53:51 -08002031 return hasIccCardUsingSlotId(mSubscriptionController.getSlotId(getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07002032 }
2033
2034 /**
2035 * @return true if a ICC card is present for a slotId
2036 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002037 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002038 public boolean hasIccCardUsingSlotId(int slotId) {
Amit Mahajana6fc2a82015-01-06 11:53:51 -08002039 int subId[] = mSubscriptionController.getSubIdUsingSlotId(slotId);
Sanket Padawe356d7632015-06-22 14:03:32 -07002040 final Phone phone = getPhone(subId[0]);
2041 if (subId != null && phone != null) {
2042 return phone.getIccCard().hasIccCard();
Amit Mahajana6fc2a82015-01-06 11:53:51 -08002043 } else {
2044 return false;
2045 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002046 }
2047
2048 /**
2049 * Return if the current radio is LTE on CDMA. This
2050 * is a tri-state return value as for a period of time
2051 * the mode may be unknown.
2052 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002053 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002054 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08002055 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002056 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002057 @Override
2058 public int getLteOnCdmaMode(String callingPackage) {
2059 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002060 }
2061
Sanket Padawe356d7632015-06-22 14:03:32 -07002062 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002063 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage) {
2064 if (!canReadPhoneState(callingPackage, "getLteOnCdmaModeForSubscriber")) {
2065 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2066 }
2067
Sanket Padawe356d7632015-06-22 14:03:32 -07002068 final Phone phone = getPhone(subId);
2069 if (phone == null) {
2070 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2071 } else {
2072 return phone.getLteOnCdmaMode();
2073 }
Wink Saville36469e72014-06-11 15:17:00 -07002074 }
2075
2076 public void setPhone(Phone phone) {
2077 mPhone = phone;
2078 }
2079
2080 /**
2081 * {@hide}
2082 * Returns Default subId, 0 in the case of single standby.
2083 */
Wink Savilleb564aae2014-10-23 10:18:09 -07002084 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002085 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07002086 }
2087
Shishir Agrawala9f32182016-04-12 12:00:16 -07002088 private int getSlotForDefaultSubscription() {
2089 return mSubscriptionController.getPhoneId(getDefaultSubscription());
2090 }
2091
Wink Savilleb564aae2014-10-23 10:18:09 -07002092 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002093 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002094 }
Ihab Awadf2177b72013-11-25 13:33:23 -08002095
2096 /**
2097 * @see android.telephony.TelephonyManager.WifiCallingChoices
2098 */
2099 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002100 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
2101 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08002102 }
2103
2104 /**
2105 * @see android.telephony.TelephonyManager.WifiCallingChoices
2106 */
2107 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002108 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
2109 Settings.System.putInt(mPhone.getContext().getContentResolver(),
2110 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08002111 }
2112
Sailesh Nepald1e68152013-12-12 19:08:02 -08002113 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07002114 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08002115 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08002116 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08002117
Shishir Agrawal566b7612013-10-28 14:41:00 -07002118 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002119 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(int subId, String AID) {
2120 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002121
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002122 if (DBG) log("iccOpenLogicalChannel: subId=" + subId + " aid=" + AID);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002123 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002124 CMD_OPEN_CHANNEL, AID, subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002125 if (DBG) log("iccOpenLogicalChannel: " + response);
2126 return response;
Shishir Agrawal566b7612013-10-28 14:41:00 -07002127 }
2128
2129 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002130 public boolean iccCloseLogicalChannel(int subId, int channel) {
2131 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002132
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002133 if (DBG) log("iccCloseLogicalChannel: subId=" + subId + " chnl=" + channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002134 if (channel < 0) {
2135 return false;
2136 }
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002137 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel, subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002138 if (DBG) log("iccCloseLogicalChannel: " + success);
2139 return success;
2140 }
2141
2142 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002143 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07002144 int command, int p1, int p2, int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002145 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002146
2147 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002148 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel +
2149 " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
Shishir Agrawal566b7612013-10-28 14:41:00 -07002150 " data=" + data);
2151 }
2152
2153 if (channel < 0) {
2154 return "";
2155 }
2156
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002157 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002158 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002159 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
2160
Shishir Agrawal566b7612013-10-28 14:41:00 -07002161 // Append the returned status code to the end of the response payload.
2162 String s = Integer.toHexString(
2163 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002164 if (response.payload != null) {
2165 s = IccUtils.bytesToHexString(response.payload) + s;
2166 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07002167 return s;
2168 }
Jake Hambye994d462014-02-03 13:10:13 -08002169
Evan Charltonc66da362014-05-16 14:06:40 -07002170 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002171 public String iccTransmitApduBasicChannel(int subId, int cla, int command, int p1, int p2,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002172 int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002173 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002174
2175 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002176 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd=" + command
2177 + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002178 }
2179
2180 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002181 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002182 if (DBG) log("iccTransmitApduBasicChannel: " + response);
2183
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002184 // Append the returned status code to the end of the response payload.
2185 String s = Integer.toHexString(
2186 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002187 if (response.payload != null) {
2188 s = IccUtils.bytesToHexString(response.payload) + s;
2189 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002190 return s;
2191 }
2192
2193 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002194 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002195 String filePath) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002196 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002197
2198 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002199 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " " +
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002200 p1 + " " + p2 + " " + p3 + ":" + filePath);
2201 }
2202
2203 IccIoResult response =
2204 (IccIoResult)sendRequest(CMD_EXCHANGE_SIM_IO,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002205 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
2206 subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002207
2208 if (DBG) {
2209 log("Exchange SIM_IO [R]" + response);
2210 }
2211
2212 byte[] result = null;
2213 int length = 2;
2214 if (response.payload != null) {
2215 length = 2 + response.payload.length;
2216 result = new byte[length];
2217 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
2218 } else {
2219 result = new byte[length];
2220 }
2221
2222 result[length - 1] = (byte) response.sw2;
2223 result[length - 2] = (byte) response.sw1;
2224 return result;
2225 }
2226
2227 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002228 public String sendEnvelopeWithStatus(int subId, String content) {
2229 enforceModifyPermissionOrCarrierPrivilege(subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002230
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002231 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content, subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002232 if (response.payload == null) {
2233 return "";
2234 }
2235
2236 // Append the returned status code to the end of the response payload.
2237 String s = Integer.toHexString(
2238 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
2239 s = IccUtils.bytesToHexString(response.payload) + s;
2240 return s;
2241 }
2242
Jake Hambye994d462014-02-03 13:10:13 -08002243 /**
2244 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2245 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2246 *
2247 * @param itemID the ID of the item to read
2248 * @return the NV item as a String, or null on error.
2249 */
2250 @Override
2251 public String nvReadItem(int itemID) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002252 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002253 if (DBG) log("nvReadItem: item " + itemID);
2254 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
2255 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
2256 return value;
2257 }
2258
2259 /**
2260 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2261 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2262 *
2263 * @param itemID the ID of the item to read
2264 * @param itemValue the value to write, as a String
2265 * @return true on success; false on any failure
2266 */
2267 @Override
2268 public boolean nvWriteItem(int itemID, String itemValue) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002269 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002270 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
2271 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
2272 new Pair<Integer, String>(itemID, itemValue));
2273 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
2274 return success;
2275 }
2276
2277 /**
2278 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
2279 * Used for device configuration by some CDMA operators.
2280 *
2281 * @param preferredRoamingList byte array containing the new PRL
2282 * @return true on success; false on any failure
2283 */
2284 @Override
2285 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002286 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002287 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
2288 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
2289 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
2290 return success;
2291 }
2292
2293 /**
2294 * Perform the specified type of NV config reset.
2295 * Used for device configuration by some CDMA operators.
2296 *
2297 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
2298 * @return true on success; false on any failure
2299 */
2300 @Override
2301 public boolean nvResetConfig(int resetType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002302 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002303 if (DBG) log("nvResetConfig: type " + resetType);
2304 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
2305 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
2306 return success;
2307 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002308
2309 /**
Wink Saville36469e72014-06-11 15:17:00 -07002310 * {@hide}
2311 * Returns Default sim, 0 in the case of single standby.
2312 */
2313 public int getDefaultSim() {
2314 //TODO Need to get it from Telephony Devcontroller
2315 return 0;
2316 }
2317
Svet Ganovb320e182015-04-16 12:30:10 -07002318 public String[] getPcscfAddress(String apnType, String callingPackage) {
2319 if (!canReadPhoneState(callingPackage, "getPcscfAddress")) {
2320 return new String[0];
2321 }
2322
2323
ram87fca6f2014-07-18 18:58:44 +05302324 return mPhone.getPcscfAddress(apnType);
Wink Saville36469e72014-06-11 15:17:00 -07002325 }
2326
2327 public void setImsRegistrationState(boolean registered) {
2328 enforceModifyPermission();
2329 mPhone.setImsRegistrationState(registered);
2330 }
2331
2332 /**
Stuart Scott54788802015-03-30 13:18:01 -07002333 * Set the network selection mode to automatic.
2334 *
2335 */
2336 @Override
2337 public void setNetworkSelectionModeAutomatic(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002338 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002339 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
2340 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
2341 }
2342
2343 /**
Shishir Agrawal302c8692015-06-19 13:49:39 -07002344 * Set the network selection mode to manual with the selected carrier.
2345 */
2346 @Override
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002347 public boolean setNetworkSelectionModeManual(int subId, OperatorInfo operator,
2348 boolean persistSelection) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002349 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002350 if (DBG) log("setNetworkSelectionModeManual: subId:" + subId + " operator:" + operator);
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002351 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operator,
2352 persistSelection);
2353 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002354 }
2355
2356 /**
2357 * Scans for available networks.
2358 */
2359 @Override
2360 public CellNetworkScanResult getCellNetworkScanResults(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002361 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002362 if (DBG) log("getCellNetworkScanResults: subId " + subId);
2363 CellNetworkScanResult result = (CellNetworkScanResult) sendRequest(
2364 CMD_PERFORM_NETWORK_SCAN, null, subId);
2365 return result;
2366 }
2367
2368 /**
Junda Liu84d15a22014-07-02 11:21:04 -07002369 * Get the calculated preferred network type.
2370 * Used for debugging incorrect network type.
2371 *
2372 * @return the preferred network type, defined in RILConstants.java.
2373 */
2374 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002375 public int getCalculatedPreferredNetworkType(String callingPackage) {
2376 if (!canReadPhoneState(callingPackage, "getCalculatedPreferredNetworkType")) {
2377 return RILConstants.PREFERRED_NETWORK_MODE;
2378 }
2379
Amit Mahajan43330e02014-11-18 11:54:45 -08002380 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext(), 0); // wink FIXME: need to get SubId from somewhere.
Junda Liu84d15a22014-07-02 11:21:04 -07002381 }
2382
2383 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08002384 * Get the preferred network type.
2385 * Used for device configuration by some CDMA operators.
2386 *
2387 * @return the preferred network type, defined in RILConstants.java.
2388 */
2389 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002390 public int getPreferredNetworkType(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002391 enforceModifyPermissionOrCarrierPrivilege(subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002392 if (DBG) log("getPreferredNetworkType");
Stuart Scott54788802015-03-30 13:18:01 -07002393 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002394 int networkType = (result != null ? result[0] : -1);
2395 if (DBG) log("getPreferredNetworkType: " + networkType);
2396 return networkType;
2397 }
2398
2399 /**
2400 * Set the preferred network type.
2401 * Used for device configuration by some CDMA operators.
2402 *
2403 * @param networkType the preferred network type, defined in RILConstants.java.
2404 * @return true on success; false on any failure.
2405 */
2406 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002407 public boolean setPreferredNetworkType(int subId, int networkType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002408 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002409 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
2410 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002411 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
Junda Liu80bc0d12014-07-14 16:36:44 -07002412 if (success) {
2413 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
Stuart Scott54788802015-03-30 13:18:01 -07002414 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
Junda Liu80bc0d12014-07-14 16:36:44 -07002415 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002416 return success;
2417 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002418
2419 /**
Junda Liu475951f2014-11-07 16:45:03 -08002420 * Check TETHER_DUN_REQUIRED and TETHER_DUN_APN settings, net.tethering.noprovisioning
2421 * SystemProperty, and config_tether_apndata to decide whether DUN APN is required for
2422 * tethering.
2423 *
2424 * @return 0: Not required. 1: required. 2: Not set.
2425 * @hide
2426 */
2427 @Override
2428 public int getTetherApnRequired() {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002429 enforceModifyPermission();
Junda Liu475951f2014-11-07 16:45:03 -08002430 int dunRequired = Settings.Global.getInt(mPhone.getContext().getContentResolver(),
2431 Settings.Global.TETHER_DUN_REQUIRED, 2);
2432 // If not set, check net.tethering.noprovisioning, TETHER_DUN_APN setting and
2433 // config_tether_apndata.
2434 if (dunRequired == 2 && mPhone.hasMatchedTetherApnSetting()) {
2435 dunRequired = 1;
2436 }
2437 return dunRequired;
2438 }
2439
2440 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07002441 * Set mobile data enabled
2442 * Used by the user through settings etc to turn on/off mobile data
2443 *
2444 * @param enable {@code true} turn turn data on, else {@code false}
2445 */
2446 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002447 public void setDataEnabled(int subId, boolean enable) {
Robert Greenwalted86e582014-05-21 20:03:20 -07002448 enforceModifyPermission();
Wink Savillee7353bb2014-12-05 14:21:41 -08002449 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002450 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002451 Phone phone = PhoneFactory.getPhone(phoneId);
2452 if (phone != null) {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002453 if (DBG) log("setDataEnabled: subId=" + subId + " enable=" + enable);
Wink Savillee7353bb2014-12-05 14:21:41 -08002454 phone.setDataEnabled(enable);
2455 } else {
2456 loge("setDataEnabled: no phone for subId=" + subId);
2457 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002458 }
2459
2460 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07002461 * Get whether mobile data is enabled.
2462 *
2463 * Note that this used to be available from ConnectivityService, gated by
2464 * ACCESS_NETWORK_STATE permission, so this will accept either that or
2465 * our MODIFY_PHONE_STATE.
Robert Greenwalted86e582014-05-21 20:03:20 -07002466 *
2467 * @return {@code true} if data is enabled else {@code false}
2468 */
2469 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002470 public boolean getDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07002471 try {
2472 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
2473 null);
2474 } catch (Exception e) {
2475 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE,
2476 null);
2477 }
Wink Savillee7353bb2014-12-05 14:21:41 -08002478 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002479 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002480 Phone phone = PhoneFactory.getPhone(phoneId);
2481 if (phone != null) {
2482 boolean retVal = phone.getDataEnabled();
Joe Onoratoa601dd22016-02-23 13:03:53 -08002483 if (DBG) log("getDataEnabled: subId=" + subId + " retVal=" + retVal);
Wink Savillee7353bb2014-12-05 14:21:41 -08002484 return retVal;
2485 } else {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002486 if (DBG) loge("getDataEnabled: no phone subId=" + subId + " retVal=false");
Wink Savillee7353bb2014-12-05 14:21:41 -08002487 return false;
2488 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002489 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002490
2491 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002492 public int getCarrierPrivilegeStatus(int subId) {
2493 final Phone phone = getPhone(subId);
2494 if (phone == null) {
2495 loge("getCarrierPrivilegeStatus: Invalid subId");
2496 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
2497 }
2498 UiccCard card = UiccController.getInstance().getUiccCard(phone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002499 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002500 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002501 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2502 }
2503 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002504 phone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002505 }
Junda Liu29340342014-07-10 15:23:27 -07002506
2507 @Override
Zach Johnson50ecba32015-05-19 00:24:21 -07002508 public int checkCarrierPrivilegesForPackage(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002509 if (TextUtils.isEmpty(pkgName))
2510 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Shishir Agrawal21409252015-01-15 23:33:50 -08002511 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002512 if (card == null) {
2513 loge("checkCarrierPrivilegesForPackage: No UICC");
2514 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2515 }
Zach Johnson50ecba32015-05-19 00:24:21 -07002516 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgName);
2517 }
2518
2519 @Override
2520 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002521 if (TextUtils.isEmpty(pkgName))
2522 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Zach Johnson50ecba32015-05-19 00:24:21 -07002523 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2524 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2525 UiccCard card = UiccController.getInstance().getUiccCard(i);
2526 if (card == null) {
Jonathan Basseri7d320df2015-06-16 12:17:08 -07002527 // No UICC in that slot.
Zach Johnson50ecba32015-05-19 00:24:21 -07002528 continue;
2529 }
2530
2531 result = card.getCarrierPrivilegeStatus(
2532 mPhone.getContext().getPackageManager(), pkgName);
2533 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2534 break;
2535 }
2536 }
2537
2538 return result;
Junda Liu29340342014-07-10 15:23:27 -07002539 }
Derek Tan89e89d42014-07-08 17:00:10 -07002540
2541 @Override
Junda Liue64de782015-04-16 17:19:16 -07002542 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
2543 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
2544 loge("phoneId " + phoneId + " is not valid.");
2545 return null;
2546 }
2547 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002548 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002549 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002550 return null ;
2551 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002552 return card.getCarrierPackageNamesForIntent(
Svetoslav483aff72015-04-21 14:16:07 -07002553 mPhone.getContext().getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002554 }
2555
Amith Yamasani6e118872016-02-19 12:53:51 -08002556 @Override
2557 public List<String> getPackagesWithCarrierPrivileges() {
2558 PackageManager pm = mPhone.getContext().getPackageManager();
2559 List<String> privilegedPackages = new ArrayList<>();
2560 List<PackageInfo> packages = null;
2561 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2562 UiccCard card = UiccController.getInstance().getUiccCard(i);
2563 if (card == null) {
2564 // No UICC in that slot.
2565 continue;
2566 }
2567 if (card.hasCarrierPrivilegeRules()) {
2568 if (packages == null) {
2569 // Only check packages in user 0 for now
2570 packages = pm.getInstalledPackagesAsUser(
2571 PackageManager.MATCH_DISABLED_COMPONENTS
2572 | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS
2573 | PackageManager.GET_SIGNATURES, UserHandle.USER_SYSTEM);
2574 }
2575 for (int p = packages.size() - 1; p >= 0; p--) {
2576 PackageInfo pkgInfo = packages.get(p);
2577 if (pkgInfo != null && pkgInfo.packageName != null
2578 && card.getCarrierPrivilegeStatus(pkgInfo)
2579 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2580 privilegedPackages.add(pkgInfo.packageName);
2581 }
2582 }
2583 }
2584 }
2585 return privilegedPackages;
2586 }
2587
Wink Savilleb564aae2014-10-23 10:18:09 -07002588 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002589 final Phone phone = getPhone(subId);
2590 UiccCard card = phone == null ? null : phone.getUiccCard();
Derek Tan97ebb422014-09-05 16:55:38 -07002591 if (card == null) {
2592 loge("getIccId: No UICC");
2593 return null;
2594 }
2595 String iccId = card.getIccId();
2596 if (TextUtils.isEmpty(iccId)) {
2597 loge("getIccId: ICC ID is null or empty.");
2598 return null;
2599 }
2600 return iccId;
2601 }
2602
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002603 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002604 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
2605 String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002606 enforceCarrierPrivilege(subId);
Derek Tan97ebb422014-09-05 16:55:38 -07002607
Jeff Sharkey85190e62014-12-05 09:40:12 -08002608 final String iccId = getIccId(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07002609 final Phone phone = getPhone(subId);
2610 if (phone == null) {
2611 return false;
2612 }
2613 final String subscriberId = phone.getSubscriberId();
Jeff Sharkey85190e62014-12-05 09:40:12 -08002614
2615 if (DBG_MERGE) {
2616 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
2617 + subscriberId + " to " + number);
2618 }
2619
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002620 if (TextUtils.isEmpty(iccId)) {
2621 return false;
Derek Tan97ebb422014-09-05 16:55:38 -07002622 }
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002623
Jeff Sharkey85190e62014-12-05 09:40:12 -08002624 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2625
2626 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002627 if (alphaTag == null) {
2628 editor.remove(alphaTagPrefKey);
2629 } else {
2630 editor.putString(alphaTagPrefKey, alphaTag);
2631 }
2632
Jeff Sharkey85190e62014-12-05 09:40:12 -08002633 // Record both the line number and IMSI for this ICCID, since we need to
2634 // track all merged IMSIs based on line number
2635 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2636 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002637 if (number == null) {
2638 editor.remove(numberPrefKey);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002639 editor.remove(subscriberPrefKey);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002640 } else {
2641 editor.putString(numberPrefKey, number);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002642 editor.putString(subscriberPrefKey, subscriberId);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002643 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002644
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002645 editor.commit();
2646 return true;
Derek Tan7226c842014-07-02 17:42:23 -07002647 }
2648
2649 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002650 public String getLine1NumberForDisplay(int subId, String callingPackage) {
Makoto Onukifee69342015-06-29 14:44:50 -07002651 // This is open to apps with WRITE_SMS.
2652 if (!canReadPhoneNumber(callingPackage, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08002653 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07002654 return null;
2655 }
Derek Tan97ebb422014-09-05 16:55:38 -07002656
2657 String iccId = getIccId(subId);
2658 if (iccId != null) {
2659 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
Amit Mahajan9cf11512015-11-09 11:40:48 -08002660 if (DBG_MERGE) {
2661 log("getLine1NumberForDisplay returning " +
2662 mTelephonySharedPreferences.getString(numberPrefKey, null));
2663 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002664 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002665 }
Amit Mahajan9cf11512015-11-09 11:40:48 -08002666 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
Derek Tan97ebb422014-09-05 16:55:38 -07002667 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002668 }
2669
2670 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002671 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
2672 if (!canReadPhoneState(callingPackage, "getLine1AlphaTagForDisplay")) {
2673 return null;
2674 }
Derek Tan97ebb422014-09-05 16:55:38 -07002675
2676 String iccId = getIccId(subId);
2677 if (iccId != null) {
2678 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002679 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002680 }
Derek Tan97ebb422014-09-05 16:55:38 -07002681 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002682 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002683
2684 @Override
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002685 public String[] getMergedSubscriberIds(String callingPackage) {
2686 if (!canReadPhoneState(callingPackage, "getMergedSubscriberIds")) {
2687 return null;
2688 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002689 final Context context = mPhone.getContext();
2690 final TelephonyManager tele = TelephonyManager.from(context);
2691 final SubscriptionManager sub = SubscriptionManager.from(context);
2692
2693 // Figure out what subscribers are currently active
2694 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002695 // Clear calling identity, when calling TelephonyManager, because callerUid must be
2696 // the process, where TelephonyManager was instantiated. Otherwise AppOps check will fail.
2697 final long identity = Binder.clearCallingIdentity();
2698 try {
2699 final int[] subIds = sub.getActiveSubscriptionIdList();
2700 for (int subId : subIds) {
2701 activeSubscriberIds.add(tele.getSubscriberId(subId));
2702 }
2703 } finally {
2704 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002705 }
2706
2707 // First pass, find a number override for an active subscriber
2708 String mergeNumber = null;
2709 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
2710 for (String key : prefs.keySet()) {
2711 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
2712 final String subscriberId = (String) prefs.get(key);
2713 if (activeSubscriberIds.contains(subscriberId)) {
2714 final String iccId = key.substring(PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
2715 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2716 mergeNumber = (String) prefs.get(numberKey);
2717 if (DBG_MERGE) {
2718 Slog.d(LOG_TAG, "Found line number " + mergeNumber
2719 + " for active subscriber " + subscriberId);
2720 }
2721 if (!TextUtils.isEmpty(mergeNumber)) {
2722 break;
2723 }
2724 }
2725 }
2726 }
2727
2728 // Shortcut when no active merged subscribers
2729 if (TextUtils.isEmpty(mergeNumber)) {
2730 return null;
2731 }
2732
2733 // Second pass, find all subscribers under that line override
2734 final ArraySet<String> result = new ArraySet<>();
2735 for (String key : prefs.keySet()) {
2736 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
2737 final String number = (String) prefs.get(key);
2738 if (mergeNumber.equals(number)) {
2739 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
2740 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
2741 final String subscriberId = (String) prefs.get(subscriberKey);
2742 if (!TextUtils.isEmpty(subscriberId)) {
2743 result.add(subscriberId);
2744 }
2745 }
2746 }
2747 }
2748
2749 final String[] resultArray = result.toArray(new String[result.size()]);
2750 Arrays.sort(resultArray);
2751 if (DBG_MERGE) {
2752 Slog.d(LOG_TAG, "Found subscribers " + Arrays.toString(resultArray) + " after merge");
2753 }
2754 return resultArray;
2755 }
2756
2757 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002758 public boolean setOperatorBrandOverride(int subId, String brand) {
2759 enforceCarrierPrivilege(subId);
2760 final Phone phone = getPhone(subId);
2761 return phone == null ? false : phone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002762 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05002763
2764 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002765 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002766 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
2767 List<String> cdmaNonRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002768 enforceCarrierPrivilege(subId);
2769 final Phone phone = getPhone(subId);
2770 if (phone == null) {
2771 return false;
2772 }
2773 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002774 cdmaNonRoamingList);
2775 }
2776
2777 @Override
Steven Liu4bf01bc2014-07-17 11:05:29 -05002778 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
2779 enforceModifyPermission();
2780
2781 int returnValue = 0;
2782 try {
2783 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
2784 if(result.exception == null) {
2785 if (result.result != null) {
2786 byte[] responseData = (byte[])(result.result);
2787 if(responseData.length > oemResp.length) {
2788 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
2789 responseData.length + "bytes. Buffer Size is " +
2790 oemResp.length + "bytes.");
2791 }
2792 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
2793 returnValue = responseData.length;
2794 }
2795 } else {
2796 CommandException ex = (CommandException) result.exception;
2797 returnValue = ex.getCommandError().ordinal();
2798 if(returnValue > 0) returnValue *= -1;
2799 }
2800 } catch (RuntimeException e) {
2801 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
2802 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
2803 if(returnValue > 0) returnValue *= -1;
2804 }
2805
2806 return returnValue;
2807 }
Wink Saville5d475dd2014-10-17 15:00:58 -07002808
2809 @Override
2810 public void setRadioCapability(RadioAccessFamily[] rafs) {
2811 try {
2812 ProxyController.getInstance().setRadioCapability(rafs);
2813 } catch (RuntimeException e) {
2814 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
2815 }
2816 }
2817
2818 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002819 public int getRadioAccessFamily(int phoneId, String callingPackage) {
2820 if (!canReadPhoneState(callingPackage, "getRadioAccessFamily")) {
2821 return RadioAccessFamily.RAF_UNKNOWN;
2822 }
2823
Wink Saville5d475dd2014-10-17 15:00:58 -07002824 return ProxyController.getInstance().getRadioAccessFamily(phoneId);
2825 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002826
2827 @Override
2828 public void enableVideoCalling(boolean enable) {
2829 enforceModifyPermission();
Tyler Gunnfdd69de2015-12-04 21:24:38 -08002830 ImsManager.setVtSetting(mPhone.getContext(), enable);
Andrew Leedf14ead2014-10-17 14:22:52 -07002831 }
2832
2833 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002834 public boolean isVideoCallingEnabled(String callingPackage) {
2835 if (!canReadPhoneState(callingPackage, "isVideoCallingEnabled")) {
2836 return false;
2837 }
2838
Andrew Lee77527ac2014-10-21 16:57:39 -07002839 // Check the user preference and the system-level IMS setting. Even if the user has
2840 // enabled video calling, if IMS is disabled we aren't able to support video calling.
2841 // In the long run, we may instead need to check if there exists a connection service
2842 // which can support video calling.
Andrew Lee312e8172014-10-23 17:01:36 -07002843 return ImsManager.isVtEnabledByPlatform(mPhone.getContext())
2844 && ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
Tyler Gunnfdd69de2015-12-04 21:24:38 -08002845 && ImsManager.isVtEnabledByUser(mPhone.getContext());
Andrew Leedf14ead2014-10-17 14:22:52 -07002846 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002847
Andrew Leea1239f22015-03-02 17:44:07 -08002848 @Override
2849 public boolean canChangeDtmfToneLength() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07002850 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08002851 }
2852
2853 @Override
2854 public boolean isWorldPhone() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07002855 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08002856 }
2857
Andrew Lee9431b832015-03-09 18:46:45 -07002858 @Override
2859 public boolean isTtyModeSupported() {
2860 TelecomManager telecomManager = TelecomManager.from(mPhone.getContext());
2861 TelephonyManager telephonyManager =
2862 (TelephonyManager) mPhone.getContext().getSystemService(Context.TELEPHONY_SERVICE);
Wooki Wu1f82f7a2016-02-15 15:59:58 +08002863 return telecomManager.isTtySupported();
Andrew Lee9431b832015-03-09 18:46:45 -07002864 }
2865
2866 @Override
2867 public boolean isHearingAidCompatibilitySupported() {
2868 return mPhone.getContext().getResources().getBoolean(R.bool.hac_enabled);
2869 }
2870
Sanket Padawe7310cc72015-01-14 09:53:20 -08002871 /**
2872 * Returns the unique device ID of phone, for example, the IMEI for
2873 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
2874 *
2875 * <p>Requires Permission:
2876 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
2877 */
2878 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002879 public String getDeviceId(String callingPackage) {
2880 if (!canReadPhoneState(callingPackage, "getDeviceId")) {
2881 return null;
2882 }
2883
Sanket Padawe7310cc72015-01-14 09:53:20 -08002884 final Phone phone = PhoneFactory.getPhone(0);
2885 if (phone != null) {
2886 return phone.getDeviceId();
2887 } else {
2888 return null;
2889 }
2890 }
2891
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002892 /*
2893 * {@hide}
2894 * Returns the IMS Registration Status
2895 */
Santos Cordon7a1885b2015-02-03 11:15:19 -08002896 @Override
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002897 public boolean isImsRegistered() {
2898 return mPhone.isImsRegistered();
2899 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08002900
2901 @Override
2902 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
2903 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
2904 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07002905
Nathan Haroldc55097a2015-03-11 18:14:50 -07002906 /*
2907 * {@hide}
2908 * Returns the IMS Registration Status
2909 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002910 public boolean isWifiCallingAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07002911 return mPhone.isWifiCallingEnabled();
2912 }
2913
2914 /*
2915 * {@hide}
2916 * Returns the IMS Registration Status
2917 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002918 public boolean isVolteAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07002919 return mPhone.isVolteEnabled();
2920 }
Svet Ganovb320e182015-04-16 12:30:10 -07002921
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002922 /*
2923 * {@hide} Returns the IMS Registration Status
2924 */
2925 public boolean isVideoTelephonyAvailable() {
2926 return mPhone.isVideoEnabled();
2927 }
2928
Svet Ganovb320e182015-04-16 12:30:10 -07002929 private boolean canReadPhoneState(String callingPackage, String message) {
Etan Cohen921655c2015-06-24 13:54:50 -07002930 try {
Amit Mahajan83ea23b2015-07-30 16:05:11 -07002931 mApp.enforceCallingOrSelfPermission(
Etan Cohen921655c2015-06-24 13:54:50 -07002932 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE, message);
2933
Amit Mahajan83ea23b2015-07-30 16:05:11 -07002934 // SKIP checking for run-time permission since caller or self has PRIVILEDGED permission
Etan Cohen921655c2015-06-24 13:54:50 -07002935 return true;
Amit Mahajan785783f2015-06-29 10:36:50 -07002936 } catch (SecurityException e) {
2937 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
2938 message);
Etan Cohen921655c2015-06-24 13:54:50 -07002939 }
Svet Ganovb320e182015-04-16 12:30:10 -07002940
2941 if (mAppOps.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(),
2942 callingPackage) != AppOpsManager.MODE_ALLOWED) {
2943 return false;
2944 }
2945
2946 return true;
2947 }
Stuart Scott8eef64f2015-04-08 15:13:54 -07002948
Makoto Onukifee69342015-06-29 14:44:50 -07002949 /**
Amit Mahajanb9b49782015-09-15 18:16:32 -07002950 * Besides READ_PHONE_STATE, WRITE_SMS and READ_SMS also allow apps to get phone numbers.
Makoto Onukifee69342015-06-29 14:44:50 -07002951 */
2952 private boolean canReadPhoneNumber(String callingPackage, String message) {
Makoto Onukie4072d12015-08-03 15:12:23 -07002953 // Default SMS app can always read it.
2954 if (mAppOps.noteOp(AppOpsManager.OP_WRITE_SMS,
2955 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED) {
2956 return true;
2957 }
2958 try {
2959 return canReadPhoneState(callingPackage, message);
Amit Mahajanb9b49782015-09-15 18:16:32 -07002960 } catch (SecurityException readPhoneStateSecurityException) {
2961 try {
2962 // Can be read with READ_SMS too.
2963 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_SMS, message);
2964 return mAppOps.noteOp(AppOpsManager.OP_READ_SMS,
2965 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED;
2966 } catch (SecurityException readSmsSecurityException) {
2967 // Throw exception with message including both READ_PHONE_STATE and READ_SMS
2968 // permissions
2969 throw new SecurityException(message + ": Neither user " + Binder.getCallingUid() +
2970 " nor current process has " + android.Manifest.permission.READ_PHONE_STATE +
2971 " or " + android.Manifest.permission.READ_SMS + ".");
2972 }
Makoto Onukie4072d12015-08-03 15:12:23 -07002973 }
Makoto Onukifee69342015-06-29 14:44:50 -07002974 }
2975
Stuart Scott8eef64f2015-04-08 15:13:54 -07002976 @Override
2977 public void factoryReset(int subId) {
2978 enforceConnectivityInternalPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07002979 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
2980 return;
2981 }
2982
Svet Ganovcc087f82015-05-12 20:35:54 -07002983 final long identity = Binder.clearCallingIdentity();
2984 try {
Stuart Scott981d8582015-04-21 14:09:50 -07002985 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
2986 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Svet Ganovcc087f82015-05-12 20:35:54 -07002987 // Enable data
2988 setDataEnabled(subId, true);
2989 // Set network selection mode to automatic
2990 setNetworkSelectionModeAutomatic(subId);
2991 // Set preferred mobile network type to the best available
2992 setPreferredNetworkType(subId, Phone.PREFERRED_NT_MODE);
2993 // Turn off roaming
2994 SubscriptionManager.from(mApp).setDataRoaming(0, subId);
2995 }
2996 } finally {
2997 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07002998 }
2999 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003000
3001 @Override
3002 public String getLocaleFromDefaultSim() {
3003 // We query all subscriptions instead of just the active ones, because
3004 // this might be called early on in the provisioning flow when the
3005 // subscriptions potentially aren't active yet.
3006 final List<SubscriptionInfo> slist = getAllSubscriptionInfoList();
3007 if (slist == null || slist.isEmpty()) {
3008 return null;
3009 }
3010
3011 // This function may be called very early, say, from the setup wizard, at
3012 // which point we won't have a default subscription set. If that's the case
3013 // we just choose the first, which will be valid in "most cases".
3014 final int defaultSubId = getDefaultSubscription();
3015 SubscriptionInfo info = null;
3016 if (defaultSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
3017 info = slist.get(0);
3018 } else {
3019 for (SubscriptionInfo item : slist) {
3020 if (item.getSubscriptionId() == defaultSubId) {
3021 info = item;
3022 break;
3023 }
3024 }
3025
3026 if (info == null) {
3027 return null;
3028 }
3029 }
3030
3031 // Try and fetch the locale from the carrier properties or from the SIM language
3032 // preferences (EF-PL and EF-LI)...
Tony Hill183b2de2015-06-24 14:53:58 +01003033 final int mcc = info.getMcc();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003034 final Phone defaultPhone = getPhone(info.getSubscriptionId());
Narayan Kamath011676f2015-07-29 12:04:08 +01003035 String simLanguage = null;
Narayan Kamath1c496c22015-04-16 14:40:19 +01003036 if (defaultPhone != null) {
3037 final Locale localeFromDefaultSim = defaultPhone.getLocaleFromSimAndCarrierPrefs();
3038 if (localeFromDefaultSim != null) {
Narayan Kamath011676f2015-07-29 12:04:08 +01003039 if (!localeFromDefaultSim.getCountry().isEmpty()) {
Tony Hill183b2de2015-06-24 14:53:58 +01003040 if (DBG) log("Using locale from default SIM:" + localeFromDefaultSim);
3041 return localeFromDefaultSim.toLanguageTag();
Narayan Kamath011676f2015-07-29 12:04:08 +01003042 } else {
3043 simLanguage = localeFromDefaultSim.getLanguage();
Tony Hill183b2de2015-06-24 14:53:58 +01003044 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003045 }
3046 }
3047
Narayan Kamath011676f2015-07-29 12:04:08 +01003048 // The SIM language preferences only store a language (e.g. fr = French), not an
3049 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
3050 // the SIM and carrier preferences does not include a country we add the country
3051 // determined from the SIM MCC to provide an exact locale.
3052 final Locale mccLocale = MccTable.getLocaleFromMcc(mPhone.getContext(), mcc, simLanguage);
Tony Hill183b2de2015-06-24 14:53:58 +01003053 if (mccLocale != null) {
3054 if (DBG) log("No locale from default SIM, using mcc locale:" + mccLocale);
3055 return mccLocale.toLanguageTag();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003056 }
3057
Tony Hill183b2de2015-06-24 14:53:58 +01003058 if (DBG) log("No locale found - returning null");
Narayan Kamath1c496c22015-04-16 14:40:19 +01003059 return null;
3060 }
3061
3062 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
3063 final long identity = Binder.clearCallingIdentity();
3064 try {
3065 return mSubscriptionController.getAllSubInfoList(
3066 mPhone.getContext().getOpPackageName());
3067 } finally {
3068 Binder.restoreCallingIdentity(identity);
3069 }
3070 }
3071
3072 private List<SubscriptionInfo> getActiveSubscriptionInfoList() {
3073 final long identity = Binder.clearCallingIdentity();
3074 try {
3075 return mSubscriptionController.getActiveSubscriptionInfoList(
3076 mPhone.getContext().getOpPackageName());
3077 } finally {
3078 Binder.restoreCallingIdentity(identity);
3079 }
3080 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003081
3082 /**
Adam Lesinski903a54c2016-04-11 14:49:52 -07003083 * Responds to the ResultReceiver with the {@link android.telephony.ModemActivityInfo} object
3084 * representing the state of the modem.
3085 *
3086 * NOTE: This clears the modem state, so there should only every be one caller.
3087 * @hide
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003088 */
3089 @Override
Adam Lesinski903a54c2016-04-11 14:49:52 -07003090 public void requestModemActivityInfo(ResultReceiver result) {
3091 enforceModifyPermission();
3092
3093 ModemActivityInfo info = (ModemActivityInfo) sendRequest(CMD_GET_MODEM_ACTIVITY_INFO, null);
3094 Bundle bundle = new Bundle();
3095 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY, info);
3096 result.send(0, bundle);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003097 }
Jack Yu85bd38a2015-11-09 11:34:32 -08003098
3099 /**
3100 * {@hide}
3101 * Returns the service state information on specified subscription.
3102 */
3103 @Override
3104 public ServiceState getServiceStateForSubscriber(int subId, String callingPackage) {
3105
3106 if (!canReadPhoneState(callingPackage, "getServiceStateForSubscriber")) {
3107 return null;
3108 }
3109
3110 final Phone phone = getPhone(subId);
3111 if (phone == null) {
3112 return null;
3113 }
3114
3115 return phone.getServiceState();
3116 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08003117
3118 /**
3119 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
3120 *
3121 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3122 * voicemail ringtone.
3123 * @return The URI for the ringtone to play when receiving a voicemail from a specific
3124 * PhoneAccount.
3125 */
3126 @Override
3127 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
3128 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
3129 if (phone == null) {
3130 return null;
3131 }
3132
3133 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone);
3134 }
3135
3136 /**
3137 * Returns whether vibration is set for voicemail notification in Phone settings.
3138 *
3139 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3140 * voicemail vibration setting.
3141 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
3142 */
3143 @Override
3144 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
3145 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
3146 if (phone == null) {
3147 return false;
3148 }
3149
3150 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone);
3151 }
3152
Youhan Wange64578a2016-05-02 15:32:42 -07003153 /**
3154 * Make sure either called from same process as self (phone) or IPC caller has read privilege.
3155 *
3156 * @throws SecurityException if the caller does not have the required permission
3157 */
3158 private void enforceReadPrivilegedPermission() {
3159 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
3160 null);
3161 }
3162
3163 /**
3164 * Return the application ID for the app type.
3165 *
3166 * @param subId the subscription ID that this request applies to.
3167 * @param appType the uicc app type.
3168 * @return Application ID for specificied app type, or null if no uicc.
3169 */
3170 @Override
3171 public String getAidForAppType(int subId, int appType) {
3172 enforceReadPrivilegedPermission();
3173 Phone phone = getPhone(subId);
3174 if (phone == null) {
3175 return null;
3176 }
3177 String aid = null;
3178 try {
3179 aid = UiccController.getInstance().getUiccCard(phone.getPhoneId())
3180 .getApplicationByType(appType).getAid();
3181 } catch (Exception e) {
3182 Log.e(LOG_TAG, "Not getting aid. Exception ex=" + e);
3183 }
3184 return aid;
3185 }
3186
Youhan Wang4001d252016-05-11 10:29:41 -07003187 /**
3188 * Return the Electronic Serial Number.
3189 *
3190 * @param subId the subscription ID that this request applies to.
3191 * @return ESN or null if error.
3192 */
3193 @Override
3194 public String getEsn(int subId) {
3195 enforceReadPrivilegedPermission();
3196 Phone phone = getPhone(subId);
3197 if (phone == null) {
3198 return null;
3199 }
3200 String esn = null;
3201 try {
3202 esn = phone.getEsn();
3203 } catch (Exception e) {
3204 Log.e(LOG_TAG, "Not getting ESN. Exception ex=" + e);
3205 }
3206 return esn;
3207 }
3208
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003209 /**
3210 * Get snapshot of Telephony histograms
3211 * @return List of Telephony histograms
3212 * @hide
3213 */
3214 @Override
3215 public List<TelephonyHistogram> getTelephonyHistograms() {
3216 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
3217 return RIL.getTelephonyRILTimingHistograms();
3218 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07003219
3220 /**
3221 * {@hide}
3222 * Set the allowed carrier list for slotId
3223 * Require system privileges. In the future we may add this to carrier APIs.
3224 *
3225 * @return The number of carriers set successfully, should match length of carriers
3226 */
3227 @Override
3228 public int setAllowedCarriers(int slotId, List<CarrierIdentifier> carriers) {
3229 enforceModifyPermission();
3230 int subId = SubscriptionManager.getSubId(slotId)[0];
3231 int[] retVal = (int[]) sendRequest(CMD_SET_ALLOWED_CARRIERS, carriers, subId);
3232 return retVal[0];
3233 }
3234
3235 /**
3236 * {@hide}
3237 * Get the allowed carrier list for slotId.
3238 * Require system privileges. In the future we may add this to carrier APIs.
3239 *
3240 * @return List of {@link android.service.telephony.CarrierIdentifier}; empty list
3241 * means all carriers are allowed.
3242 */
3243 @Override
3244 public List<CarrierIdentifier> getAllowedCarriers(int slotId) {
3245 enforceReadPrivilegedPermission();
3246 int subId = SubscriptionManager.getSubId(slotId)[0];
3247 return (List<CarrierIdentifier>) sendRequest(CMD_GET_ALLOWED_CARRIERS, null, subId);
3248 }
3249
fionaxu59545b42016-05-25 15:53:37 -07003250 /**
3251 * Action set from carrier signalling broadcast receivers to enable/disable metered apns
3252 * @param subId the subscription ID that this action applies to.
3253 * @param enabled control enable or disable metered apns.
3254 * {@hide}
3255 */
3256 @Override
3257 public void carrierActionSetMeteredApnsEnabled(int subId, boolean enabled) {
3258 enforceModifyPermission();
3259 final Phone phone = getPhone(subId);
3260 if (phone == null) {
3261 loge("carrierAction: SetMeteredApnsEnabled fails with invalid subId: " + subId);
3262 return;
3263 }
3264 try {
3265 phone.carrierActionSetMeteredApnsEnabled(enabled);
3266 } catch (Exception e) {
3267 Log.e(LOG_TAG, "carrierAction: SetMeteredApnsEnabled fails. Exception ex=" + e);
3268 }
3269 }
3270
3271 /**
3272 * Action set from carrier signalling broadcast receivers to enable/disable radio
3273 * @param subId the subscription ID that this action applies to.
3274 * @param enabled control enable or disable radio.
3275 * {@hide}
3276 */
3277 @Override
3278 public void carrierActionSetRadioEnabled(int subId, boolean enabled) {
3279 enforceModifyPermission();
3280 final Phone phone = getPhone(subId);
3281 if (phone == null) {
3282 loge("carrierAction: SetRadioEnabled fails with invalid sibId: " + subId);
3283 return;
3284 }
3285 try {
3286 phone.carrierActionSetRadioEnabled(enabled);
3287 } catch (Exception e) {
3288 Log.e(LOG_TAG, "carrierAction: SetRadioEnabled fails. Exception ex=" + e);
3289 }
3290 }
3291
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003292}