initEngine method

Future initEngine(
  1. EngineListener listener
)

Implementation

Future<dynamic> initEngine(EngineListener listener) async {
  _startTimeMonitor.startEvent(EngineMonitorEventKey.engineLoadEventInitEngine);
  try {
    // 初始化UI宽高信息, 必须放到第一位,否则可能run app之后没有UI界面宽高信息
    LogUtils.d(_kTag, "init screen info start");
    await ScreenUtil.getInstance().initScreen(
        screenInfoSource: _integratedMode == IntegratedMode.flutterApp
            ? ScreenInfoSource.flutter
            : ScreenInfoSource.native);
    LogUtils.d(_kTag, "init screen info done");
  } catch (e) {
    _currentState = EngineState.initError;
    if (e is Error) {
      LogUtils.e(_kTag, "init screen info fail, error:${e.stackTrace}");
    }
  }

  try {
    // 初始化平台相关信息和UI宽高信息, 必须放到第一位,否则可能run app之后平台信息还未初始化完成,或者没有UI界面宽高信息
    LogUtils.d(_kTag, "init platform info start");
    await PlatformManager.getInstance().initPlatform();
    LogUtils.d(_kTag, "init platform info done");
  } catch (e) {
    _currentState = EngineState.initError;
    if (e is Error) {
      LogUtils.e(_kTag, "init platform info fail, error:${e.stackTrace}");
    }
  }

  if (_currentState != EngineState.unInit) {
    _listen(listener);
    return;
  }

  _currentState = EngineState.onInit;
  _eventListenerList.add(listener);

  _globalConfigs.monitorAdapter?.reportEngineLoadStart();

  try {
    _devSupportManager = DevSupportManager(
      _globalConfigs,
      _debugMode,
      _serverHost,
      _serverBundleName,
      _remoteServerUrl,
    );
    _devSupportManager.setDevCallback(this);
    if (_debugMode) {
      String url = _devSupportManager.createResourceUrl(_serverBundleName);
      _coreBundleLoader = HttpBundleLoader(url);
      (_coreBundleLoader as HttpBundleLoader).setIsDebugMode(_debugMode);
    }
    LogUtils.d(_kTag, "start restartEngineInBackground...");
    await _restartEngineInBackground(false);
  } catch (e) {
    _currentState = EngineState.initError;
    if (e is Error) {
      LogUtils.e(_kTag, "${e.stackTrace}");
    }
    _notifyEngineInitialized(
      EngineInitStatus.initException,
      StateError(e.toString()),
    );
  }
}