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