33import android .app .Activity ;
44import android .content .ComponentName ;
55import android .content .Context ;
6+ import android .content .DialogInterface ;
7+ import android .content .DialogInterface .OnDismissListener ;
68import android .content .Intent ;
79import android .content .ServiceConnection ;
810import android .content .SharedPreferences ;
3840
3941import org .schabi .newpipe .MainActivity ;
4042import org .schabi .newpipe .R ;
41- import org .schabi .newpipe .RouterActivity ;
4243import org .schabi .newpipe .databinding .DownloadDialogBinding ;
4344import org .schabi .newpipe .error .ErrorActivity ;
4445import org .schabi .newpipe .error .ErrorInfo ;
@@ -101,6 +102,9 @@ public class DownloadDialog extends DialogFragment
101102 @ State
102103 int selectedSubtitleIndex = 0 ;
103104
105+ @ Nullable
106+ private OnDismissListener onDismissListener = null ;
107+
104108 private StoredDirectoryHelper mainStorageAudio = null ;
105109 private StoredDirectoryHelper mainStorageVideo = null ;
106110 private DownloadManager downloadManager = null ;
@@ -132,6 +136,11 @@ public class DownloadDialog extends DialogFragment
132136 registerForActivityResult (
133137 new StartActivityForResult (), this ::requestDownloadPickVideoFolderResult );
134138
139+
140+ /*//////////////////////////////////////////////////////////////////////////
141+ // Instance creation
142+ //////////////////////////////////////////////////////////////////////////*/
143+
135144 public static DownloadDialog newInstance (final StreamInfo info ) {
136145 final DownloadDialog dialog = new DownloadDialog ();
137146 dialog .setInfo (info );
@@ -153,6 +162,11 @@ public static DownloadDialog newInstance(final Context context, final StreamInfo
153162 return instance ;
154163 }
155164
165+
166+ /*//////////////////////////////////////////////////////////////////////////
167+ // Setters
168+ //////////////////////////////////////////////////////////////////////////*/
169+
156170 private void setInfo (final StreamInfo info ) {
157171 this .currentInfo = info ;
158172 }
@@ -194,6 +208,14 @@ public void setSelectedSubtitleStream(final int ssi) {
194208 this .selectedSubtitleIndex = ssi ;
195209 }
196210
211+ public void setOnDismissListener (@ Nullable final OnDismissListener onDismissListener ) {
212+ this .onDismissListener = onDismissListener ;
213+ }
214+
215+ /*//////////////////////////////////////////////////////////////////////////
216+ // Android lifecycle
217+ //////////////////////////////////////////////////////////////////////////*/
218+
197219 @ Override
198220 public void onCreate (@ Nullable final Bundle savedInstanceState ) {
199221 super .onCreate (savedInstanceState );
@@ -204,7 +226,7 @@ public void onCreate(@Nullable final Bundle savedInstanceState) {
204226
205227 if (!PermissionHelper .checkStoragePermissions (getActivity (),
206228 PermissionHelper .DOWNLOAD_DIALOG_REQUEST_CODE )) {
207- getDialog (). dismiss ();
229+ dismiss ();
208230 return ;
209231 }
210232
@@ -263,10 +285,6 @@ public void onServiceDisconnected(final ComponentName name) {
263285 }, Context .BIND_AUTO_CREATE );
264286 }
265287
266- /*//////////////////////////////////////////////////////////////////////////
267- // Inits
268- //////////////////////////////////////////////////////////////////////////*/
269-
270288 @ Override
271289 public View onCreateView (@ NonNull final LayoutInflater inflater , final ViewGroup container ,
272290 final Bundle savedInstanceState ) {
@@ -322,6 +340,60 @@ public void onStopTrackingTouch(final SeekBar p1) { }
322340 fetchStreamsSize ();
323341 }
324342
343+ private void initToolbar (final Toolbar toolbar ) {
344+ if (DEBUG ) {
345+ Log .d (TAG , "initToolbar() called with: toolbar = [" + toolbar + "]" );
346+ }
347+
348+ toolbar .setTitle (R .string .download_dialog_title );
349+ toolbar .setNavigationIcon (R .drawable .ic_arrow_back );
350+ toolbar .inflateMenu (R .menu .dialog_url );
351+ toolbar .setNavigationOnClickListener (v -> dismiss ());
352+ toolbar .setNavigationContentDescription (R .string .cancel );
353+
354+ okButton = toolbar .findViewById (R .id .okay );
355+ okButton .setEnabled (false ); // disable until the download service connection is done
356+
357+ toolbar .setOnMenuItemClickListener (item -> {
358+ if (item .getItemId () == R .id .okay ) {
359+ prepareSelectedDownload ();
360+ return true ;
361+ }
362+ return false ;
363+ });
364+ }
365+
366+ @ Override
367+ public void onDismiss (@ NonNull final DialogInterface dialog ) {
368+ super .onDismiss (dialog );
369+ if (onDismissListener != null ) {
370+ onDismissListener .onDismiss (dialog );
371+ }
372+ }
373+
374+ @ Override
375+ public void onDestroy () {
376+ super .onDestroy ();
377+ disposables .clear ();
378+ }
379+
380+ @ Override
381+ public void onDestroyView () {
382+ dialogBinding = null ;
383+ super .onDestroyView ();
384+ }
385+
386+ @ Override
387+ public void onSaveInstanceState (@ NonNull final Bundle outState ) {
388+ super .onSaveInstanceState (outState );
389+ Icepick .saveInstanceState (this , outState );
390+ }
391+
392+
393+ /*//////////////////////////////////////////////////////////////////////////
394+ // Video, audio and subtitle spinners
395+ //////////////////////////////////////////////////////////////////////////*/
396+
325397 private void fetchStreamsSize () {
326398 disposables .clear ();
327399 disposables .add (StreamSizeWrapper .fetchSizeForWrapper (wrappedVideoStreams )
@@ -356,30 +428,39 @@ private void fetchStreamsSize() {
356428 currentInfo .getServiceId ()))));
357429 }
358430
359- @ Override
360- public void onDestroy () {
361- super .onDestroy ();
362- disposables .clear ();
431+ private void setupAudioSpinner () {
432+ if (getContext () == null ) {
433+ return ;
434+ }
435+
436+ dialogBinding .qualitySpinner .setAdapter (audioStreamsAdapter );
437+ dialogBinding .qualitySpinner .setSelection (selectedAudioIndex );
438+ setRadioButtonsState (true );
363439 }
364440
365- @ Override
366- public void onDestroyView () {
367- dialogBinding = null ;
368- super .onDestroyView ();
441+ private void setupVideoSpinner () {
442+ if (getContext () == null ) {
443+ return ;
444+ }
445+
446+ dialogBinding .qualitySpinner .setAdapter (videoStreamsAdapter );
447+ dialogBinding .qualitySpinner .setSelection (selectedVideoIndex );
448+ setRadioButtonsState (true );
369449 }
370450
371- /*//////////////////////////////////////////////////////////////////////////
372- // Radio group Video&Audio options - Listener
373- //////////////////////////////////////////////////////////////////////////*/
451+ private void setupSubtitleSpinner () {
452+ if (getContext () == null ) {
453+ return ;
454+ }
374455
375- @ Override
376- public void onSaveInstanceState (@ NonNull final Bundle outState ) {
377- super .onSaveInstanceState (outState );
378- Icepick .saveInstanceState (this , outState );
456+ dialogBinding .qualitySpinner .setAdapter (subtitleStreamsAdapter );
457+ dialogBinding .qualitySpinner .setSelection (selectedSubtitleIndex );
458+ setRadioButtonsState (true );
379459 }
380460
461+
381462 /*//////////////////////////////////////////////////////////////////////////
382- // Streams Spinner Listener
463+ // Activity results
383464 //////////////////////////////////////////////////////////////////////////*/
384465
385466 private void requestDownloadPickAudioFolderResult (final ActivityResult result ) {
@@ -454,66 +535,11 @@ private void requestDownloadPickFolderResult(final ActivityResult result,
454535 }
455536 }
456537
457- private void initToolbar (final Toolbar toolbar ) {
458- if (DEBUG ) {
459- Log .d (TAG , "initToolbar() called with: toolbar = [" + toolbar + "]" );
460- }
461-
462- toolbar .setTitle (R .string .download_dialog_title );
463- toolbar .setNavigationIcon (R .drawable .ic_arrow_back );
464- toolbar .inflateMenu (R .menu .dialog_url );
465- toolbar .setNavigationOnClickListener (v -> requireDialog ().dismiss ());
466- toolbar .setNavigationContentDescription (R .string .cancel );
467-
468- okButton = toolbar .findViewById (R .id .okay );
469- okButton .setEnabled (false ); // disable until the download service connection is done
470-
471- toolbar .setOnMenuItemClickListener (item -> {
472- if (item .getItemId () == R .id .okay ) {
473- prepareSelectedDownload ();
474- if (getActivity () instanceof RouterActivity ) {
475- getActivity ().finish ();
476- }
477- return true ;
478- }
479- return false ;
480- });
481- }
482538
483539 /*//////////////////////////////////////////////////////////////////////////
484- // Utils
540+ // Listeners
485541 //////////////////////////////////////////////////////////////////////////*/
486542
487- private void setupAudioSpinner () {
488- if (getContext () == null ) {
489- return ;
490- }
491-
492- dialogBinding .qualitySpinner .setAdapter (audioStreamsAdapter );
493- dialogBinding .qualitySpinner .setSelection (selectedAudioIndex );
494- setRadioButtonsState (true );
495- }
496-
497- private void setupVideoSpinner () {
498- if (getContext () == null ) {
499- return ;
500- }
501-
502- dialogBinding .qualitySpinner .setAdapter (videoStreamsAdapter );
503- dialogBinding .qualitySpinner .setSelection (selectedVideoIndex );
504- setRadioButtonsState (true );
505- }
506-
507- private void setupSubtitleSpinner () {
508- if (getContext () == null ) {
509- return ;
510- }
511-
512- dialogBinding .qualitySpinner .setAdapter (subtitleStreamsAdapter );
513- dialogBinding .qualitySpinner .setSelection (selectedSubtitleIndex );
514- setRadioButtonsState (true );
515- }
516-
517543 @ Override
518544 public void onCheckedChanged (final RadioGroup group , @ IdRes final int checkedId ) {
519545 if (DEBUG ) {
@@ -563,6 +589,11 @@ public void onItemSelected(final AdapterView<?> parent, final View view,
563589 public void onNothingSelected (final AdapterView <?> parent ) {
564590 }
565591
592+
593+ /*//////////////////////////////////////////////////////////////////////////
594+ // Download
595+ //////////////////////////////////////////////////////////////////////////*/
596+
566597 protected void setupDownloadOptions () {
567598 setRadioButtonsState (false );
568599
@@ -575,7 +606,7 @@ protected void setupDownloadOptions() {
575606 dialogBinding .subtitleButton .setVisibility (isSubtitleStreamsAvailable
576607 ? View .VISIBLE : View .GONE );
577608
578- prefs = PreferenceManager .getDefaultSharedPreferences (getContext ());
609+ prefs = PreferenceManager .getDefaultSharedPreferences (requireContext ());
579610 final String defaultMedia = prefs .getString (getString (R .string .last_used_download_type ),
580611 getString (R .string .last_download_type_video_key ));
581612
@@ -603,7 +634,7 @@ protected void setupDownloadOptions() {
603634 } else {
604635 Toast .makeText (getContext (), R .string .no_streams_available_download ,
605636 Toast .LENGTH_SHORT ).show ();
606- getDialog (). dismiss ();
637+ dismiss ();
607638 }
608639 }
609640
0 commit comments