mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-19 19:57:22 +08:00
Contacts
This commit is contained in:
@ -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;
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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>
|
||||||
|
41
ionic/native/battery/battery.ts
Normal file
41
ionic/native/battery/battery.ts
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
91
ionic/native/contacts/contacts.ts
Normal file
91
ionic/native/contacts/contacts.ts
Normal 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
|
||||||
|
}
|
@ -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'
|
||||||
|
@ -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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user