fixed some syncing issues (hopefully)

This commit is contained in:
dakhnod 2019-11-24 00:44:14 +01:00
parent 463e3d120d
commit 2bba2c449f
6 changed files with 142 additions and 67 deletions

View File

@ -395,61 +395,69 @@ public class ConfigActivity extends AbstractGBActivity {
}
final String buttonJson = device.getDeviceInfo(FossilWatchAdapter.ITEM_BUTTONS).getDetails();
if (buttonJson != null && !buttonJson.isEmpty()) {
try {
final JSONArray buttonConfig = new JSONArray(buttonJson);
LinearLayout buttonLayout = findViewById(R.id.buttonConfigLayout);
buttonLayout.removeAllViews();
findViewById(R.id.buttonOverwriteButtons).setVisibility(View.GONE);
final ConfigPayload[] payloads = ConfigPayload.values();
final String[] names = new String[payloads.length];
for (int i = 0; i < payloads.length; i++)
names[i] = payloads[i].getDescription();
for (int i = 0; i < buttonConfig.length(); i++) {
final int currentIndex = i;
String configName = buttonConfig.getString(i);
TextView buttonTextView = new TextView(ConfigActivity.this);
buttonTextView.setTextColor(Color.WHITE);
buttonTextView.setTextSize(20);
try {
ConfigPayload payload = ConfigPayload.valueOf(configName);
buttonTextView.setText("Button " + (i + 1) + ": " + payload.getDescription());
} catch (IllegalArgumentException e) {
buttonTextView.setText("Button " + (i + 1) + ": Unknown");
}
buttonTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog dialog = new AlertDialog.Builder(ConfigActivity.this)
.setItems(names, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
ConfigPayload selected = payloads[which];
try {
buttonConfig.put(currentIndex, selected.toString());
device.addDeviceInfo(new GenericItem(FossilWatchAdapter.ITEM_BUTTONS, buttonConfig.toString()));
updateSettings();
LocalBroadcastManager.getInstance(ConfigActivity.this).sendBroadcast(new Intent(QHybridSupport.QHYBRID_COMMAND_OVERWRITE_BUTTONS));
} catch (JSONException e) {
e.printStackTrace();
}
}
})
.setTitle("Button " + (currentIndex + 1))
.create();
dialog.show();
}
});
buttonLayout.addView(buttonTextView);
}
} catch (JSONException e) {
e.printStackTrace();
GB.toast("error parsing button config", Toast.LENGTH_LONG, GB.ERROR);
try {
JSONArray buttonConfig_;
if (buttonJson == null || buttonJson.isEmpty()) {
buttonConfig_ = new JSONArray(new String[]{"", "", ""});
}else{
buttonConfig_ = new JSONArray(buttonJson);
}
final JSONArray buttonConfig = buttonConfig_;
LinearLayout buttonLayout = findViewById(R.id.buttonConfigLayout);
buttonLayout.removeAllViews();
findViewById(R.id.buttonOverwriteButtons).setVisibility(View.GONE);
final ConfigPayload[] payloads = ConfigPayload.values();
final String[] names = new String[payloads.length];
for (int i = 0; i < payloads.length; i++)
names[i] = payloads[i].getDescription();
for (int i = 0; i < buttonConfig.length(); i++) {
final int currentIndex = i;
String configName = buttonConfig.getString(i);
TextView buttonTextView = new TextView(ConfigActivity.this);
buttonTextView.setTextColor(Color.WHITE);
buttonTextView.setTextSize(20);
try {
ConfigPayload payload = ConfigPayload.valueOf(configName);
buttonTextView.setText("Button " + (i + 1) + ": " + payload.getDescription());
} catch (IllegalArgumentException e) {
buttonTextView.setText("Button " + (i + 1) + ": Unknown");
}
buttonTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog dialog = new AlertDialog.Builder(ConfigActivity.this)
.setItems(names, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
ConfigPayload selected = payloads[which];
try {
buttonConfig.put(currentIndex, selected.toString());
device.addDeviceInfo(new GenericItem(FossilWatchAdapter.ITEM_BUTTONS, buttonConfig.toString()));
updateSettings();
Intent buttonIntent = new Intent(QHybridSupport.QHYBRID_COMMAND_OVERWRITE_BUTTONS);
buttonIntent.putExtra(FossilWatchAdapter.ITEM_BUTTONS, buttonConfig.toString());
LocalBroadcastManager.getInstance(ConfigActivity.this).sendBroadcast(buttonIntent);
} catch (JSONException e) {
e.printStackTrace();
}
}
})
.setTitle("Button " + (currentIndex + 1))
.create();
dialog.show();
}
});
buttonLayout.addView(buttonTextView);
}
} catch (JSONException e) {
e.printStackTrace();
GB.toast("error parsing button config", Toast.LENGTH_LONG, GB.ERROR);
}
}
});

View File

