1
1
package fr .free .nrw .commons .media ;
2
2
3
+ import static fr .free .nrw .commons .Utils .handleWebUrl ;
4
+
3
5
import android .annotation .SuppressLint ;
4
- import android .app .DownloadManager ;
5
6
import android .content .Intent ;
6
7
import android .net .Uri ;
7
8
import android .os .Bundle ;
8
- import android .os .Environment ;
9
9
import android .os .Handler ;
10
10
import android .view .LayoutInflater ;
11
11
import android .view .Menu ;
12
12
import android .view .MenuInflater ;
13
13
import android .view .MenuItem ;
14
14
import android .view .View ;
15
15
import android .view .ViewGroup ;
16
- import android .widget .Toast ;
17
-
18
16
import androidx .fragment .app .Fragment ;
19
17
import androidx .fragment .app .FragmentManager ;
20
18
import androidx .fragment .app .FragmentStatePagerAdapter ;
21
19
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
-
28
20
import butterknife .BindView ;
29
21
import butterknife .ButterKnife ;
22
+ import com .google .android .material .snackbar .Snackbar ;
30
23
import fr .free .nrw .commons .Media ;
31
24
import fr .free .nrw .commons .R ;
32
- import fr .free .nrw .commons .category .CategoryImagesCallback ;
33
25
import fr .free .nrw .commons .auth .SessionManager ;
34
26
import fr .free .nrw .commons .bookmarks .Bookmark ;
35
27
import fr .free .nrw .commons .bookmarks .pictures .BookmarkPicturesContentProvider ;
36
28
import fr .free .nrw .commons .bookmarks .pictures .BookmarkPicturesDao ;
29
+ import fr .free .nrw .commons .category .CategoryImagesCallback ;
37
30
import fr .free .nrw .commons .contributions .Contribution ;
38
31
import fr .free .nrw .commons .di .CommonsDaggerSupportFragment ;
39
32
import fr .free .nrw .commons .kvstore .JsonKvStore ;
33
+ import fr .free .nrw .commons .utils .DownloadUtils ;
40
34
import fr .free .nrw .commons .utils .ImageUtils ;
41
35
import fr .free .nrw .commons .utils .NetworkUtils ;
42
- import fr .free .nrw .commons .utils .PermissionUtils ;
43
36
import fr .free .nrw .commons .utils .ViewUtil ;
37
+ import javax .inject .Inject ;
38
+ import javax .inject .Named ;
44
39
import timber .log .Timber ;
45
40
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
-
50
41
public class MediaDetailPagerFragment extends CommonsDaggerSupportFragment implements ViewPager .OnPageChangeListener {
51
42
52
43
@ Inject SessionManager sessionManager ;
@@ -168,7 +159,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
168
159
ViewUtil .showShortSnackbar (getView (), R .string .no_internet );
169
160
return false ;
170
161
}
171
- downloadMedia (m );
162
+ DownloadUtils . downloadMedia (getActivity (), m );
172
163
return true ;
173
164
case R .id .menu_set_as_wallpaper :
174
165
// Set wallpaper
@@ -192,53 +183,6 @@ private void setWallpaper(Media media) {
192
183
ImageUtils .setWallpaperFromImageUrl (getActivity (), Uri .parse (media .getImageUrl ()));
193
184
}
194
185
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
-
242
186
@ Override
243
187
public void onCreateOptionsMenu (Menu menu , MenuInflater inflater ) {
244
188
if (!editable ) { // Disable menu options for editable views
0 commit comments