From f073b360b676710420248b78421cbd4eb73dbf38 Mon Sep 17 00:00:00 2001 From: Thomas Kuehne Date: Sat, 30 May 2026 06:19:29 +0000 Subject: [PATCH] GBDeviceService: code review --- .../gadgetbridge/devices/EventHandler.java | 16 +++--- .../gadgetbridge/impl/GBDeviceService.java | 51 ++++++++++--------- .../gadgetbridge/model/DeviceService.java | 8 ++- .../service/DeviceCommunicationService.java | 8 +-- 4 files changed, 45 insertions(+), 38 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/EventHandler.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/EventHandler.java index be881923c8..2da5ae1a3a 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/EventHandler.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/EventHandler.java @@ -1,6 +1,6 @@ -/* Copyright (C) 2015-2024 Andreas Shimokawa, Arjan Schrijver, Carsten +/* Copyright (C) 2015-2026 Andreas Shimokawa, Arjan Schrijver, Carsten Pfeiffer, José Rebelo, Julien Pivotto, Kasha, Sebastian Kranz, Steffen - Liebergeld, Uwe Hermann + Liebergeld, Uwe Hermann, Thomas Kuehne This file is part of Gadgetbridge. @@ -48,7 +48,7 @@ import nodomain.freeyourgadget.gadgetbridge.model.WorldClock; * Implementations need to send/encode event to the connected device. */ public interface EventHandler { - void onNotification(NotificationSpec notificationSpec); + void onNotification(@NonNull NotificationSpec notificationSpec); void onDeleteNotification(int id); @@ -66,11 +66,11 @@ public interface EventHandler { void onSetCallState(CallSpec callSpec); - void onSetCannedMessages(CannedMessagesSpec cannedMessagesSpec); + void onSetCannedMessages(@NonNull CannedMessagesSpec cannedMessagesSpec); - void onSetMusicState(MusicStateSpec stateSpec); + void onSetMusicState(@NonNull MusicStateSpec stateSpec); - void onSetMusicInfo(MusicSpec musicSpec); + void onSetMusicInfo(@NonNull MusicSpec musicSpec); /** * Sets the current phone media volume. @@ -81,7 +81,7 @@ public interface EventHandler { void onChangePhoneSilentMode(int ringerMode); - void onSetNavigationInfo(NavigationInfoSpec navigationInfoSpec); + void onSetNavigationInfo(@NonNull NavigationInfoSpec navigationInfoSpec); void onEnableRealtimeSteps(boolean enable); @@ -123,7 +123,7 @@ public interface EventHandler { void onSetHeartRateMeasurementInterval(int seconds); - void onAddCalendarEvent(CalendarEventSpec calendarEventSpec); + void onAddCalendarEvent(@NonNull CalendarEventSpec calendarEventSpec); void onDeleteCalendarEvent(byte type, long id); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/impl/GBDeviceService.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/impl/GBDeviceService.java index 5af6794371..679776cfa9 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/impl/GBDeviceService.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/impl/GBDeviceService.java @@ -1,8 +1,8 @@ -/* Copyright (C) 2015-2024 Alberto, Andreas Böhler, Andreas Shimokawa, +/* Copyright (C) 2015-2026 Alberto, Andreas Böhler, Andreas Shimokawa, Arjan Schrijver, Carsten Pfeiffer, criogenic, Daniel Dakhno, Daniele Gobbetti, Davis Mosenkovs, Frank Slezak, Gabriele Monaco, Gordon Williams, ivanovlev, José Rebelo, Julien Pivotto, Kasha, mvn23, Petr Vaněk, Roi Greenberg, - Sebastian Kranz, Steffen Liebergeld + Sebastian Kranz, Steffen Liebergeld, Thomas Kuehne This file is part of Gadgetbridge. @@ -82,11 +82,11 @@ public class GBDeviceService implements DeviceService { }; private static final Logger LOG = LoggerFactory.getLogger(GBDeviceService.class); - public GBDeviceService(Context context) { + public GBDeviceService(@NonNull Context context) { this(context, null); } - public GBDeviceService(Context context, GBDevice device) { + public GBDeviceService(@NonNull Context context, GBDevice device) { mContext = context; mDevice = device; mServiceClass = DeviceCommunicationService.class; @@ -105,7 +105,7 @@ public class GBDeviceService implements DeviceService { return new Intent(mContext, mServiceClass); } - protected void invokeService(Intent intent) { + protected void invokeService(@NonNull Intent intent) { if (RtlUtils.rtlSupport()) { for (String extra : transliterationExtras) { @@ -121,7 +121,7 @@ public class GBDeviceService implements DeviceService { try { mContext.startService(intent); } catch (IllegalStateException e) { - LOG.error("IllegalStateException during startService ("+intent.getAction()+")"); + LOG.error("IllegalStateException during startService ({})", intent.getAction(), e); } } @@ -160,7 +160,7 @@ public class GBDeviceService implements DeviceService { } @Override - public void onNotification(NotificationSpec notificationSpec) { + public void onNotification(@NonNull NotificationSpec notificationSpec) { String messagePrivacyMode = GBApplication.getPrefs().getString("pref_message_privacy_mode", GBApplication.getContext().getString(R.string.p_message_privacy_mode_off)); boolean hideMessageDetails = messagePrivacyMode.equals(GBApplication.getContext().getString(R.string.p_message_privacy_mode_complete)); @@ -241,7 +241,7 @@ public class GBDeviceService implements DeviceService { } @Override - public void onSetCannedMessages(CannedMessagesSpec cannedMessagesSpec) { + public void onSetCannedMessages(@NonNull CannedMessagesSpec cannedMessagesSpec) { Intent intent = createIntent().setAction(ACTION_SETCANNEDMESSAGES) .putExtra(EXTRA_CANNEDMESSAGES_TYPE, cannedMessagesSpec.type) .putExtra(EXTRA_CANNEDMESSAGES, cannedMessagesSpec.cannedMessages); @@ -249,7 +249,7 @@ public class GBDeviceService implements DeviceService { } @Override - public void onSetMusicState(MusicStateSpec stateSpec) { + public void onSetMusicState(@NonNull MusicStateSpec stateSpec) { Intent intent = createIntent().setAction(ACTION_SETMUSICSTATE) .putExtra(EXTRA_MUSIC_REPEAT, stateSpec.repeat) .putExtra(EXTRA_MUSIC_RATE, stateSpec.playRate) @@ -302,7 +302,7 @@ public class GBDeviceService implements DeviceService { } @Override - public void onSetMusicInfo(MusicSpec musicSpec) { + public void onSetMusicInfo(@NonNull MusicSpec musicSpec) { Intent intent = createIntent().setAction(ACTION_SETMUSICINFO) .putExtra(EXTRA_MUSIC_ARTIST, musicSpec.artist) .putExtra(EXTRA_MUSIC_ALBUM, musicSpec.album) @@ -314,7 +314,7 @@ public class GBDeviceService implements DeviceService { } @Override - public void onSetNavigationInfo(NavigationInfoSpec navigationInfoSpec) { + public void onSetNavigationInfo(@NonNull NavigationInfoSpec navigationInfoSpec) { Intent intent = createIntent().setAction(ACTION_SETNAVIGATIONINFO) .putExtra(EXTRA_NAVIGATION_INSTRUCTION, navigationInfoSpec.instruction) .putExtra(EXTRA_NAVIGATION_NEXT_ACTION, navigationInfoSpec.nextAction) @@ -360,9 +360,9 @@ public class GBDeviceService implements DeviceService { } @Override - public void onAppConfiguration(UUID uuid, String config, Integer id) { + public void onAppConfiguration(UUID appUuid, String config, Integer id) { Intent intent = createIntent().setAction(ACTION_APP_CONFIGURE) - .putExtra(EXTRA_APP_UUID, uuid) + .putExtra(EXTRA_APP_UUID, appUuid) .putExtra(EXTRA_APP_CONFIG, config); if (id != null) { @@ -471,7 +471,7 @@ public class GBDeviceService implements DeviceService { } @Override - public void onAddCalendarEvent(CalendarEventSpec calendarEventSpec) { + public void onAddCalendarEvent(@NonNull CalendarEventSpec calendarEventSpec) { Intent intent = createIntent().setAction(ACTION_ADD_CALENDAREVENT) .putExtra(EXTRA_CALENDAREVENT_ID, calendarEventSpec.id) .putExtra(EXTRA_CALENDAREVENT_EVENT_ID, calendarEventSpec.eventId) @@ -501,7 +501,7 @@ public class GBDeviceService implements DeviceService { } @Override - public void onSendConfiguration(String config) { + public void onSendConfiguration(@NonNull String config) { Intent intent = createIntent().setAction(ACTION_SEND_CONFIGURATION) .putExtra(EXTRA_CONFIG, config); invokeService(intent); @@ -533,18 +533,21 @@ public class GBDeviceService implements DeviceService { * @return contact DisplayName, if found it */ private String getContactDisplayNameByNumber(String number) { + if (number == null || number.isEmpty()) { + return number; + } + Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.ENTERPRISE_CONTENT_FILTER_URI, Uri.encode(number)); String name = number; - if (number == null || number.equals("")) { - return name; - } - try (Cursor contactLookup = mContext.getContentResolver().query(uri, null, null, null, null)) { if (contactLookup != null && contactLookup.getCount() > 0) { contactLookup.moveToNext(); - name = contactLookup.getString(contactLookup.getColumnIndex(ContactsContract.Data.DISPLAY_NAME)); + int index = contactLookup.getColumnIndex(ContactsContract.Data.DISPLAY_NAME); + if (index >= 0) { + name = contactLookup.getString(index); + } } } catch (SecurityException e) { // ignore, just return name below @@ -608,10 +611,10 @@ public class GBDeviceService implements DeviceService { @Override public void onMusicOperation(int operation, int playlistIndex, String playlistName, ArrayList musicIds) { Intent intent = createIntent().setAction(ACTION_REQUEST_MUSIC_OPERATION); - intent.putExtra("operation", operation); - intent.putExtra("playlistIndex", playlistIndex); - intent.putExtra("playlistName", playlistName); - intent.putExtra("musicIds", musicIds); + intent.putExtra(EXTRA_REQUEST_MUSIC_OPERATION, operation); + intent.putExtra(EXTRA_REQUEST_MUSIC_PLAY_LIST_INDEX, playlistIndex); + intent.putExtra(EXTRA_REQUEST_MUSIC_PLAY_LIST_NAME, playlistName); + intent.putExtra(EXTRA_REQUEST_MUSIC_MUSIC_IDS, musicIds); invokeService(intent); } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/DeviceService.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/DeviceService.java index f2e596ad46..29545f6130 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/DeviceService.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/DeviceService.java @@ -1,8 +1,8 @@ -/* Copyright (C) 2015-2024 Andreas Shimokawa, Arjan Schrijver, Carsten +/* Copyright (C) 2015-2026 Andreas Shimokawa, Arjan Schrijver, Carsten Pfeiffer, Daniel Dakhno, Daniele Gobbetti, Davis Mosenkovs, Frank Slezak, Gabriele Monaco, Gordon Williams, ivanovlev, JohnnySun, José Rebelo, Julien Pivotto, Kasha, mvn23, Petr Vaněk, Sebastian Kranz, Steffen Liebergeld, - Taavi Eomäe + Taavi Eomäe, Thomas Kuehne This file is part of Gadgetbridge. @@ -84,6 +84,10 @@ public interface DeviceService extends EventHandler { String ACTION_CAMERA_STATUS_CHANGE = PREFIX + ".action.camera_status_change"; String ACTION_REQUEST_MUSIC_LIST = PREFIX + ".action.request_music_list"; String ACTION_REQUEST_MUSIC_OPERATION = PREFIX + ".action.request_music_operation"; + String EXTRA_REQUEST_MUSIC_OPERATION = "operation"; + String EXTRA_REQUEST_MUSIC_PLAY_LIST_INDEX = "playlistIndex"; + String EXTRA_REQUEST_MUSIC_PLAY_LIST_NAME = "playlistName"; + String EXTRA_REQUEST_MUSIC_MUSIC_IDS = "musicIds"; String ACTION_SLEEP_AS_ANDROID = ".action.sleep_as_android"; String EXTRA_SLEEP_AS_ANDROID_ACTION = "sleepasandroid_action"; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java index b1a27f4042..e3830ecec0 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java @@ -1256,10 +1256,10 @@ public class DeviceCommunicationService extends Service implements SharedPrefere deviceSupport.onMusicListReq(); break; case ACTION_REQUEST_MUSIC_OPERATION: - int operation = intentCopy.getIntExtra("operation", -1); - int playlistIndex = intentCopy.getIntExtra("playlistIndex", -1); - String playlistName = intentCopy.getStringExtra("playlistName"); - ArrayList musics = (ArrayList) intentCopy.getSerializableExtra("musicIds"); + int operation = intentCopy.getIntExtra(EXTRA_REQUEST_MUSIC_OPERATION, -1); + int playlistIndex = intentCopy.getIntExtra(EXTRA_REQUEST_MUSIC_PLAY_LIST_INDEX, -1); + String playlistName = intentCopy.getStringExtra(EXTRA_REQUEST_MUSIC_PLAY_LIST_NAME); + ArrayList musics = (ArrayList) intentCopy.getSerializableExtra(EXTRA_REQUEST_MUSIC_MUSIC_IDS); deviceSupport.onMusicOperation(operation, playlistIndex, playlistName, musics); break; }