Skip to content

Commit

Permalink
feat: Add support of UiModeManager service commands
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-mokhnach committed Nov 2, 2023
1 parent 5d70ff0 commit ea97349
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
43 changes: 43 additions & 0 deletions lib/commands/appearance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {mixin} from './mixins';
import {requireArgs} from '../utils';

const RESPONSE_PATTERN = /:\s+(\w+)/;

/**
* @type {import('./mixins').AppearanceMixin & ThisType<import('../driver').AndroidDriver>}
* @satisfies {import('@appium/types').ExternalDriver}
*/
const AppearanceMixin = {
/**
* Set the Ui appearance.
*
* @since Android 10
*/
async mobileSetUiMode(opts) {
const {mode, value} = requireArgs(['mode', 'value'], opts);
await this.adb.shell(['cmd', 'uimode', mode, value]);
},

/**
* Get the Ui appearance.
*
* @since Android 10
* @returns {Promise<string>} The actual state for the queried UI mode,
* for example 'yes' or 'no'
*/
async mobileGetUiMode(opts) {
const {mode} = requireArgs(['mode'], opts);
const response = await this.adb.shell(['cmd', 'uimode', mode]);
// response looks like 'Night mode: no'
const match = RESPONSE_PATTERN.exec(response);
if (!match) {
throw new Error(`Cannot parse the command response: ${response}`);
}
return match[1];
},

};

export default AppearanceMixin;

mixin(AppearanceMixin);
3 changes: 3 additions & 0 deletions lib/commands/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ const ExecuteMixin = {
isKeyboardShown: 'isKeyboardShown',

deviceidle: 'mobileDeviceidle',

setUiMode: 'mobileSetUiMode',
getUiMode: 'mobileGetUiMode',
};

if (!_.has(mobileCommandsMapping, mobileCommand)) {
Expand Down
6 changes: 6 additions & 0 deletions lib/commands/mixins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,11 @@ export interface IMEMixin {
deactivateIMEEngine: () => Promise<void>;
}

export interface AppearanceMixin {
mobileSetUiMode: (opts: types.GetUiModeOpts) => Promise<void>;
mobileGetUiMode: (opts: types.SetUiModeOpts) => Promise<string>;
}

export interface ActivityMixin {
/**
* Starts the given activity intent.
Expand Down Expand Up @@ -935,6 +940,7 @@ declare module '../driver' {
FindMixin,
GeneralMixin,
IMEMixin,
AppearanceMixin,
ActivityMixin,
KeyboardMixin,
LogMixin,
Expand Down
21 changes: 21 additions & 0 deletions lib/commands/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1100,3 +1100,24 @@ export interface DeviceidleOpts {
/** Either a single package or multiple packages to add or remove from the idle whitelist */
packages?: string|string[];
}

export interface SetUiModeOpts {
/** The UI mode to set the value for.
* Supported values are: 'night' and 'car'
*/
mode: string;
/**
* The actual mode value to set.
* Supported value for different UI modes are:
* - night: yes|no|auto|custom_schedule|custom_bedtime
* - car: yes|no
*/
value: string;
}

export interface GetUiModeOpts {
/** The UI mode to set the value for.
* Supported values are: 'night' and 'car'
*/
mode: string;
}

0 comments on commit ea97349

Please sign in to comment.