mirror of
https://gitlab.com/xynngh/YetAnotherCallBlocker.git
synced 2025-06-05 22:19:12 +02:00
Load settings subscreens from separate files
This commit is contained in:
@@ -30,6 +30,11 @@ public class AdvancedSettingsFragment extends BaseSettingsFragment {
|
||||
return PREF_SCREEN_ADVANCED;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getPreferencesResId() {
|
||||
return R.xml.advanced_preferences;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void initScreen() {
|
||||
String countryCodesExplanationSummary = getString(R.string.country_codes_info_summary)
|
||||
|
@@ -4,6 +4,9 @@ import android.os.Bundle;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.XmlRes;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.fragment.app.FragmentActivity;
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceFragmentCompat;
|
||||
import androidx.preference.PreferenceGroup;
|
||||
@@ -12,7 +15,8 @@ import androidx.preference.PreferenceScreen;
|
||||
import java.util.Objects;
|
||||
|
||||
public abstract class BaseSettingsFragment extends PreferenceFragmentCompat
|
||||
implements PreferenceFragmentCompat.OnPreferenceStartScreenCallback {
|
||||
implements PreferenceFragmentCompat.OnPreferenceStartScreenCallback,
|
||||
PreferenceFragmentCompat.OnPreferenceStartFragmentCallback {
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
@@ -25,9 +29,9 @@ public abstract class BaseSettingsFragment extends PreferenceFragmentCompat
|
||||
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
|
||||
checkScreenKey(rootKey);
|
||||
|
||||
getPreferenceManager().setStorageDeviceProtected();
|
||||
switchToDeviceProtectedStorage();
|
||||
|
||||
setPreferencesFromResource(R.xml.root_preferences, rootKey);
|
||||
setPreferencesFromResource(rootKey);
|
||||
|
||||
initScreen();
|
||||
|
||||
@@ -44,6 +48,17 @@ public abstract class BaseSettingsFragment extends PreferenceFragmentCompat
|
||||
|
||||
protected abstract String getScreenKey();
|
||||
|
||||
protected void switchToDeviceProtectedStorage() {
|
||||
getPreferenceManager().setStorageDeviceProtected();
|
||||
}
|
||||
|
||||
protected void setPreferencesFromResource(String rootKey) {
|
||||
setPreferencesFromResource(getPreferencesResId(), rootKey);
|
||||
}
|
||||
|
||||
@XmlRes
|
||||
protected abstract int getPreferencesResId();
|
||||
|
||||
protected void initScreen() {}
|
||||
|
||||
protected void disablePreferenceIcons() {
|
||||
@@ -66,16 +81,37 @@ public abstract class BaseSettingsFragment extends PreferenceFragmentCompat
|
||||
@Override
|
||||
public boolean onPreferenceStartScreen(PreferenceFragmentCompat caller,
|
||||
PreferenceScreen pref) {
|
||||
String key = pref.getKey();
|
||||
return switchToFragment(getFragmentForPreferenceScreen(caller, pref), pref.getKey());
|
||||
}
|
||||
|
||||
PreferenceFragmentCompat fragment = getSubscreenFragment(key);
|
||||
if (fragment == null) return false;
|
||||
@Override
|
||||
public boolean onPreferenceStartFragment(PreferenceFragmentCompat caller, Preference pref) {
|
||||
return switchToFragment(getFragmentForPreferenceFragment(caller, pref), pref.getKey());
|
||||
}
|
||||
|
||||
protected Fragment getFragmentForPreferenceScreen(PreferenceFragmentCompat caller,
|
||||
PreferenceScreen pref) {
|
||||
return null; // should be overridden if needed
|
||||
}
|
||||
|
||||
protected Fragment getFragmentForPreferenceFragment(PreferenceFragmentCompat caller,
|
||||
Preference pref) {
|
||||
FragmentActivity activity = requireActivity();
|
||||
|
||||
Fragment fragment = activity.getSupportFragmentManager().getFragmentFactory()
|
||||
.instantiate(activity.getClassLoader(), pref.getFragment());
|
||||
|
||||
Bundle args = new Bundle();
|
||||
args.putString(PreferenceFragmentCompat.ARG_PREFERENCE_ROOT, key);
|
||||
args.putString(PreferenceFragmentCompat.ARG_PREFERENCE_ROOT, pref.getKey());
|
||||
fragment.setArguments(args);
|
||||
|
||||
getParentFragmentManager()
|
||||
return fragment;
|
||||
}
|
||||
|
||||
protected boolean switchToFragment(Fragment fragment, String key) {
|
||||
if (fragment == null) return false;
|
||||
|
||||
requireActivity().getSupportFragmentManager()
|
||||
.beginTransaction()
|
||||
.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left,
|
||||
R.anim.enter_from_left, R.anim.exit_to_right)
|
||||
@@ -86,10 +122,6 @@ public abstract class BaseSettingsFragment extends PreferenceFragmentCompat
|
||||
return true;
|
||||
}
|
||||
|
||||
protected PreferenceFragmentCompat getSubscreenFragment(String key) {
|
||||
return null;
|
||||
}
|
||||
|
||||
protected void setPrefChangeListener(@NonNull CharSequence key,
|
||||
Preference.OnPreferenceChangeListener listener) {
|
||||
requirePreference(key).setOnPreferenceChangeListener(listener);
|
||||
|
@@ -9,7 +9,6 @@ import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceFragmentCompat;
|
||||
import androidx.preference.SwitchPreferenceCompat;
|
||||
|
||||
import dummydomain.yetanothercallblocker.utils.PackageManagerUtils;
|
||||
@@ -24,7 +23,6 @@ public class RootSettingsFragment extends BaseSettingsFragment {
|
||||
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 STATE_REQUEST_TOKEN = "STATE_REQUEST_TOKEN";
|
||||
|
||||
@@ -89,6 +87,11 @@ public class RootSettingsFragment extends BaseSettingsFragment {
|
||||
return PREF_SCREEN_ROOT;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getPreferencesResId() {
|
||||
return R.xml.root_preferences;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void initScreen() {
|
||||
setPrefChangeListener(Settings.PREF_INCOMING_CALL_NOTIFICATIONS, (pref, newValue) -> {
|
||||
@@ -217,9 +220,4 @@ public class RootSettingsFragment extends BaseSettingsFragment {
|
||||
.setChecked(App.getSettings().getNotificationsForBlockedCalls());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected PreferenceFragmentCompat getSubscreenFragment(String key) {
|
||||
return PREF_SCREEN_ADVANCED.equals(key) ? new AdvancedSettingsFragment() : null;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -4,12 +4,15 @@ import android.os.Bundle;
|
||||
|
||||
import androidx.appcompat.app.ActionBar;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.core.util.Predicate;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceFragmentCompat;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
|
||||
public class SettingsActivity extends AppCompatActivity
|
||||
implements PreferenceFragmentCompat.OnPreferenceStartScreenCallback {
|
||||
implements PreferenceFragmentCompat.OnPreferenceStartScreenCallback,
|
||||
PreferenceFragmentCompat.OnPreferenceStartFragmentCallback {
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
@@ -30,18 +33,25 @@ public class SettingsActivity extends AppCompatActivity
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onPreferenceStartScreen(PreferenceFragmentCompat preferenceFragmentCompat,
|
||||
PreferenceScreen preferenceScreen) {
|
||||
for (Fragment fragment : getSupportFragmentManager().getFragments()) {
|
||||
if (fragment instanceof BaseSettingsFragment) {
|
||||
if (((BaseSettingsFragment) fragment)
|
||||
.onPreferenceStartScreen(preferenceFragmentCompat, preferenceScreen)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
public boolean onPreferenceStartScreen(PreferenceFragmentCompat caller, PreferenceScreen pref) {
|
||||
return applyToBaseSettingsFragment(f -> f.onPreferenceStartScreen(caller, pref));
|
||||
}
|
||||
|
||||
return true;
|
||||
@Override
|
||||
public boolean onPreferenceStartFragment(PreferenceFragmentCompat caller, Preference pref) {
|
||||
return applyToBaseSettingsFragment(f -> f.onPreferenceStartFragment(caller, pref));
|
||||
}
|
||||
|
||||
private boolean applyToBaseSettingsFragment(Predicate<BaseSettingsFragment> predicate) {
|
||||
return applyToFragments(f -> f instanceof BaseSettingsFragment
|
||||
&& predicate.test((BaseSettingsFragment) f));
|
||||
}
|
||||
|
||||
private boolean applyToFragments(Predicate<Fragment> predicate) {
|
||||
for (Fragment fragment : getSupportFragmentManager().getFragments()) {
|
||||
if (predicate.test(fragment)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
56
app/src/main/res/xml/advanced_preferences.xml
Normal file
56
app/src/main/res/xml/advanced_preferences.xml
Normal file
@@ -0,0 +1,56 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
app:key="screenAdvanced"
|
||||
app:title="@string/settings_screen_advanced">
|
||||
|
||||
<Preference
|
||||
app:key="dbManagement"
|
||||
app:persistent="false"
|
||||
app:title="@string/open_db_management_activity">
|
||||
<intent
|
||||
android:targetClass="dummydomain.yetanothercallblocker.DbManagementActivity"
|
||||
android:targetPackage="@string/app_id" />
|
||||
</Preference>
|
||||
|
||||
<PreferenceCategory
|
||||
app:key="categoryCountryCodes"
|
||||
app:title="@string/settings_category_country_codes">
|
||||
<Preference
|
||||
app:key="countryCodesInfo"
|
||||
app:persistent="false"
|
||||
app:summary="@string/country_codes_info_summary"
|
||||
app:title="@string/country_codes_info" />
|
||||
<EditTextPreference
|
||||
app:key="countryCodeOverride"
|
||||
app:summary="@string/country_code_override_summary"
|
||||
app:title="@string/country_code_override" />
|
||||
<EditTextPreference
|
||||
app:key="countryCodeForReviewsOverride"
|
||||
app:summary="@string/country_code_for_reviews_override_summary"
|
||||
app:title="@string/country_code_for_reviews_override" />
|
||||
</PreferenceCategory>
|
||||
|
||||
<EditTextPreference
|
||||
app:key="databaseDownloadUrl"
|
||||
app:title="@string/database_download_url" />
|
||||
|
||||
<PreferenceCategory
|
||||
app:key="categoryDebugging"
|
||||
app:title="@string/settings_category_debugging">
|
||||
<Preference
|
||||
app:key="exportLogcat"
|
||||
app:persistent="false"
|
||||
app:summary="@string/export_logcat_summary"
|
||||
app:title="@string/export_logcat" />
|
||||
<SwitchPreferenceCompat
|
||||
app:key="saveCrashesToExternalStorage"
|
||||
app:summary="@string/save_crashes_to_external_storage_summary"
|
||||
app:title="@string/save_crashes_to_external_storage" />
|
||||
<SwitchPreferenceCompat
|
||||
app:key="saveLogcatOnCrash"
|
||||
app:summary="@string/save_logcat_on_crash_summary"
|
||||
app:title="@string/save_logcat_on_crash" />
|
||||
</PreferenceCategory>
|
||||
|
||||
</PreferenceScreen>
|
@@ -1,5 +1,4 @@
|
||||
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
app:title="@string/title_settings_activity">
|
||||
|
||||
<PreferenceCategory app:title="@string/settings_category_main">
|
||||
@@ -87,55 +86,10 @@
|
||||
</PreferenceCategory>
|
||||
|
||||
<PreferenceCategory app:title="@string/settings_screen_advanced">
|
||||
<PreferenceScreen
|
||||
<Preference
|
||||
app:fragment="dummydomain.yetanothercallblocker.AdvancedSettingsFragment"
|
||||
app:key="screenAdvanced"
|
||||
app:title="@string/settings_screen_advanced">
|
||||
<Preference
|
||||
app:key="dbManagement"
|
||||
app:persistent="false"
|
||||
app:title="@string/open_db_management_activity">
|
||||
<intent
|
||||
android:targetClass="dummydomain.yetanothercallblocker.DbManagementActivity"
|
||||
android:targetPackage="@string/app_id" />
|
||||
</Preference>
|
||||
<PreferenceCategory
|
||||
app:key="categoryCountryCodes"
|
||||
app:title="@string/settings_category_country_codes">
|
||||
<Preference
|
||||
app:key="countryCodesInfo"
|
||||
app:persistent="false"
|
||||
app:summary="@string/country_codes_info_summary"
|
||||
app:title="@string/country_codes_info" />
|
||||
<EditTextPreference
|
||||
app:key="countryCodeOverride"
|
||||
app:summary="@string/country_code_override_summary"
|
||||
app:title="@string/country_code_override" />
|
||||
<EditTextPreference
|
||||
app:key="countryCodeForReviewsOverride"
|
||||
app:summary="@string/country_code_for_reviews_override_summary"
|
||||
app:title="@string/country_code_for_reviews_override" />
|
||||
</PreferenceCategory>
|
||||
<EditTextPreference
|
||||
app:key="databaseDownloadUrl"
|
||||
app:title="@string/database_download_url" />
|
||||
<PreferenceCategory
|
||||
app:key="categoryDebugging"
|
||||
app:title="@string/settings_category_debugging">
|
||||
<Preference
|
||||
app:key="exportLogcat"
|
||||
app:persistent="false"
|
||||
app:summary="@string/export_logcat_summary"
|
||||
app:title="@string/export_logcat" />
|
||||
<SwitchPreferenceCompat
|
||||
app:key="saveCrashesToExternalStorage"
|
||||
app:summary="@string/save_crashes_to_external_storage_summary"
|
||||
app:title="@string/save_crashes_to_external_storage" />
|
||||
<SwitchPreferenceCompat
|
||||
app:key="saveLogcatOnCrash"
|
||||
app:summary="@string/save_logcat_on_crash_summary"
|
||||
app:title="@string/save_logcat_on_crash" />
|
||||
</PreferenceCategory>
|
||||
</PreferenceScreen>
|
||||
app:title="@string/settings_screen_advanced" />
|
||||
</PreferenceCategory>
|
||||
|
||||
</PreferenceScreen>
|
||||
|
Reference in New Issue
Block a user