Fix onActivityResult deprecation in DownloadSettingsFragment

This commit is contained in:
TacoTheDank 2021-06-20 14:11:00 -04:00
parent 72d305b283
commit e5a1438673

View file

@ -9,6 +9,9 @@ import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log; import android.util.Log;
import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.StringRes; import androidx.annotation.StringRes;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
@ -18,6 +21,7 @@ import androidx.preference.SwitchPreferenceCompat;
import com.nononsenseapps.filepicker.Utils; import com.nononsenseapps.filepicker.Utils;
import org.schabi.newpipe.R; import org.schabi.newpipe.R;
import org.schabi.newpipe.streams.io.StoredDirectoryHelper;
import org.schabi.newpipe.util.FilePickerActivityHelper; import org.schabi.newpipe.util.FilePickerActivityHelper;
import java.io.File; import java.io.File;
@ -27,14 +31,10 @@ import java.net.URI;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import org.schabi.newpipe.streams.io.StoredDirectoryHelper;
import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage; import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage;
public class DownloadSettingsFragment extends BasePreferenceFragment { public class DownloadSettingsFragment extends BasePreferenceFragment {
public static final boolean IGNORE_RELEASE_ON_OLD_PATH = true; public static final boolean IGNORE_RELEASE_ON_OLD_PATH = true;
private static final int REQUEST_DOWNLOAD_VIDEO_PATH = 0x1235;
private static final int REQUEST_DOWNLOAD_AUDIO_PATH = 0x1236;
private String downloadPathVideoPreference; private String downloadPathVideoPreference;
private String downloadPathAudioPreference; private String downloadPathAudioPreference;
private String storageUseSafPreference; private String storageUseSafPreference;
@ -44,6 +44,12 @@ public class DownloadSettingsFragment extends BasePreferenceFragment {
private Preference prefStorageAsk; private Preference prefStorageAsk;
private Context ctx; private Context ctx;
private final ActivityResultLauncher<Intent> requestDownloadVideoPathLauncher =
registerForActivityResult(
new StartActivityForResult(), this::requestDownloadVideoPathResult);
private final ActivityResultLauncher<Intent> requestDownloadAudioPathLauncher =
registerForActivityResult(
new StartActivityForResult(), this::requestDownloadAudioPathResult);
@Override @Override
public void onCreatePreferences(final Bundle savedInstanceState, final String rootKey) { public void onCreatePreferences(final Bundle savedInstanceState, final String rootKey) {
@ -185,7 +191,6 @@ public class DownloadSettingsFragment extends BasePreferenceFragment {
} }
final String key = preference.getKey(); final String key = preference.getKey();
final int request;
if (key.equals(storageUseSafPreference)) { if (key.equals(storageUseSafPreference)) {
if (!NewPipeSettings.useStorageAccessFramework(ctx)) { if (!NewPipeSettings.useStorageAccessFramework(ctx)) {
@ -198,43 +203,39 @@ public class DownloadSettingsFragment extends BasePreferenceFragment {
updatePreferencesSummary(); updatePreferencesSummary();
return true; return true;
} else if (key.equals(downloadPathVideoPreference)) { } else if (key.equals(downloadPathVideoPreference)) {
request = REQUEST_DOWNLOAD_VIDEO_PATH; launchDirectoryPicker(requestDownloadVideoPathLauncher);
} else if (key.equals(downloadPathAudioPreference)) { } else if (key.equals(downloadPathAudioPreference)) {
request = REQUEST_DOWNLOAD_AUDIO_PATH; launchDirectoryPicker(requestDownloadAudioPathLauncher);
} else { } else {
return super.onPreferenceTreeClick(preference); return super.onPreferenceTreeClick(preference);
} }
startActivityForResult(StoredDirectoryHelper.getPicker(ctx), request);
return true; return true;
} }
@Override private void launchDirectoryPicker(final ActivityResultLauncher<Intent> launcher) {
public void onActivityResult(final int requestCode, final int resultCode, final Intent data) { launcher.launch(StoredDirectoryHelper.getPicker(ctx));
}
private void requestDownloadVideoPathResult(final ActivityResult result) {
requestDownloadPathResult(result, downloadPathVideoPreference);
}
private void requestDownloadAudioPathResult(final ActivityResult result) {
requestDownloadPathResult(result, downloadPathAudioPreference);
}
private void requestDownloadPathResult(final ActivityResult result, final String key) {
assureCorrectAppLanguage(getContext()); assureCorrectAppLanguage(getContext());
super.onActivityResult(requestCode, resultCode, data);
if (DEBUG) {
Log.d(TAG, "onActivityResult() called with: "
+ "requestCode = [" + requestCode + "], "
+ "resultCode = [" + resultCode + "], data = [" + data + "]"
);
}
if (resultCode != Activity.RESULT_OK) { if (result.getResultCode() != Activity.RESULT_OK) {
return; return;
} }
final String key; Uri uri = null;
if (requestCode == REQUEST_DOWNLOAD_VIDEO_PATH) { if (result.getData() != null) {
key = downloadPathVideoPreference; uri = result.getData().getData();
} else if (requestCode == REQUEST_DOWNLOAD_AUDIO_PATH) {
key = downloadPathAudioPreference;
} else {
return;
} }
Uri uri = data.getData();
if (uri == null) { if (uri == null) {
showMessageDialog(R.string.general_error, R.string.invalid_directory); showMessageDialog(R.string.general_error, R.string.invalid_directory);
return; return;