From a5f792a718ccdf01d5702a38cc8a26d893d7fa94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Mon, 4 Jan 2016 10:28:51 +0100 Subject: Check for ACTION_OPEN_DOCUMENT before starting activity --- .../keychain/util/FileHelper.java | 38 ++++++++++++++++------ 1 file changed, 28 insertions(+), 10 deletions(-) (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/FileHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/FileHelper.java index 67576f88e..106775201 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/FileHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/FileHelper.java @@ -29,12 +29,15 @@ import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.security.SecureRandom; import java.text.DecimalFormat; +import java.util.List; import android.annotation.TargetApi; import android.content.ActivityNotFoundException; import android.content.ContentResolver; import android.content.Context; import android.content.Intent; +import android.content.pm.PackageManager; +import android.content.pm.ResolveInfo; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.Point; @@ -76,6 +79,8 @@ import org.sufficientlysecure.keychain.R; */ public class FileHelper { + private static Boolean hasOpenDocumentIntent; + @TargetApi(VERSION_CODES.KITKAT) public static void saveDocument(Fragment fragment, String targetName, int requestCode) { saveDocument(fragment, targetName, "*/*", requestCode); @@ -94,8 +99,9 @@ public class FileHelper { } public static void openDocument(Fragment fragment, Uri last, String mimeType, boolean multiple, int requestCode) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { - openDocumentKitKat(fragment, last, mimeType, multiple, requestCode); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT + && hasOpenDocumentIntent(fragment.getContext())) { + openDocumentKitKat(fragment, mimeType, multiple, requestCode); } else { openDocumentPreKitKat(fragment, last, mimeType, multiple, requestCode); } @@ -126,7 +132,7 @@ public class FileHelper { /** Opens the storage browser on Android 4.4 or later for opening a file */ @TargetApi(Build.VERSION_CODES.KITKAT) - private static void openDocumentKitKat(Fragment fragment, Uri last, String mimeType, boolean multiple, int requestCode) { + private static void openDocumentKitKat(Fragment fragment, String mimeType, boolean multiple, int requestCode) { Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); intent.addCategory(Intent.CATEGORY_OPENABLE); intent.setType(mimeType); @@ -134,14 +140,26 @@ public class FileHelper { intent.putExtra("android.content.extra.SHOW_ADVANCED", true); intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, multiple); - try { - fragment.startActivityForResult(intent, requestCode); - } catch (ActivityNotFoundException e) { - // NOTE: Looks like some Android distributions are missing the ACTION_OPEN_DOCUMENT intent - // even on Android 4.4, see https://github.com/open-keychain/open-keychain/issues/1625 - Log.w(Constants.TAG, "Couldn't start ACTION_OPEN_DOCUMENT intent, no activity found, falling back to "); - openDocumentPreKitKat(fragment, last, mimeType, multiple, requestCode); + fragment.startActivityForResult(intent, requestCode); + } + + /** + * Does the device actually have a ACTION_OPEN_DOCUMENT Intent? Looks like some Android + * distributions are missing the ACTION_OPEN_DOCUMENT Intent even on Android 4.4, + * see https://github.com/open-keychain/open-keychain/issues/1625 + * + * @return True, if the device supports ACTION_OPEN_DOCUMENT. False, otherwise. + */ + @TargetApi(VERSION_CODES.KITKAT) + private static boolean hasOpenDocumentIntent(Context context) { + if (hasOpenDocumentIntent == null) { + PackageManager packageManager = context.getPackageManager(); + List resolveInfoList = packageManager.queryIntentActivities( + new Intent(Intent.ACTION_OPEN_DOCUMENT), 0); + hasOpenDocumentIntent = !resolveInfoList.isEmpty(); } + + return hasOpenDocumentIntent; } public static String getFilename(Context context, Uri uri) { -- cgit v1.2.3