42
Firefox OS Web APIs, taking it to the next level Frédéric Harper Firefox OS Technical Evangelist @ Mozilla @fharper | outofcomfortzone.net Krakow Firefox OS workshop – 2013-09-12

Firefox OS Web APIs, taking it to the next level

  • View
    976

  • Download
    0

Embed Size (px)

DESCRIPTION

A quick overview of the Firefox OS Web APIs

Citation preview

Page 1: Firefox OS Web APIs, taking it to the next level

Firefox OS Web APIs, taking it to the next level

Frédéric Harper

Firefox OS Technical Evangelist @ Mozilla

@fharper | outofcomfortzone.net

Kra

kow

Fir

efo

x O

S w

ork

shop –

20

13

-09

-12

Page 2: Firefox OS Web APIs, taking it to the next level

Taking it to the next level

Web APIs

Regular APIs

Privileged APIs

Certified APIs

Page 3: Firefox OS Web APIs, taking it to the next level

Regular APIs

Page 4: Firefox OS Web APIs, taking it to the next level

Regular APIs

packaged hosted

Page 5: Firefox OS Web APIs, taking it to the next level

Regular APIsAmbient Light

Page 6: Firefox OS Web APIs, taking it to the next level

Ambient Light Sensor

window.addEventListener("devicelight", function (event) {

// The level of the ambient light in lux

// The lux values for "dim" typically begin below 50,

// and the values for "bright" begin above 10000

console.log(event.value);

});

Page 7: Firefox OS Web APIs, taking it to the next level

DEMO Firefox OS Boilerplate App

Page 8: Firefox OS Web APIs, taking it to the next level

Regular APIsBattery Status

Page 9: Firefox OS Web APIs, taking it to the next level

Battery Status

var battery = navigator.battery;

