1. #!/usr/bin/perl -w
  2. use Cwd;
  3. use Win32;
  4. use Win32::Process;
  5.  
  6. main();
  7.  
  8. sub main{
  9.     my @Emulators = GetAVDs("c:\\Documents and Settings\\Administrator.CONFIG4755VM0\\.android\\avd");
  10.     my $EmRetry = 2;
  11.     foreach my $Emulator (@Emulators){
  12.         $EmRetry = 2;
  13.         my $currentProcess = LaunchDroid("c:\\ANDROID\\android-sdk-windows\\tools",$Emulator);
  14.         ScanForAPKs("c:\\installs", $EmRetry);
  15.         KillEmulator($currentProcess);
  16.     }
  17. }
  18.  
  19. sub GetAVDs{
  20.     my ($workdir) = shift;
  21.     my ($startdir) = &cwd; # keep track of where we began
  22.  
  23.     chdir($workdir) or die "Unable to enter dir $workdir:$!\n";
  24.     opendir(DIR, ".") or die "Unable to open $workdir:$!\n";
  25.     my @names = readdir(DIR) or die "Unable to read $workdir:$!\n";
  26.     closedir(DIR);
  27.  
  28.     my @ListofAVDs = ();
  29.  
  30.     foreach my $name (@names){
  31.         next if ($name eq ".");
  32.         next if ($name eq "..");
  33.  
  34.         if (-d $name){                  # is this a directory?
  35.             $name=~ /(.*)\.avd/;
  36.             if(scalar(@ListofAVDs) == 0){
  37.                 @ListofAVDs = ($1);
  38.             }
  39.             else{
  40.                 push(@ListofAVDs,$1);
  41.             }
  42.         }
  43.     }
  44.  
  45.     chdir($startdir) or
  46.     die "Unable to change to dir $startdir:$!\n";
  47.     print ("AVDs found are: "."@ListofAVDs"."\n");
  48.     return @ListofAVDs;
  49. }
  50.  
  51. sub LaunchDroid{
  52.     my ($workdir) = shift;
  53.     my ($emmulatorname) = shift;
  54.     print ("Attempting to lauch ".$emmulatorname."\n");
  55.     print scalar localtime();
  56.     my ($startdir) = &cwd; # keep track of where we began
  57.     chdir($workdir) or die "Unable to enter dir $workdir:$!\n";
  58.  
  59.     Win32::Process::Create($ProcessObj,"emulator.exe", " -avd ".$emmulatorname,0,NORMAL_PRIORITY_CLASS,".");
  60.  
  61.     print ("\nI have now waited for the decive to be ready and will now wait for 30 seconds just to be sure\n");
  62.     print scalar localtime();
  63.     print "\n";
  64.     sleep 30;
  65.     print ("I have woken up\n"); # Ready for installing, well almost
  66.     print scalar localtime();
  67.     print "\n";
  68.     return $ProcessObj;
  69. }
  70.  
  71. sub ScanForAPKs{
  72.     my $workdir = shift;
  73.     my $emmulatorRetryCount = shift;
  74.     my $startdir = &cwd; # keep track of where we began
  75.  
  76.     chdir($workdir) or die "Unable to enter dir $workdir:$!\n";
  77.     opendir(DIR, ".") or die "Unable to open $workdir:$!\n";
  78.     my @names = readdir(DIR) or die "Unable to read $workdir:$!\n";
  79.     closedir(DIR);
  80.  
  81.     my $installOutput = "";
  82.     my $retryCount = 20;
  83.     foreach my $name (@names){
  84.         next if ($name eq ".");
  85.         next if ($name eq "..");
  86.         $retryCount = 20;
  87.  
  88.         if (-d $name){                  # is this a directory?
  89.             &ScanForAPKs($name);
  90.             next;
  91.         }
  92.         if ( $name =~ /(.*)\.apk/ ){
  93.             #print "APK is $workdir\\$name    App Name $1\n";
  94.  
  95.             my $installOutput = "";
  96.             my $cmd = "c:\\ANDROID\\android-sdk-windows\\tools\\adb install -r $workdir\\$name";
  97.             print "cmd = [$cmd]\n";
  98.             do{
  99.                 print "\nRetry count is currently at $retryCount\n";
  100.                 alarm 0; # Reset the Alarm
  101.                 print "
  102.                 eval {
  103.                     print "Entered eval, about to set Alaram and attemp to install.\n"
  104.                     local $SIG{ALRM} = sub {die "alarm\n"};
  105.                     alarm 20; # Set Timeout of 20 seconds
  106.                     $installOutput  = `$cmd`;
  107.                     alarm 0; # If we  hit this, command was successful. Turn off Alarm.
  108.                 };
  109.                 if ($@ ne "alarm\n") {
  110.                     # timed out
  111.                     print "This is the alarm or eval's output \($@\).\n";
  112.                     #die unless $@ eq "alarm\n";   #extra die condition: Add later if needed.
  113.                     print "Install did not time out.\n";
  114.                 }
  115.                 elsif ($retryCount != 1){
  116.                     print "This is the alarm or eval's output \($@\).\n";
  117.                     $retryCount = 2;
  118.                     print "Try this install one more time.\n";
  119.                 }
  120.                 print "-- STDOUT = [$installOutput] \n";
  121.                 print scalar localtime();
  122.                 print "\n";
  123.                 sleep 2;
  124.                 $retryCount -= 1;
  125.             }
  126.             while ( !($installOutput  =~ /.*(Success).*/ ) && !($installOutput  =~ /.*(Failure).*/ ) && ($retryCount > 0) );
  127.  
  128.             if($installOutput  =~ /.*(Success).*/ ){
  129.                 print scalar localtime();
  130.                 print "\nDropping out of loop because \"Success\" was detected. \n";
  131.             }
  132.             elsif ($installOutput  =~ /.*(Failure).*/){
  133.                 print scalar localtime();
  134.                 print "\nDropping out of loop because \"Failure\" was detected. \n";
  135.                 print "We understand this as an acceptable result.\n";
  136.             }
  137.             else {
  138.                 print scalar localtime();
  139.                 print "\nRetry Count Exceeded. Moving on to next apk.\n";
  140.                 print "Better luck next time $name.\n";
  141.                 $emmulatorRetryCount -= 1;
  142.             }
  143.  
  144.             if ($emmulatorRetryCount <= 0){
  145.                 print scalar localtime();
  146.                 print "\nEmulator Retry Count $emmulatorRetryCount \n";
  147.                 print "File name $name \n";
  148.                 print "\nSeems as though this Emulator didn't start.\n";
  149.                 print "We're moving on to the next.\n";
  150.                 last;
  151.             }
  152.             else{
  153.                 print scalar localtime();
  154.                 print "\nEmulator Retry Count $emmulatorRetryCount \n";
  155.                 print "File name $name \n";
  156.             }
  157.  
  158.         }#if ($name =~ /(.*)\.apk/ )
  159.        
  160.     }#foreach
  161.     chdir($startdir) or
  162.     die "Unable to change to dir $startdir:$!\n";
  163. }#ScanForAPKs
  164.  
  165.  
  166.  
  167. sub KillEmulator{
  168.     my ($processToKill) = shift;
  169.     print ("Disassemble Android\n");
  170.     print scalar localtime();
  171.     print "\n";
  172.     $processToKill->Kill(159);
  173.     system "taskkill /im emulator.exe /f";
  174.     sleep 20;
  175. }