1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-29 05:16:51 +01:00

Xiaomi: Get canned messages from watch

This commit is contained in:
José Rebelo 2023-12-06 11:17:36 +00:00 committed by José Rebelo
parent b902ee96c3
commit 03dbf7533f
11 changed files with 119 additions and 12 deletions

View File

@ -772,12 +772,14 @@ public class DeviceSpecificSettingsFragment extends AbstractPreferenceFragment i
}); });
} }
final int cannedRepliesSlotCount = coordinator.getCannedRepliesSlotCount(device);
final Preference cannedMessagesDismissCall = findPreference("canned_messages_dismisscall_send"); final Preference cannedMessagesDismissCall = findPreference("canned_messages_dismisscall_send");
if (cannedMessagesDismissCall != null) { if (cannedMessagesDismissCall != null) {
cannedMessagesDismissCall.setOnPreferenceClickListener(new androidx.preference.Preference.OnPreferenceClickListener() { cannedMessagesDismissCall.setOnPreferenceClickListener(new androidx.preference.Preference.OnPreferenceClickListener() {
public boolean onPreferenceClick(androidx.preference.Preference preference) { public boolean onPreferenceClick(androidx.preference.Preference preference) {
ArrayList<String> messages = new ArrayList<>(); ArrayList<String> messages = new ArrayList<>();
for (int i = 1; i <= 16; i++) { for (int i = 1; i <= cannedRepliesSlotCount; i++) {
String message = prefs.getString("canned_message_dismisscall_" + i, null); String message = prefs.getString("canned_message_dismisscall_" + i, null);
if (message != null && !message.equals("")) { if (message != null && !message.equals("")) {
messages.add(message); messages.add(message);
@ -790,14 +792,23 @@ public class DeviceSpecificSettingsFragment extends AbstractPreferenceFragment i
return true; return true;
} }
}); });
// TODO we could use this to auto-create preferences for watches with > 16 slots
for (int i = cannedRepliesSlotCount + 1; i <= 16; i++) {
final Preference cannedReplyPref = findPreference("canned_message_dismisscall_" + i);
if (cannedReplyPref != null) {
cannedReplyPref.setVisible(false);
}
}
} }
final Preference cannedMessagesGeneric = findPreference("canned_messages_generic_send"); final Preference cannedMessagesGeneric = findPreference("canned_messages_generic_send");
if (cannedMessagesGeneric != null) { if (cannedMessagesGeneric != null) {
cannedMessagesGeneric.setOnPreferenceClickListener(new androidx.preference.Preference.OnPreferenceClickListener() { cannedMessagesGeneric.setOnPreferenceClickListener(new androidx.preference.Preference.OnPreferenceClickListener() {
public boolean onPreferenceClick(androidx.preference.Preference preference) { public boolean onPreferenceClick(androidx.preference.Preference preference) {
final ArrayList<String> messages = new ArrayList<>(); final ArrayList<String> messages = new ArrayList<>();
for (int i = 1; i <= 16; i++) { for (int i = 1; i <= cannedRepliesSlotCount; i++) {
String message = prefs.getString("canned_reply_" + i, null); String message = prefs.getString("canned_reply_" + i, null);
if (message != null && !message.equals("")) { if (message != null && !message.equals("")) {
messages.add(message); messages.add(message);
@ -810,6 +821,14 @@ public class DeviceSpecificSettingsFragment extends AbstractPreferenceFragment i
return true; return true;
} }
}); });
// TODO we could use this to auto-create preferences for watches with > 16 slots
for (int i = cannedRepliesSlotCount + 1; i <= 16; i++) {
final Preference cannedReplyPref = findPreference("canned_reply_" + i);
if (cannedReplyPref != null) {
cannedReplyPref.setVisible(false);
}
}
} }
setInputTypeFor(HuamiConst.PREF_BUTTON_ACTION_BROADCAST_DELAY, InputType.TYPE_CLASS_NUMBER); setInputTypeFor(HuamiConst.PREF_BUTTON_ACTION_BROADCAST_DELAY, InputType.TYPE_CLASS_NUMBER);

View File

@ -432,6 +432,11 @@ public abstract class AbstractDeviceCoordinator implements DeviceCoordinator {
return 0; return 0;
} }
@Override
public int getCannedRepliesSlotCount(final GBDevice device) {
return 0;
}
@Override @Override
public int getWorldClocksSlotCount() { public int getWorldClocksSlotCount() {
return 0; return 0;

View File

@ -451,6 +451,11 @@ public interface DeviceCoordinator {
*/ */
int getReminderSlotCount(GBDevice device); int getReminderSlotCount(GBDevice device);
/**
* Indicates the maximum number of canned replies available in the device.
*/
int getCannedRepliesSlotCount(GBDevice device);
/** /**
* Indicates the maximum number of slots available for world clocks in the device. * Indicates the maximum number of slots available for world clocks in the device.
*/ */

View File

@ -283,6 +283,11 @@ public abstract class Huami2021Coordinator extends HuamiCoordinator {
return ZeppOsRemindersService.getSlotCount(getPrefs(device)); return ZeppOsRemindersService.getSlotCount(getPrefs(device));
} }
@Override
public int getCannedRepliesSlotCount(final GBDevice device) {
return 16;
}
@Override @Override
public int getContactsSlotCount(final GBDevice device) { public int getContactsSlotCount(final GBDevice device) {
return getPrefs(device).getInt(ZeppOsContactsService.PREF_CONTACTS_SLOT_COUNT, 0); return getPrefs(device).getInt(ZeppOsContactsService.PREF_CONTACTS_SLOT_COUNT, 0);

View File

@ -125,6 +125,11 @@ public class PebbleCoordinator extends AbstractBLClassicDeviceCoordinator {
return 0; return 0;
} }
@Override
public int getCannedRepliesSlotCount(final GBDevice device) {
return 16;
}
@Override @Override
public boolean supportsSmartWakeup(GBDevice device) { public boolean supportsSmartWakeup(GBDevice device) {
return false; return false;

View File

@ -146,6 +146,15 @@ public class QHybridCoordinator extends AbstractBLEDeviceCoordinator {
return this.supportsAlarmConfiguration() ? 5 : 0; return this.supportsAlarmConfiguration() ? 5 : 0;
} }
@Override
public int getCannedRepliesSlotCount(final GBDevice device) {
if (isHybridHR()) {
return 16;
}
return 0;
}
@Override @Override
public boolean supportsAlarmDescription(GBDevice device) { public boolean supportsAlarmDescription(GBDevice device) {
return isHybridHR(); return isHybridHR();

View File

@ -279,6 +279,11 @@ public abstract class XiaomiCoordinator extends AbstractBLEDeviceCoordinator {
return getPrefs(device).getInt(XiaomiPreferences.PREF_REMINDER_SLOTS, 0); return getPrefs(device).getInt(XiaomiPreferences.PREF_REMINDER_SLOTS, 0);
} }
@Override
public int getCannedRepliesSlotCount(final GBDevice device) {
return getPrefs(device).getInt(XiaomiPreferences.PREF_CANNED_MESSAGES_MAX, 0);
}
@Override @Override
public int getWorldClocksSlotCount() { public int getWorldClocksSlotCount() {
// TODO how many? also, map world clocks // TODO how many? also, map world clocks
@ -387,7 +392,9 @@ public abstract class XiaomiCoordinator extends AbstractBLEDeviceCoordinator {
settings.add(R.xml.devicesettings_donotdisturb_withauto_and_always); settings.add(R.xml.devicesettings_donotdisturb_withauto_and_always);
settings.add(R.xml.devicesettings_screen_on_on_notifications); settings.add(R.xml.devicesettings_screen_on_on_notifications);
settings.add(R.xml.devicesettings_autoremove_notifications); settings.add(R.xml.devicesettings_autoremove_notifications);
settings.add(R.xml.devicesettings_canned_reply_16); if (getCannedRepliesSlotCount(device) > 0) {
settings.add(R.xml.devicesettings_canned_dismisscall_16);
}
// //
// Calendar // Calendar

View File

@ -30,6 +30,8 @@ import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
public final class XiaomiPreferences { public final class XiaomiPreferences {
public static final String PREF_ALARM_SLOTS = "alarm_slots"; public static final String PREF_ALARM_SLOTS = "alarm_slots";
public static final String PREF_REMINDER_SLOTS = "reminder_slots"; public static final String PREF_REMINDER_SLOTS = "reminder_slots";
public static final String PREF_CANNED_MESSAGES_MIN = "canned_messages_min";
public static final String PREF_CANNED_MESSAGES_MAX = "canned_messages_max";
private XiaomiPreferences() { private XiaomiPreferences() {
// util class // util class

View File

@ -37,10 +37,12 @@ import java.util.Queue;
import nodomain.freeyourgadget.gadgetbridge.BuildConfig; import nodomain.freeyourgadget.gadgetbridge.BuildConfig;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventCallControl; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventCallControl;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventNotificationControl; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventNotificationControl;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventUpdatePreferences;
import nodomain.freeyourgadget.gadgetbridge.model.CallSpec; import nodomain.freeyourgadget.gadgetbridge.model.CallSpec;
import nodomain.freeyourgadget.gadgetbridge.model.CannedMessagesSpec; import nodomain.freeyourgadget.gadgetbridge.model.CannedMessagesSpec;
import nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec; import nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec;
import nodomain.freeyourgadget.gadgetbridge.proto.xiaomi.XiaomiProto; import nodomain.freeyourgadget.gadgetbridge.proto.xiaomi.XiaomiProto;
import nodomain.freeyourgadget.gadgetbridge.service.devices.xiaomi.XiaomiPreferences;
import nodomain.freeyourgadget.gadgetbridge.service.devices.xiaomi.XiaomiSupport; import nodomain.freeyourgadget.gadgetbridge.service.devices.xiaomi.XiaomiSupport;
import nodomain.freeyourgadget.gadgetbridge.util.BitmapUtil; import nodomain.freeyourgadget.gadgetbridge.util.BitmapUtil;
import nodomain.freeyourgadget.gadgetbridge.util.NotificationUtils; import nodomain.freeyourgadget.gadgetbridge.util.NotificationUtils;
@ -242,18 +244,36 @@ public class XiaomiNotificationService extends AbstractXiaomiService implements
} }
public void onSetCannedMessages(final CannedMessagesSpec cannedMessagesSpec) { public void onSetCannedMessages(final CannedMessagesSpec cannedMessagesSpec) {
if (cannedMessagesSpec.type != CannedMessagesSpec.TYPE_GENERIC) { if (cannedMessagesSpec.type != CannedMessagesSpec.TYPE_REJECTEDCALLS) {
LOG.warn("Got unsupported canned messages type: {}", cannedMessagesSpec.type); LOG.warn("Got unsupported canned messages type: {}", cannedMessagesSpec.type);
return; return;
} }
final int minReplies = getDevicePrefs().getInt(XiaomiPreferences.PREF_CANNED_MESSAGES_MIN, 0);
final int maxReplies = getDevicePrefs().getInt(XiaomiPreferences.PREF_CANNED_MESSAGES_MAX, 0);
if (maxReplies == 0) {
LOG.warn("Attempting to set canned messages, but max replies is 0");
return;
}
final XiaomiProto.CannedMessages.Builder cannedMessagesBuilder = XiaomiProto.CannedMessages.newBuilder() final XiaomiProto.CannedMessages.Builder cannedMessagesBuilder = XiaomiProto.CannedMessages.newBuilder()
// TODO get those from wathc .setMinReplies(minReplies)
// TODO enforce these .setMaxReplies(maxReplies);
.setMinReplies(1) int i = 0;
.setMaxReplies(10);
for (final String cannedMessage : cannedMessagesSpec.cannedMessages) { for (final String cannedMessage : cannedMessagesSpec.cannedMessages) {
if (i >= maxReplies) {
LOG.warn("Got too many canned messages ({}), limit is {}", cannedMessagesSpec.cannedMessages.length, maxReplies);
break;
}
cannedMessagesBuilder.addReply(cannedMessage); cannedMessagesBuilder.addReply(cannedMessage);
i++;
}
for (; i < minReplies; i++) {
cannedMessagesBuilder.addReply("-");
} }
final XiaomiProto.Notification.Builder notificationBuilder = XiaomiProto.Notification.newBuilder() final XiaomiProto.Notification.Builder notificationBuilder = XiaomiProto.Notification.newBuilder()
@ -281,10 +301,30 @@ public class XiaomiNotificationService extends AbstractXiaomiService implements
} }
public void handleCannedMessages(final XiaomiProto.CannedMessages cannedMessages) { public void handleCannedMessages(final XiaomiProto.CannedMessages cannedMessages) {
// TODO save them LOG.info("Got {} canned messages", cannedMessages.getReplyCount());
//final GBDeviceEventUpdatePreferences gbDeviceEventUpdatePreferences = new GBDeviceEventUpdatePreferences();
//gbDeviceEventUpdatePreferences.withPreference("canned_reply_" + i, message); final int minReplies = cannedMessages.getMinReplies();
//getSupport().evaluateGBDeviceEvent(gbDeviceEventUpdatePreferences); final int maxReplies = cannedMessages.getMaxReplies();
final GBDeviceEventUpdatePreferences gbDeviceEventUpdatePreferences = new GBDeviceEventUpdatePreferences()
.withPreference(XiaomiPreferences.PREF_CANNED_MESSAGES_MIN, minReplies)
.withPreference(XiaomiPreferences.PREF_CANNED_MESSAGES_MAX, maxReplies);
int i = 1;
for (final String reply : cannedMessages.getReplyList()) {
gbDeviceEventUpdatePreferences.withPreference("canned_message_dismisscall_" + i, reply);
i++;
if (i > maxReplies || i > 16) {
LOG.warn("Got too many canned messages ({})", i);
break;
}
}
for (int j = i; j <= maxReplies; j++) {
gbDeviceEventUpdatePreferences.withPreference("canned_message_dismisscall_" + j, null);
}
getSupport().evaluateGBDeviceEvent(gbDeviceEventUpdatePreferences);
} }
public boolean canSendSms() { public boolean canSendSms() {

View File

@ -13,6 +13,11 @@
android:summary="@string/pref_summary_canned_messages_set" android:summary="@string/pref_summary_canned_messages_set"
android:title="@string/pref_title_canned_messages_set" /> android:title="@string/pref_title_canned_messages_set" />
<!--
WARNING: Make sure to override getCannedRepliesSlotCount in the device coordinator
in order for the options below to show up.
-->
<PreferenceCategory <PreferenceCategory
android:key="pref_key_canned_reply_messages" android:key="pref_key_canned_reply_messages"
android:title="@string/pref_header_cannned_messages"> android:title="@string/pref_header_cannned_messages">

View File

@ -20,6 +20,11 @@
android:title="@string/pref_title_canned_reply_suffix" android:title="@string/pref_title_canned_reply_suffix"
app:useSimpleSummaryProvider="true" /> app:useSimpleSummaryProvider="true" />
<!--
WARNING: Make sure to override getCannedRepliesSlotCount in the device coordinator
in order for the options below to show up.
-->
<PreferenceCategory <PreferenceCategory
android:key="pref_key_canned_reply_messages" android:key="pref_key_canned_reply_messages"
android:title="@string/pref_header_cannned_messages"> android:title="@string/pref_header_cannned_messages">