1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-29 13:26:50 +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, /* Copyright (C) 2015-2023 Andreas Shimokawa, Carsten Pfeiffer, Lem Dulfo,
vanous vanous, José Rebelo
This file is part of Gadgetbridge. 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_WEIGHT_KG;
import static nodomain.freeyourgadget.gadgetbridge.model.ActivityUser.PREF_USER_YEAR_OF_BIRTH; import static nodomain.freeyourgadget.gadgetbridge.model.ActivityUser.PREF_USER_YEAR_OF_BIRTH;
import android.content.Intent;
import android.os.Bundle; 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.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 @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.about_user); setContentView(R.layout.activity_device_settings);
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);
addIntentNotificationListener(PREF_USER_STEPS_GOAL); if (savedInstanceState == null) {
addIntentNotificationListener(PREF_USER_HEIGHT_CM); Fragment fragment = getSupportFragmentManager().findFragmentByTag(AboutUserPreferencesFragment.FRAGMENT_TAG);
addIntentNotificationListener(PREF_USER_SLEEP_DURATION); if (fragment == null) {
addIntentNotificationListener(PREF_USER_STEP_LENGTH_CM); fragment = new AboutUserPreferencesFragment();
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);
} }
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.settings_container, fragment, AboutUserPreferencesFragment.FRAGMENT_TAG)
.commit();
}
}
public static class AboutUserPreferencesFragment extends AbstractPreferenceFragment {
static final String FRAGMENT_TAG = "ABOUT_USER_PREFERENCES_FRAGMENT";
@Override @Override
protected String[] getPreferenceKeysWithSummary() { public void onCreatePreferences(final Bundle savedInstanceState, final String rootKey) {
return new String[]{ addPreferencesFromResource(R.xml.about_user);
PREF_USER_YEAR_OF_BIRTH,
PREF_USER_HEIGHT_CM, addPreferenceHandlerFor(PREF_USER_NAME, true, false);
PREF_USER_WEIGHT_KG, addPreferenceHandlerFor(PREF_USER_YEAR_OF_BIRTH, true, false);
PREF_USER_SLEEP_DURATION, addPreferenceHandlerFor(PREF_USER_HEIGHT_CM, true, true);
PREF_USER_STEPS_GOAL, addPreferenceHandlerFor(PREF_USER_WEIGHT_KG, true, false);
PREF_USER_STEP_LENGTH_CM, addPreferenceHandlerFor(PREF_USER_GENDER, true, false);
PREF_USER_ACTIVETIME_MINUTES, addPreferenceHandlerFor(PREF_USER_STEPS_GOAL, true, true);
PREF_USER_CALORIES_BURNT, addPreferenceHandlerFor(PREF_USER_GOAL_WEIGHT_KG, true, true);
PREF_USER_DISTANCE_METERS, addPreferenceHandlerFor(PREF_USER_GOAL_STANDING_TIME_HOURS, true, true);
PREF_USER_GOAL_WEIGHT_KG, addPreferenceHandlerFor(PREF_USER_GOAL_FAT_BURN_TIME_MINUTES, true, true);
PREF_USER_GOAL_STANDING_TIME_HOURS, addPreferenceHandlerFor(PREF_USER_SLEEP_DURATION, false, true);
PREF_USER_GOAL_FAT_BURN_TIME_MINUTES 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); 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"?> <?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 <PreferenceCategory
android:key="pref_category_activity_personal" android:key="pref_category_activity_personal"
android:title="@string/activity_prefs_about_you"> android:title="@string/activity_prefs_about_you">
@ -8,7 +9,8 @@
android:inputType="number" android:inputType="number"
android:key="activity_user_year_of_birth" android:key="activity_user_year_of_birth"
android:maxLength="4" android:maxLength="4"
android:title="@string/activity_prefs_year_birth" /> android:title="@string/activity_prefs_year_birth"
app:useSimpleSummaryProvider="true" />
<ListPreference <ListPreference
android:defaultValue="2" android:defaultValue="2"
@ -23,72 +25,83 @@
android:inputType="number" android:inputType="number"
android:key="activity_user_height_cm" android:key="activity_user_height_cm"
android:maxLength="3" android:maxLength="3"
android:title="@string/activity_prefs_height_cm" /> android:title="@string/activity_prefs_height_cm"
app:useSimpleSummaryProvider="true" />
<EditTextPreference <EditTextPreference
android:inputType="number" android:inputType="number"
android:key="activity_user_weight_kg" android:key="activity_user_weight_kg"
android:maxLength="3" android:maxLength="3"
android:title="@string/activity_prefs_weight_kg" /> android:title="@string/activity_prefs_weight_kg"
app:useSimpleSummaryProvider="true" />
<EditTextPreference <EditTextPreference
android:inputType="number" android:inputType="number"
android:key="activity_user_goal_weight_kg" android:key="activity_user_goal_weight_kg"
android:maxLength="3" android:maxLength="3"
android:title="@string/activity_prefs_target_weight_kg" /> android:title="@string/activity_prefs_target_weight_kg"
app:useSimpleSummaryProvider="true" />
<EditTextPreference <EditTextPreference
android:inputType="number" android:inputType="number"
android:key="activity_user_step_length_cm" android:key="activity_user_step_length_cm"
android:maxLength="3" android:maxLength="3"
android:title="@string/activity_prefs_step_length_cm" /> android:title="@string/activity_prefs_step_length_cm"
app:useSimpleSummaryProvider="true" />
<EditTextPreference <EditTextPreference
android:defaultValue="10000" android:defaultValue="10000"
android:inputType="number" android:inputType="number"
android:key="fitness_goal" android:key="fitness_goal"
android:maxLength="5" android:maxLength="5"
android:title="@string/miband_prefs_fitness_goal" /> android:title="@string/miband_prefs_fitness_goal"
app:useSimpleSummaryProvider="true" />
<EditTextPreference <EditTextPreference
android:inputType="number" android:inputType="number"
android:key="activity_user_sleep_duration" android:key="activity_user_sleep_duration"
android:maxLength="2" android:maxLength="2"
android:title="@string/activity_prefs_sleep_duration" /> android:title="@string/activity_prefs_sleep_duration"
app:useSimpleSummaryProvider="true" />
<EditTextPreference <EditTextPreference
android:defaultValue="2000" android:defaultValue="2000"
android:inputType="number" android:inputType="number"
android:key="activity_user_calories_burnt" android:key="activity_user_calories_burnt"
android:maxLength="4" android:maxLength="4"
android:title="@string/activity_prefs_calories_burnt" /> android:title="@string/activity_prefs_calories_burnt"
app:useSimpleSummaryProvider="true" />
<EditTextPreference <EditTextPreference
android:defaultValue="5000" android:defaultValue="5000"
android:inputType="number" android:inputType="number"
android:key="activity_user_distance_meters" android:key="activity_user_distance_meters"
android:maxLength="5" android:maxLength="5"
android:title="@string/activity_prefs_distance_meters" /> android:title="@string/activity_prefs_distance_meters"
app:useSimpleSummaryProvider="true" />
<EditTextPreference <EditTextPreference
android:defaultValue="60" android:defaultValue="60"
android:inputType="number" android:inputType="number"
android:key="activity_user_activetime_minutes" android:key="activity_user_activetime_minutes"
android:maxLength="3" android:maxLength="3"
android:title="@string/activity_prefs_activetime_minutes" /> android:title="@string/activity_prefs_activetime_minutes"
app:useSimpleSummaryProvider="true" />
<EditTextPreference <EditTextPreference
android:defaultValue="12" android:defaultValue="12"
android:inputType="number" android:inputType="number"
android:key="activity_user_goal_standing_time_minutes" android:key="activity_user_goal_standing_time_minutes"
android:maxLength="2" 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 <EditTextPreference
android:defaultValue="30" android:defaultValue="30"
android:inputType="number" android:inputType="number"
android:key="activity_user_goal_fat_burn_time_minutes" android:key="activity_user_goal_fat_burn_time_minutes"
android:maxLength="3" 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> </PreferenceCategory>
</PreferenceScreen> </PreferenceScreen>