blob: 3e89abebcac75cdc2a87b88109132d5d84b1a630 [file] [log] [blame]
Alexander Dorokhine7abf60e2016-02-10 10:56:09 -08001#!/usr/bin/env python3.4
Ang Li73697b32015-12-03 00:41:53 +00002#
Alexander Dorokhine7abf60e2016-02-10 10:56:09 -08003# Copyright 2016 - Google
Ang Li73697b32015-12-03 00:41:53 +00004#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
Alexander Dorokhine642e63f2016-02-11 13:32:37 -080017from future import standard_library
18standard_library.install_aliases()
19
Ang Li73697b32015-12-03 00:41:53 +000020import concurrent.futures
Ang Li73697b32015-12-03 00:41:53 +000021import urllib.parse
22import time
Ang Li73697b32015-12-03 00:41:53 +000023
24from queue import Empty
Ang Li73697b32015-12-03 00:41:53 +000025from acts.controllers.android_device import AndroidDevice
Yang Liu46ed7222015-12-28 14:08:52 -080026from acts.controllers.event_dispatcher import EventDispatcher
Yang Liu9a9a4f72016-02-19 10:45:04 -080027from acts.test_utils.tel.tel_defines import AOSP_PREFIX
Yang Liu46ed7222015-12-28 14:08:52 -080028from acts.test_utils.tel.tel_defines import CARRIER_UNKNOWN
29from acts.test_utils.tel.tel_defines import DATA_STATE_CONNECTED
30from acts.test_utils.tel.tel_defines import DATA_STATE_DISCONNECTED
Yang Liu98fd9d72016-03-04 12:14:49 -080031from acts.test_utils.tel.tel_defines import GEN_4G
Yang Liu46ed7222015-12-28 14:08:52 -080032from acts.test_utils.tel.tel_defines import GEN_UNKNOWN
33from acts.test_utils.tel.tel_defines import INCALL_UI_DISPLAY_BACKGROUND
34from acts.test_utils.tel.tel_defines import INCALL_UI_DISPLAY_FOREGROUND
35from acts.test_utils.tel.tel_defines import INVALID_SIM_SLOT_INDEX
36from acts.test_utils.tel.tel_defines import INVALID_SUB_ID
37from acts.test_utils.tel.tel_defines import MAX_SAVED_VOICE_MAIL
38from acts.test_utils.tel.tel_defines import MAX_SCREEN_ON_TIME
Yang Liudf164e32016-01-07 16:49:32 -080039from acts.test_utils.tel.tel_defines import \
40 MAX_WAIT_TIME_ACCEPT_CALL_TO_OFFHOOK_EVENT
41from acts.test_utils.tel.tel_defines import MAX_WAIT_TIME_AIRPLANEMODE_EVENT
42from acts.test_utils.tel.tel_defines import MAX_WAIT_TIME_CALL_INITIATION
43from acts.test_utils.tel.tel_defines import MAX_WAIT_TIME_CALLEE_RINGING
44from acts.test_utils.tel.tel_defines import MAX_WAIT_TIME_CONNECTION_STATE_UPDATE
45from acts.test_utils.tel.tel_defines import MAX_WAIT_TIME_DATA_SUB_CHANGE
46from acts.test_utils.tel.tel_defines import MAX_WAIT_TIME_HANGUP_TO_IDLE_EVENT
47from acts.test_utils.tel.tel_defines import MAX_WAIT_TIME_NW_SELECTION
48from acts.test_utils.tel.tel_defines import MAX_WAIT_TIME_SMS_RECEIVE
49from acts.test_utils.tel.tel_defines import MAX_WAIT_TIME_SMS_SENT_SUCCESS
Yang Liu855d5f82016-01-27 15:35:48 -080050from acts.test_utils.tel.tel_defines import MAX_WAIT_TIME_TELECOM_RINGING
Yang Liudf164e32016-01-07 16:49:32 -080051from acts.test_utils.tel.tel_defines import MAX_WAIT_TIME_VOICE_MAIL_COUNT
Yang Liudfc37b62016-01-20 18:08:47 -080052from acts.test_utils.tel.tel_defines import MAX_WAIT_TIME_WFC_DISABLED
53from acts.test_utils.tel.tel_defines import MAX_WAIT_TIME_WFC_ENABLED
Yang Liue51e93d2015-12-30 10:45:42 -080054from acts.test_utils.tel.tel_defines import NETWORK_MODE_LTE_ONLY
Yang Liu46ed7222015-12-28 14:08:52 -080055from acts.test_utils.tel.tel_defines import NETWORK_CONNECTION_TYPE_CELL
56from acts.test_utils.tel.tel_defines import NETWORK_CONNECTION_TYPE_WIFI
57from acts.test_utils.tel.tel_defines import NETWORK_SERVICE_DATA
58from acts.test_utils.tel.tel_defines import NETWORK_SERVICE_VOICE
59from acts.test_utils.tel.tel_defines import PHONE_NUMBER_STRING_FORMAT_7_DIGIT
60from acts.test_utils.tel.tel_defines import PHONE_NUMBER_STRING_FORMAT_10_DIGIT
61from acts.test_utils.tel.tel_defines import PHONE_NUMBER_STRING_FORMAT_11_DIGIT
62from acts.test_utils.tel.tel_defines import PHONE_NUMBER_STRING_FORMAT_12_DIGIT
63from acts.test_utils.tel.tel_defines import RAT_FAMILY_GSM
64from acts.test_utils.tel.tel_defines import RAT_FAMILY_LTE
65from acts.test_utils.tel.tel_defines import RAT_FAMILY_WLAN
Yang Liucde45aa2016-02-22 13:46:41 -080066from acts.test_utils.tel.tel_defines import RAT_FAMILY_WCDMA
Yang Liu46ed7222015-12-28 14:08:52 -080067from acts.test_utils.tel.tel_defines import RAT_1XRTT
68from acts.test_utils.tel.tel_defines import RAT_UNKNOWN
69from acts.test_utils.tel.tel_defines import SERVICE_STATE_EMERGENCY_ONLY
70from acts.test_utils.tel.tel_defines import SERVICE_STATE_IN_SERVICE
71from acts.test_utils.tel.tel_defines import SERVICE_STATE_OUT_OF_SERVICE
72from acts.test_utils.tel.tel_defines import SERVICE_STATE_POWER_OFF
73from acts.test_utils.tel.tel_defines import SIM_STATE_READY
74from acts.test_utils.tel.tel_defines import TELEPHONY_STATE_IDLE
75from acts.test_utils.tel.tel_defines import TELEPHONY_STATE_OFFHOOK
76from acts.test_utils.tel.tel_defines import TELEPHONY_STATE_RINGING
77from acts.test_utils.tel.tel_defines import VOICEMAIL_DELETE_DIGIT
Yang Liudf164e32016-01-07 16:49:32 -080078from acts.test_utils.tel.tel_defines import WAIT_TIME_1XRTT_VOICE_ATTACH
Yang Liu46ed7222015-12-28 14:08:52 -080079from acts.test_utils.tel.tel_defines import WAIT_TIME_ANDROID_STATE_SETTLING
Yang Liu46ed7222015-12-28 14:08:52 -080080from acts.test_utils.tel.tel_defines import WAIT_TIME_IN_CALL
Yang Liudf164e32016-01-07 16:49:32 -080081from acts.test_utils.tel.tel_defines import WAIT_TIME_LEAVE_VOICE_MAIL
Yang Liu46ed7222015-12-28 14:08:52 -080082from acts.test_utils.tel.tel_defines import WAIT_TIME_REJECT_CALL
Yang Liudf164e32016-01-07 16:49:32 -080083from acts.test_utils.tel.tel_defines import WAIT_TIME_VOICE_MAIL_SERVER_RESPONSE
Yang Liu46ed7222015-12-28 14:08:52 -080084from acts.test_utils.tel.tel_defines import WFC_MODE_DISABLED
85from acts.test_utils.tel.tel_defines import EventCallStateChanged
86from acts.test_utils.tel.tel_defines import EventConnectivityChanged
87from acts.test_utils.tel.tel_defines import EventDataConnectionStateChanged
88from acts.test_utils.tel.tel_defines import EventDataSmsReceived
89from acts.test_utils.tel.tel_defines import EventMessageWaitingIndicatorChanged
90from acts.test_utils.tel.tel_defines import EventServiceStateChanged
91from acts.test_utils.tel.tel_defines import EventMmsSentSuccess
92from acts.test_utils.tel.tel_defines import EventSmsReceived
93from acts.test_utils.tel.tel_defines import EventSmsSentSuccess
Yang Liudc8564b2016-01-27 14:15:37 -080094from acts.test_utils.tel.tel_defines import CallStateContainer
Yang Liu8e6adff2016-02-05 10:24:04 -080095from acts.test_utils.tel.tel_defines import DataConnectionStateContainer
Yang Liudc8564b2016-01-27 14:15:37 -080096from acts.test_utils.tel.tel_defines import MessageWaitingIndicatorContainer
Yang Liu8e6adff2016-02-05 10:24:04 -080097from acts.test_utils.tel.tel_defines import NetworkCallbackContainer
98from acts.test_utils.tel.tel_defines import ServiceStateContainer
Yang Liu46ed7222015-12-28 14:08:52 -080099from acts.test_utils.tel.tel_lookup_tables import \
100 connection_type_from_type_string
101from acts.test_utils.tel.tel_lookup_tables import is_valid_rat
Yang Liu98fd9d72016-03-04 12:14:49 -0800102from acts.test_utils.tel.tel_lookup_tables import get_allowable_network_preference
Yang Liu46ed7222015-12-28 14:08:52 -0800103from acts.test_utils.tel.tel_lookup_tables import \
104 get_voice_mail_count_check_function
105from acts.test_utils.tel.tel_lookup_tables import get_voice_mail_number_function
106from acts.test_utils.tel.tel_lookup_tables import \
107 network_preference_for_generaton
108from acts.test_utils.tel.tel_lookup_tables import operator_name_from_plmn_id
109from acts.test_utils.tel.tel_lookup_tables import \
110 rat_families_for_network_preference
111from acts.test_utils.tel.tel_lookup_tables import rat_family_for_generation
Yang Liu7a2e7ee2015-12-28 15:32:44 -0800112from acts.test_utils.tel.tel_lookup_tables import rat_family_from_rat
113from acts.test_utils.tel.tel_lookup_tables import rat_generation_from_rat
Yang Liu46ed7222015-12-28 14:08:52 -0800114from acts.utils import load_config
Ang Li73697b32015-12-03 00:41:53 +0000115from acts.logger import LoggerProxy
116log = LoggerProxy()
117
Nathan Harold123c9da2015-12-30 16:33:25 -0800118
Ang Li73697b32015-12-03 00:41:53 +0000119class TelTestUtilsError(Exception):
120 pass
121
Nathan Harold123c9da2015-12-30 16:33:25 -0800122
Ang Li73697b32015-12-03 00:41:53 +0000123def setup_droid_properties(log, ad, sim_filename):
124
125 # Check to see if droid already has this property
126 if hasattr(ad, 'cfg'):
127 return
128
129 device_props = {}
130 device_props['subscription'] = {}
131
132 try:
133 sim_data = load_config(sim_filename)
134 except Exception:
135 log.warning("Failed to load {}!".format(sim_filename))
136 sim_data = None
137 sub_info_list = ad.droid.subscriptionGetAllSubInfoList()
138 found_sims = 0
139 for sub_info in sub_info_list:
140 sub_id = sub_info['subscriptionId']
141 if sub_info['simSlotIndex'] is not INVALID_SIM_SLOT_INDEX:
142 found_sims += 1
143 sim_record = {}
144 try:
Nathan Harold123c9da2015-12-30 16:33:25 -0800145 sim_serial = ad.droid.telephonyGetSimSerialNumberForSubscription(
146 sub_id)
Ang Li73697b32015-12-03 00:41:53 +0000147 if not sim_serial:
148 log.error("Unable to find ICC-ID for SIM on {}!".format(
149 ad.serial))
150 if sim_data is not None:
151 number = sim_data[sim_serial]["phone_num"]
152 else:
153 raise KeyError("No file to load phone number info!")
154 except KeyError:
Nathan Harold123c9da2015-12-30 16:33:25 -0800155 number = ad.droid.telephonyGetLine1NumberForSubscription(
156 sub_id)
Ang Li73697b32015-12-03 00:41:53 +0000157 if not number or number == "":
158 raise TelTestUtilsError(
159 "Failed to find valid phone number for {}"
160 .format(ad.serial))
161
162 sim_record['phone_num'] = number
163 sim_record['operator'] = get_operator_name(log, ad, sub_id)
164 device_props['subscription'][sub_id] = sim_record
Nathan Harold123c9da2015-12-30 16:33:25 -0800165 log.info(
166 "phone_info: <{}:{}>, <subId:{}> {} <{}>, ICC-ID:<{}>".format(
167 ad.model, ad.serial, sub_id, number, get_operator_name(
168 log, ad, sub_id),
169 ad.droid.telephonyGetSimSerialNumberForSubscription(
170 sub_id)))
Ang Li73697b32015-12-03 00:41:53 +0000171
172 if found_sims == 0:
173 log.warning("No Valid SIMs found in device {}".format(ad.serial))
174
175 setattr(ad, 'cfg', device_props)
176
Yang Liu6c70e022016-03-07 16:19:42 -0800177def refresh_droid_config(log, ad):
178 """ Update Android Device cfg records for each sub_id.
179 1. Update Phone Number using Line1Number (if Line1Number is valid).
180 2. Update Operator name.
Yang Liuad9c63d2016-02-25 13:59:23 -0800181
182 Args:
183 log: log object
184 ad: android device object
Yang Liuad9c63d2016-02-25 13:59:23 -0800185
186 Returns:
Yang Liu6c70e022016-03-07 16:19:42 -0800187 None
Yang Liuad9c63d2016-02-25 13:59:23 -0800188 """
Yang Liu6c70e022016-03-07 16:19:42 -0800189 for sub_id in ad.cfg['subscription']:
190 # Update Phone number
191 number = ad.droid.telephonyGetLine1NumberForSubscription(sub_id)
192 if number:
193 number = phone_number_formatter(number)
194 ad.cfg['subscription'][sub_id]['phone_num'] = number
195 # Update Operator Name
196 ad.cfg['subscription'][sub_id]['operator'] = get_operator_name(
197 log, ad, sub_id)
Nathan Harold123c9da2015-12-30 16:33:25 -0800198
Ang Li73697b32015-12-03 00:41:53 +0000199def get_subid_from_slot_index(log, ad, sim_slot_index):
200 """ Get the subscription ID for a SIM at a particular slot
201
202 Args:
203 ad: android_device object.
204
205 Returns:
206 result: Subscription ID
207 """
208 subInfo = ad.droid.subscriptionGetAllSubInfoList()
209 for info in subInfo:
210 if info['simSlotIndex'] == sim_slot_index:
211 return info['subscriptionId']
212 return INVALID_SUB_ID
213
Yang Liu7ce19982016-03-09 12:20:41 -0800214def get_slot_index_from_subid(log, ad, sub_id):
215 try:
216 info = ad.droid.subscriptionGetSubInfoForSubscriber(sub_id)
217 return info['simSlotIndex']
tturney10eda2e2016-03-30 19:55:57 -0700218 except KeyError:
Yang Liu7ce19982016-03-09 12:20:41 -0800219 return INVALID_SIM_SLOT_INDEX
220
Nathan Harold123c9da2015-12-30 16:33:25 -0800221
Ang Li73697b32015-12-03 00:41:53 +0000222def get_num_active_sims(log, ad):
223 """ Get the number of active SIM cards by counting slots
224
225 Args:
226 ad: android_device object.
227
228 Returns:
229 result: The number of loaded (physical) SIM cards
230 """
231 # using a dictionary as a cheap way to prevent double counting
232 # in the situation where multiple subscriptions are on the same SIM.
233 # yes, this is a corner corner case.
234 valid_sims = {}
235 subInfo = ad.droid.subscriptionGetAllSubInfoList()
236 for info in subInfo:
237 ssidx = info['simSlotIndex']
238 if ssidx == INVALID_SIM_SLOT_INDEX:
239 continue
240 valid_sims[ssidx] = True
241 return len(valid_sims.keys())
242
Nathan Harold123c9da2015-12-30 16:33:25 -0800243
Ang Li73697b32015-12-03 00:41:53 +0000244def toggle_airplane_mode(log, ad, new_state=None):
245 """ Toggle the state of airplane mode.
246
247 Args:
248 ad: android_device object.
249 new_state: Airplane mode state to set to.
250 If None, opposite of the current state.
251
252 Returns:
253 result: True if operation succeed. False if error happens.
254 """
255 return toggle_airplane_mode_msim(log, ad, new_state)
256
Nathan Harold123c9da2015-12-30 16:33:25 -0800257
Ang Li73697b32015-12-03 00:41:53 +0000258def is_expected_event(event_to_check, events_list):
259 """ check whether event is present in the event list
260
261 Args:
262 event_to_check: event to be checked.
263 events_list: list of events
264 Returns:
265 result: True if event present in the list. False if not.
266 """
267 for event in events_list:
268 if event in event_to_check['name']:
269 return True
270 return False
271
Nathan Harold123c9da2015-12-30 16:33:25 -0800272
Ang Li73697b32015-12-03 00:41:53 +0000273def is_sim_ready(log, ad, sim_slot_id=None):
274 """ check whether SIM is ready.
275
276 Args:
277 ad: android_device object.
278 sim_slot_id: check the SIM status for sim_slot_id
279 This is optional. If this is None, check default SIM.
280
281 Returns:
282 result: True if all SIMs are ready. False if not.
283 """
284 if sim_slot_id is None:
Yang Liuaed3eef2015-12-15 18:40:25 -0800285 status = ad.droid.telephonyGetSimState()
Ang Li73697b32015-12-03 00:41:53 +0000286 else:
Yang Liuaed3eef2015-12-15 18:40:25 -0800287 status = ad.droid.telephonyGetSimStateForSlotId(sim_slot_id)
Ang Li73697b32015-12-03 00:41:53 +0000288 if status != SIM_STATE_READY:
Nathan Harold123c9da2015-12-30 16:33:25 -0800289 log.info("Sim not ready")
290 return False
Ang Li73697b32015-12-03 00:41:53 +0000291 return True
292
Nathan Harold123c9da2015-12-30 16:33:25 -0800293
Ang Li73697b32015-12-03 00:41:53 +0000294def _is_expecting_event(event_recv_list):
295 """ check for more event is expected in event list
296
297 Args:
298 event_recv_list: list of events
299 Returns:
300 result: True if more events are expected. False if not.
301 """
302 for state in event_recv_list:
303 if state is False:
Nathan Harold123c9da2015-12-30 16:33:25 -0800304 return True
Ang Li73697b32015-12-03 00:41:53 +0000305 return False
306
Nathan Harold123c9da2015-12-30 16:33:25 -0800307
308def _set_event_list(event_recv_list, sub_id_list, sub_id, value):
Ang Li73697b32015-12-03 00:41:53 +0000309 """ set received event in expected event list
310
311 Args:
312 event_recv_list: list of received events
313 sub_id_list: subscription ID list
314 sub_id: subscription id of current event
315 value: True or False
316 Returns:
317 None.
318 """
319 for i in range(len(sub_id_list)):
320 if sub_id_list[i] == sub_id:
321 event_recv_list[i] = value
322
Nathan Harold123c9da2015-12-30 16:33:25 -0800323
Ang Li73697b32015-12-03 00:41:53 +0000324def toggle_airplane_mode_msim(log, ad, new_state=None):
325 """ Toggle the state of airplane mode.
326
327 Args:
328 ad: android_device object.
329 new_state: Airplane mode state to set to.
330 If None, opposite of the current state.
331
332 Returns:
333 result: True if operation succeed. False if error happens.
334 """
335 serial_number = ad.serial
336
337 ad.ed.clear_all_events()
338 sub_id_list = []
339
340 active_sub_info = ad.droid.subscriptionGetAllSubInfoList()
Nathan Harold123c9da2015-12-30 16:33:25 -0800341 for info in active_sub_info:
Ang Li73697b32015-12-03 00:41:53 +0000342 sub_id_list.append(info['subscriptionId'])
343
344 cur_state = ad.droid.connectivityCheckAirplaneMode()
345 if cur_state == new_state:
346 log.info("Airplane mode already <{}> on {}".format(new_state,
Nathan Harold123c9da2015-12-30 16:33:25 -0800347 serial_number))
Ang Li73697b32015-12-03 00:41:53 +0000348 return True
349 elif new_state is None:
350 log.info("Current State {} New state {}".format(cur_state, new_state))
351
352 if new_state is None:
353 new_state = not cur_state
354
Yang Liu8e6adff2016-02-05 10:24:04 -0800355 service_state_list = []
Ang Li73697b32015-12-03 00:41:53 +0000356 if new_state:
Yang Liu8e6adff2016-02-05 10:24:04 -0800357 service_state_list.append(SERVICE_STATE_POWER_OFF)
Ang Li73697b32015-12-03 00:41:53 +0000358 log.info("Turn on airplane mode: " + serial_number)
359
360 else:
361 # If either one of these 3 events show up, it should be OK.
362 # Normal SIM, phone in service
Yang Liu8e6adff2016-02-05 10:24:04 -0800363 service_state_list.append(SERVICE_STATE_IN_SERVICE)
Ang Li73697b32015-12-03 00:41:53 +0000364 # NO SIM, or Dead SIM, or no Roaming coverage.
Yang Liu8e6adff2016-02-05 10:24:04 -0800365 service_state_list.append(SERVICE_STATE_OUT_OF_SERVICE)
366 service_state_list.append(SERVICE_STATE_EMERGENCY_ONLY)
Ang Li73697b32015-12-03 00:41:53 +0000367 log.info("Turn off airplane mode: " + serial_number)
368
369 for sub_id in sub_id_list:
Nathan Harold123c9da2015-12-30 16:33:25 -0800370 ad.droid.telephonyStartTrackingServiceStateChangeForSubscription(
371 sub_id)
Ang Li73697b32015-12-03 00:41:53 +0000372 ad.droid.connectivityToggleAirplaneMode(new_state)
373
374 event = None
375
376 try:
377 try:
378 event = ad.ed.wait_for_event(EventServiceStateChanged,
Yang Liu8e6adff2016-02-05 10:24:04 -0800379 is_event_match_for_list,
Yang Liudf164e32016-01-07 16:49:32 -0800380 timeout=MAX_WAIT_TIME_AIRPLANEMODE_EVENT,
Yang Liu8e6adff2016-02-05 10:24:04 -0800381 field=ServiceStateContainer.SERVICE_STATE,
382 value_list=service_state_list)
Ang Li73697b32015-12-03 00:41:53 +0000383 except Empty:
384 pass
385 if event is None:
Yang Liu8e6adff2016-02-05 10:24:04 -0800386 log.error("Did not get expected service state {}".format(
387 service_state_list))
Ang Li73697b32015-12-03 00:41:53 +0000388 log.info("Received event: {}".format(event))
389 finally:
390 for sub_id in sub_id_list:
Nathan Harold123c9da2015-12-30 16:33:25 -0800391 ad.droid.telephonyStopTrackingServiceStateChangeForSubscription(
392 sub_id)
Ang Li73697b32015-12-03 00:41:53 +0000393
394 if new_state:
395 if (not ad.droid.connectivityCheckAirplaneMode() or
Nathan Harold123c9da2015-12-30 16:33:25 -0800396 ad.droid.wifiCheckState() or ad.droid.bluetoothCheckState()):
Ang Li73697b32015-12-03 00:41:53 +0000397 log.error("Airplane mode ON fail on {}".format(ad.serial))
398 return False
399 else:
400 if ad.droid.connectivityCheckAirplaneMode():
401 log.error("Airplane mode OFF fail on {}".format(ad.serial))
402 return False
403 return True
404
Nathan Harold123c9da2015-12-30 16:33:25 -0800405
406def wait_and_answer_call(log,
407 ad,
408 incoming_number=None,
Ang Li73697b32015-12-03 00:41:53 +0000409 incall_ui_display=INCALL_UI_DISPLAY_FOREGROUND):
410 """Wait for an incoming call on default voice subscription and
411 accepts the call.
412
413 Args:
414 ad: android device object.
415 incoming_number: Expected incoming number.
416 Optional. Default is None
Ang Li73697b32015-12-03 00:41:53 +0000417 incall_ui_display: after answer the call, bring in-call UI to foreground or
418 background. Optional, default value is INCALL_UI_DISPLAY_FOREGROUND.
419 if = INCALL_UI_DISPLAY_FOREGROUND, bring in-call UI to foreground.
420 if = INCALL_UI_DISPLAY_BACKGROUND, bring in-call UI to background.
421 else, do nothing.
422
423 Returns:
424 True: if incoming call is received and answered successfully.
425 False: for errors
426 """
427 return wait_and_answer_call_for_subscription(
Nathan Harold123c9da2015-12-30 16:33:25 -0800428 log, ad, ad.droid.subscriptionGetDefaultVoiceSubId(), incoming_number,
Yang Liu855d5f82016-01-27 15:35:48 -0800429 incall_ui_display)
Nathan Harold123c9da2015-12-30 16:33:25 -0800430
Ang Li73697b32015-12-03 00:41:53 +0000431
432def wait_for_ringing_event(log, ad, wait_time):
433 """Wait for ringing event.
434
435 Args:
436 log: log object.
437 ad: android device object.
438 wait_time: max time to wait for ringing event.
439
440 Returns:
441 event_ringing if received ringing event.
442 otherwise return None.
443 """
444 log.info("Wait for ringing.")
445 start_time = time.time()
446 remaining_time = wait_time
447 event_iter_timeout = 4
448 event_ringing = None
449
450 while remaining_time > 0:
451 try:
Nathan Harold123c9da2015-12-30 16:33:25 -0800452 event_ringing = ad.ed.wait_for_event(
453 EventCallStateChanged,
Yang Liu8e6adff2016-02-05 10:24:04 -0800454 is_event_match,
Nathan Harold123c9da2015-12-30 16:33:25 -0800455 timeout=event_iter_timeout,
Yang Liu8e6adff2016-02-05 10:24:04 -0800456 field=CallStateContainer.CALL_STATE,
457 value=TELEPHONY_STATE_RINGING)
Ang Li73697b32015-12-03 00:41:53 +0000458 except Empty:
459 if ad.droid.telecomIsRinging():
460 log.error("No Ringing event. But Callee in Ringing state.")
461 log.error("Test framework dropped event.")
462 return None
463 remaining_time = start_time + wait_time - time.time()
464 if event_ringing is not None:
465 break
466 if event_ringing is None:
467 log.error("No Ringing Event, Callee not in ringing state.")
468 log.error("No incoming call.")
469 return None
470
471 return event_ringing
472
Nathan Harold123c9da2015-12-30 16:33:25 -0800473
474def wait_and_answer_call_for_subscription(
475 log,
476 ad,
477 sub_id,
478 incoming_number=None,
Nathan Harold123c9da2015-12-30 16:33:25 -0800479 incall_ui_display=INCALL_UI_DISPLAY_FOREGROUND):
Ang Li73697b32015-12-03 00:41:53 +0000480 """Wait for an incoming call on specified subscription and
481 accepts the call.
482
483 Args:
484 ad: android device object.
485 sub_id: subscription ID
486 incoming_number: Expected incoming number.
487 Optional. Default is None
Ang Li73697b32015-12-03 00:41:53 +0000488 incall_ui_display: after answer the call, bring in-call UI to foreground or
489 background. Optional, default value is INCALL_UI_DISPLAY_FOREGROUND.
490 if = INCALL_UI_DISPLAY_FOREGROUND, bring in-call UI to foreground.
491 if = INCALL_UI_DISPLAY_BACKGROUND, bring in-call UI to background.
492 else, do nothing.
493
494 Returns:
495 True: if incoming call is received and answered successfully.
496 False: for errors
497 """
498 ad.ed.clear_all_events()
Yang Liuaed3eef2015-12-15 18:40:25 -0800499 ad.droid.telephonyStartTrackingCallStateForSubscription(sub_id)
Ang Li73697b32015-12-03 00:41:53 +0000500 if (not ad.droid.telecomIsRinging() and
Nathan Harold123c9da2015-12-30 16:33:25 -0800501 ad.droid.telephonyGetCallStateForSubscription(sub_id) !=
502 TELEPHONY_STATE_RINGING):
Ang Li73697b32015-12-03 00:41:53 +0000503 try:
504 event_ringing = wait_for_ringing_event(log, ad,
Yang Liudf164e32016-01-07 16:49:32 -0800505 MAX_WAIT_TIME_CALLEE_RINGING)
Ang Li73697b32015-12-03 00:41:53 +0000506 if event_ringing is None:
507 log.error("No Ringing Event.")
508 return False
509 finally:
Nathan Harold123c9da2015-12-30 16:33:25 -0800510 ad.droid.telephonyStopTrackingCallStateChangeForSubscription(
511 sub_id)
Ang Li73697b32015-12-03 00:41:53 +0000512
513 if not incoming_number:
514 result = True
515 else:
Nathan Harold123c9da2015-12-30 16:33:25 -0800516 result = check_phone_number_match(
Yang Liudc8564b2016-01-27 14:15:37 -0800517 event_ringing['data'][CallStateContainer.INCOMING_NUMBER],
518 incoming_number)
Ang Li73697b32015-12-03 00:41:53 +0000519
520 if not result:
521 log.error("Incoming Number not match")
Nathan Harold123c9da2015-12-30 16:33:25 -0800522 log.error("Expected number:{}, actual number:{}".format(
Yang Liudc8564b2016-01-27 14:15:37 -0800523 incoming_number,
524 event_ringing['data'][CallStateContainer.INCOMING_NUMBER]))
Ang Li73697b32015-12-03 00:41:53 +0000525 return False
526
527 ad.ed.clear_all_events()
Yang Liuaed3eef2015-12-15 18:40:25 -0800528 ad.droid.telephonyStartTrackingCallStateForSubscription(sub_id)
Yang Liu855d5f82016-01-27 15:35:48 -0800529 if not wait_for_telecom_ringing(log, ad, MAX_WAIT_TIME_TELECOM_RINGING):
530 log.error("Telecom is not ringing.")
531 return False
Ang Li73697b32015-12-03 00:41:53 +0000532 log.info("Accept on callee.")
533 ad.droid.telecomAcceptRingingCall()
534 try:
535 ad.ed.wait_for_event(EventCallStateChanged,
Yang Liu8e6adff2016-02-05 10:24:04 -0800536 is_event_match,
Yang Liudf164e32016-01-07 16:49:32 -0800537 timeout=MAX_WAIT_TIME_ACCEPT_CALL_TO_OFFHOOK_EVENT,
Yang Liu8e6adff2016-02-05 10:24:04 -0800538 field=CallStateContainer.CALL_STATE,
539 value=TELEPHONY_STATE_OFFHOOK)
Ang Li73697b32015-12-03 00:41:53 +0000540 except Empty:
541 if not ad.droid.telecomIsInCall():
542 log.error("Accept call failed.")
543 return False
544 finally:
Yang Liuaed3eef2015-12-15 18:40:25 -0800545 ad.droid.telephonyStopTrackingCallStateChangeForSubscription(sub_id)
Ang Li73697b32015-12-03 00:41:53 +0000546 if incall_ui_display == INCALL_UI_DISPLAY_FOREGROUND:
547 ad.droid.telecomShowInCallScreen()
548 elif incall_ui_display == INCALL_UI_DISPLAY_BACKGROUND:
549 ad.droid.showHomeScreen()
550 return True
551
Nathan Harold123c9da2015-12-30 16:33:25 -0800552
553def wait_and_reject_call(log,
554 ad,
555 incoming_number=None,
Ang Li73697b32015-12-03 00:41:53 +0000556 delay_reject=WAIT_TIME_REJECT_CALL):
557 """Wait for an incoming call on default voice subscription and
558 reject the call.
559
560 Args:
561 ad: android device object.
562 incoming_number: Expected incoming number.
563 Optional. Default is None
564 delay_reject: time to wait before rejecting the call
565 Optional. Default is WAIT_TIME_REJECT_CALL
566
567 Returns:
568 True: if incoming call is received and reject successfully.
569 False: for errors
570 """
571 return wait_and_reject_call_for_subscription(
Nathan Harold123c9da2015-12-30 16:33:25 -0800572 log, ad, ad.droid.subscriptionGetDefaultVoiceSubId(), incoming_number,
573 delay_reject)
Ang Li73697b32015-12-03 00:41:53 +0000574
Nathan Harold123c9da2015-12-30 16:33:25 -0800575
576def wait_and_reject_call_for_subscription(log,
577 ad,
578 sub_id,
579 incoming_number=None,
Ang Li73697b32015-12-03 00:41:53 +0000580 delay_reject=WAIT_TIME_REJECT_CALL):
581 """Wait for an incoming call on specific subscription and
582 reject the call.
583
584 Args:
585 ad: android device object.
586 sub_id: subscription ID
587 incoming_number: Expected incoming number.
588 Optional. Default is None
589 delay_reject: time to wait before rejecting the call
590 Optional. Default is WAIT_TIME_REJECT_CALL
591
592 Returns:
593 True: if incoming call is received and reject successfully.
594 False: for errors
595 """
596 ad.ed.clear_all_events()
Yang Liuaed3eef2015-12-15 18:40:25 -0800597 ad.droid.telephonyStartTrackingCallStateForSubscription(sub_id)
Ang Li73697b32015-12-03 00:41:53 +0000598 if (not ad.droid.telecomIsRinging() and
Nathan Harold123c9da2015-12-30 16:33:25 -0800599 ad.droid.telephonyGetCallStateForSubscription(sub_id) !=
600 TELEPHONY_STATE_RINGING):
Ang Li73697b32015-12-03 00:41:53 +0000601 try:
602 event_ringing = wait_for_ringing_event(log, ad,
Yang Liudf164e32016-01-07 16:49:32 -0800603 MAX_WAIT_TIME_CALLEE_RINGING)
Ang Li73697b32015-12-03 00:41:53 +0000604 if event_ringing is None:
605 log.error("No Ringing Event.")
606 return False
607 finally:
Nathan Harold123c9da2015-12-30 16:33:25 -0800608 ad.droid.telephonyStopTrackingCallStateChangeForSubscription(
609 sub_id)
Ang Li73697b32015-12-03 00:41:53 +0000610
611 if not incoming_number:
612 result = True
613 else:
Nathan Harold123c9da2015-12-30 16:33:25 -0800614 result = check_phone_number_match(
Yang Liudc8564b2016-01-27 14:15:37 -0800615 event_ringing['data'][CallStateContainer.INCOMING_NUMBER],
616 incoming_number)
Ang Li73697b32015-12-03 00:41:53 +0000617
618 if not result:
619 log.error("Incoming Number not match")
Nathan Harold123c9da2015-12-30 16:33:25 -0800620 log.error("Expected number:{}, actual number:{}".format(
Yang Liudc8564b2016-01-27 14:15:37 -0800621 incoming_number,
622 event_ringing['data'][CallStateContainer.INCOMING_NUMBER]))
Ang Li73697b32015-12-03 00:41:53 +0000623 return False
624
625 ad.ed.clear_all_events()
Yang Liuaed3eef2015-12-15 18:40:25 -0800626 ad.droid.telephonyStartTrackingCallStateForSubscription(sub_id)
Ang Li73697b32015-12-03 00:41:53 +0000627 # Delay between ringing and reject.
628 time.sleep(delay_reject)
629
630 log.info("Reject on callee.")
631 ad.droid.telecomEndCall()
632 try:
633 ad.ed.wait_for_event(EventCallStateChanged,
Yang Liu8e6adff2016-02-05 10:24:04 -0800634 is_event_match,
Yang Liudf164e32016-01-07 16:49:32 -0800635 timeout=MAX_WAIT_TIME_HANGUP_TO_IDLE_EVENT,
Yang Liu8e6adff2016-02-05 10:24:04 -0800636 field=CallStateContainer.CALL_STATE,
637 value=TELEPHONY_STATE_IDLE)
Ang Li73697b32015-12-03 00:41:53 +0000638 except Empty:
639 log.error("No onCallStateChangedIdle event received.")
640 return False
641 finally:
Yang Liuaed3eef2015-12-15 18:40:25 -0800642 ad.droid.telephonyStopTrackingCallStateChangeForSubscription(sub_id)
Ang Li73697b32015-12-03 00:41:53 +0000643 return True
644
Nathan Harold123c9da2015-12-30 16:33:25 -0800645
Ang Li73697b32015-12-03 00:41:53 +0000646def hangup_call(log, ad):
647 """Hang up ongoing active call.
648 """
649 ad.ed.clear_all_events()
Yang Liuaed3eef2015-12-15 18:40:25 -0800650 ad.droid.telephonyStartTrackingCallState()
Ang Li73697b32015-12-03 00:41:53 +0000651 log.info("Hangup call.")
652 ad.droid.telecomEndCall()
653
654 try:
655 ad.ed.wait_for_event(EventCallStateChanged,
Yang Liu8e6adff2016-02-05 10:24:04 -0800656 is_event_match,
Yang Liudf164e32016-01-07 16:49:32 -0800657 timeout=MAX_WAIT_TIME_HANGUP_TO_IDLE_EVENT,
Yang Liu8e6adff2016-02-05 10:24:04 -0800658 field=CallStateContainer.CALL_STATE,
659 value=TELEPHONY_STATE_IDLE)
Ang Li73697b32015-12-03 00:41:53 +0000660 except Empty:
661 if ad.droid.telecomIsInCall():
662 log.error("Hangup call failed.")
663 return False
664 finally:
Yang Liuaed3eef2015-12-15 18:40:25 -0800665 ad.droid.telephonyStopTrackingCallStateChange()
Ang Li73697b32015-12-03 00:41:53 +0000666 return True
667
Nathan Harold123c9da2015-12-30 16:33:25 -0800668
Ang Li73697b32015-12-03 00:41:53 +0000669def disconnect_call_by_id(log, ad, call_id):
670 """Disconnect call by call id.
671 """
672 ad.droid.telecomCallDisconnect(call_id)
673 return True
674
Nathan Harold123c9da2015-12-30 16:33:25 -0800675
Ang Li73697b32015-12-03 00:41:53 +0000676def check_phone_number_match(number1, number2):
677 """Check whether two input phone numbers match or not.
678
679 Compare the two input phone numbers.
680 If they match, return True; otherwise, return False.
681 Currently only handle phone number with the following formats:
682 (US phone number format)
683 +1abcxxxyyyy
684 1abcxxxyyyy
685 abcxxxyyyy
686 abc xxx yyyy
687 abc.xxx.yyyy
688 abc-xxx-yyyy
689
690 Args:
691 number1: 1st phone number to be compared.
692 number2: 2nd phone number to be compared.
693
694 Returns:
695 True if two phone numbers match. Otherwise False.
696 """
697 # Remove "1" or "+1"from front
698 if number1[0] == "1":
699 number1 = number1[1:]
700 elif number1[0:2] == "+1":
701 number1 = number1[2:]
702 if number2[0] == "1":
703 number2 = number2[1:]
704 elif number2[0:2] == "+1":
705 number2 = number2[2:]
706 # Remove white spaces, dashes, dots
707 number1 = number1.replace(" ", "").replace("-", "").replace(".", "")
708 number2 = number2.replace(" ", "").replace("-", "").replace(".", "")
709 return number1 == number2
710
711
712def initiate_call(log, ad_caller, callee_number, emergency=False):
713 """Make phone call from caller to callee.
714
715 Args:
716 ad_caller: Caller android device object.
717 callee_number: Callee phone number.
718 emergency : specify the call is emergency.
719 Optional. Default value is False.
720
721 Returns:
722 result: if phone call is placed successfully.
723 """
724 ad_caller.ed.clear_all_events()
725 sub_id = ad_caller.droid.subscriptionGetDefaultVoiceSubId()
Yang Liuaed3eef2015-12-15 18:40:25 -0800726 ad_caller.droid.telephonyStartTrackingCallStateForSubscription(sub_id)
Ang Li73697b32015-12-03 00:41:53 +0000727
Yang Liudf164e32016-01-07 16:49:32 -0800728 wait_time_for_incall_state = MAX_WAIT_TIME_CALL_INITIATION
Ang Li73697b32015-12-03 00:41:53 +0000729
730 try:
731 # Make a Call
732 if emergency:
Yang Liuaed3eef2015-12-15 18:40:25 -0800733 ad_caller.droid.telecomCallEmergencyNumber(callee_number)
Ang Li73697b32015-12-03 00:41:53 +0000734 else:
Yang Liuaed3eef2015-12-15 18:40:25 -0800735 ad_caller.droid.telecomCallNumber(callee_number)
Ang Li73697b32015-12-03 00:41:53 +0000736
737 # Verify OFFHOOK event
Yang Liuaed3eef2015-12-15 18:40:25 -0800738 if ad_caller.droid.telephonyGetCallState() != TELEPHONY_STATE_OFFHOOK:
Nathan Harold123c9da2015-12-30 16:33:25 -0800739 event_offhook = ad_caller.ed.wait_for_event(
740 EventCallStateChanged,
Yang Liu8e6adff2016-02-05 10:24:04 -0800741 is_event_match,
Nathan Harold123c9da2015-12-30 16:33:25 -0800742 timeout=wait_time_for_incall_state,
Yang Liu8e6adff2016-02-05 10:24:04 -0800743 field=CallStateContainer.CALL_STATE,
744 value=TELEPHONY_STATE_OFFHOOK)
Ang Li73697b32015-12-03 00:41:53 +0000745 except Empty:
746 log.error("initiate_call did not receive Telephony OFFHOOK event.")
747 return False
748 finally:
Nathan Harold123c9da2015-12-30 16:33:25 -0800749 ad_caller.droid.telephonyStopTrackingCallStateChangeForSubscription(
750 sub_id)
Ang Li73697b32015-12-03 00:41:53 +0000751
752 # Verify call state
753 while wait_time_for_incall_state > 0:
754 wait_time_for_incall_state -= 1
755 if (ad_caller.droid.telecomIsInCall() and
Nathan Harold123c9da2015-12-30 16:33:25 -0800756 (ad_caller.droid.telephonyGetCallState() ==
757 TELEPHONY_STATE_OFFHOOK) and
758 (ad_caller.droid.telecomGetCallState() ==
759 TELEPHONY_STATE_OFFHOOK)):
Ang Li73697b32015-12-03 00:41:53 +0000760 return True
761 time.sleep(1)
762 log.error("Make call fail. telecomIsInCall:{}, Telecom State:{},"
Nathan Harold123c9da2015-12-30 16:33:25 -0800763 " Telephony State:{}".format(ad_caller.droid.telecomIsInCall(
764 ), ad_caller.droid.telephonyGetCallState(
765 ), ad_caller.droid.telecomGetCallState()))
Ang Li73697b32015-12-03 00:41:53 +0000766 return False
767
Nathan Harold123c9da2015-12-30 16:33:25 -0800768
769def call_reject_leave_message(log,
770 ad_caller,
771 ad_callee,
772 verify_caller_func=None,
Yang Liudf164e32016-01-07 16:49:32 -0800773 wait_time_in_call=WAIT_TIME_LEAVE_VOICE_MAIL):
Ang Li73697b32015-12-03 00:41:53 +0000774 """On default voice subscription, Call from caller to callee,
775 reject on callee, caller leave a voice mail.
776
777 1. Caller call Callee.
778 2. Callee reject incoming call.
779 3. Caller leave a voice mail.
780 4. Verify callee received the voice mail notification.
781
782 Args:
783 ad_caller: caller android device object.
784 ad_callee: callee android device object.
785 verify_caller_func: function to verify caller is in correct state while in-call.
786 This is optional, default is None.
787 wait_time_in_call: time to wait when leaving a voice mail.
Yang Liudf164e32016-01-07 16:49:32 -0800788 This is optional, default is WAIT_TIME_LEAVE_VOICE_MAIL
Ang Li73697b32015-12-03 00:41:53 +0000789
790 Returns:
791 True: if voice message is received on callee successfully.
792 False: for errors
793 """
794 subid_caller = ad_caller.droid.subscriptionGetDefaultVoiceSubId()
795 subid_callee = ad_callee.droid.subscriptionGetDefaultVoiceSubId()
Nathan Harold123c9da2015-12-30 16:33:25 -0800796 return call_reject_leave_message_for_subscription(
797 log, ad_caller, ad_callee, subid_caller, subid_callee,
798 verify_caller_func, wait_time_in_call)
Ang Li73697b32015-12-03 00:41:53 +0000799
Nathan Harold123c9da2015-12-30 16:33:25 -0800800
801def call_reject_leave_message_for_subscription(
802 log,
803 ad_caller,
804 ad_callee,
805 subid_caller,
806 subid_callee,
807 verify_caller_func=None,
Yang Liudf164e32016-01-07 16:49:32 -0800808 wait_time_in_call=WAIT_TIME_LEAVE_VOICE_MAIL):
Ang Li73697b32015-12-03 00:41:53 +0000809 """On specific voice subscription, Call from caller to callee,
810 reject on callee, caller leave a voice mail.
811
812 1. Caller call Callee.
813 2. Callee reject incoming call.
814 3. Caller leave a voice mail.
815 4. Verify callee received the voice mail notification.
816
817 Args:
818 ad_caller: caller android device object.
819 ad_callee: callee android device object.
820 subid_caller: caller's subscription id.
821 subid_callee: callee's subscription id.
822 verify_caller_func: function to verify caller is in correct state while in-call.
823 This is optional, default is None.
824 wait_time_in_call: time to wait when leaving a voice mail.
Yang Liudf164e32016-01-07 16:49:32 -0800825 This is optional, default is WAIT_TIME_LEAVE_VOICE_MAIL
Ang Li73697b32015-12-03 00:41:53 +0000826
827 Returns:
828 True: if voice message is received on callee successfully.
829 False: for errors
830 """
Nathan Harold123c9da2015-12-30 16:33:25 -0800831
Ang Li73697b32015-12-03 00:41:53 +0000832 class _CallSequenceException(Exception):
833 pass
834 # Currently this test utility only works for TMO and ATT and SPT.
835 # It does not work for VZW (see b/21559800)
836 # "with VVM TelephonyManager APIs won't work for vm"
837
838 caller_number = ad_caller.cfg['subscription'][subid_caller]['phone_num']
839 callee_number = ad_callee.cfg['subscription'][subid_callee]['phone_num']
840
841 log.info("Call from {} to {}".format(caller_number, callee_number))
842
843 try:
844
845 if not initiate_call(log, ad_caller, callee_number):
846 raise _CallSequenceException("Initiate call failed.")
847
848 if not wait_and_reject_call_for_subscription(
Nathan Harold123c9da2015-12-30 16:33:25 -0800849 log,
850 ad_callee,
851 subid_callee,
852 incoming_number=caller_number):
Ang Li73697b32015-12-03 00:41:53 +0000853 raise _CallSequenceException("Reject call fail.")
854
Yang Liuaed3eef2015-12-15 18:40:25 -0800855 ad_callee.droid.telephonyStartTrackingVoiceMailStateChangeForSubscription(
Ang Li73697b32015-12-03 00:41:53 +0000856 subid_callee)
Yang Liuaed3eef2015-12-15 18:40:25 -0800857 voice_mail_count_before = ad_callee.droid.telephonyGetVoiceMailCountForSubscription(
Ang Li73697b32015-12-03 00:41:53 +0000858 subid_callee)
859
860 # -1 means there are unread voice mail, but the count is unknown
861 # 0 means either this API not working (VZW) or no unread voice mail.
862 if voice_mail_count_before != 0:
863 log.warning("--Pending new Voice Mail, please clear on phone.--")
864
865 # ensure that all internal states are updated in telecom
866 time.sleep(WAIT_TIME_ANDROID_STATE_SETTLING)
867 ad_callee.ed.clear_all_events()
868
869 if verify_caller_func and not verify_caller_func(log, ad_caller):
Nathan Harold123c9da2015-12-30 16:33:25 -0800870 raise _CallSequenceException("Caller not in correct state!")
Ang Li73697b32015-12-03 00:41:53 +0000871
Yang Liu7a2e7ee2015-12-28 15:32:44 -0800872 # TODO: b/26293512 Need to play some sound to leave message.
873 # Otherwise carrier voice mail server may drop this voice mail.
Ang Li73697b32015-12-03 00:41:53 +0000874
875 time.sleep(wait_time_in_call)
876
877 if not verify_caller_func:
878 caller_state_result = ad_caller.droid.telecomIsInCall()
879 else:
880 caller_state_result = verify_caller_func(log, ad_caller)
881 if not caller_state_result:
882 raise _CallSequenceException(
Nathan Harold123c9da2015-12-30 16:33:25 -0800883 "Caller not in correct state after {} seconds".format(
884 wait_time_in_call))
Ang Li73697b32015-12-03 00:41:53 +0000885
886 if not hangup_call(log, ad_caller):
Nathan Harold123c9da2015-12-30 16:33:25 -0800887 raise _CallSequenceException("Error in Hanging-Up Call")
Ang Li73697b32015-12-03 00:41:53 +0000888
889 log.info("Wait for voice mail indicator on callee.")
890 try:
Nathan Harold123c9da2015-12-30 16:33:25 -0800891 event = ad_callee.ed.wait_for_event(
892 EventMessageWaitingIndicatorChanged,
893 _is_on_message_waiting_event_true)
Ang Li73697b32015-12-03 00:41:53 +0000894 log.info(event)
895 except Empty:
Nathan Harold123c9da2015-12-30 16:33:25 -0800896 raise _CallSequenceException("No expected event {}.".format(
897 EventMessageWaitingIndicatorChanged))
Yang Liuaed3eef2015-12-15 18:40:25 -0800898 voice_mail_count_after = ad_callee.droid.telephonyGetVoiceMailCountForSubscription(
Ang Li73697b32015-12-03 00:41:53 +0000899 subid_callee)
Nathan Harold123c9da2015-12-30 16:33:25 -0800900 log.info(
901 "telephonyGetVoiceMailCount output - before: {}, after: {}".format(
902 voice_mail_count_before, voice_mail_count_after))
Ang Li73697b32015-12-03 00:41:53 +0000903
904 # voice_mail_count_after should:
905 # either equals to (voice_mail_count_before + 1) [For ATT and SPT]
906 # or equals to -1 [For TMO]
907 # -1 means there are unread voice mail, but the count is unknown
Nathan Harold123c9da2015-12-30 16:33:25 -0800908 if not check_voice_mail_count(log, ad_callee, voice_mail_count_before,
Ang Li73697b32015-12-03 00:41:53 +0000909 voice_mail_count_after):
Yang Liuaed3eef2015-12-15 18:40:25 -0800910 log.error("telephonyGetVoiceMailCount output is incorrect.")
Ang Li73697b32015-12-03 00:41:53 +0000911 return False
912
913 except _CallSequenceException as e:
914 log.error(e)
915 return False
916 finally:
Yang Liuaed3eef2015-12-15 18:40:25 -0800917 ad_callee.droid.telephonyStopTrackingVoiceMailStateChangeForSubscription(
Ang Li73697b32015-12-03 00:41:53 +0000918 subid_callee)
919 return True
920
Nathan Harold123c9da2015-12-30 16:33:25 -0800921
Ang Li73697b32015-12-03 00:41:53 +0000922def call_voicemail_erase_all_pending_voicemail(log, ad):
923 """Script for phone to erase all pending voice mail.
924 This script only works for TMO and ATT and SPT currently.
925 This script only works if phone have already set up voice mail options,
926 and phone should disable password protection for voice mail.
927
928 1. If phone don't have pending voice message, return True.
929 2. Dial voice mail number.
930 For TMO, the number is '123'
931 For ATT, the number is phone's number
932 For SPT, the number is phone's number
933 3. Wait for voice mail connection setup.
934 4. Wait for voice mail play pending voice message.
935 5. Send DTMF to delete one message.
936 The digit is '7'.
937 6. Repeat steps 4 and 5 until voice mail server drop this call.
938 (No pending message)
Yang Liuaed3eef2015-12-15 18:40:25 -0800939 6. Check telephonyGetVoiceMailCount result. it should be 0.
Ang Li73697b32015-12-03 00:41:53 +0000940
941 Args:
942 log: log object
943 ad: android device object
944 Returns:
945 False if error happens. True is succeed.
946 """
947 log.info("Erase all pending voice mail.")
Yang Liuaed3eef2015-12-15 18:40:25 -0800948 if ad.droid.telephonyGetVoiceMailCount() == 0:
Ang Li73697b32015-12-03 00:41:53 +0000949 log.info("No Pending voice mail.")
950 return True
951
952 voice_mail_number = get_voice_mail_number(log, ad)
953
954 if not initiate_call(log, ad, voice_mail_number):
955 log.error("Initiate call failed.")
956 return False
Yang Liudf164e32016-01-07 16:49:32 -0800957 time.sleep(WAIT_TIME_VOICE_MAIL_SERVER_RESPONSE)
Ang Li73697b32015-12-03 00:41:53 +0000958 callId = ad.droid.telecomCallGetCallIds()[0]
Yang Liudf164e32016-01-07 16:49:32 -0800959 time.sleep(WAIT_TIME_VOICE_MAIL_SERVER_RESPONSE)
Ang Li73697b32015-12-03 00:41:53 +0000960 count = MAX_SAVED_VOICE_MAIL
Nathan Harold123c9da2015-12-30 16:33:25 -0800961 while (is_phone_in_call(log, ad) and (count > 0)):
Ang Li73697b32015-12-03 00:41:53 +0000962 log.info("Press 7 to delete voice mail.")
963 ad.droid.telecomCallPlayDtmfTone(callId, VOICEMAIL_DELETE_DIGIT)
964 ad.droid.telecomCallStopDtmfTone(callId)
Yang Liudf164e32016-01-07 16:49:32 -0800965 time.sleep(WAIT_TIME_VOICE_MAIL_SERVER_RESPONSE)
Ang Li73697b32015-12-03 00:41:53 +0000966 count -= 1
967 log.info("Voice mail server dropped this call.")
Yang Liuaed3eef2015-12-15 18:40:25 -0800968 # wait for telephonyGetVoiceMailCount to update correct result
Yang Liudf164e32016-01-07 16:49:32 -0800969 remaining_time = MAX_WAIT_TIME_VOICE_MAIL_COUNT
Nathan Harold123c9da2015-12-30 16:33:25 -0800970 while ((remaining_time > 0) and
971 (ad.droid.telephonyGetVoiceMailCount() != 0)):
Ang Li73697b32015-12-03 00:41:53 +0000972 time.sleep(1)
973 remaining_time -= 1
Yang Liuaed3eef2015-12-15 18:40:25 -0800974 current_voice_mail_count = ad.droid.telephonyGetVoiceMailCount()
975 log.info("telephonyGetVoiceMailCount: {}".format(current_voice_mail_count))
Ang Li73697b32015-12-03 00:41:53 +0000976 return (current_voice_mail_count == 0)
977
Nathan Harold123c9da2015-12-30 16:33:25 -0800978
Ang Li73697b32015-12-03 00:41:53 +0000979def _is_on_message_waiting_event_true(event):
980 """Private function to return if the received EventMessageWaitingIndicatorChanged
Yang Liudc8564b2016-01-27 14:15:37 -0800981 event MessageWaitingIndicatorContainer.IS_MESSAGE_WAITING field is True.
Ang Li73697b32015-12-03 00:41:53 +0000982 """
Yang Liudc8564b2016-01-27 14:15:37 -0800983 return event['data'][MessageWaitingIndicatorContainer.IS_MESSAGE_WAITING]
Ang Li73697b32015-12-03 00:41:53 +0000984
Nathan Harold123c9da2015-12-30 16:33:25 -0800985
986def call_setup_teardown(log,
987 ad_caller,
988 ad_callee,
989 ad_hangup=None,
990 verify_caller_func=None,
991 verify_callee_func=None,
992 wait_time_in_call=WAIT_TIME_IN_CALL,
993 incall_ui_display=INCALL_UI_DISPLAY_FOREGROUND):
Ang Li73697b32015-12-03 00:41:53 +0000994 """ Call process, including make a phone call from caller,
995 accept from callee, and hang up. The call is on default voice subscription
996
997 In call process, call from <droid_caller> to <droid_callee>,
Yang Liu855d5f82016-01-27 15:35:48 -0800998 accept the call, (optional)then hang up from <droid_hangup>.
Ang Li73697b32015-12-03 00:41:53 +0000999
1000 Args:
1001 ad_caller: Caller Android Device Object.
1002 ad_callee: Callee Android Device Object.
1003 ad_hangup: Android Device Object end the phone call.
1004 Optional. Default value is None, and phone call will continue.
1005 verify_call_mode_caller: func_ptr to verify caller in correct mode
1006 Optional. Default is None
1007 verify_call_mode_caller: func_ptr to verify caller in correct mode
1008 Optional. Default is None
1009 incall_ui_display: after answer the call, bring in-call UI to foreground or
1010 background. Optional, default value is INCALL_UI_DISPLAY_FOREGROUND.
1011 if = INCALL_UI_DISPLAY_FOREGROUND, bring in-call UI to foreground.
1012 if = INCALL_UI_DISPLAY_BACKGROUND, bring in-call UI to background.
1013 else, do nothing.
1014
1015 Returns:
1016 True if call process without any error.
1017 False if error happened.
1018
1019 """
1020 subid_caller = ad_caller.droid.subscriptionGetDefaultVoiceSubId()
1021 subid_callee = ad_callee.droid.subscriptionGetDefaultVoiceSubId()
Nathan Haroldae6a0da2016-03-16 20:56:47 +00001022 log.info("Sub-ID Caller {}, Sub-ID Callee {}".format(subid_caller, subid_callee))
Nathan Harold123c9da2015-12-30 16:33:25 -08001023 return call_setup_teardown_for_subscription(
1024 log, ad_caller, ad_callee, subid_caller, subid_callee, ad_hangup,
1025 verify_caller_func, verify_callee_func, wait_time_in_call,
1026 incall_ui_display)
Ang Li73697b32015-12-03 00:41:53 +00001027
1028
Nathan Harold123c9da2015-12-30 16:33:25 -08001029def call_setup_teardown_for_subscription(
1030 log,
1031 ad_caller,
1032 ad_callee,
1033 subid_caller,
1034 subid_callee,
1035 ad_hangup=None,
1036 verify_caller_func=None,
1037 verify_callee_func=None,
1038 wait_time_in_call=WAIT_TIME_IN_CALL,
1039 incall_ui_display=INCALL_UI_DISPLAY_FOREGROUND):
Ang Li73697b32015-12-03 00:41:53 +00001040 """ Call process, including make a phone call from caller,
1041 accept from callee, and hang up. The call is on specified subscription
1042
1043 In call process, call from <droid_caller> to <droid_callee>,
Yang Liu855d5f82016-01-27 15:35:48 -08001044 accept the call, (optional)then hang up from <droid_hangup>.
Ang Li73697b32015-12-03 00:41:53 +00001045
1046 Args:
1047 ad_caller: Caller Android Device Object.
1048 ad_callee: Callee Android Device Object.
1049 subid_caller: Caller subscription ID
1050 subid_callee: Callee subscription ID
1051 ad_hangup: Android Device Object end the phone call.
1052 Optional. Default value is None, and phone call will continue.
1053 verify_call_mode_caller: func_ptr to verify caller in correct mode
1054 Optional. Default is None
1055 verify_call_mode_caller: func_ptr to verify caller in correct mode
1056 Optional. Default is None
1057 incall_ui_display: after answer the call, bring in-call UI to foreground or
1058 background. Optional, default value is INCALL_UI_DISPLAY_FOREGROUND.
1059 if = INCALL_UI_DISPLAY_FOREGROUND, bring in-call UI to foreground.
1060 if = INCALL_UI_DISPLAY_BACKGROUND, bring in-call UI to background.
1061 else, do nothing.
1062
1063 Returns:
1064 True if call process without any error.
1065 False if error happened.
1066
1067 """
Yang Liu13406292016-02-23 14:58:25 -08001068 CHECK_INTERVAL = 3
Nathan Harold123c9da2015-12-30 16:33:25 -08001069
Ang Li73697b32015-12-03 00:41:53 +00001070 class _CallSequenceException(Exception):
1071 pass
1072
1073 caller_number = ad_caller.cfg['subscription'][subid_caller]['phone_num']
1074 callee_number = ad_callee.cfg['subscription'][subid_callee]['phone_num']
1075
1076 log.info("Call from {} to {}".format(caller_number, callee_number))
1077
1078 try:
1079 if not initiate_call(log, ad_caller, callee_number):
1080 raise _CallSequenceException("Initiate call failed.")
1081
1082 if not wait_and_answer_call_for_subscription(
Nathan Harold123c9da2015-12-30 16:33:25 -08001083 log,
1084 ad_callee,
1085 subid_callee,
1086 incoming_number=caller_number,
Ang Li73697b32015-12-03 00:41:53 +00001087 incall_ui_display=incall_ui_display):
1088 raise _CallSequenceException("Answer call fail.")
1089
1090 # ensure that all internal states are updated in telecom
1091 time.sleep(WAIT_TIME_ANDROID_STATE_SETTLING)
1092
1093 if verify_caller_func and not verify_caller_func(log, ad_caller):
Nathan Harold123c9da2015-12-30 16:33:25 -08001094 raise _CallSequenceException("Caller not in correct state!")
Ang Li73697b32015-12-03 00:41:53 +00001095 if verify_callee_func and not verify_callee_func(log, ad_callee):
Nathan Harold123c9da2015-12-30 16:33:25 -08001096 raise _CallSequenceException("Callee not in correct state!")
Ang Li73697b32015-12-03 00:41:53 +00001097
Yang Liu13406292016-02-23 14:58:25 -08001098 elapsed_time = 0
1099 while(elapsed_time < wait_time_in_call):
1100 CHECK_INTERVAL = min(CHECK_INTERVAL, wait_time_in_call - elapsed_time)
1101 time.sleep(CHECK_INTERVAL)
1102 elapsed_time += CHECK_INTERVAL
1103 if not verify_caller_func:
1104 caller_state_result = ad_caller.droid.telecomIsInCall()
1105 else:
1106 caller_state_result = verify_caller_func(log, ad_caller)
1107 if not caller_state_result:
1108 raise _CallSequenceException(
1109 "Caller not in correct state at <{}>/<{}> second.".format(
1110 elapsed_time, wait_time_in_call))
1111 if not verify_callee_func:
1112 callee_state_result = ad_callee.droid.telecomIsInCall()
1113 else:
1114 callee_state_result = verify_callee_func(log, ad_callee)
1115 if not callee_state_result:
1116 raise _CallSequenceException(
1117 "Callee not in correct state at <{}>/<{}> second.".format(
1118 elapsed_time, wait_time_in_call))
Ang Li73697b32015-12-03 00:41:53 +00001119
1120 if not ad_hangup:
1121 return True
1122
1123 if not hangup_call(log, ad_hangup):
Nathan Harold123c9da2015-12-30 16:33:25 -08001124 raise _CallSequenceException("Error in Hanging-Up Call")
Ang Li73697b32015-12-03 00:41:53 +00001125
1126 return True
1127
1128 except _CallSequenceException as e:
1129 log.error(e)
1130 return False
1131 finally:
1132 if ad_hangup:
1133 for ad in [ad_caller, ad_callee]:
1134 try:
1135 if ad.droid.telecomIsInCall():
1136 ad.droid.telecomEndCall()
1137 except Exception as e:
1138 log.error(str(e))
1139
Nathan Harold123c9da2015-12-30 16:33:25 -08001140
Nathan Haroldae6a0da2016-03-16 20:56:47 +00001141def phone_number_formatter(input_string, format=None):
Ang Li73697b32015-12-03 00:41:53 +00001142 """Get expected format of input phone number string.
1143
1144 Args:
1145 input_string: (string) input phone number.
1146 The input could be 10/11/12 digital, with or without " "/"-"/"."
1147 format: (int) expected format, this could be 7/10/11/12
1148 if format is 7: output string would be 7 digital number.
1149 if format is 10: output string would be 10 digital (standard) number.
1150 if format is 11: output string would be "1" + 10 digital number.
1151 if format is 12: output string would be "+1" + 10 digital number.
1152
1153 Returns:
1154 If no error happen, return phone number in expected format.
1155 Else, return None.
1156 """
1157 # make sure input_string is 10 digital
1158 # Remove white spaces, dashes, dots
Nathan Harold123c9da2015-12-30 16:33:25 -08001159 input_string = input_string.replace(" ", "").replace("-", "").replace(".",
1160 "")
Nathan Haroldae6a0da2016-03-16 20:56:47 +00001161 if not format:
1162 return input_string
Ang Li73697b32015-12-03 00:41:53 +00001163 # Remove "1" or "+1"from front
1164 if (len(input_string) == PHONE_NUMBER_STRING_FORMAT_11_DIGIT and
Nathan Harold123c9da2015-12-30 16:33:25 -08001165 input_string[0] == "1"):
Ang Li73697b32015-12-03 00:41:53 +00001166 input_string = input_string[1:]
1167 elif (len(input_string) == PHONE_NUMBER_STRING_FORMAT_12_DIGIT and
1168 input_string[0:2] == "+1"):
1169 input_string = input_string[2:]
1170 elif (len(input_string) == PHONE_NUMBER_STRING_FORMAT_7_DIGIT and
1171 format == PHONE_NUMBER_STRING_FORMAT_7_DIGIT):
1172 return input_string
1173 elif len(input_string) != PHONE_NUMBER_STRING_FORMAT_10_DIGIT:
1174 return None
1175 # change input_string according to format
1176 if format == PHONE_NUMBER_STRING_FORMAT_12_DIGIT:
Nathan Harold123c9da2015-12-30 16:33:25 -08001177 input_string = "+1" + input_string
Ang Li73697b32015-12-03 00:41:53 +00001178 elif format == PHONE_NUMBER_STRING_FORMAT_11_DIGIT:
Nathan Harold123c9da2015-12-30 16:33:25 -08001179 input_string = "1" + input_string
Ang Li73697b32015-12-03 00:41:53 +00001180 elif format == PHONE_NUMBER_STRING_FORMAT_10_DIGIT:
1181 input_string = input_string
1182 elif format == PHONE_NUMBER_STRING_FORMAT_7_DIGIT:
1183 input_string = input_string[3:]
1184 else:
1185 return None
1186 return input_string
1187
Nathan Harold123c9da2015-12-30 16:33:25 -08001188
Ang Li73697b32015-12-03 00:41:53 +00001189def get_internet_connection_type(log, ad):
1190 """Get current active connection type name.
1191
1192 Args:
1193 log: Log object.
1194 ad: Android Device Object.
1195 Returns:
1196 current active connection type name.
1197 """
1198 if not ad.droid.connectivityNetworkIsConnected():
1199 return 'none'
1200 return connection_type_from_type_string(
1201 ad.droid.connectivityNetworkGetActiveConnectionTypeName())
1202
Nathan Harold123c9da2015-12-30 16:33:25 -08001203
1204def verify_http_connection(log,
1205 ad,
1206 url="http://www.google.com/",
1207 retry=3,
1208 retry_interval=5):
Ang Li73697b32015-12-03 00:41:53 +00001209 """Make ping request and return status.
1210
1211 Args:
1212 ad: Android Device Object.
1213 url: Optional. The ping request will be made to this URL.
1214 Default Value is "http://www.google.com/".
1215
1216 """
Nathan Harold123c9da2015-12-30 16:33:25 -08001217 for i in range(0, retry + 1):
Ang Li73697b32015-12-03 00:41:53 +00001218
1219 try:
1220 http_response = ad.droid.httpPing(url)
1221 except:
1222 http_response = None
1223
1224 # If httpPing failed, it may return {} (if phone just turn off APM) or
1225 # None (regular fail)
1226 # So here use "if http_response" to see if it pass or fail
Nathan Harold123c9da2015-12-30 16:33:25 -08001227 if http_response:
1228 log.info("Verify Internet succeeded after {}s.".format(
1229 i * retry_interval) if i > 0 else "Verify Internet succeeded.")
Ang Li73697b32015-12-03 00:41:53 +00001230 return True
1231 else:
1232 if i < retry:
1233 time.sleep(retry_interval)
1234 log.info("Verify Internet retry failed after {}s"
Nathan Harold123c9da2015-12-30 16:33:25 -08001235 .format(i * retry_interval))
Ang Li73697b32015-12-03 00:41:53 +00001236 return False
1237
1238
1239def _connection_state_change(_event, target_state, connection_type):
1240 if connection_type:
1241 if 'TypeName' not in _event['data']:
1242 return False
1243 connection_type_string_in_event = _event['data']['TypeName']
Nathan Harold123c9da2015-12-30 16:33:25 -08001244 cur_type = connection_type_from_type_string(
1245 connection_type_string_in_event)
Ang Li73697b32015-12-03 00:41:53 +00001246 if cur_type != connection_type:
1247 log.info(
Nathan Harold123c9da2015-12-30 16:33:25 -08001248 "_connection_state_change expect: {}, received: {} <type {}>".format(
1249 connection_type, connection_type_string_in_event,
1250 cur_type))
Ang Li73697b32015-12-03 00:41:53 +00001251 return False
1252
Nathan Harold123c9da2015-12-30 16:33:25 -08001253 if 'isConnected' in _event['data'] and _event['data'][
1254 'isConnected'] == target_state:
Ang Li73697b32015-12-03 00:41:53 +00001255 return True
1256 return False
1257
1258
Nathan Harold123c9da2015-12-30 16:33:25 -08001259def wait_for_cell_data_connection(
1260 log,
1261 ad,
1262 state,
1263 timeout_value=EventDispatcher.DEFAULT_TIMEOUT):
Ang Li73697b32015-12-03 00:41:53 +00001264 """Wait for data connection status to be expected value for default
1265 data subscription.
1266
1267 Wait for the data connection status to be DATA_STATE_CONNECTED
1268 or DATA_STATE_DISCONNECTED.
1269
1270 Args:
1271 log: Log object.
1272 ad: Android Device Object.
1273 state: Expected status: True or False.
1274 If True, it will wait for status to be DATA_STATE_CONNECTED.
1275 If False, it will wait for status ti be DATA_STATE_DISCONNECTED.
1276 timeout_value: wait for cell data timeout value.
1277 This is optional, default value is EventDispatcher.DEFAULT_TIMEOUT
1278
1279 Returns:
1280 True if success.
1281 False if failed.
1282 """
1283 sub_id = ad.droid.subscriptionGetDefaultDataSubId()
Nathan Harold123c9da2015-12-30 16:33:25 -08001284 return wait_for_cell_data_connection_for_subscription(log, ad, sub_id,
1285 state, timeout_value)
1286
Ang Li73697b32015-12-03 00:41:53 +00001287
1288def _is_data_connection_state_match(log, ad, expected_data_connection_state):
Nathan Harold123c9da2015-12-30 16:33:25 -08001289 return (expected_data_connection_state ==
1290 ad.droid.telephonyGetDataConnectionState())
Ang Li73697b32015-12-03 00:41:53 +00001291
Ang Li73697b32015-12-03 00:41:53 +00001292
Nathan Harold123c9da2015-12-30 16:33:25 -08001293def _is_network_connected_state_match(log, ad,
1294 expected_network_connected_state):
1295 return (expected_network_connected_state ==
1296 ad.droid.connectivityNetworkIsConnected())
1297
1298
1299def wait_for_cell_data_connection_for_subscription(
1300 log,
1301 ad,
1302 sub_id,
1303 state,
1304 timeout_value=EventDispatcher.DEFAULT_TIMEOUT):
Ang Li73697b32015-12-03 00:41:53 +00001305 """Wait for data connection status to be expected value for specified
1306 subscrption id.
1307
1308 Wait for the data connection status to be DATA_STATE_CONNECTED
1309 or DATA_STATE_DISCONNECTED.
1310
1311 Args:
1312 log: Log object.
1313 ad: Android Device Object.
1314 sub_id: subscription Id
1315 state: Expected status: True or False.
1316 If True, it will wait for status to be DATA_STATE_CONNECTED.
1317 If False, it will wait for status ti be DATA_STATE_DISCONNECTED.
1318 timeout_value: wait for cell data timeout value.
1319 This is optional, default value is EventDispatcher.DEFAULT_TIMEOUT
1320
1321 Returns:
1322 True if success.
1323 False if failed.
1324 """
Yang Liu8e6adff2016-02-05 10:24:04 -08001325 state_str = {
1326 True: DATA_STATE_CONNECTED,
1327 False: DATA_STATE_DISCONNECTED
Ang Li73697b32015-12-03 00:41:53 +00001328 }[state]
1329
1330 ad.ed.clear_all_events()
Nathan Harold123c9da2015-12-30 16:33:25 -08001331 ad.droid.telephonyStartTrackingDataConnectionStateChangeForSubscription(
1332 sub_id)
Ang Li73697b32015-12-03 00:41:53 +00001333 ad.droid.connectivityStartTrackingConnectivityStateChange()
1334 try:
Yang Liu7a2e7ee2015-12-28 15:32:44 -08001335 # TODO: b/26293147 There is no framework API to get data connection
1336 # state by sub id
Yang Liuaed3eef2015-12-15 18:40:25 -08001337 data_state = ad.droid.telephonyGetDataConnectionState()
Ang Li73697b32015-12-03 00:41:53 +00001338 if data_state == state_str:
Nathan Harold123c9da2015-12-30 16:33:25 -08001339 return _wait_for_nw_data_connection(
1340 log, ad, state, NETWORK_CONNECTION_TYPE_CELL, timeout_value)
Ang Li73697b32015-12-03 00:41:53 +00001341
1342 try:
1343 event = ad.ed.wait_for_event(EventDataConnectionStateChanged,
Yang Liu8e6adff2016-02-05 10:24:04 -08001344 is_event_match, timeout=timeout_value,
1345 field=DataConnectionStateContainer.DATA_CONNECTION_STATE,
1346 value=state_str)
Ang Li73697b32015-12-03 00:41:53 +00001347 except Empty:
Nathan Harold123c9da2015-12-30 16:33:25 -08001348 log.debug(
1349 "No expected event EventDataConnectionStateChanged {}.".format(
Yang Liu8e6adff2016-02-05 10:24:04 -08001350 state_str))
Ang Li73697b32015-12-03 00:41:53 +00001351
Yang Liudf164e32016-01-07 16:49:32 -08001352 # TODO: Wait for <MAX_WAIT_TIME_CONNECTION_STATE_UPDATE> seconds for
Ang Li73697b32015-12-03 00:41:53 +00001353 # data connection state.
1354 # Otherwise, the network state will not be correct.
1355 # The bug is tracked here: b/20921915
Ang Li73697b32015-12-03 00:41:53 +00001356
Yang Liu7a2e7ee2015-12-28 15:32:44 -08001357 # Previously we use _is_data_connection_state_match,
1358 # but telephonyGetDataConnectionState sometimes return wrong value.
1359 # The bug is tracked here: b/22612607
1360 # So we use _is_network_connected_state_match.
Ang Li73697b32015-12-03 00:41:53 +00001361
Yang Liudf164e32016-01-07 16:49:32 -08001362 if _wait_for_droid_in_state(log, ad, MAX_WAIT_TIME_CONNECTION_STATE_UPDATE,
Nathan Harold123c9da2015-12-30 16:33:25 -08001363 _is_network_connected_state_match, state):
1364 return _wait_for_nw_data_connection(
1365 log, ad, state, NETWORK_CONNECTION_TYPE_CELL, timeout_value)
Ang Li73697b32015-12-03 00:41:53 +00001366 else:
1367 return False
1368
1369 finally:
Nathan Harold123c9da2015-12-30 16:33:25 -08001370 ad.droid.telephonyStopTrackingDataConnectionStateChangeForSubscription(
1371 sub_id)
Ang Li73697b32015-12-03 00:41:53 +00001372
Nathan Harold123c9da2015-12-30 16:33:25 -08001373
1374def wait_for_wifi_data_connection(
1375 log,
1376 ad,
1377 state,
1378 timeout_value=EventDispatcher.DEFAULT_TIMEOUT):
Ang Li73697b32015-12-03 00:41:53 +00001379 """Wait for data connection status to be expected value and connection is by WiFi.
1380
1381 Args:
1382 log: Log object.
1383 ad: Android Device Object.
1384 state: Expected status: True or False.
1385 If True, it will wait for status to be DATA_STATE_CONNECTED.
1386 If False, it will wait for status ti be DATA_STATE_DISCONNECTED.
1387 timeout_value: wait for network data timeout value.
1388 This is optional, default value is EventDispatcher.DEFAULT_TIMEOUT
1389
1390 Returns:
1391 True if success.
1392 False if failed.
1393 """
1394 log.info("{} wait_for_wifi_data_connection".format(ad.serial))
Nathan Harold123c9da2015-12-30 16:33:25 -08001395 return _wait_for_nw_data_connection(
1396 log, ad, state, NETWORK_CONNECTION_TYPE_WIFI, timeout_value)
Ang Li73697b32015-12-03 00:41:53 +00001397
1398
Nathan Harold123c9da2015-12-30 16:33:25 -08001399def wait_for_data_connection(log,
1400 ad,
1401 state,
Ang Li73697b32015-12-03 00:41:53 +00001402 timeout_value=EventDispatcher.DEFAULT_TIMEOUT):
1403 """Wait for data connection status to be expected value.
1404
1405 Wait for the data connection status to be DATA_STATE_CONNECTED
1406 or DATA_STATE_DISCONNECTED.
1407
1408 Args:
1409 log: Log object.
1410 ad: Android Device Object.
1411 state: Expected status: True or False.
1412 If True, it will wait for status to be DATA_STATE_CONNECTED.
1413 If False, it will wait for status ti be DATA_STATE_DISCONNECTED.
1414 timeout_value: wait for network data timeout value.
1415 This is optional, default value is EventDispatcher.DEFAULT_TIMEOUT
1416
1417 Returns:
1418 True if success.
1419 False if failed.
1420 """
1421 return _wait_for_nw_data_connection(log, ad, state, None, timeout_value)
1422
1423
Nathan Harold123c9da2015-12-30 16:33:25 -08001424def _wait_for_nw_data_connection(
1425 log,
1426 ad,
1427 is_connected,
1428 connection_type=None,
1429 timeout_value=EventDispatcher.DEFAULT_TIMEOUT):
Ang Li73697b32015-12-03 00:41:53 +00001430 """Wait for data connection status to be expected value.
1431
1432 Wait for the data connection status to be DATA_STATE_CONNECTED
1433 or DATA_STATE_DISCONNECTED.
1434
1435 Args:
1436 log: Log object.
1437 ad: Android Device Object.
1438 is_connected: Expected connection status: True or False.
1439 If True, it will wait for status to be DATA_STATE_CONNECTED.
1440 If False, it will wait for status ti be DATA_STATE_DISCONNECTED.
1441 connection_type: expected connection type.
1442 This is optional, if it is None, then any connection type will return True.
1443 timeout_value: wait for network data timeout value.
1444 This is optional, default value is EventDispatcher.DEFAULT_TIMEOUT
1445
1446 Returns:
1447 True if success.
1448 False if failed.
1449 """
1450 ad.ed.clear_all_events()
1451 ad.droid.connectivityStartTrackingConnectivityStateChange()
1452 try:
1453 cur_data_connection_state = ad.droid.connectivityNetworkIsConnected()
1454 if is_connected == cur_data_connection_state:
1455 current_type = get_internet_connection_type(log, ad)
Nathan Harold123c9da2015-12-30 16:33:25 -08001456 log.info(
1457 "_wait_for_nw_data_connection: current connection type: {}".format(
1458 current_type))
Ang Li73697b32015-12-03 00:41:53 +00001459 if not connection_type:
1460 return True
1461 else:
1462 if not is_connected and current_type != connection_type:
Nathan Harold123c9da2015-12-30 16:33:25 -08001463 log.info(
1464 "wait_for_nw_data_connection success: {} data not on {}!".format(
1465 ad.serial, connection_type))
Ang Li73697b32015-12-03 00:41:53 +00001466 return True
1467 elif is_connected and current_type == connection_type:
Nathan Harold123c9da2015-12-30 16:33:25 -08001468 log.info(
1469 "wait_for_nw_data_connection success: {} data on {}!".format(
1470 ad.serial, connection_type))
Ang Li73697b32015-12-03 00:41:53 +00001471 return True
1472 else:
Nathan Harold123c9da2015-12-30 16:33:25 -08001473 log.info("{} current state: {} target: {}".format(
1474 ad.serial, cur_data_connection_state, is_connected))
Ang Li73697b32015-12-03 00:41:53 +00001475
1476 try:
Nathan Harold123c9da2015-12-30 16:33:25 -08001477 event = ad.ed.wait_for_event(
1478 EventConnectivityChanged, _connection_state_change,
1479 timeout_value, is_connected, connection_type)
1480 log.info("_wait_for_nw_data_connection received event:{}".format(
1481 event))
Ang Li73697b32015-12-03 00:41:53 +00001482 except Empty:
1483 pass
1484
Nathan Harold123c9da2015-12-30 16:33:25 -08001485 log.info(
1486 "_wait_for_nw_data_connection: check connection after wait event.")
Yang Liudf164e32016-01-07 16:49:32 -08001487 # TODO: Wait for <MAX_WAIT_TIME_CONNECTION_STATE_UPDATE> seconds for
Ang Li73697b32015-12-03 00:41:53 +00001488 # data connection state.
1489 # Otherwise, the network state will not be correct.
1490 # The bug is tracked here: b/20921915
Yang Liudf164e32016-01-07 16:49:32 -08001491 if _wait_for_droid_in_state(log, ad, MAX_WAIT_TIME_CONNECTION_STATE_UPDATE,
Nathan Harold123c9da2015-12-30 16:33:25 -08001492 _is_network_connected_state_match,
1493 is_connected):
Ang Li73697b32015-12-03 00:41:53 +00001494 current_type = get_internet_connection_type(log, ad)
Nathan Harold123c9da2015-12-30 16:33:25 -08001495 log.info(
1496 "_wait_for_nw_data_connection: current connection type: {}".format(
1497 current_type))
Ang Li73697b32015-12-03 00:41:53 +00001498 if not connection_type:
1499 return True
1500 else:
1501 if not is_connected and current_type != connection_type:
Nathan Harold123c9da2015-12-30 16:33:25 -08001502 log.info(
1503 "wait_for_nw_data_connection after event wait, success: {} data not on {}!".format(
1504 ad.serial, connection_type))
Ang Li73697b32015-12-03 00:41:53 +00001505 return True
1506 elif is_connected and current_type == connection_type:
Nathan Harold123c9da2015-12-30 16:33:25 -08001507 log.info(
1508 "wait_for_nw_data_connection after event wait, success: {} data on {}!".format(
1509 ad.serial, connection_type))
Ang Li73697b32015-12-03 00:41:53 +00001510 return True
1511 else:
1512 return False
1513 else:
1514 return False
1515 except Exception as e:
Nathan Harold123c9da2015-12-30 16:33:25 -08001516 log.error(
1517 "tel_test_utils._wait_for_nw_data_connection threw Random exception {}".format(
1518 str(e)))
Ang Li73697b32015-12-03 00:41:53 +00001519 return False
1520 finally:
1521 ad.droid.connectivityStopTrackingConnectivityStateChange()
1522
Nathan Harold123c9da2015-12-30 16:33:25 -08001523
Ang Li73697b32015-12-03 00:41:53 +00001524def verify_incall_state(log, ads, expected_status):
1525 """Verify phones in incall state or not.
1526
1527 Verify if all phones in the array <ads> are in <expected_status>.
1528
1529 Args:
1530 log: Log object.
1531 ads: Array of Android Device Object. All droid in this array will be tested.
1532 expected_status: If True, verify all Phones in incall state.
1533 If False, verify all Phones not in incall state.
1534
1535 """
1536 result = True
1537 for ad in ads:
1538 if ad.droid.telecomIsInCall() is not expected_status:
Nathan Harold123c9da2015-12-30 16:33:25 -08001539 log.error("Verify_incall_state: {} status:{}, expected:{}".format(
1540 ad.serial, ad.droid.telecomIsInCall(), expected_status))
Ang Li73697b32015-12-03 00:41:53 +00001541 result = False
1542 return result
1543
Nathan Harold123c9da2015-12-30 16:33:25 -08001544
Ang Li73697b32015-12-03 00:41:53 +00001545def verify_active_call_number(log, ad, expected_number):
1546 """Verify the number of current active call.
1547
1548 Verify if the number of current active call in <ad> is
1549 equal to <expected_number>.
1550
1551 Args:
1552 ad: Android Device Object.
1553 expected_number: Expected active call number.
1554 """
1555 calls = ad.droid.telecomCallGetCallIds()
1556 if calls is None:
1557 actual_number = 0
1558 else:
1559 actual_number = len(calls)
1560 if actual_number != expected_number:
1561 log.error("Active Call number in {}".format(ad.serial))
Nathan Harold123c9da2015-12-30 16:33:25 -08001562 log.error("Expected:{}, Actual:{}".format(expected_number,
1563 actual_number))
Ang Li73697b32015-12-03 00:41:53 +00001564 return False
1565 return True
1566
Nathan Harold123c9da2015-12-30 16:33:25 -08001567
Ang Li73697b32015-12-03 00:41:53 +00001568def num_active_calls(log, ad):
1569 """Get the count of current active calls.
1570
1571 Args:
1572 log: Log object.
1573 ad: Android Device Object.
1574
1575 Returns:
1576 Count of current active calls.
1577 """
1578 calls = ad.droid.telecomCallGetCallIds()
1579 return len(calls) if calls else 0
1580
Nathan Harold123c9da2015-12-30 16:33:25 -08001581
Ang Li73697b32015-12-03 00:41:53 +00001582def toggle_volte(log, ad, new_state=None):
1583 """Toggle enable/disable VoLTE for default voice subscription.
1584
1585 Args:
1586 ad: Android device object.
1587 new_state: VoLTE mode state to set to.
1588 True for enable, False for disable.
1589 If None, opposite of the current state.
1590
1591 Raises:
1592 TelTestUtilsError if platform does not support VoLTE.
1593 """
1594 return toggle_volte_for_subscription(
1595 log, ad, ad.droid.subscriptionGetDefaultVoiceSubId(), new_state)
1596
1597
1598def toggle_volte_for_subscription(log, ad, sub_id, new_state=None):
1599 """Toggle enable/disable VoLTE for specified voice subscription.
1600
1601 Args:
1602 ad: Android device object.
1603 sub_id: subscription ID
1604 new_state: VoLTE mode state to set to.
1605 True for enable, False for disable.
1606 If None, opposite of the current state.
1607
1608 Raises:
1609 TelTestUtilsError if platform does not support VoLTE.
1610 """
Yang Liu7a2e7ee2015-12-28 15:32:44 -08001611 # TODO: b/26293960 No framework API available to set IMS by SubId.
Ang Li73697b32015-12-03 00:41:53 +00001612 if not ad.droid.imsIsEnhanced4gLteModeSettingEnabledByPlatform():
1613 raise TelTestUtilsError("VoLTE not supported by platform.")
1614 current_state = ad.droid.imsIsEnhanced4gLteModeSettingEnabledByUser()
1615 if new_state is None:
1616 new_state = not current_state
1617 if new_state != current_state:
1618 ad.droid.imsSetEnhanced4gMode(new_state)
1619 return True
1620
Nathan Harold123c9da2015-12-30 16:33:25 -08001621
Ang Li73697b32015-12-03 00:41:53 +00001622def set_wfc_mode(log, ad, wfc_mode):
1623 """Set WFC enable/disable and mode.
1624
1625 Args:
1626 log: Log object
1627 ad: Android device object.
1628 wfc_mode: WFC mode to set to.
1629 Valid mode includes: WFC_MODE_WIFI_ONLY, WFC_MODE_CELLULAR_PREFERRED,
1630 WFC_MODE_WIFI_PREFERRED, WFC_MODE_DISABLED.
1631
1632 Returns:
1633 True if success. False if ad does not support WFC or error happened.
1634 """
1635 try:
1636 log.info("{} set wfc mode to {}".format(ad.serial, wfc_mode))
1637 if not ad.droid.imsIsWfcEnabledByPlatform():
1638 if wfc_mode == WFC_MODE_DISABLED:
1639 return True
1640 else:
1641 log.error("WFC not supported by platform.")
1642 return False
1643
1644 ad.droid.imsSetWfcMode(wfc_mode)
1645
1646 except Exception as e:
1647 log.error(e)
1648 return False
1649
1650 return True
1651
Nathan Harold123c9da2015-12-30 16:33:25 -08001652
1653def _wait_for_droid_in_state(log, ad, max_time, state_check_func, *args,
1654 **kwargs):
Ang Li73697b32015-12-03 00:41:53 +00001655 while max_time > 0:
1656 if state_check_func(log, ad, *args, **kwargs):
1657 return True
1658
1659 time.sleep(1)
1660 max_time -= 1
1661
1662 return False
1663
1664
Nathan Harold123c9da2015-12-30 16:33:25 -08001665def _wait_for_droid_in_state_for_subscription(
1666 log, ad, sub_id, max_time, state_check_func, *args, **kwargs):
Ang Li73697b32015-12-03 00:41:53 +00001667 while max_time > 0:
1668 if state_check_func(log, ad, sub_id, *args, **kwargs):
1669 return True
1670
1671 time.sleep(1)
1672 max_time -= 1
1673
1674 return False
1675
1676
Nathan Harold123c9da2015-12-30 16:33:25 -08001677def _wait_for_droids_in_state(log, ads, max_time, state_check_func, *args,
1678 **kwargs):
Ang Li73697b32015-12-03 00:41:53 +00001679 while max_time > 0:
1680 success = True
1681 for ad in ads:
1682 if not state_check_func(log, ad, *args, **kwargs):
1683 success = False
1684 break
1685 if success:
1686 return True
1687
1688 time.sleep(1)
1689 max_time -= 1
1690
1691 return False
1692
Nathan Harold123c9da2015-12-30 16:33:25 -08001693
Ang Li73697b32015-12-03 00:41:53 +00001694def is_phone_in_call(log, ad):
1695 """Return True if phone in call.
1696
1697 Args:
1698 log: log object.
1699 ad: android device.
1700 """
1701 return ad.droid.telecomIsInCall()
1702
Nathan Harold123c9da2015-12-30 16:33:25 -08001703
Ang Li73697b32015-12-03 00:41:53 +00001704def is_phone_not_in_call(log, ad):
1705 """Return True if phone not in call.
1706
1707 Args:
1708 log: log object.
1709 ad: android device.
1710 """
1711 return not ad.droid.telecomIsInCall()
1712
Nathan Harold123c9da2015-12-30 16:33:25 -08001713
Ang Li73697b32015-12-03 00:41:53 +00001714def wait_for_droid_in_call(log, ad, max_time):
1715 """Wait for android to be in call state.
1716
1717 Args:
1718 log: log object.
1719 ad: android device.
1720 max_time: maximal wait time.
1721
1722 Returns:
1723 If phone become in call state within max_time, return True.
1724 Return False if timeout.
1725 """
1726 return _wait_for_droid_in_state(log, ad, max_time, is_phone_in_call)
1727
Yang Liu855d5f82016-01-27 15:35:48 -08001728def wait_for_telecom_ringing(log, ad, max_time=MAX_WAIT_TIME_TELECOM_RINGING):
1729 """Wait for android to be in telecom ringing state.
1730
1731 Args:
1732 log: log object.
1733 ad: android device.
1734 max_time: maximal wait time. This is optional.
1735 Default Value is MAX_WAIT_TIME_TELECOM_RINGING.
1736
1737 Returns:
1738 If phone become in telecom ringing state within max_time, return True.
1739 Return False if timeout.
1740 """
1741 return _wait_for_droid_in_state(log, ad, max_time,
1742 lambda log, ad: ad.droid.telecomIsRinging())
1743
Nathan Harold123c9da2015-12-30 16:33:25 -08001744
Ang Li73697b32015-12-03 00:41:53 +00001745def wait_for_droid_not_in_call(log, ad, max_time):
1746 """Wait for android to be not in call state.
1747
1748 Args:
1749 log: log object.
1750 ad: android device.
1751 max_time: maximal wait time.
1752
1753 Returns:
1754 If phone become not in call state within max_time, return True.
1755 Return False if timeout.
1756 """
1757 return _wait_for_droid_in_state(log, ad, max_time, is_phone_not_in_call)
1758
Nathan Harold123c9da2015-12-30 16:33:25 -08001759
Ang Li73697b32015-12-03 00:41:53 +00001760def _is_attached(log, ad, voice_or_data):
1761 return _is_attached_for_subscription(
1762 log, ad, ad.droid.subscriptionGetDefaultSubId(), voice_or_data)
1763
Nathan Harold123c9da2015-12-30 16:33:25 -08001764
Ang Li73697b32015-12-03 00:41:53 +00001765def _is_attached_for_subscription(log, ad, sub_id, voice_or_data):
1766 if get_network_rat_for_subscription(log, ad, sub_id,
Nathan Harold123c9da2015-12-30 16:33:25 -08001767 voice_or_data) != RAT_UNKNOWN:
Ang Li73697b32015-12-03 00:41:53 +00001768 return True
1769 else:
1770 return False
1771
Nathan Harold123c9da2015-12-30 16:33:25 -08001772
Ang Li73697b32015-12-03 00:41:53 +00001773def wait_for_voice_attach(log, ad, max_time):
1774 """Wait for android device to attach on voice.
1775
1776 Args:
1777 log: log object.
1778 ad: android device.
1779 max_time: maximal wait time.
1780
1781 Returns:
1782 Return True if device attach voice within max_time.
1783 Return False if timeout.
1784 """
1785 return _wait_for_droid_in_state(log, ad, max_time, _is_attached,
1786 NETWORK_SERVICE_VOICE)
1787
Nathan Harold123c9da2015-12-30 16:33:25 -08001788
Ang Li73697b32015-12-03 00:41:53 +00001789def wait_for_voice_attach_for_subscription(log, ad, sub_id, max_time):
1790 """Wait for android device to attach on voice in subscription id.
1791
1792 Args:
1793 log: log object.
1794 ad: android device.
1795 sub_id: subscription id.
1796 max_time: maximal wait time.
1797
1798 Returns:
1799 Return True if device attach voice within max_time.
1800 Return False if timeout.
1801 """
Yang Liu7a2e7ee2015-12-28 15:32:44 -08001802 if not _wait_for_droid_in_state_for_subscription(
Nathan Harold123c9da2015-12-30 16:33:25 -08001803 log, ad, sub_id, max_time, _is_attached_for_subscription,
1804 NETWORK_SERVICE_VOICE):
Yang Liu7a2e7ee2015-12-28 15:32:44 -08001805 return False
1806
1807 # TODO: b/26295983 if pone attach to 1xrtt from unknown, phone may not
1808 # receive incoming call immediately.
1809 if ad.droid.telephonyGetCurrentVoiceNetworkType() == RAT_1XRTT:
Yang Liudf164e32016-01-07 16:49:32 -08001810 time.sleep(WAIT_TIME_1XRTT_VOICE_ATTACH)
Yang Liu7a2e7ee2015-12-28 15:32:44 -08001811 return True
Ang Li73697b32015-12-03 00:41:53 +00001812
Nathan Harold123c9da2015-12-30 16:33:25 -08001813
Ang Li73697b32015-12-03 00:41:53 +00001814def wait_for_data_attach(log, ad, max_time):
1815 """Wait for android device to attach on data.
1816
1817 Args:
1818 log: log object.
1819 ad: android device.
1820 max_time: maximal wait time.
1821
1822 Returns:
1823 Return True if device attach data within max_time.
1824 Return False if timeout.
1825 """
1826 return _wait_for_droid_in_state(log, ad, max_time, _is_attached,
1827 NETWORK_SERVICE_DATA)
1828
Nathan Harold123c9da2015-12-30 16:33:25 -08001829
Ang Li73697b32015-12-03 00:41:53 +00001830def wait_for_data_attach_for_subscription(log, ad, sub_id, max_time):
1831 """Wait for android device to attach on data in subscription id.
1832
1833 Args:
1834 log: log object.
1835 ad: android device.
1836 sub_id: subscription id.
1837 max_time: maximal wait time.
1838
1839 Returns:
1840 Return True if device attach data within max_time.
1841 Return False if timeout.
1842 """
1843 return _wait_for_droid_in_state_for_subscription(
1844 log, ad, sub_id, max_time, _is_attached_for_subscription,
1845 NETWORK_SERVICE_DATA)
1846
Nathan Harold123c9da2015-12-30 16:33:25 -08001847
Yang Liu9b85ce52016-02-16 12:25:11 -08001848def is_ims_registered(log, ad):
1849 """Return True if IMS registered.
1850
1851 Args:
1852 log: log object.
1853 ad: android device.
1854
1855 Returns:
1856 Return True if IMS registered.
1857 Return False if IMS not registered.
1858 """
Yang Liuaed3eef2015-12-15 18:40:25 -08001859 return ad.droid.telephonyIsImsRegistered()
Ang Li73697b32015-12-03 00:41:53 +00001860
Nathan Harold123c9da2015-12-30 16:33:25 -08001861
Ang Li73697b32015-12-03 00:41:53 +00001862def wait_for_ims_registered(log, ad, max_time):
1863 """Wait for android device to register on ims.
1864
1865 Args:
1866 log: log object.
1867 ad: android device.
1868 max_time: maximal wait time.
1869
1870 Returns:
1871 Return True if device register ims successfully within max_time.
1872 Return False if timeout.
1873 """
Yang Liu9b85ce52016-02-16 12:25:11 -08001874 return _wait_for_droid_in_state(log, ad, max_time, is_ims_registered)
Ang Li73697b32015-12-03 00:41:53 +00001875
Nathan Harold123c9da2015-12-30 16:33:25 -08001876
Yang Liu9b85ce52016-02-16 12:25:11 -08001877def is_volte_enabled(log, ad):
1878 """Return True if VoLTE feature bit is True.
1879
1880 Args:
1881 log: log object.
1882 ad: android device.
1883
1884 Returns:
1885 Return True if VoLTE feature bit is True and IMS registered.
1886 Return False if VoLTE feature bit is False or IMS not registered.
1887 """
Yang Liub93d7292016-02-19 10:41:40 -08001888 volte_status = ad.droid.telephonyIsVolteAvailable()
1889 if volte_status is True and is_ims_registered(log, ad) is False:
Yang Liu9b85ce52016-02-16 12:25:11 -08001890 log.error("Error! VoLTE is Available, but IMS is not registered.")
1891 return False
Yang Liub93d7292016-02-19 10:41:40 -08001892 return volte_status
Ang Li73697b32015-12-03 00:41:53 +00001893
Nathan Harold123c9da2015-12-30 16:33:25 -08001894
Yang Liu9b85ce52016-02-16 12:25:11 -08001895def is_video_enabled(log, ad):
1896 """Return True if Video Calling feature bit is True.
1897
1898 Args:
1899 log: log object.
1900 ad: android device.
1901
1902 Returns:
1903 Return True if Video Calling feature bit is True and IMS registered.
1904 Return False if Video Calling feature bit is False or IMS not registered.
1905 """
Yang Liub93d7292016-02-19 10:41:40 -08001906 video_status = ad.droid.telephonyIsVideoCallingAvailable()
1907 if video_status is True and is_ims_registered(log, ad) is False:
Yang Liu9b85ce52016-02-16 12:25:11 -08001908 log.error("Error! Video Call is Available, but IMS is not registered.")
1909 return False
Yang Liub93d7292016-02-19 10:41:40 -08001910 return video_status
Ang Li73697b32015-12-03 00:41:53 +00001911
Nathan Harold123c9da2015-12-30 16:33:25 -08001912
Ang Li73697b32015-12-03 00:41:53 +00001913def wait_for_volte_enabled(log, ad, max_time):
1914 """Wait for android device to report VoLTE enabled bit true.
1915
1916 Args:
1917 log: log object.
1918 ad: android device.
1919 max_time: maximal wait time.
1920
1921 Returns:
1922 Return True if device report VoLTE enabled bit true within max_time.
1923 Return False if timeout.
1924 """
Yang Liu9b85ce52016-02-16 12:25:11 -08001925 return _wait_for_droid_in_state(log, ad, max_time, is_volte_enabled)
Ang Li73697b32015-12-03 00:41:53 +00001926
Nathan Harold123c9da2015-12-30 16:33:25 -08001927
Ang Li73697b32015-12-03 00:41:53 +00001928def wait_for_video_enabled(log, ad, max_time):
1929 """Wait for android device to report Video Telephony enabled bit true.
1930
1931 Args:
1932 log: log object.
1933 ad: android device.
1934 max_time: maximal wait time.
1935
1936 Returns:
1937 Return True if device report Video Telephony enabled bit true within max_time.
1938 Return False if timeout.
1939 """
Yang Liu9b85ce52016-02-16 12:25:11 -08001940 return _wait_for_droid_in_state(log, ad, max_time, is_video_enabled)
Ang Li73697b32015-12-03 00:41:53 +00001941
Nathan Harold123c9da2015-12-30 16:33:25 -08001942
Ang Li73697b32015-12-03 00:41:53 +00001943def is_wfc_enabled(log, ad):
1944 """Return True if WiFi Calling feature bit is True.
1945
1946 Args:
1947 log: log object.
1948 ad: android device.
1949
1950 Returns:
Yang Liu9b85ce52016-02-16 12:25:11 -08001951 Return True if WiFi Calling feature bit is True and IMS registered.
1952 Return False if WiFi Calling feature bit is False or IMS not registered.
Ang Li73697b32015-12-03 00:41:53 +00001953 """
Yang Liub93d7292016-02-19 10:41:40 -08001954 wfc_status = ad.droid.telephonyIsWifiCallingAvailable()
1955 if wfc_status is True and is_ims_registered(log, ad) is False:
Yang Liu9b85ce52016-02-16 12:25:11 -08001956 log.error("Error! WiFi Calling is Available, but IMS is not registered.")
1957 return False
Yang Liub93d7292016-02-19 10:41:40 -08001958 return wfc_status
Ang Li73697b32015-12-03 00:41:53 +00001959
Nathan Harold123c9da2015-12-30 16:33:25 -08001960
Yang Liudfc37b62016-01-20 18:08:47 -08001961def wait_for_wfc_enabled(log, ad, max_time=MAX_WAIT_TIME_WFC_ENABLED):
Ang Li73697b32015-12-03 00:41:53 +00001962 """Wait for android device to report WiFi Calling enabled bit true.
1963
1964 Args:
1965 log: log object.
1966 ad: android device.
1967 max_time: maximal wait time.
Yang Liudfc37b62016-01-20 18:08:47 -08001968 Default value is MAX_WAIT_TIME_WFC_ENABLED.
Ang Li73697b32015-12-03 00:41:53 +00001969
1970 Returns:
1971 Return True if device report WiFi Calling enabled bit true within max_time.
1972 Return False if timeout.
1973 """
1974 return _wait_for_droid_in_state(log, ad, max_time, is_wfc_enabled)
1975
Nathan Harold123c9da2015-12-30 16:33:25 -08001976
Yang Liudfc37b62016-01-20 18:08:47 -08001977def wait_for_wfc_disabled(log, ad, max_time=MAX_WAIT_TIME_WFC_DISABLED):
Ang Li73697b32015-12-03 00:41:53 +00001978 """Wait for android device to report WiFi Calling enabled bit false.
1979
1980 Args:
1981 log: log object.
1982 ad: android device.
1983 max_time: maximal wait time.
Yang Liudfc37b62016-01-20 18:08:47 -08001984 Default value is MAX_WAIT_TIME_WFC_DISABLED.
Ang Li73697b32015-12-03 00:41:53 +00001985
1986 Returns:
1987 Return True if device report WiFi Calling enabled bit false within max_time.
1988 Return False if timeout.
1989 """
Nathan Harold123c9da2015-12-30 16:33:25 -08001990 return _wait_for_droid_in_state(
1991 log, ad, max_time, lambda log, ad: not is_wfc_enabled(log, ad))
1992
Ang Li73697b32015-12-03 00:41:53 +00001993
1994def get_phone_number(log, ad):
1995 """Get phone number for default subscription
1996
1997 Args:
1998 log: log object.
1999 ad: Android device object.
2000
2001 Returns:
2002 Phone number.
2003 """
Nathan Harold123c9da2015-12-30 16:33:25 -08002004 return get_phone_number_for_subscription(
2005 log, ad, ad.droid.subscriptionGetDefaultVoiceSubId())
2006
Ang Li73697b32015-12-03 00:41:53 +00002007
2008def get_phone_number_for_subscription(log, ad, subid):
2009 """Get phone number for subscription
2010
2011 Args:
2012 log: log object.
2013 ad: Android device object.
2014 subid: subscription id.
2015
2016 Returns:
2017 Phone number.
2018 """
2019 number = None
2020 try:
2021 number = ad.cfg['subscription'][subid]['phone_num']
2022 except KeyError:
Yang Liuaed3eef2015-12-15 18:40:25 -08002023 number = ad.droid.telephonyGetLine1NumberForSubscription(subid)
Ang Li73697b32015-12-03 00:41:53 +00002024 return number
2025
Nathan Harold123c9da2015-12-30 16:33:25 -08002026
Ang Li73697b32015-12-03 00:41:53 +00002027def set_phone_number(log, ad, phone_num):
2028 """Set phone number for default subscription
2029
2030 Args:
2031 log: log object.
2032 ad: Android device object.
2033 phone_num: phone number string.
2034
2035 Returns:
2036 True if success.
2037 """
Nathan Harold123c9da2015-12-30 16:33:25 -08002038 return set_phone_number_for_subscription(
2039 log, ad, ad.droid.subscriptionGetDefaultVoiceSubId(), phone_num)
2040
Ang Li73697b32015-12-03 00:41:53 +00002041
2042def set_phone_number_for_subscription(log, ad, subid, phone_num):
2043 """Set phone number for subscription
2044
2045 Args:
2046 log: log object.
2047 ad: Android device object.
2048 subid: subscription id.
2049 phone_num: phone number string.
2050
2051 Returns:
2052 True if success.
2053 """
2054 try:
2055 ad.cfg['subscription'][subid]['phone_num'] = phone_num
2056 except Exception:
2057 return False
2058 return True
2059
Nathan Harold123c9da2015-12-30 16:33:25 -08002060
Ang Li73697b32015-12-03 00:41:53 +00002061def get_operator_name(log, ad, subId=None):
2062 """Get operator name (e.g. vzw, tmo) of droid.
2063
2064 Args:
2065 ad: Android device object.
2066 sub_id: subscription ID
2067 Optional, default is None
2068
2069 Returns:
2070 Operator name.
2071 """
2072 try:
2073 if subId is not None:
2074 result = operator_name_from_plmn_id(
Yang Liuaed3eef2015-12-15 18:40:25 -08002075 ad.droid.telephonyGetSimOperatorForSubscription(subId))
Ang Li73697b32015-12-03 00:41:53 +00002076 else:
2077 result = operator_name_from_plmn_id(
Yang Liuaed3eef2015-12-15 18:40:25 -08002078 ad.droid.telephonyGetSimOperator())
Ang Li73697b32015-12-03 00:41:53 +00002079 except KeyError:
2080 result = CARRIER_UNKNOWN
2081 return result
2082
Nathan Harold123c9da2015-12-30 16:33:25 -08002083
Yang Liu9a9a4f72016-02-19 10:45:04 -08002084def get_model_name(ad):
2085 """Get android device model name
2086
2087 Args:
2088 ad: Android device object
2089
2090 Returns:
2091 model name string
2092 """
2093 # TODO: Create translate table.
2094 model = ad.model
2095 if(model.startswith(AOSP_PREFIX)):
2096 model = model[len(AOSP_PREFIX):]
2097 return model
2098
2099
Ang Li73697b32015-12-03 00:41:53 +00002100def is_sms_match(event, phonenumber_tx, text):
2101 """Return True if 'text' equals to event['data']['Text']
2102 and phone number match.
2103
2104 Args:
2105 event: Event object to verify.
2106 phonenumber_tx: phone number for sender.
2107 text: text string to verify.
2108
2109 Returns:
2110 Return True if 'text' equals to event['data']['Text']
2111 and phone number match.
2112 """
Nathan Harold123c9da2015-12-30 16:33:25 -08002113 return (
2114 check_phone_number_match(event['data']['Sender'], phonenumber_tx) and
2115 event['data']['Text'] == text)
2116
Ang Li73697b32015-12-03 00:41:53 +00002117
2118def is_sms_partial_match(event, phonenumber_tx, text):
2119 """Return True if 'text' starts with event['data']['Text']
2120 and phone number match.
2121
2122 Args:
2123 event: Event object to verify.
2124 phonenumber_tx: phone number for sender.
2125 text: text string to verify.
2126
2127 Returns:
2128 Return True if 'text' starts with event['data']['Text']
2129 and phone number match.
2130 """
Nathan Harold123c9da2015-12-30 16:33:25 -08002131 return (
2132 check_phone_number_match(event['data']['Sender'], phonenumber_tx) and
2133 text.startswith(event['data']['Text']))
2134
Ang Li73697b32015-12-03 00:41:53 +00002135
2136def sms_send_receive_verify(log, ad_tx, ad_rx, array_message):
2137 """Send SMS, receive SMS, and verify content and sender's number.
2138
2139 Send (several) SMS from droid_tx to droid_rx.
2140 Verify SMS is sent, delivered and received.
2141 Verify received content and sender's number are correct.
2142
2143 Args:
2144 log: Log object.
2145 ad_tx: Sender's Android Device Object
2146 ad_rx: Receiver's Android Device Object
2147 array_message: the array of message to send/receive
2148 """
Nathan Harold123c9da2015-12-30 16:33:25 -08002149 return sms_send_receive_verify_for_subscription(
2150 log, ad_tx, ad_rx, ad_tx.droid.subscriptionGetDefaultSmsSubId(),
2151 ad_rx.droid.subscriptionGetDefaultSmsSubId(), array_message)
Ang Li73697b32015-12-03 00:41:53 +00002152
Nathan Harold123c9da2015-12-30 16:33:25 -08002153
2154def wait_for_matching_sms(log,
2155 ad_rx,
2156 phonenumber_tx,
2157 text,
2158 allow_multi_part_long_sms=True):
Ang Li73697b32015-12-03 00:41:53 +00002159 """Wait for matching incoming SMS.
2160
2161 Args:
2162 log: Log object.
2163 ad_rx: Receiver's Android Device Object
2164 phonenumber_tx: Sender's phone number.
2165 text: SMS content string.
2166 allow_multi_part_long_sms: is long SMS allowed to be received as
2167 multiple short SMS. This is optional, default value is True.
2168
2169 Returns:
2170 True if matching incoming SMS is received.
2171 """
2172 if not allow_multi_part_long_sms:
2173 try:
2174 ad_rx.ed.wait_for_event(EventSmsReceived, is_sms_match,
Yang Liudf164e32016-01-07 16:49:32 -08002175 MAX_WAIT_TIME_SMS_RECEIVE, phonenumber_tx,
Nathan Harold123c9da2015-12-30 16:33:25 -08002176 text)
Ang Li73697b32015-12-03 00:41:53 +00002177 return True
2178 except Empty:
2179 log.error("No matched SMS received event.")
2180 return False
2181 else:
2182 try:
2183 received_sms = ''
Nathan Harold123c9da2015-12-30 16:33:25 -08002184 while (text != ''):
2185 event = ad_rx.ed.wait_for_event(
2186 EventSmsReceived, is_sms_partial_match,
Yang Liudf164e32016-01-07 16:49:32 -08002187 MAX_WAIT_TIME_SMS_RECEIVE, phonenumber_tx, text)
Ang Li73697b32015-12-03 00:41:53 +00002188 text = text[len(event['data']['Text']):]
2189 received_sms += event['data']['Text']
2190 return True
2191 except Empty:
2192 log.error("No matched SMS received event.")
2193 if received_sms != '':
Nathan Harold123c9da2015-12-30 16:33:25 -08002194 log.error("Only received partial matched SMS: {}".format(
2195 received_sms))
Ang Li73697b32015-12-03 00:41:53 +00002196 return False
2197
Nathan Harold123c9da2015-12-30 16:33:25 -08002198
Ang Li73697b32015-12-03 00:41:53 +00002199def sms_send_receive_verify_for_subscription(log, ad_tx, ad_rx, subid_tx,
Nathan Harold123c9da2015-12-30 16:33:25 -08002200 subid_rx, array_message):
Ang Li73697b32015-12-03 00:41:53 +00002201 """Send SMS, receive SMS, and verify content and sender's number.
2202
2203 Send (several) SMS from droid_tx to droid_rx.
2204 Verify SMS is sent, delivered and received.
2205 Verify received content and sender's number are correct.
2206
2207 Args:
2208 log: Log object.
2209 ad_tx: Sender's Android Device Object..
2210 ad_rx: Receiver's Android Device Object.
2211 subid_tx: Sender's subsciption ID to be used for SMS
2212 subid_rx: Receiver's subsciption ID to be used for SMS
2213 array_message: the array of message to send/receive
2214 """
2215
2216 phonenumber_tx = ad_tx.cfg['subscription'][subid_tx]['phone_num']
2217 phonenumber_rx = ad_rx.cfg['subscription'][subid_rx]['phone_num']
2218 for text in array_message:
Nathan Harold123c9da2015-12-30 16:33:25 -08002219 log.info("Sending SMS {} to {}, len: {}, content: {}.".format(
2220 phonenumber_tx, phonenumber_rx, len(text), text))
Ang Li73697b32015-12-03 00:41:53 +00002221 result = False
2222 ad_rx.ed.clear_all_events()
2223 ad_rx.droid.smsStartTrackingIncomingSmsMessage()
2224 try:
2225 ad_tx.droid.smsSendTextMessage(phonenumber_rx, text, True)
2226
2227 try:
Nathan Harold123c9da2015-12-30 16:33:25 -08002228 ad_tx.ed.pop_event(EventSmsSentSuccess,
Yang Liudf164e32016-01-07 16:49:32 -08002229 MAX_WAIT_TIME_SMS_SENT_SUCCESS)
Ang Li73697b32015-12-03 00:41:53 +00002230 except Empty:
2231 log.error("No sent_success event.")
2232 return False
2233
Nathan Harold123c9da2015-12-30 16:33:25 -08002234 if not wait_for_matching_sms(log,
2235 ad_rx,
2236 phonenumber_tx,
2237 text,
2238 allow_multi_part_long_sms=True):
Ang Li73697b32015-12-03 00:41:53 +00002239 return False
2240 finally:
2241 ad_rx.droid.smsStopTrackingIncomingSmsMessage()
2242 return True
2243
Nathan Harold123c9da2015-12-30 16:33:25 -08002244
Ang Li73697b32015-12-03 00:41:53 +00002245def mms_send_receive_verify(log, ad_tx, ad_rx, array_message):
2246 """Send SMS, receive SMS, and verify content and sender's number.
2247
2248 Send (several) SMS from droid_tx to droid_rx.
2249 Verify SMS is sent, delivered and received.
2250 Verify received content and sender's number are correct.
2251
2252 Args:
2253 log: Log object.
2254 ad_tx: Sender's Android Device Object
2255 ad_rx: Receiver's Android Device Object
2256 array_message: the array of message to send/receive
2257 """
Nathan Harold123c9da2015-12-30 16:33:25 -08002258 return mms_send_receive_verify_for_subscription(
2259 log, ad_tx, ad_rx, ad_tx.droid.subscriptionGetDefaultSmsSubId(),
2260 ad_rx.droid.subscriptionGetDefaultSmsSubId(), array_message)
2261
Ang Li73697b32015-12-03 00:41:53 +00002262
Yang Liu7a2e7ee2015-12-28 15:32:44 -08002263#TODO: b/21569494 This function is still a WIP and is disabled
Ang Li73697b32015-12-03 00:41:53 +00002264def mms_send_receive_verify_for_subscription(log, ad_tx, ad_rx, subid_tx,
Nathan Harold123c9da2015-12-30 16:33:25 -08002265 subid_rx, array_payload):
Ang Li73697b32015-12-03 00:41:53 +00002266 """Send SMS, receive SMS, and verify content and sender's number.
2267
2268 Send (several) SMS from droid_tx to droid_rx.
2269 Verify SMS is sent, delivered and received.
2270 Verify received content and sender's number are correct.
2271
2272 Args:
2273 log: Log object.
2274 ad_tx: Sender's Android Device Object..
2275 ad_rx: Receiver's Android Device Object.
2276 subid_tx: Sender's subsciption ID to be used for SMS
2277 subid_rx: Receiver's subsciption ID to be used for SMS
2278 array_message: the array of message to send/receive
2279 """
2280
2281 log.error("Function is non-working: b/21569494")
2282 return False
2283
2284 phonenumber_tx = ad_tx.cfg['subscription'][subid_tx]['phone_num']
2285 phonenumber_rx = ad_rx.cfg['subscription'][subid_rx]['phone_num']
2286 for subject, message, filename in array_payload:
Nathan Harold123c9da2015-12-30 16:33:25 -08002287 log.info("Sending MMS {} to {}, subject: {}, message: {}.".format(
2288 phonenumber_tx, phonenumber_rx, subject, message))
Ang Li73697b32015-12-03 00:41:53 +00002289 result = False
2290 ad_rx.ed.clear_all_events()
2291 ad_rx.droid.smsStartTrackingIncomingMmsMessage()
2292 ad_rx.droid.smsStartTrackingIncomingSmsMessage()
2293 try:
2294 ad_tx.droid.smsSendMultimediaMessage(
Nathan Harold123c9da2015-12-30 16:33:25 -08002295 phonenumber_rx, subject, message, phonenumber_tx, filename)
Ang Li73697b32015-12-03 00:41:53 +00002296
Yang Liudf164e32016-01-07 16:49:32 -08002297 ad_tx.ed.pop_event(EventMmsSentSuccess, MAX_WAIT_TIME_SMS_SENT_SUCCESS)
Ang Li73697b32015-12-03 00:41:53 +00002298
2299 start_time = time.time()
Yang Liudf164e32016-01-07 16:49:32 -08002300 remaining_time = MAX_WAIT_TIME_SMS_RECEIVE
Ang Li73697b32015-12-03 00:41:53 +00002301 while remaining_time > 0:
2302 event = ad_rx.ed.pop_event(EventSmsReceived, remaining_time)
Nathan Harold123c9da2015-12-30 16:33:25 -08002303 if check_phone_number_match(event['data']['Sender'],
2304 phonenumber_tx):
Ang Li73697b32015-12-03 00:41:53 +00002305 log.debug("Received SMS Indication")
2306 while remaining_time > 0:
Nathan Harold123c9da2015-12-30 16:33:25 -08002307 event = ad_rx.ed.pop_event(EventDataSmsReceived,
2308 remaining_time)
2309 if check_phone_number_match(event['data']['Sender'],
2310 phonenumber_tx):
2311 result = True
2312 break
Ang Li73697b32015-12-03 00:41:53 +00002313 remaining_time = time.time() - start_time
2314 remaining_time = time.time() - start_time
2315
2316 if not result:
2317 log.info("Expected sender:" + phonenumber_tx)
2318 log.error("Received sender:" + event['data']['Sender'])
2319 log.error("Failed in verify receiving MMS.")
2320 return False
2321 finally:
2322 ad_rx.droid.smsStopTrackingIncomingSmsMessage()
2323 ad_rx.droid.smsStopTrackingIncomingMmsMessage()
2324 return True
2325
Nathan Harold123c9da2015-12-30 16:33:25 -08002326
2327def ensure_network_rat(log,
2328 ad,
2329 network_preference,
2330 rat_family,
2331 voice_or_data=None,
Yang Liudf164e32016-01-07 16:49:32 -08002332 max_wait_time=MAX_WAIT_TIME_NW_SELECTION,
Nathan Harold123c9da2015-12-30 16:33:25 -08002333 toggle_apm_after_setting=False):
Yang Liue23e5b12015-12-07 17:17:27 -08002334 """Ensure ad's current network is in expected rat_family.
2335 """
Nathan Harold123c9da2015-12-30 16:33:25 -08002336 return ensure_network_rat_for_subscription(
2337 log, ad, ad.droid.subscriptionGetDefaultSubId(), network_preference,
Yang Liue23e5b12015-12-07 17:17:27 -08002338 rat_family, voice_or_data, max_wait_time, toggle_apm_after_setting)
Ang Li73697b32015-12-03 00:41:53 +00002339
Nathan Harold123c9da2015-12-30 16:33:25 -08002340
2341def ensure_network_rat_for_subscription(log,
2342 ad,
2343 sub_id,
2344 network_preference,
2345 rat_family,
2346 voice_or_data=None,
Yang Liudf164e32016-01-07 16:49:32 -08002347 max_wait_time=MAX_WAIT_TIME_NW_SELECTION,
Nathan Harold123c9da2015-12-30 16:33:25 -08002348 toggle_apm_after_setting=False):
Yang Liue23e5b12015-12-07 17:17:27 -08002349 """Ensure ad's current network is in expected rat_family.
2350 """
Nathan Harold123c9da2015-12-30 16:33:25 -08002351 if not ad.droid.telephonySetPreferredNetworkTypesForSubscription(
2352 network_preference, sub_id):
Yang Liue23e5b12015-12-07 17:17:27 -08002353 log.error("Set Preferred Networks failed.")
2354 return False
2355 if is_droid_in_rat_family_for_subscription(log, ad, sub_id, rat_family,
Nathan Harold123c9da2015-12-30 16:33:25 -08002356 voice_or_data):
Yang Liue23e5b12015-12-07 17:17:27 -08002357 return True
2358
2359 if toggle_apm_after_setting:
2360 toggle_airplane_mode(log, ad, True)
2361 time.sleep(WAIT_TIME_ANDROID_STATE_SETTLING)
2362 toggle_airplane_mode(log, ad, False)
2363
2364 result = wait_for_network_rat_for_subscription(
2365 log, ad, sub_id, rat_family, max_wait_time, voice_or_data)
2366
Nathan Harold123c9da2015-12-30 16:33:25 -08002367 log.info(
2368 "End of ensure_network_rat_for_subscription for {}. "
2369 "Setting to {}, Expecting {} {}. Current: voice: {}(family: {}), "
2370 "data: {}(family: {})".format(
2371 ad.serial, network_preference, rat_family, voice_or_data,
2372 ad.droid.telephonyGetCurrentVoiceNetworkTypeForSubscription(
2373 sub_id),
2374 rat_family_from_rat(
2375 ad.droid.telephonyGetCurrentVoiceNetworkTypeForSubscription(
2376 sub_id)),
2377 ad.droid.telephonyGetCurrentDataNetworkTypeForSubscription(
2378 sub_id), rat_family_from_rat(
2379 ad.droid.telephonyGetCurrentDataNetworkTypeForSubscription(
2380 sub_id))))
Yang Liue23e5b12015-12-07 17:17:27 -08002381 return result
2382
Nathan Harold123c9da2015-12-30 16:33:25 -08002383
2384def ensure_network_preference(log,
2385 ad,
2386 network_preference,
2387 voice_or_data=None,
Yang Liudf164e32016-01-07 16:49:32 -08002388 max_wait_time=MAX_WAIT_TIME_NW_SELECTION,
Nathan Harold123c9da2015-12-30 16:33:25 -08002389 toggle_apm_after_setting=False):
Yang Liue23e5b12015-12-07 17:17:27 -08002390 """Ensure that current rat is within the device's preferred network rats.
2391 """
Nathan Harold123c9da2015-12-30 16:33:25 -08002392 return ensure_network_preference_for_subscription(
2393 log, ad, ad.droid.subscriptionGetDefaultSubId(), network_preference,
Yang Liue23e5b12015-12-07 17:17:27 -08002394 voice_or_data, max_wait_time, toggle_apm_after_setting)
2395
Nathan Harold123c9da2015-12-30 16:33:25 -08002396
2397def ensure_network_preference_for_subscription(
2398 log,
2399 ad,
2400 sub_id,
2401 network_preference,
2402 voice_or_data=None,
Yang Liudf164e32016-01-07 16:49:32 -08002403 max_wait_time=MAX_WAIT_TIME_NW_SELECTION,
Nathan Harold123c9da2015-12-30 16:33:25 -08002404 toggle_apm_after_setting=False):
Yang Liue23e5b12015-12-07 17:17:27 -08002405 """Ensure ad's network preference is <network_preference> for sub_id.
2406 """
2407 rat_family_list = rat_families_for_network_preference(network_preference)
Nathan Harold123c9da2015-12-30 16:33:25 -08002408 if not ad.droid.telephonySetPreferredNetworkTypesForSubscription(
2409 network_preference, sub_id):
Yang Liue23e5b12015-12-07 17:17:27 -08002410 log.error("Set Preferred Networks failed.")
2411 return False
Nathan Harold123c9da2015-12-30 16:33:25 -08002412 if is_droid_in_rat_family_list_for_subscription(
2413 log, ad, sub_id, rat_family_list, voice_or_data):
Yang Liue23e5b12015-12-07 17:17:27 -08002414 return True
2415
2416 if toggle_apm_after_setting:
2417 toggle_airplane_mode(log, ad, True)
2418 time.sleep(WAIT_TIME_ANDROID_STATE_SETTLING)
2419 toggle_airplane_mode(log, ad, False)
2420
2421 result = wait_for_preferred_network_for_subscription(
2422 log, ad, sub_id, network_preference, max_wait_time, voice_or_data)
2423
Nathan Harold123c9da2015-12-30 16:33:25 -08002424 log.info(
2425 "End of ensure_network_preference_for_subscription for {}. "
2426 "Setting to {}, Expecting {} {}. Current: voice: {}(family: {}), "
2427 "data: {}(family: {})".format(
2428 ad.serial, network_preference, rat_family_list, voice_or_data,
2429 ad.droid.telephonyGetCurrentVoiceNetworkTypeForSubscription(
2430 sub_id),
2431 rat_family_from_rat(
2432 ad.droid.telephonyGetCurrentVoiceNetworkTypeForSubscription(
2433 sub_id)),
2434 ad.droid.telephonyGetCurrentDataNetworkTypeForSubscription(
2435 sub_id), rat_family_from_rat(
2436 ad.droid.telephonyGetCurrentDataNetworkTypeForSubscription(
2437 sub_id))))
Yang Liue23e5b12015-12-07 17:17:27 -08002438 return result
2439
Nathan Harold123c9da2015-12-30 16:33:25 -08002440
2441def ensure_network_generation(log,
2442 ad,
2443 generation,
Yang Liudf164e32016-01-07 16:49:32 -08002444 max_wait_time=MAX_WAIT_TIME_NW_SELECTION,
Nathan Harold123c9da2015-12-30 16:33:25 -08002445 voice_or_data=None,
2446 toggle_apm_after_setting=False):
Yang Liue23e5b12015-12-07 17:17:27 -08002447 """Ensure ad's network is <network generation> for default subscription ID.
2448
2449 Set preferred network generation to <generation>.
2450 Toggle ON/OFF airplane mode if necessary.
2451 Wait for ad in expected network type.
2452 """
2453 return ensure_network_generation_for_subscription(
Nathan Harold123c9da2015-12-30 16:33:25 -08002454 log, ad, ad.droid.subscriptionGetDefaultSubId(), generation,
2455 max_wait_time, voice_or_data, toggle_apm_after_setting)
Yang Liue23e5b12015-12-07 17:17:27 -08002456
Nathan Harold123c9da2015-12-30 16:33:25 -08002457
2458def ensure_network_generation_for_subscription(
2459 log,
2460 ad,
2461 sub_id,
2462 generation,
Yang Liudf164e32016-01-07 16:49:32 -08002463 max_wait_time=MAX_WAIT_TIME_NW_SELECTION,
Nathan Harold123c9da2015-12-30 16:33:25 -08002464 voice_or_data=None,
2465 toggle_apm_after_setting=False):
Yang Liue23e5b12015-12-07 17:17:27 -08002466 """Ensure ad's network is <network generation> for specified subscription ID.
2467
2468 Set preferred network generation to <generation>.
2469 Toggle ON/OFF airplane mode if necessary.
2470 Wait for ad in expected network type.
2471 """
Nathan Harold123c9da2015-12-30 16:33:25 -08002472 if is_droid_in_network_generation_for_subscription(
2473 log, ad, sub_id, generation, voice_or_data):
Yang Liue23e5b12015-12-07 17:17:27 -08002474 return True
2475
2476 operator = get_operator_name(log, ad, sub_id)
2477 try:
Nathan Harold123c9da2015-12-30 16:33:25 -08002478 network_preference = network_preference_for_generaton(generation,
2479 operator)
Yang Liue23e5b12015-12-07 17:17:27 -08002480 rat_family = rat_family_for_generation(generation, operator)
2481 except KeyError:
2482 log.error("Failed to find a rat_family entry for "
Nathan Harold123c9da2015-12-30 16:33:25 -08002483 "PLMN: {}, operator:{}, generation: {}".format(
2484 ad.droid.telephonyGetSimOperatorForSubscription(
2485 sub_id), operator, generation))
Yang Liue23e5b12015-12-07 17:17:27 -08002486 return False
2487
Nathan Harold123c9da2015-12-30 16:33:25 -08002488 if not ad.droid.telephonySetPreferredNetworkTypesForSubscription(
2489 network_preference, sub_id):
Yang Liue23e5b12015-12-07 17:17:27 -08002490 log.error("Set Preferred Networks failed.")
2491 return False
2492
2493 if toggle_apm_after_setting:
2494 toggle_airplane_mode(log, ad, True)
2495 time.sleep(WAIT_TIME_ANDROID_STATE_SETTLING)
2496 toggle_airplane_mode(log, ad, False)
2497
2498 result = wait_for_network_generation_for_subscription(
2499 log, ad, sub_id, generation, max_wait_time, voice_or_data)
2500
Nathan Harold123c9da2015-12-30 16:33:25 -08002501 log.info(
2502 "End of ensure_network_generation_for_subscription for {}. "
2503 "Setting to {}, Expecting {} {}. Current: voice: {}(family: {}), "
2504 "data: {}(family: {})".format(
2505 ad.serial, network_preference, generation, voice_or_data,
2506 ad.droid.telephonyGetCurrentVoiceNetworkTypeForSubscription(
2507 sub_id),
2508 rat_generation_from_rat(
2509 ad.droid.telephonyGetCurrentVoiceNetworkTypeForSubscription(
2510 sub_id)),
2511 ad.droid.telephonyGetCurrentDataNetworkTypeForSubscription(
2512 sub_id), rat_generation_from_rat(
2513 ad.droid.telephonyGetCurrentDataNetworkTypeForSubscription(
2514 sub_id))))
Yang Liue23e5b12015-12-07 17:17:27 -08002515
2516 return result
2517
Yang Liue23e5b12015-12-07 17:17:27 -08002518
Nathan Harold123c9da2015-12-30 16:33:25 -08002519def wait_for_network_rat(log,
2520 ad,
2521 rat_family,
Yang Liudf164e32016-01-07 16:49:32 -08002522 max_wait_time=MAX_WAIT_TIME_NW_SELECTION,
Nathan Harold123c9da2015-12-30 16:33:25 -08002523 voice_or_data=None):
2524 return wait_for_network_rat_for_subscription(
2525 log, ad, ad.droid.subscriptionGetDefaultSubId(), rat_family,
2526 max_wait_time, voice_or_data)
2527
2528
2529def wait_for_network_rat_for_subscription(log,
2530 ad,
2531 sub_id,
2532 rat_family,
Yang Liudf164e32016-01-07 16:49:32 -08002533 max_wait_time=MAX_WAIT_TIME_NW_SELECTION,
Nathan Harold123c9da2015-12-30 16:33:25 -08002534 voice_or_data=None):
Yang Liue23e5b12015-12-07 17:17:27 -08002535 return _wait_for_droid_in_state_for_subscription(
2536 log, ad, sub_id, max_wait_time,
2537 is_droid_in_rat_family_for_subscription, rat_family, voice_or_data)
2538
Yang Liue23e5b12015-12-07 17:17:27 -08002539
Nathan Harold123c9da2015-12-30 16:33:25 -08002540def wait_for_not_network_rat(log,
2541 ad,
2542 rat_family,
Yang Liudf164e32016-01-07 16:49:32 -08002543 max_wait_time=MAX_WAIT_TIME_NW_SELECTION,
Nathan Harold123c9da2015-12-30 16:33:25 -08002544 voice_or_data=None):
2545 return wait_for_not_network_rat_for_subscription(
2546 log, ad, ad.droid.subscriptionGetDefaultSubId(), rat_family,
2547 max_wait_time, voice_or_data)
2548
2549
2550def wait_for_not_network_rat_for_subscription(
2551 log,
2552 ad,
2553 sub_id,
2554 rat_family,
Yang Liudf164e32016-01-07 16:49:32 -08002555 max_wait_time=MAX_WAIT_TIME_NW_SELECTION,
Nathan Harold123c9da2015-12-30 16:33:25 -08002556 voice_or_data=None):
Yang Liue23e5b12015-12-07 17:17:27 -08002557 return _wait_for_droid_in_state_for_subscription(
2558 log, ad, sub_id, max_wait_time,
Nathan Harold123c9da2015-12-30 16:33:25 -08002559 lambda log, ad, sub_id, *args, **kwargs:
Yang Liue23e5b12015-12-07 17:17:27 -08002560 not is_droid_in_rat_family_for_subscription(
2561 log, ad, sub_id, rat_family, voice_or_data))
2562
Nathan Harold123c9da2015-12-30 16:33:25 -08002563
2564def wait_for_preferred_network(log,
2565 ad,
2566 network_preference,
Yang Liudf164e32016-01-07 16:49:32 -08002567 max_wait_time=MAX_WAIT_TIME_NW_SELECTION,
Nathan Harold123c9da2015-12-30 16:33:25 -08002568 voice_or_data=None):
2569 return wait_for_preferred_network_for_subscription(
2570 log, ad, ad.droid.subscriptionGetDefaultSubId(), network_preference,
Yang Liue23e5b12015-12-07 17:17:27 -08002571 max_wait_time, voice_or_data)
2572
Nathan Harold123c9da2015-12-30 16:33:25 -08002573
2574def wait_for_preferred_network_for_subscription(
2575 log,
2576 ad,
2577 sub_id,
2578 network_preference,
Yang Liudf164e32016-01-07 16:49:32 -08002579 max_wait_time=MAX_WAIT_TIME_NW_SELECTION,
Nathan Harold123c9da2015-12-30 16:33:25 -08002580 voice_or_data=None):
Yang Liue23e5b12015-12-07 17:17:27 -08002581 rat_family_list = rat_families_for_network_preference(network_preference)
2582 return _wait_for_droid_in_state_for_subscription(
2583 log, ad, sub_id, max_wait_time,
2584 is_droid_in_rat_family_list_for_subscription, rat_family_list,
2585 voice_or_data)
2586
Nathan Harold123c9da2015-12-30 16:33:25 -08002587
2588def wait_for_network_generation(log,
2589 ad,
2590 generation,
Yang Liudf164e32016-01-07 16:49:32 -08002591 max_wait_time=MAX_WAIT_TIME_NW_SELECTION,
Nathan Harold123c9da2015-12-30 16:33:25 -08002592 voice_or_data=None):
2593 return wait_for_network_generation_for_subscription(
2594 log, ad, ad.droid.subscriptionGetDefaultSubId(), generation,
2595 max_wait_time, voice_or_data)
2596
2597
2598def wait_for_network_generation_for_subscription(
2599 log,
2600 ad,
2601 sub_id,
2602 generation,
Yang Liudf164e32016-01-07 16:49:32 -08002603 max_wait_time=MAX_WAIT_TIME_NW_SELECTION,
Nathan Harold123c9da2015-12-30 16:33:25 -08002604 voice_or_data=None):
2605 return _wait_for_droid_in_state_for_subscription(
2606 log, ad, sub_id, max_wait_time,
2607 is_droid_in_network_generation_for_subscription, generation,
Yang Liue23e5b12015-12-07 17:17:27 -08002608 voice_or_data)
2609
Yang Liue23e5b12015-12-07 17:17:27 -08002610
2611def is_droid_in_rat_family(log, ad, rat_family, voice_or_data=None):
Nathan Harold123c9da2015-12-30 16:33:25 -08002612 return is_droid_in_rat_family_for_subscription(
2613 log, ad, ad.droid.subscriptionGetDefaultSubId(), rat_family,
2614 voice_or_data)
Yang Liue23e5b12015-12-07 17:17:27 -08002615
Yang Liue23e5b12015-12-07 17:17:27 -08002616
Nathan Harold123c9da2015-12-30 16:33:25 -08002617def is_droid_in_rat_family_for_subscription(log,
2618 ad,
2619 sub_id,
2620 rat_family,
2621 voice_or_data=None):
2622 return is_droid_in_rat_family_list_for_subscription(
2623 log, ad, sub_id, [rat_family], voice_or_data)
Yang Liue23e5b12015-12-07 17:17:27 -08002624
Nathan Harold123c9da2015-12-30 16:33:25 -08002625
2626def is_droid_in_rat_familiy_list(log, ad, rat_family_list, voice_or_data=None):
2627 return is_droid_in_rat_family_list_for_subscription(
2628 log, ad, ad.droid.subscriptionGetDefaultSubId(), rat_family_list,
2629 voice_or_data)
2630
2631
2632def is_droid_in_rat_family_list_for_subscription(log,
2633 ad,
2634 sub_id,
2635 rat_family_list,
2636 voice_or_data=None):
Yang Liue23e5b12015-12-07 17:17:27 -08002637 service_list = [NETWORK_SERVICE_DATA, NETWORK_SERVICE_VOICE]
2638 if voice_or_data:
2639 service_list = [voice_or_data]
2640
2641 for service in service_list:
2642 nw_rat = get_network_rat_for_subscription(log, ad, sub_id, service)
2643 if nw_rat == RAT_UNKNOWN or not is_valid_rat(nw_rat):
2644 continue
Yang Liu7a2e7ee2015-12-28 15:32:44 -08002645 if rat_family_from_rat(nw_rat) in rat_family_list:
Yang Liue23e5b12015-12-07 17:17:27 -08002646 return True
2647 return False
2648
Nathan Harold123c9da2015-12-30 16:33:25 -08002649
Yang Liue23e5b12015-12-07 17:17:27 -08002650def is_droid_in_network_generation(log, ad, nw_gen, voice_or_data):
2651 """Checks if a droid in expected network generation ("2g", "3g" or "4g").
Ang Li73697b32015-12-03 00:41:53 +00002652
2653 Args:
Yang Liue23e5b12015-12-07 17:17:27 -08002654 log: log object.
2655 ad: android device.
2656 nw_gen: expected generation "4g", "3g", "2g".
2657 voice_or_data: check voice network generation or data network generation
2658 This parameter is optional. If voice_or_data is None, then if
2659 either voice or data in expected generation, function will return True.
Ang Li73697b32015-12-03 00:41:53 +00002660
2661 Returns:
Yang Liue23e5b12015-12-07 17:17:27 -08002662 True if droid in expected network generation. Otherwise False.
Ang Li73697b32015-12-03 00:41:53 +00002663 """
Yang Liue23e5b12015-12-07 17:17:27 -08002664 return is_droid_in_network_generation_for_subscription(
2665 log, ad, ad.droid.subscriptionGetDefaultSubId(), nw_gen, voice_or_data)
Ang Li73697b32015-12-03 00:41:53 +00002666
Nathan Harold123c9da2015-12-30 16:33:25 -08002667
2668def is_droid_in_network_generation_for_subscription(log, ad, sub_id, nw_gen,
2669 voice_or_data):
Yang Liue23e5b12015-12-07 17:17:27 -08002670 """Checks if a droid in expected network generation ("2g", "3g" or "4g").
Ang Li73697b32015-12-03 00:41:53 +00002671
2672 Args:
Yang Liue23e5b12015-12-07 17:17:27 -08002673 log: log object.
2674 ad: android device.
2675 nw_gen: expected generation "4g", "3g", "2g".
2676 voice_or_data: check voice network generation or data network generation
2677 This parameter is optional. If voice_or_data is None, then if
2678 either voice or data in expected generation, function will return True.
Ang Li73697b32015-12-03 00:41:53 +00002679
2680 Returns:
Yang Liue23e5b12015-12-07 17:17:27 -08002681 True if droid in expected network generation. Otherwise False.
Ang Li73697b32015-12-03 00:41:53 +00002682 """
Yang Liue23e5b12015-12-07 17:17:27 -08002683 service_list = [NETWORK_SERVICE_DATA, NETWORK_SERVICE_VOICE]
Ang Li73697b32015-12-03 00:41:53 +00002684
Yang Liue23e5b12015-12-07 17:17:27 -08002685 if voice_or_data:
2686 service_list = [voice_or_data]
Ang Li73697b32015-12-03 00:41:53 +00002687
Yang Liue23e5b12015-12-07 17:17:27 -08002688 for service in service_list:
2689 nw_rat = get_network_rat_for_subscription(log, ad, sub_id, service)
Ang Li73697b32015-12-03 00:41:53 +00002690
Yang Liue23e5b12015-12-07 17:17:27 -08002691 if nw_rat == RAT_UNKNOWN or not is_valid_rat(nw_rat):
2692 continue
2693
Yang Liu7a2e7ee2015-12-28 15:32:44 -08002694 if rat_generation_from_rat(nw_rat) == nw_gen:
Yang Liue23e5b12015-12-07 17:17:27 -08002695 return True
2696 else:
2697 return False
2698
2699 return False
Ang Li73697b32015-12-03 00:41:53 +00002700
Nathan Harold123c9da2015-12-30 16:33:25 -08002701
Ang Li73697b32015-12-03 00:41:53 +00002702def get_network_rat(log, ad, voice_or_data):
2703 """Get current network type (Voice network type, or data network type)
2704 for default subscription id
2705
2706 Args:
2707 ad: Android Device Object
2708 voice_or_data: Input parameter indicating to get voice network type or
2709 data network type.
2710
2711 Returns:
2712 Current voice/data network type.
2713 """
2714 return get_network_rat_for_subscription(
2715 log, ad, ad.droid.subscriptionGetDefaultSubId(), voice_or_data)
2716
Nathan Harold123c9da2015-12-30 16:33:25 -08002717
Ang Li73697b32015-12-03 00:41:53 +00002718def get_network_rat_for_subscription(log, ad, sub_id, voice_or_data):
2719 """Get current network type (Voice network type, or data network type)
2720 for specified subscription id
2721
2722 Args:
2723 ad: Android Device Object
2724 sub_id: subscription ID
2725 voice_or_data: Input parameter indicating to get voice network type or
2726 data network type.
2727
2728 Returns:
2729 Current voice/data network type.
2730 """
2731 if voice_or_data == NETWORK_SERVICE_VOICE:
Nathan Harold123c9da2015-12-30 16:33:25 -08002732 ret_val = ad.droid.telephonyGetCurrentVoiceNetworkTypeForSubscription(
2733 sub_id)
Ang Li73697b32015-12-03 00:41:53 +00002734 elif voice_or_data == NETWORK_SERVICE_DATA:
Nathan Harold123c9da2015-12-30 16:33:25 -08002735 ret_val = ad.droid.telephonyGetCurrentDataNetworkTypeForSubscription(
2736 sub_id)
Ang Li73697b32015-12-03 00:41:53 +00002737 else:
Yang Liuaed3eef2015-12-15 18:40:25 -08002738 ret_val = ad.droid.telephonyGetNetworkTypeForSubscription(sub_id)
Ang Li73697b32015-12-03 00:41:53 +00002739
2740 if ret_val is None:
2741 log.error("get_network_rat(): Unexpected null return value")
2742 return RAT_UNKNOWN
2743 else:
2744 return ret_val
2745
Nathan Harold123c9da2015-12-30 16:33:25 -08002746
Ang Li73697b32015-12-03 00:41:53 +00002747def get_network_gen(log, ad, voice_or_data):
2748 """Get current network generation string (Voice network type, or data network type)
2749
2750 Args:
2751 ad: Android Device Object
2752 voice_or_data: Input parameter indicating to get voice network generation
2753 or data network generation.
2754
2755 Returns:
2756 Current voice/data network generation.
2757 """
2758 return get_network_gen_for_subscription(
2759 log, ad, ad.droid.subscriptionGetDefaultSubId(), voice_or_data)
2760
Nathan Harold123c9da2015-12-30 16:33:25 -08002761
Ang Li73697b32015-12-03 00:41:53 +00002762def get_network_gen_for_subscription(log, ad, sub_id, voice_or_data):
2763 """Get current network generation string (Voice network type, or data network type)
2764
2765 Args:
2766 ad: Android Device Object
2767 voice_or_data: Input parameter indicating to get voice network generation
2768 or data network generation.
2769
2770 Returns:
2771 Current voice/data network generation.
2772 """
2773 try:
Nathan Harold123c9da2015-12-30 16:33:25 -08002774 return rat_generation_from_rat(get_network_rat_for_subscription(
2775 log, ad, sub_id, voice_or_data))
Ang Li73697b32015-12-03 00:41:53 +00002776 except KeyError:
Nathan Harold123c9da2015-12-30 16:33:25 -08002777 log.error(
2778 "KeyError happened in get_network_gen, ad:{}, d/v: {}, rat: {}".format(
2779 ad.serial, voice_or_data, get_network_rat_for_subscription(
2780 log, ad, sub_id, voice_or_data)))
Yang Liue23e5b12015-12-07 17:17:27 -08002781 return GEN_UNKNOWN
Ang Li73697b32015-12-03 00:41:53 +00002782
Nathan Harold123c9da2015-12-30 16:33:25 -08002783
2784def check_voice_mail_count(log, ad, voice_mail_count_before,
2785 voice_mail_count_after):
Ang Li73697b32015-12-03 00:41:53 +00002786 """function to check if voice mail count is correct after leaving a new voice message.
2787 """
2788 return get_voice_mail_count_check_function(get_operator_name(log, ad))(
2789 voice_mail_count_before, voice_mail_count_after)
2790
Nathan Harold123c9da2015-12-30 16:33:25 -08002791
Ang Li73697b32015-12-03 00:41:53 +00002792def get_voice_mail_number(log, ad):
2793 """function to get the voice mail number
2794 """
Nathan Harold123c9da2015-12-30 16:33:25 -08002795 voice_mail_number = get_voice_mail_number_function(get_operator_name(log,
2796 ad))()
Ang Li73697b32015-12-03 00:41:53 +00002797 if voice_mail_number is None:
2798 return get_phone_number(log, ad)
2799 return voice_mail_number
2800
Nathan Harold123c9da2015-12-30 16:33:25 -08002801
2802def ensure_phones_idle(log,
2803 ads,
Ang Li73697b32015-12-03 00:41:53 +00002804 settling_time=WAIT_TIME_ANDROID_STATE_SETTLING):
2805 """Ensure ads idle (not in call).
2806 """
2807 for ad in ads:
2808 if ad.droid.telecomIsInCall():
2809 ad.droid.telecomEndCall()
2810 # Leave the delay time to make sure droid can recover to idle from ongoing call.
2811 time.sleep(settling_time)
2812 return True
2813
Nathan Harold123c9da2015-12-30 16:33:25 -08002814
2815def ensure_phone_idle(log, ad, settling_time=WAIT_TIME_ANDROID_STATE_SETTLING):
Ang Li73697b32015-12-03 00:41:53 +00002816 """Ensure ad idle (not in call).
2817 """
2818 return ensure_phones_idle(log, [ad], settling_time)
2819
Nathan Harold123c9da2015-12-30 16:33:25 -08002820
Ang Li73697b32015-12-03 00:41:53 +00002821def ensure_phone_default_state(log, ad):
2822 """Ensure ad in default state.
2823 Phone not in call.
2824 Phone have no stored WiFi network and WiFi disconnected.
2825 Phone not in airplane mode.
2826 """
2827 result = True
2828 if ad.droid.telecomIsInCall():
2829 ad.droid.telecomEndCall()
2830 set_wfc_mode(log, ad, WFC_MODE_DISABLED)
2831
Nathan Harold123c9da2015-12-30 16:33:25 -08002832 if not wait_for_not_network_rat(log,
2833 ad,
2834 RAT_FAMILY_WLAN,
2835 voice_or_data=NETWORK_SERVICE_DATA):
2836 log.error(
2837 "ensure_phones_default_state: wait_for_droid_not_in iwlan fail {}.".format(
2838 ad.serial))
Ang Li73697b32015-12-03 00:41:53 +00002839 result = False
2840 if ((not WifiUtils.wifi_reset(log, ad)) or
2841 (not WifiUtils.wifi_toggle_state(log, ad, False))):
Nathan Harold123c9da2015-12-30 16:33:25 -08002842 log.error("ensure_phones_default_state:reset WiFi fail {}.".format(
2843 ad.serial))
Ang Li73697b32015-12-03 00:41:53 +00002844 result = False
2845 if not toggle_airplane_mode(log, ad, False):
Nathan Harold123c9da2015-12-30 16:33:25 -08002846 log.error(
2847 "ensure_phones_default_state:turn off airplane mode fail {}.".format(
2848 ad.serial))
Ang Li73697b32015-12-03 00:41:53 +00002849 result = False
2850 # make sure phone data is on
Yang Liuaed3eef2015-12-15 18:40:25 -08002851 ad.droid.telephonyToggleDataConnection(True)
Ang Li73697b32015-12-03 00:41:53 +00002852
2853 # Leave the delay time to make sure droid can recover to idle from ongoing call.
2854 time.sleep(WAIT_TIME_ANDROID_STATE_SETTLING)
2855 return result
2856
Nathan Harold123c9da2015-12-30 16:33:25 -08002857
Ang Li73697b32015-12-03 00:41:53 +00002858def ensure_phones_default_state(log, ads):
2859 """Ensure ads in default state.
2860 Phone not in call.
2861 Phone have no stored WiFi network and WiFi disconnected.
2862 Phone not in airplane mode.
2863 """
2864 tasks = []
2865 for ad in ads:
2866 tasks.append((ensure_phone_default_state, (log, ad)))
2867 if not multithread_func(log, tasks):
2868 log.error("Ensure_phones_default_state Fail.")
2869 return False
2870 return True
2871
Nathan Harold123c9da2015-12-30 16:33:25 -08002872
Ang Li73697b32015-12-03 00:41:53 +00002873def ensure_wifi_connected(log, ad, wifi_ssid, wifi_pwd=None, retry=1):
2874 """Ensure ad connected to wifi.
2875
2876 Args:
2877 log: Log object.
2878 ad: Android device object.
2879 wifi_ssid: WiFi network SSID.
2880 wifi_pwd: WiFi network password. This is optional.
2881
2882 """
Nathan Harold123c9da2015-12-30 16:33:25 -08002883 while (retry >= 0):
Ang Li73697b32015-12-03 00:41:53 +00002884 WifiUtils.wifi_reset(log, ad)
2885 WifiUtils.wifi_toggle_state(log, ad, False)
2886 WifiUtils.wifi_toggle_state(log, ad, True)
2887 if WifiUtils.wifi_connect(log, ad, wifi_ssid, wifi_pwd):
2888 return True
2889 else:
2890 log.info("ensure_wifi_connected: Connect WiFi failed, retry + 1.")
2891 retry -= 1
2892 return False
2893
Nathan Harold123c9da2015-12-30 16:33:25 -08002894
Yang Liu98fd9d72016-03-04 12:14:49 -08002895def reset_preferred_network_type_to_allowable_range(log, ad):
2896 """If preferred network type is not in allowable range, reset to GEN_4G
2897 preferred network type.
2898
2899 Args:
2900 log: log object
2901 ad: android device object
2902
2903 Returns:
2904 None
2905 """
2906 sub_info_list = ad.droid.subscriptionGetAllSubInfoList()
2907 for sub_info in sub_info_list:
2908 sub_id = sub_info['subscriptionId']
2909 operator = get_operator_name(log, ad, sub_id)
2910 current_preference = \
2911 ad.droid.telephonyGetPreferredNetworkTypesForSubscription(sub_id)
2912 if current_preference not in get_allowable_network_preference(operator):
2913 network_preference = network_preference_for_generaton(GEN_4G, operator)
2914 ad.droid.telephonySetPreferredNetworkTypesForSubscription(
2915 network_preference, sub_id)
2916
Ang Li73697b32015-12-03 00:41:53 +00002917def task_wrapper(task):
2918 """Task wrapper for multithread_func
2919
2920 Args:
2921 task[0]: function to be wrapped.
2922 task[1]: function args.
2923
2924 Returns:
2925 Return value of wrapped function call.
2926 """
2927 func = task[0]
2928 params = task[1]
2929 return func(*params)
2930
Nathan Harold123c9da2015-12-30 16:33:25 -08002931
Ang Li73697b32015-12-03 00:41:53 +00002932def multithread_func(log, tasks):
2933 """Multi-thread function wrapper.
2934
2935 Args:
2936 log: log object.
2937 tasks: tasks to be executed in parallel.
2938
2939 Returns:
2940 True if all tasks return True.
2941 False if any task return False.
2942 """
2943 MAX_NUMBER_OF_WORKERS = 4
2944 number_of_workers = min(MAX_NUMBER_OF_WORKERS, len(tasks))
Nathan Harold123c9da2015-12-30 16:33:25 -08002945 executor = concurrent.futures.ThreadPoolExecutor(
2946 max_workers=number_of_workers)
Ang Li73697b32015-12-03 00:41:53 +00002947 results = list(executor.map(task_wrapper, tasks))
2948 executor.shutdown()
2949 log.info("multithread_func result: {}".format(results))
2950 for r in results:
2951 if not r:
2952 return False
2953 return True
2954
Nathan Harold123c9da2015-12-30 16:33:25 -08002955
Ang Li73697b32015-12-03 00:41:53 +00002956def set_phone_screen_on(log, ad, screen_on_time=MAX_SCREEN_ON_TIME):
2957 """Set phone screen on time.
2958
2959 Args:
2960 log: Log object.
2961 ad: Android device object.
2962 screen_on_time: screen on time.
2963 This is optional, default value is MAX_SCREEN_ON_TIME.
2964 Returns:
2965 True if set successfully.
2966 """
2967 ad.droid.setScreenTimeout(screen_on_time)
2968 return screen_on_time == ad.droid.getScreenTimeout()
2969
Nathan Harold123c9da2015-12-30 16:33:25 -08002970
Ang Li73697b32015-12-03 00:41:53 +00002971def set_phone_silent_mode(log, ad, silent_mode=True):
2972 """Set phone silent mode.
2973
2974 Args:
2975 log: Log object.
2976 ad: Android device object.
2977 silent_mode: set phone silent or not.
2978 This is optional, default value is True (silent mode on).
2979 Returns:
2980 True if set successfully.
2981 """
2982 ad.droid.toggleRingerSilentMode(silent_mode)
2983 return silent_mode == ad.droid.checkRingerSilentMode()
2984
Nathan Harold123c9da2015-12-30 16:33:25 -08002985
Ang Li73697b32015-12-03 00:41:53 +00002986def set_preferred_subid_for_sms(log, ad, sub_id):
2987 """set subscription id for SMS
2988
2989 Args:
2990 log: Log object.
2991 ad: Android device object.
2992 sub_id :Subscription ID.
2993
2994 """
Nathan Harold123c9da2015-12-30 16:33:25 -08002995 log.info("Setting subscription:{} as Message SIM for {}".format(sub_id,
2996 ad.serial))
Ang Li73697b32015-12-03 00:41:53 +00002997 ad.droid.subscriptionSetDefaultSmsSubId(sub_id)
2998 # Wait to make sure settings take effect
2999 time.sleep(WAIT_TIME_ANDROID_STATE_SETTLING)
3000 return sub_id == ad.droid.subscriptionGetDefaultSmsSubId()
3001
Nathan Harold123c9da2015-12-30 16:33:25 -08003002
Ang Li73697b32015-12-03 00:41:53 +00003003def set_preferred_subid_for_data(log, ad, sub_id):
3004 """set subscription id for data
3005
3006 Args:
3007 log: Log object.
3008 ad: Android device object.
3009 sub_id :Subscription ID.
3010
3011 """
Nathan Harold123c9da2015-12-30 16:33:25 -08003012 log.info("Setting subscription:{} as Data SIM for {}".format(sub_id,
3013 ad.serial))
Ang Li73697b32015-12-03 00:41:53 +00003014 ad.droid.subscriptionSetDefaultDataSubId(sub_id)
3015 time.sleep(WAIT_TIME_ANDROID_STATE_SETTLING)
3016 # Wait to make sure settings take effect
3017 # Data SIM change takes around 1 min
3018 # Check whether data has changed to selected sim
Yang Liudf164e32016-01-07 16:49:32 -08003019 if not wait_for_data_connection(log, ad, True, MAX_WAIT_TIME_DATA_SUB_CHANGE):
Ang Li73697b32015-12-03 00:41:53 +00003020 log.error("Data Connection failed - Not able to switch Data SIM")
3021 return False
3022 return True
3023
Nathan Harold123c9da2015-12-30 16:33:25 -08003024
Ang Li73697b32015-12-03 00:41:53 +00003025def set_preferred_subid_for_voice(log, ad, sub_id):
3026 """set subscription id for voice
3027
3028 Args:
3029 log: Log object.
3030 ad: Android device object.
3031 sub_id :Subscription ID.
3032
3033 """
Nathan Harold123c9da2015-12-30 16:33:25 -08003034 log.info("Setting subscription:{} as Voice SIM for {}".format(sub_id,
3035 ad.serial))
Ang Li73697b32015-12-03 00:41:53 +00003036 ad.droid.subscriptionSetDefaultVoiceSubId(sub_id)
3037 ad.droid.telecomSetUserSelectedOutgoingPhoneAccountBySubId(sub_id)
3038 # Wait to make sure settings take effect
3039 time.sleep(WAIT_TIME_ANDROID_STATE_SETTLING)
3040 return True
3041
Nathan Harold123c9da2015-12-30 16:33:25 -08003042
Ang Li73697b32015-12-03 00:41:53 +00003043def set_call_state_listen_level(log, ad, value, sub_id):
3044 """Set call state listen level for subscription id.
3045
3046 Args:
3047 log: Log object.
3048 ad: Android device object.
3049 value: True or False
3050 sub_id :Subscription ID.
3051
3052 Returns:
3053 True or False
3054 """
3055 if sub_id == INVALID_SUB_ID:
3056 log.error("Invalid Subscription ID")
3057 return False
Yang Liuaed3eef2015-12-15 18:40:25 -08003058 ad.droid.telephonyAdjustPreciseCallStateListenLevelForSubscription(
Nathan Harold123c9da2015-12-30 16:33:25 -08003059 "Foreground", value, sub_id)
Yang Liuaed3eef2015-12-15 18:40:25 -08003060 ad.droid.telephonyAdjustPreciseCallStateListenLevelForSubscription(
Nathan Harold123c9da2015-12-30 16:33:25 -08003061 "Ringing", value, sub_id)
Yang Liuaed3eef2015-12-15 18:40:25 -08003062 ad.droid.telephonyAdjustPreciseCallStateListenLevelForSubscription(
Nathan Harold123c9da2015-12-30 16:33:25 -08003063 "Background", value, sub_id)
Ang Li73697b32015-12-03 00:41:53 +00003064 return True
3065
Nathan Harold123c9da2015-12-30 16:33:25 -08003066
Ang Li73697b32015-12-03 00:41:53 +00003067def setup_sim(log, ad, sub_id, voice=False, sms=False, data=False):
3068 """set subscription id for voice, sms and data
3069
3070 Args:
3071 log: Log object.
3072 ad: Android device object.
3073 sub_id :Subscription ID.
3074 voice: True if to set subscription as default voice subscription
3075 sms: True if to set subscription as default sms subscription
3076 data: True if to set subscription as default data subscription
3077
3078 """
3079 if sub_id == INVALID_SUB_ID:
3080 log.error("Invalid Subscription ID")
3081 return False
3082 else:
3083 if voice:
3084 if not set_preferred_subid_for_voice(log, ad, sub_id):
3085 return False
3086 if sms:
3087 if not set_preferred_subid_for_sms(log, ad, sub_id):
3088 return False
3089 if data:
Nathan Harold123c9da2015-12-30 16:33:25 -08003090 if not set_preferred_subid_for_data(log, ad, sub_id):
Ang Li73697b32015-12-03 00:41:53 +00003091 return False
3092 return True
3093
Nathan Harold123c9da2015-12-30 16:33:25 -08003094
Ang Li73697b32015-12-03 00:41:53 +00003095def get_sub_ids_for_sim_slots(log, ads):
3096 """get subscription id for each sim slots avaialble in all devices
3097 in ads
3098
3099 Args:
3100 log: Log object.
3101 ads: Android device object list.
3102
3103 Returns:
3104 list of sub ids for all devices.
3105 it is a matrix.
3106 eg: sim_sub_ids[0][0] is sub id for sim1 in device 1
3107 """
3108 sim_sub_ids = []
3109 for index in range(len(ads)):
3110 sim_sub_ids.append([])
Yang Liuaed3eef2015-12-15 18:40:25 -08003111 sim_count = ads[index].droid.telephonyGetSimCount()
Ang Li73697b32015-12-03 00:41:53 +00003112 for count in range(sim_count):
3113 subid = get_subid_from_slot_index(log, ads[index], count)
3114 sim_sub_ids[index].append(subid)
3115 return sim_sub_ids
3116
Nathan Harold123c9da2015-12-30 16:33:25 -08003117
Yang Liu8e6adff2016-02-05 10:24:04 -08003118def is_event_match(event, field, value):
3119 """Return if <field> in "event" match <value> or not.
Ang Li73697b32015-12-03 00:41:53 +00003120
3121 Args:
Yang Liu8e6adff2016-02-05 10:24:04 -08003122 event: event to test. This event need to have <field>.
3123 field: field to match.
3124 value: value to match.
Ang Li73697b32015-12-03 00:41:53 +00003125
3126 Returns:
Yang Liu8e6adff2016-02-05 10:24:04 -08003127 True if <field> in "event" match <value>.
Ang Li73697b32015-12-03 00:41:53 +00003128 False otherwise.
3129 """
Yang Liu8e6adff2016-02-05 10:24:04 -08003130 return is_event_match_for_list(event, field, [value])
Ang Li73697b32015-12-03 00:41:53 +00003131
Nathan Harold123c9da2015-12-30 16:33:25 -08003132
Yang Liu8e6adff2016-02-05 10:24:04 -08003133def is_event_match_for_list(event, field, value_list):
3134 """Return if <field> in "event" match any one of the value
3135 in "value_list" or not.
Ang Li73697b32015-12-03 00:41:53 +00003136
3137 Args:
Yang Liu8e6adff2016-02-05 10:24:04 -08003138 event: event to test. This event need to have <field>.
3139 field: field to match.
3140 value_list: a list of value to match.
Ang Li73697b32015-12-03 00:41:53 +00003141
3142 Returns:
Yang Liu8e6adff2016-02-05 10:24:04 -08003143 True if <field> in "event" match one of the value in "value_list".
Ang Li73697b32015-12-03 00:41:53 +00003144 False otherwise.
3145 """
3146 try:
Yang Liu8e6adff2016-02-05 10:24:04 -08003147 value_in_event = event['data'][field]
Ang Li73697b32015-12-03 00:41:53 +00003148 except KeyError:
3149 return False
Yang Liu8e6adff2016-02-05 10:24:04 -08003150 for value in value_list:
3151 if value_in_event == value:
Ang Li73697b32015-12-03 00:41:53 +00003152 return True
3153 return False
3154
Nathan Harold123c9da2015-12-30 16:33:25 -08003155
Yang Liu8e6adff2016-02-05 10:24:04 -08003156def is_network_call_back_event_match(event, network_callback_id,
3157 network_callback_event):
Ang Li73697b32015-12-03 00:41:53 +00003158 try:
Yang Liu8e6adff2016-02-05 10:24:04 -08003159 return ((network_callback_id == event[
3160 'data'][NetworkCallbackContainer.ID]) and
3161 (network_callback_event == event['data'][
3162 NetworkCallbackContainer.NETWORK_CALLBACK_EVENT]))
Ang Li73697b32015-12-03 00:41:53 +00003163 except KeyError:
3164 return False
3165
Nathan Harold123c9da2015-12-30 16:33:25 -08003166
Ang Li73697b32015-12-03 00:41:53 +00003167def is_build_id(log, ad, build_id):
3168 """Return if ad's build id is the same as input parameter build_id.
3169
3170 Args:
3171 log: log object.
3172 ad: android device object.
3173 build_id: android build id.
3174
3175 Returns:
3176 True if ad's build id is the same as input parameter build_id.
3177 False otherwise.
3178 """
3179 actual_bid = ad.droid.getBuildID()
3180
3181 log.info("{} BUILD DISPLAY: {}"
3182 .format(ad.serial, ad.droid.getBuildDisplay()))
3183 #In case we want to log more stuff/more granularity...
3184 #log.info("{} BUILD ID:{} ".format(ad.serial, ad.droid.getBuildID()))
3185 #log.info("{} BUILD FINGERPRINT: {} "
3186 # .format(ad.serial), ad.droid.getBuildFingerprint())
3187 #log.info("{} BUILD TYPE: {} "
3188 # .format(ad.serial), ad.droid.getBuildType())
3189 #log.info("{} BUILD NUMBER: {} "
3190 # .format(ad.serial), ad.droid.getBuildNumber())
3191 if actual_bid.upper() != build_id.upper():
3192 log.error("{}: Incorrect Build ID".format(ad.model))
3193 return False
3194 return True
3195
Nathan Harold123c9da2015-12-30 16:33:25 -08003196
Ang Li73697b32015-12-03 00:41:53 +00003197def is_uri_equivalent(uri1, uri2):
3198 """Check whether two input uris match or not.
3199
3200 Compare Uris.
3201 If Uris are tel URI, it will only take the digit part
3202 and compare as phone number.
3203 Else, it will just do string compare.
3204
3205 Args:
3206 uri1: 1st uri to be compared.
3207 uri2: 2nd uri to be compared.
3208
3209 Returns:
3210 True if two uris match. Otherwise False.
3211 """
Nathan Harold23683d22015-12-14 16:19:08 -08003212
3213 #If either is None/empty we return false
3214 if not uri1 or not uri2:
3215 return False
3216
3217 try:
3218 if uri1.startswith('tel:') and uri2.startswith('tel:'):
Nathan Harold123c9da2015-12-30 16:33:25 -08003219 uri1_number = ''.join(i
3220 for i in urllib.parse.unquote(uri1)
3221 if i.isdigit())
3222 uri2_number = ''.join(i
3223 for i in urllib.parse.unquote(uri2)
3224 if i.isdigit())
Nathan Harold23683d22015-12-14 16:19:08 -08003225 return check_phone_number_match(uri1_number, uri2_number)
3226 else:
3227 return uri1 == uri2
3228 except AttributeError as e:
3229 return False
Ang Li73697b32015-12-03 00:41:53 +00003230
Nathan Harold123c9da2015-12-30 16:33:25 -08003231
Ang Li73697b32015-12-03 00:41:53 +00003232def get_call_uri(ad, call_id):
3233 """Get call's uri field.
3234
3235 Get Uri for call_id in ad.
3236
3237 Args:
3238 ad: android device object.
3239 call_id: the call id to get Uri from.
3240
3241 Returns:
3242 call's Uri if call is active and have uri field. None otherwise.
3243 """
3244 try:
3245 call_detail = ad.droid.telecomCallGetDetails(call_id)
3246 return call_detail["Handle"]["Uri"]
3247 except:
3248 return None
3249
Yang Liu7ce19982016-03-09 12:20:41 -08003250def set_subid_for_outgoing_call(ad, sub_id):
3251 """Set subId for outgoing voice call
3252
3253 Args:
3254 ad: android device object.
3255 sub_id: subscription id (integer)
3256
3257 Returns:
3258 None
3259 """
3260 ad.droid.telecomSetUserSelectedOutgoingPhoneAccountBySubId(sub_id)
3261
3262def set_subid_for_data(ad, sub_id, time_to_sleep=WAIT_TIME_CHANGE_DATA_SUB_ID):
3263 """Set subId for data
3264
3265 Args:
3266 ad: android device object.
3267 sub_id: subscription id (integer)
3268
3269 Returns:
3270 None
3271 """
3272 # TODO: Need to check onSubscriptionChanged event. b/27843365
3273 if ad.droid.subscriptionGetDefaultDataSubId() != sub_id:
3274 ad.droid.subscriptionSetDefaultDataSubId(sub_id)
3275 time.sleep(time_to_sleep)
3276
3277def set_subid_for_message(ad, sub_id):
3278 """Set subId for outgoing message
3279
3280 Args:
3281 ad: android device object.
3282 sub_id: subscription id (integer)
3283
3284 Returns:
3285 None
3286 """
3287 ad.droid.subscriptionSetDefaultSmsSubId(sub_id)
Nathan Harold123c9da2015-12-30 16:33:25 -08003288
Yang Liu7a2e7ee2015-12-28 15:32:44 -08003289# TODO: b/26294018 Remove wrapper class once wifi_utils methods updated
Ang Li73697b32015-12-03 00:41:53 +00003290class WifiUtils():
3291
tturneyf640a712015-12-30 15:18:15 -08003292 from acts.test_utils.wifi.wifi_test_utils \
3293 import reset_wifi as _reset_wifi
3294 from acts.test_utils.wifi.wifi_test_utils \
Ang Li73697b32015-12-03 00:41:53 +00003295 import wifi_connect as _wifi_connect
tturneyf640a712015-12-30 15:18:15 -08003296 from acts.test_utils.wifi.wifi_test_utils \
Ang Li73697b32015-12-03 00:41:53 +00003297 import wifi_toggle_state as _wifi_toggle_state
tturneyf640a712015-12-30 15:18:15 -08003298 from acts.test_utils.wifi.wifi_test_utils \
Ang Li73697b32015-12-03 00:41:53 +00003299 import start_wifi_tethering as _start_wifi_tethering
tturneyf640a712015-12-30 15:18:15 -08003300 from acts.test_utils.wifi.wifi_test_utils \
Ang Li73697b32015-12-03 00:41:53 +00003301 import stop_wifi_tethering as _stop_wifi_tethering
tturneyf640a712015-12-30 15:18:15 -08003302 from acts.test_utils.wifi.wifi_test_utils \
Ang Li73697b32015-12-03 00:41:53 +00003303 import WifiEnums as _WifiEnums
3304
3305 WIFI_CONFIG_APBAND_2G = _WifiEnums.WIFI_CONFIG_APBAND_2G
3306 WIFI_CONFIG_APBAND_5G = _WifiEnums.WIFI_CONFIG_APBAND_5G
3307 SSID_KEY = _WifiEnums.SSID_KEY
Yang Liu2bf30892015-12-17 09:53:45 -08003308 PWD_KEY = _WifiEnums.PWD_KEY
Ang Li73697b32015-12-03 00:41:53 +00003309
3310 @staticmethod
3311 def wifi_toggle_state(log, ad, state):
3312 try:
Shabeerali Karattu Parambath8a8061b2015-12-10 12:17:23 -08003313 WifiUtils._wifi_toggle_state(ad, state)
Ang Li73697b32015-12-03 00:41:53 +00003314 except Exception as e:
3315 log.error("WifiUtils.wifi_toggle_state exception: {}".format(e))
3316 return False
3317 return True
3318
3319 @staticmethod
3320 def wifi_reset(log, ad, disable_wifi=True):
3321 try:
Shabeerali Karattu Parambath8a8061b2015-12-10 12:17:23 -08003322 WifiUtils._reset_wifi(ad)
Ang Li73697b32015-12-03 00:41:53 +00003323 except Exception as e:
3324 log.error("WifiUtils.wifi_reset exception: {}".format(e))
3325 return False
3326 finally:
3327 if disable_wifi is True:
3328 ad.droid.wifiToggleState(False)
3329 # Ensure toggle state has human-time to take effect
3330 time.sleep(WAIT_TIME_ANDROID_STATE_SETTLING)
3331 return True
3332
3333 @staticmethod
3334 def wifi_connect(log, ad, ssid, password=None):
3335 if password == "":
3336 password = None
3337 try:
Yang Liu2bf30892015-12-17 09:53:45 -08003338 network = {WifiUtils.SSID_KEY: ssid}
3339 if password:
3340 network[WifiUtils.PWD_KEY] = password
3341 WifiUtils._wifi_connect(ad, network)
Ang Li73697b32015-12-03 00:41:53 +00003342 except Empty:
3343 # did not get event, then check connection info
3344 try:
Nathan Harold123c9da2015-12-30 16:33:25 -08003345 if ad.droid.wifiGetConnectionInfo()[
3346 WifiUtils.SSID_KEY] == ssid:
Ang Li73697b32015-12-03 00:41:53 +00003347 return True
3348 else:
Nathan Harold123c9da2015-12-30 16:33:25 -08003349 log.error(
3350 "WifiUtils.wifi_connect not connected."
3351 "No event received. Expected SSID: {}, current SSID:{}".format(
3352 ssid, ad.droid.wifiGetConnectionInfo()[
3353 WifiUtils.SSID_KEY]))
Ang Li73697b32015-12-03 00:41:53 +00003354 return False
3355 except Exception as e:
3356 log.error("WifiUtils.wifi_connect not connected, no event.")
3357 return False
3358 except Exception as e:
3359 log.error("WifiUtils.wifi_connect exception: {}".format(e))
3360 return False
Yang Liu2bf30892015-12-17 09:53:45 -08003361 return True
Ang Li73697b32015-12-03 00:41:53 +00003362
3363 @staticmethod
3364 def start_wifi_tethering(log, ad, ssid, password, ap_band=None):
3365 try:
3366 return WifiUtils._start_wifi_tethering(ad, ssid, password, ap_band)
3367 except Exception as e:
3368 log.error("WifiUtils.start_wifi_tethering exception: {}".format(e))
3369 return False
3370
3371 @staticmethod
3372 def stop_wifi_tethering(log, ad):
3373 try:
3374 WifiUtils._stop_wifi_tethering(ad)
3375 return True
3376 except Exception as e:
3377 log.error("WifiUtils.stop_wifi_tethering exception: {}".format(e))
3378 return False