Skip to content

Commit 7d69dfa

Browse files
TacoTheDankStypox
authored andcommitted
Fix onActivityResult deprecation in DownloadDialog
1 parent a56f17c commit 7d69dfa

1 file changed

Lines changed: 77 additions & 57 deletions

File tree

app/src/main/java/org/schabi/newpipe/download/DownloadDialog.java

Lines changed: 77 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
import android.widget.SeekBar;
2121
import android.widget.Toast;
2222

23+
import androidx.activity.result.ActivityResult;
24+
import androidx.activity.result.ActivityResultLauncher;
25+
import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult;
2326
import androidx.annotation.IdRes;
2427
import androidx.annotation.NonNull;
2528
import androidx.annotation.Nullable;
@@ -82,9 +85,6 @@ public class DownloadDialog extends DialogFragment
8285
implements RadioGroup.OnCheckedChangeListener, AdapterView.OnItemSelectedListener {
8386
private static final String TAG = "DialogFragment";
8487
private static final boolean DEBUG = MainActivity.DEBUG;
85-
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;
8888

8989
@State
9090
StreamInfo currentInfo;
@@ -122,6 +122,16 @@ public class DownloadDialog extends DialogFragment
122122
private String filenameTmp;
123123
private String mimeTmp;
124124

125+
private final ActivityResultLauncher<Intent> requestDownloadSaveAsLauncher =
126+
registerForActivityResult(
127+
new StartActivityForResult(), this::requestDownloadSaveAsResult);
128+
private final ActivityResultLauncher<Intent> requestDownloadPickAudioFolderLauncher =
129+
registerForActivityResult(
130+
new StartActivityForResult(), this::requestDownloadPickAudioFolderResult);
131+
private final ActivityResultLauncher<Intent> requestDownloadPickVideoFolderLauncher =
132+
registerForActivityResult(
133+
new StartActivityForResult(), this::requestDownloadPickVideoFolderResult);
134+
125135
public static DownloadDialog newInstance(final StreamInfo info) {
126136
final DownloadDialog dialog = new DownloadDialog();
127137
dialog.setInfo(info);
@@ -372,67 +382,75 @@ public void onSaveInstanceState(@NonNull final Bundle outState) {
372382
// Streams Spinner Listener
373383
//////////////////////////////////////////////////////////////////////////*/
374384

375-
@Override
376-
public void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
377-
super.onActivityResult(requestCode, resultCode, data);
385+
private void requestDownloadPickAudioFolderResult(final ActivityResult result) {
386+
requestDownloadPickFolderResult(
387+
result, getString(R.string.download_path_audio_key), DownloadManager.TAG_AUDIO);
388+
}
389+
390+
private void requestDownloadPickVideoFolderResult(final ActivityResult result) {
391+
requestDownloadPickFolderResult(
392+
result, getString(R.string.download_path_video_key), DownloadManager.TAG_VIDEO);
393+
}
378394

379-
if (resultCode != Activity.RESULT_OK) {
395+
private void requestDownloadSaveAsResult(final ActivityResult result) {
396+
if (result.getResultCode() != Activity.RESULT_OK) {
380397
return;
381398
}
382399

383-
if (data.getData() == null) {
400+
if (result.getData() == null || result.getData().getData() == null) {
384401
showFailedDialog(R.string.general_error);
385402
return;
386403
}
387404

388-
if (requestCode == REQUEST_DOWNLOAD_SAVE_AS) {
389-
if (FilePickerActivityHelper.isOwnFileUri(context, data.getData())) {
390-
final File file = Utils.getFileForUri(data.getData());
391-
checkSelectedDownload(null, Uri.fromFile(file), file.getName(),
392-
StoredFileHelper.DEFAULT_MIME);
393-
return;
394-
}
405+
if (FilePickerActivityHelper.isOwnFileUri(context, result.getData().getData())) {
406+
final File file = Utils.getFileForUri(result.getData().getData());
407+
checkSelectedDownload(null, Uri.fromFile(file), file.getName(),
408+
StoredFileHelper.DEFAULT_MIME);
409+
return;
410+
}
395411

396-
final DocumentFile docFile = DocumentFile.fromSingleUri(context, data.getData());
397-
if (docFile == null) {
398-
showFailedDialog(R.string.general_error);
399-
return;
400-
}
412+
final DocumentFile docFile
413+
= DocumentFile.fromSingleUri(context, result.getData().getData());
414+
if (docFile == null) {
415+
showFailedDialog(R.string.general_error);
416+
return;
417+
}
401418

402-
// check if the selected file was previously used
403-
checkSelectedDownload(null, data.getData(), docFile.getName(),
404-
docFile.getType());
405-
} else if (requestCode == REQUEST_DOWNLOAD_PICK_AUDIO_FOLDER
406-
|| requestCode == REQUEST_DOWNLOAD_PICK_VIDEO_FOLDER) {
407-
Uri uri = data.getData();
408-
if (FilePickerActivityHelper.isOwnFileUri(context, uri)) {
409-
uri = Uri.fromFile(Utils.getFileForUri(uri));
410-
} else {
411-
context.grantUriPermission(context.getPackageName(), uri,
412-
StoredDirectoryHelper.PERMISSION_FLAGS);
413-
}
419+
// check if the selected file was previously used
420+
checkSelectedDownload(null, result.getData().getData(), docFile.getName(),
421+
docFile.getType());
422+
}
414423

415-
final String key;
416-
final String tag;
417-
if (requestCode == REQUEST_DOWNLOAD_PICK_AUDIO_FOLDER) {
418-
key = getString(R.string.download_path_audio_key);
419-
tag = DownloadManager.TAG_AUDIO;
420-
} else {
421-
key = getString(R.string.download_path_video_key);
422-
tag = DownloadManager.TAG_VIDEO;
423-
}
424+
private void requestDownloadPickFolderResult(final ActivityResult result,
425+
final String key,
426+
final String tag) {
427+
if (result.getResultCode() != Activity.RESULT_OK) {
428+
return;
429+
}
424430

425-
PreferenceManager.getDefaultSharedPreferences(context).edit()
426-
.putString(key, uri.toString()).apply();
431+
if (result.getData() == null || result.getData().getData() == null) {
432+
showFailedDialog(R.string.general_error);
433+
return;
434+
}
427435

428-
try {
429-
final StoredDirectoryHelper mainStorage
430-
= new StoredDirectoryHelper(context, uri, tag);
431-
checkSelectedDownload(mainStorage, mainStorage.findFile(filenameTmp),
432-
filenameTmp, mimeTmp);
433-
} catch (final IOException e) {
434-
showFailedDialog(R.string.general_error);
435-
}
436+
Uri uri = result.getData().getData();
437+
if (FilePickerActivityHelper.isOwnFileUri(context, uri)) {
438+
uri = Uri.fromFile(Utils.getFileForUri(uri));
439+
} else {
440+
context.grantUriPermission(context.getPackageName(), uri,
441+
StoredDirectoryHelper.PERMISSION_FLAGS);
442+
}
443+
444+
PreferenceManager.getDefaultSharedPreferences(context).edit()
445+
.putString(key, uri.toString()).apply();
446+
447+
try {
448+
final StoredDirectoryHelper mainStorage
449+
= new StoredDirectoryHelper(context, uri, tag);
450+
checkSelectedDownload(mainStorage, mainStorage.findFile(filenameTmp),
451+
filenameTmp, mimeTmp);
452+
} catch (final IOException e) {
453+
showFailedDialog(R.string.general_error);
436454
}
437455
}
438456

@@ -637,6 +655,10 @@ private void showFailedDialog(@StringRes final int msg) {
637655
.show();
638656
}
639657

658+
private void launchDirectoryPicker(final ActivityResultLauncher<Intent> launcher) {
659+
launcher.launch(StoredDirectoryHelper.getPicker(context));
660+
}
661+
640662
private void prepareSelectedDownload() {
641663
final StoredDirectoryHelper mainStorage;
642664
final MediaFormat format;
@@ -691,11 +713,9 @@ private void prepareSelectedDownload() {
691713
Toast.LENGTH_LONG).show();
692714

693715
if (dialogBinding.videoAudioGroup.getCheckedRadioButtonId() == R.id.audio_button) {
694-
startActivityForResult(StoredDirectoryHelper.getPicker(context),
695-
REQUEST_DOWNLOAD_PICK_AUDIO_FOLDER);
716+
launchDirectoryPicker(requestDownloadPickAudioFolderLauncher);
696717
} else {
697-
startActivityForResult(StoredDirectoryHelper.getPicker(context),
698-
REQUEST_DOWNLOAD_PICK_VIDEO_FOLDER);
718+
launchDirectoryPicker(requestDownloadPickVideoFolderLauncher);
699719
}
700720

701721
return;
@@ -715,8 +735,8 @@ private void prepareSelectedDownload() {
715735
initialPath = Uri.parse(initialSavePath.getAbsolutePath());
716736
}
717737

718-
startActivityForResult(StoredFileHelper.getNewPicker(context,
719-
filenameTmp, mimeTmp, initialPath), REQUEST_DOWNLOAD_SAVE_AS);
738+
requestDownloadSaveAsLauncher.launch(StoredFileHelper.getNewPicker(context,
739+
filenameTmp, mimeTmp, initialPath));
720740

721741
return;
722742
}

0 commit comments

Comments
 (0)