Split SettingsFragment

This commit is contained in:
xynngh 2020-11-02 00:36:14 +04:00
parent c88e704af1
commit a2d0ba5cd5
1 changed files with 126 additions and 56 deletions

View File

@ -52,7 +52,7 @@ public class SettingsActivity extends AppCompatActivity
} else { } else {
getSupportFragmentManager() getSupportFragmentManager()
.beginTransaction() .beginTransaction()
.replace(R.id.settings, new SettingsFragment()) .replace(R.id.settings, new RootSettingsFragment())
.commit(); .commit();
} }
@ -81,18 +81,14 @@ public class SettingsActivity extends AppCompatActivity
@Override @Override
public boolean onPreferenceStartScreen(PreferenceFragmentCompat preferenceFragmentCompat, public boolean onPreferenceStartScreen(PreferenceFragmentCompat preferenceFragmentCompat,
PreferenceScreen preferenceScreen) { PreferenceScreen preferenceScreen) {
SettingsFragment fragment = new SettingsFragment(); for (Fragment fragment : getSupportFragmentManager().getFragments()) {
Bundle args = new Bundle(); if (fragment instanceof BaseSettingsFragment) {
args.putString(PreferenceFragmentCompat.ARG_PREFERENCE_ROOT, preferenceScreen.getKey()); if (((BaseSettingsFragment) fragment)
fragment.setArguments(args); .onPreferenceStartScreen(preferenceFragmentCompat, preferenceScreen)) {
return true;
getSupportFragmentManager() }
.beginTransaction() }
.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left, }
R.anim.enter_from_left, R.anim.exit_to_right)
.replace(R.id.settings, fragment, preferenceScreen.getKey())
.addToBackStack(preferenceScreen.getKey())
.commit();
return true; return true;
} }
@ -121,46 +117,48 @@ public class SettingsActivity extends AppCompatActivity
private void updateCallScreeningPreference() { private void updateCallScreeningPreference() {
for (Fragment fragment : getSupportFragmentManager().getFragments()) { for (Fragment fragment : getSupportFragmentManager().getFragments()) {
if (fragment instanceof SettingsFragment) { if (fragment instanceof BaseSettingsFragment) {
((SettingsFragment) fragment).updateCallScreeningPreference(); ((RootSettingsFragment) fragment).updateCallScreeningPreference();
} }
} }
} }
public static class SettingsFragment extends PreferenceFragmentCompat { public static abstract class BaseSettingsFragment extends PreferenceFragmentCompat
implements PreferenceFragmentCompat.OnPreferenceStartScreenCallback {
private static final String PREF_USE_CALL_SCREENING_SERVICE = "useCallScreeningService";
private static final String PREF_AUTO_UPDATE_ENABLED = "autoUpdateEnabled";
private static final String PREF_NOTIFICATION_CHANNEL_SETTINGS = "notificationChannelSettings";
private static final String PREF_CATEGORY_NOTIFICATIONS = "categoryNotifications";
private static final String PREF_CATEGORY_NOTIFICATIONS_LEGACY = "categoryNotificationsLegacy";
private static final String PREF_NOTIFICATIONS_BLOCKED_NON_PERSISTENT = "showNotificationsForBlockedCallsNonPersistent";
private static final String PREF_SCREEN_ADVANCED = "screenAdvanced";
private static final String PREF_COUNTRY_CODES_INFO = "countryCodesInfo";
private static final String PREF_EXPORT_LOGCAT = "exportLogcat";
private static final Logger LOG = LoggerFactory.getLogger(SettingsFragment.class);
private final UpdateScheduler updateScheduler = UpdateScheduler.get(App.getInstance());
@Override @Override
public void onStart() { public void onStart() {
super.onStart(); super.onStart();
requireActivity().setTitle(getPreferenceScreen().getTitle()); requireActivity().setTitle(getPreferenceScreen().getTitle());
updateBlockedCallNotificationsPreference();
} }
@Override @Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
checkScreenKey(rootKey);
getPreferenceManager().setStorageDeviceProtected(); getPreferenceManager().setStorageDeviceProtected();
setPreferencesFromResource(R.xml.root_preferences, rootKey); setPreferencesFromResource(R.xml.root_preferences, rootKey);
initRootScreen(rootKey); initScreen();
initAdvancedScreen(rootKey);
disablePreferenceIcons();
}
protected void checkScreenKey(String key) {
String screenKey = getScreenKey();
if (!TextUtils.equals(screenKey, key)) {
throw new IllegalArgumentException("Incorrect key: " + key
+ ", expected: " + screenKey);
}
}
protected abstract String getScreenKey();
protected void initScreen() {}
protected void disablePreferenceIcons() {
PreferenceScreen preferenceScreen = getPreferenceScreen(); PreferenceScreen preferenceScreen = getPreferenceScreen();
int count = preferenceScreen.getPreferenceCount(); int count = preferenceScreen.getPreferenceCount();
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
@ -177,9 +175,72 @@ public class SettingsActivity extends AppCompatActivity
} }
} }
private void initRootScreen(String rootKey) { @Override
if (rootKey != null) return; public boolean onPreferenceStartScreen(PreferenceFragmentCompat caller,
PreferenceScreen pref) {
String key = pref.getKey();
PreferenceFragmentCompat fragment = getSubscreenFragment(key);
if (fragment == null) return false;
Bundle args = new Bundle();
args.putString(PreferenceFragmentCompat.ARG_PREFERENCE_ROOT, key);
fragment.setArguments(args);
getParentFragmentManager()
.beginTransaction()
.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left,
R.anim.enter_from_left, R.anim.exit_to_right)
.replace(R.id.settings, fragment, key)
.addToBackStack(key)
.commit();
return true;
}
protected PreferenceFragmentCompat getSubscreenFragment(String key) {
return null;
}
protected void setPrefChangeListener(@NonNull CharSequence key,
Preference.OnPreferenceChangeListener listener) {
requirePreference(key).setOnPreferenceChangeListener(listener);
}
@NonNull
protected <T extends Preference> T requirePreference(@NonNull CharSequence key) {
return Objects.requireNonNull(findPreference(key));
}
}
public static class RootSettingsFragment extends BaseSettingsFragment {
private static final String PREF_SCREEN_ROOT = null;
private static final String PREF_USE_CALL_SCREENING_SERVICE = "useCallScreeningService";
private static final String PREF_AUTO_UPDATE_ENABLED = "autoUpdateEnabled";
private static final String PREF_NOTIFICATION_CHANNEL_SETTINGS = "notificationChannelSettings";
private static final String PREF_CATEGORY_NOTIFICATIONS = "categoryNotifications";
private static final String PREF_CATEGORY_NOTIFICATIONS_LEGACY = "categoryNotificationsLegacy";
private static final String PREF_NOTIFICATIONS_BLOCKED_NON_PERSISTENT = "showNotificationsForBlockedCallsNonPersistent";
private static final String PREF_SCREEN_ADVANCED = "screenAdvanced";
private final UpdateScheduler updateScheduler = UpdateScheduler.get(App.getInstance());
@Override
public void onStart() {
super.onStart();
updateBlockedCallNotificationsPreference();
}
@Override
protected String getScreenKey() {
return PREF_SCREEN_ROOT;
}
@Override
protected void initScreen() {
setPrefChangeListener(Settings.PREF_INCOMING_CALL_NOTIFICATIONS, (pref, newValue) -> { setPrefChangeListener(Settings.PREF_INCOMING_CALL_NOTIFICATIONS, (pref, newValue) -> {
if (Boolean.TRUE.equals(newValue)) { if (Boolean.TRUE.equals(newValue)) {
PermissionHelper.checkPermissions(requireActivity(), true, false, false); PermissionHelper.checkPermissions(requireActivity(), true, false, false);
@ -308,9 +369,36 @@ public class SettingsActivity extends AppCompatActivity
} }
} }
private void initAdvancedScreen(String rootKey) { public void updateCallScreeningPreference() {
if (!PREF_SCREEN_ADVANCED.equals(rootKey)) return; SwitchPreferenceCompat callScreeningPref
= findPreference(PREF_USE_CALL_SCREENING_SERVICE);
if (callScreeningPref != null) {
callScreeningPref.setChecked(PermissionHelper.isCallScreeningHeld(requireContext()));
}
}
@Override
protected PreferenceFragmentCompat getSubscreenFragment(String key) {
return PREF_SCREEN_ADVANCED.equals(key) ? new AdvancedSettingsFragment() : null;
}
}
public static class AdvancedSettingsFragment extends BaseSettingsFragment {
private static final String PREF_SCREEN_ADVANCED = "screenAdvanced";
private static final String PREF_COUNTRY_CODES_INFO = "countryCodesInfo";
private static final String PREF_EXPORT_LOGCAT = "exportLogcat";
private static final Logger LOG = LoggerFactory.getLogger(AdvancedSettingsFragment.class);
@Override
protected String getScreenKey() {
return PREF_SCREEN_ADVANCED;
}
@Override
protected void initScreen() {
String countryCodesExplanationSummary = getString(R.string.country_codes_info_summary) String countryCodesExplanationSummary = getString(R.string.country_codes_info_summary)
+ ". " + getString(R.string.country_codes_info_summary_addition, + ". " + getString(R.string.country_codes_info_summary_addition,
App.getSettings().getCachedAutoDetectedCountryCode()); App.getSettings().getCachedAutoDetectedCountryCode());
@ -349,14 +437,6 @@ public class SettingsActivity extends AppCompatActivity
}); });
} }
public void updateCallScreeningPreference() {
SwitchPreferenceCompat callScreeningPref
= findPreference(PREF_USE_CALL_SCREENING_SERVICE);
if (callScreeningPref != null) {
callScreeningPref.setChecked(PermissionHelper.isCallScreeningHeld(requireContext()));
}
}
private void exportLogcat() { private void exportLogcat() {
Activity activity = requireActivity(); Activity activity = requireActivity();
@ -373,16 +453,6 @@ public class SettingsActivity extends AppCompatActivity
} }
} }
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));
}
} }
} }