Merge pull request #6701 from Stypox/dismiss-download-dialog
Dismiss download dialog correctly
This commit is contained in:
commit
9e2ece78dd
2 changed files with 115 additions and 84 deletions
|
@ -589,9 +589,9 @@ public class RouterActivity extends AppCompatActivity {
|
||||||
downloadDialog.setVideoStreams(sortedVideoStreams);
|
downloadDialog.setVideoStreams(sortedVideoStreams);
|
||||||
downloadDialog.setAudioStreams(result.getAudioStreams());
|
downloadDialog.setAudioStreams(result.getAudioStreams());
|
||||||
downloadDialog.setSelectedVideoStream(selectedVideoStreamIndex);
|
downloadDialog.setSelectedVideoStream(selectedVideoStreamIndex);
|
||||||
|
downloadDialog.setOnDismissListener(dialog -> finish());
|
||||||
downloadDialog.show(fm, "downloadDialog");
|
downloadDialog.show(fm, "downloadDialog");
|
||||||
fm.executePendingTransactions();
|
fm.executePendingTransactions();
|
||||||
downloadDialog.requireDialog().setOnDismissListener(dialog -> finish());
|
|
||||||
}, throwable ->
|
}, throwable ->
|
||||||
showUnsupportedUrlDialog(currentUrl)));
|
showUnsupportedUrlDialog(currentUrl)));
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,8 @@ package org.schabi.newpipe.download;
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.content.ComponentName;
|
import android.content.ComponentName;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.content.DialogInterface;
|
||||||
|
import android.content.DialogInterface.OnDismissListener;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.ServiceConnection;
|
import android.content.ServiceConnection;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
|
@ -38,7 +40,6 @@ import com.nononsenseapps.filepicker.Utils;
|
||||||
|
|
||||||
import org.schabi.newpipe.MainActivity;
|
import org.schabi.newpipe.MainActivity;
|
||||||
import org.schabi.newpipe.R;
|
import org.schabi.newpipe.R;
|
||||||
import org.schabi.newpipe.RouterActivity;
|
|
||||||
import org.schabi.newpipe.databinding.DownloadDialogBinding;
|
import org.schabi.newpipe.databinding.DownloadDialogBinding;
|
||||||
import org.schabi.newpipe.error.ErrorActivity;
|
import org.schabi.newpipe.error.ErrorActivity;
|
||||||
import org.schabi.newpipe.error.ErrorInfo;
|
import org.schabi.newpipe.error.ErrorInfo;
|
||||||
|
@ -101,6 +102,9 @@ public class DownloadDialog extends DialogFragment
|
||||||
@State
|
@State
|
||||||
int selectedSubtitleIndex = 0;
|
int selectedSubtitleIndex = 0;
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private OnDismissListener onDismissListener = null;
|
||||||
|
|
||||||
private StoredDirectoryHelper mainStorageAudio = null;
|
private StoredDirectoryHelper mainStorageAudio = null;
|
||||||
private StoredDirectoryHelper mainStorageVideo = null;
|
private StoredDirectoryHelper mainStorageVideo = null;
|
||||||
private DownloadManager downloadManager = null;
|
private DownloadManager downloadManager = null;
|
||||||
|
@ -132,6 +136,11 @@ public class DownloadDialog extends DialogFragment
|
||||||
registerForActivityResult(
|
registerForActivityResult(
|
||||||
new StartActivityForResult(), this::requestDownloadPickVideoFolderResult);
|
new StartActivityForResult(), this::requestDownloadPickVideoFolderResult);
|
||||||
|
|
||||||
|
|
||||||
|
/*//////////////////////////////////////////////////////////////////////////
|
||||||
|
// Instance creation
|
||||||
|
//////////////////////////////////////////////////////////////////////////*/
|
||||||
|
|
||||||
public static DownloadDialog newInstance(final StreamInfo info) {
|
public static DownloadDialog newInstance(final StreamInfo info) {
|
||||||
final DownloadDialog dialog = new DownloadDialog();
|
final DownloadDialog dialog = new DownloadDialog();
|
||||||
dialog.setInfo(info);
|
dialog.setInfo(info);
|
||||||
|
@ -153,6 +162,11 @@ public class DownloadDialog extends DialogFragment
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*//////////////////////////////////////////////////////////////////////////
|
||||||
|
// Setters
|
||||||
|
//////////////////////////////////////////////////////////////////////////*/
|
||||||
|
|
||||||
private void setInfo(final StreamInfo info) {
|
private void setInfo(final StreamInfo info) {
|
||||||
this.currentInfo = info;
|
this.currentInfo = info;
|
||||||
}
|
}
|
||||||
|
@ -194,6 +208,14 @@ public class DownloadDialog extends DialogFragment
|
||||||
this.selectedSubtitleIndex = ssi;
|
this.selectedSubtitleIndex = ssi;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setOnDismissListener(@Nullable final OnDismissListener onDismissListener) {
|
||||||
|
this.onDismissListener = onDismissListener;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*//////////////////////////////////////////////////////////////////////////
|
||||||
|
// Android lifecycle
|
||||||
|
//////////////////////////////////////////////////////////////////////////*/
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(@Nullable final Bundle savedInstanceState) {
|
public void onCreate(@Nullable final Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
@ -204,7 +226,7 @@ public class DownloadDialog extends DialogFragment
|
||||||
|
|
||||||
if (!PermissionHelper.checkStoragePermissions(getActivity(),
|
if (!PermissionHelper.checkStoragePermissions(getActivity(),
|
||||||
PermissionHelper.DOWNLOAD_DIALOG_REQUEST_CODE)) {
|
PermissionHelper.DOWNLOAD_DIALOG_REQUEST_CODE)) {
|
||||||
getDialog().dismiss();
|
dismiss();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -263,10 +285,6 @@ public class DownloadDialog extends DialogFragment
|
||||||
}, Context.BIND_AUTO_CREATE);
|
}, Context.BIND_AUTO_CREATE);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*//////////////////////////////////////////////////////////////////////////
|
|
||||||
// Inits
|
|
||||||
//////////////////////////////////////////////////////////////////////////*/
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public View onCreateView(@NonNull final LayoutInflater inflater, final ViewGroup container,
|
public View onCreateView(@NonNull final LayoutInflater inflater, final ViewGroup container,
|
||||||
final Bundle savedInstanceState) {
|
final Bundle savedInstanceState) {
|
||||||
|
@ -322,6 +340,60 @@ public class DownloadDialog extends DialogFragment
|
||||||
fetchStreamsSize();
|
fetchStreamsSize();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void initToolbar(final Toolbar toolbar) {
|
||||||
|
if (DEBUG) {
|
||||||
|
Log.d(TAG, "initToolbar() called with: toolbar = [" + toolbar + "]");
|
||||||
|
}
|
||||||
|
|
||||||
|
toolbar.setTitle(R.string.download_dialog_title);
|
||||||
|
toolbar.setNavigationIcon(R.drawable.ic_arrow_back);
|
||||||
|
toolbar.inflateMenu(R.menu.dialog_url);
|
||||||
|
toolbar.setNavigationOnClickListener(v -> dismiss());
|
||||||
|
toolbar.setNavigationContentDescription(R.string.cancel);
|
||||||
|
|
||||||
|
okButton = toolbar.findViewById(R.id.okay);
|
||||||
|
okButton.setEnabled(false); // disable until the download service connection is done
|
||||||
|
|
||||||
|
toolbar.setOnMenuItemClickListener(item -> {
|
||||||
|
if (item.getItemId() == R.id.okay) {
|
||||||
|
prepareSelectedDownload();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDismiss(@NonNull final DialogInterface dialog) {
|
||||||
|
super.onDismiss(dialog);
|
||||||
|
if (onDismissListener != null) {
|
||||||
|
onDismissListener.onDismiss(dialog);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDestroy() {
|
||||||
|
super.onDestroy();
|
||||||
|
disposables.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDestroyView() {
|
||||||
|
dialogBinding = null;
|
||||||
|
super.onDestroyView();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onSaveInstanceState(@NonNull final Bundle outState) {
|
||||||
|
super.onSaveInstanceState(outState);
|
||||||
|
Icepick.saveInstanceState(this, outState);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*//////////////////////////////////////////////////////////////////////////
|
||||||
|
// Video, audio and subtitle spinners
|
||||||
|
//////////////////////////////////////////////////////////////////////////*/
|
||||||
|
|
||||||
private void fetchStreamsSize() {
|
private void fetchStreamsSize() {
|
||||||
disposables.clear();
|
disposables.clear();
|
||||||
disposables.add(StreamSizeWrapper.fetchSizeForWrapper(wrappedVideoStreams)
|
disposables.add(StreamSizeWrapper.fetchSizeForWrapper(wrappedVideoStreams)
|
||||||
|
@ -356,30 +428,39 @@ public class DownloadDialog extends DialogFragment
|
||||||
currentInfo.getServiceId()))));
|
currentInfo.getServiceId()))));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
private void setupAudioSpinner() {
|
||||||
public void onDestroy() {
|
if (getContext() == null) {
|
||||||
super.onDestroy();
|
return;
|
||||||
disposables.clear();
|
}
|
||||||
|
|
||||||
|
dialogBinding.qualitySpinner.setAdapter(audioStreamsAdapter);
|
||||||
|
dialogBinding.qualitySpinner.setSelection(selectedAudioIndex);
|
||||||
|
setRadioButtonsState(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
private void setupVideoSpinner() {
|
||||||
public void onDestroyView() {
|
if (getContext() == null) {
|
||||||
dialogBinding = null;
|
return;
|
||||||
super.onDestroyView();
|
}
|
||||||
|
|
||||||
|
dialogBinding.qualitySpinner.setAdapter(videoStreamsAdapter);
|
||||||
|
dialogBinding.qualitySpinner.setSelection(selectedVideoIndex);
|
||||||
|
setRadioButtonsState(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void setupSubtitleSpinner() {
|
||||||
|
if (getContext() == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
dialogBinding.qualitySpinner.setAdapter(subtitleStreamsAdapter);
|
||||||
|
dialogBinding.qualitySpinner.setSelection(selectedSubtitleIndex);
|
||||||
|
setRadioButtonsState(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*//////////////////////////////////////////////////////////////////////////
|
/*//////////////////////////////////////////////////////////////////////////
|
||||||
// Radio group Video&Audio options - Listener
|
// Activity results
|
||||||
//////////////////////////////////////////////////////////////////////////*/
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onSaveInstanceState(@NonNull final Bundle outState) {
|
|
||||||
super.onSaveInstanceState(outState);
|
|
||||||
Icepick.saveInstanceState(this, outState);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*//////////////////////////////////////////////////////////////////////////
|
|
||||||
// Streams Spinner Listener
|
|
||||||
//////////////////////////////////////////////////////////////////////////*/
|
//////////////////////////////////////////////////////////////////////////*/
|
||||||
|
|
||||||
private void requestDownloadPickAudioFolderResult(final ActivityResult result) {
|
private void requestDownloadPickAudioFolderResult(final ActivityResult result) {
|
||||||
|
@ -454,66 +535,11 @@ public class DownloadDialog extends DialogFragment
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initToolbar(final Toolbar toolbar) {
|
|
||||||
if (DEBUG) {
|
|
||||||
Log.d(TAG, "initToolbar() called with: toolbar = [" + toolbar + "]");
|
|
||||||
}
|
|
||||||
|
|
||||||
toolbar.setTitle(R.string.download_dialog_title);
|
|
||||||
toolbar.setNavigationIcon(R.drawable.ic_arrow_back);
|
|
||||||
toolbar.inflateMenu(R.menu.dialog_url);
|
|
||||||
toolbar.setNavigationOnClickListener(v -> requireDialog().dismiss());
|
|
||||||
toolbar.setNavigationContentDescription(R.string.cancel);
|
|
||||||
|
|
||||||
okButton = toolbar.findViewById(R.id.okay);
|
|
||||||
okButton.setEnabled(false); // disable until the download service connection is done
|
|
||||||
|
|
||||||
toolbar.setOnMenuItemClickListener(item -> {
|
|
||||||
if (item.getItemId() == R.id.okay) {
|
|
||||||
prepareSelectedDownload();
|
|
||||||
if (getActivity() instanceof RouterActivity) {
|
|
||||||
getActivity().finish();
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/*//////////////////////////////////////////////////////////////////////////
|
/*//////////////////////////////////////////////////////////////////////////
|
||||||
// Utils
|
// Listeners
|
||||||
//////////////////////////////////////////////////////////////////////////*/
|
//////////////////////////////////////////////////////////////////////////*/
|
||||||
|
|
||||||
private void setupAudioSpinner() {
|
|
||||||
if (getContext() == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
dialogBinding.qualitySpinner.setAdapter(audioStreamsAdapter);
|
|
||||||
dialogBinding.qualitySpinner.setSelection(selectedAudioIndex);
|
|
||||||
setRadioButtonsState(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void setupVideoSpinner() {
|
|
||||||
if (getContext() == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
dialogBinding.qualitySpinner.setAdapter(videoStreamsAdapter);
|
|
||||||
dialogBinding.qualitySpinner.setSelection(selectedVideoIndex);
|
|
||||||
setRadioButtonsState(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void setupSubtitleSpinner() {
|
|
||||||
if (getContext() == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
dialogBinding.qualitySpinner.setAdapter(subtitleStreamsAdapter);
|
|
||||||
dialogBinding.qualitySpinner.setSelection(selectedSubtitleIndex);
|
|
||||||
setRadioButtonsState(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCheckedChanged(final RadioGroup group, @IdRes final int checkedId) {
|
public void onCheckedChanged(final RadioGroup group, @IdRes final int checkedId) {
|
||||||
if (DEBUG) {
|
if (DEBUG) {
|
||||||
|
@ -563,6 +589,11 @@ public class DownloadDialog extends DialogFragment
|
||||||
public void onNothingSelected(final AdapterView<?> parent) {
|
public void onNothingSelected(final AdapterView<?> parent) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*//////////////////////////////////////////////////////////////////////////
|
||||||
|
// Download
|
||||||
|
//////////////////////////////////////////////////////////////////////////*/
|
||||||
|
|
||||||
protected void setupDownloadOptions() {
|
protected void setupDownloadOptions() {
|
||||||
setRadioButtonsState(false);
|
setRadioButtonsState(false);
|
||||||
|
|
||||||
|
@ -575,7 +606,7 @@ public class DownloadDialog extends DialogFragment
|
||||||
dialogBinding.subtitleButton.setVisibility(isSubtitleStreamsAvailable
|
dialogBinding.subtitleButton.setVisibility(isSubtitleStreamsAvailable
|
||||||
? View.VISIBLE : View.GONE);
|
? View.VISIBLE : View.GONE);
|
||||||
|
|
||||||
prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
|
prefs = PreferenceManager.getDefaultSharedPreferences(requireContext());
|
||||||
final String defaultMedia = prefs.getString(getString(R.string.last_used_download_type),
|
final String defaultMedia = prefs.getString(getString(R.string.last_used_download_type),
|
||||||
getString(R.string.last_download_type_video_key));
|
getString(R.string.last_download_type_video_key));
|
||||||
|
|
||||||
|
@ -603,7 +634,7 @@ public class DownloadDialog extends DialogFragment
|
||||||
} else {
|
} else {
|
||||||
Toast.makeText(getContext(), R.string.no_streams_available_download,
|
Toast.makeText(getContext(), R.string.no_streams_available_download,
|
||||||
Toast.LENGTH_SHORT).show();
|
Toast.LENGTH_SHORT).show();
|
||||||
getDialog().dismiss();
|
dismiss();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue