On Github lmorchard / fxos-at-selfconference
self.conference / Detroit / May 30, 2014
{computer,web,mad} scientist
{tech,scifi} writer
home{brew,roast}er
Mozillian
Hi, my name is Les Orchard Here, you can see me in my natural habitat working at home with a cat climbing me. I’ve been a computer nerd since the 80’s, I found the web when I was in college in the 90’s, and that's what I've been doing ever since. These days I also like tinkering with random gadgets and electronics with blinking things I’ve gotten a few tech books published. I write little scifi stories, but I haven't gotten any of those published. I make beer and I roast coffee. Oh, and I work for Mozilla. Six years this month.Mozilla & Firefox OS
Apps & the Open Web
Web APIs & Web Activities
Developer Tools
Seriously, what about native?
So, with the introductions out of the way, I just want to make sure that you're in the right room and I'm not wasting any more of your time. Here's a preview of what I'm going to cover today. (zoom out?)
~7 mobile operator partners, ~3 hardware partners
And this is not vaporware. There are retail devices running Firefox OS available now. In fact, first device was launched last July (2013). And now, I think we're up to at least 7 operator partners, 3 hardware partners. At least, that's what I've seen lately from my colleagues' presentations. Not to be a hipster, but you probably haven't heard of any of them, because they're pretty much all released outside the US.Appetizers!
So, we all know what the word "app" means, right? Appetizers!Appetizers!
No!
Single task
Limited attention
Focused UX
Touch UI
Cost $1 to $5 ($50 is really pushing it)
Apps tend to be focused on a single task Apps tend to have UI simplified for mobile and touch and smaller screens Apps tend to accommodate limited attention Apps tend to cost $1 or $5 or maybe $10, but that's pushing it Anyone remember the $50 Wolfram Alpha app that came out in Fall 2009? By spring 2010 it was $2 And actually, if you remember even farther back, PalmOS had apps. But, they sold for prices anywhere from $10 to $100. And, sometimes, you had to buy them on CDs that they sold in malls and you installed them with a HotSync cradle. ... Man, it's great living in the future.https://my-app.example.com/manifest.webapp
Content-Type: application/x-web-app-manifest+json
{ "name": "My App", "description": "My elevator pitch goes here", "launch_path": "/index.html", "icons": { "128": "/img/icon-128.png" }, "developer": { "name": "Your name or organization", "url": "http://your-homepage-here.org" }, "default_locale": "en" }So, even though "installing" a web app doesn't need to be more than a bookmark with special UI, it can be more. This is what we're calling an "Open Web App Manifest". It's a JSON file that lives on your web server at a public URL, and it describes a web app. These are some of the basics, like: Name, description, the home page, some icons, and some details on the developer. There's a lot more that can go in here. But, I'll get to that in a bit.
var manifestUrl = 'https://my-app.example.com/manifest.webapp'; var request = window.navigator.mozApps.install(manifestUrl); request.onsuccess = function () { // Save the App object that is returned var appRecord = this.result; alert('Installation successful!'); }; request.onerror = function () { // Display the error information from the DOMError object alert('Install failed, error: ' + this.error.name); };Here's how you install an open web app using its manifest. This is the code behind the install button on the Firefox Marketplace. You can use this code, too. Wire it up to a button on your own web page, and you've got an app store. It's an API that Mozilla has proposed as a new open standard. And like I said, this works on desktop as well as mobile. This is Mozilla's answer to "Add to home screen".
window.navigator.vibrate(200); window.navigator.vibrate([200, 100, 200]);So. when I say "add to the Open Web platform", I mean something like this. You don't need to install a special plugin or SDK. This comes out of the box with the browser and the web app runtime (otherwise known as Gecko).
var battery = navigator.battery || navigator.mozBattery || navigator.webkitBattery; function updateBatteryStatus() { console.log("Battery status: " + battery.level * 100 + " %"); if (battery.charging) { console.log("Battery is charging"); } } battery.addEventListener("chargingchange", updateBatteryStatus); battery.addEventListener("levelchange", updateBatteryStatus); updateBatteryStatus();And here's a more complex example. You can see that the feature is used here with moz and webkit prefixes, because it's on its way to becoming an official standard. But, you can see this works much like any other web API, using event listners and such.
/* For portrait, we want the tool bar on top */ @media screen and (orientation: portrait) { #toolbar { width: 100%; } } /* For landscape, we want the tool bar stick on the left */ @media screen and (orientation: landscape) { #toolbar { position: fixed; width: 2.65em; height: 100%; } }
var screen = window.screen; screen.addEventListener("orientationchange", function () { console.log("The orientation of the screen is: " + screen.orientation); }); screen.lockOrientation('landscape');Some of these APIs are actually useful in both CSS and JavaScript.
function handleOrientation(event) { var absolute = event.absolute; var z_rot = event.alpha; var x_rot = event.beta; var y_rot = event.gamma; // Do stuff with the new orientation data } window.addEventListener("deviceorientation", handleOrientation, true);There's also a Device Orientation API. Which, though it influences Screen Orientation, is exposed as its own API as well. Here, you can see some quick code that responds to changes in the device's orientation by listening for a "deviceorientation" event. There's also a "devicemotion" event that reports on physicaly acceleration on all three axes, as well as the rate of rotation on those axes.
So this is a particularly interesting Web API. It enables you to call on other apps as APIs. But, you as the app developer don't need to be aware of any other apps in particular. It centers around activities, like picking an image. Multiple apps can register to handle that activity, but the user gets to choose which one will satisfy the request. In other words, it means that this app here on the left can delegate finding an image to one of the apps listed on the right. It also helps reduce the number of apps that need access to hardware. If I can just pick an image from the gallery or launch the camera app, then my app doesn't need camera hardware access itself. This API is inspired by Intents on Android, and in fact I think there is work to allow web apps on Android to use Intents via the Web Activities API.
{ // Other App Manifest related stuff // Activity registration "activities": { // The name of the activity to handle (here "pick") "pick": { "href": "./pick.html", "disposition": "inline", "filters": { "type": ["image/*","image/jpeg","image/png"] }, "returnValue": true } } }Remember when I mentioned earlier that an Open Web App manifest could contain a bunch of other things? Declaring what activities an app can handle is one of those things. You can also specify more details about the situations where your app is relevant to the activity - like the content type filter shown here.
navigator.mozSetMessageHandler('activity', function(activityRequest) { var option = activityRequest.source; if (option.name === "pick") { // Do something to handle the activity ... // Send back the result if (picture) { activityRequest.postResult(picture); } else { activityRequest.postError("Unable to provide a picture"); } } });And then, you can use code like this in an app to handle activity requests.
var activity = new MozActivity({ // Ask for the "pick" activity name: "pick", // Provide the data required by // the filters of the activity data: { type: "image/jpeg" } }); activity.onsuccess = function() { var picture = this.result; console.log("A picture has been retrieved"); }; activity.onerror = function() { console.log(this.error); };On the other side of things, you can kick off an activity with code like this. Notice that this asks for both an activity by name and specifies the content type required. This matches up with the filters declared in the app manifest. If this code had been asking for an audio file, rather than an image, then the registered app would not have been presented as a choice to the user.
"permissions": { "contacts": { "description": "Required for autocompletion", "access": "readcreate" }, "alarms": { "description": "Required to schedule notifications" } }And when you do need to use privileged APIs, there's an optional "permissions" key in the Open Web App manifest where you need to list out what your app plans to use.
{ live demo goes here }
Now, I'm going to attempt to show a quick live demo of the Developer Tools in Firefox with a real Firefox OS device. In case it fails, I have some boring backup screenshots to show. But let's hope it works. - install fxos simulator addon - install fxos simulators (1.4, 2.0, adb helper) - show phone connection - install the fxos boilerplate app https://github.com/robnyman/Firefox-OS-Boilerplate-App - install on phone - modify live DOM on phone - use Photo Booth to show the changes?