Initial commit 🎉
fbshipit-source-id: b6fc29740c6875d2e78953b8a7123890a67930f2 Co-authored-by: Sebastian McKenzie <sebmck@fb.com> Co-authored-by: John Knox <jknox@fb.com> Co-authored-by: Emil Sjölander <emilsj@fb.com> Co-authored-by: Pritesh Nandgaonkar <prit91@fb.com>
This commit is contained in:
97
src/dispatcher/androidDevice.js
Normal file
97
src/dispatcher/androidDevice.js
Normal file
@@ -0,0 +1,97 @@
|
||||
/**
|
||||
* Copyright 2018-present Facebook.
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
* @format
|
||||
*/
|
||||
|
||||
import AndroidDevice from '../devices/AndroidDevice';
|
||||
import type {Store} from '../reducers/index.js';
|
||||
import type BaseDevice from '../devices/BaseDevice';
|
||||
const adb = require('adbkit-fb');
|
||||
|
||||
function createDecive(client, device): Promise<AndroidDevice> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const type =
|
||||
device.type !== 'device' || device.id.startsWith('emulator')
|
||||
? 'emulator'
|
||||
: 'physical';
|
||||
client.getProperties(device.id).then(props => {
|
||||
const androidDevice = new AndroidDevice(
|
||||
device.id,
|
||||
type,
|
||||
props['ro.product.model'],
|
||||
client,
|
||||
);
|
||||
androidDevice.reverse();
|
||||
resolve(androidDevice);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export default (store: Store) => {
|
||||
const client = adb.createClient();
|
||||
|
||||
client
|
||||
.trackDevices()
|
||||
.then(tracker => {
|
||||
tracker.on('error', err => {
|
||||
if (err.message === 'Connection closed') {
|
||||
// adb server has shutdown, remove all android devices
|
||||
const {devices} = store.getState();
|
||||
const deviceIDsToRemove: Array<string> = devices
|
||||
.filter((device: BaseDevice) => device instanceof AndroidDevice)
|
||||
.map((device: BaseDevice) => device.serial);
|
||||
|
||||
store.dispatch({
|
||||
type: 'UNREGISTER_DEVICES',
|
||||
payload: new Set(deviceIDsToRemove),
|
||||
});
|
||||
console.error(
|
||||
'adb server shutdown. Run `adb start-server` and restart Sonar.',
|
||||
);
|
||||
} else {
|
||||
throw err;
|
||||
}
|
||||
});
|
||||
|
||||
tracker.on('add', async device => {
|
||||
const androidDevice = await createDecive(client, device);
|
||||
if (device.type !== 'offline') {
|
||||
store.dispatch({
|
||||
type: 'REGISTER_DEVICE',
|
||||
payload: androidDevice,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
tracker.on('change', async device => {
|
||||
if (device.type === 'offline') {
|
||||
store.dispatch({
|
||||
type: 'UNREGISTER_DEVICES',
|
||||
payload: new Set([device.id]),
|
||||
});
|
||||
} else {
|
||||
const androidDevice = await createDecive(client, device);
|
||||
store.dispatch({
|
||||
type: 'REGISTER_DEVICE',
|
||||
payload: androidDevice,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
tracker.on('remove', device => {
|
||||
store.dispatch({
|
||||
type: 'UNREGISTER_DEVICES',
|
||||
payload: new Set([device.id]),
|
||||
});
|
||||
});
|
||||
})
|
||||
.catch(err => {
|
||||
if (err.code === 'ECONNREFUSED') {
|
||||
// adb server isn't running
|
||||
} else {
|
||||
throw err;
|
||||
}
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user