UNPKG

@applicaster/zapp-react-dom-app

Version:

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

336 lines (292 loc) • 8.15 kB
/* global webOS */ /* global tizen */ /* eslint-disable no-console */ import { Platform } from "react-native"; import { DEFAULT, desiredKeysMap, hasTizen, hasWebOS, PLATFORMS, } from "./const"; /** * @typedef {"tv" | "web" | "other"} DeviceType * @typedef {"samsung_tv" | "lg_tv" | unknown} CustomPlatformTypes */ /** * Retrieves the user agent string from the browser or global navigator. * @returns {string} The user agent string. */ export const getUserAgent = () => window?.navigator?.userAgent || global?.navigator?.userAgent; /** * Checks if the device is HD (1280 width). * @returns {boolean} True if the device is HD, false otherwise. */ export const isHD = () => window.innerWidth === 1280; /** * Checks if the platform is LG. * @returns {boolean} True if the platform is LG, false otherwise. */ export const isLgPlatform = () => { const userAgent = getUserAgent(); if (!userAgent) { return false; } return hasWebOS || userAgent.includes(PLATFORMS.webos); }; /** * Checks if the platform is Samsung. * @returns {boolean} True if the platform is Samsung, false otherwise. */ export const isSamsungPlatform = () => { const userAgent = getUserAgent(); if (!userAgent) { return false; } return ( hasTizen || userAgent.includes(PLATFORMS.samsung) || userAgent.includes(PLATFORMS.tizen) ); }; /** * Checks if the platform is Vizio by using the user agent string. * @returns {boolean} True if the platform is Vizio, false otherwise. */ export const isVizioPlatform = () => { const userAgent = getUserAgent(); return ( userAgent.includes(PLATFORMS.vizio) || userAgent.includes(PLATFORMS.smartcast) || userAgent.includes(PLATFORMS.conjure) ); }; /** * Checks if the Vizio APIs are available. * @returns {boolean} True if the Vizio APIs are available, false otherwise. */ export const hasVizioAPIs = () => { return ( typeof window.VIZIO !== "undefined" && window?.applicaster?.vizioLibraryDidLoad ); }; /** * Determines the device type based on the platform. * @returns {DeviceType} The device type ("tv", "web", or "other"). */ export const getDeviceType = () => { /** @type {CustomPlatformTypes} */ const platformOS = Platform.OS; switch (platformOS) { case "samsung_tv": return isSamsungPlatform() ? "tv" : "web"; case "lg_tv": return isLgPlatform() ? "tv" : "web"; default: return "other"; } }; /** * Renames keys based on a predefined map. * @param {string} key - The key to rename. * @returns {string} The renamed key. */ export const renameKeys = (key) => desiredKeysMap?.[key] || key; /** * Retrieves the connection info from WebOS. * @returns {Promise<{networkType: string} | null>} A promise that resolves with the connection info or null. */ 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); }, }); }); }; /** * Retrieves the connection info from Tizen. * @returns {Promise<{networkType: string} | null>} A promise that resolves with the connection info or null. */ 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; } };