Skip to content

Instantly share code, notes, and snippets.

@Whebcraft
Last active July 29, 2016 23:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Whebcraft/3423a90ceaa4df21dc750722709382c2 to your computer and use it in GitHub Desktop.
Save Whebcraft/3423a90ceaa4df21dc750722709382c2 to your computer and use it in GitHub Desktop.
Cordova display all the installed apps on the users phone
// call this function first to check if the icons has been generated..
function appsIconCheck() {
window.requestFileSystem(window.TEMPORARY, 1024 * 1024, function(fs) {
fs.root.getDirectory('/data/data/com.awaa.applauncher/icons', {
create: false
}, function(dirEntry) {
// icons generated lets get all the apps.
window.installedapps.getApps({
success: generateApps
}); // onSuccess this runs the generateApps() function
}, function() {
console.log('Could Not Generate Apps');
});
}, null);
}
// this function generates the apps and appends to a div #apps
function generateApps(returnVal) {
var appListArray = JSON.parse(returnVal.returnVal);
appListArray.sort(compare); // sort by name
for (var i = 0, len = appListArray.length; i < len; i++) {
document.getElementById("apps").innerHTML += '<a onclick="launch(this);" data-package="' + this.package + '" data-activity="' + this.activity + '">' +
'<img src="' + this.path + '"/>' + // app icon
'<span>' + this.name + '</span>' + // app name
'</a>';
}
}
// this function launches the clicked app based on the package and the activity.
function launch(el) {
var pkg = el.getAttribute("data-package");
var act = el.getAttribute("data-activity");
window.launch.app({
package: pkg,
activity: act
});
}
// sort by name
function compare(a, b) {
if (a.name < b.name)
return -1;
if (a.name > b.name)
return 1;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment