lib/Android/ElectricSheep/Automator/AppProperties.pm

filter: Statementstmt 88.2% 15 / 17 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 91.3% 21 / 23 CC 1 SCAR 0.5
CC
1
Cov
88%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« lib/Android/ElectricSheep/Automator/ADB/Device.pm ↑ Summary lib/Android/ElectricSheep/Automator/DeviceProperties.pm »
1 package Android::ElectricSheep::Automator::AppProperties;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 our $VERSION = '0.09';
8  
9 1 ▶use Mojo::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Config::JSON::Enhanced;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 0 ▶use XML::XPath;
Subroutine BEGIN: called CC=1 SCAR=2.6
12 use XML::LibXML;
13  
14 use Data::Roundtrip qw/perl2dump no-unicode-escape-permanently/;
15  
16 use overload ( '""' => \&toString );
17  
18 sub new {
19 my $class = $_[0];
20 my $params = $_[1] // {};
21  
22 my $parent = ( caller(1) )[3] || "N/A";
23 my $whoami = ( caller(0) )[3];
24  
25 my $self = {
26 '_private' => {
27 'logger-object' => undef,
28 'verbosity' => 0,
29 'mother' => 0,
30 },
31 'data' => {
32 # both are extracted from pkg=Package{3d33653 com.viber.voip}
33 'packageName' => '',
34 'packageId' => '',
35 # where apks are
36 'codePath' => '',
37 # path(s) to one or more apk files found under the codePath above,
38 # it can be empty []
39 'apkPaths' => [],
40 'resourcePath' => '',
41 'applicationInfo' => '',
42 'dataDir' => '',
43 'versionCode' => '',
44 'versionName' => '',
45 'flags' => [],
46 'privateFlags' => [],
47 'pkgFlags' => [],
48 'usesOptionalLibraries' => [],
49 'usesLibraryFiles' => [],
50 'timeStamp' => '',
51 'firstInstallTime' => '',
52 'lastUpdateTime' => '',
53 'signatures' => '',
54 'requestedPermissions' => [], # requested permissions
55 'installPermissions' => [],
56 'runtimePermissions' => [],
57 'declaredPermissions' => [],
58 'enabledComponents' => [],
59  
60 # activities
61 # each item is a hash with name, fully qualified name etc.
62 'MainActivity' => {}, # <<< this is an item as well if found
63 'MainActivities' => [], # only those marked as .MAIN and .LAUNCHER
64 'activities' => [], # all of them
65 }
66 };
67 bless $self => $class;
68  
69 # we need a mother object (Android::ElectricSheep::Automator)
70 if( (! exists $params->{'mother'})
71 || (! defined $params->{'mother'})
72 || (! defined $params->{'mother'}->adb())
73 ){ print STDERR "${whoami} (via $parent), line ".__LINE__." : error, input parameter 'mother' with our parent Android::ElectricSheep::Automator object was not specified.\n"; return undef }
74 $self->{'_private'}->{'mother'} = $params->{'mother'};
75 # we now have a mother
76 my $mother = $self->mother();
77  
78 if( exists $params->{'logger-object'} ){
79 $self->{'_private'}->{'logger-object'} = $params->{'logger-object'}
80 } else { $self->{'_private'}->{'logger-object'} = $self->mother->log }
81 # we now have a log
82 my $log = $self->log;
83  
84 if( exists $params->{'verbosity'} ){
85 $self->{'_private'}->{'verbosity'} = $params->{'verbosity'}
86 } else { $self->{'_private'}->{'verbosity'} = $self->mother->verbosity }
87 # we now have verbosity
88 my $verbosity = $self->verbosity;
89  
90 # caller can specify some initial data to load
91 # OR caller can specify a string which is the output of a dumpsys (for 1 package)
92 # to parse it
93 # else caller must run ->enquire() to enquire the real device at a later time
94 if( exists $params->{'data'} ){
95 my $d = $self->{'data'};
96 my $p = $params->{'data'};
97 for my $k (sort keys %$d){
98 if( exists($p->{$k}) && defined($p->{$k}) ){
99 if( $self->set($k, $p->{$k}) ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, call to ".'set()'." has failed for input parameter '$k', is its type as expected (".ref($d->{$k}).")?"); return undef }
100 }
101 }
102 } elsif( exists $params->{'package'} ){
103 # we have a package name, we will enquire about it and we
104 # fill us up with its info
105 if( 1 == $self->enquire({'package' => $params->{'package'}}) ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, failed to enquire package '".$params->{'package'}."' (call to ".'enquire()'." has failed)."); return undef }
106 }
107  
108 my $packagename = $self->get('packageName');
109 my $codepath = $self->get('codePath');
110 # and now also find the package's apk dir (where its apks are)
111 # we already have 'codePath'
112  
113 # adb shell pm path com.android.gallery2 will output:
114 # package:/product/app/Gallery2/Gallery2.apk
115 # but there may be other apks there, so why not
116 # adb shell ls "codePath"/*.apk
117 #my @cmd = ('pm', 'path', $k);
118 if( $verbosity > 0 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : finding the APK files for app '$packagename' (code path: '$codepath' ...") }
119 if( ! defined($codepath) || ($codepath=~/^\s*$/) ){ $log->warn("${whoami} (via $parent), line ".__LINE__." : warning, did not find any 'codePath' for app '".$self->get('packageName')."', no problem."); }
120 else {
121 # we have a codePath, perhaps we will find apks in there
122 # but is it really a dir? it can be an apk (ending in apk)
123 my $apkf = ($codepath =~ /\.apk$/i)
124 ? $codepath
125 : File::Spec->catfile($codepath, '*.apk')
126 ;
127 my @cmd = ('ls', '-al', $apkf);
128 if( $verbosity > 0 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : sending command to adb: @cmd") }
129 my $res = $self->mother->adb->shell(@cmd);
130 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 }
131 if( $res->[0] != 0 ){ $log->error(join(" ", @cmd)."\n${whoami} (via $parent), line ".__LINE__." : error, above shell command has failed, with:\nsSTDOUT:\n".$res->[1]."\n\nSTDERR:\n".$res->[2]."\nEND."); return undef }
132 my @apks;
133 # sieving through the output of ls -al
134 # we have the mod time (e.g. 12:21) and then the filename
135 while( $res->[1] =~ m!\d+\:\d+\s+(.+?)\s*$!gsm ){
136 push @apks, $1;
137 }
138 $self->set('apkPaths', \@apks);
139 }
140  
141 return $self;
142 }
143  
144 # this is a factory method (static) which first does:
145 # adb shell 'dumpsys package packages #<< 1 operation
146 # to find all package names installed on device.
147 # If 'lazy'==1 then it stops there, returning only the package names
148 # (and not AppProperties associated with each package name).
149 # If 'lazy'==0 then for each package installed on device OR each
150 # package the caller specified ('packages'), it creates an
151 # AppProperties object (which does a dumpsys for each package name)
152 # filled up with info, like MainActivity, permissions etc.
153 # If 'packages' it was specified, then a list of ALL packages
154 # installed on device will be returned where those in 'packages'
155 # will have an AppProperties object associated with them. The rest
156 # will have an undef value.
157 # In the case where 'packages' was specified, 'lazy' value
158 # will affect only those packages not in 'packages'.
159 # If no 'packages' were specified then a hash of all installed
160 # packages will be returned. With:
161 # 1. If 'lazy'==1 then the values will be undef.
162 # 2. If 'lazy'==0 then each package will have an AppProperties value
163 # associated with it. This can be slow (a few seconds) because
164 # it does a dumpsys for each package.
165 # Default 'lazy' value is 1. And 'lazy'=0 for 'packages'.
166 # Input parameters:
167 # 'packages' => 'packagename' or regex (e.g. qr//) or [...] or { ...} :
168 # optionally specify one or more packages to load non-lazily, all other
169 # packages will be loaded lazily. Default is to load all packages lazily.
170 # an item in 'packages' can be a string for exact package name match or
171 # a compiled regex (qr//).
172 # NOTE: at first all packages will be listed and no AppProperties will
173 # be created unless 'lazy'==0
174 # Therefore specifying 'packages' means instantiate their
175 # AppProperties object, irrespective of the 'lazy' parameter.
176 # 'lazy' => 0 or 1 : optionally specify whether to load the 'packages' lazily
177 # which means to have their name in the returned hash but the value
178 # (the AppProperties object) will be undef, leaving instantiation for when needed.
179 # Default is lazy=1 (and lazy=0 for those items in 'packages')
180 # NOTE: lazy will be 0 for the packages specified in 'packages' if any.
181 # So either specify lazy=0 for getting info on all packages installed (takes some time!)
182 # or set 'packages' to include those package names you want non-lazy, letting all other lazy.
183 # It returns undef on failure
184 # It returns %{ AppProperties objects } keyed on the app name (packageName),
185 # on success. The values of the hash can be undef if lazy>0
186 # meaning that they will be left to be enquired when someone needs them
187 # e.g. in an open_app() call.
188 sub enquire_installed_apps_factory {
189 my $params = $_[0] // {};
190  
191 my $parent = ( caller(1) )[3] || "N/A";
192 my $whoami = ( caller(0) )[3];
193  
194 # we need a mother object (Android::ElectricSheep::Automator)
195 if( (! exists $params->{'mother'})
196 || (! defined $params->{'mother'})
197 || (! defined $params->{'mother'}->adb())
198 ){ print STDERR "${whoami} (via $parent), line ".__LINE__." : error, input parameter 'mother' with our parent Android::ElectricSheep::Automator object was not specified.\n"; return undef }
199 my $mother = $params->{'mother'};
200 # we now have a mother
201  
202 my $log = $mother->log;
203 my $verbosity = $mother->verbosity;
204  
205 # default is to be lazy (except for 'packages' if any)
206 my $lazy = (exists($params->{'lazy'}) && defined($params->{'lazy'})) ? $params->{'lazy'} : 1;
207  
208 # if the user has specified packages then we will load just those non-lazily
209 my ($packages, @packages_arr);
210 if( exists($params->{'packages'}) && defined($packages=$params->{'packages'}) ){
211 # packages can be a single package name
212 my $rr = ref($packages);
213 if( ($rr eq '') || ($rr eq 'Regexp') ){ @packages_arr = ($packages) }
214 elsif( $rr eq 'ARRAY' ){ @packages_arr = @$packages }
215 elsif( $rr eq 'HASH' ){ @packages_arr = ( map { $_ } grep { $packages->{$_} > 0 } keys %$packages ) }
216 else { $log->error("${whoami} (via $parent), line ".__LINE__." : error, input parameter 'packages' must be a scalar string (for specifying just one package) or a regexp (Regexp type for compiled (".'qr//'.") regexes or an ARRAYref or a HASHref of package names and not '$rr'."); return undef }
217 }
218 my $N_packages = scalar(@packages_arr);
219 # by now we have packages as a HASHref or undef and they can contain regex or string package names.
220  
221 if( $verbosity > 0 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : called ...") }
222  
223 # NOTE
224 # dumpsys package packages
225 # and
226 # dumpsys package com.example.xyz
227 # will yield different things for the package com.example.xyz
228 # we need to enquire twice, 1 for all the package names
229 # 2 for the full content of the package info, including activities
230 # we will dumpsys specifically for that package
231  
232 # here we could also save to a file on device and then
233 # fetch it locally. We will do that if there are problems
234 # getting the dump from STDOUT
235 my @cmd = ('dumpsys', 'package', 'packages');
236 my $res = $mother->adb->shell(@cmd);
237 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 }
238 if( $res->[0] != 0 ){ $log->error(join(" ", @cmd)."\n${whoami} (via $parent), line ".__LINE__." : error, above shell command has failed, with:\nsSTDOUT:\n".$res->[1]."\n\nSTDERR:\n".$res->[2]."\nEND."); return undef }
239 my $content = $res->[1];
240  
241 my %apps;
242 while( $content =~ /^\s{2}Package\s+\[(.+?)\]\s+\(.+?\)\:[\r\n](.+?)(?:[\r\n]\s{2}[^ ]|[\r\n]$|\z)/smg ){
243 # if you want the package content:
244 #while( $content =~ /^(\s{2}Package\s+\[(.+?)\]\s+\(.+?\)\:[\r\n].+?)(?:[\r\n]\s{2}[^ ]|[\r\n]$|\z)/smg ){
245 #my $package_contents = $1; and name $2
246 my $package_name = $1; # full package name, e.g. com.google.android.calendar
247 # we will be lazy
248 my $is_this_lazy = $lazy;
249 if( $lazy == 1 ){
250 for my $ap (@packages_arr){
251 if( ref($ap) eq '' ){
252 if( $package_name eq $ap ){ $is_this_lazy = 0; last }
253 } else {
254 if( $package_name =~ $ap ){ $is_this_lazy = 0; last }
255 }
256 }
257 }
258 if( $is_this_lazy == 0 ){
259 my $app = Android::ElectricSheep::Automator::AppProperties->new({
260 'package' => $package_name,
261 'mother' => $mother,
262 });
263 if( ! defined $app ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, call to ".'Android::ElectricSheep::Automator::AppProperties->new()'." has failed."); return undef }
264 if( $verbosity > 1 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : enquired app '".$app->get('packageName')."' successfully and instantiated its AppProperties object.") }
265 $apps{$package_name} = $app;
266 } else {
267 $apps{$package_name} = undef; # <<< lazy instantiate if&when needed
268 if( $verbosity > 0 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : registered installed app '${package_name}' successfully (but not instantiated AppProperties).") }
269 }
270 }
271 # done
272 if( $verbosity > 0 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : enquired ".scalar(keys %apps)." apps.") }
273 return \%apps;
274 }
275  
276 # does a adb shell dumpsys and reads various things from it
277 # it may also do a adb shell wm density
278 # returns 0 on success, 1 on failure
279 sub enquire {
280 my ($self, $params) = @_;
281 $params //= {};
282  
283 my $parent = ( caller(1) )[3] || "N/A";
284 my $whoami = ( caller(0) )[3];
285 my $log = $self->log;
286 my $verbosity = $self->verbosity;
287  
288 # NOTE
289 # dumpsys package packages
290 # and
291 # dumpsys package com.example.xyz
292 # will yield different things for the package com.example.xyz
293 # we need to enquire twice, 1 for all the package names
294 # 2 for the full content of the package info, including activities
295 # we will dumpsys specifically for that package
296 my $package;
297 if( ! exists($params->{'package'}) || ! defined($package=$params->{'package'}) ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, input parameter 'package' (the package name e.g. com.example.calendar) was not specified. If you want to enquire all installed apps, use the factory method ".'enquire_installed_apps_factory()'." in current package. A package name is not required if you provide the dumpsys for a specific package via the 'string' parameter."); return 1 }
298  
299 if( $verbosity > 0 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : package '${package}' : called ...") }
300  
301 # here we could also save to a file on device and then
302 # fetch it locally. We will do that if there are problems
303 # getting the dump from STDOUT
304 my @cmd = ('dumpsys', 'package', $package);
305 my $res = $self->adb->shell(@cmd);
306 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 }
307 if( $res->[0] != 0 ){ $log->error(join(" ", @cmd)."\n${whoami} (via $parent), line ".__LINE__." : error, above shell command has failed, with:\nsSTDOUT:\n".$res->[1]."\n\nSTDERR:\n".$res->[2]."\nEND."); return undef }
308 my $content = $res->[1];
309  
310 if( $content =~ /^\s{2}Package.+?\:[\r\n].+?\s{4}pkg=Package\{(.+?) (.+?)\}[\r\n]/sm ){
311 $self->set('packageId', $1);
312 $self->set('packageName', $2);
313 } else { $log->error("${whoami} (via $parent), line ".__LINE__." : package '${package}' : error, failed to find 'packageName' and 'packageId'."); return 1 }
314  
315 if( $content =~ /^\s{2}Package.+?\:[\r\n].+?\s{4}codePath=(.+?)[\r\n]/sm ){
316 $self->set('codePath', $1);
317 } else { $log->error("${whoami} (via $parent), line ".__LINE__." : package '${package}' : error, failed to find 'codePath'."); return 1 }
318  
319 if( $content =~ /^\s{2}Package.+?\:[\r\n].+?\s{4}codePath=(.+?)[\r\n]/sm ){
320 $self->set('codePath', $1);
321 } else { $log->error("${whoami} (via $parent), line ".__LINE__." : package '${package}' : error, failed to find 'codePath'."); return 1 }
322  
323 if( $content =~ /^\s{2}Package.+?\:[\r\n].+?\s{4}resourcePath=(.+?)[\r\n]/sm ){
324 $self->set('resourcePath', $1);
325 } else { $log->error("${whoami} (via $parent), line ".__LINE__." : package '${package}' : error, failed to find 'resourcePath'."); return 1 }
326  
327 if( $content =~ /^\s{2}Package.+?\:[\r\n].+?\s{4}codePath=(.+?)[\r\n]/sm ){
328 $self->set('codePath', $1);
329 } else { $log->error("${whoami} (via $parent), line ".__LINE__." : package '${package}' : error, failed to find 'codePath'."); return 1 }
330  
331 if( $content =~ /^\s{2}Package.+?\:[\r\n].+?\s{4}versionCode=(.+?)[\r\n]/sm ){
332 $self->set('versionCode', $1);
333 } else { $log->error("${whoami} (via $parent), line ".__LINE__." : package '${package}' : error, failed to find 'versionCode'."); return 1 }
334  
335 if( $content =~ /^\s{2}Package.+?\:[\r\n].+?\s{4}versionName=(.+?)[\r\n]/sm ){
336 $self->set('versionName', $1);
337 } else { $log->error("${whoami} (via $parent), line ".__LINE__." : package '${package}' : error, failed to find 'versionName'."); return 1 }
338  
339 if( $content =~ /^\s{2}Package.+?\:[\r\n].+?\s{4}flags=\[\s*(.+?)\s*\][\r\n]/sm ){
340 $self->set('flags', [ split(/\s+/, $1) ]);
341 } else { $log->error("${whoami} (via $parent), line ".__LINE__." : package '${package}' : error, failed to find 'flags'."); return 1 }
342  
343 if( $content =~ /^\s{2}Package.+?\:[\r\n].+?\s{4}privateFlags=\[\s*(.+?)\s*\][\r\n]/sm ){
344 $self->set('privateFlags', [ split(/\s+/, $1) ]);
345 } else { $log->error("${whoami} (via $parent), line ".__LINE__." : package '${package}' : error, failed to find 'privateFlags'."); return 1 }
346  
347 if( $content =~ /^\s{2}Package.+?\:[\r\n].+?\s{4}pkgFlags=\[\s*(.+?)\s*\][\r\n]/sm ){
348 $self->set('pkgFlags', [ split(/\s+/, $1) ]);
349 } else { $log->error("${whoami} (via $parent), line ".__LINE__." : package '${package}' : error, failed to find 'pkgFlags'."); return 1 }
350  
351 if( $content =~ /^\s{2}Package.+?\:[\r\n].+?\s{4}usesOptionalLibraries\:[\r\n](.+?)[\r\n]\s{4}[^ ]/sm ){
352 $self->set('usesOptionalLibraries', [ map { $_ =~ s/\s+//g; $_ } split(/[\r\n]+/, $1) ]);
353 } else { $self->set('usesOptionalLibraries', [] ); } # optional
354  
355 if( $content =~ /^\s{2}Package.+?\:[\r\n].+?\s{4}usesLibraryFiles\:[\r\n](.+?)[\r\n]\s{4}[^ ]/sm ){
356 $self->set('usesLibraryFiles', [ map { $_ =~ s/\s+//g; $_ } split(/\[\r\n]+/, $1) ]);
357 } else { $self->set('usesLibraryFiles', [] ); } # optional
358  
359 if( $content =~ /^\s{2}Package.+?\:[\r\n].+?\s{4}timeStamp=(.+?)[\r\n]/sm ){
360 $self->set('timeStamp', $1);
361 } else { $log->error("${whoami} (via $parent), line ".__LINE__." : package '${package}' : error, failed to find 'timeStamp'."); return 1 }
362  
363 if( $content =~ /^\s{2}Package.+?\:[\r\n].+?\s{4}firstInstallTime=(.+?)[\r\n]/sm ){
364 $self->set('firstInstallTime', $1);
365 } else { $log->error("${whoami} (via $parent), line ".__LINE__." : package '${package}' : error, failed to find 'firstInstallTime'."); return 1 }
366  
367 if( $content =~ /^\s{2}Package.+?\:[\r\n].+?\s{4}lastUpdateTime=(.+?)[\r\n]/sm ){
368 $self->set('lastUpdateTime', $1);
369 } else { $log->error("${whoami} (via $parent), line ".__LINE__." : package '${package}' : error, failed to find 'lastUpdateTime'."); return 1 }
370  
371 if( $content =~ /^\s{2}Package.+?\:[\r\n].+?\s{4}signatures=(.+?)[\r\n]/sm ){
372 $self->set('signatures', $1);
373 } else { $log->error("${whoami} (via $parent), line ".__LINE__." : package '${package}' : error, failed to find 'signatures'."); return 1 }
374  
375 if( $content =~ /^\s{2}Package.+?\:[\r\n].+?\s{4}declared permissions\:[\r\n]\s*(.+?)\s*[\r\n]\s{4}[^ ]/sm ){
376 $self->set('declaredPermissions', [ split(/\s*[\r\n]+\s*/, $1) ]);
377 } else { $self->set('declaredPermissions', [] ); } # optional
378  
379 if( $content =~ /^\s{2}Package.+?\:[\r\n].+?\s{4}requested permissions\:[\r\n]\s*(.+?)\s*[\r\n]\s{4}[^ ]/sm ){
380 $self->set('requestedPermissions', [ split(/\s*[\r\n]+\s*/, $1) ]);
381 } else { $self->set('requestedPermissions', [] ); } # optional
382  
383 if( $content =~ /^\s{2}Package.+?\:[\r\n].+?\s{4}install permissions\:[\r\n]\s*(.+?)\s*[\r\n]\s{4}[^ ]/sm ){
384 $self->set('installPermissions', [ split(/\s*[\r\n]+\s*/, $1) ]);
385 } else { $self->set('installPermissions', [] ); } # optional
386  
387 if( $content =~ /^\s{2}Package.+?\:[\r\n].+?\s{4}runtime permissions\:[\r\n]\s*(.+?)\s*[\r\n]\s{4}[^ ]/sm ){
388 $self->set('runtimePermissions', [ split(/\s*[\r\n]+\s*/, $1) ]);
389 } else { $self->set('runtimePermissions', [] ); } # optional
390  
391 if( $content =~ /^\s{2}Package.+?\:[\r\n].+?\s{4}enabledComponents\:[\r\n]\s*(.+?)(?:[\r\n]\s{6}[^\s]+|[\r\n]{2}|\z)/sm ){
392 $self->set('enabledComponents', [ split(/\s*[\r\n]+\s*/, $1) ]);
393 } else { $self->set('enabledComponents', [] ); } # optional
394  
395 # now parse all the activities
396 if( $content =~ /^Activity Resolver Table:[\r\n].+?[\r\n]\s{6}android.intent.action.MAIN\:[\r\n](.+?)(?:[\r\n]\s{6}[^ ]|[\r\n]{2}|\z)/sm ){
397 my $sc = $1;
398 my @acts;
399 while( $sc =~ /^\s{8}(.+?)[\r\n]\s{10}Action: "(.+?)"[\r\n]\s{10}Category: "(.+?)"/gsm ){
400 my $nameall = $1;
401 my $action = $2;
402 my $category = $3;
403 my ($fullname) = $nameall =~ /^.+?\s+(.+?)\s+/;
404 my ($name) = $fullname =~ /\/(.+?)$/;
405 push @acts, {
406 'name-fully-qualified' => $fullname,
407 'name' => $name,
408 'name-all' => $nameall,
409 'action' => $action,
410 'category' => $category
411 }
412 }
413 $self->set('activities', \@acts);
414 } else { $self->set('activities', [] ); } # optional
415  
416 # parse the main activities only (1 or more)
417 if( $content =~ /^Activity Resolver Table\:[\r\n].+?[\r\n]\s{6}android.intent.action.MAIN\:[\r\n](.+?)(?:[\r\n]\s{6}[^ ]|[\r\n]{2}|\z)/sm ){
418 # we now have
419 # fb2df64 com.viber.voip/.WelcomeActivity filter a91c5cd
420 # Action: "android.intent.action.MAIN"
421 # Category: "android.intent.category.LAUNCHER"
422 # eb6c1f4 com.viber.voip/.settings.ui.SettingsHeadersActivity filter 9a11$
423 # Action: "android.intent.action.MAIN"
424 # Category: "android.intent.category.NOTIFICATION_PREFERENCES"
425 # and we need the first, not the second
426 my $sc = $1;
427 my @acts;
428 while( $sc =~ /^\s{8}(.+?)[\r\n]\s{10}Action\: "(android\.intent\.action\.MAIN)"[\r\n]\s{10}Category\: "(android\.intent\.category\.LAUNCHER)"/gsm ){
429 my $nameall = $1;
430 my $action = $2;
431 my $category = $3;
432 my ($fullname) = $nameall =~ /^.+?\s+(.+?)\s+/;
433 my ($name) = $fullname =~ /\/(.+?)$/;
434 push @acts, {
435 'name-fully-qualified' => $fullname,
436 'name' => $name,
437 'name-all' => $nameall,
438 'action' => $action,
439 'category' => $category
440 }
441 }
442 $self->set('MainActivities', \@acts);
443 } else {
444 $self->set('MainActivities', [] );
445 if( $verbosity > 1 ){ $log->warn("${whoami} (via $parent), line ".__LINE__." : package '${package}' : warning, failed to find at least one MainActivity!") }
446 }
447  
448 # choose one of the main activities for being the
449 # one when we open the app.
450 # Heuristic: just pick the shortest activity name which
451 # does not contain dots except the startinh dot:
452 my $ma = $self->get('MainActivities')->[0];
453 my $l = length $ma->{'name'};
454 for my $v (@{ $self->get('MainActivities') }){
455 my $n = $v->{'name'};
456 my $c = $n =~ tr/\.//;
457 next if $c > 1;
458 $c = length $n;
459 if( $l > $c ){ $l = $c; $ma = $v };
460 }
461 $self->set('MainActivity', $ma);
462  
463 # done!
464 if( $verbosity > 0 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : package '${package}' : done.") }
465 return 0; # success
466 }
467  
468 sub get { return $_[0]->has($_[1]) ? $_[0]->{'data'}->{$_[1]} : undef }
469 sub set {
470 # set a new value even if it is not in our store,
471 # but if it is, then check the types match
472 if( exists($_[0]->{'data'}->{$_[1]})
473 && (ref($_[2]) ne ref($_[0]->{'data'}->{$_[1]}))
474 ){ $_[0]->log()->error(__PACKAGE__."::set(), line ".__LINE__." : error, the type of parameter '$_[1]' is '".ref($_[2])."' but '".ref($_[0]->{'data'}->{$_[1]})."' was expected."); return 1 }
475 $_[0]->{'data'}->{$_[1]} = $_[2];
476 return 0; # success
477 }
478 sub has { exists $_[0]->{'data'}->{$_[1]} }
479  
480 sub toString {
481 # unfortunately as a hash it is unsorted
482 return perl2dump($_[0]->{'data'}, {terse=>1,pretty=>1});
483 }
484 sub toJSON { return perl2json($_[0]->{'data'}, {pretty=>1}); }
485 sub TO_JSON { return $_[0]->{'data'} }
486  
487 sub log { return $_[0]->{'_private'}->{'logger-object'} }
488 sub verbosity { return $_[0]->{'_private'}->{'verbosity'} }
489 sub mother { return $_[0]->{'_private'}->{'mother'} }
490 sub adb { return $_[0]->mother->adb }
491  
492 # only pod below
493 =pod
494  
495 =head1 NAME
496  
497 Android::ElectricSheep::Automator - The great new Android::ElectricSheep::Automator!
498  
499 =head1 VERSION
500  
501 Version 0.09
502  
503  
504 =head1 SYNOPSIS
505  
506 Quick summary of what the module does.
507  
508 Perhaps a little code snippet.
509  
510 use Android::ElectricSheep::Automator;
511  
512 my $foo = Android::ElectricSheep::Automator->new();
513 ...
514  
515 =head1 EXPORT
516  
517 A list of functions that can be exported. You can delete this section
518 if you don't export anything, such as for a purely object-oriented module.
519  
520 =head1 SUBROUTINES/METHODS
521  
522 =head2 function1
523  
524  
525  
526 =head2 function2
527  
528  
529 =head1 AUTHOR
530  
531 Andreas Hadjiprocopis, C<< <bliako at cpan.org> >>
532  
533 =head1 BUGS
534  
535 Please report any bugs or feature requests to C<bug-android-adb-automator at rt.cpan.org>, or through
536 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Android-ADB-Automator>. I will be notified, and then you'll
537 automatically be notified of progress on your bug as I make changes.
538  
539  
540  
541  
542 =head1 SUPPORT
543  
544 You can find documentation for this module with the perldoc command.
545  
546 perldoc Android::ElectricSheep::Automator
547  
548  
549 You can also look for information at:
550  
551 =over 4
552  
553 =item * RT: CPAN's request tracker (report bugs here)
554  
555 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Android-ADB-Automator>
556  
557 =item * CPAN Ratings
558  
559 L<https://cpanratings.perl.org/d/Android-ADB-Automator>
560  
561 =item * Search CPAN
562  
563 L<https://metacpan.org/release/Android-ADB-Automator>
564  
565 =back
566  
567  
568 =head1 ACKNOWLEDGEMENTS
569  
570  
571 =head1 LICENSE AND COPYRIGHT
572  
573 This software is Copyright (c) 2025 by Andreas Hadjiprocopis.
574  
575 This is free software, licensed under:
576  
577 The Artistic License 2.0 (GPL Compatible)
578  
579  
580 =cut
581  
582 1; # End of Android::ElectricSheep::Automator
« lib/Android/ElectricSheep/Automator/ADB/Device.pm ↑ Summary lib/Android/ElectricSheep/Automator/DeviceProperties.pm »