import os, time
from ppadb.client import Client as AdbClient

def connect_device():
    os.system('adb start-server')
    client = AdbClient(host="127.0.0.1", port=5037)
    devices = client.devices()
    if len(devices) == 1:
        device = client.device(devices[0].serial)
        return device
    elif len(devices) == 0:
        raise ConnectionError
    else:
        n = 1
        for device in devices:
            print(f'[{n}] {device.serial}')
            n += 1
        choice = input('Please enter the number of the device you would like\
             to connect to: ')
        device = client.device(devices[choice-1].serial)
        return device
    
def shell(device, command):
    return device.shell(command)

def pull(device, ipath, opath):
    print(f"Pulling {ipath} to {opath}...")
    os.system(f'adb pull {ipath} {opath}')

def reboot_bootloader():
    print("Rebooting to Bootloader...")
    os.system('adb reboot-bootloader')

def backup():
    os.system('adb backup -all')