Find Packages on a Device
Packages are the application archive files (*.apk) that download to your device when you install an app from Google Play. PackageManager keeps track of where app packages are stored on your device, and lets you remove, install and launch Android applications. If you installed packages from your desktop computer, PackageManager reports these locations as well. The code example below gets the latest package info and then rolls through the list using the manager’s InstalledPackagedCount and InstalledPackge properties. InstalledPackge returns a PackageObject that has a Name, DevicePath and PCPath.
function ListPackages(){ var manager = Mobile.Device().PackageManager; manager.RefreshPackagesInfo() ; var packageCount = manager.InstalledPackageCount; var format = "Name: %s DevicePath: %s PCPath: %s"; for(var i = 0; i < packageCount; i++){ var pkg = manager.InstalledPackage(i); Log.Message(aqString.Format(format, pkg.Name, pkg.DevicePath, pkg.PCPath)); } }
The output for this example connected device shows the Name that identifies the package and the DevicePath on the Android device. In the example, the apps were installed from the Google Play store, so PCPath is null.
Remove, Install and Run Packages
If you know the name of a package on the device, you can retrieve it by name, or you can retrieve *.apk files from your PC if you know the path. Once you have an PackageObject, you can remove, install and launch packages on the device. This example checks if there’s an *.apk on the PC to install. If the package exists on the device, PackageManager removes the package prior to install and launch. The second parameter to RemovePackage() determines if you keep the application cache, or start completely fresh.
function LaunchLatestFromPC(){ var manager = Mobile.Device().PackageManager; // get a package from the device using its name var devicePackage = manager.GetInstalledPackageByName("falafel.myapp"); // get a package from the PC using it’s path var pcPackage = manager.GetPackageFromAPK("c:\\apk\\myapp.apk"); var keepDataAndCache = true; // it there's a package on the PC to install... if (pcPackage){ // if the package also exists on the device, remove it, but keep // the data and cache if (devicePackage){ if (!manager.RemovePackage(devicePackage, keepDataAndCache)){ Log.Error("Could not remove " + devicePackage.Name) } } // install the new package and then launch it if (!manager.InstallPackage(pcPackage)){ Log.Error("Could not install " + pcPackage.Name); } if (!manager.LaunchPackage(pcPackage)){ Log.Error("Could not launch " + pcPackage.Name); } // for giggles, log a screenshot of the running app Log.Picture(Mobile.Device().Desktop, pcPackage.Name + " has installed and launched") } }
The log shows the package activity and final screenshot of the launched app.
The Android File System
The Device FileSystemManager object has methods to copy to and from the device. Used along with the PackageManager, you can use the CopyFromDevice() method to get the package from DevicePath, and copy it to a directory on the PC using its package Name.
function copyPackagesToPC(){ var folderOnPC = "c:\\apk\\installed\\"; var packageManager = Mobile.Device().PackageManager; var fileManager = Mobile.Device().FileSystemManager; var count = packageManager.InstalledPackageCount; // iterate installed packages for(var i = 0; i < count; i++){ var package = packageManager.InstalledPackage(i); Log.Message("Copying " + package.DevicePath); // copy from the Android Device to the PC var destinationPath = folderOnPC + package.Name + ".apk"; if (!fileManager.CopyFromDevice(package.DevicePath, destinationPath)){ Log.Warning("Copy failed for " + package.Name); packageManager.RemovePackage(package); } } }
The playback indicator shows the source path on the device and target path on the PC.
Recursive File Search
FileSystemManager also has methods CopyToDevice, CreateFolder, Delete, Exists, FindFiles and FindFolders, like it’s desktop cousin aqFileSystem. As of this writing, the Android version of FileSystemManager FindFiles() method isn’t recursive, so you’ll have to build your own if you need to drill down into every child folder. I was curious about the file structure on the device and wrote this example that lists files and folders recursively. Be aware that this function will take quite a while to run. You may want to pick a specific directory instead of starting at the root.
var listFiles = function localFunction(path, filePath) { var fileSystemManager = Mobile.Device().FileSystemManager; // get all the folders on the current path var folders = fileSystemManager.FindFolders(path + "/*"); if (folders.Count > 0) { while (folders.HasNext()) { // get the current folder, then get // the files that match the filePath // in the folder var folder = folders.Next(); var files = folder.FindFiles(filePath); while (files.HasNext()){ var file = files.Next(); Log.Message(file.Path); } // recurse with the new current folder path localFunction(folder.Path); } } } function testRecursive(){ listFiles("*", "*.*"); }
Note: The localFunction() JavaScript function forms a closure that keeps the path and filePath variables being overwritten by subsequent calls.
The log shows the file paths on your Android device.
Wrap Up
You can find the package (*.apk files) on an Android device using the PackageManager object. You can also install packages located on your PC, then launch and run them on your Android device. The Android Device FileSystemManager object lets you iterate through the files and folders on your device. You can also delete and copy files, just like the desktop aqFileSystem object. We also set up a recursive file search using JavaScript to list the files in a path. Tomorrow we’ll look at automating user actions on Android devices. See you then.
The post Managing Android Packages appeared first on Falafel Software Blog.