getOperatingSystemData function

OperatingSystemData getOperatingSystemData({
  1. bool isThrowOnError = false,
})

Implementation

OperatingSystemData getOperatingSystemData({
  bool isThrowOnError = false,
}) {
  try {
    if (Dart.isAndroid) {
      final Map osData = {
        "@type": "osData",
        "name": "",
      };
      String os = "Android";

      try {
        os += " ${Process.runSync("getprop", [
              "ro.build.version.release",
            ]).stdout.toString().trim()}";
      } catch (e) {}
      try {
        os += " ${Process.runSync("getprop", [
              "ro.product.name",
            ]).stdout.toString().trim()}";
      } catch (e) {}
      if (os.isEmpty) {
        os = Platform.operatingSystemVersion;
      }
      osData["name"] = os;
      return OperatingSystemData(osData);
    }
    if (Dart.isLinux) {
      final Map osData = {
        "@type": "osData",
        "name": "",
      };

      final String os_release = File("/etc/os-release").readAsStringSync();
      final lines = os_release.split("\n");
      final variables = <String, String>{};

      for (var line in lines) {
        final split = line.split("=");
        if (split.length == 2) {
          variables[split[0]] = split[1];
        }
      }
      String os = variables["PRETTY_NAME"] ??
          variables["NAME"] ??
          variables["ID"] ??
          "";
      if (os.startsWith('"')) {
        os = os.substring(1, os.length - 1);
      }

      if (os.isEmpty) {
        os = File("/proc/version_signature").readAsStringSync().trim();
      }
      os = os.trim();
      if (os.isEmpty) {
        os = Platform.operatingSystemVersion;
      }
      osData["name"] = os;
      return OperatingSystemData(osData);
    }
  } catch (e) {
    // e.printPretty();
    if (isThrowOnError) {
      rethrow;
    }
  }

  return OperatingSystemData({});
}