From 074394cba49aacdf8d0b19df5208f042f9739a73 Mon Sep 17 00:00:00 2001 From: ivanovlev Date: Sun, 15 Jan 2017 12:24:36 +0300 Subject: [PATCH] Transliteration is moved to a separate class, added settings option --- app/build.gradle | 1 - .../service/devices/hplus/HPlusSupport.java | 65 +++---------------- .../gadgetbridge/util/LanguageUtils.java | 64 ++++++++++++++++++ app/src/main/res/values-ru/strings.xml | 2 + app/src/main/res/values/strings.xml | 2 + app/src/main/res/xml/changelog_master.xml | 3 +- app/src/main/res/xml/preferences.xml | 7 ++ 7 files changed, 87 insertions(+), 57 deletions(-) create mode 100644 app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/LanguageUtils.java diff --git a/app/build.gradle b/app/build.gradle index 7a3175252..b6b68af65 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -77,7 +77,6 @@ dependencies { compile 'org.apache.commons:commons-lang3:3.4' // compile project(":DaoCore") - compile 'com.google.guava:guava:19.0' } preBuild.dependsOn(":GBDaoGenerator:genSources") diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/hplus/HPlusSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/hplus/HPlusSupport.java index aa1836876..319303dbc 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/hplus/HPlusSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/hplus/HPlusSupport.java @@ -14,8 +14,6 @@ import android.net.Uri; import android.support.v4.content.LocalBroadcastManager; import android.widget.Toast; -import com.google.common.collect.ImmutableMap; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -23,7 +21,6 @@ import java.io.IOException; import java.util.ArrayList; import java.util.Calendar; import java.util.GregorianCalendar; -import java.util.Map; import java.util.UUID; import nodomain.freeyourgadget.gadgetbridge.devices.hplus.HPlusConstants; @@ -41,10 +38,10 @@ import nodomain.freeyourgadget.gadgetbridge.service.btle.AbstractBTLEDeviceSuppo import nodomain.freeyourgadget.gadgetbridge.service.btle.GattService; import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder; import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceStateAction; -import nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.AbstractBleProfile; import nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.deviceinfo.DeviceInfo; import nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.deviceinfo.DeviceInfoProfile; import nodomain.freeyourgadget.gadgetbridge.util.GB; +import nodomain.freeyourgadget.gadgetbridge.util.LanguageUtils; public class HPlusSupport extends AbstractBTLEDeviceSupport { @@ -649,7 +646,10 @@ public class HPlusSupport extends AbstractBTLEDeviceSupport { private void showIncomingCall(String name, String number) { try { TransactionBuilder builder = performInitialized("incomingCallIcon"); - name = transliterate(name); + + if (LanguageUtils.transliterate()) { + name = LanguageUtils.transliterate(name); + } //Enable call notifications builder.write(ctrlCharacteristic, new byte[]{HPlusConstants.CMD_ACTION_INCOMING_CALL, 1}); @@ -707,8 +707,11 @@ public class HPlusSupport extends AbstractBTLEDeviceSupport { LOG.debug("Show Notification: "+title+" --> "+body); try { TransactionBuilder builder = performInitialized("notification"); - title = transliterate(title); - body = transliterate(body); + + if (LanguageUtils.transliterate()) { + title = LanguageUtils.transliterate(title); + body = LanguageUtils.transliterate(body); + } byte[] msg = new byte[20]; for (int i = 0; i < msg.length; i++) @@ -783,54 +786,6 @@ public class HPlusSupport extends AbstractBTLEDeviceSupport { } } - //replace unsupported symbols to english analog - private String transliterate(String txt){ - if (txt == null || txt.isEmpty()) { - return txt; - } - - StringBuilder message = new StringBuilder(); - - char[] chars = txt.toCharArray(); - - for (char c : chars) - { - message.append(transliterate(c)); - } - - return message.toString(); - } - - //replace unsupported symbol to english analog text - private String transliterate(char c){ - Map map = ImmutableMap.builder() - .put('а', "a").put('б', "b").put('в', "v").put('г', "g") - .put('д', "d").put('е', "e").put('ё', "jo").put('ж', "zh") - .put('з', "z").put('и', "i").put('й', "jj").put('к', "k") - .put('л', "l").put('м', "m").put('н', "n").put('о', "o") - .put('п', "p").put('р', "r").put('с', "s").put('т', "t") - .put('у', "u").put('ф', "f").put('х', "kh").put('ц', "c") - .put('ч', "ch").put('ш', "sh").put('щ', "shh").put('ъ', "\"") - .put('ы', "y").put('ь', "'").put('э', "eh").put('ю', "ju") - .put('я', "ja") - .build(); - - char lowerChar = Character.toLowerCase(c); - - if (map.containsKey(lowerChar)) { - String replace = map.get(lowerChar); - - if (lowerChar != c) - { - return replace.toUpperCase(); - } - - return replace; - } - - return String.valueOf(c); - } - @Override public boolean onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/LanguageUtils.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/LanguageUtils.java new file mode 100644 index 000000000..ae41ed3f0 --- /dev/null +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/LanguageUtils.java @@ -0,0 +1,64 @@ +package nodomain.freeyourgadget.gadgetbridge.util; + +import java.util.HashMap; +import java.util.Map; + +import nodomain.freeyourgadget.gadgetbridge.GBApplication; + +public class LanguageUtils { + //transliteration map with english equivalent for unsupported chars + private static Map transliterateMap = new HashMap(){ + { + //russian chars + put('а', "a"); put('б', "b"); put('в', "v"); put('г', "g"); put('д', "d"); put('е', "e"); put('ё', "jo"); put('ж', "zh"); + put('з', "z"); put('и', "i"); put('й', "jj"); put('к', "k"); put('л', "l"); put('м', "m"); put('н', "n"); put('о', "o"); + put('п', "p"); put('р', "r"); put('с', "s"); put('т', "t"); put('у', "u"); put('ф', "f"); put('х', "kh"); put('ц', "c"); + put('ч', "ch");put('ш', "sh");put('щ', "shh");put('ъ', "\"");put('ы', "y"); put('ь', "'"); put('э', "eh"); put('ю', "ju"); + put('я', "ja"); + + //continue for other languages... + } + }; + + //check transliterate option status + public static boolean transliterate() + { + return GBApplication.getPrefs().getBoolean("transliteration", true); + } + + //replace unsupported symbols to english analog + public static String transliterate(String txt){ + if (txt == null || txt.isEmpty()) { + return txt; + } + + StringBuilder message = new StringBuilder(); + + char[] chars = txt.toCharArray(); + + for (char c : chars) + { + message.append(transliterate(c)); + } + + return message.toString(); + } + + //replace unsupported symbol to english analog text + private static String transliterate(char c){ + char lowerChar = Character.toLowerCase(c); + + if (transliterateMap.containsKey(lowerChar)) { + String replace = transliterateMap.get(lowerChar); + + if (lowerChar != c) + { + return replace.toUpperCase(); + } + + return replace; + } + + return String.valueOf(c); + } +} diff --git a/app/src/main/res/values-ru/strings.xml b/app/src/main/res/values-ru/strings.xml index 7284db28f..2ed467a6b 100644 --- a/app/src/main/res/values-ru/strings.xml +++ b/app/src/main/res/values-ru/strings.xml @@ -52,6 +52,8 @@ … даже когда экран включён Не беспокоить Предотвращать отправку нежелательных уведомлений в режиме \"Не беспокоить\" + Транслитерация + Используйте, если устройство не может работать с вашим языком всегда когда экран выключен никогда diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 522883d9f..765cc4612 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -80,6 +80,8 @@ … also when screen is on Do Not Disturb Stop unwanted Notifications from being sent based on the Do Not Disturb mode. + Transliteration + Use, if the device can not work with your language always when screen is off diff --git a/app/src/main/res/xml/changelog_master.xml b/app/src/main/res/xml/changelog_master.xml index 34e61a413..672517f60 100644 --- a/app/src/main/res/xml/changelog_master.xml +++ b/app/src/main/res/xml/changelog_master.xml @@ -2,7 +2,8 @@ Add weather support through "Weather Notification" app - Various fixes for K9 mail when using the generic notification receiver + Various fixes for K9 mail when using the generic notification receiver + Added transliteration option for notifications in the settings screen Pebble: Support for build-in weather system app (FW 4.x) Pebble: Add weather support for various watchfaces Pebble: Delete notifications that got dismissed on the phone diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index 43985431d..48eb91d87 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -91,6 +91,13 @@ android:key="notifications_generic_whenscreenon" android:title="@string/pref_title_whenscreenon" /> + +