From 9fc02ff09b834ba5d5617419b8ee5449a36f8149 Mon Sep 17 00:00:00 2001 From: vanous Date: Sun, 21 Mar 2021 19:46:30 +0100 Subject: [PATCH] move transliteration to per device to DeviceCommunicationService, plus migration, fix #2237 --- .../gadgetbridge/GBApplication.java | 12 ++++++- .../DeviceSettingsActivity.java | 1 + .../DeviceSettingsPreferenceConst.java | 3 +- .../DeviceSpecificSettingsFragment.java | 2 ++ .../gadgetbridge/impl/GBDeviceService.java | 9 +---- .../service/DeviceCommunicationService.java | 15 +++++++- .../gadgetbridge/util/LanguageUtils.java | 19 +++++------ app/src/main/res/drawable/ic_translate.xml | 10 ++++++ .../xml/devicesettings_transliteration.xml | 9 +++++ app/src/main/res/xml/preferences.xml | 7 ---- .../gadgetbridge/test/LanguageUtilsTest.java | 34 ++++++++++++++----- 11 files changed, 85 insertions(+), 36 deletions(-) create mode 100644 app/src/main/res/drawable/ic_translate.xml create mode 100644 app/src/main/res/xml/devicesettings_transliteration.xml diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java index 2995bb641..e7ad9f050 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java @@ -107,7 +107,7 @@ public class GBApplication extends Application { private static SharedPreferences sharedPrefs; private static final String PREFS_VERSION = "shared_preferences_version"; //if preferences have to be migrated, increment the following and add the migration logic in migratePrefs below; see http://stackoverflow.com/questions/16397848/how-can-i-migrate-android-preferences-with-a-new-version - private static final int CURRENT_PREFS_VERSION = 8; + private static final int CURRENT_PREFS_VERSION = 9; private static final int ERROR_IN_GADGETBRIDGE_NOTIFICATION = 42; @@ -941,6 +941,16 @@ public class GBApplication extends Application { } } } + if (oldVersion < 9) { + try (DBHandler db = acquireDB()) { + DaoSession daoSession = db.getDaoSession(); + List activeDevices = DBHelper.getActiveDevices(daoSession); + migrateBooleanPrefToPerDevicePref("transliteration", false, "pref_transliteration_enabled", (ArrayList)activeDevices); + Log.w(TAG, "migrating transliteration settings"); + } catch (Exception e) { + Log.w(TAG, "error acquiring DB lock and migrating prefs"); + } + } editor.putString(PREFS_VERSION, Integer.toString(CURRENT_PREFS_VERSION)); editor.apply(); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/devicesettings/DeviceSettingsActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/devicesettings/DeviceSettingsActivity.java index 036ab65fe..a3753f862 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/devicesettings/DeviceSettingsActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/devicesettings/DeviceSettingsActivity.java @@ -53,6 +53,7 @@ public class DeviceSettingsActivity extends AbstractGBActivity implements if (coordinator.supportsActivityTracking()) { supportedSettings = ArrayUtils.addAll(supportedSettings, R.xml.devicesettings_chartstabs); } + supportedSettings = ArrayUtils.addAll(supportedSettings, R.xml.devicesettings_transliteration); fragment = DeviceSpecificSettingsFragment.newInstance(device.getAddress(), supportedSettings); } getSupportFragmentManager() diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/devicesettings/DeviceSettingsPreferenceConst.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/devicesettings/DeviceSettingsPreferenceConst.java index ee90563ba..51f2d10e7 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/devicesettings/DeviceSettingsPreferenceConst.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/devicesettings/DeviceSettingsPreferenceConst.java @@ -70,6 +70,7 @@ public class DeviceSettingsPreferenceConst { public static final String PREF_SONYSWR12_SMART_INTERVAL = "smart_alarm_interval_preference"; public static final String PREF_BT_CONNECTED_ADVERTISEMENT = "bt_connected_advertisement"; + public static final String PREF_TRANSLITERATION_ENABLED = "pref_transliteration_enabled"; public static final String PREF_SOUNDS = "sounds"; -} \ No newline at end of file +} diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/devicesettings/DeviceSpecificSettingsFragment.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/devicesettings/DeviceSpecificSettingsFragment.java index 3ea7311ca..da64750f7 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/devicesettings/DeviceSpecificSettingsFragment.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/devicesettings/DeviceSpecificSettingsFragment.java @@ -39,6 +39,7 @@ import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiConst; import nodomain.freeyourgadget.gadgetbridge.devices.makibeshr3.MakibesHR3Constants; import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst; +import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.model.CannedMessagesSpec; import nodomain.freeyourgadget.gadgetbridge.util.Prefs; import nodomain.freeyourgadget.gadgetbridge.util.XTimePreference; @@ -85,6 +86,7 @@ import static nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.Dev import static nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst.PREF_SONYSWR12_STAMINA; import static nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst.PREF_SOUNDS; import static nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst.PREF_TIMEFORMAT; +import static nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst.PREF_TRANSLITERATION_ENABLED; import static nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst.PREF_VIBRATION_STRENGH_PERCENTAGE; import static nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst.PREF_WEARLOCATION; import static nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiConst.PREF_ACTIVATE_DISPLAY_ON_LIFT; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/impl/GBDeviceService.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/impl/GBDeviceService.java index e6f877767..03de65e22 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/impl/GBDeviceService.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/impl/GBDeviceService.java @@ -52,7 +52,7 @@ import static nodomain.freeyourgadget.gadgetbridge.util.JavaExtensions.coalesce; public class GBDeviceService implements DeviceService { protected final Context mContext; private final Class mServiceClass; - private final String[] transliterationExtras = new String[]{ + public static final String[] transliterationExtras = new String[]{ EXTRA_NOTIFICATION_PHONENUMBER, EXTRA_NOTIFICATION_SENDER, EXTRA_NOTIFICATION_SUBJECT, @@ -78,13 +78,6 @@ public class GBDeviceService implements DeviceService { } protected void invokeService(Intent intent) { - if (LanguageUtils.transliterate()) { - for (String extra : transliterationExtras) { - if (intent.hasExtra(extra)) { - intent.putExtra(extra, LanguageUtils.transliterate(intent.getStringExtra(extra))); - } - } - } if (RtlUtils.rtlSupport()) { for (String extra : transliterationExtras) { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java index 65b821341..da6104a87 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java @@ -65,6 +65,7 @@ import nodomain.freeyourgadget.gadgetbridge.externalevents.SMSReceiver; import nodomain.freeyourgadget.gadgetbridge.externalevents.TimeChangeReceiver; import nodomain.freeyourgadget.gadgetbridge.externalevents.TinyWeatherForecastGermanyReceiver; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; +import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceService; import nodomain.freeyourgadget.gadgetbridge.model.Alarm; import nodomain.freeyourgadget.gadgetbridge.model.CalendarEventSpec; import nodomain.freeyourgadget.gadgetbridge.model.CallSpec; @@ -80,8 +81,10 @@ import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper; import nodomain.freeyourgadget.gadgetbridge.util.EmojiConverter; import nodomain.freeyourgadget.gadgetbridge.util.GB; import nodomain.freeyourgadget.gadgetbridge.util.GBPrefs; +import nodomain.freeyourgadget.gadgetbridge.util.LanguageUtils; import nodomain.freeyourgadget.gadgetbridge.util.Prefs; +import static nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst.PREF_TRANSLITERATION_ENABLED; import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_ADD_CALENDAREVENT; import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_APP_CONFIGURE; import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_APP_REORDER; @@ -380,6 +383,17 @@ public class DeviceCommunicationService extends Service implements SharedPrefere } private void handleAction(Intent intent, String action, Prefs prefs) { + Prefs devicePrefs = new Prefs(GBApplication.getDeviceSpecificSharedPrefs(mGBDevice.getAddress())); + boolean transliterate = devicePrefs.getBoolean(PREF_TRANSLITERATION_ENABLED, false); + + if (transliterate) { + for (String extra : GBDeviceService.transliterationExtras) { + if (intent.hasExtra(extra)) { + intent.putExtra(extra, LanguageUtils.transliterate(intent.getStringExtra(extra))); + } + } + } + switch (action) { case ACTION_REQUEST_DEVICEINFO: mGBDevice.sendDeviceUpdateIntent(this); @@ -410,7 +424,6 @@ public class DeviceCommunicationService extends Service implements SharedPrefere // I would rather like to save that as an array in SharedPreferences // this would work but I dont know how to do the same in the Settings Activity's xml ArrayList replies = new ArrayList<>(); - SharedPreferences devicePrefs = GBApplication.getDeviceSpecificSharedPrefs(mGBDevice.getAddress()); for (int i = 1; i <= 16; i++) { String reply = devicePrefs.getString("canned_reply_" + i, null); if (reply != null && !reply.equals("")) { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/LanguageUtils.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/LanguageUtils.java index 212832817..46a0e84c1 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/LanguageUtils.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/LanguageUtils.java @@ -18,16 +18,24 @@ along with this program. If not, see . */ package nodomain.freeyourgadget.gadgetbridge.util; +import static nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst.PREF_TRANSLITERATION_ENABLED; + +import android.content.SharedPreferences; + import org.apache.commons.lang3.text.WordUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.text.Normalizer; import java.util.HashMap; import java.util.Map; import nodomain.freeyourgadget.gadgetbridge.GBApplication; +import nodomain.freeyourgadget.gadgetbridge.R; +import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; public class LanguageUtils { - + private static final Logger LOG = LoggerFactory.getLogger(LanguageUtils.class); // Transliteration map with english equivalent for unsupported chars @SuppressWarnings("OverwrittenKey") private static final Map transliterateMap = new HashMap() { @@ -105,15 +113,6 @@ public class LanguageUtils { } }; - /** - * Checks the status of transliteration option - * @return true if transliterate option is On, and false, if Off or not exist - */ - public static boolean transliterate() - { - return GBApplication.getPrefs().getBoolean("transliteration", false); - } - /** * Replaces unsupported symbols to english * @param txt input text diff --git a/app/src/main/res/drawable/ic_translate.xml b/app/src/main/res/drawable/ic_translate.xml new file mode 100644 index 000000000..9529ce2fd --- /dev/null +++ b/app/src/main/res/drawable/ic_translate.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/xml/devicesettings_transliteration.xml b/app/src/main/res/xml/devicesettings_transliteration.xml new file mode 100644 index 000000000..20b578c8c --- /dev/null +++ b/app/src/main/res/xml/devicesettings_transliteration.xml @@ -0,0 +1,9 @@ + + + + \ 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 ab4a1f9f9..f2bb175a5 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -56,13 +56,6 @@ android:key="measurement_system" android:summary="%s" android:title="@string/pref_title_unit_system" /> - -