if( $verbosity> 0 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : no nodes matching this XPath selector (for getting the 'back-arrow' icon, hopefully, this means we reached home-page of the app): ${asel}") }
lastONBACKARROW;
} elsif( $N> 1 ){ $log->error("--begin matched nodes:\n".join("\n", @nodes)."\n--end nodes matched.\n\n${whoami} (via $parent), line ".__LINE__." : error, matched more than one node (see above) with this XPath selector (for getting the 'Chats' icon: ${asel}"); return1 }
$node= $nodes[0];
# click the 'back-arrow' at the top
$boundstr= $node->getAttribute('bounds');
if( ! defined$boundstr){ $log->error("${node}\n\n${whoami} (via $parent), line ".__LINE__." : error, above node does not have attribute 'bounds'."); return1 }
if( $boundstr!~ /\[\s*(\d+)\s*,\s*(\d+)\]\s*\[\s*(\d+)\s*,\s*(\d+)\]/ ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, failed to parse bounds string: '$boundstr'."); return1 }
$bounds= [[$1,$2],[$3,$4]];
# click it
if( $verbosity> 0 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : clicking the back-arrow ...") }
if( $self->mother->tap({'bounds'=> $bounds}) ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, failed to tap on $bounds."); return1 }
sleep(1);
}
if( $verbosity> 0 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : hopefully we are now at the very first screen of the app.") }
return0 # success
}
subsend_message {
my($self, $params) = @_;
my$parent= ( caller(1) )[3] || "N/A";
my$whoami= ( caller(0) )[3];
my$log= $self->log();
my$verbosity= $self->verbosity();
my($recipient, $message, $outbase, $outfile);
if( ! exists($params->{'recipient'}) || ! defined($recipient=$params->{'recipient'}) ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, input parameter 'recipient' was not specified."); returnundef}
if( ! exists($params->{'message'}) || ! defined($message=$params->{'message'}) ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, input parameter 'message' was not specified."); returnundef}
# for debugging purposes, save each UI we get here
$asel= '//node[@text="Chats" and @resource-id="com.viber.voip:id/bottomBarItemTitle"]';
@nodes= $dom->findnodes($asel);
$N= scalar@nodes;
if( $N== 0 ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, failed to find any nodes matching this XPath selector (for getting the 'Chats' icon): ${asel}"); returnundef}
elsif( $N> 1 ){ $log->error("--begin matched nodes:\n".join("\n", @nodes)."\n--end nodes matched.\n\n${whoami} (via $parent), line ".__LINE__." : error, matched more than one node (see above) with this XPath selector (for getting the 'Chats' icon): ${asel}"); returnundef}
$node= $nodes[0];
# click the 'Chats' at the bottom
$boundstr= $node->getAttribute('bounds');
if( ! defined$boundstr){ $log->error("${node}\n\n${whoami} (via $parent), line ".__LINE__." : error, above node does not have attribute 'bounds'."); returnundef}
if( $boundstr!~ /\[\s*(\d+)\s*,\s*(\d+)\]\s*\[\s*(\d+)\s*,\s*(\d+)\]/ ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, failed to parse bounds string: '$boundstr'."); returnundef}
$bounds= [[$1,$2],[$3,$4]];
# click it
if( $self->mother->tap({'bounds'=> $bounds}) ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, failed to tap on $bounds."); returnundef}
sleep(1);
# now we are on the chats screen, in the centre pane there are all our contacts
. ' and matches(@text,\''.$params->{'recipient'}.'\',"i")'
. ' and @resource-id="com.viber.voip:id/from"'
. ']'
;
@nodes= $xc->findnodes($asel);
$N= scalar@nodes;
if( $N== 0 ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, failed to find any nodes matching this XPath selector (for getting the recipient (".$params->{'recipient'}.") from the contacts on the central pane: ${asel}"); returnundef}
elsif( $N> 1 ){ $log->error("--begin matched nodes:\n".join("\n", @nodes)."\n--end nodes matched.\n\n${whoami} (via $parent), line ".__LINE__." : error, matched more than one node (see above) with this XPath selector (for getting the recipient (".$params->{'recipient'}.") from the contacts on the central pane: ${asel}"); returnundef}
$node= $nodes[0];
# click the Recipient contact name at the bottom
$boundstr= $node->getAttribute('bounds');
if( ! defined$boundstr){ $log->error("${node}\n\n${whoami} (via $parent), line ".__LINE__." : error, above node does not have attribute 'bounds'."); returnundef}
if( $boundstr!~ /\[\s*(\d+)\s*,\s*(\d+)\]\s*\[\s*(\d+)\s*,\s*(\d+)\]/ ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, failed to parse bounds string: '$boundstr'."); returnundef}
$bounds= [[$1,$2],[$3,$4]];
# click it
if( $self->mother->tap({'bounds'=> $bounds}) ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, failed to tap on $bounds."); returnundef}
sleep(1);
# Put the text into the text-edit Message... (note: ... is unicode ellipses something)
. ' and @resource-id="com.viber.voip:id/send_text"'
. ' and @package="com.viber.voip"'
. ']'
;
@nodes= $xc->findnodes($asel);
$N= scalar@nodes;
if( $N== 0 ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, failed to find any nodes matching this XPath selector (for getting the specified recipient (".$params->{'recipient'}."): ${asel}"); returnundef}
elsif( $N> 1 ){ $log->error("--begin matched nodes:\n".join("\n", @nodes)."\n--end nodes matched.\n\n${whoami} (via $parent), line ".__LINE__." : error, matched more than one node (see above) with this XPath selector (for getting the specified recipient (".$params->{'recipient'}.")): ${asel}"); returnundef}
$node= $nodes[0];
$boundstr= $node->getAttribute('bounds');
if( ! defined$boundstr){ $log->error("${node}\n\n${whoami} (via $parent), line ".__LINE__." : error, above node does not have attribute 'bounds'."); returnundef}
if( $boundstr!~ /\[\s*(\d+)\s*,\s*(\d+)\]\s*\[\s*(\d+)\s*,\s*(\d+)\]/ ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, failed to parse bounds string: '$boundstr'."); returnundef}
$bounds= [[$1,$2],[$3,$4]];
# add the text in the node
if( $self->mother->input_text({'bounds'=> $bounds, 'text'=> $message}) ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, failed to input text on ${boundstr}. Note that there may be a maximum length for the message. Your message was ".length($message)." chars long."); returnundef}
sleep(1);
# and send!
# on the same XML we search for the send button
$asel= '//node'
. '['
. ' matches(@class,\'FrameLayout$\',"i")'
. ' and @resource-id="com.viber.voip:id/btn_send"'
. ' and @package="com.viber.voip"'
. ']'
;
@nodes= $xc->findnodes($asel);
$N= scalar@nodes;
if( $N== 0 ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, failed to find any nodes matching this XPath selector (for getting the recipient (".$params->{'recipient'}.") from the contacts on the central pane: ${asel}"); returnundef}
elsif( $N> 1 ){ $log->error("--begin matched nodes:\n".join("\n", @nodes)."\n--end nodes matched.\n\n${whoami} (via $parent), line ".__LINE__." : error, matched more than one node (see above) with this XPath selector (for getting the recipient (".$params->{'recipient'}.") from the contacts on the central pane: ${asel}"); returnundef}
$node= $nodes[0];
# click the Send button
$boundstr= $node->getAttribute('bounds');
if( ! defined$boundstr){ $log->error("${node}\n\n${whoami} (via $parent), line ".__LINE__." : error, above node does not have attribute 'bounds'."); returnundef}
if( $boundstr!~ /\[\s*(\d+)\s*,\s*(\d+)\]\s*\[\s*(\d+)\s*,\s*(\d+)\]/ ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, failed to parse bounds string: '$boundstr'."); returnundef}
$bounds= [[$1,$2],[$3,$4]];
# click it
if( $verbosity> 0 ){ $log->info("To: '${recipient}'\nMessage:\n${message}\n--end message.\n${whoami} (via $parent), line ".__LINE__." : about to send the above message ...") }
if( $mock== 0 ){
if( $self->mother->tap({'bounds'=> $bounds}) ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, failed to tap on $bounds."); returnundef}
} else{
$log->warn("${whoami} (via $parent), line ".__LINE__." : The 'Send' button was not clicked because mock is ON.")
}
sleep(0.8);
return{};
}
# only pod below
=pod
=encoding utf8
=head1 NAME
Android::ElectricSheep::Automator::Plugins::Viber - Control the Viber app from your desktop via the ElectricSheep Automator
=head1 VERSION
Version 0.02
=head1 WARNING
Current distribution is extremely alpha. API may change.
=head1 SYNOPSIS
An L<Android::ElectricSheep::Automator> plugin which
interacts with the Viber app from the desktop.
use Android::ElectricSheep::Automator::Plugins::Viber;
my $viber = Android::ElectricSheep::Automator::Plugins::Viber->new({
'configfile' => $configfile,
'verbosity' => 1,
# we already have a device connected and ready to control
'device-is-connected' => 1,
});
# go to home screen to start fresh
$plugobj->mother->home_screen();
# open the viber app
$plugobj->open_viber_app() or die
$plugobj->send_message({
'recipient' => 'My Notes', # some of your contacts
# 1) no unicode, 2) each space must be converted to '%s'
'message' => 'thank%syou'
}) or die;
=head1 CONSTRUCTOR
=head2 new($params)
Creates a new C<Android::ElectricSheep::Automator::Plugins::Viber> object. C<$params>
is a hash reference used to pass initialization options. These options are
passed onto the main constructor.
Refer to the documentation of L<Android::ElectricSheep::Automator::new($params)> for
A configuration file or hash is required.
Here is an example configuration file to get you started:
</* $VERSION = '0.01'; */>
</* comments are allowed */>
</* and <% vars %> and <% verbatim sections %> */>