package Android::ADB; use 5.014000; use strict; use warnings; our $VERSION = '0.001'; use Android::ADB::Device; use Carp; use File::Slurp; use IPC::Open2; sub new { my ($class, %args) = @_; $args{path} //= $ENV{ADB}; $args{path} //= 'adb'; $args{args} //= []; bless \%args, $class } sub run { my ($self, @args) = @_; my ($out, $in); my @dev_args = $self->{device_serial} ? ('-s', $self->{device_serial}) : (); my $pid = open2 $out, $in, $self->{path}, @{$self->{args}}, @args; my $result = read_file $out; close $out; close $in; waitpid $pid, 0 or croak "$!"; $result; } sub start_server { shift->run('start-server') } sub kill_server { shift->run('kill-server') } sub connect { shift->run('connect', @_) } sub disconnect { shift->run('disconnect', @_) } sub devices { my @devices = split '\n', shift->run('devices', '-l'); my @result; for (@devices) { next if /^List of devices/; next unless / /; push @result, Android::ADB::Device->new(split) } @result } sub set_device { my ($self, $device) = @_; $self->{device_serial} = $device->serial; } sub wait_for_device { shift->run('wait-for-device') } sub get_state { shift->run('get-state') } sub get_serialno { shift->run('get-serialno') } sub get_devpath { shift->run('get-devpath') } sub remount { shift->run('remount') } sub reboot { shift->run('reboot', @_) } sub reboot_bootloader { shift->run('reboot-bootloader') } sub root { shift->run('root') } sub usb { shift->run('usb') } sub tcpip { shift->run('tcpip', @_) } sub push { my ($self, $local, $remote) = @_; $self->run(push => $local, $remote) } sub pull { my ($self, $remote, $local) = @_; $self->run(push => $remote, $local) } sub pull_archive { my ($self, $remote, $local) = @_; $self->run(push => '-a', $remote, $local) } sub shell { shift->run(shell => @_) } 1; __END__ =encoding utf-8 =head1 NAME Android::ADB - thin wrapper over the 'adb' command =head1 SYNOPSIS use Android::ADB;; my $adb = Android::ADB->new(path => '/opt/android/platform-tools/adb'); my @devices = $adb->devices; $adb->set_device($devices[0]); $adb->push('file.txt', '/sdcard/'); sleep 10; $adb->reboot('recovery'); =head1 DESCRIPTION This module is a minimal wrapper over the Android Debug Bridge (C) command for manipulating Android devices. Methods die on non-zero exit code and return the text printed by the C command. The available methods are: =over =item Android::ADB->B([I]) Create a new Android::ADB object. The available arguments are C, the path to the C executable (defaults to the value of the environment variable C or the string C) and C, an arrayref of arguments passed to every adb command (defaults to []). =item $adb->B Returns a list of L objects representing connected devices. =item $adb->B(I<$device>) Takes an L and directs all further commands to that device by passing C<-s serialno> to every command. =item $adb->B(I<$command>, [I<@args>]) Run an arbitrary ADB command and return its output. =item $adb->B =item $adb->B =item $adb->B(I<$host_and_port>) =item $adb->B([I<$host_and_port>]) =item $adb->B =item $adb->B =item $adb->B =item $adb->B =item $adb->B =item $adb->B([I<$where>]) =item $adb->B =item $adb->B =item $adb->B =item $adb->B(I<$port>) =item $adb->B(I<$local>, I<$remote>) =item $adb->B(I<$remote>, I<$local>) =item $adb->B(I<@args>) Analogues of the respective adb commands. =item $adb->B(I<$remote>, I<$local>) Same as C. =back =head1 AUTHOR Marius Gavrilescu, Emarius@ieval.roE =head1 COPYRIGHT AND LICENSE Copyright (C) 2017 by Marius Gavrilescu This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.24.2 or, at your option, any later version of Perl 5 you may have available. =cut