This commit is contained in:
Max Lynch
2015-09-08 16:08:17 -05:00
parent b95ddaafa2
commit ccae94013c
12 changed files with 318 additions and 84 deletions

View File

@ -5,58 +5,29 @@ import {App, Http} from 'ionic/ionic';
import {Camera, Geolocation, Vibration, Battery, Device} from 'ionic/ionic'; import {Camera, Geolocation, Vibration, Battery, Device} from 'ionic/ionic';
let testUrl = 'https://ionic-api-tester.herokuapp.com/json'; import {CameraPage} from 'pages/camera';
let testUrl404 = 'https://ionic-api-tester.herokuapp.com/404'; import {BatteryPage} from 'pages/battery';
import {ContactsPage} from 'pages/contacts';
import {DevicePage} from 'pages/device';
import {DeviceMotionPage} from 'pages/device-motion';
import {GeolocationPage} from 'pages/geolocation';
import {VibrationPage} from 'pages/vibration';
@App({ @App({
templateUrl: 'main.html' templateUrl: 'main.html'
}) })
class IonicApp { class IonicApp {
constructor() { constructor() {
} this.firstPage = CameraPage;
doDevice() { console.log('First page', CameraPage);
let device = Device.getDevice(); this.plugins = [
console.log('Got device', device); {title: 'Camera', page: CameraPage},
} {title: 'Device', page: DevicePage},
doGetLocation() { {title: 'Device Motion', page: DeviceMotionPage},
console.log('Getting location'); {title: 'Geolocation', page: GeolocationPage},
this.gettingLocation = true; {title: 'Contacts', page: ContactsPage},
Geolocation.getCurrentPosition().then((pos) => { {title: 'Battery', page: BatteryPage},
this.gettingLocation = false; {title: 'Vibration', page: VibrationPage},
console.log('Got location', pos); ]
this.location = pos;
}, (err) => {
this.gettingLocation = false;
console.error('Unable to get location', err);
});
}
doTrackLocation() {
this.gettingTrackLocation = true;
Geolocation.watchPosition().source.subscribe((pos) => {
this.gettingTrackLocation = false;
console.log('Got location', pos);
this.trackLocation = pos;
}, (err) => {
this.gettingTrackLocation = false;
console.error('Unable to get location', pos);
});
}
getPicture() {
Camera.getPicture({
}).then(data => {
console.log('Data', data);
}, err => {
alert('Unable to take picture')
})
}
doVibrate() {
Vibration.vibrate(1000);
}
doBatteryStatus() {
Battery.getStatus().then((battery) => {
this.battery = battery;
});
} }
} }

View File

@ -1,35 +1,12 @@
<ion-view> <ion-aside #aside [content]="content" id="mainMenu">
<ion-content padding> <ion-toolbar><ion-title>Plugins</ion-title></ion-toolbar>
<h2>Device</h2> <ion-content>
<button primary outline (click)="doDevice()">Get Device</button> <ion-list>
<div> <ion-item *ng-for="#p of plugins" (^click)="openPage(aside, p)">
</div> <span>{{p.title}}</span>
<h2>Camera</h2> </ion-item>
<button primary outline (click)="getPicture()">Get Picture</button> </ion-list>
<h2>Geolocation</h2>
<button primary outline (click)="doGetLocation()">Get Location</button>
<div>
<b *ng-if="gettingLocation">Fetching location...</b>
<b *ng-if="location">{{location.coords.latitude}}, {{location.coords.longitude}}</b>
</div>
<button primary outline (click)="doTrackLocation()">Track Location</button>
<div>
<b *ng-if="gettingTrackLocation">Fetching location...</b>
<b *ng-if="trackLocation">{{trackLocation.coords.latitude}}, {{trackLocation.coords.longitude}}</b>
</div>
<h2>Vibration</h2>
<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>
<h2>Contacts</h2>
<div>
</div>
</ion-content> </ion-content>
</ion-view> </ion-aside>
<ion-nav #content swipe-back-enabled="false" [root]="firstPage"></ion-nav>

View File

@ -0,0 +1,31 @@
import {IonicView} from 'ionic/ionic';
@IonicView({
template: `
<ion-navbar *navbar>
<button aside-toggle>
<icon menu></icon>
</button>
<ion-title>Battery</ion-title>
</ion-navbar>
<ion-content class="padding">
<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>
`
})
export class BatteryPage {
doBatteryStatus() {
Battery.getStatus().then((battery) => {
this.battery = battery;
});
}
}

View File

@ -0,0 +1,32 @@
import {IonicView} from 'ionic/ionic';
import {Camera} from 'ionic/ionic';
@IonicView({
template: `
<ion-navbar *navbar>
<button aside-toggle>
<icon menu></icon>
</button>
<ion-title>Camera</ion-title>
</ion-navbar>
<ion-content class="padding">
<h2>Camera</h2>
<button primary outline (click)="getPicture()">Get Picture</button>
</ion-content>
`
})
export class CameraPage {
constructor() {
}
getPicture() {
Camera.getPicture({
}).then(data => {
console.log('Data', data);
}, err => {
alert('Unable to take picture')
})
}
}

View File

@ -0,0 +1,22 @@
import {IonicView} from 'ionic/ionic';
import {Contacts} from 'ionic/ionic';
@IonicView({
template: `
<ion-navbar *navbar>
<button aside-toggle>
<icon menu></icon>
</button>
<ion-title>Contacts</ion-title>
</ion-navbar>
<ion-content class="padding">
<h2>Contacts</h2>
<div>
</div>
</ion-content>
`
})
export class ContactsPage {
}

