mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge
synced 2024-11-29 13:26:50 +01:00
Pull reusable preferences logic to AbstractPreferenceFragment
This commit is contained in:
parent
dd91716680
commit
116703adff
@ -0,0 +1,185 @@
|
||||
/* Copyright (C) 2019-2023 Andreas Shimokawa, Cre3per, José Rebelo
|
||||
|
||||
This file is part of Gadgetbridge.
|
||||
|
||||
Gadgetbridge is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as published
|
||||
by the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
Gadgetbridge is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
package nodomain.freeyourgadget.gadgetbridge.activities.charts;
|
||||
|
||||
import android.content.SharedPreferences;
|
||||
import android.os.Bundle;
|
||||
|
||||
import androidx.fragment.app.DialogFragment;
|
||||
import androidx.preference.EditTextPreference;
|
||||
import androidx.preference.ListPreference;
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceCategory;
|
||||
import androidx.preference.PreferenceFragmentCompat;
|
||||
import androidx.preference.PreferenceGroup;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
import androidx.preference.SeekBarPreference;
|
||||
import androidx.preference.SwitchPreference;
|
||||
|
||||
import com.mobeta.android.dslv.DragSortListPreference;
|
||||
import com.mobeta.android.dslv.DragSortListPreferenceFragment;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Set;
|
||||
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.XTimePreference;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.XTimePreferenceFragment;
|
||||
|
||||
public abstract class AbstractPreferenceFragment extends PreferenceFragmentCompat {
|
||||
protected static final Logger LOG = LoggerFactory.getLogger(AbstractPreferenceFragment.class);
|
||||
|
||||
private final SharedPreferencesChangeHandler sharedPreferencesChangeHandler = new SharedPreferencesChangeHandler();
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
super.onStart();
|
||||
|
||||
final SharedPreferences sharedPreferences = getPreferenceManager().getSharedPreferences();
|
||||
|
||||
reloadPreferences(sharedPreferences, getPreferenceScreen());
|
||||
|
||||
sharedPreferences.registerOnSharedPreferenceChangeListener(sharedPreferencesChangeHandler);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStop() {
|
||||
getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(sharedPreferencesChangeHandler);
|
||||
|
||||
super.onStop();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisplayPreferenceDialog(final Preference preference) {
|
||||
DialogFragment dialogFragment;
|
||||
if (preference instanceof XTimePreference) {
|
||||
dialogFragment = new XTimePreferenceFragment();
|
||||
final Bundle bundle = new Bundle(1);
|
||||
bundle.putString("key", preference.getKey());
|
||||
dialogFragment.setArguments(bundle);
|
||||
dialogFragment.setTargetFragment(this, 0);
|
||||
if (getFragmentManager() != null) {
|
||||
dialogFragment.show(getFragmentManager(), "androidx.preference.PreferenceFragment.DIALOG");
|
||||
}
|
||||
} else if (preference instanceof DragSortListPreference) {
|
||||
dialogFragment = new DragSortListPreferenceFragment();
|
||||
final Bundle bundle = new Bundle(1);
|
||||
bundle.putString("key", preference.getKey());
|
||||
dialogFragment.setArguments(bundle);
|
||||
dialogFragment.setTargetFragment(this, 0);
|
||||
if (getFragmentManager() != null) {
|
||||
dialogFragment.show(getFragmentManager(), "androidx.preference.PreferenceFragment.DIALOG");
|
||||
}
|
||||
} else {
|
||||
super.onDisplayPreferenceDialog(preference);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Keys of preferences which should print its values as a summary below the preference name.
|
||||
*/
|
||||
protected Set<String> getPreferenceKeysWithSummary() {
|
||||
return Collections.emptySet();
|
||||
}
|
||||
|
||||
protected void onSharedPreferenceChanged(final Preference preference) {
|
||||
// Nothing to do
|
||||
}
|
||||
|
||||
public void setInputTypeFor(final String preferenceKey, final int editTypeFlags) {
|
||||
final EditTextPreference textPreference = findPreference(preferenceKey);
|
||||
if (textPreference != null) {
|
||||
textPreference.setOnBindEditTextListener(editText -> editText.setInputType(editTypeFlags));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reload the preferences in the current screen. This is needed when the user enters or exists a PreferenceScreen,
|
||||
* otherwise the settings won't be reloaded by the {@link SharedPreferencesChangeHandler}, as the preferences return
|
||||
* null, since they're not visible.
|
||||
*
|
||||
* @param sharedPreferences the {@link SharedPreferences} instance
|
||||
* @param preferenceGroup the {@link PreferenceGroup} for which preferences will be reloaded
|
||||
*/
|
||||
private void reloadPreferences(final SharedPreferences sharedPreferences, final PreferenceGroup preferenceGroup) {
|
||||
if (preferenceGroup == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < preferenceGroup.getPreferenceCount(); i++) {
|
||||
final Preference preference = preferenceGroup.getPreference(i);
|
||||
|
||||
LOG.debug("Reloading {}", preference.getKey());
|
||||
|
||||
if (preference instanceof PreferenceCategory) {
|
||||
reloadPreferences(sharedPreferences, (PreferenceCategory) preference);
|
||||
continue;
|
||||
}
|
||||
|
||||
sharedPreferencesChangeHandler.onSharedPreferenceChanged(sharedPreferences, preference.getKey());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handler for preference changes, update UI accordingly (if device updates the preferences).
|
||||
*/
|
||||
private class SharedPreferencesChangeHandler implements SharedPreferences.OnSharedPreferenceChangeListener {
|
||||
@Override
|
||||
public void onSharedPreferenceChanged(final SharedPreferences prefs, final String key) {
|
||||
LOG.debug("Preference changed: {}", key);
|
||||
|
||||
if (key == null) {
|
||||
LOG.warn("Preference null, ignoring");
|
||||
return;
|
||||
}
|
||||
|
||||
final Preference preference = findPreference(key);
|
||||
if (preference == null) {
|
||||
LOG.warn("Preference {} not found", key);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (preference instanceof SeekBarPreference) {
|
||||
final SeekBarPreference seekBarPreference = (SeekBarPreference) preference;
|
||||
seekBarPreference.setValue(prefs.getInt(key, seekBarPreference.getValue()));
|
||||
} else if (preference instanceof SwitchPreference) {
|
||||
final SwitchPreference switchPreference = (SwitchPreference) preference;
|
||||
switchPreference.setChecked(prefs.getBoolean(key, switchPreference.isChecked()));
|
||||
} else if (preference instanceof ListPreference) {
|
||||
final ListPreference listPreference = (ListPreference) preference;
|
||||
listPreference.setValue(prefs.getString(key, listPreference.getValue()));
|
||||
} else if (preference instanceof EditTextPreference) {
|
||||
final EditTextPreference editTextPreference = (EditTextPreference) preference;
|
||||
editTextPreference.setText(prefs.getString(key, editTextPreference.getText()));
|
||||
} else if (preference instanceof PreferenceScreen) {
|
||||
// Ignoring
|
||||
} else {
|
||||
LOG.warn("Unknown preference class {} for {}, ignoring", preference.getClass(), key);
|
||||
}
|
||||
|
||||
if (getPreferenceKeysWithSummary().contains(key)) {
|
||||
final String summary = prefs.getString(key, preference.getSummary() != null ? preference.getSummary().toString() : "");
|
||||
preference.setSummary(summary);
|
||||
}
|
||||
|
||||
AbstractPreferenceFragment.this.onSharedPreferenceChanged(preference);
|
||||
}
|
||||
}
|
||||
}
|
@ -17,26 +17,13 @@
|
||||
package nodomain.freeyourgadget.gadgetbridge.activities.devicesettings;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.os.Bundle;
|
||||
import android.text.InputType;
|
||||
import android.widget.EditText;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.fragment.app.DialogFragment;
|
||||
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
|
||||
import androidx.preference.EditTextPreference;
|
||||
import androidx.preference.ListPreference;
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceCategory;
|
||||
import androidx.preference.PreferenceFragmentCompat;
|
||||
import androidx.preference.PreferenceGroup;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
import androidx.preference.SeekBarPreference;
|
||||
import androidx.preference.SwitchPreference;
|
||||
|
||||
import com.mobeta.android.dslv.DragSortListPreference;
|
||||
import com.mobeta.android.dslv.DragSortListPreferenceFragment;
|
||||
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
@ -53,6 +40,7 @@ import nodomain.freeyourgadget.gadgetbridge.R;
|
||||
import nodomain.freeyourgadget.gadgetbridge.activities.CalBlacklistActivity;
|
||||
import nodomain.freeyourgadget.gadgetbridge.activities.ConfigureContacts;
|
||||
import nodomain.freeyourgadget.gadgetbridge.activities.ConfigureWorldClocks;
|
||||
import nodomain.freeyourgadget.gadgetbridge.activities.charts.AbstractPreferenceFragment;
|
||||
import nodomain.freeyourgadget.gadgetbridge.activities.loyaltycards.LoyaltyCardsSettingsActivity;
|
||||
import nodomain.freeyourgadget.gadgetbridge.activities.loyaltycards.LoyaltyCardsSettingsConst;
|
||||
import nodomain.freeyourgadget.gadgetbridge.capabilities.HeartRateCapability;
|
||||
@ -66,8 +54,6 @@ import nodomain.freeyourgadget.gadgetbridge.model.CannedMessagesSpec;
|
||||
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.XTimePreference;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.XTimePreferenceFragment;
|
||||
|
||||
import static nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst.*;
|
||||
import static nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiConst.PREF_CONTROL_CENTER_SORTABLE;
|
||||
@ -94,14 +80,12 @@ import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PR
|
||||
import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PREF_NIGHT_MODE_START;
|
||||
import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PREF_SWIPE_UNLOCK;
|
||||
|
||||
public class DeviceSpecificSettingsFragment extends PreferenceFragmentCompat implements DeviceSpecificSettingsHandler {
|
||||
public class DeviceSpecificSettingsFragment extends AbstractPreferenceFragment implements DeviceSpecificSettingsHandler {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(DeviceSpecificSettingsFragment.class);
|
||||
|
||||
static final String FRAGMENT_TAG = "DEVICE_SPECIFIC_SETTINGS_FRAGMENT";
|
||||
|
||||
private final SharedPreferencesChangeHandler sharedPreferencesChangeHandler = new SharedPreferencesChangeHandler();
|
||||
|
||||
private DeviceSpecificSettingsCustomizer deviceSpecificSettingsCustomizer;
|
||||
|
||||
private GBDevice device;
|
||||
@ -182,24 +166,6 @@ public class DeviceSpecificSettingsFragment extends PreferenceFragmentCompat imp
|
||||
setChangeListener();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
super.onStart();
|
||||
|
||||
final SharedPreferences sharedPreferences = getPreferenceManager().getSharedPreferences();
|
||||
|
||||
reloadPreferences(sharedPreferences, getPreferenceScreen());
|
||||
|
||||
sharedPreferences.registerOnSharedPreferenceChangeListener(sharedPreferencesChangeHandler);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStop() {
|
||||
getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(sharedPreferencesChangeHandler);
|
||||
|
||||
super.onStop();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
@ -999,32 +965,6 @@ public class DeviceSpecificSettingsFragment extends PreferenceFragmentCompat imp
|
||||
return fragment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisplayPreferenceDialog(Preference preference) {
|
||||
DialogFragment dialogFragment;
|
||||
if (preference instanceof XTimePreference) {
|
||||
dialogFragment = new XTimePreferenceFragment();
|
||||
Bundle bundle = new Bundle(1);
|
||||
bundle.putString("key", preference.getKey());
|
||||
dialogFragment.setArguments(bundle);
|
||||
dialogFragment.setTargetFragment(this, 0);
|
||||
if (getFragmentManager() != null) {
|
||||
dialogFragment.show(getFragmentManager(), "androidx.preference.PreferenceFragment.DIALOG");
|
||||
}
|
||||
} else if (preference instanceof DragSortListPreference) {
|
||||
dialogFragment = new DragSortListPreferenceFragment();
|
||||
Bundle bundle = new Bundle(1);
|
||||
bundle.putString("key", preference.getKey());
|
||||
dialogFragment.setArguments(bundle);
|
||||
dialogFragment.setTargetFragment(this, 0);
|
||||
if (getFragmentManager() != null) {
|
||||
dialogFragment.show(getFragmentManager(), "androidx.preference.PreferenceFragment.DIALOG");
|
||||
}
|
||||
} else {
|
||||
super.onDisplayPreferenceDialog(preference);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addPreferenceHandlerFor(final String preferenceKey) {
|
||||
addPreferenceHandlerFor(preferenceKey, null);
|
||||
@ -1048,27 +988,12 @@ public class DeviceSpecificSettingsFragment extends PreferenceFragmentCompat imp
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setInputTypeFor(final String preferenceKey, final int editTypeFlags) {
|
||||
EditTextPreference textPreference = findPreference(preferenceKey);
|
||||
if (textPreference != null) {
|
||||
textPreference.setOnBindEditTextListener(new EditTextPreference.OnBindEditTextListener() {
|
||||
@Override
|
||||
public void onBindEditText(@NonNull EditText editText) {
|
||||
editText.setInputType(editTypeFlags);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public GBDevice getDevice() {
|
||||
return device;
|
||||
}
|
||||
|
||||
/**
|
||||
* Keys of preferences which should print its values as a summary below the preference name.
|
||||
*/
|
||||
@Override
|
||||
protected Set<String> getPreferenceKeysWithSummary() {
|
||||
final Set<String> keysWithSummary = new HashSet<>();
|
||||
|
||||
@ -1081,79 +1006,10 @@ public class DeviceSpecificSettingsFragment extends PreferenceFragmentCompat imp
|
||||
return keysWithSummary;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reload the preferences in the current screen. This is needed when the user enters or exists a PreferenceScreen,
|
||||
* otherwise the settings won't be reloaded by the {@link SharedPreferencesChangeHandler}, as the preferences return
|
||||
* null, since they're not visible.
|
||||
*
|
||||
* @param sharedPreferences the {@link SharedPreferences} instance
|
||||
* @param preferenceGroup the {@link PreferenceGroup} for which preferences will be reloaded
|
||||
*/
|
||||
private void reloadPreferences(final SharedPreferences sharedPreferences, final PreferenceGroup preferenceGroup) {
|
||||
if (preferenceGroup == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < preferenceGroup.getPreferenceCount(); i++) {
|
||||
final Preference preference = preferenceGroup.getPreference(i);
|
||||
|
||||
LOG.debug("Reloading {}", preference.getKey());
|
||||
|
||||
if (preference instanceof PreferenceCategory) {
|
||||
reloadPreferences(sharedPreferences, (PreferenceCategory) preference);
|
||||
continue;
|
||||
}
|
||||
|
||||
sharedPreferencesChangeHandler.onSharedPreferenceChanged(sharedPreferences, preference.getKey());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handler for preference changes, update UI accordingly (if device updates the preferences).
|
||||
*/
|
||||
private class SharedPreferencesChangeHandler implements SharedPreferences.OnSharedPreferenceChangeListener {
|
||||
@Override
|
||||
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
|
||||
LOG.debug("Preference changed: {}", key);
|
||||
|
||||
if (key == null){
|
||||
LOG.warn("Preference null, ignoring");
|
||||
return;
|
||||
}
|
||||
|
||||
final Preference preference = findPreference(key);
|
||||
if (preference == null) {
|
||||
LOG.warn("Preference {} not found", key);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (preference instanceof SeekBarPreference) {
|
||||
final SeekBarPreference seekBarPreference = (SeekBarPreference) preference;
|
||||
seekBarPreference.setValue(prefs.getInt(key, seekBarPreference.getValue()));
|
||||
} else if (preference instanceof SwitchPreference) {
|
||||
final SwitchPreference switchPreference = (SwitchPreference) preference;
|
||||
switchPreference.setChecked(prefs.getBoolean(key, switchPreference.isChecked()));
|
||||
} else if (preference instanceof ListPreference) {
|
||||
final ListPreference listPreference = (ListPreference) preference;
|
||||
listPreference.setValue(prefs.getString(key, listPreference.getValue()));
|
||||
} else if (preference instanceof EditTextPreference) {
|
||||
final EditTextPreference editTextPreference = (EditTextPreference) preference;
|
||||
editTextPreference.setText(prefs.getString(key, editTextPreference.getText()));
|
||||
} else if (preference instanceof PreferenceScreen) {
|
||||
// Ignoring
|
||||
} else {
|
||||
LOG.warn("Unknown preference class {} for {}, ignoring", preference.getClass(), key);
|
||||
}
|
||||
|
||||
if (getPreferenceKeysWithSummary().contains(key)) {
|
||||
final String summary = prefs.getString(key, preference.getSummary() != null ? preference.getSummary().toString() : "");
|
||||
preference.setSummary(summary);
|
||||
}
|
||||
|
||||
protected void onSharedPreferenceChanged(Preference preference) {
|
||||
if (deviceSpecificSettingsCustomizer != null) {
|
||||
deviceSpecificSettingsCustomizer.onPreferenceChange(preference, DeviceSpecificSettingsFragment.this);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user