Skip to content

Commit 409222a

Browse files
committed
update & bug fixes
1 parent ddd3d48 commit 409222a

7 files changed

Lines changed: 187 additions & 187 deletions

File tree

play-services-wearable/core/src/main/java/org/microg/gms/wearable/ClockworkNodePreferences.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public long getNextSeqId() {
6363
preferences.edit()
6464
.putLong(CLOCKWORK_NODE_PREFERENCE_NEXT_SEQ_ID_BLOCK, seqIdBlock + 1000)
6565
.commit();
66-
seqIdBlock = 0;
66+
seqIdInBlock = 0;
6767
}
6868

6969
if (seqIdInBlock >= 1000) {

play-services-wearable/core/src/main/java/org/microg/gms/wearable/DataTransport.java

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class DataTransport {
3434

3535
private volatile SyncStart pendingSyncStart = null;
3636
private volatile Runnable syncFallback = null;
37-
37+
private volatile long syncDeadline = 0L;
3838
private volatile boolean isV1Peer = false;
3939

4040
public DataTransport(String localNodeId, String peerNodeId, WearableImpl wearable) {
@@ -122,18 +122,11 @@ public void sendSyncStart() {
122122
+ " receivedSeqId=" + receivedSeqId + " entries=" + table.size());
123123
}
124124

125-
private static final long SYNC_FALLBACK_MS = 10_000;
125+
private static final long SYNC_FALLBACK_MS = 1500;
126+
private static final long SYNC_MAX_DEFER_MS = 4_000;
126127
public void respondToSyncStart(SyncStart syncStart) {
127-
// ConnectionConfiguration cfg = wearable.getConfigurationByPeerNodeId(peerNodeId);
128-
// if (cfg != null && !cfg.dataItemSyncEnabled) {
129-
// Log.d(TAG, "respondToSyncStart: dataItemSyncEnabled=false, deferring sync for " + peerNodeId);
130-
// pendingSyncStart = syncStart;
131-
// return;
132-
// }
133-
//
134-
// doRespondToSyncStart(syncStart);
135-
136128
pendingSyncStart = syncStart;
129+
syncDeadline = android.os.SystemClock.uptimeMillis() + SYNC_MAX_DEFER_MS;
137130

138131
scheduleFallback();
139132
Log.d(TAG, "respondToSyncStart: deferred sync for " + peerNodeId
@@ -145,7 +138,8 @@ private void scheduleFallback() {
145138
if (pendingSyncStart == null) return;
146139
ChannelManager cm = wearable.getChannelManager();
147140
boolean channelActive = cm != null && cm.hasActiveChannelForNode(peerNodeId);
148-
if (channelActive) {
141+
boolean deadlinePassed = android.os.SystemClock.uptimeMillis() >= syncDeadline;
142+
if (channelActive && !deadlinePassed) {
149143
Log.d(TAG, "syncFallback: channel still active for " + peerNodeId
150144
+ ", rescheduling");
151145
scheduleFallback();
@@ -221,12 +215,23 @@ private void runSync(Map<String, Long> remotePeerSeqIds) {
221215
Log.d(TAG, "runSync start: peer=" + peerNodeId);
222216
try {
223217
Map<String, Long> localSeqIds = wearable.getNodeDatabase().getAllCurrentSeqIds();
218+
int synced = 0;
219+
int skipped = 0;
224220
for (Map.Entry<String, Long> local : localSeqIds.entrySet()) {
225221
if (Thread.currentThread().isInterrupted()) {
226222
Log.d(TAG, "runSync interrupted for peer=" + peerNodeId);
227223
return;
228224
}
229225

226+
String src = local.getKey();
227+
long peer = remotePeerSeqIds.containsKey(src) ? remotePeerSeqIds.get(src) : -1;
228+
long localMax = local.getValue() != null ? local.getValue() : -1;
229+
230+
if (localMax <= peer) {
231+
skipped++;
232+
continue;
233+
}
234+
230235
if (hasActiveChannelForPeer()) {
231236
try {
232237
Thread.sleep(20);
@@ -236,10 +241,8 @@ private void runSync(Map<String, Long> remotePeerSeqIds) {
236241
}
237242
}
238243

239-
String src = local.getKey();
240-
long peer = remotePeerSeqIds.containsKey(src) ? remotePeerSeqIds.get(src) : -1l;
241-
242244
wearable.syncToPeer(peerNodeId, src, peer);
245+
synced++;
243246
}
244247
} catch (Exception e) {
245248
Log.w(TAG, "runSync exception for peer=" + peerNodeId, e);

play-services-wearable/core/src/main/java/org/microg/gms/wearable/WearableImpl.java

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ public class WearableImpl {
101101
private static final long ASSET_FETCH_COOLDOWN_MS = 500;
102102
private static final int ASSET_BATCH_SIZE = 10;
103103
private static final long NODE_DISCONNECT_DEBOUNCE_MS = 2500;
104+
private static final long ASSET_FETCH_INITIAL_DELAY_MS = 1500;
104105
private final Context context;
105106
private final NodeDatabaseHelper nodeDatabase;
106107
private final ConfigurationDatabaseHelper configDatabase;
@@ -779,33 +780,38 @@ public void onConnectReceived(WearableConnection connection, String nodeId, Conn
779780
}
780781

781782
if (btAddress != null) {
782-
Iterator<Map.Entry<String, WearableConnection>> connIter = activeConnections.entrySet().iterator();
783+
Set<String> staleNodeIds = new HashSet<>();
783784

784-
while (connIter.hasNext()) {
785-
Map.Entry<String, WearableConnection> e = connIter.next();
786-
787-
if (e.getKey().equals(connect.id))
785+
for (String existingId : activeConnections.keySet()) {
786+
if (existingId.equals(connect.id))
788787
continue;
789788

790789
for (ConnectionConfiguration c : getConfigurations()) {
791-
if (e.getKey().equals(c.peerNodeId) && btAddress.equals(c.address)) {
792-
connIter.remove();
790+
if (existingId.equals(c.peerNodeId) && btAddress.equals(c.address)) {
791+
staleNodeIds.add(existingId);
793792
break;
794793
}
795794
}
796795
}
797796

798797
for (Node n : connectedNodes) {
799-
if (n.getId().equals(connect.id))
798+
String existsingId = n.getId();
799+
if (existsingId.equals(connect.id))
800800
continue;
801801

802802
for (ConnectionConfiguration c : getConfigurations()) {
803-
if (n.getId().equals(c.peerNodeId) && btAddress.equals(c.address)) {
804-
connIter.remove();
803+
if (existsingId.equals(c.peerNodeId) && btAddress.equals(c.address)) {
804+
staleNodeIds.add(existsingId);
805805
break;
806806
}
807807
}
808808
}
809+
810+
for (String staleId : staleNodeIds) {
811+
Log.i(TAG, "onConnectReceived: evicting stale connection" + staleId
812+
+ " for address " + btAddress + " superseded by " + connect.id);
813+
closeConnection(staleId);
814+
}
809815
}
810816

811817
activeConnections.put(connect.id, connection);
@@ -823,7 +829,7 @@ public void onConnectReceived(WearableConnection connection, String nodeId, Conn
823829
} else {
824830
Log.d(TAG, "Connection closed before asset fetch could start");
825831
}
826-
}, 5000);
832+
}, ASSET_FETCH_INITIAL_DELAY_MS);
827833
} else {
828834
Log.w(TAG, "networkHandler is dead, skipping asset fetch scheduling for " + connect.id);
829835
}

0 commit comments

Comments
 (0)