mobile device apis

67
(Mobile) Device APIs James Pearce @jamespearce http://tripleodeon.com

Upload: james-pearce

Post on 01-Nov-2014

40.742 views

Category:

Technology


0 download

DESCRIPTION

Thoughts on the past, present and future of device APIs for (mostly mobile) web developers. NB: is probably out of date already.

TRANSCRIPT

Page 1: Mobile Device APIs

(Mobile) Device APIsJames Pearce

@jamespearcehttp://tripleodeon.com

Page 2: Mobile Device APIs

What are Device APIs?

“Client-side APIs that enable the development of Web Applications that interact with device hardware”

...and other capabilities outside of the browser’s traditional remit

Page 3: Mobile Device APIs

Why are they important?

• They’re not... ...if you’re happy with a web of documents

• They are... ... if you want the the web to be competitive with native app platforms

Page 4: Mobile Device APIs

HistoryCurrent Work

Use Cases

Page 5: Mobile Device APIs
Page 6: Mobile Device APIs
Page 7: Mobile Device APIs

http://www.wapforum.org/what/technical/wtai-30-apr-98.pdf

WTAI Make Call

<a  href="wtai://wp/mc;16505551234">    Call  us</a>

<a  href="call.wmls#makeCall()">    Call  us</a>

//  call.wmlsextern  function  makeCall()  {    WTAPublic.makeCall("16505551234");}

Page 8: Mobile Device APIs

WTAI Call Control

<a  href="calls.wmls#setupCall()">Call  us!</a><a  href="calls.wmls#acceptCall()">Brring!</a>

<a  href="wtai://cc/sd;1*2*3456">Enter  code</a>

//  calls.wmlsextern  function  setupCall()  {    WTACallCont.setup("16505551234",  1);}extern  function  acceptCall()  {    WTACallCont.accept("1",  1);}

Page 9: Mobile Device APIs

WTAI Messaging

//  sms.wmlsextern  function  sendMsg()  {    WTANetText.send("16505551234",  "WML  rocks");}

extern  function  readMsg(i)  {    var  sms  =  WTANetText.read(i);    return  WTANetText.getFieldValue(        sms,  "body"    );}

Page 10: Mobile Device APIs

WTAI Contacts

//  contacts.wmlsextern  function  addFriend(i,  num,  name)  {    WTAPhoneBook.write(i,  num,  name);}

//  return  structs  of  contact  detailsextern  function  getFriendById(i)  {    return  WTAPhoneBook.read("i",  i);}extern  function  getFriendByName(name)  {    return  WTAPhoneBook.read("t",  name);}

Page 11: Mobile Device APIs

I may be laboring the point

But that was 1998...

... and you still can’t do any of this with a contemporary mobile browser

Page 12: Mobile Device APIs
Page 13: Mobile Device APIs

http://www.omtp.org/1.11

BONDI

“enables web based content to access native device capability, intermediated through a robust, but flexible security framework”

Page 14: Mobile Device APIs

Messaging

var  sms  =  bondi.messaging.createSMS({    to:  ["16505551234"],    body:  "Home  soon"});

var  mms  =  bondi.messaging.createMMS({    to:  ["16505551234"],    subject:  "I  will  be...",    body:  "...home  soon"});

Page 15: Mobile Device APIs

Messaging

