Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
1 /* ***** BEGIN LICENSE BLOCK *****
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/.
6 *
7 * ***** END LICENSE BLOCK ***** */
9 let Ci = Components.interfaces, Cc = Components.classes, Cu = Components.utils;
11 Cu.import("resource://gre/modules/Services.jsm")
12 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
13 Cu.import("resource://gre/modules/AppsUtils.jsm");
15 #ifdef MOZ_ANDROID_SYNTHAPKS
16 XPCOMUtils.defineLazyModuleGetter(this, "WebappManager", "resource://gre/modules/WebappManager.jsm");
17 #endif
19 const DEFAULT_ICON = "chrome://browser/skin/images/default-app-icon.png";
21 let gStrings = Services.strings.createBundle("chrome://browser/locale/aboutApps.properties");
23 XPCOMUtils.defineLazyGetter(window, "gChromeWin", function()
24 window.QueryInterface(Ci.nsIInterfaceRequestor)
25 .getInterface(Ci.nsIWebNavigation)
26 .QueryInterface(Ci.nsIDocShellTreeItem)
27 .rootTreeItem
28 .QueryInterface(Ci.nsIInterfaceRequestor)
29 .getInterface(Ci.nsIDOMWindow)
30 .QueryInterface(Ci.nsIDOMChromeWindow));
32 document.addEventListener("DOMContentLoaded", onLoad, false);
34 var AppsUI = {
35 uninstall: null,
36 shortcut: null
37 };
39 function openLink(aEvent) {
40 try {
41 let formatter = Cc["@mozilla.org/toolkit/URLFormatterService;1"].getService(Ci.nsIURLFormatter);
42 let url = formatter.formatURLPref(aEvent.currentTarget.getAttribute("pref"));
43 let BrowserApp = gChromeWin.BrowserApp;
44 BrowserApp.addTab(url, { selected: true, parentId: BrowserApp.selectedTab.id });
45 } catch (ex) {}
46 }
48 #ifdef MOZ_ANDROID_SYNTHAPKS
49 function checkForUpdates(aEvent) {
50 WebappManager.checkForUpdates(true);
51 }
52 #endif
54 #ifndef MOZ_ANDROID_SYNTHAPKS
55 var ContextMenus = {
56 target: null,
58 init: function() {
59 document.addEventListener("contextmenu", this, false);
60 document.getElementById("addToHomescreenLabel").addEventListener("click", this.addToHomescreen, false);
61 document.getElementById("uninstallLabel").addEventListener("click", this.uninstall, false);
62 },
64 handleEvent: function(event) {
65 // store the target of context menu events so that we know which app to act on
66 this.target = event.target;
67 while (!this.target.hasAttribute("contextmenu")) {
68 this.target = this.target.parentNode;
69 }
70 },
72 addToHomescreen: function() {
73 let manifest = this.target.manifest;
74 gChromeWin.WebappsUI.createShortcut(manifest.name, manifest.fullLaunchPath(), manifest.biggestIconURL || DEFAULT_ICON, "webapp");
75 this.target = null;
76 },
78 uninstall: function() {
79 navigator.mozApps.mgmt.uninstall(this.target.app);
81 let manifest = this.target.manifest;
82 gChromeWin.sendMessageToJava({
83 type: "Shortcut:Remove",
84 title: manifest.name,
85 url: manifest.fullLaunchPath(),
86 origin: this.target.app.origin,
87 shortcutType: "webapp"
88 });
89 this.target = null;
90 }
91 }
92 #endif
94 function onLoad(aEvent) {
95 let elmts = document.querySelectorAll("[pref]");
96 for (let i = 0; i < elmts.length; i++) {
97 elmts[i].addEventListener("click", openLink, false);
98 }
100 #ifdef MOZ_ANDROID_SYNTHAPKS
101 document.getElementById("update-item").addEventListener("click", checkForUpdates, false);
102 #endif
104 navigator.mozApps.mgmt.oninstall = onInstall;
105 navigator.mozApps.mgmt.onuninstall = onUninstall;
106 updateList();
108 #ifndef MOZ_ANDROID_SYNTHAPKS
109 ContextMenus.init();
110 #endif
111 }
113 function updateList() {
114 let grid = document.getElementById("appgrid");
115 while (grid.lastChild) {
116 grid.removeChild(grid.lastChild);
117 }
119 let request = navigator.mozApps.mgmt.getAll();
120 request.onsuccess = function() {
121 for (let i = 0; i < request.result.length; i++)
122 addApplication(request.result[i]);
123 if (request.result.length)
124 document.getElementById("main-container").classList.remove("hidden");
125 }
126 }
128 function addApplication(aApp) {
129 let list = document.getElementById("appgrid");
130 let manifest = new ManifestHelper(aApp.manifest, aApp.origin);
132 let container = document.createElement("div");
133 container.className = "app list-item";
134 #ifndef MOZ_ANDROID_SYNTHAPKS
135 container.setAttribute("contextmenu", "appmenu");
136 #endif
137 container.setAttribute("id", "app-" + aApp.origin);
138 container.setAttribute("mozApp", aApp.origin);
139 container.setAttribute("title", manifest.name);
141 let img = document.createElement("img");
142 img.src = manifest.biggestIconURL || DEFAULT_ICON;
143 img.onerror = function() {
144 // If the image failed to load, and it was not our default icon, attempt to
145 // use our default as a fallback.
146 if (img.src != DEFAULT_ICON) {
147 img.src = DEFAULT_ICON;
148 }
149 }
150 img.setAttribute("title", manifest.name);
152 let title = document.createElement("div");
153 title.appendChild(document.createTextNode(manifest.name));
155 container.appendChild(img);
156 container.appendChild(title);
157 list.appendChild(container);
159 container.addEventListener("click", function(aEvent) {
160 aApp.launch();
161 }, false);
162 container.app = aApp;
163 container.manifest = manifest;
164 }
166 function onInstall(aEvent) {
167 let node = document.getElementById("app-" + aEvent.application.origin);
168 if (node)
169 return;
171 addApplication(aEvent.application);
172 document.getElementById("main-container").classList.remove("hidden");
173 }
175 function onUninstall(aEvent) {
176 let node = document.getElementById("app-" + aEvent.application.origin);
177 if (node) {
178 let parent = node.parentNode;
179 parent.removeChild(node);
180 if (!parent.firstChild)
181 document.getElementById("main-container").classList.add("hidden");
182 }
183 }