phonegap deep-dive

Download Phonegap deep-dive

Post on 09-May-2015

9.574 views

Category:

Technology

1 download

Embed Size (px)

TRANSCRIPT

  • 1.PhoneGap API Deep Dive Andrew Lunny, Nitobi, July 8 2010

2. PhoneGap GoalA set of consistent cross-platform APIs foraccessing native device capabilities through JavaScriptThis is possible for a large subset of PhoneGapAPIs 3. However Some functionality is not present on all hardware o Accelerometer, Compass Some features aren't exposed by all platforms o SMS, Telephony, File System Some features are exposed in varying ways o Camera, Contacts Some features are platform specific o notification.activityStart, KeyEvent 4. PhoneGap 1.0 Consistent Core API Plugins for majority of platform specific code Platform specific functionality for exceptional purposesDocumentation Push Reworking phonegap-docso http://docs2.phonegap.como http://github.com/phonegap/phonegap-docs/tree/rework Better expose and fix inconsistenciesWhen in doubt, check mobile-spec 5. API OutlineD evic e N etwork B asic Notic ation D e bug C onsoleA c c elerometerC omp ass SensorsG eoloc ation OrientationC ameraC onta ctsFile User D ata / File Stora g e Me dia (Audio)Stora g e/Store SMS/Tele phony 6. Basics Device, Network, Notification, Debugo Fundamentals for mobile apps and development o Well-supported on all platforms o Consistent core, with platform specific extensions 7. DeviceiPhone, Android, BlackBerry, Palm, Symbian.wrtA JS Object with String/Boolean fieldsEverywhere:navigator.device.uuidnavigator.device.platformexcept Palm: navigator.device.namenavigator.device.version except Palmnavigator.device.gap || and Symbian: navigator.device.gapVersion device.gap = iPhone and BlackBerry device.gapVersion = Android 8. NetworkiPhone, Android, BlackBerry, Palm, Symbian.wrtOne common public function: isReachable BlackBerry also has Network.XHR since the BlackBerry webview doesn't give us an XMLHttpRequest by defaultAndroid 2.2: ononline and onoffline events 9. 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_REACHABLEiPhone foo.remoteHostStatus != NetworkStatus.NOT_REACHABLEAndroid/BlackBerry foo.code != NetworkStatus.NOT_REACHABLE 10. NotificationiPhone, Android, BlackBerry, Palm, Symbian.wrtCommonnavigator.notification.alertNot on Symbian navigator.notification.beepNot on Palmnavigator.notification.vibrateBlackBerry only navigator.notification.blink iPhone onlyconfirm activityStart, activityStop loadingStart, loadingStop 11. DebugConsole iPhone, Android, BlackBerry, Palm, Symbian.wrtdebug.log(msg)debug.warn(msg)debug.error(msg)Android currently uses console.logBoth Android and BlackBerry should and will implement these 12. Sensors Accelerometer, Compass, Geolocation, Orientationo All related to physical state of device o Time sensitive and liable to change o Consistent between platforms and consistent between environment modules o 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 13. Sensor APIsnavigator.sensor.getCurrentVariable(success, error, options); asynchronously calls success with the current reading/statusnavigator.sensor.watchVariable(success, error, options); calls getCurrentVariable repeatedly, at frequency specified in options parameter; returns a watch idnavigator.sensor.clearWatch(watchId); cancels the watch set by watchVariable 14. Accelerometer iPhone, Android, BlackBerry, Palm, Symbian.wrtnavigator.accelerometer.getCurrentAcceleration navigator.accelerometer.watchAcceleration navigator.accelerometer.clearWatch Not supported on BlackBerry because the hardware has limited support (depending on the handset) 15. Compass iPhone, Android, BlackBerry, Palm, Symbian.wrtnavigator.compass.getCurrentHeading navigator.compass.watchHeading navigator.compass.clearWatchPalm and Symbian: no hardware support BlackBerry: depends on the device, not implementedyet 16. Geolocation iPhone, Android, BlackBerry, Palm, Symbian.wrtnavigator.geolocation.getCurrentPosition navigator.geolocation.watchPosition navigator.geolocation.clearWatchWhere available (iPhone, Android 2.0+), PhoneGapuses the browser's geolocation API, rather thanimplementing our own 17. Orientation iPhone, Android, BlackBerry, Palm, Symbian.wrt navigator.orientation.getCurrentOrientation navigator.orientation.watchOrientation navigator.orientation.clearWatch Unlikely for BlackBerry (only makes sense oncertain handsets) Android: probably wait for Browser implementation iPhone fires orientationChange eventautomagically; you can use window.orientationinstead of the PhoneGap method CSS3 media queries can also be used 18. User Data / File StorageCamera, Contacts, File, Media (Audio),Storage/Store, SMS/Telephonyo Reading and writing what's normally inaccessible o Most prone to inconsistency between platforms between device models/OS versions o Least settled PhoneGap APIs 19. Camera iPhone, Android, BlackBerry, Palm, Symbian.wrtnavigator.camera.getPicture(success, error, options); Android, iPhone success is passed a base-64 stringof image data BlackBerry, Palm success is passed a file path to the imageSymbian.wrt success is passed an array of filepaths 20. ContactsiPhone, Android, BlackBerry, Palm, Symbian.wrtAndroid, Blackberry, navigator.contacts.find Symbian(filter, success, error,options); filter is an object of typeContactiPhone getAllContactsnewContactchooseContactdisplayContactremoveContactcontactsCount 21. Contactso The most inconsistent between platforms o Test on devices, not just simulators! 22. FileiPhone, Android, BlackBerry, Palm, Symbian.wrt navigator.fileMgrFileReader objectFileWriter objectPalm webOS does not allow File I/O File.read is implemented with an XHR BlackBerry should be implemented... use Store (see later slide) insteadAndroid & iPhone: based loosely on W3C file spec: http://bit.ly/w3cfile example API usage: http://bit.ly/pg-file 23. 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 MediaiPhone and Android can record audio also iPhone: startAudioRecord,stopAudioRecord Android: startRecord,stopRecordingAudio 24. Storage/Store iPhone, Android, BlackBerry, Palm, Symbian.wrt iPhone/Android 2.0+/Palm: unneeded - use localStorage or HTML5 dbAndroid < 2.0: sets up an SQLite database, accessed through window.openDatabase Unstable: use with cautionSymbian: key/value store: navigator.storage.getItem navigator.storage.setItemBlackBerry: key/value store: navigator.store.get navigator.store.put 25. SMS/Telephony iPhone, Android, BlackBerry, Palm, Symbian.wrtnavigator.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 ormessage is placed 26. Questions or Feedback@alunny, andrew.lunny@nitobi.com