From db0c31fd6965c672fdbe976f2d6098164a05f45c Mon Sep 17 00:00:00 2001 From: Ephraim Kigamba Date: Wed, 27 Mar 2019 19:41:44 +0300 Subject: [PATCH] Retrieve the device received history and send it to the sender - Change the current connection level of the app after authorization and after negotiating the start of the data exchange Fixes #8 --- .../p2p/model/P2pReceivedHistory.java | 1 - .../p2p/presenter/P2PReceiverPresenter.java | 40 +++++++++++++++++-- .../p2p/presenter/P2PSenderPresenter.java | 10 +++++ .../p2p/sync/ConnectionLevel.java | 4 +- .../sync/IReceiverSyncLifecycleCallback.java | 6 +++ .../sync/ISenderSyncLifecycleCallback.java | 3 ++ .../p2p/tasks/GenericAsyncTask.java | 2 +- 7 files changed, 59 insertions(+), 7 deletions(-) diff --git a/p2p-sync/src/main/java/org/smartregister/p2p/model/P2pReceivedHistory.java b/p2p-sync/src/main/java/org/smartregister/p2p/model/P2pReceivedHistory.java index fa95587..4a6c40c 100644 --- a/p2p-sync/src/main/java/org/smartregister/p2p/model/P2pReceivedHistory.java +++ b/p2p-sync/src/main/java/org/smartregister/p2p/model/P2pReceivedHistory.java @@ -2,7 +2,6 @@ import android.arch.persistence.room.ColumnInfo; import android.arch.persistence.room.Entity; -import android.arch.persistence.room.PrimaryKey; import android.support.annotation.NonNull; /** diff --git a/p2p-sync/src/main/java/org/smartregister/p2p/presenter/P2PReceiverPresenter.java b/p2p-sync/src/main/java/org/smartregister/p2p/presenter/P2PReceiverPresenter.java index 54bb518..d4ffddb 100644 --- a/p2p-sync/src/main/java/org/smartregister/p2p/presenter/P2PReceiverPresenter.java +++ b/p2p-sync/src/main/java/org/smartregister/p2p/presenter/P2PReceiverPresenter.java @@ -22,6 +22,7 @@ import org.smartregister.p2p.contract.P2pModeSelectContract; import org.smartregister.p2p.handler.OnActivityRequestPermissionHandler; import org.smartregister.p2p.model.AppDatabase; +import org.smartregister.p2p.model.P2pReceivedHistory; import org.smartregister.p2p.model.SendingDevice; import org.smartregister.p2p.sync.ConnectionLevel; import org.smartregister.p2p.sync.DiscoveredDevice; @@ -30,6 +31,7 @@ import org.smartregister.p2p.tasks.Tasker; import org.smartregister.p2p.util.Constants; +import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.concurrent.Callable; @@ -244,7 +246,29 @@ public void onSuccess(@Nullable SendingDevice result) { if (sendingDevice.getAppLifetimeKey() .equals(appLifetimeKey)) { - // Todo: Get the records sent last time + Tasker.run(new Callable>() { + @Override + public List call() throws Exception { + return P2PLibrary.getInstance().getDb() + .p2pReceivedHistoryDao() + .getDeviceReceivedHistory(sendingDevice.getDeviceId()); + } + }, new GenericAsyncTask.OnFinishedCallback>() { + @Override + public void onSuccess(@Nullable List result) { + if (result == null) { + result = new ArrayList<>(); + } + + sendLastReceivedRecords(result); + } + + @Override + public void onError(Exception e) { + Timber.e(e); + disconnectAndReset(endpointId); + } + }); } else { // Clear the device history records && update device app key @@ -260,13 +284,14 @@ public void onSuccess(@Nullable Integer result) { Timber.e(view.getString(R.string.log_records_deleted), (int) result); } - // Todo: get the records sent last time and continue with the process + sendLastReceivedRecords(new ArrayList()); } @Override public void onError(Exception e) { Timber.e(view.getString(R.string.log_error_occurred_trying_to_delete_p2p_received_history_on_device) , sendingDevice.getDeviceId()); + disconnectAndReset(endpointId); } }); @@ -277,13 +302,12 @@ public void onError(Exception e) { @Override public Void call() throws Exception { registerSendingDevice(finalBasicDeviceDetails); - return null; } }, new GenericAsyncTask.OnFinishedCallback() { @Override public void onSuccess(@Nullable Void result) { - // Todo: get the records sent last time and continue with the process + sendLastReceivedRecords(new ArrayList()); } @Override @@ -311,6 +335,14 @@ public void onError(Exception e) { } } + @Override + public void sendLastReceivedRecords(@NonNull List receivedHistory) { + if (currentSender != null) { + interactor.sendMessage(new Gson().toJson(receivedHistory)); + connectionLevel = ConnectionLevel.SENT_RECEIVED_HISTORY; + } + } + private void registerSendingDevice(Map basicDeviceDetails) { SendingDevice sendingDevice = new SendingDevice(); sendingDevice.setDeviceId((String) basicDeviceDetails.get(Constants.BasicDeviceDetails.KEY_DEVICE_ID)); diff --git a/p2p-sync/src/main/java/org/smartregister/p2p/presenter/P2PSenderPresenter.java b/p2p-sync/src/main/java/org/smartregister/p2p/presenter/P2PSenderPresenter.java index 4902364..52299ef 100644 --- a/p2p-sync/src/main/java/org/smartregister/p2p/presenter/P2PSenderPresenter.java +++ b/p2p-sync/src/main/java/org/smartregister/p2p/presenter/P2PSenderPresenter.java @@ -164,6 +164,14 @@ public void onRequestConnectionFailed(@NonNull Exception exception) { prepareForDiscovering(false); } + @Override + public void processReceivedHistory(@NonNull Payload payload) { + if (currentReceiver != null) { + // Todo: I should process the received history here and use it to fetch records from the DB + connectionLevel = ConnectionLevel.RECEIPT_OF_RECEIVED_HISTORY; + } + } + @Override public void onConnectionInitiated(@NonNull final String endpointId, @NonNull final ConnectionInfo connectionInfo) { // Easier working with the device which we requested connection to, otherwise the callback for error should be called @@ -356,6 +364,8 @@ public void onPayloadReceived(@NonNull String endpointId, @NonNull Payload paylo performAuthorization(payload); } else if (connectionLevel.equals(ConnectionLevel.SENT_HASH_KEY)) { // Do nothing for now + processReceivedHistory(payload); + } else if (connectionLevel.equals(ConnectionLevel.RECEIPT_OF_RECEIVED_HISTORY)) { processPayload(endpointId, payload); } } diff --git a/p2p-sync/src/main/java/org/smartregister/p2p/sync/ConnectionLevel.java b/p2p-sync/src/main/java/org/smartregister/p2p/sync/ConnectionLevel.java index 60e67c3..7511fc8 100644 --- a/p2p-sync/src/main/java/org/smartregister/p2p/sync/ConnectionLevel.java +++ b/p2p-sync/src/main/java/org/smartregister/p2p/sync/ConnectionLevel.java @@ -6,6 +6,8 @@ public enum ConnectionLevel { AUTHENTICATED, AUTHORIZED, + SENT_HASH_KEY, RECEIVED_HASH_KEY, - SENT_HASH_KEY + SENT_RECEIVED_HISTORY, + RECEIPT_OF_RECEIVED_HISTORY } diff --git a/p2p-sync/src/main/java/org/smartregister/p2p/sync/IReceiverSyncLifecycleCallback.java b/p2p-sync/src/main/java/org/smartregister/p2p/sync/IReceiverSyncLifecycleCallback.java index cb03973..45934b9 100644 --- a/p2p-sync/src/main/java/org/smartregister/p2p/sync/IReceiverSyncLifecycleCallback.java +++ b/p2p-sync/src/main/java/org/smartregister/p2p/sync/IReceiverSyncLifecycleCallback.java @@ -3,6 +3,10 @@ import android.support.annotation.NonNull; import com.google.android.gms.nearby.connection.Payload; +import org.smartregister.p2p.model.P2pReceivedHistory; + +import java.util.List; + /** * Created by Ephraim Kigamba - ekigamba@ona.io on 15/03/2019 */ @@ -14,4 +18,6 @@ public interface IReceiverSyncLifecycleCallback extends SyncLifecycleCallback { void onAdvertisingFailed(@NonNull Exception e); void processHashKey(@NonNull final String endpointId, @NonNull Payload payload); + + void sendLastReceivedRecords(@NonNull List receivedHistory); } diff --git a/p2p-sync/src/main/java/org/smartregister/p2p/sync/ISenderSyncLifecycleCallback.java b/p2p-sync/src/main/java/org/smartregister/p2p/sync/ISenderSyncLifecycleCallback.java index 7aa6a96..c61ee21 100644 --- a/p2p-sync/src/main/java/org/smartregister/p2p/sync/ISenderSyncLifecycleCallback.java +++ b/p2p-sync/src/main/java/org/smartregister/p2p/sync/ISenderSyncLifecycleCallback.java @@ -4,6 +4,7 @@ import android.support.annotation.Nullable; import com.google.android.gms.nearby.connection.DiscoveredEndpointInfo; +import com.google.android.gms.nearby.connection.Payload; import com.google.android.gms.nearby.connection.PayloadTransferUpdate; @@ -24,4 +25,6 @@ public interface ISenderSyncLifecycleCallback extends SyncLifecycleCallback { void onRequestConnectionSuccessful(@Nullable Object result); void onRequestConnectionFailed(@NonNull Exception exception); + + void processReceivedHistory(@NonNull Payload payload); } diff --git a/p2p-sync/src/main/java/org/smartregister/p2p/tasks/GenericAsyncTask.java b/p2p-sync/src/main/java/org/smartregister/p2p/tasks/GenericAsyncTask.java index 73ad487..0a1455e 100644 --- a/p2p-sync/src/main/java/org/smartregister/p2p/tasks/GenericAsyncTask.java +++ b/p2p-sync/src/main/java/org/smartregister/p2p/tasks/GenericAsyncTask.java @@ -62,7 +62,7 @@ public void setOnFinishedCallback(@Nullable OnFinishedCallback onFinishedCall public interface OnFinishedCallback { - void onSuccess(@NonNull T result); + void onSuccess(@Nullable T result); void onError(Exception e); }