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
60 changes: 60 additions & 0 deletions .github/scripts/align_android_beta_toolchain.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/usr/bin/env python3
"""Align beta-only Android CI with the selected Flutter SDK's Android templates.

Stable Flutter jobs keep the repository's checked-in Android configuration.
Flutter beta can move its minimum AGP/Gradle versions ahead of stable releases.
"""
import os
import re
from pathlib import Path

ROOT = Path(__file__).resolve().parents[2]
sdk_root = os.environ.get("FLUTTER_ROOT")
if not sdk_root:
raise SystemExit("FLUTTER_ROOT is required after setting up Flutter beta")
defaults_path = Path(sdk_root) / "packages/flutter_tools/lib/src/android/gradle_utils.dart"
defaults = defaults_path.read_text(encoding="utf-8")


def sdk_version(constant: str) -> str:
match = re.search(rf"^const {re.escape(constant)} = '([0-9.]+)';$", defaults, re.MULTILINE)
if not match:
raise RuntimeError(f"Cannot determine Flutter SDK {constant}")
return match.group(1)


def rewrite_one(path: Path, pattern: str, replacement: str) -> None:
source = path.read_text(encoding="utf-8")
updated, changes = re.subn(pattern, replacement, source, count=1, flags=re.MULTILINE)
if changes != 1:
raise RuntimeError(f"Expected one match for {pattern!r} in {path}")
path.write_text(updated, encoding="utf-8")


agp = sdk_version("templateAndroidGradlePluginVersion")
gradle = sdk_version("templateDefaultGradleVersion")
kotlin = sdk_version("templateKotlinGradlePluginVersion")
android_root = ROOT / "wakelock_plus/example/android"

rewrite_one(
android_root / "settings.gradle",
r'(id "com\.android\.application" version ")[^"]+(" apply false)',
rf'\g<1>{agp}\g<2>',
)
rewrite_one(
android_root / "settings.gradle",
r'(id "org\.jetbrains\.kotlin\.android" version ")[^"]+(" apply false)',
rf'\g<1>{kotlin}\g<2>',
)
rewrite_one(
android_root / "gradle/wrapper/gradle-wrapper.properties",
r'(distributionUrl=https\\://services\.gradle\.org/distributions/gradle-)[0-9.]+(-bin\.zip)',
rf'\g<1>{gradle}\g<2>',
)
rewrite_one(
android_root / "gradle.properties",
r'^android\.builtInKotlin=false$',
"android.builtInKotlin=true",
)
# Keep android.newDsl=false to preserve legacy example app DSL compatibility.
print(f"Flutter beta CI: AGP {agp}, Gradle {gradle}, Kotlin {kotlin}")
22 changes: 22 additions & 0 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ jobs:
strategy:
matrix:
flutter_version: ${{ fromJson(needs.setup_matrix.outputs.flutter_versions_json) }}
fail-fast: false
name: Android Example Build (${{ matrix.flutter_version }})
steps:
- uses: actions/checkout@v6
Expand All @@ -134,6 +135,9 @@ jobs:
channel: ${{ (!contains(matrix.flutter_version, '.') && !contains(matrix.flutter_version, 'x')) && matrix.flutter_version || '' }}
cache: true
cache-key: flutter-:os:-:channel:-:version:-:arch:-:hash:-${{ hashFiles('**/pubspec.lock') }}
- name: Match Android tooling to Flutter beta
if: matrix.flutter_version == 'beta'
run: python3 .github/scripts/align_android_beta_toolchain.py
- name: Build Android APK
run: |
cd wakelock_plus/example
Expand Down Expand Up @@ -170,6 +174,9 @@ jobs:
cache: true
cache-key: flutter-:os:-:channel:-:version:-:arch:-:hash:-${{ hashFiles('**/pubspec.lock') }}

- name: Match Android tooling to Flutter beta
if: matrix.flutter_version == 'beta'
run: python3 .github/scripts/align_android_beta_toolchain.py
- name: Enable KVM
# Linux runners require KVM to run Android emulators with hardware acceleration
run: |
Expand Down Expand Up @@ -450,6 +457,21 @@ jobs:
flutter pub get
flutter drive -d windows --driver=test_driver/integration_test.dart --target=integration_test/wakelock_plus_test.dart

