From 719af8c8ba817a506f1098d93d6da6057da59972 Mon Sep 17 00:00:00 2001 From: tibbi Date: Mon, 27 Jun 2016 12:37:01 +0200 Subject: [PATCH] implement GET_CONTENT intent handling --- app/src/main/AndroidManifest.xml | 10 +++++ .../simplemobiletools/gallery/Constants.java | 6 +-- .../com/simplemobiletools/gallery/Utils.java | 9 ++++ .../gallery/activities/MainActivity.java | 41 ++++++++++++++++--- .../gallery/activities/MediaActivity.java | 14 +++---- 5 files changed, 64 insertions(+), 16 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 288627520..4d1aa1464 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -32,6 +32,16 @@ + + + + + + + + + + diff --git a/app/src/main/java/com/simplemobiletools/gallery/Constants.java b/app/src/main/java/com/simplemobiletools/gallery/Constants.java index 93f00f04b..b71a0626e 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/Constants.java +++ b/app/src/main/java/com/simplemobiletools/gallery/Constants.java @@ -3,7 +3,7 @@ package com.simplemobiletools.gallery; public class Constants { public static final String DIRECTORY = "directory"; public static final String MEDIUM = "medium"; - public static final String PICK_IMAGE_INTENT = "is_image_pick_intent"; - public static final String PICK_VIDEO_INTENT = "is_video_pick_intent"; - public static final String SET_WALLPAPER_INTENT = "is_set_wallpaper_intent"; + public static final String GET_IMAGE_INTENT = "get_image_intent"; + public static final String GET_VIDEO_INTENT = "get_video_intent"; + public static final String SET_WALLPAPER_INTENT = "set_wallpaper_intent"; } diff --git a/app/src/main/java/com/simplemobiletools/gallery/Utils.java b/app/src/main/java/com/simplemobiletools/gallery/Utils.java index 45351cece..56bc50e20 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/Utils.java +++ b/app/src/main/java/com/simplemobiletools/gallery/Utils.java @@ -9,6 +9,7 @@ import android.support.v7.app.ActionBar; import android.util.TypedValue; import android.view.View; import android.view.Window; +import android.webkit.MimeTypeMap; import android.widget.Toast; public class Utils { @@ -54,6 +55,14 @@ public class Utils { return ContextCompat.checkSelfPermission(cxt, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED; } + public static String getMimeType(String url) { + final String extension = MimeTypeMap.getFileExtensionFromUrl(url); + if (extension != null) { + return MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension); + } + return ""; + } + public static void showSystemUI(ActionBar actionbar, Window window) { if (actionbar != null) actionbar.show(); diff --git a/app/src/main/java/com/simplemobiletools/gallery/activities/MainActivity.java b/app/src/main/java/com/simplemobiletools/gallery/activities/MainActivity.java index 93751b343..aa0f67794 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/activities/MainActivity.java +++ b/app/src/main/java/com/simplemobiletools/gallery/activities/MainActivity.java @@ -61,7 +61,10 @@ public class MainActivity extends AppCompatActivity private static boolean mIsSnackbarShown; private static boolean mIsPickImageIntent; private static boolean mIsPickVideoIntent; + private static boolean mIsGetImageContentIntent; + private static boolean mIsGetVideoContentIntent; private static boolean mIsSetWallpaperIntent; + private static boolean mIsThirdPartyIntent; private static int mSelectedItemsCnt; @Override @@ -73,12 +76,16 @@ public class MainActivity extends AppCompatActivity final Intent intent = getIntent(); mIsPickImageIntent = isPickImageIntent(intent); mIsPickVideoIntent = isPickVideoIntent(intent); + mIsGetImageContentIntent = isGetImageContentIntent(intent); + mIsGetVideoContentIntent = isGetVideoContentIntent(intent); mIsSetWallpaperIntent = isSetWallpaperIntent(intent); + mIsThirdPartyIntent = mIsPickImageIntent || mIsPickVideoIntent || mIsGetImageContentIntent || mIsGetVideoContentIntent || + mIsSetWallpaperIntent; } @Override public boolean onCreateOptionsMenu(Menu menu) { - if (mIsSetWallpaperIntent) + if (mIsThirdPartyIntent) return false; getMenuInflater().inflate(R.menu.menu, menu); @@ -150,12 +157,12 @@ public class MainActivity extends AppCompatActivity final Map directories = new LinkedHashMap<>(); final List invalidFiles = new ArrayList<>(); for (int i = 0; i < 2; i++) { - if (mIsPickVideoIntent && i == 0) + if ((mIsPickVideoIntent || mIsGetVideoContentIntent) && i == 0) continue; Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; if (i == 1) { - if (mIsPickImageIntent) + if (mIsPickImageIntent || mIsGetImageContentIntent) continue; uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; @@ -360,6 +367,19 @@ public class MainActivity extends AppCompatActivity return intent != null && intent.getAction() != null && intent.getAction().equals(Intent.ACTION_PICK) && intent.getData() != null; } + private boolean isGetContentIntent(Intent intent) { + return intent != null && intent.getAction() != null && intent.getAction().equals(Intent.ACTION_GET_CONTENT) && + !intent.getType().isEmpty(); + } + + private boolean isGetImageContentIntent(Intent intent) { + return isGetContentIntent(intent) && intent.getType().startsWith("image/"); + } + + private boolean isGetVideoContentIntent(Intent intent) { + return isGetContentIntent(intent) && intent.getType().startsWith("video/"); + } + private boolean isSetWallpaperIntent(Intent intent) { return intent != null && intent.getAction() != null && intent.getAction().equals(Intent.ACTION_SET_WALLPAPER); } @@ -369,7 +389,16 @@ public class MainActivity extends AppCompatActivity if (resultCode == RESULT_OK) { if (requestCode == PICK_MEDIA && data != null) { final Intent result = new Intent(); - result.setData(data.getData()); + if (mIsGetImageContentIntent || mIsGetVideoContentIntent) { + final String path = data.getData().getPath(); + final Uri uri = Uri.fromFile(new File(path)); + final String type = Utils.getMimeType(path); + result.setDataAndTypeAndNormalize(uri, type); + result.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); + } else { + result.setData(data.getData()); + } + setResult(RESULT_OK, result); finish(); } else if (requestCode == PICK_WALLPAPER) { @@ -389,8 +418,8 @@ public class MainActivity extends AppCompatActivity intent.putExtra(Constants.SET_WALLPAPER_INTENT, true); startActivityForResult(intent, PICK_WALLPAPER); } else { - intent.putExtra(Constants.PICK_IMAGE_INTENT, mIsPickImageIntent); - intent.putExtra(Constants.PICK_VIDEO_INTENT, mIsPickVideoIntent); + intent.putExtra(Constants.GET_IMAGE_INTENT, mIsPickImageIntent || mIsGetImageContentIntent); + intent.putExtra(Constants.GET_VIDEO_INTENT, mIsPickVideoIntent || mIsGetVideoContentIntent); startActivityForResult(intent, PICK_MEDIA); } } diff --git a/app/src/main/java/com/simplemobiletools/gallery/activities/MediaActivity.java b/app/src/main/java/com/simplemobiletools/gallery/activities/MediaActivity.java index 849c78c87..7aad87f37 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/activities/MediaActivity.java +++ b/app/src/main/java/com/simplemobiletools/gallery/activities/MediaActivity.java @@ -56,8 +56,8 @@ public class MediaActivity extends AppCompatActivity private static Parcelable mState; private static boolean mIsSnackbarShown; - private static boolean mIsPickImageIntent; - private static boolean mIsPickVideoIntent; + private static boolean mIsGetImageIntent; + private static boolean mIsGetVideoIntent; private static int mSelectedItemsCnt; @Override @@ -65,8 +65,8 @@ public class MediaActivity extends AppCompatActivity super.onCreate(savedInstanceState); setContentView(R.layout.activity_media); ButterKnife.bind(this); - mIsPickImageIntent = getIntent().getBooleanExtra(Constants.PICK_IMAGE_INTENT, false); - mIsPickVideoIntent = getIntent().getBooleanExtra(Constants.PICK_VIDEO_INTENT, false); + mIsGetImageIntent = getIntent().getBooleanExtra(Constants.GET_IMAGE_INTENT, false); + mIsGetVideoIntent = getIntent().getBooleanExtra(Constants.GET_VIDEO_INTENT, false); } @Override @@ -122,12 +122,12 @@ public class MediaActivity extends AppCompatActivity final List media = new ArrayList<>(); final List invalidFiles = new ArrayList<>(); for (int i = 0; i < 2; i++) { - if (mIsPickVideoIntent && i == 0) + if (mIsGetVideoIntent && i == 0) continue; Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; if (i == 1) { - if (mIsPickImageIntent) + if (mIsGetImageIntent) continue; uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; @@ -285,7 +285,7 @@ public class MediaActivity extends AppCompatActivity finish(); } }); - } else if (mIsPickImageIntent || mIsPickVideoIntent) { + } else if (mIsGetImageIntent || mIsGetVideoIntent) { final Intent result = new Intent(); result.setData(Uri.parse(curItemPath)); setResult(RESULT_OK, result);