summaryrefslogtreecommitdiffstats
path: root/samples/ApiDemos
diff options
context:
space:
mode:
authorPeter Visontay <pvisontay@google.com>2017-11-23 13:29:42 +0000
committerPeter Visontay <pvisontay@google.com>2017-12-12 18:16:29 +0000
commit48f2a3142e5cb3f53bcc87c0850ca2bc32c8835a (patch)
treebc89c30e6b7e0c78e95821c2bbef8961204d1ca7 /samples/ApiDemos
parent6e294637ac7601486d942cce31957ce3e2db0c1d (diff)
downloadandroid_development-48f2a3142e5cb3f53bcc87c0850ca2bc32c8835a.tar.gz
android_development-48f2a3142e5cb3f53bcc87c0850ca2bc32c8835a.tar.bz2
android_development-48f2a3142e5cb3f53bcc87c0850ca2bc32c8835a.zip
Use FileProvider (instead of a world-readable file) in the APK installer demo.
World-readable files have been disallowed since N. Test: Manually. Change-Id: Iab174799403023378200d75dac5e8cd02e64dbf8
Diffstat (limited to 'samples/ApiDemos')
-rw-r--r--samples/ApiDemos/AndroidManifest.xml11
-rw-r--r--samples/ApiDemos/res/xml/file_paths.xml20
-rw-r--r--samples/ApiDemos/src/com/example/android/apis/content/InstallApk.java33
3 files changed, 58 insertions, 6 deletions
diff --git a/samples/ApiDemos/AndroidManifest.xml b/samples/ApiDemos/AndroidManifest.xml
index df4232dd1..ff65b75e0 100644
--- a/samples/ApiDemos/AndroidManifest.xml
+++ b/samples/ApiDemos/AndroidManifest.xml
@@ -1311,6 +1311,17 @@
</intent-filter>
</activity>
+ <!-- A FileProvider for passing the APK to be installed to the system. -->
+ <provider
+ android:name="android.support.v4.content.FileProvider"
+ android:authorities="com.example.android.apis.installapkprovider"
+ android:exported="false"
+ android:grantUriPermissions="true">
+ <meta-data
+ android:name="android.support.FILE_PROVIDER_PATHS"
+ android:resource="@xml/file_paths" />
+ </provider>
+
<activity android:name=".content.InstallApkSessionApi"
android:label="@string/activity_install_apk_session_api"
android:launchMode="singleTop">
diff --git a/samples/ApiDemos/res/xml/file_paths.xml b/samples/ApiDemos/res/xml/file_paths.xml
new file mode 100644
index 000000000..b2fc882ef
--- /dev/null
+++ b/samples/ApiDemos/res/xml/file_paths.xml
@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2017 The Android Open Source Project
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+
+<paths xmlns:android="http://schemas.android.com/apk/res/android">
+ <!-- Provides access to the installable APK in the package installer demo. -->
+ <files-path name="all" path=""/>
+</paths>
diff --git a/samples/ApiDemos/src/com/example/android/apis/content/InstallApk.java b/samples/ApiDemos/src/com/example/android/apis/content/InstallApk.java
index b1b1bd63e..a71f35df6 100644
--- a/samples/ApiDemos/src/com/example/android/apis/content/InstallApk.java
+++ b/samples/ApiDemos/src/com/example/android/apis/content/InstallApk.java
@@ -24,7 +24,9 @@ import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
+import android.os.Build;
import android.os.Bundle;
+import android.support.v4.content.FileProvider;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
@@ -88,7 +90,8 @@ public class InstallApk extends Activity {
private OnClickListener mUnknownSourceListener = new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
- intent.setData(Uri.fromFile(prepareApk("HelloActivity.apk")));
+ intent.setData(getApkUri("HelloActivity.apk"));
+ intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
}
};
@@ -96,7 +99,8 @@ public class InstallApk extends Activity {
private OnClickListener mMySourceListener = new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
- intent.setData(Uri.fromFile(prepareApk("HelloActivity.apk")));
+ intent.setData(getApkUri("HelloActivity.apk"));
+ intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true);
intent.putExtra(Intent.EXTRA_RETURN_RESULT, true);
intent.putExtra(Intent.EXTRA_INSTALLER_PACKAGE_NAME,
@@ -124,20 +128,37 @@ public class InstallApk extends Activity {
}
};
- private File prepareApk(String assetName) {
+ /**
+ * Returns a Uri pointing to the APK to install.
+ */
+ private Uri getApkUri(String assetName) {
+ // Before N, a MODE_WORLD_READABLE file could be passed via the ACTION_INSTALL_PACKAGE
+ // Intent. Since N, MODE_WORLD_READABLE files are forbidden, and a FileProvider is
+ // recommended.
+ boolean useFileProvider = Build.VERSION.SDK_INT >= Build.VERSION_CODES.N;
+
// Copy the given asset out into a file so that it can be installed.
// Returns the path to the file.
+ String tempFilename = "tmp.apk";
byte[] buffer = new byte[16384];
+ int fileMode = useFileProvider ? Context.MODE_PRIVATE : Context.MODE_WORLD_READABLE;
+
try (InputStream is = getAssets().open(assetName);
- FileOutputStream fout = openFileOutput("tmp.apk", Context.MODE_WORLD_READABLE)) {
+ FileOutputStream fout = openFileOutput(tempFilename, fileMode)) {
int n;
while ((n=is.read(buffer)) >= 0) {
fout.write(buffer, 0, n);
}
} catch (IOException e) {
- Log.i("InstallApk", "Failed transferring", e);
+ Log.i("InstallApk", "Failed to write temporary APK file", e);
}
- return getFileStreamPath("tmp.apk");
+ if (useFileProvider) {
+ File toInstall = new File(this.getFilesDir(), tempFilename);
+ return FileProvider.getUriForFile(
+ this, "com.example.android.apis.installapkprovider", toInstall);
+ } else {
+ return Uri.fromFile(getFileStreamPath(tempFilename));
+ }
}
}