Lefun: Use existing time format setting with handling for automatic format

Also fix "interface language" string name
This commit is contained in:
Yukai Li 2020-10-06 09:22:04 -06:00 committed by Gitea
parent bc33b4b6e0
commit d6bed776c5
5 changed files with 61 additions and 21 deletions

View File

@ -163,7 +163,7 @@ public class LefunDeviceCoordinator extends AbstractDeviceCoordinator {
public int[] getSupportedDeviceSpecificSettings(GBDevice device) {
return new int[]{
R.xml.devicesettings_liftwrist_display_noshed,
R.xml.devicesettings_ampm,
R.xml.devicesettings_timeformat,
R.xml.devicesettings_antilost,
R.xml.devicesettings_longsit,
R.xml.devicesettings_hydration_reminder,

View File

@ -23,6 +23,7 @@ import android.bluetooth.BluetoothGattCharacteristic;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.text.format.DateFormat;
import android.widget.Toast;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
@ -147,6 +148,7 @@ public class LefunDeviceSupport extends AbstractBTLEDeviceSupport {
batReq.perform();
inProgressRequests.add(batReq);
sendAmPmSettingIfNecessary(builder);
sendUnitsSetting(builder);
sendUserProfile(builder);
} catch (IOException e) {
@ -386,10 +388,8 @@ public class LefunDeviceSupport extends AbstractBTLEDeviceSupport {
public void onSendConfiguration(String config) {
SharedPreferences prefs = GBApplication.getDeviceSpecificSharedPrefs(getDevice().getAddress());
switch (config) {
case DeviceSettingsPreferenceConst.PREF_AMPM_ENABLED: {
boolean enabled = prefs.getBoolean(DeviceSettingsPreferenceConst.PREF_AMPM_ENABLED, false);
byte ampmValue = enabled ? SettingsCommand.AM_PM_12_HOUR : SettingsCommand.AM_PM_24_HOUR;
sendGeneralSettings(null, ampmValue, (byte) 0xff);
case DeviceSettingsPreferenceConst.PREF_TIMEFORMAT: {
sendAmPmSetting(null);
break;
}
case DeviceSettingsPreferenceConst.PREF_LIFTWRIST_NOSHED: {
@ -474,6 +474,47 @@ public class LefunDeviceSupport extends AbstractBTLEDeviceSupport {
sendGeneralSettings(builder, (byte) 0xff, lefunUnits);
}
/**
* Send AM/PM indicator setting based on time format pref
*
* @param builder the transaction builder to append to
*/
private void sendAmPmSetting(TransactionBuilder builder) {
SharedPreferences prefs = GBApplication.getDeviceSpecificSharedPrefs(getDevice().getAddress());
String ampmSetting = prefs.getString(DeviceSettingsPreferenceConst.PREF_TIMEFORMAT,
getContext().getString(R.string.p_timeformat_auto));
byte ampmDeviceSetting = (byte) 0xff;
if (getContext().getString(R.string.p_timeformat_auto).equals(ampmSetting)) {
if (DateFormat.is24HourFormat(getContext())) {
ampmDeviceSetting = SettingsCommand.AM_PM_24_HOUR;
} else {
ampmDeviceSetting = SettingsCommand.AM_PM_12_HOUR;
}
} else if (getContext().getString(R.string.p_timeformat_24h).equals(ampmSetting)) {
ampmDeviceSetting = SettingsCommand.AM_PM_24_HOUR;
} else if (getContext().getString(R.string.p_timeformat_am_pm).equals(ampmSetting)) {
ampmDeviceSetting = SettingsCommand.AM_PM_12_HOUR;
}
sendGeneralSettings(builder, ampmDeviceSetting, (byte) 0xff);
}
/**
* Send AM/PM indicator setting only if time format pref is set to auto
*
* @param builder the transaction builder to append to
*/
private void sendAmPmSettingIfNecessary(TransactionBuilder builder) {
SharedPreferences prefs = GBApplication.getDeviceSpecificSharedPrefs(getDevice().getAddress());
String ampmSetting = prefs.getString(DeviceSettingsPreferenceConst.PREF_TIMEFORMAT,
getContext().getString(R.string.p_timeformat_auto));
if (getContext().getString(R.string.p_timeformat_auto).equals(ampmSetting)) {
sendAmPmSetting(builder);
}
}
/**
* Gets a features command with the currently enabled features set
*
@ -628,9 +669,19 @@ public class LefunDeviceSupport extends AbstractBTLEDeviceSupport {
public void receiveGeneralSettings(int amPm, int units) {
SharedPreferences prefs = GBApplication.getDeviceSpecificSharedPrefs(getDevice().getAddress());
boolean ampmEnabled = amPm == SettingsCommand.AM_PM_12_HOUR;
prefs.edit()
.putBoolean(DeviceSettingsPreferenceConst.PREF_AMPM_ENABLED, ampmEnabled)
.apply();
String currAmpmSetting = prefs.getString(DeviceSettingsPreferenceConst.PREF_TIMEFORMAT,
getContext().getString(R.string.p_timeformat_auto));
SharedPreferences.Editor editor = prefs.edit();
// Only update AM/PM indicator setting if it is not currently set to auto
if (!getContext().getString(R.string.p_timeformat_auto).equals(currAmpmSetting)) {
String ampmValue = getContext().getString(ampmEnabled ? R.string.p_timeformat_am_pm
: R.string.p_timeformat_24h);
editor.putString(DeviceSettingsPreferenceConst.PREF_TIMEFORMAT, ampmValue);
}
editor.apply();
}
/**

View File

@ -552,9 +552,7 @@
<string name="lefun_prefs_hydration_reminder_interval_title">Hydration reminder interval (in minutes)</string>
<string name="lefun_prefs_antilost_title">Anti-loss</string>
<string name="lefun_prefs_antilost_summary">The band will vibrate if your phone disconnects from the band</string>
<string name="lefun_prefs_ampm_title">12-hour display</string>
<string name="lefun_prefs_ampm_summary">Display time in 12-hour format with AM/PM indicator</string>
<string name="lefun_interface_language_title">Interface language</string>
<string name="lefun_prefs_interface_language_title">Interface language</string>
<string name="automatic">Automatic</string>
<string name="simplified_chinese">Simplified Chinese</string>
<string name="traditional_chinese">Traditional Chinese</string>

View File

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

View File

@ -7,5 +7,5 @@
android:icon="@drawable/ic_language"
android:key="pref_lefun_interface_language"
android:summary="%s"
android:title="@string/lefun_interface_language_title" />
android:title="@string/lefun_prefs_interface_language_title" />
</androidx.preference.PreferenceScreen>