From f0067ebeaedb7cbaf545e7371fda616f8d308bc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Rebelo?= Date: Fri, 28 Jul 2023 18:37:58 +0100 Subject: [PATCH] Upgrade Mi Band preferences to androidx --- app/src/main/AndroidManifest.xml | 2 +- .../miband/MiBandPreferencesActivity.java | 121 +++++-------- app/src/main/res/drawable/ic_email.xml | 10 ++ app/src/main/res/values/strings.xml | 3 +- app/src/main/res/xml/miband_preferences.xml | 167 ++++++++++++------ app/src/main/res/xml/preferences.xml | 2 +- 6 files changed, 170 insertions(+), 135 deletions(-) create mode 100644 app/src/main/res/drawable/ic_email.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 106c5867b..76964f612 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -126,7 +126,7 @@ android:parentActivityName=".activities.SettingsActivity" /> . */ 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 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); + } } } diff --git a/app/src/main/res/drawable/ic_email.xml b/app/src/main/res/drawable/ic_email.xml new file mode 100644 index 000000000..7e6e24833 --- /dev/null +++ b/app/src/main/res/drawable/ic_email.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 494b7b18e..487cc8e58 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -584,7 +584,8 @@ "Already bonded with %1$s (%2$s), connecting…" No MAC address passed, cannot pair. Device specific settings - Mi Band / Amazfit settings + Mi Band 1/2 settings + Warning: These preferences only apply to the Mi Bands 1 and 2. Male Female Other diff --git a/app/src/main/res/xml/miband_preferences.xml b/app/src/main/res/xml/miband_preferences.xml index a0d745e10..14ad0c508 100644 --- a/app/src/main/res/xml/miband_preferences.xml +++ b/app/src/main/res/xml/miband_preferences.xml @@ -1,18 +1,26 @@ - + + + + android:title="@string/pref_header_vibration_settings" + app:iconSpaceReserved="false"> + android:persistent="false" + android:title="@string/pref_screen_notification_profile_sms"> + app:iconSpaceReserved="false" /> + app:iconSpaceReserved="false" + app:useSimpleSummaryProvider="true" /> + android:title="@string/pref_title_notifications_repetitions" + app:iconSpaceReserved="false" + app:useSimpleSummaryProvider="true" /> + android:title="@string/vibration_try" + app:iconSpaceReserved="false" /> + android:persistent="false" + android:title="@string/pref_screen_notification_profile_incoming_call"> + app:iconSpaceReserved="false" /> + app:iconSpaceReserved="false" + app:useSimpleSummaryProvider="true" /> + android:title="@string/pref_title_notifications_repetitions" + app:iconSpaceReserved="false" + app:useSimpleSummaryProvider="true" /> + android:persistent="false" + android:title="@string/pref_screen_notification_profile_email"> + app:iconSpaceReserved="false" /> + app:iconSpaceReserved="false" + app:useSimpleSummaryProvider="true" /> + android:title="@string/pref_title_notifications_repetitions" + app:iconSpaceReserved="false" + app:useSimpleSummaryProvider="true" /> + android:title="@string/vibration_try" + app:iconSpaceReserved="false" /> + android:persistent="false" + android:title="@string/pref_screen_notification_profile_generic_chat"> + app:iconSpaceReserved="false" /> + app:iconSpaceReserved="false" + app:useSimpleSummaryProvider="true" /> + android:title="@string/pref_title_notifications_repetitions" + app:iconSpaceReserved="false" + app:useSimpleSummaryProvider="true" /> + android:title="@string/vibration_try" + app:iconSpaceReserved="false" /> + android:persistent="false" + android:title="@string/pref_screen_notification_profile_generic_social"> + app:iconSpaceReserved="false" /> + app:iconSpaceReserved="false" + app:useSimpleSummaryProvider="true" /> + android:title="@string/pref_title_notifications_repetitions" + app:iconSpaceReserved="false" + app:useSimpleSummaryProvider="true" /> + android:title="@string/vibration_try" + app:iconSpaceReserved="false" /> + android:persistent="false" + android:title="@string/pref_screen_notification_profile_alarm_clock"> + app:iconSpaceReserved="false" /> + app:iconSpaceReserved="false" + app:useSimpleSummaryProvider="true" /> + android:title="@string/pref_title_notifications_repetitions" + app:iconSpaceReserved="false" + app:useSimpleSummaryProvider="true" /> + android:title="@string/vibration_try" + app:iconSpaceReserved="false" /> + android:persistent="false" + android:title="@string/pref_screen_notification_profile_generic_navigation"> + app:iconSpaceReserved="false" /> + app:iconSpaceReserved="false" + app:useSimpleSummaryProvider="true" /> + android:title="@string/pref_title_notifications_repetitions" + app:iconSpaceReserved="false" + app:useSimpleSummaryProvider="true" /> + android:persistent="false" + android:title="@string/vibration_try" + app:iconSpaceReserved="false" /> + android:persistent="false" + android:title="@string/pref_screen_notification_profile_generic"> + app:iconSpaceReserved="false" /> + app:iconSpaceReserved="false" + app:useSimpleSummaryProvider="true" /> + android:title="@string/pref_title_notifications_repetitions" + app:iconSpaceReserved="false" + app:useSimpleSummaryProvider="true" /> + android:title="@string/vibration_try" + app:iconSpaceReserved="false" /> - + + android:title="@string/pref_title_setup_bt_pairing" + app:iconSpaceReserved="false" /> - + + android:title="@string/pref_title_keep_data_on_device" + app:iconSpaceReserved="false" /> \ No newline at end of file diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index 3398e3dc4..fe2f275e5 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -225,7 +225,7 @@ + android:title="@string/preferences_miband_1_2_settings" />