#!/usr/bin/perl -w
use Cwd;
use Win32;
use Win32::Process;
main();
sub main{
my @Emulators = GetAVDs("c:\\Documents and Settings\\Administrator.CONFIG4755VM0\\.android\\avd");
my $EmRetry = 2;
foreach my $Emulator (@Emulators){
$EmRetry = 2;
my $currentProcess = LaunchDroid("c:\\ANDROID\\android-sdk-windows\\tools",$Emulator);
ScanForAPKs("c:\\installs", $EmRetry);
KillEmulator($currentProcess);
}
}
sub GetAVDs{
my ($workdir) = shift;
my ($startdir) = &cwd; # keep track of where we began
chdir($workdir) or die "Unable to enter dir $workdir:$!\n";
opendir(DIR, ".") or die "Unable to open $workdir:$!\n";
my @names = readdir(DIR) or die "Unable to read $workdir:$!\n";
closedir(DIR);
my @ListofAVDs = ();
foreach my $name (@names){
next if ($name eq ".");
next if ($name eq "..");
if (-d $name){ # is this a directory?
$name=~ /(.*)\.avd/;
if(scalar(@ListofAVDs) == 0){
@ListofAVDs = ($1);
}
else{
push(@ListofAVDs,$1);
}
}
}
chdir($startdir) or
die "Unable to change to dir $startdir:$!\n";
print ("AVDs found are: "."@ListofAVDs"."\n");
return @ListofAVDs;
}
sub LaunchDroid{
my ($workdir) = shift;
my ($emmulatorname) = shift;
print ("Attempting to lauch ".$emmulatorname."\n");
print scalar localtime();
my ($startdir) = &cwd; # keep track of where we began
chdir($workdir) or die "Unable to enter dir $workdir:$!\n";
Win32::Process::Create($ProcessObj,"emulator.exe", " -avd ".$emmulatorname,0,NORMAL_PRIORITY_CLASS,".");
print ("\nI have now waited for the decive to be ready and will now wait for 30 seconds just to be sure\n");
print scalar localtime();
print "\n";
sleep 30;
print ("I have woken up\n"); # Ready for installing, well almost
print scalar localtime();
print "\n";
return $ProcessObj;
}
sub ScanForAPKs{
my $workdir = shift;
my $emmulatorRetryCount = shift;
my $startdir = &cwd; # keep track of where we began
chdir($workdir) or die "Unable to enter dir $workdir:$!\n";
opendir(DIR, ".") or die "Unable to open $workdir:$!\n";
my @names = readdir(DIR) or die "Unable to read $workdir:$!\n";
closedir(DIR);
my $installOutput = "";
my $retryCount = 20;
foreach my $name (@names){
next if ($name eq ".");
next if ($name eq "..");
$retryCount = 20;
if (-d $name){ # is this a directory?
&ScanForAPKs($name);
next;
}
if ( $name =~ /(.*)\.apk/ ){
#print "APK is $workdir\\$name App Name $1\n";
my $installOutput = "";
my $cmd = "c:\\ANDROID\\android-sdk-windows\\tools\\adb install -r $workdir\\$name";
print "cmd = [$cmd]\n";
do{
print "\nRetry count is currently at $retryCount\n";
alarm 0; # Reset the Alarm
print "
eval {
print "Entered eval, about to set Alaram and attemp to install.\n"
local $SIG{ALRM} = sub {die "alarm\n"};
alarm 20; # Set Timeout of 20 seconds
$installOutput = `$cmd`;
alarm 0; # If we hit this, command was successful. Turn off Alarm.
};
if ($@ ne "alarm\n") {
# timed out
print "This is the alarm or eval's output \($@\).\n";
#die unless $@ eq "alarm\n"; #extra die condition: Add later if needed.
print "Install did not time out.\n";
}
elsif ($retryCount != 1){
print "This is the alarm or eval's output \($@\).\n";
$retryCount = 2;
print "Try this install one more time.\n";
}
print "-- STDOUT = [$installOutput] \n";
print scalar localtime();
print "\n";
sleep 2;
$retryCount -= 1;
}
while ( !($installOutput =~ /.*(Success).*/ ) && !($installOutput =~ /.*(Failure).*/ ) && ($retryCount > 0) );
if($installOutput =~ /.*(Success).*/ ){
print scalar localtime();
print "\nDropping out of loop because \"Success\" was detected. \n";
}
elsif ($installOutput =~ /.*(Failure).*/){
print scalar localtime();
print "\nDropping out of loop because \"Failure\" was detected. \n";
print "We understand this as an acceptable result.\n";
}
else {
print scalar localtime();
print "\nRetry Count Exceeded. Moving on to next apk.\n";
print "Better luck next time $name.\n";
$emmulatorRetryCount -= 1;
}
if ($emmulatorRetryCount <= 0){
print scalar localtime();
print "\nEmulator Retry Count $emmulatorRetryCount \n";
print "File name $name \n";
print "\nSeems as though this Emulator didn't start.\n";
print "We're moving on to the next.\n";
last;
}
else{
print scalar localtime();
print "\nEmulator Retry Count $emmulatorRetryCount \n";
print "File name $name \n";
}
}#if ($name =~ /(.*)\.apk/ )
}#foreach
chdir($startdir) or
die "Unable to change to dir $startdir:$!\n";
}#ScanForAPKs
sub KillEmulator{
my ($processToKill) = shift;
print ("Disassemble Android\n");
print scalar localtime();
print "\n";
$processToKill->Kill(159);
system "taskkill /im emulator.exe /f";
sleep 20;
}