1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-12-26 10:35:50 +01:00

Upgrade Mi Band preferences to androidx

This commit is contained in:
José Rebelo 2023-07-28 18:37:58 +01:00
parent 61a78c0ad8
commit f0067ebeae
6 changed files with 170 additions and 135 deletions

View File

@ -126,7 +126,7 @@
android:parentActivityName=".activities.SettingsActivity" />
<activity
android:name=".devices.miband.MiBandPreferencesActivity"
android:label="@string/preferences_miband_settings"
android:label="@string/preferences_miband_1_2_settings"
android:parentActivityName=".activities.SettingsActivity" />
<activity
android:name=".activities.loyaltycards.LoyaltyCardsSettingsActivity"

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2015-2021 Andreas Shimokawa, Carsten Pfeiffer, Christian
/* Copyright (C) 2015-2023 Andreas Shimokawa, Carsten Pfeiffer, Christian
Fischer, Daniele Gobbetti, José Rebelo, Szymon Tomasz Stefanek
This file is part of Gadgetbridge.
@ -17,103 +17,76 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.devices.miband;
import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PREF_MIBAND_ADDRESS;
import android.content.Intent;
import android.os.Bundle;
import android.preference.Preference;
import android.widget.Toast;
import android.text.InputType;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import java.util.HashSet;
import java.util.Set;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.activities.AbstractSettingsActivity;
import nodomain.freeyourgadget.gadgetbridge.activities.AbstractPreferenceFragment;
import nodomain.freeyourgadget.gadgetbridge.activities.AbstractSettingsActivityV2;
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceManager;
import nodomain.freeyourgadget.gadgetbridge.model.ActivityUser;
import nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec;
import nodomain.freeyourgadget.gadgetbridge.model.NotificationType;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
import static nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst.PREF_INACTIVITY_THRESHOLD;
import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.ORIGIN_ALARM_CLOCK;
import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.ORIGIN_INCOMING_CALL;
import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PREF_MIBAND_ADDRESS;
import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.VIBRATION_COUNT;
import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.getNotificationPrefKey;
import static nodomain.freeyourgadget.gadgetbridge.model.ActivityUser.PREF_USER_NAME;
public class MiBandPreferencesActivity extends AbstractSettingsActivity {
public class MiBandPreferencesActivity extends AbstractSettingsActivityV2 {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.miband_preferences);
addTryListeners();
}
/**
* delayed execution so that the preferences are applied first
*/
private void invokeLater(Runnable runnable) {
getListView().post(runnable);
protected String fragmentTag() {
return MiBandPreferencesFragment.FRAGMENT_TAG;
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
final Preference developmentMiaddr = findPreference(PREF_MIBAND_ADDRESS);
developmentMiaddr.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);
preference.setSummary(newVal.toString());
return true;
}
});
protected PreferenceFragmentCompat newFragment() {
return new MiBandPreferencesFragment();
}
private void addTryListeners() {
for (final NotificationType type : NotificationType.values()) {
String prefKey = "mi_try_" + type.getGenericType();
final Preference tryPref = findPreference(prefKey);
if (tryPref != null) {
tryPref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
public static class MiBandPreferencesFragment extends AbstractPreferenceFragment {
static final String FRAGMENT_TAG = "MIBAND_PREFERENCES_FRAGMENT";
@Override
public void onCreatePreferences(final Bundle savedInstanceState, final String rootKey) {
setPreferencesFromResource(R.xml.miband_preferences, rootKey);
for (final NotificationType type : NotificationType.values()) {
String countPrefKey = "mi_vibration_count_" + type.getGenericType();
setInputTypeFor(countPrefKey, InputType.TYPE_CLASS_NUMBER);
String tryPrefKey = "mi_try_" + type.getGenericType();
final Preference tryPref = findPreference(tryPrefKey);
if (tryPref != null) {
tryPref.setOnPreferenceClickListener(preference -> {
tryVibration(type);
return true;
}
});
}
}
final Preference developmentMiAddr = findPreference(PREF_MIBAND_ADDRESS);
if (developmentMiAddr != null) {
developmentMiAddr.setOnPreferenceChangeListener((preference, newVal) -> {
Intent refreshIntent = new Intent(DeviceManager.ACTION_REFRESH_DEVICELIST);
LocalBroadcastManager.getInstance(requireActivity().getBaseContext()).sendBroadcast(refreshIntent);
return true;
});
} else {
GB.toast(getBaseContext(), "Unable to find preference key: " + prefKey + ", trying the vibration won't work", Toast.LENGTH_LONG, GB.WARN);
}
}
}
private void tryVibration(NotificationType type) {
NotificationSpec spec = new NotificationSpec();
spec.type = type;
GBApplication.deviceService().onNotification(spec);
}
@Override
protected String[] getPreferenceKeysWithSummary() {
Set<String> prefKeys = new HashSet<>();
prefKeys.add(PREF_MIBAND_ADDRESS);
prefKeys.add(getNotificationPrefKey(VIBRATION_COUNT, ORIGIN_ALARM_CLOCK));
prefKeys.add(getNotificationPrefKey(VIBRATION_COUNT, ORIGIN_INCOMING_CALL));
for (NotificationType type : NotificationType.values()) {
String key = type.getGenericType();
prefKeys.add(getNotificationPrefKey(VIBRATION_COUNT, key));
/**
* delayed execution so that the preferences are applied first
*/
private void invokeLater(Runnable runnable) {
getListView().post(runnable);
}
return prefKeys.toArray(new String[0]);
private void tryVibration(NotificationType type) {
NotificationSpec spec = new NotificationSpec();
spec.type = type;
GBApplication.deviceService().onNotification(spec);
}
}
}

View File

@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="#7E7E7E"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@android:color/white"
android:pathData="M20,4L4,4c-1.1,0 -1.99,0.9 -1.99,2L2,18c0,1.1 0.9,2 2,2h16c1.1,0 2,-0.9 2,-2L22,6c0,-1.1 -0.9,-2 -2,-2zM20,8l-8,5 -8,-5L4,6l8,5 8,-5v2z" />
</vector>

View File

@ -584,7 +584,8 @@
<string name="pairing_already_bonded">"Already bonded with %1$s (%2$s), connecting…"</string>
<string name="message_cannot_pair_no_mac">No MAC address passed, cannot pair.</string>
<string name="preferences_category_device_specific_settings">Device specific settings</string>
<string name="preferences_miband_settings">Mi Band / Amazfit settings</string>
<string name="preferences_miband_1_2_settings" tools:ignore="TypographyFractions">Mi Band 1/2 settings</string>
<string name="preferences_miband_1_2_warning">Warning: These preferences only apply to the Mi Bands 1 and 2.</string>
<string name="male">Male</string>
<string name="female">Female</string>
<string name="other">Other</string>

View File

@ -1,18 +1,26 @@
<?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">
<Preference
android:icon="@drawable/ic_warning"
android:key="preferences_miband_1_2_warning"
android:summary="@string/preferences_miband_1_2_warning" />
<PreferenceCategory
android:key="pref_category_miband_notification"
android:title="@string/pref_header_vibration_settings">
android:title="@string/pref_header_vibration_settings"
app:iconSpaceReserved="false">
<PreferenceScreen
android:icon="@drawable/ic_message_outline"
android:key="vibration_profile_key"
android:title="@string/pref_screen_notification_profile_sms"
android:persistent="false">
android:persistent="false"
android:title="@string/pref_screen_notification_profile_sms">
<!-- workaround for missing toolbar -->
<PreferenceCategory
android:title="@string/pref_screen_notification_profile_sms"
/>
app:iconSpaceReserved="false" />
<ListPreference
android:defaultValue="@string/p_staccato"
@ -20,30 +28,35 @@
android:entryValues="@array/vibration_profile_values"
android:key="mi_vibration_profile_generic_sms"
android:title="@string/miband_prefs_vibration"
android:summary="%s" />
app:iconSpaceReserved="false"
app:useSimpleSummaryProvider="true" />
<EditTextPreference
android:defaultValue="3"
android:inputType="number"
android:key="mi_vibration_count_generic_sms"
android:maxLength="1"
android:title="@string/pref_title_notifications_repetitions" />
android:title="@string/pref_title_notifications_repetitions"
app:iconSpaceReserved="false"
app:useSimpleSummaryProvider="true" />
<Preference
android:key="mi_try_generic_sms"
android:persistent="false"
android:title="@string/vibration_try"/>
android:title="@string/vibration_try"
app:iconSpaceReserved="false" />
</PreferenceScreen>
<PreferenceScreen
android:icon="@drawable/ic_phone"
android:key="vibration_profile_key"
android:title="@string/pref_screen_notification_profile_incoming_call"
android:persistent="false">
android:persistent="false"
android:title="@string/pref_screen_notification_profile_incoming_call">
<!-- workaround for missing toolbar -->
<PreferenceCategory
android:title="@string/pref_screen_notification_profile_incoming_call"
/>
app:iconSpaceReserved="false" />
<ListPreference
android:defaultValue="@string/p_ring"
@ -51,25 +64,29 @@
android:entryValues="@array/vibration_profile_values"
android:key="mi_vibration_profile_incoming_call"
android:title="@string/miband_prefs_vibration"
android:summary="%s" />
app:iconSpaceReserved="false"
app:useSimpleSummaryProvider="true" />
<EditTextPreference
android:defaultValue="60"
android:inputType="number"
android:key="mi_vibration_count_incoming_call"
android:maxLength="2"
android:title="@string/pref_title_notifications_repetitions" />
android:title="@string/pref_title_notifications_repetitions"
app:iconSpaceReserved="false"
app:useSimpleSummaryProvider="true" />
</PreferenceScreen>
<PreferenceScreen
android:icon="@drawable/ic_email"
android:key="vibration_profile_key"
android:title="@string/pref_screen_notification_profile_email"
android:persistent="false">
android:persistent="false"
android:title="@string/pref_screen_notification_profile_email">
<!-- workaround for missing toolbar -->
<PreferenceCategory
android:title="@string/pref_screen_notification_profile_email"
/>
app:iconSpaceReserved="false" />
<ListPreference
android:defaultValue="@string/p_medium"
@ -77,29 +94,34 @@
android:entryValues="@array/vibration_profile_values"
android:key="mi_vibration_profile_generic_email"
android:title="@string/miband_prefs_vibration"
android:summary="%s" />
app:iconSpaceReserved="false"
app:useSimpleSummaryProvider="true" />
<EditTextPreference
android:defaultValue="2"
android:inputType="number"
android:key="mi_vibration_count_generic_email"
android:maxLength="1"
android:title="@string/pref_title_notifications_repetitions" />
android:title="@string/pref_title_notifications_repetitions"
app:iconSpaceReserved="false"
app:useSimpleSummaryProvider="true" />
<Preference
android:key="mi_try_generic_email"
android:persistent="false"
android:title="@string/vibration_try"/>
android:title="@string/vibration_try"
app:iconSpaceReserved="false" />
</PreferenceScreen>
<PreferenceScreen
android:icon="@drawable/ic_message_outline"
android:key="vibration_profile_key"
android:title="@string/pref_screen_notification_profile_generic_chat"
android:persistent="false">
android:persistent="false"
android:title="@string/pref_screen_notification_profile_generic_chat">
<!-- workaround for missing toolbar -->
<PreferenceCategory
android:title="@string/pref_screen_notification_profile_generic_chat"
/>
app:iconSpaceReserved="false" />
<ListPreference
android:defaultValue="@string/p_waterdrop"
@ -107,29 +129,34 @@
android:entryValues="@array/vibration_profile_values"
android:key="mi_vibration_profile_generic_chat"
android:title="@string/miband_prefs_vibration"
android:summary="%s" />
app:iconSpaceReserved="false"
app:useSimpleSummaryProvider="true" />
<EditTextPreference
android:defaultValue="3"
android:inputType="number"
android:key="mi_vibration_count_generic_chat"
android:maxLength="1"
android:title="@string/pref_title_notifications_repetitions" />
android:title="@string/pref_title_notifications_repetitions"
app:iconSpaceReserved="false"
app:useSimpleSummaryProvider="true" />
<Preference
android:key="mi_try_generic_chat"
android:persistent="false"
android:title="@string/vibration_try"/>
android:title="@string/vibration_try"
app:iconSpaceReserved="false" />
</PreferenceScreen>
<PreferenceScreen
android:icon="@drawable/ic_person"
android:key="vibration_profile_key"
android:title="@string/pref_screen_notification_profile_generic_social"
android:persistent="false">
android:persistent="false"
android:title="@string/pref_screen_notification_profile_generic_social">
<!-- workaround for missing toolbar -->
<PreferenceCategory
android:title="@string/pref_screen_notification_profile_generic_social"
/>
app:iconSpaceReserved="false" />
<ListPreference
android:defaultValue="@string/p_waterdrop"
@ -137,29 +164,34 @@
android:entryValues="@array/vibration_profile_values"
android:key="mi_vibration_profile_generic_social"
android:title="@string/miband_prefs_vibration"
android:summary="%s" />
app:iconSpaceReserved="false"
app:useSimpleSummaryProvider="true" />
<EditTextPreference
android:defaultValue="3"
android:inputType="number"
android:key="mi_vibration_count_generic_social"
android:maxLength="1"
android:title="@string/pref_title_notifications_repetitions" />
android:title="@string/pref_title_notifications_repetitions"
app:iconSpaceReserved="false"
app:useSimpleSummaryProvider="true" />
<Preference
android:key="mi_try_generic_social"
android:persistent="false"
android:title="@string/vibration_try"/>
android:title="@string/vibration_try"
app:iconSpaceReserved="false" />
</PreferenceScreen>
<PreferenceScreen
android:icon="@drawable/ic_access_alarms"
android:key="vibration_profile_key"
android:title="@string/pref_screen_notification_profile_alarm_clock"
android:persistent="false">
android:persistent="false"
android:title="@string/pref_screen_notification_profile_alarm_clock">
<!-- workaround for missing toolbar -->
<PreferenceCategory
android:title="@string/pref_screen_notification_profile_alarm_clock"
/>
app:iconSpaceReserved="false" />
<ListPreference
android:defaultValue="@string/p_alarm_clock"
@ -167,29 +199,34 @@
android:entryValues="@array/vibration_profile_values"
android:key="mi_vibration_profile_alarm_clock"
android:title="@string/miband_prefs_vibration"
android:summary="%s" />
app:iconSpaceReserved="false"
app:useSimpleSummaryProvider="true" />
<EditTextPreference
android:defaultValue="3"
android:inputType="number"
android:key="mi_vibration_count_alarm_clock"
android:maxLength="2"
android:title="@string/pref_title_notifications_repetitions" />
android:title="@string/pref_title_notifications_repetitions"
app:iconSpaceReserved="false"
app:useSimpleSummaryProvider="true" />
<Preference
android:key="mi_try_generic_alarm_clock"
android:persistent="false"
android:title="@string/vibration_try"/>
android:title="@string/vibration_try"
app:iconSpaceReserved="false" />
</PreferenceScreen>
<PreferenceScreen
android:icon="@drawable/ic_map"
android:key="vibration_profile_key"
android:title="@string/pref_screen_notification_profile_generic_navigation"
android:persistent="false">
android:persistent="false"
android:title="@string/pref_screen_notification_profile_generic_navigation">
<!-- workaround for missing toolbar -->
<PreferenceCategory
android:title="@string/pref_screen_notification_profile_generic_navigation"
/>
app:iconSpaceReserved="false" />
<ListPreference
android:defaultValue="@string/p_waterdrop"
@ -197,29 +234,34 @@
android:entryValues="@array/vibration_profile_values"
android:key="mi_vibration_profile_generic_navigation"
android:title="@string/miband_prefs_vibration"
android:summary="%s" />
app:iconSpaceReserved="false"
app:useSimpleSummaryProvider="true" />
<EditTextPreference
android:defaultValue="3"
android:inputType="number"
android:key="mi_vibration_count_generic_navigation"
android:maxLength="1"
android:title="@string/pref_title_notifications_repetitions" />
android:title="@string/pref_title_notifications_repetitions"
app:iconSpaceReserved="false"
app:useSimpleSummaryProvider="true" />
<Preference
android:persistent="false"
android:key="mi_try_generic_navigation"
android:title="@string/vibration_try"/>
android:persistent="false"
android:title="@string/vibration_try"
app:iconSpaceReserved="false" />
</PreferenceScreen>
<PreferenceScreen
android:icon="@drawable/ic_notifications"
android:key="vibration_profile_key"
android:title="@string/pref_screen_notification_profile_generic"
android:persistent="false">
android:persistent="false"
android:title="@string/pref_screen_notification_profile_generic">
<!-- workaround for missing toolbar -->
<PreferenceCategory
android:title="@string/pref_screen_notification_profile_generic"
/>
app:iconSpaceReserved="false" />
<ListPreference
android:defaultValue="@string/p_waterdrop"
@ -227,39 +269,48 @@
android:entryValues="@array/vibration_profile_values"
android:key="mi_vibration_profile_generic"
android:title="@string/miband_prefs_vibration"
android:summary="%s" />
app:iconSpaceReserved="false"
app:useSimpleSummaryProvider="true" />
<EditTextPreference
android:defaultValue="3"
android:inputType="number"
android:key="mi_vibration_count_generic"
android:maxLength="1"
android:title="@string/pref_title_notifications_repetitions" />
android:title="@string/pref_title_notifications_repetitions"
app:iconSpaceReserved="false"
app:useSimpleSummaryProvider="true" />
<Preference
android:key="mi_try_generic"
android:persistent="false"
android:title="@string/vibration_try"/>
android:title="@string/vibration_try"
app:iconSpaceReserved="false" />
</PreferenceScreen>
</PreferenceCategory>
<PreferenceCategory
android:key="pref_key_development"
android:title="@string/pref_header_development">
<CheckBoxPreference
android:title="@string/pref_header_development"
app:iconSpaceReserved="false">
<SwitchPreference
android:defaultValue="true"
android:key="mi_setup_bt_pairing"
android:title="@string/pref_title_setup_bt_pairing"
android:summary="@string/pref_summary_setup_bt_pairing"
android:defaultValue="true" />
android:title="@string/pref_title_setup_bt_pairing"
app:iconSpaceReserved="false" />
<EditTextPreference
android:digits="0123456789ABCDEF:"
android:key="development_miaddr"
android:maxLength="17"
android:title="@string/pref_title_development_miaddr" />
<CheckBoxPreference
android:title="@string/pref_title_development_miaddr"
app:iconSpaceReserved="false"
app:useSimpleSummaryProvider="true" />
<SwitchPreference
android:defaultValue="false"
android:key="mi_dont_ack_transfer"
android:title="@string/pref_title_keep_data_on_device"
android:summary="@string/pref_summary_keep_data_on_device"
android:defaultValue="false" />
android:title="@string/pref_title_keep_data_on_device"
app:iconSpaceReserved="false" />
</PreferenceCategory>
</PreferenceScreen>

View File

@ -225,7 +225,7 @@
<Preference
android:icon="@drawable/ic_device_miband"
android:key="pref_key_miband"
android:title="@string/preferences_miband_settings" />
android:title="@string/preferences_miband_1_2_settings" />
<Preference
android:icon="@drawable/ic_device_pebble"