Fix onActivityResult deprecation in DownloadDialog
This commit is contained in:
parent
a56f17cc3b
commit
7d69dfa62a
1 changed files with 77 additions and 57 deletions
|
@ -20,6 +20,9 @@ import android.widget.RadioGroup;
|
||||||
import android.widget.SeekBar;
|
import android.widget.SeekBar;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
|
import androidx.activity.result.ActivityResult;
|
||||||
|
import androidx.activity.result.ActivityResultLauncher;
|
||||||
|
import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult;
|
||||||
import androidx.annotation.IdRes;
|
import androidx.annotation.IdRes;
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
|
@ -82,9 +85,6 @@ public class DownloadDialog extends DialogFragment
|
||||||
implements RadioGroup.OnCheckedChangeListener, AdapterView.OnItemSelectedListener {
|
implements RadioGroup.OnCheckedChangeListener, AdapterView.OnItemSelectedListener {
|
||||||
private static final String TAG = "DialogFragment";
|
private static final String TAG = "DialogFragment";
|
||||||
private static final boolean DEBUG = MainActivity.DEBUG;
|
private static final boolean DEBUG = MainActivity.DEBUG;
|
||||||
private static final int REQUEST_DOWNLOAD_SAVE_AS = 0x1230;
|
|
||||||
private static final int REQUEST_DOWNLOAD_PICK_VIDEO_FOLDER = 0x789E;
|
|
||||||
private static final int REQUEST_DOWNLOAD_PICK_AUDIO_FOLDER = 0x789F;
|
|
||||||
|
|
||||||
@State
|
@State
|
||||||
StreamInfo currentInfo;
|
StreamInfo currentInfo;
|
||||||
|
@ -122,6 +122,16 @@ public class DownloadDialog extends DialogFragment
|
||||||
private String filenameTmp;
|
private String filenameTmp;
|
||||||
private String mimeTmp;
|
private String mimeTmp;
|
||||||
|
|
||||||
|
private final ActivityResultLauncher<Intent> requestDownloadSaveAsLauncher =
|
||||||
|
registerForActivityResult(
|
||||||
|
new StartActivityForResult(), this::requestDownloadSaveAsResult);
|
||||||
|
private final ActivityResultLauncher<Intent> requestDownloadPickAudioFolderLauncher =
|
||||||
|
registerForActivityResult(
|
||||||
|
new StartActivityForResult(), this::requestDownloadPickAudioFolderResult);
|
||||||
|
private final ActivityResultLauncher<Intent> requestDownloadPickVideoFolderLauncher =
|
||||||
|
registerForActivityResult(
|
||||||
|
new StartActivityForResult(), this::requestDownloadPickVideoFolderResult);
|
||||||
|
|
||||||
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);
|
||||||
|
@ -372,67 +382,75 @@ public class DownloadDialog extends DialogFragment
|
||||||
// Streams Spinner Listener
|
// Streams Spinner Listener
|
||||||
//////////////////////////////////////////////////////////////////////////*/
|
//////////////////////////////////////////////////////////////////////////*/
|
||||||
|
|
||||||
@Override
|
private void requestDownloadPickAudioFolderResult(final ActivityResult result) {
|
||||||
public void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
|
requestDownloadPickFolderResult(
|
||||||
super.onActivityResult(requestCode, resultCode, data);
|
result, getString(R.string.download_path_audio_key), DownloadManager.TAG_AUDIO);
|
||||||
|
}
|
||||||
|
|
||||||
if (resultCode != Activity.RESULT_OK) {
|
private void requestDownloadPickVideoFolderResult(final ActivityResult result) {
|
||||||
|
requestDownloadPickFolderResult(
|
||||||
|
result, getString(R.string.download_path_video_key), DownloadManager.TAG_VIDEO);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void requestDownloadSaveAsResult(final ActivityResult result) {
|
||||||
|
if (result.getResultCode() != Activity.RESULT_OK) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data.getData() == null) {
|
if (result.getData() == null || result.getData().getData() == null) {
|
||||||
showFailedDialog(R.string.general_error);
|
showFailedDialog(R.string.general_error);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (requestCode == REQUEST_DOWNLOAD_SAVE_AS) {
|
if (FilePickerActivityHelper.isOwnFileUri(context, result.getData().getData())) {
|
||||||
if (FilePickerActivityHelper.isOwnFileUri(context, data.getData())) {
|
final File file = Utils.getFileForUri(result.getData().getData());
|
||||||
final File file = Utils.getFileForUri(data.getData());
|
checkSelectedDownload(null, Uri.fromFile(file), file.getName(),
|
||||||
checkSelectedDownload(null, Uri.fromFile(file), file.getName(),
|
StoredFileHelper.DEFAULT_MIME);
|
||||||
StoredFileHelper.DEFAULT_MIME);
|
return;
|
||||||
return;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
final DocumentFile docFile = DocumentFile.fromSingleUri(context, data.getData());
|
final DocumentFile docFile
|
||||||
if (docFile == null) {
|
= DocumentFile.fromSingleUri(context, result.getData().getData());
|
||||||
showFailedDialog(R.string.general_error);
|
if (docFile == null) {
|
||||||
return;
|
showFailedDialog(R.string.general_error);
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// check if the selected file was previously used
|
// check if the selected file was previously used
|
||||||
checkSelectedDownload(null, data.getData(), docFile.getName(),
|
checkSelectedDownload(null, result.getData().getData(), docFile.getName(),
|
||||||
docFile.getType());
|
docFile.getType());
|
||||||
} else if (requestCode == REQUEST_DOWNLOAD_PICK_AUDIO_FOLDER
|
}
|
||||||
|| requestCode == REQUEST_DOWNLOAD_PICK_VIDEO_FOLDER) {
|
|
||||||
Uri uri = data.getData();
|
|
||||||
if (FilePickerActivityHelper.isOwnFileUri(context, uri)) {
|
|
||||||
uri = Uri.fromFile(Utils.getFileForUri(uri));
|
|
||||||
} else {
|
|
||||||
context.grantUriPermission(context.getPackageName(), uri,
|
|
||||||
StoredDirectoryHelper.PERMISSION_FLAGS);
|
|
||||||
}
|
|
||||||
|
|
||||||
final String key;
|
private void requestDownloadPickFolderResult(final ActivityResult result,
|
||||||
final String tag;
|
final String key,
|
||||||
if (requestCode == REQUEST_DOWNLOAD_PICK_AUDIO_FOLDER) {
|
final String tag) {
|
||||||
key = getString(R.string.download_path_audio_key);
|
if (result.getResultCode() != Activity.RESULT_OK) {
|
||||||
tag = DownloadManager.TAG_AUDIO;
|
return;
|
||||||
} else {
|
}
|
||||||
key = getString(R.string.download_path_video_key);
|
|
||||||
tag = DownloadManager.TAG_VIDEO;
|
|
||||||
}
|
|
||||||
|
|
||||||
PreferenceManager.getDefaultSharedPreferences(context).edit()
|
if (result.getData() == null || result.getData().getData() == null) {
|
||||||
.putString(key, uri.toString()).apply();
|
showFailedDialog(R.string.general_error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
Uri uri = result.getData().getData();
|
||||||
final StoredDirectoryHelper mainStorage
|
if (FilePickerActivityHelper.isOwnFileUri(context, uri)) {
|
||||||
= new StoredDirectoryHelper(context, uri, tag);
|
uri = Uri.fromFile(Utils.getFileForUri(uri));
|
||||||
checkSelectedDownload(mainStorage, mainStorage.findFile(filenameTmp),
|
} else {
|
||||||
filenameTmp, mimeTmp);
|
context.grantUriPermission(context.getPackageName(), uri,
|
||||||
} catch (final IOException e) {
|
StoredDirectoryHelper.PERMISSION_FLAGS);
|
||||||
showFailedDialog(R.string.general_error);
|
}
|
||||||
}
|
|
||||||
|
PreferenceManager.getDefaultSharedPreferences(context).edit()
|
||||||
|
.putString(key, uri.toString()).apply();
|
||||||
|
|
||||||
|
try {
|
||||||
|
final StoredDirectoryHelper mainStorage
|
||||||
|
= new StoredDirectoryHelper(context, uri, tag);
|
||||||
|
checkSelectedDownload(mainStorage, mainStorage.findFile(filenameTmp),
|
||||||
|
filenameTmp, mimeTmp);
|
||||||
|
} catch (final IOException e) {
|
||||||
|
showFailedDialog(R.string.general_error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -637,6 +655,10 @@ public class DownloadDialog extends DialogFragment
|
||||||
.show();
|
.show();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void launchDirectoryPicker(final ActivityResultLauncher<Intent> launcher) {
|
||||||
|
launcher.launch(StoredDirectoryHelper.getPicker(context));
|
||||||
|
}
|
||||||
|
|
||||||
private void prepareSelectedDownload() {
|
private void prepareSelectedDownload() {
|
||||||
final StoredDirectoryHelper mainStorage;
|
final StoredDirectoryHelper mainStorage;
|
||||||
final MediaFormat format;
|
final MediaFormat format;
|
||||||
|
@ -691,11 +713,9 @@ public class DownloadDialog extends DialogFragment
|
||||||
Toast.LENGTH_LONG).show();
|
Toast.LENGTH_LONG).show();
|
||||||
|
|
||||||
if (dialogBinding.videoAudioGroup.getCheckedRadioButtonId() == R.id.audio_button) {
|
if (dialogBinding.videoAudioGroup.getCheckedRadioButtonId() == R.id.audio_button) {
|
||||||
startActivityForResult(StoredDirectoryHelper.getPicker(context),
|
launchDirectoryPicker(requestDownloadPickAudioFolderLauncher);
|
||||||
REQUEST_DOWNLOAD_PICK_AUDIO_FOLDER);
|
|
||||||
} else {
|
} else {
|
||||||
startActivityForResult(StoredDirectoryHelper.getPicker(context),
|
launchDirectoryPicker(requestDownloadPickVideoFolderLauncher);
|
||||||
REQUEST_DOWNLOAD_PICK_VIDEO_FOLDER);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return;
|
return;
|
||||||
|
@ -715,8 +735,8 @@ public class DownloadDialog extends DialogFragment
|
||||||
initialPath = Uri.parse(initialSavePath.getAbsolutePath());
|
initialPath = Uri.parse(initialSavePath.getAbsolutePath());
|
||||||
}
|
}
|
||||||
|
|
||||||
startActivityForResult(StoredFileHelper.getNewPicker(context,
|
requestDownloadSaveAsLauncher.launch(StoredFileHelper.getNewPicker(context,
|
||||||
filenameTmp, mimeTmp, initialPath), REQUEST_DOWNLOAD_SAVE_AS);
|
filenameTmp, mimeTmp, initialPath));
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue