mobile/android/chrome/content/aboutApps.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/chrome/content/aboutApps.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,184 @@
     1.4 +/* ***** BEGIN LICENSE BLOCK *****
     1.5 + *
     1.6 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.8 + * You can obtain one at http://mozilla.org/MPL/2.0/.
     1.9 + *
    1.10 + * ***** END LICENSE BLOCK ***** */
    1.11 +
    1.12 +let Ci = Components.interfaces, Cc = Components.classes, Cu = Components.utils;
    1.13 +
    1.14 +Cu.import("resource://gre/modules/Services.jsm")
    1.15 +Cu.import("resource://gre/modules/XPCOMUtils.jsm");
    1.16 +Cu.import("resource://gre/modules/AppsUtils.jsm");
    1.17 +
    1.18 +#ifdef MOZ_ANDROID_SYNTHAPKS
    1.19 +XPCOMUtils.defineLazyModuleGetter(this, "WebappManager", "resource://gre/modules/WebappManager.jsm");
    1.20 +#endif
    1.21 +
    1.22 +const DEFAULT_ICON = "chrome://browser/skin/images/default-app-icon.png";
    1.23 +
    1.24 +let gStrings = Services.strings.createBundle("chrome://browser/locale/aboutApps.properties");
    1.25 +
    1.26 +XPCOMUtils.defineLazyGetter(window, "gChromeWin", function()
    1.27 +  window.QueryInterface(Ci.nsIInterfaceRequestor)
    1.28 +    .getInterface(Ci.nsIWebNavigation)
    1.29 +    .QueryInterface(Ci.nsIDocShellTreeItem)
    1.30 +    .rootTreeItem
    1.31 +    .QueryInterface(Ci.nsIInterfaceRequestor)
    1.32 +    .getInterface(Ci.nsIDOMWindow)
    1.33 +    .QueryInterface(Ci.nsIDOMChromeWindow));
    1.34 +
    1.35 +document.addEventListener("DOMContentLoaded", onLoad, false);
    1.36 +
    1.37 +var AppsUI = {
    1.38 +  uninstall: null,
    1.39 +  shortcut: null
    1.40 +};
    1.41 +
    1.42 +function openLink(aEvent) {
    1.43 +  try {
    1.44 +    let formatter = Cc["@mozilla.org/toolkit/URLFormatterService;1"].getService(Ci.nsIURLFormatter);
    1.45 +    let url = formatter.formatURLPref(aEvent.currentTarget.getAttribute("pref"));
    1.46 +    let BrowserApp = gChromeWin.BrowserApp;
    1.47 +    BrowserApp.addTab(url, { selected: true, parentId: BrowserApp.selectedTab.id });
    1.48 +  } catch (ex) {}
    1.49 +}
    1.50 +
    1.51 +#ifdef MOZ_ANDROID_SYNTHAPKS
    1.52 +function checkForUpdates(aEvent) {
    1.53 +  WebappManager.checkForUpdates(true);
    1.54 +}
    1.55 +#endif
    1.56 +
    1.57 +#ifndef MOZ_ANDROID_SYNTHAPKS
    1.58 +var ContextMenus = {
    1.59 +  target: null,
    1.60 +
    1.61 +  init: function() {
    1.62 +    document.addEventListener("contextmenu", this, false);
    1.63 +    document.getElementById("addToHomescreenLabel").addEventListener("click", this.addToHomescreen, false);
    1.64 +    document.getElementById("uninstallLabel").addEventListener("click", this.uninstall, false);
    1.65 +  },
    1.66 +
    1.67 +  handleEvent: function(event) {
    1.68 +    // store the target of context menu events so that we know which app to act on
    1.69 +    this.target = event.target;
    1.70 +    while (!this.target.hasAttribute("contextmenu")) {
    1.71 +      this.target = this.target.parentNode;
    1.72 +    }
    1.73 +  },
    1.74 +
    1.75 +  addToHomescreen: function() {
    1.76 +    let manifest = this.target.manifest;
    1.77 +    gChromeWin.WebappsUI.createShortcut(manifest.name, manifest.fullLaunchPath(), manifest.biggestIconURL || DEFAULT_ICON, "webapp");
    1.78 +    this.target = null;
    1.79 +  },
    1.80 +
    1.81 +  uninstall: function() {
    1.82 +    navigator.mozApps.mgmt.uninstall(this.target.app);
    1.83 +
    1.84 +    let manifest = this.target.manifest;
    1.85 +    gChromeWin.sendMessageToJava({
    1.86 +      type: "Shortcut:Remove",
    1.87 +      title: manifest.name,
    1.88 +      url: manifest.fullLaunchPath(),
    1.89 +      origin: this.target.app.origin,
    1.90 +      shortcutType: "webapp"
    1.91 +    });
    1.92 +    this.target = null;
    1.93 +  }
    1.94 +}
    1.95 +#endif
    1.96 +
    1.97 +function onLoad(aEvent) {
    1.98 +  let elmts = document.querySelectorAll("[pref]");
    1.99 +  for (let i = 0; i < elmts.length; i++) {
   1.100 +    elmts[i].addEventListener("click",  openLink,  false);
   1.101 +  }
   1.102 +
   1.103 +#ifdef MOZ_ANDROID_SYNTHAPKS
   1.104 +  document.getElementById("update-item").addEventListener("click", checkForUpdates, false);
   1.105 +#endif
   1.106 +
   1.107 +  navigator.mozApps.mgmt.oninstall = onInstall;
   1.108 +  navigator.mozApps.mgmt.onuninstall = onUninstall;
   1.109 +  updateList();
   1.110 +
   1.111 +#ifndef MOZ_ANDROID_SYNTHAPKS
   1.112 +  ContextMenus.init();
   1.113 +#endif
   1.114 +}
   1.115 +
   1.116 +function updateList() {
   1.117 +  let grid = document.getElementById("appgrid");
   1.118 +  while (grid.lastChild) {
   1.119 +    grid.removeChild(grid.lastChild);
   1.120 +  }
   1.121 +
   1.122 +  let request = navigator.mozApps.mgmt.getAll();
   1.123 +  request.onsuccess = function() {
   1.124 +    for (let i = 0; i < request.result.length; i++)
   1.125 +      addApplication(request.result[i]);
   1.126 +    if (request.result.length)
   1.127 +      document.getElementById("main-container").classList.remove("hidden");
   1.128 +  }
   1.129 +}
   1.130 +
   1.131 +function addApplication(aApp) {
   1.132 +  let list = document.getElementById("appgrid");
   1.133 +  let manifest = new ManifestHelper(aApp.manifest, aApp.origin);
   1.134 +
   1.135 +  let container = document.createElement("div");
   1.136 +  container.className = "app list-item";
   1.137 +#ifndef MOZ_ANDROID_SYNTHAPKS
   1.138 +  container.setAttribute("contextmenu", "appmenu");
   1.139 +#endif
   1.140 +  container.setAttribute("id", "app-" + aApp.origin);
   1.141 +  container.setAttribute("mozApp", aApp.origin);
   1.142 +  container.setAttribute("title", manifest.name);
   1.143 +
   1.144 +  let img = document.createElement("img");
   1.145 +  img.src = manifest.biggestIconURL || DEFAULT_ICON;
   1.146 +  img.onerror = function() {
   1.147 +    // If the image failed to load, and it was not our default icon, attempt to
   1.148 +    // use our default as a fallback.
   1.149 +    if (img.src != DEFAULT_ICON) {
   1.150 +      img.src = DEFAULT_ICON;
   1.151 +    }
   1.152 +  }
   1.153 +  img.setAttribute("title", manifest.name);
   1.154 +
   1.155 +  let title = document.createElement("div");
   1.156 +  title.appendChild(document.createTextNode(manifest.name));
   1.157 +
   1.158 +  container.appendChild(img);
   1.159 +  container.appendChild(title);
   1.160 +  list.appendChild(container);
   1.161 +
   1.162 +  container.addEventListener("click", function(aEvent) {
   1.163 +    aApp.launch();
   1.164 +  }, false);
   1.165 +  container.app = aApp;
   1.166 +  container.manifest = manifest;
   1.167 +}
   1.168 +
   1.169 +function onInstall(aEvent) {
   1.170 +  let node = document.getElementById("app-" + aEvent.application.origin);
   1.171 +  if (node)
   1.172 +    return;
   1.173 +
   1.174 +  addApplication(aEvent.application);
   1.175 +  document.getElementById("main-container").classList.remove("hidden");
   1.176 +}
   1.177 +
   1.178 +function onUninstall(aEvent) {
   1.179 +  let node = document.getElementById("app-" + aEvent.application.origin);
   1.180 +  if (node) {
   1.181 +    let parent = node.parentNode;
   1.182 +    parent.removeChild(node);
   1.183 +    if (!parent.firstChild)
   1.184 +      document.getElementById("main-container").classList.add("hidden");
   1.185 +  }
   1.186 +}
   1.187 +

mercurial