1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-12-01 14:32:54 +01:00

Introduce AbstractSettingsActivityV2

This class introduces some of the common logic across preference
screens, handling nested PreferenceScreens, as well as the back button
and action bar title setting.
This commit is contained in:
José Rebelo 2023-07-26 23:08:13 +01:00 committed by José Rebelo
parent 597373f126
commit f4b059f173
11 changed files with 193 additions and 188 deletions

View File

@ -36,30 +36,23 @@ import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import androidx.fragment.app.Fragment;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceManager;
public class AboutUserPreferencesActivity extends AbstractGBActivity {
public class AboutUserPreferencesActivity extends AbstractSettingsActivityV2 {
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_device_settings);
protected String fragmentTag() {
return AboutUserPreferencesFragment.FRAGMENT_TAG;
}
if (savedInstanceState == null) {
Fragment fragment = getSupportFragmentManager().findFragmentByTag(AboutUserPreferencesFragment.FRAGMENT_TAG);
if (fragment == null) {
fragment = new AboutUserPreferencesFragment();
}
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.settings_container, fragment, AboutUserPreferencesFragment.FRAGMENT_TAG)
.commit();
}
@Override
protected PreferenceFragmentCompat newFragment() {
return new AboutUserPreferencesFragment();
}
public static class AboutUserPreferencesFragment extends AbstractPreferenceFragment {
@ -67,7 +60,7 @@ public class AboutUserPreferencesActivity extends AbstractGBActivity {
@Override
public void onCreatePreferences(final Bundle savedInstanceState, final String rootKey) {
addPreferencesFromResource(R.xml.about_user);
setPreferencesFromResource(R.xml.about_user, rootKey);
addPreferenceHandlerFor(PREF_USER_NAME, true, false);
addPreferenceHandlerFor(PREF_USER_YEAR_OF_BIRTH, true, false);

View File

@ -33,12 +33,15 @@ import androidx.preference.SwitchPreference;
import com.mobeta.android.dslv.DragSortListPreference;
import com.mobeta.android.dslv.DragSortListPreferenceFragment;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Collections;
import java.util.Set;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsActivity;
import nodomain.freeyourgadget.gadgetbridge.util.XTimePreference;
import nodomain.freeyourgadget.gadgetbridge.util.XTimePreferenceFragment;
@ -58,6 +61,25 @@ public abstract class AbstractPreferenceFragment extends PreferenceFragmentCompa
sharedPreferences.registerOnSharedPreferenceChangeListener(sharedPreferencesChangeHandler);
}
@Override
public void onResume() {
super.onResume();
updateActionBarTitle();
}
private void updateActionBarTitle() {
try {
CharSequence title = getPreferenceScreen().getTitle();
if (StringUtils.isBlank(title)) {
title = requireActivity().getTitle();
}
((AbstractSettingsActivityV2) requireActivity()).setActionBarTitle(title);
} catch (final Exception e) {
LOG.error("Failed to update action bar title", e);
}
}
@Override
public void onStop() {
getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(sharedPreferencesChangeHandler);

View File

@ -46,7 +46,10 @@ import nodomain.freeyourgadget.gadgetbridge.util.AndroidUtils;
* If you combine such preferences with a custom OnPreferenceChangeListener, you have
* to set that listener in #onCreate, *not* in #onPostCreate, otherwise the value will
* not be displayed.
*
* @deprecated use AbstractSettingsActivityV2
*/
@Deprecated
public abstract class AbstractSettingsActivity extends AppCompatPreferenceActivity implements GBActivity {
private static final Logger LOG = LoggerFactory.getLogger(AbstractSettingsActivity.class);

View File

@ -0,0 +1,91 @@
/* Copyright (C) 2019-2023 Andreas Shimokawa, 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;
import android.os.Bundle;
import android.view.MenuItem;
import androidx.appcompat.app.ActionBar;
import androidx.fragment.app.Fragment;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceScreen;
import nodomain.freeyourgadget.gadgetbridge.R;
public abstract class AbstractSettingsActivityV2 extends AbstractGBActivity implements
PreferenceFragmentCompat.OnPreferenceStartScreenCallback {
protected abstract String fragmentTag();
protected abstract PreferenceFragmentCompat newFragment();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_device_settings);
if (savedInstanceState == null) {
Fragment fragment = getSupportFragmentManager().findFragmentByTag(fragmentTag());
if (fragment == null) {
fragment = newFragment();
}
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.settings_container, fragment, fragmentTag())
.commit();
}
}
@Override
public boolean onPreferenceStartScreen(final PreferenceFragmentCompat caller, final PreferenceScreen preferenceScreen) {
final PreferenceFragmentCompat fragment = newFragment();
final Bundle args;
if (fragment.getArguments() != null) {
args = fragment.getArguments();
} else {
args = new Bundle();
}
args.putString(PreferenceFragmentCompat.ARG_PREFERENCE_ROOT, preferenceScreen.getKey());
fragment.setArguments(args);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.settings_container, fragment, preferenceScreen.getKey())
.addToBackStack(preferenceScreen.getKey())
.commit();
return true;
}
@Override
public boolean onOptionsItemSelected(final MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// Simulate a back press, so that we don't actually exit the activity when
// in a nested PreferenceScreen
this.onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
public void setActionBarTitle(final CharSequence title) {
final ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setTitle(title);
}
}
}

View File

@ -19,34 +19,26 @@ package nodomain.freeyourgadget.gadgetbridge.activities;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.preference.PreferenceFragmentCompat;
import nodomain.freeyourgadget.gadgetbridge.R;
public class DiscoveryPairingPreferenceActivity extends AbstractGBActivity {
public class DiscoveryPairingPreferenceActivity extends AbstractSettingsActivityV2 {
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_device_settings);
protected String fragmentTag() {
return DiscoveryPairingPreferenceFragment.FRAGMENT_TAG;
}
if (savedInstanceState == null) {
Fragment fragment = getSupportFragmentManager().findFragmentByTag(DiscoveryPairingPreferenceFragment.FRAGMENT_TAG);
if (fragment == null) {
fragment = new DiscoveryPairingPreferenceFragment();
}
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.settings_container, fragment, DiscoveryPairingPreferenceFragment.FRAGMENT_TAG)
.commit();
}
@Override
protected PreferenceFragmentCompat newFragment() {
return new DiscoveryPairingPreferenceFragment();
}
public static class DiscoveryPairingPreferenceFragment extends AbstractPreferenceFragment {
static final String FRAGMENT_TAG = "DISCOVERY_PAIRING_PREFERENCES_FRAGMENT";
public void onCreatePreferences(final Bundle savedInstanceState, final String rootKey) {
addPreferencesFromResource(R.xml.discovery_pairing_preferences);
setPreferencesFromResource(R.xml.discovery_pairing_preferences, rootKey);
}
}
}

View File

@ -26,6 +26,7 @@ import android.provider.Settings;
import androidx.fragment.app.Fragment;
import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceFragmentCompat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -35,26 +36,19 @@ import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.util.GBPrefs;
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
public class NotificationManagementActivity extends AbstractGBActivity {
public class NotificationManagementActivity extends AbstractSettingsActivityV2 {
private static final Logger LOG = LoggerFactory.getLogger(NotificationManagementActivity.class);
private static final int RINGTONE_REQUEST_CODE = 4712;
private static final String DEFAULT_RINGTONE_URI = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE).toString();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_device_settings);
protected String fragmentTag() {
return NotificationPreferencesFragment.FRAGMENT_TAG;
}
if (savedInstanceState == null) {
Fragment fragment = getSupportFragmentManager().findFragmentByTag(NotificationPreferencesFragment.FRAGMENT_TAG);
if (fragment == null) {
fragment = new NotificationPreferencesFragment();
}
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.settings_container, fragment, NotificationPreferencesFragment.FRAGMENT_TAG)
.commit();
}
@Override
protected PreferenceFragmentCompat newFragment() {
return new NotificationPreferencesFragment();
}
public static class NotificationPreferencesFragment extends AbstractPreferenceFragment {
@ -76,7 +70,7 @@ public class NotificationManagementActivity extends AbstractGBActivity {
@Override
public void onCreatePreferences(final Bundle savedInstanceState, final String rootKey) {
addPreferencesFromResource(R.xml.notifications_preferences);
setPreferencesFromResource(R.xml.notifications_preferences, rootKey);
Preference pref = findPreference("notifications_generic");
pref.setOnPreferenceClickListener(preference -> {

View File

@ -21,31 +21,24 @@ import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import androidx.fragment.app.Fragment;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.activities.AboutUserPreferencesActivity;
import nodomain.freeyourgadget.gadgetbridge.activities.AbstractGBActivity;
import nodomain.freeyourgadget.gadgetbridge.activities.AbstractPreferenceFragment;
import nodomain.freeyourgadget.gadgetbridge.activities.AbstractSettingsActivityV2;
import nodomain.freeyourgadget.gadgetbridge.util.GBPrefs;
public class ChartsPreferencesActivity extends AbstractGBActivity {
public class ChartsPreferencesActivity extends AbstractSettingsActivityV2 {
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_device_settings);
protected String fragmentTag() {
return ChartsPreferencesFragment.FRAGMENT_TAG;
}
if (savedInstanceState == null) {
Fragment fragment = getSupportFragmentManager().findFragmentByTag(ChartsPreferencesFragment.FRAGMENT_TAG);
if (fragment == null) {
fragment = new ChartsPreferencesFragment();
}
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.settings_container, fragment, ChartsPreferencesFragment.FRAGMENT_TAG)
.commit();
}
@Override
protected PreferenceFragmentCompat newFragment() {
return new ChartsPreferencesFragment();
}
public static class ChartsPreferencesFragment extends AbstractPreferenceFragment {
@ -53,7 +46,7 @@ public class ChartsPreferencesActivity extends AbstractGBActivity {
@Override
public void onCreatePreferences(final Bundle savedInstanceState, final String rootKey) {
addPreferencesFromResource(R.xml.charts_preferences);
setPreferencesFromResource(R.xml.charts_preferences, rootKey);
setInputTypeFor(GBPrefs.CHART_MAX_HEART_RATE, InputType.TYPE_CLASS_NUMBER);
setInputTypeFor(GBPrefs.CHART_MIN_HEART_RATE, InputType.TYPE_CLASS_NUMBER);

View File

@ -16,30 +16,14 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.activities.devicesettings;
import android.os.Bundle;
import android.view.MenuItem;
import androidx.appcompat.app.ActionBar;
import androidx.fragment.app.Fragment;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceScreen;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.activities.AbstractGBActivity;
import nodomain.freeyourgadget.gadgetbridge.activities.AbstractSettingsActivityV2;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
public class DeviceSettingsActivity extends AbstractGBActivity implements
PreferenceFragmentCompat.OnPreferenceStartScreenCallback {
private static final Logger LOG = LoggerFactory.getLogger(DeviceSettingsActivity.class);
public class DeviceSettingsActivity extends AbstractSettingsActivityV2 {
public static final String MENU_ENTRY_POINT = "MENU_ENTRY_POINT";
GBDevice device;
MENU_ENTRY_POINTS menu_entry;
public enum MENU_ENTRY_POINTS {
DEVICE_SETTINGS,
AUTH_SETTINGS,
@ -47,58 +31,15 @@ public class DeviceSettingsActivity extends AbstractGBActivity implements
}
@Override
protected void onCreate(Bundle savedInstanceState) {
device = getIntent().getParcelableExtra(GBDevice.EXTRA_DEVICE);
menu_entry = (MENU_ENTRY_POINTS) getIntent().getSerializableExtra(MENU_ENTRY_POINT);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_device_settings);
if (savedInstanceState == null) {
Fragment fragment = getSupportFragmentManager().findFragmentByTag(DeviceSpecificSettingsFragment.FRAGMENT_TAG);
if (fragment == null) {
fragment = DeviceSpecificSettingsFragment.newInstance(device, menu_entry);
}
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.settings_container, fragment, DeviceSpecificSettingsFragment.FRAGMENT_TAG)
.commit();
}
protected String fragmentTag() {
return DeviceSpecificSettingsFragment.FRAGMENT_TAG;
}
@Override
public boolean onPreferenceStartScreen(PreferenceFragmentCompat caller, PreferenceScreen preferenceScreen) {
final PreferenceFragmentCompat fragment = DeviceSpecificSettingsFragment.newInstance(device, menu_entry);
Bundle args = fragment.getArguments();
args.putString(PreferenceFragmentCompat.ARG_PREFERENCE_ROOT, preferenceScreen.getKey());
fragment.setArguments(args);
protected PreferenceFragmentCompat newFragment() {
final GBDevice device = getIntent().getParcelableExtra(GBDevice.EXTRA_DEVICE);
final MENU_ENTRY_POINTS menu_entry = (MENU_ENTRY_POINTS) getIntent().getSerializableExtra(MENU_ENTRY_POINT);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.settings_container, fragment, preferenceScreen.getKey())
.addToBackStack(preferenceScreen.getKey())
.commit();
return true;
}
public void setActionBarTitle(final CharSequence title) {
final ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setTitle(title);
}
}
@Override
public boolean onOptionsItemSelected(final MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// Simulate a back press, so that we don't actually exit the activity when
// in a nested PreferenceScreen
this.onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
return DeviceSpecificSettingsFragment.newInstance(device, menu_entry);
}
}

View File

@ -166,25 +166,6 @@ public class DeviceSpecificSettingsFragment extends AbstractPreferenceFragment i
setChangeListener();
}
@Override
public void onResume() {
super.onResume();
updateActionBarTitle();
}
private void updateActionBarTitle() {
try {
CharSequence title = getPreferenceScreen().getTitle();
if (StringUtils.isBlank(title)) {
title = getString(R.string.title_activity_device_specific_settings);
}
((DeviceSettingsActivity) requireActivity()).setActionBarTitle(title);
} catch (final Exception e) {
LOG.error("Failed to update action bar title", e);
}
}
/*
* delayed execution so that the preferences are applied first
*/

View File

@ -21,6 +21,7 @@ import android.os.Bundle;
import androidx.preference.EditTextPreference;
import androidx.preference.ListPreference;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.SwitchPreference;
import android.view.MenuItem;
@ -29,15 +30,23 @@ import androidx.fragment.app.Fragment;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.activities.AbstractGBActivity;
import nodomain.freeyourgadget.gadgetbridge.activities.AbstractPreferenceFragment;
import nodomain.freeyourgadget.gadgetbridge.activities.AbstractSettingsActivityV2;
public class HybridHRWatchfaceSettingsActivity extends AbstractGBActivity {
public class HybridHRWatchfaceSettingsActivity extends AbstractSettingsActivityV2 {
static HybridHRWatchfaceSettings settings;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_device_settings);
protected String fragmentTag() {
return HybridHRWatchfaceSettingsFragment.FRAGMENT_TAG;
}
@Override
protected PreferenceFragmentCompat newFragment() {
return new HybridHRWatchfaceSettingsFragment();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
if (bundle != null) {
@ -46,16 +55,7 @@ public class HybridHRWatchfaceSettingsActivity extends AbstractGBActivity {
throw new IllegalArgumentException("Must provide a settings object when invoking this activity");
}
if (savedInstanceState == null) {
Fragment fragment = getSupportFragmentManager().findFragmentByTag(HybridHRWatchfaceSettingsFragment.FRAGMENT_TAG);
if (fragment == null) {
fragment = new HybridHRWatchfaceSettingsFragment();
}
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.settings_container, fragment, HybridHRWatchfaceSettingsFragment.FRAGMENT_TAG)
.commit();
}
super.onCreate(savedInstanceState);
}
@Override
@ -86,7 +86,7 @@ public class HybridHRWatchfaceSettingsActivity extends AbstractGBActivity {
@Override
public void onCreatePreferences(final Bundle savedInstanceState, final String rootKey) {
addPreferencesFromResource(R.xml.fossil_hr_watchface_settings);
setPreferencesFromResource(R.xml.fossil_hr_watchface_settings, rootKey);
EditTextPreference refresh_full = (EditTextPreference) findPreference("pref_hybridhr_watchface_refresh_full");
refresh_full.setOnPreferenceChangeListener(this);

View File

@ -18,26 +18,23 @@ package nodomain.freeyourgadget.gadgetbridge.devices.qhybrid;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import androidx.preference.EditTextPreference;
import androidx.preference.ListPreference;
import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.SwitchPreference;
import android.view.MenuItem;
import androidx.fragment.app.Fragment;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Set;
import java.util.TimeZone;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.activities.AbstractGBActivity;
import nodomain.freeyourgadget.gadgetbridge.activities.AbstractPreferenceFragment;
import nodomain.freeyourgadget.gadgetbridge.activities.AbstractSettingsActivityV2;
public class HybridHRWatchfaceWidgetActivity extends AbstractGBActivity {
public class HybridHRWatchfaceWidgetActivity extends AbstractSettingsActivityV2 {
private static int widgetIndex;
private static HybridHRWatchfaceWidget widget;
@ -51,10 +48,17 @@ public class HybridHRWatchfaceWidgetActivity extends AbstractGBActivity {
private static final Boolean WIDGET_CUSTOM_DEFAULT_SHOW_CIRCLE = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_device_settings);
protected String fragmentTag() {
return HybridHRWatchfaceWidgetFragment.FRAGMENT_TAG;
}
@Override
protected PreferenceFragmentCompat newFragment() {
return new HybridHRWatchfaceWidgetFragment();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
if (bundle != null) {
@ -64,16 +68,7 @@ public class HybridHRWatchfaceWidgetActivity extends AbstractGBActivity {
throw new IllegalArgumentException("Must provide a widget object when invoking this activity");
}
if (savedInstanceState == null) {
Fragment fragment = getSupportFragmentManager().findFragmentByTag(HybridHRWatchfaceWidgetFragment.FRAGMENT_TAG);
if (fragment == null) {
fragment = new HybridHRWatchfaceWidgetFragment();
}
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.settings_container, fragment, HybridHRWatchfaceWidgetFragment.FRAGMENT_TAG)
.commit();
}
super.onCreate(savedInstanceState);
}
@Override
@ -106,7 +101,7 @@ public class HybridHRWatchfaceWidgetActivity extends AbstractGBActivity {
@Override
public void onCreatePreferences(final Bundle savedInstanceState, final String rootKey) {
addPreferencesFromResource(R.xml.fossil_hr_widget_settings);
setPreferencesFromResource(R.xml.fossil_hr_widget_settings, rootKey);
widgetTypes = HybridHRWatchfaceWidget.getAvailableWidgetTypes(requireActivity().getBaseContext());
ListPreference widgetType = (ListPreference) findPreference("pref_hybridhr_widget_type");