lib/Android/ElectricSheep/Automator/ScreenLayout.pm

filter: Statementstmt 88.2% 15 / 17 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 91.3% 21 / 23 CC 1 SCAR 0.5
CC
1
Cov
88%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« lib/Android/ElectricSheep/Automator/DeviceProperties.pm ↑ Summary lib/Android/ElectricSheep/Automator/Plugins/Apps/Base.pm »
1 package Android::ElectricSheep::Automator::ScreenLayout;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 our $VERSION = '0.09';
8  
9 1 ▶use Mojo::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Config::JSON::Enhanced;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 0 ▶use XML::XPath;
Subroutine BEGIN: called CC=1 SCAR=2.6
12 use XML::LibXML;
13  
14 use Data::Roundtrip qw/perl2dump no-unicode-escape-permanently/;
15  
16 use overload ( '""' => \&toString );
17  
18 sub new {
19 my $class = $_[0];
20 my $params = $_[1] // {};
21  
22 my $parent = ( caller(1) )[3] || "N/A";
23 my $whoami = ( caller(0) )[3];
24  
25 my $self = {
26 '_private' => {
27 'xml' => undef,
28 'logger-object' => undef,
29 'verbosity' => 0,
30 },
31 'data' => {
32 'w' => 0,
33 'h' => 0,
34 # x1,y1,x2,y2,w,h
35 'top-area' => [0, 0, 0, 0, 0, 0],
36 # x1,y1,x2,y2,w,h
37 'app-icons-area' => [0, 0, 0, 0, 0, 0],
38 # x1,y1,x2,y2,w,h
39 'dock-divider-area' => [0, 0, 0, 0, 0, 0],
40 # x1,y1,x2,y2,w,h
41 # these are some common apps which don't change if you swipe screens i think
42 'hotseat-area' => [0, 0, 0, 0, 0, 0],
43 # x1,y1,x2,y2,w,h
44 'home-buttons-area' => [0, 0, 0, 0, 0, 0],
45 'apps' => {}, # appname => [bounds x1,y1,x2,y2]
46 'screen-name' => '',
47 }
48 };
49 bless $self => $class;
50  
51 if( exists $params->{'logger-object'} ){ $self->{'_private'}->{'logger-object'} = $params->{'logger-object'} } else { $self->{'_private'}->{'logger-object'} = Mojo::Log->new() }
52 if( exists $params->{'verbosity'} ){ $self->{'_private'}->{'verbosity'} = $params->{'verbosity'} } else { $self->{'_private'}->{'verbosity'} = Mojo::Log->new() }
53 # we now have a log and verbosity
54  
55 my $log = $self->log;
56 my $verbosity = $self->verbosity;
57  
58 if( exists $params->{'data'} ){
59 my $d = $self->{'data'};
60 my $p = $params->{'data'};
61 for my $k (sort keys %$d){
62 if( exists($p->{$k}) && defined($p->{$k}) ){
63 if( $self->set($k, $p->{$k}) ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, call to ".'set()'." has failed for input parameter '$k', is its type as expected (".ref($d->{$k}).")?"); return undef }
64 }
65 }
66 }
67  
68 if( exists $params->{'xml-string'} ){
69 if( ! defined $self->fromXML({'xml-string' => $params->{'xml-string'}}) ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, call to ".'fromXML()'." has failed."); return undef }
70 } elsif( exists $params->{'xml-filename'} ){
71 if( ! defined $self->fromXML({'xml-filename' => $params->{'xml-filename'}}) ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, call to ".'fromXML()'." has failed this XML file: '".$params->{'xml-filename'}."'."); return undef }
72 }
73  
74 return $self;
75 }
76  
77 # parses a UI Automator XML dump either from
78 # file: 'xml-filename' => ...
79 # or from a string: 'xml-string' => ...
80 # It returns 1 on error, 0 on success
81 # The big problem is incompatibility of the XML between Android API versions
82 # if you specify 'fully' => 1, it works fine with our real phone
83 # but it fails with API 30. So, we are happy only to find the width and
84 # height from that XML. That means that all fields except 'w' and 'h'
85 # will not be found here.
86 # my $xmlstring = $self->dump_current_screen_ui();
87 # my $sl = Android::ElectricSheep::Automator::ScreenLayout->new({'xml-string' => $xmlstring});
88 # will call fromXML();
89 # Or make it yourself: adb shell uiautomator dump outfile
90 # or adb exec-out uiautomator dump /dev/tty | awk '{gsub("UI hierchary dumped to: /dev/tty", "");print}'
91 sub fromXML {
92 my ($self, $params) = @_;
93 $params //= {};
94  
95 my $parent = ( caller(1) )[3] || "N/A";
96 my $whoami = ( caller(0) )[3];
97  
98 my $log = $self->log;
99 my $verbosity = $self->verbosity;
100  
101 my $doc;
102 if( exists $params->{'xml-filename'} ){
103 $doc = XML::LibXML->load_xml(location => $params->{'xml-filename'});
104 } elsif( exists $params->{'xml-string'} ){
105 $doc = XML::LibXML->load_xml(string => $params->{'xml-string'});
106 }
107 if( ! defined $doc ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, call to ".'XML::XPath->new()'." has failed."); return undef }
108  
109 # find a node AT THE TOP for setting the width and height
110 # text=""
111 # content-desc=""
112 # resource-id=""
113 # class="android.view.FrameLayout"
114 my $xpath = '/hierarchy/node[@text=\'\' and @resource-id=\'\' and @class=\'android.widget.FrameLayout\']';
115 my $numframes = 0; # paranoid check how many frames found?
116 my @nodes = eval { $doc->findnodes($xpath) };
117 if( $@ ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, call to ".'findnodes()'." has failed for this xpath: $xpath : $@"); return undef };
118 foreach my $anode (@nodes){
119 # the whole screen size is in bounds
120 my $bounds = $anode->getAttribute('bounds');
121 $bounds =~ s/\s+//;
122 if( $bounds =~ /^\[(\d+),(\d+)\]\[(\d+),(\d+)\]$/ ){
123 my ($x1, $y1, $x2, $y2) = ($1, $2, $3, $4);
124 $self->set('w', $x2-$x1);
125 $self->set('h', $y2-$y1);
126 } else { $log->error($anode."\n${whoami} (via $parent), line ".__LINE__." : error, above node has invalid bounds/1."); return undef }
127 $numframes++;
128 }
129 if( $numframes != 1 ){ $log->error($doc."\n${whoami} (via $parent), line ".__LINE__." : error, failed to find exactly one node with XPath=${xpath} but found ${numframes} instead, see above xml."); return undef }
130  
131 if( exists($params->{'fully'}) && defined($params->{'fully'}) && ($params->{'fully'}==1) ){
132 # find the app-icons-area, it may not be there
133 # text=""
134 # content-desc=""
135 # resource-id="com.huawei.android.launcher:id/workspace_screen" <<<< we will filterout huawei
136 # class="android.view.ViewGroup"
137 # ONLY XPATH1 is supported, so no ends-with or matching regex, we have to filter ourselves
138 #my $xpath = '//node[@text=\'\' and @class=\'android.view.ViewGroup\' and ends-with(@resource-id, \'/workspace_screen\')]';
139 $xpath = '//node[@text=\'\' and @content-desc=\'\' and @class=\'android.view.ViewGroup\' and contains(@resource-id, \'/workspace_screen\')]';
140 $numframes = 0; # paranoid check how many frames found?
141 @nodes = eval { $doc->findnodes($xpath) };
142 if( $@ ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, call to ".'findnodes()'." has failed for this xpath: $xpath"); return undef };
143 foreach my $anode (@nodes){
144 my $resource_id = $anode->getAttribute('resource-id');
145 if( $resource_id =~ /\/workspace_screen$/i ){
146 my $bounds = $anode->getAttribute('bounds');
147 $bounds =~ s/\s+//;
148 if( $bounds =~ /^\[(\d+),(\d+)\]\[(\d+),(\d+)\]$/ ){
149 my ($x1, $y1, $x2, $y2) = ($1, $2, $3, $4);
150 $self->set('app-icons-area', [$x1, $y1, $x2, $y2, $x2-$x1, $y2-$y1]);
151 } else { $log->error($anode."\n${whoami} (via $parent), line ".__LINE__." : error, above node has invalid bounds/2."); return undef }
152 $numframes++;
153 } else { $log->error($anode."\n${whoami} (via $parent), line ".__LINE__." : error, above node has unexpected 'resource-id' ($resource_id), does not end in 'workspace_screen'."); return undef }
154 }
155 if( $numframes != 1 ){ $log->warn($doc."\n${whoami} (via $parent), line ".__LINE__." : error, failed to find exactly one node with XPath=${xpath} but found ${numframes} instead, see above xml.") }
156  
157 # now search for dock_divider to find its dimensions, it may not be there
158 # text=""
159 # resource-id="com.huawei.android.launcher:id/dock_divider"
160 # class="android.widget.ImageView"
161 # content-desc="Page indicator"
162 $xpath = '//node[@text=\'\' and @class=\'android.widget.ImageView\' and @content-desc=\'Page indicator\' and contains(@resource-id, \'id/dock_divider\')]';
163 $numframes = 0;
164 @nodes = eval { $doc->findnodes($xpath) };
165 if( $@ ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, call to ".'findnodes()'." has failed for this xpath: $xpath"); return undef };
166 foreach my $anode (@nodes){
167 my $resource_id = $anode->getAttribute('resource-id');
168 if( $resource_id =~ /\/dock_divider$/i ){
169 # the app-icons space is inside a frame
170 my $bounds = $anode->getAttribute('bounds');
171 $bounds =~ s/\s+//;
172 if( $bounds =~ /^\[(\d+),(\d+)\]\[(\d+),(\d+)\]$/ ){
173 my ($x1, $y1, $x2, $y2) = ($1, $2, $3, $4);
174 $self->set('dock-divider-area', [$x1, $y1, $x2, $y2, $x2-$x1, $y2-$y1]);
175 } else { $log->error($anode."\n${whoami} (via $parent), line ".__LINE__." : error, above node has invalid bounds/3."); return undef }
176 $numframes++;
177 } else { $log->error($anode."\n${whoami} (via $parent), line ".__LINE__." : error, above node has unexpected 'resource-id' ($resource_id), does not end in 'dock_divider'."); return undef }
178 }
179 if( $numframes != 1 ){ $log->warn($doc."\n${whoami} (via $parent), line ".__LINE__." : error, failed to find exactly one node with XPath=${xpath} but found ${numframes} instead, see above xml.") }
180  
181 # now search for hotseat (the common apps at the bottom invariant to swiping)
182 # text=""
183 # resource-id="com.huawei.android.launcher:id/hotseat"
184 # class="android.widget.FrameLayout"
185 # content-desc=""
186 $xpath = '//node[@text=\'\' and @class=\'android.widget.FrameLayout\' and @content-desc=\'\' and contains(@resource-id, \'id/hotseat\')]';
187 $numframes = 0;
188 @nodes = eval { $doc->findnodes($xpath) };
189 if( $@ ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, call to ".'findnodes()'." has failed for this xpath: $xpath"); return undef };
190 foreach my $anode (@nodes){
191 my $resource_id = $anode->getAttribute('resource-id');
192 if( $resource_id =~ /\/hotseat$/i ){
193 # the app-icons space is inside a frame
194 my $bounds = $anode->getAttribute('bounds');
195 $bounds =~ s/\s+//;
196 if( $bounds =~ /^\[(\d+),(\d+)\]\[(\d+),(\d+)\]$/ ){
197 my ($x1, $y1, $x2, $y2) = ($1, $2, $3, $4);
198 $self->set('hotseat-area', [$x1, $y1, $x2, $y2, $x2-$x1, $y2-$y1]);
199 } else { $log->error($anode."\n${whoami} (via $parent), line ".__LINE__." : error, above node has invalid bounds/4."); return undef }
200 $numframes++;
201 } else { $log->error($anode."\n${whoami} (via $parent), line ".__LINE__." : error, above node has unexpected 'resource-id' ($resource_id), does not end in 'hotseat'."); return undef }
202 }
203 if( $numframes != 1 ){ $log->warn($doc."\n${whoami} (via $parent), line ".__LINE__." : error, failed to find exactly one node with XPath=${xpath} but found ${numframes} instead, see above xml.") }
204  
205 # the screen name, it may not be there
206 # find a node with
207 # text=""
208 # content-desc="Screen 2 of 5"
209 # resource-id="com.huawei.android.launcher:id/workspace" <<<< we will filterout huawei
210 # class="android.view.ViewGroup"
211 # ONLY XPATH1 is supported, so no ends-with or matching regex, we have to filter ourselves
212 #my $xpath = '//node[@text=\'\' and @class=\'android.view.ViewGroup\' and ends-with(@resource-id, \'/workspace\')]';
213 $xpath = '//node[@text=\'\' and @text=\'\' and @class=\'android.view.ViewGroup\' and contains(@resource-id, \'/workspace\')]';
214 $numframes = 0; # paranoid check how many frames found?
215 @nodes = eval { $doc->findnodes($xpath) };
216 if( $@ ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, call to ".'findnodes()'." has failed for this xpath: $xpath"); return undef };
217 foreach my $anode (@nodes){
218 my $resource_id = $anode->getAttribute('resource-id');
219 if( $resource_id =~ /\/workspace$/i ){
220 my $screenname = $anode->getAttribute('content-desc');
221 if( ! defined($screenname) || ($screenname =~ /^\s*$/) ){ $log->error("$anode\n${whoami} (via $parent), line ".__LINE__." : error, above node has the right class (android.widget.TextView) but its text is empty and it should have been the screen-name instead."); return undef }
222 $self->set('screen-name', $screenname);
223 $numframes++;
224 }
225 }
226 if( $numframes != 1 ){ $log->warn($doc."\n${whoami} (via $parent), line ".__LINE__." : error, failed to find exactly one node with XPath=${xpath} but found ${numframes} instead, see above xml.") }
227  
228 # now search for the apps
229 # text=""
230 # resource-id="com.huawei.android.launcher:id/workspace_screen"
231 # class="android.view.ViewGroup"
232 # content-desc=""
233 # and then it has a child of
234 # class="android.widget.TextView"
235 # content-desc=""
236 # resource-id=""
237 # text="<appname>"
238 $xpath = '//node[@text=\'\' and @class=\'android.view.ViewGroup\' and @content-desc=\'\' and contains(@resource-id, \'id/workspace_screen\')]/node[@class=\'android.view.ViewGroup\']/node[@class=\'android.widget.TextView\']';
239 @nodes = eval { $doc->findnodes($xpath) };
240 if( $@ ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, call to ".'findnodes()'." has failed for this xpath: $xpath"); return undef };
241 my %apps = ();
242 foreach my $anode (@nodes){
243 my $class = $anode->getAttribute('class');
244 if( $class =~ /^android.widget.TextView$/ ){
245 # the app-icons space is inside a frame
246 my $appname = $anode->getAttribute('text');
247 if( ! defined($appname) || ($appname =~ /^\s*$/) ){ $log->error("$anode\n${whoami} (via $parent), line ".__LINE__." : error, above node has the right class (android.widget.TextView) but its text is empty and it should have been the app-name instead."); return undef }
248 my $bounds = $anode->getAttribute('bounds');
249 $bounds =~ s/\s+//;
250 if( $bounds =~ /^\[(\d+),(\d+)\]\[(\d+),(\d+)\]$/ ){
251 $apps{$appname} = [$1,$2,$3,$4];
252 } else { $log->error($anode."\n${whoami} (via $parent), line ".__LINE__." : error, above node has invalid bounds/4."); return undef }
253 } else { $log->error($anode."\n${whoami} (via $parent), line ".__LINE__." : error, above node has unexpected 'class', it is not 'android.widget.TextView'."); return undef }
254 }
255 $self->set('apps', \%apps);
256 } # end 'fully' params exists
257  
258 return 0; # success
259 }
260  
261 sub get { return $_[0]->has($_[1]) ? $_[0]->{'data'}->{$_[1]} : undef }
262 sub set {
263 # set a new value even if it is not in our store,
264 # but if it is, then check the types match
265 if( exists($_[0]->{'data'}->{$_[1]})
266 && (ref($_[2]) ne ref($_[0]->{'data'}->{$_[1]}))
267 ){ $_[0]->log()->error(__PACKAGE__."::set(), line ".__LINE__." : error, the type of parameter '$_[1]' is '".ref($_[2])."' but '".ref($_[0]->{'data'}->{$_[1]})."' was expected."); return 1 }
268 $_[0]->{'data'}->{$_[1]} = $_[2];
269 return 0; # success
270 }
271 sub has { exists $_[0]->{'data'}->{$_[1]} }
272  
273 sub toString {
274 # unfortunately as a hash it is unsorted
275 return perl2dump($_[0]->{'data'}, {terse=>1,pretty=>1});
276 }
277 sub toJSON { return perl2json($_[0]->{'data'}, {pretty=>1}); }
278 sub TO_JSON { return $_[0]->{'data'} }
279  
280 sub log { return $_[0]->{'_private'}->{'logger-object'} }
281 sub verbosity { return $_[0]->{'_private'}->{'verbosity'} }
282  
283 # only pod below
284 =pod
285  
286 =head1 NAME
287  
288 Android::ElectricSheep::Automator - The great new Android::ElectricSheep::Automator!
289  
290 =head1 VERSION
291  
292 Version 0.09
293  
294  
295 =head1 SYNOPSIS
296  
297 Quick summary of what the module does.
298  
299 Perhaps a little code snippet.
300  
301 use Android::ElectricSheep::Automator;
302  
303 my $foo = Android::ElectricSheep::Automator->new();
304 ...
305  
306 =head1 EXPORT
307  
308 A list of functions that can be exported. You can delete this section
309 if you don't export anything, such as for a purely object-oriented module.
310  
311 =head1 SUBROUTINES/METHODS
312  
313 =head2 function1
314  
315  
316  
317 =head2 function2
318  
319  
320 =head1 AUTHOR
321  
322 Andreas Hadjiprocopis, C<< <bliako at cpan.org> >>
323  
324 =head1 BUGS
325  
326 Please report any bugs or feature requests to C<bug-android-adb-automator at rt.cpan.org>, or through
327 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Android-ADB-Automator>. I will be notified, and then you'll
328 automatically be notified of progress on your bug as I make changes.
329  
330  
331  
332  
333 =head1 SUPPORT
334  
335 You can find documentation for this module with the perldoc command.
336  
337 perldoc Android::ElectricSheep::Automator
338  
339  
340 You can also look for information at:
341  
342 =over 4
343  
344 =item * RT: CPAN's request tracker (report bugs here)
345  
346 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Android-ADB-Automator>
347  
348 =item * CPAN Ratings
349  
350 L<https://cpanratings.perl.org/d/Android-ADB-Automator>
351  
352 =item * Search CPAN
353  
354 L<https://metacpan.org/release/Android-ADB-Automator>
355  
356 =back
357  
358  
359 =head1 ACKNOWLEDGEMENTS
360  
361  
362 =head1 LICENSE AND COPYRIGHT
363  
364 This software is Copyright (c) 2025 by Andreas Hadjiprocopis.
365  
366 This is free software, licensed under:
367  
368 The Artistic License 2.0 (GPL Compatible)
369  
370  
371 =cut
372  
373 1; # End of Android::ElectricSheep::Automator
« lib/Android/ElectricSheep/Automator/DeviceProperties.pm ↑ Summary lib/Android/ElectricSheep/Automator/Plugins/Apps/Base.pm »