@@ -21,15 +21,15 @@ import {
2121 DeviceType
2222} from "mobile-devices-controller" ;
2323
24- export class DeviceManger implements IDeviceManager {
24+ export class DeviceManager implements IDeviceManager {
2525 private static _emulators : Map < string , IDevice > = new Map ( ) ;
2626
2727 constructor ( ) {
2828 }
2929
3030 public async startDevice ( args : INsCapabilities ) : Promise < IDevice > {
3131 args . appiumCaps . platformName = args . appiumCaps . platformName . toLowerCase ( ) ;
32- let device : IDevice = DeviceManger . getDefaultDevice ( args ) ;
32+ let device : IDevice = DeviceManager . getDefaultDevice ( args ) ;
3333 if ( process . env [ "DEVICE_TOKEN" ] ) {
3434 device . token = process . env [ "DEVICE_TOKEN" ] ;
3535 device . name = process . env [ "DEVICE_NAME" ] || device . name ;
@@ -41,8 +41,8 @@ export class DeviceManger implements IDeviceManager {
4141
4242 // When isSauceLab specified we simply do nothing;
4343 if ( args . isSauceLab || args . ignoreDeviceController ) {
44- args . ignoreDeviceController = true ;
45- DeviceManger . _emulators . set ( args . runType , device ) ;
44+ args . ignoreDeviceController = true ;
45+ DeviceManager . _emulators . set ( args . runType , device ) ;
4646 return device ;
4747 }
4848
@@ -89,18 +89,18 @@ export class DeviceManger implements IDeviceManager {
8989 }
9090 }
9191
92- DeviceManger . _emulators . set ( args . runType , device ) ;
92+ DeviceManager . _emulators . set ( args . runType , device ) ;
9393
9494 return device ;
9595 }
9696
9797 public async stopDevice ( args : INsCapabilities ) : Promise < any > {
98- if ( DeviceManger . _emulators . has ( args . runType )
98+ if ( DeviceManager . _emulators . has ( args . runType )
9999 && ! args . reuseDevice
100100 && ! args . isSauceLab
101101 && ! args . ignoreDeviceController ) {
102- const device = DeviceManger . _emulators . get ( args . runType ) ;
103- await DeviceManger . kill ( device ) ;
102+ const device = DeviceManager . _emulators . get ( args . runType ) ;
103+ await DeviceManager . kill ( device ) ;
104104 }
105105 }
106106
@@ -133,6 +133,76 @@ export class DeviceManger implements IDeviceManager {
133133 return device ;
134134 }
135135
136+ public static async setDontKeepActivities ( args : INsCapabilities , driver , value ) {
137+ if ( args . isAndroid ) {
138+ if ( ! args . ignoreDeviceController ) {
139+ AndroidController . setDontKeepActivities ( value , args . device ) ;
140+ } else if ( args . relaxedSecurity ) {
141+ const status = value ? 1 : 0 ;
142+ const output = await DeviceManager . executeShellCommand ( driver , { command : "settings" , args : [ 'put' , 'global' , 'always_finish_activities' , status ] } ) ;
143+ //check if set
144+ const check = await DeviceManager . executeShellCommand ( driver , { command : "settings" , args : [ 'get' , 'global' , 'always_finish_activities' ] } ) ;
145+ console . info ( `always_finish_activities: ${ check } ` ) ;
146+ }
147+ } else {
148+ // Do nothing for iOS ...
149+ }
150+ }
151+
152+ public static async executeShellCommand ( driver : IDevice , commandAndargs : { command : string , "args" : Array < any > } ) {
153+ if ( driver . platform . toLowerCase ( ) === Platform . ANDROID ) {
154+ const output = await driver . execute ( "mobile: shell" , commandAndargs ) ;
155+ return output ;
156+ }
157+ return undefined ;
158+ }
159+
160+ public static async getDensity ( args : INsCapabilities , driver ) {
161+ args . device . config = args . device . config || { } ;
162+ if ( args . appiumCaps . platformName . toLowerCase ( ) === "android" ) {
163+ if ( ! args . ignoreDeviceController ) {
164+ args . device . config . density = await AndroidController . getPhysicalDensity ( args . device ) ;
165+ }
166+
167+ if ( args . relaxedSecurity ) {
168+ args . device . config . density = await DeviceManager . executeShellCommand ( driver , { command : "wm" , args : [ "density" ] } ) ;
169+ console . log ( `Device density recieved from adb shell command ${ args . device . config . density } ` ) ;
170+ }
171+
172+ if ( args . device . config . density ) {
173+ args . device . config [ 'offsetPixels' ] = AndroidController . calculateScreenOffset ( args . device . config . density ) ;
174+ }
175+ } else {
176+ IOSController . getDevicesScreenInfo ( ) . forEach ( ( v , k , m ) => {
177+ if ( args . device . name . includes ( k ) ) {
178+ args . device . config = {
179+ density : args . device . config [ 'density' ] || v . density ,
180+ offsetPixels : v . actionBarHeight
181+ } ;
182+ }
183+ } ) ;
184+ }
185+ }
186+
187+ public static async applyDeviceAdditionsSettings ( driver , args : INsCapabilities , sessionIfno : any ) {
188+ if ( ! args . device . config || ! args . device . config . offsetPixels ) {
189+ args . device . config = { } ;
190+ let density : number = sessionIfno [ 1 ] . deviceScreenDensity ? sessionIfno [ 1 ] . deviceScreenDensity / 100 : undefined ;
191+
192+ if ( density ) {
193+ console . log ( `Get density from appium session: ${ density } ` ) ;
194+ args . device . config [ 'density' ] = density ;
195+ args . device . config [ 'offsetPixels' ] = AndroidController . calculateScreenOffset ( args . device . config . density ) ;
196+ }
197+
198+ if ( ! density ) {
199+ await DeviceManager . getDensity ( args , driver ) ;
200+ }
201+
202+ density ? console . log ( `Device setting:` , args . device . config ) : console . log ( `Could not resolve device density. Please provide offset in appium config` ) ;
203+ }
204+ }
205+
136206 public getPackageId ( device : IDevice , appPath : string ) : string {
137207 const appActivity = ( device . type === DeviceType . EMULATOR || device . platform === Platform . ANDROID ) ? AndroidController . getPackageId ( appPath ) : IOSController . getIOSPackageId ( device . type , appPath ) ;
138208 return appActivity ;
0 commit comments