Skip to content

Commit

Permalink
fix(android): more accurate device/emulator detection
Browse files Browse the repository at this point in the history
  • Loading branch information
imhoffd committed Jun 25, 2019
1 parent dc39f8c commit 5ec454b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
38 changes: 32 additions & 6 deletions src/android/utils/__tests__/adb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ emulator-5554 device product:sdk_gphone_x86 model:Android_SDK_built_for
connection: null,
manufacturer: '',
model: 'Android_SDK_built_for_x86',
product: '',
product: 'sdk_gphone_x86',
sdkVersion: '',
properties: {
product: 'sdk_gphone_x86',
Expand All @@ -51,7 +51,7 @@ LGUS996e5ef677 device usb:341835776X product:elsa_nao_us model:LG_US996
connection: 'usb',
manufacturer: '',
model: 'LG_US996',
product: '',
product: 'elsa_nao_us',
sdkVersion: '',
properties: {
usb: '341835776X',
Expand All @@ -78,7 +78,7 @@ List of devices attached
connection: 'usb',
manufacturer: '',
model: 'Nexus_7',
product: '',
product: 'razor',
sdkVersion: '',
properties: {
usb: '1-1',
Expand All @@ -90,7 +90,7 @@ List of devices attached
]);
});

it('should parse hardware device over tcpip ()', async () => {
it('should parse hardware device over tcpip (192.168.0.3:5555)', async () => {
const output = `
List of devices attached
192.168.0.3:5555 device product:mido model:Redmi_Note_4 device:mido transport_id:1\n\n`;
Expand All @@ -104,7 +104,7 @@ List of devices attached
connection: 'tcpip',
manufacturer: '',
model: 'Redmi_Note_4',
product: '',
product: 'mido',
sdkVersion: '',
properties: {
device: 'mido',
Expand All @@ -116,6 +116,32 @@ List of devices attached
]);
});

it('should parse hardware device from line without usb (98897a474748594558)', async () => {
const output = `
List of devices attached
98897a474748594558 device product:dreamqltesq model:SM_G950U device:dreamqltesq transport_id:2\n\n`;
const devices = adbUtils.parseAdbDevices(output);

expect(devices).toEqual([
{
serial: '98897a474748594558',
state: 'device',
type: 'hardware',
connection: null,
manufacturer: '',
model: 'SM_G950U',
product: 'dreamqltesq',
sdkVersion: '',
properties: {
device: 'dreamqltesq',
product: 'dreamqltesq',
model: 'SM_G950U',
transport_id: '2',
},
},
]);
});

describe('windows', () => {

let adbUtils: typeof import('../adb');
Expand All @@ -135,7 +161,7 @@ List of devices attached
{
serial: 'MWS0216B24001482',
state: 'offline',
type: 'emulator',
type: 'hardware',
connection: null,
manufacturer: '',
model: '',
Expand Down
9 changes: 5 additions & 4 deletions src/android/utils/adb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,17 +271,18 @@ export function parseAdbDevices(output: string): Device[] {
const properties = description
.split(/\s+/)
.map(prop => prop.includes(':') ? prop.split(':') : undefined)
.filter((kv: any): kv is [string, string] => typeof kv !== 'undefined' && typeof kv[0] === 'string' && typeof kv[1] === 'string')
.filter((kv): kv is [string, string] => typeof kv !== 'undefined' && kv.length >= 2)
.reduce((acc, [ k, v ]) => {
if (k && v) {
acc[k.trim()] = v.trim();
}

return acc;
}, {} as { [key: string]: string; });
}, {} as { [key: string]: string | undefined; });

const isIP = !!serial.match(ipRe);
const type = 'usb' in properties || isIP ? 'hardware' : 'emulator';
const isGenericDevice = (properties['device'] || '').startsWith('generic');
const type = 'usb' in properties || isIP || !serial.startsWith('emulator') || !isGenericDevice ? 'hardware' : 'emulator';
const connection = 'usb' in properties ? 'usb' : isIP ? 'tcpip' : null;

devices.push({
Expand All @@ -293,7 +294,7 @@ export function parseAdbDevices(output: string): Device[] {
// We might not know these yet
manufacturer: '',
model: properties['model'] || '',
product: '',
product: properties['product'] || '',
sdkVersion: '',
});
} else {
Expand Down

0 comments on commit 5ec454b

Please sign in to comment.