Example #1
0
def wait(condition: Callable,
         max_timeout: int or float = 10,
         sleep_time: int or float = 0.5,
         waiting_for: str = '',
         error_msg: str = '',
         fail: bool = False,
         xfail: bool = False,
         logger: Logger = None):
    """
    condition = callable or 'lambda: some_value'
    """
    start_time = datetime.now()
    while not condition():
        time_delta = datetime.now() - start_time

        message = f'Waiting for {waiting_for}... {time_delta.seconds}.{time_delta.microseconds} s'
        if logger:
            logger.log(message)
        else:
            print(message)

        time.sleep(sleep_time)
        if time_delta.seconds > max_timeout:
            # if xfail:
            #     pytest.xfail(reason=error_msg)
            # elif fail:
            #     pytest.fail(msg=error_msg)
            # else:
            raise TimeoutError(f'{error_msg} {time_delta}')
Example #2
0
class EnvironmentAdapter:
    environment_command = "environment_remote "
    environment_remote = "com.github.remotesdk.ENVIRONMENT_REMOTE"

    broadcast = "am broadcast -a "
    es = " --es "
    am_command = broadcast + environment_remote + es + environment_command

    get_external_storage_directory_command = am_command + "getExternalStorageDirectory"
    get_root_directory_command = am_command + "getRootDirectory"
    get_data_directory_command = am_command + "getDataDirectory"
    get_download_cache_directory_command = am_command + "getDownloadCacheDirectory"
    get_storage_volumes_command = am_command + "getStorageVolumes"

    def __init__(self, shell: AndroidShell):
        self.shell = shell
        self.logger = Logger(scope_name=shell.serial + "] [" +
                             EnvironmentAdapter.__name__)

    def get_external_storage_directory(self) -> RemoteFile:
        result = self.shell.execute_broadcast(
            self.get_external_storage_directory_command)
        self.logger.log(message="get external storage directory [" + result +
                        "]")
        return RemoteFile(self.shell, result)

    def get_root_directory(self) -> RemoteFile:
        result = self.shell.execute_broadcast(self.get_root_directory_command)
        self.logger.log(message="get root directory [" + result + "]")
        return RemoteFile(self.shell, result)

    def get_data_directory(self) -> RemoteFile:
        result = self.shell.execute_broadcast(self.get_data_directory_command)
        self.logger.log(message="get data directory [" + result + "]")
        return RemoteFile(self.shell, result)

    def get_download_cache_directory(self) -> RemoteFile:
        result = self.shell.execute_broadcast(
            self.get_download_cache_directory_command)
        self.logger.log(message="get download cache directory [" + result +
                        "]")
        return RemoteFile(self.shell, result)

    def get_storage_volumes(self) -> list:
        result = self.shell.execute_broadcast(self.get_storage_volumes_command)
        name_spaces = json.loads(result,
                                 object_hook=lambda d: StorageVolume(**d))
        self.logger.log(message="get storage volumes")
        return name_spaces
class BluetoothDevice:
    bluetooth_command = "bluetooth_remote "
    bluetooth_remote = "com.github.remotesdk.BLUETOOTH_REMOTE"
    broadcast = "am broadcast -a "
    es = " --es "
    am_command = broadcast + bluetooth_remote + es + bluetooth_command

    get_pair_state_command = am_command + "getPairState,"
    get_device_name_command = am_command + "getDeviceName,"
    get_device_type_command = am_command + "getDeviceType,"
    get_device_class_command = am_command + "getDeviceClass,"

    def __init__(self, shell: AndroidShell, address: str):
        self.shell = shell
        self.address = address
        self.logger = Logger(scope_name=shell.serial + "] [" +
                             BluetoothDevice.__name__)

    def get_pair_state(self) -> PairState:
        result = PairState(
            int(
                self.shell.execute_broadcast(self.get_pair_state_command +
                                             self.address)))
        self.logger.log(message="get bluetooth device [" + self.address +
                        "] pair state [" + str(result.name) + "]")
        return result

    def get_name(self) -> str:
        result = self.shell.execute_broadcast(self.get_device_name_command +
                                              self.address)
        self.logger.log(message="get bluetooth device [" + self.address +
                        "] name [" + str(result) + "]")
        return result

    def get_type(self) -> str:
        result = Type(
            int(
                self.shell.execute_broadcast(self.get_device_type_command +
                                             self.address)))
        self.logger.log(message="get bluetooth device [" + self.address +
                        "] type [" + str(result.name) + "]")
        return result

    def get_bluetooth_class(self) -> BluetoothClass:
        result = BluetoothClass(
            int(
                self.shell.execute_broadcast(self.get_device_class_command +
                                             self.address)))
        self.logger.log(message="get bluetooth device [" + self.address +
                        "] class [" + str(result) + "]")
        return result

    def __str__(self):
        return self.address
