Check for ACTION_OPEN_DOCUMENT before starting activity

This commit is contained in:
Dominik Schürmann
2016-01-04 10:28:51 +01:00
parent 4057c97bd2
commit a5f792a718

View File

@@ -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) {