Alexander Dorokhine | 7abf60e | 2016-02-10 10:56:09 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python3.4 |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2 | # |
Alexander Dorokhine | 7abf60e | 2016-02-10 10:56:09 -0800 | [diff] [blame] | 3 | # Copyright 2016 - Google |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 4 | # |
| 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 Dorokhine | 642e63f | 2016-02-11 13:32:37 -0800 | [diff] [blame] | 17 | from future import standard_library |
| 18 | standard_library.install_aliases() |
| 19 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 20 | import concurrent.futures |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 21 | import urllib.parse |
| 22 | import time |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 23 | |
| 24 | from queue import Empty |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 25 | from acts.controllers.android_device import AndroidDevice |
Yang Liu | 46ed722 | 2015-12-28 14:08:52 -0800 | [diff] [blame] | 26 | from acts.controllers.event_dispatcher import EventDispatcher |
Yang Liu | 9a9a4f7 | 2016-02-19 10:45:04 -0800 | [diff] [blame] | 27 | from acts.test_utils.tel.tel_defines import AOSP_PREFIX |
Yang Liu | 46ed722 | 2015-12-28 14:08:52 -0800 | [diff] [blame] | 28 | from acts.test_utils.tel.tel_defines import CARRIER_UNKNOWN |
| 29 | from acts.test_utils.tel.tel_defines import DATA_STATE_CONNECTED |
| 30 | from acts.test_utils.tel.tel_defines import DATA_STATE_DISCONNECTED |
Yang Liu | 98fd9d7 | 2016-03-04 12:14:49 -0800 | [diff] [blame] | 31 | from acts.test_utils.tel.tel_defines import GEN_4G |
Yang Liu | 46ed722 | 2015-12-28 14:08:52 -0800 | [diff] [blame] | 32 | from acts.test_utils.tel.tel_defines import GEN_UNKNOWN |
| 33 | from acts.test_utils.tel.tel_defines import INCALL_UI_DISPLAY_BACKGROUND |
| 34 | from acts.test_utils.tel.tel_defines import INCALL_UI_DISPLAY_FOREGROUND |
| 35 | from acts.test_utils.tel.tel_defines import INVALID_SIM_SLOT_INDEX |
| 36 | from acts.test_utils.tel.tel_defines import INVALID_SUB_ID |
| 37 | from acts.test_utils.tel.tel_defines import MAX_SAVED_VOICE_MAIL |
| 38 | from acts.test_utils.tel.tel_defines import MAX_SCREEN_ON_TIME |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 39 | from acts.test_utils.tel.tel_defines import \ |
| 40 | MAX_WAIT_TIME_ACCEPT_CALL_TO_OFFHOOK_EVENT |
| 41 | from acts.test_utils.tel.tel_defines import MAX_WAIT_TIME_AIRPLANEMODE_EVENT |
| 42 | from acts.test_utils.tel.tel_defines import MAX_WAIT_TIME_CALL_INITIATION |
| 43 | from acts.test_utils.tel.tel_defines import MAX_WAIT_TIME_CALLEE_RINGING |
| 44 | from acts.test_utils.tel.tel_defines import MAX_WAIT_TIME_CONNECTION_STATE_UPDATE |
| 45 | from acts.test_utils.tel.tel_defines import MAX_WAIT_TIME_DATA_SUB_CHANGE |
| 46 | from acts.test_utils.tel.tel_defines import MAX_WAIT_TIME_HANGUP_TO_IDLE_EVENT |
| 47 | from acts.test_utils.tel.tel_defines import MAX_WAIT_TIME_NW_SELECTION |
| 48 | from acts.test_utils.tel.tel_defines import MAX_WAIT_TIME_SMS_RECEIVE |
| 49 | from acts.test_utils.tel.tel_defines import MAX_WAIT_TIME_SMS_SENT_SUCCESS |
Yang Liu | 855d5f8 | 2016-01-27 15:35:48 -0800 | [diff] [blame] | 50 | from acts.test_utils.tel.tel_defines import MAX_WAIT_TIME_TELECOM_RINGING |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 51 | from acts.test_utils.tel.tel_defines import MAX_WAIT_TIME_VOICE_MAIL_COUNT |
Yang Liu | dfc37b6 | 2016-01-20 18:08:47 -0800 | [diff] [blame] | 52 | from acts.test_utils.tel.tel_defines import MAX_WAIT_TIME_WFC_DISABLED |
| 53 | from acts.test_utils.tel.tel_defines import MAX_WAIT_TIME_WFC_ENABLED |
Yang Liu | e51e93d | 2015-12-30 10:45:42 -0800 | [diff] [blame] | 54 | from acts.test_utils.tel.tel_defines import NETWORK_MODE_LTE_ONLY |
Yang Liu | 46ed722 | 2015-12-28 14:08:52 -0800 | [diff] [blame] | 55 | from acts.test_utils.tel.tel_defines import NETWORK_CONNECTION_TYPE_CELL |
| 56 | from acts.test_utils.tel.tel_defines import NETWORK_CONNECTION_TYPE_WIFI |
| 57 | from acts.test_utils.tel.tel_defines import NETWORK_SERVICE_DATA |
| 58 | from acts.test_utils.tel.tel_defines import NETWORK_SERVICE_VOICE |
| 59 | from acts.test_utils.tel.tel_defines import PHONE_NUMBER_STRING_FORMAT_7_DIGIT |
| 60 | from acts.test_utils.tel.tel_defines import PHONE_NUMBER_STRING_FORMAT_10_DIGIT |
| 61 | from acts.test_utils.tel.tel_defines import PHONE_NUMBER_STRING_FORMAT_11_DIGIT |
| 62 | from acts.test_utils.tel.tel_defines import PHONE_NUMBER_STRING_FORMAT_12_DIGIT |
| 63 | from acts.test_utils.tel.tel_defines import RAT_FAMILY_GSM |
| 64 | from acts.test_utils.tel.tel_defines import RAT_FAMILY_LTE |
| 65 | from acts.test_utils.tel.tel_defines import RAT_FAMILY_WLAN |
Yang Liu | cde45aa | 2016-02-22 13:46:41 -0800 | [diff] [blame] | 66 | from acts.test_utils.tel.tel_defines import RAT_FAMILY_WCDMA |
Yang Liu | 46ed722 | 2015-12-28 14:08:52 -0800 | [diff] [blame] | 67 | from acts.test_utils.tel.tel_defines import RAT_1XRTT |
| 68 | from acts.test_utils.tel.tel_defines import RAT_UNKNOWN |
| 69 | from acts.test_utils.tel.tel_defines import SERVICE_STATE_EMERGENCY_ONLY |
| 70 | from acts.test_utils.tel.tel_defines import SERVICE_STATE_IN_SERVICE |
| 71 | from acts.test_utils.tel.tel_defines import SERVICE_STATE_OUT_OF_SERVICE |
| 72 | from acts.test_utils.tel.tel_defines import SERVICE_STATE_POWER_OFF |
| 73 | from acts.test_utils.tel.tel_defines import SIM_STATE_READY |
| 74 | from acts.test_utils.tel.tel_defines import TELEPHONY_STATE_IDLE |
| 75 | from acts.test_utils.tel.tel_defines import TELEPHONY_STATE_OFFHOOK |
| 76 | from acts.test_utils.tel.tel_defines import TELEPHONY_STATE_RINGING |
| 77 | from acts.test_utils.tel.tel_defines import VOICEMAIL_DELETE_DIGIT |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 78 | from acts.test_utils.tel.tel_defines import WAIT_TIME_1XRTT_VOICE_ATTACH |
Yang Liu | 46ed722 | 2015-12-28 14:08:52 -0800 | [diff] [blame] | 79 | from acts.test_utils.tel.tel_defines import WAIT_TIME_ANDROID_STATE_SETTLING |
Yang Liu | 46ed722 | 2015-12-28 14:08:52 -0800 | [diff] [blame] | 80 | from acts.test_utils.tel.tel_defines import WAIT_TIME_IN_CALL |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 81 | from acts.test_utils.tel.tel_defines import WAIT_TIME_LEAVE_VOICE_MAIL |
Yang Liu | 46ed722 | 2015-12-28 14:08:52 -0800 | [diff] [blame] | 82 | from acts.test_utils.tel.tel_defines import WAIT_TIME_REJECT_CALL |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 83 | from acts.test_utils.tel.tel_defines import WAIT_TIME_VOICE_MAIL_SERVER_RESPONSE |
Yang Liu | 46ed722 | 2015-12-28 14:08:52 -0800 | [diff] [blame] | 84 | from acts.test_utils.tel.tel_defines import WFC_MODE_DISABLED |
| 85 | from acts.test_utils.tel.tel_defines import EventCallStateChanged |
| 86 | from acts.test_utils.tel.tel_defines import EventConnectivityChanged |
| 87 | from acts.test_utils.tel.tel_defines import EventDataConnectionStateChanged |
| 88 | from acts.test_utils.tel.tel_defines import EventDataSmsReceived |
| 89 | from acts.test_utils.tel.tel_defines import EventMessageWaitingIndicatorChanged |
| 90 | from acts.test_utils.tel.tel_defines import EventServiceStateChanged |
| 91 | from acts.test_utils.tel.tel_defines import EventMmsSentSuccess |
| 92 | from acts.test_utils.tel.tel_defines import EventSmsReceived |
| 93 | from acts.test_utils.tel.tel_defines import EventSmsSentSuccess |
Yang Liu | dc8564b | 2016-01-27 14:15:37 -0800 | [diff] [blame] | 94 | from acts.test_utils.tel.tel_defines import CallStateContainer |
Yang Liu | 8e6adff | 2016-02-05 10:24:04 -0800 | [diff] [blame] | 95 | from acts.test_utils.tel.tel_defines import DataConnectionStateContainer |
Yang Liu | dc8564b | 2016-01-27 14:15:37 -0800 | [diff] [blame] | 96 | from acts.test_utils.tel.tel_defines import MessageWaitingIndicatorContainer |
Yang Liu | 8e6adff | 2016-02-05 10:24:04 -0800 | [diff] [blame] | 97 | from acts.test_utils.tel.tel_defines import NetworkCallbackContainer |
| 98 | from acts.test_utils.tel.tel_defines import ServiceStateContainer |
Yang Liu | 46ed722 | 2015-12-28 14:08:52 -0800 | [diff] [blame] | 99 | from acts.test_utils.tel.tel_lookup_tables import \ |
| 100 | connection_type_from_type_string |
| 101 | from acts.test_utils.tel.tel_lookup_tables import is_valid_rat |
Yang Liu | 98fd9d7 | 2016-03-04 12:14:49 -0800 | [diff] [blame] | 102 | from acts.test_utils.tel.tel_lookup_tables import get_allowable_network_preference |
Yang Liu | 46ed722 | 2015-12-28 14:08:52 -0800 | [diff] [blame] | 103 | from acts.test_utils.tel.tel_lookup_tables import \ |
| 104 | get_voice_mail_count_check_function |
| 105 | from acts.test_utils.tel.tel_lookup_tables import get_voice_mail_number_function |
| 106 | from acts.test_utils.tel.tel_lookup_tables import \ |
| 107 | network_preference_for_generaton |
| 108 | from acts.test_utils.tel.tel_lookup_tables import operator_name_from_plmn_id |
| 109 | from acts.test_utils.tel.tel_lookup_tables import \ |
| 110 | rat_families_for_network_preference |
| 111 | from acts.test_utils.tel.tel_lookup_tables import rat_family_for_generation |
Yang Liu | 7a2e7ee | 2015-12-28 15:32:44 -0800 | [diff] [blame] | 112 | from acts.test_utils.tel.tel_lookup_tables import rat_family_from_rat |
| 113 | from acts.test_utils.tel.tel_lookup_tables import rat_generation_from_rat |
Yang Liu | 46ed722 | 2015-12-28 14:08:52 -0800 | [diff] [blame] | 114 | from acts.utils import load_config |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 115 | from acts.logger import LoggerProxy |
| 116 | log = LoggerProxy() |
| 117 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 118 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 119 | class TelTestUtilsError(Exception): |
| 120 | pass |
| 121 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 122 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 123 | def 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 145 | sim_serial = ad.droid.telephonyGetSimSerialNumberForSubscription( |
| 146 | sub_id) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 147 | 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 155 | number = ad.droid.telephonyGetLine1NumberForSubscription( |
| 156 | sub_id) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 157 | 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 165 | 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 Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 171 | |
| 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 Liu | 6c70e02 | 2016-03-07 16:19:42 -0800 | [diff] [blame] | 177 | def 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 Liu | ad9c63d | 2016-02-25 13:59:23 -0800 | [diff] [blame] | 181 | |
| 182 | Args: |
| 183 | log: log object |
| 184 | ad: android device object |
Yang Liu | ad9c63d | 2016-02-25 13:59:23 -0800 | [diff] [blame] | 185 | |
| 186 | Returns: |
Yang Liu | 6c70e02 | 2016-03-07 16:19:42 -0800 | [diff] [blame] | 187 | None |
Yang Liu | ad9c63d | 2016-02-25 13:59:23 -0800 | [diff] [blame] | 188 | """ |
Yang Liu | 6c70e02 | 2016-03-07 16:19:42 -0800 | [diff] [blame] | 189 | 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 198 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 199 | def 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 Liu | 7ce1998 | 2016-03-09 12:20:41 -0800 | [diff] [blame] | 214 | def get_slot_index_from_subid(log, ad, sub_id): |
| 215 | try: |
| 216 | info = ad.droid.subscriptionGetSubInfoForSubscriber(sub_id) |
| 217 | return info['simSlotIndex'] |
tturney | 10eda2e | 2016-03-30 19:55:57 -0700 | [diff] [blame^] | 218 | except KeyError: |
Yang Liu | 7ce1998 | 2016-03-09 12:20:41 -0800 | [diff] [blame] | 219 | return INVALID_SIM_SLOT_INDEX |
| 220 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 221 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 222 | def 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 243 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 244 | def 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 257 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 258 | def 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 272 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 273 | def 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 Liu | aed3eef | 2015-12-15 18:40:25 -0800 | [diff] [blame] | 285 | status = ad.droid.telephonyGetSimState() |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 286 | else: |
Yang Liu | aed3eef | 2015-12-15 18:40:25 -0800 | [diff] [blame] | 287 | status = ad.droid.telephonyGetSimStateForSlotId(sim_slot_id) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 288 | if status != SIM_STATE_READY: |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 289 | log.info("Sim not ready") |
| 290 | return False |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 291 | return True |
| 292 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 293 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 294 | def _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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 304 | return True |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 305 | return False |
| 306 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 307 | |
| 308 | def _set_event_list(event_recv_list, sub_id_list, sub_id, value): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 309 | """ 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 323 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 324 | def 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 341 | for info in active_sub_info: |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 342 | 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 347 | serial_number)) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 348 | 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 Liu | 8e6adff | 2016-02-05 10:24:04 -0800 | [diff] [blame] | 355 | service_state_list = [] |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 356 | if new_state: |
Yang Liu | 8e6adff | 2016-02-05 10:24:04 -0800 | [diff] [blame] | 357 | service_state_list.append(SERVICE_STATE_POWER_OFF) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 358 | 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 Liu | 8e6adff | 2016-02-05 10:24:04 -0800 | [diff] [blame] | 363 | service_state_list.append(SERVICE_STATE_IN_SERVICE) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 364 | # NO SIM, or Dead SIM, or no Roaming coverage. |
Yang Liu | 8e6adff | 2016-02-05 10:24:04 -0800 | [diff] [blame] | 365 | service_state_list.append(SERVICE_STATE_OUT_OF_SERVICE) |
| 366 | service_state_list.append(SERVICE_STATE_EMERGENCY_ONLY) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 367 | log.info("Turn off airplane mode: " + serial_number) |
| 368 | |
| 369 | for sub_id in sub_id_list: |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 370 | ad.droid.telephonyStartTrackingServiceStateChangeForSubscription( |
| 371 | sub_id) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 372 | ad.droid.connectivityToggleAirplaneMode(new_state) |
| 373 | |
| 374 | event = None |
| 375 | |
| 376 | try: |
| 377 | try: |
| 378 | event = ad.ed.wait_for_event(EventServiceStateChanged, |
Yang Liu | 8e6adff | 2016-02-05 10:24:04 -0800 | [diff] [blame] | 379 | is_event_match_for_list, |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 380 | timeout=MAX_WAIT_TIME_AIRPLANEMODE_EVENT, |
Yang Liu | 8e6adff | 2016-02-05 10:24:04 -0800 | [diff] [blame] | 381 | field=ServiceStateContainer.SERVICE_STATE, |
| 382 | value_list=service_state_list) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 383 | except Empty: |
| 384 | pass |
| 385 | if event is None: |
Yang Liu | 8e6adff | 2016-02-05 10:24:04 -0800 | [diff] [blame] | 386 | log.error("Did not get expected service state {}".format( |
| 387 | service_state_list)) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 388 | log.info("Received event: {}".format(event)) |
| 389 | finally: |
| 390 | for sub_id in sub_id_list: |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 391 | ad.droid.telephonyStopTrackingServiceStateChangeForSubscription( |
| 392 | sub_id) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 393 | |
| 394 | if new_state: |
| 395 | if (not ad.droid.connectivityCheckAirplaneMode() or |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 396 | ad.droid.wifiCheckState() or ad.droid.bluetoothCheckState()): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 397 | 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 405 | |
| 406 | def wait_and_answer_call(log, |
| 407 | ad, |
| 408 | incoming_number=None, |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 409 | 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 Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 417 | 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 428 | log, ad, ad.droid.subscriptionGetDefaultVoiceSubId(), incoming_number, |
Yang Liu | 855d5f8 | 2016-01-27 15:35:48 -0800 | [diff] [blame] | 429 | incall_ui_display) |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 430 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 431 | |
| 432 | def 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 452 | event_ringing = ad.ed.wait_for_event( |
| 453 | EventCallStateChanged, |
Yang Liu | 8e6adff | 2016-02-05 10:24:04 -0800 | [diff] [blame] | 454 | is_event_match, |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 455 | timeout=event_iter_timeout, |
Yang Liu | 8e6adff | 2016-02-05 10:24:04 -0800 | [diff] [blame] | 456 | field=CallStateContainer.CALL_STATE, |
| 457 | value=TELEPHONY_STATE_RINGING) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 458 | 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 473 | |
| 474 | def wait_and_answer_call_for_subscription( |
| 475 | log, |
| 476 | ad, |
| 477 | sub_id, |
| 478 | incoming_number=None, |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 479 | incall_ui_display=INCALL_UI_DISPLAY_FOREGROUND): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 480 | """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 Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 488 | 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 Liu | aed3eef | 2015-12-15 18:40:25 -0800 | [diff] [blame] | 499 | ad.droid.telephonyStartTrackingCallStateForSubscription(sub_id) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 500 | if (not ad.droid.telecomIsRinging() and |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 501 | ad.droid.telephonyGetCallStateForSubscription(sub_id) != |
| 502 | TELEPHONY_STATE_RINGING): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 503 | try: |
| 504 | event_ringing = wait_for_ringing_event(log, ad, |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 505 | MAX_WAIT_TIME_CALLEE_RINGING) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 506 | if event_ringing is None: |
| 507 | log.error("No Ringing Event.") |
| 508 | return False |
| 509 | finally: |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 510 | ad.droid.telephonyStopTrackingCallStateChangeForSubscription( |
| 511 | sub_id) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 512 | |
| 513 | if not incoming_number: |
| 514 | result = True |
| 515 | else: |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 516 | result = check_phone_number_match( |
Yang Liu | dc8564b | 2016-01-27 14:15:37 -0800 | [diff] [blame] | 517 | event_ringing['data'][CallStateContainer.INCOMING_NUMBER], |
| 518 | incoming_number) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 519 | |
| 520 | if not result: |
| 521 | log.error("Incoming Number not match") |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 522 | log.error("Expected number:{}, actual number:{}".format( |
Yang Liu | dc8564b | 2016-01-27 14:15:37 -0800 | [diff] [blame] | 523 | incoming_number, |
| 524 | event_ringing['data'][CallStateContainer.INCOMING_NUMBER])) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 525 | return False |
| 526 | |
| 527 | ad.ed.clear_all_events() |
Yang Liu | aed3eef | 2015-12-15 18:40:25 -0800 | [diff] [blame] | 528 | ad.droid.telephonyStartTrackingCallStateForSubscription(sub_id) |
Yang Liu | 855d5f8 | 2016-01-27 15:35:48 -0800 | [diff] [blame] | 529 | if not wait_for_telecom_ringing(log, ad, MAX_WAIT_TIME_TELECOM_RINGING): |
| 530 | log.error("Telecom is not ringing.") |
| 531 | return False |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 532 | log.info("Accept on callee.") |
| 533 | ad.droid.telecomAcceptRingingCall() |
| 534 | try: |
| 535 | ad.ed.wait_for_event(EventCallStateChanged, |
Yang Liu | 8e6adff | 2016-02-05 10:24:04 -0800 | [diff] [blame] | 536 | is_event_match, |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 537 | timeout=MAX_WAIT_TIME_ACCEPT_CALL_TO_OFFHOOK_EVENT, |
Yang Liu | 8e6adff | 2016-02-05 10:24:04 -0800 | [diff] [blame] | 538 | field=CallStateContainer.CALL_STATE, |
| 539 | value=TELEPHONY_STATE_OFFHOOK) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 540 | except Empty: |
| 541 | if not ad.droid.telecomIsInCall(): |
| 542 | log.error("Accept call failed.") |
| 543 | return False |
| 544 | finally: |
Yang Liu | aed3eef | 2015-12-15 18:40:25 -0800 | [diff] [blame] | 545 | ad.droid.telephonyStopTrackingCallStateChangeForSubscription(sub_id) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 546 | 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 552 | |
| 553 | def wait_and_reject_call(log, |
| 554 | ad, |
| 555 | incoming_number=None, |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 556 | 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 572 | log, ad, ad.droid.subscriptionGetDefaultVoiceSubId(), incoming_number, |
| 573 | delay_reject) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 574 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 575 | |
| 576 | def wait_and_reject_call_for_subscription(log, |
| 577 | ad, |
| 578 | sub_id, |
| 579 | incoming_number=None, |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 580 | 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 Liu | aed3eef | 2015-12-15 18:40:25 -0800 | [diff] [blame] | 597 | ad.droid.telephonyStartTrackingCallStateForSubscription(sub_id) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 598 | if (not ad.droid.telecomIsRinging() and |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 599 | ad.droid.telephonyGetCallStateForSubscription(sub_id) != |
| 600 | TELEPHONY_STATE_RINGING): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 601 | try: |
| 602 | event_ringing = wait_for_ringing_event(log, ad, |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 603 | MAX_WAIT_TIME_CALLEE_RINGING) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 604 | if event_ringing is None: |
| 605 | log.error("No Ringing Event.") |
| 606 | return False |
| 607 | finally: |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 608 | ad.droid.telephonyStopTrackingCallStateChangeForSubscription( |
| 609 | sub_id) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 610 | |
| 611 | if not incoming_number: |
| 612 | result = True |
| 613 | else: |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 614 | result = check_phone_number_match( |
Yang Liu | dc8564b | 2016-01-27 14:15:37 -0800 | [diff] [blame] | 615 | event_ringing['data'][CallStateContainer.INCOMING_NUMBER], |
| 616 | incoming_number) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 617 | |
| 618 | if not result: |
| 619 | log.error("Incoming Number not match") |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 620 | log.error("Expected number:{}, actual number:{}".format( |
Yang Liu | dc8564b | 2016-01-27 14:15:37 -0800 | [diff] [blame] | 621 | incoming_number, |
| 622 | event_ringing['data'][CallStateContainer.INCOMING_NUMBER])) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 623 | return False |
| 624 | |
| 625 | ad.ed.clear_all_events() |
Yang Liu | aed3eef | 2015-12-15 18:40:25 -0800 | [diff] [blame] | 626 | ad.droid.telephonyStartTrackingCallStateForSubscription(sub_id) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 627 | # 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 Liu | 8e6adff | 2016-02-05 10:24:04 -0800 | [diff] [blame] | 634 | is_event_match, |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 635 | timeout=MAX_WAIT_TIME_HANGUP_TO_IDLE_EVENT, |
Yang Liu | 8e6adff | 2016-02-05 10:24:04 -0800 | [diff] [blame] | 636 | field=CallStateContainer.CALL_STATE, |
| 637 | value=TELEPHONY_STATE_IDLE) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 638 | except Empty: |
| 639 | log.error("No onCallStateChangedIdle event received.") |
| 640 | return False |
| 641 | finally: |
Yang Liu | aed3eef | 2015-12-15 18:40:25 -0800 | [diff] [blame] | 642 | ad.droid.telephonyStopTrackingCallStateChangeForSubscription(sub_id) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 643 | return True |
| 644 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 645 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 646 | def hangup_call(log, ad): |
| 647 | """Hang up ongoing active call. |
| 648 | """ |
| 649 | ad.ed.clear_all_events() |
Yang Liu | aed3eef | 2015-12-15 18:40:25 -0800 | [diff] [blame] | 650 | ad.droid.telephonyStartTrackingCallState() |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 651 | log.info("Hangup call.") |
| 652 | ad.droid.telecomEndCall() |
| 653 | |
| 654 | try: |
| 655 | ad.ed.wait_for_event(EventCallStateChanged, |
Yang Liu | 8e6adff | 2016-02-05 10:24:04 -0800 | [diff] [blame] | 656 | is_event_match, |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 657 | timeout=MAX_WAIT_TIME_HANGUP_TO_IDLE_EVENT, |
Yang Liu | 8e6adff | 2016-02-05 10:24:04 -0800 | [diff] [blame] | 658 | field=CallStateContainer.CALL_STATE, |
| 659 | value=TELEPHONY_STATE_IDLE) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 660 | except Empty: |
| 661 | if ad.droid.telecomIsInCall(): |
| 662 | log.error("Hangup call failed.") |
| 663 | return False |
| 664 | finally: |
Yang Liu | aed3eef | 2015-12-15 18:40:25 -0800 | [diff] [blame] | 665 | ad.droid.telephonyStopTrackingCallStateChange() |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 666 | return True |
| 667 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 668 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 669 | def 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 675 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 676 | def 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 | |
| 712 | def 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 Liu | aed3eef | 2015-12-15 18:40:25 -0800 | [diff] [blame] | 726 | ad_caller.droid.telephonyStartTrackingCallStateForSubscription(sub_id) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 727 | |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 728 | wait_time_for_incall_state = MAX_WAIT_TIME_CALL_INITIATION |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 729 | |
| 730 | try: |
| 731 | # Make a Call |
| 732 | if emergency: |
Yang Liu | aed3eef | 2015-12-15 18:40:25 -0800 | [diff] [blame] | 733 | ad_caller.droid.telecomCallEmergencyNumber(callee_number) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 734 | else: |
Yang Liu | aed3eef | 2015-12-15 18:40:25 -0800 | [diff] [blame] | 735 | ad_caller.droid.telecomCallNumber(callee_number) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 736 | |
| 737 | # Verify OFFHOOK event |
Yang Liu | aed3eef | 2015-12-15 18:40:25 -0800 | [diff] [blame] | 738 | if ad_caller.droid.telephonyGetCallState() != TELEPHONY_STATE_OFFHOOK: |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 739 | event_offhook = ad_caller.ed.wait_for_event( |
| 740 | EventCallStateChanged, |
Yang Liu | 8e6adff | 2016-02-05 10:24:04 -0800 | [diff] [blame] | 741 | is_event_match, |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 742 | timeout=wait_time_for_incall_state, |
Yang Liu | 8e6adff | 2016-02-05 10:24:04 -0800 | [diff] [blame] | 743 | field=CallStateContainer.CALL_STATE, |
| 744 | value=TELEPHONY_STATE_OFFHOOK) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 745 | except Empty: |
| 746 | log.error("initiate_call did not receive Telephony OFFHOOK event.") |
| 747 | return False |
| 748 | finally: |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 749 | ad_caller.droid.telephonyStopTrackingCallStateChangeForSubscription( |
| 750 | sub_id) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 751 | |
| 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 756 | (ad_caller.droid.telephonyGetCallState() == |
| 757 | TELEPHONY_STATE_OFFHOOK) and |
| 758 | (ad_caller.droid.telecomGetCallState() == |
| 759 | TELEPHONY_STATE_OFFHOOK)): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 760 | return True |
| 761 | time.sleep(1) |
| 762 | log.error("Make call fail. telecomIsInCall:{}, Telecom State:{}," |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 763 | " Telephony State:{}".format(ad_caller.droid.telecomIsInCall( |
| 764 | ), ad_caller.droid.telephonyGetCallState( |
| 765 | ), ad_caller.droid.telecomGetCallState())) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 766 | return False |
| 767 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 768 | |
| 769 | def call_reject_leave_message(log, |
| 770 | ad_caller, |
| 771 | ad_callee, |
| 772 | verify_caller_func=None, |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 773 | wait_time_in_call=WAIT_TIME_LEAVE_VOICE_MAIL): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 774 | """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 Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 788 | This is optional, default is WAIT_TIME_LEAVE_VOICE_MAIL |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 789 | |
| 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 796 | 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 Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 799 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 800 | |
| 801 | def 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 Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 808 | wait_time_in_call=WAIT_TIME_LEAVE_VOICE_MAIL): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 809 | """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 Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 825 | This is optional, default is WAIT_TIME_LEAVE_VOICE_MAIL |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 826 | |
| 827 | Returns: |
| 828 | True: if voice message is received on callee successfully. |
| 829 | False: for errors |
| 830 | """ |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 831 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 832 | 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 849 | log, |
| 850 | ad_callee, |
| 851 | subid_callee, |
| 852 | incoming_number=caller_number): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 853 | raise _CallSequenceException("Reject call fail.") |
| 854 | |
Yang Liu | aed3eef | 2015-12-15 18:40:25 -0800 | [diff] [blame] | 855 | ad_callee.droid.telephonyStartTrackingVoiceMailStateChangeForSubscription( |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 856 | subid_callee) |
Yang Liu | aed3eef | 2015-12-15 18:40:25 -0800 | [diff] [blame] | 857 | voice_mail_count_before = ad_callee.droid.telephonyGetVoiceMailCountForSubscription( |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 858 | 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 870 | raise _CallSequenceException("Caller not in correct state!") |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 871 | |
Yang Liu | 7a2e7ee | 2015-12-28 15:32:44 -0800 | [diff] [blame] | 872 | # TODO: b/26293512 Need to play some sound to leave message. |
| 873 | # Otherwise carrier voice mail server may drop this voice mail. |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 874 | |
| 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 883 | "Caller not in correct state after {} seconds".format( |
| 884 | wait_time_in_call)) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 885 | |
| 886 | if not hangup_call(log, ad_caller): |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 887 | raise _CallSequenceException("Error in Hanging-Up Call") |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 888 | |
| 889 | log.info("Wait for voice mail indicator on callee.") |
| 890 | try: |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 891 | event = ad_callee.ed.wait_for_event( |
| 892 | EventMessageWaitingIndicatorChanged, |
| 893 | _is_on_message_waiting_event_true) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 894 | log.info(event) |
| 895 | except Empty: |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 896 | raise _CallSequenceException("No expected event {}.".format( |
| 897 | EventMessageWaitingIndicatorChanged)) |
Yang Liu | aed3eef | 2015-12-15 18:40:25 -0800 | [diff] [blame] | 898 | voice_mail_count_after = ad_callee.droid.telephonyGetVoiceMailCountForSubscription( |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 899 | subid_callee) |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 900 | log.info( |
| 901 | "telephonyGetVoiceMailCount output - before: {}, after: {}".format( |
| 902 | voice_mail_count_before, voice_mail_count_after)) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 903 | |
| 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 908 | if not check_voice_mail_count(log, ad_callee, voice_mail_count_before, |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 909 | voice_mail_count_after): |
Yang Liu | aed3eef | 2015-12-15 18:40:25 -0800 | [diff] [blame] | 910 | log.error("telephonyGetVoiceMailCount output is incorrect.") |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 911 | return False |
| 912 | |
| 913 | except _CallSequenceException as e: |
| 914 | log.error(e) |
| 915 | return False |
| 916 | finally: |
Yang Liu | aed3eef | 2015-12-15 18:40:25 -0800 | [diff] [blame] | 917 | ad_callee.droid.telephonyStopTrackingVoiceMailStateChangeForSubscription( |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 918 | subid_callee) |
| 919 | return True |
| 920 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 921 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 922 | def 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 Liu | aed3eef | 2015-12-15 18:40:25 -0800 | [diff] [blame] | 939 | 6. Check telephonyGetVoiceMailCount result. it should be 0. |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 940 | |
| 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 Liu | aed3eef | 2015-12-15 18:40:25 -0800 | [diff] [blame] | 948 | if ad.droid.telephonyGetVoiceMailCount() == 0: |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 949 | 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 Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 957 | time.sleep(WAIT_TIME_VOICE_MAIL_SERVER_RESPONSE) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 958 | callId = ad.droid.telecomCallGetCallIds()[0] |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 959 | time.sleep(WAIT_TIME_VOICE_MAIL_SERVER_RESPONSE) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 960 | count = MAX_SAVED_VOICE_MAIL |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 961 | while (is_phone_in_call(log, ad) and (count > 0)): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 962 | log.info("Press 7 to delete voice mail.") |
| 963 | ad.droid.telecomCallPlayDtmfTone(callId, VOICEMAIL_DELETE_DIGIT) |
| 964 | ad.droid.telecomCallStopDtmfTone(callId) |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 965 | time.sleep(WAIT_TIME_VOICE_MAIL_SERVER_RESPONSE) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 966 | count -= 1 |
| 967 | log.info("Voice mail server dropped this call.") |
Yang Liu | aed3eef | 2015-12-15 18:40:25 -0800 | [diff] [blame] | 968 | # wait for telephonyGetVoiceMailCount to update correct result |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 969 | remaining_time = MAX_WAIT_TIME_VOICE_MAIL_COUNT |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 970 | while ((remaining_time > 0) and |
| 971 | (ad.droid.telephonyGetVoiceMailCount() != 0)): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 972 | time.sleep(1) |
| 973 | remaining_time -= 1 |
Yang Liu | aed3eef | 2015-12-15 18:40:25 -0800 | [diff] [blame] | 974 | current_voice_mail_count = ad.droid.telephonyGetVoiceMailCount() |
| 975 | log.info("telephonyGetVoiceMailCount: {}".format(current_voice_mail_count)) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 976 | return (current_voice_mail_count == 0) |
| 977 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 978 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 979 | def _is_on_message_waiting_event_true(event): |
| 980 | """Private function to return if the received EventMessageWaitingIndicatorChanged |
Yang Liu | dc8564b | 2016-01-27 14:15:37 -0800 | [diff] [blame] | 981 | event MessageWaitingIndicatorContainer.IS_MESSAGE_WAITING field is True. |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 982 | """ |
Yang Liu | dc8564b | 2016-01-27 14:15:37 -0800 | [diff] [blame] | 983 | return event['data'][MessageWaitingIndicatorContainer.IS_MESSAGE_WAITING] |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 984 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 985 | |
| 986 | def 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 Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 994 | """ 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 Liu | 855d5f8 | 2016-01-27 15:35:48 -0800 | [diff] [blame] | 998 | accept the call, (optional)then hang up from <droid_hangup>. |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 999 | |
| 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 Harold | ae6a0da | 2016-03-16 20:56:47 +0000 | [diff] [blame] | 1022 | log.info("Sub-ID Caller {}, Sub-ID Callee {}".format(subid_caller, subid_callee)) |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1023 | 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 Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1027 | |
| 1028 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1029 | def 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 Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1040 | """ 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 Liu | 855d5f8 | 2016-01-27 15:35:48 -0800 | [diff] [blame] | 1044 | accept the call, (optional)then hang up from <droid_hangup>. |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1045 | |
| 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 Liu | 1340629 | 2016-02-23 14:58:25 -0800 | [diff] [blame] | 1068 | CHECK_INTERVAL = 3 |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1069 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1070 | 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1083 | log, |
| 1084 | ad_callee, |
| 1085 | subid_callee, |
| 1086 | incoming_number=caller_number, |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1087 | 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1094 | raise _CallSequenceException("Caller not in correct state!") |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1095 | if verify_callee_func and not verify_callee_func(log, ad_callee): |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1096 | raise _CallSequenceException("Callee not in correct state!") |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1097 | |
Yang Liu | 1340629 | 2016-02-23 14:58:25 -0800 | [diff] [blame] | 1098 | 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 Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1119 | |
| 1120 | if not ad_hangup: |
| 1121 | return True |
| 1122 | |
| 1123 | if not hangup_call(log, ad_hangup): |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1124 | raise _CallSequenceException("Error in Hanging-Up Call") |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1125 | |
| 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1140 | |
Nathan Harold | ae6a0da | 2016-03-16 20:56:47 +0000 | [diff] [blame] | 1141 | def phone_number_formatter(input_string, format=None): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1142 | """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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1159 | input_string = input_string.replace(" ", "").replace("-", "").replace(".", |
| 1160 | "") |
Nathan Harold | ae6a0da | 2016-03-16 20:56:47 +0000 | [diff] [blame] | 1161 | if not format: |
| 1162 | return input_string |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1163 | # Remove "1" or "+1"from front |
| 1164 | if (len(input_string) == PHONE_NUMBER_STRING_FORMAT_11_DIGIT and |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1165 | input_string[0] == "1"): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1166 | 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1177 | input_string = "+1" + input_string |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1178 | elif format == PHONE_NUMBER_STRING_FORMAT_11_DIGIT: |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1179 | input_string = "1" + input_string |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1180 | 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1188 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1189 | def 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1203 | |
| 1204 | def verify_http_connection(log, |
| 1205 | ad, |
| 1206 | url="http://www.google.com/", |
| 1207 | retry=3, |
| 1208 | retry_interval=5): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1209 | """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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1217 | for i in range(0, retry + 1): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1218 | |
| 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1227 | if http_response: |
| 1228 | log.info("Verify Internet succeeded after {}s.".format( |
| 1229 | i * retry_interval) if i > 0 else "Verify Internet succeeded.") |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1230 | return True |
| 1231 | else: |
| 1232 | if i < retry: |
| 1233 | time.sleep(retry_interval) |
| 1234 | log.info("Verify Internet retry failed after {}s" |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1235 | .format(i * retry_interval)) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1236 | return False |
| 1237 | |
| 1238 | |
| 1239 | def _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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1244 | cur_type = connection_type_from_type_string( |
| 1245 | connection_type_string_in_event) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1246 | if cur_type != connection_type: |
| 1247 | log.info( |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1248 | "_connection_state_change expect: {}, received: {} <type {}>".format( |
| 1249 | connection_type, connection_type_string_in_event, |
| 1250 | cur_type)) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1251 | return False |
| 1252 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1253 | if 'isConnected' in _event['data'] and _event['data'][ |
| 1254 | 'isConnected'] == target_state: |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1255 | return True |
| 1256 | return False |
| 1257 | |
| 1258 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1259 | def wait_for_cell_data_connection( |
| 1260 | log, |
| 1261 | ad, |
| 1262 | state, |
| 1263 | timeout_value=EventDispatcher.DEFAULT_TIMEOUT): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1264 | """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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1284 | return wait_for_cell_data_connection_for_subscription(log, ad, sub_id, |
| 1285 | state, timeout_value) |
| 1286 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1287 | |
| 1288 | def _is_data_connection_state_match(log, ad, expected_data_connection_state): |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1289 | return (expected_data_connection_state == |
| 1290 | ad.droid.telephonyGetDataConnectionState()) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1291 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1292 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1293 | def _is_network_connected_state_match(log, ad, |
| 1294 | expected_network_connected_state): |
| 1295 | return (expected_network_connected_state == |
| 1296 | ad.droid.connectivityNetworkIsConnected()) |
| 1297 | |
| 1298 | |
| 1299 | def wait_for_cell_data_connection_for_subscription( |
| 1300 | log, |
| 1301 | ad, |
| 1302 | sub_id, |
| 1303 | state, |
| 1304 | timeout_value=EventDispatcher.DEFAULT_TIMEOUT): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1305 | """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 Liu | 8e6adff | 2016-02-05 10:24:04 -0800 | [diff] [blame] | 1325 | state_str = { |
| 1326 | True: DATA_STATE_CONNECTED, |
| 1327 | False: DATA_STATE_DISCONNECTED |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1328 | }[state] |
| 1329 | |
| 1330 | ad.ed.clear_all_events() |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1331 | ad.droid.telephonyStartTrackingDataConnectionStateChangeForSubscription( |
| 1332 | sub_id) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1333 | ad.droid.connectivityStartTrackingConnectivityStateChange() |
| 1334 | try: |
Yang Liu | 7a2e7ee | 2015-12-28 15:32:44 -0800 | [diff] [blame] | 1335 | # TODO: b/26293147 There is no framework API to get data connection |
| 1336 | # state by sub id |
Yang Liu | aed3eef | 2015-12-15 18:40:25 -0800 | [diff] [blame] | 1337 | data_state = ad.droid.telephonyGetDataConnectionState() |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1338 | if data_state == state_str: |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1339 | return _wait_for_nw_data_connection( |
| 1340 | log, ad, state, NETWORK_CONNECTION_TYPE_CELL, timeout_value) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1341 | |
| 1342 | try: |
| 1343 | event = ad.ed.wait_for_event(EventDataConnectionStateChanged, |
Yang Liu | 8e6adff | 2016-02-05 10:24:04 -0800 | [diff] [blame] | 1344 | is_event_match, timeout=timeout_value, |
| 1345 | field=DataConnectionStateContainer.DATA_CONNECTION_STATE, |
| 1346 | value=state_str) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1347 | except Empty: |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1348 | log.debug( |
| 1349 | "No expected event EventDataConnectionStateChanged {}.".format( |
Yang Liu | 8e6adff | 2016-02-05 10:24:04 -0800 | [diff] [blame] | 1350 | state_str)) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1351 | |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 1352 | # TODO: Wait for <MAX_WAIT_TIME_CONNECTION_STATE_UPDATE> seconds for |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1353 | # data connection state. |
| 1354 | # Otherwise, the network state will not be correct. |
| 1355 | # The bug is tracked here: b/20921915 |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1356 | |
Yang Liu | 7a2e7ee | 2015-12-28 15:32:44 -0800 | [diff] [blame] | 1357 | # 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 Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1361 | |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 1362 | if _wait_for_droid_in_state(log, ad, MAX_WAIT_TIME_CONNECTION_STATE_UPDATE, |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1363 | _is_network_connected_state_match, state): |
| 1364 | return _wait_for_nw_data_connection( |
| 1365 | log, ad, state, NETWORK_CONNECTION_TYPE_CELL, timeout_value) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1366 | else: |
| 1367 | return False |
| 1368 | |
| 1369 | finally: |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1370 | ad.droid.telephonyStopTrackingDataConnectionStateChangeForSubscription( |
| 1371 | sub_id) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1372 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1373 | |
| 1374 | def wait_for_wifi_data_connection( |
| 1375 | log, |
| 1376 | ad, |
| 1377 | state, |
| 1378 | timeout_value=EventDispatcher.DEFAULT_TIMEOUT): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1379 | """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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1395 | return _wait_for_nw_data_connection( |
| 1396 | log, ad, state, NETWORK_CONNECTION_TYPE_WIFI, timeout_value) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1397 | |
| 1398 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1399 | def wait_for_data_connection(log, |
| 1400 | ad, |
| 1401 | state, |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1402 | 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1424 | def _wait_for_nw_data_connection( |
| 1425 | log, |
| 1426 | ad, |
| 1427 | is_connected, |
| 1428 | connection_type=None, |
| 1429 | timeout_value=EventDispatcher.DEFAULT_TIMEOUT): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1430 | """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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1456 | log.info( |
| 1457 | "_wait_for_nw_data_connection: current connection type: {}".format( |
| 1458 | current_type)) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1459 | if not connection_type: |
| 1460 | return True |
| 1461 | else: |
| 1462 | if not is_connected and current_type != connection_type: |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1463 | log.info( |
| 1464 | "wait_for_nw_data_connection success: {} data not on {}!".format( |
| 1465 | ad.serial, connection_type)) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1466 | return True |
| 1467 | elif is_connected and current_type == connection_type: |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1468 | log.info( |
| 1469 | "wait_for_nw_data_connection success: {} data on {}!".format( |
| 1470 | ad.serial, connection_type)) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1471 | return True |
| 1472 | else: |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1473 | log.info("{} current state: {} target: {}".format( |
| 1474 | ad.serial, cur_data_connection_state, is_connected)) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1475 | |
| 1476 | try: |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1477 | 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 Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1482 | except Empty: |
| 1483 | pass |
| 1484 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1485 | log.info( |
| 1486 | "_wait_for_nw_data_connection: check connection after wait event.") |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 1487 | # TODO: Wait for <MAX_WAIT_TIME_CONNECTION_STATE_UPDATE> seconds for |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1488 | # data connection state. |
| 1489 | # Otherwise, the network state will not be correct. |
| 1490 | # The bug is tracked here: b/20921915 |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 1491 | if _wait_for_droid_in_state(log, ad, MAX_WAIT_TIME_CONNECTION_STATE_UPDATE, |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1492 | _is_network_connected_state_match, |
| 1493 | is_connected): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1494 | current_type = get_internet_connection_type(log, ad) |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1495 | log.info( |
| 1496 | "_wait_for_nw_data_connection: current connection type: {}".format( |
| 1497 | current_type)) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1498 | if not connection_type: |
| 1499 | return True |
| 1500 | else: |
| 1501 | if not is_connected and current_type != connection_type: |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1502 | log.info( |
| 1503 | "wait_for_nw_data_connection after event wait, success: {} data not on {}!".format( |
| 1504 | ad.serial, connection_type)) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1505 | return True |
| 1506 | elif is_connected and current_type == connection_type: |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1507 | log.info( |
| 1508 | "wait_for_nw_data_connection after event wait, success: {} data on {}!".format( |
| 1509 | ad.serial, connection_type)) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1510 | return True |
| 1511 | else: |
| 1512 | return False |
| 1513 | else: |
| 1514 | return False |
| 1515 | except Exception as e: |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1516 | log.error( |
| 1517 | "tel_test_utils._wait_for_nw_data_connection threw Random exception {}".format( |
| 1518 | str(e))) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1519 | return False |
| 1520 | finally: |
| 1521 | ad.droid.connectivityStopTrackingConnectivityStateChange() |
| 1522 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1523 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1524 | def 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1539 | log.error("Verify_incall_state: {} status:{}, expected:{}".format( |
| 1540 | ad.serial, ad.droid.telecomIsInCall(), expected_status)) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1541 | result = False |
| 1542 | return result |
| 1543 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1544 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1545 | def 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1562 | log.error("Expected:{}, Actual:{}".format(expected_number, |
| 1563 | actual_number)) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1564 | return False |
| 1565 | return True |
| 1566 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1567 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1568 | def 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1581 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1582 | def 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 | |
| 1598 | def 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 Liu | 7a2e7ee | 2015-12-28 15:32:44 -0800 | [diff] [blame] | 1611 | # TODO: b/26293960 No framework API available to set IMS by SubId. |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1612 | 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1621 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1622 | def 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1652 | |
| 1653 | def _wait_for_droid_in_state(log, ad, max_time, state_check_func, *args, |
| 1654 | **kwargs): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1655 | 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1665 | def _wait_for_droid_in_state_for_subscription( |
| 1666 | log, ad, sub_id, max_time, state_check_func, *args, **kwargs): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1667 | 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1677 | def _wait_for_droids_in_state(log, ads, max_time, state_check_func, *args, |
| 1678 | **kwargs): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1679 | 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1693 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1694 | def 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1703 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1704 | def 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1713 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1714 | def 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 Liu | 855d5f8 | 2016-01-27 15:35:48 -0800 | [diff] [blame] | 1728 | def 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1744 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1745 | def 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1759 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1760 | def _is_attached(log, ad, voice_or_data): |
| 1761 | return _is_attached_for_subscription( |
| 1762 | log, ad, ad.droid.subscriptionGetDefaultSubId(), voice_or_data) |
| 1763 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1764 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1765 | def _is_attached_for_subscription(log, ad, sub_id, voice_or_data): |
| 1766 | if get_network_rat_for_subscription(log, ad, sub_id, |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1767 | voice_or_data) != RAT_UNKNOWN: |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1768 | return True |
| 1769 | else: |
| 1770 | return False |
| 1771 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1772 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1773 | def 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1788 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1789 | def 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 Liu | 7a2e7ee | 2015-12-28 15:32:44 -0800 | [diff] [blame] | 1802 | if not _wait_for_droid_in_state_for_subscription( |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1803 | log, ad, sub_id, max_time, _is_attached_for_subscription, |
| 1804 | NETWORK_SERVICE_VOICE): |
Yang Liu | 7a2e7ee | 2015-12-28 15:32:44 -0800 | [diff] [blame] | 1805 | 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 Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 1810 | time.sleep(WAIT_TIME_1XRTT_VOICE_ATTACH) |
Yang Liu | 7a2e7ee | 2015-12-28 15:32:44 -0800 | [diff] [blame] | 1811 | return True |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1812 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1813 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1814 | def 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1829 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1830 | def 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1847 | |
Yang Liu | 9b85ce5 | 2016-02-16 12:25:11 -0800 | [diff] [blame] | 1848 | def 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 Liu | aed3eef | 2015-12-15 18:40:25 -0800 | [diff] [blame] | 1859 | return ad.droid.telephonyIsImsRegistered() |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1860 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1861 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1862 | def 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 Liu | 9b85ce5 | 2016-02-16 12:25:11 -0800 | [diff] [blame] | 1874 | return _wait_for_droid_in_state(log, ad, max_time, is_ims_registered) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1875 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1876 | |
Yang Liu | 9b85ce5 | 2016-02-16 12:25:11 -0800 | [diff] [blame] | 1877 | def 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 Liu | b93d729 | 2016-02-19 10:41:40 -0800 | [diff] [blame] | 1888 | volte_status = ad.droid.telephonyIsVolteAvailable() |
| 1889 | if volte_status is True and is_ims_registered(log, ad) is False: |
Yang Liu | 9b85ce5 | 2016-02-16 12:25:11 -0800 | [diff] [blame] | 1890 | log.error("Error! VoLTE is Available, but IMS is not registered.") |
| 1891 | return False |
Yang Liu | b93d729 | 2016-02-19 10:41:40 -0800 | [diff] [blame] | 1892 | return volte_status |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1893 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1894 | |
Yang Liu | 9b85ce5 | 2016-02-16 12:25:11 -0800 | [diff] [blame] | 1895 | def 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 Liu | b93d729 | 2016-02-19 10:41:40 -0800 | [diff] [blame] | 1906 | video_status = ad.droid.telephonyIsVideoCallingAvailable() |
| 1907 | if video_status is True and is_ims_registered(log, ad) is False: |
Yang Liu | 9b85ce5 | 2016-02-16 12:25:11 -0800 | [diff] [blame] | 1908 | log.error("Error! Video Call is Available, but IMS is not registered.") |
| 1909 | return False |
Yang Liu | b93d729 | 2016-02-19 10:41:40 -0800 | [diff] [blame] | 1910 | return video_status |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1911 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1912 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1913 | def 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 Liu | 9b85ce5 | 2016-02-16 12:25:11 -0800 | [diff] [blame] | 1925 | return _wait_for_droid_in_state(log, ad, max_time, is_volte_enabled) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1926 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1927 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1928 | def 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 Liu | 9b85ce5 | 2016-02-16 12:25:11 -0800 | [diff] [blame] | 1940 | return _wait_for_droid_in_state(log, ad, max_time, is_video_enabled) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1941 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1942 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1943 | def 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 Liu | 9b85ce5 | 2016-02-16 12:25:11 -0800 | [diff] [blame] | 1951 | 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 Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1953 | """ |
Yang Liu | b93d729 | 2016-02-19 10:41:40 -0800 | [diff] [blame] | 1954 | wfc_status = ad.droid.telephonyIsWifiCallingAvailable() |
| 1955 | if wfc_status is True and is_ims_registered(log, ad) is False: |
Yang Liu | 9b85ce5 | 2016-02-16 12:25:11 -0800 | [diff] [blame] | 1956 | log.error("Error! WiFi Calling is Available, but IMS is not registered.") |
| 1957 | return False |
Yang Liu | b93d729 | 2016-02-19 10:41:40 -0800 | [diff] [blame] | 1958 | return wfc_status |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1959 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1960 | |
Yang Liu | dfc37b6 | 2016-01-20 18:08:47 -0800 | [diff] [blame] | 1961 | def wait_for_wfc_enabled(log, ad, max_time=MAX_WAIT_TIME_WFC_ENABLED): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1962 | """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 Liu | dfc37b6 | 2016-01-20 18:08:47 -0800 | [diff] [blame] | 1968 | Default value is MAX_WAIT_TIME_WFC_ENABLED. |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1969 | |
| 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1976 | |
Yang Liu | dfc37b6 | 2016-01-20 18:08:47 -0800 | [diff] [blame] | 1977 | def wait_for_wfc_disabled(log, ad, max_time=MAX_WAIT_TIME_WFC_DISABLED): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1978 | """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 Liu | dfc37b6 | 2016-01-20 18:08:47 -0800 | [diff] [blame] | 1984 | Default value is MAX_WAIT_TIME_WFC_DISABLED. |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1985 | |
| 1986 | Returns: |
| 1987 | Return True if device report WiFi Calling enabled bit false within max_time. |
| 1988 | Return False if timeout. |
| 1989 | """ |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1990 | return _wait_for_droid_in_state( |
| 1991 | log, ad, max_time, lambda log, ad: not is_wfc_enabled(log, ad)) |
| 1992 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1993 | |
| 1994 | def 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2004 | return get_phone_number_for_subscription( |
| 2005 | log, ad, ad.droid.subscriptionGetDefaultVoiceSubId()) |
| 2006 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2007 | |
| 2008 | def 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 Liu | aed3eef | 2015-12-15 18:40:25 -0800 | [diff] [blame] | 2023 | number = ad.droid.telephonyGetLine1NumberForSubscription(subid) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2024 | return number |
| 2025 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2026 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2027 | def 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2038 | return set_phone_number_for_subscription( |
| 2039 | log, ad, ad.droid.subscriptionGetDefaultVoiceSubId(), phone_num) |
| 2040 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2041 | |
| 2042 | def 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2060 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2061 | def 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 Liu | aed3eef | 2015-12-15 18:40:25 -0800 | [diff] [blame] | 2075 | ad.droid.telephonyGetSimOperatorForSubscription(subId)) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2076 | else: |
| 2077 | result = operator_name_from_plmn_id( |
Yang Liu | aed3eef | 2015-12-15 18:40:25 -0800 | [diff] [blame] | 2078 | ad.droid.telephonyGetSimOperator()) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2079 | except KeyError: |
| 2080 | result = CARRIER_UNKNOWN |
| 2081 | return result |
| 2082 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2083 | |
Yang Liu | 9a9a4f7 | 2016-02-19 10:45:04 -0800 | [diff] [blame] | 2084 | def 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 Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2100 | def 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2113 | return ( |
| 2114 | check_phone_number_match(event['data']['Sender'], phonenumber_tx) and |
| 2115 | event['data']['Text'] == text) |
| 2116 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2117 | |
| 2118 | def 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2131 | return ( |
| 2132 | check_phone_number_match(event['data']['Sender'], phonenumber_tx) and |
| 2133 | text.startswith(event['data']['Text'])) |
| 2134 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2135 | |
| 2136 | def 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2149 | 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 Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2152 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2153 | |
| 2154 | def wait_for_matching_sms(log, |
| 2155 | ad_rx, |
| 2156 | phonenumber_tx, |
| 2157 | text, |
| 2158 | allow_multi_part_long_sms=True): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2159 | """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 Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 2175 | MAX_WAIT_TIME_SMS_RECEIVE, phonenumber_tx, |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2176 | text) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2177 | return True |
| 2178 | except Empty: |
| 2179 | log.error("No matched SMS received event.") |
| 2180 | return False |
| 2181 | else: |
| 2182 | try: |
| 2183 | received_sms = '' |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2184 | while (text != ''): |
| 2185 | event = ad_rx.ed.wait_for_event( |
| 2186 | EventSmsReceived, is_sms_partial_match, |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 2187 | MAX_WAIT_TIME_SMS_RECEIVE, phonenumber_tx, text) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2188 | 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2194 | log.error("Only received partial matched SMS: {}".format( |
| 2195 | received_sms)) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2196 | return False |
| 2197 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2198 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2199 | def sms_send_receive_verify_for_subscription(log, ad_tx, ad_rx, subid_tx, |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2200 | subid_rx, array_message): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2201 | """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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2219 | log.info("Sending SMS {} to {}, len: {}, content: {}.".format( |
| 2220 | phonenumber_tx, phonenumber_rx, len(text), text)) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2221 | 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2228 | ad_tx.ed.pop_event(EventSmsSentSuccess, |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 2229 | MAX_WAIT_TIME_SMS_SENT_SUCCESS) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2230 | except Empty: |
| 2231 | log.error("No sent_success event.") |
| 2232 | return False |
| 2233 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2234 | if not wait_for_matching_sms(log, |
| 2235 | ad_rx, |
| 2236 | phonenumber_tx, |
| 2237 | text, |
| 2238 | allow_multi_part_long_sms=True): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2239 | return False |
| 2240 | finally: |
| 2241 | ad_rx.droid.smsStopTrackingIncomingSmsMessage() |
| 2242 | return True |
| 2243 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2244 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2245 | def 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2258 | 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 Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2262 | |
Yang Liu | 7a2e7ee | 2015-12-28 15:32:44 -0800 | [diff] [blame] | 2263 | #TODO: b/21569494 This function is still a WIP and is disabled |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2264 | def mms_send_receive_verify_for_subscription(log, ad_tx, ad_rx, subid_tx, |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2265 | subid_rx, array_payload): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2266 | """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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2287 | log.info("Sending MMS {} to {}, subject: {}, message: {}.".format( |
| 2288 | phonenumber_tx, phonenumber_rx, subject, message)) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2289 | 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2295 | phonenumber_rx, subject, message, phonenumber_tx, filename) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2296 | |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 2297 | ad_tx.ed.pop_event(EventMmsSentSuccess, MAX_WAIT_TIME_SMS_SENT_SUCCESS) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2298 | |
| 2299 | start_time = time.time() |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 2300 | remaining_time = MAX_WAIT_TIME_SMS_RECEIVE |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2301 | while remaining_time > 0: |
| 2302 | event = ad_rx.ed.pop_event(EventSmsReceived, remaining_time) |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2303 | if check_phone_number_match(event['data']['Sender'], |
| 2304 | phonenumber_tx): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2305 | log.debug("Received SMS Indication") |
| 2306 | while remaining_time > 0: |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2307 | 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 Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2313 | 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2326 | |
| 2327 | def ensure_network_rat(log, |
| 2328 | ad, |
| 2329 | network_preference, |
| 2330 | rat_family, |
| 2331 | voice_or_data=None, |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 2332 | max_wait_time=MAX_WAIT_TIME_NW_SELECTION, |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2333 | toggle_apm_after_setting=False): |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 2334 | """Ensure ad's current network is in expected rat_family. |
| 2335 | """ |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2336 | return ensure_network_rat_for_subscription( |
| 2337 | log, ad, ad.droid.subscriptionGetDefaultSubId(), network_preference, |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 2338 | rat_family, voice_or_data, max_wait_time, toggle_apm_after_setting) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2339 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2340 | |
| 2341 | def ensure_network_rat_for_subscription(log, |
| 2342 | ad, |
| 2343 | sub_id, |
| 2344 | network_preference, |
| 2345 | rat_family, |
| 2346 | voice_or_data=None, |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 2347 | max_wait_time=MAX_WAIT_TIME_NW_SELECTION, |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2348 | toggle_apm_after_setting=False): |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 2349 | """Ensure ad's current network is in expected rat_family. |
| 2350 | """ |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2351 | if not ad.droid.telephonySetPreferredNetworkTypesForSubscription( |
| 2352 | network_preference, sub_id): |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 2353 | 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2356 | voice_or_data): |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 2357 | 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2367 | 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 Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 2381 | return result |
| 2382 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2383 | |
| 2384 | def ensure_network_preference(log, |
| 2385 | ad, |
| 2386 | network_preference, |
| 2387 | voice_or_data=None, |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 2388 | max_wait_time=MAX_WAIT_TIME_NW_SELECTION, |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2389 | toggle_apm_after_setting=False): |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 2390 | """Ensure that current rat is within the device's preferred network rats. |
| 2391 | """ |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2392 | return ensure_network_preference_for_subscription( |
| 2393 | log, ad, ad.droid.subscriptionGetDefaultSubId(), network_preference, |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 2394 | voice_or_data, max_wait_time, toggle_apm_after_setting) |
| 2395 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2396 | |
| 2397 | def ensure_network_preference_for_subscription( |
| 2398 | log, |
| 2399 | ad, |
| 2400 | sub_id, |
| 2401 | network_preference, |
| 2402 | voice_or_data=None, |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 2403 | max_wait_time=MAX_WAIT_TIME_NW_SELECTION, |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2404 | toggle_apm_after_setting=False): |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 2405 | """Ensure ad's network preference is <network_preference> for sub_id. |
| 2406 | """ |
| 2407 | rat_family_list = rat_families_for_network_preference(network_preference) |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2408 | if not ad.droid.telephonySetPreferredNetworkTypesForSubscription( |
| 2409 | network_preference, sub_id): |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 2410 | log.error("Set Preferred Networks failed.") |
| 2411 | return False |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2412 | if is_droid_in_rat_family_list_for_subscription( |
| 2413 | log, ad, sub_id, rat_family_list, voice_or_data): |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 2414 | 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2424 | 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 Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 2438 | return result |
| 2439 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2440 | |
| 2441 | def ensure_network_generation(log, |
| 2442 | ad, |
| 2443 | generation, |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 2444 | max_wait_time=MAX_WAIT_TIME_NW_SELECTION, |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2445 | voice_or_data=None, |
| 2446 | toggle_apm_after_setting=False): |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 2447 | """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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2454 | log, ad, ad.droid.subscriptionGetDefaultSubId(), generation, |
| 2455 | max_wait_time, voice_or_data, toggle_apm_after_setting) |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 2456 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2457 | |
| 2458 | def ensure_network_generation_for_subscription( |
| 2459 | log, |
| 2460 | ad, |
| 2461 | sub_id, |
| 2462 | generation, |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 2463 | max_wait_time=MAX_WAIT_TIME_NW_SELECTION, |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2464 | voice_or_data=None, |
| 2465 | toggle_apm_after_setting=False): |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 2466 | """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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2472 | if is_droid_in_network_generation_for_subscription( |
| 2473 | log, ad, sub_id, generation, voice_or_data): |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 2474 | return True |
| 2475 | |
| 2476 | operator = get_operator_name(log, ad, sub_id) |
| 2477 | try: |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2478 | network_preference = network_preference_for_generaton(generation, |
| 2479 | operator) |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 2480 | rat_family = rat_family_for_generation(generation, operator) |
| 2481 | except KeyError: |
| 2482 | log.error("Failed to find a rat_family entry for " |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2483 | "PLMN: {}, operator:{}, generation: {}".format( |
| 2484 | ad.droid.telephonyGetSimOperatorForSubscription( |
| 2485 | sub_id), operator, generation)) |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 2486 | return False |
| 2487 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2488 | if not ad.droid.telephonySetPreferredNetworkTypesForSubscription( |
| 2489 | network_preference, sub_id): |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 2490 | 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2501 | 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 Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 2515 | |
| 2516 | return result |
| 2517 | |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 2518 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2519 | def wait_for_network_rat(log, |
| 2520 | ad, |
| 2521 | rat_family, |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 2522 | max_wait_time=MAX_WAIT_TIME_NW_SELECTION, |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2523 | 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 | |
| 2529 | def wait_for_network_rat_for_subscription(log, |
| 2530 | ad, |
| 2531 | sub_id, |
| 2532 | rat_family, |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 2533 | max_wait_time=MAX_WAIT_TIME_NW_SELECTION, |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2534 | voice_or_data=None): |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 2535 | 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 Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 2539 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2540 | def wait_for_not_network_rat(log, |
| 2541 | ad, |
| 2542 | rat_family, |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 2543 | max_wait_time=MAX_WAIT_TIME_NW_SELECTION, |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2544 | 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 | |
| 2550 | def wait_for_not_network_rat_for_subscription( |
| 2551 | log, |
| 2552 | ad, |
| 2553 | sub_id, |
| 2554 | rat_family, |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 2555 | max_wait_time=MAX_WAIT_TIME_NW_SELECTION, |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2556 | voice_or_data=None): |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 2557 | return _wait_for_droid_in_state_for_subscription( |
| 2558 | log, ad, sub_id, max_wait_time, |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2559 | lambda log, ad, sub_id, *args, **kwargs: |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 2560 | not is_droid_in_rat_family_for_subscription( |
| 2561 | log, ad, sub_id, rat_family, voice_or_data)) |
| 2562 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2563 | |
| 2564 | def wait_for_preferred_network(log, |
| 2565 | ad, |
| 2566 | network_preference, |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 2567 | max_wait_time=MAX_WAIT_TIME_NW_SELECTION, |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2568 | voice_or_data=None): |
| 2569 | return wait_for_preferred_network_for_subscription( |
| 2570 | log, ad, ad.droid.subscriptionGetDefaultSubId(), network_preference, |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 2571 | max_wait_time, voice_or_data) |
| 2572 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2573 | |
| 2574 | def wait_for_preferred_network_for_subscription( |
| 2575 | log, |
| 2576 | ad, |
| 2577 | sub_id, |
| 2578 | network_preference, |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 2579 | max_wait_time=MAX_WAIT_TIME_NW_SELECTION, |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2580 | voice_or_data=None): |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 2581 | 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2587 | |
| 2588 | def wait_for_network_generation(log, |
| 2589 | ad, |
| 2590 | generation, |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 2591 | max_wait_time=MAX_WAIT_TIME_NW_SELECTION, |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2592 | 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 | |
| 2598 | def wait_for_network_generation_for_subscription( |
| 2599 | log, |
| 2600 | ad, |
| 2601 | sub_id, |
| 2602 | generation, |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 2603 | max_wait_time=MAX_WAIT_TIME_NW_SELECTION, |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2604 | 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 Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 2608 | voice_or_data) |
| 2609 | |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 2610 | |
| 2611 | def is_droid_in_rat_family(log, ad, rat_family, voice_or_data=None): |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2612 | return is_droid_in_rat_family_for_subscription( |
| 2613 | log, ad, ad.droid.subscriptionGetDefaultSubId(), rat_family, |
| 2614 | voice_or_data) |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 2615 | |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 2616 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2617 | def 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 Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 2624 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2625 | |
| 2626 | def 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 | |
| 2632 | def is_droid_in_rat_family_list_for_subscription(log, |
| 2633 | ad, |
| 2634 | sub_id, |
| 2635 | rat_family_list, |
| 2636 | voice_or_data=None): |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 2637 | 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 Liu | 7a2e7ee | 2015-12-28 15:32:44 -0800 | [diff] [blame] | 2645 | if rat_family_from_rat(nw_rat) in rat_family_list: |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 2646 | return True |
| 2647 | return False |
| 2648 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2649 | |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 2650 | def 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 Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2652 | |
| 2653 | Args: |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 2654 | 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 Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2660 | |
| 2661 | Returns: |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 2662 | True if droid in expected network generation. Otherwise False. |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2663 | """ |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 2664 | return is_droid_in_network_generation_for_subscription( |
| 2665 | log, ad, ad.droid.subscriptionGetDefaultSubId(), nw_gen, voice_or_data) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2666 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2667 | |
| 2668 | def is_droid_in_network_generation_for_subscription(log, ad, sub_id, nw_gen, |
| 2669 | voice_or_data): |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 2670 | """Checks if a droid in expected network generation ("2g", "3g" or "4g"). |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2671 | |
| 2672 | Args: |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 2673 | 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 Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2679 | |
| 2680 | Returns: |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 2681 | True if droid in expected network generation. Otherwise False. |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2682 | """ |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 2683 | service_list = [NETWORK_SERVICE_DATA, NETWORK_SERVICE_VOICE] |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2684 | |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 2685 | if voice_or_data: |
| 2686 | service_list = [voice_or_data] |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2687 | |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 2688 | for service in service_list: |
| 2689 | nw_rat = get_network_rat_for_subscription(log, ad, sub_id, service) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2690 | |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 2691 | if nw_rat == RAT_UNKNOWN or not is_valid_rat(nw_rat): |
| 2692 | continue |
| 2693 | |
Yang Liu | 7a2e7ee | 2015-12-28 15:32:44 -0800 | [diff] [blame] | 2694 | if rat_generation_from_rat(nw_rat) == nw_gen: |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 2695 | return True |
| 2696 | else: |
| 2697 | return False |
| 2698 | |
| 2699 | return False |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2700 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2701 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2702 | def 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2717 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2718 | def 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2732 | ret_val = ad.droid.telephonyGetCurrentVoiceNetworkTypeForSubscription( |
| 2733 | sub_id) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2734 | elif voice_or_data == NETWORK_SERVICE_DATA: |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2735 | ret_val = ad.droid.telephonyGetCurrentDataNetworkTypeForSubscription( |
| 2736 | sub_id) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2737 | else: |
Yang Liu | aed3eef | 2015-12-15 18:40:25 -0800 | [diff] [blame] | 2738 | ret_val = ad.droid.telephonyGetNetworkTypeForSubscription(sub_id) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2739 | |
| 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2746 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2747 | def 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2761 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2762 | def 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2774 | return rat_generation_from_rat(get_network_rat_for_subscription( |
| 2775 | log, ad, sub_id, voice_or_data)) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2776 | except KeyError: |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2777 | 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 Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 2781 | return GEN_UNKNOWN |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2782 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2783 | |
| 2784 | def check_voice_mail_count(log, ad, voice_mail_count_before, |
| 2785 | voice_mail_count_after): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2786 | """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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2791 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2792 | def get_voice_mail_number(log, ad): |
| 2793 | """function to get the voice mail number |
| 2794 | """ |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2795 | voice_mail_number = get_voice_mail_number_function(get_operator_name(log, |
| 2796 | ad))() |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2797 | if voice_mail_number is None: |
| 2798 | return get_phone_number(log, ad) |
| 2799 | return voice_mail_number |
| 2800 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2801 | |
| 2802 | def ensure_phones_idle(log, |
| 2803 | ads, |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2804 | 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2814 | |
| 2815 | def ensure_phone_idle(log, ad, settling_time=WAIT_TIME_ANDROID_STATE_SETTLING): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2816 | """Ensure ad idle (not in call). |
| 2817 | """ |
| 2818 | return ensure_phones_idle(log, [ad], settling_time) |
| 2819 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2820 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2821 | def 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2832 | 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 Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2839 | result = False |
| 2840 | if ((not WifiUtils.wifi_reset(log, ad)) or |
| 2841 | (not WifiUtils.wifi_toggle_state(log, ad, False))): |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2842 | log.error("ensure_phones_default_state:reset WiFi fail {}.".format( |
| 2843 | ad.serial)) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2844 | result = False |
| 2845 | if not toggle_airplane_mode(log, ad, False): |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2846 | log.error( |
| 2847 | "ensure_phones_default_state:turn off airplane mode fail {}.".format( |
| 2848 | ad.serial)) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2849 | result = False |
| 2850 | # make sure phone data is on |
Yang Liu | aed3eef | 2015-12-15 18:40:25 -0800 | [diff] [blame] | 2851 | ad.droid.telephonyToggleDataConnection(True) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2852 | |
| 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2857 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2858 | def 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2872 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2873 | def 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2883 | while (retry >= 0): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2884 | 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2894 | |
Yang Liu | 98fd9d7 | 2016-03-04 12:14:49 -0800 | [diff] [blame] | 2895 | def 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 Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2917 | def 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2931 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2932 | def 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2945 | executor = concurrent.futures.ThreadPoolExecutor( |
| 2946 | max_workers=number_of_workers) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2947 | 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2955 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2956 | def 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2970 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2971 | def 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2985 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2986 | def 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2995 | log.info("Setting subscription:{} as Message SIM for {}".format(sub_id, |
| 2996 | ad.serial)) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2997 | 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 3002 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3003 | def 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 3012 | log.info("Setting subscription:{} as Data SIM for {}".format(sub_id, |
| 3013 | ad.serial)) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3014 | 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 Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 3019 | if not wait_for_data_connection(log, ad, True, MAX_WAIT_TIME_DATA_SUB_CHANGE): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3020 | log.error("Data Connection failed - Not able to switch Data SIM") |
| 3021 | return False |
| 3022 | return True |
| 3023 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 3024 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3025 | def 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 3034 | log.info("Setting subscription:{} as Voice SIM for {}".format(sub_id, |
| 3035 | ad.serial)) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3036 | 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 3042 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3043 | def 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 Liu | aed3eef | 2015-12-15 18:40:25 -0800 | [diff] [blame] | 3058 | ad.droid.telephonyAdjustPreciseCallStateListenLevelForSubscription( |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 3059 | "Foreground", value, sub_id) |
Yang Liu | aed3eef | 2015-12-15 18:40:25 -0800 | [diff] [blame] | 3060 | ad.droid.telephonyAdjustPreciseCallStateListenLevelForSubscription( |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 3061 | "Ringing", value, sub_id) |
Yang Liu | aed3eef | 2015-12-15 18:40:25 -0800 | [diff] [blame] | 3062 | ad.droid.telephonyAdjustPreciseCallStateListenLevelForSubscription( |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 3063 | "Background", value, sub_id) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3064 | return True |
| 3065 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 3066 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3067 | def 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 3090 | if not set_preferred_subid_for_data(log, ad, sub_id): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3091 | return False |
| 3092 | return True |
| 3093 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 3094 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3095 | def 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 Liu | aed3eef | 2015-12-15 18:40:25 -0800 | [diff] [blame] | 3111 | sim_count = ads[index].droid.telephonyGetSimCount() |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3112 | 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 3117 | |
Yang Liu | 8e6adff | 2016-02-05 10:24:04 -0800 | [diff] [blame] | 3118 | def is_event_match(event, field, value): |
| 3119 | """Return if <field> in "event" match <value> or not. |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3120 | |
| 3121 | Args: |
Yang Liu | 8e6adff | 2016-02-05 10:24:04 -0800 | [diff] [blame] | 3122 | event: event to test. This event need to have <field>. |
| 3123 | field: field to match. |
| 3124 | value: value to match. |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3125 | |
| 3126 | Returns: |
Yang Liu | 8e6adff | 2016-02-05 10:24:04 -0800 | [diff] [blame] | 3127 | True if <field> in "event" match <value>. |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3128 | False otherwise. |
| 3129 | """ |
Yang Liu | 8e6adff | 2016-02-05 10:24:04 -0800 | [diff] [blame] | 3130 | return is_event_match_for_list(event, field, [value]) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3131 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 3132 | |
Yang Liu | 8e6adff | 2016-02-05 10:24:04 -0800 | [diff] [blame] | 3133 | def 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 Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3136 | |
| 3137 | Args: |
Yang Liu | 8e6adff | 2016-02-05 10:24:04 -0800 | [diff] [blame] | 3138 | 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 Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3141 | |
| 3142 | Returns: |
Yang Liu | 8e6adff | 2016-02-05 10:24:04 -0800 | [diff] [blame] | 3143 | True if <field> in "event" match one of the value in "value_list". |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3144 | False otherwise. |
| 3145 | """ |
| 3146 | try: |
Yang Liu | 8e6adff | 2016-02-05 10:24:04 -0800 | [diff] [blame] | 3147 | value_in_event = event['data'][field] |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3148 | except KeyError: |
| 3149 | return False |
Yang Liu | 8e6adff | 2016-02-05 10:24:04 -0800 | [diff] [blame] | 3150 | for value in value_list: |
| 3151 | if value_in_event == value: |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3152 | return True |
| 3153 | return False |
| 3154 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 3155 | |
Yang Liu | 8e6adff | 2016-02-05 10:24:04 -0800 | [diff] [blame] | 3156 | def is_network_call_back_event_match(event, network_callback_id, |
| 3157 | network_callback_event): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3158 | try: |
Yang Liu | 8e6adff | 2016-02-05 10:24:04 -0800 | [diff] [blame] | 3159 | return ((network_callback_id == event[ |
| 3160 | 'data'][NetworkCallbackContainer.ID]) and |
| 3161 | (network_callback_event == event['data'][ |
| 3162 | NetworkCallbackContainer.NETWORK_CALLBACK_EVENT])) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3163 | except KeyError: |
| 3164 | return False |
| 3165 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 3166 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3167 | def 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 3196 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3197 | def 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 Harold | 23683d2 | 2015-12-14 16:19:08 -0800 | [diff] [blame] | 3212 | |
| 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 3219 | 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 Harold | 23683d2 | 2015-12-14 16:19:08 -0800 | [diff] [blame] | 3225 | return check_phone_number_match(uri1_number, uri2_number) |
| 3226 | else: |
| 3227 | return uri1 == uri2 |
| 3228 | except AttributeError as e: |
| 3229 | return False |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3230 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 3231 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3232 | def 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 Liu | 7ce1998 | 2016-03-09 12:20:41 -0800 | [diff] [blame] | 3250 | def 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 | |
| 3262 | def 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 | |
| 3277 | def 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 Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 3288 | |
Yang Liu | 7a2e7ee | 2015-12-28 15:32:44 -0800 | [diff] [blame] | 3289 | # TODO: b/26294018 Remove wrapper class once wifi_utils methods updated |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3290 | class WifiUtils(): |
| 3291 | |
tturney | f640a71 | 2015-12-30 15:18:15 -0800 | [diff] [blame] | 3292 | 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 Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3295 | import wifi_connect as _wifi_connect |
tturney | f640a71 | 2015-12-30 15:18:15 -0800 | [diff] [blame] | 3296 | from acts.test_utils.wifi.wifi_test_utils \ |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3297 | import wifi_toggle_state as _wifi_toggle_state |
tturney | f640a71 | 2015-12-30 15:18:15 -0800 | [diff] [blame] | 3298 | from acts.test_utils.wifi.wifi_test_utils \ |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3299 | import start_wifi_tethering as _start_wifi_tethering |
tturney | f640a71 | 2015-12-30 15:18:15 -0800 | [diff] [blame] | 3300 | from acts.test_utils.wifi.wifi_test_utils \ |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3301 | import stop_wifi_tethering as _stop_wifi_tethering |
tturney | f640a71 | 2015-12-30 15:18:15 -0800 | [diff] [blame] | 3302 | from acts.test_utils.wifi.wifi_test_utils \ |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3303 | 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 Liu | 2bf3089 | 2015-12-17 09:53:45 -0800 | [diff] [blame] | 3308 | PWD_KEY = _WifiEnums.PWD_KEY |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3309 | |
| 3310 | @staticmethod |
| 3311 | def wifi_toggle_state(log, ad, state): |
| 3312 | try: |
Shabeerali Karattu Parambath | 8a8061b | 2015-12-10 12:17:23 -0800 | [diff] [blame] | 3313 | WifiUtils._wifi_toggle_state(ad, state) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3314 | 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 Parambath | 8a8061b | 2015-12-10 12:17:23 -0800 | [diff] [blame] | 3322 | WifiUtils._reset_wifi(ad) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3323 | 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 Liu | 2bf3089 | 2015-12-17 09:53:45 -0800 | [diff] [blame] | 3338 | network = {WifiUtils.SSID_KEY: ssid} |
| 3339 | if password: |
| 3340 | network[WifiUtils.PWD_KEY] = password |
| 3341 | WifiUtils._wifi_connect(ad, network) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3342 | except Empty: |
| 3343 | # did not get event, then check connection info |
| 3344 | try: |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 3345 | if ad.droid.wifiGetConnectionInfo()[ |
| 3346 | WifiUtils.SSID_KEY] == ssid: |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3347 | return True |
| 3348 | else: |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 3349 | 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 Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3354 | 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 Liu | 2bf3089 | 2015-12-17 09:53:45 -0800 | [diff] [blame] | 3361 | return True |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3362 | |
| 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 |