phonegap deep-dive

26
PhoneGap API Deep Dive Andrew Lunny, Nitobi, July 8 2010

Upload: alunny

Post on 09-May-2015

9.596 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Phonegap deep-dive

PhoneGap API Deep Dive

Andrew Lunny, Nitobi, July 8 2010

Page 2: Phonegap deep-dive

PhoneGap Goal

A set of consistent cross-platform APIs for accessing native device capabilities through

JavaScript

This is possible for a large subset of PhoneGap APIs

Page 3: Phonegap deep-dive

However

• Some functionality is not present on all hardwareo Accelerometer, Compass

• Some features aren't exposed by all platformso SMS, Telephony, File System

• Some features are exposed in varying wayso Camera, Contacts

• Some features are platform specifico notification.activityStart, KeyEvent

Page 4: Phonegap deep-dive

PhoneGap 1.0

• Consistent Core API• Plugins for majority of platform specific code• Platform specific functionality for exceptional purposes

Documentation Push• Reworking phonegap-docso http://docs2.phonegap.como http://github.com/phonegap/phonegap-docs/tree/

rework• Better expose and fix inconsistencies

When in doubt, check mobile-spec

Page 5: Phonegap deep-dive

API Outline

Basic

DeviceNe twork

Notifica tion Debug Console

Sensors

Acce lerome terCompass

G eoloca tionOrienta tion

User Da ta / F ile Storage

C ameraContac ts

F ileMed ia (Aud io)Storage/Store

SMS/Te lephony

Page 6: Phonegap deep-dive

Basics

o Fundamentals for mobile apps and developmento Well-supported on all platformso Consistent core, with platform specific extensions

Device, Network, Notification, Debug

Page 7: Phonegap deep-dive

DeviceiPhone, Android, BlackBerry, Palm, Symbian.wrt

A JS Object with String/Boolean fields

navigator.device.uuidnavigator.device.platform

Everywhere:

except Palm: navigator.device.namenavigator.device.version

except Palm and Symbian:

navigator.device.gap || navigator.device.gapVersion

device.gap = iPhone and BlackBerrydevice.gapVersion = Android

Page 8: Phonegap deep-dive

NetworkiPhone, Android, BlackBerry, Palm, Symbian.wrt

One common public function: isReachable

BlackBerry also has Network.XHRsince the BlackBerry webview doesn't give us an XMLHttpRequest by default

Android 2.2: ononline and onoffline events

Page 9: Phonegap deep-dive

navigator.network.isReachable(hostname, callback, options);

The callback function is called with the parameter foo, which is used to compare the NetworkStatus against the NetworkStatus constants (for example, NetworkStatus.NOT_REACHABLE)

Palm/Symbian (what we want going forward)foo != NetworkStatus.NOT_REACHABLE

iPhonefoo.remoteHostStatus != NetworkStatus.NOT_REACHABLE

Android/BlackBerryfoo.code != NetworkStatus.NOT_REACHABLE

Page 10: Phonegap deep-dive

NotificationiPhone, Android, BlackBerry, Palm, Symbian.wrt

Common navigator.notification.alert

navigator.notification.beep

navigator.notification.vibrateNot on Palm

Not on Symbian

navigator.notification.blinkBlackBerry only

confirmactivityStart, activityStoploadingStart, loadingStop

iPhone only

Page 11: Phonegap deep-dive

DebugConsoleiPhone, Android, BlackBerry, Palm, Symbian.wrt

debug.log(msg)debug.warn(msg)debug.error(msg)

Android currently uses console.log

Both Android and BlackBerry should and will implement these

Page 12: Phonegap deep-dive

Sensors

o All related to physical state of deviceo Time sensitive and liable to changeo Consistent between platforms and consistent

between environment moduleso Moving into the browser•Google I/O Keynote promises all of these in

Browser soon: •http://bit.ly/android-io•cf. http://dev.w3.org/geo/api/spec-source-

orientation.html

Accelerometer, Compass, Geolocation, Orientation

Page 13: Phonegap deep-dive

Sensor APIs

navigator.sensor.getCurrentVariable(success, error, options);• asynchronously calls success with the current reading/status

navigator.sensor.watchVariable(success, error, options);• calls getCurrentVariable repeatedly, at frequency

specified in options parameter; returns a watch id

navigator.sensor.clearWatch(watchId);• cancels the watch set by watchVariable

Page 14: Phonegap deep-dive

AccelerometeriPhone, Android, BlackBerry, Palm, Symbian.wrt

navigator.accelerometer.getCurrentAccelerationnavigator.accelerometer.watchAccelerationnavigator.accelerometer.clearWatch

Not supported on BlackBerry because the hardware has limited support (depending on the handset)

Page 15: Phonegap deep-dive

CompassiPhone, Android, BlackBerry, Palm, Symbian.wrt

navigator.compass.getCurrentHeadingnavigator.compass.watchHeadingnavigator.compass.clearWatch

Palm and Symbian: no hardware support

BlackBerry: depends on the device, not implemented yet

Page 16: Phonegap deep-dive

GeolocationiPhone, Android, BlackBerry, Palm, Symbian.wrt

navigator.geolocation.getCurrentPositionnavigator.geolocation.watchPositionnavigator.geolocation.clearWatch

Where available (iPhone, Android 2.0+), PhoneGap uses the browser's geolocation API, rather than implementing our own

Page 17: Phonegap deep-dive

OrientationiPhone, Android, BlackBerry, Palm, Symbian.wrt

navigator.orientation.getCurrentOrientationnavigator.orientation.watchOrientationnavigator.orientation.clearWatch

Unlikely for BlackBerry (only makes sense on certain handsets)

Android: probably wait for Browser implementation

iPhone fires orientationChange event automagically; you can use window.orientation instead of the PhoneGap method

CSS3 media queries can also be used

Page 18: Phonegap deep-dive

User Data / File Storage

o Reading and writing what's normally inaccessibleo Most prone to inconsistency•between platforms•between device models/OS versions

o Least settled PhoneGap APIs

Camera, Contacts, File, Media (Audio), Storage/Store, SMS/Telephony

Page 19: Phonegap deep-dive

CameraiPhone, Android, BlackBerry, Palm, Symbian.wrt

navigator.camera.getPicture(success, error, options);

Android, iPhone

BlackBerry, Palm

Symbian.wrt

success is passed a base-64 string of image data

success is passed a file path to the image

success is passed an array of file paths

Page 20: Phonegap deep-dive

ContactsiPhone, Android, BlackBerry, Palm, Symbian.wrt

Android, Blackberry, Symbian

iPhone

navigator.contacts.find(filter, success, error, options);

filter is an object of type Contact

getAllContactsnewContactchooseContactdisplayContactremoveContactcontactsCount

Page 21: Phonegap deep-dive

Contacts

o The most inconsistent between platformso Test on devices, not just simulators!

Page 22: Phonegap deep-dive

FileiPhone, Android, BlackBerry, Palm, Symbian.wrt

navigator.fileMgrFileReader objectFileWriter object

Palm webOS does not allow File I/O File.read is implemented with an XHRBlackBerry should be implemented...• use Store (see later slide) instead

Android & iPhone: based loosely on W3C file spec:http://bit.ly/w3cfileexample API usage: http://bit.ly/pg-file

Page 23: Phonegap deep-dive

Media (Audio)iPhone, Android, BlackBerry, Palm, Symbian.wrt

var mySong = new Media("urthebest.mp3",success,error);mySong.play();mySong.pause();mySong.stop();

Blackberry, Palm and Symbian as Audio, not Media

iPhone and Android can record audio also• iPhone: startAudioRecord, stopAudioRecord

• Android: startRecord, stopRecordingAudio

Page 24: Phonegap deep-dive

Storage/StoreiPhone, Android, BlackBerry, Palm, Symbian.wrt

iPhone/Android 2.0+/Palm: unneeded - use localStorage or HTML5 db

Android < 2.0: sets up an SQLite database, accessed through window.openDatabase

• Unstable: use with caution

Symbian: key/value store: navigator.storage.getItemnavigator.storage.setItem

BlackBerry: key/value store: navigator.store.getnavigator.store.put

Page 25: Phonegap deep-dive

SMS/TelephonyiPhone, Android, BlackBerry, Palm, Symbian.wrt

navigator.sms.send(number, message, success, error, options);navigator.telephony.send(number);

To be implemented on iPhone/Android

Any platform: set link HREF to sms: or tel:

In all cases, the user must confirm before the call or message is placed

Page 26: Phonegap deep-dive

@alunny, [email protected]

Questions or Feedback