From 438cf8c62029a5bf451d16441c6c166120c1b367 Mon Sep 17 00:00:00 2001 From: Alexander Sosedkin Date: Sat, 6 Jul 2024 17:38:40 +0200 Subject: [PATCH] modules/android-integration: termux-wake-lock and termux-wake-unlock --- CHANGELOG.md | 4 +- modules/environment/android-integration.nix | 23 ++++++++ pkgs/android-integration/termux-tools.nix | 18 +++++-- tests/emulator/android_integration.py | 53 ++++++++++++++++++- .../on-device/config-android-integration.bats | 6 +++ .../on-device/config-android-integration.nix | 2 + 6 files changed, 100 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 847fe978..a0bcff2d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,9 @@ * New options under `android-integration`, offer some of the tools familiar to Termux users: - `am`, `termux-open`, `termux-open-url`, `termux-setup-storage` and `xdg-open`. + `am`, `termux-open`, `termux-open-url`, + `termux-setup-storage`, `termux-wake-lock`, `termux-wake-unlock` + and `xdg-open`. ### Compatibility considerations diff --git a/modules/environment/android-integration.nix b/modules/environment/android-integration.nix index 5688cb97..36ba05c9 100644 --- a/modules/environment/android-integration.nix +++ b/modules/environment/android-integration.nix @@ -62,6 +62,27 @@ in ''; }; + termux-wake-lock.enable = lib.mkOption { + type = lib.types.bool; + default = false; + example = "true"; + description = lib.mdDoc '' + Provide a `termux-wake-lock` command + that tones down Android power saving measures. + This is the same action that's available from the notification. + ''; + }; + + termux-wake-unlock.enable = lib.mkOption { + type = lib.types.bool; + default = false; + example = "true"; + description = lib.mdDoc '' + Provide a `termux-wake-unlock` command + that undoes the effect of the `termux-wake-lock` one. + ''; + }; + xdg-open.enable = lib.mkOption { type = lib.types.bool; default = false; @@ -91,6 +112,8 @@ in (ifD cfg.termux-setup-storage.enable termux-tools.setup_storage) ++ (ifD cfg.termux-open.enable termux-tools.open) ++ (ifD cfg.termux-open-url.enable termux-tools.open_url) ++ + (ifD cfg.termux-wake-lock.enable termux-tools.wake_lock) ++ + (ifD cfg.termux-wake-unlock.enable termux-tools.wake_unlock) ++ (ifD cfg.xdg-open.enable termux-tools.xdg_open) ++ (ifD cfg.unsupported.enable termux-tools.out); }; diff --git a/pkgs/android-integration/termux-tools.nix b/pkgs/android-integration/termux-tools.nix index 980d4352..ead83fa5 100644 --- a/pkgs/android-integration/termux-tools.nix +++ b/pkgs/android-integration/termux-tools.nix @@ -28,9 +28,13 @@ stdenvNoCC.mkDerivation rec { --replace @TERMUX_HOME@ /data/data/com.termux.nix/files/home/ \ --replace @TERMUX_APP_PACKAGE@ com.termux.nix substituteInPlace scripts/termux-open.in \ - --replace @TERMUX_APP_PACKAGE@.app com.termux.app \ - --replace @TERMUX_APP_PACKAGE@ com.termux.nix \ --replace 'getopt ' '${getopt}/bin/getopt ' + substituteInPlace \ + scripts/termux-open.in \ + scripts/termux-wake-lock.in \ + scripts/termux-wake-unlock.in \ + --replace @TERMUX_APP_PACKAGE@.app com.termux.app \ + --replace @TERMUX_APP_PACKAGE@ com.termux.nix ${gnused}/bin/sed -i 's|^am |${termux-am}/bin/am |' scripts/* rm -r doc # manpage is half misleading, pulling pandoc is not worth it @@ -49,6 +53,8 @@ stdenvNoCC.mkDerivation rec { "setup_storage" # termux-setup-storage "open" # termux-open "open_url" # termux-open-url + "wake_lock" # termux-wake-lock + "wake_unlock" # termux-wake-unlock "xdg_open" # xdg-open ]; postInstall = '' @@ -88,6 +94,12 @@ stdenvNoCC.mkDerivation rec { mkdir -p $open_url/bin mv $out/bin/termux-open-url $open_url/bin/ + mkdir -p $wake_lock/bin + mv $out/bin/termux-wake-lock $wake_lock/bin/ + + mkdir -p $wake_unlock/bin + mv $out/bin/termux-wake-unlock $wake_unlock/bin/ + mkdir -p $xdg_open/bin rm $out/bin/xdg-open ln -s $open/bin/termux-open $xdg_open/bin/xdg-open @@ -98,8 +110,6 @@ stdenvNoCC.mkDerivation rec { echo ./bin >> expected echo ./bin/termux-backup >> expected # entirely untested echo ./bin/termux-reload-settings >> expected # good candidate for fixing - echo ./bin/termux-wake-lock >> expected # good candidate for fixing - echo ./bin/termux-wake-unlock >> expected # good candidate for fixing echo ./share >> expected echo ./share/examples >> expected echo ./share/examples/termux >> expected diff --git a/tests/emulator/android_integration.py b/tests/emulator/android_integration.py index 456f79d1..b301c6f3 100644 --- a/tests/emulator/android_integration.py +++ b/tests/emulator/android_integration.py @@ -7,7 +7,8 @@ def run(d): OPENERS = ['termux-open', 'termux-open-url', 'xdg-open'] - TOOLS = ['am', 'termux-setup-storage'] + OPENERS + TOOLS = ['am', 'termux-setup-storage', + 'termux-wake-lock', 'termux-wake-unlock'] + OPENERS nod = bootstrap_channels.run(d) @@ -111,3 +112,53 @@ def run(d): d.ui.press('back') screenshot(d, f'{opener}-back') wait_for(d, f'{opener} https://example.org') + + # test termux-wake-lock/termux-wake-unlock + d.ui.open_notification() + screenshot(d, 'notification-opened') + d.ui(text='Nix').right(resourceId='android:id/expand_button').click() + screenshot(d, 'notification-expanded') + wait_for(d, 'Acquire wakelock') + screenshot(d, 'wakelock-initially-not-acquired') + d.ui.press('back') + + d('input text "termux-wake-lock"') + d.ui.press('enter') + time.sleep(3) + screenshot(d, 'wake-lock-command') + if 'Let app always run in background?' in d.ui.dump_hierarchy(): + screenshot(d, 'wake-lock-permission-asked') + if 'text="Allow"' in d.ui.dump_hierarchy(): + d.ui(text='Allow').click() + elif 'text="ALLOW"' in d.ui.dump_hierarchy(): + d.ui(text='ALLOW').click() + screenshot(d, 'wake-lock-permission-granted') + d.ui.open_notification() + time.sleep(.5) + screenshot(d, 'notification-opened') + wait_for(d, '(wake lock held)') + if 'Release wakelock' not in d.ui.dump_hierarchy(): + d.ui(text='Nix').right(resourceId='android:id/expand_button').click() + screenshot(d, 'notification-expanded') + wait_for(d, 'Release wakelock') + screenshot(d, 'notification-with-wakelock') + d.ui.press('back') + screenshot(d, 'back') + wait_for(d, 'termux-wake-lock') + screenshot(d, 'really-back') + + d('input text "termux-wake-unlock"') + d.ui.press('enter') + screenshot(d, 'wake-unlock-command') + d.ui.open_notification() + time.sleep(.5) + screenshot(d, 'notification-opened') + if 'Acquire wakelock' not in d.ui.dump_hierarchy(): + d.ui(text='Nix').right(resourceId='android:id/expand_button').click() + screenshot(d, 'notification-expanded') + wait_for(d, 'Acquire wakelock') + screenshot(d, 'notification-without-wakelock') + d.ui.press('back') + screenshot(d, 'back') + wait_for(d, 'termux-wake-unlock') + screenshot(d, 'really-back') diff --git a/tests/on-device/config-android-integration.bats b/tests/on-device/config-android-integration.bats index 9e2cefb5..85b51465 100644 --- a/tests/on-device/config-android-integration.bats +++ b/tests/on-device/config-android-integration.bats @@ -8,6 +8,8 @@ load lib run ! command -v termux-setup-storage run ! command -v termux-open run ! command -v termux-open-url + run ! command -v termux-wake-lock + run ! command -v termux-wake-unlock run ! command -v xdg-open run ! command -v termux-backup @@ -20,6 +22,8 @@ load lib command -v termux-setup-storage command -v termux-open command -v termux-open-url + command -v termux-wake-lock + command -v termux-wake-unlock command -v xdg-open run ! command -v termux-backup @@ -32,6 +36,8 @@ load lib command -v termux-setup-storage command -v termux-open command -v termux-open-url + command -v termux-wake-lock + command -v termux-wake-unlock command -v xdg-open command -v termux-backup diff --git a/tests/on-device/config-android-integration.nix b/tests/on-device/config-android-integration.nix index 3115e78b..70a33337 100644 --- a/tests/on-device/config-android-integration.nix +++ b/tests/on-device/config-android-integration.nix @@ -7,6 +7,8 @@ _: termux-open.enable = true; termux-open-url.enable = true; termux-setup-storage.enable = true; + termux-wake-lock.enable = true; + termux-wake-unlock.enable = true; xdg-open.enable = true; # unsupported.enable = false; };