Skip to content

Commit 55c5b1a

Browse files
authored
Convert download code to kotlin (commons-app#3665)
1 parent 0d6a79e commit 55c5b1a

File tree

2 files changed

+73
-64
lines changed

2 files changed

+73
-64
lines changed

app/src/main/java/fr/free/nrw/commons/media/MediaDetailPagerFragment.java

+8-64
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,43 @@
11
package fr.free.nrw.commons.media;
22

3+
import static fr.free.nrw.commons.Utils.handleWebUrl;
4+
35
import android.annotation.SuppressLint;
4-
import android.app.DownloadManager;
56
import android.content.Intent;
67
import android.net.Uri;
78
import android.os.Bundle;
8-
import android.os.Environment;
99
import android.os.Handler;
1010
import android.view.LayoutInflater;
1111
import android.view.Menu;
1212
import android.view.MenuInflater;
1313
import android.view.MenuItem;
1414
import android.view.View;
1515
import android.view.ViewGroup;
16-
import android.widget.Toast;
17-
1816
import androidx.fragment.app.Fragment;
1917
import androidx.fragment.app.FragmentManager;
2018
import androidx.fragment.app.FragmentStatePagerAdapter;
2119
import androidx.viewpager.widget.ViewPager;
22-
23-
import com.google.android.material.snackbar.Snackbar;
24-
25-
import javax.inject.Inject;
26-
import javax.inject.Named;
27-
2820
import butterknife.BindView;
2921
import butterknife.ButterKnife;
22+
import com.google.android.material.snackbar.Snackbar;
3023
import fr.free.nrw.commons.Media;
3124
import fr.free.nrw.commons.R;
32-
import fr.free.nrw.commons.category.CategoryImagesCallback;
3325
import fr.free.nrw.commons.auth.SessionManager;
3426
import fr.free.nrw.commons.bookmarks.Bookmark;
3527
import fr.free.nrw.commons.bookmarks.pictures.BookmarkPicturesContentProvider;
3628
import fr.free.nrw.commons.bookmarks.pictures.BookmarkPicturesDao;
29+
import fr.free.nrw.commons.category.CategoryImagesCallback;
3730
import fr.free.nrw.commons.contributions.Contribution;
3831
import fr.free.nrw.commons.di.CommonsDaggerSupportFragment;
3932
import fr.free.nrw.commons.kvstore.JsonKvStore;
33+
import fr.free.nrw.commons.utils.DownloadUtils;
4034
import fr.free.nrw.commons.utils.ImageUtils;
4135
import fr.free.nrw.commons.utils.NetworkUtils;
42-
import fr.free.nrw.commons.utils.PermissionUtils;
4336
import fr.free.nrw.commons.utils.ViewUtil;
37+
import javax.inject.Inject;
38+
import javax.inject.Named;
4439
import timber.log.Timber;
4540

46-
import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;
47-
import static android.content.Context.DOWNLOAD_SERVICE;
48-
import static fr.free.nrw.commons.Utils.handleWebUrl;
49-
5041
public class MediaDetailPagerFragment extends CommonsDaggerSupportFragment implements ViewPager.OnPageChangeListener {
5142

5243
@Inject SessionManager sessionManager;
@@ -168,7 +159,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
168159
ViewUtil.showShortSnackbar(getView(), R.string.no_internet);
169160
return false;
170161
}
171-
downloadMedia(m);
162+
DownloadUtils.downloadMedia(getActivity(), m);
172163
return true;
173164
case R.id.menu_set_as_wallpaper:
174165
// Set wallpaper
@@ -192,53 +183,6 @@ private void setWallpaper(Media media) {
192183
ImageUtils.setWallpaperFromImageUrl(getActivity(), Uri.parse(media.getImageUrl()));
193184
}
194185

195-
/**
196-
* Start the media file downloading to the local SD card/storage.
197-
* The file can then be opened in Gallery or other apps.
198-
*
199-
* @param m Media file to download
200-
*/
201-
private void downloadMedia(Media m) {
202-
String imageUrl = m.getImageUrl(), fileName = m.getFilename();
203-
204-
if (imageUrl == null
205-
|| fileName == null
206-
|| getContext() == null
207-
|| getActivity() == null) {
208-
Timber.d("Skipping download media as either imageUrl %s or filename %s activity is null", imageUrl, fileName);
209-
return;
210-
}
211-
212-
// Strip 'File:' from beginning of filename, we really shouldn't store it
213-
fileName = fileName.replaceFirst("^File:", "");
214-
215-
Uri imageUri = Uri.parse(imageUrl);
216-
217-
DownloadManager.Request req = new DownloadManager.Request(imageUri);
218-
//These are not the image title and description fields, they are download descs for notifications
219-
req.setDescription(getString(R.string.app_name));
220-
req.setTitle(m.getDisplayTitle());
221-
req.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
222-
223-
// Modern Android updates the gallery automatically. Yay!
224-
req.allowScanningByMediaScanner();
225-
req.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
226-
PermissionUtils.checkPermissionsAndPerformAction(getActivity(), WRITE_EXTERNAL_STORAGE,
227-
() -> enqueueRequest(req), () -> Toast.makeText(getContext(),
228-
R.string.download_failed_we_cannot_download_the_file_without_storage_permission,
229-
Toast.LENGTH_SHORT).show(), R.string.storage_permission,
230-
R.string.write_storage_permission_rationale);
231-
232-
}
233-
234-
private void enqueueRequest(DownloadManager.Request req) {
235-
DownloadManager systemService =
236-
(DownloadManager) getActivity().getSystemService(DOWNLOAD_SERVICE);
237-
if (systemService != null) {
238-
systemService.enqueue(req);
239-
}
240-
}
241-
242186
@Override
243187
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
244188
if (!editable) { // Disable menu options for editable views
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package fr.free.nrw.commons.utils
2+
3+
import android.Manifest.permission
4+
import android.app.Activity
5+
import android.app.DownloadManager
6+
import android.content.Context
7+
import android.net.Uri
8+
import android.os.Environment
9+
import android.widget.Toast
10+
import fr.free.nrw.commons.Media
11+
import fr.free.nrw.commons.R
12+
import timber.log.Timber
13+
14+
object DownloadUtils {
15+
/**
16+
* Start the media file downloading to the local SD card/storage. The file can then be opened in
17+
* Gallery or other apps.
18+
*
19+
* @param m Media file to download
20+
*/
21+
@JvmStatic
22+
fun downloadMedia(activity: Activity?, m: Media) {
23+
val imageUrl = m.getImageUrl()
24+
var fileName = m.getFilename()
25+
if (imageUrl == null || fileName == null || activity == null
26+
) {
27+
Timber.d(
28+
"Skipping download media as either imageUrl %s or filename %s activity is null",
29+
imageUrl, fileName
30+
)
31+
return
32+
}
33+
// Strip 'File:' from beginning of filename, we really shouldn't store it
34+
fileName = fileName.replaceFirst("^File:".toRegex(), "")
35+
val imageUri = Uri.parse(imageUrl)
36+
val req = DownloadManager.Request(imageUri)
37+
//These are not the image title and description fields, they are download descs for notifications
38+
req.setDescription(activity.getString(R.string.app_name))
39+
req.setTitle(m.displayTitle)
40+
req.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName)
41+
// Modern Android updates the gallery automatically. Yay!
42+
req.allowScanningByMediaScanner()
43+
req.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
44+
PermissionUtils.checkPermissionsAndPerformAction(
45+
activity,
46+
permission.WRITE_EXTERNAL_STORAGE,
47+
{ enqueueRequest(activity, req) },
48+
{
49+
Toast.makeText(
50+
activity,
51+
R.string.download_failed_we_cannot_download_the_file_without_storage_permission,
52+
Toast.LENGTH_SHORT
53+
).show()
54+
},
55+
R.string.storage_permission,
56+
R.string.write_storage_permission_rationale
57+
)
58+
}
59+
60+
private fun enqueueRequest(activity: Activity, req: DownloadManager.Request) {
61+
val systemService =
62+
activity.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
63+
systemService?.enqueue(req)
64+
}
65+
}

0 commit comments

Comments
 (0)