Installer: Use content URI on API >= 24 for DefaultInstaller fallback

* When there's a permission mismatch (#951, #890), the fallback
  DefaultInstaller is invoked, which enforces file and content schemes
  for API < 24 and >= 24 respectively.
* Use content URI in that case, which allows the fallback to work.
This commit is contained in:
Chirayu Desai 2017-04-18 23:05:21 +05:30
parent d844541f6e
commit db4aa3b519

View File

@ -25,6 +25,7 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.net.Uri; import android.net.Uri;
import android.os.Build;
import android.os.PatternMatcher; import android.os.PatternMatcher;
import android.support.v4.content.LocalBroadcastManager; import android.support.v4.content.LocalBroadcastManager;
import android.text.TextUtils; import android.text.TextUtils;
@ -228,6 +229,19 @@ public abstract class Installer {
* installation of that specific APK * installation of that specific APK
*/ */
public void installPackage(Uri localApkUri, Uri downloadUri) { public void installPackage(Uri localApkUri, Uri downloadUri) {
Uri sanitizedUri;
try {
// move apk file to private directory for installation and check hash
sanitizedUri = ApkFileProvider.getSafeUri(
context, localApkUri, apk, supportsContentUri());
} catch (IOException e) {
Log.e(TAG, e.getMessage(), e);
sendBroadcastInstall(downloadUri, Installer.ACTION_INSTALL_INTERRUPTED,
e.getMessage());
return;
}
try { try {
// verify that permissions of the apk file match the ones from the apk object // verify that permissions of the apk file match the ones from the apk object
ApkVerifier apkVerifier = new ApkVerifier(context, localApkUri, apk); ApkVerifier apkVerifier = new ApkVerifier(context, localApkUri, apk);
@ -245,23 +259,18 @@ public abstract class Installer {
Log.e(TAG, e.getMessage(), e); Log.e(TAG, e.getMessage(), e);
Log.e(TAG, "Falling back to AOSP DefaultInstaller!"); Log.e(TAG, "Falling back to AOSP DefaultInstaller!");
DefaultInstaller defaultInstaller = new DefaultInstaller(context, apk); DefaultInstaller defaultInstaller = new DefaultInstaller(context, apk);
defaultInstaller.installPackageInternal(localApkUri, downloadUri); // https://code.google.com/p/android/issues/detail?id=205827
if (Build.VERSION.SDK_INT >= 24) {
// content scheme for N and above
defaultInstaller.installPackageInternal(sanitizedUri, downloadUri);
} else {
// file scheme for below N
defaultInstaller.installPackageInternal(localApkUri, downloadUri);
}
return; return;
} }
} }
Uri sanitizedUri;
try {
// move apk file to private directory for installation and check hash
sanitizedUri = ApkFileProvider.getSafeUri(
context, localApkUri, apk, supportsContentUri());
} catch (IOException e) {
Log.e(TAG, e.getMessage(), e);
sendBroadcastInstall(downloadUri, Installer.ACTION_INSTALL_INTERRUPTED,
e.getMessage());
return;
}
installPackageInternal(sanitizedUri, downloadUri); installPackageInternal(sanitizedUri, downloadUri);
} }