Mi2: Add config option to configure date/time display

(added a new EventHandler method to set a specific configuration option)
This commit is contained in:
cpfeiffer 2016-11-13 20:47:24 +01:00
parent ddaf51768d
commit d89899557c
21 changed files with 149 additions and 11 deletions

View File

@ -67,5 +67,12 @@ public interface EventHandler {
void onDeleteCalendarEvent(byte type, long id);
/**
* Sets the given option in the device, typically with values from the preferences.
* The config name is device specific.
* @param config the device specific option to set on the device
*/
void onSendConfiguration(String config);
void onTestNewFunction();
}

View File

@ -0,0 +1,6 @@
package nodomain.freeyourgadget.gadgetbridge.devices.miband;
public enum DateTimeDisplay {
TIME,
DATE_TIME
}

View File

@ -7,10 +7,13 @@ import android.net.Uri;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.devices.InstallHandler;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceCandidate;
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
public class MiBand2Coordinator extends MiBandCoordinator {
private static final Logger LOG = LoggerFactory.getLogger(MiBand2Coordinator.class);
@ -55,4 +58,13 @@ public class MiBand2Coordinator extends MiBandCoordinator {
public InstallHandler findInstallHandler(Uri uri, Context context) {
return null; // not supported at the moment
}
public static DateTimeDisplay getDateDisplay(Context context) throws IllegalArgumentException {
Prefs prefs = GBApplication.getPrefs();
String dateFormatTime = context.getString(R.string.p_dateformat_time);
if (dateFormatTime.equals(prefs.getString(MiBandConst.PREF_MI2_DATEFORMAT, dateFormatTime))) {
return DateTimeDisplay.TIME;
}
return DateTimeDisplay.DATE_TIME;
}
}

View File

@ -17,6 +17,7 @@ public final class MiBandConst {
public static final String PREF_MIBAND_RESERVE_ALARM_FOR_CALENDAR = "mi_reserve_alarm_calendar";
public static final String PREF_MIBAND_USE_HR_FOR_SLEEP_DETECTION = "mi_hr_sleep_detection";
public static final String PREF_MIBAND_DEVICE_TIME_OFFSET_HOURS = "mi_device_time_offset_hours";
public static final String PREF_MI2_DATEFORMAT = "mi2_dateformat";
public static final String PREF_TRY_SMS = "mi_try_sms";

View File

@ -18,6 +18,7 @@ import nodomain.freeyourgadget.gadgetbridge.model.NotificationType;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.ORIGIN_INCOMING_CALL;
import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PREF_MI2_DATEFORMAT;
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;
@ -44,8 +45,22 @@ public class MiBandPreferencesActivity extends AbstractSettingsActivity {
return true;
}
});
final Preference setDateFormat = findPreference(PREF_MI2_DATEFORMAT);
setDateFormat.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newVal) {
getListView().post(new Runnable() { // delayed execution so that the preferences are applied first
@Override
public void run() {
GBApplication.deviceService().onSendConfiguration(PREF_MI2_DATEFORMAT);
}
});
return true;
}
});
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
final Preference developmentMiaddr = findPreference(PREF_MIBAND_ADDRESS);

View File

