From 4d303eb4f61f4ae41fdb1299e8c14475adcb6683 Mon Sep 17 00:00:00 2001 From: Alexey Date: Mon, 15 Jan 2018 17:18:27 +0300 Subject: [PATCH] Device refactoring (#98) * Start refactoring: use common object for all platforms * Implement Device backend for web platform * Implement device backend for android * Move location implementation to considered backend * Fix Location declaration * Fix build * Move 'location.js' to 'html' folder * fixed typo --- core/Device.qml | 25 +++++++++++++++++ core/Location.qml | 49 +++++++++++++++++++++++++++++++++ platform/android/.core.js | 2 ++ platform/android/Device.qml | 10 ------- platform/android/device.js | 9 +++++++ platform/html5/.core.js | 1 + platform/html5/Location.qml | 54 ------------------------------------- platform/html5/location.js | 47 ++++++++++++++++++++++++++++++++ platform/pure/Location.qml | 13 --------- platform/web/.core.js | 2 ++ platform/web/Device.qml | 11 -------- platform/web/device.js | 12 +++++++++ 12 files changed, 147 insertions(+), 88 deletions(-) create mode 100644 core/Device.qml create mode 100644 core/Location.qml delete mode 100644 platform/android/Device.qml create mode 100644 platform/android/device.js delete mode 100644 platform/html5/Location.qml create mode 100644 platform/html5/location.js delete mode 100644 platform/pure/Location.qml delete mode 100644 platform/web/Device.qml create mode 100644 platform/web/device.js diff --git a/core/Device.qml b/core/Device.qml new file mode 100644 index 000000000..e9081cbcb --- /dev/null +++ b/core/Device.qml @@ -0,0 +1,25 @@ +Object { + signal propertyUpdated; + property string macAddess; + property string modelName; + property string deviceId; + property string firmware; + property string sdk; + property bool supportingUhd; + property bool supporting3d; + + constructor: { + var backend = _globals.core.__deviceBackend + if (!backend) + throw new Error('no backend found') + backend().createDevice(this) + } + + onSdkChanged, + onDeviceIdChanged, + onFirmwareChanged, + onMacAddessChanged, + onModelNameChanged, + onSupporting3dChanged, + onSupportingUhdChanged: { this.propertyUpdated() } +} diff --git a/core/Location.qml b/core/Location.qml new file mode 100644 index 000000000..ca7eec9db --- /dev/null +++ b/core/Location.qml @@ -0,0 +1,49 @@ +/// Window location object +Object { + property string hash; ///< contains current hash value (after '#' charachter) + property string host; ///< current host with port number + property string href; ///< whole current URL + property string port; ///< current port number + property string origin; ///< current protocol, hostname and port number of a URL + property string hostname; ///< current host name + property string pathname; ///< path name of the current URL + property string protocol; ///< current protocol + property string search; ///< query string of the URL + property Object state; ///< current history state + + ///@private + constructor: { + this.impl = null + this._createLocation() + } + + ///@private + function _getLocation() { + if (this.impl === null) + this._createPlayer() + return this.impl + } + + ///@private + function _createLocation() { + if (this.impl) + return this.impl + + var backend = _globals.core.__locationBackend + if (!backend) + throw new Error('no backend found') + return this.impl = backend().createLocation(this) + } + + pushState(state, title, url): { + var location = this._getLocation() + if (location) + player.pushState(state, title, url) + } + + changeHref(href): { + var location = this._getLocation() + if (location) + player.changeHref(href) + } +} diff --git a/platform/android/.core.js b/platform/android/.core.js index 542dfd9d5..4352f3c78 100644 --- a/platform/android/.core.js +++ b/platform/android/.core.js @@ -1,4 +1,6 @@ if (navigator.userAgent.indexOf('Android') >= 0) { + _globals.core.__deviceBackend = function() { return _globals.android.device } + log = console.log.bind(console) log("Android detected") diff --git a/platform/android/Device.qml b/platform/android/Device.qml deleted file mode 100644 index 1efbc60e7..000000000 --- a/platform/android/Device.qml +++ /dev/null @@ -1,10 +0,0 @@ -Object { - property bool macAccessable: false; - property string modelName: "androidtv"; - - getDeviceId(callback): { - callback(callback("android" + Math.random().toString(36).substr(2, 9))) - } - - getMacAddress(callback): { callback("") } -} diff --git a/platform/android/device.js b/platform/android/device.js new file mode 100644 index 000000000..2981e4cc8 --- /dev/null +++ b/platform/android/device.js @@ -0,0 +1,9 @@ +var Device = function(ui) { + ui.deviceId = "android_" + Math.random().toString(36).substr(2, 9) +} + +exports.createDevice = function(ui) { + return new Device(ui) +} + +exports.Device = Device diff --git a/platform/html5/.core.js b/platform/html5/.core.js index 488551806..2847f4bff 100644 --- a/platform/html5/.core.js +++ b/platform/html5/.core.js @@ -46,3 +46,4 @@ else _globals._backend = function() { return _globals.html5.html } +_globals.core.__locationBackend = function() { return _globals.html5.location } diff --git a/platform/html5/Location.qml b/platform/html5/Location.qml deleted file mode 100644 index 5e5ef9a7a..000000000 --- a/platform/html5/Location.qml +++ /dev/null @@ -1,54 +0,0 @@ -/// Window location object -Object { - property string hash; ///< contains current hash value (after '#' charachter) - property string host; ///< current host with port number - property string href; ///< whole current URL - property string port; ///< current port number - property string origin; ///< current protocol, hostname and port number of a URL - property string hostname; ///< current host name - property string pathname; ///< path name of the current URL - property string protocol; ///< current protocol - property string search; ///< query string of the URL - property Object state; ///< current history state - - /// @private - onCompleted: { - var location = window.location - this.updateActualValues() - var self = this - var context = this._context - context.window.on("hashchange", function() { self.hash = location.hash }.bind(this)) - context.window.on("popstate", function() { self.updateActualValues() }.bind(this)) - } - - /// @private - updateActualValues: { - this.hash = window.location.hash - this.href = window.location.href - this.port = window.location.port - this.host = window.location.host - this.origin = window.location.origin - this.hostname = window.location.hostname - this.pathname = window.location.pathname - this.protocol = window.location.protocol - this.search = window.location.search - this.state = window.history.state - } - - ///change current href value method, argument is new href value - changeHref(href): { - window.location.href = href - this.updateActualValues() - } - - ///push new state to the history - pushState(state, title, url): { - if (window.location.hostname) { - window.history.pushState(state, title, url) - this.updateActualValues() - } else { - document.title = title - this.state = state - } - } -} diff --git a/platform/html5/location.js b/platform/html5/location.js new file mode 100644 index 000000000..6ac56005f --- /dev/null +++ b/platform/html5/location.js @@ -0,0 +1,47 @@ +var Location = function(ui) { + this._ui = ui + var location = window.location + this.updateActualValues() + var self = this + var context = ui._context + context.window.on("hashchange", function() { self._ui.hash = location.hash }.bind(this)) + context.window.on("popstate", function() { self.updateActualValues() }.bind(this)) +} + +Location.prototype.updateActualValues = function() { + var ui = this._ui + var windowContext = ui._context.window.dom + ui.hash = windowContext.location.hash + ui.href = windowContext.location.href + ui.port = windowContext.location.port + ui.host = windowContext.location.host + ui.origin = windowContext.location.origin + ui.hostname = windowContext.location.hostname + ui.pathname = windowContext.location.pathname + ui.protocol = windowContext.location.protocol + ui.search = windowContext.location.search + ui.state = windowContext.history.state +} + +Location.prototype.changeHref = function(href) { + this._ui._context.window.dom.location.href = href + this.updateActualValues() +} + +Location.prototype.pushState = function(state, title, url) { + var ui = this._ui + var windowContext = ui._context.window.dom + if (windowContext.location.hostname) { + windowContext.history.pushState(state, title, url) + this.updateActualValues() + } else { + ui._context.document.title = title + this.state = state + } +} + +exports.createLocation = function(ui) { + return new Location(ui) +} + +exports.Location = Location diff --git a/platform/pure/Location.qml b/platform/pure/Location.qml deleted file mode 100644 index 601279100..000000000 --- a/platform/pure/Location.qml +++ /dev/null @@ -1,13 +0,0 @@ -/// Window location object -Object { - property string hash; ///< contains current hash value (after '#' charachter) - property string host; ///< current host with port number - property string href; ///< whole current URL - property string port; ///< current port number - property string origin; ///< current protocol, hostname and port number of a URL - property string hostname; ///< current host name - property string pathname; ///< path name of the current URL - property string protocol; ///< current protocol - property string search; ///< query string of the URL - property Object state; ///< current history state -} diff --git a/platform/web/.core.js b/platform/web/.core.js index e60638efd..46ed2922c 100644 --- a/platform/web/.core.js +++ b/platform/web/.core.js @@ -1,3 +1,5 @@ +_globals.core.__deviceBackend = function() { return _globals.web.device } + exports.core.keyCodes = { 13: 'Select', 16: 'Shift', diff --git a/platform/web/Device.qml b/platform/web/Device.qml deleted file mode 100644 index 1e92cf3ac..000000000 --- a/platform/web/Device.qml +++ /dev/null @@ -1,11 +0,0 @@ -Object { - property bool macAccessable: false; - - getDeviceId(callback): { - var deviceString = this._context.system.os + "_" + this._context.system.browser - deviceString = deviceString.replace(/\s/g, '') - callback(deviceString) - } - - getMacAddress(callback): { callback("") } -} diff --git a/platform/web/device.js b/platform/web/device.js new file mode 100644 index 000000000..5a570c728 --- /dev/null +++ b/platform/web/device.js @@ -0,0 +1,12 @@ +var Device = function(ui) { + var context = ui._context + var deviceString = context.system.os + "_" + context.system.browser + deviceString = deviceString.replace(/\s/g, '') + ui.deviceId = deviceString + "_" + Math.random().toString(36).substr(2, 9) +} + +exports.createDevice = function(ui) { + return new Device(ui) +} + +exports.Device = Device