Fix app harness menu items restart and update.
Project: http://git-wip-us.apache.org/repos/asf/cordova-app-harness/repo Commit: http://git-wip-us.apache.org/repos/asf/cordova-app-harness/commit/951d8ee2 Tree: http://git-wip-us.apache.org/repos/asf/cordova-app-harness/tree/951d8ee2 Diff: http://git-wip-us.apache.org/repos/asf/cordova-app-harness/diff/951d8ee2 Branch: refs/heads/master Commit: 951d8ee2d45469d7b41f11722c0f785a6d7ceaa0 Parents: 0ae80c8 Author: Andrew Grieve <[email protected]> Authored: Wed Oct 23 14:35:11 2013 -0400 Committer: Andrew Grieve <[email protected]> Committed: Wed Oct 23 21:55:18 2013 -0400 ---------------------------------------------------------------------- www/cdvah/js/AppsService.js | 12 +----- www/cdvah/js/ContextMenuInjectScript.js | 20 +++++----- www/cdvah/js/Installer.js | 7 ++-- www/cdvah/js/ListCtrl.js | 58 +++++++++++++--------------- www/cdvahcm/ContextMenu.js | 31 ++++++--------- 5 files changed, 55 insertions(+), 73 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cordova-app-harness/blob/951d8ee2/www/cdvah/js/AppsService.js ---------------------------------------------------------------------- diff --git a/www/cdvah/js/AppsService.js b/www/cdvah/js/AppsService.js index 6b14e08..65e5d49 100644 --- a/www/cdvah/js/AppsService.js +++ b/www/cdvah/js/AppsService.js @@ -6,9 +6,8 @@ var platformId = cordova.platformId; // Map of type -> installer. var _installerFactories = {}; - + // Array of installer objects. var _installers = null; - var _lastLaunchedAppId = null; function createInstallHandlersFromJson(json) { var appList = json['appList'] || []; @@ -42,14 +41,12 @@ return readAppsJson() .then(function(appsJson) { - _lastLaunchedAppId = appsJson['lastLaunched']; _installers = createInstallHandlersFromJson(appsJson); }); } function writeAppsJson() { var appsJson = { - 'lastLaunched': _lastLaunchedAppId, 'appList': [] }; var appList = appsJson['appList']; @@ -77,12 +74,7 @@ }, launchApp : function(installer) { - _lastLaunchedAppId = installer.appId; - return writeAppsJson() - .then(function() { - var installPath = INSTALL_DIRECTORY + '/' + installer.appId; - return installer.launch(installPath); - }) + return installer.launch(_installers.indexOf(installer)) .then(function(launchUrl) { window.location = launchUrl; }); http://git-wip-us.apache.org/repos/asf/cordova-app-harness/blob/951d8ee2/www/cdvah/js/ContextMenuInjectScript.js ---------------------------------------------------------------------- diff --git a/www/cdvah/js/ContextMenuInjectScript.js b/www/cdvah/js/ContextMenuInjectScript.js index 7e64813..95ca108 100644 --- a/www/cdvah/js/ContextMenuInjectScript.js +++ b/www/cdvah/js/ContextMenuInjectScript.js @@ -3,19 +3,21 @@ /* global myApp */ myApp.factory("ContextMenuInjectScript", [ function () { var toInject = function() { - document.addEventListener('deviceready', function() { - console.log("Injecting menu script"); - var contextScript = document.createElement("script"); - contextScript.setAttribute("src", "app-harness:///cdvahcm/ContextMenu.js"); - window.__cordovaAppHarnessAppName = "appPlaceHolder"; - document.getElementsByTagName("head")[0].appendChild(contextScript); - }); + console.log("Menu script injected."); + var contextScript = document.createElement("script"); + contextScript.setAttribute("src", "app-harness:///cdvahcm/ContextMenu.js"); + window.__cordovaAppHarnessData = { + 'appIndex': appIndexPlaceHolder, + 'appName': 'appNamePlaceHolder' + }; + document.getElementsByTagName("head")[0].appendChild(contextScript); }; return { - getInjectString : function(appName){ + getInjectString : function(appName, appIndex) { var string = "\n(" + toInject.toString() + ")();"; - string = string.replace("appPlaceHolder", appName); + string = string.replace('appNamePlaceHolder', appName); + string = string.replace('appIndexPlaceHolder', appIndex); return string; } }; http://git-wip-us.apache.org/repos/asf/cordova-app-harness/blob/951d8ee2/www/cdvah/js/Installer.js ---------------------------------------------------------------------- diff --git a/www/cdvah/js/Installer.js b/www/cdvah/js/Installer.js index fa15f52..3e835ba 100644 --- a/www/cdvah/js/Installer.js +++ b/www/cdvah/js/Installer.js @@ -62,10 +62,11 @@ return $q.when(); }; - Installer.prototype.launch = function() { + Installer.prototype.launch = function(appIndex) { var installPath = this.installPath; + var appId = this.appId; if (!installPath) { - throw new Error('App ' + this.appId + ' requires an update'); + throw new Error('App ' + appId + ' requires an update'); } var configLocation = installPath + '/config.xml'; @@ -75,7 +76,7 @@ var harnessUrl = urlutil.makeAbsolute(location.pathname); var harnessDir = harnessUrl.replace(/\/[^\/]*\/[^\/]*$/, ''); var installUrl = urlutil.makeAbsolute(installPath); - var injectString = ContextMenuInjectScript.getInjectString(); + var injectString = ContextMenuInjectScript.getInjectString(appId, appIndex); var startLocation = urlutil.makeAbsolute(rawStartLocation).replace('/cdvah/', '/'); // On iOS, file:// URLs can't be re-routed via an NSURLProtocol for top-level navications. // http://stackoverflow.com/questions/12058203/using-a-custom-nsurlprotocol-on-ios-for-file-urls-causes-frame-load-interrup http://git-wip-us.apache.org/repos/asf/cordova-app-harness/blob/951d8ee2/www/cdvah/js/ListCtrl.js ---------------------------------------------------------------------- diff --git a/www/cdvah/js/ListCtrl.js b/www/cdvah/js/ListCtrl.js index 2495b97..5195086 100644 --- a/www/cdvah/js/ListCtrl.js +++ b/www/cdvah/js/ListCtrl.js @@ -1,42 +1,34 @@ (function(){ "use strict"; /* global myApp */ - myApp.controller("ListCtrl", [ "AppBundle", "notifier", "$rootScope", "$scope", "$routeParams", "AppsService", function (AppBundle, notifier, $rootScope, $scope, $routeParams, AppsService) { + myApp.controller("ListCtrl", ['$location', "notifier", "$rootScope", "$scope", "$routeParams", "AppsService", function ($location, notifier, $rootScope, $scope, $routeParams, AppsService) { $scope.appList = []; $rootScope.appTitle = 'Cordova App Harness'; - initialise(); - function initialise() { - //if we are navigating here after running an app, reset any aliases set for the app by app harness or any aliases setup by the previous app - return AppBundle.reset() - .then(function(){ - if($routeParams.lastLaunched) { - return AppsService.getLastRunApp() - .then(AppsService.launchApp, function(e){ - e = e || {}; - console.error("Error launching last run app: " + e); - notifier.error('' + e); - }); - } - else if($routeParams.updateLastLaunched) { - var app; - // updating may take a while so we show the apps list like we normally do - return $scope.loadAppsList() - .then(AppsService.getLastRunApp) - .then(function(_app){ - app = _app; - return AppsService.updateApp(app); - }) - .then(function(){ - return AppsService.launchApp(app); - }, function(e){ - e = e || {}; - console.error("Error updating last run app: " + e); - notifier.error('' + e); - }); - } else { - return $scope.loadAppsList(); + return $scope.loadAppsList() + .then(AppsService.getAppList) + .then(function(appList) { + var action = $routeParams['action']; + if (action) { + var appIndex = +$routeParams['appIndex']; + var activeApp = appList[appIndex]; + if (action == 'restart') { + return AppsService.launchApp(activeApp) + .then(null, function(e){ + console.error("Error launching last run app: " + e); + notifier.error('' + e); + }); + } else if (action == 'update') { + // updating may take a while so we show the apps list like we normally do + return AppsService.updateApp(activeApp) + .then(function() { + return AppsService.launchApp(activeApp); + }).then(null, function(e){ + console.error("Error updating last run app: " + e); + notifier.error('' + e); + }); + } } }); } @@ -91,6 +83,8 @@ }); } } + + initialise(); }]); })(); http://git-wip-us.apache.org/repos/asf/cordova-app-harness/blob/951d8ee2/www/cdvahcm/ContextMenu.js ---------------------------------------------------------------------- diff --git a/www/cdvahcm/ContextMenu.js b/www/cdvahcm/ContextMenu.js index 0a7e433..085fea7 100644 --- a/www/cdvahcm/ContextMenu.js +++ b/www/cdvahcm/ContextMenu.js @@ -1,43 +1,36 @@ (function () { + var contextMenuIframeEl = null; function initialise() { setupIframe(); - sendAppNameToIframe(); setupIframeMessaging(); //loadFirebug(false); attachErrorListener(); } - var contextMenuIframe = "__cordovaappharness_contextMenu_iframe"; function setupIframe(){ var contextHTMLUrl = "app-harness:///cdvahcm/contextMenu.html"; var el = document.createElement("iframe"); - el.setAttribute("id", contextMenuIframe); - el.setAttribute("src", contextHTMLUrl); - el.setAttribute("style", "position: fixed; left : 0px; top : 0px; z-index: 2000; width: 100%; height: 100%; display : none;"); + contextMenuIframeEl = el; + el.src = contextHTMLUrl; + el.style.cssText = 'position: fixed; left: 0; top: 0; z-index: 2000; width: 100%; height: 100%; border: none; display:none'; + el.onload = function() { + el.contentWindow.postMessage("AppHarnessAppName:" + __cordovaAppHarnessData['appName'], "*"); + }; document.body.appendChild(el); // Setup the listeners to toggle the context menu document.addEventListener("touchmove", function (event) { - if(event.touches.length >= 3) { - document.getElementById(contextMenuIframe).style.display = "inline"; + if (event.touches.length >= 3) { + el.style.display = ''; } }, false); } - function sendAppNameToIframe(){ - if(window.__cordovaAppHarnessAppName){ - var el = document.getElementById(contextMenuIframe); - el.onload = function(){ - el.contentWindow.postMessage("AppHarnessAppName:" + window.__cordovaAppHarnessAppName, "*"); - }; - } - } - function onContextMenuUpdateClicked(){ - window.location = "app-harness:///cdvah/index.html#/?updateLastLaunched=true"; + window.location = "app-harness:///cdvah/index.html#/?action=update&appIndex=" + __cordovaAppHarnessData['appIndex']; } function onContextMenuRestartClicked(){ - window.location = "app-harness:///cdvah/index.html#/?lastLaunched=true"; + window.location = "app-harness:///cdvah/index.html#/?action=restart&appIndex=" + __cordovaAppHarnessData['appIndex']; } var firebugFirstOpen = true; function onContextMenuFirebugClicked(){ @@ -67,7 +60,7 @@ window.location = "app-harness:///cdvah/index.html"; } function onContextMenuHideClicked(){ - document.getElementById(contextMenuIframe).style.display = "none"; + contextMenuIframeEl.style.display = "none"; } function onContextMenuWeinreNameChanged(newName){ var el = document.createElement("script");
