feat(native): device plugin

This commit is contained in:
Max Lynch
2015-09-08 14:39:56 -05:00
parent 1b1d91661c
commit e2c01f4b12
5 changed files with 143 additions and 3 deletions

View File

@ -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;

View File

@ -1,5 +1,9 @@
<ion-view>
<ion-content padding>
<h2>Device</h2>
<button primary outline (click)="doDevice()">Get Device</button>
<div>
</div>
<h2>Camera</h2>
<button primary outline (click)="getPicture()">Get Picture</button>
<h2>Geolocation</h2>
@ -25,7 +29,6 @@
</div>
<h2>Contacts</h2>
<div>
</div>
</ion-content>

View File

@ -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';
});
}
}

View File

@ -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'));
}
}

View File

@ -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'