viernes, 17 de febrero de 2012

Recién salido del horno, les dejo este pequeño script para interactuar con dispositivos Android utilizando La SDK. saludos y espero les guste.


#!/usr/bin/perl

##################
#
# This file was automatically generated by ZooZ.pl v1.2
# on Wed Feb  1 16:57:35 2012.
# Project: Project 1
# File:   
#
##################

##################
# Autor: DarkSpark
# web http://elblogdedarkspark.blogspot.com/
##################



#
# Headers
#
use strict;
use warnings;

use Tk 804;

#
# Global variables
#
my (
     # MainWindow
     $MW,

     # Hash of all widgets
     %ZWIDGETS,
    
    $device,);

# User-defined variables (if any)
#
my $home=`ls -la ~`;
if ($home !~/.andlinux/){mkdir "~/.andlinux";}
my $sdk="~/.andlinux/android-sdk-linux/platform-tools";
my $adb="$sdk/adb";
######################
#
# Create the MainWindow
#
######################

$MW = MainWindow->new;

######################
#
# Load any images and fonts
#
######################
ZloadImages();
ZloadFonts ();



# Widget Frame1 isa Frame
$ZWIDGETS{'Frame1'} = $MW->Frame(
   -background  => '#0000ffff0000',
   -borderwidth => 5,
  )->grid(
   -row    => 0,
   -column => 0,
  );

# Widget boton_sdk isa Button
$ZWIDGETS{'boton_sdk'} = $ZWIDGETS{Frame1}->Button(
   -activebackground => '#0000ffff0000',
   -activeforeground => '#000000000000',
   -background       => '#ffffffffffff',
   -borderwidth      => 5,
   -command          => 'main::sdk',
   -text             => 'Instalar SDK ',
  )->grid(
   -row    => 0,
   -column => 0,
   -sticky => 'ew',
  );

# Widget boton_buscar isa Button
$ZWIDGETS{'boton_buscar'} = $ZWIDGETS{Frame1}->Button(
   -activebackground => '#0000ffff0000',
   -activeforeground => '#000000000000',
   -background       => '#ffffffffffff',
   -borderwidth      => 5,
   -command          => 'main::buscar',
   -text             => 'Buscar Dispositivos',
  )->grid(
   -row    => 1,
   -column => 0,
   -sticky => 'ew',
  );

# Widget boton_subir isa Button
$ZWIDGETS{'boton_subir'} = $ZWIDGETS{Frame1}->Button(
   -activebackground => '#0000ffff0000',
   -activeforeground => '#000000000000',
   -background       => '#ffffffffffff',
   -borderwidth      => 5,
   -command          => 'main::subir',
   -text             => 'Subir Archivo',
  )->grid(
   -row    => 2,
   -column => 0,
   -sticky => 'ew',
  );

# Widget boton_bajar isa Button
$ZWIDGETS{'boton_bajar'} = $ZWIDGETS{Frame1}->Button(
   -activebackground => '#0000ffff0000',
   -activeforeground => '#000000000000',
   -background       => '#ffffffffffff',
   -borderwidth      => 5,
   -command          => 'main::bajar',
   -text             => 'Bajar Archivo',
  )->grid(
   -row    => 3,
   -column => 0,
   -sticky => 'ew',
  );

# Widget boton_instalar isa Button
$ZWIDGETS{'boton_instalar'} = $ZWIDGETS{Frame1}->Button(
   -activebackground => '#0000ffff0000',
   -activeforeground => '#000000000000',
   -background       => '#ffffffffffff',
   -borderwidth      => 5,
   -command          => 'main::install',
   -text             => 'Instalar Aplicacion',
  )->grid(
   -row    => 4,
   -column => 0,
   -sticky => 'ew',
  );

# Widget boton_desinstalar isa Button
$ZWIDGETS{'boton_desinstalar'} = $ZWIDGETS{Frame1}->Button(
   -activebackground => '#0000ffff0000',
   -activeforeground => '#000000000000',
   -background       => '#ffffffffffff',
   -borderwidth      => 5,
   -command          => 'main::uninstall',
   -text             => 'Desinstalar Aplicacion',
  )->grid(
   -row    => 5,
   -column => 0,
   -sticky => 'ew',
  );



###############
#
# MainLoop
#
###############

MainLoop;

#######################
#
# Subroutines
#
#######################

sub ZloadImages {
}

sub ZloadFonts {
}

sub install {
my $apk;
my $ven=MainWindow->new();
$ven->Entry(-textvariable=>\$apk, -width=>30)->grid(-column=>0 , -row=>0);
$ven->Button(-text=>"Buscar", -command=>sub{
                        my $types = [ ['Android Pakage', '.apk'],['All Files',   '*'],];
                        $apk=$ven->getOpenFile(-filetypes => $types, -initialfile => 'test', -defaultextension => '.apk')
                        })->grid(-column=>1 , -row=>0);
                       

$ven->Button(-text=>"Instalar", -command=>sub{system ("$adb install $apk");
                         my $dialog=$MW->Dialog(-buttons => ["Aceptar"],-text=>"Aplicacion Instalada");
                         my $item = $dialog->Show();
                         })->grid(-column=>0, -row=>1);
$ven->Button (-text=>"Cancelar" , -command=>sub{$ven->destroy;})->grid(-column=>1 , -row=>1);
MainLoop
}