if (battery) {

var batteryLevel = Math.round(battery.level * 100) + "%",

 

charging = (battery.charging)? “yes" : "no",

chargingTime = parseInt(battery.chargingTime / 60, 10,

dischargingTime = parseInt(battery.dischargingTime / 60, 10);

 

battery.addEventListener("levelchange", setStatus, false);

battery.addEventListener("chargingchange", setStatus, false);

battery.addEventListener("chargingtimechange", setStatus, false);

}

Page 10: Firefox OS Web APIs, taking it to the next level

DEMO Firefox OS Boilerplate App

Page 11: Firefox OS Web APIs, taking it to the next level

Regular APIsDevice Light

Page 12: Firefox OS Web APIs, taking it to the next level

Device Light

window.addEventListener("lightlevel", function (event) {

console.log(event.value);

});

 

/*

Possible values:

"normal"

"bright"

"dim"

*/

Page 13: Firefox OS Web APIs, taking it to the next level

Regular APIsNetwork Information

Page 14: Firefox OS Web APIs, taking it to the next level

Network Information

var connection = window.navigator.mozConnection,

online = connection.bandwidth > 0,

metered = connection.metered;

connection.addEventListener("change", updateConnectionStatus);

Page 15: Firefox OS Web APIs, taking it to the next level

Regular APIsNotification

Page 16: Firefox OS Web APIs, taking it to the next level

Notification

var notification = navigator.mozNotification;

notification.createNotification(

"See this",

"This is a notification",

iconURL

);

Page 17: Firefox OS Web APIs, taking it to the next level

A good article on Push Notification

http://j.mp/fxosPushPost

Page 18: Firefox OS Web APIs, taking it to the next level

Regular APIsProximity Sensor

Page 19: Firefox OS Web APIs, taking it to the next level

Proximity Sensor

window.addEventListener("deviceproximity", function (event) {

// Current device proximity, in centimeters

console.log(event.value);

 

// The maximum sensing distance the sensor is able to report, in centimeters

console.log(event.max);

 

// The minimum sensing distance the sensor is able to report, in centimeters

console.log(event.min);

});

Page 20: Firefox OS Web APIs, taking it to the next level

Regular APIsScreen Orientation

Page 21: Firefox OS Web APIs, taking it to the next level

Screen Orientation

// Portrait mode:

screen.mozLockOrientation("portrait");

/*

Possible values:

"landscape"

"portrait"

*/

Page 22: Firefox OS Web APIs, taking it to the next level

Screen Orientation

window.addEventListener(“onmozorientationchange”, function (event) {

var orientation = screen.mozOrientation;

}

/*

Possible values:

"landscape-primary"

"landscape-secondary"

"portrait-primary"

"portrait-secondary"

*/

Page 23: Firefox OS Web APIs, taking it to the next level

DEMO Paint app

Page 24: Firefox OS Web APIs, taking it to the next level

Regular APIsVibration

Page 25: Firefox OS Web APIs, taking it to the next level

Vibration

// Vibrate for 1 second

navigator.vibrate(1000);

 

// Vibration pattern [vibrationTime, pause, ...]

navigator.vibrate([200, 100, 200, 100]);

 

// Vibrate for 5 seconds

navigator.vibrate(5000);

 

// Turn off vibration

navigator.vibrate(0);

Page 26: Firefox OS Web APIs, taking it to the next level

Privileged APIs

Page 27: Firefox OS Web APIs, taking it to the next level

Privileged APIs

packaged

Page 28: Firefox OS Web APIs, taking it to the next level

Privileged APIsBrowser

Page 29: Firefox OS Web APIs, taking it to the next level

Browser

<div>

<span id='location-bar'></span>

<button onclick='go_button_clicked()'>Go</button>

</div>

<div id='load-status'></div>

<div id='security-status'></div>

<img id='favicon'>

<div id='title'></div>

<iframe mozbrowser src=‘yoursite.com' id='browser'></iframe>

Page 30: Firefox OS Web APIs, taking it to the next level

Browser

addEventListener('mozbrowserloadstart', function(e) {

//Do stuff

});

/*

Possible values:

"mozbrowserloadstart“ "mozbrowserloadend"

"mozbrowserlocationchange“ "mozbrowsertitlechange"

"mozbrowsericonchange“ "mozbrowsersecuritychange"

"mozbrowsercontextmenu“ "mozbrowsererror"

"mozbrowserkeyevent“ "mozbrowsershowmodalprompt"

"mozbrowseropenwindow“ "mozbrowserclose"

*/

Page 31: Firefox OS Web APIs, taking it to the next level

Privileged APIsContacts

Page 32: Firefox OS Web APIs, taking it to the next level

Contacts

var contact = new mozContact();

contact.init({name: "Tom"});

 

var request = navigator.mozContacts.save(contact);

request.onsuccess = function() {

console.log("Success");

};

 

request.onerror = function() {

console.log("Error")

};

Page 33: Firefox OS Web APIs, taking it to the next level

Certified APIs

Page 34: Firefox OS Web APIs, taking it to the next level

Certified APIsWebSMS

Page 35: Firefox OS Web APIs, taking it to the next level

WebSMS

// SMS object

var sms = navigator.mozSMS;

 

// Send a message

sms.send("123456789", "Hello world!");

// Receive a message

sms.onreceived = function (event) {

// Read message

console.log(event.message);

};

Page 36: Firefox OS Web APIs, taking it to the next level

Certified APIsWebTelephony

Page 37: Firefox OS Web APIs, taking it to the next level

WebTelephony

// Telephony object

var tel = navigator.mozTelephony;

 

// Check if the phone is muted (read/write property)

console.log(tel.muted);

 

// Check if the speaker is enabled (read/write property)

console.log(tel.speakerEnabled);

// Place a call

var cal = tel.dial(“123456789”);

Page 38: Firefox OS Web APIs, taking it to the next level

WebTelephony

// Receiving a call

tel.onincoming = function (event) {

var incomingCall = event.call;

 

// Get the number of the incoming call

console.log(incomingCall.number);

 

// Answer the call

incomingCall.answer();

};

Page 39: Firefox OS Web APIs, taking it to the next level

WebTelephony

// Disconnect a call

call.hangUp();

 

// Iterating over calls, and taking action depending on their changed status

tel.oncallschanged = function (event) {

tel.calls.forEach(function (call) {

// Log the state of each call

console.log(call.state);

});

};

Page 40: Firefox OS Web APIs, taking it to the next level

Note that for some of the Web API, you’ll need to add

permission to the manifest file.

Page 41: Firefox OS Web APIs, taking it to the next level

Resources

Web API - Mozilla Developer Network http://j.mp/fxosWebAPI

Packages vs hosted apps – Firefox Marketplace Developer Hubhttp://j.mp/fxosQuickStart

Firefox OS Boilerplate App - GitHubhttp://j.mp/fxosBoilerplate

Page 42: Firefox OS Web APIs, taking it to the next level

Frédéric Harper

[email protected]@fharper

http://hacks.mozilla.comhttp://outofcomfortzone.net