diff options
author | Dominik Schürmann <dominik@dominikschuermann.de> | 2016-01-04 10:28:51 +0100 |
---|---|---|
committer | Dominik Schürmann <dominik@dominikschuermann.de> | 2016-01-04 10:28:51 +0100 |
commit | a5f792a718ccdf01d5702a38cc8a26d893d7fa94 (patch) | |
tree | abfa7c8e5b54c171764cc1d3bddab8b87c981407 /OpenKeychain/src/main | |
parent | 4057c97bd2d6be047f5fbafe76d70c32971aa937 (diff) | |
download | open-keychain-a5f792a718ccdf01d5702a38cc8a26d893d7fa94.tar.gz open-keychain-a5f792a718ccdf01d5702a38cc8a26d893d7fa94.tar.bz2 open-keychain-a5f792a718ccdf01d5702a38cc8a26d893d7fa94.zip |
Check for ACTION_OPEN_DOCUMENT before starting activity
Diffstat (limited to 'OpenKeychain/src/main')
-rw-r--r-- | OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/FileHelper.java | 38 |
1 files changed, 28 insertions, 10 deletions
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<ResolveInfo> resolveInfoList = packageManager.queryIntentActivities( + new Intent(Intent.ACTION_OPEN_DOCUMENT), 0); + hasOpenDocumentIntent = !resolveInfoList.isEmpty(); } + + return hasOpenDocumentIntent; } public static String getFilename(Context context, Uri uri) { |