mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 20:33:53 +01:00
Convert download code to kotlin (#3665)
This commit is contained in:
parent
0d6a79e91a
commit
55c5b1abf5
2 changed files with 73 additions and 64 deletions
|
|
@ -1,11 +1,11 @@
|
||||||
package fr.free.nrw.commons.media;
|
package fr.free.nrw.commons.media;
|
||||||
|
|
||||||
|
import static fr.free.nrw.commons.Utils.handleWebUrl;
|
||||||
|
|
||||||
import android.annotation.SuppressLint;
|
import android.annotation.SuppressLint;
|
||||||
import android.app.DownloadManager;
|
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.Environment;
|
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.Menu;
|
import android.view.Menu;
|
||||||
|
|
@ -13,40 +13,31 @@ import android.view.MenuInflater;
|
||||||
import android.view.MenuItem;
|
import android.view.MenuItem;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
import android.widget.Toast;
|
|
||||||
|
|
||||||
import androidx.fragment.app.Fragment;
|
import androidx.fragment.app.Fragment;
|
||||||
import androidx.fragment.app.FragmentManager;
|
import androidx.fragment.app.FragmentManager;
|
||||||
import androidx.fragment.app.FragmentStatePagerAdapter;
|
import androidx.fragment.app.FragmentStatePagerAdapter;
|
||||||
import androidx.viewpager.widget.ViewPager;
|
import androidx.viewpager.widget.ViewPager;
|
||||||
|
|
||||||
import com.google.android.material.snackbar.Snackbar;
|
|
||||||
|
|
||||||
import javax.inject.Inject;
|
|
||||||
import javax.inject.Named;
|
|
||||||
|
|
||||||
import butterknife.BindView;
|
import butterknife.BindView;
|
||||||
import butterknife.ButterKnife;
|
import butterknife.ButterKnife;
|
||||||
|
import com.google.android.material.snackbar.Snackbar;
|
||||||
import fr.free.nrw.commons.Media;
|
import fr.free.nrw.commons.Media;
|
||||||
import fr.free.nrw.commons.R;
|
import fr.free.nrw.commons.R;
|
||||||
import fr.free.nrw.commons.category.CategoryImagesCallback;
|
|
||||||
import fr.free.nrw.commons.auth.SessionManager;
|
import fr.free.nrw.commons.auth.SessionManager;
|
||||||
import fr.free.nrw.commons.bookmarks.Bookmark;
|
import fr.free.nrw.commons.bookmarks.Bookmark;
|
||||||
import fr.free.nrw.commons.bookmarks.pictures.BookmarkPicturesContentProvider;
|
import fr.free.nrw.commons.bookmarks.pictures.BookmarkPicturesContentProvider;
|
||||||
import fr.free.nrw.commons.bookmarks.pictures.BookmarkPicturesDao;
|
import fr.free.nrw.commons.bookmarks.pictures.BookmarkPicturesDao;
|
||||||
|
import fr.free.nrw.commons.category.CategoryImagesCallback;
|
||||||
import fr.free.nrw.commons.contributions.Contribution;
|
import fr.free.nrw.commons.contributions.Contribution;
|
||||||
import fr.free.nrw.commons.di.CommonsDaggerSupportFragment;
|
import fr.free.nrw.commons.di.CommonsDaggerSupportFragment;
|
||||||
import fr.free.nrw.commons.kvstore.JsonKvStore;
|
import fr.free.nrw.commons.kvstore.JsonKvStore;
|
||||||
|
import fr.free.nrw.commons.utils.DownloadUtils;
|
||||||
import fr.free.nrw.commons.utils.ImageUtils;
|
import fr.free.nrw.commons.utils.ImageUtils;
|
||||||
import fr.free.nrw.commons.utils.NetworkUtils;
|
import fr.free.nrw.commons.utils.NetworkUtils;
|
||||||
import fr.free.nrw.commons.utils.PermissionUtils;
|
|
||||||
import fr.free.nrw.commons.utils.ViewUtil;
|
import fr.free.nrw.commons.utils.ViewUtil;
|
||||||
|
import javax.inject.Inject;
|
||||||
|
import javax.inject.Named;
|
||||||
import timber.log.Timber;
|
import timber.log.Timber;
|
||||||
|
|
||||||
import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;
|
|
||||||
import static android.content.Context.DOWNLOAD_SERVICE;
|
|
||||||
import static fr.free.nrw.commons.Utils.handleWebUrl;
|
|
||||||
|
|
||||||
public class MediaDetailPagerFragment extends CommonsDaggerSupportFragment implements ViewPager.OnPageChangeListener {
|
public class MediaDetailPagerFragment extends CommonsDaggerSupportFragment implements ViewPager.OnPageChangeListener {
|
||||||
|
|
||||||
@Inject SessionManager sessionManager;
|
@Inject SessionManager sessionManager;
|
||||||
|
|
@ -168,7 +159,7 @@ public class MediaDetailPagerFragment extends CommonsDaggerSupportFragment imple
|
||||||
ViewUtil.showShortSnackbar(getView(), R.string.no_internet);
|
ViewUtil.showShortSnackbar(getView(), R.string.no_internet);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
downloadMedia(m);
|
DownloadUtils.downloadMedia(getActivity(), m);
|
||||||
return true;
|
return true;
|
||||||
case R.id.menu_set_as_wallpaper:
|
case R.id.menu_set_as_wallpaper:
|
||||||
// Set wallpaper
|
// Set wallpaper
|
||||||
|
|
@ -192,53 +183,6 @@ public class MediaDetailPagerFragment extends CommonsDaggerSupportFragment imple
|
||||||
ImageUtils.setWallpaperFromImageUrl(getActivity(), Uri.parse(media.getImageUrl()));
|
ImageUtils.setWallpaperFromImageUrl(getActivity(), Uri.parse(media.getImageUrl()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Start the media file downloading to the local SD card/storage.
|
|
||||||
* The file can then be opened in Gallery or other apps.
|
|
||||||
*
|
|
||||||
* @param m Media file to download
|
|
||||||
*/
|
|
||||||
private void downloadMedia(Media m) {
|
|
||||||
String imageUrl = m.getImageUrl(), fileName = m.getFilename();
|
|
||||||
|
|
||||||
if (imageUrl == null
|
|
||||||
|| fileName == null
|
|
||||||
|| getContext() == null
|
|
||||||
|| getActivity() == null) {
|
|
||||||
Timber.d("Skipping download media as either imageUrl %s or filename %s activity is null", imageUrl, fileName);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Strip 'File:' from beginning of filename, we really shouldn't store it
|
|
||||||
fileName = fileName.replaceFirst("^File:", "");
|
|
||||||
|
|
||||||
Uri imageUri = Uri.parse(imageUrl);
|
|
||||||
|
|
||||||
DownloadManager.Request req = new DownloadManager.Request(imageUri);
|
|
||||||
//These are not the image title and description fields, they are download descs for notifications
|
|
||||||
req.setDescription(getString(R.string.app_name));
|
|
||||||
req.setTitle(m.getDisplayTitle());
|
|
||||||
req.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
|
|
||||||
|
|
||||||
// Modern Android updates the gallery automatically. Yay!
|
|
||||||
req.allowScanningByMediaScanner();
|
|
||||||
req.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
|
|
||||||
PermissionUtils.checkPermissionsAndPerformAction(getActivity(), WRITE_EXTERNAL_STORAGE,
|
|
||||||
() -> enqueueRequest(req), () -> Toast.makeText(getContext(),
|
|
||||||
R.string.download_failed_we_cannot_download_the_file_without_storage_permission,
|
|
||||||
Toast.LENGTH_SHORT).show(), R.string.storage_permission,
|
|
||||||
R.string.write_storage_permission_rationale);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private void enqueueRequest(DownloadManager.Request req) {
|
|
||||||
DownloadManager systemService =
|
|
||||||
(DownloadManager) getActivity().getSystemService(DOWNLOAD_SERVICE);
|
|
||||||
if (systemService != null) {
|
|
||||||
systemService.enqueue(req);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
||||||
if (!editable) { // Disable menu options for editable views
|
if (!editable) { // Disable menu options for editable views
|
||||||
|
|
|
||||||
65
app/src/main/java/fr/free/nrw/commons/utils/DownloadUtils.kt
Normal file
65
app/src/main/java/fr/free/nrw/commons/utils/DownloadUtils.kt
Normal file
|
|
@ -0,0 +1,65 @@
|
||||||
|
package fr.free.nrw.commons.utils
|
||||||
|
|
||||||
|
import android.Manifest.permission
|
||||||
|
import android.app.Activity
|
||||||
|
import android.app.DownloadManager
|
||||||
|
import android.content.Context
|
||||||
|
import android.net.Uri
|
||||||
|
import android.os.Environment
|
||||||
|
import android.widget.Toast
|
||||||
|
import fr.free.nrw.commons.Media
|
||||||
|
import fr.free.nrw.commons.R
|
||||||
|
import timber.log.Timber
|
||||||
|
|
||||||
|
object DownloadUtils {
|
||||||
|
/**
|
||||||
|
* Start the media file downloading to the local SD card/storage. The file can then be opened in
|
||||||
|
* Gallery or other apps.
|
||||||
|
*
|
||||||
|
* @param m Media file to download
|
||||||
|
*/
|
||||||
|
@JvmStatic
|
||||||
|
fun downloadMedia(activity: Activity?, m: Media) {
|
||||||
|
val imageUrl = m.getImageUrl()
|
||||||
|
var fileName = m.getFilename()
|
||||||
|
if (imageUrl == null || fileName == null || activity == null
|
||||||
|
) {
|
||||||
|
Timber.d(
|
||||||
|
"Skipping download media as either imageUrl %s or filename %s activity is null",
|
||||||
|
imageUrl, fileName
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// Strip 'File:' from beginning of filename, we really shouldn't store it
|
||||||
|
fileName = fileName.replaceFirst("^File:".toRegex(), "")
|
||||||
|
val imageUri = Uri.parse(imageUrl)
|
||||||
|
val req = DownloadManager.Request(imageUri)
|
||||||
|
//These are not the image title and description fields, they are download descs for notifications
|
||||||
|
req.setDescription(activity.getString(R.string.app_name))
|
||||||
|
req.setTitle(m.displayTitle)
|
||||||
|
req.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName)
|
||||||
|
// Modern Android updates the gallery automatically. Yay!
|
||||||
|
req.allowScanningByMediaScanner()
|
||||||
|
req.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
|
||||||
|
PermissionUtils.checkPermissionsAndPerformAction(
|
||||||
|
activity,
|
||||||
|
permission.WRITE_EXTERNAL_STORAGE,
|
||||||
|
{ enqueueRequest(activity, req) },
|
||||||
|
{
|
||||||
|
Toast.makeText(
|
||||||
|
activity,
|
||||||
|
R.string.download_failed_we_cannot_download_the_file_without_storage_permission,
|
||||||
|
Toast.LENGTH_SHORT
|
||||||
|
).show()
|
||||||
|
},
|
||||||
|
R.string.storage_permission,
|
||||||
|
R.string.write_storage_permission_rationale
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun enqueueRequest(activity: Activity, req: DownloadManager.Request) {
|
||||||
|
val systemService =
|
||||||
|
activity.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
|
||||||
|
systemService?.enqueue(req)
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue