From a4c7835f562edcbc381e0c230dd07f80f69e8d08 Mon Sep 17 00:00:00 2001 From: Helas Date: Sun, 11 Aug 2019 08:17:38 -0300 Subject: [PATCH] Using MediaStore to open newly downloaded media by touching the notification (#35) --- .../me/saket/dank/notifs/MediaDownloadService.java | 11 ++++++++++- app/src/main/java/me/saket/dank/utils/Intents.java | 6 +++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/me/saket/dank/notifs/MediaDownloadService.java b/app/src/main/java/me/saket/dank/notifs/MediaDownloadService.java index b2314dfaf..4fb128e8d 100644 --- a/app/src/main/java/me/saket/dank/notifs/MediaDownloadService.java +++ b/app/src/main/java/me/saket/dank/notifs/MediaDownloadService.java @@ -7,6 +7,7 @@ import android.app.Notification; import android.app.PendingIntent; import android.app.Service; +import android.content.ContentValues; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; @@ -16,6 +17,7 @@ import android.net.Uri; import android.os.Build; import android.os.IBinder; +import android.provider.MediaStore; import android.support.annotation.Nullable; import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationManagerCompat; @@ -357,7 +359,7 @@ private void displaySuccessNotification(MediaDownloadJob completedDownloadJob, i PendingIntent viewImagePendingIntent = PendingIntent.getActivity(this, createPendingIntentRequestId(REQUESTCODE_OPEN_IMAGE_PREFIX_, notificationId), - Intents.createForViewingMedia(this, mediaContentUri), + Intents.createForViewingMedia(this, mediaContentUri, completedDownloadJob.mediaLink().isVideo()), PendingIntent.FLAG_CANCEL_CURRENT ); @@ -588,6 +590,13 @@ private Function moveFileToUserSpaceOnDownlo String mediaFileName = Urls.parseFileNameWithExtension(downloadedMediaLink.highQualityUrl()); //noinspection LambdaParameterTypeCanBeSpecified,ConstantConditions File userAccessibleFile = Files2.INSTANCE.copyFileToPicturesDirectory(getResources(), downloadJobUpdate.downloadedFile(), mediaFileName); + + //broadcast file + ContentValues values = new ContentValues(); + values.put(MediaStore.Images.Media.TITLE, mediaFileName); + values.put(MediaStore.Images.Media.DISPLAY_NAME, mediaFileName); + getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,values); + return MediaDownloadJob.downloaded(downloadedMediaLink, userAccessibleFile, downloadJobUpdate.timestamp()); } else { diff --git a/app/src/main/java/me/saket/dank/utils/Intents.java b/app/src/main/java/me/saket/dank/utils/Intents.java index 23fd50413..5179aab20 100644 --- a/app/src/main/java/me/saket/dank/utils/Intents.java +++ b/app/src/main/java/me/saket/dank/utils/Intents.java @@ -63,13 +63,13 @@ public static Intent createForSharingMedia(Context context, Uri mediaContentUri) } @CheckResult - public static Intent createForViewingMedia(Context context, Uri mediaContentUri) { + public static Intent createForViewingMedia(Context context, Uri mediaContentUri, Boolean isVideo) { return new Intent().setAction(Intent.ACTION_VIEW) .putExtra(ShareCompat.EXTRA_CALLING_PACKAGE, context.getPackageName()) .addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT) .putExtra(Intent.EXTRA_STREAM, mediaContentUri) - .setType(context.getContentResolver().getType(mediaContentUri)) - .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); + .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) + .setDataAndType(mediaContentUri, (!isVideo) ? "image/*" : "video/*"); } public static Intent createForPlayStoreListing(Context context, String packageName) {