| 1 | use strict; | ||
| 2 | package Device::Ericsson::AccessoryMenu::Menu; | ||
| 3 | use base 'Device::Ericsson::AccessoryMenu::State'; | ||
| 4 | __PACKAGE__->mk_accessors( qw( data selected ) ); | ||
| 5 | |||
| 6 | |||
| 7 | sub _get_pairs { | ||
| 8 | my $self = shift; | ||
| 9 | |||
| 10 | my @menu = @{ $self->data }; | ||
| 11 | my @entries = @{ $menu[1] }; | ||
| 12 | my @pairs; | ||
| 13 | while (@entries) { | ||
| 14 | push @pairs, [ shift @entries, shift @entries ]; | ||
| 15 | } | ||
| 16 | print map { "$_->[0]: $_->[1]\n"} @pairs | ||
| 17 | if $self->debug && 0; | ||
| 18 | return @pairs; | ||
| 19 | } | ||
| 20 | |||
| 21 | sub on_enter { | ||
| 22 | my $self = shift; | ||
| 23 | my $selected = $self->selected || 1; | ||
| 24 | |||
| 25 | my $name = $self->data->[0]; | ||
| 26 | my @pairs = $self->_get_pairs; | ||
| 27 | my $titles = join ',', map { qq{"$_->[0]"} } @pairs; | ||
| 28 | my $length = scalar @pairs; | ||
| 29 | $self->send( qq{AT*EASM="$name",1,$selected,$length,$titles} ); | ||
| 30 | $self->expect( 'OK' ); | ||
| 31 | } | ||
| 32 | |||
| 33 | sub handle { | ||
| 34 | my $self = shift; | ||
| 35 | my $line = shift; | ||
| 36 | |||
| 37 | if ($line =~ /EAMI: (\d+)/) { # menu item | ||
| 38 | my $item = $1; | ||
| 39 | if ($item == 0) { # back up | ||
| 40 | $self->exit_state; | ||
| 41 | return; | ||
| 42 | } | ||
| 43 | |||
| 44 | my @pairs = $self->_get_pairs; | ||
| 45 | unshift @pairs, []; # dummy one so the offsets all work out | ||
| 46 | my ($name, $action) = @{ $pairs[ $item ] }; | ||
| 47 | |||
| 48 | $self->selected( $item ); | ||
| 49 | print "invoking $item: $action\n" if $self->debug; | ||
| 50 | $action = $action->( $self->parent ) if ref $action eq 'CODE'; | ||
| 51 | |||
| 52 | if (ref $action eq 'ARRAY') { # wander down | ||
| 53 | $self->enter_state( 'Menu', data => [ $name => $action ] ); | ||
| 54 | return; | ||
| 55 | } | ||
| 56 | |||
| 57 | if (defined $action && !ref $action) { | ||
| 58 | $self->parent->send_text( $name, $action ); | ||
| 59 | return; | ||
| 60 | } | ||
| 61 | |||
| 62 | # update and resend, if we're still in this state | ||
| 63 | $self->on_enter if $self->current_state == $self; | ||
| 64 | } | ||
| 65 | } | ||
| 66 | |||
| 67 | 1; | ||
| 68 |