@@ -48,10 +48,11 @@ public class SyncClientImpl implements SyncClient {
48
48
this .connectivityMonitor = builder .platform .getConnectivityMonitor ();
49
49
50
50
long boxStoreHandle = InternalAccess .getHandle (builder .boxStore );
51
- this . handle = nativeCreate (boxStoreHandle , serverUrl , builder .trustedCertPaths );
51
+ long handle = nativeCreate (boxStoreHandle , serverUrl , builder .trustedCertPaths );
52
52
if (handle == 0 ) {
53
53
throw new RuntimeException ("Failed to create sync client: handle is zero." );
54
54
}
55
+ this .handle = handle ;
55
56
56
57
// Only change setting if not default (automatic sync updates and push subscription enabled).
57
58
if (builder .requestUpdatesMode != RequestUpdatesMode .AUTO ) {
@@ -84,6 +85,14 @@ public class SyncClientImpl implements SyncClient {
84
85
InternalAccess .setSyncClient (builder .boxStore , this );
85
86
}
86
87
88
+ private long getHandle () {
89
+ long handle = this .handle ;
90
+ if (handle == 0 ) {
91
+ throw new IllegalStateException ("SyncClient already closed" );
92
+ }
93
+ return handle ;
94
+ }
95
+
87
96
@ Override
88
97
public String getServerUrl () {
89
98
return serverUrl ;
@@ -101,24 +110,24 @@ public boolean isLoggedIn() {
101
110
102
111
@ Override
103
112
public long getServerTimeNanos () {
104
- return nativeServerTime (handle );
113
+ return nativeServerTime (getHandle () );
105
114
}
106
115
107
116
@ Override
108
117
public long getServerTimeDiffNanos () {
109
- return nativeServerTimeDiff (handle );
118
+ return nativeServerTimeDiff (getHandle () );
110
119
}
111
120
112
121
@ Override
113
122
public long getRoundtripTimeNanos () {
114
- return nativeRoundtripTime (handle );
123
+ return nativeRoundtripTime (getHandle () );
115
124
}
116
125
117
126
/**
118
127
* Gets the current state of this sync client. Throws if {@link #close()} was called.
119
128
*/
120
129
public SyncState getSyncState () {
121
- return SyncState .fromId (nativeGetState (handle ));
130
+ return SyncState .fromId (nativeGetState (getHandle () ));
122
131
}
123
132
124
133
@ Override
@@ -133,7 +142,7 @@ public void setSyncCompletedListener(@Nullable SyncCompletedListener listener) {
133
142
134
143
@ Override
135
144
public void setSyncChangeListener (@ Nullable SyncChangeListener changesListener ) {
136
- nativeSetSyncChangesListener (handle , changesListener );
145
+ nativeSetSyncChangesListener (getHandle () , changesListener );
137
146
}
138
147
139
148
@ Override
@@ -158,7 +167,7 @@ public void setSyncListener(@Nullable SyncListener listener) {
158
167
@ Override
159
168
public void setLoginCredentials (SyncCredentials credentials ) {
160
169
SyncCredentialsToken credentialsInternal = (SyncCredentialsToken ) credentials ;
161
- nativeSetLoginInfo (handle , credentialsInternal .getTypeId (), credentialsInternal .getTokenBytes ());
170
+ nativeSetLoginInfo (getHandle () , credentialsInternal .getTypeId (), credentialsInternal .getTokenBytes ());
162
171
credentialsInternal .clear (); // Clear immediately, not needed anymore.
163
172
}
164
173
@@ -172,7 +181,7 @@ public boolean awaitFirstLogin(long millisToWait) {
172
181
173
182
@ Override
174
183
public synchronized void start () {
175
- nativeStart (handle );
184
+ nativeStart (getHandle () );
176
185
started = true ;
177
186
if (connectivityMonitor != null ) {
178
187
connectivityMonitor .setObserver (this );
@@ -189,11 +198,7 @@ public synchronized void stop() {
189
198
if (connectivityMonitor != null ) {
190
199
connectivityMonitor .removeObserver ();
191
200
}
192
-
193
- long handleToStop = this .handle ;
194
- if (handleToStop != 0 ) {
195
- nativeStop (handleToStop );
196
- }
201
+ nativeStop (getHandle ());
197
202
started = false ;
198
203
}
199
204
@@ -240,35 +245,35 @@ protected void finalize() throws Throwable {
240
245
@ Override
241
246
@ Experimental
242
247
public boolean requestFullSync () {
243
- return nativeRequestFullSync (handle , false );
248
+ return nativeRequestFullSync (getHandle () , false );
244
249
}
245
250
246
251
/**
247
252
* Temporary only, try not to use it.
248
253
*/
249
254
@ Experimental
250
255
public boolean requestFullSyncAndUpdates () {
251
- return nativeRequestFullSync (handle , true );
256
+ return nativeRequestFullSync (getHandle () , true );
252
257
}
253
258
254
259
@ Override
255
260
public boolean requestUpdates () {
256
- return nativeRequestUpdates (handle , true );
261
+ return nativeRequestUpdates (getHandle () , true );
257
262
}
258
263
259
264
@ Override
260
265
public boolean requestUpdatesOnce () {
261
- return nativeRequestUpdates (handle , false );
266
+ return nativeRequestUpdates (getHandle () , false );
262
267
}
263
268
264
269
@ Override
265
270
public boolean cancelUpdates () {
266
- return nativeCancelUpdates (handle );
271
+ return nativeCancelUpdates (getHandle () );
267
272
}
268
273
269
274
@ Override
270
275
public void notifyConnectionAvailable () {
271
- nativeTriggerReconnect (handle );
276
+ nativeTriggerReconnect (getHandle () );
272
277
}
273
278
274
279
@ Override
@@ -452,7 +457,7 @@ public boolean send() {
452
457
}
453
458
checkNotSent ();
454
459
sent = true ;
455
- return syncClient .nativeObjectsMessageSend (syncClient .handle , builderHandle );
460
+ return syncClient .nativeObjectsMessageSend (syncClient .getHandle () , builderHandle );
456
461
}
457
462
458
463
private void checkNotSent () {
0 commit comments