@@ -6,25 +6,73 @@ import _ from 'lodash';
66
77const  EMULATOR_RESOURCES_ROOT  =  [ 'emulator' ,  'resources' ] ; 
88const  CONFIG_NAME  =  'Toren1BD.posters' ; 
9- const  CONFIG  =  `poster wall 
9+ const  DEFAULT_CONFIG_PAYLOAD_PREFIX  =  `poster wall 
1010size 2 2 
1111position -0.807 0.320 5.316 
1212rotation 0 -150 0 
1313default poster.png 
1414
15- poster table 
15+ poster table` ; 
16+ const  DEFAULT_CONFIG_PAYLOAD  =  `${ DEFAULT_CONFIG_PAYLOAD_PREFIX }  
1617size 1 1 
1718position 0 0 -1.5 
1819rotation 0 0 0 
1920` ; 
2021const  PNG_MAGIC  =  '89504e47' ; 
2122const  PNG_MAGIC_LENGTH  =  4 ; 
2223
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 } ${ position } ${ 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 }  ) ; 
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 }   + 
62+     `${ size } ${ position } ${ rotation }   + 
63+     `Expecting further emulator restart to reload the changed config.` 
64+   ) ; 
65+   return  true ; 
66+ } 
67+ 
2368/** 
2469 * Updates the emulator configuration to show the given 
2570 * image on the foreground when one opens the Camera app. 
2671 * It is expected that the rear camera of the emulator is 
2772 * 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. 
2876 * 
2977 * @this  {AndroidDriver} 
3078 * @param  {import('./types').ImageInjectionOpts } opts 
@@ -49,9 +97,6 @@ export async function mobileInjectEmulatorCameraImage(opts) {
4997    ) ; 
5098  } 
5199
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' ) ; 
55100  const  tmpImagePath  =  await  tempDir . path ( { 
56101    // this is needed to avoid creation of multiple temp files for the same image payload 
57102    prefix : calculateImageHash ( pngBuffer ) , 
0 commit comments