1616import android .os .Bundle ;
1717import android .os .Environment ;
1818import android .os .IBinder ;
19+ import android .provider .Settings ;
1920import android .util .Log ;
2021import android .view .LayoutInflater ;
2122import android .view .View ;
@@ -147,7 +148,6 @@ public class DownloadDialog extends DialogFragment
147148 registerForActivityResult (
148149 new StartActivityForResult (), this ::requestDownloadPickVideoFolderResult );
149150
150-
151151 /*//////////////////////////////////////////////////////////////////////////
152152 // Instance creation
153153 //////////////////////////////////////////////////////////////////////////*/
@@ -565,7 +565,6 @@ private void requestDownloadPickFolderResult(@NonNull final ActivityResult resul
565565 }
566566 }
567567
568-
569568 /*//////////////////////////////////////////////////////////////////////////
570569 // Listeners
571570 //////////////////////////////////////////////////////////////////////////*/
@@ -784,6 +783,7 @@ private void prepareSelectedDownload() {
784783 final StoredDirectoryHelper mainStorage ;
785784 final MediaFormat format ;
786785 final String selectedMediaType ;
786+ final long size ;
787787
788788 // first, build the filename and get the output folder (if possible)
789789 // later, run a very very very large file checking logic
@@ -795,6 +795,7 @@ private void prepareSelectedDownload() {
795795 selectedMediaType = getString (R .string .last_download_type_audio_key );
796796 mainStorage = mainStorageAudio ;
797797 format = audioStreamsAdapter .getItem (selectedAudioIndex ).getFormat ();
798+ size = getWrappedAudioStreams ().getSizeInBytes (selectedAudioIndex );
798799 if (format == MediaFormat .WEBMA_OPUS ) {
799800 mimeTmp = "audio/ogg" ;
800801 filenameTmp += "opus" ;
@@ -807,6 +808,7 @@ private void prepareSelectedDownload() {
807808 selectedMediaType = getString (R .string .last_download_type_video_key );
808809 mainStorage = mainStorageVideo ;
809810 format = videoStreamsAdapter .getItem (selectedVideoIndex ).getFormat ();
811+ size = wrappedVideoStreams .getSizeInBytes (selectedVideoIndex );
810812 if (format != null ) {
811813 mimeTmp = format .mimeType ;
812814 filenameTmp += format .getSuffix ();
@@ -816,6 +818,7 @@ private void prepareSelectedDownload() {
816818 selectedMediaType = getString (R .string .last_download_type_subtitle_key );
817819 mainStorage = mainStorageVideo ; // subtitle & video files go together
818820 format = subtitleStreamsAdapter .getItem (selectedSubtitleIndex ).getFormat ();
821+ size = wrappedSubtitleStreams .getSizeInBytes (selectedSubtitleIndex );
819822 if (format != null ) {
820823 mimeTmp = format .mimeType ;
821824 }
@@ -871,6 +874,22 @@ private void prepareSelectedDownload() {
871874 return ;
872875 }
873876
877+ // Check for free memory space (for api 24 and up)
878+ if (android .os .Build .VERSION .SDK_INT >= android .os .Build .VERSION_CODES .N ) {
879+ final long freeSpace = mainStorage .getFreeMemory ();
880+ if (freeSpace <= size ) {
881+ Toast .makeText (context , getString (R .
882+ string .error_insufficient_storage ), Toast .LENGTH_LONG ).show ();
883+ // move the user to storage setting tab
884+ final Intent storageSettingsIntent = new Intent (Settings .
885+ ACTION_INTERNAL_STORAGE_SETTINGS );
886+ if (storageSettingsIntent .resolveActivity (context .getPackageManager ()) != null ) {
887+ startActivity (storageSettingsIntent );
888+ }
889+ return ;
890+ }
891+ }
892+
874893 // check for existing file with the same name
875894 checkSelectedDownload (mainStorage , mainStorage .findFile (filenameTmp ), filenameTmp ,
876895 mimeTmp );
0 commit comments