UNPKG

@applicaster/zapp-react-dom-app

Version:

Zapp App Component for Applicaster's Quick Brick React Native App

317 lines (271 loc) • 7.63 kB
// @flow /* global webOS */ /* global tizen */ /* eslint-disable no-console */ import { Platform } from "react-native"; import { DEFAULT, desiredKeysMap, hasTizen, hasWebOS, PLATFORMS, } from "./const"; export const getUserAgent = () => window?.navigator?.userAgent || global?.navigator?.userAgent; export const isHD = () => window.innerWidth === 1280; export const isLgPlatform = () => { const userAgent = getUserAgent(); if (!userAgent) { return false; } // Note: LG uses the number 0 in their userAgent, possibly because of Palm WebOS return hasWebOS || userAgent.includes(PLATFORMS.webos); }; export const isSamsungPlatform = () => { const userAgent = getUserAgent(); if (!userAgent) { return false; } return ( hasTizen || userAgent.includes(PLATFORMS.samsung) || userAgent.includes(PLATFORMS.tizen) ); }; /** * Checks if we are on the Vizio platform by using the userAgent string * * Use this method when you need to identify the platform on runtime * before the VIZIO Companion Library has loaded */ export const isVizioPlatform = () => { const userAgent = getUserAgent(); const isVizioAgent = userAgent.includes(PLATFORMS.vizio) || userAgent.includes(PLATFORMS.smartcast) || userAgent.includes(PLATFORMS.conjure); return isVizioAgent; }; /** * Checks if we are on the Vizio platform by checking for the global VIZIO object * and the VIZIO Companion Library. This method does not use the userAgent string, * because it mainly checks whether we have access to apis, which may not be available yet */ export const hasVizioAPIs = () => { const hasAPIs = typeof window.VIZIO !== "undefined" && window?.applicaster?.vizioLibraryDidLoad; return hasAPIs; }; /** * platformSelect can't be used, because Platform.OS is overriden * by overloadReactNativePlatform and don't support 'web' value * @returns {"tv" | "web" | "other"} value */ export const getDeviceType = () => { switch (Platform.OS) { case "samsung_tv": return isSamsungPlatform() ? "tv" : "web"; case "lg_tv": return isLgPlatform() ? "tv" : "web"; default: return "other"; } }; /** * This function is used to rename keys * @param {string} key - key you would like to rename */ export const renameKeys = (key) => desiredKeysMap?.[key] || key; /** * This function is used to get the connection info from WebOS * TODO: move to connection hook */ export const getWebOSConnectionInfo = () => { return new Promise((resolve, reject) => { if (!isLgPlatform()) { resolve(null); return; } webOS.service.request("luna://com.palm.connectionmanager", { method: "getStatus", parameters: { subscribe: false }, onSuccess: (args) => { const types = Object.keys(args) .filter((key) => args[key].state === "connected") .join(); resolve({ networkType: types }); }, onFailure: (error) => { console.error( "getWebOSConnectionInfo: There was an error getting connection info", error ); reject(error); }, }); }); }; export const getTizenConnectionInfo = () => { return new Promise((resolve, reject) => { if (!isSamsungPlatform) { resolve(null); return; } tizen.systeminfo.getPropertyValue( "NETWORK", (networkInfo) => { const networkType = networkInfo.networkType; resolve({ networkType }); }, (error) => { console.error( "getTizenConnectionInfo: There was an error getting connection info", error ); reject(error); } ); }); }; /** * Gets all of the webOS device info available to us via webOS.deviceInfo */ export const getWebOSInfo = () => { return new Promise((resolve, reject) => { if (!isLgPlatform()) { resolve(null); return; } webOS.deviceInfo((info) => { if (info) { resolve({ ...info, name: `${info.modelName} - ${info.version}`, osVersion: info.sdkVersion, // info.sdkVersion is the OS version, info.version is software version }); } else { reject(new Error("Unable to retrieve WebOS device info")); } }); }); }; /** * Invokes the getInfo method which calls the native tizen apis * i.e. https://www.tizen.org/system/tizenid */ export const getTizenInfo = () => { return new Promise((resolve, reject) => { if (!isSamsungPlatform()) { resolve(null); return; } try { const modelName = tizen.systeminfo.getCapability( "http://tizen.org/system/model_name" ); const name = `${DEFAULT.make} ${modelName}`; const osVersion = tizen.systeminfo.getCapability( "http://tizen.org/feature/platform.version" ); if (modelName && name && osVersion) { resolve({ modelName, name, osVersion, }); } else { reject(new Error("Unable to retrieve Tizen device info")); } } catch (error) { console.error( "getTizenInfo: There was an error getting Tizen device info", error ); reject(error); } }); }; /** * Gets device data from either Tizen device, WebOS device, or browser * Pulls keys from the native Tizen device using Tizen systeminfo * on tizen getCapability requires a url to the property permission * We could only retreive keys we have requested permission for in config.xml * https://www.tizen.org/feature * * Pulls keys from the native webOS device using webOSTV.js lib * https://webostv.developer.lge.com/api/webostvjs/webos/ * * Connection methods could take a while to return */ export const getDeviceData = async () => { try { let deviceData = { modelName: null, version: null, versionMajor: null, versionMinor: null, versionDot: null, sdkVersion: null, screenWidth: null, screenHeight: null, uhd: null, oled: null, ddrSize: null, uhd8K: null, hdr10: null, dolbyVision: null, dolbyAtmos: null, name: null, osVersion: null, networkType: null, }; if (isLgPlatform()) { const [webOSInfo, webOSConnectionInfo] = await Promise.all([ getWebOSInfo(), getWebOSConnectionInfo(), ]); deviceData = { platform: "lg_tv", ...webOSInfo, ...webOSConnectionInfo, device_type: getDeviceType(), }; } else if (isSamsungPlatform()) { const [tizenInfo, tizenConnectionInfo] = await Promise.all([ getTizenInfo(), getTizenConnectionInfo(), ]); deviceData = { platform: "samsung_tv", ...tizenInfo, ...tizenConnectionInfo, device_type: getDeviceType(), }; } else if (isVizioPlatform()) { deviceData = { ...deviceData, platform: "vizio", deviceMake: "Vizio", deviceType: "tv", userAgent: getUserAgent(), deviceWidth: window.innerWidth, deviceHeight: window.innerHeight, }; if (hasVizioAPIs()) { window.VIZIO.getFirmwareVersion(function (firmwareVersion) { deviceData.osVersion = firmwareVersion; }); deviceData.deviceModel = window.VIZIO.deviceModel; } } return deviceData; } catch (error) { // eslint-disable-next-line no-console console.error( "getDeviceData: There was an error retrieving device data", error ); return null; } };