Zepp OS: Add World Clocks

This commit is contained in:
José Rebelo 2022-10-31 12:09:36 +00:00 committed by Gitea
parent 4c14dd5f72
commit 164c5e52a4
13 changed files with 212 additions and 23 deletions

View File

@ -43,7 +43,7 @@ public class GBDaoGenerator {
public static void main(String[] args) throws Exception {
final Schema schema = new Schema(44, MAIN_PACKAGE + ".entities");
final Schema schema = new Schema(45, MAIN_PACKAGE + ".entities");
Entity userAttributes = addUserAttributes(schema);
Entity user = addUserInfo(schema, userAttributes);
@ -591,6 +591,8 @@ public class GBDaoGenerator {
indexUnique.makeUnique();
worldClock.addIndex(indexUnique);
worldClock.addStringProperty("label").notNull();
worldClock.addBooleanProperty("enabled");
worldClock.addStringProperty("code");
worldClock.addStringProperty("timeZoneId").notNull();
worldClock.addToOne(user, userId);
worldClock.addToOne(device, deviceId);

View File

@ -53,6 +53,7 @@ import nodomain.freeyourgadget.gadgetbridge.entities.User;
import nodomain.freeyourgadget.gadgetbridge.entities.WorldClock;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper;
import nodomain.freeyourgadget.gadgetbridge.util.StringUtils;
public class ConfigureWorldClocks extends AbstractGBActivity {
@ -165,9 +166,11 @@ public class ConfigureWorldClocks extends AbstractGBActivity {
private WorldClock createDefaultWorldClock(@NonNull Device device, @NonNull User user) {
final WorldClock worldClock = new WorldClock();
final String timezone = TimeZone.getDefault().getID();
worldClock.setEnabled(true);
worldClock.setTimeZoneId(timezone);
final String[] timezoneParts = timezone.split("/");
worldClock.setLabel(timezoneParts[timezoneParts.length - 1]);
worldClock.setCode(StringUtils.truncate(timezoneParts[timezoneParts.length - 1], 3).toUpperCase(Locale.getDefault()));
worldClock.setDeviceId(device.getId());
worldClock.setUserId(user.getId());

View File

@ -25,6 +25,7 @@ import android.text.TextWatcher;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
@ -43,6 +44,7 @@ import nodomain.freeyourgadget.gadgetbridge.entities.WorldClock;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
import nodomain.freeyourgadget.gadgetbridge.util.StringUtils;
public class WorldClockDetails extends AbstractGBActivity {
private static final Logger LOG = LoggerFactory.getLogger(WorldClockDetails.class);
@ -54,6 +56,9 @@ public class WorldClockDetails extends AbstractGBActivity {
TextView worldClockTimezone;
EditText worldClockLabel;
EditText worldClockCode;
View worldClockEnabledCard;
CheckBox worldClockEnabled;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -68,8 +73,11 @@ public class WorldClockDetails extends AbstractGBActivity {
return;
}
worldClockEnabledCard = findViewById(R.id.card_enabled);
worldClockEnabled = findViewById(R.id.world_clock_enabled);
worldClockTimezone = findViewById(R.id.world_clock_timezone);
worldClockLabel = findViewById(R.id.world_clock_label);
worldClockCode = findViewById(R.id.world_clock_code);
device = getIntent().getParcelableExtra(GBDevice.EXTRA_DEVICE);
final DeviceCoordinator coordinator = DeviceHelper.getInstance().getCoordinator(device);
@ -91,6 +99,14 @@ public class WorldClockDetails extends AbstractGBActivity {
}
});
if (coordinator.supportsDisabledWorldClocks()) {
worldClockEnabled.setOnCheckedChangeListener((buttonView, isChecked) -> {
worldClock.setEnabled(isChecked);
});
} else {
worldClockEnabledCard.setVisibility(View.GONE);
}
worldClockLabel.setFilters(new InputFilter[]{new InputFilter.LengthFilter(coordinator.getWorldClocksLabelLength())});
worldClockLabel.addTextChangedListener(new TextWatcher() {
@Override
@ -107,6 +123,22 @@ public class WorldClockDetails extends AbstractGBActivity {
}
});
worldClockCode.setFilters(new InputFilter[]{new InputFilter.LengthFilter(3)});
worldClockCode.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(final CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(final CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(final Editable s) {
worldClock.setCode(s.toString());
}
});
final FloatingActionButton fab = findViewById(R.id.fab_save);
fab.setOnClickListener(new View.OnClickListener() {
@Override
@ -150,25 +182,37 @@ public class WorldClockDetails extends AbstractGBActivity {
}
public void updateUiFromWorldClock() {
final DeviceCoordinator coordinator = DeviceHelper.getInstance().getCoordinator(device);
final int maxLabelLength = coordinator.getWorldClocksLabelLength();
worldClockEnabled.setChecked(worldClock.getEnabled() == null || worldClock.getEnabled());
final String oldTimezone = worldClockTimezone.getText().toString();
worldClockTimezone.setText(worldClock.getTimeZoneId());
// Check if the label was still the default (the timezone city name)
// If so, and if the user changed the timezone, update the label to match the new city name
if (!oldTimezone.equals(worldClock.getTimeZoneId())) {
if (!StringUtils.isNullOrEmpty(oldTimezone) && !oldTimezone.equals(worldClock.getTimeZoneId())) {
final String[] oldTimezoneParts = oldTimezone.split("/");
final String[] newTimezoneParts = worldClock.getTimeZoneId().split("/");
final String newLabel = newTimezoneParts[newTimezoneParts.length - 1];
final String oldLabel = oldTimezoneParts[oldTimezoneParts.length - 1];
final String newLabel = StringUtils.truncate(newTimezoneParts[newTimezoneParts.length - 1], maxLabelLength);
final String oldLabel = StringUtils.truncate(oldTimezoneParts[oldTimezoneParts.length - 1], maxLabelLength);
final String userLabel = worldClockLabel.getText().toString();
if (userLabel.equals(oldLabel)) {
if (StringUtils.isNullOrEmpty(userLabel) || userLabel.equals(oldLabel)) {
// The label was still the original, so let's override it with the new city
worldClock.setLabel(newLabel);
}
final String newCode = StringUtils.truncate(newLabel, 3).toUpperCase();
final String oldCode = StringUtils.truncate(oldLabel, 3).toUpperCase();
final String userCode = worldClockCode.getText().toString();
if (StringUtils.isNullOrEmpty(userCode) || userCode.equals(oldCode)) {
// The code was still the original, so let's override it with the new one
worldClock.setCode(newCode);
}
}
worldClockLabel.setText(worldClock.getLabel());
worldClockCode.setText(worldClock.getCode());
}
}

View File

@ -0,0 +1,44 @@
/* Copyright (C) 2022 José Rebelo
This file is part of Gadgetbridge.
Gadgetbridge is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Gadgetbridge is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.database.schema;
import android.database.sqlite.SQLiteDatabase;
import nodomain.freeyourgadget.gadgetbridge.database.DBHelper;
import nodomain.freeyourgadget.gadgetbridge.database.DBUpdateScript;
import nodomain.freeyourgadget.gadgetbridge.entities.WorldClockDao;
public class GadgetbridgeUpdate_45 implements DBUpdateScript {
@Override
public void upgradeSchema(final SQLiteDatabase db) {
if (!DBHelper.existsColumn(WorldClockDao.TABLENAME, WorldClockDao.Properties.Code.columnName, db)) {
final String statement = "ALTER TABLE " + WorldClockDao.TABLENAME + " ADD COLUMN "
+ WorldClockDao.Properties.Code.columnName + " TEXT";
db.execSQL(statement);
}
if (!DBHelper.existsColumn(WorldClockDao.TABLENAME, WorldClockDao.Properties.Enabled.columnName, db)) {
final String statement = "ALTER TABLE " + WorldClockDao.TABLENAME + " ADD COLUMN "
+ WorldClockDao.Properties.Enabled.columnName + " BOOLEAN DEFAULT TRUE";
db.execSQL(statement);
}
}
@Override
public void downgradeSchema(final SQLiteDatabase db) {
}
}

View File

@ -264,6 +264,11 @@ public abstract class AbstractDeviceCoordinator implements DeviceCoordinator {
return 10;
}
@Override
public boolean supportsDisabledWorldClocks() {
return false;
}
@Override
public boolean supportsRgbLedColor() {
return false;

View File

@ -397,6 +397,12 @@ public interface DeviceCoordinator {
*/
int getWorldClocksLabelLength();
/**
* Indicates whether the device supports disabled world clocks that can be enabled through
* a menu on the device.
*/
boolean supportsDisabledWorldClocks();
/**
* Indicates whether the device has an led which supports custom colors
*/

View File

@ -85,8 +85,17 @@ public abstract class Huami2021Coordinator extends HuamiCoordinator {
@Override
public int getWorldClocksSlotCount() {
// TODO: It's supported, but not implemented - even in the official app
return 0;
return 20; // as enforced by Zepp
}
@Override
public int getWorldClocksLabelLength() {
return 30; // at least
}
@Override
public boolean supportsDisabledWorldClocks() {
return true;
}
@Override
@ -162,7 +171,9 @@ public abstract class Huami2021Coordinator extends HuamiCoordinator {
settings.add(R.xml.devicesettings_header_time);
//settings.add(R.xml.devicesettings_timeformat);
settings.add(R.xml.devicesettings_dateformat_2);
// TODO settings.add(R.xml.devicesettings_world_clocks);
if (getWorldClocksSlotCount() > 0) {
settings.add(R.xml.devicesettings_world_clocks);
}
//
// Display

View File

@ -87,11 +87,6 @@ public class AmazfitNeoCoordinator extends HuamiCoordinator {
return 20; // max in Zepp app
}
@Override
public int getWorldClocksLabelLength() {
return 3; // neo has 3 letter city codes
}
@Override
public int getReminderSlotCount(final GBDevice device) {
return 0; // Neo does not support reminders

View File

@ -24,7 +24,9 @@ public interface WorldClock extends Serializable {
*/
String EXTRA_WORLD_CLOCK = "world_clock";
Boolean getEnabled();
String getWorldClockId();
String getLabel();
String getCode();
String getTimeZoneId();
}

View File

@ -785,9 +785,8 @@ public abstract class Huami2021Support extends HuamiSupport {
}
@Override
protected void sendWorldClocks(final TransactionBuilder builder,
final List<? extends WorldClock> clocks) {
// TODO not yet implemented
protected boolean isWorldClocksEncrypted() {
return true;
}
@Override

View File

@ -1101,7 +1101,11 @@ public abstract class HuamiSupport extends AbstractBTLEDeviceSupport implements
return;
}
writeToChunked2021(builder, (short) 0x0008, baos.toByteArray(), false);
writeToChunked2021(builder, (short) 0x0008, baos.toByteArray(), isWorldClocksEncrypted());
}
protected boolean isWorldClocksEncrypted() {
return false;
}
private byte[] encodeWorldClock(final WorldClock clock) {
@ -1113,8 +1117,12 @@ public abstract class HuamiSupport extends AbstractBTLEDeviceSupport implements
final TimeZone timezone = TimeZone.getTimeZone(clock.getTimeZoneId());
final ZoneId zoneId = ZoneId.of(clock.getTimeZoneId());
// Usually the 3-letter city code (eg. LIS for Lisbon), but doesn't seem to be used in the UI (used in Amazfit Neo)
baos.write(StringUtils.truncate(clock.getLabel(), 3).toUpperCase().getBytes(StandardCharsets.UTF_8));
// Usually the 3-letter city code (eg. LIS for Lisbon)
if (clock.getCode() != null) {
baos.write(StringUtils.truncate(clock.getCode(), 3).toUpperCase().getBytes(StandardCharsets.UTF_8));
} else {
baos.write(StringUtils.truncate(clock.getLabel(), 3).toUpperCase().getBytes(StandardCharsets.UTF_8));
}
baos.write(0x00);
// Some other string? Seems to be empty
@ -1164,6 +1172,10 @@ public abstract class HuamiSupport extends AbstractBTLEDeviceSupport implements
baos.write((byte) ((nextTransitionTs >> (i * 8)) & 0xff));
}
if (coordinator.supportsDisabledWorldClocks()) {
baos.write((byte) (clock.getEnabled() ? 0x01 : 0x00));
}
return baos.toByteArray();
} catch (final IOException e) {
throw new RuntimeException("This should never happen", e);

View File

@ -6,7 +6,33 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="nodomain.freeyourgadget.gadgetbridge.activities.ReminderDetails">
tools:context="nodomain.freeyourgadget.gadgetbridge.activities.WorldClockDetails">
<androidx.cardview.widget.CardView
android:id="@+id/card_enabled"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:foreground="?android:attr/selectableItemBackground"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="4dp"
card_view:contentPadding="4dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp">
<CheckBox
android:id="@+id/world_clock_enabled"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/function_enabled" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="@+id/card_timezone"
@ -16,7 +42,7 @@
android:foreground="?android:attr/selectableItemBackground"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintTop_toBottomOf="@id/card_enabled"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="4dp"
card_view:contentPadding="4dp">
@ -40,7 +66,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:text="?"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/label_timezone" />
@ -85,6 +111,44 @@
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="@+id/card_code"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:foreground="?android:attr/selectableItemBackground"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/card_label"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="4dp"
card_view:contentPadding="4dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp">
<TextView
android:id="@+id/label_code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/world_clock_code"
android:textAppearance="?android:attr/textAppearance"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/world_clock_code"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/label_code" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab_save"
android:layout_width="wrap_content"

View File

@ -651,8 +651,10 @@
<string name="world_clock_delete_confirm_description">Are you sure you want to delete the world clock?</string>
<string name="world_clock_no_free_slots_title">No free slots</string>
<string name="world_clock_no_free_slots_description">The device has no free slots for world clocks (total slots: %1$s)</string>
<string name="function_enabled">Enabled</string>
<string name="world_clock_timezone">Time Zone</string>
<string name="world_clock_label">Label</string>
<string name="world_clock_code">Code</string>
<string name="title_activity_alarm_details">Alarm details</string>
<string name="title_activity_reminder_details">Reminder details</string>
<string name="title_activity_world_clock_details">World Clock details</string>