From f08c08005b38e3b6aff83442bd58c66e3ae8c9e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Rebelo?= Date: Mon, 24 Jul 2023 23:57:54 +0100 Subject: [PATCH] Upgrade about user preferences to androidx --- .../AboutUserPreferencesActivity.java | 122 ++++++++++++------ .../activities/AbstractSettingsActivity.java | 22 ---- app/src/main/res/xml/about_user.xml | 39 ++++-- 3 files changed, 110 insertions(+), 73 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AboutUserPreferencesActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AboutUserPreferencesActivity.java index efb7fbf1e..7c24f760a 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AboutUserPreferencesActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AboutUserPreferencesActivity.java @@ -1,5 +1,5 @@ -/* Copyright (C) 2015-2020 Andreas Shimokawa, Carsten Pfeiffer, Lem Dulfo, - vanous +/* Copyright (C) 2015-2023 Andreas Shimokawa, Carsten Pfeiffer, Lem Dulfo, + vanous, José Rebelo This file is part of Gadgetbridge. @@ -32,50 +32,96 @@ import static nodomain.freeyourgadget.gadgetbridge.model.ActivityUser.PREF_USER_ import static nodomain.freeyourgadget.gadgetbridge.model.ActivityUser.PREF_USER_WEIGHT_KG; import static nodomain.freeyourgadget.gadgetbridge.model.ActivityUser.PREF_USER_YEAR_OF_BIRTH; +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 nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.R; +import nodomain.freeyourgadget.gadgetbridge.activities.charts.AbstractPreferenceFragment; +import nodomain.freeyourgadget.gadgetbridge.devices.DeviceManager; -public class AboutUserPreferencesActivity extends AbstractSettingsActivity { +public class AboutUserPreferencesActivity extends AbstractGBActivity { @Override - protected void onCreate(Bundle savedInstanceState) { + protected void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); - addPreferencesFromResource(R.xml.about_user); - addPreferenceHandlerFor(PREF_USER_NAME); - addPreferenceHandlerFor(PREF_USER_YEAR_OF_BIRTH); - addPreferenceHandlerFor(PREF_USER_HEIGHT_CM); - addPreferenceHandlerFor(PREF_USER_WEIGHT_KG); - addPreferenceHandlerFor(PREF_USER_GENDER); - addPreferenceHandlerFor(PREF_USER_STEPS_GOAL); - addPreferenceHandlerFor(PREF_USER_GOAL_WEIGHT_KG); - addPreferenceHandlerFor(PREF_USER_GOAL_STANDING_TIME_HOURS); - addPreferenceHandlerFor(PREF_USER_GOAL_FAT_BURN_TIME_MINUTES); + setContentView(R.layout.activity_device_settings); - addIntentNotificationListener(PREF_USER_STEPS_GOAL); - addIntentNotificationListener(PREF_USER_HEIGHT_CM); - addIntentNotificationListener(PREF_USER_SLEEP_DURATION); - addIntentNotificationListener(PREF_USER_STEP_LENGTH_CM); - addIntentNotificationListener(PREF_USER_DISTANCE_METERS); - addIntentNotificationListener(PREF_USER_GOAL_WEIGHT_KG); - addIntentNotificationListener(PREF_USER_GOAL_STANDING_TIME_HOURS); - addIntentNotificationListener(PREF_USER_GOAL_FAT_BURN_TIME_MINUTES); + 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 String[] getPreferenceKeysWithSummary() { - return new String[]{ - PREF_USER_YEAR_OF_BIRTH, - PREF_USER_HEIGHT_CM, - PREF_USER_WEIGHT_KG, - PREF_USER_SLEEP_DURATION, - PREF_USER_STEPS_GOAL, - PREF_USER_STEP_LENGTH_CM, - PREF_USER_ACTIVETIME_MINUTES, - PREF_USER_CALORIES_BURNT, - PREF_USER_DISTANCE_METERS, - PREF_USER_GOAL_WEIGHT_KG, - PREF_USER_GOAL_STANDING_TIME_HOURS, - PREF_USER_GOAL_FAT_BURN_TIME_MINUTES - }; + public static class AboutUserPreferencesFragment extends AbstractPreferenceFragment { + static final String FRAGMENT_TAG = "ABOUT_USER_PREFERENCES_FRAGMENT"; + + @Override + public void onCreatePreferences(final Bundle savedInstanceState, final String rootKey) { + addPreferencesFromResource(R.xml.about_user); + + addPreferenceHandlerFor(PREF_USER_NAME, true, false); + addPreferenceHandlerFor(PREF_USER_YEAR_OF_BIRTH, true, false); + addPreferenceHandlerFor(PREF_USER_HEIGHT_CM, true, true); + addPreferenceHandlerFor(PREF_USER_WEIGHT_KG, true, false); + addPreferenceHandlerFor(PREF_USER_GENDER, true, false); + addPreferenceHandlerFor(PREF_USER_STEPS_GOAL, true, true); + addPreferenceHandlerFor(PREF_USER_GOAL_WEIGHT_KG, true, true); + addPreferenceHandlerFor(PREF_USER_GOAL_STANDING_TIME_HOURS, true, true); + addPreferenceHandlerFor(PREF_USER_GOAL_FAT_BURN_TIME_MINUTES, true, true); + addPreferenceHandlerFor(PREF_USER_SLEEP_DURATION, false, true); + addPreferenceHandlerFor(PREF_USER_STEP_LENGTH_CM, false, true); + addPreferenceHandlerFor(PREF_USER_DISTANCE_METERS, false, true); + + setInputTypeFor(PREF_USER_YEAR_OF_BIRTH, InputType.TYPE_CLASS_NUMBER); + setInputTypeFor(PREF_USER_HEIGHT_CM, InputType.TYPE_CLASS_NUMBER); + setInputTypeFor(PREF_USER_WEIGHT_KG, InputType.TYPE_CLASS_NUMBER); + setInputTypeFor(PREF_USER_STEPS_GOAL, InputType.TYPE_CLASS_NUMBER); + setInputTypeFor(PREF_USER_GOAL_WEIGHT_KG, InputType.TYPE_CLASS_NUMBER); + setInputTypeFor(PREF_USER_GOAL_STANDING_TIME_HOURS, InputType.TYPE_CLASS_NUMBER); + setInputTypeFor(PREF_USER_GOAL_FAT_BURN_TIME_MINUTES, InputType.TYPE_CLASS_NUMBER); + setInputTypeFor(PREF_USER_SLEEP_DURATION, InputType.TYPE_CLASS_NUMBER); + setInputTypeFor(PREF_USER_CALORIES_BURNT, InputType.TYPE_CLASS_NUMBER); + setInputTypeFor(PREF_USER_ACTIVETIME_MINUTES, InputType.TYPE_CLASS_NUMBER); + setInputTypeFor(PREF_USER_STEP_LENGTH_CM, InputType.TYPE_CLASS_NUMBER); + setInputTypeFor(PREF_USER_DISTANCE_METERS, InputType.TYPE_CLASS_NUMBER); + } + + /** + * @param prefKey the pref key that chagned + * @param sendToDevice notify all device support classes of the preference change + * @param refreshDeviceList Ensure that the Control center is re-rendered when user preferences change + */ + private void addPreferenceHandlerFor(final String prefKey, + final boolean sendToDevice, + final boolean refreshDeviceList) { + final Preference pref = findPreference(prefKey); + if (pref == null) { + LOG.warn("Could not find preference {}", prefKey); + return; + } + + pref.setOnPreferenceChangeListener((preference, newVal) -> { + if (sendToDevice) { + GBApplication.deviceService().onSendConfiguration(prefKey); + } + if (refreshDeviceList) { + final Intent refreshIntent = new Intent(DeviceManager.ACTION_REFRESH_DEVICELIST); + LocalBroadcastManager.getInstance(requireActivity().getApplicationContext()).sendBroadcast(refreshIntent); + return true; + } + return true; + }); + } } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AbstractSettingsActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AbstractSettingsActivity.java index 9000aac48..fda471066 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AbstractSettingsActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AbstractSettingsActivity.java @@ -244,26 +244,4 @@ public abstract class AbstractSettingsActivity extends AppCompatPreferenceActivi LOG.warn("Could not find preference " + preferenceKey); } } - - // Ensure that the Control center is re-rendered when user preferences change - protected void addIntentNotificationListener(final String preferenceKey) { - Preference pref = findPreference(preferenceKey); - - if (pref != null) { - pref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { - @Override - public boolean onPreferenceChange(Preference preference, Object newVal) { - Intent refreshIntent = new Intent(DeviceManager.ACTION_REFRESH_DEVICELIST); - LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(refreshIntent); - return true; - } - }); - } else { - LOG.warn("Could not find preference " + preferenceKey); - } - } - - - - } diff --git a/app/src/main/res/xml/about_user.xml b/app/src/main/res/xml/about_user.xml index 81bfd1870..b20bedae6 100644 --- a/app/src/main/res/xml/about_user.xml +++ b/app/src/main/res/xml/about_user.xml @@ -1,5 +1,6 @@ - + @@ -8,7 +9,8 @@ android:inputType="number" android:key="activity_user_year_of_birth" android:maxLength="4" - android:title="@string/activity_prefs_year_birth" /> + android:title="@string/activity_prefs_year_birth" + app:useSimpleSummaryProvider="true" /> + android:title="@string/activity_prefs_height_cm" + app:useSimpleSummaryProvider="true" /> + android:title="@string/activity_prefs_weight_kg" + app:useSimpleSummaryProvider="true" /> + android:title="@string/activity_prefs_target_weight_kg" + app:useSimpleSummaryProvider="true" /> + android:title="@string/activity_prefs_step_length_cm" + app:useSimpleSummaryProvider="true" /> + android:title="@string/miband_prefs_fitness_goal" + app:useSimpleSummaryProvider="true" /> + android:title="@string/activity_prefs_sleep_duration" + app:useSimpleSummaryProvider="true" /> + android:title="@string/activity_prefs_calories_burnt" + app:useSimpleSummaryProvider="true" /> + android:title="@string/activity_prefs_distance_meters" + app:useSimpleSummaryProvider="true" /> + android:title="@string/activity_prefs_activetime_minutes" + app:useSimpleSummaryProvider="true" /> + android:title="@string/activity_prefs_goal_standing_time_minutes" + app:useSimpleSummaryProvider="true" /> + android:title="@string/activity_prefs_goal_fat_burn_time_minutes" + app:useSimpleSummaryProvider="true" />