implement GET_CONTENT intent handling

This commit is contained in:
tibbi 2016-06-27 12:37:01 +02:00
parent 3060fca87b
commit 719af8c8ba
5 changed files with 64 additions and 16 deletions

View File

@ -32,6 +32,16 @@
<data android:mimeType="vnd.android.cursor.dir/video"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.GET_CONTENT"/>
<category android:name="android.intent.category.OPENABLE"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="image/*"/>
<data android:mimeType="video/*"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SET_WALLPAPER"/>
<category android:name="android.intent.category.DEFAULT"/>

View File

@ -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";
}

View File

@ -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();

View File

@ -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<String, Directory> directories = new LinkedHashMap<>();
final List<String> 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();
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);
}
}

View File

@ -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<Medium> media = new ArrayList<>();
final List<String> 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);