bondi.messaging.sendSMS(    function  ()  {},  //  success    function  ()  {},  //  error    sms,  true);

bondi.messaging.sendMMS(    function  ()  {},  //  success    function  ()  {},  //  error    mms,  true);

Page 16: Mobile Device APIs

Messaging

bondi.messaging.subscribeToSMS(    function  ()  {},                      //  success    function  ()  {},                      //  error    function  ()  {                          //  listener        alert("They  replied!");    },  {                                      from:  "16505551234"          //  filter    },    true                                            //  exclusive);

Page 17: Mobile Device APIs

Contacts

bondi.pim.contact.getAddressBooks(    function(books)  {  //  success        var  contact  =  books[0].createContact({            name:  'James  Pearce',            emails:  [{email:'[email protected]'}],            phoneNumbers:  [{number:'16505551234'}]        });    },      function  ()  {}        //  error);

Page 18: Mobile Device APIs

* Yes, it’s true: you can’t actually make or receive calls

Telephony

bondi.telephonyLog.findLogEntries(    function  ()  {},  //  success    function  ()  {},  //  error,    [        bondi.telephonyLog.MISSED_CALLS_FOLDER,        bondi.telephonyLog.RECEIVED_CALLS_FOLDER    ],    {        phoneNumber:  "16505551234"    });

Page 19: Mobile Device APIs

App Launcher

bondi.applauncher.launchApplication(    function  ()  {},  //  success    function  ()  {},  //  error    "tel:16505551234");

//  mailto:...//  sms:...//  file:...

Page 20: Mobile Device APIs

http://www.omtp.org/1.11/apis

On one hand...

• Security-conscious architecture

• Comprehensive API support

• Consistent API patterns

• Conformance specifications

• Reasonable test coverage

Page 21: Mobile Device APIs

On the other...

Page 22: Mobile Device APIs

bondi.*

deviceapis.*

Page 23: Mobile Device APIs

http://specs.wacapps.net/

WAC 2.0

• Accelerometer

• Calendar

• Camera

• Contacts

• Core

• Device interaction

• Device status

• File system

• Geolocation

• Messaging

• Orientation

• Tasks

• Viewport

• Webview

Page 24: Mobile Device APIs

Which is all awesome

Except...

Page 25: Mobile Device APIs
Page 26: Mobile Device APIs
Page 27: Mobile Device APIs

http://www.w3.org/2009/dap/

Device APIs Working Group

“to create client-side APIs that enable the development of Web Applications and Web Widgets that interact with devices services such as Calendar, Contacts, Camera, etc”

Joint efforts with WebRTC Working Group, WebApps Working Group

Page 28: Mobile Device APIs

W3C Device APIs

• Media Capture (HTML)

• Media Capture (gUM)

• Contact

• Messaging

• Battery Status

• Network Information

• Sensor

• Vibration

• Calendar

• Permissions

• Menu

• Gallery

• System info

• Media Stream

Page 29: Mobile Device APIs

Public working draft, http://www.w3.org/TR/2011/WD-html-media-capture-20110414

Media Capture (HTML)

<input  type="file"  accept="image/*"  />

<!-­‐-­‐  

   The  accept  attribute  is  'a  hint'.

   Specification  suggests  that  it  can  be    used  to  offer  alternative  picker  UIs.

-­‐-­‐>

Page 30: Mobile Device APIs

Brad Lassey, September 2008

Page 31: Mobile Device APIs

Media Capture (HTML)

<input  type="file"  accept="image/*"    capture="camera"/>

<!-­‐-­‐  

   Again,  a  hint.  Valid  options  are:

       camera,  camcorder,  microphone,  filesystem

-­‐-­‐>

Page 32: Mobile Device APIs
Page 33: Mobile Device APIs

Media Capture (HTML)

<input  type="file"  accept="image/*"    capture="camera"  id="photo"/>

var  photo  =  document.getElementById('photo');photo.files[0].getFormatData(    function  (data)  {},  //  success    function  ()  {}            //  error);

//  file  is  a  MediaFile,  extending  File//  data  is  a  MediaFileData

Page 34: Mobile Device APIs

...seems to be under discussion

MediaFileData

interface  MediaFileData  {        attribute  DOMString          codecs;        attribute  unsigned  long  bitrate;        attribute  unsigned  long  height;        attribute  unsigned  long  width;        attribute  float                  duration;};

Page 35: Mobile Device APIs

Editor's draft, http://dev.w3.org/2011/webrtc/editor/getusermedia.html

Media capture (gUM)

navigator.getUserMedia(    {        audio:  true,        video:  false    },    function  (stream)  {},  //  success    function  ()  {}                //  error);

//  stream  is  a  LocalMediaStream

Page 36: Mobile Device APIs

LocalMediaStream

var  recorder  =  stream.record();

//  recorder  is  a  MediaStreamRecorder

recorder.getRecordedData(    function  (file)  {}    //  success);

//  file  is  a  File

URL.createObjectURL(stream);

//  a  Blob  URI  for,  say  <video  src='...'  />    

Page 37: Mobile Device APIs

Editor's draft, http://dev.w3.org/2009/dap/messaging/

Messaging

navigator.sendMessage(    "sms:16505551234?"  +        "body=JavaScript%20says%20hi",      [],                          //  attachments  as  File  array    function  ()  {},  //  success    function  ()  {}    //  error);

//  also  mms:  and  mailto:

//  oh...  were  you  looking  for  receipt?

Page 38: Mobile Device APIs

Editor's draft, http://dev.w3.org/2009/dap/system-info/Sensors.html

Sensorsnavigator.findSensors().onsuccess  =  function()  {    this.result.forEach(function  (sensor)  {        //  ...    });};

var  sensor  =  new  SensorConnection('Temperature');sensor.addEventListener('sensordata',  function(e)  {    if(e.data  >  20.0)  {        window.console.log('Hawt');    }});sensor.startWatch({  interval:  2000  });

Page 39: Mobile Device APIs

Sensors

• Temperature, ºC

• AmbientLight, Lux

• AmbientNoise, dbA

• MagneticField, uTesla

• Proximity, cm

• AtmPressure, kP

• RelHumidity, %

Page 40: Mobile Device APIs

Editor's draft, http://w3c-test.org/dap/contacts/

Contacts

navigator.contacts.find(    [                                              //  fields        'name',          'emails'    ],      function  (contacts)  {},  //  success    function  ()  {},                  //  error    {                                              //  find  options          filter:  'James',            multiple:  true    });

Page 41: Mobile Device APIs

Contactinterface  Contact  {        readonly  attribute  DOMString                              id;                          attribute  DOMString?                            displayName;                          attribute  ContactName?                        name;                          attribute  DOMString?                            nickname;                          attribute  ContactField[]?                  phoneNumbers;                          attribute  ContactField[]?                  emails;                          attribute  ContactAddress[]?              addresses;                          attribute  ContactField[]?                  ims;                          attribute  ContactOrganization[]?    organizations;                          attribute  Date?                                      birthday;                          attribute  DOMString?                            note;                          attribute  ContactField[]?                  photos;                          attribute  DOMString[]?                        categories;                          attribute  ContactField[]?                  urls;};

Page 42: Mobile Device APIs

Cool, but...

“The ability to add and update contact information is not a function of the API provided in this specification.”

Page 43: Mobile Device APIs

Holy Contacts Editing WTF

var  vcard  =      'BEGIN:VCARD\r\n'  +    'VERSION:3.0\r\n'  +    'UID:'  +  contact.id  +  '\r\n'  +          'N:Pearce;James\r\n'  +    'FN:James  Pearce\r\n'  +    'END:VCARD';

document.getElementById('vcard').href  =    'data:text/x-­‐vcard;charset=utf-­‐8,'  +    encodeURIComponent(vcard);  

Page 44: Mobile Device APIs
Page 45: Mobile Device APIs
Page 46: Mobile Device APIs

https://wiki.mozilla.org/WebAPI

WebAPI

“We are aiming at providing all the necessary APIs to build a basic HTML5 phone experience within the next 3-6 months”

Page 47: Mobile Device APIs

WebAPI

• Telephony

• Messaging

• Contacts

• Camera

• Network

• USB

• Embedded browser

• Battery

• Device Status

• Settings

• Mouse Lock

• Vibrator

Page 48: Mobile Device APIs

Hopefully W3C++

• “Our proposal might or might not look like it for the moment”

Page 49: Mobile Device APIs

http://www.youtube.com/watch?v=RuIQskGD3u0#t=19s

Telephony!

var  tcall  =    navigator.telephony.dial("16505551234");

tcall.readyState;    //  dialing,  ringing,  busy,  connecting,    //  connected,  disconnecting,  disconnected,      //  incoming

tcall.onconnected  =  function  ()  {};    //  onbusy,  ondisconnected  etc

tcall2.answer();

Page 50: Mobile Device APIs

Contacts

“We need read/write”

Page 51: Mobile Device APIs

USB

var  usb  =  new  MozUSBManager();

usb.addEventListener("attachdevice",      function  ()  {});usb.addEventListener("detachdevice",      function  ()  {});

usb.claimDevice({    deviceClass:  ...,    deviceSubClass:  ...});

Page 52: Mobile Device APIs
Page 53: Mobile Device APIs

We deserve Device APIs...

• As forward-looking as WTAI

• As well-structured as BONDI

• As comprehensive as WAC

• As respectable as W3C DAP

• As agile as WebAPI

Page 54: Mobile Device APIs

And we’ve got...

Page 55: Mobile Device APIs

PhoneGap

• Accelerometer

• Camera

• Capture

• Compass

• Connection

• Contacts

• Device

• Events

• File

• Geolocation

• Media

• Notification

• Storage

Page 56: Mobile Device APIs

Use Cases

Page 57: Mobile Device APIs

https://github.com/jamesgpearce/compios5

Useless Demos

Page 58: Mobile Device APIs

http://vimeo.com/adactio/iosbug https://github.com/scottjehl/iOS-Orientationchange-Fix

Fixing layout bugs, FML

window.addEventListener("deviceorientation",    function  (e)  {        orientation  =  Math.abs(w.orientation);        rotation  =  Math.abs(e.gamma);        if  (rotation  >  8  &&  orientation  ===  0)  {            if  (enabled)  {                disableZoom();            }  else  {                restoreZoom();            }        }    },  false);

Page 59: Mobile Device APIs

Camera

• Profile pictures

• Review sites

• Photo sharing

• Recognition

• Bar-codes

• Text

• Faces

Page 60: Mobile Device APIs

https://github.com/rendro/vintageJS

Filters!

$('img').vintage({    vignette:  {        black:  0.8,        white:  0.2    },    noise:  20,    screen:  {        red:  12,        ...    },    desaturate:  0.05});

Page 61: Mobile Device APIs

http://fhtr.org/JSARToolKit/

Augmented reality?

Page 62: Mobile Device APIs

Contacts & calendaring

• Add business details to address book

• Web-based messaging

• Facial recognition again...

• Presence

• Context-sensitivity of content

Page 63: Mobile Device APIs

Messaging

• Sharing

• Notifications

• Offline transport fallback

• Frictionless web sign-on

Page 64: Mobile Device APIs

Expect more

Page 65: Mobile Device APIs

I still want...

• Audio processing

• Bluetooth

• RFID & NFC

• Both cameras

• Memory & power

• Native recognition

Page 66: Mobile Device APIs

PS: does not work

window.addEventListener(    'appointmentdue',    function(appt)  {        if  (device.near(WORK))  {            siri.remind(                contacts.get('Peter'),                calendar.getDetails(appt)            );        }    },    false);

Page 67: Mobile Device APIs

James Pearce@jamespearce

http://tripleodeon.com