Slightly refactor SettingsActivity

This commit is contained in:
xynngh 2020-11-01 22:56:48 +04:00
parent 92a2a30619
commit c88e704af1
1 changed files with 39 additions and 48 deletions

View File

@ -15,7 +15,6 @@ import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity; import androidx.fragment.app.FragmentActivity;
import androidx.preference.EditTextPreference;
import androidx.preference.Preference; import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat; import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceGroup; import androidx.preference.PreferenceGroup;
@ -27,16 +26,14 @@ import org.slf4j.LoggerFactory;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.Objects;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import dummydomain.yetanothercallblocker.preference.IntListPreference;
import dummydomain.yetanothercallblocker.utils.DebuggingUtils; import dummydomain.yetanothercallblocker.utils.DebuggingUtils;
import dummydomain.yetanothercallblocker.utils.FileUtils; import dummydomain.yetanothercallblocker.utils.FileUtils;
import dummydomain.yetanothercallblocker.utils.PackageManagerUtils; import dummydomain.yetanothercallblocker.utils.PackageManagerUtils;
import dummydomain.yetanothercallblocker.work.UpdateScheduler; import dummydomain.yetanothercallblocker.work.UpdateScheduler;
import static java.util.Objects.requireNonNull;
public class SettingsActivity extends AppCompatActivity public class SettingsActivity extends AppCompatActivity
implements PreferenceFragmentCompat.OnPreferenceStartScreenCallback { implements PreferenceFragmentCompat.OnPreferenceStartScreenCallback {
@ -183,9 +180,7 @@ public class SettingsActivity extends AppCompatActivity
private void initRootScreen(String rootKey) { private void initRootScreen(String rootKey) {
if (rootKey != null) return; if (rootKey != null) return;
SwitchPreferenceCompat incomingCallNotificationPref = setPrefChangeListener(Settings.PREF_INCOMING_CALL_NOTIFICATIONS, (pref, newValue) -> {
requireNonNull(findPreference(Settings.PREF_INCOMING_CALL_NOTIFICATIONS));
incomingCallNotificationPref.setOnPreferenceChangeListener((preference, newValue) -> {
if (Boolean.TRUE.equals(newValue)) { if (Boolean.TRUE.equals(newValue)) {
PermissionHelper.checkPermissions(requireActivity(), true, false, false); PermissionHelper.checkPermissions(requireActivity(), true, false, false);
} }
@ -198,15 +193,12 @@ public class SettingsActivity extends AppCompatActivity
} }
return true; return true;
}; };
requireNonNull((SwitchPreferenceCompat) findPreference(Settings.PREF_BLOCK_NEGATIVE_SIA_NUMBERS)) setPrefChangeListener(Settings.PREF_BLOCK_NEGATIVE_SIA_NUMBERS, callBlockingListener);
.setOnPreferenceChangeListener(callBlockingListener); setPrefChangeListener(Settings.PREF_BLOCK_HIDDEN_NUMBERS, callBlockingListener);
requireNonNull((SwitchPreferenceCompat) findPreference(Settings.PREF_BLOCK_HIDDEN_NUMBERS)) setPrefChangeListener(Settings.PREF_BLOCK_BLACKLISTED, callBlockingListener);
.setOnPreferenceChangeListener(callBlockingListener);
requireNonNull((SwitchPreferenceCompat) findPreference(Settings.PREF_BLOCK_BLACKLISTED))
.setOnPreferenceChangeListener(callBlockingListener);
SwitchPreferenceCompat callScreeningPref = SwitchPreferenceCompat callScreeningPref =
requireNonNull(findPreference(PREF_USE_CALL_SCREENING_SERVICE)); requirePreference(PREF_USE_CALL_SCREENING_SERVICE);
callScreeningPref.setChecked(PermissionHelper.isCallScreeningHeld(requireContext())); callScreeningPref.setChecked(PermissionHelper.isCallScreeningHeld(requireContext()));
callScreeningPref.setOnPreferenceChangeListener((preference, newValue) -> { callScreeningPref.setOnPreferenceChangeListener((preference, newValue) -> {
if (Boolean.TRUE.equals(newValue)) { if (Boolean.TRUE.equals(newValue)) {
@ -228,9 +220,7 @@ public class SettingsActivity extends AppCompatActivity
callScreeningPref.setVisible(false); callScreeningPref.setVisible(false);
} }
SwitchPreferenceCompat monitoringPref = setPrefChangeListener(Settings.PREF_USE_MONITORING_SERVICE, (pref, newValue) -> {
requireNonNull(findPreference(Settings.PREF_USE_MONITORING_SERVICE));
monitoringPref.setOnPreferenceChangeListener((preference, newValue) -> {
boolean enabled = Boolean.TRUE.equals(newValue); boolean enabled = Boolean.TRUE.equals(newValue);
Context context = requireContext(); Context context = requireContext();
@ -246,7 +236,7 @@ public class SettingsActivity extends AppCompatActivity
}); });
SwitchPreferenceCompat nonPersistentAutoUpdatePref = SwitchPreferenceCompat nonPersistentAutoUpdatePref =
requireNonNull(findPreference(PREF_AUTO_UPDATE_ENABLED)); requirePreference(PREF_AUTO_UPDATE_ENABLED);
nonPersistentAutoUpdatePref.setChecked(updateScheduler.isAutoUpdateScheduled()); nonPersistentAutoUpdatePref.setChecked(updateScheduler.isAutoUpdateScheduled());
nonPersistentAutoUpdatePref.setOnPreferenceChangeListener((preference, newValue) -> { nonPersistentAutoUpdatePref.setOnPreferenceChangeListener((preference, newValue) -> {
if (Boolean.TRUE.equals(newValue)) { if (Boolean.TRUE.equals(newValue)) {
@ -257,25 +247,21 @@ public class SettingsActivity extends AppCompatActivity
return true; return true;
}); });
SwitchPreferenceCompat useContactsPref = setPrefChangeListener(Settings.PREF_USE_CONTACTS, (preference, newValue) -> {
requireNonNull(findPreference(Settings.PREF_USE_CONTACTS));
useContactsPref.setOnPreferenceChangeListener((preference, newValue) -> {
if (Boolean.TRUE.equals(newValue)) { if (Boolean.TRUE.equals(newValue)) {
PermissionHelper.checkPermissions(requireActivity(), false, false, true); PermissionHelper.checkPermissions(requireActivity(), false, false, true);
} }
return true; return true;
}); });
IntListPreference uiModePref = requireNonNull(findPreference(Settings.PREF_UI_MODE)); setPrefChangeListener(Settings.PREF_UI_MODE, (preference, newValue) -> {
uiModePref.setOnPreferenceChangeListener((preference, newValue) -> {
App.setUiMode(Integer.parseInt((String) newValue)); App.setUiMode(Integer.parseInt((String) newValue));
return true; return true;
}); });
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Preference notificationChannelSettings = requireNonNull( requirePreference(PREF_NOTIFICATION_CHANNEL_SETTINGS)
findPreference(PREF_NOTIFICATION_CHANNEL_SETTINGS)); .setOnPreferenceClickListener(preference -> {
notificationChannelSettings.setOnPreferenceClickListener(preference -> {
Intent intent = new Intent( Intent intent = new Intent(
android.provider.Settings.ACTION_APP_NOTIFICATION_SETTINGS); android.provider.Settings.ACTION_APP_NOTIFICATION_SETTINGS);
intent.putExtra(android.provider.Settings.EXTRA_APP_PACKAGE, intent.putExtra(android.provider.Settings.EXTRA_APP_PACKAGE,
@ -284,14 +270,12 @@ public class SettingsActivity extends AppCompatActivity
return true; return true;
}); });
Preference legacyCategory = requireNonNull(findPreference(PREF_CATEGORY_NOTIFICATIONS_LEGACY)); requirePreference(PREF_CATEGORY_NOTIFICATIONS_LEGACY).setVisible(false);
legacyCategory.setVisible(false);
} else { } else {
Preference modernCategory = requireNonNull(findPreference(PREF_CATEGORY_NOTIFICATIONS)); requirePreference(PREF_CATEGORY_NOTIFICATIONS).setVisible(false);
modernCategory.setVisible(false);
SwitchPreferenceCompat blockedCallNotificationsPref = SwitchPreferenceCompat blockedCallNotificationsPref =
requireNonNull(findPreference(PREF_NOTIFICATIONS_BLOCKED_NON_PERSISTENT)); requirePreference(PREF_NOTIFICATIONS_BLOCKED_NON_PERSISTENT);
blockedCallNotificationsPref.setChecked( blockedCallNotificationsPref.setChecked(
App.getSettings().getNotificationsForBlockedCalls()); App.getSettings().getNotificationsForBlockedCalls());
blockedCallNotificationsPref.setOnPreferenceChangeListener((pref, newValue) -> { blockedCallNotificationsPref.setOnPreferenceChangeListener((pref, newValue) -> {
@ -331,8 +315,7 @@ public class SettingsActivity extends AppCompatActivity
+ ". " + getString(R.string.country_codes_info_summary_addition, + ". " + getString(R.string.country_codes_info_summary_addition,
App.getSettings().getCachedAutoDetectedCountryCode()); App.getSettings().getCachedAutoDetectedCountryCode());
Preference countryCodesInfoPreference Preference countryCodesInfoPreference = requirePreference(PREF_COUNTRY_CODES_INFO);
= requireNonNull(findPreference(PREF_COUNTRY_CODES_INFO));
countryCodesInfoPreference.setSummary(countryCodesExplanationSummary); countryCodesInfoPreference.setSummary(countryCodesExplanationSummary);
countryCodesInfoPreference.setOnPreferenceClickListener(preference -> { countryCodesInfoPreference.setOnPreferenceClickListener(preference -> {
new AlertDialog.Builder(requireActivity()) new AlertDialog.Builder(requireActivity())
@ -355,15 +338,11 @@ public class SettingsActivity extends AppCompatActivity
return false; return false;
}; };
EditTextPreference countryCodePreference setPrefChangeListener(Settings.PREF_COUNTRY_CODE_OVERRIDE, countryCodeChangeListener);
= requireNonNull(findPreference(Settings.PREF_COUNTRY_CODE_OVERRIDE)); setPrefChangeListener(Settings.PREF_COUNTRY_CODE_FOR_REVIEWS_OVERRIDE,
countryCodePreference.setOnPreferenceChangeListener(countryCodeChangeListener); countryCodeChangeListener);
EditTextPreference countryCodeForReviewsPreference requirePreference(PREF_EXPORT_LOGCAT)
= requireNonNull(findPreference(Settings.PREF_COUNTRY_CODE_FOR_REVIEWS_OVERRIDE));
countryCodeForReviewsPreference.setOnPreferenceChangeListener(countryCodeChangeListener);
requireNonNull((Preference) findPreference(PREF_EXPORT_LOGCAT))
.setOnPreferenceClickListener(preference -> { .setOnPreferenceClickListener(preference -> {
exportLogcat(); exportLogcat();
return true; return true;
@ -393,5 +372,17 @@ public class SettingsActivity extends AppCompatActivity
FileUtils.shareFile(activity, new File(path)); FileUtils.shareFile(activity, new File(path));
} }
} }
private void setPrefChangeListener(@NonNull CharSequence key,
Preference.OnPreferenceChangeListener listener) {
requirePreference(key).setOnPreferenceChangeListener(listener);
} }
@NonNull
private <T extends Preference> T requirePreference(@NonNull CharSequence key) {
return Objects.requireNonNull(findPreference(key));
}
}
} }