diff --git a/ionic/components/app/test/native/index.ts b/ionic/components/app/test/native/index.ts index 5d4b9f1d51..474d23e509 100644 --- a/ionic/components/app/test/native/index.ts +++ b/ionic/components/app/test/native/index.ts @@ -1,7 +1,9 @@ import {Component} from 'angular2/angular2'; import {Control, ControlGroup} from 'angular2/forms'; -import {App, Http, Camera, Geolocation, Vibration} from 'ionic/ionic'; +import {App, Http} from 'ionic/ionic'; + +import {Camera, Geolocation, Vibration, Battery} from 'ionic/ionic'; let testUrl = 'https://ionic-api-tester.herokuapp.com/json'; let testUrl404 = 'https://ionic-api-tester.herokuapp.com/404'; @@ -48,4 +50,9 @@ class IonicApp { doVibrate() { Vibration.vibrate(1000); } + doBatteryStatus() { + Battery.getStatus().then((battery) => { + this.battery = battery; + }); + } } diff --git a/ionic/components/app/test/native/main.html b/ionic/components/app/test/native/main.html index 567e252430..b058941c56 100644 --- a/ionic/components/app/test/native/main.html +++ b/ionic/components/app/test/native/main.html @@ -1,19 +1,28 @@

Camera

- +

Geolocation

- +
Fetching location... {{location.coords.latitude}}, {{location.coords.longitude}}
- +
Fetching location... {{trackLocation.coords.latitude}}, {{trackLocation.coords.longitude}}

Vibration

- + +

Battery

+ +
+ Battery charging: {{battery.charging}}
+ Battery level: {{battery.level * 100}}%
+ Battery charging time: {{battery.chargingTime}}s
+ Battery discharging time: {{battery.dischargingTime}}s
+
+
diff --git a/ionic/native/battery/battery.ts b/ionic/native/battery/battery.ts new file mode 100644 index 0000000000..82cc2fb7e6 --- /dev/null +++ b/ionic/native/battery/battery.ts @@ -0,0 +1,41 @@ +import * as util from 'ionic/util'; + +import {NativePlugin} from '../plugin'; + +@NativePlugin({ + name: 'Battery', + platforms: { + cordova: 'cordova-plugin-battery-status' + } +}) +export class Battery { + static getStatus() { + return new Promise((resolve, reject) => { + if(navigator.getBattery) { + + navigator.getBattery().then((battery) => { + this.battery = battery; + resolve(Battery._format(battery)); + }); + + } else { + + window.addEventListener('batterystatus', (battery) => { + resolve(Battery._format(battery)); + }); + } + }); + } + static _format(batteryObj) { + if(typeof batteryObj.isPlugged !== 'undefined') { + // This is the old format, map it to the new format + util.extend(batteryObj, { + charging: batteryObj.isPlugged, + level: batteryObj.level / 100, + chargingTime: 0, //not provided, + dischargingTime: 0 //not provided + }); + } + return batteryObj; + } +} diff --git a/ionic/native/contacts/contacts.ts b/ionic/native/contacts/contacts.ts new file mode 100644 index 0000000000..b1421158b1 --- /dev/null +++ b/ionic/native/contacts/contacts.ts @@ -0,0 +1,91 @@ +// install : cordova plugin add cordova-plugin-contacts +// link : https://github.com/apache/cordova-plugin-contacts +import * as Rx from 'rx'; + +import * as util from 'ionic/util'; +import {NativePlugin} from '../plugin'; + +@NativePlugin({ + name: 'Contacts', + platforms: { + cordova: 'cordova-plugin-contacts' + } +}) +export class Contacts { + static save(contact) { + return new Promise((resolve, reject) => { + if(!navigator.contacts) { + this.pluginWarn(); + reject('Contacts plugin not installed'); + } + var deviceContact = navigator.contacts.create(contact); + + deviceContact.save(function (result) { + resolve(result); + }, function (err) { + reject(err); + }); + }); + } + + static remove(contact) { + return new Promise((resolve, reject) => { + if(!navigator.contacts) { + this.pluginWarn(); + reject('Contacts plugin not installed'); + } + var deviceContact = navigator.contacts.create(contact); + + deviceContact.remove(function (result) { + resolve(result); + }, function (err) { + reject(err); + }); + }) + } + + static clone(contact) { + if(!navigator.contacts) { + this.pluginWarn(); + return null; + } + var deviceContact = navigator.contacts.create(contact); + return deviceContact.clone(contact); + } + + static find(options) { + return new Promise((resolve, reject) => { + var fields = options.fields || ['id', 'displayName']; + delete options.fields; + if (Object.keys(options).length === 0) { + navigator.contacts.find(fields, function (results) { + resolve(results); + },function (err) { + reject(err); + }); + } else { + navigator.contacts.find(fields, function (results) { + resolve(results); + }, function (err) { + reject(err); + }, options); + } + }); + } + + static pickContact() { + return new Promise((resolve, reject) => { + navigator.contacts.pickContact(function (contact) { + resolve(contact); + }, function (err) { + reject(err); + }); + }) + } + + // TODO: method to set / get ContactAddress + // TODO: method to set / get ContactError + // TODO: method to set / get ContactField + // TODO: method to set / get ContactName + // TODO: method to set / get ContactOrganization +} diff --git a/ionic/native/plugins.ts b/ionic/native/plugins.ts index d8d776a0a0..975d2815ce 100644 --- a/ionic/native/plugins.ts +++ b/ionic/native/plugins.ts @@ -1,4 +1,6 @@ export * from './plugin' +export * from './battery/battery' export * from './camera/camera' +export * from './contacts/contacts' export * from './geolocation/geolocation' export * from './vibration/vibration' diff --git a/ionic/native/vibration/vibration.ts b/ionic/native/vibration/vibration.ts index ba6c9bd2ce..4b2c24d871 100644 --- a/ionic/native/vibration/vibration.ts +++ b/ionic/native/vibration/vibration.ts @@ -5,17 +5,17 @@ import {NativePlugin} from '../plugin'; @NativePlugin({ name: 'Vibration', - platforms { + platforms: { cordova: 'cordova-plugin-vibration' } }) export class Vibration { static vibrate(pattern) { - this.pluginWarn(); if(!navigator.vibrate) { + this.pluginWarn(); console.log('Vibrate (dev): ', pattern); - return; + } else { + navigator.vibrate(pattern); } - navigator.vibrate(pattern); } }