Skip to content

Commit

Permalink
Keep screen on during discovering,advertising and syncing
Browse files Browse the repository at this point in the history
  • Loading branch information
ekigamba committed May 3, 2019
1 parent a07ed5d commit ca4ba36
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -9,6 +12,8 @@
import java.util.HashMap;
import java.util.HashSet;

import timber.log.Timber;

/**
* Created by Ephraim Kigamba - [email protected] on 08/03/2019
*/
Expand All @@ -21,6 +26,8 @@ public abstract class BaseP2pModeSelectPresenter implements P2pModeSelectContrac
protected HashMap<String, Long> rejectedDevices = new HashMap<>();
protected HashSet<String> blacklistedDevices = new HashSet<>();

private int keepScreenOnCounter;

public BaseP2pModeSelectPresenter(@NonNull P2pModeSelectContract.View view) {
this(view, new P2pModeSelectInteractor(view.getContext()));
}
Expand Down Expand Up @@ -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"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -527,7 +530,7 @@ private void resetState() {
connectionLevel = null;
view.dismissAllDialogs();
view.enableSendReceiveButtons(true);
currentSender = null;
setCurrentDevice(null);
currentSendingDevice = null;
}

Expand Down Expand Up @@ -567,5 +570,6 @@ public DiscoveredDevice getCurrentPeerDevice() {
@Override
public void setCurrentDevice(@Nullable DiscoveredDevice discoveredDevice) {
currentSender = discoveredDevice;
keepScreenOn(discoveredDevice != null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();

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

Expand Down Expand Up @@ -544,6 +547,7 @@ public DiscoveredDevice getCurrentPeerDevice() {
@Override
public void setCurrentDevice(@Nullable DiscoveredDevice discoveredDevice) {
currentReceiver = discoveredDevice;
keepScreenOn(discoveredDevice != null);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit ca4ba36

Please sign in to comment.