From 9dae40fae01c4ad07cf825dfabc2c251eea65124 Mon Sep 17 00:00:00 2001 From: Ammar Githam Date: Mon, 3 May 2021 17:31:00 +0900 Subject: [PATCH] More null checks. Probably resolves austinhuang0131/barinsta#992 --- .../settings/MorePreferencesFragment.java | 55 +++++++++++-------- 1 file changed, 33 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/fragments/settings/MorePreferencesFragment.java b/app/src/main/java/awais/instagrabber/fragments/settings/MorePreferencesFragment.java index a07c9221..a022d9ee 100644 --- a/app/src/main/java/awais/instagrabber/fragments/settings/MorePreferencesFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/settings/MorePreferencesFragment.java @@ -60,7 +60,6 @@ public class MorePreferencesFragment extends BasePreferencesFragment { final MainActivity activity = (MainActivity) getActivity(); // screen.addPreference(new MoreHeaderPreference(getContext())); final Context context = getContext(); - final Resources resources = context.getResources(); if (context == null) return; accountRepository = AccountRepository.getInstance(AccountDataSource.getInstance(context)); final PreferenceCategory accountCategory = new PreferenceCategory(context); @@ -77,9 +76,7 @@ public class MorePreferencesFragment extends BasePreferencesFragment { // shouldRecreate(); Toast.makeText(context1, R.string.logout_success, Toast.LENGTH_SHORT).show(); settingsHelper.putString(Constants.COOKIE, ""); - AppExecutors.getInstance().mainThread().execute(() -> { - ProcessPhoenix.triggerRebirth(context1); - }, 200); + AppExecutors.getInstance().mainThread().execute(() -> ProcessPhoenix.triggerRebirth(context1), 200); return true; })); } @@ -88,27 +85,35 @@ public class MorePreferencesFragment extends BasePreferencesFragment { public void onSuccess(@NonNull final List accounts) { if (!isLoggedIn) { if (accounts.size() > 0) { - final AccountSwitcherPreference preference = getAccountSwitcherPreference(null, context); + final Context context1 = getContext(); + final AccountSwitcherPreference preference = getAccountSwitcherPreference(null, context1); if (preference == null) return; accountCategory.addPreference(preference); } // Need to show something to trigger login activity final Preference preference1 = getPreference(R.string.add_account, R.drawable.ic_add, preference -> { - startActivityForResult(new Intent(getContext(), Login.class), Constants.LOGIN_RESULT_CODE); + final Context context1 = getContext(); + if (context1 == null) return false; + startActivityForResult(new Intent(context1, Login.class), Constants.LOGIN_RESULT_CODE); return true; }); if (preference1 == null) return; accountCategory.addPreference(preference1); } if (accounts.size() > 0) { - accountCategory - .addPreference(getPreference(R.string.remove_all_acc, null, R.drawable.ic_account_multiple_remove_24, preference -> { + final Preference preference1 = getPreference( + R.string.remove_all_acc, + null, + R.drawable.ic_account_multiple_remove_24, + preference -> { if (getContext() == null) return false; new AlertDialog.Builder(getContext()) .setTitle(R.string.logout) .setMessage(R.string.remove_all_acc_warning) .setPositiveButton(R.string.yes, (dialog, which) -> { - CookieUtils.removeAllAccounts(context, new RepositoryCallback() { + final Context context1 = getContext(); + if (context1 == null) return; + CookieUtils.removeAllAccounts(context1, new RepositoryCallback() { @Override public void onSuccess(final Void result) { // shouldRecreate(); @@ -116,9 +121,8 @@ public class MorePreferencesFragment extends BasePreferencesFragment { if (context1 == null) return; Toast.makeText(context1, R.string.logout_success, Toast.LENGTH_SHORT).show(); settingsHelper.putString(Constants.COOKIE, ""); - AppExecutors.getInstance().mainThread().execute(() -> { - ProcessPhoenix.triggerRebirth(context1); - }, 200); + AppExecutors.getInstance().mainThread() + .execute(() -> ProcessPhoenix.triggerRebirth(context1), 200); } @Override @@ -128,7 +132,9 @@ public class MorePreferencesFragment extends BasePreferencesFragment { .setNegativeButton(R.string.cancel, null) .show(); return true; - })); + }); + if (preference1 == null) return; + accountCategory.addPreference(preference1); } } @@ -236,12 +242,14 @@ public class MorePreferencesFragment extends BasePreferencesFragment { -1, preference -> { if (BuildConfig.isPre) return true; + if (activity == null) return false; FlavorTown.updateCheck(activity, true); return true; })); screen.addPreference(getDivider(context)); final Preference reminderPreference = getPreference(R.string.reminder, R.string.reminder_summary, R.drawable.ic_warning, null); + if (reminderPreference == null) return; reminderPreference.setSelectable(false); screen.addPreference(reminderPreference); } @@ -304,6 +312,7 @@ public class MorePreferencesFragment extends BasePreferencesFragment { } } + @Nullable private AccountSwitcherPreference getAccountSwitcherPreference(final String cookie, final Context context) { if (context == null) return null; return new AccountSwitcherPreference(context, cookie, accountRepository, v -> showAccountSwitcherDialog()); @@ -318,12 +327,14 @@ public class MorePreferencesFragment extends BasePreferencesFragment { dialogFragment.show(fragmentManager, "accountSwitcher"); } + @Nullable private Preference getPreference(final int title, final int icon, final Preference.OnPreferenceClickListener clickListener) { return getPreference(title, -1, icon, clickListener); } + @Nullable private Preference getPreference(final int title, final int summary, final int icon, @@ -339,6 +350,7 @@ public class MorePreferencesFragment extends BasePreferencesFragment { return getPreference(title, string, icon, clickListener); } + @Nullable private Preference getPreference(final int title, final String summary, final int icon, @@ -356,15 +368,14 @@ public class MorePreferencesFragment extends BasePreferencesFragment { return preference; } - - public static class MoreHeaderPreference extends Preference { - - public MoreHeaderPreference(final Context context) { - super(context); - setLayoutResource(R.layout.pref_more_header); - setSelectable(false); - } - } + // public static class MoreHeaderPreference extends Preference { + // + // public MoreHeaderPreference(final Context context) { + // super(context); + // setLayoutResource(R.layout.pref_more_header); + // setSelectable(false); + // } + // } public static class AccountSwitcherPreference extends Preference {