2

I have an Activity called InstallationHelper whose job is to install a downloaded APK from the file system. When the installApk() is called below it is passed a filepath to the APK which resides in a Environment.getExternalStorageDirectory() directory we create.

private void installApk(String filepath)
{
  Intent installAPK = new Intent(Intent.ACTION_VIEW);
  installAPK.setDataAndType(Uri.fromFile(
    new File(filepath)), "application/vnd.android.package-archive");
  startActivityForResult(installAPK, ACTION_INSTALL_APK);
}

protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
  Log.d(LOG_TAG, "Installation activity completed, " +
     "request code: " + requestCode +
     ", result code: " + resultCode +
     ", intent: " + data);
  deleteFile();
  finish();
}

private void deleteFile()
{
  Log.d(LOG_TAG, "Attempting to delete file: " + file.getCanonicalPath());
  ...
}

This has been working very well until more recently when Google introduced their PackageVerificationService (https://support.google.com/accounts/answer/2812853?hl=en). Now when the startActivityForResult() is called the user is prompted with a dialog to handle the Intent (i.e., "Package installer", Verify and install" (Google) and any other filters like Lookout). The code works with the default package installer and Lookout, but when the "Verify and install" is selected the onActivityResult() is immediately called before the installation can occur. Here is the log:

Using "Package installer":

07-26 10:44:01.167: D/InstallationHelper(24528): Attempting to install file: /storage/sdcard0/SC/599173
07-26 10:44:01.167: D/InstallationHelper(24528): startActivityGotResult()
...
07-26 10:44:17.993: I/System.out(23660): siso added package name is package:com.opensignal.weathersignal
07-26 10:44:18.043: I/MediaHubAPP(3068): MHDisable Receiver action = android.intent.action.PACKAGE_ADDED
07-26 10:44:18.043: I/MediaHubAPP(3068): MHDisable Receiver PackageName = com.opensignal.weathersignal
...
07-26 10:44:20.276: I/InstallAppProgress(14735): Finished installing com.opensignal.weathersignal
07-26 10:44:20.336: D/InstallationHelper(24528): Installation activity completed, request code: 1, result code: 0, intent: null
07-26 10:44:20.396: D/InstallationHelper(24528): Attempting to delete file: /storage/sdcard0/SC/599173

Using "Verify and install":

07-26 10:58:42.366: D/InstallationHelper(24528): Starting InstallationHelper...
07-26 10:58:42.366: D/InstallationHelper(24528): Attempting to install file: /storage/sdcard0/SC/599173
07-26 10:58:42.366: D/InstallationHelper(24528): startActivityGotResult()
07-26 10:58:49.894: D/InstallationHelper(24528): Installation activity completed, request code: 1, result code: 0, intent: null
07-26 10:58:49.934: D/InstallationHelper(24528): Attempting to delete file: /storage/sdcard0/SC/599173
07-26 10:58:49.954: D/Finsky(14410): [47670] PackageVerificationService.getPackageInfo: Error while calculating sha256 for file=file:///storage/sdcard0/SC/599173, error=java.io.FileNotFoundException: /storage/sdcard0/SC/599173: open failed: ENOENT (No such file or directory)
07-26 10:58:50.155: D/InstallationHelper(24528): Installation activity being destroyed.

Because the file is deleted before the PackageVerificationService can parse it, I get a "There is a problem parsing the package" error dialog and the APK is not installed.

The parameters passed to onActivityResult() are the same whether the Package Installer or Verify App is selected, so I cannot differentiate the two.

Is this happening because the Activity started to install the APK is being finalized and passed to the PackageVerificationService, so my onActivityResult() gets called prematurely? Is there a way to prevent this?

Mark B
  • 271
  • 3
  • 12

0 Answers0