diff --git a/ionic/components/app/test/native/index.ts b/ionic/components/app/test/native/index.ts index 894026b2be..e2bbea6418 100644 --- a/ionic/components/app/test/native/index.ts +++ b/ionic/components/app/test/native/index.ts @@ -5,58 +5,29 @@ import {App, Http} from 'ionic/ionic'; import {Camera, Geolocation, Vibration, Battery, Device} from 'ionic/ionic'; -let testUrl = 'https://ionic-api-tester.herokuapp.com/json'; -let testUrl404 = 'https://ionic-api-tester.herokuapp.com/404'; - +import {CameraPage} from 'pages/camera'; +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({ templateUrl: 'main.html' }) class IonicApp { constructor() { - } - doDevice() { - let device = Device.getDevice(); - console.log('Got device', device); - } - 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); - }); - } - 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; - }); + this.firstPage = CameraPage; + console.log('First page', CameraPage); + this.plugins = [ + {title: 'Camera', page: CameraPage}, + {title: 'Device', page: DevicePage}, + {title: 'Device Motion', page: DeviceMotionPage}, + {title: 'Geolocation', page: GeolocationPage}, + {title: 'Contacts', page: ContactsPage}, + {title: 'Battery', page: BatteryPage}, + {title: 'Vibration', page: VibrationPage}, + ] } } diff --git a/ionic/components/app/test/native/main.html b/ionic/components/app/test/native/main.html index 820585f7c2..4543760755 100644 --- a/ionic/components/app/test/native/main.html +++ b/ionic/components/app/test/native/main.html @@ -1,35 +1,12 @@ - - -

Device

- -
-
-

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

Contacts

-
-
- + + Plugins + + + + {{p.title}} + + -
+ + + diff --git a/ionic/components/app/test/native/pages/battery.ts b/ionic/components/app/test/native/pages/battery.ts new file mode 100644 index 0000000000..50632e5cd1 --- /dev/null +++ b/ionic/components/app/test/native/pages/battery.ts @@ -0,0 +1,31 @@ +import {IonicView} from 'ionic/ionic'; + + +@IonicView({ + template: ` + + + Battery + + +

Battery

+ +
+ Battery charging: {{battery.charging}}
+ Battery level: {{battery.level * 100}}%
+ Battery charging time: {{battery.chargingTime}}s
+ Battery discharging time: {{battery.dischargingTime}}s
+
+ +
+ ` +}) +export class BatteryPage { + doBatteryStatus() { + Battery.getStatus().then((battery) => { + this.battery = battery; + }); + } +} diff --git a/ionic/components/app/test/native/pages/camera.ts b/ionic/components/app/test/native/pages/camera.ts new file mode 100644 index 0000000000..2ec0970cac --- /dev/null +++ b/ionic/components/app/test/native/pages/camera.ts @@ -0,0 +1,32 @@ +import {IonicView} from 'ionic/ionic'; + +import {Camera} from 'ionic/ionic'; + +@IonicView({ + template: ` + + + Camera + + +

Camera

+ +
+ ` +}) +export class CameraPage { + constructor() { + + } + getPicture() { + Camera.getPicture({ + + }).then(data => { + console.log('Data', data); + }, err => { + alert('Unable to take picture') + }) + } +} diff --git a/ionic/components/app/test/native/pages/contacts.ts b/ionic/components/app/test/native/pages/contacts.ts new file mode 100644 index 0000000000..ac3215b544 --- /dev/null +++ b/ionic/components/app/test/native/pages/contacts.ts @@ -0,0 +1,22 @@ +import {IonicView} from 'ionic/ionic'; + +import {Contacts} from 'ionic/ionic'; + +@IonicView({ + template: ` + + + Contacts + + +

Contacts

+
+
+
+ ` +}) +export class ContactsPage { + +} diff --git a/ionic/components/app/test/native/pages/device-motion.ts b/ionic/components/app/test/native/pages/device-motion.ts new file mode 100644 index 0000000000..87ca4e2f95 --- /dev/null +++ b/ionic/components/app/test/native/pages/device-motion.ts @@ -0,0 +1,18 @@ +import {IonicView} from 'ionic/ionic'; + + +@IonicView({ + template: ` + + + Device Motion + + + + ` +}) +export class DeviceMotionPage { + +} diff --git a/ionic/components/app/test/native/pages/device.ts b/ionic/components/app/test/native/pages/device.ts new file mode 100644 index 0000000000..518f2f2b49 --- /dev/null +++ b/ionic/components/app/test/native/pages/device.ts @@ -0,0 +1,26 @@ +import {IonicView} from 'ionic/ionic'; + + +@IonicView({ + template: ` + + + Vibration + + +

Device

+ +
+
+
+ ` +}) +export class DevicePage { + + doDevice() { + let device = Device.getDevice(); + console.log('Got device', device); + } +} diff --git a/ionic/components/app/test/native/pages/geolocation.ts b/ionic/components/app/test/native/pages/geolocation.ts new file mode 100644 index 0000000000..7a55a74686 --- /dev/null +++ b/ionic/components/app/test/native/pages/geolocation.ts @@ -0,0 +1,52 @@ +import {IonicView} from 'ionic/ionic'; + +import {Geolocation} from 'ionic/ionic'; + +@IonicView({ + template: ` + + + Vibration + + +

Geolocation

+ +
+ Fetching location... + {{location.coords.latitude}}, {{location.coords.longitude}} +
+ +
+ Fetching location... + {{trackLocation.coords.latitude}}, {{trackLocation.coords.longitude}} +
+
+ ` +}) +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); + }); + } +} diff --git a/ionic/components/app/test/native/pages/vibration.ts b/ionic/components/app/test/native/pages/vibration.ts new file mode 100644 index 0000000000..7747bfe8f8 --- /dev/null +++ b/ionic/components/app/test/native/pages/vibration.ts @@ -0,0 +1,23 @@ +import {IonicView} from 'ionic/ionic'; + +import {Vibration} from 'ionic/ionic'; + +@IonicView({ + template: ` + + + Vibration + + +

Vibration

+ +
+ ` +}) +export class VibrationPage { + doVibrate() { + Vibration.vibrate(1000); + } +} diff --git a/ionic/native/battery/battery.ts b/ionic/native/battery/battery.ts index 82cc2fb7e6..16dee7b891 100644 --- a/ionic/native/battery/battery.ts +++ b/ionic/native/battery/battery.ts @@ -20,9 +20,11 @@ export class Battery { } else { - window.addEventListener('batterystatus', (battery) => { - resolve(Battery._format(battery)); - }); + var fnCb = function fnCb(battery) { + resolve(battery); + window.removeEventListener('batterystatus', fnCb); + } + window.addEventListener('batterystatus', fnCb); } }); } diff --git a/ionic/native/device-motion/device-motion.ts b/ionic/native/device-motion/device-motion.ts new file mode 100644 index 0000000000..749ff11da6 --- /dev/null +++ b/ionic/native/device-motion/device-motion.ts @@ -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); + } + } + } + } +} diff --git a/ionic/native/plugins.ts b/ionic/native/plugins.ts index 0987e69131..f24fd63c1d 100644 --- a/ionic/native/plugins.ts +++ b/ionic/native/plugins.ts @@ -3,5 +3,6 @@ export * from './battery/battery' export * from './camera/camera' export * from './contacts/contacts' export * from './device/device' +export * from './device-motion/device-motion' export * from './geolocation/geolocation' export * from './vibration/vibration'