-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Keep screen on during discovering,advertising and syncing
- Loading branch information
Showing
4 changed files
with
46 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
@@ -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 | ||
*/ | ||
|
@@ -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())); | ||
} | ||
|
@@ -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")); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters