Skip to content

Commit

Permalink
Using MediaStore to open newly downloaded media by touching the notif…
Browse files Browse the repository at this point in the history
…ication (Tunous#35)
  • Loading branch information
Helas authored and Mauro Vietri committed Aug 24, 2019
1 parent c956655 commit a4c7835
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
11 changes: 10 additions & 1 deletion app/src/main/java/me/saket/dank/notifs/MediaDownloadService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
);

Expand Down Expand Up @@ -588,6 +590,13 @@ private Function<MediaDownloadJob, MediaDownloadJob> 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 {
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/me/saket/dank/utils/Intents.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit a4c7835

Please sign in to comment.