diff --git a/p2p-sync/src/main/java/org/smartregister/p2p/presenter/BaseP2pModeSelectPresenter.java b/p2p-sync/src/main/java/org/smartregister/p2p/presenter/BaseP2pModeSelectPresenter.java index ed9c733..be7f7e6 100644 --- a/p2p-sync/src/main/java/org/smartregister/p2p/presenter/BaseP2pModeSelectPresenter.java +++ b/p2p-sync/src/main/java/org/smartregister/p2p/presenter/BaseP2pModeSelectPresenter.java @@ -1,6 +1,9 @@ package org.smartregister.p2p.presenter; +import android.app.Activity; +import android.content.Context; import android.support.annotation.NonNull; +import android.view.WindowManager; import org.smartregister.p2p.P2PLibrary; import org.smartregister.p2p.contract.P2pModeSelectContract; @@ -9,6 +12,8 @@ import java.util.HashMap; import java.util.HashSet; +import timber.log.Timber; + /** * Created by Ephraim Kigamba - ekigamba@ona.io on 08/03/2019 */ @@ -21,6 +26,8 @@ public abstract class BaseP2pModeSelectPresenter implements P2pModeSelectContrac protected HashMap rejectedDevices = new HashMap<>(); protected HashSet blacklistedDevices = new HashSet<>(); + private int keepScreenOnCounter; + public BaseP2pModeSelectPresenter(@NonNull P2pModeSelectContract.View view) { this(view, new P2pModeSelectInteractor(view.getContext())); } @@ -97,4 +104,31 @@ public void rejectDeviceOnAuthentication(@NonNull String endpointId) { addDeviceToRejectedList(endpointId); } } + + /** + * Enables or disables the keep screen on flag to avoid the device going to sleep while there + * is a sync happening + * + * @param enable {@code TRUE} to enable or {@code FALSE} disable + */ + protected void keepScreenOn(boolean enable) { + Context context = getView().getContext(); + if (context instanceof Activity) { + if (enable) { + keepScreenOnCounter++; + + if (keepScreenOnCounter == 1) { + ((Activity) context).getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); + } + } else { + keepScreenOnCounter--; + + if (keepScreenOnCounter == 0) { + ((Activity) context).getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); + } + } + } else { + Timber.e("Could not %s KEEP_SCREEN_ON because the view-context is not an activity", (enable ? "enable" : "disable")); + } + } } 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 20df8cc..aaae621 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 @@ -106,9 +106,11 @@ public void handlePermissionResult(@NonNull String[] permissions, @NonNull int[] public void startAdvertisingMode() { if (!interactor.isAdvertising()) { view.enableSendReceiveButtons(false); + keepScreenOn(true); view.showReceiveProgressDialog(new P2pModeSelectContract.View.DialogCancelCallback() { @Override public void onCancelClicked(DialogInterface dialogInterface) { + keepScreenOn(false); interactor.stopAdvertising(); dialogInterface.dismiss(); view.enableSendReceiveButtons(true); @@ -144,9 +146,10 @@ public void onConnectionInitiated(@NonNull String endpointId, @NonNull Connectio // Reject when already connected or the connecting device is blacklisted if (currentSender == null && !blacklistedDevices.contains(endpointId)) { - currentSender = new DiscoveredDevice(endpointId, connectionInfo); + setCurrentDevice(new DiscoveredDevice(endpointId, connectionInfo)); // First stop advertising + keepScreenOn(false); interactor.stopAdvertising(); view.removeReceiveProgressDialog(); @@ -527,7 +530,7 @@ private void resetState() { connectionLevel = null; view.dismissAllDialogs(); view.enableSendReceiveButtons(true); - currentSender = null; + setCurrentDevice(null); currentSendingDevice = null; } @@ -567,5 +570,6 @@ public DiscoveredDevice getCurrentPeerDevice() { @Override public void setCurrentDevice(@Nullable DiscoveredDevice discoveredDevice) { currentSender = discoveredDevice; + keepScreenOn(discoveredDevice != null); } } 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 a2b2efe..04e5b0a 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 @@ -114,9 +114,11 @@ public void handlePermissionResult(@NonNull String[] permissions, @NonNull int[] public void startDiscoveringMode() { if (!interactor.isDiscovering()) { view.enableSendReceiveButtons(false); + keepScreenOn(true); view.showDiscoveringProgressDialog (new P2pModeSelectContract.View.DialogCancelCallback() { @Override public void onCancelClicked(DialogInterface dialogInterface) { + keepScreenOn(false); interactor.stopDiscovering(); dialogInterface.dismiss(); view.enableSendReceiveButtons(true); @@ -184,9 +186,10 @@ public void onDeviceFound(@NonNull final String endpointId, @NonNull final Disco // Reject when already connected or the connecting device is blacklisted if (currentReceiver == null && !blacklistedDevices.contains(endpointId)) { - currentReceiver = new DiscoveredDevice(endpointId, discoveredEndpointInfo); + setCurrentDevice(new DiscoveredDevice(endpointId, discoveredEndpointInfo)); // First stop discovering + keepScreenOn(false); interactor.stopDiscovering(); view.removeDiscoveringProgressDialog(); @@ -492,7 +495,7 @@ public void onPayloadReceived(@NonNull String endpointId, @NonNull Payload paylo private void resetState() { connectionLevel = null; view.dismissAllDialogs(); - currentReceiver = null; + setCurrentDevice(null); view.enableSendReceiveButtons(true); } @@ -544,6 +547,7 @@ public DiscoveredDevice getCurrentPeerDevice() { @Override public void setCurrentDevice(@Nullable DiscoveredDevice discoveredDevice) { currentReceiver = discoveredDevice; + keepScreenOn(discoveredDevice != null); } @Override diff --git a/p2p-sync/src/test/java/org/smartregister/p2p/sync/SyncSenderHandlerTest.java b/p2p-sync/src/test/java/org/smartregister/p2p/sync/SyncSenderHandlerTest.java index 4f4414c..8417887 100644 --- a/p2p-sync/src/test/java/org/smartregister/p2p/sync/SyncSenderHandlerTest.java +++ b/p2p-sync/src/test/java/org/smartregister/p2p/sync/SyncSenderHandlerTest.java @@ -16,7 +16,6 @@ import org.robolectric.RobolectricTestRunner; import org.robolectric.RuntimeEnvironment; import org.robolectric.annotation.Config; -import org.robolectric.shadows.ShadowApplication; import org.robolectric.util.ReflectionHelpers; import org.smartregister.p2p.P2PLibrary; import org.smartregister.p2p.authorizer.P2PAuthorizationService;