class BluetoothAdapter:
    bluetooth_address_pattern = "[\\s\\S]*address:[\\s\\S](.*)[\\s\\S]*"
    bluetooth_command = "bluetooth_remote "
    bluetooth_remote = "com.github.remotesdk.BLUETOOTH_REMOTE"
    broadcast = "am broadcast -a "
    es = " --es "
    am_command = broadcast + bluetooth_remote + es + bluetooth_command

    enable_command = am_command + "enable"
    disable_command = am_command + "disable"
    is_enabled_command = am_command + "isEnabled"
    get_state_command = am_command + "getState"
    get_address_command = "dumpsys bluetooth_manager"
    discoverable_command = am_command + "discoverable,"
    get_scan_mode_command = am_command + "getScanMode"
    set_scan_mode_command = am_command + "setScanMode,"
    get_name_command = am_command + "getName"
    set_name_command = am_command + "setName,"
    start_discovery_command = am_command + "startDiscovery"
    cancel_discovery_command = am_command + "cancelDiscovery"
    is_discovering_command = am_command + "isDiscovering"
    pair_command = am_command + "pairDevice,"
    get_discovered_devices_command = am_command + "getDiscoveredDevices"
    get_paired_devices_command = am_command + "getBondedDevices"
    get_bluetooth_device_command = am_command + "getRemoteDevice,"
    factory_reset_command = am_command + "factoryReset"
    remove_pair_command = am_command + "removeBond,"
    get_pair_state_command = am_command + "getPairState,"
    get_supported_profiles_command = am_command + "getSupportedProfiles"
    get_profile_connected_state_command = am_command + "getProfileConnectionState,"
    is_bluetooth_a2dp_on_command = am_command + "isBluetoothA2dpOn"

    def __init__(self, shell: AndroidShell):
        self.shell = shell
        self.logger = Logger(scope_name=shell.serial + "] [" +
                             BluetoothAdapter.__name__)

    def enable(self) -> None:
        self.shell.execute_broadcast(self.enable_command)
        self.logger.log(message="enable")

    def disable(self) -> None:
        self.shell.execute_broadcast(self.disable_command)
        self.logger.log(message="disable")

    def is_enabled(self) -> bool:
        result = eval(
            self.shell.execute_broadcast(self.is_enabled_command).title())
        self.logger.log(message="is enabled [" + str(result) + "]")
        return result

    def get_state(self) -> State:
        result = State(
            int(self.shell.execute_broadcast(self.get_state_command)))
        self.logger.log(message="get state [" + result.name + "]")
        return result

    def get_address(self) -> str:
        result = self.shell.execute(self.get_address_command)
        if re.match(self.bluetooth_address_pattern, result):
            address = re.search(self.bluetooth_address_pattern,
                                result).group(1)
            self.logger.log(message="get address [" + address + "]")
            return address
        else:
            return ""

    def get_scan_mode(self) -> ScanMode:
        result = ScanMode(
            int(self.shell.execute_broadcast(self.get_scan_mode_command)))
        self.logger.log(message="get scan mode [" + result.name + "]")
        return result

    def set_scan_mode(self, scan_mode: ScanMode, time: int) -> bool:
        result = eval(
            self.shell.execute_broadcast(self.set_scan_mode_command +
                                         str(scan_mode.value) + "," +
                                         str(time)).title())
        self.logger.log(message="set scan mode [" + str(scan_mode.name) +
                        "] [" + str(result) + "]")
        return result

    def start_discoverable(self, time: int) -> None:
        self.shell.execute_broadcast(self.discoverable_command + str(time))
        self.logger.log(message="start discoverable")

    def cancel_discoverable(self) -> None:
        self.set_scan_mode(ScanMode.SCAN_MODE_NONE, 0)
        self.logger.log(message="cancel discoverable")

    def is_discoverable(self) -> bool:
        result = self.get_scan_mode(
        ) is ScanMode.SCAN_MODE_CONNECTABLE_DISCOVERABLE
        self.logger.log(message="is discoverable [" + str(result) + "]")
        return result

    def get_name(self) -> str:
        result = self.shell.execute_broadcast(self.get_name_command)
        self.logger.log(message="get name [" + str(result) + "]")
        return result

    def set_name(self, name: str) -> bool:
        result = eval(
            self.shell.execute_broadcast(self.set_name_command + name).title())
        self.logger.log(message="set name [" + str(name) + "] [" +
                        str(result) + "]")
        return result

    def start_discovery(self) -> bool:
        result = eval(
            self.shell.execute_broadcast(self.start_discovery_command).title())
        self.logger.log(message="start discovery [" + str(result) + "]")
        return result

    def cancel_discovery(self) -> bool:
        result = eval(
            self.shell.execute_broadcast(
                self.cancel_discovery_command).title())
        self.logger.log(message="cancel discovery [" + str(result) + "]")
        return result

    def is_discovering(self) -> bool:
        result = eval(
            self.shell.execute_broadcast(self.is_discovering_command).title())
        self.logger.log(message="is discovering [" + str(result) + "]")
        return result

    @Overload
    @signature("str")
    def pair(self, address: str) -> bool:
        result = eval(
            self.shell.execute_broadcast(self.pair_command +
                                         str(address)).title())
        self.logger.log(message="pair device [" + str(address) + "] [" +
                        str(result) + "]")
        return result

    @pair.overload
    @signature("BluetoothDevice")
    def pair(self, bluetooth_device: BluetoothDevice) -> bool:
        return self.pair(bluetooth_device.address)

    def get_discovered_devices(self) -> list:
        devices = []
        result = self.shell.execute_broadcast(
            self.get_discovered_devices_command)
        for address in result.split(","):
            if address:
                devices.append(BluetoothDevice(self.shell, address))
        self.logger.log(message="get discovered devices")
        return devices

    def get_paired_devices(self) -> list:
        devices = []
        result = self.shell.execute_broadcast(self.get_paired_devices_command)
        for address in result.split(","):
            if address:
                devices.append(BluetoothDevice(self.shell, address))
        self.logger.log(message="get paired devices")
        return devices

    def get_bluetooth_device(self, address: str) -> BluetoothDevice:
        result = BluetoothDevice(
            self.shell,
            self.shell.execute_broadcast(self.get_bluetooth_device_command +
                                         address))
        self.logger.log(message="get bluetooth device [" + str(address) + "]")
        return result

    def factory_reset(self) -> bool:
        result = eval(
            self.shell.execute_broadcast(self.factory_reset_command).title())
        self.logger.log(message="factory reset [" + str(result) + "]")
        return result

    def remove_pair(self, address: str) -> bool:
        result = eval(
            self.shell.execute_broadcast(self.remove_pair_command +
                                         address).title())
        self.logger.log(message="remove pair [" + str(address) + "] [" +
                        str(result) + "]")
        return result

    def remove_all_paired_devices(self) -> None:
        for device in self.get_paired_devices():
            self.remove_pair(device.address)

    def get_pair_state(self, address: str) -> PairState:
        result = PairState(
            int(
                self.shell.execute_broadcast(self.get_pair_state_command +
                                             address)))
        self.logger.log(message="get device [" + address + "] pair state [" +
                        str(result.name) + "]")
        return result

    def get_supported_profiles(self) -> list:
        profiles = []
        result = self.shell.execute_broadcast(
            self.get_supported_profiles_command)
        for profile in result.split(","):
            if profile:
                profiles.append(ProfileType(int(profile)))
        self.logger.log(message="get supported profiles")
        return profiles

    def get_profile_connected_state(self,
                                    profile: ProfileType) -> ConnectedState:
        result = ConnectedState(
            int(
                self.shell.execute_broadcast(
                    self.get_profile_connected_state_command +
                    str(profile.value))))
        self.logger.log(message="get profile [" + str(profile.name) +
                        "] connected state [" + str(result.name) + "]")

    def is_a2dp_on(self) -> bool:
        result = eval(
            self.shell.execute_broadcast(
                self.is_bluetooth_a2dp_on_command).title())
        self.logger.log(message="is A2DP on [" + str(result) + "]")
        return result