View File

@ -0,0 +1,18 @@
import {IonicView} from 'ionic/ionic';
@IonicView({
template: `
<ion-navbar *navbar>
<button aside-toggle>
<icon menu></icon>
</button>
<ion-title>Device Motion</ion-title>
</ion-navbar>
<ion-content class="padding">
</ion-content>
`
})
export class DeviceMotionPage {
}

View File

@ -0,0 +1,26 @@
import {IonicView} from 'ionic/ionic';
@IonicView({
template: `
<ion-navbar *navbar>
<button aside-toggle>
<icon menu></icon>
</button>
<ion-title>Vibration</ion-title>
</ion-navbar>
<ion-content class="padding">
<h2>Device</h2>
<button primary outline (click)="doDevice()">Get Device</button>
<div>
</div>
</ion-content>
`
})
export class DevicePage {
doDevice() {
let device = Device.getDevice();
console.log('Got device', device);
}
}

View File

@ -0,0 +1,52 @@
import {IonicView} from 'ionic/ionic';
import {Geolocation} from 'ionic/ionic';
@IonicView({
template: `
<ion-navbar *navbar>
<button aside-toggle>
<icon menu></icon>
</button>
<ion-title>Vibration</ion-title>
</ion-navbar>
<ion-content class="padding">
<h2>Geolocation</h2>
<button primary outline (click)="doGetLocation()">Get Location</button>
<div>
<b *ng-if="gettingLocation">Fetching location...</b>
<b *ng-if="location">{{location.coords.latitude}}, {{location.coords.longitude}}</b>
</div>
<button primary outline (click)="doTrackLocation()">Track Location</button>
<div>
<b *ng-if="gettingTrackLocation">Fetching location...</b>
<b *ng-if="trackLocation">{{trackLocation.coords.latitude}}, {{trackLocation.coords.longitude}}</b>
</div>
</ion-content>
`
})
export class GeolocationPage {
doGetLocation() {
console.log('Getting location');
this.gettingLocation = true;
Geolocation.getCurrentPosition().then((pos) => {
this.gettingLocation = false;
console.log('Got location', pos);
this.location = pos;
}, (err) => {
this.gettingLocation = false;
console.error('Unable to get location', err);
});
}
doTrackLocation() {
this.gettingTrackLocation = true;
Geolocation.watchPosition().source.subscribe((pos) => {
this.gettingTrackLocation = false;
console.log('Got location', pos);
this.trackLocation = pos;
}, (err) => {
this.gettingTrackLocation = false;
console.error('Unable to get location', pos);
});
}
}

View File

@ -0,0 +1,23 @@
import {IonicView} from 'ionic/ionic';
import {Vibration} from 'ionic/ionic';
@IonicView({
template: `
<ion-navbar *navbar>
<button aside-toggle>
<icon menu></icon>
</button>
<ion-title>Vibration</ion-title>
</ion-navbar>
<ion-content class="padding">
<h2>Vibration</h2>
<button primary outline (click)="doVibrate()">Vibrate</button>
</ion-content>
`
})
export class VibrationPage {
doVibrate() {
Vibration.vibrate(1000);
}
}

View File

@ -20,9 +20,11 @@ export class Battery {
} else { } else {
window.addEventListener('batterystatus', (battery) => { var fnCb = function fnCb(battery) {
resolve(Battery._format(battery)); resolve(battery);
}); window.removeEventListener('batterystatus', fnCb);
}
window.addEventListener('batterystatus', fnCb);
} }
}); });
} }

View File

@ -0,0 +1,79 @@
import * as Rx from 'rx';
import * as util from 'ionic/util';
import {NativePlugin} from '../plugin';
import {Platform} from '../../platform/platform';
@NativePlugin({
name: 'Device Motion',
platforms: {
cordova: 'cordova-plugin-device-motion'
}
})
export class DeviceMotion {
getCurrentAcceleration() {
return new Promise((resolve, reject) => {
if(navigator.accelerometer) {
navigator.accelerometer.getCurrentAcceleration(function (result) {
resolve(result);
}, function (err) {
reject(err);
});
} else if(window.DeviceMotionEvent || ('listenForDeviceMovement' in window)) {
var fnCb = function fnCb(eventData) {
resolve(eventData);
window.removeEventListener('devicemotion', fnCb);
}
window.addEventListener('devicemotion', fnCb);
} else {
this.pluginWarn();
reject('The Device does not support device motion events.');
return;
}
});
}
watchAcceleration(options) {
if(navigator.accelerometer) {
let watchID;
let source = Rx.Observable.create((observer) => {
watchID = navigator.accelerometer.watchAcceleration(function (result) {
observer.onNext(result);
}, function (err) {
observer.onError(err, observer);
}, options);
});
return {
source: source,
watchID: watchID,
clear: () => {
navigator.accelerometer.clearWatch(watchID);
}
}
} else {
let watchID;
let source = Rx.Observable.create((observer) => {
var fnCb = function fnCb(eventData) {
observer.onNext(eventData);
};
window.addEventListener('devicemotion', cbFn);
});
return {
source: source,
watchID: watchID,
clear: () => {
window.removeEventListener('devicemotion', cbFn);
}
}
}
}
}

View File

@ -3,5 +3,6 @@ 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 './device/device'
export * from './device-motion/device-motion'
export * from './geolocation/geolocation' export * from './geolocation/geolocation'
export * from './vibration/vibration' export * from './vibration/vibration'