Skip to content

Commit

Permalink
modules/android-integration: add am command
Browse files Browse the repository at this point in the history
  • Loading branch information
t184256 committed Jul 5, 2024
1 parent 45fcd2d commit 7a72fd9
Show file tree
Hide file tree
Showing 10 changed files with 157 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/emulator.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ jobs:
# below 28: bootstrap didn't start, IDK why
# 34: sometimes work, but doesn't seem stable, even w/o caching images
script:
- android_integration
- bootstrap_flakes
- bootstrap_channels
- poke_around
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

## Release 24.05 (unreleased)

### New Options

* New options under `android-integration`,
offer some of the tools familiar to Termux users.
Currently it's just `am`.

### Compatibility considerations

* `nixOnDroidConfigurations` `pkgs` argument is now mandatory.
Expand Down
36 changes: 36 additions & 0 deletions modules/environment/android-integration.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright (c) 2019-2024, see AUTHORS. Licensed under MIT License, see LICENSE.

{ config, lib, pkgs, ... }:

let
cfg = config.android-integration;

termux-am =
pkgs.callPackage (import ../../pkgs/android-integration/termux-am.nix) { };
in
{

###### interface

options.android-integration = {

am.enable = lib.mkOption {
type = lib.types.bool;
default = false;
example = "true";
description = lib.mdDoc ''
Provide an `am` (activity manager) command.
Is not guaranteed to be a real deal, could be of limited compatibility
with real `am` (like `termux-am`).
'';
};

};

###### implementation

config = {
environment.packages =
lib.mkIf cfg.am.enable [ termux-am ];
};
}
1 change: 1 addition & 0 deletions modules/module-list.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
[
./build/activation.nix
./build/config.nix
./environment/android-integration.nix
./environment/ca.nix
./environment/etc
./environment/links.nix
Expand Down
32 changes: 32 additions & 0 deletions pkgs/android-integration/termux-am.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright (c) 2019-2024, see AUTHORS. Licensed under MIT License, see LICENSE.

{ stdenv, fetchFromGitHub, cmake }:

let
appPath = "/data/data/com.termux.nix/files/apps/com.termux.nix";
socketPath = "${appPath}/termux-am/am.sock";
in
stdenv.mkDerivation rec {
name = "termux-am";
version = "1.5.0";
src = fetchFromGitHub {
owner = "termux";
repo = "termux-am-socket";
rev = version;
sha256 = "sha256-6pCv2HMBRp8Hi56b43mQqnaFaI7y5DfhS9gScANwg2I=";
};
nativeBuildInputs = [ cmake ];
patchPhase = ''
# Header generation doesn't seem to work on android
echo "#define SOCKET_PATH \"${socketPath}\"" > termux-am.h
cat termux-am.h
# Fix the bash link so that nix can patch it
substituteInPlace termux-am.sh.in --replace @TERMUX_PREFIX@ ""
head termux-am.sh.in
'';
postInstall = ''
# Scripts use 'am' as an alias.
ln -s $out/bin/termux-am $out/bin/am
'';
}
53 changes: 53 additions & 0 deletions tests/emulator/android_integration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import bootstrap_channels

from common import screenshot, wait_for


def run(d):
nod = bootstrap_channels.run(d)

d('input text "am"')
d.ui.press('enter')
wait_for(d, 'bash: am: command not found')
screenshot(d, 'no-am')

# Apply a config that enables am
cfg = ('/data/local/tmp/n-o-d/unpacked/tests/on-device/'
'config-android-integration.nix')
d(f'input text \'cp {cfg} .config/nixpkgs/nix-on-droid.nix\'')
d.ui.press('enter')
screenshot(d, 'pre-switch')
d('input text "nix-on-droid switch"')
d.ui.press('enter')
screenshot(d, 'post-switch')

# Verify am is there
d('input text "am | head -n2"')
d.ui.press('enter')
wait_for(d, 'termux-am is a wrapper script')
screenshot(d, 'am-appears')

# Smoke-test that am doesn't work yet
d('input text "am start -a android.settings.SETTINGS 2>&1 | head -n5"')
d.ui.press('enter')
screenshot(d, 'am-invoked for the first time')
wait_for(d, 'Nix requires "Display over other apps" permission')
wait_for(d, 'https://dontkillmyapp.com')
screenshot(d, 'am-wants-permission')

# Grant nix app 'Draw over other apps' permission
nod.permissions += 'android.permission.SYSTEM_ALERT_WINDOW'

# Smoke-test that am works
d('input text "am start -a android.settings.SETTINGS"')
d.ui.press('enter')
screenshot(d, 'settings-opening')
wait_for(d, 'Search settings')
wait_for(d, 'Network')
d.ui.press('back')
screenshot(d, 'back-from-settings')

# Verify we're back
d('input text "am | head -n2"')
d.ui.press('enter')
wait_for(d, 'termux-am is a wrapper script')
2 changes: 2 additions & 0 deletions tests/emulator/bootstrap_channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,5 @@ def run(d):
wait_for(d, 'c21va2UtdGVzdAo=')

screenshot(d, 'success-bootstrap-channels')

return nod
2 changes: 2 additions & 0 deletions tests/emulator/bootstrap_flakes.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,5 @@ def run(d):
wait_for(d, 'c21va2UtdGVzdAo=')

screenshot(d, 'success-bootstrap-flakes')

return nod
16 changes: 16 additions & 0 deletions tests/on-device/config-android-integration.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright (c) 2019-2024, see AUTHORS. Licensed under MIT License, see LICENSE.

load lib

@test 'android-integration options can be used' {
run ! command -v am

cp \
"$ON_DEVICE_TESTS_DIR/config-android-integration.nix" \
~/.config/nixpkgs/nix-on-droid.nix
nix-on-droid switch

command -v am

switch_to_default_config
}
8 changes: 8 additions & 0 deletions tests/on-device/config-android-integration.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
_:

{
system.stateVersion = "23.11";
android-integration = {
am.enable = true;
};
}

0 comments on commit 7a72fd9

Please sign in to comment.