@ -93,7 +93,7 @@ public class QHybridCoordinator extends AbstractDeviceCoordinator {
public boolean supportsAlarmConfiguration() {
GBDevice connectedDevice = GBApplication.app().getDeviceManager().getSelectedDevice();
if(connectedDevice == null || connectedDevice.getType() != DeviceType.FOSSILQHYBRID){
if(connectedDevice == null || connectedDevice.getType() != DeviceType.FOSSILQHYBRID || connectedDevice.getState() != GBDevice.State.INITIALIZED){
return false;
}
return true;

View File

@ -51,6 +51,7 @@ import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder;
import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceStateAction;
import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.adapter.WatchAdapter;
import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.adapter.WatchAdapterFactory;
import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.adapter.fossil.FossilWatchAdapter;
import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.misfit.DownloadFileRequest;
import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.misfit.PlayNotificationRequest;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
@ -178,7 +179,8 @@ public class QHybridSupport extends QHybridBaseSupport {
break;
}
case QHYBRID_COMMAND_OVERWRITE_BUTTONS: {
watchAdapter.overwriteButtons();
String buttonConfig = intent.getStringExtra(FossilWatchAdapter.ITEM_BUTTONS);
watchAdapter.overwriteButtons(buttonConfig);
break;
}
case QHYBRID_COMMAND_NOTIFICATION_CONFIG_CHANGED: {

View File

@ -31,7 +31,7 @@ public abstract class WatchAdapter {
public abstract void playPairingAnimation();
public abstract void playNotification(NotificationConfiguration config);
public abstract void setTime();
public abstract void overwriteButtons();
public abstract void overwriteButtons(String buttonConfigJson);
public abstract void setActivityHand(double progress);
public abstract void setHands(short hour, short minute);
public abstract void vibrate(PlayNotificationRequest.VibrationType vibration);

View File

@ -3,6 +3,7 @@ package nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.adapter.fos
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.util.Log;
import android.widget.Toast;
@ -16,6 +17,7 @@ import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.GBException;
import nodomain.freeyourgadget.gadgetbridge.devices.qhybrid.NotificationConfiguration;
import nodomain.freeyourgadget.gadgetbridge.devices.qhybrid.PackageConfigHelper;
@ -31,10 +33,7 @@ import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.Req
import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil.FossilRequest;
import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil.RequestMtuRequest;
import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil.SetDeviceStateRequest;
import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil.alarm.AlarmsGetRequest;
import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil.alarm.AlarmsSetRequest;
import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil.button.ButtonConfigurationGetRequest;
import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil.configuration.ConfigurationGetRequest;
import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil.configuration.ConfigurationPutRequest;
import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil.file.FilePutRequest;
import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil.notification.NotificationFilterPutRequest;
@ -45,6 +44,9 @@ import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.mis
import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.misfit.RequestHandControlRequest;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
import static nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.QHybridSupport.ITEM_STEP_GOAL;
import static nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.QHybridSupport.ITEM_TIMEZONE_OFFSET;
import static nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.QHybridSupport.ITEM_VIBRATION_STRENGTH;
import static nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.QHybridSupport.QHYBRID_EVENT_BUTTON_PRESS;
import static nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.QHybridSupport.QHYBRID_EVENT_MULTI_BUTTON_PRESS;
@ -55,9 +57,14 @@ public class FossilWatchAdapter extends WatchAdapter {
private int MTU = 23;
private String ITEM_MTU = "MTU";
private final String ITEM_MTU = "MTU";
static public final String ITEM_BUTTONS = "BUTTONS";
private final String CONFIG_ITEM_STEP_GOAL = "step_goal";
private final String CONFIG_ITEM_VIBRATION_STRENGTH = "vibration_strength";
private final String CONFIG_ITEM_TIMEZONE_OFFSET = "timezone_offset";
public final String CONFIG_ITEM_BUTTONS = "buttons";
private int lastButtonIndex = -1;
public FossilWatchAdapter(QHybridSupport deviceSupport) {
@ -71,11 +78,16 @@ public class FossilWatchAdapter extends WatchAdapter {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
queueWrite(new RequestMtuRequest(512), false);
}
queueWrite(new ConfigurationGetRequest(this), false);
// queueWrite(new FileCloseRequest((short) 0xFFFF));
// queueWrite(new ConfigurationGetRequest(this), false);
syncConfiguration();
syncNotificationSettings();
queueWrite(new ButtonConfigurationGetRequest(this) {
syncButtonSettings();
/* queueWrite(new ButtonConfigurationGetRequest(this) {
@Override
public void onConfigurationsGet(ConfigPayload[] configs) {
super.onConfigurationsGet(configs);
@ -85,11 +97,43 @@ public class FossilWatchAdapter extends WatchAdapter {
String json = buttons.toString();
getDeviceSupport().getDevice().addDeviceInfo(new GenericItem(ITEM_BUTTONS, json));
}
});
}); */
queueWrite(new SetDeviceStateRequest(GBDevice.State.INITIALIZED), false);
}
private void syncButtonSettings(){
String buttonConfig = getDeviceSpecificPreferences().getString(CONFIG_ITEM_BUTTONS, null);
getDeviceSupport().getDevice().addDeviceInfo(new GenericItem(ITEM_BUTTONS, buttonConfig));
overwriteButtons(buttonConfig);
}
private SharedPreferences getDeviceSpecificPreferences(){
return GBApplication.getDeviceSpecificSharedPrefs(
getDeviceSupport().getDevice().getAddress()
);
}
private void syncConfiguration(){
SharedPreferences preferences = getDeviceSpecificPreferences();
int stepGoal = preferences.getInt(CONFIG_ITEM_STEP_GOAL, 1000000);
byte vibrationStrength = (byte) preferences.getInt(CONFIG_ITEM_VIBRATION_STRENGTH, 100);
int timezoneOffset = preferences.getInt(CONFIG_ITEM_TIMEZONE_OFFSET, 0);
GBDevice device = getDeviceSupport().getDevice();
device.addDeviceInfo(new GenericItem(ITEM_STEP_GOAL, String.valueOf(stepGoal)));
device.addDeviceInfo(new GenericItem(ITEM_VIBRATION_STRENGTH, String.valueOf(vibrationStrength)));
device.addDeviceInfo(new GenericItem(ITEM_TIMEZONE_OFFSET, String.valueOf(timezoneOffset)));
queueWrite(new ConfigurationPutRequest(new ConfigurationPutRequest.ConfigItem[]{
new ConfigurationPutRequest.DailyStepGoalConfigItem(stepGoal),
new ConfigurationPutRequest.VibrationStrengthConfigItem(vibrationStrength),
new ConfigurationPutRequest.TimezoneOffsetConfigItem((short) timezoneOffset)
}, this));
}
public int getMTU() {
if (this.MTU < 0) throw new RuntimeException("MTU not configured");
@ -127,9 +171,15 @@ public class FossilWatchAdapter extends WatchAdapter {
}
@Override
public void overwriteButtons() {
public void overwriteButtons(String jsonConfigString) {
try {
JSONArray buttonConfigJson = new JSONArray(getDeviceSupport().getDevice().getDeviceInfo(ITEM_BUTTONS).getDetails());
if(jsonConfigString == null) return;
getDeviceSpecificPreferences()
.edit()
.putString(CONFIG_ITEM_BUTTONS, jsonConfigString)
.apply();
JSONArray buttonConfigJson = new JSONArray(jsonConfigString);
// JSONArray buttonConfigJson = new JSONArray(getDeviceSupport().getDevice().getDeviceInfo(ITEM_BUTTONS).getDetails());
ConfigPayload[] payloads = new ConfigPayload[buttonConfigJson.length()];
@ -193,6 +243,11 @@ public class FossilWatchAdapter extends WatchAdapter {
@Override
public void setStepGoal(int stepGoal) {
getDeviceSpecificPreferences()
.edit()
.putInt(CONFIG_ITEM_STEP_GOAL, stepGoal)
.apply();
queueWrite(new ConfigurationPutRequest(new ConfigurationPutRequest.DailyStepGoalConfigItem(stepGoal), this) {
@Override
public void onFilePut(boolean success) {
@ -205,6 +260,11 @@ public class FossilWatchAdapter extends WatchAdapter {
@Override
public void setVibrationStrength(short strength) {
getDeviceSpecificPreferences()
.edit()
.putInt(CONFIG_ITEM_VIBRATION_STRENGTH, (byte) strength)
.apply();
ConfigurationPutRequest.ConfigItem vibrationItem = new ConfigurationPutRequest.VibrationStrengthConfigItem((byte) strength);
@ -263,6 +323,11 @@ public class FossilWatchAdapter extends WatchAdapter {
@Override
public void setTimezoneOffsetMinutes(short offset) {
getDeviceSpecificPreferences()
.edit()
.putInt(CONFIG_ITEM_TIMEZONE_OFFSET, offset)
.apply();
queueWrite(new ConfigurationPutRequest(new ConfigurationPutRequest.TimezoneOffsetConfigItem(offset), this){
@Override
public void onFilePut(boolean success) {

View File

@ -462,7 +462,7 @@ public class MisfitWatchAdapter extends WatchAdapter {
}
@Override
public void overwriteButtons() {
public void overwriteButtons(String jsonConfigString) {
uploadFileRequest = new UploadFileRequest((short) 0x0800, new byte[]{
(byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x03, (byte) 0x10, (byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x0C, (byte) 0x00, (byte) 0x00, (byte) 0x20, (byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x0C, (byte) 0x00, (byte) 0x00,
(byte) 0x30, (byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x0C, (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x01, (byte) 0x00, (byte) 0x01, (byte) 0x01, (byte) 0x0C, (byte) 0x2E, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01,