Example #5
0
class RemoteFile:
    environment_command = "environment_remote "
    environment_remote = "com.github.remotesdk.ENVIRONMENT_REMOTE"

    broadcast = "am broadcast -a "
    es = " --es "
    am_command = broadcast + environment_remote + es + environment_command

    is_file_exist_command = am_command + "isFileExist,"
    list_files_command = am_command + "listFiles,"
    is_directory_command = am_command + "isDirectory,"
    is_file_command = am_command + "isFile,"
    get_name_command = am_command + "getName,"
    get_parent_command = am_command + "getParent,"
    can_execute_command = am_command + "canExecute,"
    can_read_command = am_command + "canRead,"
    can_write_command = am_command + "canWrite,"
    is_absolute_command = am_command + "isAbsolute,"
    is_hidden_command = am_command + "isHidden,"
    delete_command = am_command + "deleteFile,"
    create_new_file_command = am_command + "createNewFile,"
    make_dir_command = am_command + "makeDir,"
    make_dirs_command = am_command + "createDirs,"
    rename_to_command = am_command + "renameTo,"
    set_readable_command = am_command + "setReadable,"
    set_writable_command = am_command + "setWritable,"
    set_executable_command = am_command + "setExecutable,"
    get_total_space_command = am_command + "getTotalSpace,"
    last_modified_command = am_command + "lastModified,"

    def __init__(self, shell: AndroidShell, path: str):
        self.shell = shell
        self.path = path
        self.logger = Logger(scope_name=shell.serial + "] [" +
                             RemoteFile.__name__)

    def exist(self) -> bool:
        result = eval(
            self.shell.execute_broadcast(self.is_file_exist_command +
                                         self.path).title())
        self.logger.log(message="is [" + self.path + "] exist [" +
                        str(result) + "]")
        return result

    def list_files(self) -> list:
        result = self.shell.execute_broadcast(self.list_files_command +
                                              self.path)
        listFiles = []
        try:
            for item in result.replace("[", "").replace("]", "").split(","):
                listFiles.append(RemoteFile(self.shell, item.strip()))
        except:
            pass
        self.logger.log(message="list files [" + self.path + "]")
        return listFiles

    def is_directory(self) -> bool:
        result = eval(
            self.shell.execute_broadcast(self.is_directory_command +
                                         self.path).title())
        self.logger.log(message="is [" + self.path + "] directory [" +
                        str(result) + "]")
        return result

    def is_file(self) -> bool:
        result = eval(
            self.shell.execute_broadcast(self.is_file_command +
                                         self.path).title())
        self.logger.log(message="is [" + self.path + "] file [" + str(result) +
                        "]")
        return result

    def get_name(self) -> str:
        result = self.shell.execute_broadcast(self.get_name_command +
                                              self.path)
        self.logger.log(message="get [" + self.path + "] name [" +
                        str(result) + "]")
        return result

    def get_parent(self):
        result = self.shell.execute_broadcast(self.get_parent_command +
                                              self.path)
        self.logger.log(message="get [" + self.path + "] parent [" +
                        str(result) + "]")
        return RemoteFile(self.shell, result)

    def can_execute(self) -> bool:
        result = eval(
            self.shell.execute_broadcast(self.can_execute_command +
                                         self.path).title())
        self.logger.log(message="can execute [" + self.path + "] [" +
                        str(result) + "]")
        return result

    def can_read(self) -> bool:
        result = eval(
            self.shell.execute_broadcast(self.can_read_command +
                                         self.path).title())
        self.logger.log(message="can read [" + self.path + "] [" +
                        str(result) + "]")
        return result

    def can_write(self) -> bool:
        result = eval(
            self.shell.execute_broadcast(self.can_write_command +
                                         self.path).title())
        self.logger.log(message="can write [" + self.path + "] [" +
                        str(result) + "]")
        return result

    def is_absolute(self) -> bool:
        result = eval(
            self.shell.execute_broadcast(self.is_absolute_command +
                                         self.path).title())
        self.logger.log(message="is absolute [" + self.path + "] [" +
                        str(result) + "]")
        return result

    def is_hidden(self) -> bool:
        result = eval(
            self.shell.execute_broadcast(self.is_hidden_command +
                                         self.path).title())
        self.logger.log(message="is hidden [" + self.path + "] [" +
                        str(result) + "]")
        return result

    def delete(self) -> bool:
        result = eval(
            self.shell.execute_broadcast(self.delete_command +
                                         self.path).title())
        self.logger.log(message="delete [" + self.path + "] [" + str(result) +
                        "]")
        return result

    def create_new_file(self) -> bool:
        result = eval(
            self.shell.execute_broadcast(self.create_new_file_command +
                                         self.path).title())
        self.logger.log(message="create new file [" + self.path + "] [" +
                        str(result) + "]")
        return result

    def make_dir(self) -> bool:
        result = eval(
            self.shell.execute_broadcast(self.make_dir_command +
                                         self.path).title())
        self.logger.log(message="make dir [" + self.path + "] [" +
                        str(result) + "]")
        return result

    def make_dirs(self) -> bool:
        result = eval(
            self.shell.execute_broadcast(self.make_dirs_command +
                                         self.path).title())
        self.logger.log(message="make dirs [" + self.path + "] [" +
                        str(result) + "]")
        return result

    def rename_to(self, rename: str) -> bool:
        result = eval(
            self.shell.execute_broadcast(self.rename_to_command + self.path +
                                         "," + rename).title())
        if rename:
            self.path = rename
        self.logger.log(message="rename  [" + self.path + "] to [" + rename +
                        "] [" + str(result) + "]")
        return result

    def get_total_space(self) -> int:
        result = int(
            self.shell.execute_broadcast(self.get_total_space_command +
                                         self.path))
        self.logger.log(message="get total space [" + self.path + "] [" +
                        str(result) + "]")
        return result

    def last_modified(self) -> int:
        result = int(
            self.shell.execute_broadcast(self.last_modified_command +
                                         self.path))
        self.logger.log(message="last modified [" + self.path + "] [" +
                        str(result) + "]")
        return result

    def __str__(self):
        return self.path
