From cae160b5be42ebf605a2f1ff0bd6c71e47c7f028 Mon Sep 17 00:00:00 2001 From: TacoTheDank Date: Tue, 15 Jun 2021 22:09:07 -0400 Subject: [PATCH] Fix onActivityResult deprecation in SubscriptionsImportFragment --- .../SubscriptionsImportFragment.java | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/local/subscription/SubscriptionsImportFragment.java b/app/src/main/java/org/schabi/newpipe/local/subscription/SubscriptionsImportFragment.java index 27786bd44..4e667f2b9 100644 --- a/app/src/main/java/org/schabi/newpipe/local/subscription/SubscriptionsImportFragment.java +++ b/app/src/main/java/org/schabi/newpipe/local/subscription/SubscriptionsImportFragment.java @@ -12,6 +12,9 @@ import android.widget.Button; import android.widget.EditText; import android.widget.TextView; +import androidx.activity.result.ActivityResult; +import androidx.activity.result.ActivityResultLauncher; +import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.annotation.StringRes; @@ -27,6 +30,7 @@ import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.extractor.exceptions.ExtractionException; import org.schabi.newpipe.extractor.subscription.SubscriptionExtractor; import org.schabi.newpipe.local.subscription.services.SubscriptionsImportService; +import org.schabi.newpipe.streams.io.StoredFileHelper; import org.schabi.newpipe.util.Constants; import org.schabi.newpipe.util.ServiceHelper; @@ -34,7 +38,6 @@ import java.util.Collections; import java.util.List; import icepick.State; -import org.schabi.newpipe.streams.io.StoredFileHelper; import static org.schabi.newpipe.extractor.subscription.SubscriptionExtractor.ContentSource.CHANNEL_URL; import static org.schabi.newpipe.local.subscription.services.SubscriptionsImportService.CHANNEL_URL_MODE; @@ -43,8 +46,6 @@ import static org.schabi.newpipe.local.subscription.services.SubscriptionsImport import static org.schabi.newpipe.local.subscription.services.SubscriptionsImportService.KEY_VALUE; public class SubscriptionsImportFragment extends BaseFragment { - private static final int REQUEST_IMPORT_FILE_CODE = 666; - @State int currentServiceId = Constants.NO_SERVICE_ID; @@ -62,6 +63,9 @@ public class SubscriptionsImportFragment extends BaseFragment { private EditText inputText; private Button inputButton; + private final ActivityResultLauncher requestImportFileLauncher = + registerForActivityResult(new StartActivityForResult(), this::requestImportFileResult); + public static SubscriptionsImportFragment getInstance(final int serviceId) { final SubscriptionsImportFragment instance = new SubscriptionsImportFragment(); instance.setInitialData(serviceId); @@ -173,22 +177,19 @@ public class SubscriptionsImportFragment extends BaseFragment { } public void onImportFile() { - startActivityForResult(StoredFileHelper.getPicker(activity), REQUEST_IMPORT_FILE_CODE); + requestImportFileLauncher.launch(StoredFileHelper.getPicker(activity)); } - @Override - public void onActivityResult(final int requestCode, final int resultCode, final Intent data) { - super.onActivityResult(requestCode, resultCode, data); - if (data == null) { + private void requestImportFileResult(final ActivityResult result) { + if (result.getData() == null) { return; } - if (resultCode == Activity.RESULT_OK && requestCode == REQUEST_IMPORT_FILE_CODE - && data.getData() != null) { + if (result.getResultCode() == Activity.RESULT_OK && result.getData().getData() != null) { ImportConfirmationDialog.show(this, new Intent(activity, SubscriptionsImportService.class) .putExtra(KEY_MODE, INPUT_STREAM_MODE) - .putExtra(KEY_VALUE, data.getData()) + .putExtra(KEY_VALUE, result.getData().getData()) .putExtra(Constants.KEY_SERVICE_ID, currentServiceId)); } }