@ -281,6 +281,13 @@ public class GBDeviceService implements DeviceService {
invokeService(intent);
}
@Override
public void onSendConfiguration(String config) {
Intent intent = createIntent().setAction(ACTION_SEND_CONFIGURATION)
.putExtra(EXTRA_CONFIG, config);
invokeService(intent);
}
@Override
public void onTestNewFunction() {
Intent intent = createIntent().setAction(ACTION_TEST_NEW_FUNCTION);

View File

@ -48,6 +48,7 @@ public interface DeviceService extends EventHandler {
String ACTION_HEARTRATE_MEASUREMENT = PREFIX + ".action.hr_measurement";
String ACTION_ADD_CALENDAREVENT = PREFIX + ".action.add_calendarevent";
String ACTION_DELETE_CALENDAREVENT = PREFIX + ".action.delete_calendarevent";
String ACTION_SEND_CONFIGURATION = PREFIX + ".action.send_configuration";
String ACTION_TEST_NEW_FUNCTION = PREFIX + ".action.test_new_function";
String EXTRA_DEVICE_ADDRESS = "device_address";
String EXTRA_NOTIFICATION_BODY = "notification_body";
@ -80,6 +81,7 @@ public interface DeviceService extends EventHandler {
String EXTRA_APP_START = "app_start";
String EXTRA_APP_CONFIG = "app_config";
String EXTRA_URI = "uri";
String EXTRA_CONFIG = "config";
String EXTRA_ALARMS = "alarms";
String EXTRA_PERFORM_PAIR = "perform_pair";
String EXTRA_BOOLEAN_ENABLE = "enable_realtime_steps";

View File

@ -72,6 +72,7 @@ import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_RE
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_REQUEST_APPINFO;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_REQUEST_DEVICEINFO;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_REQUEST_SCREENSHOT;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_SEND_CONFIGURATION;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_SETCANNEDMESSAGES;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_SETMUSICINFO;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_SETMUSICSTATE;
@ -96,6 +97,7 @@ import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_CAL
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_CALL_PHONENUMBER;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_CANNEDMESSAGES;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_CANNEDMESSAGES_TYPE;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_CONFIG;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_DEVICE_ADDRESS;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_FIND_START;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_MUSIC_ALBUM;
@ -485,6 +487,11 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
mDeviceSupport.onEnableRealtimeHeartRateMeasurement(enable);
break;
}
case ACTION_SEND_CONFIGURATION: {
String config = intent.getStringExtra(EXTRA_CONFIG);
mDeviceSupport.onSendConfiguration(config);
break;
}
case ACTION_TEST_NEW_FUNCTION: {
mDeviceSupport.onTestNewFunction();
break;

View File

@ -320,6 +320,14 @@ public class ServiceDeviceSupport implements DeviceSupport {
delegate.onDeleteCalendarEvent(type, id);
}
@Override
public void onSendConfiguration(String config) {
if (checkBusy("send configuration: " + config)) {
return;
}
delegate.onSendConfiguration(config);
}
@Override
public void onTestNewFunction() {
if (checkBusy("test new function event")) {

View File

@ -26,6 +26,7 @@ import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventBatteryInfo;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventVersionInfo;
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBand2Coordinator;
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBand2Service;
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst;
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandCoordinator;
@ -56,7 +57,7 @@ import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceStateA
import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.WriteAction;
import nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.deviceinfo.DeviceInfoProfile;
import nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.heartrate.HeartRateProfile;
import nodomain.freeyourgadget.gadgetbridge.service.devices.miband2.DateDisplay;
import nodomain.freeyourgadget.gadgetbridge.devices.miband.DateTimeDisplay;
import nodomain.freeyourgadget.gadgetbridge.service.devices.miband2.Mi2NotificationStrategy;
import nodomain.freeyourgadget.gadgetbridge.service.devices.miband2.operations.InitOperation;
import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils;
@ -401,6 +402,7 @@ public class MiBand2Support extends AbstractBTLEDeviceSupport {
LOG.info("Attempting to set wear location...");
BluetoothGattCharacteristic characteristic = getCharacteristic(MiBand2Service.UUID_UNKNOWN_CHARACTERISTIC8);
if (characteristic != null) {
builder.notify(characteristic, true);
int location = MiBandCoordinator.getWearLocation(getDevice().getAddress());
switch (location) {
case 0: // left hand
@ -410,6 +412,7 @@ public class MiBand2Support extends AbstractBTLEDeviceSupport {
builder.write(characteristic, MiBand2Service.WEAR_LOCATION_RIGHT_WRIST);
break;
}
builder.notify(characteristic, false); // TODO: this should actually be in some kind of finally-block in the queue
}
return this;
}
@ -444,6 +447,7 @@ public class MiBand2Support extends AbstractBTLEDeviceSupport {
BluetoothGattCharacteristic characteristicHRControlPoint = getCharacteristic(GattCharacteristic.UUID_CHARACTERISTIC_HEART_RATE_CONTROL_POINT);
final boolean enableHrSleepSupport = MiBandCoordinator.getHeartrateSleepSupport(getDevice().getAddress());
if (characteristicHRControlPoint != null) {
builder.notify(characteristicHRControlPoint, true);
if (enableHrSleepSupport) {
LOG.info("Enabling heartrate sleep support...");
builder.write(characteristicHRControlPoint, MiBand2Service.COMMAND_ENABLE_HR_SLEEP_MEASUREMENT);
@ -451,6 +455,7 @@ public class MiBand2Support extends AbstractBTLEDeviceSupport {
LOG.info("Disabling heartrate sleep support...");
builder.write(characteristicHRControlPoint, MiBand2Service.COMMAND_DISABLE_HR_SLEEP_MEASUREMENT);
}
builder.notify(characteristicHRControlPoint, false); // TODO: this should run in some kind of finally-block in the queue
}
return this;
}
@ -1154,13 +1159,30 @@ public class MiBand2Support extends AbstractBTLEDeviceSupport {
}
}
@Override
public void onSendConfiguration(String config) {
TransactionBuilder builder = null;
try {
builder = performInitialized("Sending configuration for option: " + config);
switch (config) {
case MiBandConst.PREF_MI2_DATEFORMAT:
setDateDisplay(builder);
break;
}
builder.queue(getQueue());
} catch (IOException e) {
GB.toast("Error setting configuration", Toast.LENGTH_LONG, GB.ERROR, e);
}
}
@Override
public void onTestNewFunction() {
}
public MiBand2Support setDateDisplay(DateDisplay displayConfig, TransactionBuilder builder) {
LOG.info("Setting date display to " + displayConfig);
switch (displayConfig) {
private MiBand2Support setDateDisplay(TransactionBuilder builder) {
DateTimeDisplay dateTimeDisplay = MiBand2Coordinator.getDateDisplay(getContext());
LOG.info("Setting date display to " + dateTimeDisplay);
switch (dateTimeDisplay) {
case TIME:
builder.write(getCharacteristic(MiBand2Service.UUID_UNKNOWN_CHARACTERISTIC3), MiBand2Service.DATEFORMAT_TIME);
break;
@ -1174,8 +1196,9 @@ public class MiBand2Support extends AbstractBTLEDeviceSupport {
public void phase2Initialize(TransactionBuilder builder) {
LOG.info("phase2Initialize...");
enableFurtherNotifications(builder, true);
setDateDisplay(DateDisplay.TIME, builder);
setDateDisplay(builder);
setWearLocation(builder);
setHeartrateSleepSupport(builder);
}
}

View File

@ -1190,6 +1190,11 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport {
}
}
@Override
public void onSendConfiguration(String config) {
// nothing yet
}
@Override
public void onTestNewFunction() {
try {

View File

@ -1,6 +0,0 @@
package nodomain.freeyourgadget.gadgetbridge.service.devices.miband2;
public enum DateDisplay {
TIME,
DATE_TIME
}

View File

@ -157,6 +157,13 @@ public class PebbleSupport extends AbstractSerialDeviceSupport {
}
}
@Override
public void onSendConfiguration(String config) {
if (reconnect()) {
super.onSendConfiguration(config);
}
}
@Override
public void onTestNewFunction() {
if (reconnect()) {

View File

@ -272,6 +272,11 @@ public class VibratissimoSupport extends AbstractBTLEDeviceSupport {
return false;
}
@Override
public void onSendConfiguration(String config) {
}
@Override
public void onTestNewFunction() {

View File

@ -215,6 +215,12 @@ public abstract class AbstractSerialDeviceSupport extends AbstractDeviceSupport
sendToDevice(bytes);
}
@Override
public void onSendConfiguration(String config) {
byte[] bytes = gbDeviceProtocol.encodeSendConfiguration(config);
sendToDevice(bytes);
}
@Override
public void onTestNewFunction() {
byte[] bytes = gbDeviceProtocol.encodeTestNewFunction();

View File

@ -94,6 +94,10 @@ public abstract class GBDeviceProtocol {
return null;
}
public byte[] encodeSendConfiguration(String config) {
return null;
}
public byte[] encodeTestNewFunction() { return null; }
public GBDeviceEvent[] decodeResponse(byte[] responseData) {
@ -103,4 +107,5 @@ public abstract class GBDeviceProtocol {
public GBDevice getDevice() {
return mDevice;
}
}

View File

@ -113,5 +113,13 @@
<item>3</item>
<item>1</item>
</string-array>
<string-array name="mi2_dateformats">
<item>@string/dateformat_time</item>
<item>@string/dateformat_date_time</item>
</string-array>
<string-array name="mi2_dateformats_values">
<item>@string/p_dateformat_time</item>
<item>@string/p_dateformat_datetime</item>
</string-array>
</resources>

View File

@ -345,4 +345,7 @@
<!-- Strings related to Vibration Activity -->
<string name="title_activity_vibration">Vibration</string>
<string name="miband2_prefs_dateformat">Mi2: Date Format</string>
<string name="dateformat_time">Time</string>
<string name="dateformat_date_time"><![CDATA[Time & Date]]></string>
</resources>

View File

@ -9,4 +9,7 @@
<item name="p_ring" type="string">ring</item>
<item name="p_alarm_clock" type="string">alarm_clock</item>
<item name="p_dateformat_time" type="string">dateformat_time</item>
<item name="p_dateformat_datetime" type="string">dateformat_datetime</item>
</resources>

View File

@ -43,6 +43,15 @@
android:maxLength="2"
android:title="@string/miband_prefs_device_time_offset_hours" />
<ListPreference
android:defaultValue="@string/p_dateformat_time"
android:entries="@array/mi2_dateformats"
android:entryValues="@array/mi2_dateformats_values"
android:key="mi2_dateformat"
android:title="@string/miband2_prefs_dateformat"
android:summary="%s" />
</PreferenceCategory>
<PreferenceCategory

View File

@ -168,6 +168,11 @@ public class TestDeviceSupport extends AbstractDeviceSupport {
}
@Override
public void onSendConfiguration(String config) {
}
@Override
public void onTestNewFunction() {