« lib/Android/ElectricSheep/Automator/Plugins/Apps/Base.pm
↑ Summary
lib/Android/ElectricSheep/Automator/Plugins/Apps/Viber.pm »
| 1 | package Android::ElectricSheep::Automator; | ||
| 2 | |||
| 3 | # see also https://www.reddit.com/r/privacytoolsIO/comments/fit0tr/taking_almost_full_control_of_your_unrooted/ | ||
| 4 | # swipe adb shell input touchscreen swipe 300 1200 100 1200 100 | ||
| 5 | |||
| 6 | 1 | ▶ | use 5.006; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | our $VERSION = '0.09'; | ||
| 11 | |||
| 12 | 1 | ▶ | use Mojo::Log; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | 1 | ▶ | use Config::JSON::Enhanced; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | # it requires v0.002 (which is with my modifications) | ||
| 15 | #use Android::ADB; | ||
| 16 | # issue filed for Android::ADB : | ||
| 17 | # https://rt.cpan.org/Public/Bug/Display.html?id=163391 | ||
| 18 | # until this is resolved I am copying Android::ADB | ||
| 19 | # into my distribution, fixing the issues and renaming it to | ||
| 20 | # Android::ElectricSheep::Automator::ADB | ||
| 21 | # and using that. When the issue is resolved I will go back | ||
| 22 | # using Android::ADB | ||
| 23 | # Credits for Android::ADB (now Android::ElectricSheep::Automator::ADB) | ||
| 24 | # go to Marius Gavrilescu (marius@ieval.ro) | ||
| 25 | # as seen in: | ||
| 26 | # https://metacpan.org/pod/Android::ADB | ||
| 27 | # | ||
| 28 | 1 | ▶ | use Android::ElectricSheep::Automator::ADB; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 29 | 1 | ▶ | use File::Temp qw/tempfile/; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 30 | 1 | ▶ | use File::Basename; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 31 | 1 | ▶ | use File::Spec; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 32 | 1 | ▶ | use File::Path qw/make_path/; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 33 | 1 | ▶ | use Cwd; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 34 | 1 | ▶ | use FindBin; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 35 | 1 | ▶ | use Time::HiRes qw/usleep/; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 36 | 0 | ▶ | use Image::PNG; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 37 | use XML::LibXML; | ||
| 38 | use XML::LibXML::XPathContext; | ||
| 39 | use Text::ParseWords; | ||
| 40 | |||
| 41 | use Data::Roundtrip qw/perl2dump perl2json no-unicode-escape-permanently/; | ||
| 42 | |||
| 43 | use Android::ElectricSheep::Automator::DeviceProperties; | ||
| 44 | use Android::ElectricSheep::Automator::AppProperties; | ||
| 45 | use Android::ElectricSheep::Automator::ScreenLayout; | ||
| 46 | use Android::ElectricSheep::Automator::XMLParsers; | ||
| 47 | |||
| 48 | my $_DEFAULT_CONFIG = <<'EODC'; | ||
| 49 | </* $VERSION = '0.09'; */> | ||
| 50 | </* comments are allowed */> | ||
| 51 | </* and <% vars %> and <% verbatim sections %> */> | ||
| 52 | { | ||
| 53 | "adb" : { | ||
| 54 | "path-to-executable" : "/usr/local/android-sdk/platform-tools/adb" | ||
| 55 | }, | ||
| 56 | "debug" : { | ||
| 57 | "verbosity" : 0, | ||
| 58 | </* cleanup temp files on exit */> | ||
| 59 | "cleanup" : 1 | ||
| 60 | }, | ||
| 61 | "logger" : { | ||
| 62 | </* log to file if you uncomment this */> | ||
| 63 | </* "filename" : "..." */> | ||
| 64 | } | ||
| 65 | </* config for our plugins (each can go to separate file also) */> | ||
| 66 | } | ||
| 67 | EODC | ||
| 68 | |||
| 69 | # NOTE: by default, it assumes that no device is connected | ||
| 70 | # and so it does not enquire about screen size etc on startup | ||
| 71 | # In order to tell it that a device (just one) | ||
| 72 | # is connected to the desktop and that we should connect to | ||
| 73 | # it, | ||
| 74 | # use param 'device-is-connected' => 1 | ||
| 75 | # if there are more than one devices and you want to connect to | ||
| 76 | # one of them, then | ||
| 77 | # use param 'device-serial' => <serial-of-device-to-connect> | ||
| 78 | # or | ||
| 79 | # use param 'device-object' => <device object> | ||
| 80 | # (of type Android::ElectricSheep::Automator::ADB::Device) | ||
| 81 | # or after instantiation with $obj->connect_device(...); | ||
| 82 | # and similarly for disconnect_device() | ||
| 83 | # NOTE: without connecting to a device you can not use | ||
| 84 | # methods which require a connected device, e.g. open_app(), swipe() etc. | ||
| 85 | sub new { | ||
| 86 | my $class = ref($_[0]) || $_[0]; # aka proto | ||
| 87 | my $params = $_[1] // {}; | ||
| 88 | |||
| 89 | my $parent = ( caller(1) )[3] || "N/A"; | ||
| 90 | my $whoami = ( caller(0) )[3]; | ||
| 91 | |||
| 92 | my $self = { | ||
| 93 | '_private' => { | ||
| 94 | 'confighash' => undef, | ||
| 95 | 'configfile' => '', # this should never be undef | ||
| 96 | 'Android::ADB' => undef, | ||
| 97 | 'debug' => { | ||
| 98 | 'verbosity' => 0, | ||
| 99 | 'cleanup' => 1, | ||
| 100 | }, | ||
| 101 | 'log' => { | ||
| 102 | 'logger-object' => undef, | ||
| 103 | 'logfile' => undef | ||
| 104 | }, | ||
| 105 | }, | ||
| 106 | |||
| 107 | # object of type Android::ElectricSheep::Automator::DeviceProperties | ||
| 108 | # if this is undef, then it means caller did not call connect_device() | ||
| 109 | # when caller calls disconnect_device(), this becomes undef again | ||
| 110 | # this is a cheap way to not proceed to device-needed subs, e.g. swipe() | ||
| 111 | # of course we could make an adb query with e.g. adb get-state | ||
| 112 | 'device-properties' => undef, | ||
| 113 | |||
| 114 | # object of type Android::ElectricSheep::Automator::ADB::Device | ||
| 115 | # which is created when we call connect_device() | ||
| 116 | 'device-object' => undef, | ||
| 117 | |||
| 118 | # a hash of installed apps by package name (e.g. android.google.calendar) | ||
| 119 | # the value will be an AppProperties object if it was enquired or undef | ||
| 120 | # if it wasn't. As the addition of apps is done in a lazy way, when | ||
| 121 | # needed, unless specified otherwise. In any event open_app() will add an | ||
| 122 | # AppProperties object if missing to the specified package. | ||
| 123 | 'apps' => {}, | ||
| 124 | |||
| 125 | # legacy, no worries. | ||
| 126 | 'apps-roundabout-way' => undef, | ||
| 127 | }; | ||
| 128 | bless $self => $class; | ||
| 129 | |||
| 130 | # this will read configuration and create confighash, | ||
| 131 | # make logger, verbosity, | ||
| 132 | # instantiate any objects we need here etc. | ||
| 133 | if( $self->init($params) ){ print STDERR __PACKAGE__."${whoami} (via $parent), line ".__LINE__." : error, call to init() has failed.\n"; return undef } | ||
| 134 | |||
| 135 | # Now we have a logger | ||
| 136 | my $log = $self->log(); | ||
| 137 | |||
| 138 | # do module-specific init | ||
| 139 | if( $self->init_module_specific($params) ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, call to init_module_specific() has failed."); return undef } | ||
| 140 | |||
| 141 | # optional params, defaults exist above or in the configfile | ||
| 142 | if( exists($params->{'verbosity'}) && defined($params->{'verbosity'}) ){ $self->verbosity($params->{'verbosity'}) } # later we will call verbosity() | ||
| 143 | if( exists($params->{'cleanup'}) && defined($params->{'cleanup'}) ){ $self->cleanup($params->{'cleanup'}) } | ||
| 144 | else { $self->cleanup($self->confighash->{'debug'}->{'cleanup'}) } | ||
| 145 | |||
| 146 | my $verbosity = $self->verbosity; | ||
| 147 | |||
| 148 | if( $verbosity > 0 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : done, success (verbosity is set to ".$self->verbosity." and cleanup to ".$self->cleanup.").") } | ||
| 149 | |||
| 150 | return $self; | ||
| 151 | } | ||
| 152 | |||
| 153 | # This signals our object that there is at least one device connected | ||
| 154 | # to the desktop which ADB can access and so can we. | ||
| 155 | # set the device by specifying one of | ||
| 156 | # 'serial' : the device's serial | ||
| 157 | # 'device-object' : a Android::ADB::Device object | ||
| 158 | # as returned by any item of $self->adb->devices() | ||
| 159 | # However, if there is ONLY ONE device connected to the desktop, then | ||
| 160 | # you do not need to specify a device, use this method without arguments | ||
| 161 | # | ||
| 162 | # It returns the device object (Android::ADB::Device) on success | ||
| 163 | # or undef on failure | ||
| 164 | sub connect_device { | ||
| 165 | my ($self, $params) = @_; | ||
| 166 | $params //= {}; | ||
| 167 | |||
| 168 | my $parent = ( caller(1) )[3] || "N/A"; | ||
| 169 | my $whoami = ( caller(0) )[3]; | ||
| 170 | |||
| 171 | my $log = $self->log(); | ||
| 172 | my $verbosity = $self->verbosity; | ||
| 173 | |||
| 174 | my ($what_device, $m); | ||
| 175 | |||
| 176 | if( exists($params->{'serial'}) && defined($m=$params->{'serial'}) ){ | ||
| 177 | my $devs = $self->devices(); | ||
| 178 | |||
| 179 | for (@$devs){ | ||
| 180 | if( $_->serial eq $m ){ $what_device = $_; last } | ||
| 181 | } | ||
| 182 | if( ! defined $what_device ){ $log->error(devices_toString($devs)."\n${whoami} (via $parent), line ".__LINE__." : error, there is no device with specified serial '$m', above are all the connected devices."); return undef } | ||
| 183 | } elsif( exists($params->{'device-object'}) && defined($m=$params->{'device-object'}) | ||
| 184 | && (ref($params->{'device-object'})eq'Android::ElectricSheep::Automator::ADB::Device') | ||
| 185 | ){ | ||
| 186 | $what_device = $m | ||
| 187 | } else { | ||
| 188 | # no params means we assume there is exactly 1 device connected to the desktop | ||
| 189 | my $devs = $self->devices(); | ||
| 190 | if( scalar(@$devs) == 1 ){ | ||
| 191 | $what_device = $devs->[0]; | ||
| 192 | } else { $log->error("${whoami} (via $parent), line ".__LINE__." : error, expecting exactly one device connected to the desktop but found ".scalar(@$devs)." instead. In the case of more than one devices connected to the desktop then specify which one you want to target by using parameter 'serial' or 'device-object'. If no devices are connected then connect one to the desktop first."); return undef } | ||
| 193 | } | ||
| 194 | |||
| 195 | # this can die | ||
| 196 | my $res = eval { $self->adb->set_device($what_device) }; | ||
| 197 | if( $@ || ! defined $res ){ $log->error(device_toString($what_device)."\n${whoami} (via $parent), line ".__LINE__." : error, call to ".'adb->set_device()'." has failed for above device."); return undef } | ||
| 198 | |||
| 199 | # and get the device properties of the set device | ||
| 200 | # that method will also set $self->{'device-properties'} to the returned object | ||
| 201 | my $device_properties = $self->find_current_device_properties(); | ||
| 202 | if( ! defined $device_properties ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, call to ".'find_current_device_properties()'." has failed."); return 1; } | ||
| 203 | |||
| 204 | $self->{'device-object'} = $what_device; | ||
| 205 | |||
| 206 | return $what_device; # Android::ADB::Device object | ||
| 207 | } | ||
| 208 | |||
| 209 | sub devices_toString { join("\n", map { device_toString($_) } @{$_[0]}) } | ||
| 210 | sub device_toString { join('/', $_[0]->serial, $_[0]->product, $_[0]->model, $_[0]->device) } | ||
| 211 | |||
| 212 | # returns the device properties object | ||
| 213 | # use $self->device_properties->get('w') to get or ->set('w', 12) to set. | ||
| 214 | sub device_properties { return $_[0]->{'device-properties'} } | ||
| 215 | |||
| 216 | # it dumps the current screen UI as XML and returns that as a scalar string, | ||
| 217 | # optionally saving it to the specified file | ||
| 218 | # It returns undef on failure. | ||
| 219 | # On success, it returns a hash with 2 keys: | ||
| 220 | # 'raw' : contains the raw XML content (as a string) | ||
| 221 | # 'XML::LibXML' : contains an XML::LibXML object with the parsed | ||
| 222 | # XML string, ready to do XPath queries | ||
| 223 | # it needs that connect_device() to have been called prior to this call | ||
| 224 | sub dump_current_screen_ui { | ||
| 225 | my ($self, $params) = @_; | ||
| 226 | |||
| 227 | my $parent = ( caller(1) )[3] || "N/A"; | ||
| 228 | my $whoami = ( caller(0) )[3]; | ||
| 229 | |||
| 230 | my $log = $self->log(); | ||
| 231 | my $verbosity = $self->verbosity; | ||
| 232 | |||
| 233 | if( ! $self->is_device_connected() ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, you need to connect a device to the desktop and ALSO explicitly call ".'device_connected()'." before calling this."); return undef } | ||
| 234 | |||
| 235 | my $filename = exists($params->{'filename'}) && defined($params->{'filename'}) ? $params->{'filename'} : undef; | ||
| 236 | |||
| 237 | my $FH; | ||
| 238 | if( ! defined $filename ){ | ||
| 239 | ($FH, $filename) = tempfile(CLEANUP=>$self->cleanup); | ||
| 240 | close $FH; | ||
| 241 | } | ||
| 242 | # WARNING, you need to wake up the phone before dumping !!!! | ||
| 243 | my $devicefile = File::Spec->catfile('/', 'data', 'local', 'tmp', $$.'.xml'); | ||
| 244 | |||
| 245 | my (@cmd, $res); | ||
| 246 | |||
| 247 | my $maxiters = 7; | ||
| 248 | WMA: | ||
| 249 | while( $maxiters-- > 0 ){ | ||
| 250 | @cmd = ('uiautomator', 'dump', $devicefile); | ||
| 251 | if( $verbosity > 0 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : maxiters $maxiters : sending command to adb: @cmd") } | ||
| 252 | my $res = $self->adb->shell(@cmd); | ||
| 253 | 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 undef } | ||
| 254 | if( $res->[0] != 0 ){ $log->error("--begin result:\n".perl2dump($res)."--end result.\n--begin command:\n".join(" ", @cmd)."\n--end command.\n\n"."${whoami} (via $parent), line ".__LINE__." : error, above shell command has failed, with:\nSTDOUT:\n".$res->[1]."\n\nSTDERR:\n".$res->[2]."\nEND."); return undef } | ||
| 255 | |||
| 256 | # check twice with a sleep if the dump is there, else | ||
| 257 | # we are repeating the previous command to dump the ui | ||
| 258 | for(1..2){ | ||
| 259 | # now check if the dump file is there, sometimes it is not. repeat if not | ||
| 260 | if( $verbosity > 0 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : maxiters $maxiters : in order to find if the uiautomator dump succeeded : sending command to adb: @cmd") } | ||
| 261 | @cmd = ('if', '[', '-f', $devicefile, ']', ';', 'then', 'echo', 'found', ';', 'else', 'echo', 'notfound', ';', 'fi'); | ||
| 262 | $res = $self->adb->shell(@cmd); | ||
| 263 | 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 undef } | ||
| 264 | if( $res->[0] != 0 ){ $log->error("--begin result:\n".perl2dump($res)."--end result.\n--begin command:\n".join(" ", @cmd)."\n--end command.\n\n"."${whoami} (via $parent), line ".__LINE__." : error, above shell command has failed, with:\nSTDOUT:\n".$res->[1]."\n\nSTDERR:\n".$res->[2]."\nEND."); return undef } | ||
| 265 | if( $res->[1] =~ /^found/ ){ | ||
| 266 | if( $verbosity > 0 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : maxiters $maxiters : found the dump file '$devicefile' on device, stopping the loop.") } | ||
| 267 | last WMA | ||
| 268 | } else { if( $verbosity > 0 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : maxiters $maxiters : DID NOT FIND the dump file '$devicefile' on device, continue the loop until iters reach 0.") } } | ||
| 269 | usleep(0.5); | ||
| 270 | } | ||
| 271 | } | ||
| 272 | # This may fail because above dump sometimes fails, no error | ||
| 273 | # just not producing any output file. If this persists we | ||
| 274 | # can try dumping to stdout. | ||
| 275 | |||
| 276 | # pull the output | ||
| 277 | $res = $self->adb->pull($devicefile, $filename); | ||
| 278 | if( ! defined $res ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, failed to pull remote file '$devicefile' into local file '$filename', because undefined was returned, this should not be happening."); return undef } | ||
| 279 | if( $res->[0] != 0 ){ $log->error("--begin result:\n".perl2dump($res)."--end result.\n"."${whoami} (via $parent), line ".__LINE__." : error, failed to pull remote file '$devicefile' into local file '$filename' with:\nSTDOUT:\n".$res->[1]."\n\nSTDERR:\n".$res->[2]."\nEND."); return undef } | ||
| 280 | |||
| 281 | # remove the device file | ||
| 282 | @cmd = ("rm", "-f", $devicefile); | ||
| 283 | if( $verbosity > 0 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : sending command to adb: @cmd") } | ||
| 284 | $res = $self->adb->shell(@cmd); | ||
| 285 | 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 undef } | ||
| 286 | if( $res->[0] != 0 ){ $log->error(join(" ", @cmd)."\n${whoami} (via $parent), line ".__LINE__." : error, above shell command has failed, with:\nSTDOUT:\n".$res->[1]."\n\nSTDERR:\n".$res->[2]."\nEND."); return undef } | ||
| 287 | |||
| 288 | # let's return the string content back | ||
| 289 | my $contents; | ||
| 290 | if( ! open($FH, '<', $filename) ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, failed to open file with dump for reading '$filename', $!"); return undef } | ||
| 291 | { local $/ = undef; $contents = <$FH> } close $FH; | ||
| 292 | |||
| 293 | my $retxmlobj = XML::LibXML->load_xml(string => $contents); | ||
| 294 | if( ! defined $retxmlobj ){ $log->error("${contents}\n${whoami} (via $parent), line ".__LINE__." : error, failed to parse above XML content."); return undef } | ||
| 295 | my $xc = XML::LibXML::XPathContext->new($retxmlobj); | ||
| 296 | $xc->registerFunction('matches', \&Android::ElectricSheep::Automator::XMLParsers::xpath_matches); | ||
| 297 | return { | ||
| 298 | 'raw' => $contents, | ||
| 299 | 'XML::LibXML' => $retxmlobj, | ||
| 300 | 'XML::LibXML::XPathContext' => $xc | ||
| 301 | } | ||
| 302 | } | ||
| 303 | |||
| 304 | # return an ARRAYref of all connected devices. This array can be empty if none is connected | ||
| 305 | # but an arrayref is definetely returned. | ||
| 306 | sub devices { | ||
| 307 | my $self = $_[0]; | ||
| 308 | my @devs = $self->adb->devices(); | ||
| 309 | return \@devs; | ||
| 310 | } | ||
| 311 | |||
| 312 | # It installs an app specified by its APK file | ||
| 313 | # it accepts extra parameters as specified in the | ||
| 314 | # documentation of adb https://developer.android.com/tools/adb | ||
| 315 | # (search for 'install') | ||
| 316 | # Inputs parameters: | ||
| 317 | # 'apk-filename' => this can be a string or an array of strings | ||
| 318 | # containing existing APK (.apk) file(s) to be installed on device. | ||
| 319 | # Sometimes an app consists of many apks, in this case use the ARRAY mode. | ||
| 320 | # 'install-params' => an array of installation parameters in the fashion of when shelling out a @cmd | ||
| 321 | # each parameter and each of its attributes, if any, must be | ||
| 322 | # in a separate item in the array, e.g. ['-r', '-i', 'newpagename'] | ||
| 323 | # it needs that connect_device() to have been called prior to this call | ||
| 324 | # It returns 1 on failure, 0 on success. | ||
| 325 | sub install_app { | ||
| 326 | my ($self, $params) = @_; | ||
| 327 | $params //= {}; | ||
| 328 | |||
| 329 | my $parent = ( caller(1) )[3] || "N/A"; | ||
| 330 | my $whoami = ( caller(0) )[3]; | ||
| 331 | my $log = $self->log(); | ||
| 332 | my $verbosity = $self->verbosity(); | ||
| 333 | |||
| 334 | if( ! $self->is_device_connected() ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, you need to connect a device to the desktop and ALSO explicitly call ".'connect_device()'." before calling this."); return 1 } | ||
| 335 | |||
| 336 | my $apkfilename = exists($params->{'apk-filename'}) ? $params->{'apk-filename'} : undef; | ||
| 337 | if( ! defined $apkfilename ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, input parameter 'apk-filename' is missing."); return 1 } | ||
| 338 | my $installparams = exists($params->{'install-parameters'}) ? $params->{'install-parameters'} : []; | ||
| 339 | |||
| 340 | my @cmd = ('later...'); | ||
| 341 | for (@$installparams){ push @cmd, $_ } | ||
| 342 | if( ref($apkfilename) eq '' ){ | ||
| 343 | # just one apk file | ||
| 344 | push @cmd, $apkfilename; | ||
| 345 | $cmd[0] = 'install'; | ||
| 346 | } elsif( ref($apkfilename) eq 'ARRAY' ){ | ||
| 347 | push @cmd, @$apkfilename; | ||
| 348 | $cmd[0] = 'install-multiple'; | ||
| 349 | } else { $log->error("${whoami} (via $parent), line ".__LINE__." : error, input parameter 'apk-filename' must either be a string scalar or an ARRAY of strings. Instead it is '".ref($apkfilename)."'."); return 1 } | ||
| 350 | if( $verbosity > 0 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : sending command to adb: @cmd") } | ||
| 351 | my $res = $self->adb->run(@cmd); | ||
| 352 | if( ! defined $res ){ $log->error(join(" ", @cmd)."\n${whoami} (via $parent), line ".__LINE__." : error, above adb command has failed, got undefined result, most likely adb command did not run at all, this should not be happening."); return 1 } | ||
| 353 | if( $res->[0] != 0 ){ $log->error(join(" ", @cmd)."\n${whoami} (via $parent), line ".__LINE__." : error, above adb command has failed, with:\nSTDOUT:\n".$res->[1]."\n\nSTDERR:\n".$res->[2]."\nEND."); return 1 } | ||
| 354 | if( $res->[1] !~ /\bSuccess$/s ){ $log->error(join(" ", @cmd)."\n${whoami} (via $parent), line ".__LINE__." : error, above adb command has failed, with:\nSTDOUT:\n".$res->[1]."\n\nSTDERR:\n".$res->[2]."\nEND."); return 1 } | ||
| 355 | return 0; # success | ||
| 356 | } | ||
| 357 | |||
| 358 | # It extracts the specified application from the device | ||
| 359 | # in the form of APK file. | ||
| 360 | # Inputs parameters: | ||
| 361 | # 'output-dir' => directory where we save the APK(s) | ||
| 362 | # these are same as those for search_app(): | ||
| 363 | # 'package' => required package name as a SCALAR (for an exact search) | ||
| 364 | # or a regex (qr//) object for regex search including case-insensitive. | ||
| 365 | # 'force-reload-apps-list' => 0,1 : optionally call find_installed_apps() if > 0 | ||
| 366 | # but restricted only to the packages match 'package' NOT ALL. | ||
| 367 | # 'lazy' => 0,1 : pass this lazy value to the find_installed_apps() | ||
| 368 | # and be lazy (i.e. without enquiring on each app's specifics | ||
| 369 | # and creating an AppProperties object) if ==1 | ||
| 370 | # or not be lazy if ==0 ... | ||
| 371 | # ... (which means an AppProperties object is created for EACH package in the device) | ||
| 372 | # Default is force-reload-apps-list=>0 | ||
| 373 | # it needs that connect_device() to have been called prior to this call | ||
| 374 | # It returns a hash whose keys are package-names it extracted their apk(s) | ||
| 375 | # and value is an array of 0, 1 or more apk(s) related to the package and which | ||
| 376 | # are extracted. Each item in that array is a hash with fields 'local-name' | ||
| 377 | # (the apk saved locally) and 'device-name' (the path of the apk on the device) | ||
| 378 | sub pull_app_apk_from_device { | ||
| 379 | my ($self, $params) = @_; | ||
| 380 | $params //= {}; | ||
| 381 | |||
| 382 | my $parent = ( caller(1) )[3] || "N/A"; | ||
| 383 | my $whoami = ( caller(0) )[3]; | ||
| 384 | my $log = $self->log(); | ||
| 385 | my $verbosity = $self->verbosity(); | ||
| 386 | |||
| 387 | if( ! $self->is_device_connected() ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, you need to connect a device to the desktop and ALSO explicitly call ".'connect_device()'." before calling this."); return undef } | ||
| 388 | |||
| 389 | my $outdir = exists($params->{'output-dir'}) ? $params->{'output-dir'} : undef; | ||
| 390 | if( ! defined $outdir ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, input parameter 'output-dir' is missing."); return undef } | ||
| 391 | |||
| 392 | # this can be regex or absolute string, like what search_apps() takes | ||
| 393 | my $package = exists($params->{'package'}) ? $params->{'package'} : undef; | ||
| 394 | if( ! defined $package ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, input parameter 'package' is missing."); return undef } | ||
| 395 | |||
| 396 | my $sparams = { | ||
| 397 | 'package' => $package, | ||
| 398 | }; | ||
| 399 | for ('lazy', 'force-reload-apps-list'){ | ||
| 400 | if( exists $params->{$_} ){ $sparams->{$_} = $params->{$_} } | ||
| 401 | } | ||
| 402 | my $searchres = $self->search_app($sparams); | ||
| 403 | if( ! defined $searchres ){ $log->error(perl2dump($sparams)."${whoami} (via $parent), line ".__LINE__." : error, call to ".'search_app()'." has failed with above parameters."); return undef } | ||
| 404 | |||
| 405 | # now we should have 1 or more items in the hash for the matched apps | ||
| 406 | if( ! -d $outdir ){ File::Path::make_path($outdir); if( ! -d $outdir ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, failed to create output dir '$outdir', or it is not a dir."); return undef } } | ||
| 407 | my %ret; | ||
| 408 | for my $packagename (sort keys %$searchres){ | ||
| 409 | my $appobj = $searchres->{$packagename}; | ||
| 410 | my $apks = $appobj->get('apkPaths'); | ||
| 411 | my $codePath = $appobj->get('codePath'); | ||
| 412 | next unless defined $apks; # actually it can not be undef, the worst it can be is empty, [] | ||
| 413 | my @arr; | ||
| 414 | for my $apkfilename (@$apks){ | ||
| 415 | my $inbasename = File::Basename::basename($apkfilename); | ||
| 416 | my $outfile = File::Spec->catfile($outdir, $inbasename); | ||
| 417 | my $res = $self->adb->pull($apkfilename, $outfile); | ||
| 418 | if( ! defined $res ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, failed to pull remote file '$apkfilename' into local file '$outfile', because undefined was returned, this should not be happening."); return undef } | ||
| 419 | if( $res->[0] != 0 ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, failed to pull remote file '$apkfilename' into local file '$outfile' with:\nSTDOUT:\n".$res->[1]."\n\nSTDERR:\n".$res->[2]."\nEND."); return undef } | ||
| 420 | # it must be in outdir now... | ||
| 421 | if( $verbosity > 0 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : extracted apk '$apkfilename' of package '$packagename' into '$outfile'.") } | ||
| 422 | push @arr, { | ||
| 423 | 'device-path' => $apkfilename, | ||
| 424 | 'local-path' => $outfile | ||
| 425 | }; | ||
| 426 | } | ||
| 427 | $ret{$packagename} = \@arr; | ||
| 428 | # each package name has an array of file specs as hash | ||
| 429 | } | ||
| 430 | return \%ret; # success | ||
| 431 | } | ||
| 432 | |||
| 433 | # It returns 0 on success, 1 on failure. | ||
| 434 | # It swipes the screen as per the 'direction' | ||
| 435 | # or full spec (x1,y1,x2,y2) (e.g. from x1 to x2 etc.) | ||
| 436 | # optional parameter 'dt' is the milliseconds to take for the swipe | ||
| 437 | # small is fast. Some speed is needed for certain gestures, so | ||
| 438 | # this is important parameter. E.g. for swiping to another screen. | ||
| 439 | # it needs that connect_device() to have been called prior to this call | ||
| 440 | sub swipe { | ||
| 441 | my ($self, $params) = @_; | ||
| 442 | $params //= {}; | ||
| 443 | |||
| 444 | my $parent = ( caller(1) )[3] || "N/A"; | ||
| 445 | my $whoami = ( caller(0) )[3]; | ||
| 446 | my $log = $self->log(); | ||
| 447 | my $verbosity = $self->verbosity(); | ||
| 448 | |||
| 449 | if( ! $self->is_device_connected() ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, you need to connect a device to the desktop and ALSO explicitly call ".'connect_device()'." before calling this."); return 1 } | ||
| 450 | |||
| 451 | my $w = $self->device_properties->get('w'); | ||
| 452 | my $h = $self->device_properties->get('h'); | ||
| 453 | |||
| 454 | my @fullspec; | ||
| 455 | for ('x1', 'y1', 'x2', 'y2', 'dt'){ | ||
| 456 | if( ! exists($params->{$_}) || ! defined($params->{$_}) ){ last } | ||
| 457 | push @fullspec, $params->{$_}; | ||
| 458 | } | ||
| 459 | my @cmd; | ||
| 460 | if( scalar(@fullspec) == 4 ){ | ||
| 461 | if( ($params->{'x1'} < 0) || ($params->{'x1'} > $w) ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, input parameter 'x1' has a value (".$params->{'x1'}.") which is out of bounds."); return 1 } | ||
| 462 | if( ($params->{'y1'} < 0) || ($params->{'y1'} > $h) ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, input parameter 'y1' has a value (".$params->{'y1'}.") which is out of bounds."); return 1 } | ||
| 463 | if( ($params->{'x2'} < 0) || ($params->{'x2'} > $w) ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, input parameter 'x2' has a value (".$params->{'x2'}.") which is out of bounds."); return 1 } | ||
| 464 | if( ($params->{'y2'} < 0) || ($params->{'y2'} > $h) ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, input parameter 'y2' has a value (".$params->{'y2'}.") which is out of bounds."); return 1 } | ||
| 465 | @cmd = ('input', 'touchscreen', 'swipe', @fullspec); | ||
| 466 | } else { | ||
| 467 | # the time to do the move in milliseconds, there is a default of 100 | ||
| 468 | # which is enough to swipe to next screen | ||
| 469 | my $dt = (exists($params->{'dt'}) && defined($params->{'dt'})) ? $params->{'dt'} : 100; | ||
| 470 | my $direction = (exists($params->{'direction'}) && defined($params->{'direction'})) ? $params->{'direction'} : undef; | ||
| 471 | if( ! defined $direction ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, input parameter 'direction' was not specified."); return 1 } | ||
| 472 | |||
| 473 | my ($x1, $y1, $x2, $y2); | ||
| 474 | |||
| 475 | if( ($direction =~ /^l(eft)?$/i) || ($direction =~ /^r(ight)?$/i) ){ | ||
| 476 | # horizontal | ||
| 477 | $y1 = $y2 = int(3*$h/4); | ||
| 478 | } else { | ||
| 479 | # vertical | ||
| 480 | $x1 = $x2 = int($w/2); | ||
| 481 | } | ||
| 482 | if( ($direction =~ /^r(ight)?$/i) ){ | ||
| 483 | $x1 = int(0.2*$w); | ||
| 484 | $x2 = $w - $x1; | ||
| 485 | } | ||
| 486 | if( ($direction =~ /^l(eft)?$/i) ){ | ||
| 487 | $x2 = int(0.2*$w); | ||
| 488 | $x1 = $w - $x2; | ||
| 489 | } | ||
| 490 | if( ($direction =~ /^d(own)?$/i) ){ | ||
| 491 | $y1 = int(0.2*$h); | ||
| 492 | $y2 = $h - $y1; | ||
| 493 | } | ||
| 494 | if( ($direction =~ /^u(p)?$/i) ){ | ||
| 495 | $y2 = int(0.2*$h); | ||
| 496 | $y1 = $h - $y2; | ||
| 497 | } | ||
| 498 | @cmd = ('input', 'touchscreen', 'swipe', $x1, $y1, $x2, $y2, $dt); | ||
| 499 | } | ||
| 500 | |||
| 501 | # unfortunately Android::ADB uses IPC::Open2::open2() to run the adb shell command | ||
| 502 | # which does not capture STDERR, and so either they use open3 or we capture stderr thusly: | ||
| 503 | |||
| 504 | if( $verbosity > 0 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : sending command to adb: @cmd") } | ||
| 505 | my $res = $self->adb->shell(@cmd); | ||
| 506 | 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 undef } | ||
| 507 | if( $res->[0] != 0 ){ $log->error(join(" ", @cmd)."\n${whoami} (via $parent), line ".__LINE__." : error, above shell command has failed, with:\nSTDOUT:\n".$res->[1]."\n\nSTDERR:\n".$res->[2]."\nEND."); return undef } | ||
| 508 | |||
| 509 | return 0; # success | ||
| 510 | } | ||
| 511 | |||
| 512 | # It finds all installed apps on device and appends with | ||
| 513 | # earlier results in $self->apps, and returns them as a hash | ||
| 514 | # keyed on appname. It also saves the returned hash into $self->apps | ||
| 515 | # optionally erasing previous entries (if 'make-fresh-apps-list'==1) | ||
| 516 | # The big question is whether to enquire the device for each package (app) | ||
| 517 | # installed and create an AppProperties object for each or not. | ||
| 518 | # The former is expensive and so you can do it on if-and-when-needed-basis | ||
| 519 | # (lazily). Or you can do it all at once here, it takes like 10-20 seconds. | ||
| 520 | # In any way, lazy or not, at the end the $self->apps HASH will be filled | ||
| 521 | # with the names of all packages installed on device (key). | ||
| 522 | # If 'lazy'==0, then for each installed app the device will be enquired | ||
| 523 | # and an AppProperties object will be created (it contains MainActivity, | ||
| 524 | # Permissions, etc.) and set as the value in the apps HASH. | ||
| 525 | # If 'packages' is specified and 'lazy'==0, then only those packages | ||
| 526 | # in 'packages' will be enquired, the $self->apps list will be | ||
| 527 | # refreshed with all the package names (key) but only some of them will | ||
| 528 | # contain an AppProperties object as the value, the other values | ||
| 529 | # will be undef. | ||
| 530 | # If no 'packages' was specified and 'lazy'==0, then all | ||
| 531 | # packages installed on device will be enquired and an AppProperties object | ||
| 532 | # will be associated with each of them in $self->apps. | ||
| 533 | # Default 'lazy' value is 1. | ||
| 534 | # Input: | ||
| 535 | # 'packages' => 'packagename' or regex (e.g. qr//) or [...] or { ...} : | ||
| 536 | # optionally specify a list of package names to find ONLY, | ||
| 537 | # default is to find ALL apps, which can be expensive if lazy>0 | ||
| 538 | # the 'packages' items can be a scalar string for exact match | ||
| 539 | # or a compiled regex (qr//) | ||
| 540 | # 'force-reload-apps-list' => 0 or 1 : if 1, erase current list and start afresh | ||
| 541 | # 'lazy' => 1 or 0 : optionally specify not to enquire each package in detail, | ||
| 542 | # i.e. creating an AppProperties object for each package, | ||
| 543 | # but just add the package name as a key to the hash, | ||
| 544 | # leaving the value undef. This value will be | ||
| 545 | # created when needed, e.g. in an open_app() call. | ||
| 546 | # Default is to be lazy, 1. | ||
| 547 | # It needs that connect_device() to have been called prior to this call | ||
| 548 | # | ||
| 549 | # On failure it returns undef. | ||
| 550 | # On success it saves the results in $self->apps HASH and returns that. | ||
| 551 | # | ||
| 552 | # NOTE: enquiring installed apps entails this: | ||
| 553 | # 1. adb shell dumpsys package packages | ||
| 554 | # which is a general output of installed packages but does not contain | ||
| 555 | # activity information. | ||
| 556 | # 2. For a given installed app name (package) we enquire by | ||
| 557 | # adb shell dumpsys package com.example.myapp | ||
| 558 | # WARNING: the 2nd step if called for all the installed apps can take some | ||
| 559 | # time as it is done for each installed app. | ||
| 560 | # Perhaps a better approach is to lazily find apps by | ||
| 561 | # specifying the name of the app with the input parameter | ||
| 562 | # 'packages' => [...] | ||
| 563 | # This will append to apps() the result. | ||
| 564 | sub find_installed_apps { | ||
| 565 | my ($self, $params) = @_; | ||
| 566 | $params //= {}; | ||
| 567 | |||
| 568 | my $parent = ( caller(1) )[3] || "N/A"; | ||
| 569 | my $whoami = ( caller(0) )[3]; | ||
| 570 | my $log = $self->log(); | ||
| 571 | my $verbosity = $self->verbosity(); | ||
| 572 | |||
| 573 | if( ! $self->is_device_connected() ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, you need to connect a device to the desktop and ALSO explicitly call ".'connect_device()'." before calling this."); return undef } | ||
| 574 | |||
| 575 | # optionally caller can specify a list of app names to enquire | ||
| 576 | # either as exact package name (string), a Regexp object (qr//) | ||
| 577 | # an ARRAY of package names or a HASH of package names: | ||
| 578 | my $packages = exists($params->{'packages'}) && defined($params->{'packages'}) ? $params->{'packages'} : undef; | ||
| 579 | my $rr = ref $packages; | ||
| 580 | if( ($rr ne '')&&($rr ne 'Regexp')&&($rr ne 'ARRAY')&&($rr ne 'HASH') ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, the type of input parameter 'packages' must be one of scalar string, Regexp, ARRAY or HASH and not '$rr'."); return undef } | ||
| 581 | |||
| 582 | # NOTE: that all those 'packages', if any, | ||
| 583 | # will be non-lazily even if 'lazy' is 1 | ||
| 584 | # omit 'packages' and you will have all packages according to the 'lazy' param | ||
| 585 | # Also even if 'packages' is specified, the list of ALL apps will | ||
| 586 | # be saved in the hash, except that those not in 'packages' will have | ||
| 587 | # undef value and the others will have an AppProperties object value. | ||
| 588 | my $epars = { | ||
| 589 | 'mother' => $self, | ||
| 590 | ('packages' => $params->{'packages'})x!!(exists($params->{'packages'}) && defined($params->{'packages'})), | ||
| 591 | ('lazy' => $params->{'lazy'})x!!(exists($params->{'lazy'}) && defined($params->{'lazy'})), | ||
| 592 | }; | ||
| 593 | my $apps = Android::ElectricSheep::Automator::AppProperties::enquire_installed_apps_factory($epars); | ||
| 594 | if( ! defined $apps ){ $epars->{'mother'} = '<redacted>'; $log->error(perl2dump($epars)."${whoami} (via $parent), line ".__LINE__." : error, failed to find all installed apps, call to ".'Android::ElectricSheep::Automator::AppProperties::enquire_installed_apps_factory()'." has failed."); return undef } | ||
| 595 | |||
| 596 | # erase the current list before adding? | ||
| 597 | $self->{'apps'} = {} if exists($params->{'make-fresh-apps-list'}) | ||
| 598 | && defined($params->{'make-fresh-apps-list'}) | ||
| 599 | && ($params->{'make-fresh-apps-list'}>0) | ||
| 600 | ; | ||
| 601 | |||
| 602 | # we will append to our apps list, which could have just been emptied | ||
| 603 | # but if there is a key with AppProperties value | ||
| 604 | # (and now it has undef) then we will keep that value instead of undef: | ||
| 605 | for my $k (keys %$apps){ | ||
| 606 | next if exists($self->{'apps'}->{$k}) | ||
| 607 | && defined($self->{'apps'}->{$k}) | ||
| 608 | ; | ||
| 609 | $self->{'apps'}->{$k} = $apps->{$k}; | ||
| 610 | } | ||
| 611 | #my @k = keys %$apps; @{ $self->{'apps'} }{@k} = @{ $apps }{@k}; | ||
| 612 | |||
| 613 | return $self->apps; # success | ||
| 614 | } | ||
| 615 | |||
| 616 | # searches the current list of installed apps in $self->{'apps'} | ||
| 617 | # for the input app name or name regex. | ||
| 618 | # Note that a call to find_installed_apps() will populate | ||
| 619 | # the $self->{'apps'} with the keys (package names) | ||
| 620 | # but the expensive query for each and every key (package name) | ||
| 621 | # will be done lazily on a if-and-when-needed-basis | ||
| 622 | # but ALL the package names exist in $self->apps. | ||
| 623 | |||
| 624 | # Inputs parameters: | ||
| 625 | # 'package' => required package name as a SCALAR (for an exact search) | ||
| 626 | # or a regex (qr//) object for regex search including case-insensitive. | ||
| 627 | # 'force-reload-apps-list' => 0,1 : optionally call find_installed_apps() if > 0 | ||
| 628 | # but restricted only to the packages match 'package' NOT ALL. | ||
| 629 | # 'lazy' => 0,1 : pass this lazy value to the find_installed_apps() | ||
| 630 | # and be lazy (i.e. without enquiring on each app's specifics | ||
| 631 | # and creating an AppProperties object) if ==1 | ||
| 632 | # or not be lazy if ==0 ... | ||
| 633 | # ... (which means an AppProperties object is created for EACH package in the device) | ||
| 634 | # Default is force-reload-apps-list=>0 | ||
| 635 | # It returns the found packages (full package name (which is a key in $self->{'apps'})) | ||
| 636 | # as a HASHref if any found, or {} if none found. | ||
| 637 | # the key to the returned hash is the full package name and the value | ||
| 638 | # will be AppProperties object or undef if not instantiated for that app | ||
| 639 | # because of lazy=1 | ||
| 640 | # It returns undef on failure. | ||
| 641 | sub search_app { | ||
| 642 | my ($self, $params) = @_; | ||
| 643 | $params //= {}; | ||
| 644 | |||
| 645 | my $parent = ( caller(1) )[3] || "N/A"; | ||
| 646 | my $whoami = ( caller(0) )[3]; | ||
| 647 | my $log = $self->log(); | ||
| 648 | my $verbosity = $self->verbosity(); | ||
| 649 | |||
| 650 | my $package; | ||
| 651 | if( ! exists($params->{'package'}) || ! defined($package=$params->{'package'}) ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, input parameter 'package' was not specified, it must be a package name or a compiled regex (e.g. via ".'qr//'.") for searching a package name in the list of installed apps."); return undef } | ||
| 652 | if( (ref($package)ne'') && (ref($package)ne'Regexp') ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, the type of input parameter 'package' must be a scalar string (the package name) or a Regexp object (compiled regex via ".'qr//'.")."); return undef } | ||
| 653 | |||
| 654 | my $force_reload = (exists($params->{'force-reload-apps-list'}) && defined($params->{'force-reload-apps-list'})) ? $params->{'force-reload-apps-list'} : 0; | ||
| 655 | my $lazy = (exists($params->{'lazy'}) && defined($params->{'lazy'})) ? $params->{'lazy'} : 1; | ||
| 656 | |||
| 657 | my $apps = $self->apps(); | ||
| 658 | # if we have no apps list or we have a force reload, then | ||
| 659 | if( (0 == scalar(keys %$apps)) | ||
| 660 | || ($force_reload>0) | ||
| 661 | ){ | ||
| 662 | my $fpars = { | ||
| 663 | 'packages' => $package, | ||
| 664 | 'force-reload-apps-list' => $force_reload, | ||
| 665 | 'lazy' => $lazy | ||
| 666 | }; | ||
| 667 | if( ! defined($apps=$self->find_installed_apps($fpars)) ){ $log->error(perl2dump($fpars)."${whoami} (via $parent), line ".__LINE__." : error, failed to load list of installed apps, call to ".'find_installed_apps()'." has failed with above parameters."); return undef } | ||
| 668 | if( 0 == scalar(keys %$apps) ){ | ||
| 669 | $log->error("${whoami} (via $parent), line ".__LINE__." : error, there are no installed apps, even after enquiring them. The target device has no apps installed. Weird."); | ||
| 670 | return undef | ||
| 671 | } | ||
| 672 | } | ||
| 673 | |||
| 674 | # We are returning a hash whose keys are package-names | ||
| 675 | # and values are AppProperties objects | ||
| 676 | # if not a regex for package-name then this hash will contain | ||
| 677 | # 1 item only, otherwise 0, 1 or more. | ||
| 678 | |||
| 679 | # we have a package name, so return it, if found | ||
| 680 | if( ref($package) eq '' ){ | ||
| 681 | # a scalar string : exact match of a package name | ||
| 682 | return exists($apps->{$package}) | ||
| 683 | ? { $package => $apps->{$package} } | ||
| 684 | : {} | ||
| 685 | ; | ||
| 686 | } | ||
| 687 | |||
| 688 | # otherwise, we have a regex contained in package name, | ||
| 689 | # return those matched | ||
| 690 | return { map { $_ => $apps->{$_} } | ||
| 691 | grep { $_ =~ $package } | ||
| 692 | sort keys %$apps | ||
| 693 | }; | ||
| 694 | } | ||
| 695 | |||
| 696 | # It searches the process table (using pidof) for the | ||
| 697 | # specified app name to check if it is running. | ||
| 698 | # Inputs parameters: | ||
| 699 | # 'appname' => an exact app name to find if it is running | ||
| 700 | # It returns 1 if the specified app is running, 0 if it is not | ||
| 701 | # It returns undef on failure. | ||
| 702 | sub is_app_running { | ||
| 703 | my ($self, $params) = @_; | ||
| 704 | $params //= {}; | ||
| 705 | |||
| 706 | my $parent = ( caller(1) )[3] || "N/A"; | ||
| 707 | my $whoami = ( caller(0) )[3]; | ||
| 708 | my $log = $self->log(); | ||
| 709 | my $verbosity = $self->verbosity(); | ||
| 710 | |||
| 711 | my $appname; | ||
| 712 | if( ! exists($params->{'appname'}) || ! defined($appname=$params->{'appname'}) ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, input parameter 'appname' was not specified. It can be an exact app name or an extended regular expression which Android's pgrep understands."); return undef } | ||
| 713 | |||
| 714 | my $res = $self->pidof({'name' => $appname}); | ||
| 715 | if( ! defined $res ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, call to ".'pidof()'." has failed."); return undef } | ||
| 716 | |||
| 717 | # it returns undef on failure | ||
| 718 | # it returns -1 if it is not found in the process table | ||
| 719 | # it retuns >0 as the pid of the running app | ||
| 720 | #if( $res == -1 ){ return 0 } # no it is not running | ||
| 721 | if( $res =~ /^\d+$/ ){ return 1 } # yes it is running | ||
| 722 | return 0; # no it is not running | ||
| 723 | } | ||
| 724 | |||
| 725 | # Inputs parameters: | ||
| 726 | # 'package' : required package name as a SCALAR (for an exact search) | ||
| 727 | # or a regex (qr//) object for regex search including case-insensitive. | ||
| 728 | # 'activity' : optional activity name to start additionally to the app/package name. | ||
| 729 | # If not present, we will try to find the MAIN activities of the package via | ||
| 730 | # AppProperties. There could be several MAIN activities and there are heuristics | ||
| 731 | # to pick one. See AppProperties::enquire(). | ||
| 732 | # The spec must yield exactly 1 match, it will complain if more than 1 matches found. | ||
| 733 | # 'force-reload-apps-list' => 0,1 : optionally call find_installed_apps() if > 0 | ||
| 734 | # but restricted only to the packages match 'package' NOT ALL. | ||
| 735 | # 'lazy' => 0,1 : pass this lazy value to the find_installed_apps() | ||
| 736 | # and be lazy (i.e. without enquiring on each app's specifics | ||
| 737 | # and creating an AppProperties object) if ==1 | ||
| 738 | # or not be lazy if ==0 ... | ||
| 739 | # ... (which means an AppProperties object is created for each found package) | ||
| 740 | # Default is force-reload-apps-list=>0 | ||
| 741 | # THIS APPLIES ONLY TO THE MATCHED 'package' | ||
| 742 | # On success it returns a hash of {appname => appproperties} of the opened app | ||
| 743 | # (which will be created if not existing). It may return {} if no match | ||
| 744 | # was found for the specified 'package' name. | ||
| 745 | # On failure it returns undef | ||
| 746 | # it needs that connect_device() to have been called prior to this call | ||
| 747 | sub open_app { | ||
| 748 | my ($self, $params) = @_; | ||
| 749 | $params //= {}; | ||
| 750 | |||
| 751 | my $parent = ( caller(1) )[3] || "N/A"; | ||
| 752 | my $whoami = ( caller(0) )[3]; | ||
| 753 | my $log = $self->log(); | ||
| 754 | my $verbosity = $self->verbosity(); | ||
| 755 | |||
| 756 | if( ! $self->is_device_connected() ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, you need to connect a device to the desktop and ALSO explicitly call ".'connect_device()'." before calling this."); return undef } | ||
| 757 | |||
| 758 | my ($package); | ||
| 759 | if( ! exists($params->{'package'}) || ! defined($package=$params->{'package'}) ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, input parameter 'package' is required."); return undef } | ||
| 760 | if( (ref($package)ne'') && (ref($package)ne'Regexp') ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, the type of input parameter 'package' must be a scalar string (the package name) or a Regexp object (compiled regex via ".'qr//'.")."); return undef } | ||
| 761 | |||
| 762 | # optional activity, else we will see if we find one | ||
| 763 | my $activity = (exists($params->{'activity'}) && defined($params->{'activity'})) ? $params->{'activity'} : undef; | ||
| 764 | |||
| 765 | my $force_reload = (exists($params->{'force-reload-apps-list'}) && defined($params->{'force-reload-apps-list'})) ? $params->{'force-reload-apps-list'} : 0; | ||
| 766 | my $lazy = (exists($params->{'lazy'}) && defined($params->{'lazy'})) ? $params->{'lazy'} : 1; | ||
| 767 | |||
| 768 | my $apps = $self->apps(); | ||
| 769 | if( (0 == scalar(keys %$apps)) | ||
| 770 | || ($force_reload>0) | ||
| 771 | ){ | ||
| 772 | my $fpars = { | ||
| 773 | 'packages' => $package, | ||
| 774 | 'force-reload-apps-list' => $force_reload, | ||
| 775 | 'lazy' => $lazy | ||
| 776 | }; | ||
| 777 | if( ! defined($apps=$self->find_installed_apps($fpars)) ){ $log->error(perl2dump($fpars)."${whoami} (via $parent), line ".__LINE__." : error, failed to load list of installed apps, call to ".'find_installed_apps()'." has failed with above parameters."); return undef } | ||
| 778 | if( 0 == scalar(keys %$apps) ){ | ||
| 779 | $log->error("${whoami} (via $parent), line ".__LINE__." : error, there are no installed apps, even after enquiring them. The target device has no apps installed. Weird."); | ||
| 780 | return undef | ||
| 781 | } | ||
| 782 | } | ||
| 783 | |||
| 784 | # by now we are sure we have the list of installed apps updated | ||
| 785 | # but it is likely that there is no AppProperties object for each | ||
| 786 | # app in the list, but we need it, so make a search and if | ||
| 787 | # AppProperties is undef, then we need to call find_installed_apps() again. | ||
| 788 | my $searchres = $self->search_app({ | ||
| 789 | 'package' => $package, | ||
| 790 | 'force-reload-apps-list' => 0, | ||
| 791 | }); | ||
| 792 | if( ! defined $searchres ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, call to ".'search_app()'." has failed for this search term (package) : ${package}"); return undef } | ||
| 793 | my $num_searchres = scalar keys %$searchres; | ||
| 794 | if( $num_searchres == 0 ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, no app was found for this search term (package) : ${package}"); return undef } | ||
| 795 | elsif( $num_searchres > 1 ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, more than one app was found for this search term (package) : ${package} . Apps found: '".join("', '", sort keys %$searchres)."'."); return undef } | ||
| 796 | # only 1 tupple in the hash, get it: | ||
| 797 | my ($found_app_name, $found_app_properties) = %$searchres; | ||
| 798 | |||
| 799 | if( $verbosity > 0 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : app to open has been matched to '${found_app_name}'.") } | ||
| 800 | |||
| 801 | if( ! defined $found_app_properties ){ | ||
| 802 | # the app is there in the list but it does not have AppProperties yet. | ||
| 803 | # So get just this one package and non-lazily because we need the AppProperties object: | ||
| 804 | my $fpars = { | ||
| 805 | 'force-reload-apps-list' => 0, | ||
| 806 | 'lazy' => 1, # << lazy for all other packages except our 'package' | ||
| 807 | 'packages' => $found_app_name, | ||
| 808 | }; | ||
| 809 | if( ! defined $self->find_installed_apps($fpars) ){ $log->error(perl2dump($fpars)."${whoami} (via $parent), line ".__LINE__." : error, failed to load list of installed apps, call to ".'find_installed_apps()'." has failed with above parameters."); return undef } | ||
| 810 | $apps = $self->apps(); | ||
| 811 | $found_app_properties = $apps->{$found_app_name}; | ||
| 812 | } | ||
| 813 | |||
| 814 | if( $verbosity > 0 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : opening app '".$found_app_properties->get('packageName')."' ...") } | ||
| 815 | |||
| 816 | my ($MainActivity, $fact, @cmd); | ||
| 817 | if( ! defined($MainActivity=$found_app_properties->get('MainActivity')) | ||
| 818 | || ! exists($MainActivity->{'name-fully-qualified'}) | ||
| 819 | || ! defined($fact=$MainActivity->{'name-fully-qualified'}) | ||
| 820 | ){ | ||
| 821 | $log->warn("${whoami} (via $parent), line ".__LINE__." : error, above app (package '${package}') does not contain a 'MainActivity' entry. Launching without it ..."); | ||
| 822 | $fact = $found_app_properties->get('packageName'); | ||
| 823 | @cmd = ('am', 'start', $fact); | ||
| 824 | } else { | ||
| 825 | @cmd = ('am', 'start', '-n', $fact); | ||
| 826 | } | ||
| 827 | |||
| 828 | # open it | ||
| 829 | if( $verbosity > 0 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : sending command to adb: @cmd") } | ||
| 830 | my $res = $self->adb->shell(@cmd); | ||
| 831 | 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 undef } | ||
| 832 | if( $res->[0] != 0 ){ $log->error(join(" ", @cmd)."\n${whoami} (via $parent), line ".__LINE__." : error, above shell command has failed, with:\nSTDOUT:\n".$res->[1]."\n\nSTDERR:\n".$res->[2]."\nEND."); return undef } | ||
| 833 | |||
| 834 | # we are returning a hash of name=>appproperties | ||
| 835 | # but because we allow 1 match only, this hash will only contain 1 item | ||
| 836 | # but it will be easier to allow more apps in the future if | ||
| 837 | # we return a hash here | ||
| 838 | return { $found_app_name => $found_app_properties }; | ||
| 839 | } | ||
| 840 | |||
| 841 | # Inputs parameters: | ||
| 842 | # 'package' => required package name as a SCALAR (for an exact search) | ||
| 843 | # or a regex (qr//) object for regex search including case-insensitive. | ||
| 844 | # The spec must yield exactly 1 match, it will complain if more than 1 matches found. | ||
| 845 | # 'force-reload-apps-list' => 0,1 : optionally call find_installed_apps() if > 0 | ||
| 846 | # but restricted only to the packages match 'package' NOT ALL. | ||
| 847 | # 'lazy' => 0,1 : pass this lazy value to the find_installed_apps() | ||
| 848 | # and be lazy (i.e. without enquiring on each app's specifics | ||
| 849 | # and creating an AppProperties object) if ==1 | ||
| 850 | # or not be lazy if ==0 ... | ||
| 851 | # ... (which means an AppProperties object is created for each found package) | ||
| 852 | # Default is force-reload-apps-list=>0 | ||
| 853 | # THIS APPLIES ONLY TO THE MATCHED 'package' | ||
| 854 | # On success it returns the a hash of {appname => appproperties} of the closed app | ||
| 855 | # (which will be created if not existing). It may return {} if no match. | ||
| 856 | # On failure it returns undef | ||
| 857 | # it needs that connect_device() to have been called prior to this call | ||
| 858 | sub close_app { | ||
| 859 | my ($self, $params) = @_; | ||
| 860 | $params //= {}; | ||
| 861 | |||
| 862 | my $parent = ( caller(1) )[3] || "N/A"; | ||
| 863 | my $whoami = ( caller(0) )[3]; | ||
| 864 | my $log = $self->log(); | ||
| 865 | my $verbosity = $self->verbosity(); | ||
| 866 | |||
| 867 | if( ! $self->is_device_connected() ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, you need to connect a device to the desktop and ALSO explicitly call ".'connect_device()'." before calling this."); return undef } | ||
| 868 | |||
| 869 | my ($package); | ||
| 870 | if( ! exists($params->{'package'}) || ! defined($package=$params->{'package'}) ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, input parameter 'package' is required."); return undef } | ||
| 871 | if( (ref($package)ne'') && (ref($package)ne'Regexp') ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, the type of input parameter 'package' must be a scalar string (the package name) or a Regexp object (compiled regex via ".'qr//'.")."); return undef } | ||
| 872 | |||
| 873 | # optional activity, else we will see if we find one | ||
| 874 | my $activity = (exists($params->{'activity'}) && defined($params->{'activity'})) ? $params->{'activity'} : undef; | ||
| 875 | |||
| 876 | my $force_reload = (exists($params->{'force-reload-apps-list'}) && defined($params->{'force-reload-apps-list'})) ? $params->{'force-reload-apps-list'} : 0; | ||
| 877 | my $lazy = (exists($params->{'lazy'}) && defined($params->{'lazy'})) ? $params->{'lazy'} : 1; | ||
| 878 | |||
| 879 | my $apps = $self->apps(); | ||
| 880 | if( (0 == scalar(keys %$apps)) | ||
| 881 | || ($force_reload>0) | ||
| 882 | ){ | ||
| 883 | my $fpars = { | ||
| 884 | 'packages' => $package, | ||
| 885 | 'force-reload-apps-list' => $force_reload, | ||
| 886 | 'lazy' => $lazy | ||
| 887 | }; | ||
| 888 | if( ! defined($apps=$self->find_installed_apps($fpars)) ){ $log->error(perl2dump($fpars)."${whoami} (via $parent), line ".__LINE__." : error, failed to load list of installed apps, call to ".'find_installed_apps()'." has failed with above parameters."); return undef } | ||
| 889 | if( 0 == scalar(keys %$apps) ){ | ||
| 890 | $log->error("${whoami} (via $parent), line ".__LINE__." : error, there are no installed apps, even after enquiring them. The target device has no apps installed. Weird."); | ||
| 891 | return undef | ||
| 892 | } | ||
| 893 | } | ||
| 894 | |||
| 895 | # by now we are sure we have the list of installed apps updated | ||
| 896 | # but it is likely that there is no AppProperties object for each | ||
| 897 | # app in the list, but we need it, so make a search and if | ||
| 898 | # AppProperties is undef, then we need to call find_installed_apps() again. | ||
| 899 | my $searchres = $self->search_app({ | ||
| 900 | 'package' => $package, | ||
| 901 | 'force-reload-apps-list' => 0, | ||
| 902 | }); | ||
| 903 | if( ! defined $searchres ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, call to ".'search_app()'." has failed for this search term (package) : ${package}"); return undef } | ||
| 904 | my $num_searchres = scalar keys %$searchres; | ||
| 905 | if( $num_searchres == 0 ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, no app was found for this search term (package) : ${package}"); return {} } | ||
| 906 | elsif( $num_searchres > 1 ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, more than one app was found for this search term (package) : ${package} . Apps found: '".join("', '", sort keys %$searchres)."'."); return undef } | ||
| 907 | # only 1 tupple in the hash, get it: | ||
| 908 | my ($found_app_name, $found_app_properties) = %$searchres; | ||
| 909 | |||
| 910 | if( $verbosity > 0 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : app to open has been matched to '${found_app_name}'.") } | ||
| 911 | |||
| 912 | if( ! defined $found_app_properties ){ | ||
| 913 | # the app is there in the list but it does not have AppProperties yet. | ||
| 914 | # So get just this one package and non-lazily because we need the AppProperties object: | ||
| 915 | my $fpars = { | ||
| 916 | 'force-reload-apps-list' => 0, | ||
| 917 | 'lazy' => 1, # << lazy for all other packages except our 'package' | ||
| 918 | 'packages' => $found_app_name, | ||
| 919 | }; | ||
| 920 | if( ! defined $self->find_installed_apps($fpars) ){ $log->error(perl2dump($fpars)."${whoami} (via $parent), line ".__LINE__." : error, failed to load list of installed apps, call to ".'find_installed_apps()'." has failed with above parameters."); return undef } | ||
| 921 | $apps = $self->apps(); | ||
| 922 | $found_app_properties = $apps->{$found_app_name}; | ||
| 923 | } | ||
| 924 | |||
| 925 | if( $verbosity > 0 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : closing app '".$found_app_properties->get('packageName')."' ...") } | ||
| 926 | |||
| 927 | # adb shell am force-stop com.my.app | ||
| 928 | my @cmd = ('am', 'force-stop', $found_app_properties->get('packageName')); | ||
| 929 | if( $verbosity > 0 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : sending command to adb: @cmd") } | ||
| 930 | my $res = $self->adb->shell(@cmd); | ||
| 931 | 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 undef } | ||
| 932 | if( $res->[0] != 0 ){ $log->error(join(" ", @cmd)."\n${whoami} (via $parent), line ".__LINE__." : error, above shell command has failed, with:\nSTDOUT:\n".$res->[1]."\n\nSTDERR:\n".$res->[2]."\nEND."); return undef } | ||
| 933 | |||
| 934 | # we are returning a hash of name=>appproperties | ||
| 935 | # but because we allow 1 match only, this hash will only contain 1 item | ||
| 936 | # but it will be easier to allow more apps in the future if | ||
| 937 | # we return a hash here | ||
| 938 | return { $found_app_name => $found_app_properties }; | ||
| 939 | } | ||
| 940 | |||
| 941 | # returns pid (as a non-negative integer) of the specified app by its exact name | ||
| 942 | # or -1 if nothing was matched in the process table. | ||
| 943 | # on error it returns undef | ||
| 944 | # Note: if you do not know the exact app name e.g. com.viber.voip | ||
| 945 | # use pgrep() | ||
| 946 | sub pidof { | ||
| 947 | my ($self, $params) = @_; | ||
| 948 | $params //= {}; | ||
| 949 | |||
| 950 | my $parent = ( caller(1) )[3] || "N/A"; | ||
| 951 | my $whoami = ( caller(0) )[3]; | ||
| 952 | my $log = $self->log(); | ||
| 953 | my $verbosity = $self->verbosity(); | ||
| 954 | |||
| 955 | if( ! $self->is_device_connected() ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, you need to connect a device to the desktop and ALSO explicitly call ".'connect_device()'." before calling this."); return undef } | ||
| 956 | |||
| 957 | if( ! exists($params->{'name'}) || ! defined($params->{'name'}) ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, missing parameter 'name' is required, it must be the exact app name, if you do not have the exact name then use 'pgrep()'."); return undef } | ||
| 958 | |||
| 959 | my @cmd = ('pidof', $params->{'name'}); | ||
| 960 | if( $verbosity > 0 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : sending command to adb: @cmd") } | ||
| 961 | my $res = $self->adb->shell(@cmd); | ||
| 962 | 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 undef } | ||
| 963 | |||
| 964 | # if not found it exits with 1 | ||
| 965 | if( $res->[0] == 1 ){ return -1 } # nothing matched | ||
| 966 | |||
| 967 | # an error | ||
| 968 | if( $res->[0] != 0 ){ $log->error(join(" ", @cmd)."\n${whoami} (via $parent), line ".__LINE__." : error, above shell command has failed, with:\nSTDOUT:\n".$res->[1]."\n\nSTDERR:\n".$res->[2]."\nEND."); return undef } | ||
| 969 | |||
| 970 | my $pid = $res->[1]; | ||
| 971 | if( ! defined $pid ){ $log->error(join(" ", @cmd)."\n${whoami} (via $parent), line ".__LINE__." : error, above shell command has failed, because the result got back was undef:\nSTDOUT:\n".$res->[1]."\n\nSTDERR:\n".$res->[2]."\nEND."); return undef } | ||
| 972 | # this is not happening, it exits with 1 | ||
| 973 | if( $pid =~ /^\s*$/ ){ return -1 } # nothing matched | ||
| 974 | |||
| 975 | # wrong pid format found | ||
| 976 | if( $pid !~ /^\s*(\d+)\s*$/m ){ $log->error(join(" ", @cmd)."\n${whoami} (via $parent), line ".__LINE__." : error, above shell command has failed, because the result got back was not a pid (as numbers+spaces) but '$pid':\nSTDOUT:\n".$res->[1]."\n\nSTDERR:\n".$res->[2]."\nEND."); return undef } | ||
| 977 | $pid = $1; | ||
| 978 | return $pid; # success, this is the pid | ||
| 979 | } | ||
| 980 | |||
| 981 | # returns an array of pids of the specified app(s) by part of its name, | ||
| 982 | # There may be more than 1 items in the array if the specified part of its name | ||
| 983 | # matches many apps. | ||
| 984 | # on error it returns undef | ||
| 985 | sub pgrep { | ||
| 986 | my ($self, $params) = @_; | ||
| 987 | $params //= {}; | ||
| 988 | |||
| 989 | my $parent = ( caller(1) )[3] || "N/A"; | ||
| 990 | my $whoami = ( caller(0) )[3]; | ||
| 991 | my $log = $self->log(); | ||
| 992 | my $verbosity = $self->verbosity(); | ||
| 993 | |||
| 994 | if( ! $self->is_device_connected() ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, you need to connect a device to the desktop and ALSO explicitly call ".'connect_device()'." before calling this."); return undef } | ||
| 995 | |||
| 996 | if( ! exists($params->{'name'}) || ! defined($params->{'name'}) ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, missing parameter 'name' is required, it must be the exact app name, if you do not have the exact name then use 'pgrep()'."); return undef } | ||
| 997 | |||
| 998 | my $dont_show_command_name = (exists($params->{'dont-show-command-name'}) && defined($params->{'dont-show-command-name'}) && ($params->{'dont-show-command-name'}>0)) ? 1 : 0; | ||
| 999 | # -f will search the full command name | ||
| 1000 | # -l will include the command name which is the default | ||
| 1001 | my @cmdparams = ('-f'); | ||
| 1002 | push(@cmdparams, '-l') unless $dont_show_command_name; | ||
| 1003 | |||
| 1004 | my @cmd = ('pgrep', @cmdparams, $params->{'name'}); | ||
| 1005 | if( $verbosity > 0 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : sending command to adb: @cmd") } | ||
| 1006 | my $res = $self->adb->shell(@cmd); | ||
| 1007 | 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 undef } | ||
| 1008 | if( $res->[0] != 0 ){ $log->error(join(" ", @cmd)."\n${whoami} (via $parent), line ".__LINE__." : error, above shell command has failed, with:\nSTDOUT:\n".$res->[1]."\n\nSTDERR:\n".$res->[2]."\nEND."); return undef } | ||
| 1009 | |||
| 1010 | # we get something like "954\n995\n1217\n1372\n1392\n1642\n1741\n1856\n1898\n2549\n3236\n4456\n6570\n9245\n10115\n10746\n" | ||
| 1011 | |||
| 1012 | my $xx = $res->[1]; | ||
| 1013 | if( ! defined $xx ){ $log->error(join(" ", @cmd)."\n${whoami} (via $parent), line ".__LINE__." : error, above shell command has failed, because the result got back was undef:\nSTDOUT:\n".$res->[1]."\n\nSTDERR:\n".$res->[2]."\nEND."); return undef } | ||
| 1014 | |||
| 1015 | my @results; | ||
| 1016 | while( $xx =~ /^\s*(\d+)(\s+(.+?))?\s*$/smg ){ | ||
| 1017 | push @results, { | ||
| 1018 | 'pid' => $1, | ||
| 1019 | 'command' => $2 // '' | ||
| 1020 | }; | ||
| 1021 | } | ||
| 1022 | |||
| 1023 | return \@results; # success | ||
| 1024 | # a list of pids, can have 0, 1 or more items | ||
| 1025 | } | ||
| 1026 | |||
| 1027 | # it takes the position on screen to tap either as a | ||
| 1028 | # 'position' => [x,y] | ||
| 1029 | # or | ||
| 1030 | # 'bounds' => [ [topleftX,topleftY], [bottomrightX, bottomrightY] ] | ||
| 1031 | # in which case, the tap position will be the mid-point of the 'bounds' | ||
| 1032 | # rectangle. | ||
| 1033 | # It returns 1 on failure, 0 on success | ||
| 1034 | # it needs that connect_device() to have been called prior to this call | ||
| 1035 | sub tap { | ||
| 1036 | my ($self, $params) = @_; | ||
| 1037 | $params //= {}; | ||
| 1038 | |||
| 1039 | my $parent = ( caller(1) )[3] || "N/A"; | ||
| 1040 | my $whoami = ( caller(0) )[3]; | ||
| 1041 | my $log = $self->log(); | ||
| 1042 | my $verbosity = $self->verbosity(); | ||
| 1043 | |||
| 1044 | if( ! $self->is_device_connected() ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, you need to connect a device to the desktop and ALSO explicitly call ".'connect_device()'." before calling this."); return undef } | ||
| 1045 | |||
| 1046 | my (@position, $m); | ||
| 1047 | if( exists($params->{'position'}) && defined($m=$params->{'position'}) ){ | ||
| 1048 | @position = ($m->[0], $m->[1]); | ||
| 1049 | } elsif( exists($params->{'bounds'}) && defined($m=$params->{'bounds'}) ){ | ||
| 1050 | @position = ( int(($m->[1]->[0] + $m->[0]->[0])/2), int(($m->[1]->[1] + $m->[0]->[1])/2) ); | ||
| 1051 | } else { $log->error("${whoami} (via $parent), line ".__LINE__." : error, input parameter 'position' (as ['x','y']) or 'bounds' (as [lefttopX,lefttopY],[bottomrightX,bottomrighY]) was not specified."); return 1 } | ||
| 1052 | |||
| 1053 | my @cmd = ('input', 'tap', @position); | ||
| 1054 | if( $verbosity > 0 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : sending command to adb: @cmd") } | ||
| 1055 | my $res = $self->adb->shell(@cmd); | ||
| 1056 | 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 } | ||
| 1057 | if( $res->[0] != 0 ){ $log->error(join(" ", @cmd)."\n${whoami} (via $parent), line ".__LINE__." : error, above shell command has failed, with:\nSTDOUT:\n".$res->[1]."\n\nSTDERR:\n".$res->[2]."\nEND."); return 1 } | ||
| 1058 | |||
| 1059 | return 0; # success | ||
| 1060 | } | ||
| 1061 | |||
| 1062 | # "type" the specified message into a text box expected to | ||
| 1063 | # be found on the specified position. | ||
| 1064 | # the message will be sanitised: spaces will be replaced with %s | ||
| 1065 | # unicode in text is not supported. | ||
| 1066 | # It returns 1 on failure, 0 on success | ||
| 1067 | # it needs that connect_device() to have been called prior to this call | ||
| 1068 | sub input_text { | ||
| 1069 | my ($self, $params) = @_; | ||
| 1070 | $params //= {}; | ||
| 1071 | |||
| 1072 | my $parent = ( caller(1) )[3] || "N/A"; | ||
| 1073 | my $whoami = ( caller(0) )[3]; | ||
| 1074 | my $log = $self->log(); | ||
| 1075 | my $verbosity = $self->verbosity(); | ||
| 1076 | |||
| 1077 | if( ! $self->is_device_connected() ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, you need to connect a device to the desktop and ALSO explicitly call ".'connect_device()'." before calling this."); return undef } | ||
| 1078 | |||
| 1079 | my (@position, $m); | ||
| 1080 | if( exists($params->{'position'}) && defined($m=$params->{'position'}) ){ | ||
| 1081 | @position = ($m->[0], $m->[1]); | ||
| 1082 | } elsif( exists($params->{'bounds'}) && defined($m=$params->{'bounds'}) ){ | ||
| 1083 | @position = ( int(($m->[1]->[0] + $m->[0]->[0])/2), int(($m->[1]->[1] + $m->[0]->[1])/2) ); | ||
| 1084 | } else { $log->error("${whoami} (via $parent), line ".__LINE__." : error, input parameter 'position' (as ['x','y']) or 'bounds' (as [lefttopX,lefttopY],[bottomrightX,bottomrighY]) was not specified."); return 1 } | ||
| 1085 | # optional text, else we send just '' (but we clicked on it) | ||
| 1086 | my $text = (exists($params->{'text'}) && defined($params->{'text'})) ? $params->{'text'} : ''; | ||
| 1087 | |||
| 1088 | # sanitise the text a bit | ||
| 1089 | # replace spaces with %s, | ||
| 1090 | # also newlines seem not to be supported so replaces these as well | ||
| 1091 | $text =~ s/[\n \t]/%s/g; | ||
| 1092 | |||
| 1093 | # first tap on the text edit widget at the specified coordinates to get focus | ||
| 1094 | if( $self->tap($params) ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, failed to tap on the position of the recipient of the text input"); return 1 } | ||
| 1095 | usleep(0.8); | ||
| 1096 | |||
| 1097 | # and send the text | ||
| 1098 | # adb shell input text 'hello%sworld' | ||
| 1099 | # does not support unicode and also spaces must be converted to %s (already have done this) | ||
| 1100 | my @cmd = ('input', 'text', $text); | ||
| 1101 | if( $verbosity > 0 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : sending command to adb: @cmd") } | ||
| 1102 | my $res = $self->adb->shell(@cmd); | ||
| 1103 | 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 } | ||
| 1104 | if( $res->[0] != 0 ){ $log->error(join(" ", @cmd)."\n${whoami} (via $parent), line ".__LINE__." : error, above shell command has failed, with:\nSTDOUT:\n".$res->[1]."\n\nSTDERR:\n".$res->[2]."\nEND."); return 1 } | ||
| 1105 | |||
| 1106 | return 0; # success | ||
| 1107 | } | ||
| 1108 | |||
| 1109 | # returns 1 on failure, 0 on success | ||
| 1110 | # it needs that connect_device() to have been called prior to this call | ||
| 1111 | sub clear_input_field { | ||
| 1112 | my ($self, $params) = @_; | ||
| 1113 | $params //= {}; | ||
| 1114 | |||
| 1115 | my $parent = ( caller(1) )[3] || "N/A"; | ||
| 1116 | my $whoami = ( caller(0) )[3]; | ||
| 1117 | my $log = $self->log(); | ||
| 1118 | my $verbosity = $self->verbosity(); | ||
| 1119 | |||
| 1120 | if( ! $self->is_device_connected() ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, you need to connect a device to the desktop and ALSO explicitly call ".'connect_device()'." before calling this."); return undef } | ||
| 1121 | |||
| 1122 | my (@position, $m); | ||
| 1123 | if( exists($params->{'position'}) && defined($m=$params->{'position'}) ){ | ||
| 1124 | @position = ($m->[0], $m->[1]); | ||
| 1125 | } elsif( exists($params->{'bounds'}) && defined($m=$params->{'bounds'}) ){ | ||
| 1126 | @position = ( int(($m->[1]->[0] + $m->[0]->[0])/2), int(($m->[1]->[1] + $m->[0]->[1])/2) ); | ||
| 1127 | } else { $log->error("${whoami} (via $parent), line ".__LINE__." : error, input parameter 'position' (as ['x','y']) or 'bounds' (as [lefttopX,lefttopY],[bottomrightX,bottomrighY]) was not specified."); return 1 } | ||
| 1128 | |||
| 1129 | # first tap on the text edit widget at the specified coordinates to get focus | ||
| 1130 | if( $self->tap($params) ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, failed to tap on the position of the recipient of the text input"); return 1 } | ||
| 1131 | usleep(0.8); | ||
| 1132 | |||
| 1133 | # from: https://stackoverflow.com/questions/32433303/clear-edit-text-adb | ||
| 1134 | # the simplest way is input keycombination 113 29 && input keyevent 67 | ||
| 1135 | # but may not work | ||
| 1136 | # then we try the lame way by erasing all chars one after the other | ||
| 1137 | # part1: | ||
| 1138 | my @cmd = ('input', 'keycombination', '113', '29'); | ||
| 1139 | if( $verbosity > 0 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : sending command to adb: @cmd") } | ||
| 1140 | my $res = $self->adb->shell(@cmd); | ||
| 1141 | if( ! defined($res) || ($res->[0] != 0) || ($res->[1]=~/Error: Unknown command/) || ($res->[2]=~/Error: Unknown command/) ){ | ||
| 1142 | $log->warn(join(" ", @cmd)."\n${whoami} (via $parent), line ".__LINE__." : error, above shell command has failed which happens and now will try an alternative ..."); | ||
| 1143 | # alternative/part1 | ||
| 1144 | @cmd = ('input', 'keyevent', 'KEYCODE_MOVE_END'); | ||
| 1145 | if( $verbosity > 0 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : sending command to adb: @cmd") } | ||
| 1146 | $res = $self->adb->shell(@cmd); | ||
| 1147 | 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. Info: this is the alternative part1."); return 1 } | ||
| 1148 | if( $res->[0] != 0 ){ $log->error(join(" ", @cmd)."\n${whoami} (via $parent), line ".__LINE__." : error, above shell command has failed (Info: this is alternative part1), with:\nSTDOUT:\n".$res->[1]."\n\nSTDERR:\n".$res->[2]."\nEND."); return 1 } | ||
| 1149 | # alternative/part2 | ||
| 1150 | # optional number of chars in the text-edit box, meaning how many | ||
| 1151 | # times to press backspace, default is here (250) | ||
| 1152 | # this is only needed for the second method (the failsafe) | ||
| 1153 | my $numchars = (exists($params->{'num-characters'}) && defined($params->{'num-characters'}) && ($params->{'num-characters'}=~/^\d+$/) ) ? $params->{'num-characters'} : 250; | ||
| 1154 | @cmd = ('input', 'keyevent', '--longpress', ('KEYCODE_DEL')x$numchars); | ||
| 1155 | if( $verbosity > 0 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : sending command to adb: @cmd") } | ||
| 1156 | $res = $self->adb->shell(@cmd); | ||
| 1157 | 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. Info: this is the alternative part1."); return 1 } | ||
| 1158 | if( $res->[0] != 0 ){ $log->error(join(" ", @cmd)."\n${whoami} (via $parent), line ".__LINE__." : error, above shell command has failed (Info: this is alternative part1), with:\nSTDOUT:\n".$res->[1]."\n\nSTDERR:\n".$res->[2]."\nEND."); return 1 } | ||
| 1159 | } else { | ||
| 1160 | # part2 | ||
| 1161 | # it worked, now on to part 2 | ||
| 1162 | @cmd = ('input', 'keyevent', 'KEYCODE_DEL'); | ||
| 1163 | if( $verbosity > 0 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : sending command to adb: @cmd") } | ||
| 1164 | $res = $self->adb->shell(@cmd); | ||
| 1165 | 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. Info: this is the part2."); return 1 } | ||
| 1166 | if( $res->[0] != 0 ){ $log->error(join(" ", @cmd)."\n${whoami} (via $parent), line ".__LINE__." : error, above shell command has failed (Info: this is part2), with:\nSTDOUT:\n".$res->[1]."\n\nSTDERR:\n".$res->[2]."\nEND."); return 1 } | ||
| 1167 | } | ||
| 1168 | return 0; # success | ||
| 1169 | } | ||
| 1170 | |||
| 1171 | # Finds the running processes on device (using a `ps`), | ||
| 1172 | # optionally can save the (parsed) `ps` | ||
| 1173 | # results as JSON to the specified 'filename'. | ||
| 1174 | # It needs that connect_device() to have been called prior to this call | ||
| 1175 | # It returns undef on failure. | ||
| 1176 | # On success, it returns a hash with keys | ||
| 1177 | # 'raw' : the raw output of `ps` as a string | ||
| 1178 | # 'perl': the parsed output of `ps` as a Perl hash of hashes, | ||
| 1179 | # each process | ||
| 1180 | # is represented by a hashref of items, e.g. %CPU | ||
| 1181 | # (basically all the items from the header of the `ps` command) | ||
| 1182 | # keyed on the full process command and its arguments (verbatim from `ps` output). | ||
| 1183 | # 'json': the above perl data structure converted to JSON. | ||
| 1184 | # NOTE: it uses _ps_parse_output() which is copied verbatim from System::Process | ||
| 1185 | # I wish they would load ps info from a string rather than running their own `ps` | ||
| 1186 | sub list_running_processes { | ||
| 1187 | my ($self, $params) = @_; | ||
| 1188 | |||
| 1189 | my $parent = ( caller(1) )[3] || "N/A"; | ||
| 1190 | my $whoami = ( caller(0) )[3]; | ||
| 1191 | |||
| 1192 | my $log = $self->log(); | ||
| 1193 | my $verbosity = $self->verbosity; | ||
| 1194 | |||
| 1195 | if( ! $self->is_device_connected() ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, you need to connect a device to the desktop and ALSO explicitly call ".'device_connected()'." before calling this."); return undef } | ||
| 1196 | |||
| 1197 | my $filename = exists($params->{'filename'}) && defined($params->{'filename'}) ? $params->{'filename'} : undef; | ||
| 1198 | my $extrafields = exists($params->{'extra-fields'}) && defined($params->{'extra-fields'}) ? $params->{'extra-fields'} : []; | ||
| 1199 | |||
| 1200 | my ($FH, $tmpfilename) = tempfile(CLEANUP=>$self->cleanup); | ||
| 1201 | close $FH; | ||
| 1202 | |||
| 1203 | # WARNING, you need to wake up the phone before dumping !!!! | ||
| 1204 | my $devicefile = File::Spec->catfile('/', 'data', 'local', 'tmp', $$.'.csv'); | ||
| 1205 | |||
| 1206 | my @cmd = ('ps', '-O', '%CPU', '-O', 'CPU'); | ||
| 1207 | for my $ef (@$extrafields){ push @cmd, '-O', $ef } | ||
| 1208 | push @cmd, '-f', '-l', '>', $devicefile; | ||
| 1209 | |||
| 1210 | if( $verbosity > 0 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : sending command to adb: @cmd") } | ||
| 1211 | my $res = $self->adb->shell(@cmd); | ||
| 1212 | 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 undef } | ||
| 1213 | if( $res->[0] != 0 ){ $log->error(join(" ", @cmd)."\n${whoami} (via $parent), line ".__LINE__." : error, above shell command has failed, with:\nSTDOUT:\n".$res->[1]."\n\nSTDERR:\n".$res->[2]."\nEND."); return undef } | ||
| 1214 | |||
| 1215 | $res = $self->adb->pull($devicefile, $tmpfilename); | ||
| 1216 | if( ! defined $res ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, failed to pull remote file '$devicefile' into local file '$tmpfilename', because undefined was returned, this should not be happening."); return undef } | ||
| 1217 | if( $res->[0] != 0 ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, failed to pull remote file '$devicefile' into local file '$tmpfilename' with:\nSTDOUT:\n".$res->[1]."\n\nSTDERR:\n".$res->[2]."\nEND."); return undef } | ||
| 1218 | |||
| 1219 | @cmd = ("rm", "-f", $devicefile); | ||
| 1220 | if( $verbosity > 0 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : sending command to adb: @cmd") } | ||
| 1221 | $res = $self->adb->shell(@cmd); | ||
| 1222 | 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 undef } | ||
| 1223 | if( $res->[0] != 0 ){ $log->error(join(" ", @cmd)."\n${whoami} (via $parent), line ".__LINE__." : error, above shell command has failed, with:\nSTDOUT:\n".$res->[1]."\n\nSTDERR:\n".$res->[2]."\nEND."); return undef } | ||
| 1224 | |||
| 1225 | # parse | ||
| 1226 | if( ! open($FH, '<', $tmpfilename) ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, failed to open file with dump for reading '$tmpfilename', $!"); return undef } | ||
| 1227 | my $contents; | ||
| 1228 | { local $/ = undef; $contents = <$FH> } close $FH; | ||
| 1229 | $contents =~ s/\R+//; | ||
| 1230 | my @rows = split /\R+/, $contents; | ||
| 1231 | my @header = split /\s+/, shift @rows; | ||
| 1232 | my %headerh = map { $_ => 1 } @header; | ||
| 1233 | my $id = 'PID'; | ||
| 1234 | my $posid = exists($headerh{$id}) ? $headerh{$id} : undef; | ||
| 1235 | if( ! defined $posid ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, failed to find column name '$id' in above 'ps' output, where is it? what is it called?"); return undef } | ||
| 1236 | $id = 'CMD'; | ||
| 1237 | my $poscmd = exists($headerh{$id}) ? $headerh{$id} : undef; | ||
| 1238 | |||
| 1239 | my $n = scalar @header; | ||
| 1240 | |||
| 1241 | my %psdata; | ||
| 1242 | while( my $row = shift @rows ){ | ||
| 1243 | $row =~ s/^\s*//; | ||
| 1244 | my @rowitems = split /\s+/, $row, $n+1; | ||
| 1245 | my $k = $rowitems[defined($poscmd) ? $poscmd : $posid]; | ||
| 1246 | @{ $psdata{$k} }{@header} = splice @rowitems, 0, $n; | ||
| 1247 | if( defined $poscmd ){ | ||
| 1248 | $psdata{$k}->{'CMD'} = [ Text::ParseWords::shellwords($psdata{$k}->{'CMD'}) ]; | ||
| 1249 | # now CMD is an arrayref | ||
| 1250 | } | ||
| 1251 | } | ||
| 1252 | |||
| 1253 | my $jsonstr = perl2json(\%psdata); | ||
| 1254 | if( ! defined $jsonstr ){ $log->error(perl2dump(\%psdata)."${whoami} (via $parent), line ".__LINE__." : error, failed to convert above perl data hash to JSON string."); return undef } | ||
| 1255 | if( defined $filename ){ | ||
| 1256 | if( ! open($FH, '>:raw', $filename) ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, failed to open file '$filename' for writing, $!"); return undef } | ||
| 1257 | print $FH $jsonstr; | ||
| 1258 | close $FH; | ||
| 1259 | } | ||
| 1260 | return { | ||
| 1261 | 'raw' => $contents, | ||
| 1262 | 'json' => $jsonstr, | ||
| 1263 | 'perl' => \%psdata | ||
| 1264 | } | ||
| 1265 | } | ||
| 1266 | |||
| 1267 | # ONLY FOR EMULATORS, it fixes the Geolocation to the | ||
| 1268 | # specified coordinates (with 'latitude' and 'longitude'). | ||
| 1269 | # returns 1 on failure, 0 on success | ||
| 1270 | # it needs that connect_device() to have been called prior to this call | ||
| 1271 | sub geofix { | ||
| 1272 | my ($self, $params) = @_; | ||
| 1273 | $params //= {}; | ||
| 1274 | |||
| 1275 | my $parent = ( caller(1) )[3] || "N/A"; | ||
| 1276 | my $whoami = ( caller(0) )[3]; | ||
| 1277 | my $log = $self->log(); | ||
| 1278 | my $verbosity = $self->verbosity(); | ||
| 1279 | |||
| 1280 | if( ! $self->is_device_connected() ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, you need to connect a device to the desktop and ALSO explicitly call ".'connect_device()'." before calling this."); return undef } | ||
| 1281 | |||
| 1282 | for ('latitude', 'longitude'){ | ||
| 1283 | if( ! exists $params->{$_} ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, input parameter '$_' was not specified (as [x,y])."); return 1 } | ||
| 1284 | } | ||
| 1285 | |||
| 1286 | my @cmd = ('emu', 'geo', 'fix', $params->{'longitude'}, $params->{'latitude'}); | ||
| 1287 | if( $verbosity > 0 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : sending command to adb: @cmd") } | ||
| 1288 | my $res = $self->adb->run(@cmd); | ||
| 1289 | 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 } | ||
| 1290 | if( $res->[0] != 0 ){ $log->error(join(" ", @cmd)."\n${whoami} (via $parent), line ".__LINE__." : error, above shell command has failed, with:\nSTDOUT:\n".$res->[1]."\n\nSTDERR:\n".$res->[2]."\nEND."); return 1 } | ||
| 1291 | |||
| 1292 | return 0; # success | ||
| 1293 | } | ||
| 1294 | |||
| 1295 | # Get the current GPS location of the device | ||
| 1296 | # according to ALL the GPS providers as a HASH | ||
| 1297 | # keyed on GPS provider name with the information | ||
| 1298 | # the provider provided including lat/lon | ||
| 1299 | # It returns undef undef on failure or the above hash on success. | ||
| 1300 | # NOTE: some providers may exist but have the Location[...] string as null | ||
| 1301 | # meaning not available (e.g. 'network provider' when no internet exists) | ||
| 1302 | # in this case lat,lon etc. will be '<na>' and the strings will be 'null'. | ||
| 1303 | # it needs that connect_device() to have been called prior to this call | ||
| 1304 | sub dump_current_location { | ||
| 1305 | my ($self, $params) = @_; | ||
| 1306 | $params //= {}; | ||
| 1307 | |||
| 1308 | my $parent = ( caller(1) )[3] || "N/A"; | ||
| 1309 | my $whoami = ( caller(0) )[3]; | ||
| 1310 | my $log = $self->log(); | ||
| 1311 | my $verbosity = $self->verbosity(); | ||
| 1312 | |||
| 1313 | if( ! $self->is_device_connected() ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, you need to connect a device to the desktop and ALSO explicitly call ".'connect_device()'." before calling this."); return undef } | ||
| 1314 | |||
| 1315 | my @cmd = ('dumpsys', 'location'); | ||
| 1316 | if( $verbosity > 0 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : sending command to adb: @cmd") } | ||
| 1317 | my $res = $self->adb->shell(@cmd); | ||
| 1318 | 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 } | ||
| 1319 | if( $res->[0] != 0 ){ $log->error(join(" ", @cmd)."\n${whoami} (via $parent), line ".__LINE__." : error, above shell command has failed, with:\nSTDOUT:\n".$res->[1]."\n\nSTDERR:\n".$res->[2]."\nEND."); return 1 } | ||
| 1320 | my $content = $res->[1]; | ||
| 1321 | # these are the GPS providers in order of preference: | ||
| 1322 | my $gps; | ||
| 1323 | for my $prov ('gps provider', 'fused provider', 'passive provider', 'network provider'){ | ||
| 1324 | if( $content =~ /Geofences\:\s+Location Providers\:.*?\n\s+\Q${prov}\E\:\s+last location=(.+?)\R\s+last coarse location=(.+?)\R/sm ){ | ||
| 1325 | my $last_location = $1; | ||
| 1326 | my $last_coarse_location = $2; | ||
| 1327 | $gps //= {}; | ||
| 1328 | my ($realprov, $lat, $lon, $las); | ||
| 1329 | if( $last_location =~ /\s*null\s*$/ ){ | ||
| 1330 | $realprov = '<na>'; | ||
| 1331 | $lat = '<na>'; | ||
| 1332 | $lon = '<na>'; | ||
| 1333 | } else { | ||
| 1334 | if( $last_location !~ /Location\[(.+?)\s+([-+]?\d+(?:\.\d+)?)\s*[:,]\s*([-+]?\d+(?:\.\d+)?)/ ){ $log->error("--BEGIN content:\n${content}\n--END content\n${whoami} (via $parent), line ".__LINE__." : error, failed to parse last location string: ${last_location}"); return undef } | ||
| 1335 | $realprov = $1; | ||
| 1336 | $lat = $2; | ||
| 1337 | $lon = $3; | ||
| 1338 | } | ||
| 1339 | $gps->{$prov} = { | ||
| 1340 | 'provider' => $realprov, | ||
| 1341 | 'latitude' => $lat, | ||
| 1342 | 'longitude' => $lon, | ||
| 1343 | 'last-location-string' => $last_location | ||
| 1344 | }; | ||
| 1345 | if( ($last_coarse_location !~ /\s*null\s*$/) | ||
| 1346 | && ($last_coarse_location !~ /Location\[(.+?)\s+([-+]?\d+(?:\.\d+)?)\s*[:,]\s*([-+]?\d+(?:\.\d+)?)/) | ||
| 1347 | ){ $log->error("--BEGIN content:\n${content}\n--END content\n${whoami} (via $parent), line ".__LINE__." : error, failed to parse last coarse location string: ${last_coarse_location}"); return undef } | ||
| 1348 | $gps->{$prov}->{'last-coarse-location-string'} = $last_coarse_location; | ||
| 1349 | } | ||
| 1350 | } | ||
| 1351 | if( ! defined $gps ){ $log->error("--BEGIN content:\n${content}\n--END content\n${whoami} (via $parent), line ".__LINE__." : error, location not found in above dumpsys, perhaps it is not enabled?"); return undef } | ||
| 1352 | return $gps; | ||
| 1353 | } | ||
| 1354 | |||
| 1355 | # It lists the IDs of all the physical displays connected to the | ||
| 1356 | # device, including the main one and returns these back as a HASH | ||
| 1357 | # keyed on display ID. | ||
| 1358 | # it needs that connect_device() to have been called prior to this call | ||
| 1359 | sub list_physical_displays { | ||
| 1360 | my ($self, $params) = @_; | ||
| 1361 | $params //= {}; | ||
| 1362 | |||
| 1363 | my $parent = ( caller(1) )[3] || "N/A"; | ||
| 1364 | my $whoami = ( caller(0) )[3]; | ||
| 1365 | my $log = $self->log(); | ||
| 1366 | my $verbosity = $self->verbosity(); | ||
| 1367 | |||
| 1368 | if( ! $self->is_device_connected() ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, you need to connect a device to the desktop and ALSO explicitly call ".'connect_device()'." before calling this."); return undef } | ||
| 1369 | |||
| 1370 | my @cmd = ('dumpsys', 'SurfaceFlinger', '--display-id'); | ||
| 1371 | if( $verbosity > 0 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : sending command to adb: @cmd") } | ||
| 1372 | my $res = $self->adb->shell(@cmd); | ||
| 1373 | 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 undef } | ||
| 1374 | if( $res->[0] != 0 ){ $log->error(join(" ", @cmd)."\n${whoami} (via $parent), line ".__LINE__." : error, above shell command has failed, with:\nSTDOUT:\n".$res->[1]."\n\nSTDERR:\n".$res->[2]."\nEND."); return undef } | ||
| 1375 | my $content = $res->[1]; | ||
| 1376 | my %ids; | ||
| 1377 | while( $content =~ /^(Display\s+(.+?)\s+.+?)$/gsm ){ | ||
| 1378 | $ids{$2} = $1 | ||
| 1379 | } | ||
| 1380 | return \%ids; | ||
| 1381 | } | ||
| 1382 | |||
| 1383 | # It takes a screendump of current screen on device and returns it as | ||
| 1384 | # a Image::PNG object, optionally saving it to the specified file. | ||
| 1385 | # it needs that connect_device() to have been called prior to this call | ||
| 1386 | # It returns undef on failure or the screenshot as an Image::PNG object | ||
| 1387 | # on success. | ||
| 1388 | # it needs that connect_device() to have been called prior to this call | ||
| 1389 | sub dump_current_screen_shot { | ||
| 1390 | my ($self, $params) = @_; | ||
| 1391 | |||
| 1392 | my $parent = ( caller(1) )[3] || "N/A"; | ||
| 1393 | my $whoami = ( caller(0) )[3]; | ||
| 1394 | |||
| 1395 | my $log = $self->log(); | ||
| 1396 | my $verbosity = $self->verbosity; | ||
| 1397 | |||
| 1398 | if( ! $self->is_device_connected() ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, you need to connect a device to the desktop and ALSO explicitly call ".'device_connected()'." before calling this."); return undef } | ||
| 1399 | |||
| 1400 | my $filename = exists($params->{'filename'}) && defined($params->{'filename'}) ? $params->{'filename'} : undef; | ||
| 1401 | |||
| 1402 | my $FH; | ||
| 1403 | if( ! defined $filename ){ | ||
| 1404 | ($FH, $filename) = tempfile(CLEANUP=>$self->cleanup); | ||
| 1405 | close $FH; | ||
| 1406 | } | ||
| 1407 | |||
| 1408 | # optional display-id (TODO: confirm that this display id is valid with | ||
| 1409 | # dumpsys SurfaceFlinger --display-id | ||
| 1410 | my @options; | ||
| 1411 | if( exists($params->{'display-id'}) && defined($params->{'display-id'}) ){ | ||
| 1412 | push @options, '--display-id', $params->{'display-id'} | ||
| 1413 | } | ||
| 1414 | |||
| 1415 | # WARNING, you need to wake up the phone before dumping !!!! | ||
| 1416 | my $devicefile = File::Spec->catfile('/', 'data', 'local', 'tmp', $$.'.png'); | ||
| 1417 | |||
| 1418 | my @cmd = ('screencap', @options, '-p', $devicefile); | ||
| 1419 | if( $verbosity > 0 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : sending command to adb: @cmd") } | ||
| 1420 | my $res = $self->adb->shell(@cmd); | ||
| 1421 | 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 undef } | ||
| 1422 | if( $res->[0] != 0 ){ $log->error(join(" ", @cmd)."\n${whoami} (via $parent), line ".__LINE__." : error, above shell command has failed, with:\nSTDOUT:\n".$res->[1]."\n\nSTDERR:\n".$res->[2]."\nEND."); return undef } | ||
| 1423 | |||
| 1424 | $res = $self->adb->pull($devicefile, $filename); | ||
| 1425 | if( ! defined $res ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, failed to pull remote file '$devicefile' into local file '$filename', because undefined was returned, this should not be happening."); return undef } | ||
| 1426 | if( $res->[0] != 0 ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, failed to pull remote file '$devicefile' into local file '$filename' with:\nSTDOUT:\n".$res->[1]."\n\nSTDERR:\n".$res->[2]."\nEND."); return undef } | ||
| 1427 | |||
| 1428 | @cmd = ("rm", "-f", $devicefile); | ||
| 1429 | if( $verbosity > 0 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : sending command to adb: @cmd") } | ||
| 1430 | $res = $self->adb->shell(@cmd); | ||
| 1431 | 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 undef } | ||
| 1432 | if( $res->[0] != 0 ){ $log->error(join(" ", @cmd)."\n${whoami} (via $parent), line ".__LINE__." : error, above shell command has failed, with:\nSTDOUT:\n".$res->[1]."\n\nSTDERR:\n".$res->[2]."\nEND."); return undef } | ||
| 1433 | |||
| 1434 | # let's return the string content back, no we return back an Image::PNG | ||
| 1435 | #my $contents; | ||
| 1436 | #if( ! open($FH, '<', $filename) ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, failed to open file with dump for reading '$filename', $!"); return undef } | ||
| 1437 | #{ local $/ = undef; $contents = <$FH> } close $FH; | ||
| 1438 | |||
| 1439 | # create an Image::PNG to return back | ||
| 1440 | my $img = Image::PNG->new(); | ||
| 1441 | if( ! defined $img ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, call to ".'Image::PNG->new()'." has failed."); return undef } | ||
| 1442 | if( ! $img->read($filename) ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, failed to read local file '$filename' as PNG (call to ".'Image::PNG->read()'." has failed)."); return undef } | ||
| 1443 | |||
| 1444 | return $img; | ||
| 1445 | } | ||
| 1446 | |||
| 1447 | # It takes a video recording of current screen on device and | ||
| 1448 | # saves its to the specified file ($filename). | ||
| 1449 | # Optionally specify 'time-limit' or a default of 10s is used. | ||
| 1450 | # Optionally specify 'bit-rate'. | ||
| 1451 | # Optionally specify %size = ('width' => ..., 'height' => ...) | ||
| 1452 | # Optionally specify if $bugreport==1, then Android will overlay debug info on movie. | ||
| 1453 | # Optionally specify 'display-id'. | ||
| 1454 | # Output format of recording is MP4. | ||
| 1455 | # It returns 1 on failure, 0 on success. | ||
| 1456 | # it needs that connect_device() to have been called prior to this call | ||
| 1457 | sub dump_current_screen_video { | ||
| 1458 | my ($self, $params) = @_; | ||
| 1459 | |||
| 1460 | my $parent = ( caller(1) )[3] || "N/A"; | ||
| 1461 | my $whoami = ( caller(0) )[3]; | ||
| 1462 | |||
| 1463 | my $log = $self->log(); | ||
| 1464 | my $verbosity = $self->verbosity; | ||
| 1465 | |||
| 1466 | if( ! $self->is_device_connected() ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, you need to connect a device to the desktop and ALSO explicitly call ".'device_connected()'." before calling this."); return 1 } | ||
| 1467 | |||
| 1468 | my $filename = exists($params->{'filename'}) && defined($params->{'filename'}) ? $params->{'filename'} : undef; | ||
| 1469 | if( ! defined $filename ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, input parameter 'filename' is not specified, an output filename must be specified."); return 1 } | ||
| 1470 | |||
| 1471 | my @options; | ||
| 1472 | # optional duration or default of 10 seconds. (Android default is 180 which is stupidly huge for us) | ||
| 1473 | if( exists($params->{'time-limit'}) && defined($params->{'time-limit'}) ){ | ||
| 1474 | push @options, '--time-limit', $params->{'time-limit'} | ||
| 1475 | } else { push @options, '--time-limit', '10' } | ||
| 1476 | |||
| 1477 | # optional bitrate (don't know what Android default is) | ||
| 1478 | if( exists($params->{'bit-rate'}) && defined($params->{'bit-rate'}) ){ | ||
| 1479 | push @options, '--bit-rate', $params->{'bit-rate'} | ||
| 1480 | } | ||
| 1481 | |||
| 1482 | # optional bugreport (Android overlays timestamp etc.) | ||
| 1483 | if( exists($params->{'bugreport'}) && defined($params->{'bugreport'}) | ||
| 1484 | && ($params->{'bugreport'} > 0) | ||
| 1485 | ){ | ||
| 1486 | push @options, '--bugreport' | ||
| 1487 | } | ||
| 1488 | |||
| 1489 | # optional size | ||
| 1490 | if( exists($params->{'size'}) && defined($params->{'size'}) ){ | ||
| 1491 | if( (ref($params->{'size'}) ne 'HASH') | ||
| 1492 | || (! exists $$params->{'size'}->{'width'}) | ||
| 1493 | || (! defined $$params->{'size'}->{'width'}) | ||
| 1494 | || (! exists $$params->{'size'}->{'height'}) | ||
| 1495 | || (! defined $$params->{'size'}->{'height'}) ){ $log->info("${whoami} (via $parent), line ".__LINE__." : error, specified parameter 'size' is either not a HASHref or it does not contain the two required keys 'width' and 'height'."); return 1 } | ||
| 1496 | push @options, '--size', $params->{'size'}->{'width'}, $params->{'size'}->{'height'} | ||
| 1497 | } | ||
| 1498 | |||
| 1499 | # optional display-id (TODO: confirm that this display id is valid with | ||
| 1500 | # dumpsys SurfaceFlinger --display-id | ||
| 1501 | if( exists($params->{'display-id'}) && defined($params->{'display-id'}) ){ | ||
| 1502 | push @options, '--display-id', $params->{'display-id'} | ||
| 1503 | } | ||
| 1504 | |||
| 1505 | # WARNING, you need to wake up the phone before dumping !!!! | ||
| 1506 | my $devicefile = File::Spec->catfile('/', 'data', 'local', 'tmp', $$.'.mp4'); | ||
| 1507 | |||
| 1508 | my @cmd = ('screenrecord', @options, $devicefile); | ||
| 1509 | if( $verbosity > 0 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : sending command to adb: @cmd") } | ||
| 1510 | my $res = $self->adb->shell(@cmd); | ||
| 1511 | |||
| 1512 | 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 } | ||
| 1513 | if( $res->[0] != 0 ){ $log->error(join(" ", @cmd)."\n${whoami} (via $parent), line ".__LINE__." : error, above shell command has failed, with:\nSTDOUT:\n".$res->[1]."\n\nSTDERR:\n".$res->[2]."\nEND."); return 1 } | ||
| 1514 | |||
| 1515 | $res = $self->adb->pull($devicefile, $filename); | ||
| 1516 | if( ! defined $res ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, failed to pull remote file '$devicefile' into local file '$filename', because undefined was returned, this should not be happening."); return 1 } | ||
| 1517 | if( $res->[0] != 0 ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, failed to pull remote file '$devicefile' into local file '$filename' with:\nSTDOUT:\n".$res->[1]."\n\nSTDERR:\n".$res->[2]."\nEND."); return 1 } | ||
| 1518 | |||
| 1519 | @cmd = ("rm", "-f", $devicefile); | ||
| 1520 | if( $verbosity > 0 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : sending command to adb: @cmd") } | ||
| 1521 | $res = $self->adb->shell(@cmd); | ||
| 1522 | 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 } | ||
| 1523 | if( $res->[0] != 0 ){ $log->error(join(" ", @cmd)."\n${whoami} (via $parent), line ".__LINE__." : error, above shell command has failed, with:\nSTDOUT:\n".$res->[1]."\n\nSTDERR:\n".$res->[2]."\nEND."); return 1 } | ||
| 1524 | |||
| 1525 | # let's return the string content back, no we return back 1 or 0, output must be saved to file | ||
| 1526 | #my $contents; | ||
| 1527 | #if( ! open($FH, '<', $filename) ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, failed to open file with dump for reading '$filename', $!"); return 1 } | ||
| 1528 | #{ local $/ = undef; $contents = <$FH> } close $FH; | ||
| 1529 | |||
| 1530 | return 0; # success | ||
| 1531 | } | ||
| 1532 | |||
| 1533 | # returns 1 on failure, 0 on success | ||
| 1534 | # it needs that connect_device() to have been called prior to this call | ||
| 1535 | sub wake_up { | ||
| 1536 | my ($self, $params) = @_; | ||
| 1537 | $params //= {}; | ||
| 1538 | |||
| 1539 | my $parent = ( caller(1) )[3] || "N/A"; | ||
| 1540 | my $whoami = ( caller(0) )[3]; | ||
| 1541 | my $log = $self->log(); | ||
| 1542 | my $verbosity = $self->verbosity(); | ||
| 1543 | |||
| 1544 | if( ! $self->is_device_connected() ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, you need to connect a device to the desktop and ALSO explicitly call ".'connect_device()'." before calling this."); return undef } | ||
| 1545 | |||
| 1546 | my @cmd = qw/input keyevent KEYCODE_WAKEUP/; | ||
| 1547 | if( $verbosity > 0 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : sending command to adb: @cmd") } | ||
| 1548 | my $res = $self->adb->shell(@cmd); | ||
| 1549 | 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 undef } | ||
| 1550 | if( $res->[0] != 0 ){ $log->error(join(" ", @cmd)."\n${whoami} (via $parent), line ".__LINE__." : error, above shell command has failed, with:\nSTDOUT:\n".$res->[1]."\n\nSTDERR:\n".$res->[2]."\nEND."); return undef } | ||
| 1551 | |||
| 1552 | return 0; # success | ||
| 1553 | } | ||
| 1554 | # goes to the home screen | ||
| 1555 | # returns 1 on success, 0 on failure | ||
| 1556 | # it needs that connect_device() to have been called prior to this call | ||
| 1557 | sub home_screen { | ||
| 1558 | my ($self, $params) = @_; | ||
| 1559 | $params //= {}; | ||
| 1560 | |||
| 1561 | my $parent = ( caller(1) )[3] || "N/A"; | ||
| 1562 | my $whoami = ( caller(0) )[3]; | ||
| 1563 | my $log = $self->log(); | ||
| 1564 | my $verbosity = $self->verbosity(); | ||
| 1565 | |||
| 1566 | if( ! $self->is_device_connected() ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, you need to connect a device to the desktop and ALSO explicitly call ".'connect_device()'." before calling this."); return undef } | ||
| 1567 | |||
| 1568 | $self->wake_up(); | ||
| 1569 | |||
| 1570 | my @cmd = qw/am start -a android.intent.action.MAIN -c android.intent.category.HOME/; | ||
| 1571 | if( $verbosity > 0 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : sending command to adb: @cmd") } | ||
| 1572 | my $res = $self->adb->shell(@cmd); | ||
| 1573 | 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 undef } | ||
| 1574 | if( $res->[0] != 0 ){ $log->error(join(" ", @cmd)."\n${whoami} (via $parent), line ".__LINE__." : error, above shell command has failed, with:\nSTDOUT:\n".$res->[1]."\n\nSTDERR:\n".$res->[2]."\nEND."); return undef } | ||
| 1575 | |||
| 1576 | return 0; # success | ||
| 1577 | } | ||
| 1578 | # It swipes right basically | ||
| 1579 | # it returns 0 on success, 1 on failure | ||
| 1580 | # it needs that connect_device() to have been called prior to this call | ||
| 1581 | sub next_screen { | ||
| 1582 | my ($self, $params) = @_; | ||
| 1583 | $params //= {}; | ||
| 1584 | |||
| 1585 | my $parent = ( caller(1) )[3] || "N/A"; | ||
| 1586 | my $whoami = ( caller(0) )[3]; | ||
| 1587 | my $log = $self->log(); | ||
| 1588 | my $verbosity = $self->verbosity(); | ||
| 1589 | |||
| 1590 | if( ! $self->is_device_connected() ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, you need to connect a device to the desktop and ALSO explicitly call ".'connect_device()'." before calling this."); return undef } | ||
| 1591 | |||
| 1592 | if( $self->swipe({'direction' => 'right', dt => 100}) ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, call to ".'swipe()'." has failed."); return undef } | ||
| 1593 | |||
| 1594 | return 0; # success | ||
| 1595 | } | ||
| 1596 | |||
| 1597 | # It swipes left basically | ||
| 1598 | # it returns 0 on success, 1 on failure | ||
| 1599 | # it needs that connect_device() to have been called prior to this call | ||
| 1600 | sub previous_screen { | ||
| 1601 | my ($self, $params) = @_; | ||
| 1602 | $params //= {}; | ||
| 1603 | |||
| 1604 | my $parent = ( caller(1) )[3] || "N/A"; | ||
| 1605 | my $whoami = ( caller(0) )[3]; | ||
| 1606 | my $log = $self->log(); | ||
| 1607 | my $verbosity = $self->verbosity(); | ||
| 1608 | |||
| 1609 | if( ! $self->is_device_connected() ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, you need to connect a device to the desktop and ALSO explicitly call ".'connect_device()'." before calling this."); return undef } | ||
| 1610 | |||
| 1611 | if( $self->swipe({'direction' => 'left', dt => 100}) ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, call to ".'swipe()'." has failed."); return undef } | ||
| 1612 | |||
| 1613 | return 0; # success | ||
| 1614 | } | ||
| 1615 | |||
| 1616 | # it returns 0 on success, 1 on failure | ||
| 1617 | # it needs that connect_device() to have been called prior to this call | ||
| 1618 | # the left-triangle button (see http://developer.android.com/reference/android/view/KeyEvent.html) | ||
| 1619 | sub navigation_menu_back_button { | ||
| 1620 | my ($self, $params) = @_; | ||
| 1621 | $params //= {}; | ||
| 1622 | |||
| 1623 | my $parent = ( caller(1) )[3] || "N/A"; | ||
| 1624 | my $whoami = ( caller(0) )[3]; | ||
| 1625 | my $log = $self->log(); | ||
| 1626 | my $verbosity = $self->verbosity(); | ||
| 1627 | |||
| 1628 | if( ! $self->is_device_connected() ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, you need to connect a device to the desktop and ALSO explicitly call ".'connect_device()'." before calling this."); return undef } | ||
| 1629 | |||
| 1630 | my @cmd = ('input', 'keyevent', 'KEYCODE_BACK'); | ||
| 1631 | if( $verbosity > 0 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : sending command to adb: @cmd") } | ||
| 1632 | my $res = $self->adb->shell(@cmd); | ||
| 1633 | 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 undef } | ||
| 1634 | if( $res->[0] != 0 ){ $log->error(join(" ", @cmd)."\n${whoami} (via $parent), line ".__LINE__." : error, above shell command has failed, with:\nSTDOUT:\n".$res->[1]."\n\nSTDERR:\n".$res->[2]."\nEND."); return undef } | ||
| 1635 | |||
| 1636 | return 0; # success | ||
| 1637 | } | ||
| 1638 | |||
| 1639 | # it returns 0 on success, 1 on failure | ||
| 1640 | # it needs that connect_device() to have been called prior to this call | ||
| 1641 | # the round button, it goes to home (see http://developer.android.com/reference/android/view/KeyEvent.html) | ||
| 1642 | sub navigation_menu_home_button { | ||
| 1643 | my ($self, $params) = @_; | ||
| 1644 | $params //= {}; | ||
| 1645 | |||
| 1646 | my $parent = ( caller(1) )[3] || "N/A"; | ||
| 1647 | my $whoami = ( caller(0) )[3]; | ||
| 1648 | my $log = $self->log(); | ||
| 1649 | my $verbosity = $self->verbosity(); | ||
| 1650 | |||
| 1651 | if( ! $self->is_device_connected() ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, you need to connect a device to the desktop and ALSO explicitly call ".'connect_device()'." before calling this."); return undef } | ||
| 1652 | |||
| 1653 | my @cmd = ('input', 'keyevent', 'KEYCODE_HOME'); | ||
| 1654 | if( $verbosity > 0 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : sending command to adb: @cmd") } | ||
| 1655 | my $res = $self->adb->shell(@cmd); | ||
| 1656 | 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 undef } | ||
| 1657 | if( $res->[0] != 0 ){ $log->error(join(" ", @cmd)."\n${whoami} (via $parent), line ".__LINE__." : error, above shell command has failed, with:\nSTDOUT:\n".$res->[1]."\n\nSTDERR:\n".$res->[2]."\nEND."); return undef } | ||
| 1658 | |||
| 1659 | return 0; # success | ||
| 1660 | } | ||
| 1661 | |||
| 1662 | # it returns 0 on success, 1 on failure | ||
| 1663 | # it needs that connect_device() to have been called prior to this call | ||
| 1664 | # the square button, aka overview, shows all apps running in some sort of gallery view | ||
| 1665 | # (see http://developer.android.com/reference/android/view/KeyEvent.html) | ||
| 1666 | sub navigation_menu_overview_button { | ||
| 1667 | my ($self, $params) = @_; | ||
| 1668 | $params //= {}; | ||
| 1669 | |||
| 1670 | my $parent = ( caller(1) )[3] || "N/A"; | ||
| 1671 | my $whoami = ( caller(0) )[3]; | ||
| 1672 | my $log = $self->log(); | ||
| 1673 | my $verbosity = $self->verbosity(); | ||
| 1674 | |||
| 1675 | if( ! $self->is_device_connected() ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, you need to connect a device to the desktop and ALSO explicitly call ".'connect_device()'." before calling this."); return undef } | ||
| 1676 | |||
| 1677 | my @cmd = ('input', 'keyevent', 'KEYCODE_APP_SWITCH'); | ||
| 1678 | if( $verbosity > 0 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : sending command to adb: @cmd") } | ||
| 1679 | my $res = $self->adb->shell(@cmd); | ||
| 1680 | 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 undef } | ||
| 1681 | if( $res->[0] != 0 ){ $log->error(join(" ", @cmd)."\n${whoami} (via $parent), line ".__LINE__." : error, above shell command has failed, with:\nSTDOUT:\n".$res->[1]."\n\nSTDERR:\n".$res->[2]."\nEND."); return undef } | ||
| 1682 | |||
| 1683 | return 0; # success | ||
| 1684 | } | ||
| 1685 | |||
| 1686 | # it swipes up and lists all the apps found in that "drawer" | ||
| 1687 | # with their icon bounds (which is where you "tap"). | ||
| 1688 | # It returns undef on failure | ||
| 1689 | # It returns a hashref of all the apps found with their name as key and bounds content etc. | ||
| 1690 | # it needs that connect_device() to have been called prior to this call | ||
| 1691 | sub find_all_apps_roundabout_way { | ||
| 1692 | my ($self, $params) = @_; | ||
| 1693 | $params //= {}; | ||
| 1694 | |||
| 1695 | my $parent = ( caller(1) )[3] || "N/A"; | ||
| 1696 | my $whoami = ( caller(0) )[3]; | ||
| 1697 | my $log = $self->log(); | ||
| 1698 | my $verbosity = $self->verbosity(); | ||
| 1699 | |||
| 1700 | if( ! $self->is_device_connected() ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, you need to connect a device to the desktop and ALSO explicitly call ".'connect_device()'." before calling this."); return undef } | ||
| 1701 | |||
| 1702 | # go to home, swipe up and all apps will be revealed | ||
| 1703 | # then dump the UI | ||
| 1704 | # then swipe down | ||
| 1705 | if( $self->home_screen() ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, call to ".'home_screen()'." has failed."); return undef } | ||
| 1706 | usleep(300); | ||
| 1707 | |||
| 1708 | if( $self->swipe({'direction'=>'up'}) ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, call to ".'swipe()'." has failed."); return undef } | ||
| 1709 | usleep(300); | ||
| 1710 | |||
| 1711 | my $xmlstr = $self->dump_current_screen_ui(); | ||
| 1712 | if( ! defined $xmlstr ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, call to ".'dump_current_screen_ui()'." has failed."); return undef } | ||
| 1713 | |||
| 1714 | if( $self->swipe({'direction'=>'down'}) ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, call to ".'swipe()'." has failed."); return undef } | ||
| 1715 | |||
| 1716 | my $apps = Android::ElectricSheep::Automator::XMLParsers::XMLParser_find_all_apps({ | ||
| 1717 | 'xml-string' => $xmlstr | ||
| 1718 | }); | ||
| 1719 | if( ! defined $apps ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, call to ".'Android::ElectricSheep::Automator::XMLParsers::XMLParser_find_all_apps()'." has failed."); return undef } | ||
| 1720 | |||
| 1721 | $self->{'apps-roundabout-way'} = $apps; | ||
| 1722 | |||
| 1723 | return $apps; # success | ||
| 1724 | } | ||
| 1725 | |||
| 1726 | # It opens the specified (by 'name' or 'bounds' - for tapping) app | ||
| 1727 | # and returns the a hashref with appname (only if name was specified) and bounds | ||
| 1728 | # it needs that connect_device() to have been called prior to this call | ||
| 1729 | sub open_app_roundabout_way { | ||
| 1730 | my ($self, $params) = @_; | ||
| 1731 | $params //= {}; | ||
| 1732 | |||
| 1733 | my $parent = ( caller(1) )[3] || "N/A"; | ||
| 1734 | my $whoami = ( caller(0) )[3]; | ||
| 1735 | my $log = $self->log(); | ||
| 1736 | my $verbosity = $self->verbosity(); | ||
| 1737 | |||
| 1738 | if( ! $self->is_device_connected() ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, you need to connect a device to the desktop and ALSO explicitly call ".'connect_device()'." before calling this."); return undef } | ||
| 1739 | |||
| 1740 | my $apps = $self->apps_roundabout_way(); | ||
| 1741 | if( (! defined($apps)) | ||
| 1742 | || (exists($params->{'force-reload-apps-list'}) && defined($params->{'force-reload-apps-list'}) && ($params->{'force-reload-apps-list'}>0)) | ||
| 1743 | ){ | ||
| 1744 | if( ! defined $self->find_all_apps_roundabout_way() ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, failed to load list of installed apps, call to ".'find_all_apps_roundabout_way()'." has failed."); return undef } | ||
| 1745 | $apps = $self->apps_roundabout_way(); | ||
| 1746 | } | ||
| 1747 | |||
| 1748 | my ($position, $appname, $bounds, $name); | ||
| 1749 | if( exists($params->{'name'}) && defined($name=$params->{'name'}) ){ | ||
| 1750 | my $app; | ||
| 1751 | # this is an exact match! i don't think a regex match should be allowed, | ||
| 1752 | # although a case insensitive match would be a good idea | ||
| 1753 | # so make a new hash of apps, keyed on uppercase names | ||
| 1754 | if( ! exists($apps->{$name}) | ||
| 1755 | || ! defined($app=$apps->{$name}) | ||
| 1756 | ){ | ||
| 1757 | # case insensitive match | ||
| 1758 | my $ucname = uc $name; | ||
| 1759 | my $ucapps = { map { uc $_ => $apps->{$_} } keys %$apps }; | ||
| 1760 | if( ! exists($ucapps->{$ucname}) | ||
| 1761 | || ! defined($app=$ucapps->{$ucname}) | ||
| 1762 | ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, app with name '".$params->{'name'}."' does not exist either as it is or with a case insensitive search. These are the apps found so far: '".join("', '", sort keys %$apps)."'."); return undef } | ||
| 1763 | } | ||
| 1764 | $bounds = $app->{'bounds'}; | ||
| 1765 | $appname = $app->{'name'}; | ||
| 1766 | $position = [ int(($bounds->[0] + $bounds->[2])/2), int(($bounds->[1] + $bounds->[3])/2) ]; | ||
| 1767 | } elsif( exists($params->{'position'}) ){ | ||
| 1768 | $position = $params->{'position'}; | ||
| 1769 | } else { $log->error("${whoami} (via $parent), line ".__LINE__." : error, one of 'name' or 'position' must be specified in the input parameters, but not both."); return undef } | ||
| 1770 | |||
| 1771 | # go to home, swipe up and all apps will be revealed | ||
| 1772 | # click the app (what if it is not visible?) | ||
| 1773 | # then swipe down | ||
| 1774 | if( $self->home_screen() ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, call to ".'home_screen()'." has failed."); return undef } | ||
| 1775 | if( $self->swipe({'direction'=>'up'}) ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, call to ".'swipe()'." has failed."); return undef } | ||
| 1776 | if( $self->tap({'position'=>$position}) ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, call to ".'position()'." has failed."); return undef } | ||
| 1777 | if( $self->swipe({'direction'=>'down'}) ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, call to ".'swipe()'." has failed."); return undef } | ||
| 1778 | |||
| 1779 | return { | ||
| 1780 | 'name' => $appname, # can be undef in only position was specified, TODO: we can find that too | ||
| 1781 | 'bounds' => defined($bounds) ? $bounds : [@$position, 0, 0], # this can leave x2,y2 0 | ||
| 1782 | }; # success | ||
| 1783 | } | ||
| 1784 | |||
| 1785 | sub apps { return $_[0]->{'apps'} } | ||
| 1786 | sub apps_roundabout_way { return $_[0]->{'apps-roundabout-way'} } | ||
| 1787 | sub adb { return $_[0]->{'_private'}->{'Android::ADB'} } | ||
| 1788 | sub log { return $_[0]->{'_private'}->{'log'}->{'logger-object'} } | ||
| 1789 | # returns the current verbosity level optionally setting its value | ||
| 1790 | # Value must be an integer >= 0 | ||
| 1791 | # setting a verbosity level will also spawn a chain of other debug subs, | ||
| 1792 | sub verbosity { | ||
| 1793 | my ($self, $m) = @_; | ||
| 1794 | my $log = $self->log(); | ||
| 1795 | if( defined $m ){ | ||
| 1796 | my $parent = ( caller(1) )[3] || "N/A"; | ||
| 1797 | my $whoami = ( caller(0) )[3]; | ||
| 1798 | $self->{'_private'}->{'debug'}->{'verbosity'} = $m; | ||
| 1799 | if( defined $self->adb ){ $self->adb->{'verbosity'} = $m } | ||
| 1800 | } | ||
| 1801 | return $self->{'_private'}->{'debug'}->{'verbosity'} | ||
| 1802 | } | ||
| 1803 | sub cleanup { | ||
| 1804 | my ($self, $m) = @_; | ||
| 1805 | my $log = $self->log(); | ||
| 1806 | if( defined $m ){ | ||
| 1807 | my $parent = ( caller(1) )[3] || "N/A"; | ||
| 1808 | my $whoami = ( caller(0) )[3]; | ||
| 1809 | $self->{'_private'}->{'debug'}->{'cleanup'} = $m; | ||
| 1810 | } | ||
| 1811 | return $self->{'_private'}->{'debug'}->{'cleanup'} | ||
| 1812 | } | ||
| 1813 | |||
| 1814 | # return configfile or read+check+set a configfile, | ||
| 1815 | # returns undef on failure or the configfile on success | ||
| 1816 | sub configfile { | ||
| 1817 | my ($self, $infile) = @_; | ||
| 1818 | |||
| 1819 | return $self->{'_private'}->{'configfile'} unless defined $infile; | ||
| 1820 | |||
| 1821 | my $parent = ( caller(1) )[3] || "N/A"; | ||
| 1822 | my $whoami = ( caller(0) )[3]; | ||
| 1823 | |||
| 1824 | # this can be called before the logger is created, so create a temp logger for this | ||
| 1825 | my $log = $self->log() // Mojo::Log->new(); | ||
| 1826 | |||
| 1827 | my $ch = parse_configfile($infile, $log); | ||
| 1828 | if( ! defined $ch ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, call to ".'parse_configfile()'." has failed for configuration file '$infile'."); return undef } | ||
| 1829 | |||
| 1830 | # set it in self, it will also do checks on required keys | ||
| 1831 | if( ! defined $self->confighash($ch) ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, failed to load specified confighash, , there must be errors in the configuration."); return undef } | ||
| 1832 | |||
| 1833 | $self->{'_private'}->{'configfile'} = $infile; | ||
| 1834 | |||
| 1835 | return $infile #success | ||
| 1836 | } | ||
| 1837 | |||
| 1838 | # return configfile or read+check+set a configfile, | ||
| 1839 | # returns undef on failure or the configfile on success | ||
| 1840 | sub parse_configfile { | ||
| 1841 | my ($infile, $log) = @_; | ||
| 1842 | |||
| 1843 | my $parent = ( caller(1) )[3] || "N/A"; | ||
| 1844 | my $whoami = ( caller(0) )[3]; | ||
| 1845 | |||
| 1846 | # this can be called before the logger is created, so create a temp logger for this | ||
| 1847 | $log //= Mojo::Log->new(); | ||
| 1848 | |||
| 1849 | my $ch = Config::JSON::Enhanced::config2perl({ | ||
| 1850 | 'filename' => $infile, | ||
| 1851 | 'commentstyle' => 'custom(</*)(*/>)', | ||
| 1852 | 'tags' => ['<%','%>'], | ||
| 1853 | 'variable-substitutions' => { | ||
| 1854 | 'SCRIPTDIR' => Cwd::abs_path($FindBin::Bin), | ||
| 1855 | }, | ||
| 1856 | }); | ||
| 1857 | |||
| 1858 | if( ! defined $ch ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, call to ".'Config::JSON::Enhanced::config2perl()'." has failed for configuration file '$infile'."); return undef } | ||
| 1859 | |||
| 1860 | return $ch; #success | ||
| 1861 | } | ||
| 1862 | |||
| 1863 | # returns the confighash stored or if one is supplied | ||
| 1864 | # it checks it and sets it and returns it | ||
| 1865 | # or it returns undef on failure | ||
| 1866 | # NOTE, if param is specified then we assume we do not have any configuration, | ||
| 1867 | # we do not have a logger yet, we have no configuration, no verbosity, etc. | ||
| 1868 | sub confighash { | ||
| 1869 | my ($self, $m) = @_; | ||
| 1870 | |||
| 1871 | if( ! defined $m ){ return $self->{'_private'}->{'confighash'} } | ||
| 1872 | |||
| 1873 | my $parent = ( caller(1) )[3] || "N/A"; | ||
| 1874 | my $whoami = ( caller(0) )[3]; | ||
| 1875 | |||
| 1876 | #print STDOUT "${whoami} (via $parent), line ".__LINE__." : called ...\n"; | ||
| 1877 | |||
| 1878 | # we are storing specified confighash but first check it for some fields | ||
| 1879 | # required fields: | ||
| 1880 | for ('adb', 'debug', 'logger'){ | ||
| 1881 | if( ! exists($m->{$_}) || ! defined($m->{$_}) ){ print STDERR "${whoami} (via $parent), line ".__LINE__." : error, configuration does not have key '$_'.\n"; return undef } | ||
| 1882 | } | ||
| 1883 | |||
| 1884 | my $x; | ||
| 1885 | # adb params | ||
| 1886 | $x = $m->{'adb'}; | ||
| 1887 | for ('path-to-executable'){ | ||
| 1888 | if( ! exists($x->{$_}) || ! defined($x->{$_}) ){ print STDERR "${whoami} (via $parent), line ".__LINE__." : error, configuration does not have key '$_'.\n"; return undef } | ||
| 1889 | } | ||
| 1890 | |||
| 1891 | # debug params | ||
| 1892 | $x = $m->{'debug'}; | ||
| 1893 | if( exists($x->{'verbosity'}) && defined($x->{'verbosity'}) ){ | ||
| 1894 | $self->verbosity($x->{'verbosity'}); | ||
| 1895 | } | ||
| 1896 | if( exists($x->{'cleanup'}) && defined($x->{'cleanup'}) ){ | ||
| 1897 | $self->cleanup($x->{'cleanup'}); | ||
| 1898 | } | ||
| 1899 | |||
| 1900 | # create logger if specified but only if one does not exist | ||
| 1901 | $x = $m->{'logger'}; | ||
| 1902 | if( exists($x->{'filename'}) && defined($x->{'filename'}) | ||
| 1903 | && (! defined($self->log())) | ||
| 1904 | ){ | ||
| 1905 | $self->log(Mojo::Log->new(path => $x->{'filename'})); | ||
| 1906 | } | ||
| 1907 | |||
| 1908 | # ok! | ||
| 1909 | $self->{'_private'}->{'confighash'} = $m; | ||
| 1910 | return $m | ||
| 1911 | } | ||
| 1912 | |||
| 1913 | # initialises | ||
| 1914 | # do the verbositys | ||
| 1915 | # returns 1 on failure, 0 on success | ||
| 1916 | sub init { | ||
| 1917 | my ($self, $params) = @_; | ||
| 1918 | my $parent = ( caller(1) )[3] || "N/A"; | ||
| 1919 | my $whoami = ( caller(0) )[3]; | ||
| 1920 | |||
| 1921 | # Confighash | ||
| 1922 | # first see if either user specified a config file or the default is present and read it, | ||
| 1923 | # then we will overwrite with user-specified params if any | ||
| 1924 | my ($configfile, $confighash); | ||
| 1925 | if( exists($params->{'configfile'}) && defined($configfile=$params->{'configfile'}) ){ | ||
| 1926 | if( ! -f $configfile ){ print STDERR __PACKAGE__."${whoami} (via $parent), line ".__LINE__." : error, specified configfile '$configfile' does not exist or it is not a file.\n"; return 1 } | ||
| 1927 | # this reads, creates confighash and calls confighash() which will do all the tests | ||
| 1928 | if( ! defined $self->configfile($configfile) ){ print STDERR __PACKAGE__."${whoami} (via $parent), line ".__LINE__." : error, call to ".'configfile()'." has failed for configfile '$configfile'.\n"; return 1 } | ||
| 1929 | $confighash = $self->confighash(); | ||
| 1930 | } elsif( exists($params->{'confighash'}) && defined($params->{'confighash'}) ){ | ||
| 1931 | $confighash = $params->{'confighash'}; | ||
| 1932 | # this sets the confighash and checks it too | ||
| 1933 | if( ! defined $self->confighash($confighash) ){ print STDERR __PACKAGE__."${whoami} (via $parent), line ".__LINE__." : error, call to ".'confighash()'." has failed.\n"; return 1 } | ||
| 1934 | } else { | ||
| 1935 | # use default config | ||
| 1936 | $confighash = Config::JSON::Enhanced::config2perl({ | ||
| 1937 | 'string' => $_DEFAULT_CONFIG, | ||
| 1938 | 'commentstyle' => 'custom(</*)(*/>)', | ||
| 1939 | 'tags' => ['<%','%>'], | ||
| 1940 | 'variable-substitutions' => { | ||
| 1941 | 'SCRIPTDIR' => Cwd::abs_path($FindBin::Bin), | ||
| 1942 | }, | ||
| 1943 | }); | ||
| 1944 | if( ! defined $confighash ){ print STDERR $_DEFAULT_CONFIG."\n\n".__PACKAGE__."${whoami} (via $parent), line ".__LINE__." : error, failed to parse default configuration string, above.\n"; return undef } | ||
| 1945 | if( ! defined $self->confighash($confighash) ){ print STDERR __PACKAGE__."${whoami} (via $parent), line ".__LINE__." : error, call to ".'confighash()'." has failed.\n"; return 1 } | ||
| 1946 | } | ||
| 1947 | # by now we have a confighash in self or died | ||
| 1948 | |||
| 1949 | # for creating the logger: check | ||
| 1950 | # 1. params if they have logger or logfile | ||
| 1951 | # 2. our own confighash if it contains logfile | ||
| 1952 | # 3. if all else fails, create a vanilla logger | ||
| 1953 | if( exists($params->{'logger'}) && defined($params->{'logger'}) ){ | ||
| 1954 | $self->{'_private'}->{'log'}->{'logger-object'} = $params->{'logger'}; | ||
| 1955 | #print STDOUT "${whoami} (via $parent), line ".__LINE__." : using user-supplied logger object.\n"; | ||
| 1956 | } elsif( exists($params->{'logfile'}) && defined($params->{'logfile'}) ){ | ||
| 1957 | $self->{'_private'}->{'log'}->{'logger-object'} = Mojo::Log->new(path => $params->{'logfile'}); | ||
| 1958 | #print STDOUT "${whoami} (via $parent), line ".__LINE__." : logging to file '".$params->{'logfile'}."'.\n"; | ||
| 1959 | } elsif( ! defined($self->{'_private'}->{'log'}->{'logger-object'}) ){ | ||
| 1960 | $self->{'_private'}->{'log'}->{'logger-object'} = Mojo::Log->new(); | ||
| 1961 | #print STDOUT "${whoami} (via $parent), line ".__LINE__." : a vanilla logger has been created to log to the console.\n"; | ||
| 1962 | } | ||
| 1963 | |||
| 1964 | # Now we have a logger | ||
| 1965 | my $log = $self->log(); | ||
| 1966 | $log->short(1); | ||
| 1967 | |||
| 1968 | # Set verbosity and cleanup as follows: | ||
| 1969 | # 1. check if exists in params | ||
| 1970 | # 2. check if exists in confighash | ||
| 1971 | # 3. set default value | ||
| 1972 | my $v; | ||
| 1973 | if( exists($params->{'verbosity'}) && defined($params->{'verbosity'}) ){ | ||
| 1974 | $v = $params->{'verbosity'}; | ||
| 1975 | } elsif( exists($confighash->{'debug'}) && exists($confighash->{'debug'}->{'verbosity'}) && defined($confighash->{'debug'}->{'verbosity'}) ){ | ||
| 1976 | $v = $confighash->{'debug'}->{'verbosity'}; | ||
| 1977 | } else { | ||
| 1978 | $v = 0; # default | ||
| 1979 | } | ||
| 1980 | if( $self->verbosity($v) < 0 ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, call to 'verbosity()' has failed for value '$v'."); return 1 } | ||
| 1981 | |||
| 1982 | if( exists($params->{'cleanup'}) && defined($params->{'cleanup'}) ){ | ||
| 1983 | $v = $params->{'cleanup'}; | ||
| 1984 | } elsif( exists($confighash->{'debug'}) && exists($confighash->{'debug'}->{'cleanup'}) && defined($confighash->{'debug'}->{'cleanup'}) ){ | ||
| 1985 | $v = $confighash->{'debug'}->{'cleanup'}; | ||
| 1986 | } else { | ||
| 1987 | $v = 0; # default | ||
| 1988 | } | ||
| 1989 | if( $self->cleanup($v) < 0 ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, call to 'cleanup()' has failed for value '$v'."); return 1 } | ||
| 1990 | |||
| 1991 | return 0 # success | ||
| 1992 | } | ||
| 1993 | |||
| 1994 | # initialises module-specific things, no need to copy this to other modules | ||
| 1995 | # returns 1 on failure, 0 on success | ||
| 1996 | sub init_module_specific { | ||
| 1997 | my ($self, $params) = @_; | ||
| 1998 | my $parent = ( caller(1) )[3] || "N/A"; | ||
| 1999 | my $whoami = ( caller(0) )[3]; | ||
| 2000 | my $log = $self->log(); | ||
| 2001 | my $verbosity = $self->verbosity(); | ||
| 2002 | my $confighash = $self->confighash(); | ||
| 2003 | if( $verbosity > 0 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : called ...") } | ||
| 2004 | |||
| 2005 | # set or create the Android::ADB object | ||
| 2006 | if( exists($params->{'adb'}) && defined($params->{'adb'}) ){ | ||
| 2007 | # caller supplied an existing ADB object | ||
| 2008 | $self->{'_private'}->{'Android::ADB'} = $params->{'adb'} | ||
| 2009 | } else { | ||
| 2010 | # no, we need to instantiate one, we need params in confighash | ||
| 2011 | my $pathtoadb = (exists($params->{'adb-path-to-executable'}) && defined($params->{'adb-path-to-executable'})) | ||
| 2012 | ? $params->{'adb-path-to-executable'} | ||
| 2013 | : $self->confighash->{'adb'}->{'path-to-executable'} | ||
| 2014 | ; | ||
| 2015 | if( ! -x $pathtoadb ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, specified adb executable '${pathtoadb}' is not an executable or does not exist."); return 1; } | ||
| 2016 | if( ! defined ($self->{'_private'}->{'Android::ADB'}=Android::ElectricSheep::Automator::ADB->new( | ||
| 2017 | path => $pathtoadb, | ||
| 2018 | verbosity => $self->verbosity | ||
| 2019 | )) ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, call to ".'Android::ElectricSheep::Automator::ADB->new()'." has failed (path to executable was specified as '$pathtoadb')."); return 1; } | ||
| 2020 | } | ||
| 2021 | |||
| 2022 | # does caller have a device connected to the desktop and wants us to | ||
| 2023 | # target it? | ||
| 2024 | # if just one device, we don't need serial etc: | ||
| 2025 | my $device_params; | ||
| 2026 | if( exists($params->{'device-is-connected'}) && defined($params->{'device-is-connected'}) && ($params->{'device-is-connected'}>0) ){ | ||
| 2027 | # just one device, we don't need serial of the device | ||
| 2028 | $device_params = {}; | ||
| 2029 | } elsif( exists($params->{'device-serial'}) && defined($params->{'device-serial'}) ){ | ||
| 2030 | $device_params = {'serial' => $params->{'device-serial'}}; | ||
| 2031 | } elsif( exists($params->{'device-object'}) && defined($params->{'device-object'}) ){ | ||
| 2032 | $device_params = {'device-object' => $params->{'device-object'}}; | ||
| 2033 | } | ||
| 2034 | if( defined $device_params ){ | ||
| 2035 | my $devobj = $self->connect_device($device_params); | ||
| 2036 | if( ! defined $devobj ){ $log->error(perl2dump($device_params)."${whoami} (via $parent), line ".__LINE__." : error, failed to connect to device with above parameters (call to ".'connect_device()'." has failed."); return undef } | ||
| 2037 | if( $verbosity > 0 ){ $log->info($devobj."\n${whoami} (via $parent), line ".__LINE__." : device set as above.") } | ||
| 2038 | } | ||
| 2039 | |||
| 2040 | # done | ||
| 2041 | if( $verbosity > 0 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : ".__PACKAGE__." has been initialised ...") } | ||
| 2042 | return 0 # success | ||
| 2043 | } | ||
| 2044 | |||
| 2045 | sub disconnect_device { | ||
| 2046 | my ($self, $params) = @_; | ||
| 2047 | my $parent = ( caller(1) )[3] || "N/A"; | ||
| 2048 | my $whoami = ( caller(0) )[3]; | ||
| 2049 | my $log = $self->log(); | ||
| 2050 | my $verbosity = $self->verbosity(); | ||
| 2051 | |||
| 2052 | if( $verbosity > 0 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : called ...") } | ||
| 2053 | |||
| 2054 | $self->{'device-properties'} = undef; | ||
| 2055 | $self->{'device-object'} = undef; | ||
| 2056 | |||
| 2057 | if( $verbosity > 0 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : done.") } | ||
| 2058 | return 0; # success | ||
| 2059 | } | ||
| 2060 | # prefer to return 1 0 than '', 1 | ||
| 2061 | sub is_device_connected { return defined($_[0]->{'device-properties'}) ? 1 : 0 } | ||
| 2062 | |||
| 2063 | # enquire screen properties (e.g. w, h) and save them to our $self | ||
| 2064 | # if already exists a DeviceProperties object, then we just return that | ||
| 2065 | # and do nothing UNLESS param 'force' => 1 | ||
| 2066 | # On failure it returns undef | ||
| 2067 | # On success it creates a new DeviceProperties Object which is saved in $self | ||
| 2068 | # and also returned to caller | ||
| 2069 | sub find_current_device_properties { | ||
| 2070 | my ($self, $params) = @_; | ||
| 2071 | my $parent = ( caller(1) )[3] || "N/A"; | ||
| 2072 | my $whoami = ( caller(0) )[3]; | ||
| 2073 | my $log = $self->log(); | ||
| 2074 | my $verbosity = $self->verbosity(); | ||
| 2075 | my $confighash = $self->confighash(); | ||
| 2076 | if( $verbosity > 0 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : called ...") } | ||
| 2077 | |||
| 2078 | my $sl; | ||
| 2079 | if( defined($sl=$self->device_properties()) | ||
| 2080 | || ( | ||
| 2081 | exists($params->{'force'}) | ||
| 2082 | && defined($params->{'force'}) | ||
| 2083 | && ($params->{'force'}==0) | ||
| 2084 | ) | ||
| 2085 | ){ | ||
| 2086 | # there is no need to re-enquire, we have them already | ||
| 2087 | # and no 'force' was specified | ||
| 2088 | return $sl | ||
| 2089 | } | ||
| 2090 | |||
| 2091 | $sl = Android::ElectricSheep::Automator::DeviceProperties->new({'mother'=>$self}); | ||
| 2092 | if( ! defined $sl ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, call to ".'Android::ElectricSheep::Automator::DeviceProperties->new()'." has failed."); return undef } | ||
| 2093 | if( $sl->enquire() ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, call to ".'Android::ElectricSheep::Automator::DeviceProperties->enquire()'." has failed."); return undef } | ||
| 2094 | |||
| 2095 | $self->{'device-properties'} = $sl; | ||
| 2096 | |||
| 2097 | return $sl; | ||
| 2098 | } | ||
| 2099 | |||
| 2100 | # only pod below | ||
| 2101 | =pod | ||
| 2102 | |||
| 2103 | =encoding utf8 | ||
| 2104 | |||
| 2105 | =head1 NAME | ||
| 2106 | |||
| 2107 | Android::ElectricSheep::Automator - Do Androids Dream of Electric Sheep? Smartphone control from your desktop. | ||
| 2108 | |||
| 2109 | =head1 VERSION | ||
| 2110 | |||
| 2111 | Version 0.09 | ||
| 2112 | |||
| 2113 | =head1 WARNING | ||
| 2114 | |||
| 2115 | Current distribution is extremely alpha. API may change. | ||
| 2116 | |||
| 2117 | =head1 SYNOPSIS | ||
| 2118 | |||
| 2119 | The present package fascilitates the control | ||
| 2120 | of a USB-debugging-enabled | ||
| 2121 | Android device, e.g. a real smartphone, | ||
| 2122 | or an emulated (virtual) Android device, | ||
| 2123 | from your desktop computer using Perl. | ||
| 2124 | It's basically a thickishly-thin wrapper | ||
| 2125 | to the omnipotent Android Debug Bridge (adb) | ||
| 2126 | program. | ||
| 2127 | |||
| 2128 | B<Note that absolutely nothing is | ||
| 2129 | installed on the connected device, | ||
| 2130 | neither any of its settings will be modified by this package>. | ||
| 2131 | See L</WILL ANYTHING BE INSTALLED ON THE DEVICE?>. | ||
| 2132 | |||
| 2133 | use Android::ElectricSheep::Automator; | ||
| 2134 | |||
| 2135 | my $mother = Android::ElectricSheep::Automator->new({ | ||
| 2136 | # optional as there is a default, but you may have | ||
| 2137 | # problems with the location of the adb executable | ||
| 2138 | 'configfile' => $configfile, | ||
| 2139 | 'verbosity' => 1, | ||
| 2140 | # we already have a device connected and ready to control | ||
| 2141 | 'device-is-connected' => 1, | ||
| 2142 | }); | ||
| 2143 | |||
| 2144 | # find the devices connected to desktop and set one. | ||
| 2145 | my @devices = $mother->adb->devices; | ||
| 2146 | $mother->connect_device({'serial' => $devices->[0]->serial}) | ||
| 2147 | or die; | ||
| 2148 | # no device identification is required for the method call | ||
| 2149 | # if there is only one connected device: | ||
| 2150 | $mother->connect_device() if scalar(@devices)==0; | ||
| 2151 | |||
| 2152 | # Go Home | ||
| 2153 | $mother->home_screen() or die; | ||
| 2154 | |||
| 2155 | # swipe up/down/left/right | ||
| 2156 | $mother->swipe({'direction'=>up}) or die; | ||
| 2157 | # dt is the time to swipe in millis, | ||
| 2158 | # the shorter the faster the swipe | ||
| 2159 | $mother->swipe({'direction'=>left, 'dt'=>100}) or die; | ||
| 2160 | |||
| 2161 | # tap | ||
| 2162 | $mother->tap({'position'=>[100,200]}); | ||
| 2163 | |||
| 2164 | # uses swipe() to move in screens (horizontally): | ||
| 2165 | $mother->next_screen() or die; | ||
| 2166 | $mother->previous_screen() or die; | ||
| 2167 | |||
| 2168 | # bottom navigation: | ||
| 2169 | # the "triangle" back button | ||
| 2170 | $mother->navigation_menu_back_button() or die; | ||
| 2171 | # the "circle" home button | ||
| 2172 | $mother->navigation_menu_home_button() or die; | ||
| 2173 | # the "square" overview button | ||
| 2174 | $mother->navigation_menu_overview_button() or die; | ||
| 2175 | |||
| 2176 | # open/close apps | ||
| 2177 | $mother->open_app({'package'=>qr/calendar$/i}) or die; | ||
| 2178 | $mother->close_app({'package'=>qr/calendar$/i}) or die; | ||
| 2179 | |||
| 2180 | # push pull files | ||
| 2181 | $mother->adb->pull($deviceFile, $localFile); | ||
| 2182 | $mother->adb->push($localFile, $deviceFileOrDir); | ||
| 2183 | |||
| 2184 | # guess what! | ||
| 2185 | my $xmlstr = $mother->dump_current_screen_ui(); | ||
| 2186 | |||
| 2187 | # Pull the apk(s) for an app from device and save locally | ||
| 2188 | my $res = $mother->pull_app_apk_from_device({ | ||
| 2189 | package => 'com.google.android.calendar' | ||
| 2190 | # or qr/calendar/i | ||
| 2191 | 'output-dir' => '/tmp/apks-of-calendar-app', | ||
| 2192 | }); | ||
| 2193 | print $res->{'com.google.android.calendar'}->[0]->['local-path'}; | ||
| 2194 | |||
| 2195 | # Install apk(s) for an app onto the device | ||
| 2196 | $mother->install_app({ | ||
| 2197 | 'apk-filename' => ['/tmp/apks/base.apk', '/tmp/apks/config.apk'], | ||
| 2198 | # or just a string scalar '/tmp/apks/1.apk' | ||
| 2199 | # optional params to the adb install command | ||
| 2200 | 'install-parameters' => ['-r', '-g'] | ||
| 2201 | }); | ||
| 2202 | |||
| 2203 | =head1 CONSTRUCTOR | ||
| 2204 | |||
| 2205 | =head2 B<C<new($params)>> | ||
| 2206 | |||
| 2207 | Creates a new C<Android::ElectricSheep::Automator> object. C<$params> | ||
| 2208 | is a hash reference used to pass initialization options which may | ||
| 2209 | or should include the following: | ||
| 2210 | |||
| 2211 | =over 4 | ||
| 2212 | |||
| 2213 | =item * B<C<confighash>> or B<C<configfile>> | ||
| 2214 | |||
| 2215 | the configuration | ||
| 2216 | file holds | ||
| 2217 | configuration parameters. Its format is "enhanced" JSON | ||
| 2218 | (see L<Config::JSON::Enhanced>) which is basically JSON | ||
| 2219 | which allows comments between C< E<lt>/* > and C< */E<gt> >. | ||
| 2220 | |||
| 2221 | Here is an example configuration file to get you started: | ||
| 2222 | |||
| 2223 | { | ||
| 2224 | "adb" : { | ||
| 2225 | "path-to-executable" : "/usr/local/android-sdk/platform-tools/adb" | ||
| 2226 | }, | ||
| 2227 | "debug" : { | ||
| 2228 | "verbosity" : 0, | ||
| 2229 | </* cleanup temp files on exit */> | ||
| 2230 | "cleanup" : 1 | ||
| 2231 | }, | ||
| 2232 | "logger" : { | ||
| 2233 | </* log to file if you uncomment this, else console */> | ||
| 2234 | "filename" : "my.log" | ||
| 2235 | } | ||
| 2236 | } | ||
| 2237 | |||
| 2238 | All sections in the configuration are mandatory. | ||
| 2239 | Setting C<"adb"> to the wrong path will yield problems. | ||
| 2240 | |||
| 2241 | C<confighash> is a hash of configuration options with | ||
| 2242 | structure as above and can be supplied to the constructor | ||
| 2243 | instead of the configuration file. | ||
| 2244 | |||
| 2245 | If no configuration is specified, then a default | ||
| 2246 | configuration will be used. In this case please | ||
| 2247 | specify B<C<adb-path-to-executable>> to point | ||
| 2248 | to the location of C<adb>. Most likely | ||
| 2249 | the default path will not work for you. | ||
| 2250 | |||
| 2251 | =item * B<C<adb-path-to-executable>> | ||
| 2252 | |||
| 2253 | optionally specify the path to the C<adb> executable in | ||
| 2254 | your desktop system. This will override the setting | ||
| 2255 | C< 'adb'-E<gt>'path-to-executable' > in the configuration, | ||
| 2256 | if it was provided. Use this option if you are not | ||
| 2257 | providing any configuration and so the default configuration | ||
| 2258 | will be used. But it will most likely fail because of this | ||
| 2259 | path not being correct for your system. So, if you are going | ||
| 2260 | to omit providing a configuration and the default configuration | ||
| 2261 | will be used do specify the C<adb> path via this option (but you | ||
| 2262 | don't have to and your mileage may vary). | ||
| 2263 | |||
| 2264 | =item * B<C<device-serial>> or B<C<device-object>> | ||
| 2265 | |||
| 2266 | optionally specify the serial | ||
| 2267 | of a device to connect to on instantiation, | ||
| 2268 | or a L<Android::ElectricSheep::Automator::DeviceProperties> | ||
| 2269 | object you already have handy. Alternatively, | ||
| 2270 | use L</connect_device($params)> to set the connected device at a later | ||
| 2271 | time. Note that there is no need to specify a | ||
| 2272 | device if there is exactly one connected device. | ||
| 2273 | |||
| 2274 | =item * B<C<adb>> | ||
| 2275 | |||
| 2276 | optionally specify an already created L<Android::ADB> object. | ||
| 2277 | Otherwise, a fresh object will be created based | ||
| 2278 | on the configuration under the C<adb> section of the configuration. | ||
| 2279 | |||
| 2280 | =item * B<C<device-is-connected>> | ||
| 2281 | |||
| 2282 | optionally set it to 1 | ||
| 2283 | in order to communicate with the device | ||
| 2284 | and get some information about it like | ||
| 2285 | screen size, resolution, orientation, etc. | ||
| 2286 | And also allow use of | ||
| 2287 | functionality which needs communicating with a device | ||
| 2288 | like L</swipe($params)>, L</home_screen($params)>, | ||
| 2289 | L</open_app($params)>, etc. | ||
| 2290 | After instantiation, you can use the | ||
| 2291 | method L</connect_device($params)> and | ||
| 2292 | L</disconnect_device()> for conveying | ||
| 2293 | this information to the module. | ||
| 2294 | Also note that if there are | ||
| 2295 | more than one devices connected to the desktop, make sure | ||
| 2296 | you specify which one with the C<device> parameter. | ||
| 2297 | Default value is 0. | ||
| 2298 | |||
| 2299 | =item * B<C<logger>> | ||
| 2300 | |||
| 2301 | optionally specify a logger object | ||
| 2302 | to be used (instead of creating a fresh one). This object | ||
| 2303 | must implement these methods: C<info()>, C<warn()>, C<error()>. | ||
| 2304 | L<Mojo::Log> fits perfectly. | ||
| 2305 | |||
| 2306 | =item * B<C<logfile>> | ||
| 2307 | |||
| 2308 | optionally specify a file to | ||
| 2309 | save logging output to. This overrides the C<filename> | ||
| 2310 | key under section C<logger> of the configuration. | ||
| 2311 | |||
| 2312 | =item * B<C<verbosity>> | ||
| 2313 | |||
| 2314 | optionally specify a verbosity level | ||
| 2315 | which will override what the configuration contains. Default | ||
| 2316 | is C<0>. | ||
| 2317 | |||
| 2318 | =item * B<C<cleanup>> | ||
| 2319 | |||
| 2320 | optionally specify a flag to clean up | ||
| 2321 | any temp files after exit which will override what the | ||
| 2322 | configuration contains. Default is C<1>, meaning Yes!. | ||
| 2323 | |||
| 2324 | =back | ||
| 2325 | |||
| 2326 | =head1 METHODS | ||
| 2327 | |||
| 2328 | Note: | ||
| 2329 | |||
| 2330 | =over 4 | ||
| 2331 | |||
| 2332 | =item * B<C<ARRAY_REF>> : C<my $ar = [1,2,3]; my $ar = \@ahash; my @anarray = @$ar;> | ||
| 2333 | |||
| 2334 | =item * B<C<HASH_REF>> : C<my $hr = {1=>1, 2=>2}; my $hr = \%ahash; my %ahash = %$hr;> | ||
| 2335 | |||
| 2336 | =item * In this module parameters to functions are passed as a HASH_REF. | ||
| 2337 | Functions return back objects, ARRAY_REF or HASH_REF. | ||
| 2338 | |||
| 2339 | =back | ||
| 2340 | |||
| 2341 | |||
| 2342 | =head2 B<C<devices()>> | ||
| 2343 | |||
| 2344 | Lists all Android devices connected to your | ||
| 2345 | desktop and returns these as an ARRAY_REF which can be empty. | ||
| 2346 | |||
| 2347 | It returns C<undef> on failure. | ||
| 2348 | |||
| 2349 | =head2 B<C<connect_device($params)>> | ||
| 2350 | |||
| 2351 | Specifies the current Android device to control. Its use is | ||
| 2352 | required only if you have more than one devices connected. | ||
| 2353 | C<$params> is a HASH_REF which should contain exactly | ||
| 2354 | one of the following: | ||
| 2355 | |||
| 2356 | =over 4 | ||
| 2357 | |||
| 2358 | =item * B<C<serial>> should contain | ||
| 2359 | the serial (string) of the connected device as returned | ||
| 2360 | by L</devices()>. | ||
| 2361 | |||
| 2362 | =item * B<C<device-object>> should be | ||
| 2363 | an already existing L<Android::ElectricSheep::Automator::DeviceProperties> | ||
| 2364 | object. | ||
| 2365 | |||
| 2366 | =back | ||
| 2367 | |||
| 2368 | It returns C<0> on success, C<1> on failure. | ||
| 2369 | |||
| 2370 | =head2 B<C<dump_current_screen_ui($params)>> | ||
| 2371 | |||
| 2372 | It dumps the current screen as XML and returns that as | ||
| 2373 | a string, optionally saving it to the specified file. | ||
| 2374 | |||
| 2375 | C<$params> is a HASH_REF which may or should contain: | ||
| 2376 | |||
| 2377 | =over 4 | ||
| 2378 | |||
| 2379 | =item * B<C<filename>> | ||
| 2380 | |||
| 2381 | optionally save the returned XML string to the specified file. | ||
| 2382 | |||
| 2383 | =back | ||
| 2384 | |||
| 2385 | It returns C<undef> on failure or the UI XML dump, as a string, on success. | ||
| 2386 | |||
| 2387 | =head2 B<C<dump_current_screen_shot($params)>> | ||
| 2388 | |||
| 2389 | It dumps the current screen as a PNG image and returns that as | ||
| 2390 | a L<Image::PNG> object, optionally saving it to the specified file. | ||
| 2391 | |||
| 2392 | C<$params> is a HASH_REF which may or should contain: | ||
| 2393 | |||
| 2394 | =over 4 | ||
| 2395 | |||
| 2396 | =item * B<C<filename>> | ||
| 2397 | |||
| 2398 | optionally save the returned XML string to the specified file. | ||
| 2399 | |||
| 2400 | =back | ||
| 2401 | |||
| 2402 | It returns C<undef> on failure or a L<Image::PNG> image, on success. | ||
| 2403 | |||
| 2404 | =head2 B<C<dump_current_screen_video($params)>> | ||
| 2405 | |||
| 2406 | It dumps the current screen as MP4 video and saves that | ||
| 2407 | in specified file. | ||
| 2408 | |||
| 2409 | C<$params> is a HASH_REF which may or should contain: | ||
| 2410 | |||
| 2411 | =over 4 | ||
| 2412 | |||
| 2413 | =item * B<C<filename>> | ||
| 2414 | |||
| 2415 | save the recorded video to the specified file in MP4 format. This | ||
| 2416 | is required. | ||
| 2417 | |||
| 2418 | =item * B<C<time-limit>> | ||
| 2419 | |||
| 2420 | optionally specify the duration of the recorded video, in seconds. Default is 10 seconds. | ||
| 2421 | |||
| 2422 | =item * B<C<bit-rate>> | ||
| 2423 | |||
| 2424 | optionally specify the bit rate of the recorded video in bits per second. Default is 20Mbps. | ||
| 2425 | |||
| 2426 | =item * B<C<size>> | ||
| 2427 | |||
| 2428 | optionally specify the size (geometry) of the recorded video as a | ||
| 2429 | HASH_REF with keys C<width> and C<height>, in pixels. Default is "I<the | ||
| 2430 | device's main display resolution>". | ||
| 2431 | |||
| 2432 | =item * B<C<bugreport>> | ||
| 2433 | |||
| 2434 | optionally set this flag to 1 to have Android overlay debug information | ||
| 2435 | on the recorded video, e.g. timestamp. | ||
| 2436 | |||
| 2437 | =item * B<C<display-id>> | ||
| 2438 | |||
| 2439 | for a device set up with multiple physical displays, optionally | ||
| 2440 | specify which one to record -- if not the main display -- by providing the | ||
| 2441 | display id. You can find display ids with L</list_physical_displays()> | ||
| 2442 | or, from the CLI, by C<adb shell dumpsys SurfaceFlinger --display-id> | ||
| 2443 | |||
| 2444 | =back | ||
| 2445 | |||
| 2446 | C<adb shell screenrecord --help> contains some more documentation. | ||
| 2447 | |||
| 2448 | =head2 B<C<list_physical_displays()>> | ||
| 2449 | |||
| 2450 | It lists the IDs of all the physical displays connected to the | ||
| 2451 | device, including the main one and returns these back as a HASH_REF | ||
| 2452 | keyed on display ID. It needs that connect_device() to have been called prior to this call | ||
| 2453 | |||
| 2454 | It returns C<undef> on failure or the results as a HASH_REF keyed on display ID. | ||
| 2455 | |||
| 2456 | =head2 B<C<list_running_processes($params)>> | ||
| 2457 | |||
| 2458 | It finds the running processes on device (using a `ps`), | ||
| 2459 | optionally can save the (parsed) `ps` | ||
| 2460 | results as JSON to the specified 'filename'. | ||
| 2461 | It returns C<undef> on failure or the results as a hash of hashes on success. | ||
| 2462 | |||
| 2463 | C<$params> is a HASH_REF which may or should contain: | ||
| 2464 | |||
| 2465 | =over 4 | ||
| 2466 | |||
| 2467 | =item * B<C<extra-fields>> | ||
| 2468 | |||
| 2469 | optionally add more fields (columns) to the report by C<ps>, as an ARRAY_REF. | ||
| 2470 | For example, C<['TTY','TIME']>. | ||
| 2471 | |||
| 2472 | =back | ||
| 2473 | |||
| 2474 | It needs that connect_device() to have been called prior to this call | ||
| 2475 | |||
| 2476 | It returns C<undef> on failure or a hash with these keys on success: | ||
| 2477 | |||
| 2478 | =over 4 | ||
| 2479 | |||
| 2480 | =item * B<C<raw>> : contains the raw `ps` output as a string. | ||
| 2481 | |||
| 2482 | =item * B<C<perl>> : contains the parsed raw output as a Perl hash with | ||
| 2483 | each item corresponding to one process, keyed on process command and arguments | ||
| 2484 | (as reported by C<ps>, verbatim), as a hash keyed on each field (column) | ||
| 2485 | of the C<ps> output. | ||
| 2486 | |||
| 2487 | =item * B<C<json>> : the above data converted into a JSON string. | ||
| 2488 | |||
| 2489 | =back | ||
| 2490 | |||
| 2491 | =head2 B<C<pidof($params)>> | ||
| 2492 | |||
| 2493 | It returns the PID of the specified command name. | ||
| 2494 | The specified command name must match the app or command | ||
| 2495 | name exactly. B<Use L/pgrep()> if you want to match command | ||
| 2496 | names with a regular expression>. | ||
| 2497 | |||
| 2498 | C<$params> is a HASH_REF which should contain: | ||
| 2499 | |||
| 2500 | =over 4 | ||
| 2501 | |||
| 2502 | =item * B<C<name>> | ||
| 2503 | |||
| 2504 | the name of the process. It can be a command name, | ||
| 2505 | e.g. C<audioserver> or an app name e.g. C<android.hardware.vibrator-service.example>. | ||
| 2506 | |||
| 2507 | =back | ||
| 2508 | |||
| 2509 | It returns C<undef> on failure or the PID of the matched command on success. | ||
| 2510 | |||
| 2511 | |||
| 2512 | =head2 B<C<pgrep($params)>> | ||
| 2513 | |||
| 2514 | It returns the PIDs matching the specified command or app | ||
| 2515 | name (which can be an extended regular expression that C<pgrep> | ||
| 2516 | understands). The returned array will contain zero, one or more | ||
| 2517 | hashes with keys C<pid> and C<command>. The former key is the pid of the command | ||
| 2518 | whose full name (as per the process table) will be under the latter key. | ||
| 2519 | Unless parameter C<dont-show-command-name> was set to C<1>. | ||
| 2520 | |||
| 2521 | C<$params> is a HASH_REF which should contain: | ||
| 2522 | |||
| 2523 | =over 4 | ||
| 2524 | |||
| 2525 | =item * B<C<name>> | ||
| 2526 | |||
| 2527 | the name of the process. It can be a command name, | ||
| 2528 | e.g. C<audioserver> or an app name e.g. C<android.hardware.vibrator-service.example> | ||
| 2529 | or part of these e.g. C<audio> or C<hardware> or an extended | ||
| 2530 | regular expression that Android's C<pgrep> understands, e.g. | ||
| 2531 | C<^com.+google.+mess>. | ||
| 2532 | |||
| 2533 | =back | ||
| 2534 | |||
| 2535 | It returns C<undef> on failure or an ARRAY_REF containing | ||
| 2536 | a HASH_REF of data for each command matched (under keys C<pid> and C<command>). | ||
| 2537 | The returned ARRAY_REF can contain 0, 1 or more items depending | ||
| 2538 | on what was matched. | ||
| 2539 | |||
| 2540 | |||
| 2541 | =head2 B<C<geofix($params)>> | ||
| 2542 | |||
| 2543 | It fixes the geolocation of the device to the specified coordinates. | ||
| 2544 | After this, app API calls to get current geolocation will result to this | ||
| 2545 | position (unless they use their own, roundabout way). | ||
| 2546 | |||
| 2547 | C<$params> is a HASH_REF which should contain: | ||
| 2548 | |||
| 2549 | =over 4 | ||
| 2550 | |||
| 2551 | =item * B<C<latitude>> | ||
| 2552 | |||
| 2553 | the latitude of the position as a floating point number. | ||
| 2554 | |||
| 2555 | =item * B<C<longitude>> | ||
| 2556 | |||
| 2557 | the longitude of the position as a floating point number. | ||
| 2558 | |||
| 2559 | =back | ||
| 2560 | |||
| 2561 | It returns C<1> on failure or a C<0> on success. | ||
| 2562 | |||
| 2563 | =head2 B<C<dump_current_location()>> | ||
| 2564 | |||
| 2565 | It finds the current GPS location of the device | ||
| 2566 | according to ALL the GPS providers available. | ||
| 2567 | |||
| 2568 | It needs that connect_device() to have been called prior to this call | ||
| 2569 | |||
| 2570 | It takes no parameters. | ||
| 2571 | |||
| 2572 | On failure, it returns C<undef>. | ||
| 2573 | |||
| 2574 | On success, it returns a HASH_REF of results. | ||
| 2575 | Each item will be keyed on provider name (e.g. 'C<network provider>') | ||
| 2576 | and will contain the parsed output of | ||
| 2577 | what each GPS provider returned as a HASH_REF with | ||
| 2578 | the following keys: | ||
| 2579 | |||
| 2580 | =over 4 | ||
| 2581 | |||
| 2582 | =item * B<C<provider>> : the provider name. This is also the key of the item | ||
| 2583 | in the parent hash. | ||
| 2584 | |||
| 2585 | =item * B<C<latitude>> : the latitude as a floating point number (can be negative too) | ||
| 2586 | or C< E<lt>naE<gt> > if the provider failed to return valid output. | ||
| 2587 | |||
| 2588 | =item * B<C<longitude>> : the longitude as a floating point number (can be negative too) | ||
| 2589 | or C< E<lt>na E<gt> > if the provider failed to return valid output. | ||
| 2590 | |||
| 2591 | =item * B<C<last-location-string>> : the last location string, or | ||
| 2592 | C< E<lt>na E<gt> > if the provider failed to return valid output. | ||
| 2593 | |||
| 2594 | =back | ||
| 2595 | |||
| 2596 | =head2 B<C<pull_app_apk_from_device($params)>> | ||
| 2597 | |||
| 2598 | It pulls the APK file (bytecode) for the | ||
| 2599 | app(s) matched by the specified package | ||
| 2600 | specification, | ||
| 2601 | from the device and writes them | ||
| 2602 | into the specified output directory, locally. | ||
| 2603 | |||
| 2604 | C<$params> is a HASH_REF which should contain: | ||
| 2605 | |||
| 2606 | =over 4 | ||
| 2607 | |||
| 2608 | =item * B<C<package>> | ||
| 2609 | |||
| 2610 | the package name of the app to pull from the device. | ||
| 2611 | C<package> can be a string containing the fully qualified package name | ||
| 2612 | or a C<Regexp> object. | ||
| 2613 | The former should be a fully qualified package name, for example 'C<com.android.gallery2>'. | ||
| 2614 | The latter is a regular expression matching the package name and | ||
| 2615 | can be created via the C<qr//modifiers> construct, | ||
| 2616 | for example: C<qr/gallery\d$/i>. The regular expression does not | ||
| 2617 | need to match exactly one app. Any apps matched they will be extracted. | ||
| 2618 | |||
| 2619 | =item * B<C<output-dir>> | ||
| 2620 | |||
| 2621 | the output directory to write the APK(s) matched into. | ||
| 2622 | It will be created if it does not exist. | ||
| 2623 | |||
| 2624 | =item * B<C<lazy>> | ||
| 2625 | |||
| 2626 | Optional with sane defaults, refer to L</search_app($params)> for what this does. | ||
| 2627 | |||
| 2628 | =item * B<C<force-reload-apps-list>> | ||
| 2629 | |||
| 2630 | Optional with sane defaults, refer to L</search_app($params)> for what this does. | ||
| 2631 | |||
| 2632 | =back | ||
| 2633 | |||
| 2634 | It returns C<undef> on failure. | ||
| 2635 | On success it returns a HASH_REF with as | ||
| 2636 | many items as the packages matched which | ||
| 2637 | had APK files available on the device. | ||
| 2638 | Each entry is keyed on the fully qualified | ||
| 2639 | package name and its value is a ARRAY_REF | ||
| 2640 | with as many APK files pulled from the device | ||
| 2641 | and saved locally. Each item in this array | ||
| 2642 | is a HASH_REF with keys C<device-path> | ||
| 2643 | containing the path of the APK in the device's storage | ||
| 2644 | and C<local-path> pointing to the location where | ||
| 2645 | the APK was saved, locally. | ||
| 2646 | |||
| 2647 | Note that there is a script available | ||
| 2648 | which utilises this method, see L<electric-sheep-pull-app-apk.pl>. | ||
| 2649 | |||
| 2650 | =head2 B<C<install_app($params)>> | ||
| 2651 | |||
| 2652 | It installs the app from its specified | ||
| 2653 | APK (bytecode archive and more) file. | ||
| 2654 | |||
| 2655 | C<$params> is a HASH_REF which should contain: | ||
| 2656 | |||
| 2657 | =over 4 | ||
| 2658 | |||
| 2659 | =item * B<C<apk-filename>> | ||
| 2660 | |||
| 2661 | The APK filename to install onto the device. It must | ||
| 2662 | exist locally, obviously. | ||
| 2663 | |||
| 2664 | =item * B<C<install-parameters>> | ||
| 2665 | |||
| 2666 | Optional parameters to be passed on to the C<adb install> | ||
| 2667 | command. Nothing is expected here. Refer to the | ||
| 2668 | L<adb documentation|https://developer.android.com/tools/adb> | ||
| 2669 | for what parameters are supported. For example, C<-r> is for | ||
| 2670 | re-installation of an existing app and retaining its | ||
| 2671 | previous data. | ||
| 2672 | |||
| 2673 | =back | ||
| 2674 | |||
| 2675 | It returns C<1> on failure. | ||
| 2676 | It returns C<0> on success. | ||
| 2677 | |||
| 2678 | Note that there is a script available | ||
| 2679 | which utilises this method, see L<electric-sheep-install-app.pl>. | ||
| 2680 | |||
| 2681 | |||
| 2682 | =head2 B<C<is_app_running($params)>> | ||
| 2683 | |||
| 2684 | It checks if the specified app is running on the device. | ||
| 2685 | The name of the app must be exact. | ||
| 2686 | Note that you can search for running apps / commands | ||
| 2687 | with extended regular expressions using L/pgrep()> | ||
| 2688 | |||
| 2689 | C<$params> is a HASH_REF which should contain: | ||
| 2690 | |||
| 2691 | =over 4 | ||
| 2692 | |||
| 2693 | =item * B<C<appname>> | ||
| 2694 | |||
| 2695 | the name of the app to check if it is running. | ||
| 2696 | It must be its exact name. Basically it checks the | ||
| 2697 | output of L<pidof()>. | ||
| 2698 | |||
| 2699 | =back | ||
| 2700 | |||
| 2701 | It returns C<undef> on failure, | ||
| 2702 | C<1> if the app is running or C<0> if the app is not running. | ||
| 2703 | |||
| 2704 | =head2 B<C<find_current_device_properties($params)>> | ||
| 2705 | |||
| 2706 | It enquires the device currently connected, | ||
| 2707 | and specified with L</connect_device($params)>, if needed, | ||
| 2708 | and returns back an L<Android::ElectricSheep::Automator::DeviceProperties> | ||
| 2709 | object containing this information, for example screen size, | ||
| 2710 | resolution, serial number, etc. | ||
| 2711 | |||
| 2712 | It returns L<Android::ElectricSheep::Automator::DeviceProperties> | ||
| 2713 | object on success or C<undef> on failure. | ||
| 2714 | |||
| 2715 | =head2 B<C<connect_device()>> | ||
| 2716 | |||
| 2717 | It signals to our object that there is now | ||
| 2718 | a device connected to the desktop and its | ||
| 2719 | enquiry and subsequent control can commence. | ||
| 2720 | If this is not called and neither C<device-is-connected =E<gt> 1> | ||
| 2721 | is specified as a parameter to the constructor, then | ||
| 2722 | the functionality will be limited and access | ||
| 2723 | to functions like L</swipe($params)>, L</open_app($params)>, etc. | ||
| 2724 | will be blocked until the caller signals that | ||
| 2725 | a device is now connected to the desktop. | ||
| 2726 | |||
| 2727 | Using L</connect_device($params)> to specify which device | ||
| 2728 | to target in the case of multiple devices | ||
| 2729 | connected to the desktop will also call this | ||
| 2730 | method. | ||
| 2731 | |||
| 2732 | This method will try to enquire the connected device | ||
| 2733 | about some of its properties, like screen size, | ||
| 2734 | resolution, orientation, serial number etc. | ||
| 2735 | This information will subsequently be available | ||
| 2736 | via C<$self-E<gt>>device_properties()>. | ||
| 2737 | |||
| 2738 | It returns C<0> on success, C<1> on failure. | ||
| 2739 | |||
| 2740 | =head2 B<C<disconnect_device()>> | ||
| 2741 | |||
| 2742 | Signals to our object that it should consider | ||
| 2743 | that there is currently no device connected to | ||
| 2744 | the desktop (irrespective of that is true or not) | ||
| 2745 | which will block access to L</swipe($params)>, L</open_app($params)>, etc. | ||
| 2746 | |||
| 2747 | =head2 B<C<device_properties()>> | ||
| 2748 | |||
| 2749 | It returns the currently connected device properties | ||
| 2750 | as a L<Android::ElectricSheep::Automator::DeviceProperties> | ||
| 2751 | object or C<undef> if there is no connected device. | ||
| 2752 | The returned object is constructed during a call | ||
| 2753 | to L</find_current_device_properties($params)> | ||
| 2754 | which is called via L</connect_device($params)> and will persist | ||
| 2755 | for the duration of the connection. | ||
| 2756 | However, after a call to L</disconnect_device()> | ||
| 2757 | this object will be discarded and C<undef> will be | ||
| 2758 | returned. | ||
| 2759 | |||
| 2760 | =head2 B<C<swipe($params)>> | ||
| 2761 | |||
| 2762 | Emulates a "swipe" in four directions. | ||
| 2763 | Sets the current Android device to control. It is only | ||
| 2764 | required if you have more than one device connected. | ||
| 2765 | C<$params> is a HASH_REF which may or should contain: | ||
| 2766 | |||
| 2767 | =over 4 | ||
| 2768 | |||
| 2769 | =item * B<C<direction>> | ||
| 2770 | |||
| 2771 | should be one of | ||
| 2772 | |||
| 2773 | =over 4 | ||
| 2774 | |||
| 2775 | =item up | ||
| 2776 | |||
| 2777 | =item down | ||
| 2778 | |||
| 2779 | =item left | ||
| 2780 | |||
| 2781 | =item right | ||
| 2782 | |||
| 2783 | =back | ||
| 2784 | |||
| 2785 | =item * B<C<dt>> | ||
| 2786 | |||
| 2787 | denotes the time taken for the swipe | ||
| 2788 | in milliseconds. The smaller its value the faster | ||
| 2789 | the swipe. A value of C<100> is fast enough to swipe to | ||
| 2790 | the next screen. | ||
| 2791 | |||
| 2792 | =back | ||
| 2793 | |||
| 2794 | It returns C<0> on success, C<1> on failure. | ||
| 2795 | |||
| 2796 | =head2 B<C<tap($params)>> | ||
| 2797 | |||
| 2798 | Emulates a "tap" at the specified location. | ||
| 2799 | C<$params> is a HASH_REF which must contain one | ||
| 2800 | of the following items: | ||
| 2801 | |||
| 2802 | =over 4 | ||
| 2803 | |||
| 2804 | =item * B<C<position>> | ||
| 2805 | |||
| 2806 | should be an ARRAY_REF | ||
| 2807 | as the C<X,Y> coordinates of the point to "tap". | ||
| 2808 | |||
| 2809 | =item * B<C<bounds>> | ||
| 2810 | |||
| 2811 | should be an ARRAY_REF of a bounding rectangle | ||
| 2812 | of the widget to tap. Which contains two ARRAY_REFs | ||
| 2813 | for the top-left and bottom-right coordinates, e.g. | ||
| 2814 | C< [ [tlX,tlY], [brX,brY] ] >. This is convenient | ||
| 2815 | when the widget is extracted from an XML dump of | ||
| 2816 | the UI (see L</dump_current_screen_ui($params)>) which | ||
| 2817 | contains exactly this bounding rectangle. | ||
| 2818 | |||
| 2819 | =back | ||
| 2820 | |||
| 2821 | It returns C<0> on success, C<1> on failure. | ||
| 2822 | |||
| 2823 | =head2 B<C<input_text($params)>> | ||
| 2824 | |||
| 2825 | It "C<types>" the specified text into the specified position, | ||
| 2826 | where a text-input widget is expected to exist. | ||
| 2827 | At first it taps at the widget's | ||
| 2828 | location in order to get the focus. And then it enters | ||
| 2829 | the text. You need to find the position of the desired | ||
| 2830 | text-input widget by first getting the current screen UI | ||
| 2831 | (using L</dump_current_screen_ui($params)>) and then using an XPath | ||
| 2832 | selector to identify the desired widget by name/id/attributes. | ||
| 2833 | See the source code of method L</send_message()> in file | ||
| 2834 | C<lib/Android/ElectricSheep/Automator/Plugins/Apps/Viber.pm> | ||
| 2835 | for how this is done for the message-sending text-input widget | ||
| 2836 | of the Viber app. | ||
| 2837 | |||
| 2838 | C<$params> is a HASH_REF which must contain C<text> | ||
| 2839 | and one of the two position (of the text-edit widget) | ||
| 2840 | specifiers C<position> or C<bounds>: | ||
| 2841 | |||
| 2842 | =over 4 | ||
| 2843 | |||
| 2844 | =item * B<C<text>> | ||
| 2845 | |||
| 2846 | the text to write on the text edit widget. At the | ||
| 2847 | moment, this must be plain ASCII string, not unicode. | ||
| 2848 | No spaces are accepted. | ||
| 2849 | Each space character must be replaced with C<%s>. | ||
| 2850 | |||
| 2851 | =item * B<C<position>> | ||
| 2852 | |||
| 2853 | should be an ARRAY_REF | ||
| 2854 | as the C<X,Y> coordinates of the point to "tap" in order | ||
| 2855 | to get the focus of the text edit widget, preceding the | ||
| 2856 | text input. | ||
| 2857 | |||
| 2858 | =item * B<C<bounds>> | ||
| 2859 | |||
| 2860 | should be an ARRAY_REF of a bounding rectangle | ||
| 2861 | of the widget to tap, in order to get the focus, preceding | ||
| 2862 | the text input. Which contains two ARRAY_REFs | ||
| 2863 | for the top-left and bottom-right coordinates, e.g. | ||
| 2864 | C< [ [tlX,tlY], [brX,brY] ] >. This is convenient | ||
| 2865 | when the widget is extracted from an XML dump of | ||
| 2866 | the UI (see L</dump_current_screen_ui($params)>) which | ||
| 2867 | contains exactly this bounding rectangle. | ||
| 2868 | |||
| 2869 | =back | ||
| 2870 | |||
| 2871 | It returns C<0> on success, C<1> on failure. | ||
| 2872 | |||
| 2873 | =head2 B<C<clear_input_field($params)>> | ||
| 2874 | |||
| 2875 | It clears the contents of a text-input widget | ||
| 2876 | at specified location. | ||
| 2877 | |||
| 2878 | There are several ways to do this. The simplest way | ||
| 2879 | (with C<keycombination>) does not work in some | ||
| 2880 | devices, in which case a failsafe way is employed | ||
| 2881 | which deletes characters one after the other for | ||
| 2882 | 250 times. | ||
| 2883 | |||
| 2884 | C<$params> is a HASH_REF which must contain | ||
| 2885 | one of the two position (of the text-edit widget) | ||
| 2886 | specifiers C<position> or C<bounds>: | ||
| 2887 | |||
| 2888 | =over 4 | ||
| 2889 | |||
| 2890 | =item <B<C<position>> | ||
| 2891 | |||
| 2892 | should be an ARRAY_REF | ||
| 2893 | as the C<X,Y> coordinates of the point to "tap" in order | ||
| 2894 | to get the focus of the text edit widget, preceding the | ||
| 2895 | text input. | ||
| 2896 | |||
| 2897 | =item B<C<bounds>> | ||
| 2898 | |||
| 2899 | should be an ARRAY_REF of a bounding rectangle | ||
| 2900 | of the widget to tap, in order to get the focus, preceding | ||
| 2901 | the text input. Which contains two ARRAY_REFs | ||
| 2902 | for the top-left and bottom-right coordinates, e.g. | ||
| 2903 | C< [ [tlX,tlY], [brX,brY] ] >. This is convenient | ||
| 2904 | when the widget is extracted from an XML dump of | ||
| 2905 | the UI (see L</dump_current_screen_ui($params)>) which | ||
| 2906 | contains exactly this bounding rectangle. | ||
| 2907 | |||
| 2908 | =item B<C<num-characters>> | ||
| 2909 | |||
| 2910 | how many times to press the backspace? Default is 250! | ||
| 2911 | But if you know the length of the text currently at | ||
| 2912 | the text-edit widget then enter this here. | ||
| 2913 | |||
| 2914 | =back | ||
| 2915 | |||
| 2916 | It returns C<0> on success, C<1> on failure. | ||
| 2917 | |||
| 2918 | =head2 B<C<home_screen()>> | ||
| 2919 | |||
| 2920 | Go to the "home" screen. | ||
| 2921 | |||
| 2922 | It returns C<0> on success, C<1> on failure. | ||
| 2923 | |||
| 2924 | |||
| 2925 | =head2 B<C<wake_up()>> | ||
| 2926 | |||
| 2927 | "Wake" up the device. | ||
| 2928 | |||
| 2929 | It returns C<0> on success, C<1> on failure. | ||
| 2930 | |||
| 2931 | |||
| 2932 | =head2 B<C<next_screen()>> | ||
| 2933 | |||
| 2934 | Swipe to the next screen (on the right). | ||
| 2935 | |||
| 2936 | It returns C<0> on success, C<1> on failure. | ||
| 2937 | |||
| 2938 | |||
| 2939 | =head2 B<C<previous_screen()>> | ||
| 2940 | |||
| 2941 | Swipe to the previous screen (on the left). | ||
| 2942 | |||
| 2943 | It returns C<0> on success, C<1> on failure. | ||
| 2944 | |||
| 2945 | |||
| 2946 | =head2 B<C<navigation_menu_back_button()>> | ||
| 2947 | |||
| 2948 | Press the "back" button which is the triangular | ||
| 2949 | button at the left of the navigation menu at the bottom. | ||
| 2950 | |||
| 2951 | It returns C<0> on success, C<1> on failure. | ||
| 2952 | |||
| 2953 | =head2 B<C<navigation_menu_home_button()>> | ||
| 2954 | |||
| 2955 | Press the "home" button which is the circular | ||
| 2956 | button in the middle of the navigation menu at the bottom. | ||
| 2957 | |||
| 2958 | It returns C<0> on success, C<1> on failure. | ||
| 2959 | |||
| 2960 | =head2 B<C<navigation_menu_overview_button()>> | ||
| 2961 | |||
| 2962 | Press the "overview" button which is the square | ||
| 2963 | button at the right of the navigation menu at the bottom. | ||
| 2964 | |||
| 2965 | It returns C<0> on success, C<1> on failure. | ||
| 2966 | |||
| 2967 | =head2 B<C<apps()>> | ||
| 2968 | |||
| 2969 | It returns a HASH_REF containing all the | ||
| 2970 | packages (apps) installed on the device | ||
| 2971 | keyed on package name (which is like C<com.android.settings>. | ||
| 2972 | The list of installed apps is populated either | ||
| 2973 | if C<device-is-connected> is set to 1 during construction | ||
| 2974 | or a call has been made to any of these | ||
| 2975 | methods: L</open_app($params)>, L</close_app($params)>, | ||
| 2976 | L</search_app($params)>, L</find_installed_apps($params)>. | ||
| 2977 | |||
| 2978 | =head2 B<C<find_installed_apps($params)>> | ||
| 2979 | |||
| 2980 | It enquires the device about all the installed | ||
| 2981 | packages (apps) it has for the purpose of | ||
| 2982 | opening and closing apps with L</open_app($params)> and L</close_app($params)>. | ||
| 2983 | This list is available using C<$self->apps> (where C<$self> | ||
| 2984 | is a L<Android::ElectricSheep::Automator> object. | ||
| 2985 | |||
| 2986 | Finding the package names is done in a single | ||
| 2987 | operation and does | ||
| 2988 | not take long. But enquiring with the connected device | ||
| 2989 | about the main activity/ies | ||
| 2990 | of each package takes some time as there should be | ||
| 2991 | one enquiry for each package. By default, | ||
| 2992 | L</find_installed_apps($params)> will find all the package names | ||
| 2993 | but will not enquire each package (fast). | ||
| 2994 | This enquiry will be | ||
| 2995 | done lazily if and when you need to open or close that | ||
| 2996 | app. | ||
| 2997 | |||
| 2998 | C<$params> is a HASH_REF which may or should contain: | ||
| 2999 | |||
| 3000 | =over 4 | ||
| 3001 | |||
| 3002 | =item * B<C<packages>> | ||
| 3003 | |||
| 3004 | is a list of package names to enquire | ||
| 3005 | about with the device. It can be a scalar string with the | ||
| 3006 | exact package name, e.g. C<com.android.settings>, or | ||
| 3007 | a L<Regexp> object which is a compiled regular expression | ||
| 3008 | created by e.g. C<qr/^\.com.+?\.settings$/i>, or | ||
| 3009 | an ARRAY_REF of package names. Or a HASH_REF where | ||
| 3010 | keys are package names. For each of the packages matched | ||
| 3011 | witht this specification a full enquiry will be made | ||
| 3012 | with the connected device. The information will | ||
| 3013 | be saved in a L<Android::ElectricSheep::Automator::AppProperties> | ||
| 3014 | object and will include the main activity/ies, permissions requested etc. | ||
| 3015 | |||
| 3016 | =item * B<C<lazy>> | ||
| 3017 | |||
| 3018 | is a flag to denote whether to enquire | ||
| 3019 | information about each package (app) at the time of this | ||
| 3020 | call (set it to C<1>) or lazily, on a if-and-when-needed basis | ||
| 3021 | (set it to C<0> which is the default). C<lazy> affects | ||
| 3022 | all packages except those specified in C<packages>, if any. | ||
| 3023 | Default is C<1>. | ||
| 3024 | |||
| 3025 | =item * B<C<force-reload-apps-list>> | ||
| 3026 | |||
| 3027 | can be set to 1 to | ||
| 3028 | erase previous packages information and start fresh. | ||
| 3029 | Default is C<0>. | ||
| 3030 | |||
| 3031 | =back | ||
| 3032 | |||
| 3033 | It returns a HASH_REF of packages names (keys) along | ||
| 3034 | with enquired information (as a L<Android::ElectricSheep::Automator::AppProperties> | ||
| 3035 | object) or C<undef> if this information was not | ||
| 3036 | obtained (e.g. when C<lazy> is set to 1). | ||
| 3037 | It also sets the exact same data to be available | ||
| 3038 | via C<$self->apps>. | ||
| 3039 | |||
| 3040 | =head2 B<C<search_app($params)>> | ||
| 3041 | |||
| 3042 | It searches the list of installed packages (apps) | ||
| 3043 | on the current device and returns the match(es) | ||
| 3044 | as a HASH_REF keyed on package name which may | ||
| 3045 | have as values L<Android::ElectricSheep::Automator::AppProperties> | ||
| 3046 | objects with packages information. If there are | ||
| 3047 | no entries yet in the list of installed packages, | ||
| 3048 | it calls the L</find_installed_apps($params)> first to populate it. | ||
| 3049 | |||
| 3050 | C<$params> is a HASH_REF which may or should contain: | ||
| 3051 | |||
| 3052 | =over 4 | ||
| 3053 | |||
| 3054 | =item * B<C<package>> | ||
| 3055 | |||
| 3056 | is required. It can either be | ||
| 3057 | a scalar string with the exact package name | ||
| 3058 | or a L<Regexp> object which is a compiled regular expression | ||
| 3059 | created by e.g. C<qr/^\.com.+?\.settings$/i>. | ||
| 3060 | |||
| 3061 | =item * B<C<lazy>> | ||
| 3062 | |||
| 3063 | is a flag to be passed on to L</find_installed_apps($params)>, | ||
| 3064 | if needed, to denote whether to enquire | ||
| 3065 | information about each package (app) at the time of this | ||
| 3066 | call (set it to C<1>) or lazily, on a if-and-when-needed basis | ||
| 3067 | (set it to C<0> which is the default). C<lazy> affects | ||
| 3068 | all packages except those specified in C<packages>, if any. Default is C<1>. | ||
| 3069 | |||
| 3070 | =item * B<C<force-reload-apps-list>> | ||
| 3071 | |||
| 3072 | is a flag to be passed on to L</find_installed_apps($params)>, | ||
| 3073 | if needed, and can be set to 1 to | ||
| 3074 | erase previous packages information and start fresh. Default is C<0>. | ||
| 3075 | |||
| 3076 | =back | ||
| 3077 | |||
| 3078 | It returns a HASH_REF of matched packages names (keys) along | ||
| 3079 | with enquired information (as a L<Android::ElectricSheep::Automator::AppProperties> | ||
| 3080 | object) or C<undef> if this information was not | ||
| 3081 | obtained (e.g. when C<lazy> is set to 1). | ||
| 3082 | |||
| 3083 | =head2 B<C<open_app($params)>> | ||
| 3084 | |||
| 3085 | It opens the package specified in C<$params> | ||
| 3086 | on the current device. If there are | ||
| 3087 | no entries yet in the list of installed packages, | ||
| 3088 | it calls the L</find_installed_apps($params)> first to populate it. | ||
| 3089 | It will refuse to open multiple apps matched perhaps | ||
| 3090 | by a regular expression in the package specification. | ||
| 3091 | |||
| 3092 | C<$params> is a HASH_REF which may or should contain: | ||
| 3093 | |||
| 3094 | =over 4 | ||
| 3095 | |||
| 3096 | =item * B<C<package>> | ||
| 3097 | |||
| 3098 | is required. It can either be | ||
| 3099 | a scalar string with the exact package name | ||
| 3100 | or a L<Regexp> object which is a compiled regular expression | ||
| 3101 | created by e.g. C<qr/^\.com.+?\.settings$/i>. If a regular | ||
| 3102 | expression, the call will fail if there is not | ||
| 3103 | exactly one match. | ||
| 3104 | |||
| 3105 | =item * B<C<lazy>> | ||
| 3106 | |||
| 3107 | is a flag to be passed on to L</find_installed_apps($params)>, | ||
| 3108 | if needed, to denote whether to enquire | ||
| 3109 | information about each package (app) at the time of this | ||
| 3110 | call (set it to C<1>) or lazily, on a if-and-when-needed basis | ||
| 3111 | (set it to C<0> which is the default). C<lazy> affects | ||
| 3112 | all packages except those specified in C<packages>, if any. Default is C<1>. | ||
| 3113 | |||
| 3114 | =item * B<C<force-reload-apps-list>> | ||
| 3115 | |||
| 3116 | is a flag to be passed on to L</find_installed_apps($params)>, | ||
| 3117 | if needed, and can be set to 1 to | ||
| 3118 | erase previous packages information and start fresh. Default is C<0>. | ||
| 3119 | |||
| 3120 | =back | ||
| 3121 | |||
| 3122 | It returns a HASH_REF of matched packages names (keys) along | ||
| 3123 | with enquired information (as a L<Android::ElectricSheep::Automator::AppProperties> | ||
| 3124 | object). At the moment, because L</open_app($params)> allows opening only a single app, | ||
| 3125 | this hash will contain only one entry unless we allow opening multiple | ||
| 3126 | apps (e.g. via a regex which it is already supported) in the future. | ||
| 3127 | |||
| 3128 | =head2 B<C<close_app($params)>> | ||
| 3129 | |||
| 3130 | It closes the package specified in C<$params> | ||
| 3131 | on the current device. If there are | ||
| 3132 | no entries yet in the list of installed packages, | ||
| 3133 | it calls the L</find_installed_apps($params)> first to populate it. | ||
| 3134 | It will refuse to close multiple apps matched perhaps | ||
| 3135 | by a regular expression in the package specification. | ||
| 3136 | |||
| 3137 | C<$params> is a HASH_REF which may or should contain: | ||
| 3138 | |||
| 3139 | =over 4 | ||
| 3140 | |||
| 3141 | =item * B<C<package>> | ||
| 3142 | |||
| 3143 | is required. It can either be | ||
| 3144 | a scalar string with the exact package name | ||
| 3145 | or a L<Regexp> object which is a compiled regular expression | ||
| 3146 | created by e.g. C<qr/^\.com.+?\.settings$/i>. If a regular | ||
| 3147 | expression, the call will fail if there is not | ||
| 3148 | exactly one match. | ||
| 3149 | |||
| 3150 | =item * B<C<lazy>> | ||
| 3151 | |||
| 3152 | is a flag to be passed on to L</find_installed_apps($params)>, | ||
| 3153 | if needed, to denote whether to enquire | ||
| 3154 | information about each package (app) at the time of this | ||
| 3155 | call (set it to C<1>) or lazily, on a if-and-when-needed basis | ||
| 3156 | (set it to C<0> which is the default). C<lazy> affects | ||
| 3157 | all packages except those specified in C<packages>, if any. Default is C<1>. | ||
| 3158 | |||
| 3159 | =item * B<C<force-reload-apps-list>> | ||
| 3160 | |||
| 3161 | is a flag to be passed on to L</find_installed_apps($params)>, | ||
| 3162 | if needed, and can be set to 1 to | ||
| 3163 | erase previous packages information and start fresh. Default is C<0>. | ||
| 3164 | |||
| 3165 | =back | ||
| 3166 | |||
| 3167 | It returns a HASH_REF of matched packages names (keys) along | ||
| 3168 | with enquired information (as a L<Android::ElectricSheep::Automator::AppProperties> | ||
| 3169 | object). At the moment, because L</close_app($params)> allows closing only a single app, | ||
| 3170 | this hash will contain only one entry unless we allow closing multiple | ||
| 3171 | apps (e.g. via a regex which it is already supported) in the future. | ||
| 3172 | |||
| 3173 | |||
| 3174 | =head1 SCRIPTS | ||
| 3175 | |||
| 3176 | For convenience, a few simple scripts are provided: | ||
| 3177 | |||
| 3178 | =head2 B<C<electric-sheep-find-installed-apps.pl>> | ||
| 3179 | |||
| 3180 | Find all install packages in the connected device. E.g. | ||
| 3181 | |||
| 3182 | electric-sheep-find-installed-apps.pl --configfile config/myapp.conf --device Pixel_2_API_30_x86_ --output myapps.json | ||
| 3183 | |||
| 3184 | electric-sheep-find-installed-apps.pl --configfile config/myapp.conf --device Pixel_2_API_30_x86_ --output myapps.json --fast | ||
| 3185 | |||
| 3186 | |||
| 3187 | =head2 B<C<electric-sheep-open-app.pl>> | ||
| 3188 | |||
| 3189 | Open an app by its exact name or a keyword matching it (uniquely): | ||
| 3190 | |||
| 3191 | electric-sheep-open-app.pl --configfile config/myapp.conf --name com.android.settings | ||
| 3192 | |||
| 3193 | electric-sheep-open-app.pl --configfile config/myapp.conf --keyword 'clock' | ||
| 3194 | |||
| 3195 | Note that it constructs a regular expression from escaped user input. | ||
| 3196 | |||
| 3197 | =head2 B<C<electric-sheep-close-app.pl>> | ||
| 3198 | |||
| 3199 | Close an app by its exact name or a keyword matching it (uniquely): | ||
| 3200 | |||
| 3201 | electric-sheep-close-app.pl --configfile config/myapp.conf --name com.android.settings | ||
| 3202 | |||
| 3203 | electric-sheep-close-app.pl --configfile config/myapp.conf --keyword 'clock' | ||
| 3204 | |||
| 3205 | Note that it constructs a regular expression from escaped user input. | ||
| 3206 | |||
| 3207 | =head2 B<C<electric-sheep-dump-ui.pl>> | ||
| 3208 | |||
| 3209 | Dump the current screen UI as XML to STDOUT or to a file: | ||
| 3210 | |||
| 3211 | electric-sheep-dump-ui.pl --configfile config/myapp.conf --output ui.xml | ||
| 3212 | |||
| 3213 | Note that it constructs a regular expression from escaped user input. | ||
| 3214 | |||
| 3215 | =head2 B<C<electric-sheep-dump-current-location.pl>> | ||
| 3216 | |||
| 3217 | Dump the GPS / geo-location position for the device from its various providers, if enabled. | ||
| 3218 | |||
| 3219 | electric-sheep-dump-current-location.pl --configfile config/myapp.conf --output geolocation.json | ||
| 3220 | |||
| 3221 | =head2 B<C<electric-sheep-emulator-geofix.pl>> | ||
| 3222 | |||
| 3223 | Set the GPS / geo-location position to the specified coordinates. | ||
| 3224 | |||
| 3225 | electric-sheep-dump-ui.pl --configfile config/myapp.conf --latitude 12.3 --longitude 45.6 | ||
| 3226 | |||
| 3227 | =head2 B<C<electric-sheep-dump-screen-shot.pl>> | ||
| 3228 | |||
| 3229 | Take a screenshot of the device (current screen) and save to a PNG file. | ||
| 3230 | |||
| 3231 | electric-sheep-dump-screen-shot.pl --configfile config/myapp.conf --output screenshot.png | ||
| 3232 | |||
| 3233 | =head2 B<C<electric-sheep-dump-screen-video.pl>> | ||
| 3234 | |||
| 3235 | Record a video of the device's current screen and save to an MP4 file. | ||
| 3236 | |||
| 3237 | electric-sheep-dump-screen-video.pl --configfile config/myapp.conf --output video.mp4 --time-limit 30 | ||
| 3238 | |||
| 3239 | =head2 B<C<electric-sheep-pull-app-apk.pl>> | ||
| 3240 | |||
| 3241 | Extract the APK file (java bytecode) for an app installed on the device and save locally, perhaps, for disassembly and/or modification and/or re-installation. | ||
| 3242 | |||
| 3243 | electric-sheep-pull-app-apk.pl --package calendar2 --wildcard --output anoutdir --configfile config/myapp.conf --device Pixel_2_API_30_x86_ | ||
| 3244 | |||
| 3245 | =head2 B<C<electric-sheep-install-app>> | ||
| 3246 | |||
| 3247 | Install an APK file onto the device, passing extra installation | ||
| 3248 | parameters C<-r> (for re-install) and C<-g> (for granting permissions), | ||
| 3249 | |||
| 3250 | electric-sheep-install-app --apk-filename test.apk -p '-r' -p '-g' --configfile config/myapp.conf --device Pixel_2_API_30_x86_ | ||
| 3251 | |||
| 3252 | |||
| 3253 | =head2 B<C<electric-sheep-viber-send-message.pl>> | ||
| 3254 | |||
| 3255 | Send a message using the Viber app. | ||
| 3256 | |||
| 3257 | electric-sheep-viber-send-message.pl --message 'hello%sthere' --recipient 'george' --configfile config/myapp.conf --device Pixel_2_API_30_x86_ | ||
| 3258 | |||
| 3259 | This one saves a lot of debugging information to C<debug> which can be used to | ||
| 3260 | deal with special cases or different versions of Viber: | ||
| 3261 | |||
| 3262 | electric-sheep-viber-send-message.pl --outbase debug --verbosity 1 --message 'hello%sthere' --recipient 'george' --configfile config/myapp.conf --device Pixel_2_API_30_x86_ | ||
| 3263 | |||
| 3264 | |||
| 3265 | =head1 TESTING | ||
| 3266 | |||
| 3267 | The normal tests under the C<t/> directory, initiated with C<make test> command, | ||
| 3268 | are quite limited in scope because they do not assume | ||
| 3269 | a connected device. That is, they do not check any | ||
| 3270 | functions which require interaction with a connected | ||
| 3271 | device. | ||
| 3272 | |||
| 3273 | The I<live tests> under the C<xt/live> directory, initiated with | ||
| 3274 | C<make livetest> command, require | ||
| 3275 | an Android emulator or real device (the latter B<is not recommended>) | ||
| 3276 | connected to your desktop computer on which you are doing the testing. | ||
| 3277 | Note that testing | ||
| 3278 | with your smartphone is not a good idea, please do not do this, | ||
| 3279 | unless it is some phone which you do not store important data. | ||
| 3280 | It is very easy to get an emulated Android device running on any OS. | ||
| 3281 | |||
| 3282 | So, prior to C<make livetest> make sure you have an android | ||
| 3283 | emulator up and running with, for example, | ||
| 3284 | C<emulator -avd Pixel_2_API_30_x86_> . See section | ||
| 3285 | L<Android Emulators> for how to install, list and run them | ||
| 3286 | buggers. | ||
| 3287 | |||
| 3288 | At least one of the I<author tests> under the C<xt/author> directory, | ||
| 3289 | initiated with | ||
| 3290 | C<make authortest> command, require an APK file (to be installed on the connected | ||
| 3291 | device) which is quite large and it is not included in the distribution | ||
| 3292 | bundle of this module. Anyway, it is not a good idea to install | ||
| 3293 | an unknown APK to your device. But if you want to make this test | ||
| 3294 | then pull an APK of an existing app on your connected device | ||
| 3295 | with L<electric-sheep-pull-app-apk.pl> and point the test file | ||
| 3296 | to this APK. | ||
| 3297 | |||
| 3298 | Testing will not send any messages via the device's apps. | ||
| 3299 | E.g. the plugin L<Android::ElectricSheep::Automator::Plugins::Apps::Viber> | ||
| 3300 | will not send a message via Viber but it will mock it. | ||
| 3301 | |||
| 3302 | The live tests will sometimes fail because, so far, | ||
| 3303 | something unexpected happened in the device. For example, | ||
| 3304 | in testing sending input text to a text-edit widget, | ||
| 3305 | the calendar will be opened and a new entry will be added | ||
| 3306 | and its text-edit widget will be targeted. Well, sometimes | ||
| 3307 | the calendar app will give you some notification | ||
| 3308 | on startup and this messes up with the focus. | ||
| 3309 | Other times, the OS will detect that some app is taking too | ||
| 3310 | long to launch and pops up a notification about | ||
| 3311 | "I<something is not responding, shall I close it>". | ||
| 3312 | This steals the focus and sometimes it causes | ||
| 3313 | the tests to fail. | ||
| 3314 | |||
| 3315 | =head1 PREREQUISITES | ||
| 3316 | |||
| 3317 | =head2 Android Studio | ||
| 3318 | |||
| 3319 | This is not a prerequisite but it is | ||
| 3320 | highly recommended to install it | ||
| 3321 | (from L<https://developer.android.com/studio>) | ||
| 3322 | on your desktop computer because it contains | ||
| 3323 | all the executables you will need, | ||
| 3324 | saved in a well documented file system hierarchy, | ||
| 3325 | which can then be accessed from the command line. | ||
| 3326 | You will not be using the IDE or anything, just | ||
| 3327 | the accompaniying binaries and libraries it comes with. | ||
| 3328 | |||
| 3329 | Additionally, Android Studio offers possibly the | ||
| 3330 | easiest way to create Android Virtual Devices (AVD) which emulate | ||
| 3331 | an Android phone of various specifications, phone models and sizes, | ||
| 3332 | API levels, etc. | ||
| 3333 | I mention this because one can install apps | ||
| 3334 | on an AVD and control them from your desktop | ||
| 3335 | as long as you are able to receive sms verification | ||
| 3336 | codes from a real phone. Perhaps you will need an Android | ||
| 3337 | emulator image which comes with Google Play Services, | ||
| 3338 | if you are installing apps from their store. | ||
| 3339 | This is great for | ||
| 3340 | experimenting without plugging in your real | ||
| 3341 | smartphone on your desktop. | ||
| 3342 | |||
| 3343 | The bottom line is that by installing Android Studio, | ||
| 3344 | you have all the executables you need for running things | ||
| 3345 | from the command line and, additionally, you have | ||
| 3346 | the easiest way for creating Android | ||
| 3347 | Virtual Devices, which emulate Android devices: phones, | ||
| 3348 | tablets, automotive displays. Once you have this set up, you | ||
| 3349 | will not need to open Android Studio ever again unless you | ||
| 3350 | want to update your kit. All the functionality | ||
| 3351 | will be accessible from the command line. | ||
| 3352 | |||
| 3353 | =head2 ADB | ||
| 3354 | |||
| 3355 | Android Debug Bridge (ADB) is the program | ||
| 3356 | which communicates with your smartphone or | ||
| 3357 | an Android Virtual Device from | ||
| 3358 | your desktop (Linux, osx and the unnamed C<0$>). | ||
| 3359 | |||
| 3360 | If you do not want to install Android Studio, the C<adb> executable | ||
| 3361 | is included in the package called | ||
| 3362 | "Android SDK Platform Tools" available from | ||
| 3363 | the Android official site, here: | ||
| 3364 | L<https://developer.android.com/tools/releases/platform-tools#downloads> | ||
| 3365 | |||
| 3366 | You will need the C<adb> executable to be on your path | ||
| 3367 | or specify its fullpath in the configuration file | ||
| 3368 | supplied to L<Android::ElectricSheep::Automator>'s constructor. | ||
| 3369 | |||
| 3370 | =head2 USB Debugging | ||
| 3371 | |||
| 3372 | The targeted smartphone must have "USB Debugging" enabled | ||
| 3373 | via the "Developer mode". | ||
| 3374 | This is not | ||
| 3375 | to be confused with 'rooted' or 'jailbroken' modes, none of | ||
| 3376 | these are required for experimenting with the current module. | ||
| 3377 | |||
| 3378 | In order to enable "USB Debugging", you need | ||
| 3379 | to set the smartphone to enter "Developer" mode by | ||
| 3380 | following this procedure: | ||
| 3381 | |||
| 3382 | Go to C<Settings-E<gt>System-E<gt>About Phone> | ||
| 3383 | Tap on C<Build Number> 7 times [sic!]. | ||
| 3384 | Enter your phone pin and you are in developer mode. | ||
| 3385 | |||
| 3386 | You can exit Developer Mode by going to | ||
| 3387 | C<Settings-E<gt>System-E<gt>Developer> and turn it off. | ||
| 3388 | It is highly advised to turn off Developer Mode | ||
| 3389 | for everyday use of your phone. | ||
| 3390 | B<Do not connect your smartphone | ||
| 3391 | to public WIFI networks with Developer Mode ON>. | ||
| 3392 | |||
| 3393 | B<Do not leave home with Developer Mode ON>. | ||
| 3394 | |||
| 3395 | Once you have enabled "USB Debugging", you have | ||
| 3396 | two options for making your device visible to | ||
| 3397 | your desktop and, consequently, to ADB and to this module: | ||
| 3398 | |||
| 3399 | =over 4 | ||
| 3400 | |||
| 3401 | =item * connect your android device via a USB cable | ||
| 3402 | to your desktop computer. I am not sure if you also | ||
| 3403 | need to tap on the USB charging options and allow | ||
| 3404 | "Transfer Files". | ||
| 3405 | |||
| 3406 | =item * connect your device to the same WIFI network | ||
| 3407 | as your desktop computer. Then follow instructions | ||
| 3408 | from, e.g., here L<https://developer.android.com>. | ||
| 3409 | This requires a newer Android version. | ||
| 3410 | |||
| 3411 | =back | ||
| 3412 | |||
| 3413 | =head2 Android Emulators | ||
| 3414 | |||
| 3415 | It is possible to do most things your | ||
| 3416 | smartphone does with an Android Virtual Device. | ||
| 3417 | You can install apps on the the virtual device which | ||
| 3418 | you can register by supplying your real smartphone | ||
| 3419 | number. | ||
| 3420 | |||
| 3421 | List all virtual devices currently available | ||
| 3422 | in your desktop computer, with C<emulator -list-avds> | ||
| 3423 | which outputs something like: | ||
| 3424 | |||
| 3425 | Pixel_2_API_27_x86_ | ||
| 3426 | Pixel_2_API_30_x86_ | ||
| 3427 | |||
| 3428 | Start a virtual device with C<emulator -avd Pixel_2_API_30_x86_> | ||
| 3429 | |||
| 3430 | And hey, you have an android phone running on your | ||
| 3431 | desktop in its own space, able to access the network | ||
| 3432 | but not the telephone network (no SIM card). | ||
| 3433 | |||
| 3434 | It is possible to create a virtual device | ||
| 3435 | from the command line. | ||
| 3436 | But perhaps it is easier if you download Android Studio | ||
| 3437 | from: L<https://developer.android.com/studio> and follow | ||
| 3438 | the setup there using the GUI. You will need to do this just | ||
| 3439 | once for creating the device, you can then uninstall Android Studio. | ||
| 3440 | |||
| 3441 | Android Studio will download all the | ||
| 3442 | required files and will create some Android Virtual | ||
| 3443 | Devices (the "emulators") for you. It will also be easy to | ||
| 3444 | update your stack in the future. Once you have done the above, | ||
| 3445 | you no longer need to run Android Studio except perhaps for | ||
| 3446 | checking for updates and B<all the required executables by this | ||
| 3447 | package will be available from the command line>. | ||
| 3448 | |||
| 3449 | Otherwise, download "Android SDK Platform Tools" available from | ||
| 3450 | the Android official site, here: | ||
| 3451 | L<https://developer.android.com/tools/releases/platform-tools#downloads> | ||
| 3452 | (this download is mentioned in L<ADB> if you already fetched it). | ||
| 3453 | |||
| 3454 | Fetch the required packages with this command: | ||
| 3455 | |||
| 3456 | C<sdkmanager --sdk_root=/usr/local/android-sdk "platform-tools" "platforms;android-30" "cmdline-tools;latest" "emulator"> | ||
| 3457 | |||
| 3458 | Note that C<sdkmanager --list> will list the latest android versions etc. | ||
| 3459 | |||
| 3460 | Now you should have access to C<avdmanager> executable | ||
| 3461 | (it should be located here: C</usr/local/android-sdk/cmdline-tools/latest/bin/avdmanager>) | ||
| 3462 | which you can use to create an emulator. | ||
| 3463 | |||
| 3464 | List all available android virtual devices availabe to you B<to create>: C<avdmanager list target> | ||
| 3465 | |||
| 3466 | List all available devices you can emulate: C<avdmanager list device> | ||
| 3467 | |||
| 3468 | List all available devices which have been created already and are available to boot right now: C<avdmanager list avd> | ||
| 3469 | |||
| 3470 | Create virtual device: C<avdmanager create avd -d "Nexus 6" -n myavd -k "system-images;android-29;google_apis;x86"> | ||
| 3471 | |||
| 3472 | (source: L<https://stackoverflow.com/a/77599934>) | ||
| 3473 | |||
| 3474 | In Linux, the Android emulator image files are stored | ||
| 3475 | at C<~/.config/.android/avd> and/or at C<~/.android/avd> | ||
| 3476 | Each image consists of an C<.avd> file and a C<.ini> file. | ||
| 3477 | As said before, you can boot a device with C<emulator -avd 'Pixel_9'> | ||
| 3478 | (the images will be C<Pixel_9.avd> and C<Pixel_9.ini>) | ||
| 3479 | |||
| 3480 | =head1 NO ACCESS TO GOOGLE PLAY? | ||
| 3481 | |||
| 3482 | See here if your Android Emulator has no access to Google's App Store: | ||
| 3483 | |||
| 3484 | L<https://stackoverflow.com/questions/71815181/how-can-i-get-google-play-to-work-on-android-emulator-in-android-studio-bumblebe> | ||
| 3485 | |||
| 3486 | Your mileage will lean on the low side. | ||
| 3487 | |||
| 3488 | =head1 USING YOUR REAL SMARTPHONE | ||
| 3489 | |||
| 3490 | Using your real smartphone | ||
| 3491 | with such a powerful tool may not be such | ||
| 3492 | a good idea. | ||
| 3493 | |||
| 3494 | One can only imagine what | ||
| 3495 | kind of viruses MICROSOFT WINDOWS can pass on to an | ||
| 3496 | Android device connected to it. Refrain from doing | ||
| 3497 | so unless you are using a more secure OS. | ||
| 3498 | |||
| 3499 | Start with an emulator. | ||
| 3500 | |||
| 3501 | =head1 WILL ANYTHING BE INSTALLED ON THE DEVICE? | ||
| 3502 | |||
| 3503 | Absolutely NOTHING! | ||
| 3504 | |||
| 3505 | This package | ||
| 3506 | B<does not mess with the connected device, | ||
| 3507 | neither it installs anything on it | ||
| 3508 | neither it modifies | ||
| 3509 | any of its settings>. Unless the user explicitly | ||
| 3510 | does something, e.g. explicitly | ||
| 3511 | a user installs / uninstalls apps | ||
| 3512 | programmatically using this package. | ||
| 3513 | |||
| 3514 | Unlike this Python library: | ||
| 3515 | L<https://github.com/openatx/uiautomator2>, | ||
| 3516 | (not to be confused with google's namesake), | ||
| 3517 | which sneakily installs their ADB server to your device! | ||
| 3518 | |||
| 3519 | |||
| 3520 | =head1 AUTHOR | ||
| 3521 | |||
| 3522 | Andreas Hadjiprocopis, C<< <bliako at cpan.org> >> | ||
| 3523 | |||
| 3524 | |||
| 3525 | =head1 BUGS | ||
| 3526 | |||
| 3527 | Please report any bugs or feature requests to C<bug-Android-ElectricSheep-Automator at rt.cpan.org>, or through | ||
| 3528 | the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Android-ElectricSheep-Automator>. I will be notified, and then you'll | ||
| 3529 | automatically be notified of progress on your bug as I make changes. | ||
| 3530 | |||
| 3531 | |||
| 3532 | =head1 SUPPORT | ||
| 3533 | |||
| 3534 | You can find documentation for this module with the perldoc command. | ||
| 3535 | |||
| 3536 | perldoc Android::ElectricSheep::Automator | ||
| 3537 | |||
| 3538 | |||
| 3539 | You can also look for information at: | ||
| 3540 | |||
| 3541 | =over 4 | ||
| 3542 | |||
| 3543 | =item * RT: CPAN's request tracker (report bugs here) | ||
| 3544 | |||
| 3545 | L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Android-ElectricSheep-Automator> | ||
| 3546 | |||
| 3547 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 3548 | |||
| 3549 | L<http://annocpan.org/dist/Android-ElectricSheep-Automator> | ||
| 3550 | |||
| 3551 | =item * Search CPAN | ||
| 3552 | |||
| 3553 | L<https://metacpan.org/release/Android-ElectricSheep-Automator> | ||
| 3554 | |||
| 3555 | =back | ||
| 3556 | |||
| 3557 | =head1 SEE ALSO | ||
| 3558 | |||
| 3559 | =over 4 | ||
| 3560 | |||
| 3561 | =item * L<Android::ADB> is a thin wrapper of the C<adb> command | ||
| 3562 | created by Marius Gavrilescu, C<marius@ieval.ro>. | ||
| 3563 | It is used by current module, albeit modified. | ||
| 3564 | |||
| 3565 | =back | ||
| 3566 | |||
| 3567 | =head1 HUGS | ||
| 3568 | |||
| 3569 | =over 4 | ||
| 3570 | |||
| 3571 | =item * Πτηνού, my chicken now laying in the big coop in the sky ... | ||
| 3572 | |||
| 3573 | =back | ||
| 3574 | |||
| 3575 | |||
| 3576 | =head1 LICENSE AND COPYRIGHT | ||
| 3577 | |||
| 3578 | This software is Copyright (c) 2025 by Andreas Hadjiprocopis. | ||
| 3579 | |||
| 3580 | This is free software, licensed under: | ||
| 3581 | |||
| 3582 | The Artistic License 2.0 (GPL Compatible) | ||
| 3583 | |||
| 3584 | |||
| 3585 | =cut | ||
| 3586 | |||
| 3587 | 1; # End of Android::ElectricSheep::Automator | ||