Ensure calls are ended before disabling wifi calling

-Right now, in the event that call wasn't properly clenaed up
 within a test case, we call EndCall, but do not wait for the
 call to actually (attempt to) clean up before disabling wifi
 calling. Performing these operations out-of-order is unavailable
 to the user, so we should avoid it if possible.

-Add a telephony-layer check to wait_for_not_in_call() in
 addition to the telecom layer check.

Bug: 31064564
Change-Id: If5c6590af5f5558308f95b1f1b57a99f41e7565b
diff --git a/acts/framework/acts/test_utils/tel/tel_test_utils.py b/acts/framework/acts/test_utils/tel/tel_test_utils.py
index 69189a9..6c08209 100644
--- a/acts/framework/acts/test_utils/tel/tel_test_utils.py
+++ b/acts/framework/acts/test_utils/tel/tel_test_utils.py
@@ -39,6 +39,7 @@
 from acts.test_utils.tel.tel_defines import \
     MAX_WAIT_TIME_ACCEPT_CALL_TO_OFFHOOK_EVENT
 from acts.test_utils.tel.tel_defines import MAX_WAIT_TIME_AIRPLANEMODE_EVENT
+from acts.test_utils.tel.tel_defines import MAX_WAIT_TIME_CALL_DROP
 from acts.test_utils.tel.tel_defines import MAX_WAIT_TIME_CALL_INITIATION
 from acts.test_utils.tel.tel_defines import MAX_WAIT_TIME_CALLEE_RINGING
 from acts.test_utils.tel.tel_defines import MAX_WAIT_TIME_CONNECTION_STATE_UPDATE
@@ -1847,7 +1848,8 @@
         log: log object.
         ad:  android device.
     """
-    return not ad.droid.telecomIsInCall()
+    return ((not ad.droid.telecomIsInCall()) and
+            (ad.droid.telephonyGetCallState() == TELEPHONY_STATE_IDLE))
 
 
 def wait_for_droid_in_call(log, ad, max_time):
@@ -1882,7 +1884,7 @@
         log, ad, max_time, lambda log, ad: ad.droid.telecomIsRinging())
 
 
-def wait_for_droid_not_in_call(log, ad, max_time):
+def wait_for_droid_not_in_call(log, ad, max_time=MAX_WAIT_TIME_CALL_DROP):
     """Wait for android to be not in call state.
 
     Args:
@@ -2973,6 +2975,12 @@
     result = True
     if ad.droid.telecomIsInCall():
         ad.droid.telecomEndCall()
+
+    if not wait_for_droid_not_in_call(log, ad):
+        log.error("Failed to end call on {} "
+            "while ensuring phone in default state.".format(ad.serial))
+        result = False
+
     set_wfc_mode(log, ad, WFC_MODE_DISABLED)
 
     if not wait_for_not_network_rat(log,
@@ -2983,10 +2991,12 @@
             "ensure_phones_default_state: wait_for_droid_not_in iwlan fail {}.".format(
                 ad.serial))
         result = False
+
     if (not WifiUtils.wifi_reset(log, ad)):
         log.error("ensure_phones_default_state:reset WiFi fail {}.".format(
             ad.serial))
         result = False
+
     if not toggle_airplane_mode(log, ad, False):
         log.error(
             "ensure_phones_default_state:turn off airplane mode fail {}.".format(
@@ -3005,6 +3015,10 @@
     Phone not in call.
     Phone have no stored WiFi network and WiFi disconnected.
     Phone not in airplane mode.
+
+    Returns:
+        True if all steps of restoring default state succeed.
+        False if any of the steps to restore default state fails.
     """
     tasks = []
     for ad in ads: