diff --git a/android/app/src/main/java/com/hyzq/boss/SkillInventoryActivity.java b/android/app/src/main/java/com/hyzq/boss/SkillInventoryActivity.java index 48b898d..e164be3 100644 --- a/android/app/src/main/java/com/hyzq/boss/SkillInventoryActivity.java +++ b/android/app/src/main/java/com/hyzq/boss/SkillInventoryActivity.java @@ -9,12 +9,18 @@ import androidx.annotation.Nullable; import org.json.JSONArray; import org.json.JSONObject; +import java.util.LinkedHashMap; +import java.util.Map; + public class SkillInventoryActivity extends BossScreenActivity { public static final String EXTRA_DEVICE_ID = "device_id"; public static final String EXTRA_DEVICE_NAME = "device_name"; + private static final long REALTIME_RELOAD_THROTTLE_MS = 900L; private String deviceId; private String deviceName; + private @Nullable BossRealtimeClient realtimeClient; + private final Map recentRealtimeEventTimestamps = new LinkedHashMap<>(); @Override protected void onCreate(@Nullable Bundle savedInstanceState) { @@ -22,9 +28,28 @@ public class SkillInventoryActivity extends BossScreenActivity { deviceId = getIntent().getStringExtra(EXTRA_DEVICE_ID); deviceName = getIntent().getStringExtra(EXTRA_DEVICE_NAME); configureScreen("技能", deviceName == null ? "当前设备 Skill 清单" : deviceName); + realtimeClient = new BossRealtimeClient(apiClient, this::handleRealtimeEvent); reload(); } + @Override + protected void onResume() { + super.onResume(); + updateRealtimeSubscription(); + } + + @Override + protected void onPause() { + stopRealtimeUpdates(); + super.onPause(); + } + + @Override + protected void onDestroy() { + stopRealtimeUpdates(); + super.onDestroy(); + } + @Override protected void reload() { setRefreshing(true); @@ -44,6 +69,72 @@ public class SkillInventoryActivity extends BossScreenActivity { }); } + private void updateRealtimeSubscription() { + if (apiClient != null && apiClient.hasSessionHints() && realtimeClient != null) { + realtimeClient.start(); + return; + } + stopRealtimeUpdates(); + } + + private void stopRealtimeUpdates() { + if (realtimeClient != null) { + realtimeClient.stop(); + } + } + + void handleRealtimeEvent(BossRealtimeEvent event) { + if (event == null || event.eventName.isEmpty()) { + return; + } + if (!shouldReloadForRealtimeEvent(event)) { + return; + } + String eventFingerprint = BossRealtimeClient.buildEventFingerprint(event); + if (eventFingerprint.isEmpty()) { + return; + } + long now = System.currentTimeMillis(); + if (isDuplicateRealtimeEvent(eventFingerprint, now)) { + return; + } + runOnUiThread(this::reload); + } + + private boolean shouldReloadForRealtimeEvent(BossRealtimeEvent event) { + if (!"devices.skills.updated".equals(event.eventName) && !"devices.updated".equals(event.eventName)) { + return false; + } + String payloadDeviceId = event.payload.optString("deviceId", "").trim(); + if (payloadDeviceId.isEmpty()) { + return true; + } + if (deviceId == null || deviceId.isEmpty()) { + return true; + } + return payloadDeviceId.equals(deviceId); + } + + private boolean isDuplicateRealtimeEvent(String eventFingerprint, long now) { + pruneRecentRealtimeEvents(now); + Long previousEventAt = recentRealtimeEventTimestamps.get(eventFingerprint); + if (previousEventAt != null && now - previousEventAt < REALTIME_RELOAD_THROTTLE_MS) { + return true; + } + recentRealtimeEventTimestamps.put(eventFingerprint, now); + return false; + } + + private void pruneRecentRealtimeEvents(long now) { + java.util.Iterator> iterator = recentRealtimeEventTimestamps.entrySet().iterator(); + while (iterator.hasNext()) { + Map.Entry entry = iterator.next(); + if (now - entry.getValue() >= REALTIME_RELOAD_THROTTLE_MS) { + iterator.remove(); + } + } + } + private String resolveTargetDeviceId() throws Exception { String explicitDeviceId = deviceId; String boundDeviceId = null; diff --git a/android/app/src/test/java/com/hyzq/boss/SkillInventoryActivityTest.java b/android/app/src/test/java/com/hyzq/boss/SkillInventoryActivityTest.java new file mode 100644 index 0000000..7f6e4ea --- /dev/null +++ b/android/app/src/test/java/com/hyzq/boss/SkillInventoryActivityTest.java @@ -0,0 +1,84 @@ +package com.hyzq.boss; + +import static org.junit.Assert.assertEquals; + +import android.content.Intent; + +import org.json.JSONObject; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.Robolectric; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.Shadows; +import org.robolectric.annotation.Config; +import org.robolectric.util.ReflectionHelpers; + +@RunWith(RobolectricTestRunner.class) +@Config(sdk = 34) +public class SkillInventoryActivityTest { + @Test + public void matchingSkillsUpdatedEventTriggersReload() throws Exception { + Intent intent = new Intent() + .putExtra(SkillInventoryActivity.EXTRA_DEVICE_ID, "device-1") + .putExtra(SkillInventoryActivity.EXTRA_DEVICE_NAME, "Mac Studio"); + TestSkillInventoryActivity activity = Robolectric + .buildActivity(TestSkillInventoryActivity.class, intent) + .setup() + .resume() + .get(); + activity.reloadEnabled = true; + activity.reloadCount = 0; + + ReflectionHelpers.callInstanceMethod( + activity, + "handleRealtimeEvent", + ReflectionHelpers.ClassParameter.from( + BossRealtimeEvent.class, + new BossRealtimeEvent("devices.skills.updated", new JSONObject().put("deviceId", "device-1")) + ) + ); + Shadows.shadowOf(activity.getMainLooper()).idle(); + + assertEquals(1, activity.reloadCount); + } + + @Test + public void unrelatedDeviceEventDoesNotTriggerReload() throws Exception { + Intent intent = new Intent() + .putExtra(SkillInventoryActivity.EXTRA_DEVICE_ID, "device-1") + .putExtra(SkillInventoryActivity.EXTRA_DEVICE_NAME, "Mac Studio"); + TestSkillInventoryActivity activity = Robolectric + .buildActivity(TestSkillInventoryActivity.class, intent) + .setup() + .resume() + .get(); + activity.reloadEnabled = true; + activity.reloadCount = 0; + + ReflectionHelpers.callInstanceMethod( + activity, + "handleRealtimeEvent", + ReflectionHelpers.ClassParameter.from( + BossRealtimeEvent.class, + new BossRealtimeEvent("devices.skills.updated", new JSONObject().put("deviceId", "device-2")) + ) + ); + Shadows.shadowOf(activity.getMainLooper()).idle(); + + assertEquals(0, activity.reloadCount); + } + + public static class TestSkillInventoryActivity extends SkillInventoryActivity { + private boolean reloadEnabled; + private int reloadCount; + + @Override + protected void reload() { + if (!reloadEnabled) { + return; + } + reloadCount += 1; + setRefreshing(false); + } + } +} diff --git a/public/downloads/boss-android-latest.apk b/public/downloads/boss-android-latest.apk index 7bd8e34..9daae4b 100644 Binary files a/public/downloads/boss-android-latest.apk and b/public/downloads/boss-android-latest.apk differ diff --git a/public/downloads/boss-android-latest.json b/public/downloads/boss-android-latest.json index e71719b..63ef977 100644 --- a/public/downloads/boss-android-latest.json +++ b/public/downloads/boss-android-latest.json @@ -1,9 +1,9 @@ { "fileName": "boss-android-v2.5.11-release.apk", "urlPath": "/api/v1/user/ota/package", - "sizeBytes": 3355141, - "updatedAt": "2026-04-07T08:01:27Z", - "sha256": "2313bb47c3e54a0e22d94ededbf9735869e1918e3c75f5e2a8b9ebaade8246f0", + "sizeBytes": 3354908, + "updatedAt": "2026-04-07T08:07:13Z", + "sha256": "519c26f094aa0babb102ff557e8ed44f2f97290133765300f2f3ba1766923290", "versionName": "2.5.11", "versionCode": 24, "buildFlavor": "release" diff --git a/public/downloads/boss-android-v2.5.11-release.apk b/public/downloads/boss-android-v2.5.11-release.apk index 7bd8e34..9daae4b 100644 Binary files a/public/downloads/boss-android-v2.5.11-release.apk and b/public/downloads/boss-android-v2.5.11-release.apk differ