Example #6
0
class RemoteController:
    def __init__(self, serial):
        self.serial = serial
        self.shell = AndroidShell(serial)
        self.logger = Logger(scope_name=RemoteController.__name__)

    def get_bluetooth_adapter(self) -> BluetoothAdapter:
        self.logger.log(message="get bluetooth adapter")
        return BluetoothAdapter(self.shell)

    def get_wifi_adapter(self) -> WifiAdapter:
        self.logger.log(message="get wifi adapter")
        return WifiAdapter(self.shell)

    def get_telecom_adapter(self) -> TelecomAdapter:
        self.logger.log(message="get telecom adapter")
        return TelecomAdapter(self.shell)

    def get_environment_adapter(self) -> EnvironmentAdapter:
        self.logger.log(message="get environment adapter")
        return EnvironmentAdapter(self.shell)

    def get_remote_file(self, path: str) -> RemoteFile:
        self.logger.log(message="get remote file")
        return RemoteFile(self.shell, path)

    def get_usb_adapter(self) -> UsbAdapter:
        self.logger.log(message="get usb adapter")
        return UsbAdapter(self.shell)

    def get_player_adapter(self) -> PlayerAdapter:
        self.logger.log(message="get player adapter")
        return PlayerAdapter(self.shell)

    def start_view(self, actions: Actions):
        self.shell.execute(
            "am start -a " + actions.value +
            " -f 0x20000000 --activity-single-top --activity-clear-when-task-reset"
        )
        self.logger.log(message="start action [" + actions.value + "]")
