mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2026-03-13 10:22:08 +08:00
Contacts
This commit is contained in:
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 './battery/battery'
|
||||
export * from './camera/camera'
|
||||
export * from './contacts/contacts'
|
||||
export * from './geolocation/geolocation'
|
||||
export * from './vibration/vibration'
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user