sub sdk {
system("cd ~/.andlinux ; xterm -e wget http://dl.google.com/android/android-sdk_r16-linux.tgz");
system ("cd ~/.andlinux ; xterm -e tar xvzf  android-sdk_r16-linux.tgz");
#system ("cd ~/.andlinux ; rm android-sdk_r16-linux.tgz");
my $vtn=MainWindow->new();
$vtn->Label(-text=>"Es necesario instalar las herramientas del SDK.\n Seleccione los paquetes \"Android SDK Tools\" y \"Android SDK Platform-tools\" y presione el botón de instalar\n cuando termine cierre el SDK Manager para continuar con la instalación")->pack();
$vtn->Button(-text=>"Aceptar",-command=>sub {
                         $vtn->destroy;
                         system("~/.andlinux/android-sdk-linux/tools/android");
                         my $test=`ls ~/.andlinux/android-sdk-linux/platform-tools/`;
                         if ($test=~/adb/){
                                   my $dialog=$MW->Dialog(-buttons => ["Aceptar"],-text=>"El SDK Se a Instalado Exitosamente");
                                   my $item = $dialog->Show();
                                  }
                         else{my $dialog=$MW->Dialog(-buttons => ["Aceptar"],-text=>"El SDK No se Instalo Correctamenet, Intente Otra vez");
                          my $item = $dialog->Show();
                         }
                    })->pack();
MainLoop;
}

sub uninstall {
my @list=`$adb shell ls /system/app`;
chop(@list);
chop(@list);
my $venta=MainWindow->new();
my $lista=$venta->Listbox(-width=>35)->grid(-column=>0 , -row=>0);
    for (my $i=0;$i<@list;$i++) {
    if ($list[$i] =~/.apk/){
            $lista->insert('end', $list[$i]);
        }
    }
$venta->Button(-text=>"Desinstalar", -command=>sub{
                          my $un=$lista->get( $lista->curselection );
                          system ("$adb uninstall $un");
                          my $dialog=$MW->Dialog(-buttons => ["Aceptar"],-text=>"La Applicacion Se Desinstalo");
                          my $item = $dialog->Show();
                          })->grid(-column=>0 , -row=>1);
MainLoop;
}

sub bajar {
my $dir2=`ls ~/`;
if ($dir2 !~/AndLinux/){
   system("mkdir ~/AndLinux");
   }
my $ven=MainWindow->new();
my $entry2=$ven->Entry(-width=>30)->grid(-column=>0 , -row=>0);
my $archivo=$entry2->get();
$ven->Button(-text=>"Bajar", -command=>sub{ my $archivo=$entry2->get();
                        system ("$adb pull $archivo ~/AndLinux/");
                        my $dialog=$MW->Dialog(-buttons => ["Aceptar"],-text=>"Archivo Bajado");
                        my $item = $dialog->Show();
                         })->grid(-column=>0, -row=>1);
$ven->Button (-text=>"Cancelar" , -command=>sub{$ven->destroy;})->grid(-column=>1 , -row=>1);
MainLoop;
}

sub subir {
my $dir=`$adb shell ls /sdcard/`;
if ($dir !~/AndLinux/){
   system("$adb shell mkdir /sdcard/AndLinux");
   }
my $ven=MainWindow->new();
my $file;
$ven->Entry(-textvariable=>\$file, -width=>30)->grid(-column=>0 , -row=>0);
$ven->Button(-text=>"Buscar", -command=>sub{
                        my $types = [ ['All Files',   '*']];
                        $file=$ven->getOpenFile(-filetypes => $types, -initialfile => 'test')
                        })->grid(-column=>1 , -row=>0);
                       

$ven->Button(-text=>"Subir", -command=>sub{system ("$adb push $file /sdcard/AndLinux/");
                         my $dialog=$MW->Dialog(-buttons => ["Aceptar"],-text=>"Archivo Subido");
                         my $item = $dialog->Show();
                         })->grid(-column=>0, -row=>1);
$ven->Button (-text=>"Cancelar" , -command=>sub{$ven->destroy;})->grid(-column=>1 , -row=>1);
MainLoop;

}



sub buscar {
my @dev=`$adb devices`;
if ($dev[1] =~/device/){
$device="$dev[1]";

}
else {
$device="No Se Encontraron Dispositivos";
}

my $ven=MainWindow->new();
$ven->Label(-text=>"Dispositivos Encontrados:")->pack();
$ven->Label (-textvariable=>\$device)->pack();
MainLoop;
}