mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-19 03:32:21 +08:00
feat(native): device plugin
This commit is contained in:
@ -3,7 +3,7 @@ import {Control, ControlGroup} from 'angular2/forms';
|
|||||||
|
|
||||||
import {App, Http} from 'ionic/ionic';
|
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 testUrl = 'https://ionic-api-tester.herokuapp.com/json';
|
||||||
let testUrl404 = 'https://ionic-api-tester.herokuapp.com/404';
|
let testUrl404 = 'https://ionic-api-tester.herokuapp.com/404';
|
||||||
@ -15,6 +15,10 @@ let testUrl404 = 'https://ionic-api-tester.herokuapp.com/404';
|
|||||||
class IonicApp {
|
class IonicApp {
|
||||||
constructor() {
|
constructor() {
|
||||||
}
|
}
|
||||||
|
doDevice() {
|
||||||
|
let device = Device.getDevice();
|
||||||
|
console.log('Got device', device);
|
||||||
|
}
|
||||||
doGetLocation() {
|
doGetLocation() {
|
||||||
console.log('Getting location');
|
console.log('Getting location');
|
||||||
this.gettingLocation = true;
|
this.gettingLocation = true;
|
||||||
|
@ -1,5 +1,9 @@
|
|||||||
<ion-view>
|
<ion-view>
|
||||||
<ion-content padding>
|
<ion-content padding>
|
||||||
|
<h2>Device</h2>
|
||||||
|
<button primary outline (click)="doDevice()">Get Device</button>
|
||||||
|
<div>
|
||||||
|
</div>
|
||||||
<h2>Camera</h2>
|
<h2>Camera</h2>
|
||||||
<button primary outline (click)="getPicture()">Get Picture</button>
|
<button primary outline (click)="getPicture()">Get Picture</button>
|
||||||
<h2>Geolocation</h2>
|
<h2>Geolocation</h2>
|
||||||
@ -25,7 +29,6 @@
|
|||||||
</div>
|
</div>
|
||||||
<h2>Contacts</h2>
|
<h2>Contacts</h2>
|
||||||
<div>
|
<div>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
</ion-content>
|
</ion-content>
|
||||||
|
117
ionic/native/device/device.ts
Normal file
117
ionic/native/device/device.ts
Normal 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';
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
@ -3,13 +3,28 @@ export class NativePluginDecorator {
|
|||||||
this.cls = cls;
|
this.cls = cls;
|
||||||
this.config = config;
|
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 = () => {
|
cls.pluginWarn = () => {
|
||||||
let platformString = [];
|
let platformString = [];
|
||||||
for(var k in this.config.platforms) {
|
for(var k in this.config.platforms) {
|
||||||
platformString.push('\t' + k + ': '+ this.config.platforms[k]);
|
platformString.push('\t' + k + ': '+ this.config.platforms[k]);
|
||||||
}
|
}
|
||||||
console.warn('Plugin for ' + this.config.name +
|
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'));
|
platformString.join('\n'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,5 +2,6 @@ export * from './plugin'
|
|||||||
export * from './battery/battery'
|
export * from './battery/battery'
|
||||||
export * from './camera/camera'
|
export * from './camera/camera'
|
||||||
export * from './contacts/contacts'
|
export * from './contacts/contacts'
|
||||||
|
export * from './device/device'
|
||||||
export * from './geolocation/geolocation'
|
export * from './geolocation/geolocation'
|
||||||
export * from './vibration/vibration'
|
export * from './vibration/vibration'
|
||||||
|
Reference in New Issue
Block a user