UNPKG

@swrve/smarttv-sdk

Version:

Swrve marketing engagement platform SDK for SmartTV OTT devices

245 lines (215 loc) 7.82 kB
import BasePlatform from "./base"; import { IPlatformName, DevicePropertyName, NetworkMonitorHandle, NETWORK_DISCONNECTED, NETWORK_CONNECTED, } from "./IPlatform"; import { IKeyMapping } from "./IKeymapping"; import SwrveLogger from "../SwrveLogger"; import ISwrveConfig from "../../Config/ISwrveConfig"; /** * Samsung platform */ export default class SamsungPlatform extends BasePlatform { protected tizenKeyMapping: IKeyMapping = { 38: "Up", 40: "Down", 37: "Left", 39: "Right", 13: "Enter", 403: "ColorF0Red", 404: "B", 405: "C", 406: "D", 10009: "Back", 415: "Play", 417: "FastForward", 412: "Rewind", 413: "Stop", 10252: "PlayPause", 19: "Pause", 65376: "Done", 65385: "Cancel", }; constructor(config?: Readonly<ISwrveConfig>) { super(); // this platform does not need a proxy. this.needsProxy = false; if (config) { this.config = config; if (config.customKeyMappingTizen) { this.tizenKeyMapping = config.customKeyMappingTizen; } } } public name(): IPlatformName { if (this.config?.customDeviceName) { return this.config?.customDeviceName; } else { return { name: "Samsung", variation: "Tizen", }; } } public exit(toMenu: boolean = false): void { if (toMenu) { tizen.application.getCurrentApplication().hide(); } else { tizen.application.getCurrentApplication().exit(); } } public init(deviceProperties: ReadonlyArray<DevicePropertyName> = []): Promise<void> { super.init(deviceProperties); tizen.tvinputdevice.registerKey("0"); tizen.tvinputdevice.registerKey("1"); tizen.tvinputdevice.registerKey("2"); tizen.tvinputdevice.registerKey("3"); tizen.tvinputdevice.registerKey("4"); tizen.tvinputdevice.registerKey("5"); tizen.tvinputdevice.registerKey("6"); tizen.tvinputdevice.registerKey("7"); tizen.tvinputdevice.registerKey("8"); tizen.tvinputdevice.registerKey("9"); tizen.tvinputdevice.registerKey("MediaPlay"); tizen.tvinputdevice.registerKey("MediaPause"); tizen.tvinputdevice.registerKey("MediaFastForward"); tizen.tvinputdevice.registerKey("MediaRewind"); tizen.tvinputdevice.registerKey("MediaStop"); tizen.tvinputdevice.registerKey("ColorF0Red"); return deviceProperties.reduce( (promise, property) => promise.then(() => this.getDeviceProperty(property)), Promise.resolve(), ); } public disableScreenSaver(): void { if (typeof webapis !== "undefined") { webapis.appcommon.setScreenSaver(webapis.appcommon.AppCommonScreenSaverState.SCREEN_SAVER_OFF); } else { SwrveLogger.error("Tizen TV webapis are not loaded can not disable screensaver"); } } public get firmware(): string { if (this._firmware === undefined) { this._firmware = webapis.productinfo.getFirmware(); } return this._firmware || ""; } public get model(): string { if (this._model === undefined) { this._model = webapis.productinfo.getRealModel(); } return this._model || ""; } public get os(): string { return this.config?.customOS ?? "tizen"; } public get osVersion(): string { if (this.config?.customOSVersion) { return this.config?.customOSVersion; } else { if (this._osVersion === undefined) { this._osVersion = tizen.systeminfo.getCapability("http://tizen.org/feature/platform.version"); } return this._osVersion || ""; } } public get language(): string { return this._language || ""; } public get countryCode(): string { return this._countryCode || ""; } public get appStore(): string { return this.config?.customAppStore ?? "tizen"; } public openLink(link: string): void { const appControl = new tizen.ApplicationControl("http://tizen.org/appcontrol/operation/view", link); tizen.application.launchAppControl( appControl, null, () => { SwrveLogger.info("launch application control succeed"); }, (e) => { SwrveLogger.error("launch application control failed. reason: " + e.message); }, ); } public getLanguage(): Promise<string> { if (this._language === undefined) { return this.loadLocale().then(() => this._language || ""); } return Promise.resolve(this._language || ""); } public getCountryCode(): Promise<string> { if (this._countryCode === undefined) { return this.loadLocale().then(() => this._countryCode || ""); } return Promise.resolve(this._countryCode || ""); } public getScreenHeight(): Promise<number> { if (this._screenHeight === undefined) { return this.loadDisplay().then(() => this._screenHeight || 1080); } return Promise.resolve(this._screenHeight || 1080); } public getScreenWidth(): Promise<number> { if (this._screenWidth === undefined) { return this.loadDisplay().then(() => this._screenWidth || 1920); } return Promise.resolve(this._screenWidth || 1920); } public getKeymapping(): IKeyMapping { return this.tizenKeyMapping; } public supportsHDR(): boolean { return webapis.avinfo.isHdrTvSupport(); } protected initNetworkListener(): NetworkMonitorHandle { return webapis.network.addNetworkStateChangeListener((value) => { if (value === webapis.network.NetworkState.GATEWAY_DISCONNECTED) { this.triggerNetworkChange(NETWORK_DISCONNECTED); } else if (value === webapis.network.NetworkState.GATEWAY_CONNECTED) { this.triggerNetworkChange(NETWORK_CONNECTED); } }); } protected removeNetworkListener(handle: number): void { webapis.network.removeNetworkStateChangeListener(handle); } private getDeviceProperty(property: DevicePropertyName): Promise<any> | undefined { switch (property) { case "language": return this.getLanguage(); case "countryCode": return this.getCountryCode(); case "deviceHeight": return this.getScreenHeight(); case "deviceWidth": return this.getScreenWidth(); default: return undefined; } } private loadLocale(): Promise<any> { return new Promise((resolve, reject) => tizen.systeminfo.getPropertyValue("LOCALE", (result) => { this._language = result.language.split("_")[0]; this._countryCode = result.country.replace(/\..*$/, "").split("_")[1].toLowerCase(); resolve(result); }), ); } private loadDisplay(): Promise<any> { return new Promise((resolve, reject) => tizen.systeminfo.getPropertyValue("DISPLAY", (result) => { this._screenHeight = (result.resolutionHeight > 0) ? result.resolutionHeight : 1080; this._screenWidth = (result.resolutionWidth > 0) ? result.resolutionWidth : 1920; resolve(result); }), ); } }