lib/Android/ElectricSheep/Automator/DeviceProperties.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/AppProperties.pm ↑ Summary lib/Android/ElectricSheep/Automator/ScreenLayout.pm »
1 package Android::ElectricSheep::Automator::DeviceProperties;
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 'logger-object' => undef,
28 'verbosity' => 0,
29 'mother' => 0,
30 },
31 'data' => {
32 # corresponds to dumpsys->RawSurfaceWidth,RawSurfaceHeight
33 'w' => 0,
34 'h' => 0,
35 'orientation' => 0,
36 'density' => 0,
37 'density-x' => 0,
38 'density-y' => 0,
39 'serial' => '<na>'
40 }
41 };
42 bless $self => $class;
43  
44 if( exists $params->{'logger-object'} ){ $self->{'_private'}->{'logger-object'} = $params->{'logger-object'} } else { $self->{'_private'}->{'logger-object'} = Mojo::Log->new() }
45 if( exists $params->{'verbosity'} ){ $self->{'_private'}->{'verbosity'} = $params->{'verbosity'} } else { $self->{'_private'}->{'verbosity'} = Mojo::Log->new() }
46 # we now have a log and verbosity
47  
48 my $log = $self->log;
49 my $verbosity = $self->verbosity;
50  
51 # we need a mother object (Android::ElectricSheep::Automator)
52 if( (! exists $params->{'mother'})
53 || (! defined $params->{'mother'})
54 || (! defined $params->{'mother'}->adb())
55 ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, input parameter 'mother' with our parent Android::ElectricSheep::Automator object was not specified."); return undef }
56 $self->{'_private'}->{'mother'} = $params->{'mother'};
57  
58 # caller can specify some initial data to load
59 # else caller must run ->enquire() to enquire the real device
60 if( exists $params->{'data'} ){
61 my $d = $self->{'data'};
62 my $p = $params->{'data'};
63 for my $k (sort keys %$d){
64 if( exists($p->{$k}) && defined($p->{$k}) ){
65 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 }
66 }
67 }
68 }
69  
70 return $self;
71 }
72  
73 # does a adb shell dumpsys and reads various things from it
74 # it may also do a adb shell wm density
75 # returns 0 on success, 1 on failure
76 sub enquire {
77 my ($self, $params) = @_;
78 $params //= {};
79  
80 my $parent = ( caller(1) )[3] || "N/A";
81 my $whoami = ( caller(0) )[3];
82  
83 my $log = $self->log;
84 my $verbosity = $self->verbosity;
85  
86 # first get the serial of the device
87 my @cmd = ('get-serialno');
88 my $res = $self->adb->run(@cmd);
89 if( ! defined $res ){ $log->error(join(" ", @cmd)."\n${whoami} (via $parent), line ".__LINE__." : error, above shell command has failed, got undefined result, most likely shell command did not run at all, this should not be happening."); return 1 }
90 if( $res->[0] != 0 ){ $log->error(join(" ", @cmd)."\n${whoami} (via $parent), line ".__LINE__." : error, above shell command has failed, with:\nsSTDOUT:\n".$res->[1]."\n\nSTDERR:\n".$res->[2]."\nEND."); return 1 }
91 $self->set('serial', $res->[1]=~s/\s*$//gmr); # it has a newline at the end
92  
93 # here we could also save to a file on device and then
94 # fetch it locally. We will do that if there are problems
95 # getting the dump from STDOUT
96 @cmd = ('wm', 'size');
97 $res = $self->adb->shell(@cmd);
98 if( ! defined $res ){ $log->error(join(" ", @cmd)."\n${whoami} (via $parent), line ".__LINE__." : error, above shell command has failed, got undefined result, most likely shell command did not run at all, this should not be happening."); return 1 }
99 if( $res->[0] != 0 ){ $log->error(join(" ", @cmd)."\n${whoami} (via $parent), line ".__LINE__." : error, above shell command has failed, with:\nsSTDOUT:\n".$res->[1]."\n\nSTDERR:\n".$res->[2]."\nEND."); return 1 }
100  
101 if( $res->[1] =~ /Physical size:\s*(\d+)\s*x\s*(\d+)/s ){
102 $self->set('w', $1);
103 $self->set('h', $2);
104 } else { $log->error($res->[1]."\n${whoami} (via $parent), line ".__LINE__." : error, failed to find screen size in above output of command: ".join(' ', @cmd)); return 1 }
105  
106 @cmd = ('wm', 'density');
107 $res = $self->adb->shell(@cmd);
108 if( ! defined $res ){ $log->error(join(" ", @cmd)."\n${whoami} (via $parent), line ".__LINE__." : error, above shell command has failed, got undefined result, most likely shell command did not run at all, this should not be happening."); return 1 }
109 if( $res->[0] != 0 ){ $log->error(join(" ", @cmd)."\n${whoami} (via $parent), line ".__LINE__." : error, above shell command has failed, with:\nsSTDOUT:\n".$res->[1]."\n\nSTDERR:\n".$res->[2]."\nEND."); return 1 }
110  
111 if( $res->[1] =~ /Physical density:\s*(\d+)/s ){
112 $self->set('density', $1);
113 #$self->set('density-x', $2); # no obsolete
114 #$self->set('density-y', $3);
115 } else { $log->error($res->[1]."\n${whoami} (via $parent), line ".__LINE__." : error, failed to find screen size in above output of command: ".join(' ', @cmd)); return 1 }
116  
117 # here we could also save to a file on device and then
118 # fetch it locally. We will do that if there are problems
119 # getting the dump from STDOUT
120 @cmd = qw/dumpsys window displays/;
121 $res = $self->adb->shell(@cmd);
122 if( ! defined $res ){ $log->error(join(" ", @cmd)."\n${whoami} (via $parent), line ".__LINE__." : error, above shell command has failed, got undefined result, most likely shell command did not run at all, this should not be happening."); return 1 }
123 if( $res->[0] != 0 ){ $log->error(join(" ", @cmd)."\n${whoami} (via $parent), line ".__LINE__." : error, above shell command has failed, with:\nsSTDOUT:\n".$res->[1]."\n\nSTDERR:\n".$res->[2]."\nEND."); return 1 }
124  
125 # this can fail as they keep changing all these...
126 if( $res->[1] =~ /DisplayRotation.+?mCurrentRotation=(ROTATION_\d+)/s ){
127 $self->set('orientation', $1);
128 } else { $log->error("DUMPSYS:\n".$res->[1]."\nEND DUMPSYS\n${whoami} (via $parent), line ".__LINE__." : error, failed to find orientation in above dumpsys."); return 1 }
129  
130 return 0; # success
131 }
132  
133 sub get { return $_[0]->has($_[1]) ? $_[0]->{'data'}->{$_[1]} : undef }
134 sub set {
135 # set a new value even if it is not in our store,
136 # but if it is, then check the types match
137 if( exists($_[0]->{'data'}->{$_[1]})
138 && (ref($_[2]) ne ref($_[0]->{'data'}->{$_[1]}))
139 ){ $_[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 }
140 $_[0]->{'data'}->{$_[1]} = $_[2];
141 return 0; # success
142 }
143 sub has { exists $_[0]->{'data'}->{$_[1]} }
144  
145 sub toString {
146 return perl2dump($_[0]->{'data'}, {terse=>1});
147 # my $self = $_[0];
148 # my $ret = "";
149 # for my $k (sort keys %{$self->{'data'}}){
150 # my $v = $self->get($k);
151 # $ret .= $k . '=' . (ref($v)eq'' ? $v : '['.join(',', @$v).']')."\n";
152 # }
153 # return $ret;
154 }
155  
156 sub toJSON { return perl2json($_[0]->{'data'}, {pretty=>1}); }
157 sub TO_JSON { return $_[0]->{'data'} }
158  
159 sub log { return $_[0]->{'_private'}->{'logger-object'} }
160 sub verbosity { return $_[0]->{'_private'}->{'verbosity'} }
161 sub mother { return $_[0]->{'_private'}->{'mother'} }
162 sub adb { return $_[0]->mother->adb }
163  
164 # only pod below
165 =pod
166  
167 =head1 NAME
168  
169 Android::ElectricSheep::Automator - The great new Android::ElectricSheep::Automator!
170  
171 =head1 VERSION
172  
173 Version 0.09
174  
175  
176 =head1 SYNOPSIS
177  
178 Quick summary of what the module does.
179  
180 Perhaps a little code snippet.
181  
182 use Android::ElectricSheep::Automator;
183  
184 my $foo = Android::ElectricSheep::Automator->new();
185 ...
186  
187 =head1 EXPORT
188  
189 A list of functions that can be exported. You can delete this section
190 if you don't export anything, such as for a purely object-oriented module.
191  
192 =head1 SUBROUTINES/METHODS
193  
194 =head2 function1
195  
196  
197  
198 =head2 function2
199  
200  
201 =head1 AUTHOR
202  
203 Andreas Hadjiprocopis, C<< <bliako at cpan.org> >>
204  
205 =head1 BUGS
206  
207 Please report any bugs or feature requests to C<bug-android-adb-automator at rt.cpan.org>, or through
208 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Android-ADB-Automator>. I will be notified, and then you'll
209 automatically be notified of progress on your bug as I make changes.
210  
211  
212  
213  
214 =head1 SUPPORT
215  
216 You can find documentation for this module with the perldoc command.
217  
218 perldoc Android::ElectricSheep::Automator
219  
220  
221 You can also look for information at:
222  
223 =over 4
224  
225 =item * RT: CPAN's request tracker (report bugs here)
226  
227 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Android-ADB-Automator>
228  
229 =item * CPAN Ratings
230  
231 L<https://cpanratings.perl.org/d/Android-ADB-Automator>
232  
233 =item * Search CPAN
234  
235 L<https://metacpan.org/release/Android-ADB-Automator>
236  
237 =back
238  
239  
240 =head1 ACKNOWLEDGEMENTS
241  
242  
243 =head1 LICENSE AND COPYRIGHT
244  
245 This software is Copyright (c) 2025 by Andreas Hadjiprocopis.
246  
247 This is free software, licensed under:
248  
249 The Artistic License 2.0 (GPL Compatible)
250  
251  
252 =cut
253  
254 1; # End of Android::ElectricSheep::Automator
« lib/Android/ElectricSheep/Automator/AppProperties.pm ↑ Summary lib/Android/ElectricSheep/Automator/ScreenLayout.pm »