Dbrant has uploaded a new change for review.
https://gerrit.wikimedia.org/r/322125
Change subject: Fix MediaDownloadReceiver to work with all API versions.
......................................................................
Fix MediaDownloadReceiver to work with all API versions.
This resolves one of the blockers for targeting API 24+.
This fixes the way we update the device's MediaStore upon completion of a
downloaded image or other media file. Instead of using the deprecated
COLUMN_LOCAL_FILENAME, we now use COLUMN_LOCAL_URI. All of the other logic
remains unchanged.
Also, it looks like the MediaDownloadReceiver wasn't wired correctly into
MainFragment, so it wasn't receiving the download completion event. This
patch wires it in correctly.
Bug: T144920
Change-Id: Ie760cd7f86f52a18d0c63ad658f73eba8f0e7c1e
---
M app/src/main/java/org/wikipedia/main/MainFragment.java
M app/src/main/java/org/wikipedia/page/gallery/MediaDownloadReceiver.java
2 files changed, 22 insertions(+), 10 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/apps/android/wikipedia
refs/changes/25/322125/1
diff --git a/app/src/main/java/org/wikipedia/main/MainFragment.java
b/app/src/main/java/org/wikipedia/main/MainFragment.java
index 822b093..2155771 100644
--- a/app/src/main/java/org/wikipedia/main/MainFragment.java
+++ b/app/src/main/java/org/wikipedia/main/MainFragment.java
@@ -2,8 +2,10 @@
import android.annotation.TargetApi;
import android.app.Activity;
+import android.app.DownloadManager;
import android.content.ActivityNotFoundException;
import android.content.Intent;
+import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.location.Location;
import android.os.Build;
@@ -75,6 +77,7 @@
@BindView(R.id.fragment_main_nav_tab_layout) TabLayout tabLayout;
private Unbinder unbinder;
private ExclusiveBottomSheetPresenter bottomSheetPresenter = new
ExclusiveBottomSheetPresenter();
+ private MediaDownloadReceiver mediaDownloadReceiver;
// The permissions request API doesn't take a callback, so in the event we
have to
// ask for permission to download a featured image from the feed, we'll
have to hold
@@ -110,8 +113,16 @@
return view;
}
+ @Override
+ public void onPause() {
+ super.onPause();
+ getContext().unregisterReceiver(mediaDownloadReceiver);
+ }
+
@Override public void onResume() {
super.onResume();
+ getContext().registerReceiver(mediaDownloadReceiver,
+ new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
// update toolbar, since Tab count might have changed
getActivity().supportInvalidateOptionsMenu();
// reset the last-page-viewed timer
@@ -146,6 +157,7 @@
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setHasOptionsMenu(true);
+ mediaDownloadReceiver = new MediaDownloadReceiver(getActivity());
}
@Override
diff --git
a/app/src/main/java/org/wikipedia/page/gallery/MediaDownloadReceiver.java
b/app/src/main/java/org/wikipedia/page/gallery/MediaDownloadReceiver.java
index 63896b5..fa40e86 100644
--- a/app/src/main/java/org/wikipedia/page/gallery/MediaDownloadReceiver.java
+++ b/app/src/main/java/org/wikipedia/page/gallery/MediaDownloadReceiver.java
@@ -21,21 +21,21 @@
public class MediaDownloadReceiver extends BroadcastReceiver {
private static final String FILE_NAMESPACE = "File:";
- private Activity activity;
- private DownloadManager downloadManager;
+ @NonNull private Activity activity;
+ @NonNull private DownloadManager downloadManager;
- public MediaDownloadReceiver(Activity activity) {
+ public MediaDownloadReceiver(@NonNull Activity activity) {
this.activity = activity;
downloadManager = (DownloadManager)
activity.getSystemService(Context.DOWNLOAD_SERVICE);
}
- public void download(FeaturedImage featuredImage) {
+ public void download(@NonNull FeaturedImage featuredImage) {
String filename = FileUtil.sanitizeFileName(featuredImage.title());
String targetDirectory = Environment.DIRECTORY_PICTURES;
performDownloadRequest(featuredImage.image().source(),
targetDirectory, filename, null);
}
- public void download(GalleryItem galleryItem) {
+ public void download(@NonNull GalleryItem galleryItem) {
String saveFilename =
FileUtil.sanitizeFileName(trimFileNamespace(galleryItem.getName()));
String targetDirectory;
if (FileUtil.isVideo(galleryItem.getMimeType())) {
@@ -77,11 +77,12 @@
try {
if (c.moveToFirst()) {
int statusIndex =
c.getColumnIndexOrThrow(DownloadManager.COLUMN_STATUS);
- int pathIndex =
c.getColumnIndexOrThrow(DownloadManager.COLUMN_LOCAL_FILENAME);
+ int pathIndex =
c.getColumnIndexOrThrow(DownloadManager.COLUMN_LOCAL_URI);
int mimeIndex =
c.getColumnIndexOrThrow(DownloadManager.COLUMN_MEDIA_TYPE);
if (DownloadManager.STATUS_SUCCESSFUL ==
c.getInt(statusIndex)) {
- notifyContentResolver(c.getString(pathIndex),
c.getString(mimeIndex));
FeedbackUtil.showMessage(activity,
R.string.gallery_save_success);
+
notifyContentResolver(Uri.parse(c.getString(pathIndex)).getPath(),
+ c.getString(mimeIndex));
}
}
} finally {
@@ -90,11 +91,11 @@
}
}
- private String trimFileNamespace(String filename) {
+ @NonNull private String trimFileNamespace(@NonNull String filename) {
return filename.startsWith(FILE_NAMESPACE) ?
filename.substring(FILE_NAMESPACE.length()) : filename;
}
- private void notifyContentResolver(String path, String mimeType) {
+ private void notifyContentResolver(@NonNull String path, @NonNull String
mimeType) {
ContentValues values = new ContentValues();
Uri contentUri;
if (FileUtil.isVideo(mimeType)) {
@@ -113,4 +114,3 @@
activity.getContentResolver().insert(contentUri, values);
}
}
-
--
To view, visit https://gerrit.wikimedia.org/r/322125
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ie760cd7f86f52a18d0c63ad658f73eba8f0e7c1e
Gerrit-PatchSet: 1
Gerrit-Project: apps/android/wikipedia
Gerrit-Branch: master
Gerrit-Owner: Dbrant <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits