From 62828e5158289867f900da793a70006d415aeae5 Mon Sep 17 00:00:00 2001 From: cpfeiffer Date: Mon, 10 Oct 2016 22:39:20 +0200 Subject: [PATCH] Remove ORIGIN_ notification constants, clean up - MI preferences now use NotificationType.getFixedName() for preference keys --- .../devices/miband/MiBandConst.java | 8 ----- .../miband/MiBandPreferencesActivity.java | 32 +++++++------------ .../gadgetbridge/model/NotificationType.java | 12 ++++++- .../devices/miband/MiBand2Support.java | 19 +---------- .../service/devices/miband/MiBandSupport.java | 25 ++++++--------- app/src/main/res/xml/miband_preferences.xml | 22 ++++++------- 6 files changed, 43 insertions(+), 75 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBandConst.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBandConst.java index 4ae3fc4b..fce2f84d 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBandConst.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBandConst.java @@ -21,15 +21,7 @@ public final class MiBandConst { public static final String PREF_TRY_SMS = "mi_try_sms"; - public static final String ORIGIN_SMS = "sms"; - public static final String ORIGIN_CHAT = "chat"; - public static final String ORIGIN_TELEGRAM = "telegram"; - public static final String ORIGIN_TWITTER = "twitter"; - public static final String ORIGIN_FACEBOOK = "facebook"; public static final String ORIGIN_INCOMING_CALL = "incoming_call"; - public static final String ORIGIN_K9MAIL = "k9mail"; - public static final String ORIGIN_PEBBLEMSG = "pebblemsg"; - public static final String ORIGIN_GENERIC = "generic"; public static final String MI_GENERAL_NAME_PREFIX = "MI"; public static final String MI_BAND2_NAME = "MI Band 2"; public static final String MI_1 = "1"; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBandPreferencesActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBandPreferencesActivity.java index d06f4ec6..dbb83aaf 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBandPreferencesActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBandPreferencesActivity.java @@ -14,21 +14,12 @@ import nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec; import nodomain.freeyourgadget.gadgetbridge.model.NotificationType; import nodomain.freeyourgadget.gadgetbridge.util.GB; -import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.ORIGIN_CHAT; -import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.ORIGIN_FACEBOOK; -import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.ORIGIN_GENERIC; import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.ORIGIN_INCOMING_CALL; -import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.ORIGIN_K9MAIL; -import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.ORIGIN_PEBBLEMSG; -import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.ORIGIN_SMS; -import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.ORIGIN_TELEGRAM; -import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.ORIGIN_TWITTER; import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PREF_MIBAND_ADDRESS; import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PREF_MIBAND_DEVICE_TIME_OFFSET_HOURS; import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PREF_MIBAND_FITNESS_GOAL; import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PREF_MIBAND_RESERVE_ALARM_FOR_CALENDAR; import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PREF_MIBAND_USE_HR_FOR_SLEEP_DETECTION; -import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PREF_TRY_SMS; import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PREF_USER_ALIAS; import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.VIBRATION_COUNT; import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.getNotificationPrefKey; @@ -66,7 +57,7 @@ public class MiBandPreferencesActivity extends AbstractSettingsActivity { private void addTryListeners() { for (final NotificationType type : NotificationType.values()) { - String prefKey = "mi_try_" + type.name(); + String prefKey = "mi_try_" + type.getFixedValue(); final Preference tryPref = findPreference(prefKey); if (tryPref != null) { tryPref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { @@ -90,21 +81,20 @@ public class MiBandPreferencesActivity extends AbstractSettingsActivity { @Override protected String[] getPreferenceKeysWithSummary() { - return new String[]{ + String[] strings1 = { PREF_USER_ALIAS, PREF_MIBAND_ADDRESS, PREF_MIBAND_FITNESS_GOAL, PREF_MIBAND_RESERVE_ALARM_FOR_CALENDAR, - PREF_MIBAND_DEVICE_TIME_OFFSET_HOURS, - getNotificationPrefKey(VIBRATION_COUNT, ORIGIN_SMS), - getNotificationPrefKey(VIBRATION_COUNT, ORIGIN_CHAT), - getNotificationPrefKey(VIBRATION_COUNT, ORIGIN_TELEGRAM), - getNotificationPrefKey(VIBRATION_COUNT, ORIGIN_TWITTER), - getNotificationPrefKey(VIBRATION_COUNT, ORIGIN_FACEBOOK), - getNotificationPrefKey(VIBRATION_COUNT, ORIGIN_INCOMING_CALL), - getNotificationPrefKey(VIBRATION_COUNT, ORIGIN_K9MAIL), - getNotificationPrefKey(VIBRATION_COUNT, ORIGIN_PEBBLEMSG), - getNotificationPrefKey(VIBRATION_COUNT, ORIGIN_GENERIC), + PREF_MIBAND_DEVICE_TIME_OFFSET_HOURS, + getNotificationPrefKey(VIBRATION_COUNT, ORIGIN_INCOMING_CALL) }; + String[] strings2 = new String[NotificationType.values().length]; + int i = 0; + for (NotificationType type : NotificationType.values()) { + strings2[i++] = type.getFixedValue(); + } + + return org.apache.commons.lang3.ArrayUtils.addAll(strings1, strings2); } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/NotificationType.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/NotificationType.java index 49c9cd24..a63b3d8c 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/NotificationType.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/NotificationType.java @@ -9,5 +9,15 @@ public enum NotificationType { FACEBOOK, SMS, TWITTER, - TELEGRAM, + TELEGRAM; + + /** + * Returns the enum constant as a fixed String value, e.g. to be used + * as preference key. In case the keys are ever changed, this method + * may be used to bring backward compatibility. + * @return + */ + public String getFixedValue() { + return name().toLowerCase(); + } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband/MiBand2Support.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband/MiBand2Support.java index bbd81549..0a6ddb91 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband/MiBand2Support.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband/MiBand2Support.java @@ -54,7 +54,6 @@ import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.AbortTransactio import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.ConditionalWriteAction; import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceStateAction; import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.WriteAction; -import nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.alertnotification.AlertLevel; import nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.deviceinfo.DeviceInfoProfile; import nodomain.freeyourgadget.gadgetbridge.service.devices.miband2.Mi2NotificationStrategy; import nodomain.freeyourgadget.gadgetbridge.service.devices.miband2.operations.InitOperation; @@ -74,14 +73,6 @@ import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.FL import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.FLASH_COUNT; import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.FLASH_DURATION; import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.FLASH_ORIGINAL_COLOUR; -import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.ORIGIN_CHAT; -import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.ORIGIN_FACEBOOK; -import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.ORIGIN_GENERIC; -import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.ORIGIN_K9MAIL; -import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.ORIGIN_PEBBLEMSG; -import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.ORIGIN_SMS; -import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.ORIGIN_TELEGRAM; -import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.ORIGIN_TWITTER; import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.VIBRATION_COUNT; import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.VIBRATION_DURATION; import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.VIBRATION_PAUSE; @@ -561,46 +552,38 @@ public class MiBand2Support extends AbstractBTLEDeviceSupport { @Override public void onNotification(NotificationSpec notificationSpec) { String task; - String origin; int alertLevel; switch (notificationSpec.type) { case SMS: task = "sms received"; - origin = ORIGIN_SMS; alertLevel = MiBand2Service.ALERT_LEVEL_MESSAGE; break; case EMAIL: task = "email received"; - origin = ORIGIN_K9MAIL; alertLevel = MiBand2Service.ALERT_LEVEL_MESSAGE; break; case FACEBOOK: task = "facebook message received"; - origin = ORIGIN_FACEBOOK; alertLevel = MiBand2Service.ALERT_LEVEL_MESSAGE; break; case TWITTER: task = "twitter message received"; - origin = ORIGIN_TWITTER; alertLevel = MiBand2Service.ALERT_LEVEL_MESSAGE; break; case TELEGRAM: task = "telegram message received"; - origin = ORIGIN_TELEGRAM; alertLevel = MiBand2Service.ALERT_LEVEL_MESSAGE; break; case CHAT: task = "chat message received"; - origin = ORIGIN_CHAT; alertLevel = MiBand2Service.ALERT_LEVEL_MESSAGE; break; case UNDEFINED: default: task = "generic notification received"; - origin = ORIGIN_GENERIC; alertLevel = MiBand2Service.ALERT_LEVEL_VIBRATE_ONLY; } - performPreferredNotification(task, origin, alertLevel, null); + performPreferredNotification(task, notificationSpec.type.getFixedValue(), alertLevel, null); } @Override diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband/MiBandSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband/MiBandSupport.java index 4c2bb041..cc4429ad 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband/MiBandSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband/MiBandSupport.java @@ -36,7 +36,6 @@ import nodomain.freeyourgadget.gadgetbridge.devices.miband.VibrationProfile; import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession; import nodomain.freeyourgadget.gadgetbridge.entities.Device; import nodomain.freeyourgadget.gadgetbridge.entities.MiBandActivitySample; -import nodomain.freeyourgadget.gadgetbridge.entities.MiBandActivitySampleDao; import nodomain.freeyourgadget.gadgetbridge.entities.User; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice.State; @@ -77,13 +76,6 @@ import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.FL import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.FLASH_COUNT; import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.FLASH_DURATION; import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.FLASH_ORIGINAL_COLOUR; -import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.ORIGIN_FACEBOOK; -import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.ORIGIN_GENERIC; -import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.ORIGIN_K9MAIL; -import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.ORIGIN_PEBBLEMSG; -import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.ORIGIN_SMS; -import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.ORIGIN_TELEGRAM; -import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.ORIGIN_TWITTER; import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.VIBRATION_COUNT; import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.VIBRATION_DURATION; import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.VIBRATION_PAUSE; @@ -542,28 +534,29 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport { @Override public void onNotification(NotificationSpec notificationSpec) { - // FIXME: these ORIGIN contants do not really make sense anymore + String origin = notificationSpec.type.getFixedValue(); switch (notificationSpec.type) { case SMS: - performPreferredNotification("sms received", ORIGIN_SMS, null); + performPreferredNotification("sms received", origin, null); break; case EMAIL: - performPreferredNotification("email received", ORIGIN_K9MAIL, null); + performPreferredNotification("email received", origin, null); break; case CHAT: - performPreferredNotification("chat message received", ORIGIN_PEBBLEMSG, null); + performPreferredNotification("chat message received", origin, null); break; case TELEGRAM: - performPreferredNotification("chat message received", ORIGIN_TELEGRAM, null); + performPreferredNotification("chat message received", origin, null); break; case TWITTER: - performPreferredNotification("chat message received", ORIGIN_TWITTER, null); + performPreferredNotification("chat message received", origin, null); break; case FACEBOOK: - performPreferredNotification("chat message received", ORIGIN_FACEBOOK, null); + performPreferredNotification("chat message received", origin, null); break; + case UNDEFINED: default: - performPreferredNotification("generic notification received", ORIGIN_GENERIC, null); + performPreferredNotification("generic notification received", origin, null); } } diff --git a/app/src/main/res/xml/miband_preferences.xml b/app/src/main/res/xml/miband_preferences.xml index be0fa494..ff6816a5 100644 --- a/app/src/main/res/xml/miband_preferences.xml +++ b/app/src/main/res/xml/miband_preferences.xml @@ -70,7 +70,7 @@ android:title="@string/pref_title_notifications_repetitions" /> @@ -104,18 +104,18 @@ android:defaultValue="@string/p_medium" android:entries="@array/vibration_profile" android:entryValues="@array/vibration_profile_values" - android:key="mi_vibration_profile_k9mail" + android:key="mi_vibration_profile_email" android:title="@string/miband_prefs_vibration" android:summary="%s" /> @@ -139,7 +139,7 @@ android:maxLength="1" android:title="@string/pref_title_notifications_repetitions" /> @@ -163,7 +163,7 @@ android:maxLength="1" android:title="@string/pref_title_notifications_repetitions" /> @@ -187,7 +187,7 @@ android:maxLength="1" android:title="@string/pref_title_notifications_repetitions" /> @@ -211,7 +211,7 @@ android:maxLength="1" android:title="@string/pref_title_notifications_repetitions" /> @@ -224,18 +224,18 @@ android:defaultValue="@string/p_waterdrop" android:entries="@array/vibration_profile" android:entryValues="@array/vibration_profile_values" - android:key="mi_vibration_profile_generic" + android:key="mi_vibration_profile_undefined" android:title="@string/miband_prefs_vibration" android:summary="%s" />