firefox os workshop, jsfoo, india

104
Getting started with Firefox OS and Open Web Apps

Upload: robert-nyman

Post on 06-May-2015

1.573 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Firefox OS workshop, JSFoo, India

Getting started with Firefox OS and Open Web Apps

Page 2: Firefox OS workshop, JSFoo, India

14:30 Introduction

15:30 Get set up

15:45 Start first app

16:30 Break

16:45 Hacking

Agenda:

Page 3: Firefox OS workshop, JSFoo, India
Page 4: Firefox OS workshop, JSFoo, India
Page 6: Firefox OS workshop, JSFoo, India

Using HTML5, CSS and JavaScript together with a number of APIs to build apps and customize the UI.

Firefox OS

Page 8: Firefox OS workshop, JSFoo, India

Open Web Apps

Page 9: Firefox OS workshop, JSFoo, India
Page 10: Firefox OS workshop, JSFoo, India

HTML5 + manifest file (JSON)

Page 11: Firefox OS workshop, JSFoo, India

Steps to Take

Page 12: Firefox OS workshop, JSFoo, India

Develop Web App using HTML5, CSS, & Javascript1.

Create an app manifest file2.

Publish/install the app3.

Page 14: Firefox OS workshop, JSFoo, India

1. Develop Web App using HTML5, CSS & JavaScript

Page 15: Firefox OS workshop, JSFoo, India

Reuse any existing web site/app or develop from scratch with open web standards.

Utilize HTML5 features such as localStorage, offline manifest, IndexedDB and access Web APIs for more options.

Responsive web design for adapting to varying resolutions and screen orientation.

Page 16: Firefox OS workshop, JSFoo, India

2. Create an app manifest file

Page 17: Firefox OS workshop, JSFoo, India

Create a file with a .webapp extension

Page 18: Firefox OS workshop, JSFoo, India

{ "version": "1", "name": "Firefox OS Boilerplate App", "launch_path": "/index.html", "description": "Boilerplate Firefox OS app", "icons": { "16": "/images/logo16.png", "32": "/images/logo32.png", "48": "/images/logo48.png", "64": "/images/logo64.png", "128": "/images/logo128.png" }, "developer": { "name": "Robert Nyman", "url": "http://robertnyman.com" }, "installs_allowed_from": ["*"], "default_locale": "en"}

Page 19: Firefox OS workshop, JSFoo, India

MANIFEST CHECKERhttp://appmanifest.org/

Page 20: Firefox OS workshop, JSFoo, India

Serve with Content-type/MIME type:

application/x-web-app-manifest+json

Page 21: Firefox OS workshop, JSFoo, India

Apache - in mime.types:

application/x-web-app-manifest+json webapp

Apache - in .htaccess:

AddType application/x-web-app-manifest+json webapp

NGinx - in mime.types:

types { text/html html htm shtml; text/css css; text/xml xml; application/x-web-app-manifest+json webapp;}

Page 22: Firefox OS workshop, JSFoo, India

IIS:

In IIS Manager, right-click the local computer, and click Properties.

Click the MIME Types button.

Click New.

In the Extension box, type the file name extension.

In the MIME type box, type a description that exactly matches the file type defined on the computer.

Click OK.

Page 23: Firefox OS workshop, JSFoo, India

curl -I http://mozillalabs.com/manifest.webapp

Page 24: Firefox OS workshop, JSFoo, India

3. Publish/install the app

Page 25: Firefox OS workshop, JSFoo, India

Firefox Marketplace

Page 28: Firefox OS workshop, JSFoo, India

Installing/hosting the app

Page 29: Firefox OS workshop, JSFoo, India

var request = navigator.mozApps.install( "http://robnyman.github.com/Firefox-OS-Boilerplate-App/manifest.webapp", { user_id: "some_user" });

