99import android .os .Bundle ;
1010import android .util .Log ;
1111
12+ import androidx .activity .result .ActivityResult ;
13+ import androidx .activity .result .ActivityResultLauncher ;
14+ import androidx .activity .result .contract .ActivityResultContracts .StartActivityForResult ;
1215import androidx .annotation .NonNull ;
1316import androidx .annotation .StringRes ;
1417import androidx .appcompat .app .AlertDialog ;
1821import com .nononsenseapps .filepicker .Utils ;
1922
2023import org .schabi .newpipe .R ;
24+ import org .schabi .newpipe .streams .io .StoredDirectoryHelper ;
2125import org .schabi .newpipe .util .FilePickerActivityHelper ;
2226
2327import java .io .File ;
2731import java .net .URLDecoder ;
2832import java .nio .charset .StandardCharsets ;
2933
30- import org .schabi .newpipe .streams .io .StoredDirectoryHelper ;
31-
3234import static org .schabi .newpipe .util .Localization .assureCorrectAppLanguage ;
3335
3436public class DownloadSettingsFragment extends BasePreferenceFragment {
3537 public static final boolean IGNORE_RELEASE_ON_OLD_PATH = true ;
36- private static final int REQUEST_DOWNLOAD_VIDEO_PATH = 0x1235 ;
37- private static final int REQUEST_DOWNLOAD_AUDIO_PATH = 0x1236 ;
3838 private String downloadPathVideoPreference ;
3939 private String downloadPathAudioPreference ;
4040 private String storageUseSafPreference ;
@@ -44,6 +44,12 @@ public class DownloadSettingsFragment extends BasePreferenceFragment {
4444 private Preference prefStorageAsk ;
4545
4646 private Context ctx ;
47+ private final ActivityResultLauncher <Intent > requestDownloadVideoPathLauncher =
48+ registerForActivityResult (
49+ new StartActivityForResult (), this ::requestDownloadVideoPathResult );
50+ private final ActivityResultLauncher <Intent > requestDownloadAudioPathLauncher =
51+ registerForActivityResult (
52+ new StartActivityForResult (), this ::requestDownloadAudioPathResult );
4753
4854 @ Override
4955 public void onCreatePreferences (final Bundle savedInstanceState , final String rootKey ) {
@@ -185,7 +191,6 @@ public boolean onPreferenceTreeClick(final Preference preference) {
185191 }
186192
187193 final String key = preference .getKey ();
188- final int request ;
189194
190195 if (key .equals (storageUseSafPreference )) {
191196 if (!NewPipeSettings .useStorageAccessFramework (ctx )) {
@@ -198,43 +203,39 @@ public boolean onPreferenceTreeClick(final Preference preference) {
198203 updatePreferencesSummary ();
199204 return true ;
200205 } else if (key .equals (downloadPathVideoPreference )) {
201- request = REQUEST_DOWNLOAD_VIDEO_PATH ;
206+ launchDirectoryPicker ( requestDownloadVideoPathLauncher ) ;
202207 } else if (key .equals (downloadPathAudioPreference )) {
203- request = REQUEST_DOWNLOAD_AUDIO_PATH ;
208+ launchDirectoryPicker ( requestDownloadAudioPathLauncher ) ;
204209 } else {
205210 return super .onPreferenceTreeClick (preference );
206211 }
207212
208- startActivityForResult (StoredDirectoryHelper .getPicker (ctx ), request );
209-
210213 return true ;
211214 }
212215
213- @ Override
214- public void onActivityResult (final int requestCode , final int resultCode , final Intent data ) {
216+ private void launchDirectoryPicker (final ActivityResultLauncher <Intent > launcher ) {
217+ launcher .launch (StoredDirectoryHelper .getPicker (ctx ));
218+ }
219+
220+ private void requestDownloadVideoPathResult (final ActivityResult result ) {
221+ requestDownloadPathResult (result , downloadPathVideoPreference );
222+ }
223+
224+ private void requestDownloadAudioPathResult (final ActivityResult result ) {
225+ requestDownloadPathResult (result , downloadPathAudioPreference );
226+ }
227+
228+ private void requestDownloadPathResult (final ActivityResult result , final String key ) {
215229 assureCorrectAppLanguage (getContext ());
216- super .onActivityResult (requestCode , resultCode , data );
217- if (DEBUG ) {
218- Log .d (TAG , "onActivityResult() called with: "
219- + "requestCode = [" + requestCode + "], "
220- + "resultCode = [" + resultCode + "], data = [" + data + "]"
221- );
222- }
223230
224- if (resultCode != Activity .RESULT_OK ) {
231+ if (result . getResultCode () != Activity .RESULT_OK ) {
225232 return ;
226233 }
227234
228- final String key ;
229- if (requestCode == REQUEST_DOWNLOAD_VIDEO_PATH ) {
230- key = downloadPathVideoPreference ;
231- } else if (requestCode == REQUEST_DOWNLOAD_AUDIO_PATH ) {
232- key = downloadPathAudioPreference ;
233- } else {
234- return ;
235+ Uri uri = null ;
236+ if (result .getData () != null ) {
237+ uri = result .getData ().getData ();
235238 }
236-
237- Uri uri = data .getData ();
238239 if (uri == null ) {
239240 showMessageDialog (R .string .general_error , R .string .invalid_directory );
240241 return ;
0 commit comments