App Basics for FirefoxOS - Web Activities
-
0:07 - 0:10Web activities are the way that your app
-
0:10 - 0:12can talk to other apps. You don't always
-
0:12 - 0:14want to implement functionality that our
-
0:14 - 0:16other apps already implement so in this
-
0:16 - 0:18screencast I will show you how to use
-
0:18 - 0:21web activities effectively.
-
0:21 - 0:23Web activities allow your app to talk to
-
0:23 - 0:25other apps. They can be used by any app
-
0:25 - 0:27to request other apps to handle many
-
0:27 - 0:30kinds of situations. Any app can register
-
0:30 - 0:33itself as handler of a particular file type.
-
0:33 - 0:35Much like right-clicking a document
-
0:35 - 0:37on your desktop and selecting open with
-
0:37 - 0:39creates a connection between a file type
-
0:39 - 0:44and an application. For example, let's say
-
0:44 - 0:45that you want your application to let
-
0:45 - 0:48users set a background picture. There are
-
0:48 - 0:50many ways users can pick an image. They
-
0:50 - 0:52can take one with the camera, select one
-
0:52 - 0:54from the gallery, select one from
-
0:54 - 0:56wallpapers, or even paint a new picture
-
0:56 - 0:59Writing one solution for each of these
-
0:59 - 1:01use cases is hard work so why not let
-
1:01 - 1:04the system do the work for you. Using web
-
1:04 - 1:07activities, you just tell the system that
-
1:07 - 1:09you want the user to pick an image.
-
1:09 - 1:11The system then knows what kind of apps the
-
1:11 - 1:13user has that allowed to get an image
-
1:13 - 1:16and give this one back to you. You don't
-
1:16 - 1:18need to know and the user doesn't need
-
1:18 - 1:20to grant you access to the app. You just
-
1:20 - 1:22get back an image which is exactly what
-
1:22 - 1:23you wanted. Let's see how that process
-
1:23 - 1:26looks. Our application has a background
-
1:26 - 1:28image in place and we want to be able to
-
1:28 - 1:29change the image with a click of a
-
1:29 - 1:32button. Our app shouldn't have to access
-
1:32 - 1:34the data of other apps which is why we
-
1:34 - 1:36call a web activity in order to pick an
-
1:36 - 1:39image. That way the user can pick the app
-
1:39 - 1:42best suited to pick an image. In other
-
1:42 - 1:44words, we don't access other apps for the
-
1:44 - 1:49users but instead leave them in control.
-
1:49 - 1:52Now let's take a look at the code.
-
1:52 - 1:54The app is a very small self-contained HTML
-
1:54 - 1:56file with some JavaScript and an
-
1:56 - 1:59external CSS file which is not important
-
1:59 - 2:02but makes things look a bit nicer.
-
2:02 - 2:05The HTML contains only two elements: an image
-
2:05 - 2:08and a button. The image is the background
-
2:08 - 2:10of the application and will be replaced
-
2:10 - 2:11with the image we'll retrieve from the
-
2:11 - 2:13user by calling a web activity.
-
2:13 - 2:16In the JavaScript, we access the button
-
2:16 - 2:20and attach a click handler to call a web activity.
-
2:20 - 2:21We create a new activity using
-
2:21 - 2:23a mouse prefix as web activities aren't
-
2:23 - 2:26standardarized. When we create the
-
2:26 - 2:28activity, we pass it a configuration
-
2:28 - 2:30object in this case the name of the
-
2:30 - 2:32activity is pick which allows us to get
-
2:32 - 2:35pictures and the data object contains a
-
2:35 - 2:36type property with an array of mime
-
2:36 - 2:38types that will filter the kinds of
-
2:38 - 2:41images will receive. Using this the web
-
2:41 - 2:44activity knows to tell the user to give
-
2:44 - 2:47us an image. When the user clicks our
-
2:47 - 2:49button the web activities UI launches
-
2:49 - 2:51asking the user to give us an image and
-
2:51 - 2:54listing all the possible sources.
-
2:54 - 2:56Web activities are dumb requests which are
-
2:56 - 2:59objects that work is asynchronously which
-
2:59 - 3:01is why we need to define two handlers.
-
3:01 - 3:03One for the success case and another for
-
3:03 - 3:06the error case. We do that by assigning
-
3:06 - 3:08functions to the unsuccess and an error
-
3:08 - 3:12properties of the pic web activity.
-
3:12 - 3:13The success handler of the big web activity
-
3:13 - 3:16returns a blob to display a blob as an
-
3:16 - 3:18image we have to convert it to a URL
-
3:18 - 3:19object that we can assign to the image
-
3:19 - 3:24we do that by using window.url.create.object.url
-
3:24 - 3:26All we need then to
-
3:26 - 3:27display the image is to assign it to the
-
3:27 - 3:32SRC attribute of the image in the HTML document.
-
3:32 - 3:33If the error handler is called,
-
3:33 - 3:35it means that either something went
-
3:35 - 3:37wrong at some point in the flow or that
-
3:37 - 3:40the user canceled the flow altogether.
-
3:40 - 3:42If a real error occurred, we will show an
-
3:42 - 3:45alert dialog showing the error. If the
-
3:45 - 3:47user canceled the flow, we'll will simply exit
-
3:47 - 3:50for the sake of this example. Now we have
-
3:50 - 3:51our application working just as we wanted
-
3:51 - 3:53and we didn't have to implement any
-
3:53 - 3:56logic for picking images. Using web
-
3:56 - 3:58activities, the system automatically
-
3:58 - 3:59provided a wrap with the necessary
-
3:59 - 4:03resources. This example shows just a
-
4:03 - 4:04small part of the power of web
-
4:04 - 4:06activities. To get a real feel of what
-
4:06 - 4:08can be done with them, head over to the
-
4:08 - 4:10MDN wiki where you can find
-
4:10 - 4:11documentation for all the possible
-
4:11 - 4:14activities, their parameters, and examples
-
4:14 - 4:17showing how to use them.
- Title:
- App Basics for FirefoxOS - Web Activities
- Description:
-
Web Activities are an alternative way to Web APIs to access the hardware in a certain device. Instead of getting an API to speak to the device directly, Web Activities allow you to create an ecosystem of applications on the device talking to each other. For example, instead of trying to access the camera, your app would ask for an image and the users of the device can use their favourite application to take a photo. Instead of asking the users for access to the hardware (which is important in terms of security), you allow them to use applications they already trust to do that. Furthermore, you can register your application as the go-to application for certain tasks in the operating system. You can think of Web Activities as the same process right-clicking on a file in a Desktop OS does: you get several apps as the option to open this file and you have a chance to tell the OS to always use this app from now on for this kind of file. Web Activities allow apps to talk to each other - on the device, without any need of a server in-between. All they transmit from one app to the other is the final data.
More on Web Activities: https://wiki.mozilla.org/WebAPI/WebActivities
More information on the video series: https://developer.mozilla.org/en-US/Firefox_OS/Screencast_series:_App_Basics_for_Firefox_OS - Video Language:
- English
- Duration:
- 04:19
![]() |
Retired user edited English subtitles for App Basics for FirefoxOS - Web Activities | |
![]() |
Retired user edited English subtitles for App Basics for FirefoxOS - Web Activities | |
![]() |
havihoffman edited English subtitles for App Basics for FirefoxOS - Web Activities |