# Run the browser API mocks without requiring a real browser or Flutter SDK.
web_js_regression_tests:
runs-on: ubuntu-latest
timeout-minutes: 5
name: Web JavaScript Regression Tests
steps:
- uses: actions/checkout@v6
- uses: actions/setup-node@v4
with:
node-version: '22'
- name: Validate JavaScript syntax
run: node --check wakelock_plus/lib/assets/no_sleep.js
- name: Run web concurrency regression tests
run: node --test wakelock_plus/test/no_sleep_concurrency.test.cjs

# --- WEB ---

web_example_build:
Expand Down
206 changes: 114 additions & 92 deletions wakelock_plus/lib/assets/no_sleep.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,5 @@
/*! Based On NoSleep.js v0.12.0 - git.io/vfn01 - Rich Tibbett - MIT license */

class PromiseCompleter {
_promise;
_resolve;
_reject;
constructor() {
this._promise = new Promise((resolve, reject) => {
this._resolve = resolve;
this._reject = reject;
});
}

isCompleted = false;

get future() {
return this._promise;
}

complete(value) {
this.isCompleted = true;
this._resolve(value);
}

completeError(error) {
this._reject(error);
}
}

var webm =
'data:video/webm;base64,GkXfo0AgQoaBAUL3gQFC8oEEQvOBCEKCQAR3ZWJtQoeBAkKFgQIYU4BnQI0VSalmQCgq17FAAw9CQE2AQAZ3aGFtbXlXQUAGd2hhbW15RIlACECPQAAAAAAAFlSua0AxrkAu14EBY8WBAZyBACK1nEADdW5khkAFVl9WUDglhohAA1ZQOIOBAeBABrCBCLqBCB9DtnVAIueBAKNAHIEAAIAwAQCdASoIAAgAAUAmJaQAA3AA/vz0AAA='
var mp4 =
Expand Down Expand Up @@ -59,8 +32,6 @@ function _classCallCheck(instance, Constructor) {
var nativeWakeLock = 'wakeLock' in navigator

var NoSleep = (function () {
var _nativeEnabledCompleter;
var _playVideoCompleter;

function NoSleep() {
var _this = this
Expand All @@ -70,6 +41,9 @@ var NoSleep = (function () {
this.nativeEnabled = false
if (nativeWakeLock) {
this._wakeLock = null
this._nativeOperation = Promise.resolve()
this._nativePendingEnable = null
this._requestEnabling = false
var handleVisibilityChange = function handleVisibilityChange() {
if (_this._wakeLock !== null && document.visibilityState === 'visible') {
_this.enable()
Expand All @@ -86,6 +60,9 @@ var NoSleep = (function () {

this._addSourceToVideo(this.noSleepVideo, 'webm', webm)
this._addSourceToVideo(this.noSleepVideo, 'mp4', mp4)
this._videoOperation = null
this._videoPendingEnable = null
this._requestEnabling = false

this.noSleepVideo.addEventListener('loadedmetadata', function () {
if (_this.noSleepVideo.duration <= 1) {
Expand Down Expand Up @@ -116,73 +93,126 @@ var NoSleep = (function () {
{
key: 'enable',
value: async function enable() {
var _this2 = this

if (nativeWakeLock) {
// Disable any previously held wakelocks.
await this.disable()
if (_nativeEnabledCompleter == null) {
_nativeEnabledCompleter = new PromiseCompleter()
this._requestEnabling = true
// Share in-flight requests and serialize state changes.
if (this._nativePendingEnable != null) {
return this._nativePendingEnable
}
navigator.wakeLock
.request('screen')
.then(function (wakeLock) {
_this2._wakeLock = wakeLock
_this2.nativeEnabled = true

// We now have a wakelock. Notify all of the existing callers.
_this2._wakeLock.addEventListener('release', function () {
_this2.nativeEnabled = false
_this2._wakeLock = null
})

_nativeEnabledCompleter.complete()
_nativeEnabledCompleter = null
})
.catch(function (err) {
_this2.nativeEnabled = false
var errorMessage = err.name + ', ' + err.message
_nativeEnabledCompleter.completeError(errorMessage)
_nativeEnabledCompleter = null
const operation = this._nativeOperation.then(async () => {
if (!this._requestEnabling || this._wakeLock != null) {
return
}
const wakeLock = await navigator.wakeLock.request('screen')
wakeLock.addEventListener('release', () => {
// A late event from a previous lock must not clear the current one.
if (this._wakeLock === wakeLock) {
this._wakeLock = null
this.nativeEnabled = false
}
})
// We then wait for screen to be made available.
return _nativeEnabledCompleter.future
if (!this._requestEnabling) {
await wakeLock.release()
return
}
this._wakeLock = wakeLock
this.nativeEnabled = true
})
this._nativePendingEnable = operation
// Keep the queue usable after a rejection, but reject the callers.
this._nativeOperation = operation.catch(() => {})
void operation.then(
() => {
if (this._nativePendingEnable === operation) {
this._nativePendingEnable = null
}
},
() => {
if (this._nativePendingEnable === operation) {
this._nativePendingEnable = null
}
},
)
return operation
} else {
if (_playVideoCompleter == null) {
_playVideoCompleter = new PromiseCompleter()
this._requestEnabling = true
if (this._videoPendingEnable != null) {
return this._videoPendingEnable
}
const play = async () => {
if (!this._requestEnabling || !this.noSleepVideo.paused) {
return
}
// Preserve user activation by calling play synchronously when idle.
await this.noSleepVideo.play()
if (!this._requestEnabling) {
this.noSleepVideo.pause()
}
}
var playPromise = this.noSleepVideo.play()
playPromise.then(function (res) {
_playVideoCompleter.complete()
_playVideoCompleter = null
}).catch(function (err) {
var errorMessage = err.name + ', ' + err.message
_playVideoCompleter.completeError(errorMessage)
_playVideoCompleter = null
});
return _playVideoCompleter.future
const operation = this._videoOperation == null
? play()
: this._videoOperation.then(play)
this._videoPendingEnable = operation
const queue = operation.catch(() => {})
this._videoOperation = queue
void queue.then(() => {
if (this._videoOperation === queue) {
this._videoOperation = null
}
})
void operation.then(
() => {
if (this._videoPendingEnable === operation) {
this._videoPendingEnable = null
}
},
() => {
if (this._videoPendingEnable === operation) {
this._videoPendingEnable = null
}
},
)
return operation
}
},
},
{
key: 'disable',
value: async function disable() {
if (nativeWakeLock) {
// If we're still trying to enable the wakelock, wait for it to be enabled
if (_nativeEnabledCompleter != null) {
await _nativeEnabledCompleter.future
}
if (this._wakeLock != null) {
this._requestEnabling = false
// Do not let a later enable inherit an obsolete pending request.
this._nativePendingEnable = null
const operation = this._nativeOperation.then(async () => {
if (this._requestEnabling || this._wakeLock == null) {
return
}
const wakeLock = this._wakeLock
this._wakeLock = null
this.nativeEnabled = false
this._wakeLock.release()
}

this._wakeLock = null
await wakeLock.release()
})
this._nativeOperation = operation.catch(() => {})
return operation
} else {
if (_playVideoCompleter != null) {
await _playVideoCompleter.future
this._requestEnabling = false
this._videoPendingEnable = null
const pause = () => {
if (!this._requestEnabling) {
this.noSleepVideo.pause()
}
}
this.noSleepVideo.pause()
const operation = this._videoOperation == null
? Promise.resolve(pause())
: this._videoOperation.then(pause)
const queue = operation.catch(() => {})
this._videoOperation = queue
void queue.then(() => {
if (this._videoOperation === queue) {
this._videoOperation = null
}
})
return operation
}
return Promise.resolve();
},
Expand All @@ -191,20 +221,12 @@ var NoSleep = (function () {
key: 'isEnabled',
value: async function isEnabled() {
if (nativeWakeLock) {
// If we're still trying to enable the wakelock, wait for it to be enabled
if (_nativeEnabledCompleter != null) {
await _nativeEnabledCompleter.future
}

await this._nativeOperation
return this.nativeEnabled
} else {
if (_playVideoCompleter != null) {
await _playVideoCompleter.future
if (this._videoOperation != null) {
await this._videoOperation
}
if (this.noSleepVideo == undefined) {
return false
}

return !this.noSleepVideo.paused
}
},
Expand Down
Loading