Skip to content

Commit

Permalink
Add support for enabling hardware keyboard.
Browse files Browse the repository at this point in the history
  • Loading branch information
ychescale9 committed Dec 24, 2021
1 parent a86d743 commit d0e9b3f
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -155,6 +155,7 @@ jobs:
| `disable-animations` | Optional | `true` | Whether to disable animations - `true` or `false`. |
| `disable-spellchecker` | Optional | `false` | Whether to disable spellchecker - `true` or `false`. |
| `disable-linux-hw-accel` | Optional | `true` | Whether to disable hardware acceleration on Linux machines - `true` or `false`. Note that this is true by default as Github-hosted Linux runners do not support hardware acceleration. |
| `enable-hw-keyboard` | Optional | `false` | Whether to enable hardware keyboard - `true` or `false`. |
| `emulator-build` | Optional | N/A | Build number of a specific version of the emulator binary to use e.g. `6061023` for emulator v29.3.0.0. |
| `working-directory` | Optional | `./` | A custom working directory - e.g. `./android` if your root Gradle project is under the `./android` sub-directory within your repository. |
| `ndk` | Optional | N/A | Version of NDK to install - e.g. `21.0.6113669` |
Expand Down
21 changes: 21 additions & 0 deletions __tests__/input-validator.test.ts
Expand Up @@ -227,6 +227,27 @@ describe('disable-linux-hw-accel validator tests', () => {
});
});

describe('enable-hw-keyboard validator tests', () => {
it('Throws if enable-hw-keyboard is not a boolean', () => {
const func = () => {
validator.checkEnableHardwareKeyboard('yes');
};
expect(func).toThrowError(`Input for input.enable-hw-keyboard should be either 'true' or 'false'.`);
});

it('Validates successfully if enable-hardware-keyboard is either true or false', () => {
const func1 = () => {
validator.checkEnableHardwareKeyboard('true');
};
expect(func1).not.toThrow();

const func2 = () => {
validator.checkEnableHardwareKeyboard('false');
};
expect(func2).not.toThrow();
});
});

