2012-07-20 08:41:30 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
"use strict"
|
|
|
|
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cu = Components.utils;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
|
2014-04-07 06:59:48 -07:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "SystemAppProxy",
|
|
|
|
"resource://gre/modules/SystemAppProxy.jsm");
|
|
|
|
|
2012-07-20 08:41:30 -07:00
|
|
|
function ActivitiesDialog() {
|
|
|
|
this._id = 0;
|
|
|
|
|
|
|
|
this.activities = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
ActivitiesDialog.prototype = {
|
|
|
|
run: function ap_run() {
|
|
|
|
let id = "activity-choice" + this._id++;
|
|
|
|
let activity = this.activities.shift();
|
|
|
|
|
|
|
|
let choices = [];
|
|
|
|
activity.list.forEach(function(item) {
|
2013-01-07 06:48:56 -08:00
|
|
|
choices.push({ manifest: item.manifest, icon: item.icon });
|
2012-07-20 08:41:30 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// Keep up the frond-end of an activity choice. The messages contains
|
|
|
|
// a list of {names, icons} for applications able to handle this particular
|
|
|
|
// activity. The front-end should display a UI to pick one.
|
2012-08-19 12:00:19 -07:00
|
|
|
let detail = {
|
2012-07-20 08:41:30 -07:00
|
|
|
type: "activity-choice",
|
|
|
|
id: id,
|
|
|
|
name: activity.name,
|
|
|
|
choices: choices
|
2012-08-19 12:00:19 -07:00
|
|
|
};
|
2012-07-20 08:41:30 -07:00
|
|
|
|
2014-10-30 22:50:00 -07:00
|
|
|
if (activity.type) {
|
|
|
|
detail.activityType = activity.type;
|
|
|
|
}
|
|
|
|
|
2012-07-20 08:41:30 -07:00
|
|
|
// Listen the resulting choice from the front-end. If there is no choice,
|
|
|
|
// let's return -1, which means the user has cancelled the dialog.
|
2014-04-07 06:59:48 -07:00
|
|
|
SystemAppProxy.addEventListener("mozContentEvent", function act_getChoice(evt) {
|
2012-07-20 08:41:30 -07:00
|
|
|
if (evt.detail.id != id)
|
|
|
|
return;
|
|
|
|
|
2014-04-07 06:59:48 -07:00
|
|
|
SystemAppProxy.removeEventListener("mozContentEvent", act_getChoice);
|
2014-04-25 07:29:00 -07:00
|
|
|
activity.callback.handleEvent(Ci.nsIActivityUIGlueCallback.WEBAPPS_ACTIVITY,
|
|
|
|
evt.detail.value !== undefined
|
2013-09-05 09:45:16 -07:00
|
|
|
? evt.detail.value
|
|
|
|
: -1);
|
2012-07-20 08:41:30 -07:00
|
|
|
});
|
|
|
|
|
2014-04-07 06:59:48 -07:00
|
|
|
SystemAppProxy.dispatchEvent(detail);
|
2012-07-20 08:41:30 -07:00
|
|
|
},
|
|
|
|
|
2014-04-25 07:28:00 -07:00
|
|
|
chooseActivity: function ap_chooseActivity(aOptions, aActivities, aCallback) {
|
2014-04-25 07:29:00 -07:00
|
|
|
// B2G does not have an alternate activity system, make no choice and return.
|
|
|
|
if (aActivities.length === 0) {
|
2014-05-12 16:30:30 -07:00
|
|
|
aCallback.handleEvent(Ci.nsIActivityUIGlueCallback.WEBAPPS_ACTIVITY, -1);
|
2014-04-25 07:29:00 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-30 22:50:00 -07:00
|
|
|
let activity = {
|
2014-04-25 07:28:00 -07:00
|
|
|
name: aOptions.name,
|
2012-07-20 08:41:30 -07:00
|
|
|
list: aActivities,
|
|
|
|
callback: aCallback
|
2014-10-30 22:50:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
if (aOptions.data && aOptions.data.type) {
|
|
|
|
activity.type = aOptions.data.type;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.activities.push(activity);
|
2012-07-20 08:41:30 -07:00
|
|
|
Services.tm.currentThread.dispatch(this, Ci.nsIEventTarget.DISPATCH_NORMAL);
|
|
|
|
},
|
|
|
|
|
2014-04-25 07:28:00 -07:00
|
|
|
classID: Components.ID("{3a54788b-48cc-4ab4-93d6-0d6a8ef74f8e}"),
|
2012-07-20 08:41:30 -07:00
|
|
|
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIActivityUIGlue, Ci.nsIRunnable])
|
|
|
|
}
|
|
|
|
|
2012-10-31 09:13:28 -07:00
|
|
|
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([ActivitiesDialog]);
|
2012-07-20 08:41:30 -07:00
|
|
|
|