From 9591f14551fdd98f11700ac7ca5c0841b786c7b7 Mon Sep 17 00:00:00 2001 From: TacoTheDank Date: Thu, 10 Jun 2021 12:51:30 -0400 Subject: [PATCH 1/6] Update AndroidX Fragment to 1.3.4 --- app/build.gradle | 1 + 1 file changed, 1 insertion(+) diff --git a/app/build.gradle b/app/build.gradle index 795f4488f..6b668529c 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -198,6 +198,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.lifecycle:lifecycle-livedata:${androidxLifecycleVersion}" implementation "androidx.lifecycle:lifecycle-viewmodel:${androidxLifecycleVersion}" implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0' From e4885e3c521650e3e41f9b65044955492e6ce4af Mon Sep 17 00:00:00 2001 From: TacoTheDank Date: Tue, 15 Jun 2021 22:08:25 -0400 Subject: [PATCH 2/6] Fix some older deprecations from previous Fragment versions --- .../subscription/ImportConfirmationDialog.java | 6 +----- .../local/subscription/SubscriptionFragment.kt | 2 +- .../settings/tabs/ChooseTabsFragment.java | 6 +++--- .../schabi/newpipe/util/StreamDialogEntry.java | 16 +++++++--------- 4 files changed, 12 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/local/subscription/ImportConfirmationDialog.java b/app/src/main/java/org/schabi/newpipe/local/subscription/ImportConfirmationDialog.java index 602e418a0..5ab0699eb 100644 --- a/app/src/main/java/org/schabi/newpipe/local/subscription/ImportConfirmationDialog.java +++ b/app/src/main/java/org/schabi/newpipe/local/subscription/ImportConfirmationDialog.java @@ -23,13 +23,9 @@ public class ImportConfirmationDialog extends DialogFragment { public static void show(@NonNull final Fragment fragment, @NonNull final Intent resultServiceIntent) { - if (fragment.getFragmentManager() == null) { - return; - } - final ImportConfirmationDialog confirmationDialog = new ImportConfirmationDialog(); confirmationDialog.setResultServiceIntent(resultServiceIntent); - confirmationDialog.show(fragment.getFragmentManager(), null); + confirmationDialog.show(fragment.getParentFragmentManager(), null); } public void setResultServiceIntent(final Intent resultServiceIntent) { diff --git a/app/src/main/java/org/schabi/newpipe/local/subscription/SubscriptionFragment.kt b/app/src/main/java/org/schabi/newpipe/local/subscription/SubscriptionFragment.kt index 095c8dbc7..88203b69e 100644 --- a/app/src/main/java/org/schabi/newpipe/local/subscription/SubscriptionFragment.kt +++ b/app/src/main/java/org/schabi/newpipe/local/subscription/SubscriptionFragment.kt @@ -198,7 +198,7 @@ class SubscriptionFragment : BaseStateFragment() { } private fun openReorderDialog() { - FeedGroupReorderDialog().show(requireFragmentManager(), null) + FeedGroupReorderDialog().show(parentFragmentManager, null) } override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { diff --git a/app/src/main/java/org/schabi/newpipe/settings/tabs/ChooseTabsFragment.java b/app/src/main/java/org/schabi/newpipe/settings/tabs/ChooseTabsFragment.java index 9c1a9bdd7..26cfda329 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/tabs/ChooseTabsFragment.java +++ b/app/src/main/java/org/schabi/newpipe/settings/tabs/ChooseTabsFragment.java @@ -192,13 +192,13 @@ public class ChooseTabsFragment extends Fragment { final SelectKioskFragment selectKioskFragment = new SelectKioskFragment(); selectKioskFragment.setOnSelectedListener((serviceId, kioskId, kioskName) -> addTab(new Tab.KioskTab(serviceId, kioskId))); - selectKioskFragment.show(requireFragmentManager(), "select_kiosk"); + selectKioskFragment.show(getParentFragmentManager(), "select_kiosk"); return; case CHANNEL: final SelectChannelFragment selectChannelFragment = new SelectChannelFragment(); selectChannelFragment.setOnSelectedListener((serviceId, url, name) -> addTab(new Tab.ChannelTab(serviceId, url, name))); - selectChannelFragment.show(requireFragmentManager(), "select_channel"); + selectChannelFragment.show(getParentFragmentManager(), "select_channel"); return; case PLAYLIST: final SelectPlaylistFragment selectPlaylistFragment = new SelectPlaylistFragment(); @@ -215,7 +215,7 @@ public class ChooseTabsFragment extends Fragment { addTab(new Tab.PlaylistTab(serviceId, url, name)); } }); - selectPlaylistFragment.show(requireFragmentManager(), "select_playlist"); + selectPlaylistFragment.show(getParentFragmentManager(), "select_playlist"); return; default: addTab(type.getTab()); diff --git a/app/src/main/java/org/schabi/newpipe/util/StreamDialogEntry.java b/app/src/main/java/org/schabi/newpipe/util/StreamDialogEntry.java index 610f9f852..5a080bc21 100644 --- a/app/src/main/java/org/schabi/newpipe/util/StreamDialogEntry.java +++ b/app/src/main/java/org/schabi/newpipe/util/StreamDialogEntry.java @@ -66,16 +66,14 @@ public enum StreamDialogEntry { }), // has to be set manually append_playlist(R.string.append_playlist, (fragment, item) -> { - if (fragment.getFragmentManager() != null) { - final PlaylistAppendDialog d = PlaylistAppendDialog - .fromStreamInfoItems(Collections.singletonList(item)); + final PlaylistAppendDialog d = PlaylistAppendDialog + .fromStreamInfoItems(Collections.singletonList(item)); - PlaylistAppendDialog.onPlaylistFound(fragment.getContext(), - () -> d.show(fragment.getFragmentManager(), "StreamDialogEntry@append_playlist"), - () -> PlaylistCreationDialog.newInstance(d) - .show(fragment.getFragmentManager(), "StreamDialogEntry@create_playlist") - ); - } + PlaylistAppendDialog.onPlaylistFound(fragment.getContext(), + () -> d.show(fragment.getParentFragmentManager(), "StreamDialogEntry@append_playlist"), + () -> PlaylistCreationDialog.newInstance(d) + .show(fragment.getParentFragmentManager(), "StreamDialogEntry@create_playlist") + ); }), play_with_kodi(R.string.play_with_kodi_title, (fragment, item) -> { From 1061fca6a39bb5e080a9a0d0d1a31635e645836d Mon Sep 17 00:00:00 2001 From: TacoTheDank Date: Tue, 15 Jun 2021 22:08:41 -0400 Subject: [PATCH 3/6] Add super.onRequestPermissionsResult where missing --- app/src/main/java/org/schabi/newpipe/MainActivity.java | 1 + app/src/main/java/org/schabi/newpipe/RouterActivity.java | 1 + 2 files changed, 2 insertions(+) diff --git a/app/src/main/java/org/schabi/newpipe/MainActivity.java b/app/src/main/java/org/schabi/newpipe/MainActivity.java index 26ee261a4..9bd289376 100644 --- a/app/src/main/java/org/schabi/newpipe/MainActivity.java +++ b/app/src/main/java/org/schabi/newpipe/MainActivity.java @@ -603,6 +603,7 @@ public class MainActivity extends AppCompatActivity { public void onRequestPermissionsResult(final int requestCode, @NonNull final String[] permissions, @NonNull final int[] grantResults) { + super.onRequestPermissionsResult(requestCode, permissions, grantResults); for (final int i : grantResults) { if (i == PackageManager.PERMISSION_DENIED) { return; diff --git a/app/src/main/java/org/schabi/newpipe/RouterActivity.java b/app/src/main/java/org/schabi/newpipe/RouterActivity.java index 0d70a7181..eb5643df7 100644 --- a/app/src/main/java/org/schabi/newpipe/RouterActivity.java +++ b/app/src/main/java/org/schabi/newpipe/RouterActivity.java @@ -590,6 +590,7 @@ public class RouterActivity extends AppCompatActivity { public void onRequestPermissionsResult(final int requestCode, @NonNull final String[] permissions, @NonNull final int[] grantResults) { + super.onRequestPermissionsResult(requestCode, permissions, grantResults); for (final int i : grantResults) { if (i == PackageManager.PERMISSION_DENIED) { finish(); From aa4e5da146d7ceb12d956b63f3e423e4b250232f Mon Sep 17 00:00:00 2001 From: TacoTheDank Date: Tue, 15 Jun 2021 22:08:55 -0400 Subject: [PATCH 4/6] Fix onActivityResult deprecation in ContentSettingsFragment --- .../settings/ContentSettingsFragment.java | 73 +++++++++---------- 1 file changed, 36 insertions(+), 37 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java b/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java index 4ef5b8b25..6c2b5dbe8 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java +++ b/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java @@ -2,7 +2,6 @@ package org.schabi.newpipe.settings; import android.app.Activity; import android.content.Context; -import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.net.Uri; @@ -10,6 +9,9 @@ import android.os.Bundle; import android.util.Log; import android.widget.Toast; +import androidx.activity.result.ActivityResult; +import androidx.activity.result.ActivityResultLauncher; +import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult; import androidx.annotation.Nullable; import androidx.appcompat.app.AlertDialog; import androidx.core.content.ContextCompat; @@ -39,8 +41,6 @@ import static org.schabi.newpipe.extractor.utils.Utils.isBlank; import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage; public class ContentSettingsFragment extends BasePreferenceFragment { - private static final int REQUEST_IMPORT_PATH = 8945; - private static final int REQUEST_EXPORT_PATH = 30945; private static final String ZIP_MIME_TYPE = "application/zip"; private static final SimpleDateFormat EXPORT_DATE_FORMAT = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US); @@ -55,6 +55,10 @@ public class ContentSettingsFragment extends BasePreferenceFragment { private Localization initialSelectedLocalization; private ContentCountry initialSelectedContentCountry; private String initialLanguage; + private final ActivityResultLauncher requestImportPathLauncher = + registerForActivityResult(new StartActivityForResult(), this::requestImportPathResult); + private final ActivityResultLauncher requestExportPathLauncher = + registerForActivityResult(new StartActivityForResult(), this::requestExportPathResult); @Override public void onCreatePreferences(final Bundle savedInstanceState, final String rootKey) { @@ -71,20 +75,18 @@ public class ContentSettingsFragment extends BasePreferenceFragment { final Preference importDataPreference = requirePreference(R.string.import_data); importDataPreference.setOnPreferenceClickListener((Preference p) -> { - startActivityForResult( - StoredFileHelper.getPicker(requireContext(), getImportExportDataUri()), - REQUEST_IMPORT_PATH); + requestImportPathLauncher.launch( + StoredFileHelper.getPicker(requireContext(), getImportExportDataUri())); return true; }); final Preference exportDataPreference = requirePreference(R.string.export_data); exportDataPreference.setOnPreferenceClickListener((final Preference p) -> { - startActivityForResult( + requestExportPathLauncher.launch( StoredFileHelper.getNewPicker(requireContext(), "NewPipeData-" + EXPORT_DATE_FORMAT.format(new Date()) + ".zip", - ZIP_MIME_TYPE, getImportExportDataUri()), - REQUEST_EXPORT_PATH); + ZIP_MIME_TYPE, getImportExportDataUri())); return true; }); @@ -156,37 +158,34 @@ public class ContentSettingsFragment extends BasePreferenceFragment { } } - @Override - public void onActivityResult(final int requestCode, - final int resultCode, - @Nullable final Intent data) { + private void requestExportPathResult(final ActivityResult result) { assureCorrectAppLanguage(getContext()); - super.onActivityResult(requestCode, resultCode, data); - if (DEBUG) { - Log.d(TAG, "onActivityResult() called with: " - + "requestCode = [" + requestCode + "], " - + "resultCode = [" + resultCode + "], " - + "data = [" + data + "]"); - } - - if ((requestCode == REQUEST_IMPORT_PATH || requestCode == REQUEST_EXPORT_PATH) - && resultCode == Activity.RESULT_OK && data != null && data.getData() != null) { - - lastImportExportDataUri = data.getData(); // will be saved only on success + if (result.getResultCode() == Activity.RESULT_OK && result.getData() != null) { + lastImportExportDataUri = result.getData().getData(); // will be saved only on success final StoredFileHelper file - = new StoredFileHelper(getContext(), data.getData(), ZIP_MIME_TYPE); - if (requestCode == REQUEST_EXPORT_PATH) { - exportDatabase(file); - } else { - final AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity()); - builder.setMessage(R.string.override_current_data) - .setPositiveButton(R.string.finish, - (DialogInterface d, int id) -> importDatabase(file)) - .setNegativeButton(R.string.cancel, - (DialogInterface d, int id) -> d.cancel()); - builder.create().show(); - } + = new StoredFileHelper(getContext(), result.getData().getData(), ZIP_MIME_TYPE); + + exportDatabase(file); + } + } + + private void requestImportPathResult(final ActivityResult result) { + assureCorrectAppLanguage(getContext()); + if (result.getResultCode() == Activity.RESULT_OK && result.getData() != null) { + lastImportExportDataUri = result.getData().getData(); // will be saved only on success + + final StoredFileHelper file + = new StoredFileHelper(getContext(), result.getData().getData(), ZIP_MIME_TYPE); + + new AlertDialog.Builder(requireActivity()) + .setMessage(R.string.override_current_data) + .setPositiveButton(R.string.finish, (d, id) -> + importDatabase(file)) + .setNegativeButton(R.string.cancel, (d, id) -> + d.cancel()) + .create() + .show(); } } From cae160b5be42ebf605a2f1ff0bd6c71e47c7f028 Mon Sep 17 00:00:00 2001 From: TacoTheDank Date: Tue, 15 Jun 2021 22:09:07 -0400 Subject: [PATCH 5/6] 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)); } } From a1432e939f417eca4ce633b35ecebc41848c0396 Mon Sep 17 00:00:00 2001 From: TacoTheDank Date: Tue, 15 Jun 2021 22:09:19 -0400 Subject: [PATCH 6/6] Fix onActivityResult deprecation in SubscriptionFragment --- .../subscription/SubscriptionFragment.kt | 52 ++++++++++--------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/local/subscription/SubscriptionFragment.kt b/app/src/main/java/org/schabi/newpipe/local/subscription/SubscriptionFragment.kt index 88203b69e..c4e3780c8 100644 --- a/app/src/main/java/org/schabi/newpipe/local/subscription/SubscriptionFragment.kt +++ b/app/src/main/java/org/schabi/newpipe/local/subscription/SubscriptionFragment.kt @@ -15,6 +15,8 @@ import android.view.MenuInflater import android.view.View import android.view.ViewGroup import android.widget.Toast +import androidx.activity.result.ActivityResult +import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult import androidx.appcompat.app.AlertDialog import androidx.lifecycle.ViewModelProvider import androidx.localbroadcastmanager.content.LocalBroadcastManager @@ -82,6 +84,11 @@ class SubscriptionFragment : BaseStateFragment() { private lateinit var feedGroupsSortMenuItem: HeaderWithMenuItem private val subscriptionsSection = Section() + private val requestExportLauncher = + registerForActivityResult(StartActivityForResult(), this::requestExportResult) + private val requestImportLauncher = + registerForActivityResult(StartActivityForResult(), this::requestImportResult) + @State @JvmField var itemsListState: Parcelable? = null @@ -184,16 +191,15 @@ class SubscriptionFragment : BaseStateFragment() { } private fun onImportPreviousSelected() { - startActivityForResult(StoredFileHelper.getPicker(activity), REQUEST_IMPORT_CODE) + requestImportLauncher.launch(StoredFileHelper.getPicker(activity)) } private fun onExportSelected() { val date = SimpleDateFormat("yyyyMMddHHmm", Locale.ENGLISH).format(Date()) val exportName = "newpipe_subscriptions_$date.json" - startActivityForResult( - StoredFileHelper.getNewPicker(activity, exportName, "application/json", null), - REQUEST_EXPORT_CODE + requestExportLauncher.launch( + StoredFileHelper.getNewPicker(activity, exportName, "application/json", null) ) } @@ -201,22 +207,23 @@ class SubscriptionFragment : BaseStateFragment() { FeedGroupReorderDialog().show(parentFragmentManager, null) } - override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { - super.onActivityResult(requestCode, resultCode, data) - if (data != null && data.data != null && resultCode == Activity.RESULT_OK) { - if (requestCode == REQUEST_EXPORT_CODE) { - activity.startService( - Intent(activity, SubscriptionsExportService::class.java) - .putExtra(SubscriptionsExportService.KEY_FILE_PATH, data.data) - ) - } else if (requestCode == REQUEST_IMPORT_CODE) { - ImportConfirmationDialog.show( - this, - Intent(activity, SubscriptionsImportService::class.java) - .putExtra(KEY_MODE, PREVIOUS_EXPORT_MODE) - .putExtra(KEY_VALUE, data.data) - ) - } + fun requestExportResult(result: ActivityResult) { + if (result.data != null && result.resultCode == Activity.RESULT_OK) { + activity.startService( + Intent(activity, SubscriptionsExportService::class.java) + .putExtra(SubscriptionsExportService.KEY_FILE_PATH, result.data?.data) + ) + } + } + + fun requestImportResult(result: ActivityResult) { + if (result.data != null && result.resultCode == Activity.RESULT_OK) { + ImportConfirmationDialog.show( + this, + Intent(activity, SubscriptionsImportService::class.java) + .putExtra(KEY_MODE, PREVIOUS_EXPORT_MODE) + .putExtra(KEY_VALUE, result.data?.data) + ) } } @@ -436,9 +443,4 @@ class SubscriptionFragment : BaseStateFragment() { val minWidth = resources.getDimensionPixelSize(R.dimen.channel_item_grid_min_width) return max(1, floor(resources.displayMetrics.widthPixels / minWidth.toDouble()).toInt()) } - - companion object { - private const val REQUEST_EXPORT_CODE = 666 - private const val REQUEST_IMPORT_CODE = 667 - } }