diff --git a/app/build.gradle b/app/build.gradle index 4b6890eac..5c6bd703b 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -201,7 +201,7 @@ dependencies { implementation 'androidx.constraintlayout:constraintlayout:2.0.4' implementation 'androidx.core:core-ktx:1.3.2' implementation 'androidx.documentfile:documentfile:1.0.1' - implementation 'androidx.fragment:fragment-ktx:1.3.4' + implementation 'androidx.fragment:fragment-ktx:1.3.5' implementation "androidx.lifecycle:lifecycle-livedata:${androidxLifecycleVersion}" implementation "androidx.lifecycle:lifecycle-viewmodel:${androidxLifecycleVersion}" implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0' diff --git a/app/src/main/java/org/schabi/newpipe/download/DownloadDialog.java b/app/src/main/java/org/schabi/newpipe/download/DownloadDialog.java index c055f8f23..af8c1b258 100644 --- a/app/src/main/java/org/schabi/newpipe/download/DownloadDialog.java +++ b/app/src/main/java/org/schabi/newpipe/download/DownloadDialog.java @@ -20,6 +20,9 @@ import android.widget.RadioGroup; import android.widget.SeekBar; 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.NonNull; import androidx.annotation.Nullable; @@ -82,9 +85,6 @@ public class DownloadDialog extends DialogFragment implements RadioGroup.OnCheckedChangeListener, AdapterView.OnItemSelectedListener { private static final String TAG = "DialogFragment"; 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 StreamInfo currentInfo; @@ -122,6 +122,16 @@ public class DownloadDialog extends DialogFragment private String filenameTmp; private String mimeTmp; + private final ActivityResultLauncher requestDownloadSaveAsLauncher = + registerForActivityResult( + new StartActivityForResult(), this::requestDownloadSaveAsResult); + private final ActivityResultLauncher requestDownloadPickAudioFolderLauncher = + registerForActivityResult( + new StartActivityForResult(), this::requestDownloadPickAudioFolderResult); + private final ActivityResultLauncher requestDownloadPickVideoFolderLauncher = + registerForActivityResult( + new StartActivityForResult(), this::requestDownloadPickVideoFolderResult); + public static DownloadDialog newInstance(final StreamInfo info) { final DownloadDialog dialog = new DownloadDialog(); dialog.setInfo(info); @@ -372,67 +382,75 @@ public class DownloadDialog extends DialogFragment // Streams Spinner Listener //////////////////////////////////////////////////////////////////////////*/ - @Override - public void onActivityResult(final int requestCode, final int resultCode, final Intent data) { - super.onActivityResult(requestCode, resultCode, data); + private void requestDownloadPickAudioFolderResult(final ActivityResult result) { + requestDownloadPickFolderResult( + 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; } - if (data.getData() == null) { + if (result.getData() == null || result.getData().getData() == null) { showFailedDialog(R.string.general_error); return; } - if (requestCode == REQUEST_DOWNLOAD_SAVE_AS) { - if (FilePickerActivityHelper.isOwnFileUri(context, data.getData())) { - final File file = Utils.getFileForUri(data.getData()); - checkSelectedDownload(null, Uri.fromFile(file), file.getName(), - StoredFileHelper.DEFAULT_MIME); - return; - } + if (FilePickerActivityHelper.isOwnFileUri(context, result.getData().getData())) { + final File file = Utils.getFileForUri(result.getData().getData()); + checkSelectedDownload(null, Uri.fromFile(file), file.getName(), + StoredFileHelper.DEFAULT_MIME); + return; + } - final DocumentFile docFile = DocumentFile.fromSingleUri(context, data.getData()); - if (docFile == null) { - showFailedDialog(R.string.general_error); - return; - } + final DocumentFile docFile + = DocumentFile.fromSingleUri(context, result.getData().getData()); + if (docFile == null) { + showFailedDialog(R.string.general_error); + return; + } - // check if the selected file was previously used - checkSelectedDownload(null, data.getData(), docFile.getName(), - 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); - } + // check if the selected file was previously used + checkSelectedDownload(null, result.getData().getData(), docFile.getName(), + docFile.getType()); + } - final String key; - final String tag; - if (requestCode == REQUEST_DOWNLOAD_PICK_AUDIO_FOLDER) { - key = getString(R.string.download_path_audio_key); - tag = DownloadManager.TAG_AUDIO; - } else { - key = getString(R.string.download_path_video_key); - tag = DownloadManager.TAG_VIDEO; - } + private void requestDownloadPickFolderResult(final ActivityResult result, + final String key, + final String tag) { + if (result.getResultCode() != Activity.RESULT_OK) { + return; + } - PreferenceManager.getDefaultSharedPreferences(context).edit() - .putString(key, uri.toString()).apply(); + if (result.getData() == null || result.getData().getData() == null) { + showFailedDialog(R.string.general_error); + return; + } - 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); - } + Uri uri = result.getData().getData(); + if (FilePickerActivityHelper.isOwnFileUri(context, uri)) { + uri = Uri.fromFile(Utils.getFileForUri(uri)); + } else { + context.grantUriPermission(context.getPackageName(), uri, + StoredDirectoryHelper.PERMISSION_FLAGS); + } + + 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(); } + private void launchDirectoryPicker(final ActivityResultLauncher launcher) { + launcher.launch(StoredDirectoryHelper.getPicker(context)); + } + private void prepareSelectedDownload() { final StoredDirectoryHelper mainStorage; final MediaFormat format; @@ -691,11 +713,9 @@ public class DownloadDialog extends DialogFragment Toast.LENGTH_LONG).show(); if (dialogBinding.videoAudioGroup.getCheckedRadioButtonId() == R.id.audio_button) { - startActivityForResult(StoredDirectoryHelper.getPicker(context), - REQUEST_DOWNLOAD_PICK_AUDIO_FOLDER); + launchDirectoryPicker(requestDownloadPickAudioFolderLauncher); } else { - startActivityForResult(StoredDirectoryHelper.getPicker(context), - REQUEST_DOWNLOAD_PICK_VIDEO_FOLDER); + launchDirectoryPicker(requestDownloadPickVideoFolderLauncher); } return; @@ -715,8 +735,8 @@ public class DownloadDialog extends DialogFragment initialPath = Uri.parse(initialSavePath.getAbsolutePath()); } - startActivityForResult(StoredFileHelper.getNewPicker(context, - filenameTmp, mimeTmp, initialPath), REQUEST_DOWNLOAD_SAVE_AS); + requestDownloadSaveAsLauncher.launch(StoredFileHelper.getNewPicker(context, + filenameTmp, mimeTmp, initialPath)); return; } diff --git a/app/src/main/java/org/schabi/newpipe/settings/DownloadSettingsFragment.java b/app/src/main/java/org/schabi/newpipe/settings/DownloadSettingsFragment.java index a882acf3b..b4af0e43b 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/DownloadSettingsFragment.java +++ b/app/src/main/java/org/schabi/newpipe/settings/DownloadSettingsFragment.java @@ -9,6 +9,9 @@ import android.os.Build; import android.os.Bundle; 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.StringRes; import androidx.appcompat.app.AlertDialog; @@ -18,6 +21,7 @@ import androidx.preference.SwitchPreferenceCompat; import com.nononsenseapps.filepicker.Utils; import org.schabi.newpipe.R; +import org.schabi.newpipe.streams.io.StoredDirectoryHelper; import org.schabi.newpipe.util.FilePickerActivityHelper; import java.io.File; @@ -27,14 +31,10 @@ import java.net.URI; import java.net.URLDecoder; import java.nio.charset.StandardCharsets; -import org.schabi.newpipe.streams.io.StoredDirectoryHelper; - import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage; public class DownloadSettingsFragment extends BasePreferenceFragment { 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 downloadPathAudioPreference; private String storageUseSafPreference; @@ -44,6 +44,12 @@ public class DownloadSettingsFragment extends BasePreferenceFragment { private Preference prefStorageAsk; private Context ctx; + private final ActivityResultLauncher requestDownloadVideoPathLauncher = + registerForActivityResult( + new StartActivityForResult(), this::requestDownloadVideoPathResult); + private final ActivityResultLauncher requestDownloadAudioPathLauncher = + registerForActivityResult( + new StartActivityForResult(), this::requestDownloadAudioPathResult); @Override public void onCreatePreferences(final Bundle savedInstanceState, final String rootKey) { @@ -185,7 +191,6 @@ public class DownloadSettingsFragment extends BasePreferenceFragment { } final String key = preference.getKey(); - final int request; if (key.equals(storageUseSafPreference)) { if (!NewPipeSettings.useStorageAccessFramework(ctx)) { @@ -198,43 +203,39 @@ public class DownloadSettingsFragment extends BasePreferenceFragment { updatePreferencesSummary(); return true; } else if (key.equals(downloadPathVideoPreference)) { - request = REQUEST_DOWNLOAD_VIDEO_PATH; + launchDirectoryPicker(requestDownloadVideoPathLauncher); } else if (key.equals(downloadPathAudioPreference)) { - request = REQUEST_DOWNLOAD_AUDIO_PATH; + launchDirectoryPicker(requestDownloadAudioPathLauncher); } else { return super.onPreferenceTreeClick(preference); } - startActivityForResult(StoredDirectoryHelper.getPicker(ctx), request); - return true; } - @Override - public void onActivityResult(final int requestCode, final int resultCode, final Intent data) { + private void launchDirectoryPicker(final ActivityResultLauncher launcher) { + 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()); - 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; } - final String key; - if (requestCode == REQUEST_DOWNLOAD_VIDEO_PATH) { - key = downloadPathVideoPreference; - } else if (requestCode == REQUEST_DOWNLOAD_AUDIO_PATH) { - key = downloadPathAudioPreference; - } else { - return; + Uri uri = null; + if (result.getData() != null) { + uri = result.getData().getData(); } - - Uri uri = data.getData(); if (uri == null) { showMessageDialog(R.string.general_error, R.string.invalid_directory); return; diff --git a/app/src/main/java/us/shandian/giga/ui/fragment/MissionsFragment.java b/app/src/main/java/us/shandian/giga/ui/fragment/MissionsFragment.java index 793d147b5..2cca3239b 100644 --- a/app/src/main/java/us/shandian/giga/ui/fragment/MissionsFragment.java +++ b/app/src/main/java/us/shandian/giga/ui/fragment/MissionsFragment.java @@ -17,6 +17,9 @@ import android.view.View; import android.view.ViewGroup; import android.widget.Toast; +import androidx.activity.result.ActivityResult; +import androidx.activity.result.ActivityResultLauncher; +import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult; import androidx.annotation.NonNull; import androidx.appcompat.app.AlertDialog; import androidx.fragment.app.Fragment; @@ -44,7 +47,6 @@ import us.shandian.giga.ui.adapter.MissionAdapter; public class MissionsFragment extends Fragment { private static final int SPAN_SIZE = 2; - private static final int REQUEST_DOWNLOAD_SAVE_AS = 0x1230; private SharedPreferences mPrefs; private boolean mLinear; @@ -64,7 +66,8 @@ public class MissionsFragment extends Fragment { private boolean mForceUpdate; private DownloadMission unsafeMissionTarget = null; - + private final ActivityResultLauncher requestDownloadSaveAsLauncher = + registerForActivityResult(new StartActivityForResult(), this::requestDownloadSaveAsResult); private final ServiceConnection mConnection = new ServiceConnection() { @Override @@ -254,8 +257,9 @@ public class MissionsFragment extends Fragment { initialPath = Uri.parse(initialSavePath.getAbsolutePath()); } - startActivityForResult(StoredFileHelper.getNewPicker(mContext, mission.storage.getName(), - mission.storage.getType(), initialPath), REQUEST_DOWNLOAD_SAVE_AS); + requestDownloadSaveAsLauncher.launch( + StoredFileHelper.getNewPicker(mContext, mission.storage.getName(), + mission.storage.getType(), initialPath)); } @Override @@ -289,18 +293,17 @@ public class MissionsFragment extends Fragment { if (mBinder != null) mBinder.enableNotifications(true); } - @Override - public void onActivityResult(int requestCode, int resultCode, Intent data) { - super.onActivityResult(requestCode, resultCode, data); + private void requestDownloadSaveAsResult(final ActivityResult result) { + if (result.getResultCode() != Activity.RESULT_OK) { + return; + } - if (requestCode != REQUEST_DOWNLOAD_SAVE_AS || resultCode != Activity.RESULT_OK) return; - - if (unsafeMissionTarget == null || data.getData() == null) { + if (unsafeMissionTarget == null || result.getData() == null) { return; } try { - Uri fileUri = data.getData(); + Uri fileUri = result.getData().getData(); if (fileUri.getAuthority() != null && FilePickerActivityHelper.isOwnFileUri(mContext, fileUri)) { fileUri = Uri.fromFile(Utils.getFileForUri(fileUri)); }