Example #7
0
class UsbAdapter:
    usb_command = "usb_remote "
    usb_remote = "com.github.remotesdk.USB_REMOTE"

    broadcast = "am broadcast -a "
    es = " --es "
    am_command = broadcast + usb_remote + es + usb_command

    get_device_list_command = am_command + "getDeviceList"
    get_input_device_list_command = am_command + "getInputDeviceList"

    def __init__(self, shell: AndroidShell):
        self.shell = shell
        self.logger = Logger(scope_name=shell.serial + "] [" +
                             UsbAdapter.__name__)

    def get_input_device_list(self) -> list:
        list_devices = []
        result = self.shell.execute_broadcast(
            self.get_input_device_list_command)
        name_spaces = json.loads(result,
                                 object_hook=lambda d: SimpleNamespace(**d))
        for item in name_spaces:
            list_devices.append(
                InputDevice(generation=item.generation,
                            virtual=item.virtual,
                            productId=item.productId,
                            sources=item.sources,
                            vendorId=item.vendorId,
                            descriptor=item.descriptor,
                            enabled=item.enabled,
                            external=item.external,
                            keyboardType=item.keyboardType,
                            controllerNumber=item.controllerNumber,
                            name=item.name,
                            id=item.id,
                            fullKeyboard=item.fullKeyboard))
        self.logger.log(message="get input device list")
        return list_devices

    def get_usb_device_list(self) -> list:
        list_devices = []
        result = self.shell.execute_broadcast(self.get_device_list_command)
        name_spaces = json.loads(result,
                                 object_hook=lambda d: SimpleNamespace(**d))
        for item in name_spaces:
            list_devices.append(
                UsbDevice(deviceProtocol=item.deviceProtocol,
                          hasVideoPlayback=item.hasVideoPlayback,
                          serialNumber=item.serialNumber,
                          productId=item.productId,
                          manufacturerName=item.manufacturerName,
                          vendorId=item.vendorId,
                          deviceSubclass=item.deviceSubclass,
                          deviceId=item.deviceId,
                          deviceName=item.deviceName,
                          version=item.version,
                          hasAudioPlayback=item.hasAudioPlayback,
                          productName=item.productName,
                          configurationCount=item.configurationCount,
                          deviceClass=item.deviceClass,
                          interfaceCount=item.interfaceCount,
                          hasVideoCapture=item.hasVideoCapture,
                          hasMidi=item.hasMidi,
                          hasAudioCapture=item.hasAudioCapture))
        self.logger.log(message="get usb device list")
        return list_devices
class PlayerAdapter:
    player_command = "player_command "
    player_remote = "com.github.remotesdk.PLAYER_REMOTE"

    broadcast = "am broadcast -a "
    es = " --es "
    am_command = broadcast + player_remote + es + player_command

    play_song_command = am_command + "playSong,"
    stop_song_command = am_command + "stopSong"
    seek_to_command = am_command + "seekToSong,"
    get_duration_command = am_command + "getSongDuration"
    get_current_position_command = am_command + "getSongCurrentPosition"

    is_song_playing_command = am_command + "isSongPlaying"
    is_song_looping_command = am_command + "isSongLooping"
    set_looping_command_command = am_command + "setSongLooping"
    display_video_command = am_command + "displayVideo,"

    def __init__(self, shell: AndroidShell):
        self.shell = shell
        self.logger = Logger(scope_name=shell.serial + "] [" +
                             PlayerAdapter.__name__)

    @Overload
    @signature("str")
    def play(self, file: str) -> None:
        self.shell.execute_broadcast(self.play_song_command + file)
        self.logger.log(message="play [" + file + "]")

    @play.overload
    @signature("RemoteFile")
    def play(self, file: RemoteFile):
        self.play(file.path)

    def stop(self) -> None:
        self.shell.execute_broadcast(self.stop_song_command)
        self.logger.log(message="stop play")

    def seek_to(self, seek_time: int) -> None:
        self.shell.execute_broadcast(self.seek_to_command + str(seek_time))
        self.logger.log(message="seek to [" + str(seek_time) + "]")

    def get_duration(self) -> int:
        result = int(self.shell.execute_broadcast(self.get_duration_command))
        self.logger.log(message="get duration [" + str(result) + "]")
        return result

    def get_current_position(self) -> int:
        result = int(
            self.shell.execute_broadcast(self.get_current_position_command))
        self.logger.log(message="get current position [" + str(result) + "]")
        return result

    def is_playing(self) -> bool:
        result = eval(
            self.shell.execute_broadcast(self.is_song_playing_command).title())
        self.logger.log(message="is playing [" + str(result) + "]")
        return result
