1
+ 'use strict' ;
2
+
3
+ const log = require ( 'electron-log' ) ;
4
+ const Promise = require ( 'bluebird' ) ;
5
+ const adb = require ( 'adbkit' ) ;
6
+ const client = adb . createClient ( ) ;
7
+ const toTitleCase = require ( 'titlecase' )
8
+
9
+ const ipcMain = require ( 'electron' ) . ipcMain ;
10
+
11
+ ipcMain . on ( 'adbkit-observe-devices' , function ( event , arg ) {
12
+ observeDevices ( event , arg ) ;
13
+ } ) ;
14
+
15
+ function observeDevices ( event , arg ) {
16
+ log . debug ( `Starting to observe device state changes` ) ;
17
+ client . trackDevices ( )
18
+ . then ( function ( tracker ) {
19
+ tracker . on ( 'add' , function ( device ) {
20
+ event . sender . send ( 'adbkit-device-added' , device ) ;
21
+ } ) ;
22
+ tracker . on ( 'remove' , function ( device ) {
23
+ event . sender . send ( 'adbkit-device-removed' , device ) ;
24
+ } ) ;
25
+ tracker . on ( 'end' , function ( ) {
26
+ log . debug ( `Device state observation stopped` ) ;
27
+ } ) ;
28
+ } )
29
+ . catch ( function ( err ) {
30
+ log . warn ( 'Unable to observe devices:' , err . stack )
31
+ } ) ;
32
+ }
33
+
34
+ ipcMain . on ( 'adbkit-update-devices' , function ( event , arg ) {
35
+ updateDevices ( event , arg ) ;
36
+ } ) ;
37
+
38
+ function updateDevices ( event , arg ) {
39
+ log . debug ( `Updating device list` ) ;
40
+ client . listDevicesWithPaths ( )
41
+ . then ( function ( devices ) {
42
+ event . sender . send ( 'adbkit-devices-updated' , devices ) ;
43
+ } )
44
+ . catch ( function ( err ) {
45
+ log . warn ( 'Unable to get devices:' , err . stack )
46
+ event . sender . send ( 'adbkit-devices-updated' , [ ] ) ;
47
+ } ) ;
48
+ }
49
+
50
+ ipcMain . on ( 'adbkit-update-device' , function ( event , device ) {
51
+ updateDeviceFeatures ( event , device ) ;
52
+ updateDeviceIpAddress ( event , device ) ;
53
+ updateDeviceId ( event , device ) ;
54
+ updateDeviceName ( event , device ) ;
55
+ updateDeviceManufacturer ( event , device ) ;
56
+ updateDeviceModel ( event , device ) ;
57
+ } ) ;
58
+
59
+ function updateDeviceFeatures ( event , device ) {
60
+ log . debug ( `Updating features of device: ${ device . id } ` ) ;
61
+ client . getFeatures ( device . id )
62
+ . then ( function ( features ) {
63
+ device . features = features ;
64
+ event . sender . send ( 'adbkit-device-features-updated' , device ) ;
65
+ } )
66
+ . catch ( function ( err ) {
67
+ log . warn ( 'Unable to get device features:' , err . stack )
68
+ } ) ;
69
+ }
70
+
71
+ function updateDeviceIpAddress ( event , device ) {
72
+ log . debug ( `Updating IP address of device: ${ device . id } ` ) ;
73
+ runShellCommand ( device , 'ip addr show wlan0 | grep \'inet \' | cut -d\' \' -f6|cut -d/ -f1' )
74
+ . then ( ( output ) => {
75
+ device . ipAddress = output ;
76
+ event . sender . send ( 'adbkit-device-updated' , device ) ;
77
+ } )
78
+ . catch ( ( err ) => {
79
+ log . warn ( 'Unable to get device IP address:' , err . stack )
80
+ } ) ;
81
+ }
82
+
83
+ function updateDeviceId ( event , device ) {
84
+ log . debug ( `Updating Android ID of device: ${ device . id } ` ) ;
85
+ runShellCommand ( device , createGetSettingCommand ( 'secure' , 'android_id' ) )
86
+ . then ( ( output ) => {
87
+ device . androidId = output ;
88
+ event . sender . send ( 'adbkit-device-updated' , device ) ;
89
+ } )
90
+ . catch ( ( err ) => {
91
+ log . warn ( 'Unable to get device ID:' , err . stack )
92
+ } ) ;
93
+ }
94
+
95
+ function updateDeviceName ( event , device ) {
96
+ log . debug ( `Updating name of device: ${ device . id } ` ) ;
97
+ runShellCommand ( device , createGetSettingCommand ( 'secure' , 'bluetooth_name' ) )
98
+ . then ( ( output ) => {
99
+ device . bluetoothName = output ;
100
+ event . sender . send ( 'adbkit-device-updated' , device ) ;
101
+ } )
102
+ . catch ( ( err ) => {
103
+ log . warn ( 'Unable to get device name:' , err . stack )
104
+ } ) ;
105
+ }
106
+
107
+ function updateDeviceManufacturer ( event , device ) {
108
+ log . debug ( `Updating manufacturer of device: ${ device . id } ` ) ;
109
+ runShellCommand ( device , createGetPropCommand ( 'ro.product.manufacturer' ) )
110
+ . then ( ( output ) => {
111
+ device . manufacturer = toTitleCase ( output . toLowerCase ( ) ) ;
112
+ event . sender . send ( 'adbkit-device-updated' , device ) ;
113
+ } )
114
+ . catch ( ( err ) => {
115
+ log . warn ( 'Unable to get device name:' , err . stack )
116
+ } ) ;
117
+ }
118
+
119
+ function updateDeviceModel ( event , device ) {
120
+ log . debug ( `Updating model of device: ${ device . id } ` ) ;
121
+ runShellCommand ( device , createGetPropCommand ( 'ro.product.model' ) )
122
+ . then ( ( output ) => {
123
+ device . model = toTitleCase ( output . toLowerCase ( ) ) ;
124
+ event . sender . send ( 'adbkit-device-updated' , device ) ;
125
+ } )
126
+ . catch ( ( err ) => {
127
+ log . warn ( 'Unable to get device model:' , err . stack )
128
+ } ) ;
129
+ }
130
+
131
+ function runShellCommand ( device , command ) {
132
+ log . debug ( `Executing shell command on ${ device . id } : ${ command } ` ) ;
133
+ return new Promise ( ( resolve , reject ) => {
134
+ client . shell ( device . id , command )
135
+ . then ( function ( outputStream ) {
136
+ adb . util . readAll ( outputStream )
137
+ . then ( function ( outputBuffer ) {
138
+ let output = outputBuffer . toString ( 'utf-8' ) ;
139
+ // remove linebreaks from start and end
140
+ output = output . replace ( / ^ \s + | \s + $ / g, '' ) ;
141
+ resolve ( output ) ;
142
+ } ) ;
143
+ } )
144
+ . catch ( reject ) ;
145
+ } ) ;
146
+ }
147
+
148
+ function createGetPropCommand ( propertyId ) {
149
+ return `getprop | grep "${ propertyId } " | cut -d ":" -f2 | cut -d "[" -f2 | cut -d "]" -f1` ;
150
+ }
151
+
152
+ function createGetSettingCommand ( namespace , settingId ) {
153
+ return `settings get ${ namespace } "${ settingId } "` ;
154
+ }
155
+
156
+ module . exports = {
157
+ updateDevices : updateDevices
158
+ } ;
0 commit comments