Updated Branches: refs/heads/master 609e620ec -> 8979c1d0e
Uninstall an app Ability to uninstall an app Bug fix to clear target directory before installing 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/2d2c05f8 Tree: http://git-wip-us.apache.org/repos/asf/cordova-app-harness/tree/2d2c05f8 Diff: http://git-wip-us.apache.org/repos/asf/cordova-app-harness/diff/2d2c05f8 Branch: refs/heads/master Commit: 2d2c05f8647b3908737c10343bf67f6ebbe2a220 Parents: 609e620 Author: Shravan Narayan <[email protected]> Authored: Wed Apr 24 14:55:37 2013 -0400 Committer: Shravan Narayan <[email protected]> Committed: Wed Apr 24 14:56:21 2013 -0400 ---------------------------------------------------------------------- www/js/AppsService.js | 31 +++++++++++++++++++++- www/js/ListCtrl.js | 17 ++++++++--- www/js/ResourcesLoader.js | 56 ++++++++++++++++++++++++++++++++------- 3 files changed, 88 insertions(+), 16 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cordova-app-harness/blob/2d2c05f8/www/js/AppsService.js ---------------------------------------------------------------------- diff --git a/www/js/AppsService.js b/www/js/AppsService.js index f6dadd6..fd90de3 100644 --- a/www/js/AppsService.js +++ b/www/js/AppsService.js @@ -7,7 +7,10 @@ var fileName = TEMP_DIRECTORY + appName + ".zip"; var _fullFilePath; - return ResourcesLoader.downloadFromUrl(appUrl, fileName) + return ResourcesLoader.deleteDirectory(INSTALL_DIRECTORY + appName) + .then(function(){ + return ResourcesLoader.downloadFromUrl(appUrl, fileName); + }) .then(function(fullFilePath){ _fullFilePath = fullFilePath; return ResourcesLoader.ensureDirectoryExists(INSTALL_DIRECTORY + appName); @@ -95,6 +98,32 @@ } return addNewAppFromUrl(appName, appUrl); }); + }, + + uninstallApp : function(appName) { + return ResourcesLoader.ensureDirectoryExists(APPS_JSON) + .then(function() { + return ResourcesLoader.readJSONFileContents(APPS_JSON); + }) + .then(function(result){ + result.installedApps = result.installedApps || []; + var found = false; + + for(var i = 0; i < result.installedApps.length; i++){ + if(result.installedApps[i].Name === appName) { + result.installedApps.splice(i, 1); + found = true; + } + } + + if(!found) { + throw new Error("The app " + appName + " was not found."); + } + return ResourcesLoader.writeJSONFileContents(APPS_JSON, result); + }) + .then(function(){ + return ResourcesLoader.deleteDirectory(INSTALL_DIRECTORY + appName); + }); } }; }]); http://git-wip-us.apache.org/repos/asf/cordova-app-harness/blob/2d2c05f8/www/js/ListCtrl.js ---------------------------------------------------------------------- diff --git a/www/js/ListCtrl.js b/www/js/ListCtrl.js index 14c1ab4..f214fb5 100644 --- a/www/js/ListCtrl.js +++ b/www/js/ListCtrl.js @@ -5,13 +5,13 @@ $scope.appsList = []; - $scope.loadAppsList = function( source ) { + $scope.loadAppsList = function(callApply) { AppsService.getAppsList() .then(function(newAppsList){ //clear the old apps list $scope.appsList.splice(0, $scope.appsList.length); angular.extend($scope.appsList, newAppsList); - if(source === "deviceready") { + if(callApply) { $scope.$apply(); } }, function(error){ @@ -24,7 +24,7 @@ $scope.launchApp = function(app){ AppsService.launchApp(app) .then(null, function(error){ - console.error("Error during loading of app: " + error); + console.error("Error during loading of app " + app + ": " + error); alert("Something went wrong during the loading of the app. Please try again."); }); }; @@ -34,9 +34,16 @@ }; $scope.removeApp = function(app) { - alert("removeApp called: " + app); + var shouldUninstall = confirm("Are you sure you want to uninstall " + app + "?"); + if(shouldUninstall) { + AppsService.uninstallApp(app) + .then(function() { $scope.loadAppsList(true); }, function(error){ + console.error("Error during uninstall of app " + app + ": " + error); + alert("Something went wrong during the uninstall of the app. Please try again."); + }); + } }; - document.addEventListener("deviceready", function() { $scope.loadAppsList("deviceready"); }, false); + document.addEventListener("deviceready", function() { $scope.loadAppsList(true); }, false); }]); })(); \ No newline at end of file http://git-wip-us.apache.org/repos/asf/cordova-app-harness/blob/2d2c05f8/www/js/ResourcesLoader.js ---------------------------------------------------------------------- diff --git a/www/js/ResourcesLoader.js b/www/js/ResourcesLoader.js index 0bd244c..118a975 100644 --- a/www/js/ResourcesLoader.js +++ b/www/js/ResourcesLoader.js @@ -74,6 +74,26 @@ return path; } + //promise returns the directory entry + function getDirectoryEntry(directoryName) { + var deferred = Q.defer(); + + try { + var errorWhileGettingDirectoryEntry = function(error) { + var str = "There was an error while getting the directory entry for directory " + directoryName + " " + JSON.stringify(error); + deferred.reject(new Error(str)); + }; + var success = function(directoryEntry) { + deferred.resolve(directoryEntry); + }; + fs.root.getDirectory(directoryName, {create: true, exclusive: false}, success, errorWhileGettingDirectoryEntry); + } catch(e) { + deferred.reject(new Error(e)); + } finally { + return deferred.promise; + } + } + //promise returns the file entry function getFileEntry(fileName) { var deferred = Q.defer(); @@ -127,8 +147,8 @@ return { // returns a promise with a full path to the dir ensureDirectoryExists : function(directory) { - return initialiseFileSystem(). - then(function(){ + return initialiseFileSystem() + .then(function(){ var deferred = Q.defer(); directory = truncateToDirectoryPath(directory); @@ -150,8 +170,8 @@ // promise returns full path to file getFullFilePath : function(filePath) { - return initialiseFileSystem(). - then(function(){ + return initialiseFileSystem() + .then(function(){ var deferred = Q.defer(); // Use the file's parent folder to get the full path @@ -188,8 +208,8 @@ // returns a promise with a full path to the downloaded file downloadFromUrl : function(url, filePath) { var self = this; - return initialiseFileSystem(). - then(function(){ + return initialiseFileSystem() + .then(function(){ return self.ensureDirectoryExists(filePath); }) .then(function(){ @@ -202,8 +222,8 @@ //returns a promise with the contents of the file readFileContents : function(fileName) { - return initialiseFileSystem(). - then(function(){ + return initialiseFileSystem() + .then(function(){ return getFile(fileName); }) .then(function(file){ @@ -238,8 +258,8 @@ //returns a promise when file is written writeFileContents : function(fileName, contents) { - return initialiseFileSystem(). - then(function(){ + return initialiseFileSystem() + .then(function(){ return getFileEntry(fileName); }) .then(function(fileEntry){ @@ -271,6 +291,22 @@ stringContents = JSON.stringify(contents); } return this.writeFileContents(fileName, stringContents); + }, + + deleteDirectory : function(directoryName) { + return initialiseFileSystem() + .then(function(){ + return getDirectoryEntry(directoryName); + }) + .then(function(dirEntry){ + var deferred = Q.defer(); + var failedToDeleteDirectory = function(error) { + var str = "There was an error deleting the directory: " + directoryName + " " + JSON.stringify(error); + deferred.reject(new Error(str)); + }; + dirEntry.removeRecursively(deferred.resolve, failedToDeleteDirectory); + return deferred.promise; + }); } }; }]);
