Search in sources :

Example 1 with SyncInfo

use of com.fitpay.android.paymentdevice.models.SyncInfo in project fitpay-android-sdk by fitpay.

the class DeviceSyncManager method add.

public void add(final SyncRequest request) {
    if (request == null) {
        return;
    }
    // if we have a syncId, dedupe it to avoid re-running syncs arriving through multiple channels
    if (request.getSyncId() != null) {
        if (dedupeSyncIds.contains(request.getSyncId())) {
            FPLog.i("duplicated sync received, skipping: " + request);
            return;
        }
        dedupeSyncIds.add(request.getSyncId());
        // ensure the dedupe size remains consistent and doesn't grow unbounded
        while (dedupeSyncIds.size() > DEDUPE_LIMIT) {
            dedupeSyncIds.remove(0);
        }
    }
    try {
        SyncInfo syncInfo = request.getSyncInfo();
        if (syncInfo != null) {
            syncInfo.sendAckSync(request.getSyncId(), new ApiCallback<Void>() {

                @Override
                public void onSuccess(Void result) {
                    FPLog.i("ackSync has been sent successfully.");
                }

                @Override
                public void onFailure(int errorCode, String errorMessage) {
                    FPLog.w("ackSync failed to send.");
                }
            });
        }
        requests.put(request);
        FPLog.d("added sync request to queue for processing, current queue size [" + requests.size() + "]: " + request);
        for (DeviceSyncManagerCallback callback : syncManagerCallbacks) {
            callback.syncRequestAdded(request);
        }
    } catch (InterruptedException e) {
        FPLog.w("interrupted exception while waiting to add sync request to processing queue");
    }
}
Also used : SyncInfo(com.fitpay.android.paymentdevice.models.SyncInfo)

Example 2 with SyncInfo

use of com.fitpay.android.paymentdevice.models.SyncInfo in project fitpay-android-sdk by fitpay.

the class WebViewCommunicatorImpl method getUserAndDevice.

private void getUserAndDevice(final String deviceId, final String callbackId) {
    ApiManager.getInstance().getUser(new ApiCallback<User>() {

        @Override
        public void onSuccess(User result) {
            if (result == null) {
                onTaskError(EventCallback.USER_CREATED, callbackId, "getUser failed: result is null");
                return;
            }
            WebViewCommunicatorImpl.this.user = result;
            RxBus.getInstance().post(new UserReceived(user.getId(), user.getUsername()));
            EventCallback eventCallback = new EventCallback.Builder().setCommand(EventCallback.USER_CREATED).setStatus(EventCallback.STATUS_OK).build();
            eventCallback.send();
            result.getDevice(deviceId, new ApiCallback<Device>() {

                @Override
                public void onSuccess(Device result) {
                    WebViewCommunicatorImpl.this.device = result;
                    String token = ApiManager.getPushToken();
                    String deviceToken = device.getNotificationToken();
                    final Runnable onSuccess = () -> onTaskSuccess(EventCallback.GET_USER_AND_DEVICE, callbackId);
                    boolean automaticallySubscribeToUserEventStream = true;
                    if (ApiManager.getConfig().containsKey(ApiManager.PROPERTY_AUTOMATICALLY_SUBSCRIBE_TO_USER_EVENT_STREAM)) {
                        automaticallySubscribeToUserEventStream = "true".equals(ApiManager.getConfig().get(ApiManager.PROPERTY_AUTOMATICALLY_SUBSCRIBE_TO_USER_EVENT_STREAM));
                    }
                    if (automaticallySubscribeToUserEventStream) {
                        try {
                            UserEventStreamManager.subscribe(user.getId());
                        } catch (IOException e) {
                            FPLog.e(e);
                        }
                        boolean automaticSyncThroughUserEventStream = true;
                        if (ApiManager.getConfig().containsKey(ApiManager.PROPERTY_AUTOMATICALLY_SYNC_FROM_USER_EVENT_STREAM)) {
                            automaticSyncThroughUserEventStream = "true".equals(ApiManager.getConfig().get(ApiManager.PROPERTY_AUTOMATICALLY_SYNC_FROM_USER_EVENT_STREAM));
                        }
                        if (automaticSyncThroughUserEventStream) {
                            userEventStreamSyncListener = new UserEventStreamListener() {

                                @Override
                                public void onUserEvent(UserStreamEvent event) {
                                    if ("SYNC".equals(event.getType())) {
                                        SyncInfo syncInfo = gson.fromJson(event.getPayload(), SyncInfo.class);
                                        syncInfo.setInitiator(SyncInitiator.PLATFORM);
                                        SyncRequest syncRequest = new SyncRequest.Builder().setSyncId(syncInfo.getSyncId()).setSyncInfo(syncInfo).setConnector(deviceService.getPaymentDeviceConnector()).setDevice(device).setUser(user).build();
                                        RxBus.getInstance().post(syncRequest);
                                    }
                                }
                            };
                            NotificationManager.getInstance().addListener(userEventStreamSyncListener);
                        }
                    }
                    if (deviceToken == null || !deviceToken.equals(token)) {
                        Device updatedDevice = new Device.Builder().setNotificationToken(token).build();
                        device.updateToken(updatedDevice, deviceToken == null, new ApiCallback<Device>() {

                            @Override
                            public void onSuccess(Device result) {
                                WebViewCommunicatorImpl.this.device = result;
                                onSuccess.run();
                            }

                            @Override
                            public void onFailure(@ResultCode.Code int errorCode, String errorMessage) {
                                onTaskError(EventCallback.GET_USER_AND_DEVICE, callbackId, "update device failed:" + errorMessage);
                            }
                        });
                    } else {
                        onSuccess.run();
                    }
                }

                @Override
                public void onFailure(@ResultCode.Code int errorCode, String errorMessage) {
                    onTaskError(EventCallback.GET_USER_AND_DEVICE, callbackId, "getDevice failed " + errorMessage);
                }
            });
        }

        @Override
        public void onFailure(@ResultCode.Code int errorCode, String errorMessage) {
            onTaskError(EventCallback.USER_CREATED, callbackId, "getUser failed " + errorMessage);
        }
    });
}
Also used : User(com.fitpay.android.api.models.user.User) ApiCallback(com.fitpay.android.api.callbacks.ApiCallback) Device(com.fitpay.android.api.models.device.Device) UserStreamEvent(com.fitpay.android.api.models.UserStreamEvent) UserEventStreamListener(com.fitpay.android.api.sse.UserEventStreamListener) EventCallback(com.fitpay.android.utils.EventCallback) IOException(java.io.IOException) SyncRequest(com.fitpay.android.paymentdevice.models.SyncRequest) NotificationSyncRequest(com.fitpay.android.paymentdevice.events.NotificationSyncRequest) SyncInfo(com.fitpay.android.paymentdevice.models.SyncInfo) UserReceived(com.fitpay.android.webview.events.UserReceived)

