Skip to content

Commit

Permalink
Retrieve the device received history and send it to the sender
Browse files Browse the repository at this point in the history
- Change the current connection level of the app after authorization
and after negotiating the start of the data exchange

Fixes #8
  • Loading branch information
ekigamba committed Apr 1, 2019
1 parent 2aca35b commit d8835e3
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<List<P2pReceivedHistory>>() {
@Override
public List<P2pReceivedHistory> call() throws Exception {
return P2PLibrary.getInstance().getDb()
.p2pReceivedHistoryDao()
.getDeviceReceivedHistory(sendingDevice.getDeviceId());
}
}, new GenericAsyncTask.OnFinishedCallback<List<P2pReceivedHistory>>() {
@Override
public void onSuccess(@Nullable List<P2pReceivedHistory> 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
Expand All @@ -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<P2pReceivedHistory>());
}

@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);
}
});

Expand All @@ -277,13 +302,12 @@ public void onError(Exception e) {
@Override
public Void call() throws Exception {
registerSendingDevice(finalBasicDeviceDetails);

return null;
}
}, new GenericAsyncTask.OnFinishedCallback<Void>() {
@Override
public void onSuccess(@Nullable Void result) {
// Todo: get the records sent last time and continue with the process
sendLastReceivedRecords(new ArrayList<P2pReceivedHistory>());
}

@Override
Expand Down Expand Up @@ -311,6 +335,14 @@ public void onError(Exception e) {
}
}

@Override
public void sendLastReceivedRecords(@NonNull List<P2pReceivedHistory> receivedHistory) {
if (currentSender != null) {
interactor.sendMessage(new Gson().toJson(receivedHistory));
connectionLevel = ConnectionLevel.SENT_RECEIVED_HISTORY;
}
}

private void registerSendingDevice(Map<String, Object> basicDeviceDetails) {
SendingDevice sendingDevice = new SendingDevice();
sendingDevice.setDeviceId((String) basicDeviceDetails.get(Constants.BasicDeviceDetails.KEY_DEVICE_ID));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 - [email protected] on 15/03/2019
*/
Expand All @@ -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<P2pReceivedHistory> receivedHistory);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand All @@ -24,4 +25,6 @@ public interface ISenderSyncLifecycleCallback extends SyncLifecycleCallback {
void onRequestConnectionSuccessful(@Nullable Object result);

void onRequestConnectionFailed(@NonNull Exception exception);

void processReceivedHistory(@NonNull Payload payload);
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void setOnFinishedCallback(@Nullable OnFinishedCallback<T> onFinishedCall

public interface OnFinishedCallback<T> {

void onSuccess(@NonNull T result);
void onSuccess(@Nullable T result);

void onError(Exception e);
}
Expand Down

0 comments on commit d8835e3

Please sign in to comment.