@@ -6,25 +6,73 @@ import _ from 'lodash';
6
6
7
7
const EMULATOR_RESOURCES_ROOT = [ 'emulator' , 'resources' ] ;
8
8
const CONFIG_NAME = 'Toren1BD.posters' ;
9
- const CONFIG = `poster wall
9
+ const DEFAULT_CONFIG_PAYLOAD_PREFIX = `poster wall
10
10
size 2 2
11
11
position -0.807 0.320 5.316
12
12
rotation 0 -150 0
13
13
default poster.png
14
14
15
- poster table
15
+ poster table` ;
16
+ const DEFAULT_CONFIG_PAYLOAD = `${ DEFAULT_CONFIG_PAYLOAD_PREFIX }
16
17
size 1 1
17
18
position 0 0 -1.5
18
19
rotation 0 0 0
19
20
` ;
20
21
const PNG_MAGIC = '89504e47' ;
21
22
const PNG_MAGIC_LENGTH = 4 ;
22
23
24
+ /**
25
+ * Updates the emulator configuration to show the
26
+ * image using the given properties when one opens the Camera app.
27
+ *
28
+ * @this {AndroidDriver}
29
+ * @param {string } sdkRoot ADB SDK root folder path
30
+ * @returns {Promise<boolean> } If emulator services restart is needed
31
+ * to load the updated config or false if the current config is already up to date
32
+ * or does not need to be updated
33
+ */
34
+ export async function prepareEmulatorForImageInjection ( sdkRoot ) {
35
+ const { injectedImageProperties : props } = this . opts ;
36
+ if ( ! props ) {
37
+ return false ;
38
+ }
39
+
40
+ const size = `size ${ props . size ?. scaleX ?? 1 } ${ props . size ?. scaleY ?? 1 } ` ;
41
+ const position = `position ${ props . position ?. x ?? 0 } ${ props . position ?. y ?? 0 } ${ props . position ?. z ?? - 1.5 } ` ;
42
+ const rotation = `rotation ${ props . rotation ?. x ?? 0 } ${ props . rotation ?. y ?? 0 } ${ props . rotation ?. z ?? 0 } ` ;
43
+ const strProps = `${ size } \n${ position } \n${ rotation } ` ;
44
+ const configPath = path . resolve ( sdkRoot , ...EMULATOR_RESOURCES_ROOT , CONFIG_NAME ) ;
45
+ if ( await fs . exists ( configPath ) ) {
46
+ const configPayload = await fs . readFile ( configPath , 'utf8' ) ;
47
+ if ( configPayload . includes ( strProps ) ) {
48
+ this . log . info ( `The image injection config at '${ configPath } ' is already up to date. Doing nothing` ) ;
49
+ return false ;
50
+ }
51
+ const updatedPayload = `${ DEFAULT_CONFIG_PAYLOAD_PREFIX }
52
+ ${ size }
53
+ ${ position }
54
+ ${ rotation }
55
+ ` ;
56
+ await fs . writeFile ( configPath , updatedPayload , 'utf-8' ) ;
57
+ } else {
58
+ await fs . writeFile ( configPath , DEFAULT_CONFIG_PAYLOAD , 'utf-8' ) ;
59
+ }
60
+ this . log . info (
61
+ `The image injection config at '${ configPath } ' has been successfully updated with ` +
62
+ `${ size } , ${ position } , ${ rotation } . ` +
63
+ `Expecting further emulator restart to reload the changed config.`
64
+ ) ;
65
+ return true ;
66
+ }
67
+
23
68
/**
24
69
* Updates the emulator configuration to show the given
25
70
* image on the foreground when one opens the Camera app.
26
71
* It is expected that the rear camera of the emulator is
27
72
* configured to show VirtualScene for this feature to work.
73
+ * It is expected that the Virtual Scene posters config is
74
+ * already prepared and loaded either manually or using the
75
+ * `injectedImageProperties` capability.
28
76
*
29
77
* @this {AndroidDriver}
30
78
* @param {import('./types').ImageInjectionOpts } opts
@@ -49,9 +97,6 @@ export async function mobileInjectEmulatorCameraImage(opts) {
49
97
) ;
50
98
}
51
99
52
- const sdkRoot = /** @type {string } */ ( this . adb . sdkRoot ) ;
53
- const destinationFolder = path . resolve ( sdkRoot , ...EMULATOR_RESOURCES_ROOT ) ;
54
- await fs . writeFile ( path . resolve ( destinationFolder , CONFIG_NAME ) , CONFIG , 'utf-8' ) ;
55
100
const tmpImagePath = await tempDir . path ( {
56
101
// this is needed to avoid creation of multiple temp files for the same image payload
57
102
prefix : calculateImageHash ( pngBuffer ) ,
0 commit comments