Mark De Ruyter | 1a7ae57 | 2018-03-02 15:35:36 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
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 |
Betty Zhou | 2e01bc8 | 2017-03-17 10:55:57 -0700 | [diff] [blame] | 21 | import json |
Ang Li | 5bd83f3 | 2016-05-23 14:39:38 -0700 | [diff] [blame] | 22 | import logging |
Betty Zhou | 17dcabb | 2017-05-25 19:14:58 -0700 | [diff] [blame] | 23 | import re |
Betty Zhou | ccd171d | 2017-02-13 15:11:33 -0800 | [diff] [blame] | 24 | import os |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 25 | import urllib.parse |
| 26 | import time |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 27 | |
Betty Zhou | 3b2de07 | 2018-03-15 16:46:26 -0700 | [diff] [blame] | 28 | from acts import signals |
Betty Zhou | e62b1f1 | 2018-01-24 17:05:20 -0800 | [diff] [blame] | 29 | from acts import utils |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 30 | from queue import Empty |
Betty Zhou | 5534e67 | 2017-03-07 13:47:10 -0800 | [diff] [blame] | 31 | from acts.asserts import abort_all |
Betty Zhou | 2e01bc8 | 2017-03-17 10:55:57 -0700 | [diff] [blame] | 32 | from acts.controllers.adb import AdbError |
Betty Zhou | 9a96fc3 | 2018-02-01 16:44:05 -0800 | [diff] [blame] | 33 | from acts.controllers.android_device import DEFAULT_QXDM_LOG_PATH |
Betty Zhou | 9243770 | 2018-02-07 19:49:07 -0800 | [diff] [blame] | 34 | from acts.controllers.android_device import SL4A_APK_NAME |
Betty Zhou | 3db27a3 | 2018-04-23 14:31:25 -0700 | [diff] [blame] | 35 | from acts.libs.proc import job |
Yang Liu | 9a9a4f7 | 2016-02-19 10:45:04 -0800 | [diff] [blame] | 36 | from acts.test_utils.tel.tel_defines import AOSP_PREFIX |
Betty Zhou | d49a0ce | 2018-01-19 17:49:53 -0800 | [diff] [blame] | 37 | from acts.test_utils.tel.tel_defines import CARD_POWER_DOWN |
| 38 | from acts.test_utils.tel.tel_defines import CARD_POWER_UP |
Yang Liu | 46ed722 | 2015-12-28 14:08:52 -0800 | [diff] [blame] | 39 | from acts.test_utils.tel.tel_defines import CARRIER_UNKNOWN |
Betty Zhou | 9e54fc2 | 2017-01-19 12:15:53 -0800 | [diff] [blame] | 40 | from acts.test_utils.tel.tel_defines import COUNTRY_CODE_LIST |
Yang Liu | 46ed722 | 2015-12-28 14:08:52 -0800 | [diff] [blame] | 41 | from acts.test_utils.tel.tel_defines import DATA_STATE_CONNECTED |
| 42 | from acts.test_utils.tel.tel_defines import DATA_STATE_DISCONNECTED |
Betty Zhou | 9e2bf40 | 2017-02-01 19:04:09 -0800 | [diff] [blame] | 43 | from acts.test_utils.tel.tel_defines import DATA_ROAMING_ENABLE |
| 44 | from acts.test_utils.tel.tel_defines import DATA_ROAMING_DISABLE |
Yang Liu | 98fd9d7 | 2016-03-04 12:14:49 -0800 | [diff] [blame] | 45 | from acts.test_utils.tel.tel_defines import GEN_4G |
Yang Liu | 46ed722 | 2015-12-28 14:08:52 -0800 | [diff] [blame] | 46 | from acts.test_utils.tel.tel_defines import GEN_UNKNOWN |
| 47 | from acts.test_utils.tel.tel_defines import INCALL_UI_DISPLAY_BACKGROUND |
| 48 | from acts.test_utils.tel.tel_defines import INCALL_UI_DISPLAY_FOREGROUND |
| 49 | from acts.test_utils.tel.tel_defines import INVALID_SIM_SLOT_INDEX |
| 50 | from acts.test_utils.tel.tel_defines import INVALID_SUB_ID |
| 51 | from acts.test_utils.tel.tel_defines import MAX_SAVED_VOICE_MAIL |
| 52 | from acts.test_utils.tel.tel_defines import MAX_SCREEN_ON_TIME |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 53 | from acts.test_utils.tel.tel_defines import \ |
| 54 | MAX_WAIT_TIME_ACCEPT_CALL_TO_OFFHOOK_EVENT |
| 55 | from acts.test_utils.tel.tel_defines import MAX_WAIT_TIME_AIRPLANEMODE_EVENT |
Nathan Harold | eb60b19 | 2016-08-24 14:41:55 -0700 | [diff] [blame] | 56 | from acts.test_utils.tel.tel_defines import MAX_WAIT_TIME_CALL_DROP |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 57 | from acts.test_utils.tel.tel_defines import MAX_WAIT_TIME_CALL_INITIATION |
| 58 | from acts.test_utils.tel.tel_defines import MAX_WAIT_TIME_CALLEE_RINGING |
| 59 | from acts.test_utils.tel.tel_defines import MAX_WAIT_TIME_CONNECTION_STATE_UPDATE |
| 60 | from acts.test_utils.tel.tel_defines import MAX_WAIT_TIME_DATA_SUB_CHANGE |
Yang Liu | 598b93d | 2016-03-22 17:07:59 -0700 | [diff] [blame] | 61 | from acts.test_utils.tel.tel_defines import MAX_WAIT_TIME_CALL_IDLE_EVENT |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 62 | from acts.test_utils.tel.tel_defines import MAX_WAIT_TIME_NW_SELECTION |
| 63 | from acts.test_utils.tel.tel_defines import MAX_WAIT_TIME_SMS_RECEIVE |
| 64 | 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] | 65 | 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] | 66 | 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] | 67 | from acts.test_utils.tel.tel_defines import MAX_WAIT_TIME_WFC_DISABLED |
| 68 | 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] | 69 | from acts.test_utils.tel.tel_defines import NETWORK_MODE_LTE_ONLY |
Yang Liu | 46ed722 | 2015-12-28 14:08:52 -0800 | [diff] [blame] | 70 | from acts.test_utils.tel.tel_defines import NETWORK_CONNECTION_TYPE_CELL |
| 71 | from acts.test_utils.tel.tel_defines import NETWORK_CONNECTION_TYPE_WIFI |
| 72 | from acts.test_utils.tel.tel_defines import NETWORK_SERVICE_DATA |
| 73 | from acts.test_utils.tel.tel_defines import NETWORK_SERVICE_VOICE |
| 74 | from acts.test_utils.tel.tel_defines import PHONE_NUMBER_STRING_FORMAT_7_DIGIT |
| 75 | from acts.test_utils.tel.tel_defines import PHONE_NUMBER_STRING_FORMAT_10_DIGIT |
| 76 | from acts.test_utils.tel.tel_defines import PHONE_NUMBER_STRING_FORMAT_11_DIGIT |
| 77 | from acts.test_utils.tel.tel_defines import PHONE_NUMBER_STRING_FORMAT_12_DIGIT |
| 78 | from acts.test_utils.tel.tel_defines import RAT_FAMILY_GSM |
| 79 | from acts.test_utils.tel.tel_defines import RAT_FAMILY_LTE |
| 80 | from acts.test_utils.tel.tel_defines import RAT_FAMILY_WLAN |
Yang Liu | cde45aa | 2016-02-22 13:46:41 -0800 | [diff] [blame] | 81 | from acts.test_utils.tel.tel_defines import RAT_FAMILY_WCDMA |
Yang Liu | 46ed722 | 2015-12-28 14:08:52 -0800 | [diff] [blame] | 82 | from acts.test_utils.tel.tel_defines import RAT_1XRTT |
| 83 | from acts.test_utils.tel.tel_defines import RAT_UNKNOWN |
| 84 | from acts.test_utils.tel.tel_defines import SERVICE_STATE_EMERGENCY_ONLY |
| 85 | from acts.test_utils.tel.tel_defines import SERVICE_STATE_IN_SERVICE |
Betty Zhou | 688c103 | 2017-11-20 20:08:04 -0800 | [diff] [blame] | 86 | from acts.test_utils.tel.tel_defines import SERVICE_STATE_MAPPING |
Yang Liu | 46ed722 | 2015-12-28 14:08:52 -0800 | [diff] [blame] | 87 | from acts.test_utils.tel.tel_defines import SERVICE_STATE_OUT_OF_SERVICE |
| 88 | from acts.test_utils.tel.tel_defines import SERVICE_STATE_POWER_OFF |
Betty Zhou | 6dd0ac6 | 2018-04-27 18:59:12 -0700 | [diff] [blame^] | 89 | from acts.test_utils.tel.tel_defines import SIM_STATE_ABSENT |
Betty Zhou | e66efb4 | 2018-01-31 19:45:56 -0800 | [diff] [blame] | 90 | from acts.test_utils.tel.tel_defines import SIM_STATE_LOADED |
| 91 | from acts.test_utils.tel.tel_defines import SIM_STATE_NOT_READY |
Betty Zhou | 59ccab6 | 2017-08-17 18:17:29 -0700 | [diff] [blame] | 92 | from acts.test_utils.tel.tel_defines import SIM_STATE_PIN_REQUIRED |
Yang Liu | 46ed722 | 2015-12-28 14:08:52 -0800 | [diff] [blame] | 93 | from acts.test_utils.tel.tel_defines import SIM_STATE_READY |
Betty Zhou | d49a0ce | 2018-01-19 17:49:53 -0800 | [diff] [blame] | 94 | from acts.test_utils.tel.tel_defines import SIM_STATE_UNKNOWN |
Yang Liu | 46ed722 | 2015-12-28 14:08:52 -0800 | [diff] [blame] | 95 | from acts.test_utils.tel.tel_defines import TELEPHONY_STATE_IDLE |
| 96 | from acts.test_utils.tel.tel_defines import TELEPHONY_STATE_OFFHOOK |
| 97 | from acts.test_utils.tel.tel_defines import TELEPHONY_STATE_RINGING |
| 98 | from acts.test_utils.tel.tel_defines import VOICEMAIL_DELETE_DIGIT |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 99 | 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] | 100 | from acts.test_utils.tel.tel_defines import WAIT_TIME_ANDROID_STATE_SETTLING |
Betty Zhou | f25ce44 | 2017-03-03 14:28:36 -0800 | [diff] [blame] | 101 | from acts.test_utils.tel.tel_defines import WAIT_TIME_BETWEEN_STATE_CHECK |
Betty Zhou | ddc7640 | 2018-01-23 16:29:25 -0800 | [diff] [blame] | 102 | from acts.test_utils.tel.tel_defines import MAX_WAIT_TIME_FOR_STATE_CHANGE |
tturney | 0eddcae | 2016-03-30 19:59:14 -0700 | [diff] [blame] | 103 | from acts.test_utils.tel.tel_defines import WAIT_TIME_CHANGE_DATA_SUB_ID |
Yang Liu | 46ed722 | 2015-12-28 14:08:52 -0800 | [diff] [blame] | 104 | from acts.test_utils.tel.tel_defines import WAIT_TIME_IN_CALL |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 105 | 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] | 106 | from acts.test_utils.tel.tel_defines import WAIT_TIME_REJECT_CALL |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 107 | 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] | 108 | from acts.test_utils.tel.tel_defines import WFC_MODE_DISABLED |
Jaineel | 760329b | 2018-01-16 13:55:24 -0800 | [diff] [blame] | 109 | from acts.test_utils.tel.tel_defines import TYPE_MOBILE |
| 110 | from acts.test_utils.tel.tel_defines import TYPE_WIFI |
Yang Liu | 46ed722 | 2015-12-28 14:08:52 -0800 | [diff] [blame] | 111 | from acts.test_utils.tel.tel_defines import EventCallStateChanged |
| 112 | from acts.test_utils.tel.tel_defines import EventConnectivityChanged |
| 113 | from acts.test_utils.tel.tel_defines import EventDataConnectionStateChanged |
| 114 | from acts.test_utils.tel.tel_defines import EventDataSmsReceived |
| 115 | from acts.test_utils.tel.tel_defines import EventMessageWaitingIndicatorChanged |
| 116 | from acts.test_utils.tel.tel_defines import EventServiceStateChanged |
Betty Zhou | 4411c20 | 2018-03-29 18:27:25 -0700 | [diff] [blame] | 117 | from acts.test_utils.tel.tel_defines import EventMmsSentFailure |
Yang Liu | 46ed722 | 2015-12-28 14:08:52 -0800 | [diff] [blame] | 118 | from acts.test_utils.tel.tel_defines import EventMmsSentSuccess |
Betty Zhou | a27e5d4 | 2017-01-17 11:33:04 -0800 | [diff] [blame] | 119 | from acts.test_utils.tel.tel_defines import EventMmsDownloaded |
Yang Liu | 46ed722 | 2015-12-28 14:08:52 -0800 | [diff] [blame] | 120 | from acts.test_utils.tel.tel_defines import EventSmsReceived |
Betty Zhou | c9f723b | 2018-04-10 18:08:21 -0700 | [diff] [blame] | 121 | from acts.test_utils.tel.tel_defines import EventSmsDeliverFailure |
| 122 | from acts.test_utils.tel.tel_defines import EventSmsDeliverSuccess |
Betty Zhou | 4411c20 | 2018-03-29 18:27:25 -0700 | [diff] [blame] | 123 | from acts.test_utils.tel.tel_defines import EventSmsSentFailure |
Yang Liu | 46ed722 | 2015-12-28 14:08:52 -0800 | [diff] [blame] | 124 | from acts.test_utils.tel.tel_defines import EventSmsSentSuccess |
Yang Liu | dc8564b | 2016-01-27 14:15:37 -0800 | [diff] [blame] | 125 | from acts.test_utils.tel.tel_defines import CallStateContainer |
Yang Liu | 8e6adff | 2016-02-05 10:24:04 -0800 | [diff] [blame] | 126 | from acts.test_utils.tel.tel_defines import DataConnectionStateContainer |
Yang Liu | dc8564b | 2016-01-27 14:15:37 -0800 | [diff] [blame] | 127 | from acts.test_utils.tel.tel_defines import MessageWaitingIndicatorContainer |
Yang Liu | 8e6adff | 2016-02-05 10:24:04 -0800 | [diff] [blame] | 128 | from acts.test_utils.tel.tel_defines import NetworkCallbackContainer |
| 129 | from acts.test_utils.tel.tel_defines import ServiceStateContainer |
Yang Liu | 46ed722 | 2015-12-28 14:08:52 -0800 | [diff] [blame] | 130 | from acts.test_utils.tel.tel_lookup_tables import \ |
| 131 | connection_type_from_type_string |
| 132 | from acts.test_utils.tel.tel_lookup_tables import is_valid_rat |
Yang Liu | 98fd9d7 | 2016-03-04 12:14:49 -0800 | [diff] [blame] | 133 | 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] | 134 | from acts.test_utils.tel.tel_lookup_tables import \ |
| 135 | get_voice_mail_count_check_function |
Betty Zhou | 9402318 | 2017-06-07 18:02:14 -0700 | [diff] [blame] | 136 | from acts.test_utils.tel.tel_lookup_tables import get_voice_mail_check_number |
Betty Zhou | a6c82eb | 2017-04-13 18:09:03 -0700 | [diff] [blame] | 137 | from acts.test_utils.tel.tel_lookup_tables import get_voice_mail_delete_digit |
Yang Liu | 46ed722 | 2015-12-28 14:08:52 -0800 | [diff] [blame] | 138 | from acts.test_utils.tel.tel_lookup_tables import \ |
Jaineel | c52d685 | 2017-10-27 15:03:54 -0700 | [diff] [blame] | 139 | network_preference_for_generation |
Betty Zhou | b14c101 | 2018-04-20 11:27:00 -0700 | [diff] [blame] | 140 | from acts.test_utils.tel.tel_lookup_tables import \ |
| 141 | operator_name_from_network_name |
Yang Liu | 46ed722 | 2015-12-28 14:08:52 -0800 | [diff] [blame] | 142 | from acts.test_utils.tel.tel_lookup_tables import operator_name_from_plmn_id |
| 143 | from acts.test_utils.tel.tel_lookup_tables import \ |
| 144 | rat_families_for_network_preference |
| 145 | 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] | 146 | from acts.test_utils.tel.tel_lookup_tables import rat_family_from_rat |
| 147 | from acts.test_utils.tel.tel_lookup_tables import rat_generation_from_rat |
Yang Liu | cc1c650 | 2016-04-19 10:50:06 -0700 | [diff] [blame] | 148 | from acts.test_utils.tel.tel_subscription_utils import \ |
| 149 | get_default_data_sub_id |
| 150 | from acts.test_utils.tel.tel_subscription_utils import \ |
| 151 | get_outgoing_message_sub_id |
| 152 | from acts.test_utils.tel.tel_subscription_utils import \ |
| 153 | get_outgoing_voice_sub_id |
| 154 | from acts.test_utils.tel.tel_subscription_utils import \ |
| 155 | get_incoming_voice_sub_id |
| 156 | from acts.test_utils.tel.tel_subscription_utils import \ |
| 157 | get_incoming_message_sub_id |
Betty Zhou | f987b8f | 2017-03-09 16:34:00 -0800 | [diff] [blame] | 158 | from acts.test_utils.wifi import wifi_test_utils |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 159 | from acts.test_utils.wifi import wifi_constants |
Betty Zhou | 8e11e44 | 2017-03-30 20:00:34 -0700 | [diff] [blame] | 160 | from acts.utils import adb_shell_ping |
Yang Liu | 46ed722 | 2015-12-28 14:08:52 -0800 | [diff] [blame] | 161 | from acts.utils import load_config |
Jaineel | 55ae6f9 | 2017-06-29 17:44:19 -0700 | [diff] [blame] | 162 | from acts.utils import create_dir |
| 163 | from acts.utils import start_standing_subprocess |
| 164 | from acts.utils import stop_standing_subprocess |
Jaineel | c9e7bfa | 2017-08-07 14:11:30 -0700 | [diff] [blame] | 165 | from acts.logger import epoch_to_log_line_timestamp |
| 166 | from acts.logger import normalize_log_line_timestamp |
| 167 | from acts.utils import get_current_epoch_time |
Jaineel | 5576d43 | 2017-10-19 15:36:42 -0700 | [diff] [blame] | 168 | from acts.utils import exe_cmd |
Ang Li | 5bd83f3 | 2016-05-23 14:39:38 -0700 | [diff] [blame] | 169 | |
Betty Zhou | f987b8f | 2017-03-09 16:34:00 -0800 | [diff] [blame] | 170 | WIFI_SSID_KEY = wifi_test_utils.WifiEnums.SSID_KEY |
| 171 | WIFI_PWD_KEY = wifi_test_utils.WifiEnums.PWD_KEY |
| 172 | WIFI_CONFIG_APBAND_2G = wifi_test_utils.WifiEnums.WIFI_CONFIG_APBAND_2G |
| 173 | WIFI_CONFIG_APBAND_5G = wifi_test_utils.WifiEnums.WIFI_CONFIG_APBAND_5G |
Ang Li | 5bd83f3 | 2016-05-23 14:39:38 -0700 | [diff] [blame] | 174 | log = logging |
Betty Zhou | dd781f8 | 2017-08-08 14:46:23 -0700 | [diff] [blame] | 175 | STORY_LINE = "+19523521350" |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 176 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 177 | |
Betty Zhou | 7f45f55 | 2017-03-15 19:12:52 -0700 | [diff] [blame] | 178 | class _CallSequenceException(Exception): |
| 179 | pass |
| 180 | |
| 181 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 182 | class TelTestUtilsError(Exception): |
| 183 | pass |
| 184 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 185 | |
Betty Zhou | 5534e67 | 2017-03-07 13:47:10 -0800 | [diff] [blame] | 186 | def abort_all_tests(log, msg): |
| 187 | log.error("Aborting all ongoing tests due to: %s.", msg) |
| 188 | abort_all(msg) |
| 189 | |
| 190 | |
Betty Zhou | 5b069a0 | 2016-12-06 19:23:44 -0800 | [diff] [blame] | 191 | def get_phone_number_by_adb(ad): |
Betty Zhou | d2da7ba | 2017-03-24 12:54:34 -0700 | [diff] [blame] | 192 | return phone_number_formatter( |
| 193 | ad.adb.shell("service call iphonesubinfo 13")) |
Betty Zhou | 5b069a0 | 2016-12-06 19:23:44 -0800 | [diff] [blame] | 194 | |
| 195 | |
| 196 | def get_iccid_by_adb(ad): |
| 197 | return ad.adb.shell("service call iphonesubinfo 11") |
| 198 | |
| 199 | |
| 200 | def get_operator_by_adb(ad): |
| 201 | return ad.adb.getprop("gsm.sim.operator.alpha") |
| 202 | |
| 203 | |
Betty Zhou | 8ecd1da | 2017-10-05 15:44:24 -0700 | [diff] [blame] | 204 | def get_plmn_by_adb(ad): |
| 205 | return ad.adb.getprop("gsm.sim.operator.numeric") |
| 206 | |
| 207 | |
Betty Zhou | 5b069a0 | 2016-12-06 19:23:44 -0800 | [diff] [blame] | 208 | def get_sub_id_by_adb(ad): |
| 209 | return ad.adb.shell("service call iphonesubinfo 5") |
| 210 | |
| 211 | |
| 212 | def setup_droid_properties_by_adb(log, ad, sim_filename=None): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 213 | |
Betty Zhou | 5b069a0 | 2016-12-06 19:23:44 -0800 | [diff] [blame] | 214 | sim_data = None |
| 215 | if sim_filename: |
| 216 | try: |
| 217 | sim_data = load_config(sim_filename) |
| 218 | except Exception: |
Betty Zhou | ccd171d | 2017-02-13 15:11:33 -0800 | [diff] [blame] | 219 | log.warning("Failed to load %s!", sim_filename) |
Betty Zhou | 5b069a0 | 2016-12-06 19:23:44 -0800 | [diff] [blame] | 220 | |
Betty Zhou | 76a83f7 | 2016-12-20 14:21:11 -0800 | [diff] [blame] | 221 | sub_id = get_sub_id_by_adb(ad) |
Betty Zhou | 8ecd1da | 2017-10-05 15:44:24 -0700 | [diff] [blame] | 222 | iccid = get_iccid_by_adb(ad) |
| 223 | ad.log.info("iccid = %s", iccid) |
| 224 | if sim_data.get(iccid) and sim_data[iccid].get("phone_num"): |
Betty Zhou | d2da7ba | 2017-03-24 12:54:34 -0700 | [diff] [blame] | 225 | phone_number = phone_number_formatter(sim_data[iccid]["phone_num"]) |
| 226 | else: |
| 227 | phone_number = get_phone_number_by_adb(ad) |
Betty Zhou | ddb361d | 2017-09-07 17:07:20 -0700 | [diff] [blame] | 228 | if not phone_number and hasattr(ad, phone_number): |
| 229 | phone_number = ad.phone_number |
Betty Zhou | 5b069a0 | 2016-12-06 19:23:44 -0800 | [diff] [blame] | 230 | if not phone_number: |
Betty Zhou | 8ecd1da | 2017-10-05 15:44:24 -0700 | [diff] [blame] | 231 | ad.log.error("Failed to find valid phone number for %s", iccid) |
Betty Zhou | d4b75c5 | 2018-04-03 15:20:00 -0700 | [diff] [blame] | 232 | abort_all_tests(ad.log, "Failed to find valid phone number for %s") |
Betty Zhou | 3b2de07 | 2018-03-15 16:46:26 -0700 | [diff] [blame] | 233 | sub_record = { |
Betty Zhou | d2da7ba | 2017-03-24 12:54:34 -0700 | [diff] [blame] | 234 | 'phone_num': phone_number, |
Betty Zhou | a27e5d4 | 2017-01-17 11:33:04 -0800 | [diff] [blame] | 235 | 'iccid': get_iccid_by_adb(ad), |
Betty Zhou | 8ecd1da | 2017-10-05 15:44:24 -0700 | [diff] [blame] | 236 | 'sim_operator_name': get_operator_by_adb(ad), |
| 237 | 'operator': operator_name_from_plmn_id(get_plmn_by_adb(ad)) |
Betty Zhou | a27e5d4 | 2017-01-17 11:33:04 -0800 | [diff] [blame] | 238 | } |
Betty Zhou | 3b2de07 | 2018-03-15 16:46:26 -0700 | [diff] [blame] | 239 | device_props = {'subscription': {sub_id: sub_record}} |
| 240 | ad.log.info("subId %s SIM record: %s", sub_id, sub_record) |
| 241 | setattr(ad, 'telephony', device_props) |
Betty Zhou | 5b069a0 | 2016-12-06 19:23:44 -0800 | [diff] [blame] | 242 | |
| 243 | |
| 244 | def setup_droid_properties(log, ad, sim_filename=None): |
| 245 | |
Betty Zhou | 5b069a0 | 2016-12-06 19:23:44 -0800 | [diff] [blame] | 246 | if ad.skip_sl4a: |
Betty Zhou | a27e5d4 | 2017-01-17 11:33:04 -0800 | [diff] [blame] | 247 | return setup_droid_properties_by_adb( |
| 248 | log, ad, sim_filename=sim_filename) |
Betty Zhou | ccd171d | 2017-02-13 15:11:33 -0800 | [diff] [blame] | 249 | |
Betty Zhou | bbbc1cf | 2017-03-29 16:08:36 -0700 | [diff] [blame] | 250 | refresh_droid_config(log, ad) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 251 | device_props = {} |
| 252 | device_props['subscription'] = {} |
| 253 | |
Betty Zhou | bbbc1cf | 2017-03-29 16:08:36 -0700 | [diff] [blame] | 254 | sim_data = {} |
| 255 | if sim_filename: |
| 256 | try: |
| 257 | sim_data = load_config(sim_filename) |
| 258 | except Exception: |
| 259 | log.warning("Failed to load %s!", sim_filename) |
Betty Zhou | 3b2de07 | 2018-03-15 16:46:26 -0700 | [diff] [blame] | 260 | if not ad.telephony["subscription"]: |
Betty Zhou | 28e07e1 | 2018-04-11 12:12:11 -0700 | [diff] [blame] | 261 | abort_all_tests(ad.log, "No valid subscription") |
Betty Zhou | de88d17 | 2017-10-27 18:20:29 -0700 | [diff] [blame] | 262 | result = True |
Betty Zhou | e32dd3b | 2017-11-28 19:05:55 -0800 | [diff] [blame] | 263 | active_sub_id = get_outgoing_voice_sub_id(ad) |
Betty Zhou | 3b2de07 | 2018-03-15 16:46:26 -0700 | [diff] [blame] | 264 | for sub_id, sub_info in ad.telephony["subscription"].items(): |
Betty Zhou | d2da7ba | 2017-03-24 12:54:34 -0700 | [diff] [blame] | 265 | sub_info["operator"] = get_operator_name(log, ad, sub_id) |
| 266 | iccid = sub_info["iccid"] |
| 267 | if not iccid: |
Betty Zhou | 3b2de07 | 2018-03-15 16:46:26 -0700 | [diff] [blame] | 268 | ad.log.warning("Unable to find ICC-ID for subscriber %s", sub_id) |
Betty Zhou | d2da7ba | 2017-03-24 12:54:34 -0700 | [diff] [blame] | 269 | continue |
Betty Zhou | e32dd3b | 2017-11-28 19:05:55 -0800 | [diff] [blame] | 270 | if sub_info.get("phone_num"): |
Betty Zhou | 3b2de07 | 2018-03-15 16:46:26 -0700 | [diff] [blame] | 271 | if iccid in sim_data and sim_data[iccid].get("phone_num"): |
| 272 | if not check_phone_number_match(sim_data[iccid]["phone_num"], |
| 273 | sub_info["phone_num"]): |
Betty Zhou | e32dd3b | 2017-11-28 19:05:55 -0800 | [diff] [blame] | 274 | ad.log.warning( |
| 275 | "phone_num %s in sim card data file for iccid %s" |
Betty Zhou | 3b2de07 | 2018-03-15 16:46:26 -0700 | [diff] [blame] | 276 | " do not match phone_num %s from subscription", |
Betty Zhou | e32dd3b | 2017-11-28 19:05:55 -0800 | [diff] [blame] | 277 | sim_data[iccid]["phone_num"], iccid, |
| 278 | sub_info["phone_num"]) |
Betty Zhou | 3b2de07 | 2018-03-15 16:46:26 -0700 | [diff] [blame] | 279 | sub_info["phone_num"] = sim_data[iccid]["phone_num"] |
| 280 | else: |
| 281 | if iccid in sim_data and sim_data[iccid].get("phone_num"): |
| 282 | sub_info["phone_num"] = sim_data[iccid]["phone_num"] |
| 283 | elif sub_id == active_sub_id: |
| 284 | phone_number = get_phone_number_by_secret_code( |
| 285 | ad, sub_info["sim_operator_name"]) |
| 286 | if phone_number: |
| 287 | sub_info["phone_num"] = phone_number |
| 288 | elif getattr(ad, "phone_num", None): |
| 289 | sub_info["phone_num"] = ad.phone_number |
Betty Zhou | e32dd3b | 2017-11-28 19:05:55 -0800 | [diff] [blame] | 290 | if (not sub_info.get("phone_num")) and sub_id == active_sub_id: |
| 291 | ad.log.info("sub_id %s sub_info = %s", sub_id, sub_info) |
| 292 | ad.log.error( |
| 293 | "Unable to retrieve phone number for sub %s with iccid" |
| 294 | " %s from device or testbed config or sim card file %s", |
| 295 | sub_id, iccid, sim_filename) |
| 296 | result = False |
Betty Zhou | ee31105 | 2017-12-19 13:09:56 -0800 | [diff] [blame] | 297 | if not hasattr( |
| 298 | ad, 'roaming' |
Betty Zhou | 3b2de07 | 2018-03-15 16:46:26 -0700 | [diff] [blame] | 299 | ) and sub_info["sim_plmn"] != sub_info["network_plmn"] and sub_info["sim_operator_name"].strip( |
| 300 | ) not in sub_info["network_operator_name"].strip(): |
Betty Zhou | 2cf788e | 2017-06-27 17:25:53 -0700 | [diff] [blame] | 301 | ad.log.info("roaming is not enabled, enable it") |
Betty Zhou | bbbc1cf | 2017-03-29 16:08:36 -0700 | [diff] [blame] | 302 | setattr(ad, 'roaming', True) |
Betty Zhou | 70cc433 | 2017-09-20 15:06:13 -0700 | [diff] [blame] | 303 | ad.log.info("SubId %s info: %s", sub_id, sorted(sub_info.items())) |
Betty Zhou | 2cf788e | 2017-06-27 17:25:53 -0700 | [diff] [blame] | 304 | data_roaming = getattr(ad, 'roaming', False) |
| 305 | if get_cell_data_roaming_state_by_adb(ad) != data_roaming: |
| 306 | set_cell_data_roaming_state_by_adb(ad, data_roaming) |
Betty Zhou | 739d6b7 | 2018-04-18 10:38:53 -0700 | [diff] [blame] | 307 | # Setup VoWiFi MDN for Verizon. b/33187374 |
Betty Zhou | ddb361d | 2017-09-07 17:07:20 -0700 | [diff] [blame] | 308 | if not result: |
| 309 | abort_all_tests(ad.log, "Failed to find valid phone number") |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 310 | |
Betty Zhou | 3b2de07 | 2018-03-15 16:46:26 -0700 | [diff] [blame] | 311 | ad.log.debug("telephony = %s", ad.telephony) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 312 | |
Nathan Harold | 7642fc9 | 2016-05-02 18:29:11 -0700 | [diff] [blame] | 313 | |
Yang Liu | 6c70e02 | 2016-03-07 16:19:42 -0800 | [diff] [blame] | 314 | def refresh_droid_config(log, ad): |
Betty Zhou | 3b2de07 | 2018-03-15 16:46:26 -0700 | [diff] [blame] | 315 | """ Update Android Device telephony records for each sub_id. |
Yang Liu | ad9c63d | 2016-02-25 13:59:23 -0800 | [diff] [blame] | 316 | |
| 317 | Args: |
| 318 | log: log object |
| 319 | ad: android device object |
Yang Liu | ad9c63d | 2016-02-25 13:59:23 -0800 | [diff] [blame] | 320 | |
| 321 | Returns: |
Yang Liu | 6c70e02 | 2016-03-07 16:19:42 -0800 | [diff] [blame] | 322 | None |
Yang Liu | ad9c63d | 2016-02-25 13:59:23 -0800 | [diff] [blame] | 323 | """ |
Betty Zhou | 3b2de07 | 2018-03-15 16:46:26 -0700 | [diff] [blame] | 324 | if not getattr(ad, 'telephony', {}): |
| 325 | setattr(ad, 'telephony', {"subscription": {}}) |
Betty Zhou | d2da7ba | 2017-03-24 12:54:34 -0700 | [diff] [blame] | 326 | droid = ad.droid |
| 327 | sub_info_list = droid.subscriptionGetAllSubInfoList() |
Betty Zhou | 3b2de07 | 2018-03-15 16:46:26 -0700 | [diff] [blame] | 328 | active_sub_id = get_outgoing_voice_sub_id(ad) |
Betty Zhou | d2da7ba | 2017-03-24 12:54:34 -0700 | [diff] [blame] | 329 | for sub_info in sub_info_list: |
| 330 | sub_id = sub_info["subscriptionId"] |
| 331 | sim_slot = sub_info["simSlotIndex"] |
| 332 | if sim_slot != INVALID_SIM_SLOT_INDEX: |
Betty Zhou | 3b2de07 | 2018-03-15 16:46:26 -0700 | [diff] [blame] | 333 | if sub_id not in ad.telephony["subscription"]: |
| 334 | ad.telephony["subscription"][sub_id] = {} |
| 335 | sub_record = ad.telephony["subscription"][sub_id] |
Betty Zhou | e32dd3b | 2017-11-28 19:05:55 -0800 | [diff] [blame] | 336 | if sub_info.get("iccId"): |
Betty Zhou | 3b2de07 | 2018-03-15 16:46:26 -0700 | [diff] [blame] | 337 | sub_record["iccid"] = sub_info["iccId"] |
Betty Zhou | e32dd3b | 2017-11-28 19:05:55 -0800 | [diff] [blame] | 338 | else: |
Betty Zhou | 3b2de07 | 2018-03-15 16:46:26 -0700 | [diff] [blame] | 339 | sub_record[ |
Betty Zhou | e32dd3b | 2017-11-28 19:05:55 -0800 | [diff] [blame] | 340 | "iccid"] = droid.telephonyGetSimSerialNumberForSubscription( |
| 341 | sub_id) |
Betty Zhou | 3b2de07 | 2018-03-15 16:46:26 -0700 | [diff] [blame] | 342 | sub_record["sim_slot"] = sim_slot |
| 343 | if sub_info.get("mcc"): |
| 344 | sub_record["mcc"] = sub_info["mcc"] |
| 345 | if sub_info.get("mnc"): |
| 346 | sub_record["mnc"] = sub_info["mnc"] |
| 347 | if sub_info.get("displayName"): |
| 348 | sub_record["display_name"] = sub_info["displayName"] |
Betty Zhou | d2da7ba | 2017-03-24 12:54:34 -0700 | [diff] [blame] | 349 | try: |
Betty Zhou | 3b2de07 | 2018-03-15 16:46:26 -0700 | [diff] [blame] | 350 | sub_record[ |
Betty Zhou | d2da7ba | 2017-03-24 12:54:34 -0700 | [diff] [blame] | 351 | "phone_type"] = droid.telephonyGetPhoneTypeForSubscription( |
| 352 | sub_id) |
| 353 | except: |
Betty Zhou | 3b2de07 | 2018-03-15 16:46:26 -0700 | [diff] [blame] | 354 | if not sub_record.get("phone_type"): |
| 355 | sub_record["phone_type"] = droid.telephonyGetPhoneType() |
| 356 | sub_record[ |
Betty Zhou | d2da7ba | 2017-03-24 12:54:34 -0700 | [diff] [blame] | 357 | "sim_plmn"] = droid.telephonyGetSimOperatorForSubscription( |
| 358 | sub_id) |
Betty Zhou | 3b2de07 | 2018-03-15 16:46:26 -0700 | [diff] [blame] | 359 | sub_record[ |
Betty Zhou | d2da7ba | 2017-03-24 12:54:34 -0700 | [diff] [blame] | 360 | "sim_operator_name"] = droid.telephonyGetSimOperatorNameForSubscription( |
| 361 | sub_id) |
Betty Zhou | 3b2de07 | 2018-03-15 16:46:26 -0700 | [diff] [blame] | 362 | sub_record[ |
Betty Zhou | d2da7ba | 2017-03-24 12:54:34 -0700 | [diff] [blame] | 363 | "network_plmn"] = droid.telephonyGetNetworkOperatorForSubscription( |
| 364 | sub_id) |
Betty Zhou | 3b2de07 | 2018-03-15 16:46:26 -0700 | [diff] [blame] | 365 | sub_record[ |
Betty Zhou | d2da7ba | 2017-03-24 12:54:34 -0700 | [diff] [blame] | 366 | "network_operator_name"] = droid.telephonyGetNetworkOperatorNameForSubscription( |
| 367 | sub_id) |
Betty Zhou | 3b2de07 | 2018-03-15 16:46:26 -0700 | [diff] [blame] | 368 | sub_record[ |
Betty Zhou | d2da7ba | 2017-03-24 12:54:34 -0700 | [diff] [blame] | 369 | "sim_country"] = droid.telephonyGetSimCountryIsoForSubscription( |
| 370 | sub_id) |
Betty Zhou | 3b2de07 | 2018-03-15 16:46:26 -0700 | [diff] [blame] | 371 | if active_sub_id == sub_id: |
| 372 | try: |
| 373 | sub_record[ |
| 374 | "carrier_id"] = ad.droid.telephonyGetSimCarrierId() |
| 375 | sub_record[ |
| 376 | "carrier_id_name"] = ad.droid.telephonyGetSimCarrierIdName( |
| 377 | ) |
| 378 | except: |
| 379 | ad.log.info("Carrier ID is not supported") |
| 380 | if not sub_info.get("number"): |
| 381 | sub_info[ |
| 382 | "number"] = droid.telephonyGetLine1NumberForSubscription( |
| 383 | sub_id) |
Betty Zhou | e32dd3b | 2017-11-28 19:05:55 -0800 | [diff] [blame] | 384 | if sub_info.get("number"): |
Betty Zhou | 3b2de07 | 2018-03-15 16:46:26 -0700 | [diff] [blame] | 385 | if sub_record.get("phone_num"): |
| 386 | # Use the phone number provided in sim info file by default |
| 387 | # as the sub_info["number"] may not be formatted in a |
| 388 | # dialable number |
| 389 | if not check_phone_number_match(sub_info["number"], |
| 390 | sub_record["phone_num"]): |
| 391 | ad.log.info( |
| 392 | "Subscriber phone number changed from %s to %s", |
| 393 | sub_record["phone_num"], sub_info["number"]) |
| 394 | sub_record["phone_num"] = sub_info["number"] |
| 395 | else: |
| 396 | sub_record["phone_num"] = phone_number_formatter( |
| 397 | sub_info["number"]) |
| 398 | #ad.telephony['subscription'][sub_id] = sub_record |
| 399 | ad.log.info("SubId %s info: %s", sub_id, sorted( |
| 400 | sub_record.items())) |
Nathan Harold | 7642fc9 | 2016-05-02 18:29:11 -0700 | [diff] [blame] | 401 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 402 | |
Betty Zhou | ddb361d | 2017-09-07 17:07:20 -0700 | [diff] [blame] | 403 | def get_phone_number_by_secret_code(ad, operator): |
| 404 | if "T-Mobile" in operator: |
| 405 | ad.droid.telecomDialNumber("#686#") |
| 406 | ad.send_keycode("ENTER") |
Betty Zhou | b406237 | 2017-09-08 11:29:38 -0700 | [diff] [blame] | 407 | for _ in range(12): |
| 408 | output = ad.search_logcat("mobile number") |
| 409 | if output: |
| 410 | result = re.findall(r"mobile number is (\S+)", |
| 411 | output[-1]["log_message"]) |
Betty Zhou | 70cc433 | 2017-09-20 15:06:13 -0700 | [diff] [blame] | 412 | ad.send_keycode("BACK") |
Betty Zhou | b406237 | 2017-09-08 11:29:38 -0700 | [diff] [blame] | 413 | return result[0] |
| 414 | else: |
| 415 | time.sleep(5) |
| 416 | return "" |
Betty Zhou | ddb361d | 2017-09-07 17:07:20 -0700 | [diff] [blame] | 417 | |
| 418 | |
Betty Zhou | dd9a9ea | 2018-04-04 13:23:56 -0700 | [diff] [blame] | 419 | def get_user_config_profile(ad): |
| 420 | return { |
| 421 | "WFC Mode": |
| 422 | ad.droid.imsGetWfcMode(), |
| 423 | "WFC Enabled": |
| 424 | is_wfc_enabled(ad.log, ad), |
| 425 | "Airplane Mode": |
| 426 | ad.droid.connectivityCheckAirplaneMode(), |
| 427 | "Platform Enhanced 4G LTE Mode": |
| 428 | ad.droid.imsIsEnhanced4gLteModeSettingEnabledByPlatform(), |
| 429 | "User Enhanced 4G LTE Mode": |
| 430 | ad.droid.imsIsEnhanced4gLteModeSettingEnabledByUser(), |
| 431 | "VoLTE Enabled": |
| 432 | ad.droid.telephonyIsVolteAvailable() |
| 433 | } |
| 434 | |
| 435 | |
Yang Liu | 7ce1998 | 2016-03-09 12:20:41 -0800 | [diff] [blame] | 436 | def get_slot_index_from_subid(log, ad, sub_id): |
| 437 | try: |
| 438 | info = ad.droid.subscriptionGetSubInfoForSubscriber(sub_id) |
| 439 | return info['simSlotIndex'] |
tturney | 10eda2e | 2016-03-30 19:55:57 -0700 | [diff] [blame] | 440 | except KeyError: |
Yang Liu | 7ce1998 | 2016-03-09 12:20:41 -0800 | [diff] [blame] | 441 | return INVALID_SIM_SLOT_INDEX |
| 442 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 443 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 444 | def get_num_active_sims(log, ad): |
| 445 | """ Get the number of active SIM cards by counting slots |
| 446 | |
| 447 | Args: |
| 448 | ad: android_device object. |
| 449 | |
| 450 | Returns: |
| 451 | result: The number of loaded (physical) SIM cards |
| 452 | """ |
| 453 | # using a dictionary as a cheap way to prevent double counting |
| 454 | # in the situation where multiple subscriptions are on the same SIM. |
| 455 | # yes, this is a corner corner case. |
| 456 | valid_sims = {} |
| 457 | subInfo = ad.droid.subscriptionGetAllSubInfoList() |
| 458 | for info in subInfo: |
| 459 | ssidx = info['simSlotIndex'] |
| 460 | if ssidx == INVALID_SIM_SLOT_INDEX: |
| 461 | continue |
| 462 | valid_sims[ssidx] = True |
| 463 | return len(valid_sims.keys()) |
| 464 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 465 | |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 466 | def toggle_airplane_mode_by_adb(log, ad, new_state=None): |
Betty Zhou | 5b069a0 | 2016-12-06 19:23:44 -0800 | [diff] [blame] | 467 | """ Toggle the state of airplane mode. |
| 468 | |
| 469 | Args: |
| 470 | log: log handler. |
| 471 | ad: android_device object. |
| 472 | new_state: Airplane mode state to set to. |
| 473 | If None, opposite of the current state. |
| 474 | strict_checking: Whether to turn on strict checking that checks all features. |
| 475 | |
| 476 | Returns: |
| 477 | result: True if operation succeed. False if error happens. |
| 478 | """ |
| 479 | cur_state = bool(int(ad.adb.shell("settings get global airplane_mode_on"))) |
| 480 | if new_state == cur_state: |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 481 | ad.log.info("Airplane mode already in %s", new_state) |
Betty Zhou | 5b069a0 | 2016-12-06 19:23:44 -0800 | [diff] [blame] | 482 | return True |
| 483 | elif new_state is None: |
| 484 | new_state = not cur_state |
| 485 | |
| 486 | ad.adb.shell("settings put global airplane_mode_on %s" % int(new_state)) |
| 487 | ad.adb.shell("am broadcast -a android.intent.action.AIRPLANE_MODE") |
| 488 | return True |
| 489 | |
| 490 | |
Betty Zhou | 2873296 | 2016-11-28 15:00:58 -0800 | [diff] [blame] | 491 | def toggle_airplane_mode(log, ad, new_state=None, strict_checking=True): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 492 | """ Toggle the state of airplane mode. |
| 493 | |
| 494 | Args: |
Betty Zhou | 2873296 | 2016-11-28 15:00:58 -0800 | [diff] [blame] | 495 | log: log handler. |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 496 | ad: android_device object. |
| 497 | new_state: Airplane mode state to set to. |
| 498 | If None, opposite of the current state. |
Betty Zhou | 2873296 | 2016-11-28 15:00:58 -0800 | [diff] [blame] | 499 | strict_checking: Whether to turn on strict checking that checks all features. |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 500 | |
| 501 | Returns: |
| 502 | result: True if operation succeed. False if error happens. |
| 503 | """ |
Betty Zhou | 5b069a0 | 2016-12-06 19:23:44 -0800 | [diff] [blame] | 504 | if ad.skip_sl4a: |
| 505 | return toggle_airplane_mode_by_adb(log, ad, new_state) |
| 506 | else: |
Betty Zhou | a27e5d4 | 2017-01-17 11:33:04 -0800 | [diff] [blame] | 507 | return toggle_airplane_mode_msim( |
| 508 | log, ad, new_state, strict_checking=strict_checking) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 509 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 510 | |
Betty Zhou | 17dcabb | 2017-05-25 19:14:58 -0700 | [diff] [blame] | 511 | def get_telephony_signal_strength(ad): |
Betty Zhou | 17dcabb | 2017-05-25 19:14:58 -0700 | [diff] [blame] | 512 | #{'evdoEcio': -1, 'asuLevel': 28, 'lteSignalStrength': 14, 'gsmLevel': 0, |
| 513 | # 'cdmaAsuLevel': 99, 'evdoDbm': -120, 'gsmDbm': -1, 'cdmaEcio': -160, |
| 514 | # 'level': 2, 'lteLevel': 2, 'cdmaDbm': -120, 'dbm': -112, 'cdmaLevel': 0, |
| 515 | # 'lteAsuLevel': 28, 'gsmAsuLevel': 99, 'gsmBitErrorRate': 0, |
| 516 | # 'lteDbm': -112, 'gsmSignalStrength': 99} |
Betty Zhou | f7da39e | 2018-04-16 16:28:58 -0700 | [diff] [blame] | 517 | try: |
| 518 | signal_strength = ad.droid.telephonyGetSignalStrength() |
| 519 | if not signal_strength: |
| 520 | signal_strength = {} |
| 521 | except Exception as e: |
| 522 | ad.log.error(e) |
| 523 | signal_strength = {} |
Betty Zhou | 17dcabb | 2017-05-25 19:14:58 -0700 | [diff] [blame] | 524 | out = ad.adb.shell("dumpsys telephony.registry | grep -i signalstrength") |
| 525 | signals = re.findall(r"(-*\d+)", out) |
| 526 | for i, val in enumerate( |
| 527 | ("gsmSignalStrength", "gsmBitErrorRate", "cdmaDbm", "cdmaEcio", |
| 528 | "evdoDbm", "evdoEcio", "evdoSnr", "lteSignalStrength", "lteRsrp", |
| 529 | "lteRsrq", "lteRssnr", "lteCqi", "lteRsrpBoost")): |
| 530 | signal_strength[val] = signal_strength.get(val, int(signals[i])) |
| 531 | ad.log.info("Telephony Signal strength = %s", signal_strength) |
| 532 | return signal_strength |
| 533 | |
| 534 | |
Jaineel | e64be82 | 2017-11-30 16:36:36 -0800 | [diff] [blame] | 535 | def get_wifi_signal_strength(ad): |
| 536 | signal_strength = ad.droid.wifiGetConnectionInfo()['rssi'] |
| 537 | ad.log.info("WiFi Signal Strength is %s" % signal_strength) |
| 538 | return signal_strength |
| 539 | |
| 540 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 541 | def is_expected_event(event_to_check, events_list): |
| 542 | """ check whether event is present in the event list |
| 543 | |
| 544 | Args: |
| 545 | event_to_check: event to be checked. |
| 546 | events_list: list of events |
| 547 | Returns: |
| 548 | result: True if event present in the list. False if not. |
| 549 | """ |
| 550 | for event in events_list: |
| 551 | if event in event_to_check['name']: |
| 552 | return True |
| 553 | return False |
| 554 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 555 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 556 | def is_sim_ready(log, ad, sim_slot_id=None): |
| 557 | """ check whether SIM is ready. |
| 558 | |
| 559 | Args: |
| 560 | ad: android_device object. |
| 561 | sim_slot_id: check the SIM status for sim_slot_id |
| 562 | This is optional. If this is None, check default SIM. |
| 563 | |
| 564 | Returns: |
| 565 | result: True if all SIMs are ready. False if not. |
| 566 | """ |
| 567 | if sim_slot_id is None: |
Yang Liu | aed3eef | 2015-12-15 18:40:25 -0800 | [diff] [blame] | 568 | status = ad.droid.telephonyGetSimState() |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 569 | else: |
Yang Liu | aed3eef | 2015-12-15 18:40:25 -0800 | [diff] [blame] | 570 | status = ad.droid.telephonyGetSimStateForSlotId(sim_slot_id) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 571 | if status != SIM_STATE_READY: |
Betty Zhou | 5c7dbd1 | 2018-03-13 18:27:44 -0700 | [diff] [blame] | 572 | ad.log.info("Sim state is %s, not ready", status) |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 573 | return False |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 574 | return True |
| 575 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 576 | |
Betty Zhou | 688c103 | 2017-11-20 20:08:04 -0800 | [diff] [blame] | 577 | def is_sim_ready_by_adb(log, ad): |
Betty Zhou | e66efb4 | 2018-01-31 19:45:56 -0800 | [diff] [blame] | 578 | state = ad.adb.getprop("gsm.sim.state") |
Betty Zhou | 28e07e1 | 2018-04-11 12:12:11 -0700 | [diff] [blame] | 579 | ad.log.info("gsm.sim.state = %s", state) |
Betty Zhou | e66efb4 | 2018-01-31 19:45:56 -0800 | [diff] [blame] | 580 | return state == SIM_STATE_READY or state == SIM_STATE_LOADED |
Betty Zhou | 688c103 | 2017-11-20 20:08:04 -0800 | [diff] [blame] | 581 | |
| 582 | |
| 583 | def wait_for_sim_ready_by_adb(log, ad, wait_time=90): |
| 584 | return _wait_for_droid_in_state(log, ad, wait_time, is_sim_ready_by_adb) |
| 585 | |
| 586 | |
| 587 | def get_service_state_by_adb(log, ad): |
| 588 | output = ad.adb.shell("dumpsys telephony.registry | grep mServiceState") |
| 589 | if "mVoiceRegState" in output: |
| 590 | result = re.search(r"mVoiceRegState=(\S+)\((\S+)\)", output) |
| 591 | if result: |
Betty Zhou | ee31105 | 2017-12-19 13:09:56 -0800 | [diff] [blame] | 592 | ad.log.info("mVoiceRegState is %s %s", result.group(1), |
| 593 | result.group(2)) |
Betty Zhou | 688c103 | 2017-11-20 20:08:04 -0800 | [diff] [blame] | 594 | return result.group(2) |
| 595 | else: |
| 596 | result = re.search(r"mServiceState=(\S+)", output) |
| 597 | if result: |
Betty Zhou | ee31105 | 2017-12-19 13:09:56 -0800 | [diff] [blame] | 598 | ad.log.info("mServiceState=%s %s", result.group(1), |
Betty Zhou | 688c103 | 2017-11-20 20:08:04 -0800 | [diff] [blame] | 599 | SERVICE_STATE_MAPPING[result.group(1)]) |
| 600 | return SERVICE_STATE_MAPPING[result.group(1)] |
| 601 | |
| 602 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 603 | def _is_expecting_event(event_recv_list): |
| 604 | """ check for more event is expected in event list |
| 605 | |
| 606 | Args: |
| 607 | event_recv_list: list of events |
| 608 | Returns: |
| 609 | result: True if more events are expected. False if not. |
| 610 | """ |
| 611 | for state in event_recv_list: |
| 612 | if state is False: |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 613 | return True |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 614 | return False |
| 615 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 616 | |
| 617 | 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] | 618 | """ set received event in expected event list |
| 619 | |
| 620 | Args: |
| 621 | event_recv_list: list of received events |
| 622 | sub_id_list: subscription ID list |
| 623 | sub_id: subscription id of current event |
| 624 | value: True or False |
| 625 | Returns: |
| 626 | None. |
| 627 | """ |
| 628 | for i in range(len(sub_id_list)): |
| 629 | if sub_id_list[i] == sub_id: |
| 630 | event_recv_list[i] = value |
| 631 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 632 | |
Nathan Harold | 54129bb | 2016-09-29 19:40:52 -0700 | [diff] [blame] | 633 | def _wait_for_bluetooth_in_state(log, ad, state, max_wait): |
| 634 | # FIXME: These event names should be defined in a common location |
| 635 | _BLUETOOTH_STATE_ON_EVENT = 'BluetoothStateChangedOn' |
| 636 | _BLUETOOTH_STATE_OFF_EVENT = 'BluetoothStateChangedOff' |
Nathan Harold | 9b1b2f1 | 2016-10-14 12:42:57 -0700 | [diff] [blame] | 637 | ad.ed.clear_events(_BLUETOOTH_STATE_ON_EVENT) |
| 638 | ad.ed.clear_events(_BLUETOOTH_STATE_OFF_EVENT) |
| 639 | |
Nathan Harold | 54129bb | 2016-09-29 19:40:52 -0700 | [diff] [blame] | 640 | ad.droid.bluetoothStartListeningForAdapterStateChange() |
| 641 | try: |
| 642 | bt_state = ad.droid.bluetoothCheckState() |
| 643 | if bt_state == state: |
| 644 | return True |
| 645 | if max_wait <= 0: |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 646 | ad.log.error("Time out: bluetooth state still %s, expecting %s", |
| 647 | bt_state, state) |
Nathan Harold | 54129bb | 2016-09-29 19:40:52 -0700 | [diff] [blame] | 648 | return False |
| 649 | |
Betty Zhou | a27e5d4 | 2017-01-17 11:33:04 -0800 | [diff] [blame] | 650 | event = { |
| 651 | False: _BLUETOOTH_STATE_OFF_EVENT, |
| 652 | True: _BLUETOOTH_STATE_ON_EVENT |
| 653 | }[state] |
Betty Zhou | 5c7dbd1 | 2018-03-13 18:27:44 -0700 | [diff] [blame] | 654 | event = ad.ed.pop_event(event, max_wait) |
| 655 | ad.log.info("Got event %s", event['name']) |
Nathan Harold | 54129bb | 2016-09-29 19:40:52 -0700 | [diff] [blame] | 656 | return True |
| 657 | except Empty: |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 658 | ad.log.error("Time out: bluetooth state still in %s, expecting %s", |
| 659 | bt_state, state) |
Nathan Harold | 54129bb | 2016-09-29 19:40:52 -0700 | [diff] [blame] | 660 | return False |
| 661 | finally: |
| 662 | ad.droid.bluetoothStopListeningForAdapterStateChange() |
| 663 | |
| 664 | |
| 665 | # TODO: replace this with an event-based function |
| 666 | def _wait_for_wifi_in_state(log, ad, state, max_wait): |
| 667 | return _wait_for_droid_in_state(log, ad, max_wait, |
| 668 | lambda log, ad, state: \ |
| 669 | (True if ad.droid.wifiCheckState() == state else False), |
| 670 | state) |
| 671 | |
| 672 | |
Betty Zhou | 2873296 | 2016-11-28 15:00:58 -0800 | [diff] [blame] | 673 | def toggle_airplane_mode_msim(log, ad, new_state=None, strict_checking=True): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 674 | """ Toggle the state of airplane mode. |
| 675 | |
| 676 | Args: |
Betty Zhou | 2873296 | 2016-11-28 15:00:58 -0800 | [diff] [blame] | 677 | log: log handler. |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 678 | ad: android_device object. |
| 679 | new_state: Airplane mode state to set to. |
| 680 | If None, opposite of the current state. |
Betty Zhou | 2873296 | 2016-11-28 15:00:58 -0800 | [diff] [blame] | 681 | strict_checking: Whether to turn on strict checking that checks all features. |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 682 | |
| 683 | Returns: |
| 684 | result: True if operation succeed. False if error happens. |
| 685 | """ |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 686 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 687 | cur_state = ad.droid.connectivityCheckAirplaneMode() |
| 688 | if cur_state == new_state: |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 689 | ad.log.info("Airplane mode already in %s", new_state) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 690 | return True |
| 691 | elif new_state is None: |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 692 | new_state = not cur_state |
Betty Zhou | b66d48f | 2018-04-09 12:00:53 -0700 | [diff] [blame] | 693 | ad.log.info("Toggle APM mode, from current tate %s to %s", cur_state, |
| 694 | new_state) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 695 | |
Betty Zhou | e123c67 | 2018-03-21 19:57:11 -0700 | [diff] [blame] | 696 | sub_id_list = [] |
| 697 | active_sub_info = ad.droid.subscriptionGetAllSubInfoList() |
| 698 | for info in active_sub_info: |
| 699 | sub_id_list.append(info['subscriptionId']) |
| 700 | |
| 701 | ad.ed.clear_all_events() |
| 702 | time.sleep(0.1) |
Yang Liu | 8e6adff | 2016-02-05 10:24:04 -0800 | [diff] [blame] | 703 | service_state_list = [] |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 704 | if new_state: |
Yang Liu | 8e6adff | 2016-02-05 10:24:04 -0800 | [diff] [blame] | 705 | service_state_list.append(SERVICE_STATE_POWER_OFF) |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 706 | ad.log.info("Turn on airplane mode") |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 707 | |
| 708 | else: |
| 709 | # If either one of these 3 events show up, it should be OK. |
| 710 | # Normal SIM, phone in service |
Yang Liu | 8e6adff | 2016-02-05 10:24:04 -0800 | [diff] [blame] | 711 | service_state_list.append(SERVICE_STATE_IN_SERVICE) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 712 | # NO SIM, or Dead SIM, or no Roaming coverage. |
Yang Liu | 8e6adff | 2016-02-05 10:24:04 -0800 | [diff] [blame] | 713 | service_state_list.append(SERVICE_STATE_OUT_OF_SERVICE) |
| 714 | service_state_list.append(SERVICE_STATE_EMERGENCY_ONLY) |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 715 | ad.log.info("Turn off airplane mode") |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 716 | |
| 717 | for sub_id in sub_id_list: |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 718 | ad.droid.telephonyStartTrackingServiceStateChangeForSubscription( |
| 719 | sub_id) |
Nathan Harold | 54129bb | 2016-09-29 19:40:52 -0700 | [diff] [blame] | 720 | |
| 721 | timeout_time = time.time() + MAX_WAIT_TIME_AIRPLANEMODE_EVENT |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 722 | ad.droid.connectivityToggleAirplaneMode(new_state) |
| 723 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 724 | try: |
| 725 | try: |
Nathan Harold | 7642fc9 | 2016-05-02 18:29:11 -0700 | [diff] [blame] | 726 | event = ad.ed.wait_for_event( |
| 727 | EventServiceStateChanged, |
| 728 | is_event_match_for_list, |
| 729 | timeout=MAX_WAIT_TIME_AIRPLANEMODE_EVENT, |
| 730 | field=ServiceStateContainer.SERVICE_STATE, |
| 731 | value_list=service_state_list) |
Betty Zhou | 5c7dbd1 | 2018-03-13 18:27:44 -0700 | [diff] [blame] | 732 | ad.log.info("Got event %s", event) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 733 | except Empty: |
Betty Zhou | dd9a9ea | 2018-04-04 13:23:56 -0700 | [diff] [blame] | 734 | ad.log.warning("Did not get expected service state change to %s", |
| 735 | service_state_list) |
Betty Zhou | a36c435 | 2018-04-05 18:49:32 -0700 | [diff] [blame] | 736 | finally: |
| 737 | for sub_id in sub_id_list: |
| 738 | ad.droid.telephonyStopTrackingServiceStateChangeForSubscription( |
| 739 | sub_id) |
| 740 | except Exception as e: |
| 741 | ad.log.error(e) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 742 | |
Nathan Harold | 54129bb | 2016-09-29 19:40:52 -0700 | [diff] [blame] | 743 | # APM on (new_state=True) will turn off bluetooth but may not turn it on |
Betty Zhou | e1cf85f | 2016-11-18 19:18:17 -0800 | [diff] [blame] | 744 | try: |
| 745 | if new_state and not _wait_for_bluetooth_in_state( |
Betty Zhou | a27e5d4 | 2017-01-17 11:33:04 -0800 | [diff] [blame] | 746 | log, ad, False, timeout_time - time.time()): |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 747 | ad.log.error( |
| 748 | "Failed waiting for bluetooth during airplane mode toggle") |
Betty Zhou | 2873296 | 2016-11-28 15:00:58 -0800 | [diff] [blame] | 749 | if strict_checking: return False |
Betty Zhou | e1cf85f | 2016-11-18 19:18:17 -0800 | [diff] [blame] | 750 | except Exception as e: |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 751 | ad.log.error("Failed to check bluetooth state due to %s", e) |
Betty Zhou | 2873296 | 2016-11-28 15:00:58 -0800 | [diff] [blame] | 752 | if strict_checking: |
| 753 | raise |
Nathan Harold | 54129bb | 2016-09-29 19:40:52 -0700 | [diff] [blame] | 754 | |
| 755 | # APM on (new_state=True) will turn off wifi but may not turn it on |
Nathan Harold | 450a768 | 2016-10-12 12:57:17 -0700 | [diff] [blame] | 756 | if new_state and not _wait_for_wifi_in_state(log, ad, False, |
| 757 | timeout_time - time.time()): |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 758 | ad.log.error("Failed waiting for wifi during airplane mode toggle on") |
| 759 | if strict_checking: return False |
Nathan Harold | 54129bb | 2016-09-29 19:40:52 -0700 | [diff] [blame] | 760 | |
| 761 | if ad.droid.connectivityCheckAirplaneMode() != new_state: |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 762 | ad.log.error("Set airplane mode to %s failed", new_state) |
Nathan Harold | 54129bb | 2016-09-29 19:40:52 -0700 | [diff] [blame] | 763 | return False |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 764 | return True |
| 765 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 766 | |
| 767 | def wait_and_answer_call(log, |
| 768 | ad, |
| 769 | incoming_number=None, |
Betty Zhou | d2da7ba | 2017-03-24 12:54:34 -0700 | [diff] [blame] | 770 | incall_ui_display=INCALL_UI_DISPLAY_FOREGROUND, |
| 771 | caller=None): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 772 | """Wait for an incoming call on default voice subscription and |
| 773 | accepts the call. |
| 774 | |
| 775 | Args: |
| 776 | ad: android device object. |
| 777 | incoming_number: Expected incoming number. |
| 778 | Optional. Default is None |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 779 | incall_ui_display: after answer the call, bring in-call UI to foreground or |
| 780 | background. Optional, default value is INCALL_UI_DISPLAY_FOREGROUND. |
| 781 | if = INCALL_UI_DISPLAY_FOREGROUND, bring in-call UI to foreground. |
| 782 | if = INCALL_UI_DISPLAY_BACKGROUND, bring in-call UI to background. |
| 783 | else, do nothing. |
| 784 | |
| 785 | Returns: |
| 786 | True: if incoming call is received and answered successfully. |
| 787 | False: for errors |
| 788 | """ |
| 789 | return wait_and_answer_call_for_subscription( |
Betty Zhou | d2da7ba | 2017-03-24 12:54:34 -0700 | [diff] [blame] | 790 | log, |
| 791 | ad, |
| 792 | get_incoming_voice_sub_id(ad), |
| 793 | incoming_number, |
| 794 | incall_ui_display=incall_ui_display, |
| 795 | caller=caller) |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 796 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 797 | |
| 798 | def wait_for_ringing_event(log, ad, wait_time): |
Nathan Harold | 54c7c74 | 2016-08-04 19:40:44 -0700 | [diff] [blame] | 799 | log.warning("***DEPRECATED*** wait_for_ringing_event()") |
| 800 | return _wait_for_ringing_event(log, ad, wait_time) |
| 801 | |
| 802 | |
| 803 | def _wait_for_ringing_event(log, ad, wait_time): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 804 | """Wait for ringing event. |
| 805 | |
| 806 | Args: |
| 807 | log: log object. |
| 808 | ad: android device object. |
| 809 | wait_time: max time to wait for ringing event. |
| 810 | |
| 811 | Returns: |
| 812 | event_ringing if received ringing event. |
| 813 | otherwise return None. |
| 814 | """ |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 815 | event_ringing = None |
| 816 | |
Nathan Harold | 0111e20 | 2016-09-27 17:03:00 -0700 | [diff] [blame] | 817 | try: |
| 818 | event_ringing = ad.ed.wait_for_event( |
| 819 | EventCallStateChanged, |
| 820 | is_event_match, |
| 821 | timeout=wait_time, |
| 822 | field=CallStateContainer.CALL_STATE, |
| 823 | value=TELEPHONY_STATE_RINGING) |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 824 | ad.log.info("Receive ringing event") |
Nathan Harold | 0111e20 | 2016-09-27 17:03:00 -0700 | [diff] [blame] | 825 | except Empty: |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 826 | ad.log.info("No Ringing Event") |
Nathan Harold | 0111e20 | 2016-09-27 17:03:00 -0700 | [diff] [blame] | 827 | finally: |
| 828 | return event_ringing |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 829 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 830 | |
Nathan Harold | 54c7c74 | 2016-08-04 19:40:44 -0700 | [diff] [blame] | 831 | def wait_for_ringing_call(log, ad, incoming_number=None): |
| 832 | """Wait for an incoming call on default voice subscription and |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 833 | accepts the call. |
| 834 | |
| 835 | Args: |
Nathan Harold | 54c7c74 | 2016-08-04 19:40:44 -0700 | [diff] [blame] | 836 | log: log object. |
| 837 | ad: android device object. |
| 838 | incoming_number: Expected incoming number. |
| 839 | Optional. Default is None |
| 840 | |
| 841 | Returns: |
| 842 | True: if incoming call is received and answered successfully. |
| 843 | False: for errors |
| 844 | """ |
| 845 | return wait_for_ringing_call_for_subscription( |
| 846 | log, ad, get_incoming_voice_sub_id(ad), incoming_number) |
| 847 | |
| 848 | |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 849 | def wait_for_ringing_call_for_subscription( |
| 850 | log, |
| 851 | ad, |
| 852 | sub_id, |
| 853 | incoming_number=None, |
Betty Zhou | 7f45f55 | 2017-03-15 19:12:52 -0700 | [diff] [blame] | 854 | caller=None, |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 855 | event_tracking_started=False, |
Betty Zhou | bcffe44 | 2017-07-05 17:27:55 -0700 | [diff] [blame] | 856 | timeout=MAX_WAIT_TIME_CALLEE_RINGING, |
| 857 | retries=1): |
Nathan Harold | 54c7c74 | 2016-08-04 19:40:44 -0700 | [diff] [blame] | 858 | """Wait for an incoming call on specified subscription. |
| 859 | |
| 860 | Args: |
| 861 | log: log object. |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 862 | ad: android device object. |
| 863 | sub_id: subscription ID |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 864 | incoming_number: Expected incoming number. Default is None |
| 865 | event_tracking_started: True if event tracking already state outside |
| 866 | timeout: time to wait for ring |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 867 | |
| 868 | Returns: |
| 869 | True: if incoming call is received and answered successfully. |
| 870 | False: for errors |
| 871 | """ |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 872 | if not event_tracking_started: |
Betty Zhou | 287f9df | 2018-01-23 10:57:55 -0800 | [diff] [blame] | 873 | ad.ed.clear_events(EventCallStateChanged) |
Nathan Harold | 54c7c74 | 2016-08-04 19:40:44 -0700 | [diff] [blame] | 874 | ad.droid.telephonyStartTrackingCallStateForSubscription(sub_id) |
Betty Zhou | bcffe44 | 2017-07-05 17:27:55 -0700 | [diff] [blame] | 875 | event_ringing = None |
| 876 | for i in range(retries): |
| 877 | event_ringing = _wait_for_ringing_event(log, ad, timeout) |
| 878 | if event_ringing: |
| 879 | ad.log.info("callee received ring event") |
| 880 | break |
| 881 | if ad.droid.telephonyGetCallStateForSubscription( |
| 882 | sub_id |
| 883 | ) == TELEPHONY_STATE_RINGING or ad.droid.telecomIsRinging(): |
| 884 | ad.log.info("callee in ringing state") |
| 885 | break |
| 886 | if i == retries - 1: |
Betty Zhou | e32dd3b | 2017-11-28 19:05:55 -0800 | [diff] [blame] | 887 | ad.log.info( |
Betty Zhou | bcffe44 | 2017-07-05 17:27:55 -0700 | [diff] [blame] | 888 | "callee didn't receive ring event or got into ringing state") |
| 889 | return False |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 890 | if not event_tracking_started: |
Nathan Harold | 54c7c74 | 2016-08-04 19:40:44 -0700 | [diff] [blame] | 891 | ad.droid.telephonyStopTrackingCallStateChangeForSubscription(sub_id) |
Betty Zhou | 7f45f55 | 2017-03-15 19:12:52 -0700 | [diff] [blame] | 892 | if caller and not caller.droid.telecomIsInCall(): |
| 893 | caller.log.error("Caller not in call state") |
| 894 | raise _CallSequenceException("Caller not in call state") |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 895 | if not incoming_number: |
| 896 | return True |
| 897 | |
Betty Zhou | bcffe44 | 2017-07-05 17:27:55 -0700 | [diff] [blame] | 898 | if event_ringing and not check_phone_number_match( |
| 899 | event_ringing['data'][CallStateContainer.INCOMING_NUMBER], |
| 900 | incoming_number): |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 901 | ad.log.error( |
| 902 | "Incoming Number not match. Expected number:%s, actual number:%s", |
| 903 | incoming_number, |
| 904 | event_ringing['data'][CallStateContainer.INCOMING_NUMBER]) |
| 905 | return False |
| 906 | return True |
| 907 | |
| 908 | |
| 909 | def wait_for_call_offhook_event( |
| 910 | log, |
| 911 | ad, |
Betty Zhou | bb19248 | 2017-03-01 14:38:56 -0800 | [diff] [blame] | 912 | sub_id, |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 913 | event_tracking_started=False, |
| 914 | timeout=MAX_WAIT_TIME_ACCEPT_CALL_TO_OFFHOOK_EVENT): |
| 915 | """Wait for an incoming call on specified subscription. |
| 916 | |
| 917 | Args: |
| 918 | log: log object. |
| 919 | ad: android device object. |
| 920 | event_tracking_started: True if event tracking already state outside |
| 921 | timeout: time to wait for event |
| 922 | |
| 923 | Returns: |
| 924 | True: if call offhook event is received. |
| 925 | False: if call offhook event is not received. |
| 926 | """ |
| 927 | if not event_tracking_started: |
Betty Zhou | 287f9df | 2018-01-23 10:57:55 -0800 | [diff] [blame] | 928 | ad.ed.clear_events(EventCallStateChanged) |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 929 | ad.droid.telephonyStartTrackingCallStateForSubscription(sub_id) |
| 930 | try: |
| 931 | ad.ed.wait_for_event( |
| 932 | EventCallStateChanged, |
| 933 | is_event_match, |
| 934 | timeout=timeout, |
| 935 | field=CallStateContainer.CALL_STATE, |
| 936 | value=TELEPHONY_STATE_OFFHOOK) |
Betty Zhou | 5c7dbd1 | 2018-03-13 18:27:44 -0700 | [diff] [blame] | 937 | ad.log.info("Got event %s", TELEPHONY_STATE_OFFHOOK) |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 938 | except Empty: |
Betty Zhou | bb19248 | 2017-03-01 14:38:56 -0800 | [diff] [blame] | 939 | ad.log.info("No event for call state change to OFFHOOK") |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 940 | return False |
| 941 | finally: |
| 942 | if not event_tracking_started: |
| 943 | ad.droid.telephonyStopTrackingCallStateChangeForSubscription( |
| 944 | sub_id) |
Nathan Harold | 54c7c74 | 2016-08-04 19:40:44 -0700 | [diff] [blame] | 945 | return True |
| 946 | |
| 947 | |
| 948 | def wait_and_answer_call_for_subscription( |
| 949 | log, |
| 950 | ad, |
| 951 | sub_id, |
| 952 | incoming_number=None, |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 953 | incall_ui_display=INCALL_UI_DISPLAY_FOREGROUND, |
Betty Zhou | d2da7ba | 2017-03-24 12:54:34 -0700 | [diff] [blame] | 954 | timeout=MAX_WAIT_TIME_CALLEE_RINGING, |
| 955 | caller=None): |
Nathan Harold | 54c7c74 | 2016-08-04 19:40:44 -0700 | [diff] [blame] | 956 | """Wait for an incoming call on specified subscription and |
| 957 | accepts the call. |
| 958 | |
| 959 | Args: |
| 960 | log: log object. |
| 961 | ad: android device object. |
| 962 | sub_id: subscription ID |
| 963 | incoming_number: Expected incoming number. |
| 964 | Optional. Default is None |
| 965 | incall_ui_display: after answer the call, bring in-call UI to foreground or |
| 966 | background. Optional, default value is INCALL_UI_DISPLAY_FOREGROUND. |
| 967 | if = INCALL_UI_DISPLAY_FOREGROUND, bring in-call UI to foreground. |
| 968 | if = INCALL_UI_DISPLAY_BACKGROUND, bring in-call UI to background. |
| 969 | else, do nothing. |
| 970 | |
| 971 | Returns: |
| 972 | True: if incoming call is received and answered successfully. |
| 973 | False: for errors |
| 974 | """ |
Betty Zhou | 287f9df | 2018-01-23 10:57:55 -0800 | [diff] [blame] | 975 | ad.ed.clear_events(EventCallStateChanged) |
Yang Liu | aed3eef | 2015-12-15 18:40:25 -0800 | [diff] [blame] | 976 | ad.droid.telephonyStartTrackingCallStateForSubscription(sub_id) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 977 | try: |
Betty Zhou | f25ce44 | 2017-03-03 14:28:36 -0800 | [diff] [blame] | 978 | if not _wait_for_droid_in_state( |
| 979 | log, |
| 980 | ad, |
| 981 | timeout, |
| 982 | wait_for_ringing_call_for_subscription, |
| 983 | sub_id, |
| 984 | incoming_number=None, |
Betty Zhou | 7f45f55 | 2017-03-15 19:12:52 -0700 | [diff] [blame] | 985 | caller=caller, |
Betty Zhou | e5c0b23 | 2017-03-14 18:55:40 -0700 | [diff] [blame] | 986 | event_tracking_started=True, |
| 987 | timeout=WAIT_TIME_BETWEEN_STATE_CHECK): |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 988 | ad.log.info("Could not answer a call: phone never rang.") |
| 989 | return False |
Betty Zhou | f25ce44 | 2017-03-03 14:28:36 -0800 | [diff] [blame] | 990 | time.sleep(WAIT_TIME_BETWEEN_STATE_CHECK) |
Betty Zhou | bb19248 | 2017-03-01 14:38:56 -0800 | [diff] [blame] | 991 | ad.log.info("Accept the ring call") |
Betty Zhou | f25ce44 | 2017-03-03 14:28:36 -0800 | [diff] [blame] | 992 | ad.droid.telecomAcceptRingingCall() |
| 993 | |
Betty Zhou | 68fc0d0 | 2017-04-26 13:42:54 -0700 | [diff] [blame] | 994 | if ad.droid.telecomIsInCall() or wait_for_call_offhook_event( |
Betty Zhou | bb19248 | 2017-03-01 14:38:56 -0800 | [diff] [blame] | 995 | log, ad, sub_id, event_tracking_started=True, |
| 996 | timeout=timeout) or ad.droid.telecomIsInCall(): |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 997 | ad.log.info("Call answered successfully.") |
| 998 | return True |
| 999 | else: |
| 1000 | ad.log.error("Could not answer the call.") |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1001 | return False |
Betty Zhou | 7f45f55 | 2017-03-15 19:12:52 -0700 | [diff] [blame] | 1002 | except Exception as e: |
| 1003 | log.error(e) |
| 1004 | return False |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1005 | finally: |
Yang Liu | aed3eef | 2015-12-15 18:40:25 -0800 | [diff] [blame] | 1006 | ad.droid.telephonyStopTrackingCallStateChangeForSubscription(sub_id) |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 1007 | if incall_ui_display == INCALL_UI_DISPLAY_FOREGROUND: |
| 1008 | ad.droid.telecomShowInCallScreen() |
| 1009 | elif incall_ui_display == INCALL_UI_DISPLAY_BACKGROUND: |
| 1010 | ad.droid.showHomeScreen() |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1011 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1012 | |
| 1013 | def wait_and_reject_call(log, |
| 1014 | ad, |
| 1015 | incoming_number=None, |
Yang Liu | 598b93d | 2016-03-22 17:07:59 -0700 | [diff] [blame] | 1016 | delay_reject=WAIT_TIME_REJECT_CALL, |
| 1017 | reject=True): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1018 | """Wait for an incoming call on default voice subscription and |
| 1019 | reject the call. |
| 1020 | |
| 1021 | Args: |
Nathan Harold | 54c7c74 | 2016-08-04 19:40:44 -0700 | [diff] [blame] | 1022 | log: log object. |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1023 | ad: android device object. |
| 1024 | incoming_number: Expected incoming number. |
| 1025 | Optional. Default is None |
| 1026 | delay_reject: time to wait before rejecting the call |
| 1027 | Optional. Default is WAIT_TIME_REJECT_CALL |
| 1028 | |
| 1029 | Returns: |
| 1030 | True: if incoming call is received and reject successfully. |
| 1031 | False: for errors |
| 1032 | """ |
Betty Zhou | ee31105 | 2017-12-19 13:09:56 -0800 | [diff] [blame] | 1033 | return wait_and_reject_call_for_subscription(log, ad, |
| 1034 | get_incoming_voice_sub_id(ad), |
| 1035 | incoming_number, delay_reject, |
| 1036 | reject) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1037 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1038 | |
| 1039 | def wait_and_reject_call_for_subscription(log, |
| 1040 | ad, |
| 1041 | sub_id, |
| 1042 | incoming_number=None, |
Yang Liu | 598b93d | 2016-03-22 17:07:59 -0700 | [diff] [blame] | 1043 | delay_reject=WAIT_TIME_REJECT_CALL, |
| 1044 | reject=True): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1045 | """Wait for an incoming call on specific subscription and |
| 1046 | reject the call. |
| 1047 | |
| 1048 | Args: |
Nathan Harold | 54c7c74 | 2016-08-04 19:40:44 -0700 | [diff] [blame] | 1049 | log: log object. |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1050 | ad: android device object. |
| 1051 | sub_id: subscription ID |
| 1052 | incoming_number: Expected incoming number. |
| 1053 | Optional. Default is None |
| 1054 | delay_reject: time to wait before rejecting the call |
| 1055 | Optional. Default is WAIT_TIME_REJECT_CALL |
| 1056 | |
| 1057 | Returns: |
| 1058 | True: if incoming call is received and reject successfully. |
| 1059 | False: for errors |
| 1060 | """ |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1061 | |
Nathan Harold | 54c7c74 | 2016-08-04 19:40:44 -0700 | [diff] [blame] | 1062 | if not wait_for_ringing_call_for_subscription(log, ad, sub_id, |
| 1063 | incoming_number): |
Betty Zhou | a6c82eb | 2017-04-13 18:09:03 -0700 | [diff] [blame] | 1064 | ad.log.error("Could not reject a call: phone never rang.") |
Nathan Harold | 54c7c74 | 2016-08-04 19:40:44 -0700 | [diff] [blame] | 1065 | return False |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1066 | |
Betty Zhou | 287f9df | 2018-01-23 10:57:55 -0800 | [diff] [blame] | 1067 | ad.ed.clear_events(EventCallStateChanged) |
Yang Liu | aed3eef | 2015-12-15 18:40:25 -0800 | [diff] [blame] | 1068 | ad.droid.telephonyStartTrackingCallStateForSubscription(sub_id) |
Yang Liu | 598b93d | 2016-03-22 17:07:59 -0700 | [diff] [blame] | 1069 | if reject is True: |
| 1070 | # Delay between ringing and reject. |
| 1071 | time.sleep(delay_reject) |
Yang Liu | 598b93d | 2016-03-22 17:07:59 -0700 | [diff] [blame] | 1072 | is_find = False |
| 1073 | # Loop the call list and find the matched one to disconnect. |
| 1074 | for call in ad.droid.telecomCallGetCallIds(): |
| 1075 | if check_phone_number_match( |
| 1076 | get_number_from_tel_uri(get_call_uri(ad, call)), |
| 1077 | incoming_number): |
| 1078 | ad.droid.telecomCallDisconnect(call) |
Betty Zhou | a6c82eb | 2017-04-13 18:09:03 -0700 | [diff] [blame] | 1079 | ad.log.info("Callee reject the call") |
Yang Liu | 598b93d | 2016-03-22 17:07:59 -0700 | [diff] [blame] | 1080 | is_find = True |
| 1081 | if is_find is False: |
Betty Zhou | a6c82eb | 2017-04-13 18:09:03 -0700 | [diff] [blame] | 1082 | ad.log.error("Callee did not find matching call to reject.") |
Yang Liu | 598b93d | 2016-03-22 17:07:59 -0700 | [diff] [blame] | 1083 | return False |
| 1084 | else: |
| 1085 | # don't reject on callee. Just ignore the incoming call. |
Betty Zhou | a6c82eb | 2017-04-13 18:09:03 -0700 | [diff] [blame] | 1086 | ad.log.info("Callee received incoming call. Ignore it.") |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1087 | try: |
Yang Liu | 598b93d | 2016-03-22 17:07:59 -0700 | [diff] [blame] | 1088 | ad.ed.wait_for_event( |
| 1089 | EventCallStateChanged, |
| 1090 | is_event_match_for_list, |
| 1091 | timeout=MAX_WAIT_TIME_CALL_IDLE_EVENT, |
| 1092 | field=CallStateContainer.CALL_STATE, |
| 1093 | value_list=[TELEPHONY_STATE_IDLE, TELEPHONY_STATE_OFFHOOK]) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1094 | except Empty: |
Betty Zhou | a6c82eb | 2017-04-13 18:09:03 -0700 | [diff] [blame] | 1095 | ad.log.error("No onCallStateChangedIdle event received.") |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1096 | return False |
| 1097 | finally: |
Yang Liu | aed3eef | 2015-12-15 18:40:25 -0800 | [diff] [blame] | 1098 | ad.droid.telephonyStopTrackingCallStateChangeForSubscription(sub_id) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1099 | return True |
| 1100 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1101 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1102 | def hangup_call(log, ad): |
| 1103 | """Hang up ongoing active call. |
Nathan Harold | 54c7c74 | 2016-08-04 19:40:44 -0700 | [diff] [blame] | 1104 | |
| 1105 | Args: |
| 1106 | log: log object. |
| 1107 | ad: android device object. |
| 1108 | |
| 1109 | Returns: |
Nathan Harold | d2d3009 | 2016-10-12 12:45:55 -0700 | [diff] [blame] | 1110 | True: if all calls are cleared |
Nathan Harold | 54c7c74 | 2016-08-04 19:40:44 -0700 | [diff] [blame] | 1111 | False: for errors |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1112 | """ |
Nathan Harold | d2d3009 | 2016-10-12 12:45:55 -0700 | [diff] [blame] | 1113 | # short circuit in case no calls are active |
| 1114 | if not ad.droid.telecomIsInCall(): |
| 1115 | return True |
Betty Zhou | 287f9df | 2018-01-23 10:57:55 -0800 | [diff] [blame] | 1116 | ad.ed.clear_events(EventCallStateChanged) |
Yang Liu | aed3eef | 2015-12-15 18:40:25 -0800 | [diff] [blame] | 1117 | ad.droid.telephonyStartTrackingCallState() |
Betty Zhou | f27f548 | 2017-07-24 18:56:17 -0700 | [diff] [blame] | 1118 | ad.log.info("Hangup call.") |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1119 | ad.droid.telecomEndCall() |
| 1120 | |
| 1121 | try: |
Nathan Harold | 4a144a4 | 2016-09-19 14:16:24 -0700 | [diff] [blame] | 1122 | ad.ed.wait_for_event( |
| 1123 | EventCallStateChanged, |
| 1124 | is_event_match, |
| 1125 | timeout=MAX_WAIT_TIME_CALL_IDLE_EVENT, |
| 1126 | field=CallStateContainer.CALL_STATE, |
| 1127 | value=TELEPHONY_STATE_IDLE) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1128 | except Empty: |
| 1129 | if ad.droid.telecomIsInCall(): |
Betty Zhou | fe726dc | 2018-04-25 19:31:33 -0700 | [diff] [blame] | 1130 | ad.log.error("Telecom is in call, hangup call failed.") |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1131 | return False |
| 1132 | finally: |
Yang Liu | aed3eef | 2015-12-15 18:40:25 -0800 | [diff] [blame] | 1133 | ad.droid.telephonyStopTrackingCallStateChange() |
Betty Zhou | fe726dc | 2018-04-25 19:31:33 -0700 | [diff] [blame] | 1134 | if not wait_for_state(ad.droid.telecomIsInCall, False, 15, 1): |
| 1135 | ad.log.error("Telecom is in call, hangup call failed.") |
| 1136 | return False |
| 1137 | return True |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1138 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1139 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1140 | def disconnect_call_by_id(log, ad, call_id): |
| 1141 | """Disconnect call by call id. |
| 1142 | """ |
| 1143 | ad.droid.telecomCallDisconnect(call_id) |
| 1144 | return True |
| 1145 | |
Nathan Harold | 54c7c74 | 2016-08-04 19:40:44 -0700 | [diff] [blame] | 1146 | |
Yang Liu | 3197de2 | 2016-04-11 13:59:41 -0700 | [diff] [blame] | 1147 | def _phone_number_remove_prefix(number): |
| 1148 | """Remove the country code and other prefix from the input phone number. |
| 1149 | Currently only handle phone number with the following formats: |
| 1150 | (US phone number format) |
| 1151 | +1abcxxxyyyy |
| 1152 | 1abcxxxyyyy |
| 1153 | abcxxxyyyy |
| 1154 | abc xxx yyyy |
| 1155 | abc.xxx.yyyy |
| 1156 | abc-xxx-yyyy |
| 1157 | (EEUK phone number format) |
| 1158 | +44abcxxxyyyy |
| 1159 | 0abcxxxyyyy |
| 1160 | |
| 1161 | Args: |
| 1162 | number: input phone number |
| 1163 | |
| 1164 | Returns: |
| 1165 | Phone number without country code or prefix |
| 1166 | """ |
Yang Liu | 8fcffd5 | 2016-05-31 18:38:35 -0700 | [diff] [blame] | 1167 | if number is None: |
| 1168 | return None, None |
Betty Zhou | 9e54fc2 | 2017-01-19 12:15:53 -0800 | [diff] [blame] | 1169 | for country_code in COUNTRY_CODE_LIST: |
Yang Liu | 3197de2 | 2016-04-11 13:59:41 -0700 | [diff] [blame] | 1170 | if number.startswith(country_code): |
| 1171 | return number[len(country_code):], country_code |
| 1172 | if number[0] == "1" or number[0] == "0": |
| 1173 | return number[1:], None |
Yang Liu | 8fcffd5 | 2016-05-31 18:38:35 -0700 | [diff] [blame] | 1174 | return number, None |
Yang Liu | 3197de2 | 2016-04-11 13:59:41 -0700 | [diff] [blame] | 1175 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1176 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1177 | def check_phone_number_match(number1, number2): |
| 1178 | """Check whether two input phone numbers match or not. |
| 1179 | |
| 1180 | Compare the two input phone numbers. |
| 1181 | If they match, return True; otherwise, return False. |
| 1182 | Currently only handle phone number with the following formats: |
| 1183 | (US phone number format) |
| 1184 | +1abcxxxyyyy |
| 1185 | 1abcxxxyyyy |
| 1186 | abcxxxyyyy |
| 1187 | abc xxx yyyy |
| 1188 | abc.xxx.yyyy |
| 1189 | abc-xxx-yyyy |
Yang Liu | 3197de2 | 2016-04-11 13:59:41 -0700 | [diff] [blame] | 1190 | (EEUK phone number format) |
| 1191 | +44abcxxxyyyy |
| 1192 | 0abcxxxyyyy |
| 1193 | |
| 1194 | There are some scenarios we can not verify, one example is: |
| 1195 | number1 = +15555555555, number2 = 5555555555 |
| 1196 | (number2 have no country code) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1197 | |
| 1198 | Args: |
| 1199 | number1: 1st phone number to be compared. |
| 1200 | number2: 2nd phone number to be compared. |
| 1201 | |
| 1202 | Returns: |
| 1203 | True if two phone numbers match. Otherwise False. |
| 1204 | """ |
Betty Zhou | d2da7ba | 2017-03-24 12:54:34 -0700 | [diff] [blame] | 1205 | number1 = phone_number_formatter(number1) |
| 1206 | number2 = phone_number_formatter(number2) |
Betty Zhou | 9e54fc2 | 2017-01-19 12:15:53 -0800 | [diff] [blame] | 1207 | # Handle extra country code attachment when matching phone number |
Betty Zhou | e32dd3b | 2017-11-28 19:05:55 -0800 | [diff] [blame] | 1208 | if number1[-7:] in number2 or number2[-7:] in number1: |
Betty Zhou | 9e54fc2 | 2017-01-19 12:15:53 -0800 | [diff] [blame] | 1209 | return True |
Betty Zhou | d2da7ba | 2017-03-24 12:54:34 -0700 | [diff] [blame] | 1210 | else: |
Betty Zhou | 9e54fc2 | 2017-01-19 12:15:53 -0800 | [diff] [blame] | 1211 | logging.info("phone number1 %s and number2 %s does not match" % |
| 1212 | (number1, number2)) |
| 1213 | return False |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1214 | |
| 1215 | |
Betty Zhou | bb19248 | 2017-03-01 14:38:56 -0800 | [diff] [blame] | 1216 | def initiate_call(log, |
| 1217 | ad, |
| 1218 | callee_number, |
| 1219 | emergency=False, |
| 1220 | timeout=MAX_WAIT_TIME_CALL_INITIATION, |
Betty Zhou | f7da39e | 2018-04-16 16:28:58 -0700 | [diff] [blame] | 1221 | checking_interval=5): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1222 | """Make phone call from caller to callee. |
| 1223 | |
| 1224 | Args: |
| 1225 | ad_caller: Caller android device object. |
| 1226 | callee_number: Callee phone number. |
| 1227 | emergency : specify the call is emergency. |
| 1228 | Optional. Default value is False. |
| 1229 | |
| 1230 | Returns: |
| 1231 | result: if phone call is placed successfully. |
| 1232 | """ |
Betty Zhou | 287f9df | 2018-01-23 10:57:55 -0800 | [diff] [blame] | 1233 | ad.ed.clear_events(EventCallStateChanged) |
Betty Zhou | bb19248 | 2017-03-01 14:38:56 -0800 | [diff] [blame] | 1234 | sub_id = get_outgoing_voice_sub_id(ad) |
| 1235 | ad.droid.telephonyStartTrackingCallStateForSubscription(sub_id) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1236 | |
| 1237 | try: |
| 1238 | # Make a Call |
Betty Zhou | bb19248 | 2017-03-01 14:38:56 -0800 | [diff] [blame] | 1239 | ad.log.info("Make a phone call to %s", callee_number) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1240 | if emergency: |
Betty Zhou | bb19248 | 2017-03-01 14:38:56 -0800 | [diff] [blame] | 1241 | ad.droid.telecomCallEmergencyNumber(callee_number) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1242 | else: |
Betty Zhou | bb19248 | 2017-03-01 14:38:56 -0800 | [diff] [blame] | 1243 | ad.droid.telecomCallNumber(callee_number) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1244 | |
| 1245 | # Verify OFFHOOK event |
Betty Zhou | bb19248 | 2017-03-01 14:38:56 -0800 | [diff] [blame] | 1246 | checking_retries = int(timeout / checking_interval) |
| 1247 | for i in range(checking_retries): |
| 1248 | if (ad.droid.telecomIsInCall() and |
| 1249 | ad.droid.telephonyGetCallState() == TELEPHONY_STATE_OFFHOOK |
Betty Zhou | ee31105 | 2017-12-19 13:09:56 -0800 | [diff] [blame] | 1250 | and ad.droid.telecomGetCallState() == |
| 1251 | TELEPHONY_STATE_OFFHOOK) or wait_for_call_offhook_event( |
| 1252 | log, ad, sub_id, True, checking_interval): |
Betty Zhou | bb19248 | 2017-03-01 14:38:56 -0800 | [diff] [blame] | 1253 | return True |
Betty Zhou | 8da0378 | 2017-07-28 18:02:24 -0700 | [diff] [blame] | 1254 | ad.log.info( |
Betty Zhou | bb19248 | 2017-03-01 14:38:56 -0800 | [diff] [blame] | 1255 | "Make call to %s fail. telecomIsInCall:%s, Telecom State:%s," |
Betty Zhou | ee31105 | 2017-12-19 13:09:56 -0800 | [diff] [blame] | 1256 | " Telephony State:%s", callee_number, ad.droid.telecomIsInCall(), |
Betty Zhou | bb19248 | 2017-03-01 14:38:56 -0800 | [diff] [blame] | 1257 | ad.droid.telephonyGetCallState(), ad.droid.telecomGetCallState()) |
Betty Zhou | a301c20 | 2018-02-13 15:11:47 -0800 | [diff] [blame] | 1258 | reasons = ad.search_logcat( |
| 1259 | "qcril_qmi_voice_map_qmi_to_ril_last_call_failure_cause") |
| 1260 | if reasons: |
| 1261 | ad.log.info(reasons[-1]["log_message"]) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1262 | return False |
| 1263 | finally: |
Betty Zhou | bb19248 | 2017-03-01 14:38:56 -0800 | [diff] [blame] | 1264 | ad.droid.telephonyStopTrackingCallStateChangeForSubscription(sub_id) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1265 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1266 | |
Betty Zhou | 8da0378 | 2017-07-28 18:02:24 -0700 | [diff] [blame] | 1267 | def dial_phone_number(ad, callee_number): |
| 1268 | for number in str(callee_number): |
| 1269 | if number == "#": |
| 1270 | ad.send_keycode("POUND") |
| 1271 | elif number == "*": |
| 1272 | ad.send_keycode("STAR") |
| 1273 | elif number in ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"]: |
| 1274 | ad.send_keycode("%s" % number) |
| 1275 | |
| 1276 | |
| 1277 | def get_call_state_by_adb(ad): |
| 1278 | return ad.adb.shell("dumpsys telephony.registry | grep mCallState") |
| 1279 | |
| 1280 | |
| 1281 | def check_call_state_connected_by_adb(ad): |
| 1282 | return "2" in get_call_state_by_adb(ad) |
| 1283 | |
| 1284 | |
| 1285 | def check_call_state_idle_by_adb(ad): |
| 1286 | return "0" in get_call_state_by_adb(ad) |
| 1287 | |
| 1288 | |
| 1289 | def check_call_state_ring_by_adb(ad): |
| 1290 | return "1" in get_call_state_by_adb(ad) |
| 1291 | |
| 1292 | |
| 1293 | def get_incoming_call_number_by_adb(ad): |
| 1294 | output = ad.adb.shell( |
| 1295 | "dumpsys telephony.registry | grep mCallIncomingNumber") |
| 1296 | return re.search(r"mCallIncomingNumber=(.*)", output).group(1) |
| 1297 | |
| 1298 | |
| 1299 | def emergency_dialer_call_by_keyevent(ad, callee_number): |
| 1300 | for i in range(3): |
| 1301 | if "EmergencyDialer" in ad.get_my_current_focus_window(): |
| 1302 | ad.log.info("EmergencyDialer is the current focus window") |
| 1303 | break |
| 1304 | elif i <= 2: |
| 1305 | ad.adb.shell("am start -a com.android.phone.EmergencyDialer.DIAL") |
| 1306 | time.sleep(1) |
| 1307 | else: |
| 1308 | ad.log.error("Unable to bring up EmergencyDialer") |
| 1309 | return False |
| 1310 | ad.log.info("Make a phone call to %s", callee_number) |
| 1311 | dial_phone_number(ad, callee_number) |
| 1312 | ad.send_keycode("CALL") |
| 1313 | |
| 1314 | |
| 1315 | def initiate_emergency_dialer_call_by_adb( |
| 1316 | log, |
| 1317 | ad, |
| 1318 | callee_number, |
| 1319 | timeout=MAX_WAIT_TIME_CALL_INITIATION, |
| 1320 | checking_interval=5): |
| 1321 | """Make emergency call by EmergencyDialer. |
| 1322 | |
| 1323 | Args: |
| 1324 | ad: Caller android device object. |
| 1325 | callee_number: Callee phone number. |
| 1326 | emergency : specify the call is emergency. |
| 1327 | Optional. Default value is False. |
| 1328 | |
| 1329 | Returns: |
| 1330 | result: if phone call is placed successfully. |
| 1331 | """ |
| 1332 | try: |
| 1333 | # Make a Call |
| 1334 | ad.wakeup_screen() |
Betty Zhou | 3c2e254 | 2018-02-21 12:23:04 -0800 | [diff] [blame] | 1335 | ad.send_keycode("MENU") |
Betty Zhou | 8da0378 | 2017-07-28 18:02:24 -0700 | [diff] [blame] | 1336 | ad.log.info("Call %s", callee_number) |
Betty Zhou | 4617cba | 2017-08-10 11:58:39 -0700 | [diff] [blame] | 1337 | ad.adb.shell("am start -a com.android.phone.EmergencyDialer.DIAL") |
Betty Zhou | 8da0378 | 2017-07-28 18:02:24 -0700 | [diff] [blame] | 1338 | ad.adb.shell( |
| 1339 | "am start -a android.intent.action.CALL_EMERGENCY -d tel:%s" % |
| 1340 | callee_number) |
Betty Zhou | f7da39e | 2018-04-16 16:28:58 -0700 | [diff] [blame] | 1341 | if not timeout: return True |
Betty Zhou | 8da0378 | 2017-07-28 18:02:24 -0700 | [diff] [blame] | 1342 | ad.log.info("Check call state") |
| 1343 | # Verify Call State |
| 1344 | elapsed_time = 0 |
| 1345 | while elapsed_time < timeout: |
| 1346 | time.sleep(checking_interval) |
| 1347 | elapsed_time += checking_interval |
| 1348 | if check_call_state_connected_by_adb(ad): |
| 1349 | ad.log.info("Call to %s is connected", callee_number) |
| 1350 | return True |
Betty Zhou | 13e7adf | 2017-09-06 14:01:10 -0700 | [diff] [blame] | 1351 | if check_call_state_idle_by_adb(ad): |
| 1352 | ad.log.info("Call to %s failed", callee_number) |
| 1353 | return False |
Betty Zhou | 8da0378 | 2017-07-28 18:02:24 -0700 | [diff] [blame] | 1354 | ad.log.info("Make call to %s failed", callee_number) |
| 1355 | return False |
| 1356 | except Exception as e: |
| 1357 | ad.log.error("initiate emergency call failed with error %s", e) |
| 1358 | |
| 1359 | |
Betty Zhou | cbda412 | 2018-01-16 19:15:42 -0800 | [diff] [blame] | 1360 | def hangup_call_by_adb(ad): |
Betty Zhou | 8da0378 | 2017-07-28 18:02:24 -0700 | [diff] [blame] | 1361 | """Make emergency call by EmergencyDialer. |
| 1362 | |
| 1363 | Args: |
| 1364 | ad: Caller android device object. |
| 1365 | callee_number: Callee phone number. |
| 1366 | """ |
| 1367 | ad.log.info("End call by adb") |
| 1368 | ad.send_keycode("ENDCALL") |
| 1369 | |
| 1370 | |
Betty Zhou | f7da39e | 2018-04-16 16:28:58 -0700 | [diff] [blame] | 1371 | def dumpsys_all_call_info(ad): |
Betty Zhou | dd781f8 | 2017-08-08 14:46:23 -0700 | [diff] [blame] | 1372 | """ Get call information by dumpsys telecom. """ |
Betty Zhou | 8da0378 | 2017-07-28 18:02:24 -0700 | [diff] [blame] | 1373 | output = ad.adb.shell("dumpsys telecom") |
| 1374 | calls = re.findall("Call TC@\d+: {(.*?)}", output, re.DOTALL) |
| 1375 | calls_info = [] |
| 1376 | for call in calls: |
| 1377 | call_info = {} |
Betty Zhou | dd781f8 | 2017-08-08 14:46:23 -0700 | [diff] [blame] | 1378 | for attr in ("startTime", "endTime", "direction", "isInterrupted", |
Betty Zhou | 8da0378 | 2017-07-28 18:02:24 -0700 | [diff] [blame] | 1379 | "callTechnologies", "callTerminationsReason", |
Betty Zhou | 739d6b7 | 2018-04-18 10:38:53 -0700 | [diff] [blame] | 1380 | "connectionService", "isVideoCall", "callProperties"): |
Betty Zhou | 8da0378 | 2017-07-28 18:02:24 -0700 | [diff] [blame] | 1381 | match = re.search(r"%s: (.*)" % attr, call) |
| 1382 | if match: |
Betty Zhou | 739d6b7 | 2018-04-18 10:38:53 -0700 | [diff] [blame] | 1383 | if attr in ("startTime", "endTime"): |
| 1384 | call_info[attr] = epoch_to_log_line_timestamp( |
| 1385 | int(match.group(1))) |
| 1386 | else: |
| 1387 | call_info[attr] = match.group(1) |
Betty Zhou | 8da0378 | 2017-07-28 18:02:24 -0700 | [diff] [blame] | 1388 | call_info["inCallServices"] = re.findall(r"name: (.*)", call) |
| 1389 | calls_info.append(call_info) |
| 1390 | ad.log.debug("calls_info = %s", calls_info) |
| 1391 | return calls_info |
| 1392 | |
| 1393 | |
Betty Zhou | f7da39e | 2018-04-16 16:28:58 -0700 | [diff] [blame] | 1394 | def dumpsys_last_call_info(ad): |
| 1395 | """ Get call information by dumpsys telecom. """ |
| 1396 | num = dumpsys_last_call_number(ad) |
| 1397 | output = ad.adb.shell("dumpsys telecom") |
| 1398 | result = re.search(r"Call TC@%s: {(.*?)}" % num, output, re.DOTALL) |
Betty Zhou | 739d6b7 | 2018-04-18 10:38:53 -0700 | [diff] [blame] | 1399 | call_info = {"TC": num} |
Betty Zhou | f7da39e | 2018-04-16 16:28:58 -0700 | [diff] [blame] | 1400 | if result: |
| 1401 | result = result.group(1) |
| 1402 | for attr in ("startTime", "endTime", "direction", "isInterrupted", |
| 1403 | "callTechnologies", "callTerminationsReason", |
Betty Zhou | 739d6b7 | 2018-04-18 10:38:53 -0700 | [diff] [blame] | 1404 | "isVideoCall", "callProperties"): |
Betty Zhou | f7da39e | 2018-04-16 16:28:58 -0700 | [diff] [blame] | 1405 | match = re.search(r"%s: (.*)" % attr, result) |
| 1406 | if match: |
Betty Zhou | 739d6b7 | 2018-04-18 10:38:53 -0700 | [diff] [blame] | 1407 | if attr in ("startTime", "endTime"): |
| 1408 | call_info[attr] = epoch_to_log_line_timestamp( |
| 1409 | int(match.group(1))) |
| 1410 | else: |
| 1411 | call_info[attr] = match.group(1) |
Betty Zhou | f7da39e | 2018-04-16 16:28:58 -0700 | [diff] [blame] | 1412 | ad.log.debug("call_info = %s", call_info) |
| 1413 | return call_info |
| 1414 | |
| 1415 | |
| 1416 | def dumpsys_last_call_number(ad): |
| 1417 | output = ad.adb.shell("dumpsys telecom") |
| 1418 | call_nums = re.findall("Call TC@(\d+):", output) |
| 1419 | if not call_nums: |
| 1420 | return 0 |
| 1421 | else: |
| 1422 | return int(call_nums[-1]) |
| 1423 | |
| 1424 | |
Betty Zhou | fe726dc | 2018-04-25 19:31:33 -0700 | [diff] [blame] | 1425 | def dumpsys_new_call_info(ad, last_tc_number, retries=3, interval=5): |
| 1426 | for i in range(retries): |
| 1427 | if dumpsys_last_call_number(ad) > last_tc_number: |
| 1428 | call_info = dumpsys_last_call_info(ad) |
| 1429 | ad.log.info("New call info = %s", sorted(call_info.items())) |
| 1430 | return call_info |
| 1431 | else: |
| 1432 | time.sleep(interval) |
| 1433 | ad.log.error("New call is not in sysdump telecom") |
| 1434 | return {} |
| 1435 | |
| 1436 | |
Yang Liu | 598b93d | 2016-03-22 17:07:59 -0700 | [diff] [blame] | 1437 | def call_reject(log, ad_caller, ad_callee, reject=True): |
| 1438 | """Caller call Callee, then reject on callee. |
| 1439 | |
| 1440 | |
| 1441 | """ |
| 1442 | subid_caller = ad_caller.droid.subscriptionGetDefaultVoiceSubId() |
| 1443 | subid_callee = ad_callee.incoming_voice_sub_id |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 1444 | ad_caller.log.info("Sub-ID Caller %s, Sub-ID Callee %s", subid_caller, |
| 1445 | subid_callee) |
Yang Liu | 598b93d | 2016-03-22 17:07:59 -0700 | [diff] [blame] | 1446 | return call_reject_for_subscription(log, ad_caller, ad_callee, |
| 1447 | subid_caller, subid_callee, reject) |
| 1448 | |
| 1449 | |
| 1450 | def call_reject_for_subscription(log, |
| 1451 | ad_caller, |
| 1452 | ad_callee, |
| 1453 | subid_caller, |
| 1454 | subid_callee, |
| 1455 | reject=True): |
| 1456 | """ |
| 1457 | """ |
| 1458 | |
Betty Zhou | 3b2de07 | 2018-03-15 16:46:26 -0700 | [diff] [blame] | 1459 | caller_number = ad_caller.telephony['subscription'][subid_caller][ |
| 1460 | 'phone_num'] |
| 1461 | callee_number = ad_callee.telephony['subscription'][subid_callee][ |
| 1462 | 'phone_num'] |
Yang Liu | 598b93d | 2016-03-22 17:07:59 -0700 | [diff] [blame] | 1463 | |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 1464 | ad_caller.log.info("Call from %s to %s", caller_number, callee_number) |
Yang Liu | 598b93d | 2016-03-22 17:07:59 -0700 | [diff] [blame] | 1465 | try: |
| 1466 | if not initiate_call(log, ad_caller, callee_number): |
| 1467 | raise _CallSequenceException("Initiate call failed.") |
| 1468 | |
| 1469 | if not wait_and_reject_call_for_subscription( |
| 1470 | log, ad_callee, subid_callee, caller_number, |
| 1471 | WAIT_TIME_REJECT_CALL, reject): |
| 1472 | raise _CallSequenceException("Reject call fail.") |
| 1473 | # Check if incoming call is cleared on callee or not. |
| 1474 | if ad_callee.droid.telephonyGetCallStateForSubscription( |
| 1475 | subid_callee) == TELEPHONY_STATE_RINGING: |
| 1476 | raise _CallSequenceException("Incoming call is not cleared.") |
| 1477 | # Hangup on caller |
| 1478 | hangup_call(log, ad_caller) |
| 1479 | except _CallSequenceException as e: |
| 1480 | log.error(e) |
| 1481 | return False |
| 1482 | return True |
| 1483 | |
| 1484 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1485 | def call_reject_leave_message(log, |
| 1486 | ad_caller, |
| 1487 | ad_callee, |
| 1488 | verify_caller_func=None, |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 1489 | wait_time_in_call=WAIT_TIME_LEAVE_VOICE_MAIL): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1490 | """On default voice subscription, Call from caller to callee, |
| 1491 | reject on callee, caller leave a voice mail. |
| 1492 | |
| 1493 | 1. Caller call Callee. |
| 1494 | 2. Callee reject incoming call. |
| 1495 | 3. Caller leave a voice mail. |
| 1496 | 4. Verify callee received the voice mail notification. |
| 1497 | |
| 1498 | Args: |
| 1499 | ad_caller: caller android device object. |
| 1500 | ad_callee: callee android device object. |
| 1501 | verify_caller_func: function to verify caller is in correct state while in-call. |
| 1502 | This is optional, default is None. |
| 1503 | wait_time_in_call: time to wait when leaving a voice mail. |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 1504 | This is optional, default is WAIT_TIME_LEAVE_VOICE_MAIL |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1505 | |
| 1506 | Returns: |
| 1507 | True: if voice message is received on callee successfully. |
| 1508 | False: for errors |
| 1509 | """ |
Yang Liu | 963c93c | 2016-04-05 10:52:00 -0700 | [diff] [blame] | 1510 | subid_caller = get_outgoing_voice_sub_id(ad_caller) |
| 1511 | subid_callee = get_incoming_voice_sub_id(ad_callee) |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1512 | return call_reject_leave_message_for_subscription( |
| 1513 | log, ad_caller, ad_callee, subid_caller, subid_callee, |
| 1514 | verify_caller_func, wait_time_in_call) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1515 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1516 | |
| 1517 | def call_reject_leave_message_for_subscription( |
| 1518 | log, |
| 1519 | ad_caller, |
| 1520 | ad_callee, |
| 1521 | subid_caller, |
| 1522 | subid_callee, |
| 1523 | verify_caller_func=None, |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 1524 | wait_time_in_call=WAIT_TIME_LEAVE_VOICE_MAIL): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1525 | """On specific voice subscription, Call from caller to callee, |
| 1526 | reject on callee, caller leave a voice mail. |
| 1527 | |
| 1528 | 1. Caller call Callee. |
| 1529 | 2. Callee reject incoming call. |
| 1530 | 3. Caller leave a voice mail. |
| 1531 | 4. Verify callee received the voice mail notification. |
| 1532 | |
| 1533 | Args: |
| 1534 | ad_caller: caller android device object. |
| 1535 | ad_callee: callee android device object. |
| 1536 | subid_caller: caller's subscription id. |
| 1537 | subid_callee: callee's subscription id. |
| 1538 | verify_caller_func: function to verify caller is in correct state while in-call. |
| 1539 | This is optional, default is None. |
| 1540 | wait_time_in_call: time to wait when leaving a voice mail. |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 1541 | This is optional, default is WAIT_TIME_LEAVE_VOICE_MAIL |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1542 | |
| 1543 | Returns: |
| 1544 | True: if voice message is received on callee successfully. |
| 1545 | False: for errors |
| 1546 | """ |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1547 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1548 | # Currently this test utility only works for TMO and ATT and SPT. |
| 1549 | # It does not work for VZW (see b/21559800) |
| 1550 | # "with VVM TelephonyManager APIs won't work for vm" |
| 1551 | |
Betty Zhou | 3b2de07 | 2018-03-15 16:46:26 -0700 | [diff] [blame] | 1552 | caller_number = ad_caller.telephony['subscription'][subid_caller][ |
| 1553 | 'phone_num'] |
| 1554 | callee_number = ad_callee.telephony['subscription'][subid_callee][ |
| 1555 | 'phone_num'] |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1556 | |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 1557 | ad_caller.log.info("Call from %s to %s", caller_number, callee_number) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1558 | |
| 1559 | try: |
Betty Zhou | a6c82eb | 2017-04-13 18:09:03 -0700 | [diff] [blame] | 1560 | voice_mail_count_before = ad_callee.droid.telephonyGetVoiceMailCountForSubscription( |
| 1561 | subid_callee) |
| 1562 | ad_callee.log.info("voice mail count is %s", voice_mail_count_before) |
| 1563 | # -1 means there are unread voice mail, but the count is unknown |
| 1564 | # 0 means either this API not working (VZW) or no unread voice mail. |
| 1565 | if voice_mail_count_before != 0: |
| 1566 | log.warning("--Pending new Voice Mail, please clear on phone.--") |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1567 | |
| 1568 | if not initiate_call(log, ad_caller, callee_number): |
| 1569 | raise _CallSequenceException("Initiate call failed.") |
| 1570 | |
| 1571 | if not wait_and_reject_call_for_subscription( |
Nathan Harold | 4a144a4 | 2016-09-19 14:16:24 -0700 | [diff] [blame] | 1572 | log, ad_callee, subid_callee, incoming_number=caller_number): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1573 | raise _CallSequenceException("Reject call fail.") |
| 1574 | |
Yang Liu | aed3eef | 2015-12-15 18:40:25 -0800 | [diff] [blame] | 1575 | ad_callee.droid.telephonyStartTrackingVoiceMailStateChangeForSubscription( |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1576 | subid_callee) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1577 | |
| 1578 | # ensure that all internal states are updated in telecom |
| 1579 | time.sleep(WAIT_TIME_ANDROID_STATE_SETTLING) |
Betty Zhou | 287f9df | 2018-01-23 10:57:55 -0800 | [diff] [blame] | 1580 | ad_callee.ed.ad.ed.clear_events(EventCallStateChanged) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1581 | |
| 1582 | if verify_caller_func and not verify_caller_func(log, ad_caller): |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1583 | raise _CallSequenceException("Caller not in correct state!") |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1584 | |
Yang Liu | 7a2e7ee | 2015-12-28 15:32:44 -0800 | [diff] [blame] | 1585 | # TODO: b/26293512 Need to play some sound to leave message. |
| 1586 | # Otherwise carrier voice mail server may drop this voice mail. |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1587 | time.sleep(wait_time_in_call) |
| 1588 | |
| 1589 | if not verify_caller_func: |
| 1590 | caller_state_result = ad_caller.droid.telecomIsInCall() |
| 1591 | else: |
| 1592 | caller_state_result = verify_caller_func(log, ad_caller) |
| 1593 | if not caller_state_result: |
| 1594 | raise _CallSequenceException( |
Betty Zhou | a6c82eb | 2017-04-13 18:09:03 -0700 | [diff] [blame] | 1595 | "Caller %s not in correct state after %s seconds" % |
| 1596 | (ad_caller.serial, wait_time_in_call)) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1597 | |
| 1598 | if not hangup_call(log, ad_caller): |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1599 | raise _CallSequenceException("Error in Hanging-Up Call") |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1600 | |
| 1601 | log.info("Wait for voice mail indicator on callee.") |
| 1602 | try: |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1603 | event = ad_callee.ed.wait_for_event( |
| 1604 | EventMessageWaitingIndicatorChanged, |
| 1605 | _is_on_message_waiting_event_true) |
Betty Zhou | 5c7dbd1 | 2018-03-13 18:27:44 -0700 | [diff] [blame] | 1606 | ad_callee.log.info("Got event %s", event) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1607 | except Empty: |
Betty Zhou | a6c82eb | 2017-04-13 18:09:03 -0700 | [diff] [blame] | 1608 | ad_callee.log.warning("No expected event %s", |
| 1609 | EventMessageWaitingIndicatorChanged) |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1610 | raise _CallSequenceException("No expected event {}.".format( |
| 1611 | EventMessageWaitingIndicatorChanged)) |
Yang Liu | aed3eef | 2015-12-15 18:40:25 -0800 | [diff] [blame] | 1612 | voice_mail_count_after = ad_callee.droid.telephonyGetVoiceMailCountForSubscription( |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1613 | subid_callee) |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 1614 | ad_callee.log.info( |
| 1615 | "telephonyGetVoiceMailCount output - before: %s, after: %s", |
| 1616 | voice_mail_count_before, voice_mail_count_after) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1617 | |
| 1618 | # voice_mail_count_after should: |
| 1619 | # either equals to (voice_mail_count_before + 1) [For ATT and SPT] |
| 1620 | # or equals to -1 [For TMO] |
| 1621 | # -1 means there are unread voice mail, but the count is unknown |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1622 | 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] | 1623 | voice_mail_count_after): |
Betty Zhou | a6c82eb | 2017-04-13 18:09:03 -0700 | [diff] [blame] | 1624 | log.error("before and after voice mail count is not incorrect.") |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1625 | return False |
| 1626 | |
| 1627 | except _CallSequenceException as e: |
| 1628 | log.error(e) |
| 1629 | return False |
| 1630 | finally: |
Yang Liu | aed3eef | 2015-12-15 18:40:25 -0800 | [diff] [blame] | 1631 | ad_callee.droid.telephonyStopTrackingVoiceMailStateChangeForSubscription( |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1632 | subid_callee) |
| 1633 | return True |
| 1634 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1635 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1636 | def call_voicemail_erase_all_pending_voicemail(log, ad): |
| 1637 | """Script for phone to erase all pending voice mail. |
| 1638 | This script only works for TMO and ATT and SPT currently. |
| 1639 | This script only works if phone have already set up voice mail options, |
| 1640 | and phone should disable password protection for voice mail. |
| 1641 | |
| 1642 | 1. If phone don't have pending voice message, return True. |
| 1643 | 2. Dial voice mail number. |
| 1644 | For TMO, the number is '123' |
| 1645 | For ATT, the number is phone's number |
| 1646 | For SPT, the number is phone's number |
| 1647 | 3. Wait for voice mail connection setup. |
| 1648 | 4. Wait for voice mail play pending voice message. |
| 1649 | 5. Send DTMF to delete one message. |
| 1650 | The digit is '7'. |
| 1651 | 6. Repeat steps 4 and 5 until voice mail server drop this call. |
| 1652 | (No pending message) |
Yang Liu | aed3eef | 2015-12-15 18:40:25 -0800 | [diff] [blame] | 1653 | 6. Check telephonyGetVoiceMailCount result. it should be 0. |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1654 | |
| 1655 | Args: |
| 1656 | log: log object |
| 1657 | ad: android device object |
| 1658 | Returns: |
| 1659 | False if error happens. True is succeed. |
| 1660 | """ |
| 1661 | log.info("Erase all pending voice mail.") |
Betty Zhou | a6c82eb | 2017-04-13 18:09:03 -0700 | [diff] [blame] | 1662 | count = ad.droid.telephonyGetVoiceMailCount() |
| 1663 | if count == 0: |
| 1664 | ad.log.info("No Pending voice mail.") |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1665 | return True |
Betty Zhou | a6c82eb | 2017-04-13 18:09:03 -0700 | [diff] [blame] | 1666 | if count == -1: |
| 1667 | ad.log.info("There is pending voice mail, but the count is unknown") |
| 1668 | count = MAX_SAVED_VOICE_MAIL |
| 1669 | else: |
| 1670 | ad.log.info("There are %s voicemails", count) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1671 | |
| 1672 | voice_mail_number = get_voice_mail_number(log, ad) |
Betty Zhou | a6c82eb | 2017-04-13 18:09:03 -0700 | [diff] [blame] | 1673 | delete_digit = get_voice_mail_delete_digit(get_operator_name(log, ad)) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1674 | if not initiate_call(log, ad, voice_mail_number): |
Betty Zhou | a6c82eb | 2017-04-13 18:09:03 -0700 | [diff] [blame] | 1675 | log.error("Initiate call to voice mail failed.") |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1676 | return False |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 1677 | time.sleep(WAIT_TIME_VOICE_MAIL_SERVER_RESPONSE) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1678 | callId = ad.droid.telecomCallGetCallIds()[0] |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 1679 | time.sleep(WAIT_TIME_VOICE_MAIL_SERVER_RESPONSE) |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1680 | while (is_phone_in_call(log, ad) and (count > 0)): |
Betty Zhou | a6c82eb | 2017-04-13 18:09:03 -0700 | [diff] [blame] | 1681 | ad.log.info("Press %s to delete voice mail.", delete_digit) |
| 1682 | ad.droid.telecomCallPlayDtmfTone(callId, delete_digit) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1683 | ad.droid.telecomCallStopDtmfTone(callId) |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 1684 | time.sleep(WAIT_TIME_VOICE_MAIL_SERVER_RESPONSE) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1685 | count -= 1 |
Betty Zhou | a6c82eb | 2017-04-13 18:09:03 -0700 | [diff] [blame] | 1686 | if is_phone_in_call(log, ad): |
| 1687 | hangup_call(log, ad) |
| 1688 | |
Yang Liu | aed3eef | 2015-12-15 18:40:25 -0800 | [diff] [blame] | 1689 | # wait for telephonyGetVoiceMailCount to update correct result |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 1690 | remaining_time = MAX_WAIT_TIME_VOICE_MAIL_COUNT |
Betty Zhou | ee31105 | 2017-12-19 13:09:56 -0800 | [diff] [blame] | 1691 | while ((remaining_time > 0) |
| 1692 | and (ad.droid.telephonyGetVoiceMailCount() != 0)): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1693 | time.sleep(1) |
| 1694 | remaining_time -= 1 |
Yang Liu | aed3eef | 2015-12-15 18:40:25 -0800 | [diff] [blame] | 1695 | current_voice_mail_count = ad.droid.telephonyGetVoiceMailCount() |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 1696 | ad.log.info("telephonyGetVoiceMailCount: %s", current_voice_mail_count) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1697 | return (current_voice_mail_count == 0) |
| 1698 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1699 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1700 | def _is_on_message_waiting_event_true(event): |
| 1701 | """Private function to return if the received EventMessageWaitingIndicatorChanged |
Yang Liu | dc8564b | 2016-01-27 14:15:37 -0800 | [diff] [blame] | 1702 | event MessageWaitingIndicatorContainer.IS_MESSAGE_WAITING field is True. |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1703 | """ |
Yang Liu | dc8564b | 2016-01-27 14:15:37 -0800 | [diff] [blame] | 1704 | return event['data'][MessageWaitingIndicatorContainer.IS_MESSAGE_WAITING] |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1705 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1706 | |
| 1707 | def call_setup_teardown(log, |
| 1708 | ad_caller, |
| 1709 | ad_callee, |
| 1710 | ad_hangup=None, |
| 1711 | verify_caller_func=None, |
| 1712 | verify_callee_func=None, |
| 1713 | wait_time_in_call=WAIT_TIME_IN_CALL, |
Jaineel | f08d6a2 | 2017-09-11 14:17:48 -0700 | [diff] [blame] | 1714 | incall_ui_display=INCALL_UI_DISPLAY_FOREGROUND, |
Betty Zhou | 3b2de07 | 2018-03-15 16:46:26 -0700 | [diff] [blame] | 1715 | dialing_number_length=None): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1716 | """ Call process, including make a phone call from caller, |
| 1717 | accept from callee, and hang up. The call is on default voice subscription |
| 1718 | |
| 1719 | In call process, call from <droid_caller> to <droid_callee>, |
Yang Liu | 855d5f8 | 2016-01-27 15:35:48 -0800 | [diff] [blame] | 1720 | accept the call, (optional)then hang up from <droid_hangup>. |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1721 | |
| 1722 | Args: |
| 1723 | ad_caller: Caller Android Device Object. |
| 1724 | ad_callee: Callee Android Device Object. |
| 1725 | ad_hangup: Android Device Object end the phone call. |
| 1726 | Optional. Default value is None, and phone call will continue. |
| 1727 | verify_call_mode_caller: func_ptr to verify caller in correct mode |
| 1728 | Optional. Default is None |
| 1729 | verify_call_mode_caller: func_ptr to verify caller in correct mode |
| 1730 | Optional. Default is None |
| 1731 | incall_ui_display: after answer the call, bring in-call UI to foreground or |
| 1732 | background. Optional, default value is INCALL_UI_DISPLAY_FOREGROUND. |
| 1733 | if = INCALL_UI_DISPLAY_FOREGROUND, bring in-call UI to foreground. |
| 1734 | if = INCALL_UI_DISPLAY_BACKGROUND, bring in-call UI to background. |
| 1735 | else, do nothing. |
| 1736 | |
| 1737 | Returns: |
| 1738 | True if call process without any error. |
| 1739 | False if error happened. |
| 1740 | |
| 1741 | """ |
Yang Liu | 963c93c | 2016-04-05 10:52:00 -0700 | [diff] [blame] | 1742 | subid_caller = get_outgoing_voice_sub_id(ad_caller) |
| 1743 | subid_callee = get_incoming_voice_sub_id(ad_callee) |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1744 | return call_setup_teardown_for_subscription( |
| 1745 | log, ad_caller, ad_callee, subid_caller, subid_callee, ad_hangup, |
| 1746 | verify_caller_func, verify_callee_func, wait_time_in_call, |
Betty Zhou | f7da39e | 2018-04-16 16:28:58 -0700 | [diff] [blame] | 1747 | incall_ui_display, dialing_number_length) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1748 | |
| 1749 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1750 | def call_setup_teardown_for_subscription( |
| 1751 | log, |
| 1752 | ad_caller, |
| 1753 | ad_callee, |
| 1754 | subid_caller, |
| 1755 | subid_callee, |
| 1756 | ad_hangup=None, |
| 1757 | verify_caller_func=None, |
| 1758 | verify_callee_func=None, |
| 1759 | wait_time_in_call=WAIT_TIME_IN_CALL, |
Jaineel | f08d6a2 | 2017-09-11 14:17:48 -0700 | [diff] [blame] | 1760 | incall_ui_display=INCALL_UI_DISPLAY_FOREGROUND, |
Betty Zhou | 3b2de07 | 2018-03-15 16:46:26 -0700 | [diff] [blame] | 1761 | dialing_number_length=None): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1762 | """ Call process, including make a phone call from caller, |
| 1763 | accept from callee, and hang up. The call is on specified subscription |
| 1764 | |
| 1765 | In call process, call from <droid_caller> to <droid_callee>, |
Yang Liu | 855d5f8 | 2016-01-27 15:35:48 -0800 | [diff] [blame] | 1766 | accept the call, (optional)then hang up from <droid_hangup>. |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1767 | |
| 1768 | Args: |
| 1769 | ad_caller: Caller Android Device Object. |
| 1770 | ad_callee: Callee Android Device Object. |
| 1771 | subid_caller: Caller subscription ID |
| 1772 | subid_callee: Callee subscription ID |
| 1773 | ad_hangup: Android Device Object end the phone call. |
| 1774 | Optional. Default value is None, and phone call will continue. |
| 1775 | verify_call_mode_caller: func_ptr to verify caller in correct mode |
| 1776 | Optional. Default is None |
| 1777 | verify_call_mode_caller: func_ptr to verify caller in correct mode |
| 1778 | Optional. Default is None |
| 1779 | incall_ui_display: after answer the call, bring in-call UI to foreground or |
| 1780 | background. Optional, default value is INCALL_UI_DISPLAY_FOREGROUND. |
| 1781 | if = INCALL_UI_DISPLAY_FOREGROUND, bring in-call UI to foreground. |
| 1782 | if = INCALL_UI_DISPLAY_BACKGROUND, bring in-call UI to background. |
| 1783 | else, do nothing. |
| 1784 | |
| 1785 | Returns: |
| 1786 | True if call process without any error. |
| 1787 | False if error happened. |
| 1788 | |
| 1789 | """ |
Betty Zhou | f27f548 | 2017-07-24 18:56:17 -0700 | [diff] [blame] | 1790 | CHECK_INTERVAL = 5 |
Betty Zhou | a301c20 | 2018-02-13 15:11:47 -0800 | [diff] [blame] | 1791 | begin_time = get_current_epoch_time() |
Betty Zhou | 7448172 | 2018-04-13 16:19:25 -0700 | [diff] [blame] | 1792 | if not verify_caller_func: |
| 1793 | verify_caller_func = is_phone_in_call |
| 1794 | if not verify_callee_func: |
| 1795 | verify_callee_func = is_phone_in_call |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1796 | |
Betty Zhou | 3b2de07 | 2018-03-15 16:46:26 -0700 | [diff] [blame] | 1797 | caller_number = ad_caller.telephony['subscription'][subid_caller][ |
| 1798 | 'phone_num'] |
| 1799 | callee_number = ad_callee.telephony['subscription'][subid_callee][ |
| 1800 | 'phone_num'] |
| 1801 | if dialing_number_length: |
| 1802 | skip_test = False |
| 1803 | trunc_position = 0 - int(dialing_number_length) |
| 1804 | try: |
| 1805 | caller_area_code = caller_number[:trunc_position] |
| 1806 | callee_area_code = callee_number[:trunc_position] |
| 1807 | callee_dial_number = callee_number[trunc_position:] |
| 1808 | except: |
| 1809 | skip_test = True |
| 1810 | if caller_area_code != callee_area_code: |
| 1811 | skip_test = True |
| 1812 | if skip_test: |
| 1813 | msg = "Cannot make call from %s to %s by %s digits" % ( |
| 1814 | caller_number, callee_number, dialing_number_length) |
| 1815 | ad_caller.log.info(msg) |
| 1816 | raise signals.TestSkip(msg) |
| 1817 | else: |
| 1818 | callee_number = callee_dial_number |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1819 | |
Betty Zhou | f27f548 | 2017-07-24 18:56:17 -0700 | [diff] [blame] | 1820 | result = True |
Betty Zhou | 628b98e | 2018-01-08 15:45:25 -0800 | [diff] [blame] | 1821 | msg = "Call from %s to %s" % (caller_number, callee_number) |
| 1822 | if ad_hangup: |
| 1823 | msg = "%s for duration of %s seconds" % (msg, wait_time_in_call) |
| 1824 | ad_caller.log.info(msg) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1825 | |
Betty Zhou | 0d77458 | 2018-03-01 17:46:44 -0800 | [diff] [blame] | 1826 | for ad in (ad_caller, ad_callee): |
| 1827 | call_ids = ad.droid.telecomCallGetCallIds() |
| 1828 | setattr(ad, "call_ids", call_ids) |
| 1829 | ad.log.info("Before making call, existing phone calls %s", call_ids) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1830 | try: |
Betty Zhou | f7da39e | 2018-04-16 16:28:58 -0700 | [diff] [blame] | 1831 | if not initiate_call(log, ad_caller, callee_number): |
Betty Zhou | f27f548 | 2017-07-24 18:56:17 -0700 | [diff] [blame] | 1832 | ad_caller.log.error("Initiate call failed.") |
Betty Zhou | f27f548 | 2017-07-24 18:56:17 -0700 | [diff] [blame] | 1833 | return False |
Betty Zhou | 9402318 | 2017-06-07 18:02:14 -0700 | [diff] [blame] | 1834 | else: |
| 1835 | ad_caller.log.info("Caller initate call successfully") |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1836 | if not wait_and_answer_call_for_subscription( |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1837 | log, |
| 1838 | ad_callee, |
| 1839 | subid_callee, |
| 1840 | incoming_number=caller_number, |
Betty Zhou | 2e01bc8 | 2017-03-17 10:55:57 -0700 | [diff] [blame] | 1841 | caller=ad_caller, |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1842 | incall_ui_display=incall_ui_display): |
Betty Zhou | f27f548 | 2017-07-24 18:56:17 -0700 | [diff] [blame] | 1843 | ad_callee.log.error("Answer call fail.") |
Betty Zhou | f27f548 | 2017-07-24 18:56:17 -0700 | [diff] [blame] | 1844 | return False |
Betty Zhou | 9402318 | 2017-06-07 18:02:14 -0700 | [diff] [blame] | 1845 | else: |
| 1846 | ad_callee.log.info("Callee answered the call successfully") |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1847 | |
Betty Zhou | 7448172 | 2018-04-13 16:19:25 -0700 | [diff] [blame] | 1848 | for ad, call_func in zip([ad_caller, ad_callee], |
| 1849 | [verify_caller_func, verify_callee_func]): |
Betty Zhou | 0d77458 | 2018-03-01 17:46:44 -0800 | [diff] [blame] | 1850 | call_ids = ad.droid.telecomCallGetCallIds() |
Betty Zhou | 5dd53c0 | 2018-03-22 20:08:33 -0700 | [diff] [blame] | 1851 | new_call_ids = set(call_ids) - set(ad.call_ids) |
| 1852 | if not new_call_ids: |
| 1853 | ad.log.error( |
| 1854 | "No new call ids are found after call establishment") |
Betty Zhou | f25fdab | 2018-04-23 19:20:17 -0700 | [diff] [blame] | 1855 | ad.log.error("telecomCallGetCallIds returns %s", |
| 1856 | ad.droid.telecomCallGetCallIds()) |
Betty Zhou | cf04854 | 2018-02-22 17:14:08 -0800 | [diff] [blame] | 1857 | result = False |
Betty Zhou | 5dd53c0 | 2018-03-22 20:08:33 -0700 | [diff] [blame] | 1858 | for new_call_id in new_call_ids: |
| 1859 | if not wait_for_in_call_active(ad, call_id=new_call_id): |
| 1860 | result = False |
Betty Zhou | c4dfad1 | 2018-04-11 18:36:49 -0700 | [diff] [blame] | 1861 | |
Betty Zhou | f31dffe | 2018-02-23 19:29:28 -0800 | [diff] [blame] | 1862 | if not ad.droid.telecomCallGetAudioState(): |
| 1863 | ad.log.error("Audio is not in call state") |
| 1864 | result = False |
Betty Zhou | cf04854 | 2018-02-22 17:14:08 -0800 | [diff] [blame] | 1865 | |
Betty Zhou | 7448172 | 2018-04-13 16:19:25 -0700 | [diff] [blame] | 1866 | if call_func(log, ad): |
| 1867 | ad.log.info("Call is in %s state", call_func.__name__) |
| 1868 | else: |
| 1869 | ad.log.error("Call is not in %s state", call_func.__name__) |
| 1870 | result = False |
Betty Zhou | d749f81 | 2018-04-24 12:11:02 -0700 | [diff] [blame] | 1871 | if not result: |
| 1872 | return False |
Yang Liu | 1340629 | 2016-02-23 14:58:25 -0800 | [diff] [blame] | 1873 | elapsed_time = 0 |
Nathan Harold | 7642fc9 | 2016-05-02 18:29:11 -0700 | [diff] [blame] | 1874 | while (elapsed_time < wait_time_in_call): |
| 1875 | CHECK_INTERVAL = min(CHECK_INTERVAL, |
| 1876 | wait_time_in_call - elapsed_time) |
Yang Liu | 1340629 | 2016-02-23 14:58:25 -0800 | [diff] [blame] | 1877 | time.sleep(CHECK_INTERVAL) |
| 1878 | elapsed_time += CHECK_INTERVAL |
Betty Zhou | 9402318 | 2017-06-07 18:02:14 -0700 | [diff] [blame] | 1879 | time_message = "at <%s>/<%s> second." % (elapsed_time, |
| 1880 | wait_time_in_call) |
Betty Zhou | c280199 | 2018-01-30 16:40:28 -0800 | [diff] [blame] | 1881 | for ad, call_func in [(ad_caller, verify_caller_func), |
| 1882 | (ad_callee, verify_callee_func)]: |
| 1883 | if not call_func(log, ad): |
| 1884 | ad.log.error("NOT in correct %s state at %s", |
| 1885 | call_func.__name__, time_message) |
| 1886 | result = False |
| 1887 | else: |
| 1888 | ad.log.info("In correct %s state at %s", |
| 1889 | call_func.__name__, time_message) |
| 1890 | if not ad.droid.telecomCallGetAudioState(): |
| 1891 | ad.log.error("Audio is not in call state at %s", |
| 1892 | time_message) |
| 1893 | result = False |
| 1894 | if not result: |
Betty Zhou | d749f81 | 2018-04-24 12:11:02 -0700 | [diff] [blame] | 1895 | return False |
Betty Zhou | fe726dc | 2018-04-25 19:31:33 -0700 | [diff] [blame] | 1896 | if ad_hangup: |
| 1897 | ad_callee_ids = ad_callee.droid.telecomCallGetCallIds() |
| 1898 | ad_caller_ids = ad_caller.droid.telecomCallGetCallIds() |
| 1899 | if not hangup_call(log, ad_hangup): |
| 1900 | ad_hangup.log.info("Failed to hang up the call") |
| 1901 | result = False |
| 1902 | else: |
| 1903 | if not wait_for_call_id_clearing(ad_caller, ad_caller_ids): |
| 1904 | result = False |
| 1905 | if not wait_for_call_id_clearing(ad_callee, ad_callee_ids): |
| 1906 | result = False |
Betty Zhou | fd1e0da | 2018-02-20 14:39:35 -0800 | [diff] [blame] | 1907 | return result |
| 1908 | finally: |
Betty Zhou | f27f548 | 2017-07-24 18:56:17 -0700 | [diff] [blame] | 1909 | if not result: |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1910 | for ad in [ad_caller, ad_callee]: |
Betty Zhou | a301c20 | 2018-02-13 15:11:47 -0800 | [diff] [blame] | 1911 | reasons = ad.search_logcat( |
| 1912 | "qcril_qmi_voice_map_qmi_to_ril_last_call_failure_cause", |
| 1913 | begin_time) |
| 1914 | if reasons: |
| 1915 | ad.log.info(reasons[-1]["log_message"]) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1916 | try: |
| 1917 | if ad.droid.telecomIsInCall(): |
Betty Zhou | fe726dc | 2018-04-25 19:31:33 -0700 | [diff] [blame] | 1918 | ad.log.info("In call. End now.") |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1919 | ad.droid.telecomEndCall() |
| 1920 | except Exception as e: |
| 1921 | log.error(str(e)) |
| 1922 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1923 | |
Betty Zhou | e7cc367 | 2018-04-26 16:58:56 -0700 | [diff] [blame] | 1924 | def wait_for_call_id_clearing(ad, |
| 1925 | previous_ids, |
| 1926 | timeout=MAX_WAIT_TIME_CALL_DROP): |
Betty Zhou | fe726dc | 2018-04-25 19:31:33 -0700 | [diff] [blame] | 1927 | while timeout > 0: |
| 1928 | new_call_ids = ad.droid.telecomCallGetCallIds() |
| 1929 | if len(new_call_ids) < len(previous_ids): |
| 1930 | return True |
| 1931 | time.sleep(5) |
| 1932 | timeout = timeout - 5 |
Betty Zhou | e7cc367 | 2018-04-26 16:58:56 -0700 | [diff] [blame] | 1933 | ad.log.error("There is no call id clearing: %s vs. %s", previous_ids, |
| 1934 | new_call_ids) |
Betty Zhou | fe726dc | 2018-04-25 19:31:33 -0700 | [diff] [blame] | 1935 | return False |
| 1936 | |
| 1937 | |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 1938 | def phone_number_formatter(input_string, formatter=None): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1939 | """Get expected format of input phone number string. |
| 1940 | |
| 1941 | Args: |
| 1942 | input_string: (string) input phone number. |
| 1943 | The input could be 10/11/12 digital, with or without " "/"-"/"." |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 1944 | formatter: (int) expected format, this could be 7/10/11/12 |
| 1945 | if formatter is 7: output string would be 7 digital number. |
| 1946 | if formatter is 10: output string would be 10 digital (standard) number. |
| 1947 | if formatter is 11: output string would be "1" + 10 digital number. |
| 1948 | if formatter is 12: output string would be "+1" + 10 digital number. |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1949 | |
| 1950 | Returns: |
| 1951 | If no error happen, return phone number in expected format. |
| 1952 | Else, return None. |
| 1953 | """ |
Betty Zhou | ddb361d | 2017-09-07 17:07:20 -0700 | [diff] [blame] | 1954 | if not input_string: |
| 1955 | return "" |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1956 | # make sure input_string is 10 digital |
| 1957 | # Remove white spaces, dashes, dots |
Betty Zhou | e955be2 | 2017-04-12 17:28:05 -0700 | [diff] [blame] | 1958 | input_string = input_string.replace(" ", "").replace("-", "").replace( |
Betty Zhou | 77aa5f5 | 2017-05-08 20:23:35 -0700 | [diff] [blame] | 1959 | ".", "").lstrip("0") |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 1960 | if not formatter: |
Nathan Harold | ae6a0da | 2016-03-16 20:56:47 +0000 | [diff] [blame] | 1961 | return input_string |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1962 | # Remove "1" or "+1"from front |
Betty Zhou | ee31105 | 2017-12-19 13:09:56 -0800 | [diff] [blame] | 1963 | if (len(input_string) == PHONE_NUMBER_STRING_FORMAT_11_DIGIT |
| 1964 | and input_string[0] == "1"): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1965 | input_string = input_string[1:] |
Betty Zhou | ee31105 | 2017-12-19 13:09:56 -0800 | [diff] [blame] | 1966 | elif (len(input_string) == PHONE_NUMBER_STRING_FORMAT_12_DIGIT |
| 1967 | and input_string[0:2] == "+1"): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1968 | input_string = input_string[2:] |
Betty Zhou | ee31105 | 2017-12-19 13:09:56 -0800 | [diff] [blame] | 1969 | elif (len(input_string) == PHONE_NUMBER_STRING_FORMAT_7_DIGIT |
| 1970 | and formatter == PHONE_NUMBER_STRING_FORMAT_7_DIGIT): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1971 | return input_string |
| 1972 | elif len(input_string) != PHONE_NUMBER_STRING_FORMAT_10_DIGIT: |
| 1973 | return None |
| 1974 | # change input_string according to format |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 1975 | if formatter == PHONE_NUMBER_STRING_FORMAT_12_DIGIT: |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1976 | input_string = "+1" + input_string |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 1977 | elif formatter == PHONE_NUMBER_STRING_FORMAT_11_DIGIT: |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1978 | input_string = "1" + input_string |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 1979 | elif formatter == PHONE_NUMBER_STRING_FORMAT_10_DIGIT: |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1980 | input_string = input_string |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 1981 | elif formatter == PHONE_NUMBER_STRING_FORMAT_7_DIGIT: |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1982 | input_string = input_string[3:] |
| 1983 | else: |
| 1984 | return None |
| 1985 | return input_string |
| 1986 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 1987 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 1988 | def get_internet_connection_type(log, ad): |
| 1989 | """Get current active connection type name. |
| 1990 | |
| 1991 | Args: |
| 1992 | log: Log object. |
| 1993 | ad: Android Device Object. |
| 1994 | Returns: |
| 1995 | current active connection type name. |
| 1996 | """ |
| 1997 | if not ad.droid.connectivityNetworkIsConnected(): |
| 1998 | return 'none' |
| 1999 | return connection_type_from_type_string( |
| 2000 | ad.droid.connectivityNetworkGetActiveConnectionTypeName()) |
| 2001 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2002 | |
| 2003 | def verify_http_connection(log, |
| 2004 | ad, |
Betty Zhou | 9f4dfbb | 2018-03-20 16:00:42 -0700 | [diff] [blame] | 2005 | url="https://www.google.com", |
Betty Zhou | f809c5c | 2017-03-21 14:55:59 -0700 | [diff] [blame] | 2006 | retry=5, |
Betty Zhou | 32e403a | 2017-10-25 20:08:12 -0700 | [diff] [blame] | 2007 | retry_interval=15, |
| 2008 | expected_state=True): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2009 | """Make ping request and return status. |
| 2010 | |
| 2011 | Args: |
Nathan Harold | 54c7c74 | 2016-08-04 19:40:44 -0700 | [diff] [blame] | 2012 | log: log object |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2013 | ad: Android Device Object. |
| 2014 | url: Optional. The ping request will be made to this URL. |
| 2015 | Default Value is "http://www.google.com/". |
| 2016 | |
| 2017 | """ |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2018 | for i in range(0, retry + 1): |
Betty Zhou | 9f4dfbb | 2018-03-20 16:00:42 -0700 | [diff] [blame] | 2019 | try: |
| 2020 | http_response = ad.droid.httpPing(url) |
| 2021 | except: |
| 2022 | http_response = None |
Betty Zhou | e7cc367 | 2018-04-26 16:58:56 -0700 | [diff] [blame] | 2023 | ad.log.info("Http ping response for %s is %s, expecting %s", url, |
| 2024 | http_response, expected_state) |
Betty Zhou | 9f4dfbb | 2018-03-20 16:00:42 -0700 | [diff] [blame] | 2025 | if (expected_state and http_response) or (not expected_state |
| 2026 | and not http_response): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2027 | return True |
Betty Zhou | 32e403a | 2017-10-25 20:08:12 -0700 | [diff] [blame] | 2028 | if i < retry: |
Betty Zhou | 32e403a | 2017-10-25 20:08:12 -0700 | [diff] [blame] | 2029 | time.sleep(retry_interval) |
Betty Zhou | e7cc367 | 2018-04-26 16:58:56 -0700 | [diff] [blame] | 2030 | ad.log.error("Http ping to %s is %s after %s second, expecting %s", url, |
| 2031 | http_response, i * retry_interval, expected_state) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2032 | return False |
| 2033 | |
| 2034 | |
Betty Zhou | cea4b43 | 2017-07-20 18:29:24 -0700 | [diff] [blame] | 2035 | def _generate_file_directory_and_file_name(url, out_path): |
Betty Zhou | ccd171d | 2017-02-13 15:11:33 -0800 | [diff] [blame] | 2036 | file_name = url.split("/")[-1] |
| 2037 | if not out_path: |
Betty Zhou | cea4b43 | 2017-07-20 18:29:24 -0700 | [diff] [blame] | 2038 | file_directory = "/sdcard/Download/" |
| 2039 | elif not out_path.endswith("/"): |
| 2040 | file_directory, file_name = os.path.split(out_path) |
Betty Zhou | ccd171d | 2017-02-13 15:11:33 -0800 | [diff] [blame] | 2041 | else: |
Betty Zhou | cea4b43 | 2017-07-20 18:29:24 -0700 | [diff] [blame] | 2042 | file_directory = out_path |
| 2043 | return file_directory, file_name |
Betty Zhou | ccd171d | 2017-02-13 15:11:33 -0800 | [diff] [blame] | 2044 | |
| 2045 | |
Betty Zhou | cea4b43 | 2017-07-20 18:29:24 -0700 | [diff] [blame] | 2046 | def _check_file_existance(ad, file_path, expected_file_size=None): |
| 2047 | """Check file existance by file_path. If expected_file_size |
Betty Zhou | ccd171d | 2017-02-13 15:11:33 -0800 | [diff] [blame] | 2048 | is provided, then also check if the file meet the file size requirement. |
| 2049 | """ |
Girish Moturu | 2c21eb3 | 2017-05-25 14:37:55 +0530 | [diff] [blame] | 2050 | out = None |
| 2051 | try: |
| 2052 | out = ad.adb.shell('stat -c "%%s" %s' % file_path) |
| 2053 | except AdbError: |
| 2054 | pass |
Betty Zhou | ccd171d | 2017-02-13 15:11:33 -0800 | [diff] [blame] | 2055 | # Handle some old version adb returns error message "No such" into std_out |
Betty Zhou | f17f069 | 2017-06-28 13:10:31 -0700 | [diff] [blame] | 2056 | if out and "No such" not in out: |
Betty Zhou | ccd171d | 2017-02-13 15:11:33 -0800 | [diff] [blame] | 2057 | if expected_file_size: |
Betty Zhou | f17f069 | 2017-06-28 13:10:31 -0700 | [diff] [blame] | 2058 | file_size = int(out) |
Betty Zhou | ccd171d | 2017-02-13 15:11:33 -0800 | [diff] [blame] | 2059 | if file_size >= expected_file_size: |
Betty Zhou | cea4b43 | 2017-07-20 18:29:24 -0700 | [diff] [blame] | 2060 | ad.log.info("File %s of size %s exists", file_path, file_size) |
Betty Zhou | ccd171d | 2017-02-13 15:11:33 -0800 | [diff] [blame] | 2061 | return True |
| 2062 | else: |
Betty Zhou | cea4b43 | 2017-07-20 18:29:24 -0700 | [diff] [blame] | 2063 | ad.log.info("File %s is of size %s, does not meet expected %s", |
| 2064 | file_path, file_size, expected_file_size) |
Betty Zhou | ccd171d | 2017-02-13 15:11:33 -0800 | [diff] [blame] | 2065 | return False |
| 2066 | else: |
Betty Zhou | cea4b43 | 2017-07-20 18:29:24 -0700 | [diff] [blame] | 2067 | ad.log.info("File %s exists", file_path) |
Betty Zhou | ccd171d | 2017-02-13 15:11:33 -0800 | [diff] [blame] | 2068 | return True |
| 2069 | else: |
Betty Zhou | cea4b43 | 2017-07-20 18:29:24 -0700 | [diff] [blame] | 2070 | ad.log.info("File %s does not exist.", file_path) |
Betty Zhou | ccd171d | 2017-02-13 15:11:33 -0800 | [diff] [blame] | 2071 | return False |
| 2072 | |
| 2073 | |
Betty Zhou | f1cc3b6 | 2018-02-28 12:02:05 -0800 | [diff] [blame] | 2074 | def check_curl_availability(ad): |
Betty Zhou | f3b7d25 | 2017-07-19 17:51:06 -0700 | [diff] [blame] | 2075 | if not hasattr(ad, "curl_capable"): |
| 2076 | try: |
Jaineel | 3ee3701 | 2017-08-21 15:21:57 -0700 | [diff] [blame] | 2077 | out = ad.adb.shell("/data/curl --version") |
Betty Zhou | f3b7d25 | 2017-07-19 17:51:06 -0700 | [diff] [blame] | 2078 | if not out or "not found" in out: |
| 2079 | setattr(ad, "curl_capable", False) |
| 2080 | ad.log.info("curl is unavailable, use chrome to download file") |
| 2081 | else: |
| 2082 | setattr(ad, "curl_capable", True) |
| 2083 | except Exception: |
| 2084 | setattr(ad, "curl_capable", False) |
| 2085 | ad.log.info("curl is unavailable, use chrome to download file") |
Betty Zhou | f1cc3b6 | 2018-02-28 12:02:05 -0800 | [diff] [blame] | 2086 | return ad.curl_capable |
Betty Zhou | f3b7d25 | 2017-07-19 17:51:06 -0700 | [diff] [blame] | 2087 | |
Betty Zhou | f1cc3b6 | 2018-02-28 12:02:05 -0800 | [diff] [blame] | 2088 | |
Betty Zhou | f50cab3 | 2018-03-05 19:18:15 -0800 | [diff] [blame] | 2089 | def start_youtube_video(ad, url="https://www.youtube.com/watch?v=VHF-XK0Vg1s"): |
| 2090 | ad.log.info("Open an youtube video") |
| 2091 | ad.ensure_screen_on() |
| 2092 | ad.adb.shell('am start -a android.intent.action.VIEW -d "%s"' % url) |
| 2093 | if wait_for_state(ad.droid.audioIsMusicActive, True, 15, 1): |
| 2094 | ad.log.info("Started a video in youtube, audio is in MUSIC_state") |
| 2095 | else: |
| 2096 | ad.log.warning( |
| 2097 | "Started a video in youtube, but audio is not in MUSIC state") |
| 2098 | |
| 2099 | |
Betty Zhou | 3b2de07 | 2018-03-15 16:46:26 -0700 | [diff] [blame] | 2100 | def active_file_download_task(log, ad, file_name="5MB", method="curl"): |
Betty Zhou | 8667192 | 2017-05-10 15:32:10 -0700 | [diff] [blame] | 2101 | # files available for download on the same website: |
| 2102 | # 1GB.zip, 512MB.zip, 200MB.zip, 50MB.zip, 20MB.zip, 10MB.zip, 5MB.zip |
| 2103 | # download file by adb command, as phone call will use sl4a |
Betty Zhou | 8667192 | 2017-05-10 15:32:10 -0700 | [diff] [blame] | 2104 | file_map_dict = { |
| 2105 | '5MB': 5000000, |
| 2106 | '10MB': 10000000, |
| 2107 | '20MB': 20000000, |
| 2108 | '50MB': 50000000, |
Betty Zhou | 461f310 | 2017-08-04 17:04:44 -0700 | [diff] [blame] | 2109 | '100MB': 100000000, |
Betty Zhou | 8667192 | 2017-05-10 15:32:10 -0700 | [diff] [blame] | 2110 | '200MB': 200000000, |
Betty Zhou | 461f310 | 2017-08-04 17:04:44 -0700 | [diff] [blame] | 2111 | '512MB': 512000000 |
Betty Zhou | 8667192 | 2017-05-10 15:32:10 -0700 | [diff] [blame] | 2112 | } |
| 2113 | file_size = file_map_dict.get(file_name) |
| 2114 | if not file_size: |
| 2115 | log.warning("file_name %s for download is not available", file_name) |
| 2116 | return False |
Jaineel | 5719eb5 | 2017-09-12 12:57:07 -0700 | [diff] [blame] | 2117 | timeout = min(max(file_size / 100000, 600), 3600) |
Betty Zhou | 8667192 | 2017-05-10 15:32:10 -0700 | [diff] [blame] | 2118 | output_path = "/sdcard/Download/" + file_name + ".zip" |
Betty Zhou | 3468a8b | 2018-02-14 14:34:56 -0800 | [diff] [blame] | 2119 | url = "http://ipv4.download.thinkbroadband.com/" + file_name + ".zip" |
Betty Zhou | f1cc3b6 | 2018-02-28 12:02:05 -0800 | [diff] [blame] | 2120 | if method == "sl4a": |
| 2121 | return (http_file_download_by_sl4a, (ad, url, output_path, file_size, |
| 2122 | True, timeout)) |
| 2123 | if method == "curl" and check_curl_availability(ad): |
| 2124 | url = "http://146.148.91.8/download/" + file_name + ".zip" |
| 2125 | return (http_file_download_by_curl, (ad, url, output_path, file_size, |
| 2126 | True, timeout)) |
| 2127 | elif method == "sl4a": |
| 2128 | return (http_file_download_by_sl4a, (ad, url, output_path, file_size, |
| 2129 | True, timeout)) |
| 2130 | else: |
| 2131 | return (http_file_download_by_chrome, (ad, url, file_size, True, |
| 2132 | timeout)) |
Betty Zhou | 2e01bc8 | 2017-03-17 10:55:57 -0700 | [diff] [blame] | 2133 | |
| 2134 | |
Betty Zhou | a3f248f | 2018-03-05 17:33:49 -0800 | [diff] [blame] | 2135 | def active_file_download_test(log, ad, file_name="5MB", method="sl4a"): |
Betty Zhou | f1cc3b6 | 2018-02-28 12:02:05 -0800 | [diff] [blame] | 2136 | task = active_file_download_task(log, ad, file_name, method=method) |
Betty Zhou | 2e01bc8 | 2017-03-17 10:55:57 -0700 | [diff] [blame] | 2137 | return task[0](*task[1]) |
| 2138 | |
| 2139 | |
Betty Zhou | 9f4dfbb | 2018-03-20 16:00:42 -0700 | [diff] [blame] | 2140 | def verify_internet_connection_by_ping(log, ad, retries=1, |
| 2141 | expected_state=True): |
Betty Zhou | 8e11e44 | 2017-03-30 20:00:34 -0700 | [diff] [blame] | 2142 | """Verify internet connection by ping test. |
| 2143 | |
| 2144 | Args: |
| 2145 | log: log object |
| 2146 | ad: Android Device Object. |
| 2147 | |
| 2148 | """ |
Betty Zhou | 9f4dfbb | 2018-03-20 16:00:42 -0700 | [diff] [blame] | 2149 | ip_addr = "54.230.144.105" |
| 2150 | for dest in ("www.google.com", "www.amazon.com", ip_addr): |
| 2151 | for i in range(retries): |
| 2152 | ad.log.info("Ping %s - attempt %d", dest, i + 1) |
Jaineel | 6d74772 | 2017-11-20 15:03:02 -0800 | [diff] [blame] | 2153 | result = adb_shell_ping( |
| 2154 | ad, count=5, timeout=60, loss_tolerance=40, dest_ip=dest) |
Betty Zhou | 9f4dfbb | 2018-03-20 16:00:42 -0700 | [diff] [blame] | 2155 | if result == expected_state: |
| 2156 | ad.log.info( |
Betty Zhou | dd9a9ea | 2018-04-04 13:23:56 -0700 | [diff] [blame] | 2157 | "Internet connection by pinging to %s is %s as expected", |
Betty Zhou | 9f4dfbb | 2018-03-20 16:00:42 -0700 | [diff] [blame] | 2158 | dest, expected_state) |
| 2159 | if dest == ip_addr: |
| 2160 | ad.log.warning("Suspect dns failure") |
| 2161 | ad.log.info("DNS config: %s", |
Betty Zhou | dd9a9ea | 2018-04-04 13:23:56 -0700 | [diff] [blame] | 2162 | ad.adb.shell("getprop | grep dns").replace( |
| 2163 | "\n", " ")) |
| 2164 | return False |
Jaineel | 6d74772 | 2017-11-20 15:03:02 -0800 | [diff] [blame] | 2165 | return True |
Betty Zhou | 9f4dfbb | 2018-03-20 16:00:42 -0700 | [diff] [blame] | 2166 | else: |
| 2167 | ad.log.error( |
Betty Zhou | dd9a9ea | 2018-04-04 13:23:56 -0700 | [diff] [blame] | 2168 | "Internet connection test by pinging %s is %s, expecting %s", |
| 2169 | dest, result, expected_state) |
Betty Zhou | 9f4dfbb | 2018-03-20 16:00:42 -0700 | [diff] [blame] | 2170 | return False |
| 2171 | |
| 2172 | |
| 2173 | def verify_internet_connection(log, ad, retries=3, expected_state=True): |
| 2174 | """Verify internet connection by ping test and http connection. |
| 2175 | |
| 2176 | Args: |
| 2177 | log: log object |
| 2178 | ad: Android Device Object. |
| 2179 | |
| 2180 | """ |
| 2181 | if verify_internet_connection_by_ping( |
| 2182 | log, ad, retries=retries, expected_state=expected_state): |
| 2183 | return True |
| 2184 | for url in ("https://www.google.com", "https://www.amazon.com"): |
| 2185 | if verify_http_connection( |
| 2186 | log, ad, url=url, retry=retries, |
| 2187 | expected_state=expected_state): |
| 2188 | return True |
Betty Zhou | e123c67 | 2018-03-21 19:57:11 -0700 | [diff] [blame] | 2189 | ad.log.info("DNS config: %s", " ".join( |
| 2190 | ad.adb.shell("getprop | grep dns").split())) |
Betty Zhou | fe726dc | 2018-04-25 19:31:33 -0700 | [diff] [blame] | 2191 | ad.log.info("Interface info:\n%s", ad.adb.shell("ifconfig")) |
Betty Zhou | e123c67 | 2018-03-21 19:57:11 -0700 | [diff] [blame] | 2192 | ad.log.info("NetworkAgentInfo: %s", |
| 2193 | ad.adb.shell("dumpsys connectivity | grep NetworkAgentInfo")) |
Jaineel | 6c576c9 | 2017-09-11 10:02:23 -0700 | [diff] [blame] | 2194 | return False |
Betty Zhou | 8e11e44 | 2017-03-30 20:00:34 -0700 | [diff] [blame] | 2195 | |
| 2196 | |
Betty Zhou | 2e01bc8 | 2017-03-17 10:55:57 -0700 | [diff] [blame] | 2197 | def iperf_test_by_adb(log, |
| 2198 | ad, |
| 2199 | iperf_server, |
Jaineel | 6f88b5b | 2017-04-17 09:58:46 -0700 | [diff] [blame] | 2200 | port_num=None, |
| 2201 | reverse=False, |
Betty Zhou | 2e01bc8 | 2017-03-17 10:55:57 -0700 | [diff] [blame] | 2202 | timeout=180, |
Jaineel | 6f88b5b | 2017-04-17 09:58:46 -0700 | [diff] [blame] | 2203 | limit_rate=None, |
Betty Zhou | 2e01bc8 | 2017-03-17 10:55:57 -0700 | [diff] [blame] | 2204 | omit=10, |
Jaineel | e74e8e1 | 2017-05-08 15:59:42 -0700 | [diff] [blame] | 2205 | ipv6=False, |
Patrick Chiang | 75b8986 | 2017-10-13 17:02:29 -0700 | [diff] [blame] | 2206 | rate_dict=None, |
| 2207 | blocking=True, |
| 2208 | log_file_path=None): |
Betty Zhou | 2e01bc8 | 2017-03-17 10:55:57 -0700 | [diff] [blame] | 2209 | """Iperf test by adb. |
| 2210 | |
| 2211 | Args: |
| 2212 | log: log object |
| 2213 | ad: Android Device Object. |
Jaineel | 6f88b5b | 2017-04-17 09:58:46 -0700 | [diff] [blame] | 2214 | iperf_Server: The iperf host url". |
| 2215 | port_num: TCP/UDP server port |
Betty Zhou | 2e01bc8 | 2017-03-17 10:55:57 -0700 | [diff] [blame] | 2216 | timeout: timeout for file download to complete. |
| 2217 | limit_rate: iperf bandwidth option. None by default |
| 2218 | omit: the omit option provided in iperf command. |
| 2219 | """ |
| 2220 | iperf_option = "-t %s -O %s -J" % (timeout, omit) |
| 2221 | if limit_rate: iperf_option += " -b %s" % limit_rate |
Jaineel | 6f88b5b | 2017-04-17 09:58:46 -0700 | [diff] [blame] | 2222 | if port_num: iperf_option += " -p %s" % port_num |
Betty Zhou | 2e01bc8 | 2017-03-17 10:55:57 -0700 | [diff] [blame] | 2223 | if ipv6: iperf_option += " -6" |
Jaineel | 6f88b5b | 2017-04-17 09:58:46 -0700 | [diff] [blame] | 2224 | if reverse: iperf_option += " -R" |
Betty Zhou | 2e01bc8 | 2017-03-17 10:55:57 -0700 | [diff] [blame] | 2225 | try: |
Patrick Chiang | 75b8986 | 2017-10-13 17:02:29 -0700 | [diff] [blame] | 2226 | if log_file_path: |
| 2227 | ad.adb.shell("rm %s" % log_file_path, ignore_status=True) |
Betty Zhou | 2e01bc8 | 2017-03-17 10:55:57 -0700 | [diff] [blame] | 2228 | ad.log.info("Running adb iperf test with server %s", iperf_server) |
Patrick Chiang | 75b8986 | 2017-10-13 17:02:29 -0700 | [diff] [blame] | 2229 | if not blocking: |
| 2230 | ad.run_iperf_client_nb( |
| 2231 | iperf_server, |
| 2232 | iperf_option, |
| 2233 | timeout=timeout + 60, |
| 2234 | log_file_path=log_file_path) |
| 2235 | return True |
Betty Zhou | 2e01bc8 | 2017-03-17 10:55:57 -0700 | [diff] [blame] | 2236 | result, data = ad.run_iperf_client( |
| 2237 | iperf_server, iperf_option, timeout=timeout + 60) |
| 2238 | ad.log.info("Iperf test result with server %s is %s", iperf_server, |
| 2239 | result) |
| 2240 | if result: |
| 2241 | data_json = json.loads(''.join(data)) |
| 2242 | tx_rate = data_json['end']['sum_sent']['bits_per_second'] |
| 2243 | rx_rate = data_json['end']['sum_received']['bits_per_second'] |
| 2244 | ad.log.info( |
| 2245 | 'iPerf3 upload speed is %sbps, download speed is %sbps', |
| 2246 | tx_rate, rx_rate) |
Jaineel | e74e8e1 | 2017-05-08 15:59:42 -0700 | [diff] [blame] | 2247 | if rate_dict is not None: |
| 2248 | rate_dict["Uplink"] = tx_rate |
| 2249 | rate_dict["Downlink"] = rx_rate |
Betty Zhou | 2e01bc8 | 2017-03-17 10:55:57 -0700 | [diff] [blame] | 2250 | return result |
| 2251 | except Exception as e: |
Betty Zhou | f809c5c | 2017-03-21 14:55:59 -0700 | [diff] [blame] | 2252 | ad.log.warning("Fail to run iperf test with exception %s", e) |
Betty Zhou | 2e01bc8 | 2017-03-17 10:55:57 -0700 | [diff] [blame] | 2253 | return False |
| 2254 | |
Betty Zhou | e955be2 | 2017-04-12 17:28:05 -0700 | [diff] [blame] | 2255 | |
Betty Zhou | 8667192 | 2017-05-10 15:32:10 -0700 | [diff] [blame] | 2256 | def http_file_download_by_curl(ad, |
| 2257 | url, |
| 2258 | out_path=None, |
| 2259 | expected_file_size=None, |
| 2260 | remove_file_after_check=True, |
Betty Zhou | 35ea595 | 2017-06-26 10:01:09 -0700 | [diff] [blame] | 2261 | timeout=3600, |
Betty Zhou | 8667192 | 2017-05-10 15:32:10 -0700 | [diff] [blame] | 2262 | limit_rate=None, |
| 2263 | retry=3): |
Betty Zhou | ccd171d | 2017-02-13 15:11:33 -0800 | [diff] [blame] | 2264 | """Download http file by adb curl. |
| 2265 | |
| 2266 | Args: |
Betty Zhou | ccd171d | 2017-02-13 15:11:33 -0800 | [diff] [blame] | 2267 | ad: Android Device Object. |
| 2268 | url: The url that file to be downloaded from". |
| 2269 | out_path: Optional. Where to download file to. |
| 2270 | out_path is /sdcard/Download/ by default. |
| 2271 | expected_file_size: Optional. Provided if checking the download file meet |
| 2272 | expected file size in unit of byte. |
| 2273 | remove_file_after_check: Whether to remove the downloaded file after |
| 2274 | check. |
| 2275 | timeout: timeout for file download to complete. |
| 2276 | limit_rate: download rate in bps. None, if do not apply rate limit. |
| 2277 | retry: the retry request times provided in curl command. |
| 2278 | """ |
Betty Zhou | cea4b43 | 2017-07-20 18:29:24 -0700 | [diff] [blame] | 2279 | file_directory, file_name = _generate_file_directory_and_file_name( |
| 2280 | url, out_path) |
| 2281 | file_path = os.path.join(file_directory, file_name) |
Jaineel | 3ee3701 | 2017-08-21 15:21:57 -0700 | [diff] [blame] | 2282 | curl_cmd = "/data/curl" |
Betty Zhou | ccd171d | 2017-02-13 15:11:33 -0800 | [diff] [blame] | 2283 | if limit_rate: |
| 2284 | curl_cmd += " --limit-rate %s" % limit_rate |
| 2285 | if retry: |
| 2286 | curl_cmd += " --retry %s" % retry |
Betty Zhou | cea4b43 | 2017-07-20 18:29:24 -0700 | [diff] [blame] | 2287 | curl_cmd += " --url %s > %s" % (url, file_path) |
Betty Zhou | ccd171d | 2017-02-13 15:11:33 -0800 | [diff] [blame] | 2288 | try: |
Betty Zhou | cea4b43 | 2017-07-20 18:29:24 -0700 | [diff] [blame] | 2289 | ad.log.info("Download %s to %s by adb shell command %s", url, |
| 2290 | file_path, curl_cmd) |
Betty Zhou | ccd171d | 2017-02-13 15:11:33 -0800 | [diff] [blame] | 2291 | ad.adb.shell(curl_cmd, timeout=timeout) |
Betty Zhou | cea4b43 | 2017-07-20 18:29:24 -0700 | [diff] [blame] | 2292 | if _check_file_existance(ad, file_path, expected_file_size): |
| 2293 | ad.log.info("%s is downloaded to %s successfully", url, file_path) |
Betty Zhou | e57ab69 | 2018-03-09 18:39:30 -0800 | [diff] [blame] | 2294 | return True |
Betty Zhou | ccd171d | 2017-02-13 15:11:33 -0800 | [diff] [blame] | 2295 | else: |
Betty Zhou | cea4b43 | 2017-07-20 18:29:24 -0700 | [diff] [blame] | 2296 | ad.log.warning("Fail to download %s", url) |
Betty Zhou | ccd171d | 2017-02-13 15:11:33 -0800 | [diff] [blame] | 2297 | return False |
| 2298 | except Exception as e: |
Betty Zhou | cea4b43 | 2017-07-20 18:29:24 -0700 | [diff] [blame] | 2299 | ad.log.warning("Download %s failed with exception %s", url, e) |
Betty Zhou | ccd171d | 2017-02-13 15:11:33 -0800 | [diff] [blame] | 2300 | return False |
| 2301 | finally: |
| 2302 | if remove_file_after_check: |
Betty Zhou | cea4b43 | 2017-07-20 18:29:24 -0700 | [diff] [blame] | 2303 | ad.log.info("Remove the downloaded file %s", file_path) |
| 2304 | ad.adb.shell("rm %s" % file_path, ignore_status=True) |
| 2305 | |
| 2306 | |
| 2307 | def open_url_by_adb(ad, url): |
Betty Zhou | 8da0378 | 2017-07-28 18:02:24 -0700 | [diff] [blame] | 2308 | ad.adb.shell('am start -a android.intent.action.VIEW -d "%s"' % url) |
Betty Zhou | ccd171d | 2017-02-13 15:11:33 -0800 | [diff] [blame] | 2309 | |
| 2310 | |
Betty Zhou | 8667192 | 2017-05-10 15:32:10 -0700 | [diff] [blame] | 2311 | def http_file_download_by_chrome(ad, |
| 2312 | url, |
| 2313 | expected_file_size=None, |
| 2314 | remove_file_after_check=True, |
Betty Zhou | 35ea595 | 2017-06-26 10:01:09 -0700 | [diff] [blame] | 2315 | timeout=3600): |
Betty Zhou | 8667192 | 2017-05-10 15:32:10 -0700 | [diff] [blame] | 2316 | """Download http file by chrome. |
| 2317 | |
| 2318 | Args: |
| 2319 | ad: Android Device Object. |
| 2320 | url: The url that file to be downloaded from". |
Betty Zhou | 8667192 | 2017-05-10 15:32:10 -0700 | [diff] [blame] | 2321 | expected_file_size: Optional. Provided if checking the download file meet |
| 2322 | expected file size in unit of byte. |
| 2323 | remove_file_after_check: Whether to remove the downloaded file after |
| 2324 | check. |
| 2325 | timeout: timeout for file download to complete. |
| 2326 | """ |
Betty Zhou | 9243770 | 2018-02-07 19:49:07 -0800 | [diff] [blame] | 2327 | chrome_apk = "com.android.chrome" |
Betty Zhou | cea4b43 | 2017-07-20 18:29:24 -0700 | [diff] [blame] | 2328 | file_directory, file_name = _generate_file_directory_and_file_name( |
Betty Zhou | bcffe44 | 2017-07-05 17:27:55 -0700 | [diff] [blame] | 2329 | url, "/sdcard/Download/") |
Betty Zhou | cea4b43 | 2017-07-20 18:29:24 -0700 | [diff] [blame] | 2330 | file_path = os.path.join(file_directory, file_name) |
Betty Zhou | f17f069 | 2017-06-28 13:10:31 -0700 | [diff] [blame] | 2331 | # Remove pre-existing file |
Betty Zhou | 9243770 | 2018-02-07 19:49:07 -0800 | [diff] [blame] | 2332 | ad.force_stop_apk(chrome_apk) |
markdr | 6607bf1 | 2018-01-02 14:45:38 -0800 | [diff] [blame] | 2333 | file_to_be_delete = os.path.join(file_directory, "*%s*" % file_name) |
Betty Zhou | e5f61b3 | 2018-01-26 10:48:00 -0800 | [diff] [blame] | 2334 | ad.adb.shell("rm -f %s" % file_to_be_delete) |
| 2335 | ad.adb.shell("rm -rf /sdcard/Download/.*") |
| 2336 | ad.adb.shell("rm -f /sdcard/Download/.*") |
Betty Zhou | 9243770 | 2018-02-07 19:49:07 -0800 | [diff] [blame] | 2337 | data_accounting = { |
Betty Zhou | f1cc3b6 | 2018-02-28 12:02:05 -0800 | [diff] [blame] | 2338 | "total_rx_bytes": ad.droid.getTotalRxBytes(), |
| 2339 | "mobile_rx_bytes": ad.droid.getMobileRxBytes(), |
| 2340 | "subscriber_mobile_data_usage": get_mobile_data_usage(ad, None, None), |
| 2341 | "chrome_mobile_data_usage": get_mobile_data_usage( |
| 2342 | ad, None, chrome_apk) |
Betty Zhou | 02571f5 | 2018-02-16 14:11:16 -0800 | [diff] [blame] | 2343 | } |
Betty Zhou | 9243770 | 2018-02-07 19:49:07 -0800 | [diff] [blame] | 2344 | ad.log.info("Before downloading: %s", data_accounting) |
Betty Zhou | cea4b43 | 2017-07-20 18:29:24 -0700 | [diff] [blame] | 2345 | ad.log.info("Download %s with timeout %s", url, timeout) |
Betty Zhou | c80af15 | 2018-04-27 18:22:17 -0700 | [diff] [blame] | 2346 | ad.ensure_screen_on() |
Betty Zhou | cea4b43 | 2017-07-20 18:29:24 -0700 | [diff] [blame] | 2347 | open_url_by_adb(ad, url) |
Betty Zhou | 8667192 | 2017-05-10 15:32:10 -0700 | [diff] [blame] | 2348 | elapse_time = 0 |
Betty Zhou | 9243770 | 2018-02-07 19:49:07 -0800 | [diff] [blame] | 2349 | result = True |
Betty Zhou | 8667192 | 2017-05-10 15:32:10 -0700 | [diff] [blame] | 2350 | while elapse_time < timeout: |
| 2351 | time.sleep(30) |
Betty Zhou | cea4b43 | 2017-07-20 18:29:24 -0700 | [diff] [blame] | 2352 | if _check_file_existance(ad, file_path, expected_file_size): |
| 2353 | ad.log.info("%s is downloaded successfully", url) |
Betty Zhou | 8667192 | 2017-05-10 15:32:10 -0700 | [diff] [blame] | 2354 | if remove_file_after_check: |
Betty Zhou | cea4b43 | 2017-07-20 18:29:24 -0700 | [diff] [blame] | 2355 | ad.log.info("Remove the downloaded file %s", file_path) |
Betty Zhou | e5f61b3 | 2018-01-26 10:48:00 -0800 | [diff] [blame] | 2356 | ad.adb.shell("rm -f %s" % file_to_be_delete) |
| 2357 | ad.adb.shell("rm -rf /sdcard/Download/.*") |
| 2358 | ad.adb.shell("rm -f /sdcard/Download/.*") |
Betty Zhou | 08c1a57 | 2018-02-13 20:07:55 -0800 | [diff] [blame] | 2359 | #time.sleep(30) |
Betty Zhou | 9243770 | 2018-02-07 19:49:07 -0800 | [diff] [blame] | 2360 | new_data_accounting = { |
Betty Zhou | 02571f5 | 2018-02-16 14:11:16 -0800 | [diff] [blame] | 2361 | "mobile_rx_bytes": |
| 2362 | ad.droid.getMobileRxBytes(), |
| 2363 | "subscriber_mobile_data_usage": |
Betty Zhou | f1cc3b6 | 2018-02-28 12:02:05 -0800 | [diff] [blame] | 2364 | get_mobile_data_usage(ad, None, None), |
Betty Zhou | 02571f5 | 2018-02-16 14:11:16 -0800 | [diff] [blame] | 2365 | "chrome_mobile_data_usage": |
Betty Zhou | f1cc3b6 | 2018-02-28 12:02:05 -0800 | [diff] [blame] | 2366 | get_mobile_data_usage(ad, None, chrome_apk) |
Betty Zhou | 9243770 | 2018-02-07 19:49:07 -0800 | [diff] [blame] | 2367 | } |
| 2368 | ad.log.info("After downloading: %s", new_data_accounting) |
Betty Zhou | 02571f5 | 2018-02-16 14:11:16 -0800 | [diff] [blame] | 2369 | accounting_diff = { |
| 2370 | key: value - data_accounting[key] |
| 2371 | for key, value in new_data_accounting.items() |
| 2372 | } |
Betty Zhou | 9243770 | 2018-02-07 19:49:07 -0800 | [diff] [blame] | 2373 | ad.log.info("Data accounting difference: %s", accounting_diff) |
Betty Zhou | 1b302fd | 2017-11-17 11:43:09 -0800 | [diff] [blame] | 2374 | if getattr(ad, "on_mobile_data", False): |
Betty Zhou | 9243770 | 2018-02-07 19:49:07 -0800 | [diff] [blame] | 2375 | for key, value in accounting_diff.items(): |
| 2376 | if value < expected_file_size: |
Betty Zhou | 02571f5 | 2018-02-16 14:11:16 -0800 | [diff] [blame] | 2377 | ad.log.warning("%s diff is %s less than %s", key, |
| 2378 | value, expected_file_size) |
Betty Zhou | 9243770 | 2018-02-07 19:49:07 -0800 | [diff] [blame] | 2379 | ad.data_accounting["%s_failure" % key] += 1 |
Betty Zhou | 1b302fd | 2017-11-17 11:43:09 -0800 | [diff] [blame] | 2380 | else: |
Betty Zhou | 9243770 | 2018-02-07 19:49:07 -0800 | [diff] [blame] | 2381 | for key, value in accounting_diff.items(): |
| 2382 | if value >= expected_file_size: |
| 2383 | ad.log.error("%s diff is %s. File download is " |
| 2384 | "consuming mobile data", key, value) |
| 2385 | result = False |
| 2386 | return result |
Betty Zhou | cea4b43 | 2017-07-20 18:29:24 -0700 | [diff] [blame] | 2387 | elif _check_file_existance(ad, "%s.crdownload" % file_path): |
| 2388 | ad.log.info("Chrome is downloading %s", url) |
| 2389 | elif elapse_time < 60: |
| 2390 | # download not started, retry download wit chrome again |
| 2391 | open_url_by_adb(ad, url) |
Betty Zhou | 8667192 | 2017-05-10 15:32:10 -0700 | [diff] [blame] | 2392 | else: |
Betty Zhou | cea4b43 | 2017-07-20 18:29:24 -0700 | [diff] [blame] | 2393 | ad.log.error("Unable to download file from %s", url) |
| 2394 | break |
| 2395 | elapse_time += 30 |
Betty Zhou | f336601 | 2017-11-21 18:23:20 -0800 | [diff] [blame] | 2396 | ad.log.warning("Fail to download file from %s", url) |
Betty Zhou | 95ed186 | 2017-07-21 13:29:02 -0700 | [diff] [blame] | 2397 | ad.force_stop_apk("com.android.chrome") |
Betty Zhou | e5f61b3 | 2018-01-26 10:48:00 -0800 | [diff] [blame] | 2398 | ad.adb.shell("rm -f %s" % file_to_be_delete) |
| 2399 | ad.adb.shell("rm -rf /sdcard/Download/.*") |
| 2400 | ad.adb.shell("rm -f /sdcard/Download/.*") |
Betty Zhou | 8667192 | 2017-05-10 15:32:10 -0700 | [diff] [blame] | 2401 | return False |
| 2402 | |
| 2403 | |
Betty Zhou | 9243770 | 2018-02-07 19:49:07 -0800 | [diff] [blame] | 2404 | def http_file_download_by_sl4a(ad, |
Betty Zhou | ccd171d | 2017-02-13 15:11:33 -0800 | [diff] [blame] | 2405 | url, |
| 2406 | out_path=None, |
| 2407 | expected_file_size=None, |
| 2408 | remove_file_after_check=True, |
| 2409 | timeout=300): |
| 2410 | """Download http file by sl4a RPC call. |
| 2411 | |
| 2412 | Args: |
Betty Zhou | ccd171d | 2017-02-13 15:11:33 -0800 | [diff] [blame] | 2413 | ad: Android Device Object. |
| 2414 | url: The url that file to be downloaded from". |
| 2415 | out_path: Optional. Where to download file to. |
| 2416 | out_path is /sdcard/Download/ by default. |
| 2417 | expected_file_size: Optional. Provided if checking the download file meet |
| 2418 | expected file size in unit of byte. |
| 2419 | remove_file_after_check: Whether to remove the downloaded file after |
| 2420 | check. |
| 2421 | timeout: timeout for file download to complete. |
| 2422 | """ |
Betty Zhou | ee31105 | 2017-12-19 13:09:56 -0800 | [diff] [blame] | 2423 | file_folder, file_name = _generate_file_directory_and_file_name( |
| 2424 | url, out_path) |
Betty Zhou | cea4b43 | 2017-07-20 18:29:24 -0700 | [diff] [blame] | 2425 | file_path = os.path.join(file_folder, file_name) |
Betty Zhou | 08c1a57 | 2018-02-13 20:07:55 -0800 | [diff] [blame] | 2426 | ad.adb.shell("rm -f %s" % file_path) |
Betty Zhou | 9243770 | 2018-02-07 19:49:07 -0800 | [diff] [blame] | 2427 | accounting_apk = SL4A_APK_NAME |
| 2428 | result = True |
Betty Zhou | ccd171d | 2017-02-13 15:11:33 -0800 | [diff] [blame] | 2429 | try: |
Betty Zhou | 08c1a57 | 2018-02-13 20:07:55 -0800 | [diff] [blame] | 2430 | if not getattr(ad, "downloading_droid", None): |
| 2431 | ad.downloading_droid, ad.downloading_ed = ad.get_droid() |
| 2432 | ad.downloading_ed.start() |
Betty Zhou | f31dffe | 2018-02-23 19:29:28 -0800 | [diff] [blame] | 2433 | else: |
| 2434 | try: |
| 2435 | if not ad.downloading_droid.is_live: |
| 2436 | ad.downloading_droid, ad.downloading_ed = ad.get_droid() |
| 2437 | ad.downloading_ed.start() |
Betty Zhou | e57ab69 | 2018-03-09 18:39:30 -0800 | [diff] [blame] | 2438 | except Exception: |
| 2439 | ad.log.info("Start new sl4a session for file download") |
Betty Zhou | f31dffe | 2018-02-23 19:29:28 -0800 | [diff] [blame] | 2440 | ad.downloading_droid, ad.downloading_ed = ad.get_droid() |
| 2441 | ad.downloading_ed.start() |
Betty Zhou | 9243770 | 2018-02-07 19:49:07 -0800 | [diff] [blame] | 2442 | data_accounting = { |
Betty Zhou | 02571f5 | 2018-02-16 14:11:16 -0800 | [diff] [blame] | 2443 | "mobile_rx_bytes": |
| 2444 | ad.droid.getMobileRxBytes(), |
| 2445 | "subscriber_mobile_data_usage": |
Betty Zhou | f1cc3b6 | 2018-02-28 12:02:05 -0800 | [diff] [blame] | 2446 | get_mobile_data_usage(ad, None, None), |
Betty Zhou | 02571f5 | 2018-02-16 14:11:16 -0800 | [diff] [blame] | 2447 | "sl4a_mobile_data_usage": |
Betty Zhou | f1cc3b6 | 2018-02-28 12:02:05 -0800 | [diff] [blame] | 2448 | get_mobile_data_usage(ad, None, accounting_apk) |
Betty Zhou | 02571f5 | 2018-02-16 14:11:16 -0800 | [diff] [blame] | 2449 | } |
Betty Zhou | 9243770 | 2018-02-07 19:49:07 -0800 | [diff] [blame] | 2450 | ad.log.info("Before downloading: %s", data_accounting) |
Betty Zhou | ccd171d | 2017-02-13 15:11:33 -0800 | [diff] [blame] | 2451 | ad.log.info("Download file from %s to %s by sl4a RPC call", url, |
Betty Zhou | cea4b43 | 2017-07-20 18:29:24 -0700 | [diff] [blame] | 2452 | file_path) |
Betty Zhou | 14c0837 | 2018-02-15 10:26:23 -0800 | [diff] [blame] | 2453 | try: |
Betty Zhou | 02571f5 | 2018-02-16 14:11:16 -0800 | [diff] [blame] | 2454 | ad.downloading_droid.httpDownloadFile( |
| 2455 | url, file_path, timeout=timeout) |
Betty Zhou | 14c0837 | 2018-02-15 10:26:23 -0800 | [diff] [blame] | 2456 | except Exception as e: |
| 2457 | ad.log.warning("SL4A file download error: %s", e) |
| 2458 | return False |
Betty Zhou | cea4b43 | 2017-07-20 18:29:24 -0700 | [diff] [blame] | 2459 | if _check_file_existance(ad, file_path, expected_file_size): |
| 2460 | ad.log.info("%s is downloaded successfully", url) |
Betty Zhou | 9243770 | 2018-02-07 19:49:07 -0800 | [diff] [blame] | 2461 | new_data_accounting = { |
Betty Zhou | 02571f5 | 2018-02-16 14:11:16 -0800 | [diff] [blame] | 2462 | "mobile_rx_bytes": |
| 2463 | ad.droid.getMobileRxBytes(), |
| 2464 | "subscriber_mobile_data_usage": |
Betty Zhou | f1cc3b6 | 2018-02-28 12:02:05 -0800 | [diff] [blame] | 2465 | get_mobile_data_usage(ad, None, None), |
Betty Zhou | 02571f5 | 2018-02-16 14:11:16 -0800 | [diff] [blame] | 2466 | "sl4a_mobile_data_usage": |
Betty Zhou | f1cc3b6 | 2018-02-28 12:02:05 -0800 | [diff] [blame] | 2467 | get_mobile_data_usage(ad, None, accounting_apk) |
Betty Zhou | 9243770 | 2018-02-07 19:49:07 -0800 | [diff] [blame] | 2468 | } |
| 2469 | ad.log.info("After downloading: %s", new_data_accounting) |
Betty Zhou | 02571f5 | 2018-02-16 14:11:16 -0800 | [diff] [blame] | 2470 | accounting_diff = { |
| 2471 | key: value - data_accounting[key] |
| 2472 | for key, value in new_data_accounting.items() |
| 2473 | } |
Betty Zhou | 9243770 | 2018-02-07 19:49:07 -0800 | [diff] [blame] | 2474 | ad.log.info("Data accounting difference: %s", accounting_diff) |
| 2475 | if getattr(ad, "on_mobile_data", False): |
| 2476 | for key, value in accounting_diff.items(): |
| 2477 | if value < expected_file_size: |
Betty Zhou | 02571f5 | 2018-02-16 14:11:16 -0800 | [diff] [blame] | 2478 | ad.log.warning("%s diff is %s less than %s", key, |
| 2479 | value, expected_file_size) |
Betty Zhou | 9243770 | 2018-02-07 19:49:07 -0800 | [diff] [blame] | 2480 | ad.data_accounting["%s_failure"] += 1 |
| 2481 | else: |
| 2482 | for key, value in accounting_diff.items(): |
| 2483 | if value >= expected_file_size: |
| 2484 | ad.log.error("%s diff is %s. File download is " |
| 2485 | "consuming mobile data", key, value) |
| 2486 | result = False |
| 2487 | return result |
Betty Zhou | ccd171d | 2017-02-13 15:11:33 -0800 | [diff] [blame] | 2488 | else: |
Betty Zhou | cea4b43 | 2017-07-20 18:29:24 -0700 | [diff] [blame] | 2489 | ad.log.warning("Fail to download %s", url) |
Betty Zhou | ccd171d | 2017-02-13 15:11:33 -0800 | [diff] [blame] | 2490 | return False |
| 2491 | except Exception as e: |
Betty Zhou | cea4b43 | 2017-07-20 18:29:24 -0700 | [diff] [blame] | 2492 | ad.log.error("Download %s failed with exception %s", url, e) |
Betty Zhou | ccd171d | 2017-02-13 15:11:33 -0800 | [diff] [blame] | 2493 | raise |
| 2494 | finally: |
| 2495 | if remove_file_after_check: |
Betty Zhou | cea4b43 | 2017-07-20 18:29:24 -0700 | [diff] [blame] | 2496 | ad.log.info("Remove the downloaded file %s", file_path) |
| 2497 | ad.adb.shell("rm %s" % file_path, ignore_status=True) |
Betty Zhou | ccd171d | 2017-02-13 15:11:33 -0800 | [diff] [blame] | 2498 | |
Betty Zhou | 2cf788e | 2017-06-27 17:25:53 -0700 | [diff] [blame] | 2499 | |
Betty Zhou | f1cc3b6 | 2018-02-28 12:02:05 -0800 | [diff] [blame] | 2500 | def get_mobile_data_usage(ad, subscriber_id=None, apk=None): |
Betty Zhou | f336601 | 2017-11-21 18:23:20 -0800 | [diff] [blame] | 2501 | if not subscriber_id: |
| 2502 | subscriber_id = ad.droid.telephonyGetSubscriberId() |
Betty Zhou | a3f248f | 2018-03-05 17:33:49 -0800 | [diff] [blame] | 2503 | |
Betty Zhou | f1cc3b6 | 2018-02-28 12:02:05 -0800 | [diff] [blame] | 2504 | if not getattr(ad, "data_metering_begin_time", None) or not getattr( |
| 2505 | ad, "data_metering_end_time", None): |
| 2506 | current_time = int(time.time() * 1000) |
| 2507 | setattr(ad, "data_metering_begin_time", |
| 2508 | current_time - 24 * 60 * 60 * 1000) |
| 2509 | setattr(ad, "data_metering_end_time", |
| 2510 | current_time + 30 * 24 * 60 * 60 * 1000) |
| 2511 | begin_time = ad.data_metering_begin_time |
| 2512 | end_time = ad.data_metering_end_time |
Betty Zhou | a3f248f | 2018-03-05 17:33:49 -0800 | [diff] [blame] | 2513 | |
Betty Zhou | 9243770 | 2018-02-07 19:49:07 -0800 | [diff] [blame] | 2514 | if apk: |
| 2515 | uid = ad.get_apk_uid(apk) |
Betty Zhou | a3f248f | 2018-03-05 17:33:49 -0800 | [diff] [blame] | 2516 | ad.log.info("apk %s uid = %s", apk, uid) |
| 2517 | func = ad.droid.connectivityQueryDetailsForUid |
| 2518 | func_args = [[TYPE_MOBILE, subscriber_id, begin_time, end_time, uid], |
| 2519 | [subscriber_id, begin_time, end_time, uid]] |
Betty Zhou | 9243770 | 2018-02-07 19:49:07 -0800 | [diff] [blame] | 2520 | else: |
Betty Zhou | a3f248f | 2018-03-05 17:33:49 -0800 | [diff] [blame] | 2521 | func = ad.droid.connectivityQuerySummaryForDevice |
| 2522 | func_args = [[TYPE_MOBILE, subscriber_id, begin_time, end_time], |
| 2523 | [subscriber_id, begin_time, end_time]] |
| 2524 | for args in func_args: |
Betty Zhou | 9243770 | 2018-02-07 19:49:07 -0800 | [diff] [blame] | 2525 | try: |
Betty Zhou | a3f248f | 2018-03-05 17:33:49 -0800 | [diff] [blame] | 2526 | usage = func(*args) |
| 2527 | ad.log.info("%s mobile data usage is %s", apk |
| 2528 | if apk else "Subscriber", usage) |
| 2529 | return usage |
| 2530 | except Exception as e: |
| 2531 | ad.log.warning(e) |
| 2532 | return None |
Betty Zhou | f336601 | 2017-11-21 18:23:20 -0800 | [diff] [blame] | 2533 | |
| 2534 | |
| 2535 | def set_mobile_data_usage_limit(ad, limit, subscriber_id=None): |
| 2536 | if not subscriber_id: |
| 2537 | subscriber_id = ad.droid.telephonyGetSubscriberId() |
| 2538 | ad.log.info("Set subscriber mobile data usage limit to %s", limit) |
Betty Zhou | 10f887e | 2018-04-10 12:45:00 -0700 | [diff] [blame] | 2539 | ad.droid.logV("Setting subscriber mobile data usage limit to %s" % limit) |
Betty Zhou | f336601 | 2017-11-21 18:23:20 -0800 | [diff] [blame] | 2540 | ad.droid.connectivitySetDataUsageLimit(subscriber_id, str(limit)) |
| 2541 | |
| 2542 | |
| 2543 | def remove_mobile_data_usage_limit(ad, subscriber_id=None): |
| 2544 | if not subscriber_id: |
| 2545 | subscriber_id = ad.droid.telephonyGetSubscriberId() |
Betty Zhou | e57ab69 | 2018-03-09 18:39:30 -0800 | [diff] [blame] | 2546 | ad.log.debug("Remove subscriber mobile data usage limit") |
Betty Zhou | 10f887e | 2018-04-10 12:45:00 -0700 | [diff] [blame] | 2547 | ad.droid.logV( |
| 2548 | "Setting subscriber mobile data usage limit to -1, unlimited") |
Betty Zhou | f336601 | 2017-11-21 18:23:20 -0800 | [diff] [blame] | 2549 | ad.droid.connectivitySetDataUsageLimit(subscriber_id, "-1") |
| 2550 | |
| 2551 | |
Betty Zhou | ae9d6a8 | 2018-02-15 20:05:34 -0800 | [diff] [blame] | 2552 | def trigger_modem_crash(ad, timeout=120): |
Jaineel | 4088478 | 2017-06-08 15:53:39 -0700 | [diff] [blame] | 2553 | cmd = "echo restart > /sys/kernel/debug/msm_subsys/modem" |
Betty Zhou | ae9d6a8 | 2018-02-15 20:05:34 -0800 | [diff] [blame] | 2554 | ad.log.info("Triggering Modem Crash from kernel using adb command %s", cmd) |
| 2555 | ad.adb.shell(cmd) |
| 2556 | time.sleep(timeout) |
Jaineel | 4088478 | 2017-06-08 15:53:39 -0700 | [diff] [blame] | 2557 | return True |
Betty Zhou | ccd171d | 2017-02-13 15:11:33 -0800 | [diff] [blame] | 2558 | |
Betty Zhou | 2cf788e | 2017-06-27 17:25:53 -0700 | [diff] [blame] | 2559 | |
Betty Zhou | ae9d6a8 | 2018-02-15 20:05:34 -0800 | [diff] [blame] | 2560 | def trigger_modem_crash_by_modem(ad, timeout=120): |
| 2561 | begin_time = get_current_epoch_time() |
| 2562 | ad.adb.shell( |
| 2563 | "setprop persist.sys.modem.diag.mdlog false", ignore_status=True) |
| 2564 | stop_qxdm_logger(ad) |
Betty Zhou | 02571f5 | 2018-02-16 14:11:16 -0800 | [diff] [blame] | 2565 | cmd = ('am instrument -w -e request "4b 25 03 00" ' |
| 2566 | '"com.google.mdstest/com.google.mdstest.instrument.' |
| 2567 | 'ModemCommandInstrumentation"') |
| 2568 | ad.log.info("Crash modem by %s", cmd) |
| 2569 | ad.adb.shell(cmd, ignore_status=True) |
| 2570 | time.sleep(timeout) # sleep time for sl4a stability |
Betty Zhou | ae9d6a8 | 2018-02-15 20:05:34 -0800 | [diff] [blame] | 2571 | reasons = ad.search_logcat("modem subsystem failure reason", begin_time) |
| 2572 | if reasons: |
| 2573 | ad.log.info("Modem crash is triggered successfully") |
| 2574 | ad.log.info(reasons[-1]["log_message"]) |
| 2575 | return True |
| 2576 | else: |
Betty Zhou | fe726dc | 2018-04-25 19:31:33 -0700 | [diff] [blame] | 2577 | ad.log.warning("There is no modem subsystem failure reason logcat") |
Betty Zhou | ae9d6a8 | 2018-02-15 20:05:34 -0800 | [diff] [blame] | 2578 | return False |
| 2579 | |
| 2580 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2581 | def _connection_state_change(_event, target_state, connection_type): |
| 2582 | if connection_type: |
| 2583 | if 'TypeName' not in _event['data']: |
| 2584 | return False |
| 2585 | connection_type_string_in_event = _event['data']['TypeName'] |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2586 | cur_type = connection_type_from_type_string( |
| 2587 | connection_type_string_in_event) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2588 | if cur_type != connection_type: |
| 2589 | log.info( |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 2590 | "_connection_state_change expect: %s, received: %s <type %s>", |
| 2591 | connection_type, connection_type_string_in_event, cur_type) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2592 | return False |
| 2593 | |
Betty Zhou | ee31105 | 2017-12-19 13:09:56 -0800 | [diff] [blame] | 2594 | if 'isConnected' in _event['data'] and _event['data']['isConnected'] == target_state: |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2595 | return True |
| 2596 | return False |
| 2597 | |
| 2598 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2599 | def wait_for_cell_data_connection( |
Betty Zhou | 58ad52a | 2018-02-08 16:38:16 -0800 | [diff] [blame] | 2600 | log, ad, state, timeout_value=MAX_WAIT_TIME_CONNECTION_STATE_UPDATE): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2601 | """Wait for data connection status to be expected value for default |
| 2602 | data subscription. |
| 2603 | |
| 2604 | Wait for the data connection status to be DATA_STATE_CONNECTED |
| 2605 | or DATA_STATE_DISCONNECTED. |
| 2606 | |
| 2607 | Args: |
| 2608 | log: Log object. |
| 2609 | ad: Android Device Object. |
| 2610 | state: Expected status: True or False. |
| 2611 | If True, it will wait for status to be DATA_STATE_CONNECTED. |
| 2612 | If False, it will wait for status ti be DATA_STATE_DISCONNECTED. |
| 2613 | timeout_value: wait for cell data timeout value. |
Betty Zhou | 58ad52a | 2018-02-08 16:38:16 -0800 | [diff] [blame] | 2614 | This is optional, default value is MAX_WAIT_TIME_CONNECTION_STATE_UPDATE |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2615 | |
| 2616 | Returns: |
| 2617 | True if success. |
| 2618 | False if failed. |
| 2619 | """ |
Yang Liu | 963c93c | 2016-04-05 10:52:00 -0700 | [diff] [blame] | 2620 | sub_id = get_default_data_sub_id(ad) |
Betty Zhou | ee31105 | 2017-12-19 13:09:56 -0800 | [diff] [blame] | 2621 | return wait_for_cell_data_connection_for_subscription( |
| 2622 | log, ad, sub_id, state, timeout_value) |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2623 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2624 | |
| 2625 | def _is_data_connection_state_match(log, ad, expected_data_connection_state): |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2626 | return (expected_data_connection_state == |
| 2627 | ad.droid.telephonyGetDataConnectionState()) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2628 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2629 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2630 | def _is_network_connected_state_match(log, ad, |
| 2631 | expected_network_connected_state): |
| 2632 | return (expected_network_connected_state == |
| 2633 | ad.droid.connectivityNetworkIsConnected()) |
| 2634 | |
| 2635 | |
| 2636 | def wait_for_cell_data_connection_for_subscription( |
Betty Zhou | 02571f5 | 2018-02-16 14:11:16 -0800 | [diff] [blame] | 2637 | log, |
| 2638 | ad, |
| 2639 | sub_id, |
| 2640 | state, |
| 2641 | timeout_value=MAX_WAIT_TIME_CONNECTION_STATE_UPDATE): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2642 | """Wait for data connection status to be expected value for specified |
| 2643 | subscrption id. |
| 2644 | |
| 2645 | Wait for the data connection status to be DATA_STATE_CONNECTED |
| 2646 | or DATA_STATE_DISCONNECTED. |
| 2647 | |
| 2648 | Args: |
| 2649 | log: Log object. |
| 2650 | ad: Android Device Object. |
| 2651 | sub_id: subscription Id |
| 2652 | state: Expected status: True or False. |
| 2653 | If True, it will wait for status to be DATA_STATE_CONNECTED. |
| 2654 | If False, it will wait for status ti be DATA_STATE_DISCONNECTED. |
| 2655 | timeout_value: wait for cell data timeout value. |
Betty Zhou | 58ad52a | 2018-02-08 16:38:16 -0800 | [diff] [blame] | 2656 | This is optional, default value is MAX_WAIT_TIME_CONNECTION_STATE_UPDATE |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2657 | |
| 2658 | Returns: |
| 2659 | True if success. |
| 2660 | False if failed. |
| 2661 | """ |
Yang Liu | 8e6adff | 2016-02-05 10:24:04 -0800 | [diff] [blame] | 2662 | state_str = { |
| 2663 | True: DATA_STATE_CONNECTED, |
| 2664 | False: DATA_STATE_DISCONNECTED |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2665 | }[state] |
| 2666 | |
Betty Zhou | 68fc0d0 | 2017-04-26 13:42:54 -0700 | [diff] [blame] | 2667 | data_state = ad.droid.telephonyGetDataConnectionState() |
| 2668 | if not state and ad.droid.telephonyGetDataConnectionState() == state_str: |
| 2669 | return True |
Betty Zhou | c9f723b | 2018-04-10 18:08:21 -0700 | [diff] [blame] | 2670 | |
Betty Zhou | 287f9df | 2018-01-23 10:57:55 -0800 | [diff] [blame] | 2671 | ad.ed.clear_events(EventDataConnectionStateChanged) |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2672 | ad.droid.telephonyStartTrackingDataConnectionStateChangeForSubscription( |
| 2673 | sub_id) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2674 | ad.droid.connectivityStartTrackingConnectivityStateChange() |
| 2675 | try: |
Betty Zhou | c9f723b | 2018-04-10 18:08:21 -0700 | [diff] [blame] | 2676 | ad.log.info("User data enabled for sub_id %s: %s", sub_id, |
| 2677 | ad.droid.telephonyIsDataEnabledForSubscription(sub_id)) |
Yang Liu | aed3eef | 2015-12-15 18:40:25 -0800 | [diff] [blame] | 2678 | data_state = ad.droid.telephonyGetDataConnectionState() |
Betty Zhou | c9f723b | 2018-04-10 18:08:21 -0700 | [diff] [blame] | 2679 | ad.log.info("Data connection state is %s", data_state) |
| 2680 | ad.log.info("Network is connected: %s", |
| 2681 | ad.droid.connectivityNetworkIsConnected()) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2682 | if data_state == state_str: |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2683 | return _wait_for_nw_data_connection( |
| 2684 | log, ad, state, NETWORK_CONNECTION_TYPE_CELL, timeout_value) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2685 | |
| 2686 | try: |
Betty Zhou | 1c8c8d4 | 2018-03-14 12:43:50 -0700 | [diff] [blame] | 2687 | ad.ed.wait_for_event( |
Nathan Harold | 7642fc9 | 2016-05-02 18:29:11 -0700 | [diff] [blame] | 2688 | EventDataConnectionStateChanged, |
| 2689 | is_event_match, |
| 2690 | timeout=timeout_value, |
Yang Liu | 8e6adff | 2016-02-05 10:24:04 -0800 | [diff] [blame] | 2691 | field=DataConnectionStateContainer.DATA_CONNECTION_STATE, |
| 2692 | value=state_str) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2693 | except Empty: |
Betty Zhou | f25ce44 | 2017-03-03 14:28:36 -0800 | [diff] [blame] | 2694 | ad.log.info("No expected event EventDataConnectionStateChanged %s", |
| 2695 | state_str) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2696 | |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 2697 | # TODO: Wait for <MAX_WAIT_TIME_CONNECTION_STATE_UPDATE> seconds for |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2698 | # data connection state. |
| 2699 | # Otherwise, the network state will not be correct. |
| 2700 | # The bug is tracked here: b/20921915 |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2701 | |
Yang Liu | 7a2e7ee | 2015-12-28 15:32:44 -0800 | [diff] [blame] | 2702 | # Previously we use _is_data_connection_state_match, |
| 2703 | # but telephonyGetDataConnectionState sometimes return wrong value. |
| 2704 | # The bug is tracked here: b/22612607 |
| 2705 | # So we use _is_network_connected_state_match. |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2706 | |
Betty Zhou | 58ad52a | 2018-02-08 16:38:16 -0800 | [diff] [blame] | 2707 | if _wait_for_droid_in_state(log, ad, timeout_value, |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2708 | _is_network_connected_state_match, state): |
| 2709 | return _wait_for_nw_data_connection( |
| 2710 | log, ad, state, NETWORK_CONNECTION_TYPE_CELL, timeout_value) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2711 | else: |
| 2712 | return False |
| 2713 | |
| 2714 | finally: |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2715 | ad.droid.telephonyStopTrackingDataConnectionStateChangeForSubscription( |
| 2716 | sub_id) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2717 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2718 | |
| 2719 | def wait_for_wifi_data_connection( |
Betty Zhou | 58ad52a | 2018-02-08 16:38:16 -0800 | [diff] [blame] | 2720 | log, ad, state, timeout_value=MAX_WAIT_TIME_CONNECTION_STATE_UPDATE): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2721 | """Wait for data connection status to be expected value and connection is by WiFi. |
| 2722 | |
| 2723 | Args: |
| 2724 | log: Log object. |
| 2725 | ad: Android Device Object. |
| 2726 | state: Expected status: True or False. |
| 2727 | If True, it will wait for status to be DATA_STATE_CONNECTED. |
| 2728 | If False, it will wait for status ti be DATA_STATE_DISCONNECTED. |
| 2729 | timeout_value: wait for network data timeout value. |
Betty Zhou | 58ad52a | 2018-02-08 16:38:16 -0800 | [diff] [blame] | 2730 | This is optional, default value is MAX_WAIT_TIME_NW_SELECTION |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2731 | |
| 2732 | Returns: |
| 2733 | True if success. |
| 2734 | False if failed. |
| 2735 | """ |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 2736 | ad.log.info("wait_for_wifi_data_connection") |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2737 | return _wait_for_nw_data_connection( |
| 2738 | log, ad, state, NETWORK_CONNECTION_TYPE_WIFI, timeout_value) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2739 | |
| 2740 | |
Betty Zhou | 02571f5 | 2018-02-16 14:11:16 -0800 | [diff] [blame] | 2741 | def wait_for_data_connection( |
| 2742 | log, ad, state, timeout_value=MAX_WAIT_TIME_CONNECTION_STATE_UPDATE): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2743 | """Wait for data connection status to be expected value. |
| 2744 | |
| 2745 | Wait for the data connection status to be DATA_STATE_CONNECTED |
| 2746 | or DATA_STATE_DISCONNECTED. |
| 2747 | |
| 2748 | Args: |
| 2749 | log: Log object. |
| 2750 | ad: Android Device Object. |
| 2751 | state: Expected status: True or False. |
| 2752 | If True, it will wait for status to be DATA_STATE_CONNECTED. |
| 2753 | If False, it will wait for status ti be DATA_STATE_DISCONNECTED. |
| 2754 | timeout_value: wait for network data timeout value. |
Betty Zhou | 58ad52a | 2018-02-08 16:38:16 -0800 | [diff] [blame] | 2755 | This is optional, default value is MAX_WAIT_TIME_CONNECTION_STATE_UPDATE |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2756 | |
| 2757 | Returns: |
| 2758 | True if success. |
| 2759 | False if failed. |
| 2760 | """ |
| 2761 | return _wait_for_nw_data_connection(log, ad, state, None, timeout_value) |
| 2762 | |
| 2763 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2764 | def _wait_for_nw_data_connection( |
| 2765 | log, |
| 2766 | ad, |
| 2767 | is_connected, |
| 2768 | connection_type=None, |
Betty Zhou | 58ad52a | 2018-02-08 16:38:16 -0800 | [diff] [blame] | 2769 | timeout_value=MAX_WAIT_TIME_CONNECTION_STATE_UPDATE): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2770 | """Wait for data connection status to be expected value. |
| 2771 | |
| 2772 | Wait for the data connection status to be DATA_STATE_CONNECTED |
| 2773 | or DATA_STATE_DISCONNECTED. |
| 2774 | |
| 2775 | Args: |
| 2776 | log: Log object. |
| 2777 | ad: Android Device Object. |
| 2778 | is_connected: Expected connection status: True or False. |
| 2779 | If True, it will wait for status to be DATA_STATE_CONNECTED. |
| 2780 | If False, it will wait for status ti be DATA_STATE_DISCONNECTED. |
| 2781 | connection_type: expected connection type. |
| 2782 | This is optional, if it is None, then any connection type will return True. |
| 2783 | timeout_value: wait for network data timeout value. |
Betty Zhou | 58ad52a | 2018-02-08 16:38:16 -0800 | [diff] [blame] | 2784 | This is optional, default value is MAX_WAIT_TIME_CONNECTION_STATE_UPDATE |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2785 | |
| 2786 | Returns: |
| 2787 | True if success. |
| 2788 | False if failed. |
| 2789 | """ |
Betty Zhou | e34f9e2 | 2018-01-23 18:58:24 -0800 | [diff] [blame] | 2790 | ad.ed.clear_events(EventConnectivityChanged) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2791 | ad.droid.connectivityStartTrackingConnectivityStateChange() |
| 2792 | try: |
| 2793 | cur_data_connection_state = ad.droid.connectivityNetworkIsConnected() |
| 2794 | if is_connected == cur_data_connection_state: |
| 2795 | current_type = get_internet_connection_type(log, ad) |
Betty Zhou | e955be2 | 2017-04-12 17:28:05 -0700 | [diff] [blame] | 2796 | ad.log.info("current data connection type: %s", current_type) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2797 | if not connection_type: |
| 2798 | return True |
| 2799 | else: |
| 2800 | if not is_connected and current_type != connection_type: |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 2801 | ad.log.info("data connection not on %s!", connection_type) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2802 | return True |
| 2803 | elif is_connected and current_type == connection_type: |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 2804 | ad.log.info("data connection on %s as expected", |
| 2805 | connection_type) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2806 | return True |
| 2807 | else: |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 2808 | ad.log.info("current data connection state: %s target: %s", |
| 2809 | cur_data_connection_state, is_connected) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2810 | |
| 2811 | try: |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2812 | event = ad.ed.wait_for_event( |
| 2813 | EventConnectivityChanged, _connection_state_change, |
| 2814 | timeout_value, is_connected, connection_type) |
Betty Zhou | 5c7dbd1 | 2018-03-13 18:27:44 -0700 | [diff] [blame] | 2815 | ad.log.info("Got event: %s", event) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2816 | except Empty: |
| 2817 | pass |
| 2818 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2819 | log.info( |
| 2820 | "_wait_for_nw_data_connection: check connection after wait event.") |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 2821 | # TODO: Wait for <MAX_WAIT_TIME_CONNECTION_STATE_UPDATE> seconds for |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2822 | # data connection state. |
| 2823 | # Otherwise, the network state will not be correct. |
| 2824 | # The bug is tracked here: b/20921915 |
Betty Zhou | 02571f5 | 2018-02-16 14:11:16 -0800 | [diff] [blame] | 2825 | if _wait_for_droid_in_state(log, ad, timeout_value, |
| 2826 | _is_network_connected_state_match, |
| 2827 | is_connected): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2828 | current_type = get_internet_connection_type(log, ad) |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 2829 | ad.log.info("current data connection type: %s", current_type) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2830 | if not connection_type: |
| 2831 | return True |
| 2832 | else: |
| 2833 | if not is_connected and current_type != connection_type: |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 2834 | ad.log.info("data connection not on %s", connection_type) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2835 | return True |
| 2836 | elif is_connected and current_type == connection_type: |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 2837 | ad.log.info("after event wait, data connection on %s", |
| 2838 | connection_type) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2839 | return True |
| 2840 | else: |
| 2841 | return False |
| 2842 | else: |
| 2843 | return False |
| 2844 | except Exception as e: |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 2845 | ad.log.error("Exception error %s", str(e)) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2846 | return False |
| 2847 | finally: |
| 2848 | ad.droid.connectivityStopTrackingConnectivityStateChange() |
| 2849 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2850 | |
Betty Zhou | 2cf788e | 2017-06-27 17:25:53 -0700 | [diff] [blame] | 2851 | def get_cell_data_roaming_state_by_adb(ad): |
| 2852 | """Get Cell Data Roaming state. True for enabled, False for disabled""" |
| 2853 | adb_str = {"1": True, "0": False} |
| 2854 | out = ad.adb.shell("settings get global data_roaming") |
| 2855 | return adb_str[out] |
| 2856 | |
| 2857 | |
| 2858 | def get_cell_data_roaming_state_by_adb(ad): |
| 2859 | """Get Cell Data Roaming state. True for enabled, False for disabled""" |
| 2860 | state_mapping = {"1": True, "0": False} |
| 2861 | return state_mapping[ad.adb.shell("settings get global data_roaming")] |
| 2862 | |
| 2863 | |
| 2864 | def set_cell_data_roaming_state_by_adb(ad, state): |
| 2865 | """Set Cell Data Roaming state.""" |
| 2866 | state_mapping = {True: "1", False: "0"} |
| 2867 | ad.log.info("Set data roaming to %s", state) |
| 2868 | ad.adb.shell("settings put global data_roaming %s" % state_mapping[state]) |
| 2869 | |
| 2870 | |
Betty Zhou | 9e2bf40 | 2017-02-01 19:04:09 -0800 | [diff] [blame] | 2871 | def toggle_cell_data_roaming(ad, state): |
| 2872 | """Enable cell data roaming for default data subscription. |
| 2873 | |
| 2874 | Wait for the data roaming status to be DATA_STATE_CONNECTED |
| 2875 | or DATA_STATE_DISCONNECTED. |
| 2876 | |
| 2877 | Args: |
| 2878 | log: Log object. |
| 2879 | ad: Android Device Object. |
| 2880 | state: True or False for enable or disable cell data roaming. |
| 2881 | |
| 2882 | Returns: |
| 2883 | True if success. |
| 2884 | False if failed. |
| 2885 | """ |
| 2886 | state_int = {True: DATA_ROAMING_ENABLE, False: DATA_ROAMING_DISABLE}[state] |
| 2887 | action_str = {True: "Enable", False: "Disable"}[state] |
| 2888 | if ad.droid.connectivityCheckDataRoamingMode() == state: |
| 2889 | ad.log.info("Data roaming is already in state %s", state) |
| 2890 | return True |
| 2891 | if not ad.droid.connectivitySetDataRoaming(state_int): |
| 2892 | ad.error.info("Fail to config data roaming into state %s", state) |
| 2893 | return False |
| 2894 | if ad.droid.connectivityCheckDataRoamingMode() == state: |
| 2895 | ad.log.info("Data roaming is configured into state %s", state) |
| 2896 | return True |
| 2897 | else: |
| 2898 | ad.log.error("Data roaming is not configured into state %s", state) |
| 2899 | return False |
| 2900 | |
| 2901 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2902 | def verify_incall_state(log, ads, expected_status): |
| 2903 | """Verify phones in incall state or not. |
| 2904 | |
| 2905 | Verify if all phones in the array <ads> are in <expected_status>. |
| 2906 | |
| 2907 | Args: |
| 2908 | log: Log object. |
| 2909 | ads: Array of Android Device Object. All droid in this array will be tested. |
| 2910 | expected_status: If True, verify all Phones in incall state. |
| 2911 | If False, verify all Phones not in incall state. |
| 2912 | |
| 2913 | """ |
| 2914 | result = True |
| 2915 | for ad in ads: |
| 2916 | if ad.droid.telecomIsInCall() is not expected_status: |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 2917 | ad.log.error("InCall status:%s, expected:%s", |
| 2918 | ad.droid.telecomIsInCall(), expected_status) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2919 | result = False |
| 2920 | return result |
| 2921 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2922 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2923 | def verify_active_call_number(log, ad, expected_number): |
| 2924 | """Verify the number of current active call. |
| 2925 | |
| 2926 | Verify if the number of current active call in <ad> is |
| 2927 | equal to <expected_number>. |
| 2928 | |
| 2929 | Args: |
| 2930 | ad: Android Device Object. |
| 2931 | expected_number: Expected active call number. |
| 2932 | """ |
| 2933 | calls = ad.droid.telecomCallGetCallIds() |
| 2934 | if calls is None: |
| 2935 | actual_number = 0 |
| 2936 | else: |
| 2937 | actual_number = len(calls) |
| 2938 | if actual_number != expected_number: |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 2939 | ad.log.error("Active Call number is %s, expecting", actual_number, |
| 2940 | expected_number) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2941 | return False |
| 2942 | return True |
| 2943 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2944 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2945 | def num_active_calls(log, ad): |
| 2946 | """Get the count of current active calls. |
| 2947 | |
| 2948 | Args: |
| 2949 | log: Log object. |
| 2950 | ad: Android Device Object. |
| 2951 | |
| 2952 | Returns: |
| 2953 | Count of current active calls. |
| 2954 | """ |
| 2955 | calls = ad.droid.telecomCallGetCallIds() |
| 2956 | return len(calls) if calls else 0 |
| 2957 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2958 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2959 | def toggle_volte(log, ad, new_state=None): |
| 2960 | """Toggle enable/disable VoLTE for default voice subscription. |
| 2961 | |
| 2962 | Args: |
| 2963 | ad: Android device object. |
| 2964 | new_state: VoLTE mode state to set to. |
| 2965 | True for enable, False for disable. |
| 2966 | If None, opposite of the current state. |
| 2967 | |
| 2968 | Raises: |
| 2969 | TelTestUtilsError if platform does not support VoLTE. |
| 2970 | """ |
Betty Zhou | ee31105 | 2017-12-19 13:09:56 -0800 | [diff] [blame] | 2971 | return toggle_volte_for_subscription( |
| 2972 | log, ad, get_outgoing_voice_sub_id(ad), new_state) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2973 | |
| 2974 | |
| 2975 | def toggle_volte_for_subscription(log, ad, sub_id, new_state=None): |
| 2976 | """Toggle enable/disable VoLTE for specified voice subscription. |
| 2977 | |
| 2978 | Args: |
| 2979 | ad: Android device object. |
| 2980 | sub_id: subscription ID |
| 2981 | new_state: VoLTE mode state to set to. |
| 2982 | True for enable, False for disable. |
| 2983 | If None, opposite of the current state. |
| 2984 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2985 | """ |
Yang Liu | 7a2e7ee | 2015-12-28 15:32:44 -0800 | [diff] [blame] | 2986 | # TODO: b/26293960 No framework API available to set IMS by SubId. |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2987 | if not ad.droid.imsIsEnhanced4gLteModeSettingEnabledByPlatform(): |
Betty Zhou | b14c101 | 2018-04-20 11:27:00 -0700 | [diff] [blame] | 2988 | ad.log.info("Enhanced 4G Lte Mode Setting is not enabled by platform.") |
| 2989 | return False |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2990 | current_state = ad.droid.imsIsEnhanced4gLteModeSettingEnabledByUser() |
| 2991 | if new_state is None: |
| 2992 | new_state = not current_state |
| 2993 | if new_state != current_state: |
Betty Zhou | 5dd53c0 | 2018-03-22 20:08:33 -0700 | [diff] [blame] | 2994 | ad.log.info("Toggle Enhanced 4G LTE Mode") |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 2995 | ad.droid.imsSetEnhanced4gMode(new_state) |
| 2996 | return True |
| 2997 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 2998 | |
Betty Zhou | b14c101 | 2018-04-20 11:27:00 -0700 | [diff] [blame] | 2999 | def wait_for_enhanced_4g_lte_setting(log, |
| 3000 | ad, |
| 3001 | max_time=MAX_WAIT_TIME_FOR_STATE_CHANGE): |
| 3002 | """Wait for android device to enable enhance 4G LTE setting. |
| 3003 | |
| 3004 | Args: |
| 3005 | log: log object. |
| 3006 | ad: android device. |
| 3007 | max_time: maximal wait time. |
| 3008 | |
| 3009 | Returns: |
| 3010 | Return True if device report VoLTE enabled bit true within max_time. |
| 3011 | Return False if timeout. |
| 3012 | """ |
| 3013 | return wait_for_state( |
| 3014 | ad.droid.imsIsEnhanced4gLteModeSettingEnabledByPlatform, |
| 3015 | True, |
| 3016 | max_wait_time=max_time) |
| 3017 | |
| 3018 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3019 | def set_wfc_mode(log, ad, wfc_mode): |
| 3020 | """Set WFC enable/disable and mode. |
| 3021 | |
| 3022 | Args: |
| 3023 | log: Log object |
| 3024 | ad: Android device object. |
| 3025 | wfc_mode: WFC mode to set to. |
| 3026 | Valid mode includes: WFC_MODE_WIFI_ONLY, WFC_MODE_CELLULAR_PREFERRED, |
| 3027 | WFC_MODE_WIFI_PREFERRED, WFC_MODE_DISABLED. |
| 3028 | |
| 3029 | Returns: |
| 3030 | True if success. False if ad does not support WFC or error happened. |
| 3031 | """ |
| 3032 | try: |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 3033 | ad.log.info("Set wfc mode to %s", wfc_mode) |
Betty Zhou | 96b5bf4 | 2018-03-23 19:18:41 -0700 | [diff] [blame] | 3034 | if wfc_mode != WFC_MODE_DISABLED: |
Betty Zhou | 2bb1168 | 2018-04-19 17:05:30 -0700 | [diff] [blame] | 3035 | start_adb_tcpdump(ad, interface="wlan0", mask="all") |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3036 | if not ad.droid.imsIsWfcEnabledByPlatform(): |
| 3037 | if wfc_mode == WFC_MODE_DISABLED: |
| 3038 | return True |
| 3039 | else: |
Betty Zhou | 9402318 | 2017-06-07 18:02:14 -0700 | [diff] [blame] | 3040 | ad.log.error("WFC not supported by platform.") |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3041 | return False |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3042 | ad.droid.imsSetWfcMode(wfc_mode) |
Betty Zhou | 9402318 | 2017-06-07 18:02:14 -0700 | [diff] [blame] | 3043 | mode = ad.droid.imsGetWfcMode() |
| 3044 | if mode != wfc_mode: |
| 3045 | ad.log.error("WFC mode is %s, not in %s", mode, wfc_mode) |
| 3046 | return False |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3047 | except Exception as e: |
| 3048 | log.error(e) |
| 3049 | return False |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3050 | return True |
| 3051 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 3052 | |
Nathan Harold | 0a01153 | 2016-08-24 19:17:44 -0700 | [diff] [blame] | 3053 | def toggle_video_calling(log, ad, new_state=None): |
| 3054 | """Toggle enable/disable Video calling for default voice subscription. |
| 3055 | |
| 3056 | Args: |
| 3057 | ad: Android device object. |
| 3058 | new_state: Video mode state to set to. |
| 3059 | True for enable, False for disable. |
| 3060 | If None, opposite of the current state. |
| 3061 | |
| 3062 | Raises: |
| 3063 | TelTestUtilsError if platform does not support Video calling. |
| 3064 | """ |
| 3065 | if not ad.droid.imsIsVtEnabledByPlatform(): |
| 3066 | if new_state is not False: |
| 3067 | raise TelTestUtilsError("VT not supported by platform.") |
| 3068 | # if the user sets VT false and it's unavailable we just let it go |
| 3069 | return False |
| 3070 | |
| 3071 | current_state = ad.droid.imsIsVtEnabledByUser() |
| 3072 | if new_state is None: |
| 3073 | new_state = not current_state |
| 3074 | if new_state != current_state: |
| 3075 | ad.droid.imsSetVtSetting(new_state) |
| 3076 | return True |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 3077 | |
Nathan Harold | 72f9bff | 2016-09-19 14:16:24 -0700 | [diff] [blame] | 3078 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 3079 | def _wait_for_droid_in_state(log, ad, max_time, state_check_func, *args, |
| 3080 | **kwargs): |
Nathan Harold | 97cd3f8 | 2016-09-29 10:58:29 -0700 | [diff] [blame] | 3081 | while max_time >= 0: |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3082 | if state_check_func(log, ad, *args, **kwargs): |
| 3083 | return True |
| 3084 | |
Betty Zhou | f25ce44 | 2017-03-03 14:28:36 -0800 | [diff] [blame] | 3085 | time.sleep(WAIT_TIME_BETWEEN_STATE_CHECK) |
| 3086 | max_time -= WAIT_TIME_BETWEEN_STATE_CHECK |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3087 | |
| 3088 | return False |
| 3089 | |
| 3090 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 3091 | def _wait_for_droid_in_state_for_subscription( |
| 3092 | log, ad, sub_id, max_time, state_check_func, *args, **kwargs): |
Nathan Harold | 97cd3f8 | 2016-09-29 10:58:29 -0700 | [diff] [blame] | 3093 | while max_time >= 0: |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3094 | if state_check_func(log, ad, sub_id, *args, **kwargs): |
| 3095 | return True |
| 3096 | |
Betty Zhou | f25ce44 | 2017-03-03 14:28:36 -0800 | [diff] [blame] | 3097 | time.sleep(WAIT_TIME_BETWEEN_STATE_CHECK) |
| 3098 | max_time -= WAIT_TIME_BETWEEN_STATE_CHECK |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3099 | |
| 3100 | return False |
| 3101 | |
| 3102 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 3103 | def _wait_for_droids_in_state(log, ads, max_time, state_check_func, *args, |
| 3104 | **kwargs): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3105 | while max_time > 0: |
| 3106 | success = True |
| 3107 | for ad in ads: |
| 3108 | if not state_check_func(log, ad, *args, **kwargs): |
| 3109 | success = False |
| 3110 | break |
| 3111 | if success: |
| 3112 | return True |
| 3113 | |
Betty Zhou | f25ce44 | 2017-03-03 14:28:36 -0800 | [diff] [blame] | 3114 | time.sleep(WAIT_TIME_BETWEEN_STATE_CHECK) |
| 3115 | max_time -= WAIT_TIME_BETWEEN_STATE_CHECK |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3116 | |
| 3117 | return False |
| 3118 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 3119 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3120 | def is_phone_in_call(log, ad): |
| 3121 | """Return True if phone in call. |
| 3122 | |
| 3123 | Args: |
| 3124 | log: log object. |
| 3125 | ad: android device. |
| 3126 | """ |
Betty Zhou | 377f72b | 2018-01-12 19:43:26 -0800 | [diff] [blame] | 3127 | try: |
| 3128 | return ad.droid.telecomIsInCall() |
| 3129 | except: |
| 3130 | return "mCallState=2" in ad.adb.shell( |
| 3131 | "dumpsys telephony.registry | grep mCallState") |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3132 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 3133 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3134 | def is_phone_not_in_call(log, ad): |
| 3135 | """Return True if phone not in call. |
| 3136 | |
| 3137 | Args: |
| 3138 | log: log object. |
| 3139 | ad: android device. |
| 3140 | """ |
Betty Zhou | 9402318 | 2017-06-07 18:02:14 -0700 | [diff] [blame] | 3141 | in_call = ad.droid.telecomIsInCall() |
| 3142 | call_state = ad.droid.telephonyGetCallState() |
| 3143 | if in_call: |
| 3144 | ad.log.info("Device is In Call") |
| 3145 | if call_state != TELEPHONY_STATE_IDLE: |
| 3146 | ad.log.info("Call_state is %s, not %s", call_state, |
| 3147 | TELEPHONY_STATE_IDLE) |
| 3148 | return ((not in_call) and (call_state == TELEPHONY_STATE_IDLE)) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3149 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 3150 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3151 | def wait_for_droid_in_call(log, ad, max_time): |
| 3152 | """Wait for android to be in call state. |
| 3153 | |
| 3154 | Args: |
| 3155 | log: log object. |
| 3156 | ad: android device. |
| 3157 | max_time: maximal wait time. |
| 3158 | |
| 3159 | Returns: |
| 3160 | If phone become in call state within max_time, return True. |
| 3161 | Return False if timeout. |
| 3162 | """ |
| 3163 | return _wait_for_droid_in_state(log, ad, max_time, is_phone_in_call) |
| 3164 | |
Nathan Harold | 7642fc9 | 2016-05-02 18:29:11 -0700 | [diff] [blame] | 3165 | |
Betty Zhou | cf04854 | 2018-02-22 17:14:08 -0800 | [diff] [blame] | 3166 | def is_phone_in_call_active(ad, call_id=None): |
| 3167 | """Return True if phone in active call. |
| 3168 | |
| 3169 | Args: |
| 3170 | log: log object. |
| 3171 | ad: android device. |
| 3172 | call_id: the call id |
| 3173 | """ |
Jaineel | 78b05dd | 2018-03-27 13:54:17 -0700 | [diff] [blame] | 3174 | if ad.droid.telecomIsInCall(): |
| 3175 | if not call_id: |
| 3176 | call_id = ad.droid.telecomCallGetCallIds()[0] |
| 3177 | call_state = ad.droid.telecomCallGetCallState(call_id) |
| 3178 | ad.log.info("%s state is %s", call_id, call_state) |
| 3179 | return call_state == "ACTIVE" |
| 3180 | else: |
| 3181 | ad.log.error("No calls are found on this device to check state") |
| 3182 | return False |
Betty Zhou | cf04854 | 2018-02-22 17:14:08 -0800 | [diff] [blame] | 3183 | |
Betty Zhou | 4411c20 | 2018-03-29 18:27:25 -0700 | [diff] [blame] | 3184 | |
Betty Zhou | cf04854 | 2018-02-22 17:14:08 -0800 | [diff] [blame] | 3185 | def wait_for_in_call_active(ad, timeout=5, interval=1, call_id=None): |
| 3186 | """Wait for call reach active state. |
| 3187 | |
| 3188 | Args: |
| 3189 | log: log object. |
| 3190 | ad: android device. |
| 3191 | call_id: the call id |
| 3192 | """ |
| 3193 | if not call_id: |
| 3194 | call_id = ad.droid.telecomCallGetCallIds()[0] |
| 3195 | args = [ad, call_id] |
| 3196 | if not wait_for_state(is_phone_in_call_active, True, timeout, interval, |
| 3197 | *args): |
| 3198 | ad.log.error("Call did not reach ACTIVE state") |
| 3199 | return False |
| 3200 | else: |
| 3201 | return True |
| 3202 | |
| 3203 | |
Yang Liu | 855d5f8 | 2016-01-27 15:35:48 -0800 | [diff] [blame] | 3204 | def wait_for_telecom_ringing(log, ad, max_time=MAX_WAIT_TIME_TELECOM_RINGING): |
| 3205 | """Wait for android to be in telecom ringing state. |
| 3206 | |
| 3207 | Args: |
| 3208 | log: log object. |
| 3209 | ad: android device. |
| 3210 | max_time: maximal wait time. This is optional. |
| 3211 | Default Value is MAX_WAIT_TIME_TELECOM_RINGING. |
| 3212 | |
| 3213 | Returns: |
| 3214 | If phone become in telecom ringing state within max_time, return True. |
| 3215 | Return False if timeout. |
| 3216 | """ |
Nathan Harold | 7642fc9 | 2016-05-02 18:29:11 -0700 | [diff] [blame] | 3217 | return _wait_for_droid_in_state( |
| 3218 | log, ad, max_time, lambda log, ad: ad.droid.telecomIsRinging()) |
Yang Liu | 855d5f8 | 2016-01-27 15:35:48 -0800 | [diff] [blame] | 3219 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 3220 | |
Nathan Harold | eb60b19 | 2016-08-24 14:41:55 -0700 | [diff] [blame] | 3221 | def wait_for_droid_not_in_call(log, ad, max_time=MAX_WAIT_TIME_CALL_DROP): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3222 | """Wait for android to be not in call state. |
| 3223 | |
| 3224 | Args: |
| 3225 | log: log object. |
| 3226 | ad: android device. |
| 3227 | max_time: maximal wait time. |
| 3228 | |
| 3229 | Returns: |
| 3230 | If phone become not in call state within max_time, return True. |
| 3231 | Return False if timeout. |
| 3232 | """ |
| 3233 | return _wait_for_droid_in_state(log, ad, max_time, is_phone_not_in_call) |
| 3234 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 3235 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3236 | def _is_attached(log, ad, voice_or_data): |
| 3237 | return _is_attached_for_subscription( |
| 3238 | log, ad, ad.droid.subscriptionGetDefaultSubId(), voice_or_data) |
| 3239 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 3240 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3241 | def _is_attached_for_subscription(log, ad, sub_id, voice_or_data): |
Betty Zhou | f25ce44 | 2017-03-03 14:28:36 -0800 | [diff] [blame] | 3242 | rat = get_network_rat_for_subscription(log, ad, sub_id, voice_or_data) |
Betty Zhou | cb2cbc4 | 2017-06-29 14:39:34 -0700 | [diff] [blame] | 3243 | ad.log.info("Sub_id %s network RAT is %s for %s", sub_id, rat, |
Betty Zhou | f25ce44 | 2017-03-03 14:28:36 -0800 | [diff] [blame] | 3244 | voice_or_data) |
| 3245 | return rat != RAT_UNKNOWN |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3246 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 3247 | |
Betty Zhou | 33c0529 | 2017-05-24 15:12:43 -0700 | [diff] [blame] | 3248 | def is_voice_attached(log, ad): |
| 3249 | return _is_attached_for_subscription( |
| 3250 | log, ad, ad.droid.subscriptionGetDefaultSubId(), NETWORK_SERVICE_VOICE) |
| 3251 | |
| 3252 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3253 | def wait_for_voice_attach(log, ad, max_time): |
| 3254 | """Wait for android device to attach on voice. |
| 3255 | |
| 3256 | Args: |
| 3257 | log: log object. |
| 3258 | ad: android device. |
| 3259 | max_time: maximal wait time. |
| 3260 | |
| 3261 | Returns: |
| 3262 | Return True if device attach voice within max_time. |
| 3263 | Return False if timeout. |
| 3264 | """ |
| 3265 | return _wait_for_droid_in_state(log, ad, max_time, _is_attached, |
| 3266 | NETWORK_SERVICE_VOICE) |
| 3267 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 3268 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3269 | def wait_for_voice_attach_for_subscription(log, ad, sub_id, max_time): |
| 3270 | """Wait for android device to attach on voice in subscription id. |
| 3271 | |
| 3272 | Args: |
| 3273 | log: log object. |
| 3274 | ad: android device. |
| 3275 | sub_id: subscription id. |
| 3276 | max_time: maximal wait time. |
| 3277 | |
| 3278 | Returns: |
| 3279 | Return True if device attach voice within max_time. |
| 3280 | Return False if timeout. |
| 3281 | """ |
Yang Liu | 7a2e7ee | 2015-12-28 15:32:44 -0800 | [diff] [blame] | 3282 | if not _wait_for_droid_in_state_for_subscription( |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 3283 | log, ad, sub_id, max_time, _is_attached_for_subscription, |
| 3284 | NETWORK_SERVICE_VOICE): |
Yang Liu | 7a2e7ee | 2015-12-28 15:32:44 -0800 | [diff] [blame] | 3285 | return False |
| 3286 | |
| 3287 | # TODO: b/26295983 if pone attach to 1xrtt from unknown, phone may not |
| 3288 | # receive incoming call immediately. |
| 3289 | if ad.droid.telephonyGetCurrentVoiceNetworkType() == RAT_1XRTT: |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 3290 | time.sleep(WAIT_TIME_1XRTT_VOICE_ATTACH) |
Yang Liu | 7a2e7ee | 2015-12-28 15:32:44 -0800 | [diff] [blame] | 3291 | return True |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3292 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 3293 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3294 | def wait_for_data_attach(log, ad, max_time): |
| 3295 | """Wait for android device to attach on data. |
| 3296 | |
| 3297 | Args: |
| 3298 | log: log object. |
| 3299 | ad: android device. |
| 3300 | max_time: maximal wait time. |
| 3301 | |
| 3302 | Returns: |
| 3303 | Return True if device attach data within max_time. |
| 3304 | Return False if timeout. |
| 3305 | """ |
| 3306 | return _wait_for_droid_in_state(log, ad, max_time, _is_attached, |
| 3307 | NETWORK_SERVICE_DATA) |
| 3308 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 3309 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3310 | def wait_for_data_attach_for_subscription(log, ad, sub_id, max_time): |
| 3311 | """Wait for android device to attach on data in subscription id. |
| 3312 | |
| 3313 | Args: |
| 3314 | log: log object. |
| 3315 | ad: android device. |
| 3316 | sub_id: subscription id. |
| 3317 | max_time: maximal wait time. |
| 3318 | |
| 3319 | Returns: |
| 3320 | Return True if device attach data within max_time. |
| 3321 | Return False if timeout. |
| 3322 | """ |
| 3323 | return _wait_for_droid_in_state_for_subscription( |
| 3324 | log, ad, sub_id, max_time, _is_attached_for_subscription, |
| 3325 | NETWORK_SERVICE_DATA) |
| 3326 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 3327 | |
Yang Liu | 9b85ce5 | 2016-02-16 12:25:11 -0800 | [diff] [blame] | 3328 | def is_ims_registered(log, ad): |
| 3329 | """Return True if IMS registered. |
| 3330 | |
| 3331 | Args: |
| 3332 | log: log object. |
| 3333 | ad: android device. |
| 3334 | |
| 3335 | Returns: |
| 3336 | Return True if IMS registered. |
| 3337 | Return False if IMS not registered. |
| 3338 | """ |
Yang Liu | aed3eef | 2015-12-15 18:40:25 -0800 | [diff] [blame] | 3339 | return ad.droid.telephonyIsImsRegistered() |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3340 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 3341 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3342 | def wait_for_ims_registered(log, ad, max_time): |
| 3343 | """Wait for android device to register on ims. |
| 3344 | |
| 3345 | Args: |
| 3346 | log: log object. |
| 3347 | ad: android device. |
| 3348 | max_time: maximal wait time. |
| 3349 | |
| 3350 | Returns: |
| 3351 | Return True if device register ims successfully within max_time. |
| 3352 | Return False if timeout. |
| 3353 | """ |
Yang Liu | 9b85ce5 | 2016-02-16 12:25:11 -0800 | [diff] [blame] | 3354 | 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] | 3355 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 3356 | |
Yang Liu | 9b85ce5 | 2016-02-16 12:25:11 -0800 | [diff] [blame] | 3357 | def is_volte_enabled(log, ad): |
| 3358 | """Return True if VoLTE feature bit is True. |
| 3359 | |
| 3360 | Args: |
| 3361 | log: log object. |
| 3362 | ad: android device. |
| 3363 | |
| 3364 | Returns: |
| 3365 | Return True if VoLTE feature bit is True and IMS registered. |
| 3366 | Return False if VoLTE feature bit is False or IMS not registered. |
| 3367 | """ |
Betty Zhou | e32dd3b | 2017-11-28 19:05:55 -0800 | [diff] [blame] | 3368 | if not is_ims_registered(log, ad): |
| 3369 | ad.log.info("IMS is not registered.") |
Betty Zhou | b66d48f | 2018-04-09 12:00:53 -0700 | [diff] [blame] | 3370 | return False |
| 3371 | if not ad.droid.telephonyIsVolteAvailable(): |
| 3372 | ad.log.info("IsVolteCallingAvailble is False") |
| 3373 | return False |
Betty Zhou | e32dd3b | 2017-11-28 19:05:55 -0800 | [diff] [blame] | 3374 | else: |
Betty Zhou | b66d48f | 2018-04-09 12:00:53 -0700 | [diff] [blame] | 3375 | ad.log.info("IsVolteCallingAvailble is True") |
| 3376 | return True |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3377 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 3378 | |
Yang Liu | 9b85ce5 | 2016-02-16 12:25:11 -0800 | [diff] [blame] | 3379 | def is_video_enabled(log, ad): |
| 3380 | """Return True if Video Calling feature bit is True. |
| 3381 | |
| 3382 | Args: |
| 3383 | log: log object. |
| 3384 | ad: android device. |
| 3385 | |
| 3386 | Returns: |
| 3387 | Return True if Video Calling feature bit is True and IMS registered. |
| 3388 | Return False if Video Calling feature bit is False or IMS not registered. |
| 3389 | """ |
Yang Liu | b93d729 | 2016-02-19 10:41:40 -0800 | [diff] [blame] | 3390 | video_status = ad.droid.telephonyIsVideoCallingAvailable() |
| 3391 | 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] | 3392 | log.error("Error! Video Call is Available, but IMS is not registered.") |
| 3393 | return False |
Yang Liu | b93d729 | 2016-02-19 10:41:40 -0800 | [diff] [blame] | 3394 | return video_status |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3395 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 3396 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3397 | def wait_for_volte_enabled(log, ad, max_time): |
| 3398 | """Wait for android device to report VoLTE enabled bit true. |
| 3399 | |
| 3400 | Args: |
| 3401 | log: log object. |
| 3402 | ad: android device. |
| 3403 | max_time: maximal wait time. |
| 3404 | |
| 3405 | Returns: |
| 3406 | Return True if device report VoLTE enabled bit true within max_time. |
| 3407 | Return False if timeout. |
| 3408 | """ |
Yang Liu | 9b85ce5 | 2016-02-16 12:25:11 -0800 | [diff] [blame] | 3409 | 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] | 3410 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 3411 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3412 | def wait_for_video_enabled(log, ad, max_time): |
| 3413 | """Wait for android device to report Video Telephony enabled bit true. |
| 3414 | |
| 3415 | Args: |
| 3416 | log: log object. |
| 3417 | ad: android device. |
| 3418 | max_time: maximal wait time. |
| 3419 | |
| 3420 | Returns: |
| 3421 | Return True if device report Video Telephony enabled bit true within max_time. |
| 3422 | Return False if timeout. |
| 3423 | """ |
Yang Liu | 9b85ce5 | 2016-02-16 12:25:11 -0800 | [diff] [blame] | 3424 | 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] | 3425 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 3426 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3427 | def is_wfc_enabled(log, ad): |
| 3428 | """Return True if WiFi Calling feature bit is True. |
| 3429 | |
| 3430 | Args: |
| 3431 | log: log object. |
| 3432 | ad: android device. |
| 3433 | |
| 3434 | Returns: |
Yang Liu | 9b85ce5 | 2016-02-16 12:25:11 -0800 | [diff] [blame] | 3435 | Return True if WiFi Calling feature bit is True and IMS registered. |
| 3436 | 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] | 3437 | """ |
Betty Zhou | b66d48f | 2018-04-09 12:00:53 -0700 | [diff] [blame] | 3438 | if not is_ims_registered(log, ad): |
| 3439 | ad.log.info("IMS is not registered.") |
| 3440 | return False |
Betty Zhou | 9402318 | 2017-06-07 18:02:14 -0700 | [diff] [blame] | 3441 | if not ad.droid.telephonyIsWifiCallingAvailable(): |
| 3442 | ad.log.info("IsWifiCallingAvailble is False") |
Yang Liu | 9b85ce5 | 2016-02-16 12:25:11 -0800 | [diff] [blame] | 3443 | return False |
Betty Zhou | 9402318 | 2017-06-07 18:02:14 -0700 | [diff] [blame] | 3444 | else: |
| 3445 | ad.log.info("IsWifiCallingAvailble is True") |
Betty Zhou | 9402318 | 2017-06-07 18:02:14 -0700 | [diff] [blame] | 3446 | return True |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3447 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 3448 | |
Yang Liu | dfc37b6 | 2016-01-20 18:08:47 -0800 | [diff] [blame] | 3449 | 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] | 3450 | """Wait for android device to report WiFi Calling enabled bit true. |
| 3451 | |
| 3452 | Args: |
| 3453 | log: log object. |
| 3454 | ad: android device. |
| 3455 | max_time: maximal wait time. |
Yang Liu | dfc37b6 | 2016-01-20 18:08:47 -0800 | [diff] [blame] | 3456 | Default value is MAX_WAIT_TIME_WFC_ENABLED. |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3457 | |
| 3458 | Returns: |
| 3459 | Return True if device report WiFi Calling enabled bit true within max_time. |
| 3460 | Return False if timeout. |
| 3461 | """ |
| 3462 | return _wait_for_droid_in_state(log, ad, max_time, is_wfc_enabled) |
| 3463 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 3464 | |
Yang Liu | dfc37b6 | 2016-01-20 18:08:47 -0800 | [diff] [blame] | 3465 | 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] | 3466 | """Wait for android device to report WiFi Calling enabled bit false. |
| 3467 | |
| 3468 | Args: |
| 3469 | log: log object. |
| 3470 | ad: android device. |
| 3471 | max_time: maximal wait time. |
Yang Liu | dfc37b6 | 2016-01-20 18:08:47 -0800 | [diff] [blame] | 3472 | Default value is MAX_WAIT_TIME_WFC_DISABLED. |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3473 | |
| 3474 | Returns: |
| 3475 | Return True if device report WiFi Calling enabled bit false within max_time. |
| 3476 | Return False if timeout. |
| 3477 | """ |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 3478 | return _wait_for_droid_in_state( |
| 3479 | log, ad, max_time, lambda log, ad: not is_wfc_enabled(log, ad)) |
| 3480 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3481 | |
| 3482 | def get_phone_number(log, ad): |
| 3483 | """Get phone number for default subscription |
| 3484 | |
| 3485 | Args: |
| 3486 | log: log object. |
| 3487 | ad: Android device object. |
| 3488 | |
| 3489 | Returns: |
| 3490 | Phone number. |
| 3491 | """ |
Nathan Harold | 7642fc9 | 2016-05-02 18:29:11 -0700 | [diff] [blame] | 3492 | return get_phone_number_for_subscription(log, ad, |
| 3493 | get_outgoing_voice_sub_id(ad)) |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 3494 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3495 | |
| 3496 | def get_phone_number_for_subscription(log, ad, subid): |
| 3497 | """Get phone number for subscription |
| 3498 | |
| 3499 | Args: |
| 3500 | log: log object. |
| 3501 | ad: Android device object. |
| 3502 | subid: subscription id. |
| 3503 | |
| 3504 | Returns: |
| 3505 | Phone number. |
| 3506 | """ |
| 3507 | number = None |
| 3508 | try: |
Betty Zhou | 3b2de07 | 2018-03-15 16:46:26 -0700 | [diff] [blame] | 3509 | number = ad.telephony['subscription'][subid]['phone_num'] |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3510 | except KeyError: |
Yang Liu | aed3eef | 2015-12-15 18:40:25 -0800 | [diff] [blame] | 3511 | number = ad.droid.telephonyGetLine1NumberForSubscription(subid) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3512 | return number |
| 3513 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 3514 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3515 | def set_phone_number(log, ad, phone_num): |
| 3516 | """Set phone number for default subscription |
| 3517 | |
| 3518 | Args: |
| 3519 | log: log object. |
| 3520 | ad: Android device object. |
| 3521 | phone_num: phone number string. |
| 3522 | |
| 3523 | Returns: |
| 3524 | True if success. |
| 3525 | """ |
Betty Zhou | f809c5c | 2017-03-21 14:55:59 -0700 | [diff] [blame] | 3526 | return set_phone_number_for_subscription(log, ad, |
| 3527 | get_outgoing_voice_sub_id(ad), |
| 3528 | phone_num) |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 3529 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3530 | |
| 3531 | def set_phone_number_for_subscription(log, ad, subid, phone_num): |
| 3532 | """Set phone number for subscription |
| 3533 | |
| 3534 | Args: |
| 3535 | log: log object. |
| 3536 | ad: Android device object. |
| 3537 | subid: subscription id. |
| 3538 | phone_num: phone number string. |
| 3539 | |
| 3540 | Returns: |
| 3541 | True if success. |
| 3542 | """ |
| 3543 | try: |
Betty Zhou | 3b2de07 | 2018-03-15 16:46:26 -0700 | [diff] [blame] | 3544 | ad.telephony['subscription'][subid]['phone_num'] = phone_num |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3545 | except Exception: |
| 3546 | return False |
| 3547 | return True |
| 3548 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 3549 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3550 | def get_operator_name(log, ad, subId=None): |
| 3551 | """Get operator name (e.g. vzw, tmo) of droid. |
| 3552 | |
| 3553 | Args: |
| 3554 | ad: Android device object. |
| 3555 | sub_id: subscription ID |
| 3556 | Optional, default is None |
| 3557 | |
| 3558 | Returns: |
| 3559 | Operator name. |
| 3560 | """ |
| 3561 | try: |
| 3562 | if subId is not None: |
| 3563 | result = operator_name_from_plmn_id( |
Betty Zhou | d2da7ba | 2017-03-24 12:54:34 -0700 | [diff] [blame] | 3564 | ad.droid.telephonyGetNetworkOperatorForSubscription(subId)) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3565 | else: |
| 3566 | result = operator_name_from_plmn_id( |
Betty Zhou | d2da7ba | 2017-03-24 12:54:34 -0700 | [diff] [blame] | 3567 | ad.droid.telephonyGetNetworkOperator()) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3568 | except KeyError: |
Betty Zhou | 3ea5612 | 2018-03-14 17:37:24 -0700 | [diff] [blame] | 3569 | try: |
| 3570 | if subId is not None: |
| 3571 | result = ad.droid.telephonyGetNetworkOperatorNameForSubscription( |
| 3572 | sub_id) |
| 3573 | else: |
| 3574 | result = ad.droid.telephonyGetNetworkOperatorName() |
Betty Zhou | b14c101 | 2018-04-20 11:27:00 -0700 | [diff] [blame] | 3575 | result = operator_name_from_network_name(result) |
Betty Zhou | 3ea5612 | 2018-03-14 17:37:24 -0700 | [diff] [blame] | 3576 | except Exception: |
| 3577 | result = CARRIER_UNKNOWN |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3578 | return result |
| 3579 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 3580 | |
Yang Liu | 9a9a4f7 | 2016-02-19 10:45:04 -0800 | [diff] [blame] | 3581 | def get_model_name(ad): |
| 3582 | """Get android device model name |
| 3583 | |
| 3584 | Args: |
| 3585 | ad: Android device object |
| 3586 | |
| 3587 | Returns: |
| 3588 | model name string |
| 3589 | """ |
| 3590 | # TODO: Create translate table. |
| 3591 | model = ad.model |
Nathan Harold | 7642fc9 | 2016-05-02 18:29:11 -0700 | [diff] [blame] | 3592 | if (model.startswith(AOSP_PREFIX)): |
Yang Liu | 9a9a4f7 | 2016-02-19 10:45:04 -0800 | [diff] [blame] | 3593 | model = model[len(AOSP_PREFIX):] |
| 3594 | return model |
| 3595 | |
| 3596 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3597 | def is_sms_match(event, phonenumber_tx, text): |
| 3598 | """Return True if 'text' equals to event['data']['Text'] |
| 3599 | and phone number match. |
| 3600 | |
| 3601 | Args: |
| 3602 | event: Event object to verify. |
| 3603 | phonenumber_tx: phone number for sender. |
| 3604 | text: text string to verify. |
| 3605 | |
| 3606 | Returns: |
| 3607 | Return True if 'text' equals to event['data']['Text'] |
| 3608 | and phone number match. |
| 3609 | """ |
Betty Zhou | ee31105 | 2017-12-19 13:09:56 -0800 | [diff] [blame] | 3610 | return (check_phone_number_match(event['data']['Sender'], phonenumber_tx) |
| 3611 | and event['data']['Text'] == text) |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 3612 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3613 | |
| 3614 | def is_sms_partial_match(event, phonenumber_tx, text): |
| 3615 | """Return True if 'text' starts with event['data']['Text'] |
| 3616 | and phone number match. |
| 3617 | |
| 3618 | Args: |
| 3619 | event: Event object to verify. |
| 3620 | phonenumber_tx: phone number for sender. |
| 3621 | text: text string to verify. |
| 3622 | |
| 3623 | Returns: |
| 3624 | Return True if 'text' starts with event['data']['Text'] |
| 3625 | and phone number match. |
| 3626 | """ |
Betty Zhou | 1bc1209 | 2018-03-19 14:00:23 -0700 | [diff] [blame] | 3627 | event_text = event['data']['Text'] |
| 3628 | if event_text.startswith("("): |
| 3629 | event_text = event_text.split(")")[-1] |
Betty Zhou | ee31105 | 2017-12-19 13:09:56 -0800 | [diff] [blame] | 3630 | return (check_phone_number_match(event['data']['Sender'], phonenumber_tx) |
Betty Zhou | 1bc1209 | 2018-03-19 14:00:23 -0700 | [diff] [blame] | 3631 | and text.startswith(event_text)) |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 3632 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3633 | |
Betty Zhou | 02571f5 | 2018-02-16 14:11:16 -0800 | [diff] [blame] | 3634 | def sms_send_receive_verify(log, |
| 3635 | ad_tx, |
| 3636 | ad_rx, |
| 3637 | array_message, |
Betty Zhou | 632793c | 2018-02-09 18:45:39 -0800 | [diff] [blame] | 3638 | max_wait_time=MAX_WAIT_TIME_SMS_RECEIVE): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3639 | """Send SMS, receive SMS, and verify content and sender's number. |
| 3640 | |
| 3641 | Send (several) SMS from droid_tx to droid_rx. |
| 3642 | Verify SMS is sent, delivered and received. |
| 3643 | Verify received content and sender's number are correct. |
| 3644 | |
| 3645 | Args: |
| 3646 | log: Log object. |
| 3647 | ad_tx: Sender's Android Device Object |
| 3648 | ad_rx: Receiver's Android Device Object |
| 3649 | array_message: the array of message to send/receive |
| 3650 | """ |
Yang Liu | 963c93c | 2016-04-05 10:52:00 -0700 | [diff] [blame] | 3651 | subid_tx = get_outgoing_message_sub_id(ad_tx) |
| 3652 | subid_rx = get_incoming_message_sub_id(ad_rx) |
Betty Zhou | 74cf992 | 2018-04-18 20:18:12 -0700 | [diff] [blame] | 3653 | result = sms_send_receive_verify_for_subscription( |
Betty Zhou | 632793c | 2018-02-09 18:45:39 -0800 | [diff] [blame] | 3654 | log, ad_tx, ad_rx, subid_tx, subid_rx, array_message, max_wait_time) |
Betty Zhou | 74cf992 | 2018-04-18 20:18:12 -0700 | [diff] [blame] | 3655 | if not result: |
| 3656 | log_messaging_screen_shot(ad_tx, test_name="sms_tx") |
| 3657 | log_messaging_screen_shot(ad_rx, test_name="sms_rx") |
| 3658 | return result |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3659 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 3660 | |
| 3661 | def wait_for_matching_sms(log, |
| 3662 | ad_rx, |
| 3663 | phonenumber_tx, |
| 3664 | text, |
Betty Zhou | d32e0bd | 2017-12-19 19:44:58 -0800 | [diff] [blame] | 3665 | allow_multi_part_long_sms=True, |
Betty Zhou | 632793c | 2018-02-09 18:45:39 -0800 | [diff] [blame] | 3666 | begin_time=None, |
| 3667 | max_wait_time=MAX_WAIT_TIME_SMS_RECEIVE): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3668 | """Wait for matching incoming SMS. |
| 3669 | |
| 3670 | Args: |
| 3671 | log: Log object. |
| 3672 | ad_rx: Receiver's Android Device Object |
| 3673 | phonenumber_tx: Sender's phone number. |
| 3674 | text: SMS content string. |
| 3675 | allow_multi_part_long_sms: is long SMS allowed to be received as |
| 3676 | multiple short SMS. This is optional, default value is True. |
| 3677 | |
| 3678 | Returns: |
| 3679 | True if matching incoming SMS is received. |
| 3680 | """ |
| 3681 | if not allow_multi_part_long_sms: |
| 3682 | try: |
Betty Zhou | 632793c | 2018-02-09 18:45:39 -0800 | [diff] [blame] | 3683 | ad_rx.messaging_ed.wait_for_event(EventSmsReceived, is_sms_match, |
| 3684 | max_wait_time, phonenumber_tx, |
| 3685 | text) |
Betty Zhou | 5c7dbd1 | 2018-03-13 18:27:44 -0700 | [diff] [blame] | 3686 | ad_rx.log.info("Got event %s", EventSmsReceived) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3687 | return True |
| 3688 | except Empty: |
Betty Zhou | f809c5c | 2017-03-21 14:55:59 -0700 | [diff] [blame] | 3689 | ad_rx.log.error("No matched SMS received event.") |
Betty Zhou | d32e0bd | 2017-12-19 19:44:58 -0800 | [diff] [blame] | 3690 | if begin_time: |
Betty Zhou | 4c1c4b1 | 2017-12-21 17:06:13 -0800 | [diff] [blame] | 3691 | if sms_mms_receive_logcat_check(ad_rx, "sms", begin_time): |
Betty Zhou | d32e0bd | 2017-12-19 19:44:58 -0800 | [diff] [blame] | 3692 | ad_rx.log.info("Receivd SMS message is seen in logcat") |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3693 | return False |
| 3694 | else: |
| 3695 | try: |
| 3696 | received_sms = '' |
Betty Zhou | 1bc1209 | 2018-03-19 14:00:23 -0700 | [diff] [blame] | 3697 | remaining_text = text |
| 3698 | while (remaining_text != ''): |
Betty Zhou | 1c8c8d4 | 2018-03-14 12:43:50 -0700 | [diff] [blame] | 3699 | event = ad_rx.messaging_ed.wait_for_event( |
Betty Zhou | 02571f5 | 2018-02-16 14:11:16 -0800 | [diff] [blame] | 3700 | EventSmsReceived, is_sms_partial_match, max_wait_time, |
Betty Zhou | 1bc1209 | 2018-03-19 14:00:23 -0700 | [diff] [blame] | 3701 | phonenumber_tx, remaining_text) |
| 3702 | event_text = event['data']['Text'].split(")")[-1] |
| 3703 | event_text_length = len(event_text) |
| 3704 | ad_rx.log.info("Got event %s of text length %s from %s", |
| 3705 | EventSmsReceived, event_text_length, |
| 3706 | phonenumber_tx) |
| 3707 | remaining_text = remaining_text[event_text_length:] |
| 3708 | received_sms += event_text |
| 3709 | ad_rx.log.info("Received SMS of length %s", len(received_sms)) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3710 | return True |
| 3711 | except Empty: |
Betty Zhou | 1bc1209 | 2018-03-19 14:00:23 -0700 | [diff] [blame] | 3712 | ad_rx.log.error( |
| 3713 | "Missing SMS received event of text length %s from %s", |
| 3714 | len(remaining_text), phonenumber_tx) |
Betty Zhou | d32e0bd | 2017-12-19 19:44:58 -0800 | [diff] [blame] | 3715 | if begin_time: |
Betty Zhou | 4c1c4b1 | 2017-12-21 17:06:13 -0800 | [diff] [blame] | 3716 | if sms_mms_receive_logcat_check(ad_rx, "sms", begin_time): |
Betty Zhou | 1bc1209 | 2018-03-19 14:00:23 -0700 | [diff] [blame] | 3717 | ad_rx.log.info("Received SMS message is seen in logcat") |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3718 | if received_sms != '': |
Betty Zhou | 1bc1209 | 2018-03-19 14:00:23 -0700 | [diff] [blame] | 3719 | ad_rx.log.error( |
| 3720 | "Only received partial matched SMS of length %s", |
| 3721 | len(received_sms)) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3722 | return False |
| 3723 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 3724 | |
Betty Zhou | a27e5d4 | 2017-01-17 11:33:04 -0800 | [diff] [blame] | 3725 | def is_mms_match(event, phonenumber_tx, text): |
| 3726 | """Return True if 'text' equals to event['data']['Text'] |
| 3727 | and phone number match. |
| 3728 | |
| 3729 | Args: |
| 3730 | event: Event object to verify. |
| 3731 | phonenumber_tx: phone number for sender. |
| 3732 | text: text string to verify. |
| 3733 | |
| 3734 | Returns: |
| 3735 | Return True if 'text' equals to event['data']['Text'] |
| 3736 | and phone number match. |
| 3737 | """ |
| 3738 | #TODO: add mms matching after mms message parser is added in sl4a. b/34276948 |
| 3739 | return True |
| 3740 | |
| 3741 | |
Betty Zhou | 02571f5 | 2018-02-16 14:11:16 -0800 | [diff] [blame] | 3742 | def wait_for_matching_mms(log, |
| 3743 | ad_rx, |
| 3744 | phonenumber_tx, |
| 3745 | text, |
| 3746 | begin_time=None, |
Betty Zhou | 632793c | 2018-02-09 18:45:39 -0800 | [diff] [blame] | 3747 | max_wait_time=MAX_WAIT_TIME_SMS_RECEIVE): |
Betty Zhou | a27e5d4 | 2017-01-17 11:33:04 -0800 | [diff] [blame] | 3748 | """Wait for matching incoming SMS. |
| 3749 | |
| 3750 | Args: |
| 3751 | log: Log object. |
| 3752 | ad_rx: Receiver's Android Device Object |
| 3753 | phonenumber_tx: Sender's phone number. |
| 3754 | text: SMS content string. |
| 3755 | allow_multi_part_long_sms: is long SMS allowed to be received as |
| 3756 | multiple short SMS. This is optional, default value is True. |
| 3757 | |
| 3758 | Returns: |
| 3759 | True if matching incoming SMS is received. |
| 3760 | """ |
| 3761 | try: |
| 3762 | #TODO: add mms matching after mms message parser is added in sl4a. b/34276948 |
Betty Zhou | 632793c | 2018-02-09 18:45:39 -0800 | [diff] [blame] | 3763 | ad_rx.messaging_ed.wait_for_event(EventMmsDownloaded, is_mms_match, |
| 3764 | max_wait_time, phonenumber_tx, text) |
Betty Zhou | 5c7dbd1 | 2018-03-13 18:27:44 -0700 | [diff] [blame] | 3765 | ad_rx.log.info("Got event %s", EventMmsDownloaded) |
Betty Zhou | 6b3593f | 2018-03-16 20:13:55 -0700 | [diff] [blame] | 3766 | smshandle_logs = ad_rx.search_logcat( |
Betty Zhou | 1bc1209 | 2018-03-19 14:00:23 -0700 | [diff] [blame] | 3767 | "InboundSmsHandler: No broadcast sent on processing EVENT_BROADCAST_SMS", |
Betty Zhou | 6b3593f | 2018-03-16 20:13:55 -0700 | [diff] [blame] | 3768 | begin_time=begin_time) |
| 3769 | if smshandle_logs: |
| 3770 | ad_rx.log.warning("Found %s", smshandle_logs[-1]["log_message"]) |
Betty Zhou | a27e5d4 | 2017-01-17 11:33:04 -0800 | [diff] [blame] | 3771 | return True |
| 3772 | except Empty: |
Betty Zhou | f809c5c | 2017-03-21 14:55:59 -0700 | [diff] [blame] | 3773 | ad_rx.log.warning("No matched MMS downloaded event.") |
Betty Zhou | d32e0bd | 2017-12-19 19:44:58 -0800 | [diff] [blame] | 3774 | if begin_time: |
Betty Zhou | 4c1c4b1 | 2017-12-21 17:06:13 -0800 | [diff] [blame] | 3775 | if sms_mms_receive_logcat_check(ad_rx, "mms", begin_time): |
Betty Zhou | d32e0bd | 2017-12-19 19:44:58 -0800 | [diff] [blame] | 3776 | return True |
Betty Zhou | a27e5d4 | 2017-01-17 11:33:04 -0800 | [diff] [blame] | 3777 | return False |
| 3778 | |
| 3779 | |
Betty Zhou | 632793c | 2018-02-09 18:45:39 -0800 | [diff] [blame] | 3780 | def sms_send_receive_verify_for_subscription( |
Betty Zhou | 02571f5 | 2018-02-16 14:11:16 -0800 | [diff] [blame] | 3781 | log, |
| 3782 | ad_tx, |
| 3783 | ad_rx, |
| 3784 | subid_tx, |
| 3785 | subid_rx, |
| 3786 | array_message, |
| 3787 | max_wait_time=MAX_WAIT_TIME_SMS_RECEIVE): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3788 | """Send SMS, receive SMS, and verify content and sender's number. |
| 3789 | |
| 3790 | Send (several) SMS from droid_tx to droid_rx. |
| 3791 | Verify SMS is sent, delivered and received. |
| 3792 | Verify received content and sender's number are correct. |
| 3793 | |
| 3794 | Args: |
| 3795 | log: Log object. |
| 3796 | ad_tx: Sender's Android Device Object.. |
| 3797 | ad_rx: Receiver's Android Device Object. |
| 3798 | subid_tx: Sender's subsciption ID to be used for SMS |
| 3799 | subid_rx: Receiver's subsciption ID to be used for SMS |
| 3800 | array_message: the array of message to send/receive |
| 3801 | """ |
Betty Zhou | 3b2de07 | 2018-03-15 16:46:26 -0700 | [diff] [blame] | 3802 | phonenumber_tx = ad_tx.telephony['subscription'][subid_tx]['phone_num'] |
| 3803 | phonenumber_rx = ad_rx.telephony['subscription'][subid_rx]['phone_num'] |
Betty Zhou | d32e0bd | 2017-12-19 19:44:58 -0800 | [diff] [blame] | 3804 | |
Betty Zhou | 632793c | 2018-02-09 18:45:39 -0800 | [diff] [blame] | 3805 | for ad in (ad_tx, ad_rx): |
| 3806 | if not getattr(ad, "messaging_droid", None): |
Betty Zhou | 08c1a57 | 2018-02-13 20:07:55 -0800 | [diff] [blame] | 3807 | ad.messaging_droid, ad.messaging_ed = ad.get_droid() |
| 3808 | ad.messaging_ed.start() |
Betty Zhou | f31dffe | 2018-02-23 19:29:28 -0800 | [diff] [blame] | 3809 | else: |
| 3810 | try: |
| 3811 | if not ad.messaging_droid.is_live: |
| 3812 | ad.messaging_droid, ad.messaging_ed = ad.get_droid() |
| 3813 | ad.messaging_ed.start() |
Betty Zhou | 6b3593f | 2018-03-16 20:13:55 -0700 | [diff] [blame] | 3814 | else: |
| 3815 | ad.messaging_ed.clear_all_events() |
| 3816 | ad.messaging_droid.logI( |
| 3817 | "Start sms_send_receive_verify_for_subscription test") |
Betty Zhou | e57ab69 | 2018-03-09 18:39:30 -0800 | [diff] [blame] | 3818 | except Exception: |
| 3819 | ad.log.info("Create new sl4a session for messaging") |
Betty Zhou | f31dffe | 2018-02-23 19:29:28 -0800 | [diff] [blame] | 3820 | ad.messaging_droid, ad.messaging_ed = ad.get_droid() |
| 3821 | ad.messaging_ed.start() |
| 3822 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3823 | for text in array_message: |
Betty Zhou | cbea025 | 2018-01-17 19:07:22 -0800 | [diff] [blame] | 3824 | # set begin_time 300ms before current time to system time discrepency |
| 3825 | begin_time = get_current_epoch_time() - 300 |
Betty Zhou | d2da7ba | 2017-03-24 12:54:34 -0700 | [diff] [blame] | 3826 | length = len(text) |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 3827 | ad_tx.log.info("Sending SMS from %s to %s, len: %s, content: %s.", |
Betty Zhou | d2da7ba | 2017-03-24 12:54:34 -0700 | [diff] [blame] | 3828 | phonenumber_tx, phonenumber_rx, length, text) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3829 | try: |
Betty Zhou | 632793c | 2018-02-09 18:45:39 -0800 | [diff] [blame] | 3830 | ad_rx.messaging_ed.clear_events(EventSmsReceived) |
| 3831 | ad_tx.messaging_ed.clear_events(EventSmsSentSuccess) |
Betty Zhou | 4411c20 | 2018-03-29 18:27:25 -0700 | [diff] [blame] | 3832 | ad_tx.messaging_ed.clear_events(EventSmsSentFailure) |
Betty Zhou | 632793c | 2018-02-09 18:45:39 -0800 | [diff] [blame] | 3833 | ad_rx.messaging_droid.smsStartTrackingIncomingSmsMessage() |
Betty Zhou | f1cc3b6 | 2018-02-28 12:02:05 -0800 | [diff] [blame] | 3834 | time.sleep(1) #sleep 100ms after starting event tracking |
Betty Zhou | 4411c20 | 2018-03-29 18:27:25 -0700 | [diff] [blame] | 3835 | ad_tx.messaging_droid.logI("Sending SMS of length %s" % length) |
| 3836 | ad_rx.messaging_droid.logI("Expecting SMS of length %s" % length) |
Betty Zhou | 02571f5 | 2018-02-16 14:11:16 -0800 | [diff] [blame] | 3837 | ad_tx.messaging_droid.smsSendTextMessage(phonenumber_rx, text, |
Betty Zhou | 4411c20 | 2018-03-29 18:27:25 -0700 | [diff] [blame] | 3838 | True) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3839 | try: |
Betty Zhou | 4411c20 | 2018-03-29 18:27:25 -0700 | [diff] [blame] | 3840 | events = ad_tx.messaging_ed.pop_events( |
Betty Zhou | c9f723b | 2018-04-10 18:08:21 -0700 | [diff] [blame] | 3841 | "(%s|%s|%s|%s)" % |
| 3842 | (EventSmsSentSuccess, EventSmsSentFailure, |
| 3843 | EventSmsDeliverSuccess, |
| 3844 | EventSmsDeliverFailure), max_wait_time) |
Betty Zhou | 4411c20 | 2018-03-29 18:27:25 -0700 | [diff] [blame] | 3845 | for event in events: |
| 3846 | ad_tx.log.info("Got event %s", event["name"]) |
Betty Zhou | c9f723b | 2018-04-10 18:08:21 -0700 | [diff] [blame] | 3847 | if event["name"] == EventSmsSentFailure or event["name"] == EventSmsDeliverFailure: |
Betty Zhou | 4411c20 | 2018-03-29 18:27:25 -0700 | [diff] [blame] | 3848 | if event.get("data") and event["data"].get("Reason"): |
| 3849 | ad_tx.log.error("%s with reason: %s", |
| 3850 | event["name"], |
| 3851 | event["data"]["Reason"]) |
| 3852 | return False |
Betty Zhou | c9f723b | 2018-04-10 18:08:21 -0700 | [diff] [blame] | 3853 | elif event["name"] == EventSmsSentSuccess or event["name"] == EventSmsDeliverSuccess: |
Betty Zhou | 4411c20 | 2018-03-29 18:27:25 -0700 | [diff] [blame] | 3854 | break |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3855 | except Empty: |
Betty Zhou | 4411c20 | 2018-03-29 18:27:25 -0700 | [diff] [blame] | 3856 | ad_tx.log.error("No %s or %s event for SMS of length %s.", |
| 3857 | EventSmsSentSuccess, EventSmsSentFailure, |
Betty Zhou | d2da7ba | 2017-03-24 12:54:34 -0700 | [diff] [blame] | 3858 | length) |
Betty Zhou | d32e0bd | 2017-12-19 19:44:58 -0800 | [diff] [blame] | 3859 | # check log message as a work around for the missing sl4a |
| 3860 | # event dispatcher event |
Betty Zhou | 6b91ab2 | 2017-12-21 18:16:36 -0800 | [diff] [blame] | 3861 | if not sms_mms_send_logcat_check(ad_tx, "sms", begin_time): |
| 3862 | return False |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3863 | |
Nathan Harold | 4a144a4 | 2016-09-19 14:16:24 -0700 | [diff] [blame] | 3864 | if not wait_for_matching_sms( |
| 3865 | log, |
| 3866 | ad_rx, |
| 3867 | phonenumber_tx, |
| 3868 | text, |
Betty Zhou | d32e0bd | 2017-12-19 19:44:58 -0800 | [diff] [blame] | 3869 | allow_multi_part_long_sms=True, |
| 3870 | begin_time=begin_time): |
Betty Zhou | d2da7ba | 2017-03-24 12:54:34 -0700 | [diff] [blame] | 3871 | ad_rx.log.error("No matching received SMS of length %s.", |
| 3872 | length) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3873 | return False |
Betty Zhou | 632793c | 2018-02-09 18:45:39 -0800 | [diff] [blame] | 3874 | except Exception as e: |
| 3875 | log.error("Exception error %s", e) |
| 3876 | raise |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3877 | finally: |
Betty Zhou | 632793c | 2018-02-09 18:45:39 -0800 | [diff] [blame] | 3878 | ad_rx.messaging_droid.smsStopTrackingIncomingSmsMessage() |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3879 | return True |
| 3880 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 3881 | |
Betty Zhou | 02571f5 | 2018-02-16 14:11:16 -0800 | [diff] [blame] | 3882 | def mms_send_receive_verify(log, |
| 3883 | ad_tx, |
| 3884 | ad_rx, |
| 3885 | array_message, |
Betty Zhou | 632793c | 2018-02-09 18:45:39 -0800 | [diff] [blame] | 3886 | max_wait_time=MAX_WAIT_TIME_SMS_RECEIVE): |
Betty Zhou | 2e08ac3 | 2017-01-27 14:21:29 -0800 | [diff] [blame] | 3887 | """Send MMS, receive MMS, and verify content and sender's number. |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3888 | |
Betty Zhou | 2e08ac3 | 2017-01-27 14:21:29 -0800 | [diff] [blame] | 3889 | Send (several) MMS from droid_tx to droid_rx. |
| 3890 | Verify MMS is sent, delivered and received. |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3891 | Verify received content and sender's number are correct. |
| 3892 | |
| 3893 | Args: |
| 3894 | log: Log object. |
| 3895 | ad_tx: Sender's Android Device Object |
| 3896 | ad_rx: Receiver's Android Device Object |
| 3897 | array_message: the array of message to send/receive |
| 3898 | """ |
Betty Zhou | 74cf992 | 2018-04-18 20:18:12 -0700 | [diff] [blame] | 3899 | result = mms_send_receive_verify_for_subscription( |
Betty Zhou | ee31105 | 2017-12-19 13:09:56 -0800 | [diff] [blame] | 3900 | log, ad_tx, ad_rx, get_outgoing_message_sub_id(ad_tx), |
Betty Zhou | 632793c | 2018-02-09 18:45:39 -0800 | [diff] [blame] | 3901 | get_incoming_message_sub_id(ad_rx), array_message, max_wait_time) |
Betty Zhou | 74cf992 | 2018-04-18 20:18:12 -0700 | [diff] [blame] | 3902 | if not result: |
| 3903 | log_messaging_screen_shot(ad_tx, test_name="mms_tx") |
| 3904 | log_messaging_screen_shot(ad_rx, test_name="mms_rx") |
| 3905 | return result |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 3906 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3907 | |
Betty Zhou | 38028b7 | 2017-12-20 17:54:12 -0800 | [diff] [blame] | 3908 | def sms_mms_send_logcat_check(ad, type, begin_time): |
| 3909 | type = type.upper() |
| 3910 | log_results = ad.search_logcat( |
| 3911 | "%s Message sent successfully" % type, begin_time=begin_time) |
| 3912 | if log_results: |
Betty Zhou | 1bc1209 | 2018-03-19 14:00:23 -0700 | [diff] [blame] | 3913 | ad.log.info("Found %s sent successful log message: %s", type, |
Betty Zhou | f1cc3b6 | 2018-02-28 12:02:05 -0800 | [diff] [blame] | 3914 | log_results[-1]["log_message"]) |
Betty Zhou | 38028b7 | 2017-12-20 17:54:12 -0800 | [diff] [blame] | 3915 | return True |
| 3916 | else: |
| 3917 | log_results = ad.search_logcat( |
| 3918 | "ProcessSentMessageAction: Done sending %s message" % type, |
| 3919 | begin_time=begin_time) |
| 3920 | if log_results: |
| 3921 | for log_result in log_results: |
| 3922 | if "status is SUCCEEDED" in log_result["log_message"]: |
| 3923 | ad.log.info( |
Betty Zhou | f1cc3b6 | 2018-02-28 12:02:05 -0800 | [diff] [blame] | 3924 | "Found BugleDataModel %s send succeed log message: %s", |
| 3925 | type, log_result["log_message"]) |
Betty Zhou | 38028b7 | 2017-12-20 17:54:12 -0800 | [diff] [blame] | 3926 | return True |
| 3927 | return False |
| 3928 | |
| 3929 | |
| 3930 | def sms_mms_receive_logcat_check(ad, type, begin_time): |
| 3931 | type = type.upper() |
Betty Zhou | 6b3593f | 2018-03-16 20:13:55 -0700 | [diff] [blame] | 3932 | smshandle_logs = ad.search_logcat( |
Betty Zhou | 1bc1209 | 2018-03-19 14:00:23 -0700 | [diff] [blame] | 3933 | "InboundSmsHandler: No broadcast sent on processing EVENT_BROADCAST_SMS", |
Betty Zhou | 6b3593f | 2018-03-16 20:13:55 -0700 | [diff] [blame] | 3934 | begin_time=begin_time) |
| 3935 | if smshandle_logs: |
| 3936 | ad.log.warning("Found %s", smshandle_logs[-1]["log_message"]) |
Betty Zhou | 38028b7 | 2017-12-20 17:54:12 -0800 | [diff] [blame] | 3937 | log_results = ad.search_logcat( |
Jaineel | 6cd6d44 | 2018-01-08 14:50:44 -0800 | [diff] [blame] | 3938 | "New %s Received" % type, begin_time=begin_time) or \ |
| 3939 | ad.search_logcat("New %s Downloaded" % type, begin_time=begin_time) |
Betty Zhou | 38028b7 | 2017-12-20 17:54:12 -0800 | [diff] [blame] | 3940 | if log_results: |
Betty Zhou | f1cc3b6 | 2018-02-28 12:02:05 -0800 | [diff] [blame] | 3941 | ad.log.info("Found SL4A %s received log message: %s", type, |
| 3942 | log_results[-1]["log_message"]) |
Betty Zhou | 38028b7 | 2017-12-20 17:54:12 -0800 | [diff] [blame] | 3943 | return True |
| 3944 | else: |
| 3945 | log_results = ad.search_logcat( |
| 3946 | "Received %s message" % type, begin_time=begin_time) |
| 3947 | if log_results: |
Betty Zhou | f1cc3b6 | 2018-02-28 12:02:05 -0800 | [diff] [blame] | 3948 | ad.log.info("Found %s received log message: %s", type, |
| 3949 | log_results[-1]["log_message"]) |
Betty Zhou | 10f887e | 2018-04-10 12:45:00 -0700 | [diff] [blame] | 3950 | log_results = ad.search_logcat( |
| 3951 | "ProcessDownloadedMmsAction", begin_time=begin_time) |
| 3952 | for log_result in log_results: |
| 3953 | ad.log.info("Found %s", log_result["log_message"]) |
| 3954 | if "status is SUCCEEDED" in log_result["log_message"]: |
| 3955 | ad.log.info("Download succeed with ProcessDownloadedMmsAction") |
| 3956 | return True |
Betty Zhou | 38028b7 | 2017-12-20 17:54:12 -0800 | [diff] [blame] | 3957 | return False |
| 3958 | |
| 3959 | |
Betty Zhou | a27e5d4 | 2017-01-17 11:33:04 -0800 | [diff] [blame] | 3960 | #TODO: add mms matching after mms message parser is added in sl4a. b/34276948 |
Betty Zhou | 632793c | 2018-02-09 18:45:39 -0800 | [diff] [blame] | 3961 | def mms_send_receive_verify_for_subscription( |
Betty Zhou | 02571f5 | 2018-02-16 14:11:16 -0800 | [diff] [blame] | 3962 | log, |
| 3963 | ad_tx, |
| 3964 | ad_rx, |
| 3965 | subid_tx, |
| 3966 | subid_rx, |
| 3967 | array_payload, |
| 3968 | max_wait_time=MAX_WAIT_TIME_SMS_RECEIVE): |
Betty Zhou | 2e08ac3 | 2017-01-27 14:21:29 -0800 | [diff] [blame] | 3969 | """Send MMS, receive MMS, and verify content and sender's number. |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3970 | |
Betty Zhou | 2e08ac3 | 2017-01-27 14:21:29 -0800 | [diff] [blame] | 3971 | Send (several) MMS from droid_tx to droid_rx. |
| 3972 | Verify MMS is sent, delivered and received. |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 3973 | Verify received content and sender's number are correct. |
| 3974 | |
| 3975 | Args: |
| 3976 | log: Log object. |
| 3977 | ad_tx: Sender's Android Device Object.. |
| 3978 | ad_rx: Receiver's Android Device Object. |
| 3979 | subid_tx: Sender's subsciption ID to be used for SMS |
| 3980 | subid_rx: Receiver's subsciption ID to be used for SMS |
| 3981 | array_message: the array of message to send/receive |
| 3982 | """ |
| 3983 | |
Betty Zhou | 3b2de07 | 2018-03-15 16:46:26 -0700 | [diff] [blame] | 3984 | phonenumber_tx = ad_tx.telephony['subscription'][subid_tx]['phone_num'] |
| 3985 | phonenumber_rx = ad_rx.telephony['subscription'][subid_rx]['phone_num'] |
Betty Zhou | f715c79 | 2018-02-12 17:32:10 -0800 | [diff] [blame] | 3986 | |
Betty Zhou | e57ab69 | 2018-03-09 18:39:30 -0800 | [diff] [blame] | 3987 | for ad in (ad_tx, ad_rx): |
Betty Zhou | c3eedd7 | 2017-11-09 17:50:01 -0800 | [diff] [blame] | 3988 | if "Permissive" not in ad.adb.shell("su root getenforce"): |
| 3989 | ad.adb.shell("su root setenforce 0") |
Betty Zhou | 632793c | 2018-02-09 18:45:39 -0800 | [diff] [blame] | 3990 | if not getattr(ad, "messaging_droid", None): |
Betty Zhou | 08c1a57 | 2018-02-13 20:07:55 -0800 | [diff] [blame] | 3991 | ad.messaging_droid, ad.messaging_ed = ad.get_droid() |
| 3992 | ad.messaging_ed.start() |
Betty Zhou | e57ab69 | 2018-03-09 18:39:30 -0800 | [diff] [blame] | 3993 | else: |
| 3994 | try: |
| 3995 | if not ad.messaging_droid.is_live: |
| 3996 | ad.messaging_droid, ad.messaging_ed = ad.get_droid() |
| 3997 | ad.messaging_ed.start() |
Betty Zhou | 6b3593f | 2018-03-16 20:13:55 -0700 | [diff] [blame] | 3998 | else: |
| 3999 | ad.messaging_ed.clear_all_events() |
| 4000 | ad.messaging_droid.logI( |
| 4001 | "Start mms_send_receive_verify_for_subscription test") |
Betty Zhou | e57ab69 | 2018-03-09 18:39:30 -0800 | [diff] [blame] | 4002 | except Exception: |
| 4003 | ad.log.info("Create new sl4a session for messaging") |
| 4004 | ad.messaging_droid, ad.messaging_ed = ad.get_droid() |
| 4005 | ad.messaging_ed.start() |
Betty Zhou | 38028b7 | 2017-12-20 17:54:12 -0800 | [diff] [blame] | 4006 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 4007 | for subject, message, filename in array_payload: |
Betty Zhou | d32e0bd | 2017-12-19 19:44:58 -0800 | [diff] [blame] | 4008 | begin_time = get_current_epoch_time() |
Betty Zhou | 632793c | 2018-02-09 18:45:39 -0800 | [diff] [blame] | 4009 | ad_tx.messaging_ed.clear_events(EventMmsSentSuccess) |
Betty Zhou | 4411c20 | 2018-03-29 18:27:25 -0700 | [diff] [blame] | 4010 | ad_tx.messaging_ed.clear_events(EventMmsSentFailure) |
Betty Zhou | 632793c | 2018-02-09 18:45:39 -0800 | [diff] [blame] | 4011 | ad_rx.messaging_ed.clear_events(EventMmsDownloaded) |
| 4012 | ad_rx.messaging_droid.smsStartTrackingIncomingMmsMessage() |
Betty Zhou | f715c79 | 2018-02-12 17:32:10 -0800 | [diff] [blame] | 4013 | ad_tx.log.info( |
| 4014 | "Sending MMS from %s to %s, subject: %s, message: %s, file: %s.", |
| 4015 | phonenumber_tx, phonenumber_rx, subject, message, filename) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 4016 | try: |
Betty Zhou | 632793c | 2018-02-09 18:45:39 -0800 | [diff] [blame] | 4017 | ad_tx.messaging_droid.smsSendMultimediaMessage( |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 4018 | phonenumber_rx, subject, message, phonenumber_tx, filename) |
Betty Zhou | a27e5d4 | 2017-01-17 11:33:04 -0800 | [diff] [blame] | 4019 | try: |
Betty Zhou | 4411c20 | 2018-03-29 18:27:25 -0700 | [diff] [blame] | 4020 | events = ad_tx.messaging_ed.pop_events( |
| 4021 | "(%s|%s)" % (EventMmsSentSuccess, |
| 4022 | EventMmsSentFailure), max_wait_time) |
| 4023 | for event in events: |
| 4024 | ad_tx.log.info("Got event %s", event["name"]) |
| 4025 | if event["name"] == EventMmsSentFailure: |
| 4026 | if event.get("data") and event["data"].get("Reason"): |
| 4027 | ad_tx.log.error("%s with reason: %s", |
| 4028 | event["name"], |
| 4029 | event["data"]["Reason"]) |
| 4030 | return False |
| 4031 | elif event["name"] == EventMmsSentSuccess: |
| 4032 | break |
Betty Zhou | a27e5d4 | 2017-01-17 11:33:04 -0800 | [diff] [blame] | 4033 | except Empty: |
Betty Zhou | 4411c20 | 2018-03-29 18:27:25 -0700 | [diff] [blame] | 4034 | ad_tx.log.warning("No %s or %s event.", EventMmsSentSuccess, |
| 4035 | EventMmsSentFailure) |
Betty Zhou | d32e0bd | 2017-12-19 19:44:58 -0800 | [diff] [blame] | 4036 | # check log message as a work around for the missing sl4a |
| 4037 | # event dispatcher event |
Betty Zhou | 38028b7 | 2017-12-20 17:54:12 -0800 | [diff] [blame] | 4038 | if not sms_mms_send_logcat_check(ad_tx, "mms", begin_time): |
Betty Zhou | d32e0bd | 2017-12-19 19:44:58 -0800 | [diff] [blame] | 4039 | return False |
Betty Zhou | 2e08ac3 | 2017-01-27 14:21:29 -0800 | [diff] [blame] | 4040 | |
Betty Zhou | d32e0bd | 2017-12-19 19:44:58 -0800 | [diff] [blame] | 4041 | if not wait_for_matching_mms( |
| 4042 | log, ad_rx, phonenumber_tx, message, |
| 4043 | begin_time=begin_time): |
Betty Zhou | 2e08ac3 | 2017-01-27 14:21:29 -0800 | [diff] [blame] | 4044 | return False |
Betty Zhou | 632793c | 2018-02-09 18:45:39 -0800 | [diff] [blame] | 4045 | except Exception as e: |
| 4046 | log.error("Exception error %s", e) |
| 4047 | raise |
Betty Zhou | 2e08ac3 | 2017-01-27 14:21:29 -0800 | [diff] [blame] | 4048 | finally: |
Betty Zhou | 38028b7 | 2017-12-20 17:54:12 -0800 | [diff] [blame] | 4049 | ad_rx.droid.smsStopTrackingIncomingMmsMessage() |
Betty Zhou | 2e08ac3 | 2017-01-27 14:21:29 -0800 | [diff] [blame] | 4050 | return True |
| 4051 | |
| 4052 | |
Betty Zhou | 632793c | 2018-02-09 18:45:39 -0800 | [diff] [blame] | 4053 | def mms_receive_verify_after_call_hangup( |
Betty Zhou | 02571f5 | 2018-02-16 14:11:16 -0800 | [diff] [blame] | 4054 | log, ad_tx, ad_rx, array_message, |
| 4055 | max_wait_time=MAX_WAIT_TIME_SMS_RECEIVE): |
Betty Zhou | 2e08ac3 | 2017-01-27 14:21:29 -0800 | [diff] [blame] | 4056 | """Verify the suspanded MMS during call will send out after call release. |
| 4057 | |
| 4058 | Hangup call from droid_tx to droid_rx. |
| 4059 | Verify MMS is sent, delivered and received. |
| 4060 | Verify received content and sender's number are correct. |
| 4061 | |
| 4062 | Args: |
| 4063 | log: Log object. |
| 4064 | ad_tx: Sender's Android Device Object |
| 4065 | ad_rx: Receiver's Android Device Object |
| 4066 | array_message: the array of message to send/receive |
| 4067 | """ |
| 4068 | return mms_receive_verify_after_call_hangup_for_subscription( |
Betty Zhou | ee31105 | 2017-12-19 13:09:56 -0800 | [diff] [blame] | 4069 | log, ad_tx, ad_rx, get_outgoing_message_sub_id(ad_tx), |
Betty Zhou | 632793c | 2018-02-09 18:45:39 -0800 | [diff] [blame] | 4070 | get_incoming_message_sub_id(ad_rx), array_message, max_wait_time) |
Betty Zhou | 2e08ac3 | 2017-01-27 14:21:29 -0800 | [diff] [blame] | 4071 | |
| 4072 | |
| 4073 | #TODO: add mms matching after mms message parser is added in sl4a. b/34276948 |
| 4074 | def mms_receive_verify_after_call_hangup_for_subscription( |
Betty Zhou | 02571f5 | 2018-02-16 14:11:16 -0800 | [diff] [blame] | 4075 | log, |
| 4076 | ad_tx, |
| 4077 | ad_rx, |
| 4078 | subid_tx, |
| 4079 | subid_rx, |
| 4080 | array_payload, |
Betty Zhou | 632793c | 2018-02-09 18:45:39 -0800 | [diff] [blame] | 4081 | max_wait_time=MAX_WAIT_TIME_SMS_RECEIVE): |
Betty Zhou | 2e08ac3 | 2017-01-27 14:21:29 -0800 | [diff] [blame] | 4082 | """Verify the suspanded MMS during call will send out after call release. |
| 4083 | |
| 4084 | Hangup call from droid_tx to droid_rx. |
| 4085 | Verify MMS is sent, delivered and received. |
| 4086 | Verify received content and sender's number are correct. |
| 4087 | |
| 4088 | Args: |
| 4089 | log: Log object. |
| 4090 | ad_tx: Sender's Android Device Object.. |
| 4091 | ad_rx: Receiver's Android Device Object. |
| 4092 | subid_tx: Sender's subsciption ID to be used for SMS |
| 4093 | subid_rx: Receiver's subsciption ID to be used for SMS |
| 4094 | array_message: the array of message to send/receive |
| 4095 | """ |
| 4096 | |
Betty Zhou | 3b2de07 | 2018-03-15 16:46:26 -0700 | [diff] [blame] | 4097 | phonenumber_tx = ad_tx.telephony['subscription'][subid_tx]['phone_num'] |
| 4098 | phonenumber_rx = ad_rx.telephony['subscription'][subid_rx]['phone_num'] |
Betty Zhou | 08c1a57 | 2018-02-13 20:07:55 -0800 | [diff] [blame] | 4099 | for ad in (ad_tx, ad_rx): |
| 4100 | if not getattr(ad, "messaging_droid", None): |
| 4101 | ad.messaging_droid, ad.messaging_ed = ad.get_droid() |
| 4102 | ad.messaging_ed.start() |
Betty Zhou | 2e08ac3 | 2017-01-27 14:21:29 -0800 | [diff] [blame] | 4103 | for subject, message, filename in array_payload: |
Betty Zhou | d32e0bd | 2017-12-19 19:44:58 -0800 | [diff] [blame] | 4104 | begin_time = get_current_epoch_time() |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 4105 | ad_rx.log.info( |
| 4106 | "Waiting MMS from %s to %s, subject: %s, message: %s, file: %s.", |
| 4107 | phonenumber_tx, phonenumber_rx, subject, message, filename) |
Betty Zhou | 632793c | 2018-02-09 18:45:39 -0800 | [diff] [blame] | 4108 | ad_rx.messaging_droid.smsStartTrackingIncomingMmsMessage() |
Betty Zhou | 2e08ac3 | 2017-01-27 14:21:29 -0800 | [diff] [blame] | 4109 | time.sleep(5) |
| 4110 | try: |
| 4111 | hangup_call(log, ad_tx) |
| 4112 | hangup_call(log, ad_rx) |
| 4113 | try: |
Betty Zhou | 3ea5612 | 2018-03-14 17:37:24 -0700 | [diff] [blame] | 4114 | ad_tx.messaging_ed.pop_event(EventMmsSentSuccess, |
| 4115 | max_wait_time) |
Betty Zhou | 1c8c8d4 | 2018-03-14 12:43:50 -0700 | [diff] [blame] | 4116 | ad_tx.log.info("Got event %s", EventMmsSentSuccess) |
Betty Zhou | 2e08ac3 | 2017-01-27 14:21:29 -0800 | [diff] [blame] | 4117 | except Empty: |
Betty Zhou | f809c5c | 2017-03-21 14:55:59 -0700 | [diff] [blame] | 4118 | log.warning("No sent_success event.") |
Betty Zhou | 38028b7 | 2017-12-20 17:54:12 -0800 | [diff] [blame] | 4119 | if not sms_mms_send_logcat_check(ad_tx, "mms", begin_time): |
Betty Zhou | d32e0bd | 2017-12-19 19:44:58 -0800 | [diff] [blame] | 4120 | return False |
| 4121 | if not wait_for_matching_mms( |
| 4122 | log, ad_rx, phonenumber_tx, message, |
| 4123 | begin_time=begin_time): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 4124 | return False |
| 4125 | finally: |
Betty Zhou | 38028b7 | 2017-12-20 17:54:12 -0800 | [diff] [blame] | 4126 | ad_rx.droid.smsStopTrackingIncomingMmsMessage() |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 4127 | return True |
| 4128 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 4129 | |
| 4130 | def ensure_network_rat(log, |
| 4131 | ad, |
| 4132 | network_preference, |
| 4133 | rat_family, |
| 4134 | voice_or_data=None, |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 4135 | max_wait_time=MAX_WAIT_TIME_NW_SELECTION, |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 4136 | toggle_apm_after_setting=False): |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 4137 | """Ensure ad's current network is in expected rat_family. |
| 4138 | """ |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 4139 | return ensure_network_rat_for_subscription( |
Betty Zhou | ee31105 | 2017-12-19 13:09:56 -0800 | [diff] [blame] | 4140 | log, ad, ad.droid.subscriptionGetDefaultSubId(), network_preference, |
| 4141 | rat_family, voice_or_data, max_wait_time, toggle_apm_after_setting) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 4142 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 4143 | |
Nathan Harold | 7642fc9 | 2016-05-02 18:29:11 -0700 | [diff] [blame] | 4144 | def ensure_network_rat_for_subscription( |
| 4145 | log, |
| 4146 | ad, |
| 4147 | sub_id, |
| 4148 | network_preference, |
| 4149 | rat_family, |
| 4150 | voice_or_data=None, |
| 4151 | max_wait_time=MAX_WAIT_TIME_NW_SELECTION, |
| 4152 | toggle_apm_after_setting=False): |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 4153 | """Ensure ad's current network is in expected rat_family. |
| 4154 | """ |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 4155 | if not ad.droid.telephonySetPreferredNetworkTypesForSubscription( |
| 4156 | network_preference, sub_id): |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 4157 | ad.log.error("Set sub_id %s Preferred Networks Type %s failed.", |
| 4158 | sub_id, network_preference) |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 4159 | return False |
| 4160 | 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] | 4161 | voice_or_data): |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 4162 | ad.log.info("Sub_id %s in RAT %s for %s", sub_id, rat_family, |
| 4163 | voice_or_data) |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 4164 | return True |
| 4165 | |
| 4166 | if toggle_apm_after_setting: |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 4167 | toggle_airplane_mode(log, ad, new_state=True, strict_checking=False) |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 4168 | time.sleep(WAIT_TIME_ANDROID_STATE_SETTLING) |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 4169 | toggle_airplane_mode(log, ad, new_state=None, strict_checking=False) |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 4170 | |
| 4171 | result = wait_for_network_rat_for_subscription( |
| 4172 | log, ad, sub_id, rat_family, max_wait_time, voice_or_data) |
| 4173 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 4174 | log.info( |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 4175 | "End of ensure_network_rat_for_subscription for %s. " |
| 4176 | "Setting to %s, Expecting %s %s. Current: voice: %s(family: %s), " |
| 4177 | "data: %s(family: %s)", ad.serial, network_preference, rat_family, |
| 4178 | voice_or_data, |
| 4179 | ad.droid.telephonyGetCurrentVoiceNetworkTypeForSubscription(sub_id), |
| 4180 | rat_family_from_rat( |
| 4181 | ad.droid.telephonyGetCurrentVoiceNetworkTypeForSubscription( |
| 4182 | sub_id)), |
| 4183 | ad.droid.telephonyGetCurrentDataNetworkTypeForSubscription(sub_id), |
| 4184 | rat_family_from_rat( |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 4185 | ad.droid.telephonyGetCurrentDataNetworkTypeForSubscription( |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 4186 | sub_id))) |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 4187 | return result |
| 4188 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 4189 | |
| 4190 | def ensure_network_preference(log, |
| 4191 | ad, |
| 4192 | network_preference, |
| 4193 | voice_or_data=None, |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 4194 | max_wait_time=MAX_WAIT_TIME_NW_SELECTION, |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 4195 | toggle_apm_after_setting=False): |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 4196 | """Ensure that current rat is within the device's preferred network rats. |
| 4197 | """ |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 4198 | return ensure_network_preference_for_subscription( |
Betty Zhou | ee31105 | 2017-12-19 13:09:56 -0800 | [diff] [blame] | 4199 | log, ad, ad.droid.subscriptionGetDefaultSubId(), network_preference, |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 4200 | voice_or_data, max_wait_time, toggle_apm_after_setting) |
| 4201 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 4202 | |
| 4203 | def ensure_network_preference_for_subscription( |
| 4204 | log, |
| 4205 | ad, |
| 4206 | sub_id, |
| 4207 | network_preference, |
| 4208 | voice_or_data=None, |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 4209 | max_wait_time=MAX_WAIT_TIME_NW_SELECTION, |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 4210 | toggle_apm_after_setting=False): |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 4211 | """Ensure ad's network preference is <network_preference> for sub_id. |
| 4212 | """ |
| 4213 | rat_family_list = rat_families_for_network_preference(network_preference) |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 4214 | if not ad.droid.telephonySetPreferredNetworkTypesForSubscription( |
| 4215 | network_preference, sub_id): |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 4216 | log.error("Set Preferred Networks failed.") |
| 4217 | return False |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 4218 | if is_droid_in_rat_family_list_for_subscription( |
| 4219 | log, ad, sub_id, rat_family_list, voice_or_data): |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 4220 | return True |
| 4221 | |
| 4222 | if toggle_apm_after_setting: |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 4223 | toggle_airplane_mode(log, ad, new_state=True, strict_checking=False) |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 4224 | time.sleep(WAIT_TIME_ANDROID_STATE_SETTLING) |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 4225 | toggle_airplane_mode(log, ad, new_state=False, strict_checking=False) |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 4226 | |
| 4227 | result = wait_for_preferred_network_for_subscription( |
| 4228 | log, ad, sub_id, network_preference, max_wait_time, voice_or_data) |
| 4229 | |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 4230 | ad.log.info( |
| 4231 | "End of ensure_network_preference_for_subscription. " |
| 4232 | "Setting to %s, Expecting %s %s. Current: voice: %s(family: %s), " |
| 4233 | "data: %s(family: %s)", network_preference, rat_family_list, |
| 4234 | voice_or_data, |
| 4235 | ad.droid.telephonyGetCurrentVoiceNetworkTypeForSubscription(sub_id), |
| 4236 | rat_family_from_rat( |
| 4237 | ad.droid.telephonyGetCurrentVoiceNetworkTypeForSubscription( |
| 4238 | sub_id)), |
| 4239 | ad.droid.telephonyGetCurrentDataNetworkTypeForSubscription(sub_id), |
| 4240 | rat_family_from_rat( |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 4241 | ad.droid.telephonyGetCurrentDataNetworkTypeForSubscription( |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 4242 | sub_id))) |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 4243 | return result |
| 4244 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 4245 | |
| 4246 | def ensure_network_generation(log, |
| 4247 | ad, |
| 4248 | generation, |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 4249 | max_wait_time=MAX_WAIT_TIME_NW_SELECTION, |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 4250 | voice_or_data=None, |
| 4251 | toggle_apm_after_setting=False): |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 4252 | """Ensure ad's network is <network generation> for default subscription ID. |
| 4253 | |
| 4254 | Set preferred network generation to <generation>. |
| 4255 | Toggle ON/OFF airplane mode if necessary. |
| 4256 | Wait for ad in expected network type. |
| 4257 | """ |
| 4258 | return ensure_network_generation_for_subscription( |
Betty Zhou | ee31105 | 2017-12-19 13:09:56 -0800 | [diff] [blame] | 4259 | log, ad, ad.droid.subscriptionGetDefaultSubId(), generation, |
| 4260 | max_wait_time, voice_or_data, toggle_apm_after_setting) |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 4261 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 4262 | |
| 4263 | def ensure_network_generation_for_subscription( |
| 4264 | log, |
| 4265 | ad, |
| 4266 | sub_id, |
| 4267 | generation, |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 4268 | max_wait_time=MAX_WAIT_TIME_NW_SELECTION, |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 4269 | voice_or_data=None, |
| 4270 | toggle_apm_after_setting=False): |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 4271 | """Ensure ad's network is <network generation> for specified subscription ID. |
| 4272 | |
| 4273 | Set preferred network generation to <generation>. |
| 4274 | Toggle ON/OFF airplane mode if necessary. |
| 4275 | Wait for ad in expected network type. |
| 4276 | """ |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 4277 | ad.log.info( |
| 4278 | "RAT network type voice: %s, data: %s", |
| 4279 | ad.droid.telephonyGetCurrentVoiceNetworkTypeForSubscription(sub_id), |
| 4280 | ad.droid.telephonyGetCurrentDataNetworkTypeForSubscription(sub_id)) |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 4281 | |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 4282 | try: |
Betty Zhou | d2da7ba | 2017-03-24 12:54:34 -0700 | [diff] [blame] | 4283 | ad.log.info("Finding the network preference for generation %s for " |
| 4284 | "operator %s phone type %s", generation, |
Betty Zhou | 3b2de07 | 2018-03-15 16:46:26 -0700 | [diff] [blame] | 4285 | ad.telephony["subscription"][sub_id]["operator"], |
| 4286 | ad.telephony["subscription"][sub_id]["phone_type"]) |
Jaineel | c52d685 | 2017-10-27 15:03:54 -0700 | [diff] [blame] | 4287 | network_preference = network_preference_for_generation( |
Betty Zhou | 3b2de07 | 2018-03-15 16:46:26 -0700 | [diff] [blame] | 4288 | generation, ad.telephony["subscription"][sub_id]["operator"], |
| 4289 | ad.telephony["subscription"][sub_id]["phone_type"]) |
Betty Zhou | d2da7ba | 2017-03-24 12:54:34 -0700 | [diff] [blame] | 4290 | ad.log.info("Network preference for %s is %s", generation, |
| 4291 | network_preference) |
| 4292 | rat_family = rat_family_for_generation( |
Betty Zhou | 3b2de07 | 2018-03-15 16:46:26 -0700 | [diff] [blame] | 4293 | generation, ad.telephony["subscription"][sub_id]["operator"], |
| 4294 | ad.telephony["subscription"][sub_id]["phone_type"]) |
Betty Zhou | d2da7ba | 2017-03-24 12:54:34 -0700 | [diff] [blame] | 4295 | except KeyError as e: |
| 4296 | ad.log.error("Failed to find a rat_family entry for generation %s" |
| 4297 | " for subscriber %s with error %s", generation, |
Betty Zhou | 3b2de07 | 2018-03-15 16:46:26 -0700 | [diff] [blame] | 4298 | ad.telephony["subscription"][sub_id], e) |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 4299 | return False |
| 4300 | |
Betty Zhou | 1eedf72 | 2018-04-27 14:27:04 -0700 | [diff] [blame] | 4301 | if not set_preferred_network_mode_pref(log, ad, sub_id, |
| 4302 | network_preference): |
| 4303 | return False |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 4304 | |
Nathan Harold | 4a144a4 | 2016-09-19 14:16:24 -0700 | [diff] [blame] | 4305 | if is_droid_in_network_generation_for_subscription( |
| 4306 | log, ad, sub_id, generation, voice_or_data): |
| 4307 | return True |
| 4308 | |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 4309 | if toggle_apm_after_setting: |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 4310 | toggle_airplane_mode(log, ad, new_state=True, strict_checking=False) |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 4311 | time.sleep(WAIT_TIME_ANDROID_STATE_SETTLING) |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 4312 | toggle_airplane_mode(log, ad, new_state=False, strict_checking=False) |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 4313 | |
| 4314 | result = wait_for_network_generation_for_subscription( |
| 4315 | log, ad, sub_id, generation, max_wait_time, voice_or_data) |
| 4316 | |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 4317 | ad.log.info( |
Betty Zhou | 0f5efc4 | 2017-04-10 18:38:14 -0700 | [diff] [blame] | 4318 | "Ensure network %s %s %s. With network preference %s, " |
Betty Zhou | e955be2 | 2017-04-12 17:28:05 -0700 | [diff] [blame] | 4319 | "current: voice: %s(family: %s), data: %s(family: %s)", generation, |
| 4320 | voice_or_data, result, network_preference, |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 4321 | ad.droid.telephonyGetCurrentVoiceNetworkTypeForSubscription(sub_id), |
| 4322 | rat_generation_from_rat( |
| 4323 | ad.droid.telephonyGetCurrentVoiceNetworkTypeForSubscription( |
| 4324 | sub_id)), |
| 4325 | ad.droid.telephonyGetCurrentDataNetworkTypeForSubscription(sub_id), |
| 4326 | rat_generation_from_rat( |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 4327 | ad.droid.telephonyGetCurrentDataNetworkTypeForSubscription( |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 4328 | sub_id))) |
Betty Zhou | 351d179 | 2017-11-10 16:26:30 -0800 | [diff] [blame] | 4329 | if not result: |
Betty Zhou | fe726dc | 2018-04-25 19:31:33 -0700 | [diff] [blame] | 4330 | get_telephony_signal_strength(ad) |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 4331 | return result |
| 4332 | |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 4333 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 4334 | def wait_for_network_rat(log, |
| 4335 | ad, |
| 4336 | rat_family, |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 4337 | max_wait_time=MAX_WAIT_TIME_NW_SELECTION, |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 4338 | voice_or_data=None): |
| 4339 | return wait_for_network_rat_for_subscription( |
Betty Zhou | ee31105 | 2017-12-19 13:09:56 -0800 | [diff] [blame] | 4340 | log, ad, ad.droid.subscriptionGetDefaultSubId(), rat_family, |
| 4341 | max_wait_time, voice_or_data) |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 4342 | |
| 4343 | |
Nathan Harold | 7642fc9 | 2016-05-02 18:29:11 -0700 | [diff] [blame] | 4344 | def wait_for_network_rat_for_subscription( |
| 4345 | log, |
| 4346 | ad, |
| 4347 | sub_id, |
| 4348 | rat_family, |
| 4349 | max_wait_time=MAX_WAIT_TIME_NW_SELECTION, |
| 4350 | voice_or_data=None): |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 4351 | return _wait_for_droid_in_state_for_subscription( |
| 4352 | log, ad, sub_id, max_wait_time, |
| 4353 | is_droid_in_rat_family_for_subscription, rat_family, voice_or_data) |
| 4354 | |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 4355 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 4356 | def wait_for_not_network_rat(log, |
| 4357 | ad, |
| 4358 | rat_family, |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 4359 | max_wait_time=MAX_WAIT_TIME_NW_SELECTION, |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 4360 | voice_or_data=None): |
| 4361 | return wait_for_not_network_rat_for_subscription( |
Betty Zhou | ee31105 | 2017-12-19 13:09:56 -0800 | [diff] [blame] | 4362 | log, ad, ad.droid.subscriptionGetDefaultSubId(), rat_family, |
| 4363 | max_wait_time, voice_or_data) |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 4364 | |
| 4365 | |
| 4366 | def wait_for_not_network_rat_for_subscription( |
| 4367 | log, |
| 4368 | ad, |
| 4369 | sub_id, |
| 4370 | rat_family, |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 4371 | max_wait_time=MAX_WAIT_TIME_NW_SELECTION, |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 4372 | voice_or_data=None): |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 4373 | return _wait_for_droid_in_state_for_subscription( |
| 4374 | log, ad, sub_id, max_wait_time, |
Betty Zhou | f809c5c | 2017-03-21 14:55:59 -0700 | [diff] [blame] | 4375 | lambda log, ad, sub_id, *args, **kwargs: not is_droid_in_rat_family_for_subscription(log, ad, sub_id, rat_family, voice_or_data) |
Betty Zhou | a27e5d4 | 2017-01-17 11:33:04 -0800 | [diff] [blame] | 4376 | ) |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 4377 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 4378 | |
| 4379 | def wait_for_preferred_network(log, |
| 4380 | ad, |
| 4381 | network_preference, |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 4382 | max_wait_time=MAX_WAIT_TIME_NW_SELECTION, |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 4383 | voice_or_data=None): |
| 4384 | return wait_for_preferred_network_for_subscription( |
Betty Zhou | ee31105 | 2017-12-19 13:09:56 -0800 | [diff] [blame] | 4385 | log, ad, ad.droid.subscriptionGetDefaultSubId(), network_preference, |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 4386 | max_wait_time, voice_or_data) |
| 4387 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 4388 | |
| 4389 | def wait_for_preferred_network_for_subscription( |
| 4390 | log, |
| 4391 | ad, |
| 4392 | sub_id, |
| 4393 | network_preference, |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 4394 | max_wait_time=MAX_WAIT_TIME_NW_SELECTION, |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 4395 | voice_or_data=None): |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 4396 | rat_family_list = rat_families_for_network_preference(network_preference) |
| 4397 | return _wait_for_droid_in_state_for_subscription( |
| 4398 | log, ad, sub_id, max_wait_time, |
| 4399 | is_droid_in_rat_family_list_for_subscription, rat_family_list, |
| 4400 | voice_or_data) |
| 4401 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 4402 | |
| 4403 | def wait_for_network_generation(log, |
| 4404 | ad, |
| 4405 | generation, |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 4406 | max_wait_time=MAX_WAIT_TIME_NW_SELECTION, |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 4407 | voice_or_data=None): |
| 4408 | return wait_for_network_generation_for_subscription( |
Betty Zhou | ee31105 | 2017-12-19 13:09:56 -0800 | [diff] [blame] | 4409 | log, ad, ad.droid.subscriptionGetDefaultSubId(), generation, |
| 4410 | max_wait_time, voice_or_data) |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 4411 | |
| 4412 | |
| 4413 | def wait_for_network_generation_for_subscription( |
| 4414 | log, |
| 4415 | ad, |
| 4416 | sub_id, |
| 4417 | generation, |
Yang Liu | df164e3 | 2016-01-07 16:49:32 -0800 | [diff] [blame] | 4418 | max_wait_time=MAX_WAIT_TIME_NW_SELECTION, |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 4419 | voice_or_data=None): |
| 4420 | return _wait_for_droid_in_state_for_subscription( |
| 4421 | log, ad, sub_id, max_wait_time, |
| 4422 | is_droid_in_network_generation_for_subscription, generation, |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 4423 | voice_or_data) |
| 4424 | |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 4425 | |
| 4426 | 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] | 4427 | return is_droid_in_rat_family_for_subscription( |
Betty Zhou | ee31105 | 2017-12-19 13:09:56 -0800 | [diff] [blame] | 4428 | log, ad, ad.droid.subscriptionGetDefaultSubId(), rat_family, |
| 4429 | voice_or_data) |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 4430 | |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 4431 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 4432 | def is_droid_in_rat_family_for_subscription(log, |
| 4433 | ad, |
| 4434 | sub_id, |
| 4435 | rat_family, |
| 4436 | voice_or_data=None): |
| 4437 | return is_droid_in_rat_family_list_for_subscription( |
| 4438 | log, ad, sub_id, [rat_family], voice_or_data) |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 4439 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 4440 | |
| 4441 | def is_droid_in_rat_familiy_list(log, ad, rat_family_list, voice_or_data=None): |
| 4442 | return is_droid_in_rat_family_list_for_subscription( |
Betty Zhou | ee31105 | 2017-12-19 13:09:56 -0800 | [diff] [blame] | 4443 | log, ad, ad.droid.subscriptionGetDefaultSubId(), rat_family_list, |
| 4444 | voice_or_data) |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 4445 | |
| 4446 | |
| 4447 | def is_droid_in_rat_family_list_for_subscription(log, |
| 4448 | ad, |
| 4449 | sub_id, |
| 4450 | rat_family_list, |
| 4451 | voice_or_data=None): |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 4452 | service_list = [NETWORK_SERVICE_DATA, NETWORK_SERVICE_VOICE] |
| 4453 | if voice_or_data: |
| 4454 | service_list = [voice_or_data] |
| 4455 | |
| 4456 | for service in service_list: |
| 4457 | nw_rat = get_network_rat_for_subscription(log, ad, sub_id, service) |
| 4458 | if nw_rat == RAT_UNKNOWN or not is_valid_rat(nw_rat): |
| 4459 | continue |
Yang Liu | 7a2e7ee | 2015-12-28 15:32:44 -0800 | [diff] [blame] | 4460 | if rat_family_from_rat(nw_rat) in rat_family_list: |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 4461 | return True |
| 4462 | return False |
| 4463 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 4464 | |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 4465 | def is_droid_in_network_generation(log, ad, nw_gen, voice_or_data): |
| 4466 | """Checks if a droid in expected network generation ("2g", "3g" or "4g"). |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 4467 | |
| 4468 | Args: |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 4469 | log: log object. |
| 4470 | ad: android device. |
| 4471 | nw_gen: expected generation "4g", "3g", "2g". |
| 4472 | voice_or_data: check voice network generation or data network generation |
| 4473 | This parameter is optional. If voice_or_data is None, then if |
| 4474 | either voice or data in expected generation, function will return True. |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 4475 | |
| 4476 | Returns: |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 4477 | True if droid in expected network generation. Otherwise False. |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 4478 | """ |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 4479 | return is_droid_in_network_generation_for_subscription( |
| 4480 | log, ad, ad.droid.subscriptionGetDefaultSubId(), nw_gen, voice_or_data) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 4481 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 4482 | |
| 4483 | def is_droid_in_network_generation_for_subscription(log, ad, sub_id, nw_gen, |
| 4484 | voice_or_data): |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 4485 | """Checks if a droid in expected network generation ("2g", "3g" or "4g"). |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 4486 | |
| 4487 | Args: |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 4488 | log: log object. |
| 4489 | ad: android device. |
| 4490 | nw_gen: expected generation "4g", "3g", "2g". |
| 4491 | voice_or_data: check voice network generation or data network generation |
| 4492 | This parameter is optional. If voice_or_data is None, then if |
| 4493 | either voice or data in expected generation, function will return True. |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 4494 | |
| 4495 | Returns: |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 4496 | True if droid in expected network generation. Otherwise False. |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 4497 | """ |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 4498 | service_list = [NETWORK_SERVICE_DATA, NETWORK_SERVICE_VOICE] |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 4499 | |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 4500 | if voice_or_data: |
| 4501 | service_list = [voice_or_data] |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 4502 | |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 4503 | for service in service_list: |
| 4504 | nw_rat = get_network_rat_for_subscription(log, ad, sub_id, service) |
Betty Zhou | 89d090b | 2017-05-23 11:12:19 -0700 | [diff] [blame] | 4505 | ad.log.info("%s network rat is %s", service, nw_rat) |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 4506 | if nw_rat == RAT_UNKNOWN or not is_valid_rat(nw_rat): |
| 4507 | continue |
| 4508 | |
Yang Liu | 7a2e7ee | 2015-12-28 15:32:44 -0800 | [diff] [blame] | 4509 | if rat_generation_from_rat(nw_rat) == nw_gen: |
Betty Zhou | 89d090b | 2017-05-23 11:12:19 -0700 | [diff] [blame] | 4510 | ad.log.info("%s network rat %s is expected %s", service, nw_rat, |
| 4511 | nw_gen) |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 4512 | return True |
| 4513 | else: |
Betty Zhou | 89d090b | 2017-05-23 11:12:19 -0700 | [diff] [blame] | 4514 | ad.log.info("%s network rat %s is %s, does not meet expected %s", |
Betty Zhou | ee31105 | 2017-12-19 13:09:56 -0800 | [diff] [blame] | 4515 | service, nw_rat, rat_generation_from_rat(nw_rat), |
| 4516 | nw_gen) |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 4517 | return False |
| 4518 | |
| 4519 | return False |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 4520 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 4521 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 4522 | def get_network_rat(log, ad, voice_or_data): |
| 4523 | """Get current network type (Voice network type, or data network type) |
| 4524 | for default subscription id |
| 4525 | |
| 4526 | Args: |
| 4527 | ad: Android Device Object |
| 4528 | voice_or_data: Input parameter indicating to get voice network type or |
| 4529 | data network type. |
| 4530 | |
| 4531 | Returns: |
| 4532 | Current voice/data network type. |
| 4533 | """ |
| 4534 | return get_network_rat_for_subscription( |
| 4535 | log, ad, ad.droid.subscriptionGetDefaultSubId(), voice_or_data) |
| 4536 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 4537 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 4538 | def get_network_rat_for_subscription(log, ad, sub_id, voice_or_data): |
| 4539 | """Get current network type (Voice network type, or data network type) |
| 4540 | for specified subscription id |
| 4541 | |
| 4542 | Args: |
| 4543 | ad: Android Device Object |
| 4544 | sub_id: subscription ID |
| 4545 | voice_or_data: Input parameter indicating to get voice network type or |
| 4546 | data network type. |
| 4547 | |
| 4548 | Returns: |
| 4549 | Current voice/data network type. |
| 4550 | """ |
| 4551 | if voice_or_data == NETWORK_SERVICE_VOICE: |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 4552 | ret_val = ad.droid.telephonyGetCurrentVoiceNetworkTypeForSubscription( |
| 4553 | sub_id) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 4554 | elif voice_or_data == NETWORK_SERVICE_DATA: |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 4555 | ret_val = ad.droid.telephonyGetCurrentDataNetworkTypeForSubscription( |
| 4556 | sub_id) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 4557 | else: |
Yang Liu | aed3eef | 2015-12-15 18:40:25 -0800 | [diff] [blame] | 4558 | ret_val = ad.droid.telephonyGetNetworkTypeForSubscription(sub_id) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 4559 | |
| 4560 | if ret_val is None: |
| 4561 | log.error("get_network_rat(): Unexpected null return value") |
| 4562 | return RAT_UNKNOWN |
| 4563 | else: |
| 4564 | return ret_val |
| 4565 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 4566 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 4567 | def get_network_gen(log, ad, voice_or_data): |
| 4568 | """Get current network generation string (Voice network type, or data network type) |
| 4569 | |
| 4570 | Args: |
| 4571 | ad: Android Device Object |
| 4572 | voice_or_data: Input parameter indicating to get voice network generation |
| 4573 | or data network generation. |
| 4574 | |
| 4575 | Returns: |
| 4576 | Current voice/data network generation. |
| 4577 | """ |
| 4578 | return get_network_gen_for_subscription( |
| 4579 | log, ad, ad.droid.subscriptionGetDefaultSubId(), voice_or_data) |
| 4580 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 4581 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 4582 | def get_network_gen_for_subscription(log, ad, sub_id, voice_or_data): |
| 4583 | """Get current network generation string (Voice network type, or data network type) |
| 4584 | |
| 4585 | Args: |
| 4586 | ad: Android Device Object |
| 4587 | voice_or_data: Input parameter indicating to get voice network generation |
| 4588 | or data network generation. |
| 4589 | |
| 4590 | Returns: |
| 4591 | Current voice/data network generation. |
| 4592 | """ |
| 4593 | try: |
Nathan Harold | 4a144a4 | 2016-09-19 14:16:24 -0700 | [diff] [blame] | 4594 | return rat_generation_from_rat( |
| 4595 | get_network_rat_for_subscription(log, ad, sub_id, voice_or_data)) |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 4596 | except KeyError as e: |
| 4597 | ad.log.error("KeyError %s", e) |
Yang Liu | e23e5b1 | 2015-12-07 17:17:27 -0800 | [diff] [blame] | 4598 | return GEN_UNKNOWN |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 4599 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 4600 | |
| 4601 | def check_voice_mail_count(log, ad, voice_mail_count_before, |
| 4602 | voice_mail_count_after): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 4603 | """function to check if voice mail count is correct after leaving a new voice message. |
| 4604 | """ |
| 4605 | return get_voice_mail_count_check_function(get_operator_name(log, ad))( |
| 4606 | voice_mail_count_before, voice_mail_count_after) |
| 4607 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 4608 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 4609 | def get_voice_mail_number(log, ad): |
| 4610 | """function to get the voice mail number |
| 4611 | """ |
Betty Zhou | 9402318 | 2017-06-07 18:02:14 -0700 | [diff] [blame] | 4612 | voice_mail_number = get_voice_mail_check_number(get_operator_name(log, ad)) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 4613 | if voice_mail_number is None: |
| 4614 | return get_phone_number(log, ad) |
| 4615 | return voice_mail_number |
| 4616 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 4617 | |
Betty Zhou | f809c5c | 2017-03-21 14:55:59 -0700 | [diff] [blame] | 4618 | def ensure_phones_idle(log, ads, max_time=MAX_WAIT_TIME_CALL_DROP): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 4619 | """Ensure ads idle (not in call). |
| 4620 | """ |
Betty Zhou | f809c5c | 2017-03-21 14:55:59 -0700 | [diff] [blame] | 4621 | result = True |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 4622 | for ad in ads: |
Betty Zhou | f809c5c | 2017-03-21 14:55:59 -0700 | [diff] [blame] | 4623 | if not ensure_phone_idle(log, ad, max_time=max_time): |
| 4624 | result = False |
| 4625 | return result |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 4626 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 4627 | |
Betty Zhou | f809c5c | 2017-03-21 14:55:59 -0700 | [diff] [blame] | 4628 | def ensure_phone_idle(log, ad, max_time=MAX_WAIT_TIME_CALL_DROP): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 4629 | """Ensure ad idle (not in call). |
| 4630 | """ |
Betty Zhou | f809c5c | 2017-03-21 14:55:59 -0700 | [diff] [blame] | 4631 | if ad.droid.telecomIsInCall(): |
| 4632 | ad.droid.telecomEndCall() |
| 4633 | if not wait_for_droid_not_in_call(log, ad, max_time=max_time): |
Betty Zhou | 9402318 | 2017-06-07 18:02:14 -0700 | [diff] [blame] | 4634 | ad.log.error("Failed to end call") |
Betty Zhou | f809c5c | 2017-03-21 14:55:59 -0700 | [diff] [blame] | 4635 | return False |
| 4636 | return True |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 4637 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 4638 | |
Betty Zhou | 7f45f55 | 2017-03-15 19:12:52 -0700 | [diff] [blame] | 4639 | def ensure_phone_subscription(log, ad): |
| 4640 | """Ensure Phone Subscription. |
| 4641 | """ |
| 4642 | #check for sim and service |
Betty Zhou | 9ba6d2e | 2017-07-24 14:03:45 -0700 | [diff] [blame] | 4643 | duration = 0 |
| 4644 | while duration < MAX_WAIT_TIME_NW_SELECTION: |
| 4645 | subInfo = ad.droid.subscriptionGetAllSubInfoList() |
| 4646 | if subInfo and len(subInfo) >= 1: |
Betty Zhou | e32dd3b | 2017-11-28 19:05:55 -0800 | [diff] [blame] | 4647 | ad.log.debug("Find valid subcription %s", subInfo) |
Betty Zhou | 9ba6d2e | 2017-07-24 14:03:45 -0700 | [diff] [blame] | 4648 | break |
| 4649 | else: |
Betty Zhou | b14c101 | 2018-04-20 11:27:00 -0700 | [diff] [blame] | 4650 | ad.log.info("Did not find any subscription") |
Betty Zhou | 9ba6d2e | 2017-07-24 14:03:45 -0700 | [diff] [blame] | 4651 | time.sleep(5) |
| 4652 | duration += 5 |
| 4653 | else: |
Betty Zhou | b14c101 | 2018-04-20 11:27:00 -0700 | [diff] [blame] | 4654 | ad.log.error("Unable to find a valid subscription!") |
Betty Zhou | 7f45f55 | 2017-03-15 19:12:52 -0700 | [diff] [blame] | 4655 | return False |
Betty Zhou | b14c101 | 2018-04-20 11:27:00 -0700 | [diff] [blame] | 4656 | while duration < MAX_WAIT_TIME_NW_SELECTION: |
| 4657 | data_sub_id = ad.droid.subscriptionGetDefaultDataSubId() |
| 4658 | voice_sub_id = ad.droid.subscriptionGetDefaultVoiceSubId() |
| 4659 | if data_sub_id > INVALID_SUB_ID or voice_sub_id > INVALID_SUB_ID: |
| 4660 | ad.log.debug("Find valid voice or data sub id") |
| 4661 | break |
| 4662 | else: |
| 4663 | ad.log.info("Did not find valid data or voice sub id") |
| 4664 | time.sleep(5) |
| 4665 | duration += 5 |
| 4666 | else: |
| 4667 | ad.log.error("Unable to find valid data or voice sub id") |
Betty Zhou | 7f45f55 | 2017-03-15 19:12:52 -0700 | [diff] [blame] | 4668 | return False |
Betty Zhou | b14c101 | 2018-04-20 11:27:00 -0700 | [diff] [blame] | 4669 | while duration < MAX_WAIT_TIME_NW_SELECTION: |
| 4670 | data_sub_id = ad.droid.subscriptionGetDefaultVoiceSubId() |
| 4671 | if data_sub_id > INVALID_SUB_ID: |
| 4672 | data_rat = get_network_rat_for_subscription( |
| 4673 | log, ad, data_sub_id, NETWORK_SERVICE_DATA) |
| 4674 | else: |
| 4675 | data_rat = RAT_UNKNOWN |
| 4676 | if voice_sub_id > INVALID_SUB_ID: |
| 4677 | voice_rat = get_network_rat_for_subscription( |
| 4678 | log, ad, voice_sub_id, NETWORK_SERVICE_VOICE) |
| 4679 | else: |
| 4680 | voice_rat = RAT_UNKNOWN |
| 4681 | if data_rat != RAT_UNKNOWN or voice_rat != RAT_UNKNOWN: |
| 4682 | ad.log.info("Data sub_id %s in %s, voice sub_id %s in %s", |
| 4683 | data_sub_id, data_rat, voice_sub_id, voice_rat) |
| 4684 | return True |
| 4685 | else: |
| 4686 | ad.log.info("Did not attach for data or voice service") |
| 4687 | time.sleep(5) |
| 4688 | duration += 5 |
| 4689 | else: |
| 4690 | ad.log.error("Did not attach for voice or data service") |
Betty Zhou | 7f45f55 | 2017-03-15 19:12:52 -0700 | [diff] [blame] | 4691 | return False |
Betty Zhou | 7f45f55 | 2017-03-15 19:12:52 -0700 | [diff] [blame] | 4692 | |
| 4693 | |
Betty Zhou | 68fc0d0 | 2017-04-26 13:42:54 -0700 | [diff] [blame] | 4694 | def ensure_phone_default_state(log, ad, check_subscription=True): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 4695 | """Ensure ad in default state. |
| 4696 | Phone not in call. |
| 4697 | Phone have no stored WiFi network and WiFi disconnected. |
| 4698 | Phone not in airplane mode. |
| 4699 | """ |
| 4700 | result = True |
Betty Zhou | f25ce44 | 2017-03-03 14:28:36 -0800 | [diff] [blame] | 4701 | if not toggle_airplane_mode(log, ad, False, False): |
| 4702 | ad.log.error("Fail to turn off airplane mode") |
| 4703 | result = False |
Betty Zhou | 9402318 | 2017-06-07 18:02:14 -0700 | [diff] [blame] | 4704 | try: |
Betty Zhou | 77f2bed | 2017-10-04 18:39:07 -0700 | [diff] [blame] | 4705 | set_wifi_to_default(log, ad) |
Betty Zhou | 9402318 | 2017-06-07 18:02:14 -0700 | [diff] [blame] | 4706 | if ad.droid.telecomIsInCall(): |
| 4707 | ad.droid.telecomEndCall() |
| 4708 | if not wait_for_droid_not_in_call(log, ad): |
| 4709 | ad.log.error("Failed to end call") |
| 4710 | ad.droid.telephonyFactoryReset() |
| 4711 | ad.droid.imsFactoryReset() |
Betty Zhou | 2cf788e | 2017-06-27 17:25:53 -0700 | [diff] [blame] | 4712 | data_roaming = getattr(ad, 'roaming', False) |
| 4713 | if get_cell_data_roaming_state_by_adb(ad) != data_roaming: |
| 4714 | set_cell_data_roaming_state_by_adb(ad, data_roaming) |
Betty Zhou | f336601 | 2017-11-21 18:23:20 -0800 | [diff] [blame] | 4715 | remove_mobile_data_usage_limit(ad) |
Betty Zhou | 77f2bed | 2017-10-04 18:39:07 -0700 | [diff] [blame] | 4716 | if not wait_for_not_network_rat( |
| 4717 | log, ad, RAT_FAMILY_WLAN, voice_or_data=NETWORK_SERVICE_DATA): |
| 4718 | ad.log.error("%s still in %s", NETWORK_SERVICE_DATA, |
| 4719 | RAT_FAMILY_WLAN) |
| 4720 | result = False |
| 4721 | |
| 4722 | if check_subscription and not ensure_phone_subscription(log, ad): |
| 4723 | ad.log.error("Unable to find a valid subscription!") |
| 4724 | result = False |
Betty Zhou | 9402318 | 2017-06-07 18:02:14 -0700 | [diff] [blame] | 4725 | except Exception as e: |
| 4726 | ad.log.error("%s failure, toggle APM instead", e) |
Betty Zhou | 77f2bed | 2017-10-04 18:39:07 -0700 | [diff] [blame] | 4727 | toggle_airplane_mode_by_adb(log, ad, True) |
| 4728 | toggle_airplane_mode_by_adb(log, ad, False) |
| 4729 | ad.send_keycode("ENDCALL") |
| 4730 | ad.adb.shell("settings put global wfc_ims_enabled 0") |
| 4731 | ad.adb.shell("settings put global mobile_data 1") |
Betty Zhou | f809c5c | 2017-03-21 14:55:59 -0700 | [diff] [blame] | 4732 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 4733 | return result |
| 4734 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 4735 | |
Betty Zhou | 68fc0d0 | 2017-04-26 13:42:54 -0700 | [diff] [blame] | 4736 | def ensure_phones_default_state(log, ads, check_subscription=True): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 4737 | """Ensure ads in default state. |
| 4738 | Phone not in call. |
| 4739 | Phone have no stored WiFi network and WiFi disconnected. |
| 4740 | Phone not in airplane mode. |
Nathan Harold | eb60b19 | 2016-08-24 14:41:55 -0700 | [diff] [blame] | 4741 | |
| 4742 | Returns: |
| 4743 | True if all steps of restoring default state succeed. |
| 4744 | False if any of the steps to restore default state fails. |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 4745 | """ |
| 4746 | tasks = [] |
| 4747 | for ad in ads: |
Betty Zhou | 68fc0d0 | 2017-04-26 13:42:54 -0700 | [diff] [blame] | 4748 | tasks.append((ensure_phone_default_state, (log, ad, |
| 4749 | check_subscription))) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 4750 | if not multithread_func(log, tasks): |
| 4751 | log.error("Ensure_phones_default_state Fail.") |
| 4752 | return False |
| 4753 | return True |
| 4754 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 4755 | |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 4756 | def check_is_wifi_connected(log, ad, wifi_ssid): |
| 4757 | """Check if ad is connected to wifi wifi_ssid. |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 4758 | |
| 4759 | Args: |
| 4760 | log: Log object. |
| 4761 | ad: Android device object. |
| 4762 | wifi_ssid: WiFi network SSID. |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 4763 | |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 4764 | Returns: |
| 4765 | True if wifi is connected to wifi_ssid |
| 4766 | False if wifi is not connected to wifi_ssid |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 4767 | """ |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 4768 | wifi_info = ad.droid.wifiGetConnectionInfo() |
Betty Zhou | ee31105 | 2017-12-19 13:09:56 -0800 | [diff] [blame] | 4769 | if wifi_info["supplicant_state"] == "completed" and wifi_info["SSID"] == wifi_ssid: |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 4770 | ad.log.info("Wifi is connected to %s", wifi_ssid) |
Betty Zhou | 1b302fd | 2017-11-17 11:43:09 -0800 | [diff] [blame] | 4771 | ad.on_mobile_data = False |
Betty Zhou | ccd171d | 2017-02-13 15:11:33 -0800 | [diff] [blame] | 4772 | return True |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 4773 | else: |
| 4774 | ad.log.info("Wifi is not connected to %s", wifi_ssid) |
| 4775 | ad.log.debug("Wifi connection_info=%s", wifi_info) |
Betty Zhou | 1b302fd | 2017-11-17 11:43:09 -0800 | [diff] [blame] | 4776 | ad.on_mobile_data = True |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 4777 | return False |
| 4778 | |
| 4779 | |
| 4780 | def ensure_wifi_connected(log, ad, wifi_ssid, wifi_pwd=None, retries=3): |
| 4781 | """Ensure ad connected to wifi on network wifi_ssid. |
| 4782 | |
| 4783 | Args: |
| 4784 | log: Log object. |
| 4785 | ad: Android device object. |
| 4786 | wifi_ssid: WiFi network SSID. |
| 4787 | wifi_pwd: optional secure network password. |
| 4788 | retries: the number of retries. |
| 4789 | |
| 4790 | Returns: |
| 4791 | True if wifi is connected to wifi_ssid |
| 4792 | False if wifi is not connected to wifi_ssid |
| 4793 | """ |
Betty Zhou | f987b8f | 2017-03-09 16:34:00 -0800 | [diff] [blame] | 4794 | network = {WIFI_SSID_KEY: wifi_ssid} |
Betty Zhou | f25ce44 | 2017-03-03 14:28:36 -0800 | [diff] [blame] | 4795 | if wifi_pwd: |
Betty Zhou | f987b8f | 2017-03-09 16:34:00 -0800 | [diff] [blame] | 4796 | network[WIFI_PWD_KEY] = wifi_pwd |
Betty Zhou | f25ce44 | 2017-03-03 14:28:36 -0800 | [diff] [blame] | 4797 | for i in range(retries): |
| 4798 | if not ad.droid.wifiCheckState(): |
| 4799 | ad.log.info("Wifi state is down. Turn on Wifi") |
| 4800 | ad.droid.wifiToggleState(True) |
| 4801 | if check_is_wifi_connected(log, ad, wifi_ssid): |
| 4802 | ad.log.info("Wifi is connected to %s", wifi_ssid) |
Betty Zhou | 5dd53c0 | 2018-03-22 20:08:33 -0700 | [diff] [blame] | 4803 | return verify_internet_connection(log, ad, retries=3) |
Betty Zhou | f25ce44 | 2017-03-03 14:28:36 -0800 | [diff] [blame] | 4804 | else: |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 4805 | ad.log.info("Connecting to wifi %s", wifi_ssid) |
Betty Zhou | 9a27c6a | 2017-05-22 12:55:50 -0700 | [diff] [blame] | 4806 | try: |
| 4807 | ad.droid.wifiConnectByConfig(network) |
| 4808 | except Exception: |
Betty Zhou | 9402318 | 2017-06-07 18:02:14 -0700 | [diff] [blame] | 4809 | ad.log.info("Connecting to wifi by wifiConnect instead") |
Betty Zhou | 9a27c6a | 2017-05-22 12:55:50 -0700 | [diff] [blame] | 4810 | ad.droid.wifiConnect(network) |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 4811 | time.sleep(20) |
| 4812 | if check_is_wifi_connected(log, ad, wifi_ssid): |
Betty Zhou | 68fc0d0 | 2017-04-26 13:42:54 -0700 | [diff] [blame] | 4813 | ad.log.info("Connected to Wifi %s", wifi_ssid) |
Betty Zhou | 5dd53c0 | 2018-03-22 20:08:33 -0700 | [diff] [blame] | 4814 | return verify_internet_connection(log, ad, retries=3) |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 4815 | ad.log.info("Fail to connected to wifi %s", wifi_ssid) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 4816 | return False |
| 4817 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 4818 | |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 4819 | def forget_all_wifi_networks(log, ad): |
| 4820 | """Forget all stored wifi network information |
| 4821 | |
| 4822 | Args: |
| 4823 | log: log object |
| 4824 | ad: AndroidDevice object |
| 4825 | |
| 4826 | Returns: |
| 4827 | boolean success (True) or failure (False) |
| 4828 | """ |
| 4829 | if not ad.droid.wifiGetConfiguredNetworks(): |
Betty Zhou | 1b302fd | 2017-11-17 11:43:09 -0800 | [diff] [blame] | 4830 | ad.on_mobile_data = True |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 4831 | return True |
| 4832 | try: |
| 4833 | old_state = ad.droid.wifiCheckState() |
Betty Zhou | f987b8f | 2017-03-09 16:34:00 -0800 | [diff] [blame] | 4834 | wifi_test_utils.reset_wifi(ad) |
Betty Zhou | 7f45f55 | 2017-03-15 19:12:52 -0700 | [diff] [blame] | 4835 | wifi_toggle_state(log, ad, old_state) |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 4836 | except Exception as e: |
| 4837 | log.error("forget_all_wifi_networks with exception: %s", e) |
| 4838 | return False |
Betty Zhou | 1b302fd | 2017-11-17 11:43:09 -0800 | [diff] [blame] | 4839 | ad.on_mobile_data = True |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 4840 | return True |
| 4841 | |
| 4842 | |
Betty Zhou | f987b8f | 2017-03-09 16:34:00 -0800 | [diff] [blame] | 4843 | def wifi_reset(log, ad, disable_wifi=True): |
| 4844 | """Forget all stored wifi networks and (optionally) disable WiFi |
| 4845 | |
| 4846 | Args: |
| 4847 | log: log object |
| 4848 | ad: AndroidDevice object |
| 4849 | disable_wifi: boolean to disable wifi, defaults to True |
| 4850 | Returns: |
| 4851 | boolean success (True) or failure (False) |
| 4852 | """ |
| 4853 | if not forget_all_wifi_networks(log, ad): |
| 4854 | ad.log.error("Unable to forget all networks") |
| 4855 | return False |
| 4856 | if not wifi_toggle_state(log, ad, not disable_wifi): |
| 4857 | ad.log.error("Failed to toggle WiFi state to %s!", not disable_wifi) |
| 4858 | return False |
| 4859 | return True |
| 4860 | |
| 4861 | |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 4862 | def set_wifi_to_default(log, ad): |
| 4863 | """Set wifi to default state (Wifi disabled and no configured network) |
| 4864 | |
| 4865 | Args: |
| 4866 | log: log object |
| 4867 | ad: AndroidDevice object |
| 4868 | |
| 4869 | Returns: |
| 4870 | boolean success (True) or failure (False) |
| 4871 | """ |
Betty Zhou | f25ce44 | 2017-03-03 14:28:36 -0800 | [diff] [blame] | 4872 | ad.droid.wifiFactoryReset() |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 4873 | ad.droid.wifiToggleState(False) |
Betty Zhou | 1b302fd | 2017-11-17 11:43:09 -0800 | [diff] [blame] | 4874 | ad.on_mobile_data = True |
Betty Zhou | f987b8f | 2017-03-09 16:34:00 -0800 | [diff] [blame] | 4875 | |
| 4876 | |
| 4877 | def wifi_toggle_state(log, ad, state, retries=3): |
| 4878 | """Toggle the WiFi State |
| 4879 | |
| 4880 | Args: |
| 4881 | log: log object |
| 4882 | ad: AndroidDevice object |
| 4883 | state: True, False, or None |
| 4884 | |
| 4885 | Returns: |
| 4886 | boolean success (True) or failure (False) |
| 4887 | """ |
| 4888 | for i in range(retries): |
| 4889 | if wifi_test_utils.wifi_toggle_state(ad, state, assert_on_fail=False): |
Betty Zhou | 1b302fd | 2017-11-17 11:43:09 -0800 | [diff] [blame] | 4890 | ad.on_mobile_data = not state |
Betty Zhou | f987b8f | 2017-03-09 16:34:00 -0800 | [diff] [blame] | 4891 | return True |
| 4892 | time.sleep(WAIT_TIME_BETWEEN_STATE_CHECK) |
| 4893 | return False |
| 4894 | |
| 4895 | |
| 4896 | def start_wifi_tethering(log, ad, ssid, password, ap_band=None): |
| 4897 | """Start a Tethering Session |
| 4898 | |
| 4899 | Args: |
| 4900 | log: log object |
| 4901 | ad: AndroidDevice object |
| 4902 | ssid: the name of the WiFi network |
| 4903 | password: optional password, used for secure networks. |
| 4904 | ap_band=DEPRECATED specification of 2G or 5G tethering |
| 4905 | Returns: |
| 4906 | boolean success (True) or failure (False) |
| 4907 | """ |
| 4908 | return wifi_test_utils._assert_on_fail_handler( |
| 4909 | wifi_test_utils.start_wifi_tethering, |
| 4910 | False, |
| 4911 | ad, |
| 4912 | ssid, |
| 4913 | password, |
| 4914 | band=ap_band) |
| 4915 | |
| 4916 | |
| 4917 | def stop_wifi_tethering(log, ad): |
| 4918 | """Stop a Tethering Session |
| 4919 | |
| 4920 | Args: |
| 4921 | log: log object |
| 4922 | ad: AndroidDevice object |
| 4923 | Returns: |
| 4924 | boolean success (True) or failure (False) |
| 4925 | """ |
| 4926 | return wifi_test_utils._assert_on_fail_handler( |
| 4927 | wifi_test_utils.stop_wifi_tethering, False, ad) |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 4928 | |
| 4929 | |
Yang Liu | 98fd9d7 | 2016-03-04 12:14:49 -0800 | [diff] [blame] | 4930 | def reset_preferred_network_type_to_allowable_range(log, ad): |
| 4931 | """If preferred network type is not in allowable range, reset to GEN_4G |
| 4932 | preferred network type. |
| 4933 | |
| 4934 | Args: |
| 4935 | log: log object |
| 4936 | ad: android device object |
| 4937 | |
| 4938 | Returns: |
| 4939 | None |
| 4940 | """ |
Betty Zhou | 3b2de07 | 2018-03-15 16:46:26 -0700 | [diff] [blame] | 4941 | for sub_id, sub_info in ad.telephony["subscription"].items(): |
Yang Liu | 98fd9d7 | 2016-03-04 12:14:49 -0800 | [diff] [blame] | 4942 | current_preference = \ |
| 4943 | ad.droid.telephonyGetPreferredNetworkTypesForSubscription(sub_id) |
Betty Zhou | bb19248 | 2017-03-01 14:38:56 -0800 | [diff] [blame] | 4944 | ad.log.debug("sub_id network preference is %s", current_preference) |
Yang Liu | 4072cfa | 2016-04-06 11:25:07 -0700 | [diff] [blame] | 4945 | try: |
Nathan Harold | 7642fc9 | 2016-05-02 18:29:11 -0700 | [diff] [blame] | 4946 | if current_preference not in get_allowable_network_preference( |
Betty Zhou | d2da7ba | 2017-03-24 12:54:34 -0700 | [diff] [blame] | 4947 | sub_info["operator"], sub_info["phone_type"]): |
Jaineel | c52d685 | 2017-10-27 15:03:54 -0700 | [diff] [blame] | 4948 | network_preference = network_preference_for_generation( |
Betty Zhou | d2da7ba | 2017-03-24 12:54:34 -0700 | [diff] [blame] | 4949 | GEN_4G, sub_info["operator"], sub_info["phone_type"]) |
Yang Liu | 4072cfa | 2016-04-06 11:25:07 -0700 | [diff] [blame] | 4950 | ad.droid.telephonySetPreferredNetworkTypesForSubscription( |
| 4951 | network_preference, sub_id) |
| 4952 | except KeyError: |
| 4953 | pass |
Yang Liu | 98fd9d7 | 2016-03-04 12:14:49 -0800 | [diff] [blame] | 4954 | |
Nathan Harold | 7642fc9 | 2016-05-02 18:29:11 -0700 | [diff] [blame] | 4955 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 4956 | def task_wrapper(task): |
| 4957 | """Task wrapper for multithread_func |
| 4958 | |
| 4959 | Args: |
| 4960 | task[0]: function to be wrapped. |
| 4961 | task[1]: function args. |
| 4962 | |
| 4963 | Returns: |
| 4964 | Return value of wrapped function call. |
| 4965 | """ |
| 4966 | func = task[0] |
| 4967 | params = task[1] |
| 4968 | return func(*params) |
| 4969 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 4970 | |
Patrick Chiang | 75b8986 | 2017-10-13 17:02:29 -0700 | [diff] [blame] | 4971 | def run_multithread_func_async(log, task): |
| 4972 | """Starts a multi-threaded function asynchronously. |
| 4973 | |
| 4974 | Args: |
| 4975 | log: log object. |
| 4976 | task: a task to be executed in parallel. |
| 4977 | |
| 4978 | Returns: |
| 4979 | Future object representing the execution of the task. |
| 4980 | """ |
| 4981 | executor = concurrent.futures.ThreadPoolExecutor(max_workers=1) |
| 4982 | try: |
| 4983 | future_object = executor.submit(task_wrapper, task) |
| 4984 | except Exception as e: |
| 4985 | log.error("Exception error %s", e) |
| 4986 | raise |
| 4987 | return future_object |
| 4988 | |
| 4989 | |
Betty Zhou | ccd171d | 2017-02-13 15:11:33 -0800 | [diff] [blame] | 4990 | def run_multithread_func(log, tasks): |
| 4991 | """Run multi-thread functions and return results. |
| 4992 | |
| 4993 | Args: |
| 4994 | log: log object. |
| 4995 | tasks: a list of tasks to be executed in parallel. |
| 4996 | |
| 4997 | Returns: |
| 4998 | results for tasks. |
| 4999 | """ |
Betty Zhou | ee31105 | 2017-12-19 13:09:56 -0800 | [diff] [blame] | 5000 | MAX_NUMBER_OF_WORKERS = 10 |
Betty Zhou | ccd171d | 2017-02-13 15:11:33 -0800 | [diff] [blame] | 5001 | number_of_workers = min(MAX_NUMBER_OF_WORKERS, len(tasks)) |
| 5002 | executor = concurrent.futures.ThreadPoolExecutor( |
| 5003 | max_workers=number_of_workers) |
Betty Zhou | 8802ec6 | 2017-12-18 19:44:29 -0800 | [diff] [blame] | 5004 | if not log: log = logging |
Betty Zhou | 95ed186 | 2017-07-21 13:29:02 -0700 | [diff] [blame] | 5005 | try: |
| 5006 | results = list(executor.map(task_wrapper, tasks)) |
| 5007 | except Exception as e: |
| 5008 | log.error("Exception error %s", e) |
| 5009 | raise |
Betty Zhou | ccd171d | 2017-02-13 15:11:33 -0800 | [diff] [blame] | 5010 | executor.shutdown() |
Betty Zhou | 0111ae0 | 2017-11-29 20:37:26 -0800 | [diff] [blame] | 5011 | if log: |
| 5012 | log.info("multithread_func %s result: %s", |
| 5013 | [task[0].__name__ for task in tasks], results) |
Betty Zhou | ccd171d | 2017-02-13 15:11:33 -0800 | [diff] [blame] | 5014 | return results |
| 5015 | |
| 5016 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 5017 | def multithread_func(log, tasks): |
| 5018 | """Multi-thread function wrapper. |
| 5019 | |
| 5020 | Args: |
| 5021 | log: log object. |
| 5022 | tasks: tasks to be executed in parallel. |
| 5023 | |
| 5024 | Returns: |
| 5025 | True if all tasks return True. |
| 5026 | False if any task return False. |
| 5027 | """ |
Betty Zhou | ccd171d | 2017-02-13 15:11:33 -0800 | [diff] [blame] | 5028 | results = run_multithread_func(log, tasks) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 5029 | for r in results: |
| 5030 | if not r: |
| 5031 | return False |
| 5032 | return True |
| 5033 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 5034 | |
Betty Zhou | ccd171d | 2017-02-13 15:11:33 -0800 | [diff] [blame] | 5035 | def multithread_func_and_check_results(log, tasks, expected_results): |
| 5036 | """Multi-thread function wrapper. |
| 5037 | |
| 5038 | Args: |
| 5039 | log: log object. |
| 5040 | tasks: tasks to be executed in parallel. |
| 5041 | expected_results: check if the results from tasks match expected_results. |
| 5042 | |
| 5043 | Returns: |
| 5044 | True if expected_results are met. |
| 5045 | False if expected_results are not met. |
| 5046 | """ |
| 5047 | return_value = True |
| 5048 | results = run_multithread_func(log, tasks) |
| 5049 | log.info("multithread_func result: %s, expecting %s", results, |
| 5050 | expected_results) |
| 5051 | for task, result, expected_result in zip(tasks, results, expected_results): |
| 5052 | if result != expected_result: |
| 5053 | logging.info("Result for task %s is %s, expecting %s", task[0], |
| 5054 | result, expected_result) |
| 5055 | return_value = False |
| 5056 | return return_value |
| 5057 | |
| 5058 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 5059 | def set_phone_screen_on(log, ad, screen_on_time=MAX_SCREEN_ON_TIME): |
| 5060 | """Set phone screen on time. |
| 5061 | |
| 5062 | Args: |
| 5063 | log: Log object. |
| 5064 | ad: Android device object. |
| 5065 | screen_on_time: screen on time. |
| 5066 | This is optional, default value is MAX_SCREEN_ON_TIME. |
| 5067 | Returns: |
| 5068 | True if set successfully. |
| 5069 | """ |
| 5070 | ad.droid.setScreenTimeout(screen_on_time) |
| 5071 | return screen_on_time == ad.droid.getScreenTimeout() |
| 5072 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 5073 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 5074 | def set_phone_silent_mode(log, ad, silent_mode=True): |
| 5075 | """Set phone silent mode. |
| 5076 | |
| 5077 | Args: |
| 5078 | log: Log object. |
| 5079 | ad: Android device object. |
| 5080 | silent_mode: set phone silent or not. |
| 5081 | This is optional, default value is True (silent mode on). |
| 5082 | Returns: |
| 5083 | True if set successfully. |
| 5084 | """ |
| 5085 | ad.droid.toggleRingerSilentMode(silent_mode) |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 5086 | ad.droid.setMediaVolume(0) |
| 5087 | ad.droid.setVoiceCallVolume(0) |
| 5088 | ad.droid.setAlarmVolume(0) |
Betty Zhou | 38391dc | 2017-07-17 17:25:37 -0700 | [diff] [blame] | 5089 | out = ad.adb.shell("settings list system | grep volume") |
| 5090 | for attr in re.findall(r"(volume_.*)=\d+", out): |
| 5091 | ad.adb.shell("settings put system %s 0" % attr) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 5092 | return silent_mode == ad.droid.checkRingerSilentMode() |
| 5093 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 5094 | |
Betty Zhou | 1eedf72 | 2018-04-27 14:27:04 -0700 | [diff] [blame] | 5095 | def set_preferred_network_mode_pref(log, |
| 5096 | ad, |
| 5097 | sub_id, |
| 5098 | network_preference, |
| 5099 | timeout=WAIT_TIME_ANDROID_STATE_SETTLING): |
Jaineel | cd74820 | 2017-08-10 12:23:42 -0700 | [diff] [blame] | 5100 | """Set Preferred Network Mode for Sub_id |
| 5101 | Args: |
| 5102 | log: Log object. |
| 5103 | ad: Android device object. |
| 5104 | sub_id: Subscription ID. |
| 5105 | network_preference: Network Mode Type |
| 5106 | """ |
Betty Zhou | 1eedf72 | 2018-04-27 14:27:04 -0700 | [diff] [blame] | 5107 | begin_time = get_device_epoch_time(ad) |
| 5108 | if ad.droid.telephonyGetPreferredNetworkTypesForSubscription( |
| 5109 | sub_id) == network_preference: |
| 5110 | ad.log.info("Current ModePref for Sub %s is in %s", sub_id, |
| 5111 | network_preference) |
| 5112 | return True |
Jaineel | cd74820 | 2017-08-10 12:23:42 -0700 | [diff] [blame] | 5113 | ad.log.info("Setting ModePref to %s for Sub %s", network_preference, |
| 5114 | sub_id) |
Betty Zhou | 1eedf72 | 2018-04-27 14:27:04 -0700 | [diff] [blame] | 5115 | while timeout >= 0: |
| 5116 | if ad.droid.telephonySetPreferredNetworkTypesForSubscription( |
| 5117 | network_preference, sub_id): |
| 5118 | return True |
| 5119 | time.sleep(WAIT_TIME_BETWEEN_STATE_CHECK) |
| 5120 | timeout = timeout - WAIT_TIME_BETWEEN_STATE_CHECK |
| 5121 | error_msg = "Failed to set sub_id %s PreferredNetworkType to %s" % ( |
| 5122 | sub_id, network_preference) |
| 5123 | search_results = ad.search_logcat( |
| 5124 | "REQUEST_SET_PREFERRED_NETWORK_TYPE error", begin_time=begin_time) |
| 5125 | if search_results: |
| 5126 | log_message = search_results[-1]["log_message"] |
| 5127 | if "DEVICE_IN_USE" in log_message: |
| 5128 | error_msg = "%s due to DEVICE_IN_USE" % error_msg |
| 5129 | else: |
| 5130 | error_msg = "%s due to %s" % (error_msg, log_message) |
| 5131 | ad.log.error(error_msg) |
| 5132 | return False |
Jaineel | cd74820 | 2017-08-10 12:23:42 -0700 | [diff] [blame] | 5133 | |
| 5134 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 5135 | def set_preferred_subid_for_sms(log, ad, sub_id): |
| 5136 | """set subscription id for SMS |
| 5137 | |
| 5138 | Args: |
| 5139 | log: Log object. |
| 5140 | ad: Android device object. |
| 5141 | sub_id :Subscription ID. |
| 5142 | |
| 5143 | """ |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 5144 | ad.log.info("Setting subscription %s as preferred SMS SIM", sub_id) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 5145 | ad.droid.subscriptionSetDefaultSmsSubId(sub_id) |
| 5146 | # Wait to make sure settings take effect |
| 5147 | time.sleep(WAIT_TIME_ANDROID_STATE_SETTLING) |
| 5148 | return sub_id == ad.droid.subscriptionGetDefaultSmsSubId() |
| 5149 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 5150 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 5151 | def set_preferred_subid_for_data(log, ad, sub_id): |
| 5152 | """set subscription id for data |
| 5153 | |
| 5154 | Args: |
| 5155 | log: Log object. |
| 5156 | ad: Android device object. |
| 5157 | sub_id :Subscription ID. |
| 5158 | |
| 5159 | """ |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 5160 | ad.log.info("Setting subscription %s as preferred Data SIM", sub_id) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 5161 | ad.droid.subscriptionSetDefaultDataSubId(sub_id) |
| 5162 | time.sleep(WAIT_TIME_ANDROID_STATE_SETTLING) |
| 5163 | # Wait to make sure settings take effect |
| 5164 | # Data SIM change takes around 1 min |
| 5165 | # Check whether data has changed to selected sim |
Nathan Harold | 7642fc9 | 2016-05-02 18:29:11 -0700 | [diff] [blame] | 5166 | if not wait_for_data_connection(log, ad, True, |
| 5167 | MAX_WAIT_TIME_DATA_SUB_CHANGE): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 5168 | log.error("Data Connection failed - Not able to switch Data SIM") |
| 5169 | return False |
| 5170 | return True |
| 5171 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 5172 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 5173 | def set_preferred_subid_for_voice(log, ad, sub_id): |
| 5174 | """set subscription id for voice |
| 5175 | |
| 5176 | Args: |
| 5177 | log: Log object. |
| 5178 | ad: Android device object. |
| 5179 | sub_id :Subscription ID. |
| 5180 | |
| 5181 | """ |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 5182 | ad.log.info("Setting subscription %s as Voice SIM", sub_id) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 5183 | ad.droid.subscriptionSetDefaultVoiceSubId(sub_id) |
| 5184 | ad.droid.telecomSetUserSelectedOutgoingPhoneAccountBySubId(sub_id) |
| 5185 | # Wait to make sure settings take effect |
| 5186 | time.sleep(WAIT_TIME_ANDROID_STATE_SETTLING) |
| 5187 | return True |
| 5188 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 5189 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 5190 | def set_call_state_listen_level(log, ad, value, sub_id): |
| 5191 | """Set call state listen level for subscription id. |
| 5192 | |
| 5193 | Args: |
| 5194 | log: Log object. |
| 5195 | ad: Android device object. |
| 5196 | value: True or False |
| 5197 | sub_id :Subscription ID. |
| 5198 | |
| 5199 | Returns: |
| 5200 | True or False |
| 5201 | """ |
| 5202 | if sub_id == INVALID_SUB_ID: |
| 5203 | log.error("Invalid Subscription ID") |
| 5204 | return False |
Yang Liu | aed3eef | 2015-12-15 18:40:25 -0800 | [diff] [blame] | 5205 | ad.droid.telephonyAdjustPreciseCallStateListenLevelForSubscription( |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 5206 | "Foreground", value, sub_id) |
Yang Liu | aed3eef | 2015-12-15 18:40:25 -0800 | [diff] [blame] | 5207 | ad.droid.telephonyAdjustPreciseCallStateListenLevelForSubscription( |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 5208 | "Ringing", value, sub_id) |
Yang Liu | aed3eef | 2015-12-15 18:40:25 -0800 | [diff] [blame] | 5209 | ad.droid.telephonyAdjustPreciseCallStateListenLevelForSubscription( |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 5210 | "Background", value, sub_id) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 5211 | return True |
| 5212 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 5213 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 5214 | def setup_sim(log, ad, sub_id, voice=False, sms=False, data=False): |
| 5215 | """set subscription id for voice, sms and data |
| 5216 | |
| 5217 | Args: |
| 5218 | log: Log object. |
| 5219 | ad: Android device object. |
| 5220 | sub_id :Subscription ID. |
| 5221 | voice: True if to set subscription as default voice subscription |
| 5222 | sms: True if to set subscription as default sms subscription |
| 5223 | data: True if to set subscription as default data subscription |
| 5224 | |
| 5225 | """ |
| 5226 | if sub_id == INVALID_SUB_ID: |
| 5227 | log.error("Invalid Subscription ID") |
| 5228 | return False |
| 5229 | else: |
| 5230 | if voice: |
| 5231 | if not set_preferred_subid_for_voice(log, ad, sub_id): |
| 5232 | return False |
| 5233 | if sms: |
| 5234 | if not set_preferred_subid_for_sms(log, ad, sub_id): |
| 5235 | return False |
| 5236 | if data: |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 5237 | if not set_preferred_subid_for_data(log, ad, sub_id): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 5238 | return False |
| 5239 | return True |
| 5240 | |
Nathan Harold | 7642fc9 | 2016-05-02 18:29:11 -0700 | [diff] [blame] | 5241 | |
Yang Liu | 8e6adff | 2016-02-05 10:24:04 -0800 | [diff] [blame] | 5242 | def is_event_match(event, field, value): |
| 5243 | """Return if <field> in "event" match <value> or not. |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 5244 | |
| 5245 | Args: |
Yang Liu | 8e6adff | 2016-02-05 10:24:04 -0800 | [diff] [blame] | 5246 | event: event to test. This event need to have <field>. |
| 5247 | field: field to match. |
| 5248 | value: value to match. |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 5249 | |
| 5250 | Returns: |
Yang Liu | 8e6adff | 2016-02-05 10:24:04 -0800 | [diff] [blame] | 5251 | True if <field> in "event" match <value>. |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 5252 | False otherwise. |
| 5253 | """ |
Yang Liu | 8e6adff | 2016-02-05 10:24:04 -0800 | [diff] [blame] | 5254 | return is_event_match_for_list(event, field, [value]) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 5255 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 5256 | |
Yang Liu | 8e6adff | 2016-02-05 10:24:04 -0800 | [diff] [blame] | 5257 | def is_event_match_for_list(event, field, value_list): |
| 5258 | """Return if <field> in "event" match any one of the value |
| 5259 | in "value_list" or not. |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 5260 | |
| 5261 | Args: |
Yang Liu | 8e6adff | 2016-02-05 10:24:04 -0800 | [diff] [blame] | 5262 | event: event to test. This event need to have <field>. |
| 5263 | field: field to match. |
| 5264 | value_list: a list of value to match. |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 5265 | |
| 5266 | Returns: |
Yang Liu | 8e6adff | 2016-02-05 10:24:04 -0800 | [diff] [blame] | 5267 | 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] | 5268 | False otherwise. |
| 5269 | """ |
| 5270 | try: |
Yang Liu | 8e6adff | 2016-02-05 10:24:04 -0800 | [diff] [blame] | 5271 | value_in_event = event['data'][field] |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 5272 | except KeyError: |
| 5273 | return False |
Yang Liu | 8e6adff | 2016-02-05 10:24:04 -0800 | [diff] [blame] | 5274 | for value in value_list: |
| 5275 | if value_in_event == value: |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 5276 | return True |
| 5277 | return False |
| 5278 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 5279 | |
Yang Liu | 8e6adff | 2016-02-05 10:24:04 -0800 | [diff] [blame] | 5280 | def is_network_call_back_event_match(event, network_callback_id, |
| 5281 | network_callback_event): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 5282 | try: |
Nathan Harold | 4a144a4 | 2016-09-19 14:16:24 -0700 | [diff] [blame] | 5283 | return ( |
| 5284 | (network_callback_id == event['data'][NetworkCallbackContainer.ID]) |
Betty Zhou | ee31105 | 2017-12-19 13:09:56 -0800 | [diff] [blame] | 5285 | and (network_callback_event == event['data'] |
| 5286 | [NetworkCallbackContainer.NETWORK_CALLBACK_EVENT])) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 5287 | except KeyError: |
| 5288 | return False |
| 5289 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 5290 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 5291 | def is_build_id(log, ad, build_id): |
| 5292 | """Return if ad's build id is the same as input parameter build_id. |
| 5293 | |
| 5294 | Args: |
| 5295 | log: log object. |
| 5296 | ad: android device object. |
| 5297 | build_id: android build id. |
| 5298 | |
| 5299 | Returns: |
| 5300 | True if ad's build id is the same as input parameter build_id. |
| 5301 | False otherwise. |
| 5302 | """ |
| 5303 | actual_bid = ad.droid.getBuildID() |
| 5304 | |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 5305 | ad.log.info("BUILD DISPLAY: %s", ad.droid.getBuildDisplay()) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 5306 | #In case we want to log more stuff/more granularity... |
| 5307 | #log.info("{} BUILD ID:{} ".format(ad.serial, ad.droid.getBuildID())) |
| 5308 | #log.info("{} BUILD FINGERPRINT: {} " |
| 5309 | # .format(ad.serial), ad.droid.getBuildFingerprint()) |
| 5310 | #log.info("{} BUILD TYPE: {} " |
| 5311 | # .format(ad.serial), ad.droid.getBuildType()) |
| 5312 | #log.info("{} BUILD NUMBER: {} " |
| 5313 | # .format(ad.serial), ad.droid.getBuildNumber()) |
| 5314 | if actual_bid.upper() != build_id.upper(): |
Betty Zhou | a37acd3 | 2017-02-23 20:04:24 -0800 | [diff] [blame] | 5315 | ad.log.error("%s: Incorrect Build ID", ad.model) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 5316 | return False |
| 5317 | return True |
| 5318 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 5319 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 5320 | def is_uri_equivalent(uri1, uri2): |
| 5321 | """Check whether two input uris match or not. |
| 5322 | |
| 5323 | Compare Uris. |
| 5324 | If Uris are tel URI, it will only take the digit part |
| 5325 | and compare as phone number. |
| 5326 | Else, it will just do string compare. |
| 5327 | |
| 5328 | Args: |
| 5329 | uri1: 1st uri to be compared. |
| 5330 | uri2: 2nd uri to be compared. |
| 5331 | |
| 5332 | Returns: |
| 5333 | True if two uris match. Otherwise False. |
| 5334 | """ |
Nathan Harold | 23683d2 | 2015-12-14 16:19:08 -0800 | [diff] [blame] | 5335 | |
| 5336 | #If either is None/empty we return false |
| 5337 | if not uri1 or not uri2: |
| 5338 | return False |
| 5339 | |
| 5340 | try: |
| 5341 | if uri1.startswith('tel:') and uri2.startswith('tel:'): |
Yang Liu | 598b93d | 2016-03-22 17:07:59 -0700 | [diff] [blame] | 5342 | uri1_number = get_number_from_tel_uri(uri1) |
| 5343 | uri2_number = get_number_from_tel_uri(uri2) |
Nathan Harold | 23683d2 | 2015-12-14 16:19:08 -0800 | [diff] [blame] | 5344 | return check_phone_number_match(uri1_number, uri2_number) |
| 5345 | else: |
| 5346 | return uri1 == uri2 |
| 5347 | except AttributeError as e: |
| 5348 | return False |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 5349 | |
Nathan Harold | 123c9da | 2015-12-30 16:33:25 -0800 | [diff] [blame] | 5350 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 5351 | def get_call_uri(ad, call_id): |
| 5352 | """Get call's uri field. |
| 5353 | |
| 5354 | Get Uri for call_id in ad. |
| 5355 | |
| 5356 | Args: |
| 5357 | ad: android device object. |
| 5358 | call_id: the call id to get Uri from. |
| 5359 | |
| 5360 | Returns: |
| 5361 | call's Uri if call is active and have uri field. None otherwise. |
| 5362 | """ |
| 5363 | try: |
| 5364 | call_detail = ad.droid.telecomCallGetDetails(call_id) |
| 5365 | return call_detail["Handle"]["Uri"] |
| 5366 | except: |
| 5367 | return None |
| 5368 | |
Nathan Harold | 7642fc9 | 2016-05-02 18:29:11 -0700 | [diff] [blame] | 5369 | |
Yang Liu | 598b93d | 2016-03-22 17:07:59 -0700 | [diff] [blame] | 5370 | def get_number_from_tel_uri(uri): |
| 5371 | """Get Uri number from tel uri |
| 5372 | |
| 5373 | Args: |
| 5374 | uri: input uri |
| 5375 | |
| 5376 | Returns: |
| 5377 | If input uri is tel uri, return the number part. |
| 5378 | else return None. |
| 5379 | """ |
| 5380 | if uri.startswith('tel:'): |
Nathan Harold | 4a144a4 | 2016-09-19 14:16:24 -0700 | [diff] [blame] | 5381 | uri_number = ''.join( |
| 5382 | i for i in urllib.parse.unquote(uri) if i.isdigit()) |
Yang Liu | 598b93d | 2016-03-22 17:07:59 -0700 | [diff] [blame] | 5383 | return uri_number |
| 5384 | else: |
| 5385 | return None |
Betty Zhou | 5cf94d8 | 2017-06-09 19:18:09 -0700 | [diff] [blame] | 5386 | |
| 5387 | |
Betty Zhou | 9a96fc3 | 2018-02-01 16:44:05 -0800 | [diff] [blame] | 5388 | def find_qxdm_log_mask(ad, mask="default.cfg"): |
Betty Zhou | 550fd37 | 2017-10-16 17:22:47 -0700 | [diff] [blame] | 5389 | """Find QXDM logger mask.""" |
| 5390 | if "/" not in mask: |
Betty Zhou | a18d098 | 2017-10-18 14:30:43 -0700 | [diff] [blame] | 5391 | # Call nexuslogger to generate log mask |
| 5392 | start_nexuslogger(ad) |
Betty Zhou | 550fd37 | 2017-10-16 17:22:47 -0700 | [diff] [blame] | 5393 | # Find the log mask path |
Betty Zhou | 9a96fc3 | 2018-02-01 16:44:05 -0800 | [diff] [blame] | 5394 | for path in (DEFAULT_QXDM_LOG_PATH, "/data/diag_logs", |
| 5395 | "/vendor/etc/mdlog/"): |
Betty Zhou | 02571f5 | 2018-02-16 14:11:16 -0800 | [diff] [blame] | 5396 | out = ad.adb.shell( |
| 5397 | "find %s -type f -iname %s" % (path, mask), ignore_status=True) |
Betty Zhou | 9a96fc3 | 2018-02-01 16:44:05 -0800 | [diff] [blame] | 5398 | if out and "No such" not in out and "Permission denied" not in out: |
| 5399 | if path.startswith("/vendor/"): |
Betty Zhou | 739d6b7 | 2018-04-18 10:38:53 -0700 | [diff] [blame] | 5400 | setattr(ad, "qxdm_log_path", DEFAULT_QXDM_LOG_PATH) |
Betty Zhou | 9a96fc3 | 2018-02-01 16:44:05 -0800 | [diff] [blame] | 5401 | else: |
Betty Zhou | 739d6b7 | 2018-04-18 10:38:53 -0700 | [diff] [blame] | 5402 | setattr(ad, "qxdm_log_path", path) |
Betty Zhou | 9a96fc3 | 2018-02-01 16:44:05 -0800 | [diff] [blame] | 5403 | return out.split("\n")[0] |
| 5404 | if mask in ad.adb.shell("ls /vendor/etc/mdlog/"): |
Betty Zhou | 739d6b7 | 2018-04-18 10:38:53 -0700 | [diff] [blame] | 5405 | setattr(ad, "qxdm_log_path", DEFAULT_QXDM_LOG_PATH) |
Betty Zhou | 9a96fc3 | 2018-02-01 16:44:05 -0800 | [diff] [blame] | 5406 | return "%s/%s" % ("/vendor/etc/mdlog/", mask) |
Betty Zhou | 550fd37 | 2017-10-16 17:22:47 -0700 | [diff] [blame] | 5407 | else: |
| 5408 | out = ad.adb.shell("ls %s" % mask, ignore_status=True) |
| 5409 | if out and "No such" not in out: |
Betty Zhou | 739d6b7 | 2018-04-18 10:38:53 -0700 | [diff] [blame] | 5410 | qxdm_log_path, cfg_name = os.path.split(mask) |
| 5411 | setattr(ad, "qxdm_log_path", qxdm_log_path) |
Betty Zhou | a18d098 | 2017-10-18 14:30:43 -0700 | [diff] [blame] | 5412 | return mask |
Betty Zhou | 550fd37 | 2017-10-16 17:22:47 -0700 | [diff] [blame] | 5413 | ad.log.warning("Could NOT find QXDM logger mask path for %s", mask) |
| 5414 | |
| 5415 | |
| 5416 | def set_qxdm_logger_command(ad, mask=None): |
| 5417 | """Set QXDM logger always on. |
| 5418 | |
| 5419 | Args: |
| 5420 | ad: android device object. |
| 5421 | |
| 5422 | """ |
| 5423 | ## Neet to check if log mask will be generated without starting nexus logger |
Betty Zhou | 9a96fc3 | 2018-02-01 16:44:05 -0800 | [diff] [blame] | 5424 | masks = [] |
| 5425 | mask_path = None |
Betty Zhou | 550fd37 | 2017-10-16 17:22:47 -0700 | [diff] [blame] | 5426 | if mask: |
Betty Zhou | 9a96fc3 | 2018-02-01 16:44:05 -0800 | [diff] [blame] | 5427 | masks = [mask] |
| 5428 | masks.extend(["QC_Default.cfg", "default.cfg"]) |
| 5429 | for mask in masks: |
| 5430 | mask_path = find_qxdm_log_mask(ad, mask) |
| 5431 | if mask_path: break |
Betty Zhou | 550fd37 | 2017-10-16 17:22:47 -0700 | [diff] [blame] | 5432 | if not mask_path: |
Betty Zhou | 9a96fc3 | 2018-02-01 16:44:05 -0800 | [diff] [blame] | 5433 | ad.log.error("Cannot find QXDM mask %s", mask) |
Betty Zhou | 550fd37 | 2017-10-16 17:22:47 -0700 | [diff] [blame] | 5434 | ad.qxdm_logger_command = None |
Betty Zhou | a18d098 | 2017-10-18 14:30:43 -0700 | [diff] [blame] | 5435 | return False |
Betty Zhou | 550fd37 | 2017-10-16 17:22:47 -0700 | [diff] [blame] | 5436 | else: |
Betty Zhou | a18d098 | 2017-10-18 14:30:43 -0700 | [diff] [blame] | 5437 | ad.log.info("Use QXDM log mask %s", mask_path) |
Betty Zhou | 9a96fc3 | 2018-02-01 16:44:05 -0800 | [diff] [blame] | 5438 | ad.log.debug("qxdm_log_path = %s", ad.qxdm_log_path) |
| 5439 | output_path = os.path.join(ad.qxdm_log_path, "logs") |
Betty Zhou | 31c402a | 2018-01-25 18:14:20 -0800 | [diff] [blame] | 5440 | ad.qxdm_logger_command = ("diag_mdlog -f %s -o %s -s 50 -c" % |
Betty Zhou | 9a96fc3 | 2018-02-01 16:44:05 -0800 | [diff] [blame] | 5441 | (mask_path, output_path)) |
Betty Zhou | e7cc367 | 2018-04-26 16:58:56 -0700 | [diff] [blame] | 5442 | for prop in ("persist.sys.modem.diag.mdlog", |
| 5443 | "persist.vendor.sys.modem.diag.mdlog"): |
| 5444 | if ad.adb.getprop(prop): |
| 5445 | # Enable qxdm always on if supported |
| 5446 | for conf_path in ("/data/vendor/radio/diag_logs", |
| 5447 | "/vendor/etc/mdlog"): |
| 5448 | if "diag.conf" in ad.adb.shell( |
| 5449 | "ls %s" % conf_path, ignore_status=True): |
| 5450 | conf_path = "%s/diag.conf" % conf_path |
| 5451 | ad.adb.shell('echo "%s" > %s' % |
| 5452 | (ad.qxdm_logger_command, conf_path)) |
| 5453 | break |
| 5454 | ad.adb.shell("%s true" % prop, ignore_status=True) |
| 5455 | break |
Betty Zhou | a18d098 | 2017-10-18 14:30:43 -0700 | [diff] [blame] | 5456 | return True |
Betty Zhou | 550fd37 | 2017-10-16 17:22:47 -0700 | [diff] [blame] | 5457 | |
| 5458 | |
Betty Zhou | 0111ae0 | 2017-11-29 20:37:26 -0800 | [diff] [blame] | 5459 | def stop_qxdm_logger(ad): |
| 5460 | """Stop QXDM logger.""" |
| 5461 | for cmd in ("diag_mdlog -k", "killall diag_mdlog"): |
| 5462 | output = ad.adb.shell("ps -ef | grep mdlog") or "" |
| 5463 | if "diag_mdlog" not in output: |
| 5464 | break |
| 5465 | ad.log.debug("Kill the existing qxdm process") |
| 5466 | ad.adb.shell(cmd, ignore_status=True) |
| 5467 | time.sleep(5) |
| 5468 | |
| 5469 | |
Betty Zhou | ee31105 | 2017-12-19 13:09:56 -0800 | [diff] [blame] | 5470 | def start_qxdm_logger(ad, begin_time=None): |
Betty Zhou | 550fd37 | 2017-10-16 17:22:47 -0700 | [diff] [blame] | 5471 | """Start QXDM logger.""" |
Betty Zhou | 3eceae0 | 2018-02-09 12:27:51 -0800 | [diff] [blame] | 5472 | if not getattr(ad, "qxdm_log", True): return |
Betty Zhou | ee31105 | 2017-12-19 13:09:56 -0800 | [diff] [blame] | 5473 | # Delete existing QXDM logs 5 minutes earlier than the begin_time |
Betty Zhou | a9a354a | 2018-03-21 15:01:59 -0700 | [diff] [blame] | 5474 | current_time = get_current_epoch_time() |
Betty Zhou | 9a96fc3 | 2018-02-01 16:44:05 -0800 | [diff] [blame] | 5475 | if getattr(ad, "qxdm_log_path", None): |
| 5476 | seconds = None |
Betty Zhou | a9a354a | 2018-03-21 15:01:59 -0700 | [diff] [blame] | 5477 | file_count = ad.adb.shell( |
| 5478 | "find %s -type f -iname *.qmdl | wc -l" % ad.qxdm_log_path) |
| 5479 | if int(file_count) > 50: |
| 5480 | if begin_time: |
| 5481 | # if begin_time specified, delete old qxdm logs modified |
| 5482 | # 10 minutes before begin time |
| 5483 | seconds = int((current_time - begin_time) / 1000.0) + 10 * 60 |
| 5484 | else: |
| 5485 | # if begin_time is not specified, delete old qxdm logs modified |
| 5486 | # 15 minutes before current time |
| 5487 | seconds = 15 * 60 |
Betty Zhou | 9a96fc3 | 2018-02-01 16:44:05 -0800 | [diff] [blame] | 5488 | if seconds: |
Betty Zhou | a9a354a | 2018-03-21 15:01:59 -0700 | [diff] [blame] | 5489 | # Remove qxdm logs modified more than specified seconds ago |
Betty Zhou | 9a96fc3 | 2018-02-01 16:44:05 -0800 | [diff] [blame] | 5490 | ad.adb.shell( |
Betty Zhou | 02571f5 | 2018-02-16 14:11:16 -0800 | [diff] [blame] | 5491 | "find %s -type f -iname *.qmdl -not -mtime -%ss -delete" % |
| 5492 | (ad.qxdm_log_path, seconds)) |
Betty Zhou | a9a354a | 2018-03-21 15:01:59 -0700 | [diff] [blame] | 5493 | ad.adb.shell( |
| 5494 | "find %s -type f -iname *.xml -not -mtime -%ss -delete" % |
| 5495 | (ad.qxdm_log_path, seconds)) |
Betty Zhou | 550fd37 | 2017-10-16 17:22:47 -0700 | [diff] [blame] | 5496 | if getattr(ad, "qxdm_logger_command", None): |
| 5497 | output = ad.adb.shell("ps -ef | grep mdlog") or "" |
| 5498 | if ad.qxdm_logger_command not in output: |
| 5499 | ad.log.debug("QXDM logging command %s is not running", |
| 5500 | ad.qxdm_logger_command) |
| 5501 | if "diag_mdlog" in output: |
Betty Zhou | a9a354a | 2018-03-21 15:01:59 -0700 | [diff] [blame] | 5502 | # Kill the existing non-matching diag_mdlog process |
Betty Zhou | 550fd37 | 2017-10-16 17:22:47 -0700 | [diff] [blame] | 5503 | # Only one diag_mdlog process can be run |
Betty Zhou | 0111ae0 | 2017-11-29 20:37:26 -0800 | [diff] [blame] | 5504 | stop_qxdm_logger(ad) |
Betty Zhou | 550fd37 | 2017-10-16 17:22:47 -0700 | [diff] [blame] | 5505 | ad.log.info("Start QXDM logger") |
Betty Zhou | 88a3939 | 2017-12-01 17:33:57 -0800 | [diff] [blame] | 5506 | ad.adb.shell_nb(ad.qxdm_logger_command) |
Betty Zhou | a19e444 | 2018-04-19 19:38:42 -0700 | [diff] [blame] | 5507 | time.sleep(10) |
Betty Zhou | a9a354a | 2018-03-21 15:01:59 -0700 | [diff] [blame] | 5508 | else: |
Betty Zhou | a19e444 | 2018-04-19 19:38:42 -0700 | [diff] [blame] | 5509 | run_time = check_qxdm_logger_run_time(ad) |
| 5510 | if run_time < 600: |
| 5511 | # the last diag_mdlog started within 10 minutes ago |
| 5512 | # no need to restart |
| 5513 | return True |
Betty Zhou | a9a354a | 2018-03-21 15:01:59 -0700 | [diff] [blame] | 5514 | if ad.search_logcat( |
| 5515 | "Diag_Lib: diag: In delete_log", |
Betty Zhou | a19e444 | 2018-04-19 19:38:42 -0700 | [diff] [blame] | 5516 | begin_time=current_time - |
| 5517 | run_time) or not ad.get_file_names( |
| 5518 | ad.qxdm_log_path, |
| 5519 | begin_time=current_time - 600000, |
| 5520 | match_string="*.qmdl"): |
Betty Zhou | a9a354a | 2018-03-21 15:01:59 -0700 | [diff] [blame] | 5521 | # diag_mdlog starts deleting files or no qmdl logs were |
Betty Zhou | a19e444 | 2018-04-19 19:38:42 -0700 | [diff] [blame] | 5522 | # modified in the past 10 minutes |
Betty Zhou | a9a354a | 2018-03-21 15:01:59 -0700 | [diff] [blame] | 5523 | ad.log.debug("Quit existing diag_mdlog and start a new one") |
| 5524 | stop_qxdm_logger(ad) |
| 5525 | ad.adb.shell_nb(ad.qxdm_logger_command) |
Betty Zhou | a19e444 | 2018-04-19 19:38:42 -0700 | [diff] [blame] | 5526 | time.sleep(10) |
Betty Zhou | 0111ae0 | 2017-11-29 20:37:26 -0800 | [diff] [blame] | 5527 | return True |
Betty Zhou | 550fd37 | 2017-10-16 17:22:47 -0700 | [diff] [blame] | 5528 | |
| 5529 | |
Betty Zhou | a19e444 | 2018-04-19 19:38:42 -0700 | [diff] [blame] | 5530 | def check_qxdm_logger_run_time(ad): |
| 5531 | output = ad.adb.shell("ps -eo etime,cmd | grep diag_mdlog") |
| 5532 | result = re.search(r"(\d+):(\d+):(\d+) diag_mdlog", output) |
| 5533 | if result: |
| 5534 | return int(result.group(1)) * 60 * 60 + int( |
| 5535 | result.group(2)) * 60 + int(result.group(3)) |
| 5536 | else: |
| 5537 | result = re.search(r"(\d+):(\d+) diag_mdlog", output) |
| 5538 | if result: |
| 5539 | return int(result.group(1)) * 60 + int(result.group(2)) |
| 5540 | else: |
| 5541 | return 0 |
| 5542 | |
| 5543 | |
Betty Zhou | ee31105 | 2017-12-19 13:09:56 -0800 | [diff] [blame] | 5544 | def start_qxdm_loggers(log, ads, begin_time=None): |
Betty Zhou | 02571f5 | 2018-02-16 14:11:16 -0800 | [diff] [blame] | 5545 | tasks = [(start_qxdm_logger, [ad, begin_time]) for ad in ads |
| 5546 | if getattr(ad, "qxdm_log", True)] |
Betty Zhou | 3eceae0 | 2018-02-09 12:27:51 -0800 | [diff] [blame] | 5547 | if tasks: run_multithread_func(log, tasks) |
Betty Zhou | 550fd37 | 2017-10-16 17:22:47 -0700 | [diff] [blame] | 5548 | |
| 5549 | |
Betty Zhou | 0111ae0 | 2017-11-29 20:37:26 -0800 | [diff] [blame] | 5550 | def stop_qxdm_loggers(log, ads): |
| 5551 | tasks = [(stop_qxdm_logger, [ad]) for ad in ads] |
| 5552 | run_multithread_func(log, tasks) |
| 5553 | |
| 5554 | |
Betty Zhou | 550fd37 | 2017-10-16 17:22:47 -0700 | [diff] [blame] | 5555 | def start_nexuslogger(ad): |
Betty Zhou | 807a81f | 2017-11-27 14:53:48 -0800 | [diff] [blame] | 5556 | """Start Nexus/Pixel Logger Apk.""" |
| 5557 | qxdm_logger_apk = None |
Betty Zhou | ee31105 | 2017-12-19 13:09:56 -0800 | [diff] [blame] | 5558 | for apk, activity in (("com.android.nexuslogger", ".MainActivity"), |
| 5559 | ("com.android.pixellogger", |
| 5560 | ".ui.main.MainActivity")): |
Betty Zhou | 807a81f | 2017-11-27 14:53:48 -0800 | [diff] [blame] | 5561 | if ad.is_apk_installed(apk): |
| 5562 | qxdm_logger_apk = apk |
| 5563 | break |
| 5564 | if not qxdm_logger_apk: return |
| 5565 | if ad.is_apk_running(qxdm_logger_apk): |
Betty Zhou | a18d098 | 2017-10-18 14:30:43 -0700 | [diff] [blame] | 5566 | if "granted=true" in ad.adb.shell( |
Betty Zhou | 807a81f | 2017-11-27 14:53:48 -0800 | [diff] [blame] | 5567 | "dumpsys package %s | grep WRITE_EXTERN" % qxdm_logger_apk): |
Betty Zhou | a18d098 | 2017-10-18 14:30:43 -0700 | [diff] [blame] | 5568 | return True |
| 5569 | else: |
Betty Zhou | 807a81f | 2017-11-27 14:53:48 -0800 | [diff] [blame] | 5570 | ad.log.info("Kill %s" % qxdm_logger_apk) |
| 5571 | ad.force_stop_apk(qxdm_logger_apk) |
Betty Zhou | 32e403a | 2017-10-25 20:08:12 -0700 | [diff] [blame] | 5572 | time.sleep(5) |
Betty Zhou | 807a81f | 2017-11-27 14:53:48 -0800 | [diff] [blame] | 5573 | for perm in ("READ", "WRITE"): |
| 5574 | ad.adb.shell("pm grant %s android.permission.%s_EXTERNAL_STORAGE" % |
| 5575 | (qxdm_logger_apk, perm)) |
| 5576 | time.sleep(2) |
Jaineel | 464a18e | 2017-12-21 14:37:41 -0800 | [diff] [blame] | 5577 | for i in range(3): |
| 5578 | ad.log.info("Start %s Attempt %d" % (qxdm_logger_apk, i + 1)) |
| 5579 | ad.adb.shell("am start -n %s/%s" % (qxdm_logger_apk, activity)) |
| 5580 | time.sleep(5) |
| 5581 | if ad.is_apk_running(qxdm_logger_apk): |
| 5582 | ad.send_keycode("HOME") |
| 5583 | return True |
| 5584 | return False |
Betty Zhou | 5cf94d8 | 2017-06-09 19:18:09 -0700 | [diff] [blame] | 5585 | |
| 5586 | |
Betty Zhou | 8ac04da | 2017-10-12 15:27:15 -0700 | [diff] [blame] | 5587 | def check_qxdm_logger_mask(ad, mask_file="QC_Default.cfg"): |
Betty Zhou | 5cf94d8 | 2017-06-09 19:18:09 -0700 | [diff] [blame] | 5588 | """Check if QXDM logger always on is set. |
| 5589 | |
| 5590 | Args: |
| 5591 | ad: android device object. |
| 5592 | |
| 5593 | """ |
Betty Zhou | 4b0f171 | 2017-10-12 20:20:14 -0700 | [diff] [blame] | 5594 | output = ad.adb.shell( |
| 5595 | "ls /data/vendor/radio/diag_logs/", ignore_status=True) |
| 5596 | if not output or "No such" in output: |
| 5597 | return True |
Betty Zhou | 5cf94d8 | 2017-06-09 19:18:09 -0700 | [diff] [blame] | 5598 | if mask_file not in ad.adb.shell( |
| 5599 | "cat /data/vendor/radio/diag_logs/diag.conf", ignore_status=True): |
| 5600 | return False |
| 5601 | return True |
Jaineel | 55ae6f9 | 2017-06-29 17:44:19 -0700 | [diff] [blame] | 5602 | |
| 5603 | |
Betty Zhou | 9d0366a | 2018-03-26 13:49:57 -0700 | [diff] [blame] | 5604 | def start_tcpdumps(ads, |
| 5605 | test_name="", |
| 5606 | begin_time=None, |
| 5607 | interface="any", |
Betty Zhou | 2bb1168 | 2018-04-19 17:05:30 -0700 | [diff] [blame] | 5608 | mask="all"): |
Betty Zhou | e123c67 | 2018-03-21 19:57:11 -0700 | [diff] [blame] | 5609 | for ad in ads: |
| 5610 | start_adb_tcpdump( |
Betty Zhou | 9d0366a | 2018-03-26 13:49:57 -0700 | [diff] [blame] | 5611 | ad, |
| 5612 | test_name=test_name, |
| 5613 | begin_time=begin_time, |
| 5614 | interface=interface, |
| 5615 | mask=mask) |
Betty Zhou | e123c67 | 2018-03-21 19:57:11 -0700 | [diff] [blame] | 5616 | |
| 5617 | |
Betty Zhou | 9d0366a | 2018-03-26 13:49:57 -0700 | [diff] [blame] | 5618 | def start_adb_tcpdump(ad, |
| 5619 | test_name="", |
| 5620 | begin_time=None, |
| 5621 | interface="any", |
Betty Zhou | 2bb1168 | 2018-04-19 17:05:30 -0700 | [diff] [blame] | 5622 | mask="all"): |
Jaineel | 55ae6f9 | 2017-06-29 17:44:19 -0700 | [diff] [blame] | 5623 | """Start tcpdump on any iface |
| 5624 | |
| 5625 | Args: |
| 5626 | ad: android device object. |
| 5627 | test_name: tcpdump file name will have this |
| 5628 | |
| 5629 | """ |
Jaineel | 7d0408e | 2017-12-22 15:52:59 -0800 | [diff] [blame] | 5630 | out = ad.adb.shell("ls -l /sdcard/tcpdump/") |
| 5631 | if "No such file" in out or not out: |
| 5632 | ad.adb.shell("mkdir /sdcard/tcpdump") |
| 5633 | else: |
| 5634 | ad.adb.shell("rm -rf /sdcard/tcpdump/*", ignore_status=True) |
| 5635 | |
Betty Zhou | e123c67 | 2018-03-21 19:57:11 -0700 | [diff] [blame] | 5636 | if not begin_time: |
| 5637 | begin_time = get_current_epoch_time() |
Jaineel | 7d0408e | 2017-12-22 15:52:59 -0800 | [diff] [blame] | 5638 | |
Betty Zhou | 5dd53c0 | 2018-03-22 20:08:33 -0700 | [diff] [blame] | 5639 | out = ad.adb.shell("ifconfig | grep encap") |
Betty Zhou | d8b8b43 | 2018-04-19 17:44:38 -0700 | [diff] [blame] | 5640 | if interface in ("any", "all"): |
Betty Zhou | 5dd53c0 | 2018-03-22 20:08:33 -0700 | [diff] [blame] | 5641 | intfs = [ |
| 5642 | intf for intf in ("wlan0", "rmnet_data0", "rmnet_data6") |
| 5643 | if intf in out |
| 5644 | ] |
Jaineel | c9e7bfa | 2017-08-07 14:11:30 -0700 | [diff] [blame] | 5645 | else: |
Betty Zhou | 5dd53c0 | 2018-03-22 20:08:33 -0700 | [diff] [blame] | 5646 | intfs = [interface] |
| 5647 | |
| 5648 | out = ad.adb.shell("ps -ef | grep tcpdump") |
| 5649 | cmds = [] |
| 5650 | for intf in intfs: |
| 5651 | if intf in out: |
| 5652 | ad.log.info("tcpdump on interface %s is already running", intf) |
| 5653 | continue |
| 5654 | else: |
Betty Zhou | 2bb1168 | 2018-04-19 17:05:30 -0700 | [diff] [blame] | 5655 | log_file_name = "/sdcard/tcpdump/tcpdump_%s_%s_%s_%s.pcap" % ( |
Betty Zhou | 9d0366a | 2018-03-26 13:49:57 -0700 | [diff] [blame] | 5656 | ad.serial, intf, test_name, begin_time) |
Betty Zhou | 5dd53c0 | 2018-03-22 20:08:33 -0700 | [diff] [blame] | 5657 | if mask == "ims": |
| 5658 | cmds.append( |
| 5659 | "adb -s %s shell tcpdump -i %s -s0 -n -p udp port 500 or " |
Betty Zhou | 9d0366a | 2018-03-26 13:49:57 -0700 | [diff] [blame] | 5660 | "udp port 4500 -w %s" % (ad.serial, intf, log_file_name)) |
Betty Zhou | 5dd53c0 | 2018-03-22 20:08:33 -0700 | [diff] [blame] | 5661 | else: |
Betty Zhou | 9d0366a | 2018-03-26 13:49:57 -0700 | [diff] [blame] | 5662 | cmds.append("adb -s %s shell tcpdump -i %s -s0 -w %s" % |
| 5663 | (ad.serial, intf, log_file_name)) |
Betty Zhou | 5dd53c0 | 2018-03-22 20:08:33 -0700 | [diff] [blame] | 5664 | for cmd in cmds: |
| 5665 | ad.log.info(cmd) |
| 5666 | try: |
| 5667 | start_standing_subprocess(cmd, 10) |
| 5668 | except Exception as e: |
Betty Zhou | dd9a9ea | 2018-04-04 13:23:56 -0700 | [diff] [blame] | 5669 | ad.log.error(e) |
Jaineel | 55ae6f9 | 2017-06-29 17:44:19 -0700 | [diff] [blame] | 5670 | |
| 5671 | |
Betty Zhou | e123c67 | 2018-03-21 19:57:11 -0700 | [diff] [blame] | 5672 | def stop_tcpdumps(ads): |
| 5673 | for ad in ads: |
| 5674 | stop_adb_tcpdump(ad) |
| 5675 | |
| 5676 | |
Betty Zhou | 5dd53c0 | 2018-03-22 20:08:33 -0700 | [diff] [blame] | 5677 | def stop_adb_tcpdump(ad, interface="any"): |
Jaineel | 55ae6f9 | 2017-06-29 17:44:19 -0700 | [diff] [blame] | 5678 | """Stops tcpdump on any iface |
| 5679 | Pulls the tcpdump file in the tcpdump dir |
| 5680 | |
| 5681 | Args: |
| 5682 | ad: android device object. |
Jaineel | 55ae6f9 | 2017-06-29 17:44:19 -0700 | [diff] [blame] | 5683 | |
| 5684 | """ |
Betty Zhou | 5dd53c0 | 2018-03-22 20:08:33 -0700 | [diff] [blame] | 5685 | if interface == "any": |
| 5686 | try: |
| 5687 | ad.adb.shell("killall -9 tcpdump") |
| 5688 | except Exception as e: |
Betty Zhou | a36c435 | 2018-04-05 18:49:32 -0700 | [diff] [blame] | 5689 | ad.log.error("Killing tcpdump with exception %s", e) |
Betty Zhou | 5dd53c0 | 2018-03-22 20:08:33 -0700 | [diff] [blame] | 5690 | else: |
| 5691 | out = ad.adb.shell("ps -ef | grep tcpdump | grep %s" % interface) |
| 5692 | if "tcpdump -i" in out: |
| 5693 | pids = re.findall(r"\S+\s+(\d+).*tcpdump -i", out) |
| 5694 | for pid in pids: |
| 5695 | ad.adb.shell("kill -9 %s" % pid) |
Betty Zhou | e123c67 | 2018-03-21 19:57:11 -0700 | [diff] [blame] | 5696 | |
| 5697 | |
| 5698 | def get_tcpdump_log(ad, test_name="", begin_time=None): |
| 5699 | """Stops tcpdump on any iface |
| 5700 | Pulls the tcpdump file in the tcpdump dir |
| 5701 | |
| 5702 | Args: |
| 5703 | ad: android device object. |
| 5704 | test_name: test case name |
| 5705 | begin_time: test begin time |
| 5706 | """ |
| 5707 | logs = ad.get_file_names("/sdcard/tcpdump", begin_time=begin_time) |
| 5708 | if logs: |
| 5709 | ad.log.info("Pulling tcpdumps %s", logs) |
Betty Zhou | e62b1f1 | 2018-01-24 17:05:20 -0800 | [diff] [blame] | 5710 | log_path = os.path.join(ad.log_path, test_name, |
| 5711 | "TCPDUMP_%s" % ad.serial) |
| 5712 | utils.create_dir(log_path) |
Betty Zhou | e123c67 | 2018-03-21 19:57:11 -0700 | [diff] [blame] | 5713 | ad.pull_files(logs, log_path) |
| 5714 | stop_adb_tcpdump(ad) |
Jaineel | 55ae6f9 | 2017-06-29 17:44:19 -0700 | [diff] [blame] | 5715 | return True |
Betty Zhou | 166a51c | 2017-08-14 17:40:59 -0700 | [diff] [blame] | 5716 | |
| 5717 | |
| 5718 | def fastboot_wipe(ad, skip_setup_wizard=True): |
| 5719 | """Wipe the device in fastboot mode. |
| 5720 | |
| 5721 | Pull sl4a apk from device. Terminate all sl4a sessions, |
| 5722 | Reboot the device to bootloader, wipe the device by fastboot. |
| 5723 | Reboot the device. wait for device to complete booting |
| 5724 | Re-intall and start an sl4a session. |
| 5725 | """ |
Betty Zhou | 77f2bed | 2017-10-04 18:39:07 -0700 | [diff] [blame] | 5726 | status = True |
Betty Zhou | 166a51c | 2017-08-14 17:40:59 -0700 | [diff] [blame] | 5727 | # Pull sl4a apk from device |
Betty Zhou | 9243770 | 2018-02-07 19:49:07 -0800 | [diff] [blame] | 5728 | out = ad.adb.shell("pm path %s" % SL4A_APK_NAME) |
Betty Zhou | 166a51c | 2017-08-14 17:40:59 -0700 | [diff] [blame] | 5729 | result = re.search(r"package:(.*)", out) |
| 5730 | if not result: |
| 5731 | ad.log.error("Couldn't find sl4a apk") |
| 5732 | else: |
| 5733 | sl4a_apk = result.group(1) |
| 5734 | ad.log.info("Get sl4a apk from %s", sl4a_apk) |
| 5735 | ad.pull_files([sl4a_apk], "/tmp/") |
| 5736 | ad.stop_services() |
Betty Zhou | 3e983f2 | 2018-04-09 20:37:36 -0700 | [diff] [blame] | 5737 | attemps = 3 |
| 5738 | for i in range(1, attemps + 1): |
Betty Zhou | 77f2bed | 2017-10-04 18:39:07 -0700 | [diff] [blame] | 5739 | try: |
Betty Zhou | 3e983f2 | 2018-04-09 20:37:36 -0700 | [diff] [blame] | 5740 | ad.log.info("Reboot to bootloader") |
| 5741 | ad.adb.reboot("bootloader", ignore_status=True) |
| 5742 | ad.log.info("Wipe in fastboot") |
| 5743 | ad.fastboot._w(timeout=180) |
| 5744 | time.sleep(30) |
Betty Zhou | 77f2bed | 2017-10-04 18:39:07 -0700 | [diff] [blame] | 5745 | ad.log.info("Reboot in fastboot") |
| 5746 | ad.fastboot.reboot() |
| 5747 | ad.wait_for_boot_completion() |
Betty Zhou | 3e983f2 | 2018-04-09 20:37:36 -0700 | [diff] [blame] | 5748 | ad.root_adb() |
| 5749 | if ad.skip_sl4a: |
| 5750 | break |
Betty Zhou | 02589bb | 2017-09-06 15:04:25 -0700 | [diff] [blame] | 5751 | if ad.is_sl4a_installed(): |
| 5752 | break |
Betty Zhou | 166a51c | 2017-08-14 17:40:59 -0700 | [diff] [blame] | 5753 | ad.log.info("Re-install sl4a") |
Betty Zhou | 3e983f2 | 2018-04-09 20:37:36 -0700 | [diff] [blame] | 5754 | ad.adb.install("-r /tmp/base.apk") |
Betty Zhou | 166a51c | 2017-08-14 17:40:59 -0700 | [diff] [blame] | 5755 | time.sleep(10) |
Betty Zhou | 3e983f2 | 2018-04-09 20:37:36 -0700 | [diff] [blame] | 5756 | break |
| 5757 | except Exception as e: |
| 5758 | ad.log.warning(e) |
| 5759 | if i == attemps: |
| 5760 | raise |
Betty Zhou | 3c2e254 | 2018-02-21 12:23:04 -0800 | [diff] [blame] | 5761 | try: |
| 5762 | ad.start_adb_logcat() |
| 5763 | except: |
Betty Zhou | a36c435 | 2018-04-05 18:49:32 -0700 | [diff] [blame] | 5764 | ad.log.error("Failed to start adb logcat!") |
Betty Zhou | 3c2e254 | 2018-02-21 12:23:04 -0800 | [diff] [blame] | 5765 | if skip_setup_wizard: |
| 5766 | ad.exit_setup_wizard() |
Betty Zhou | a9a354a | 2018-03-21 15:01:59 -0700 | [diff] [blame] | 5767 | start_qxdm_logger(ad) |
Betty Zhou | 739d6b7 | 2018-04-18 10:38:53 -0700 | [diff] [blame] | 5768 | # Setup VoWiFi MDN for Verizon. b/33187374 |
Betty Zhou | e7cc367 | 2018-04-26 16:58:56 -0700 | [diff] [blame] | 5769 | if "Verizon" in ad.adb.getprop( |
| 5770 | "gsm.sim.operator.alpha") and ad.is_apk_installed( |
| 5771 | "com.google.android.wfcactivation"): |
Betty Zhou | 739d6b7 | 2018-04-18 10:38:53 -0700 | [diff] [blame] | 5772 | ad.log.info("setup VoWiFi MDN per b/33187374") |
| 5773 | ad.adb.shell("setprop dbg.vzw.force_wfc_nv_enabled true") |
| 5774 | ad.adb.shell("am start --ei EXTRA_LAUNCH_CARRIER_APP 0 -n " |
| 5775 | "\"com.google.android.wfcactivation/" |
| 5776 | ".VzwEmergencyAddressActivity\"") |
Betty Zhou | f25fdab | 2018-04-23 19:20:17 -0700 | [diff] [blame] | 5777 | if ad.skip_sl4a: return status |
| 5778 | bring_up_sl4a(ad) |
Betty Zhou | 77f2bed | 2017-10-04 18:39:07 -0700 | [diff] [blame] | 5779 | return status |
Betty Zhou | 166a51c | 2017-08-14 17:40:59 -0700 | [diff] [blame] | 5780 | |
| 5781 | |
Betty Zhou | 3c2e254 | 2018-02-21 12:23:04 -0800 | [diff] [blame] | 5782 | def bring_up_sl4a(ad, attemps=3): |
| 5783 | for i in range(attemps): |
| 5784 | try: |
| 5785 | droid, ed = ad.get_droid() |
| 5786 | ed.start() |
| 5787 | ad.log.info("Broght up new sl4a session") |
Betty Zhou | 3e983f2 | 2018-04-09 20:37:36 -0700 | [diff] [blame] | 5788 | break |
Betty Zhou | 3c2e254 | 2018-02-21 12:23:04 -0800 | [diff] [blame] | 5789 | except Exception as e: |
| 5790 | if i < attemps - 1: |
| 5791 | ad.log.info(e) |
| 5792 | time.sleep(10) |
| 5793 | else: |
| 5794 | ad.log.error(e) |
| 5795 | raise |
| 5796 | |
| 5797 | |
Betty Zhou | ae9d6a8 | 2018-02-15 20:05:34 -0800 | [diff] [blame] | 5798 | def reboot_device(ad): |
| 5799 | ad.reboot() |
Betty Zhou | a9a354a | 2018-03-21 15:01:59 -0700 | [diff] [blame] | 5800 | start_qxdm_logger(ad) |
Betty Zhou | ae9d6a8 | 2018-02-15 20:05:34 -0800 | [diff] [blame] | 5801 | ad.ensure_screen_on() |
| 5802 | unlock_sim(ad) |
| 5803 | |
| 5804 | |
Betty Zhou | 166a51c | 2017-08-14 17:40:59 -0700 | [diff] [blame] | 5805 | def unlocking_device(ad, device_password=None): |
| 5806 | """First unlock device attempt, required after reboot""" |
| 5807 | ad.unlock_screen(device_password) |
| 5808 | time.sleep(2) |
| 5809 | ad.adb.wait_for_device(timeout=180) |
| 5810 | if not ad.is_waiting_for_unlock_pin(): |
| 5811 | return True |
| 5812 | else: |
| 5813 | ad.unlock_screen(device_password) |
| 5814 | time.sleep(2) |
| 5815 | ad.adb.wait_for_device(timeout=180) |
| 5816 | if ad.wait_for_window_ready(): |
| 5817 | return True |
| 5818 | ad.log.error("Unable to unlock to user window") |
| 5819 | return False |
| 5820 | |
| 5821 | |
Betty Zhou | 0fbf86f | 2017-09-21 18:09:32 -0700 | [diff] [blame] | 5822 | def refresh_sl4a_session(ad): |
| 5823 | try: |
| 5824 | ad.droid.logI("Checking SL4A connection") |
Betty Zhou | 4bc31fc | 2018-03-12 18:28:50 -0700 | [diff] [blame] | 5825 | ad.log.debug("Existing sl4a session is active") |
| 5826 | return True |
| 5827 | except Exception as e: |
Betty Zhou | fe726dc | 2018-04-25 19:31:33 -0700 | [diff] [blame] | 5828 | ad.log.warning("Existing sl4a session is NOT active: %s", e) |
Betty Zhou | 4bc31fc | 2018-03-12 18:28:50 -0700 | [diff] [blame] | 5829 | try: |
Betty Zhou | 0fbf86f | 2017-09-21 18:09:32 -0700 | [diff] [blame] | 5830 | ad.terminate_all_sessions() |
Betty Zhou | 4bc31fc | 2018-03-12 18:28:50 -0700 | [diff] [blame] | 5831 | except Exception as e: |
| 5832 | ad.log.info("terminate_all_sessions with error %s", e) |
| 5833 | ad.ensure_screen_on() |
| 5834 | ad.log.info("Open new sl4a connection") |
| 5835 | bring_up_sl4a(ad) |
Betty Zhou | 0fbf86f | 2017-09-21 18:09:32 -0700 | [diff] [blame] | 5836 | |
| 5837 | |
Betty Zhou | 166a51c | 2017-08-14 17:40:59 -0700 | [diff] [blame] | 5838 | def reset_device_password(ad, device_password=None): |
| 5839 | # Enable or Disable Device Password per test bed config |
Betty Zhou | 0fbf86f | 2017-09-21 18:09:32 -0700 | [diff] [blame] | 5840 | unlock_sim(ad) |
Betty Zhou | 166a51c | 2017-08-14 17:40:59 -0700 | [diff] [blame] | 5841 | screen_lock = ad.is_screen_lock_enabled() |
| 5842 | if device_password: |
Betty Zhou | e66efb4 | 2018-01-31 19:45:56 -0800 | [diff] [blame] | 5843 | try: |
Betty Zhou | f715c79 | 2018-02-12 17:32:10 -0800 | [diff] [blame] | 5844 | refresh_sl4a_session(ad) |
Betty Zhou | e66efb4 | 2018-01-31 19:45:56 -0800 | [diff] [blame] | 5845 | ad.droid.setDevicePassword(device_password) |
| 5846 | except Exception as e: |
| 5847 | ad.log.warning("setDevicePassword failed with %s", e) |
Betty Zhou | 3c2e254 | 2018-02-21 12:23:04 -0800 | [diff] [blame] | 5848 | try: |
| 5849 | ad.droid.setDevicePassword(device_password, "1111") |
| 5850 | except Exception as e: |
| 5851 | ad.log.warning( |
| 5852 | "setDevicePassword providing previous password error: %s", |
| 5853 | e) |
Betty Zhou | 166a51c | 2017-08-14 17:40:59 -0700 | [diff] [blame] | 5854 | time.sleep(2) |
| 5855 | if screen_lock: |
| 5856 | # existing password changed |
| 5857 | return |
| 5858 | else: |
| 5859 | # enable device password and log in for the first time |
| 5860 | ad.log.info("Enable device password") |
| 5861 | ad.adb.wait_for_device(timeout=180) |
Betty Zhou | 166a51c | 2017-08-14 17:40:59 -0700 | [diff] [blame] | 5862 | else: |
| 5863 | if not screen_lock: |
| 5864 | # no existing password, do not set password |
| 5865 | return |
| 5866 | else: |
| 5867 | # password is enabled on the device |
| 5868 | # need to disable the password and log in on the first time |
| 5869 | # with unlocking with a swipe |
| 5870 | ad.log.info("Disable device password") |
Betty Zhou | 688c103 | 2017-11-20 20:08:04 -0800 | [diff] [blame] | 5871 | ad.unlock_screen(password="1111") |
Betty Zhou | 0fbf86f | 2017-09-21 18:09:32 -0700 | [diff] [blame] | 5872 | refresh_sl4a_session(ad) |
Betty Zhou | 351d179 | 2017-11-10 16:26:30 -0800 | [diff] [blame] | 5873 | ad.ensure_screen_on() |
Betty Zhou | e66efb4 | 2018-01-31 19:45:56 -0800 | [diff] [blame] | 5874 | try: |
| 5875 | ad.droid.disableDevicePassword() |
| 5876 | except Exception as e: |
| 5877 | ad.log.warning("disableDevicePassword failed with %s", e) |
Betty Zhou | f715c79 | 2018-02-12 17:32:10 -0800 | [diff] [blame] | 5878 | fastboot_wipe(ad) |
Betty Zhou | 166a51c | 2017-08-14 17:40:59 -0700 | [diff] [blame] | 5879 | time.sleep(2) |
| 5880 | ad.adb.wait_for_device(timeout=180) |
Betty Zhou | 0fbf86f | 2017-09-21 18:09:32 -0700 | [diff] [blame] | 5881 | refresh_sl4a_session(ad) |
Betty Zhou | 166a51c | 2017-08-14 17:40:59 -0700 | [diff] [blame] | 5882 | if not ad.is_adb_logcat_on: |
| 5883 | ad.start_adb_logcat() |
Betty Zhou | 59ccab6 | 2017-08-17 18:17:29 -0700 | [diff] [blame] | 5884 | |
| 5885 | |
Betty Zhou | e66efb4 | 2018-01-31 19:45:56 -0800 | [diff] [blame] | 5886 | def get_sim_state(ad): |
Betty Zhou | 13e7adf | 2017-09-06 14:01:10 -0700 | [diff] [blame] | 5887 | try: |
Betty Zhou | e66efb4 | 2018-01-31 19:45:56 -0800 | [diff] [blame] | 5888 | state = ad.droid.telephonyGetSimState() |
Betty Zhou | 13e7adf | 2017-09-06 14:01:10 -0700 | [diff] [blame] | 5889 | except: |
Betty Zhou | e66efb4 | 2018-01-31 19:45:56 -0800 | [diff] [blame] | 5890 | state = ad.adb.getprop("gsm.sim.state") |
| 5891 | return state |
| 5892 | |
| 5893 | |
| 5894 | def is_sim_locked(ad): |
Betty Zhou | b554e34 | 2018-02-01 19:01:38 -0800 | [diff] [blame] | 5895 | return get_sim_state(ad) == SIM_STATE_PIN_REQUIRED |
Betty Zhou | 59ccab6 | 2017-08-17 18:17:29 -0700 | [diff] [blame] | 5896 | |
| 5897 | |
Betty Zhou | 28e07e1 | 2018-04-11 12:12:11 -0700 | [diff] [blame] | 5898 | def is_sim_lock_enabled(ad): |
| 5899 | # TODO: add sl4a fascade to check if sim is locked |
| 5900 | return getattr(ad, "is_sim_locked", False) |
| 5901 | |
| 5902 | |
Betty Zhou | 59ccab6 | 2017-08-17 18:17:29 -0700 | [diff] [blame] | 5903 | def unlock_sim(ad): |
| 5904 | #The puk and pin can be provided in testbed config file. |
| 5905 | #"AndroidDevice": [{"serial": "84B5T15A29018214", |
| 5906 | # "adb_logcat_param": "-b all", |
| 5907 | # "puk": "12345678", |
| 5908 | # "puk_pin": "1234"}] |
Betty Zhou | c3f1f9c | 2017-08-18 15:50:41 -0700 | [diff] [blame] | 5909 | if not is_sim_locked(ad): |
| 5910 | return True |
Betty Zhou | e66efb4 | 2018-01-31 19:45:56 -0800 | [diff] [blame] | 5911 | else: |
| 5912 | ad.is_sim_locked = True |
Betty Zhou | 59ccab6 | 2017-08-17 18:17:29 -0700 | [diff] [blame] | 5913 | puk_pin = getattr(ad, "puk_pin", "1111") |
| 5914 | try: |
| 5915 | if not hasattr(ad, 'puk'): |
Betty Zhou | b47a4ea | 2017-09-06 11:18:40 -0700 | [diff] [blame] | 5916 | ad.log.info("Enter SIM pin code") |
Betty Zhou | e66efb4 | 2018-01-31 19:45:56 -0800 | [diff] [blame] | 5917 | ad.droid.telephonySupplyPin(puk_pin) |
Betty Zhou | 59ccab6 | 2017-08-17 18:17:29 -0700 | [diff] [blame] | 5918 | else: |
| 5919 | ad.log.info("Enter PUK code and pin") |
Betty Zhou | e66efb4 | 2018-01-31 19:45:56 -0800 | [diff] [blame] | 5920 | ad.droid.telephonySupplyPuk(ad.puk, puk_pin) |
Betty Zhou | 59ccab6 | 2017-08-17 18:17:29 -0700 | [diff] [blame] | 5921 | except: |
| 5922 | # if sl4a is not available, use adb command |
| 5923 | ad.unlock_screen(puk_pin) |
| 5924 | if is_sim_locked(ad): |
| 5925 | ad.unlock_screen(puk_pin) |
Betty Zhou | b47a4ea | 2017-09-06 11:18:40 -0700 | [diff] [blame] | 5926 | time.sleep(30) |
Betty Zhou | 59ccab6 | 2017-08-17 18:17:29 -0700 | [diff] [blame] | 5927 | return not is_sim_locked(ad) |
Betty Zhou | f3c5bc3 | 2017-08-28 17:09:19 -0700 | [diff] [blame] | 5928 | |
| 5929 | |
| 5930 | def send_dialer_secret_code(ad, secret_code): |
| 5931 | """Send dialer secret code. |
| 5932 | |
| 5933 | ad: android device controller |
| 5934 | secret_code: the secret code to be sent to dialer. the string between |
| 5935 | code prefix *#*# and code postfix #*#*. *#*#<xxx>#*#* |
| 5936 | """ |
| 5937 | action = 'android.provider.Telephony.SECRET_CODE' |
| 5938 | uri = 'android_secret_code://%s' % secret_code |
| 5939 | intent = ad.droid.makeIntent( |
| 5940 | action, |
| 5941 | uri, |
| 5942 | None, # type |
| 5943 | None, # extras |
| 5944 | None, # categories, |
| 5945 | None, # packagename, |
| 5946 | None, # classname, |
| 5947 | 0x01000000) # flags |
| 5948 | ad.log.info('Issuing dialer secret dialer code: %s', secret_code) |
| 5949 | ad.droid.sendBroadcastIntent(intent) |
Betty Zhou | 5b5f80a | 2017-09-29 18:24:01 -0700 | [diff] [blame] | 5950 | |
| 5951 | |
Betty Zhou | f7da39e | 2018-04-16 16:28:58 -0700 | [diff] [blame] | 5952 | def enable_radio_log_on(ad): |
| 5953 | if ad.adb.getprop(" persist.vendor.radio.adb_log_on") != "1": |
| 5954 | adb_disable_verity(ad) |
| 5955 | ad.adb.shell("setprop persist.vendor.radio.adb_log_on 1") |
| 5956 | reboot_device(ad) |
| 5957 | |
| 5958 | |
| 5959 | def adb_disable_verity(ad): |
| 5960 | if ad.adb.getprop("ro.boot.veritymode") == "enforcing": |
| 5961 | ad.adb.disable_verity() |
| 5962 | reboot_device(ad) |
| 5963 | ad.adb.remount() |
| 5964 | |
| 5965 | |
Betty Zhou | 5b5f80a | 2017-09-29 18:24:01 -0700 | [diff] [blame] | 5966 | def system_file_push(ad, src_file_path, dst_file_path): |
| 5967 | """Push system file on a device. |
| 5968 | |
| 5969 | Push system file need to change some system setting and remount. |
| 5970 | """ |
Betty Zhou | a9a70f6 | 2017-11-16 14:53:11 -0800 | [diff] [blame] | 5971 | cmd = "%s %s" % (src_file_path, dst_file_path) |
| 5972 | out = ad.adb.push(cmd, timeout=300, ignore_status=True) |
markdr | 6607bf1 | 2018-01-02 14:45:38 -0800 | [diff] [blame] | 5973 | skip_sl4a = True if "sl4a.apk" in src_file_path else False |
Betty Zhou | a9a70f6 | 2017-11-16 14:53:11 -0800 | [diff] [blame] | 5974 | if "Read-only file system" in out: |
| 5975 | ad.log.info("Change read-only file system") |
Betty Zhou | f7da39e | 2018-04-16 16:28:58 -0700 | [diff] [blame] | 5976 | adb_disable_verity(ad) |
Betty Zhou | a9a70f6 | 2017-11-16 14:53:11 -0800 | [diff] [blame] | 5977 | out = ad.adb.push(cmd, timeout=300, ignore_status=True) |
| 5978 | if "Read-only file system" in out: |
Betty Zhou | 856ae21 | 2018-01-18 19:49:35 -0800 | [diff] [blame] | 5979 | ad.reboot(skip_sl4a) |
Betty Zhou | a9a70f6 | 2017-11-16 14:53:11 -0800 | [diff] [blame] | 5980 | out = ad.adb.push(cmd, timeout=300, ignore_status=True) |
| 5981 | if "error" in out: |
| 5982 | ad.log.error("%s failed with %s", cmd, out) |
| 5983 | return False |
| 5984 | else: |
Betty Zhou | 856ae21 | 2018-01-18 19:49:35 -0800 | [diff] [blame] | 5985 | ad.log.info("push %s succeed") |
| 5986 | if skip_sl4a: ad.reboot(skip_sl4a) |
Betty Zhou | a9a70f6 | 2017-11-16 14:53:11 -0800 | [diff] [blame] | 5987 | return True |
| 5988 | else: |
| 5989 | return True |
| 5990 | elif "error" in out: |
Betty Zhou | 5b5f80a | 2017-09-29 18:24:01 -0700 | [diff] [blame] | 5991 | return False |
Betty Zhou | a9a70f6 | 2017-11-16 14:53:11 -0800 | [diff] [blame] | 5992 | else: |
| 5993 | return True |
Betty Zhou | 8ecd1da | 2017-10-05 15:44:24 -0700 | [diff] [blame] | 5994 | |
| 5995 | |
| 5996 | def flash_radio(ad, file_path, skip_setup_wizard=True): |
| 5997 | """Flash radio image.""" |
| 5998 | ad.stop_services() |
| 5999 | ad.log.info("Reboot to bootloader") |
| 6000 | ad.adb.reboot_bootloader(ignore_status=True) |
| 6001 | ad.log.info("Flash radio in fastboot") |
| 6002 | try: |
| 6003 | ad.fastboot.flash("radio %s" % file_path, timeout=300) |
| 6004 | except Exception as e: |
| 6005 | ad.log.error(e) |
Betty Zhou | d4b75c5 | 2018-04-03 15:20:00 -0700 | [diff] [blame] | 6006 | ad.fastboot.reboot("bootloader") |
| 6007 | time.sleep(5) |
| 6008 | output = ad.fastboot.getvar("version-baseband") |
| 6009 | result = re.search(r"version-baseband: (\S+)", output) |
| 6010 | if not result: |
| 6011 | ad.log.error("fastboot getvar version-baseband output = %s", output) |
| 6012 | abort_all_tests(ad.log, "Radio version-baseband is not provided") |
| 6013 | fastboot_radio_version_output = result.group(1) |
Betty Zhou | 8ecd1da | 2017-10-05 15:44:24 -0700 | [diff] [blame] | 6014 | for _ in range(2): |
| 6015 | try: |
| 6016 | ad.log.info("Reboot in fastboot") |
| 6017 | ad.fastboot.reboot() |
| 6018 | ad.wait_for_boot_completion() |
| 6019 | break |
| 6020 | except Exception as e: |
| 6021 | ad.log.error("Exception error %s", e) |
| 6022 | ad.root_adb() |
Betty Zhou | d4b75c5 | 2018-04-03 15:20:00 -0700 | [diff] [blame] | 6023 | adb_radio_version_output = ad.adb.getprop("gsm.version.baseband") |
| 6024 | ad.log.info("adb getprop gsm.version.baseband = %s", |
| 6025 | adb_radio_version_output) |
| 6026 | if adb_radio_version_output != fastboot_radio_version_output: |
| 6027 | msg = ("fastboot radio version output %s does not match with adb" |
| 6028 | " radio version output %s" % (fastboot_radio_version_output, |
| 6029 | adb_radio_version_output)) |
| 6030 | abort_all_tests(ad.log, msg) |
Betty Zhou | 8ecd1da | 2017-10-05 15:44:24 -0700 | [diff] [blame] | 6031 | if not ad.ensure_screen_on(): |
| 6032 | ad.log.error("User window cannot come up") |
| 6033 | ad.start_services(ad.skip_sl4a, skip_setup_wizard=skip_setup_wizard) |
Betty Zhou | 447080c | 2018-02-06 10:43:26 -0800 | [diff] [blame] | 6034 | unlock_sim(ad) |
Betty Zhou | 8ecd1da | 2017-10-05 15:44:24 -0700 | [diff] [blame] | 6035 | |
| 6036 | |
Jaineel | 13ac2fc | 2017-10-26 16:25:50 -0700 | [diff] [blame] | 6037 | def set_preferred_apn_by_adb(ad, pref_apn_name): |
| 6038 | """Select Pref APN |
| 6039 | Set Preferred APN on UI using content query/insert |
| 6040 | It needs apn name as arg, and it will match with plmn id |
| 6041 | """ |
| 6042 | try: |
| 6043 | plmn_id = get_plmn_by_adb(ad) |
| 6044 | out = ad.adb.shell("content query --uri content://telephony/carriers " |
| 6045 | "--where \"apn='%s' and numeric='%s'\"" % |
| 6046 | (pref_apn_name, plmn_id)) |
| 6047 | if "No result found" in out: |
| 6048 | ad.log.warning("Cannot find APN %s on device", pref_apn_name) |
| 6049 | return False |
| 6050 | else: |
| 6051 | apn_id = re.search(r'_id=(\d+)', out).group(1) |
| 6052 | ad.log.info("APN ID is %s", apn_id) |
| 6053 | ad.adb.shell("content insert --uri content:" |
| 6054 | "//telephony/carriers/preferapn --bind apn_id:i:%s" % |
| 6055 | (apn_id)) |
| 6056 | out = ad.adb.shell("content query --uri " |
| 6057 | "content://telephony/carriers/preferapn") |
| 6058 | if "No result found" in out: |
| 6059 | ad.log.error("Failed to set prefer APN %s", pref_apn_name) |
| 6060 | return False |
| 6061 | elif apn_id == re.search(r'_id=(\d+)', out).group(1): |
| 6062 | ad.log.info("Preferred APN set to %s", pref_apn_name) |
| 6063 | return True |
| 6064 | except Exception as e: |
| 6065 | ad.log.error("Exception while setting pref apn %s", e) |
| 6066 | return True |
| 6067 | |
| 6068 | |
Jaineel | 13fb98d | 2017-10-20 12:26:50 -0700 | [diff] [blame] | 6069 | def check_apm_mode_on_by_serial(ad, serial_id): |
| 6070 | try: |
| 6071 | apm_check_cmd = "|".join(("adb -s %s shell dumpsys wifi" % serial_id, |
| 6072 | "grep -i airplanemodeon", "cut -f2 -d ' '")) |
| 6073 | output = exe_cmd(apm_check_cmd) |
| 6074 | if output.decode("utf-8").split("\n")[0] == "true": |
| 6075 | return True |
| 6076 | else: |
| 6077 | return False |
| 6078 | except Exception as e: |
| 6079 | ad.log.warning("Exception during check apm mode on %s", e) |
Jaineel | 5576d43 | 2017-10-19 15:36:42 -0700 | [diff] [blame] | 6080 | return True |
Jaineel | 5576d43 | 2017-10-19 15:36:42 -0700 | [diff] [blame] | 6081 | |
| 6082 | |
Jaineel | 13fb98d | 2017-10-20 12:26:50 -0700 | [diff] [blame] | 6083 | def set_apm_mode_on_by_serial(ad, serial_id): |
| 6084 | try: |
| 6085 | cmd1 = "adb -s %s shell settings put global airplane_mode_on 1" % serial_id |
| 6086 | cmd2 = "adb -s %s shell am broadcast -a android.intent.action.AIRPLANE_MODE" % serial_id |
| 6087 | exe_cmd(cmd1) |
| 6088 | exe_cmd(cmd2) |
| 6089 | except Exception as e: |
| 6090 | ad.log.warning("Exception during set apm mode on %s", e) |
| 6091 | return True |
Jaineel | 5576d43 | 2017-10-19 15:36:42 -0700 | [diff] [blame] | 6092 | |
| 6093 | |
Betty Zhou | 8ecd1da | 2017-10-05 15:44:24 -0700 | [diff] [blame] | 6094 | def print_radio_info(ad, extra_msg=""): |
| 6095 | for prop in ("gsm.version.baseband", "persist.radio.ver_info", |
Betty Zhou | 447080c | 2018-02-06 10:43:26 -0800 | [diff] [blame] | 6096 | "persist.radio.cnv.ver_info"): |
Betty Zhou | 8ecd1da | 2017-10-05 15:44:24 -0700 | [diff] [blame] | 6097 | output = ad.adb.getprop(prop) |
Betty Zhou | 447080c | 2018-02-06 10:43:26 -0800 | [diff] [blame] | 6098 | ad.log.info("%s%s = %s", extra_msg, prop, output) |
Betty Zhou | 32e403a | 2017-10-25 20:08:12 -0700 | [diff] [blame] | 6099 | |
| 6100 | |
| 6101 | def wait_for_state(state_check_func, |
| 6102 | state, |
Betty Zhou | ddc7640 | 2018-01-23 16:29:25 -0800 | [diff] [blame] | 6103 | max_wait_time=MAX_WAIT_TIME_FOR_STATE_CHANGE, |
markdr | 6607bf1 | 2018-01-02 14:45:38 -0800 | [diff] [blame] | 6104 | checking_interval=WAIT_TIME_BETWEEN_STATE_CHECK, |
Betty Zhou | 32e403a | 2017-10-25 20:08:12 -0700 | [diff] [blame] | 6105 | *args, |
| 6106 | **kwargs): |
| 6107 | while max_wait_time >= 0: |
| 6108 | if state_check_func(*args, **kwargs) == state: |
| 6109 | return True |
| 6110 | time.sleep(checking_interval) |
| 6111 | max_wait_time -= checking_interval |
| 6112 | return False |
Betty Zhou | d49a0ce | 2018-01-19 17:49:53 -0800 | [diff] [blame] | 6113 | |
| 6114 | |
Betty Zhou | 6dd0ac6 | 2018-04-27 18:59:12 -0700 | [diff] [blame^] | 6115 | def power_off_sim(ad, sim_slot_id=None, |
| 6116 | timeout=MAX_WAIT_TIME_FOR_STATE_CHANGE): |
Betty Zhou | d49a0ce | 2018-01-19 17:49:53 -0800 | [diff] [blame] | 6117 | try: |
| 6118 | if sim_slot_id is None: |
| 6119 | ad.droid.telephonySetSimPowerState(CARD_POWER_DOWN) |
| 6120 | verify_func = ad.droid.telephonyGetSimState |
| 6121 | verify_args = [] |
| 6122 | else: |
markdr | 6607bf1 | 2018-01-02 14:45:38 -0800 | [diff] [blame] | 6123 | ad.droid.telephonySetSimStateForSlotId(sim_slot_id, |
| 6124 | CARD_POWER_DOWN) |
Betty Zhou | d49a0ce | 2018-01-19 17:49:53 -0800 | [diff] [blame] | 6125 | verify_func = ad.droid.telephonyGetSimStateForSlotId |
| 6126 | verify_args = [sim_slot_id] |
| 6127 | except Exception as e: |
| 6128 | ad.log.error(e) |
| 6129 | return False |
Betty Zhou | 6dd0ac6 | 2018-04-27 18:59:12 -0700 | [diff] [blame^] | 6130 | while timeout > 0: |
| 6131 | sim_state = verify_func(*verify_args) |
| 6132 | if sim_state in (SIM_STATE_UNKNOWN, SIM_STATE_ABSENT): |
| 6133 | ad.log.info("SIM slot is powered off, SIM state is %s", sim_state) |
| 6134 | return True |
| 6135 | timeout = timeout - WAIT_TIME_BETWEEN_STATE_CHECK |
| 6136 | time.sleep(WAIT_TIME_BETWEEN_STATE_CHECK) |
| 6137 | ad.log.warning("Fail to power off SIM slot, sim_state=%s", |
| 6138 | verify_func(*verify_args)) |
| 6139 | return False |
Betty Zhou | d49a0ce | 2018-01-19 17:49:53 -0800 | [diff] [blame] | 6140 | |
| 6141 | |
| 6142 | def power_on_sim(ad, sim_slot_id=None): |
| 6143 | try: |
| 6144 | if sim_slot_id is None: |
| 6145 | ad.droid.telephonySetSimPowerState(CARD_POWER_UP) |
| 6146 | verify_func = ad.droid.telephonyGetSimState |
| 6147 | verify_args = [] |
| 6148 | else: |
| 6149 | ad.droid.telephonySetSimStateForSlotId(sim_slot_id, CARD_POWER_UP) |
| 6150 | verify_func = ad.droid.telephonyGetSimStateForSlotId |
| 6151 | verify_args = [sim_slot_id] |
| 6152 | except Exception as e: |
| 6153 | ad.log.error(e) |
| 6154 | return False |
Betty Zhou | ddc7640 | 2018-01-23 16:29:25 -0800 | [diff] [blame] | 6155 | if wait_for_state(verify_func, SIM_STATE_READY, |
| 6156 | MAX_WAIT_TIME_FOR_STATE_CHANGE, |
| 6157 | WAIT_TIME_BETWEEN_STATE_CHECK, *verify_args): |
Betty Zhou | a046979 | 2018-01-22 18:48:02 -0800 | [diff] [blame] | 6158 | ad.log.info("SIM slot is powered on, SIM state is READY") |
Betty Zhou | d49a0ce | 2018-01-19 17:49:53 -0800 | [diff] [blame] | 6159 | return True |
| 6160 | elif verify_func(*verify_args) == SIM_STATE_PIN_REQUIRED: |
Betty Zhou | e66efb4 | 2018-01-31 19:45:56 -0800 | [diff] [blame] | 6161 | ad.log.info("SIM is pin locked") |
| 6162 | return True |
Betty Zhou | d49a0ce | 2018-01-19 17:49:53 -0800 | [diff] [blame] | 6163 | else: |
| 6164 | ad.log.error("Fail to power on SIM slot") |
markdr | 6607bf1 | 2018-01-02 14:45:38 -0800 | [diff] [blame] | 6165 | return False |
Betty Zhou | 6357f45 | 2018-02-05 18:09:07 -0800 | [diff] [blame] | 6166 | |
| 6167 | |
Betty Zhou | 3db27a3 | 2018-04-23 14:31:25 -0700 | [diff] [blame] | 6168 | def extract_test_log(log, src_file, dst_file, test_tag): |
| 6169 | cmd = "grep -n '%s' %s" % (test_tag, src_file) |
| 6170 | result = job.run(cmd, ignore_status=True) |
| 6171 | if not result.stdout or result.exit_status == 1: |
Betty Zhou | e26fe89 | 2018-04-23 15:29:53 -0700 | [diff] [blame] | 6172 | log.warning("Command %s returns %s", cmd, result) |
Betty Zhou | 3db27a3 | 2018-04-23 14:31:25 -0700 | [diff] [blame] | 6173 | return |
| 6174 | line_nums = re.findall(r"(\d+).*", result.stdout) |
| 6175 | if line_nums: |
| 6176 | begin_line = line_nums[0] |
| 6177 | end_line = line_nums[-1] |
| 6178 | log.info("Extract %s from line %s to line %s to %s", src_file, |
| 6179 | begin_line, end_line, dst_file) |
| 6180 | job.run("awk 'NR >= %s && NR <= %s' %s > %s" % (begin_line, end_line, |
| 6181 | src_file, dst_file)) |
| 6182 | |
| 6183 | |
Betty Zhou | 0ca65c5 | 2018-04-26 15:47:38 -0700 | [diff] [blame] | 6184 | def get_device_epoch_time(ad): |
| 6185 | return int(1000 * float(ad.adb.shell("date +%s.%N"))) |
| 6186 | |
| 6187 | |
| 6188 | def synchronize_device_time(ad): |
| 6189 | ad.adb.shell("put global auto_time 0; date `date +%m%d%H%M%G.%S` ; " |
| 6190 | "am broadcast -a android.intent.action.TIME_SET") |
| 6191 | |
| 6192 | |
Betty Zhou | 74cf992 | 2018-04-18 20:18:12 -0700 | [diff] [blame] | 6193 | def log_messaging_screen_shot(ad, test_name=""): |
| 6194 | ad.adb.shell( |
| 6195 | "am start -n com.google.android.apps.messaging/.ui.ConversationListActivity" |
| 6196 | ) |
| 6197 | log_screen_shot(ad, test_name) |
| 6198 | ad.send_keycode("ENTER") |
| 6199 | ad.send_keycode("ENTER") |
| 6200 | log_screen_shot(ad, test_name) |
| 6201 | ad.send_keycode("HOME") |
| 6202 | |
| 6203 | |
Betty Zhou | a36c435 | 2018-04-05 18:49:32 -0700 | [diff] [blame] | 6204 | def log_screen_shot(ad, test_name=""): |
Betty Zhou | 74cf992 | 2018-04-18 20:18:12 -0700 | [diff] [blame] | 6205 | file_name = "/sdcard/Pictures/screencap" |
| 6206 | if test_name: |
| 6207 | file_name = "%s_%s" % (file_name, test_name) |
| 6208 | file_name = "%s_%s.png" % (file_name, utils.get_current_epoch_time()) |
Betty Zhou | e57ab69 | 2018-03-09 18:39:30 -0800 | [diff] [blame] | 6209 | try: |
| 6210 | ad.adb.shell("screencap -p %s" % file_name) |
Betty Zhou | e57ab69 | 2018-03-09 18:39:30 -0800 | [diff] [blame] | 6211 | except: |
Betty Zhou | e123c67 | 2018-03-21 19:57:11 -0700 | [diff] [blame] | 6212 | ad.log.error("Fail to log screen shot to %s", file_name) |
| 6213 | |
| 6214 | |
| 6215 | def get_screen_shot_log(ad, test_name="", begin_time=None): |
| 6216 | logs = ad.get_file_names("/sdcard/Pictures", begin_time=begin_time) |
| 6217 | if logs: |
| 6218 | ad.log.info("Pulling %s", logs) |
| 6219 | log_path = os.path.join(ad.log_path, test_name, |
Betty Zhou | a36c435 | 2018-04-05 18:49:32 -0700 | [diff] [blame] | 6220 | "Screenshot_%s" % ad.serial) |
Betty Zhou | e123c67 | 2018-03-21 19:57:11 -0700 | [diff] [blame] | 6221 | utils.create_dir(log_path) |
Betty Zhou | a36c435 | 2018-04-05 18:49:32 -0700 | [diff] [blame] | 6222 | ad.pull_files(logs, log_path) |
Betty Zhou | e123c67 | 2018-03-21 19:57:11 -0700 | [diff] [blame] | 6223 | ad.adb.shell("rm -rf /sdcard/Pictures/screencap_*", ignore_status=True) |
| 6224 | |
| 6225 | |
| 6226 | def get_screen_shot_logs(ads, test_name="", begin_time=None): |
| 6227 | for ad in ads: |
| 6228 | get_screen_shot_log(ad, test_name=test_name, begin_time=begin_time) |