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