1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-29 05:16:51 +01:00

Upgrade about user preferences to androidx

This commit is contained in:
José Rebelo 2023-07-24 23:57:54 +01:00
parent cac41ea945
commit f08c08005b
3 changed files with 110 additions and 73 deletions

View File

@ -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;
});
}
}
}

View File

@ -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);
}
}
}

View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<PreferenceCategory
android:key="pref_category_activity_personal"
android:title="@string/activity_prefs_about_you">
@ -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" />
<ListPreference
android:defaultValue="2"
@ -23,72 +25,83 @@
android:inputType="number"
android:key="activity_user_height_cm"
android:maxLength="3"
android:title="@string/activity_prefs_height_cm" />
android:title="@string/activity_prefs_height_cm"
app:useSimpleSummaryProvider="true" />
<EditTextPreference
android:inputType="number"
android:key="activity_user_weight_kg"
android:maxLength="3"
android:title="@string/activity_prefs_weight_kg" />
android:title="@string/activity_prefs_weight_kg"
app:useSimpleSummaryProvider="true" />
<EditTextPreference
android:inputType="number"
android:key="activity_user_goal_weight_kg"
android:maxLength="3"
android:title="@string/activity_prefs_target_weight_kg" />
android:title="@string/activity_prefs_target_weight_kg"
app:useSimpleSummaryProvider="true" />
<EditTextPreference
android:inputType="number"
android:key="activity_user_step_length_cm"
android:maxLength="3"
android:title="@string/activity_prefs_step_length_cm" />
android:title="@string/activity_prefs_step_length_cm"
app:useSimpleSummaryProvider="true" />
<EditTextPreference
android:defaultValue="10000"
android:inputType="number"
android:key="fitness_goal"
android:maxLength="5"
android:title="@string/miband_prefs_fitness_goal" />
android:title="@string/miband_prefs_fitness_goal"
app:useSimpleSummaryProvider="true" />
<EditTextPreference
android:inputType="number"
android:key="activity_user_sleep_duration"
android:maxLength="2"
android:title="@string/activity_prefs_sleep_duration" />
android:title="@string/activity_prefs_sleep_duration"
app:useSimpleSummaryProvider="true" />
<EditTextPreference
android:defaultValue="2000"
android:inputType="number"
android:key="activity_user_calories_burnt"
android:maxLength="4"
android:title="@string/activity_prefs_calories_burnt" />
android:title="@string/activity_prefs_calories_burnt"
app:useSimpleSummaryProvider="true" />
<EditTextPreference
android:defaultValue="5000"
android:inputType="number"
android:key="activity_user_distance_meters"
android:maxLength="5"
android:title="@string/activity_prefs_distance_meters" />
android:title="@string/activity_prefs_distance_meters"
app:useSimpleSummaryProvider="true" />
<EditTextPreference
android:defaultValue="60"
android:inputType="number"
android:key="activity_user_activetime_minutes"
android:maxLength="3"
android:title="@string/activity_prefs_activetime_minutes" />
android:title="@string/activity_prefs_activetime_minutes"
app:useSimpleSummaryProvider="true" />
<EditTextPreference
android:defaultValue="12"
android:inputType="number"
android:key="activity_user_goal_standing_time_minutes"
android:maxLength="2"
android:title="@string/activity_prefs_goal_standing_time_minutes" />
android:title="@string/activity_prefs_goal_standing_time_minutes"
app:useSimpleSummaryProvider="true" />
<EditTextPreference
android:defaultValue="30"
android:inputType="number"
android:key="activity_user_goal_fat_burn_time_minutes"
android:maxLength="3"
android:title="@string/activity_prefs_goal_fat_burn_time_minutes" />
android:title="@string/activity_prefs_goal_fat_burn_time_minutes"
app:useSimpleSummaryProvider="true" />
</PreferenceCategory>
</PreferenceScreen>