Skip to content

Commit

Permalink
Add option to enable hw keyboard and disable soft keyboard
Browse files Browse the repository at this point in the history
  • Loading branch information
AfzalivE committed Feb 24, 2021
1 parent 393c3c5 commit 4b6ca99
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ jobs:
| `disable-spellchecker` | Optional | `false` | Whether to disable spellchecker - `true` or `false`. |
| `disable-autofill` | Optional | `false` | Whether to disable autofill - `true` or `false`. |
| `longpress-timeout` | Optional | 500 | Longpress timeout in milliseconds. |
| `enable-hw-keyboard` | Optional | `false` | Whether to enable the hw keyboard and disable soft 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
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,27 @@ describe('disable-autofill validator tests', () => {
});
});

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

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

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

describe('longpress-timeout validator tests', () => {
it('Throws if longpress-timeout is not a number', () => {
const func = () => {
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ inputs:
longpress-timeout:
description: Longpress timeout in milliseconds.
default: 500
enable-hw-keyboard:
description: Enable hw keyboard and disable soft 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
8 changes: 7 additions & 1 deletion lib/emulator-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,16 @@ const EMULATOR_BOOT_TIMEOUT_SECONDS = 600;
/**
* Creates and launches a new AVD instance with the specified configurations.
*/
function launchEmulator(apiLevel, target, arch, profile, sdcardPathOrSize, avdName, emulatorOptions, disableAnimations, disableSpellChecker, disableAutofill, longPressTimeout) {
function launchEmulator(apiLevel, target, arch, profile, sdcardPathOrSize, avdName, emulatorOptions, disableAnimations, disableSpellChecker, disableAutofill, longPressTimeout, enableHwKeyboard) {
return __awaiter(this, void 0, void 0, function* () {
// create a new AVD
const profileOption = profile.trim() !== '' ? `--device '${profile}'` : '';
const sdcardPathOrSizeOption = sdcardPathOrSize.trim() !== '' ? `--sdcard '${sdcardPathOrSize}'` : '';
console.log(`Creating AVD.`);
yield exec.exec(`sh -c \\"echo no | avdmanager create avd --force -n "${avdName}" --abi '${target}/${arch}' --package 'system-images;android-${apiLevel};${target};${arch}' ${profileOption} ${sdcardPathOrSizeOption}"`);
if (enableHwKeyboard) {
yield exec.exec(`sh -c \\"printf 'hw.keyboard=yes\n' >> ~/.android/avd/"${avdName}".avd"/config.ini`);
}
// start emulator
console.log('Starting emulator.');
// turn off hardware acceleration on Linux
Expand Down Expand Up @@ -75,6 +78,9 @@ function launchEmulator(apiLevel, target, arch, profile, sdcardPathOrSize, avdNa
if (longPressTimeout) {
yield exec.exec(`adb shell settings put secure long_press_timeout ${longPressTimeout}`);
}
if (enableHwKeyboard) {
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkEmulatorBuild = exports.checkLongPressTimeout = exports.checkDisableAutofill = exports.checkDisableSpellchecker = exports.checkDisableAnimations = exports.checkArch = exports.checkTarget = exports.checkApiLevel = exports.VALID_ARCHS = exports.VALID_TARGETS = exports.MIN_API_LEVEL = void 0;
exports.checkEmulatorBuild = exports.checkLongPressTimeout = exports.checkEnableHwKeyboard = exports.checkDisableAutofill = exports.checkDisableSpellchecker = exports.checkDisableAnimations = exports.checkArch = exports.checkTarget = exports.checkApiLevel = exports.VALID_ARCHS = exports.VALID_TARGETS = exports.MIN_API_LEVEL = void 0;
exports.MIN_API_LEVEL = 15;
exports.VALID_TARGETS = ['default', 'google_apis', 'google_apis_playstore'];
exports.VALID_ARCHS = ['x86', 'x86_64'];
Expand Down Expand Up @@ -43,6 +43,12 @@ function checkDisableAutofill(disableAutofill) {
}
}
exports.checkDisableAutofill = checkDisableAutofill;
function checkEnableHwKeyboard(enableHwKeyboard) {
if (!isValidBoolean(enableHwKeyboard)) {
throw new Error(`Input for input.enable-hw-keyboard should be either 'true' or 'false'.`);
}
}
exports.checkEnableHwKeyboard = checkEnableHwKeyboard;
function checkLongPressTimeout(timeout) {
if (isNaN(Number(timeout)) || !Number.isInteger(Number(timeout))) {
throw new Error(`Unexpected longpress-timeout: '${timeout}'.`);
Expand Down
7 changes: 6 additions & 1 deletion lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ function run() {
input_validator_1.checkDisableAnimations(disableAnimationsInput);
const disableAnimations = disableAnimationsInput === 'true';
console.log(`disable animations: ${disableAnimations}`);
// disable ime
const enableHwKeyboardInput = core.getInput('enable-hw-keyboard');
input_validator_1.checkEnableHwKeyboard(enableHwKeyboardInput);
const enableHwKeyboard = enableHwKeyboardInput === 'true';
console.log(`enable hw keyboard: ${enableHwKeyboard}`);
// disable spellchecker
const disableSpellcheckerInput = core.getInput('disable-spellchecker');
input_validator_1.checkDisableSpellchecker(disableSpellcheckerInput);
Expand Down Expand Up @@ -127,7 +132,7 @@ function run() {
// install SDK
yield sdk_installer_1.installAndroidSdk(apiLevel, target, arch, emulatorBuild, ndkVersion, cmakeVersion);
// launch an emulator
yield emulator_manager_1.launchEmulator(apiLevel, target, arch, profile, sdcardPathOrSize, avdName, emulatorOptions, disableAnimations, disableSpellchecker, disableAutofill, longPressTimeout);
yield emulator_manager_1.launchEmulator(apiLevel, target, arch, profile, sdcardPathOrSize, avdName, emulatorOptions, disableAnimations, disableSpellchecker, disableAutofill, longPressTimeout, enableHwKeyboard);
// execute the custom script
try {
// move to custom working directory if set
Expand Down
10 changes: 9 additions & 1 deletion src/emulator-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ export async function launchEmulator(
disableAnimations: boolean,
disableSpellChecker: boolean,
disableAutofill: boolean,
longPressTimeout: number
longPressTimeout: number,
enableHwKeyboard: boolean
): Promise<void> {
// create a new AVD
const profileOption = profile.trim() !== '' ? `--device '${profile}'` : '';
Expand All @@ -26,6 +27,10 @@ export async function launchEmulator(
`sh -c \\"echo no | avdmanager create avd --force -n "${avdName}" --abi '${target}/${arch}' --package 'system-images;android-${apiLevel};${target};${arch}' ${profileOption} ${sdcardPathOrSizeOption}"`
);

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

// start emulator
console.log('Starting emulator.');

Expand Down Expand Up @@ -64,6 +69,9 @@ export async function launchEmulator(
if (longPressTimeout) {
await exec.exec(`adb shell settings put secure long_press_timeout ${longPressTimeout}`);
}
if (enableHwKeyboard) {
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
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ export function checkDisableAutofill(disableAutofill: string): void {
}
}

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

export function checkLongPressTimeout(timeout: string): void {
if (isNaN(Number(timeout)) || !Number.isInteger(Number(timeout))) {
throw new Error(`Unexpected longpress-timeout: '${timeout}'.`);
Expand Down
20 changes: 18 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import * as core from '@actions/core';
import { installAndroidSdk } from './sdk-installer';
import { checkApiLevel, checkTarget, checkArch, checkDisableAnimations, checkEmulatorBuild, checkDisableSpellchecker, checkDisableAutofill, checkLongPressTimeout } from './input-validator';
import {
checkApiLevel,
checkTarget,
checkArch,
checkDisableAnimations,
checkEmulatorBuild,
checkDisableSpellchecker,
checkDisableAutofill,
checkLongPressTimeout,
checkEnableHwKeyboard
} from './input-validator';
import { launchEmulator, killEmulator } from './emulator-manager';
import * as exec from '@actions/exec';
import { parseScript } from './script-parser';
Expand Down Expand Up @@ -57,6 +67,12 @@ async function run() {
const disableAnimations = disableAnimationsInput === 'true';
console.log(`disable animations: ${disableAnimations}`);

// disable ime
const enableHwKeyboardInput = core.getInput('enable-hw-keyboard');
checkEnableHwKeyboard(enableHwKeyboardInput);
const enableHwKeyboard = enableHwKeyboardInput === 'true';
console.log(`enable hw keyboard: ${enableHwKeyboard}`);

// disable spellchecker
const disableSpellcheckerInput = core.getInput('disable-spellchecker');
checkDisableSpellchecker(disableSpellcheckerInput);
Expand Down Expand Up @@ -116,7 +132,7 @@ async function run() {
await installAndroidSdk(apiLevel, target, arch, emulatorBuild, ndkVersion, cmakeVersion);

// launch an emulator
await launchEmulator(apiLevel, target, arch, profile, sdcardPathOrSize, avdName, emulatorOptions, disableAnimations, disableSpellchecker, disableAutofill, longPressTimeout);
await launchEmulator(apiLevel, target, arch, profile, sdcardPathOrSize, avdName, emulatorOptions, disableAnimations, disableSpellchecker, disableAutofill, longPressTimeout, enableHwKeyboard);

// execute the custom script
try {
Expand Down

0 comments on commit 4b6ca99

Please sign in to comment.