Callback for programatically install .apk - android

I update my app using a local downloaded .apk, via DownloadManager, using this code:
val installIntent = Intent(Intent.ACTION_VIEW)
installIntent.setDataAndType(localUri, "application/vnd.android.package-archive")
installIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
this.startActivity(installIntent)
Although most of the time it works, sometimes, only some of the users get the "There was a problem parsing the package error" popup.
I'm interested if there is any callback that we can get in our app, whenever this happens, or user press 'OK' in the popoup . I found here for instance, a callback that checks if the package exists (meaning that it was installed) but I'm more interested for updates, in this case I already know that the app was previously installed.
Is there a callback (maybe startActivityForResults) for this specific intent, so we know that it was successfully installed or it failed?

ACTION_VIEW is not designed to return a result. On Android 5.0 and higher, PackageInstaller gives you a direct API for installing apps, where you can provide a callback (in the form of an IntentSender) to find out about how the installation proceeds.
This sample project shows the basics of using PackageInstaller, and you should be able to find open source apps that use it in a more sophisticated fashion. For example, the F-Droid client probably uses PackageInstaller.

Related

Dropbox AuthActivity from Android. Fails with "User may have restricted profile"

I'm trying to modify an existing app (that uses Gdrive) to work with Dropbox. To that effect, I'm adapting code from https://github.com/dropbox/dropbox-sdk-java/tree/master/examples/android.
Unfortunately, I'm stuck at the first step: Auth.startOAuth2Authentication
Android Studio's log shows the following:
08-02 13:13:30.338 7994-7994/rudy.android.stdb E/com.dropbox.core.android.AuthActivity: Could not launch intent. User may have restricted profile
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=https://www.dropbox.com/... }
Any ideas?
This error seems to be indicating that there is no app available to handle this intent to view the supplied URL, i.e., there is no browser installed. It sounds like this may be due to the use of a Restricted Profile.
If there isn't a browser or the Dropbox app available, the SDK won't be able to process the authorization flow. If this is the issue, you should enable a browser or switch off of the Restricted Profile.
[Cross-linking for reference: https://www.dropboxforum.com/t5/API-Support-Feedback/Auth-startOAuth2Authentication-Android-returns-quot-User-may/m-p/288600 ]

When will android broadcasts intent ACTION_PACKAGE_NEEDS_VERIFICATION?

About ACTION_PACKAGE_NEEDS_VERIFICATION intent, the android docs says:
`Sent to the system package verifier when a package needs to be verified. The data contains the package URI.
This is a protected intent that can only be sent by the system.`
Found at https://developer.android.com/reference/android/content/Intent.html#ACTION_PACKAGE_NEEDS_VERIFICATION
But what kind of verification does it means? In what kinds of occasions or scenarios will android broadcast this intent?
Looking at the recent source code (around line 10669), it appears that the system will broadcast this intent during the installation of a new package if there are any package verifier installed:
/*
* Determine if we have any installed package verifiers. If we
* do, then we'll defer to them to verify the packages.
*/
As for an explanation of what package verifiers are and what they do, my understanding is that they will need to have the PACKAGE_VERIFICATION_AGENT permission (reserved for system apps) which gives them the ability to respond to the broadcast to accept or reject the installation of the new package. The Google Play Store app is a verification agent that appears to use Google's online app verification service to perform this function.
Here is a page that has a lot of interesting information on the subject (along with several other pages that comes up if you do a "android package verifier" search online).

Remote update app on multiple devices NOT using Playstore

I'm trying to find a solution to do a remote update of an APK to 80 tablets. This should preferably be as automated as possible and if this can happen completely in the background without any user input that would be great. Basically what the Playstore currently do which I unfortunately can't use.
Is something like this possible without rooting the device? Any suggestion on libraries/ services that does this?
I'm running Android 4.1.1 and they will all be connected to a Wi-Fi.
You can download the new APK file to SD card, then call this to install it:
Intent shareIntent = new Intent(Intent.ACTION_VIEW);
shareIntent.setDataAndType(Uri.fromFile(new File("path-to-APK-file")),
"application/vnd.android.package-archive");
try {
context.startActivity(shareIntent);
} catch (Throwable t) {
// handle the exception here
}
There is only one thing not automatic: the final step. The system will ask the user to confirm installation.
About the MIME type of APK files, here's the wiki page.
No, in the background isn't possible without rooting or having the device's signing key at least as a standard Android APK update. The only semi-reasonable way I can envision something similar to this working is for your app to always check for/download code to run which you load using a class loader. This would be a significant amount of work and not easy.
However, if you're willing to live with some user interaction, it really shouldn't be that hard (though it'll still take some building of infrastructure). Keep a web service that returns the latest version number, compare with the current version number and download the new APK as necessary. Installing an APK programmatically has been covered in many SO questions.

"no activity found to handle intent" Not all phones have Android Market installed

Not all phones have Android Market installed, and therefore using intent to open market app fails.
What's the best way to handle this?
Hide this feature if user doesn't have Android Market installed (how would I detect this?).
Handle the possible error, how (and possibly suggest that the user downloads the Android Market)?
The problem with the answer above is that if you just pass a URL the user will be prompted how to handle the Intent.
A more graceful way to do it IMO, expanding upon the 1st answer above, is to test whether the market app is installed, and then if not, pass a URL (which actually you would then want to test to see if something can handle that intent, but if you happen to have a device without both the play store and a browser then I would question why the user would have my app installed in the first place (another story I suppose)....
Perhaps there is a better way, but here's what works for me:
private String getMarketURI(String marketURL) {
String returnURL = "";
PackageManager packageManager = getApplication().getPackageManager();
Uri marketUri = Uri.parse("market://" + marketURL);
Intent marketIntent = new Intent(Intent.ACTION_VIEW).setData(marketUri);
if (marketIntent.resolveActivity(packageManager) != null) {
returnURL = "market://" + marketURL;
} else {
returnURL = "https://play.google.com/store/apps/" + marketURL;
}
return returnURL;
}
And then use it like so:
marketIntent.setData(Uri.parse(getMarketURI("details?id=com.yourapps.packagename")));
If your app is being provided by Android Market, then it does have Android Market installed. :)
Okay that is snide, but there is an important truth -- Google goes to a lot of effort to enforce compatibility guarantees on devices for them to be allowed to ship with Android Market, so that is how you can know that whatever you are running on will behave as it should.
If you are delivering your app from something besides Android Market, you need to get information from whoever is delivering the app about what compatibility guarantees they have.
If they don't have compatibility guarantees (or you are just putting a raw .apk up on a web site or such), then you have a complete crap shoot. The device you are running on could have had its software modified in pretty much any way, and have any kind of differences in behavior you can imagine.
That said, if you want to determine whether there is an activity on the current device to handle a particular Intent, you can use this: PackageManager.resolveActivity
Use the web address as the intent target and then if there is no android market it will open in a browser.

Auto-Update for (private) Android apps

I'm developing a non-public Android app, i.e. the app won't be available in the global Android Market. The app will be installed on a limited number of clients, e.g. by using an apk file.
How can I enable an auto-update functionality in this app?
I see different potential options (I do not know if those are technically hard or even impossible to implement or if there are any existing functionalities that can be reused):
On each launch the app tests if a new version exists (by requesting a server), if so downloads the new apk and replaces itself with the new version.
Use (or develop?) a separated app or service that undertakes the update-check and replacement-process.
Use (or develop?) a private market app which has an auto-update option. This option is similar to the second one, but more generic: The market app would be connected to a repository, i.e. it would handle an arbitrary number of (private) apps.
I would prefer option one since the auto-update functionality is included in the app which needs less development efforts.
janjonas, in the company I work we had a similar problem with Windows Mobile 6.x, and we use pretty much the same solution pointed by EboMike:
The main app check if it's updated, against a WebService. It receives the current version & the URL from where download the new version, if necessary. The main app then start the Updater app, passing the URL, and quit.
The Updater do the download of the new program, via HTTP, showing to the user the % downloaded. The user can cancel the download anytime, in a controlled way, and the Updater can registry this cancellation.
Since the new app is downloaded, the Updater run the new app, and quit.
I think option one is the least amount of work for you, and actually the cleanest one too since it will go through the proper channel of using Android's built-in package installer which includes user notification and the option for the user to abort the installation if desired.
You already have it all outlined - check for a new version on a server (would be nice to give the user the option to turn that off), and if there is a new version, you could either just link to the URL with the APK (which will, IIRC, use the browser's download manager to download it), or you could download it with your app and then point the intent to your local file. Using the HTTP link is technically less work and cleaner - the more you let the operating system do, the better - unless there's a reason not to.
Enabling "Install non-market app" is still needed for any application outside the Google Play. If it not enabled, the installation process is going to ask for it and redirect the user to the Application Settings, and after that, the user can install the app.
Depending on your needs, you can delegate to a third part lib.
Some of the permissions we'll use to get this done are the following:
<uses-permission android:name="android.permission.ACCESS_SUPERUSER" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Let me explain a bit... The last, WRITE_EXTERNAL_STORAGE, is self-explanatory. With ACCESS_SUPERUSER we'll tell the system that we intend to use root privileges. READ_EXTERNAL_STORAGE will be needed in the future in order for your app to read files on SD card.
Assuming that you have downloaded the file and that all those devices can be rooted (limited number of clients, not on Play, etc.), you could do this:
String filePath = Environment.getExternalStorageDirectory().toString() + "/your_app_directory/your_app_filename.apk";
Process installProcess = null;
int installResult = -1337;
try {
installProcess = Runtime.getRuntime().exec("su -c pm install -r " + filePath);
} catch (IOException e) {
// Handle IOException the way you like.
}
if (installProcess != null) {
try {
installResult = installProcess.waitFor();
} catch(InterruptedException e) {
// Handle InterruptedException the way you like.
}
if (installResult == 0) {
// Success!
} else {
// Failure. :-/
}
} else {
// Failure 2. :-(
}
Here might be a very lame method but for some companies, if you believe its applicable, this might be very easy to implement.
Create an password screen (passwordActivity) that asks a password to access the application.
Once the password is entered, raise a flag (set a boolean value from false to true using sharedpreferences)
Place the .apk file on Google Store.
Change the password once everyone installs the app, and release a new update on Google Play Store.
Since the software is going to cache the flag value, the password screen won`t show up even the password is change. It will only show up for new installations so might need to repeat the process.
Note: This method might better fit if there is not hundreds of users using the application. And don`t forget this method is also not secure. To sum up, if you are looking a way to keep the application private and have no security concerns, this is what I recommend.
Update app
Make sure that you already have your new apk download on location
void installNewVersion(String location) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(location + "app-debug.apk")),
"application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}

Categories

Resources