< Return to Video

App Basics for FirefoxOS - Web Activities

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

more » « less
Video Language:
English
Duration:
04:19

English subtitles

Revisions