@@ -83,6 +83,8 @@ public class DownloadDialog extends DialogFragment
8383 private static final String TAG = "DialogFragment" ;
8484 private static final boolean DEBUG = MainActivity .DEBUG ;
8585 private static final int REQUEST_DOWNLOAD_SAVE_AS = 0x1230 ;
86+ private static final int REQUEST_DOWNLOAD_PICK_VIDEO_FOLDER = 0x789E ;
87+ private static final int REQUEST_DOWNLOAD_PICK_AUDIO_FOLDER = 0x789F ;
8688
8789 @ State
8890 StreamInfo currentInfo ;
@@ -116,6 +118,10 @@ public class DownloadDialog extends DialogFragment
116118
117119 private SharedPreferences prefs ;
118120
121+ // Variables for file name and MIME type when picking new folder because it's not set yet
122+ private String filenameTmp ;
123+ private String mimeTmp ;
124+
119125 public static DownloadDialog newInstance (final StreamInfo info ) {
120126 final DownloadDialog dialog = new DownloadDialog ();
121127 dialog .setInfo (info );
@@ -374,12 +380,16 @@ public void onSaveInstanceState(@NonNull final Bundle outState) {
374380 public void onActivityResult (final int requestCode , final int resultCode , final Intent data ) {
375381 super .onActivityResult (requestCode , resultCode , data );
376382
377- if (requestCode == REQUEST_DOWNLOAD_SAVE_AS && resultCode == Activity .RESULT_OK ) {
378- if (data .getData () == null ) {
379- showFailedDialog (R .string .general_error );
380- return ;
381- }
383+ if (resultCode != Activity .RESULT_OK ) {
384+ return ;
385+ }
382386
387+ if (data .getData () == null ) {
388+ showFailedDialog (R .string .general_error );
389+ return ;
390+ }
391+
392+ if (requestCode == REQUEST_DOWNLOAD_SAVE_AS ) {
383393 if (FilePickerActivityHelper .isOwnFileUri (context , data .getData ())) {
384394 final File file = Utils .getFileForUri (data .getData ());
385395 checkSelectedDownload (null , Uri .fromFile (file ), file .getName (),
@@ -396,6 +406,37 @@ public void onActivityResult(final int requestCode, final int resultCode, final
396406 // check if the selected file was previously used
397407 checkSelectedDownload (null , data .getData (), docFile .getName (),
398408 docFile .getType ());
409+ } else if (requestCode == REQUEST_DOWNLOAD_PICK_AUDIO_FOLDER
410+ || requestCode == REQUEST_DOWNLOAD_PICK_VIDEO_FOLDER ) {
411+ Uri uri = data .getData ();
412+ if (FilePickerActivityHelper .isOwnFileUri (context , uri )) {
413+ uri = Uri .fromFile (Utils .getFileForUri (uri ));
414+ } else {
415+ context .grantUriPermission (context .getPackageName (), uri ,
416+ StoredDirectoryHelper .PERMISSION_FLAGS );
417+ }
418+
419+ final String key ;
420+ final String tag ;
421+ if (requestCode == REQUEST_DOWNLOAD_PICK_AUDIO_FOLDER ) {
422+ key = getString (R .string .download_path_audio_key );
423+ tag = DownloadManager .TAG_AUDIO ;
424+ } else {
425+ key = getString (R .string .download_path_video_key );
426+ tag = DownloadManager .TAG_VIDEO ;
427+ }
428+
429+ PreferenceManager .getDefaultSharedPreferences (context ).edit ()
430+ .putString (key , uri .toString ()).apply ();
431+
432+ try {
433+ final StoredDirectoryHelper mainStorage
434+ = new StoredDirectoryHelper (context , uri , tag );
435+ checkSelectedDownload (mainStorage , mainStorage .findFile (filenameTmp ),
436+ filenameTmp , mimeTmp );
437+ } catch (final IOException e ) {
438+ showFailedDialog (R .string .general_error );
439+ }
399440 }
400441 }
401442
@@ -603,84 +644,89 @@ private void showFailedDialog(@StringRes final int msg) {
603644 private void prepareSelectedDownload () {
604645 final StoredDirectoryHelper mainStorage ;
605646 final MediaFormat format ;
606- final String mime ;
607647 final String selectedMediaType ;
608648
609649 // first, build the filename and get the output folder (if possible)
610650 // later, run a very very very large file checking logic
611651
612- String filename = getNameEditText ().concat ("." );
652+ filenameTmp = getNameEditText ().concat ("." );
613653
614654 switch (dialogBinding .videoAudioGroup .getCheckedRadioButtonId ()) {
615655 case R .id .audio_button :
616656 selectedMediaType = getString (R .string .last_download_type_audio_key );
617657 mainStorage = mainStorageAudio ;
618658 format = audioStreamsAdapter .getItem (selectedAudioIndex ).getFormat ();
619- switch (format ) {
620- case WEBMA_OPUS :
621- mime = "audio/ogg" ;
622- filename += "opus" ;
623- break ;
624- default :
625- mime = format .mimeType ;
626- filename += format .suffix ;
627- break ;
659+ if (format == MediaFormat .WEBMA_OPUS ) {
660+ mimeTmp = "audio/ogg" ;
661+ filenameTmp += "opus" ;
662+ } else {
663+ mimeTmp = format .mimeType ;
664+ filenameTmp += format .suffix ;
628665 }
629666 break ;
630667 case R .id .video_button :
631668 selectedMediaType = getString (R .string .last_download_type_video_key );
632669 mainStorage = mainStorageVideo ;
633670 format = videoStreamsAdapter .getItem (selectedVideoIndex ).getFormat ();
634- mime = format .mimeType ;
635- filename += format .suffix ;
671+ mimeTmp = format .mimeType ;
672+ filenameTmp += format .suffix ;
636673 break ;
637674 case R .id .subtitle_button :
638675 selectedMediaType = getString (R .string .last_download_type_subtitle_key );
639676 mainStorage = mainStorageVideo ; // subtitle & video files go together
640677 format = subtitleStreamsAdapter .getItem (selectedSubtitleIndex ).getFormat ();
641- mime = format .mimeType ;
642- filename += (format == MediaFormat .TTML ? MediaFormat .SRT : format ).suffix ;
678+ mimeTmp = format .mimeType ;
679+ filenameTmp += (format == MediaFormat .TTML ? MediaFormat .SRT : format ).suffix ;
643680 break ;
644681 default :
645682 throw new RuntimeException ("No stream selected" );
646683 }
647684
648- if (mainStorage == null || askForSavePath ) {
649- // This part is called if with SAF preferred:
650- // * older android version running
651- // * save path not defined (via download settings)
652- // * the user checked the "ask where to download" option
653-
654- if (!askForSavePath ) {
655- Toast .makeText (context , getString (R .string .no_available_dir ),
656- Toast .LENGTH_LONG ).show ();
685+ if (!askForSavePath && (mainStorage == null || (mainStorage .isDirect ()
686+ == NewPipeSettings .useStorageAccessFramework (context )))) {
687+ // Pick new download folder if one of:
688+ // - Download folder is not set
689+ // - Download folder uses SAF while SAF is disabled
690+ // - Download folder doesn't use SAF while SAF is enabled
691+ Toast .makeText (context , getString (R .string .no_dir_yet ),
692+ Toast .LENGTH_LONG ).show ();
693+
694+ if (dialogBinding .videoAudioGroup .getCheckedRadioButtonId () == R .id .audio_button ) {
695+ startActivityForResult (StoredDirectoryHelper .getPicker (context ),
696+ REQUEST_DOWNLOAD_PICK_AUDIO_FOLDER );
697+ } else {
698+ startActivityForResult (StoredDirectoryHelper .getPicker (context ),
699+ REQUEST_DOWNLOAD_PICK_VIDEO_FOLDER );
657700 }
658701
702+ return ;
703+ }
704+
705+ if (askForSavePath ) {
706+ final String startPath ;
659707 if (NewPipeSettings .useStorageAccessFramework (context )) {
660- StoredFileHelper .requestSafWithFileCreation (this , REQUEST_DOWNLOAD_SAVE_AS ,
661- filename , mime );
708+ startPath = null ;
662709 } else {
663- File initialSavePath ;
710+ final File initialSavePath ;
664711 if (dialogBinding .videoAudioGroup .getCheckedRadioButtonId () == R .id .audio_button ) {
665712 initialSavePath = NewPipeSettings .getDir (Environment .DIRECTORY_MUSIC );
666713 } else {
667714 initialSavePath = NewPipeSettings .getDir (Environment .DIRECTORY_MOVIES );
668715 }
669-
670- initialSavePath = new File (initialSavePath , filename );
671- startActivityForResult (FilePickerActivityHelper .chooseFileToSave (context ,
672- initialSavePath .getAbsolutePath ()), REQUEST_DOWNLOAD_SAVE_AS );
716+ startPath = initialSavePath .getAbsolutePath ();
673717 }
674718
719+ startActivityForResult (StoredFileHelper .getNewPicker (context , startPath ,
720+ filenameTmp ), REQUEST_DOWNLOAD_SAVE_AS );
721+
675722 return ;
676723 }
677724
678725 // check for existing file with the same name
679- checkSelectedDownload (mainStorage , mainStorage .findFile (filename ), filename , mime );
726+ checkSelectedDownload (mainStorage , mainStorage .findFile (filenameTmp ), filenameTmp , mimeTmp );
680727
681728 // remember the last media type downloaded by the user
682- prefs .edit ()
683- .putString (getString (R .string .last_used_download_type ), selectedMediaType )
729+ prefs .edit ().putString (getString (R .string .last_used_download_type ), selectedMediaType )
684730 .apply ();
685731 }
686732
0 commit comments