describe('emulator-build validator tests', () => {
it('Throws if emulator-build is not a number', () => {
const func = () => {
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Expand Up @@ -41,6 +41,9 @@ inputs:
disable-linux-hw-accel:
description: 'whether to disable hardware acceleration on Linux machines - `true` or `false`'
default: 'true'
enable-hw-keyboard:
description: 'whether to enable hardware keyboard - `true` or `false`.'
default: 'false'
emulator-build:
description: 'build number of a specific version of the emulator binary to use - e.g. `6061023` for emulator v29.3.0.0'
working-directory:
Expand Down
9 changes: 7 additions & 2 deletions lib/emulator-manager.js
Expand Up @@ -35,7 +35,7 @@ const EMULATOR_BOOT_TIMEOUT_SECONDS = 600;
/**
* Creates and launches a new AVD instance with the specified configurations.
*/
function launchEmulator(apiLevel, target, arch, profile, cores, ramSize, sdcardPathOrSize, avdName, forceAvdCreation, emulatorOptions, disableAnimations, disableSpellChecker, disableLinuxHardwareAcceleration) {
function launchEmulator(apiLevel, target, arch, profile, cores, ramSize, sdcardPathOrSize, avdName, forceAvdCreation, emulatorOptions, disableAnimations, disableSpellChecker, disableLinuxHardwareAcceleration, enableHardwareKeyboard) {
return __awaiter(this, void 0, void 0, function* () {
// create a new AVD if AVD directory does not already exist or forceAvdCreation is true
const avdPath = `${process.env.ANDROID_AVD_HOME}/${avdName}.avd`;
Expand All @@ -51,6 +51,9 @@ function launchEmulator(apiLevel, target, arch, profile, cores, ramSize, sdcardP
if (ramSize) {
yield exec.exec(`sh -c \\"printf 'hw.ramSize=${ramSize}\n' >> ${process.env.ANDROID_AVD_HOME}/"${avdName}".avd"/config.ini`);
}
if (enableHardwareKeyboard) {
yield exec.exec(`sh -c \\"printf 'hw.keyboard=yes\n' >> ${process.env.ANDROID_AVD_HOME}/"${avdName}".avd"/config.ini`);
}
//turn off hardware acceleration on Linux
if (process.platform === 'linux' && disableLinuxHardwareAcceleration) {
console.log('Disabling Linux hardware acceleration.');
Expand All @@ -70,7 +73,6 @@ function launchEmulator(apiLevel, target, arch, profile, cores, ramSize, sdcardP
// wait for emulator to complete booting
yield waitForDevice();
yield exec.exec(`adb shell input keyevent 82`);
// disable animations
if (disableAnimations) {
console.log('Disabling animations.');
yield exec.exec(`adb shell settings put global window_animation_scale 0.0`);
Expand All @@ -80,6 +82,9 @@ function launchEmulator(apiLevel, target, arch, profile, cores, ramSize, sdcardP
if (disableSpellChecker) {
yield exec.exec(`adb shell settings put secure spell_checker_enabled 0`);
}
if (enableHardwareKeyboard) {
yield exec.exec(`adb shell settings put secure show_ime_with_hard_keyboard 0`);
}
});
}
exports.launchEmulator = launchEmulator;
Expand Down
8 changes: 7 additions & 1 deletion lib/input-validator.js
@@ -1,6 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkEmulatorBuild = exports.checkDisableLinuxHardwareAcceleration = exports.checkDisableSpellchecker = exports.checkDisableAnimations = exports.checkForceAvdCreation = exports.checkChannel = exports.checkArch = exports.checkTarget = exports.checkApiLevel = exports.VALID_CHANNELS = exports.VALID_ARCHS = exports.VALID_TARGETS = exports.MIN_API_LEVEL = void 0;
exports.checkEmulatorBuild = exports.checkEnableHardwareKeyboard = exports.checkDisableLinuxHardwareAcceleration = exports.checkDisableSpellchecker = exports.checkDisableAnimations = exports.checkForceAvdCreation = exports.checkChannel = exports.checkArch = exports.checkTarget = exports.checkApiLevel = exports.VALID_CHANNELS = exports.VALID_ARCHS = exports.VALID_TARGETS = exports.MIN_API_LEVEL = void 0;
exports.MIN_API_LEVEL = 15;
exports.VALID_TARGETS = ['default', 'google_apis', 'aosp_atd', 'google_atd', 'google_apis_playstore', 'android-wear', 'android-wear-cn', 'android-tv', 'google-tv'];
exports.VALID_ARCHS = ['x86', 'x86_64', 'arm64-v8a'];
Expand Down Expand Up @@ -56,6 +56,12 @@ function checkDisableLinuxHardwareAcceleration(disableLinuxHardwareAcceleration)
}
}
exports.checkDisableLinuxHardwareAcceleration = checkDisableLinuxHardwareAcceleration;
function checkEnableHardwareKeyboard(enableHardwareKeyboard) {
if (!isValidBoolean(enableHardwareKeyboard)) {
throw new Error(`Input for input.enable-hw-keyboard should be either 'true' or 'false'.`);
}
}
exports.checkEnableHardwareKeyboard = checkEnableHardwareKeyboard;
function checkEmulatorBuild(emulatorBuild) {
if (isNaN(Number(emulatorBuild)) || !Number.isInteger(Number(emulatorBuild))) {
throw new Error(`Unexpected emulator build: '${emulatorBuild}'.`);
Expand Down
7 changes: 6 additions & 1 deletion lib/main.js
Expand Up @@ -99,6 +99,11 @@ function run() {
input_validator_1.checkDisableLinuxHardwareAcceleration(disableLinuxHardwareAccelerationInput);
const disableLinuxHardwareAcceleration = disableLinuxHardwareAccelerationInput === 'true';
console.log(`disable Linux hardware acceleration: ${disableLinuxHardwareAcceleration}`);
// enable hardware keyboard
const enableHardwareKeyboardInput = core.getInput('enable-hw-keyboard');
input_validator_1.checkEnableHardwareKeyboard(enableHardwareKeyboardInput);
const enableHardwareKeyboard = enableHardwareKeyboardInput === 'true';
console.log(`enable hardware keyboard: ${enableHardwareKeyboard}`);
// emulator build
const emulatorBuildInput = core.getInput('emulator-build');
if (emulatorBuildInput) {
Expand Down Expand Up @@ -139,7 +144,7 @@ function run() {
// install SDK
yield sdk_installer_1.installAndroidSdk(apiLevel, target, arch, channelId, emulatorBuild, ndkVersion, cmakeVersion);
// launch an emulator
yield emulator_manager_1.launchEmulator(apiLevel, target, arch, profile, cores, ramSize, sdcardPathOrSize, avdName, forceAvdCreation, emulatorOptions, disableAnimations, disableSpellchecker, disableLinuxHardwareAcceleration);
yield emulator_manager_1.launchEmulator(apiLevel, target, arch, profile, cores, ramSize, sdcardPathOrSize, avdName, forceAvdCreation, emulatorOptions, disableAnimations, disableSpellchecker, disableLinuxHardwareAcceleration, enableHardwareKeyboard);
// execute the custom script
try {
// move to custom working directory if set
Expand Down
11 changes: 9 additions & 2 deletions src/emulator-manager.ts
Expand Up @@ -19,7 +19,8 @@ export async function launchEmulator(
emulatorOptions: string,
disableAnimations: boolean,
disableSpellChecker: boolean,
disableLinuxHardwareAcceleration: boolean
disableLinuxHardwareAcceleration: boolean,
enableHardwareKeyboard: boolean
): Promise<void> {
// create a new AVD if AVD directory does not already exist or forceAvdCreation is true
const avdPath = `${process.env.ANDROID_AVD_HOME}/${avdName}.avd`;
Expand All @@ -40,6 +41,10 @@ export async function launchEmulator(
await exec.exec(`sh -c \\"printf 'hw.ramSize=${ramSize}\n' >> ${process.env.ANDROID_AVD_HOME}/"${avdName}".avd"/config.ini`);
}

if (enableHardwareKeyboard) {
await exec.exec(`sh -c \\"printf 'hw.keyboard=yes\n' >> ${process.env.ANDROID_AVD_HOME}/"${avdName}".avd"/config.ini`);
}

//turn off hardware acceleration on Linux
if (process.platform === 'linux' && disableLinuxHardwareAcceleration) {
console.log('Disabling Linux hardware acceleration.');
Expand All @@ -63,7 +68,6 @@ export async function launchEmulator(
await waitForDevice();
await exec.exec(`adb shell input keyevent 82`);

// disable animations
if (disableAnimations) {
console.log('Disabling animations.');
await exec.exec(`adb shell settings put global window_animation_scale 0.0`);
Expand All @@ -73,6 +77,9 @@ export async function launchEmulator(
if (disableSpellChecker) {
await exec.exec(`adb shell settings put secure spell_checker_enabled 0`);
}
if (enableHardwareKeyboard) {
await exec.exec(`adb shell settings put secure show_ime_with_hard_keyboard 0`);
}
}

/**
Expand Down
6 changes: 6 additions & 0 deletions src/input-validator.ts
Expand Up @@ -54,6 +54,12 @@ export function checkDisableLinuxHardwareAcceleration(disableLinuxHardwareAccele
}
}

export function checkEnableHardwareKeyboard(enableHardwareKeyboard: string): void {
if (!isValidBoolean(enableHardwareKeyboard)) {
throw new Error(`Input for input.enable-hw-keyboard should be either 'true' or 'false'.`);
}
}

export function checkEmulatorBuild(emulatorBuild: string): void {
if (isNaN(Number(emulatorBuild)) || !Number.isInteger(Number(emulatorBuild))) {
throw new Error(`Unexpected emulator build: '${emulatorBuild}'.`);
Expand Down
12 changes: 10 additions & 2 deletions src/main.ts
Expand Up @@ -9,7 +9,8 @@ import {
checkDisableSpellchecker,
checkDisableLinuxHardwareAcceleration,
checkForceAvdCreation,
checkChannel
checkChannel,
checkEnableHardwareKeyboard
} from './input-validator';
import { launchEmulator, killEmulator } from './emulator-manager';
import * as exec from '@actions/exec';
Expand Down Expand Up @@ -94,6 +95,12 @@ async function run() {
const disableLinuxHardwareAcceleration = disableLinuxHardwareAccelerationInput === 'true';
console.log(`disable Linux hardware acceleration: ${disableLinuxHardwareAcceleration}`);

// enable hardware keyboard
const enableHardwareKeyboardInput = core.getInput('enable-hw-keyboard');
checkEnableHardwareKeyboard(enableHardwareKeyboardInput);
const enableHardwareKeyboard = enableHardwareKeyboardInput === 'true';
console.log(`enable hardware keyboard: ${enableHardwareKeyboard}`);

// emulator build
const emulatorBuildInput = core.getInput('emulator-build');
if (emulatorBuildInput) {
Expand Down Expand Up @@ -154,7 +161,8 @@ async function run() {
emulatorOptions,
disableAnimations,
disableSpellchecker,
disableLinuxHardwareAcceleration
disableLinuxHardwareAcceleration,
enableHardwareKeyboard
);

// execute the custom script
Expand Down

0 comments on commit d0e9b3f

Please sign in to comment.