Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
.buildlog/
.history
.svn/
.metadata
.metadata
CLAUDE.md
3 changes: 3 additions & 0 deletions wakelock_plus/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## [1.6.2]
* Android: Fixed `NoActivityException` ("wakelock requires a foreground activity") being thrown when `toggle`/`enabled` were called with no foreground activity attached (e.g. while the app is backgrounded or during a lifecycle transition). The requested wakelock state is now remembered and re-applied once an activity (re)attaches instead of throwing.

## [1.6.1]
* [#133](https://github.com/fluttercommunity/wakelock_plus/pull/133): wakelock_plus Flutter 3.38 downgrade. Thanks [diegotori](https://github.com/diegotori).
- Library now requires Dart version `3.10` or higher, restoring previous compatibility.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,35 @@ import android.app.Activity
import android.view.WindowManager

internal class Wakelock {
var activity: Activity? = null

private val enabled
get() = activity!!.window.attributes.flags and
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON != 0
// The desired wakelock state. Tracked independently of [activity] so that a
// toggle requested while no activity is attached (e.g. the app is in the
// background or mid lifecycle transition) is remembered and re-applied once an
// activity (re)attaches, instead of throwing a NoActivityException.
private var enableWakelock = false

fun toggle(message: ToggleMessage) {
if (activity == null) {
throw NoActivityException()
var activity: Activity? = null
set(value) {
field = value
// Re-assert the wakelock on the newly attached activity's window, but only
// when it was actually requested. If the user never enabled it (or last
// disabled it), leave the flag alone — the activity may keep the screen on
// for its own reasons.
if (enableWakelock) applyWakelock()
}

val activity = this.activity!!
val enabled = this.enabled

if (message.enable!!) {
if (!enabled) activity.window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
} else if (enabled) {
activity.window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
private fun applyWakelock() {
val window = activity?.window ?: return
if (enableWakelock) {
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
} else {
window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
}
}

fun isEnabled(): IsEnabledMessage {
if (activity == null) {
throw NoActivityException()
}

return IsEnabledMessage(enabled = enabled)
fun toggle(message: ToggleMessage) {
enableWakelock = message.enable!!
applyWakelock()
}
}

class NoActivityException : Exception("wakelock requires a foreground activity")
fun isEnabled(): IsEnabledMessage = IsEnabledMessage(enabled = enableWakelock)
}
2 changes: 1 addition & 1 deletion wakelock_plus/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: wakelock_plus
description: >-2
Plugin that allows you to keep the device screen awake, i.e. prevent the screen from sleeping on
Android, iOS, macOS, Windows, Linux, and web.
version: 1.6.1
version: 1.6.2
repository: https://github.com/fluttercommunity/wakelock_plus/tree/main/wakelock_plus

environment:
Expand Down