private createDevice()

in desktop/flipper-server-core/src/devices/android/androidDeviceManager.tsx [29:128]


  private createDevice(
    adbClient: ADBClient,
    device: Device,
  ): Promise<AndroidDevice | undefined> {
    return new Promise(async (resolve, reject) => {
      const type =
        device.type !== 'device' || device.id.startsWith('emulator')
          ? 'emulator'
          : 'physical';

      try {
        const props = await adbClient.getProperties(device.id);
        try {
          let name = props['ro.product.model'];
          const abiString = props['ro.product.cpu.abilist'] || '';
          const sdkVersion = props['ro.build.version.sdk'] || '';
          const abiList = abiString.length > 0 ? abiString.split(',') : [];
          if (type === 'emulator') {
            name = (await this.getRunningEmulatorName(device.id)) || name;
          }
          const isKaiOSDevice = Object.keys(props).some(
            (name) => name.startsWith('kaios') || name.startsWith('ro.kaios'),
          );
          const androidLikeDevice = new (
            isKaiOSDevice ? KaiOSDevice : AndroidDevice
          )(
            this.flipperServer,
            device.id,
            type,
            name,
            adbClient,
            abiList,
            sdkVersion,
          );
          const ports = getServerPortsConfig();
          if (ports.serverPorts) {
            await androidLikeDevice
              .reverse([
                ports.serverPorts.secure,
                ports.serverPorts.insecure,
                ports.altServerPorts.secure,
                ports.altServerPorts.insecure,
              ])
              // We may not be able to establish a reverse connection, e.g. for old Android SDKs.
              // This is *generally* fine, because we hard-code the ports on the SDK side.
              .catch((e) => {
                console.warn(
                  `Failed to reverse-proxy ports on device ${androidLikeDevice.serial}: ${e}`,
                );
              });
          }
          if (type === 'physical') {
            // forward port for React DevTools, which is fixed on React Native
            await androidLikeDevice.reverse([8097]).catch((e) => {
              console.warn(
                `Failed to reverse-proxy React DevTools port 8097 on ${androidLikeDevice.serial}`,
                e,
              );
            });
          }

          // The default way of capturing screenshots through adb does not seem to work
          // There is a way of getting a screenshot through KaiOS dev tools though
          if (androidLikeDevice instanceof AndroidDevice) {
            const screenRecordAvailable =
              await androidLikeDevice.screenRecordAvailable();
            androidLikeDevice.info.features.screenCaptureAvailable =
              screenRecordAvailable;
            androidLikeDevice.info.features.screenshotAvailable =
              screenRecordAvailable;
          }

          resolve(androidLikeDevice);
        } catch (e) {
          reject(e);
        }
      } catch (e) {
        const message = `${e.message ?? e}`;
        if (
          message.includes('device still connecting') ||
          message.includes('device still authorizing')
        ) {
          console.log('[conn] Device still connecting: ' + device.id);
        } else {
          const isAuthorizationError = message.includes('device unauthorized');
          if (!isAuthorizationError) {
            console.error('Failed to connect to android device', e);
          }
          this.flipperServer.emit('notification', {
            type: 'error',
            title: 'Could not connect to ' + device.id,
            description: isAuthorizationError
              ? 'Make sure to authorize debugging on the phone'
              : 'Failed to setup connection: ' + message,
          });
        }
        resolve(undefined); // not ready yet, we will find it in the next tick
      }
    });
  }