This commit is contained in:
Max Lynch
2015-09-08 13:49:24 -05:00
parent 05473c07af
commit fa6c82ff52
6 changed files with 159 additions and 9 deletions

View File

@ -1,7 +1,9 @@
import {Component} from 'angular2/angular2'; import {Component} from 'angular2/angular2';
import {Control, ControlGroup} from 'angular2/forms'; 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 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';
@ -48,4 +50,9 @@ class IonicApp {
doVibrate() { doVibrate() {
Vibration.vibrate(1000); Vibration.vibrate(1000);
} }
doBatteryStatus() {
Battery.getStatus().then((battery) => {
this.battery = battery;
});
}
} }

View File

@ -1,19 +1,28 @@
<ion-view> <ion-view>
<ion-content padding> <ion-content padding>
<h2>Camera</h2> <h2>Camera</h2>
<button primary (click)="getPicture()">Get Picture</button> <button primary outline (click)="getPicture()">Get Picture</button>
<h2>Geolocation</h2> <h2>Geolocation</h2>
<button primary (click)="doGetLocation()">Get Location</button> <button primary outline (click)="doGetLocation()">Get Location</button>
<div> <div>
<b *ng-if="gettingLocation">Fetching location...</b> <b *ng-if="gettingLocation">Fetching location...</b>
<b *ng-if="location">{{location.coords.latitude}}, {{location.coords.longitude}}</b> <b *ng-if="location">{{location.coords.latitude}}, {{location.coords.longitude}}</b>
</div> </div>
<button primary (click)="doTrackLocation()">Track Location</button> <button primary outline (click)="doTrackLocation()">Track Location</button>
<div> <div>
<b *ng-if="gettingTrackLocation">Fetching location...</b> <b *ng-if="gettingTrackLocation">Fetching location...</b>
<b *ng-if="trackLocation">{{trackLocation.coords.latitude}}, {{trackLocation.coords.longitude}}</b> <b *ng-if="trackLocation">{{trackLocation.coords.latitude}}, {{trackLocation.coords.longitude}}</b>
</div> </div>
<h2>Vibration</h2> <h2>Vibration</h2>
<button primary (click)="doVibrate()">Vibrate</button> <button primary outline (click)="doVibrate()">Vibrate</button>
<h2>Battery</h2>
<button primary outline (click)="doBatteryStatus()">Get Status</button>
<div *ng-if="battery">
Battery charging: <b>{{battery.charging}}</b><br>
Battery level: <b>{{battery.level * 100}}</b>%<br>
Battery charging time: <b>{{battery.chargingTime}}</b>s<br>
Battery discharging time: <b>{{battery.dischargingTime}}</b>s<br>
</div>
</ion-content> </ion-content>
</ion-view> </ion-view>

View File

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

View File

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

View File

@ -1,4 +1,6 @@
export * from './plugin' export * from './plugin'
export * from './battery/battery'
export * from './camera/camera' export * from './camera/camera'
export * from './contacts/contacts'
export * from './geolocation/geolocation' export * from './geolocation/geolocation'
export * from './vibration/vibration' export * from './vibration/vibration'

View File

@ -5,17 +5,17 @@ import {NativePlugin} from '../plugin';
@NativePlugin({ @NativePlugin({
name: 'Vibration', name: 'Vibration',
platforms { platforms: {
cordova: 'cordova-plugin-vibration' cordova: 'cordova-plugin-vibration'
} }
}) })
export class Vibration { export class Vibration {
static vibrate(pattern) { static vibrate(pattern) {
this.pluginWarn();
if(!navigator.vibrate) { if(!navigator.vibrate) {
this.pluginWarn();
console.log('Vibrate (dev): ', pattern); console.log('Vibrate (dev): ', pattern);
return; } else {
}
navigator.vibrate(pattern); navigator.vibrate(pattern);
} }
} }
}