request.onsuccess = function() { // Success! Notification, launch page etc}

request.onerror = function() { // Failed. this.error.name has details}

Page 30: Firefox OS workshop, JSFoo, India

Build excellent interfaces!

Page 31: Firefox OS workshop, JSFoo, India

Packaged & hosted apps

Page 32: Firefox OS workshop, JSFoo, India

WebAPIs

Page 33: Firefox OS workshop, JSFoo, India
Page 34: Firefox OS workshop, JSFoo, India

Security Levels

Page 35: Firefox OS workshop, JSFoo, India

Web Content

Regular web content

Installed Web App

A regular web app

Privileged Web App

More access, more responsibility

Certified Web App

Device-critical applications

Page 37: Firefox OS workshop, JSFoo, India

"permissions": { "contacts": { "description": "Required for autocompletion in the share screen", "access": "readcreate" }, "alarms": { "description": "Required to schedule notifications" }}

Page 39: Firefox OS workshop, JSFoo, India

Vibration API (W3C)

Screen Orientation

Geolocation API

Mouse Lock API (W3C)

Open WebApps

Network Information API (W3C)

Battery Status API (W3C)

Alarm API

Web Activities

Push Notifications API

WebFM API

WebPayment

IndexedDB (W3C)

Ambient light sensor

Proximity sensor

Notification

REGULAR APIS

Page 40: Firefox OS workshop, JSFoo, India

BATTERY STATUS API

Page 41: Firefox OS workshop, JSFoo, India

var battery = navigator.battery;if (battery) { var batteryLevel = Math.round(battery.level * 100) + "%", charging = (battery.charging)? "" : "not ", chargingTime = parseInt(battery.chargingTime / 60, 10), dischargingTime = parseInt(battery.dischargingTime / 60, 10); // Set events battery.addEventListener("levelchange", setStatus, false); battery.addEventListener("chargingchange", setStatus, false); battery.addEventListener("chargingtimechange", setStatus, false); battery.addEventListener("dischargingtimechange", setStatus, false); }

Page 42: Firefox OS workshop, JSFoo, India

NOTIFICATION

Page 43: Firefox OS workshop, JSFoo, India

var notification = navigator.mozNotification;notification.createNotification( "See this", "This is a notification", iconURL);

Page 44: Firefox OS workshop, JSFoo, India

SCREENORIENTATION API

Page 45: Firefox OS workshop, JSFoo, India

// Portrait mode:screen.mozLockOrientation("portrait");

/* Possible values: "landscape" "portrait" "landscape-primary" "landscape-secondary" "portrait-primary" "portrait-secondary"*/

Page 46: Firefox OS workshop, JSFoo, India

VIBRATION API

Page 47: Firefox OS workshop, JSFoo, India

// Vibrate for one secondnavigator.vibrate(1000);

// Vibration pattern [vibrationTime, pause,…]navigator.vibrate([200, 100, 200, 100]);

// Vibrate for 5 secondsnavigator.vibrate(5000);

// Turn off vibrationnavigator.vibrate(0);

Page 48: Firefox OS workshop, JSFoo, India

WEB PAYMENTS

Page 49: Firefox OS workshop, JSFoo, India

var pay = navigator.mozPay(paymentToken);pay.onsuccess = function (event) { // Weee! Money!};

Page 51: Firefox OS workshop, JSFoo, India

mozmarket.receipts.Prompter({ storeURL: "https://marketplace.mozilla.org/app/myapp", supportHTML: '<a href="mailto:[email protected]">email [email protected]</a>', verify: true});

Page 52: Firefox OS workshop, JSFoo, India
Page 53: Firefox OS workshop, JSFoo, India

NETWORK INFORMATION API

Page 54: Firefox OS workshop, JSFoo, India

var connection = window.navigator.mozConnection, online = connection.bandwidth > 0, metered = connection.metered;

Page 55: Firefox OS workshop, JSFoo, India

ALARM API

Page 56: Firefox OS workshop, JSFoo, India

var alarmId1, request = navigator.mozAlarms.add( new Date("May 15, 2012 16:20:00"), "honorTimezone", { mydata: "my event" } ); request.onsuccess = function (event) { alarmId1 = event.target.result;};

request.onerror = function (event) { console.log(event.target.error.name);};

Page 57: Firefox OS workshop, JSFoo, India

var request = navigator.mozAlarms.getAll();

request.onsuccess = function (event) { console.log(JSON.stringify(event.target.result));};

request.onerror = function (event) { console.log(event.target.error.name);};

Page 58: Firefox OS workshop, JSFoo, India

navigator.mozSetMessageHandler( "alarm", function (message) { // Note: message has to be set in the manifest file console.log("Alarm fired: " + JSON.stringify(message)); });

Page 59: Firefox OS workshop, JSFoo, India

DEVICEPROXIMITY

Page 60: Firefox OS workshop, JSFoo, India

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 61: Firefox OS workshop, JSFoo, India

AMBIENT LIGHT EVENTS

Page 62: Firefox OS workshop, JSFoo, India

window.addEventListener("devicelight", function (event) { // The lux values for "dim" typically begin below 50, // and the values for "bright" begin above 10000 console.log(event.value);});

Page 63: Firefox OS workshop, JSFoo, India

PAGE VISIBILITY

Page 64: Firefox OS workshop, JSFoo, India

document.addEventListener("visibilitychange", function () { if (document.hidden) { console.log("App is hidden"); } else { console.log("App has focus"); }});

Page 65: Firefox OS workshop, JSFoo, India

Device Storage API

Browser API

TCP Socket API

Contacts API

systemXHR

PRIVILEGED APIS

Page 66: Firefox OS workshop, JSFoo, India

DEVICE STORAGE API

Page 67: Firefox OS workshop, JSFoo, India

var deviceStorage = navigator.getDeviceStorage("videos");

Page 68: Firefox OS workshop, JSFoo, India

var storage = navigator.getDeviceStorage("videos"), cursor = storage.enumerate(); cursor.onerror = function() { console.error("Error in DeviceStorage.enumerate()", cursor.error.name);};

cursor.onsuccess = function() { if (!cursor.result) return; var file = cursor.result;

// If this isn't a video, skip it if (file.type.substring(0, 6) !== "video/") { cursor.continue(); return; }

// If it isn't playable, skip it var testplayer = document.createElement("video"); if (!testplayer.canPlayType(file.type)) { cursor.continue(); return; }};

Page 69: Firefox OS workshop, JSFoo, India

CONTACTS API

Page 70: Firefox OS workshop, JSFoo, India

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 71: Firefox OS workshop, JSFoo, India

WebTelephony

WebSMS

Idle API

Settings API

Power Management API

Mobile Connection API

WiFi Information API

WebBluetooth

Permissions API

Network Stats API

Camera API

Time/Clock API

Attention screen

Voicemail

CERTIFIED APIS

Page 72: Firefox OS workshop, JSFoo, India

WEBTELEPHONY

Page 73: Firefox OS workshop, JSFoo, India

// Telephony objectvar 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);

Page 74: Firefox OS workshop, JSFoo, India

// Place a callvar cal = tel.dial(“123456789”);

Page 75: Firefox OS workshop, JSFoo, India

// Receiving a calltel.onincoming = function (event) { var incomingCall = event.call;

// Get the number of the incoming call console.log(incomingCall.number);

// Answer the call incomingCall.answer();};

// Disconnect a callcall.hangUp();

// Iterating over calls, and taking action depending on their changed statustel.oncallschanged = function (event) { tel.calls.forEach(function (call) { // Log the state of each call console.log(call.state); });};

Page 76: Firefox OS workshop, JSFoo, India

WEBSMS

Page 77: Firefox OS workshop, JSFoo, India

// SMS objectvar sms = navigator.mozSMS;

// Send a messagesms.send("123456789", "Hello world!");

Page 78: Firefox OS workshop, JSFoo, India

// Recieve a messagesms.onreceived = function (event) { // Read message console.log(event.message);};

Page 79: Firefox OS workshop, JSFoo, India

WEB ACTIVITIES

Page 81: Firefox OS workshop, JSFoo, India
Page 82: Firefox OS workshop, JSFoo, India

var activity = new MozActivity({ name: "view", data: { type: "image/png", url: ... }});

activity.onsuccess = function () { console.log("Showing the image!");};

activity.onerror = function () { console.log("Can't view the image!");};

Page 83: Firefox OS workshop, JSFoo, India

{ "activities": { "share": { "filters": { "type": ["image/png", "image/gif"] } "href": "sharing.html", "disposition": "window" } }}

Page 84: Firefox OS workshop, JSFoo, India

var register = navigator.mozRegisterActivityHandler({ name: "view", disposition: "inline", filters: { type: "image/png" }});

register.onerror = function () { console.log("Failed to register activity");}

Page 85: Firefox OS workshop, JSFoo, India

navigator.mozSetMessageHandler("activity", function (a) { var img = getImageObject(); img.src = a.source.url; // Call a.postResult() or a.postError() if // the activity should return a value});

Page 86: Firefox OS workshop, JSFoo, India
Page 87: Firefox OS workshop, JSFoo, India

Future APIs

Page 88: Firefox OS workshop, JSFoo, India
Page 89: Firefox OS workshop, JSFoo, India

Resource lock API

UDP Datagram Socket API

Peer to Peer API

WebNFC

WebUSB

HTTP-cache API

Calendar API

Spellcheck API

LogAPI

Keyboard/IME API

WebRTC

FileHandle API

Sync API

Page 90: Firefox OS workshop, JSFoo, India

Web Apps from Mozilla

Page 91: Firefox OS workshop, JSFoo, India
Page 92: Firefox OS workshop, JSFoo, India

Dialer

Contacts

Settings

SMS

Web browser

Gallery

Video Player

Music Player

E-mail (POP, IMAP)

Calendar

Alarm Clock

Camera

Notes

First Run Experience

Notifications

Home Screen

Mozilla Marketplace

System Updater

Localization Support

Page 94: Firefox OS workshop, JSFoo, India

Web Components & Mozilla Brick

Page 96: Firefox OS workshop, JSFoo, India

<x-flipbox> <div>I'm the front face.</div> <div>And I'm the back face.</div></x-flipbox>

Page 97: Firefox OS workshop, JSFoo, India

// assume that toggleButton and flipBox are already// defined as their respective DOM elementstoggleButton.addEventListener("click", function(){ flipBox.toggle(); });

Page 98: Firefox OS workshop, JSFoo, India

appbarcalendardatepickerdeckflipboxiconbuttonlayout

slideboxslidertabbartoggletogglegrouptooltip

Page 99: Firefox OS workshop, JSFoo, India

Get started

Page 102: Firefox OS workshop, JSFoo, India

Trying things out

Page 103: Firefox OS workshop, JSFoo, India