Example 3 with SyncInfo

use of com.fitpay.android.paymentdevice.models.SyncInfo in project fitpay-android-sdk by fitpay.

the class RtmParserV2 method parseMessage.

public void parseMessage(RtmMessage msg) {
    String callbackId = msg.getCallbackId();
    switch(msg.getType()) {
        case RtmType.USER_DATA:
            String deviceId = null;
            String token = null;
            String userId = null;
            try {
                JSONObject obj = new JSONObject(msg.getData());
                deviceId = obj.getString("deviceId");
                token = obj.getString("token");
                userId = obj.getString("userId");
            } catch (Exception e) {
                FPLog.e(WV_DATA, e);
                throwException("missing required message data");
            }
            impl.sendUserData(callbackId, deviceId, token, userId);
            break;
        case RtmType.SYNC:
            SyncInfo syncInfo = Constants.getGson().fromJson(msg.getData(), SyncInfo.class);
            if (null != syncInfo && null != syncInfo.getSyncId()) {
                syncInfo.setInitiator(SyncInitiator.WEB_VIEW);
                impl.sync(callbackId, syncInfo);
            } else {
                impl.sync(callbackId);
            }
            break;
        case RtmType.VERSION:
            try {
                RtmVersion webAppRtmVersion = Constants.getGson().fromJson(msg.getJsonData(), RtmVersion.class);
                if (webAppRtmVersion != null) {
                    impl.setWebAppRtmVersion(webAppRtmVersion);
                } else {
                    throw new NullPointerException("RtmVersion is empty");
                }
            } catch (Exception e) {
                FPLog.e(WV_DATA, e);
                throwException("missing required message data");
            }
            break;
        case RtmType.NO_HISTORY:
            impl.onNoHistory();
            break;
        default:
            super.parseMessage(msg);
    }
}
Also used : JSONObject(org.json.JSONObject) SyncInfo(com.fitpay.android.paymentdevice.models.SyncInfo) RtmVersion(com.fitpay.android.webview.models.RtmVersion)

Aggregations

SyncInfo (com.fitpay.android.paymentdevice.models.SyncInfo)3 ApiCallback (com.fitpay.android.api.callbacks.ApiCallback)1 UserStreamEvent (com.fitpay.android.api.models.UserStreamEvent)1 Device (com.fitpay.android.api.models.device.Device)1 User (com.fitpay.android.api.models.user.User)1 UserEventStreamListener (com.fitpay.android.api.sse.UserEventStreamListener)1 NotificationSyncRequest (com.fitpay.android.paymentdevice.events.NotificationSyncRequest)1 SyncRequest (com.fitpay.android.paymentdevice.models.SyncRequest)1 EventCallback (com.fitpay.android.utils.EventCallback)1 UserReceived (com.fitpay.android.webview.events.UserReceived)1 RtmVersion (com.fitpay.android.webview.models.RtmVersion)1 IOException (java.io.IOException)1 JSONObject (org.json.JSONObject)1