Skip to content

Commit

Permalink
refactor!: Drop APIs related to io.appium.settings interactions (#716)
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-mokhnach authored Jan 12, 2024
1 parent 15f2cf8 commit dcfe5a6
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 1,576 deletions.
112 changes: 2 additions & 110 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ appium-adb
[![NPM version](http://img.shields.io/npm/v/appium-adb.svg)](https://npmjs.org/package/appium-adb)
[![Downloads](http://img.shields.io/npm/dm/appium-adb.svg)](https://npmjs.org/package/appium-adb)

A wrapper over android-adb, implemented using ES6 and along with `async/await`. This package is mainly used by Appium to perform all adb operations on android device.

*Note*: Issue tracking for this repo has been disabled. Please use the [main Appium issue tracker](https://github.com/appium/appium/issues) instead.
A wrapper over [Android Debugger Bridge](https://developer.android.com/tools/adb), implemented using ES6
and along with `async/await`. This package is mainly used by Appium to perform all adb operations on Android devices.

## Installing

Expand Down Expand Up @@ -49,110 +48,3 @@ import ADB from 'appium-adb';
const adb = await ADB.createADB();
console.log(await adb.getPIDsByName('com.android.phone'));
```

### List of methods:

- `createADB`
- `getAdbWithCorrectAdbPath`
- `initAapt`
- `initZipAlign`
- `getApiLevel`
- `isDeviceConnected`
- `mkdir`
- `isValidClass`
- `forceStop`
- `clear`
- `stopAndClear`
- `availableIMEs`
- `enabledIMEs`
- `enableIME`
- `disableIME`
- `setIME`
- `defaultIME`
- `keyevent`
- `lock`
- `back`
- `goToHome`
- `isScreenLocked`
- `isSoftKeyboardPresent`
- `sendTelnetCommand`
- `isAirplaneModeOn`
- `setAirplaneMode`
- `broadcastAirplaneMode`
- `isWifiOn`
- `getScreenSize`
- `getScreenDensity`
- `setWifiState`
- `isDataOn`
- `setDataState`
- `setWifiAndData`
- `rimraf`
- `push`
- `pull`
- `processExists`
- `forwardPort`
- `reversePort` (ApiLevel >=21)
- `forwardAbstractPort`
- `ping`
- `restart`
- `startLogcat`
- `stopLogcat`
- `getLogcatLogs`
- `getPIDsByName`
- `killProcessesByName`
- `killProcessByPID`
- `broadcastProcessEnd`
- `broadcast`
- `packageAndLaunchActivityFromManifest`
- `compileManifest`
- `insertManifest`
- `hasInternetPermissionFromManifest`
- `getSdkBinaryPath`
- `getBinaryFromSdkRoot`
- `getBinaryFromPath`
- `getConnectedDevices`
- `getDevicesWithRetry`
- `restartAdb`
- `adbExec`
- `shell`
- `getAdbServerPort`
- `getEmulatorPort`
- `getPortFromEmulatorString`
- `getConnectedEmulators`
- `setEmulatorPort`
- `setDeviceId`
- `getRunningAVD`
- `getRunningAVDWithRetry`
- `killAllEmulators`
- `launchAVD`
- `waitForEmulatorReady`
- `waitForDevice`
- `reboot`
- `signWithDefaultCert`
- `signWithCustomCert`
- `sign`
- `zipAlignApk`
- `checkApkCert`
- `checkCustomApkCert`
- `getKeystoreHash`
- `isAppInstalled`
- `startApp`
- `startUri`
- `getFocusedPackageAndActivity`
- `waitForActivityOrNot`
- `waitForActivity`
- `waitForNotActivity`
- `uninstallApk`
- `installFromDevicePath`
- `install`
- `fingerprint` (ApiLevel >=23 | emulator only)
- `sendSMS` (emulator only)
- `rotate` (emulator only)
- `powerAC` (emulator only)
- `powerCapacity` (emulator only)
- `powerOFF` (emulator only)
- `gsmCall` (emulator only)
- `gsmSignal` (emulator only)
- `gsmVoice` (emulator only)
- `root`
- `unroot`
41 changes: 1 addition & 40 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -867,45 +867,6 @@ async function formatConfigMarker (configsGetter, desiredMarker, defaultMarker)
return configMarker;
}

/**
* Parses the output in JSON format retrieved from
* the corresponding Appium Settings broadcast calls
*
* @param {string} output The actual command output
* @param {string} entityName The name of the entity which is
* going to be parsed
* @returns {Object} The parsed JSON object
* @throws {Error} If the output cannot be parsed
* as a valid JSON
*/
function parseJsonData (output, entityName) {
if (!/\bresult=-1\b/.test(output) || !/\bdata="/.test(output)) {
log.debug(output);
throw new Error(
`Cannot retrieve ${entityName} from the device. ` +
'Check the server log for more details'
);
}
const match = /\bdata="(.+)",?/.exec(output);
if (!match) {
log.debug(output);
throw new Error(
`Cannot parse ${entityName} from the command output. ` +
'Check the server log for more details'
);
}
const jsonStr = _.trim(match[1]);
try {
return JSON.parse(jsonStr);
} catch (e) {
log.debug(jsonStr);
throw new Error(
`Cannot parse ${entityName} from the resulting data string. ` +
'Check the server log for more details'
);
}
}

/**
* Transforms the given language and country abbreviations
* to AVD arguments array
Expand Down Expand Up @@ -1096,7 +1057,7 @@ export {
getApkanalyzerForOs, getOpenSslForOs, extractMatchingPermissions, APKS_EXTENSION,
APK_INSTALL_TIMEOUT, APKS_INSTALL_TIMEOUT, buildInstallArgs, APK_EXTENSION,
DEFAULT_ADB_EXEC_TIMEOUT, parseManifest, parseAaptStrings, parseAapt2Strings,
formatConfigMarker, parseJsonData, unsignApk, toAvdLocaleArgs, requireSdkRoot,
formatConfigMarker, unsignApk, toAvdLocaleArgs, requireSdkRoot,
getSdkRootFromEnv, getAndroidPrefsRoot, dirExists, escapeShellArg,
parseLaunchableActivityNames, matchComponentName, getResourcePath
};
2 changes: 0 additions & 2 deletions lib/mixins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
SystemCalls,
ApkUtils,
ADBCommands,
SettingsClientCommands,
ADBEmuCommands,
LockManagementCommands,
ManifestMethods,
Expand All @@ -29,7 +28,6 @@ declare module './adb' {
ApksUtils,
SystemCalls,
ADBOptions,
SettingsClientCommands,
ADBEmuCommands,
LockManagementCommands,
ManifestMethods,
Expand Down
58 changes: 42 additions & 16 deletions lib/tools/adb-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -923,22 +923,6 @@ methods.isDataOn = async function isDataOn () {
return (parseInt(stdout, 10) !== 0);
};

/**
* Change the state of WiFi and/or Data transfer on the device under test.
*
* @this {import('../adb.js').ADB}
* @param {{wifi?: boolean, data?: boolean}} opts - True to enable wifi and data transfer
* @param {boolean} [isEmulator] - Set it to true if the device under test is an emulator rather than a real device.
*/
methods.setWifiAndData = async function setWifiAndData ({wifi, data}, isEmulator = false) {
if (util.hasValue(wifi)) {
await this.setWifiState(wifi, isEmulator);
}
if (util.hasValue(data)) {
await this.setDataState(data, isEmulator);
}
};

/**
* Check the state of animation on the device under test.
*
Expand Down Expand Up @@ -1953,6 +1937,48 @@ methods.takeScreenshot = async function takeScreenshot (displayId) {
return stdout;
};

/**
* Change the state of WiFi on the device under test.
* Only works for real devices since API 30
*
* @this {import('../adb.js').ADB}
* @param {boolean} on - True to enable and false to disable it.
* @param {boolean} [isEmulator=false] - Set it to true if the device under test
* is an emulator rather than a real device.
*/
methods.setWifiState = async function setWifiState (on, isEmulator = false) {
if (isEmulator) {
// The svc command does not require to be root since API 26
await this.shell(['svc', 'wifi', on ? 'enable' : 'disable'], {
privileged: await this.getApiLevel() < 26,
});
return;
}

await this.shell(['cmd', '-w', 'wifi', 'set-wifi-enabled', on ? 'enabled' : 'disabled']);
};

/**
* Change the state of Data transfer on the device under test.
* Only works for real devices since API 30
*
* @this {import('../adb.js').ADB}
* @param {boolean} on - True to enable and false to disable it.
* @param {boolean} [isEmulator=false] - Set it to true if the device under test
* is an emulator rather than a real device.
*/
methods.setDataState = async function setDataState (on, isEmulator = false) {
if (isEmulator) {
// The svc command does not require to be root since API 26
await this.shell(['svc', 'data', on ? 'enable' : 'disable'], {
privileged: await this.getApiLevel() < 26,
});
return;
}

await this.shell(['cmd', 'phone', 'data', on ? 'enable' : 'disable']);
};

export default methods;

/**
Expand Down
62 changes: 0 additions & 62 deletions lib/tools/apk-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -973,24 +973,6 @@ apkUtilsMethods.getDeviceLocale = async function getDeviceLocale () {
return locale;
};

/**
* Set the locale name of the device under test and the format of the locale is en-US, for example.
* This method call setDeviceLanguageCountry, so, please use setDeviceLanguageCountry as possible.
*
* @this {import('../adb.js').ADB}
* @param {string} locale - Names of the device language and the country connected with `-`. e.g. en-US.
*/
apkUtilsMethods.setDeviceLocale = async function setDeviceLocale (locale) {
const validateLocale = new RegExp(/[a-zA-Z]+-[a-zA-Z0-9]+/);
if (!validateLocale.test(locale)) {
log.warn(`setDeviceLocale requires the following format: en-US or ja-JP`);
return;
}

let split_locale = locale.split('-');
await this.setDeviceLanguageCountry(split_locale[0], split_locale[1]);
};

/**
* Make sure current device locale is expected or not.
*
Expand Down Expand Up @@ -1060,50 +1042,6 @@ apkUtilsMethods.ensureCurrentLocale = async function ensureCurrentLocale (langua
}));
};

/**
* Set the locale name of the device under test.
*
* @this {import('../adb.js').ADB}
* @privateRemarks FIXME: language or country is required
* @param {string} [language] - Language. The language field is case insensitive, but Locale always canonicalizes to lower case.
* format: [a-zA-Z]{2,8}. e.g. en, ja : https://developer.android.com/reference/java/util/Locale.html
* @param {string} [country] - Country. The country (region) field is case insensitive, but Locale always canonicalizes to upper case.
* format: [a-zA-Z]{2} | [0-9]{3}. e.g. US, JP : https://developer.android.com/reference/java/util/Locale.html
* @param {string?} [script] - Script. The script field is case insensitive but Locale always canonicalizes to title case.
* format: [a-zA-Z]{4}. e.g. Hans in zh-Hans-CN : https://developer.android.com/reference/java/util/Locale.html
*/
apkUtilsMethods.setDeviceLanguageCountry = async function setDeviceLanguageCountry (language, country, script = null) {
let hasLanguage = language && _.isString(language);
let hasCountry = country && _.isString(country);
if (!hasLanguage || !hasCountry) {
log.warn(`setDeviceLanguageCountry requires language and country at least`);
log.warn(`Got language: '${language}' and country: '${country}'`);
return;
}
let apiLevel = await this.getApiLevel();

language = (language || '').toLowerCase();
country = (country || '').toUpperCase();

if (apiLevel < 23) {
let curLanguage = (await this.getDeviceLanguage()).toLowerCase();
let curCountry = (await this.getDeviceCountry()).toUpperCase();

if (language !== curLanguage || country !== curCountry) {
await this.setDeviceSysLocaleViaSettingApp(language, country);
}
} else {
let curLocale = await this.getDeviceLocale();

// zh-Hans-CN : zh-CN
const localeCode = script ? `${language}-${script}-${country}` : `${language}-${country}`;
log.debug(`Current locale: '${curLocale}'; requested locale: '${localeCode}'`);
if (localeCode.toLowerCase() !== curLocale.toLowerCase()) {
await this.setDeviceSysLocaleViaSettingApp(language, country, script);
}
}
};

/**
* @typedef {Object} AppInfo
* @property {string} name - Package name, for example 'com.acme.app'.
Expand Down
3 changes: 0 additions & 3 deletions lib/tools/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import apkUtilsMethods from './apk-utils';
import apksUtilsMethods from './apks-utils';
import aabUtilsMethods from './aab-utils';
import emuMethods from './adb-emu-commands';
import settingsClientCommands from './settings-client-commands';
import lockManagementCommands from './lockmgmt';
import keyboardCommands from './keyboard-commands';

Expand All @@ -25,7 +24,6 @@ Object.assign(
apkUtilsMethods,
apksUtilsMethods,
aabUtilsMethods,
settingsClientCommands,
lockManagementCommands,
keyboardCommands
);
Expand All @@ -40,7 +38,6 @@ export type * from './apk-signing';
export type * from './apk-utils';
export type * from './apks-utils';
export type * from './aab-utils';
export type * from './settings-client-commands';
export type * from './android-manifest';
export type * from './keyboard-commands';
export type * from './lockmgmt';
Loading

0 comments on commit dcfe5a6

Please sign in to comment.