1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-28 21:06:50 +01:00

move transliteration to per device to DeviceCommunicationService, plus migration, fix #2237

This commit is contained in:
vanous 2021-03-21 19:46:30 +01:00
parent bdf48cfff2
commit 9fc02ff09b
11 changed files with 85 additions and 36 deletions

View File

@ -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<Device> 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();

View File

@ -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()

View File

@ -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";
}
}

View File

@ -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;

View File

@ -52,7 +52,7 @@ import static nodomain.freeyourgadget.gadgetbridge.util.JavaExtensions.coalesce;
public class GBDeviceService implements DeviceService {
protected final Context mContext;
private final Class<? extends Service> 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) {

View File

@ -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<String> 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("")) {

View File

@ -18,16 +18,24 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. */
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<Character, String> transliterateMap = new HashMap<Character, String>() {
@ -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

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="M12.65 15.67c0.14-0.36 0.05-0.77-0.23-1.05l-2.09-2.06 0.03-0.03c1.74-1.94 2.98-4.17 3.71-6.53h1.94C16.55 6 17 5.55 17 5.01V4.99C17 4.45 16.55 4 16.01 4H10V3c0-0.55-0.45-1-1-1S8 2.45 8 3v1H1.99C1.45 4 1 4.45 1 4.99c0 0.55 0.45 0.99 0.99 0.99h10.18C11.5 7.92 10.44 9.75 9 11.35c-0.81-0.89-1.49-1.86-2.06-2.88C6.78 8.18 6.49 8 6.16 8 5.47 8 5.03 8.75 5.37 9.35c0.63 1.13 1.4 2.21 2.3 3.21L3.3 16.87c-0.4 0.39-0.4 1.03 0 1.42 0.39 0.39 1.02 0.39 1.42 0L9 14l2.02 2.02c0.51 0.51 1.38 0.32 1.63-0.35zM17.5 10c-0.6 0-1.14 0.37-1.35 0.94l-3.67 9.8C12.24 21.35 12.7 22 13.35 22c0.39 0 0.74-0.24 0.88-0.61L15.12 19h4.75l0.9 2.39c0.14 0.36 0.49 0.61 0.88 0.61 0.65 0 1.11-0.65 0.88-1.26l-3.67-9.8C18.64 10.37 18.1 10 17.5 10zm-1.62 7l1.62-4.33L19.12 17h-3.24z" />
</vector>

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<SwitchPreference
android:defaultValue="false"
android:key="pref_transliteration_enabled"
android:icon="@drawable/ic_translate"
android:summary="@string/pref_summary_transliteration"
android:title="@string/pref_title_transliteration" />
</androidx.preference.PreferenceScreen>

View File

@ -56,13 +56,6 @@
android:key="measurement_system"
android:summary="%s"
android:title="@string/pref_title_unit_system" />
<CheckBoxPreference
android:layout="@layout/preference_checkbox"
android:defaultValue="false"
android:key="transliteration"
android:summary="@string/pref_summary_transliteration"
android:title="@string/pref_title_transliteration" />
<PreferenceScreen
android:title="@string/preferences_rtl_settings">
<CheckBoxPreference

View File

@ -5,17 +5,30 @@ import android.content.SharedPreferences;
import org.junit.Test;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.util.LanguageUtils;
import nodomain.freeyourgadget.gadgetbridge.util.KoreanLanguageUtils;
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst.PREF_TRANSLITERATION_ENABLED;
/**
* Tests LanguageUtils
*/
public class LanguageUtilsTest extends TestBase {
private GBDevice dummyGBDevice;
@Override
public void setUp() throws Exception {
super.setUp();
dummyGBDevice = createDummyGDevice("00:00:00:00:10");
}
@Test
public void testStringTransliterateCyrillic() throws Exception {
// input with cyrillic and diacritic letters
@ -137,23 +150,28 @@ public class LanguageUtilsTest extends TestBase {
public void testTransliterateOption() throws Exception {
setDefaultTransliteration();
assertFalse("Transliteration option fail! Expected 'Off' by default, but result is 'On'",
LanguageUtils.transliterate());
getTransliteration());
enableTransliteration(true);
assertTrue("Transliteration option fail! Expected 'On', but result is 'Off'", LanguageUtils.transliterate());
assertTrue("Transliteration option fail! Expected 'On', but result is 'Off'", getTransliteration());
}
private void setDefaultTransliteration() {
SharedPreferences settings = GBApplication.getPrefs().getPreferences();
SharedPreferences.Editor editor = settings.edit();
editor.remove("transliteration");
SharedPreferences devicePrefs = GBApplication.getDeviceSpecificSharedPrefs(dummyGBDevice.getAddress());
SharedPreferences.Editor editor = devicePrefs.edit();
editor.remove(PREF_TRANSLITERATION_ENABLED);
editor.apply();
}
private void enableTransliteration(boolean enable) {
SharedPreferences settings = GBApplication.getPrefs().getPreferences();
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("transliteration", enable);
SharedPreferences devicePrefs = GBApplication.getDeviceSpecificSharedPrefs(dummyGBDevice.getAddress());
SharedPreferences.Editor editor = devicePrefs.edit();
editor.putBoolean(PREF_TRANSLITERATION_ENABLED, enable);
editor.apply();
}
private boolean getTransliteration(){
Prefs devicePrefs = new Prefs(GBApplication.getDeviceSpecificSharedPrefs(dummyGBDevice.getAddress()));
return devicePrefs.getBoolean(PREF_TRANSLITERATION_ENABLED, false);
}
}