Example #9
0
class TelecomAdapter:
    telecom_command = "telephony_remote "
    telecom_remote = "com.github.remotesdk.TELEPHONY_REMOTE"
    broadcast = "am broadcast -a "
    es = " --es "
    am_command = broadcast + telecom_remote + es + telecom_command

    get_call_state_command = am_command + "getCallState"
    get_call_history_command = am_command + "getCallHistory"
    get_contacts_command = am_command + "getContacts"
    call_command = "am start -a android.intent.action.CALL -d tel:"
    end_call_command = am_command + "endCallProgrammatically"
    answer_ringing_call_command = am_command + "answerRingingCall"
    get_incoming_call_number_command = am_command + "getIncomingCallNumber"
    set_data_enabled_command = am_command + "setDataEnabled,"
    is_data_enabled_command = am_command + "isDataEnabled"
    get_data_state_command = am_command + "getDataState"
    get_data_network_type_command = am_command + "getDataNetworkType"
    get_phone_type_command = am_command + "getPhoneType"
    get_sim_state_command = am_command + "getSimState"
    get_network_operator_name_command = am_command + "getNetworkOperatorName"
    send_ussd_request_command = am_command + "sendUssdRequest,"
    get_ussd_responce_command = am_command + "getUssdResponse"
    get_mobile_phone_command = am_command + "getMobilePhone"

    def __init__(self, shell: AndroidShell):
        self.shell = shell
        self.logger = Logger(scope_name=shell.serial + "] [" +
                             TelecomAdapter.__name__)

    def get_call_state(self) -> CallState:
        result = CallState(
            int(self.shell.execute_broadcast(self.get_call_state_command)))
        self.logger.log(message="get call state [" + result.name + "]")
        return result

    def get_call_history(self) -> list:
        result = self.shell.execute_broadcast(self.get_call_history_command)
        call_history_list = json.loads(result,
                                       object_hook=lambda d: CallHistory(**d))
        self.logger.log(message="get call history")
        return call_history_list

    def get_contacts(self) -> list:
        result = self.shell.execute_broadcast(self.get_contacts_command)
        contacts_list = json.loads(result, object_hook=lambda d: Contacts(**d))
        self.logger.log(message="get contacts")
        return contacts_list

    def call(self, number: str) -> None:
        self.shell.execute(self.call_command + number)
        self.logger.log(message="call number [" + number + "]")

    def end_call(self) -> None:
        self.shell.execute_broadcast(self.end_call_command)
        self.logger.log(message="end call")

    def answer_incoming_call(self) -> None:
        self.shell.execute_broadcast(self.answer_ringing_call_command)
        self.logger.log(message="answer incoming call")

    def get_incoming_call_number(self) -> str:
        result = self.shell.execute_broadcast(self.answer_ringing_call_command)
        self.logger.log(message="get incoming call number [" + result + "]")
        return result

    def set_data_enabled(self, state: bool) -> None:
        self.shell.execute_broadcast(self.set_data_enabled_command +
                                     str(state))
        self.logger.log(message="set data enabled [" + str(state) + "]")

    def is_data_enabled(self) -> bool:
        result = eval(
            self.shell.execute_broadcast(self.is_data_enabled_command).title())
        self.logger.log(message="is data enabled [" + str(result) + "]")

    def get_data_state(self) -> DataState:
        result = DataState(
            int(self.shell.execute_broadcast(self.get_data_state_command)))
        self.logger.log(message="get data state [" + result.name + "]")
        return result

    def get_data_network_type(self) -> DataNetworkType:
        result = DataNetworkType(
            int(
                self.shell.execute_broadcast(
                    self.get_data_network_type_command)))
        self.logger.log(message="get data network type [" + result.name + "]")
        return result

    def get_phone_type(self) -> PhoneType:
        result = PhoneType(
            int(self.shell.execute_broadcast(self.get_phone_type_command)))
        self.logger.log(message="get phone type [" + result.name + "]")
        return result

    def get_sim_state(self) -> SimState:
        result = SimState(
            int(self.shell.execute_broadcast(self.get_sim_state_command)))
        self.logger.log(message="get sim state [" + result.name + "]")
        return result

    def get_network_operator_name(self) -> str:
        result = self.shell.execute_broadcast(
            self.get_network_operator_name_command)
        self.logger.log(message="get network operator name [" + result + "]")
        return result

    def send_ussd_request(self, ussd: str) -> str:
        self.shell.execute_broadcast(self.send_ussd_request_command + ussd)
        time.sleep(2)
        result = self.shell.execute_broadcast(self.get_ussd_responce_command)
        self.logger.log(message="send ussd request [" + ussd + "] [" + result +
                        "]")
        return result

    def get_mobile_phone(self) -> str:
        result = self.shell.execute_broadcast(self.get_mobile_phone_command)
        self.logger.log(message="get mobile phone [" + result + "]")
        return result
