b2g/components/ActivitiesGlue.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/b2g/components/ActivitiesGlue.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,74 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +"use strict"
     1.9 +
    1.10 +const Cc = Components.classes;
    1.11 +const Ci = Components.interfaces;
    1.12 +const Cu = Components.utils;
    1.13 +
    1.14 +Cu.import("resource://gre/modules/XPCOMUtils.jsm");
    1.15 +Cu.import("resource://gre/modules/Services.jsm");
    1.16 +
    1.17 +XPCOMUtils.defineLazyModuleGetter(this, "SystemAppProxy",
    1.18 +                                  "resource://gre/modules/SystemAppProxy.jsm");
    1.19 +
    1.20 +function ActivitiesDialog() {
    1.21 +  this._id = 0;
    1.22 +
    1.23 +  this.activities = [];
    1.24 +}
    1.25 +
    1.26 +ActivitiesDialog.prototype = {
    1.27 +  run: function ap_run() {
    1.28 +    let id = "activity-choice" + this._id++;
    1.29 +    let activity = this.activities.shift();
    1.30 +
    1.31 +    let choices = [];
    1.32 +    activity.list.forEach(function(item) {
    1.33 +      choices.push({ manifest: item.manifest, icon: item.icon });
    1.34 +    });
    1.35 +
    1.36 +
    1.37 +    // Keep up the frond-end of an activity choice. The messages contains
    1.38 +    // a list of {names, icons} for applications able to handle this particular
    1.39 +    // activity. The front-end should display a UI to pick one.
    1.40 +    let detail = {
    1.41 +      type: "activity-choice",
    1.42 +      id: id,
    1.43 +      name: activity.name,
    1.44 +      choices: choices
    1.45 +    };
    1.46 +
    1.47 +    // Listen the resulting choice from the front-end. If there is no choice,
    1.48 +    // let's return -1, which means the user has cancelled the dialog.
    1.49 +    SystemAppProxy.addEventListener("mozContentEvent", function act_getChoice(evt) {
    1.50 +      if (evt.detail.id != id)
    1.51 +        return;
    1.52 +
    1.53 +      SystemAppProxy.removeEventListener("mozContentEvent", act_getChoice);
    1.54 +      activity.callback.handleEvent(evt.detail.value !== undefined
    1.55 +                                      ? evt.detail.value
    1.56 +                                      : -1);
    1.57 +    });
    1.58 +
    1.59 +    SystemAppProxy.dispatchEvent(detail);
    1.60 +  },
    1.61 +
    1.62 +  chooseActivity: function ap_chooseActivity(aName, aActivities, aCallback) {
    1.63 +    this.activities.push({
    1.64 +      name: aName,
    1.65 +      list: aActivities,
    1.66 +      callback: aCallback
    1.67 +    });
    1.68 +    Services.tm.currentThread.dispatch(this, Ci.nsIEventTarget.DISPATCH_NORMAL);
    1.69 +  },
    1.70 +
    1.71 +  classID: Components.ID("{70a83123-7467-4389-a309-3e81c74ad002}"),
    1.72 +
    1.73 +  QueryInterface: XPCOMUtils.generateQI([Ci.nsIActivityUIGlue, Ci.nsIRunnable])
    1.74 +}
    1.75 +
    1.76 +this.NSGetFactory = XPCOMUtils.generateNSGetFactory([ActivitiesDialog]);
    1.77 +

mercurial