From b9378a7c1fedee40ec0ba16a42aba113f0659cc7 Mon Sep 17 00:00:00 2001 From: Stypox Date: Mon, 16 Jan 2023 22:30:28 +0100 Subject: [PATCH] Fix NPEs after OnSharedPreferenceChangeListener changes Apps targeting {@link android.os.Build.VERSION_CODES#R} on devices running OS versions {@link android.os.Build.VERSION_CODES#R Android R} or later, will receive a {@code null} value when preferences are cleared. --- .../newpipe/fragments/detail/VideoDetailFragment.java | 6 +++--- .../newpipe/fragments/list/BaseListFragment.java | 2 +- .../fragments/list/videos/RelatedItemsFragment.java | 8 +++----- .../schabi/newpipe/local/BaseLocalListFragment.java | 2 +- .../org/schabi/newpipe/settings/tabs/TabsManager.java | 2 +- .../shandian/giga/service/DownloadManagerService.java | 10 +++++----- 6 files changed, 14 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java index e46c054e5..917d89e09 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java @@ -172,13 +172,13 @@ public final class VideoDetailFragment private final SharedPreferences.OnSharedPreferenceChangeListener preferenceChangeListener = (sharedPreferences, key) -> { - if (key.equals(getString(R.string.show_comments_key))) { + if (getString(R.string.show_comments_key).equals(key)) { showComments = sharedPreferences.getBoolean(key, true); tabSettingsChanged = true; - } else if (key.equals(getString(R.string.show_next_video_key))) { + } else if (getString(R.string.show_next_video_key).equals(key)) { showRelatedItems = sharedPreferences.getBoolean(key, true); tabSettingsChanged = true; - } else if (key.equals(getString(R.string.show_description_key))) { + } else if (getString(R.string.show_description_key).equals(key)) { showDescription = sharedPreferences.getBoolean(key, true); tabSettingsChanged = true; } diff --git a/app/src/main/java/org/schabi/newpipe/fragments/list/BaseListFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/list/BaseListFragment.java index 0b0acff86..8a117a47a 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/list/BaseListFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/list/BaseListFragment.java @@ -474,7 +474,7 @@ public abstract class BaseListFragment extends BaseStateFragment @Override public void onSharedPreferenceChanged(final SharedPreferences sharedPreferences, final String key) { - if (key.equals(getString(R.string.list_view_mode_key))) { + if (getString(R.string.list_view_mode_key).equals(key)) { updateFlags |= LIST_MODE_UPDATE_FLAG; } } diff --git a/app/src/main/java/org/schabi/newpipe/fragments/list/videos/RelatedItemsFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/list/videos/RelatedItemsFragment.java index dc5dbd74f..65664e0e3 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/list/videos/RelatedItemsFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/list/videos/RelatedItemsFragment.java @@ -159,11 +159,9 @@ public class RelatedItemsFragment extends BaseListInfoFragment extends BaseStateFragment @Override public void onSharedPreferenceChanged(final SharedPreferences sharedPreferences, final String key) { - if (key.equals(getString(R.string.list_view_mode_key))) { + if (getString(R.string.list_view_mode_key).equals(key)) { updateFlags |= LIST_MODE_UPDATE_FLAG; } } diff --git a/app/src/main/java/org/schabi/newpipe/settings/tabs/TabsManager.java b/app/src/main/java/org/schabi/newpipe/settings/tabs/TabsManager.java index 2836fe52b..c885b803c 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/tabs/TabsManager.java +++ b/app/src/main/java/org/schabi/newpipe/settings/tabs/TabsManager.java @@ -73,7 +73,7 @@ public final class TabsManager { private SharedPreferences.OnSharedPreferenceChangeListener getPreferenceChangeListener() { return (sp, key) -> { - if (key.equals(savedTabsKey)) { + if (savedTabsKey.equals(key)) { if (savedTabsChangeListener != null) { savedTabsChangeListener.onTabsChanged(); } diff --git a/app/src/main/java/us/shandian/giga/service/DownloadManagerService.java b/app/src/main/java/us/shandian/giga/service/DownloadManagerService.java index dc56ee205..edc6bb6fd 100755 --- a/app/src/main/java/us/shandian/giga/service/DownloadManagerService.java +++ b/app/src/main/java/us/shandian/giga/service/DownloadManagerService.java @@ -310,7 +310,7 @@ public class DownloadManagerService extends Service { } private void handlePreferenceChange(SharedPreferences prefs, @NonNull String key) { - if (key.equals(getString(R.string.downloads_maximum_retry))) { + if (getString(R.string.downloads_maximum_retry).equals(key)) { try { String value = prefs.getString(key, getString(R.string.downloads_maximum_retry_default)); mManager.mPrefMaxRetry = value == null ? 0 : Integer.parseInt(value); @@ -318,13 +318,13 @@ public class DownloadManagerService extends Service { mManager.mPrefMaxRetry = 0; } mManager.updateMaximumAttempts(); - } else if (key.equals(getString(R.string.downloads_cross_network))) { + } else if (getString(R.string.downloads_cross_network).equals(key)) { mManager.mPrefMeteredDownloads = prefs.getBoolean(key, false); - } else if (key.equals(getString(R.string.downloads_queue_limit))) { + } else if (getString(R.string.downloads_queue_limit).equals(key)) { mManager.mPrefQueueLimit = prefs.getBoolean(key, true); - } else if (key.equals(getString(R.string.download_path_video_key))) { + } else if (getString(R.string.download_path_video_key).equals(key)) { mManager.mMainStorageVideo = loadMainVideoStorage(); - } else if (key.equals(getString(R.string.download_path_audio_key))) { + } else if (getString(R.string.download_path_audio_key).equals(key)) { mManager.mMainStorageAudio = loadMainAudioStorage(); } }