Android Scripting Environment: Shell Scripting and Android

I just upgraded to Android 2.2 from 2.1. The GPS issue that was troublesome in 2.1 seems to have been fixed. Some of web browsing seems faster, but it could just be my connection is better today :wink: Flash works in some browsers but not very good and it is too slow for Flash apps designed for the non-mobile web.

Afterwords, I did some digging around and found some young, but interesting none-the-less, shell script applications for Android. Naturally, these apps have a ways to go. Unix and Linux was not built "in a day" as they say (Rome was not "built in a day") so don't expect too much yet!

Android Scripting Environment

Current downloads.

---------- Post updated at 19:44 ---------- Previous update was at 19:25 ----------

Here are some example PERL scripts on Android, for example:

use Android;
 
sub dumphash {
   my $hash = pop;
   my $key, $down;
   for $key (keys %$hash) {
      if ($key eq "result") {
         $down = $hash->{$key};
         for $key (keys %$down) {
            print "result ==> $key => $down->{$key}\n";
         }
      }
   }
}
 
my $a = Android->new();
my $r;
 
$r=$a->startSensing("SENSOR_ACCELEROMETER");
&dumphash ($r);
# ^c (Ball-C) to break
for($ii=0;$ii<2000;$ii++){
  $r=$a->readSensors("SENSOR_ACCELEROMETER");
#  printf ("r %3.0f  p %3.0f  a %3.0f  \n", $r->{'result'}->{'roll'},  $r->{'result'}->{'pitch'},  $r->{'result'}->{'azimuth'});
  printf ("x %5.2f  y %5.2f  z %5.2f  \n", $r->{'result'}->{'xmag'},  $r->{'result'}->{'ymag'},  $r->{'result'}->{'zmag'});
#  &dumphash ($r);
}
$r=$a->stopSensing("SENSOR_ACCELEROMETER");
&dumphash ($r);

---------- Post updated at 19:47 ---------- Previous update was at 19:44 ----------

Here is a very crude Android webserver written in PERL:

use Android;
my $a = Android->new();

#connect to: http://localhost:8888

use IO::Socket;
use IO::Select;

$ctrl_port = 8888;
$timeout_mins = 30; # 30 min. timeout
$selectcnt = 0;
$debug = 0;
$noisy = 0;


while ($arg = shift) {
    if ($arg =~ /help/) {
        print "Useage:\n";
        print "perl $0 [debug] [noisy] [ctrl_port=#;8888] [to=#min.]\n";
        print "If [srvr=#] is missing, report client address\n";
        exit;
    }
    if ($arg =~ /debug/) {
        $debug++;
        print "ARGS debug\n";
    }
    if ($arg =~ /noisy/) {
        $noisy = 1;
        print "ARGS noisy\n";
    }
    if ($arg =~ /to=(.+)/) {
        $timeout_mins = $1;
        print "ARGS to=$timeout_mins\n";
    }
    if ($arg =~ /ctrl_port=(.+)/) {
        $ctrl_port = $1;
        print "ARGS ctrl=$ctrl_port\n";
    }
}


$ctrl_lstn_sock = IO::Socket::INET->new (
    LocalPort => $ctrl_port,
    Listen => 5, 
    Reuse => 1
);
die "Can't create socket for listening: $!" unless $ctrl_lstn_sock;

my $readable = IO::Select->new;     # Create a new IO::Select object
$readable->add($ctrl_lstn_sock);          # Add the lstnsock to it


sub debug_info {
    print "CONN_DBG:lstnsock      $lstn_sock\n";
}


$tend = time + $timeout_mins * 60;
$tstart = time + 0;
$tickdelta = 1;
$tick = time + $tickdelta;