Example #10
0
class WifiAdapter:
    wifi_command = "wifi_remote "
    wifi_remote = "com.github.remotesdk.WIFI_REMOTE"
    broadcast = "am broadcast -a "
    es = " --es "
    am_command = broadcast + wifi_remote + es + wifi_command

    enable_command = am_command + "enable"
    disable_command = am_command + "disable"
    get_state_command = am_command + "getState"
    is_enabled_command = am_command + "isEnabled"
    get_wifi_hotspot_config_command = am_command + "getWifiApConfiguration"
    add_network_command = am_command + "addNetwork,"
    enable_network_command = am_command + "enableNetwork,"
    disable_network_command = am_command + "disableNetwork,"
    remove_network_command = am_command + "removeNetwork,"
    get_configured_networks_command = am_command + "getConfiguredNetworks"
    disconnect_command = am_command + "disconnect"
    set_hotspot_config_command = am_command + "setWifiApConfiguration,"
    is_hotspot_enabled_command = am_command + "isWifiApEnabled"
    start_tethering_command = am_command + "startTethering"
    stop_tethering_command = am_command + "stopTethering"
    get_hotspot_state_command = am_command + "getWifiApState"
    reconnect_command = am_command + "reconnect"
    reassociate_command = am_command + "reassociate"
    start_scan_command = am_command + "startScan"
    get_scan_results_command = am_command + "getScanResults"
    is_connected_network_command = am_command + "isWifiNetworkConnected"

    set_scan_always_available_command = am_command + "setScanAlwaysAvailable,"
    get_scan_always_available_command = am_command + "getScanAlwaysAvailable"

    def __init__(self, shell: AndroidShell):
        self.shell = shell
        self.logger = Logger(scope_name=shell.serial + "] [" +
                             WifiAdapter.__name__)

    def enable(self) -> bool:
        result = eval(
            self.shell.execute_broadcast(self.enable_command).title())
        self.logger.log(message="enable [" + str(result) + "]")
        return result

    def disable(self) -> bool:
        result = eval(
            self.shell.execute_broadcast(self.disable_command).title())
        self.logger.log(message="disable [" + str(result) + "]")
        return result

    def get_state(self) -> State:
        result = State(
            int(self.shell.execute_broadcast(self.get_state_command)))
        self.logger.log(message="get satate [" + str(result.name) + "]")
        return result

    def is_enabled(self) -> bool:
        result = eval(
            self.shell.execute_broadcast(self.is_enabled_command).title())
        self.logger.log(message="is enabled [" + str(result) + "]")
        return result

    def get_hotspot_configuration(self) -> WifiConfiguration:
        result = self.shell.execute_broadcast(
            self.get_wifi_hotspot_config_command)
        name_spaces = json.loads(result,
                                 object_hook=lambda d: SimpleNamespace(**d))
        config = WifiConfiguration(SSID=name_spaces.SSID,
                                   preSharedKey=name_spaces.preSharedKey,
                                   networkId=name_spaces.networkId,
                                   security_type=SecurityType.PASS)
        self.logger.log(message="get hotspot configuration")
        return config

    @Overload
    @signature("str", "str", "SecurityType")
    def add_network(self, ssid: str, password: str,
                    security_type: SecurityType) -> int:
        result = int(
            self.shell.execute_broadcast(self.add_network_command + ssid +
                                         "," + password + "," +
                                         str(security_type.value)))
        self.logger.log(message="add network ssid [" + ssid + "] pass [" +
                        password + "] config [" + str(security_type.name) +
                        "] [" + str(result) + "]")
        return result

    @add_network.overload
    @signature("WifiConfiguration")
    def add_network(self, wifi_configuration: WifiConfiguration) -> int:
        return self.add_network(wifi_configuration.SSID,
                                wifi_configuration.preSharedKey,
                                wifi_configuration.security_type)

    def enable_network(self, net_id: int) -> bool:
        result = eval(
            self.shell.execute_broadcast(self.enable_network_command +
                                         str(net_id)).title())
        self.logger.log(message="enable network id [" + str(net_id) + "] [" +
                        str(result) + "]")
        return result

    def disable_network(self, net_id: int) -> bool:
        result = eval(
            self.shell.execute_broadcast(self.disable_network_command +
                                         str(net_id)).title())
        self.logger.log(message="enable network id [" + str(net_id) + "] [" +
                        str(result) + "]")
        return result

    def remove_network(self, net_id: int) -> bool:
        result = eval(
            self.shell.execute_broadcast(self.remove_network_command +
                                         str(net_id)).title())
        self.logger.log(message="remove network id [" + str(net_id) + "] [" +
                        str(result) + "]")
        return result

    def remove_all_networks(self) -> None:
        for config in self.get_configured_networks():
            self.remove_network(config.networkId)

    def get_configured_networks(self) -> list:
        networks = []
        result = self.shell.execute_broadcast(
            self.get_configured_networks_command)
        name_spaces = json.loads(result,
                                 object_hook=lambda d: SimpleNamespace(**d))
        for item in name_spaces:
            networks.append(
                WifiConfiguration(SSID=item.SSID,
                                  preSharedKey=item.preSharedKey,
                                  networkId=int(item.networkId),
                                  security_type=SecurityType.PASS))
        self.logger.log(message="get configured networks")
        return networks

    def disconnect(self) -> bool:
        result = eval(
            self.shell.execute_broadcast(self.disconnect_command).title())
        self.logger.log(message="disconnect [" + str(result) + "]")
        return result

    @Overload
    @signature("str", "str", "SecurityType")
    def set_hotspot_configuration(self, ssid: str, password: str,
                                  security_type: SecurityType) -> bool:
        result = eval(
            self.shell.execute_broadcast(self.set_hotspot_config_command +
                                         ssid + "," + password + "," +
                                         str(security_type.value)).title())
        self.logger.log(message="set hotspot configuration [" + ssid +
                        "] pass [" + password + "] config [" +
                        str(security_type.name) + "] [" + str(result) + "]")
        return result

    @set_hotspot_configuration.overload
    @signature("WifiConfiguration")
    def set_hotspot_configuration(
            self, wifi_configuration: WifiConfiguration) -> bool:
        return self.set_hotspot_configuration(
            ssid=wifi_configuration.SSID,
            password=wifi_configuration.preSharedKey,
            security_type=wifi_configuration.security_type)

    def is_hotspot_enabled(self) -> bool:
        result = eval(
            self.shell.execute_broadcast(
                self.is_hotspot_enabled_command).title())
        self.logger.log(message="is hotspot enabled [" + str(result) + "]")
        return result

    def start_tethering(self) -> bool:
        result = eval(
            self.shell.execute_broadcast(self.start_tethering_command).title())
        self.logger.log(message="start hotspot tethering [" + str(result) +
                        "]")
        return result

    def stop_tethering(self):
        self.shell.execute_broadcast(self.stop_tethering_command)
        self.logger.log(message="stop hotspot tethering")

    def get_hotspot_state(self) -> State:
        result = State(
            int(self.shell.execute_broadcast(self.get_hotspot_state_command)))
        self.logger.log(message="get hotspot state [" + str(result.name) + "]")
        return result

    def reconnect(self) -> bool:
        result = eval(
            self.shell.execute_broadcast(self.reconnect_command).title())
        self.logger.log(message="reconnect [" + str(result) + "]")
        return result

    def reassociate(self) -> bool:
        result = eval(
            self.shell.execute_broadcast(self.reassociate_command).title())
        self.logger.log(message="reassociate [" + str(result) + "]")
        return result

    def start_scan(self) -> bool:
        result = eval(
            self.shell.execute_broadcast(self.start_scan_command).title())
        self.logger.log(message="start scan [" + str(result) + "]")
        return result

    def get_scan_results(self) -> list:
        scan_results = []
        result = self.shell.execute_broadcast(self.get_scan_results_command)
        name_spaces = json.loads(result,
                                 object_hook=lambda d: SimpleNamespace(**d))
        for item in name_spaces:
            scan_results.append(
                ScanResult(SSID=item.SSID,
                           BSSID=item.BSSID,
                           hessid=item.hessid,
                           anqpDomainId=item.anqpDomainId,
                           capabilities=item.capabilities,
                           level=item.level,
                           frequency=item.frequency,
                           channelWidth=item.channelWidth,
                           timestamp=item.timestamp,
                           seen=item.seen,
                           untrusted=item.untrusted,
                           numUsage=item.numUsage,
                           distanceCm=item.distanceCm,
                           distanceSdCm=item.distanceSdCm))
        self.logger.log(message="get scan results")
        return scan_results

    def is_network_connected(self) -> bool:
        result = eval(
            self.shell.execute_broadcast(
                self.is_connected_network_command).title())
        self.logger.log(message="is network connected [" + str(result) + "]")
        return result

    def set_scan_always_available(self, state: bool) -> bool:
        result = eval(
            self.shell.execute_broadcast(
                self.set_scan_always_available_command + state).title())
        self.logger.log(message="set scan always available [" + str(result) +
                        "]")
        return result

    def get_scan_always_available(self) -> bool:
        result = eval(
            self.shell.execute_broadcast(
                self.get_scan_always_available_command).title())
        self.logger.log(message="get scan always available [" + str(result) +
                        "]")
        return result