diff --git a/ionic/components/app/test/native/index.ts b/ionic/components/app/test/native/index.ts index 474d23e509..894026b2be 100644 --- a/ionic/components/app/test/native/index.ts +++ b/ionic/components/app/test/native/index.ts @@ -3,7 +3,7 @@ import {Control, ControlGroup} from 'angular2/forms'; import {App, Http} from 'ionic/ionic'; -import {Camera, Geolocation, Vibration, Battery} from 'ionic/ionic'; +import {Camera, Geolocation, Vibration, Battery, Device} from 'ionic/ionic'; let testUrl = 'https://ionic-api-tester.herokuapp.com/json'; let testUrl404 = 'https://ionic-api-tester.herokuapp.com/404'; @@ -15,6 +15,10 @@ let testUrl404 = 'https://ionic-api-tester.herokuapp.com/404'; class IonicApp { constructor() { } + doDevice() { + let device = Device.getDevice(); + console.log('Got device', device); + } doGetLocation() { console.log('Getting location'); this.gettingLocation = true; diff --git a/ionic/components/app/test/native/main.html b/ionic/components/app/test/native/main.html index 48c2948f6b..820585f7c2 100644 --- a/ionic/components/app/test/native/main.html +++ b/ionic/components/app/test/native/main.html @@ -1,5 +1,9 @@ +

Device

+ +
+

Camera

Geolocation

@@ -25,7 +29,6 @@

Contacts

-
diff --git a/ionic/native/device/device.ts b/ionic/native/device/device.ts new file mode 100644 index 0000000000..f1ff8c3f61 --- /dev/null +++ b/ionic/native/device/device.ts @@ -0,0 +1,117 @@ +import * as Rx from 'rx'; + +import * as util from 'ionic/util'; +import {NativePlugin} from '../plugin'; +import {Platform} from '../../platform/platform'; + +@NativePlugin({ + name: 'Device', + platforms: { + cordova: 'cordova-plugin-device' + } +}) +export class Device { + /** + * Returns the whole device object. + * @see https://github.com/apache/cordova-plugin-device + * @returns {Object} The device object. + */ + static getDevice() { + return this.ifPlugin(window.device, () => { + return device; + }, () => { + return { + name: Platform.platforms().join(',') + } + }); + } + + /** + * Returns the Cordova version. + * @see https://github.com/apache/cordova-plugin-device#devicecordova + * @returns {String} The Cordova version. + */ + static getCordova() { + this.ifPlugin(window.device, () => { + return device.cordova; + }); + } + + /** + * Returns the name of the device's model or product. + * @see https://github.com/apache/cordova-plugin-device#devicemodel + * @returns {String} The name of the device's model or product. + */ + static getModel() { + this.ifPlugin(window.device, () => { + return device.model; + }, () => { + return 'unknown' + }); + } + + /** + * @deprecated device.name is deprecated as of version 2.3.0. Use device.model instead. + * @returns {String} + */ + static getName() { + this.ifPlugin(window.device, () => { + return device.name; + }, () => { + return 'unknown' + }); + } + + /** + * Returns the device's operating system name. + * @see https://github.com/apache/cordova-plugin-device#deviceplatform + * @returns {String} The device's operating system name. + */ + static getPlatform() { + this.ifPlugin(window.device, () => { + return device.name; + }, () => { + return { + name: Platform.name() + } + }); + } + + /** + * Returns the device's Universally Unique Identifier. + * @see https://github.com/apache/cordova-plugin-device#deviceuuid + * @returns {String} The device's Universally Unique Identifier + */ + static getUUID() { + this.ifPlugin(window.device, () => { + return device.uuid; + }, () => { + return 'unknown'; + }); + } + + /** + * Returns the operating system version. + * @see https://github.com/apache/cordova-plugin-device#deviceversion + * @returns {String} + */ + static getVersion() { + this.ifPlugin(window.device, () => { + return device.version; + }, () => { + return 'unknown'; + }); + } + + /** + * Returns the device manufacturer. + * @returns {String} + */ + static getManufacturer() { + this.ifPlugin(window.device, () => { + return device.manufacturer; + }, () => { + return 'unknown'; + }); + } +} diff --git a/ionic/native/plugin.ts b/ionic/native/plugin.ts index b506519f1b..9b37805a92 100644 --- a/ionic/native/plugin.ts +++ b/ionic/native/plugin.ts @@ -3,13 +3,28 @@ export class NativePluginDecorator { this.cls = cls; this.config = config; + cls.ifPlugin = (check, cb, returnType=null) => { + // Convert to boolean the plugin param + var exists = !!check; + if(typeof check === 'function') { + exists = check(); + } + if(exists) { + return cb(); + } + + cls.pluginWarn(); + + return (typeof returnType === 'function') ? returnType() : returnType; + }; + cls.pluginWarn = () => { let platformString = []; for(var k in this.config.platforms) { platformString.push('\t' + k + ': '+ this.config.platforms[k]); } console.warn('Plugin for ' + this.config.name + - ' not installed. For native functionality, please instead the correct plugin for your platform:\n' + + ' not installed. For native functionality, please install the correct plugin for your platform:\n' + platformString.join('\n')); } } diff --git a/ionic/native/plugins.ts b/ionic/native/plugins.ts index 975d2815ce..0987e69131 100644 --- a/ionic/native/plugins.ts +++ b/ionic/native/plugins.ts @@ -2,5 +2,6 @@ export * from './plugin' export * from './battery/battery' export * from './camera/camera' export * from './contacts/contacts' +export * from './device/device' export * from './geolocation/geolocation' export * from './vibration/vibration'