GraphQLCall.Callback<Query.Data> deltaQueryCallback, long baseRefreshIntervalInSeconds) { AWSAppSyncDeltaSync helper = new AWSAppSyncDeltaSync(baseQuery,this, applicationContext); helper.setBaseQueryCallback(baseQueryCallback); helper.setSubscription(subscription); helper.setSubscriptionCallback( subscriptionCallback); helper.setDeltaQuery(baseQuery); helper.setDeltaQueryCallback(baseQueryCallback); helper.setDeltaQuery(deltaQuery); helper.setDeltaQueryCallback(deltaQueryCallback); helper.setBaseRefreshIntervalInSeconds(baseRefreshIntervalInSeconds); return new AWSAppSyncDeltaSyncWatcher(helper.execute(false));
@Override public void cancel() { if (!canceled) { AWSAppSyncDeltaSync.cancel(id); canceled = true; } }
static void handleNetworkUpEvent() { //Guard against duplicate notifications of network up. The transition should result in //delta sync being run once. synchronized (networkLock) { if ( !networkUp ) { networkUp = true; for (Map.Entry<Long, AWSAppSyncDeltaSync> ds : deltaSyncObjects.entrySet()) { Log.d(TAG, "Delta Sync: Network Up detected. Running DeltaSync for ds object [" + ds.getKey() + "]"); ds.getValue().cancelRetry(); ds.getValue().execute(false); } } } }
@Override public void handleMessage(Message msg) { if (msg.what == MSG_CHECK) { // remove messages of the same type networkUpdateHandler.removeMessages(MSG_CHECK); // set shouldProcess to true Log.d(TAG, "Thread:[" + Thread.currentThread().getId() +"]: Internet CONNECTED."); synchronized (shouldProcessMutationsLock) { shouldProcessMutations = true; } // Send EXEC message to queueHandler if (queueHandler != null ) { Message message = new Message(); message.obj = new MutationInterceptorMessage(); message.what = MessageNumberUtil.SUCCESSFUL_EXEC; queueHandler.sendMessage(message); } //Trigger DeltaSync to handle Network up events. AWSAppSyncDeltaSync.handleNetworkUpEvent(); } else if (msg.what == MSG_DISCONNECT) { // Network Disconnected, pause mutations Log.d(TAG, "Thread:[" + Thread.currentThread().getId() +"]: Internet DISCONNECTED."); synchronized (shouldProcessMutationsLock) { shouldProcessMutations = false; } //Propagate network down event to DeltaSync AWSAppSyncDeltaSync.handleNetworkDownEvent(); } } }
@Override public void run() { if (thisObjectRef.get() != null ) { thisObjectRef.get().execute(true); } } }, runAtTime, TimeUnit.SECONDS);
@OnLifecycleEvent(Lifecycle.Event.ON_START) public void startSomething() { Log.v(TAG, "Thread:[" + Thread.currentThread().getId() +"]: Delta Sync: App is in FOREGROUND"); AWSAppSyncDeltaSync.handleAppForeground(); }
@OnLifecycleEvent(Lifecycle.Event.ON_STOP) public void stopSomething() { Log.v(TAG, "Thread:[" + Thread.currentThread().getId() +"]: Delta Sync: App is in BACKGROUND"); AWSAppSyncDeltaSync.handleAppBackground(); } }
private void initializeIfRequired() { synchronized(initializationLock) { Log.d(TAG, "In initialize method"); if (awsAppSyncDeltaSyncSqlHelper == null ) { //Setup up the local store Log.d(TAG, "Initializing the database"); awsAppSyncDeltaSyncSqlHelper = new AWSAppSyncDeltaSyncSqlHelper(mContext); } if( dbHelper == null ) { dbHelper = new AWSAppSyncDeltaSyncDBOperations(awsAppSyncDeltaSyncSqlHelper); } if (!recordCreatedOrFound) { AWSAppSyncDeltaSyncDBOperations.DeltaSyncRecord record; if ((record = dbHelper.getRecordByKey(getKey())) == null ) { this.id = dbHelper.createRecord(getKey(), lastRunTimeInMilliSeconds); } else { this.id = record.id; this.lastRunTimeInMilliSeconds = record.lastRunTimeInMilliSeconds; } deltaSyncObjects.put(id,this); recordCreatedOrFound = true; } } }
awsAppSyncClient.query(adjust(deltaQuery)) .responseFetcher(AppSyncResponseFetchers.NETWORK_ONLY) .enqueue(dcb);
@Override public void run() { if (thisObjectRef.get() != null ) { thisObjectRef.get().execute(false); } } }, runAtTime, TimeUnit.MILLISECONDS);
static void handleAppForeground() { //Guard against duplicate notifications of app transitioning to foreground . The transition should result in //delta sync being run once. synchronized (foregroundLock) { if ( !appInForeground ) { appInForeground = true; synchronized (networkLock) { if (networkUp ) { for (Map.Entry<Long, AWSAppSyncDeltaSync> ds : deltaSyncObjects.entrySet()) { Log.d(TAG, "Delta Sync: Foreground transition detected. Running DeltaSync for ds object [" + ds.getKey() + "]"); ds.getValue().cancelRetry(); ds.getValue().execute(false); } } } } } }
static void cancel(Long id) { AWSAppSyncDeltaSync deltaSync = deltaSyncObjects.get(id); if (deltaSync != null ) { deltaSync.cancel(); } }