while(1) {
    if (time >= $tend) {
        print STDERR "\n\n=== $timeout_mins minutes timeout occured, stop\n\n";
        last;
    }
    if (time >= $tick) {
        $tick = time + $tickdelta;
        $now_string = localtime;
        print STDERR "\r$now_string: PID $$ up ", time - $tstart, " S $selectcnt connections \r";
    }

    # Get a list of sockets that are ready to talk to us.
    my ($ready) = IO::Select->select($readable, undef, undef, $tickdelta);
    foreach my $curr_socket (@$ready) {
        # Is it a new connection?
        &debug_info, if ($debug);
        $selectcnt++;
        if($curr_socket == $ctrl_lstn_sock) {
            print "============================ SELECT Control READY $selectcnt =========================\n$now_string\n", if ($noisy);
#---------------------- Form Control Page handler  -----------------------
            $now_string = localtime;
            $ctrl_sock = $ctrl_lstn_sock->accept;
            $addr = $ctrl_sock->peeraddr ();
            if (defined $addr) {
                $client_ip = inet_ntoa ($addr);
            } else {
                $client_ip = "unknown";
            }
            $commands = "";
            $ctrlportcnt++;
            print "---------------------------- CTRL HTML $ctrlportcnt begin ----------------------------\n", if ($noisy);
            while (<$ctrl_sock>) {
                if (/^ *\n*\r*$/) {
                    last;
                }
                print "CTRL $_", if ($noisy);
                if (/GET \/portfwrd_ctrlport\.pl\?([^ ]+) HTTP/) {
                    $commands = $1;
                }
            }
            print "---------------------------- CTRL HTML $ctrlportcnt   end----------------------------\n", if ($noisy);
            print "CTRL parameters: >$commands<\n", if ($noisy);
            print "CTRL $now_string: $client_ip connected to control port\n", if ($noisy);

            # ------------------- parameters input processing
            @cmd_param_pairs = split ('&', $commands);
            undef %params;
            foreach $cmd_param_pair (@cmd_param_pairs) {
                ($name, $param) = split ('=', $cmd_param_pair);
                print "CTRL_CMD $name=$param\n", if ($noisy);
                $params{$name} = $param;
            }


            if ($params{'debug'} eq "on") {
                $debug = 1;
            } else {
                $debug = 0;
            }
            if ($params{'noisy'} eq "on") {
                $noisy = 1;
            } else {
                $noisy = 0;
            }


            print $ctrl_sock "HTTP/1.0 200 OK\r\n\r\n<html><body>\n";
            print $ctrl_sock "$now_string: you have connected to the control port from $client_ip\n";


            print $ctrl_sock "<form action=\"/portfwrd_ctrlport.pl\" method=\"PUT\">\n";
            print $ctrl_sock "    <table border=\"1\" cellpadding=\"5\" cellspacing=\"3\">\n";

            print $ctrl_sock "        <tr>\n";
            print $ctrl_sock "            <td>Parameters</td>\n";
            print $ctrl_sock "            <td>Descriptions</td>\n";
            print $ctrl_sock "        </tr>\n";


            if ($debug) {
                $buf = "checked";
            } else {
                $buf = "";
            }
            print $ctrl_sock "        <tr>\n";
            print $ctrl_sock "            <td><input type=\"checkbox\" $buf name=\"debug\">debug</td>\n";
            print $ctrl_sock "            <td>debug, prints to console</td>\n";
            print $ctrl_sock "        </tr>\n";

            if ($noisy) {
                $buf = "checked";
            } else {
                $buf = "";
            }
            print $ctrl_sock "        <tr>\n";
            print $ctrl_sock "            <td><input type=\"checkbox\" $buf name=\"noisy\">noisy</td>\n";
            print $ctrl_sock "            <td>noisy, prints to console</td>\n";
            print $ctrl_sock "        </tr>\n";


            print $ctrl_sock "        <tr>\n";
            print $ctrl_sock "            <td><input type=\"submit\" value=\"Set Config\"></td>\n";
            print $ctrl_sock "            <td>$selectcnt connections</td>\n";
            print $ctrl_sock "        </tr>\n";


            print $ctrl_sock "    </table>\n";
            print $ctrl_sock "</form>\n";
            print $ctrl_sock "</body></html>";

            $ctrl_sock->close;
            $ctrl_sock = 0;
        } else {
            # multiple connection?
            print STDERR "ERROR  s $curr_socket\n";
        }
    }
}

---------- Post updated at 19:50 ---------- Previous update was at 19:47 ----------

More details here:

Android: A micro HTTP applet server V0.23 - 2010/05/23

---------- Post updated at 19:53 ---------- Previous update was at 19:50 ----------

On a related note, here is an example bash shell for Android.....

---------- Post updated at 20:10 ---------- Previous update was at 19:53 ----------

"Hello World" in Perl for ASE:

use Android;
my $a = Android->new();
$a->makeToast("Hello, Android!");
1 Like