import { Observable , Subject , Subscription } from 'rxjs' ; import { IAdbDevice , IAdbProcess } from '../../../../../../../../common/interfaces/interface.adb' ; import ElectronIpcService , { IPC } from '../../../../services/service.electron.ipc' ; import * as Toolkit from 'chipmunk.client.toolkit' ; export interface IAmount { guid : string ; amount : string ; } export enum EAdbStatus { init = 'init' , ready = 'ready' , error = 'error' } export class SidebarAppAdbService { private _status : EAdbStatus = EAdbStatus . init ; private _errorMessage : string = '' ; private _subscriptions : { [ key ] : Toolkit . Subscription | Subscription ; } = { } ; private _subjects : { onAmount : Subject < IAmount > ; onDisconnect : Subject < IPC . IAdbDeviceDisconnected > ; } = { onAmount : new Subject < IAmount > ( ) , onDisconnect : new Subject < IPC . IAdbDeviceDisconnected > ( ) , } ; constructor ( ) { this . _subscriptions . AdbStreamUpdated = ElectronIpcService . subscribe ( IPC . AdbStreamUpdated , this . _onAdbStreamUpdated . bind ( this ) ) ; this . _subscriptions . AdbDeviceDisconnected = ElectronIpcService . subscribe ( IPC . AdbDeviceDisconnected , this . _onAdbDeviceDisconnected . bind ( this ) ) ; } public destroy ( ) { Object . keys ( this . _subscriptions ) . forEach ( ( key ) => { this . _subscriptions [ key ] . unsubscribe ( ) ; } ) ; } public getObservable ( ) { return { onAmount : this . _subjects . onAmount . asObservable ( ) , onDisconnect : this . _subjects . onDisconnect . asObservable ( ) , } ; } public set status ( status ) { this . _status = status ; } public get status ( ) : EAdbStatus { return this . _status ; } public get errorMessage ( ) : string { return this . _errorMessage ; } public prepare ( request ) { this . _status = EAdbStatus . init ; return new Promise ( ( resolve , reject ) => { ElectronIpcService . request < IPC . AdbStartServerResponse > ( new IPC . AdbStartServerRequest ( request ) , IPC . AdbStartServerResponse ) . then ( ( response ) => { if ( response . error !== undefined ) { this . _errorMessage = response . error ; this . _status = EAdbStatus . error ; return reject ( new Error ( response . error ) ) ; } this . _status = EAdbStatus . ready ; resolve ( ) ; } ) . catch ( ( error ) => { this . _errorMessage = error . message ; this . _status = EAdbStatus . error ; reject ( error ) ; } ) ; } ) ; } public getDevices ( request ) { return new Promise ( ( resolve , reject ) => { ElectronIpcService . request < IPC . AdbDevicesResponse > ( new IPC . AdbDevicesRequest ( request ) , IPC . AdbDevicesResponse ) . then ( ( response ) => { if ( response . error !== undefined ) { return reject ( new Error ( response . error ) ) ; } if ( response . devices === undefined ) { return reject ( new Error ( `AdbDevicesResponse returns invalid response` ) ) ; } resolve ( response . devices ) ; } ) . catch ( ( error ) => { reject ( error ) ; } ) ; } ) ; } public getProcesses ( request ) { return new Promise ( ( resolve , reject ) => { ElectronIpcService . request < IPC . AdbProcessesResponse > ( new IPC . AdbProcessesRequest ( request ) , IPC . AdbProcessesResponse ) . then ( ( response ) => { if ( response . error !== undefined ) { return reject ( new Error ( response . error ) ) ; } if ( response . processes === undefined ) { return reject ( new Error ( `AdbProcessesResponse returns invalid response` ) ) ; } resolve ( response . processes ) ; } ) . catch ( ( error ) => { reject ( error ) ; } ) ; } ) ; } public start ( request ) { return new Promise ( ( resolve , reject ) => { ElectronIpcService . request ( new IPC . AdbStartRequest ( request ) , IPC . AdbStartResponse ) . then ( ( response ) => { resolve ( ) ; } ) . catch ( ( error ) => { reject ( error ) ; } ) ; } ) ; } public stop ( request ) { return new Promise ( ( resolve , reject ) => { ElectronIpcService . request ( new IPC . AdbStopRequest ( request ) , IPC . AdbStopResponse ) . then ( ( response ) => { if ( response . error !== undefined ) { return reject ( new Error ( response . error ) ) ; } resolve ( ) ; } ) . catch ( ( error ) => { reject ( error ) ; } ) ; } ) ; } public change ( request ) { return new Promise ( ( resolve , reject ) => { this . start ( request ) . then ( ( ) => { resolve ( ) ; } ) . catch ( ( error ) => { reject ( error ) ; } ) ; } ) ; } public restore ( request ) { return new Promise ( ( resolve , reject ) => { ElectronIpcService . request < IPC . AdbLoadResponse > ( new IPC . AdbLoadRequest ( request ) , IPC . AdbLoadResponse ) . then ( ( response ) => { resolve ( response ) ; } ) . catch ( ( error ) => { reject ( error ) ; } ) ; } ) ; } public bytesToString ( amount ) { if ( amount < 1024 ) { return `${ amount } bytes` ; } else if ( amount / 1024 < 1024 ) { return `${ ( amount / 1024 ) . toFixed ( 2 ) } kB` ; } else if ( amount / 1024 / 1024 < 1024 * 1024 ) { return `${ ( amount / 1024 / 1024 ) . toFixed ( 4 ) } Mb` ; } else { return `${ ( amount / 1024 / 1024 / 1024 ) . toFixed ( 5 ) } Gb` ; } } private _onAdbStreamUpdated ( response ) { this . _subjects . onAmount . next ( { guid : response . guid , amount : this . bytesToString ( response . amount ) , } ) ; } private _onAdbDeviceDisconnected ( response ) { this . _subjects . onDisconnect . next ( { guid : response . guid , device : response . device , } ) ; } }