1#  Copyright (C) 2023 The Android Open Source Project
2#
3#  Licensed under the Apache License, Version 2.0 (the "License");
4#  you may not use this file except in compliance with the License.
5#  You may obtain a copy of the License at
6#
7#       http://www.apache.org/licenses/LICENSE-2.0
8#
9#  Unless required by applicable law or agreed to in writing, software
10#  distributed under the License is distributed on an "AS IS" BASIS,
11#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12#  See the License for the specific language governing permissions and
13#  limitations under the License.
14
15import logging
16import time
17
18from utilities import constants
19
20
21class CommonUtils:
22    """A common utilities for HU and phone device"""
23
24    def __init__(self, target, discoverer):
25        self.target = target
26        self.discoverer = discoverer
27
28    # Checking is current page has UI element with exact text on HU
29    def has_ui_element_with_text(self, ui_element_text_content):
30        logging.info('Checking is current page has UI element on HU with text <%s>',
31                     ui_element_text_content)
32        is_current_page_has_ui_element = self.discoverer.mbs.hasUIElementWithText(
33            ui_element_text_content)
34        logging.info('Current page on HU has UI element with text: %s',
35                     is_current_page_has_ui_element)
36        return is_current_page_has_ui_element
37
38    # Click on UI element on HU with exact text
39    def click_on_ui_element_with_text(self, ui_element_text_content):
40        logging.info('Click on UI element on HU with text <%s>', ui_element_text_content)
41        if self.has_ui_element_with_text(ui_element_text_content) is True:
42            self.discoverer.mbs.clickUIElementWithText(ui_element_text_content)
43
44    # Wait for specific time
45    def wait_with_log(self, wait_time):
46        logging.info("Sleep for %s seconds", wait_time)
47        time.sleep(wait_time)
48
49    # Enable WIFI on phone device
50    def enable_wifi_on_phone_device(self):
51        logging.info("Enable WIFI on phone device")
52        self.target.mbs.wifiEnable()
53
54    # Grant restricted permission on HU
55    def grant_restricted_permission_on_hu(self, permission):
56        logging.info('Grant dangerous permission <%s> on HU', permission)
57        self.discoverer.mbs.grantRestrictedPermissionsForBTMedia(permission)
58
59    # TODO remove this function after BT Media local mac address issue resolved b/316199227
60    # Grant LOCAL_MAC_ADDRESS permission
61    def grant_local_mac_address_permission(self):
62        logging.info("Grant <LOCAL_MAC_ADDRESS> permission on HU")
63        self.grant_restricted_permission_on_hu(constants.LOCAL_MAC_ADDRESS_PERMISSION)
64