lib/Android/ElectricSheep/Automator/Plugins/Apps/Base.pm

filter: Statementstmt 93.1% 27 / 29 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod n/a totaltotal 94.8% 37 / 39 CC 1 SCAR 0.3
CC
1
Cov
93%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« lib/Android/ElectricSheep/Automator/ScreenLayout.pm ↑ Summary lib/Android/ElectricSheep/Automator.pm »
1 package Android::ElectricSheep::Automator::Plugins::Apps::Base;
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 1 ▶use File::Temp qw/tempfile/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use FindBin;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 ▶use Data::Roundtrip qw/perl2dump no-unicode-escape-permanently/;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 0 ▶use Android::ElectricSheep::Automator;
Subroutine BEGIN: called CC=1 SCAR=2.6
21 use Android::ElectricSheep::Automator::ScreenLayout;
22 use Android::ElectricSheep::Automator::XMLParsers;
23  
24 my $_DEFAULT_CONFIG = <<'EODC';
25 </* comments are allowed */>
26 </* and <% vars %> and <% verbatim sections %> */>
27 {
28 "Android::ElectricSheep::Automator" : {
29 "adb" : {
30 "path-to-executable" : "/usr/local/android-sdk/platform-tools/adb"
31 },
32 "debug" : {
33 "verbosity" : 0,
34 </* cleanup temp files on exit */>
35 "cleanup" : 1
36 },
37 "logger" : {
38 </* log to file if you uncomment this */>
39 </* "filename" : "..." */>
40 }
41 </* config for our plugins (each can go to separate file also) */>
42 },
43 "Android::ElectricSheep::Automator::Plugins::Apps::Viber" : {
44 }
45 }
46 EODC
47  
48 sub new {
49 my $class = ref($_[0]) || $_[0];
50 my $params = $_[1] // {};
51  
52 my $parent = ( caller(1) )[3] || "N/A";
53 my $whoami = ( caller(0) )[3];
54  
55 my $self = {
56 '_private' => {
57 'child-class' => $class,
58 'mother' => undef, # the Android::ElectricSheep::Automator object
59 'confighash' => undef,
60 'configfile' => '', # this should never be undef
61 },
62 };
63 bless $self => $class;
64  
65 # this will read the confighash, check our params, confighash params, etc.
66 # and also instantiate ua etc. and also do the verbosity etc.
67 if( $self->init($params) ){ print STDERR __PACKAGE__." (via ".$self->child_class.") : ${whoami} (via $parent), line ".__LINE__." : error, call to init() has failed.\n"; return undef }
68  
69 # do module-specific init
70 if( $self->init_module_specific($params) ){ print STDERR __PACKAGE__." (via ".$self->child_class.") : ${whoami} (via $parent), line ".__LINE__." : error, call to init_module_specific() has failed.\n"; return undef }
71  
72 # Now we have a logger
73 my $log = $self->log();
74  
75  
76 my $verbosity = $self->verbosity;
77  
78 if( $verbosity > 0 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : done, success (verbosity is set to ".$self->verbosity." and cleanup to ".$self->cleanup.").") }
79  
80 return $self;
81 }
82  
83 sub is_app_running {
84 my ($self, $params) = @_;
85 my $parent = ( caller(1) )[3] || "N/A";
86 my $whoami = ( caller(0) )[3];
87 my $log = $self->log();
88 my $verbosity = $self->verbosity();
89  
90 my $ret = $self->mother->is_app_running({'appname' => $self->appname});
91 if( ! defined $ret ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, call to ".'is_app_running()'." has failed."); return undef }
92 if( $verbosity > 0 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : app '".$self->appname."' is ".($ret>0?"":"not")." running ...") }
93 return $ret;
94 }
95 sub appname { return $_[0]->{'_private'}->{'appname'} }
96 sub open_app {
97 my ($self, $params) = @_;
98 my $parent = ( caller(1) )[3] || "N/A";
99 my $whoami = ( caller(0) )[3];
100 my $log = $self->log();
101 my $verbosity = $self->verbosity();
102  
103 my $packagename = $self->appname;
104 my $ret = $self->mother->open_app({'package' => $packagename});
105 if( ! defined $ret ){
106 my $instapps = $self->mother->find_installed_apps();
107 if( defined $instapps ){
108 $log->error("All installed apps on current device:\n".join("\n ".$_, sort keys %$instapps)."\n\n");
109 }
110 $log->error("${whoami} (via $parent), line ".__LINE__." : failed to open app '$packagename'.");
111 return undef
112 }
113 if( $verbosity > 0 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : app '$packagename' is now opening ...") }
114 return $ret;
115 }
116  
117 sub close_app {
118 my ($self, $params) = @_;
119 my $parent = ( caller(1) )[3] || "N/A";
120 my $whoami = ( caller(0) )[3];
121 my $log = $self->log();
122 my $verbosity = $self->verbosity();
123  
124 my $packagename = $self->appname;
125 my $ret = $self->mother->close_app({'package' => $packagename});
126 if( ! defined $ret ){ $log->error("${whoami} (via $parent), line ".__LINE__." : failed to close app '$packagename'."); return undef }
127 if( $verbosity > 0 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : app '$packagename' is now closing ...") }
128 return $ret;
129 }
130  
131 sub adb { return $_[0]->{'_private'}->{'Android::ADB'} }
132 sub log { return $_[0]->{'_private'}->{'log'}->{'logger-object'} }
133 sub mother { return $_[0]->{'_private'}->{'mother'} }
134 sub child_class { return $_[0]->{'_private'}->{'child-class'} }
135  
136 # returns the current verbosity level optionally setting its value
137 # Value must be an integer >= 0
138 # setting a verbosity level will also spawn a chain of other debug subs,
139 sub verbosity {
140 my ($self, $m) = @_;
141 my $log = $self->log();
142 if( defined $m ){
143 my $parent = ( caller(1) )[3] || "N/A";
144 my $whoami = ( caller(0) )[3];
145 $self->{'_private'}->{'debug'}->{'verbosity'} = $m;
146 if( defined $self->adb ){ $self->adb->{'verbosity'} = $m }
147 }
148 return $self->{'_private'}->{'debug'}->{'verbosity'}
149 }
150 sub cleanup {
151 my ($self, $m) = @_;
152 my $log = $self->log();
153 if( defined $m ){
154 my $parent = ( caller(1) )[3] || "N/A";
155 my $whoami = ( caller(0) )[3];
156 $self->{'_private'}->{'debug'}->{'cleanup'} = $m;
157 }
158 return $self->{'_private'}->{'debug'}->{'cleanup'}
159 }
160  
161 # return configfile or read+check+set a configfile,
162 # returns undef on failure or the configfile on success
163 sub configfile {
164 my ($self, $infile) = @_;
165  
166 return $self->{'_private'}->{'configfile'} unless defined $infile;
167  
168 my $parent = ( caller(1) )[3] || "N/A";
169 my $whoami = ( caller(0) )[3];
170 my $log = $self->log();
171  
172 my $ch = Android::ElectricSheep::Automator::parse_configfile($infile, $log);
173 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 }
174  
175 # set it in self, it will also do checks on required keys
176 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 }
177  
178 $self->{'_private'}->{'configfile'} = $infile;
179  
180 return $infile #success
181 }
182  
183 # returns the confighash stored or if one is supplied
184 # it checks it and sets it and returns it
185 # or it returns undef on failure
186 # NOTE, if param is specified then we assume we do not have any configuration,
187 # we do not have a logger yet, we have no configuration, no verbosity, etc.
188 sub confighash {
189 my ($self, $m) = @_;
190  
191 if( ! defined $m ){ return $self->{'_private'}->{'confighash'} }
192  
193 my $parent = ( caller(1) )[3] || "N/A";
194 my $whoami = ( caller(0) )[3];
195  
196 print STDOUT "${whoami} (via $parent), line ".__LINE__." : called ...\n";
197  
198 # the confighash must contain a mother section: 'Android::ElectricSheep::Automator'
199 # and a child section,
200 # e.g. 'Android::ElectricSheep::Automator::Plugins::Apps::Viber'
201 # check for both here:
202 for ('Android::ElectricSheep::Automator', $self->child_class){
203 if( ! exists($m->{$_}) || ! defined($m->{$_}) || (ref($m->{$_})ne'HASH') ){ print STDERR perl2dump($m)."${whoami} (via $parent), line ".__LINE__." : error, configuration (see above) does not have key '$_' or its value is not a HASHref.\n"; return undef }
204 }
205  
206 my $x = 'Android::ElectricSheep::Automator';
207 # we are storing specified confighash but first check it for some fields
208 # required fields:
209 for ('debug', 'logger'){
210 if( ! exists($m->{$x}->{$_}) || ! defined($m->{$x}->{$_}) ){ print STDERR "${whoami} (via $parent), line ".__LINE__." : error, configuration does not have key '$x'->'$_'.\n"; return undef }
211 }
212  
213 $x = $self->child_class;
214 # we are storing specified confighash but first check it for some fields
215 # required fields:
216 #for ('debug', 'logger'){
217 # if( ! exists($m->{$x}->{$_}) || ! defined($m->{$x}->{$_}) ){ print STDERR "${whoami} (via $parent), line ".__LINE__." : error, configuration does not have key '$x'->'$_'.\n"; return undef }
218 #}
219  
220 # ok!
221 $self->{'_private'}->{'confighash'} = $m;
222 return $m
223 }
224  
225 # initialises
226 # returns 1 on failure, 0 on success
227 sub init {
228 my ($self, $params) = @_;
229 my $parent = ( caller(1) )[3] || "N/A";
230 my $whoami = ( caller(0) )[3];
231  
232 # we don't have a log yet
233  
234 if( exists($params->{'child-class'}) && defined($params->{'child-class'}) ){
235 $self->{'_private'}->{'child-class'} = $params->{'child-class'};
236 } else { print STDERR "${whoami} (via $parent), line ".__LINE__." : error, input parameter 'child-class' was not specified, this is the Plugin class calling us, e.g. 'Android::ElectricSheep::Automator::Plugins::Apps::Viber'. At the moment this is not extracted from caller but needs to be specified explicitly by the caller (e.g. the Plugin class).\n"; return 1 }
237  
238 return 0 # success
239 }
240  
241 # initialises module-specific things
242 # by now we already have read confighash and have logger, verbosity etc.
243 # returns 1 on failure, 0 on success
244 sub init_module_specific {
245 my ($self, $params) = @_;
246 my $parent = ( caller(1) )[3] || "N/A";
247 my $whoami = ( caller(0) )[3];
248  
249 # Confighash
250 # first see if either user specified a config file or the default is present and read it,
251 # then we will overwrite with user-specified params if any
252 my ($configfile, $confighash);
253  
254 if( exists($params->{'configfile'}) && defined($configfile=$params->{'configfile'}) ){
255 if( ! -f $configfile ){ print STDERR __PACKAGE__." (via ".$self->child_class.") : ${whoami} (via $parent), line ".__LINE__." : error, specified configfile '$configfile' does not exist or it is not a file.\n"; return 1 }
256 # this reads, creates confighash and calls confighash() which will do all the tests
257 if( ! defined $self->configfile($configfile) ){ print STDERR __PACKAGE__." (via ".$self->child_class.") : ${whoami} (via $parent), line ".__LINE__." : error, call to ".'configfile()'." has failed for configfile '$configfile'.\n"; return 1 }
258 $confighash = $self->confighash();
259 } elsif( exists($params->{'confighash'}) && defined($params->{'confighash'}) ){
260 $confighash = $params->{'confighash'};
261 # this sets the confighash and checks it too
262 if( ! defined $self->confighash($confighash) ){ print STDERR __PACKAGE__." (via ".$self->child_class.") : ${whoami} (via $parent), line ".__LINE__." : error, call to ".'confighash()'." has failed.\n"; return 1 }
263 } else {
264 # use default config
265 $confighash = Config::JSON::Enhanced::config2perl({
266 'string' => $_DEFAULT_CONFIG,
267 'commentstyle' => 'custom(</*)(*/>)',
268 'tags' => ['<%','%>'],
269 'variable-substitutions' => {
270 'SCRIPTDIR' => Cwd::abs_path($FindBin::Bin),
271 },
272 });
273 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 }
274 if( ! defined $self->confighash($confighash) ){ print STDERR __PACKAGE__."${whoami} (via $parent), line ".__LINE__." : error, call to ".'confighash()'." has failed.\n"; return 1 }
275 }
276  
277 if( exists($params->{'mother'}) && defined($params->{'mother'}) ){
278 $self->{'_private'}->{'mother'} = $params->{'mother'};
279 } else {
280 # create the mother
281 my $mparams = { %$params };
282 delete $mparams->{'configfile'};
283 $mparams->{'confighash'} = $self->confighash->{'Android::ElectricSheep::Automator'};
284 my $m = Android::ElectricSheep::Automator->new($mparams);
285 if( ! defined $m ){ print STDERR __PACKAGE__." (via ".$self->child_class.") : ${whoami} (via $parent), line ".__LINE__." : error, failed to instantiate mother class ".'Android::ElectricSheep::Automator'.".\n"; return 1 }
286 $self->{'_private'}->{'mother'} = $m;
287 }
288 # remove the mother class config, the confighash now is all ours
289 $self->{'_private'}->{'confighash'} = $self->{'_private'}->{'confighash'}->{$self->child_class};
290  
291 # by now we have a confighash in self or died
292  
293 # for creating the logger: check
294 # 1. params if they have logger or logfile
295 # 2. our own confighash if it contains logfile
296 # 3. if all else fails, take logger from mother which does exist even if vanilla
297  
298 if( exists($params->{'logger'}) && defined($params->{'logger'}) ){
299 $self->{'_private'}->{'log'}->{'logger-object'} = $params->{'logger'};
300 #print STDOUT "${whoami} (via $parent), line ".__LINE__." : using user-supplied logger object.\n";
301 } elsif( exists($params->{'logfile'}) && defined($params->{'logfile'}) ){
302 $self->{'_private'}->{'log'}->{'logger-object'} = Mojo::Log->new(path => $params->{'logfile'});
303 #print STDOUT "${whoami} (via $parent), line ".__LINE__." : logging to file '".$params->{'logfile'}."'.\n";
304 } elsif( exists($confighash->{'logger'}->{'logfile'}) && defined($confighash->{'logger'}->{'logfile'}) ){
305 $self->{'_private'}->{'log'}->{'logger-object'} = Mojo::Log->new(path => $confighash->{'logger'}->{'logfile'});
306 #print STDOUT "${whoami} (via $parent), line ".__LINE__." : logging to file '".$confighash->{'logger'}->{'logfile'}."'.\n";
307 } else {
308 $self->{'_private'}->{'log'}->{'logger-object'} = $self->mother()->log();
309 #print STDOUT "${whoami} (via $parent), line ".__LINE__." : a vanilla logger has been inherited from mother.\n";
310 }
311  
312 # Now we have a logger
313 my $log = $self->log();
314 $log->short(1);
315  
316 my $v;
317 if( exists($params->{'verbosity'}) && defined($params->{'verbosity'}) ){
318 $v = $params->{'verbosity'};
319 } elsif( exists($confighash->{'debug'}) && exists($confighash->{'debug'}->{'verbosity'}) && defined($confighash->{'debug'}->{'verbosity'}) ){
320 $v = $confighash->{'debug'}->{'verbosity'};
321 } else {
322 $v = $self->mother()->verbosity;
323 }
324  
325 if( $self->verbosity($v) < 0 ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, call to 'verbosity()' has failed for value '$v'."); return 1 }
326  
327 if( exists($params->{'cleanup'}) && defined($params->{'cleanup'}) ){
328 $v = $params->{'cleanup'};
329 } elsif( exists($confighash->{'debug'}) && exists($confighash->{'debug'}->{'cleanup'}) && defined($confighash->{'debug'}->{'cleanup'}) ){
330 $v = $confighash->{'debug'}->{'cleanup'};
331 } else {
332 $v = $self->mother()->cleanup;
333 }
334 if( $self->cleanup($v) < 0 ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, call to 'cleanup()' has failed for value '$v'."); return 1 }
335  
336 # optional params to overwrite confighash settings,
337 # defaults exist above or in the configfile
338 if( exists($params->{'verbosity'}) && defined($params->{'verbosity'}) ){ $self->verbosity($params->{'verbosity'}) } # later we will call verbosity()
339 if( exists($params->{'cleanup'}) && defined($params->{'cleanup'}) ){ $self->cleanup($params->{'cleanup'}) }
340 else { $self->cleanup($confighash->{'debug'}->{'cleanup'}) }
341  
342 if( $self->verbosity > 0 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : ".__PACKAGE__." has been initialised ...") }
343  
344 return 0 # success
345 }
346  
347 # only pod below
348 =pod
349  
350 =head1 NAME
351  
352 Android::ElectricSheep::Automator::Plugins::Apps::Base - base class for our plugins
353  
354 =head1 VERSION
355  
356 Version 0.09
357  
358  
359 =head1 SYNOPSIS
360  
361 This is the parent class of all L<Android::ElectricSheep::Automator>
362 plugins. You do not need to override anything except perhaps the
363 constructor if you are going to be needing extra input parameters
364 to it. There is already one plugin provided L<Android::ElectricSheep::Automator::Plugins::Apps::Viber>
365 which can serve as an example for creating new plugins.
366 It is as simple as this:
367  
368 package Android::ElectricSheep::Automator::Plugins::Apps::MyNewPlugin;
369  
370 use parent 'Android::ElectricSheep::Automator::Plugins::Apps::Base';
371  
372 sub new {
373 my ($class, $params) = @_;
374 my $self = $class->SUPER::new({
375 %$params,
376 'child-class' => $class,
377 });
378 # add some extra internal fields, e.g. the name of the app
379 # we are dealing with
380 $self->{'_private'}->{'appname'} = 'com.viber.voip';
381
382 return $self;
383 }
384 # new methods
385 # getter of the app name
386 sub appname { return $_[0]->{'_private'}->{'appname'} }
387 # any methods for controlling the app, e.g.
388 sub open_viber_app {
389 my ($self, $params) = @_;
390 ...
391 }
392 ...
393  
394 Then use the plugin as:
395  
396 use Android::ElectricSheep::Automator::Plugins::Apps::MyNewPlugin;
397 my $vib = Android::ElectricSheep::Automator::Plugins::Apps::MyNewPlugin->new({
398 configfile=>'config/plugins/viber.conf',
399 'device-is-connected' => 1
400 });
401 $vib->open_app();
402 $vib->is_app_running() or ...;
403 $vib->send_message({recipient=>'My Notes', message=>'hello%sMonkees'});
404 $vib->close_app();
405  
406 =head1 AUTHOR
407  
408 Andreas Hadjiprocopis, C<< <bliako at cpan.org> >>
409  
410 =head1 BUGS
411  
412 Please report any bugs or feature requests to C<bug-android-adb-automator at rt.cpan.org>, or through
413 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Android-ADB-Automator>. I will be notified, and then you'll
414 automatically be notified of progress on your bug as I make changes.
415  
416  
417  
418  
419 =head1 SUPPORT
420  
421 You can find documentation for this module with the perldoc command.
422  
423 perldoc Android::ElectricSheep::Automator::Plugins::Apps::Base
424  
425  
426 You can also look for information at:
427  
428 =over 4
429  
430 =item * RT: CPAN's request tracker (report bugs here)
431  
432 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Android-ADB-Automator>
433  
434 =item * CPAN Ratings
435  
436 L<https://cpanratings.perl.org/d/Android-ADB-Automator>
437  
438 =item * Search CPAN
439  
440 L<https://metacpan.org/release/Android-ADB-Automator>
441  
442 =back
443  
444  
445 =head1 ACKNOWLEDGEMENTS
446  
447  
448 =head1 LICENSE AND COPYRIGHT
449  
450 This software is Copyright (c) 2025 by Andreas Hadjiprocopis.
451  
452 This is free software, licensed under:
453  
454 The Artistic License 2.0 (GPL Compatible)
455  
456  
457 =cut
458  
459 1; # End of Android::ElectricSheep::Automator::Plugins::Apps::Base
« lib/Android/ElectricSheep/Automator/ScreenLayout.pm ↑ Summary lib/Android/ElectricSheep/Automator.pm »