View Javadoc
1   package com.simpligility.maven.plugins.android.standalonemojos;
2   
3   import com.simpligility.maven.plugins.android.AbstractAndroidMojo;
4   import com.simpligility.maven.plugins.android.CommandExecutor;
5   import com.simpligility.maven.plugins.android.ExecutionException;
6   
7   import org.apache.maven.plugin.MojoExecutionException;
8   import org.apache.maven.plugin.MojoFailureException;
9   import org.apache.maven.plugins.annotations.Mojo;
10  
11  import java.util.ArrayList;
12  import java.util.List;
13  
14  /**
15   * Connect external IP addresses to the ADB server.
16   *
17   * @author demey.emmanuel@gmail.com
18   */
19  @Mojo( name = "connect", requiresProject = false )
20  public class ConnectMojo extends AbstractAndroidMojo
21  {
22  
23      @Override
24      public void execute() throws MojoExecutionException, MojoFailureException
25      {
26  
27          if ( ips.length > 0 )
28          {
29              CommandExecutor executor = getExecutor();
30  
31              for ( String ip : ips )
32              {
33                  getLog().debug( "Connecting " + ip );
34  
35                  // It would be better to use the AndroidDebugBridge class
36                  // rather than calling the command line tool
37                  String command = getAndroidSdk().getAdbPath();
38                  // We first have to the put the bridge in tcpip mode or else it will fail to connect
39                  // First make sure everything is clean ...
40                  List<String> parameters = new ArrayList<String>();
41                  parameters.add( "kill-server" );
42  
43                  try
44                  {
45                      executor.executeCommand( command, parameters, false );
46                      parameters.clear();
47                      // initial connect to get adb in the right frame of mind
48                      // http://stackoverflow.com/questions/14899935/set-adb-in-tcp-ip-mode-device-not-found
49                      executor = getExecutor();
50                      parameters.add( "connect" );
51                      parameters.add( ip );
52                      executor.executeCommand( command, parameters, false );
53                      parameters.clear();
54                      // ... now put in wireless mode ...
55                      executor = getExecutor();
56                      String hostport[] = ip.split( ":" );
57                      parameters.add( "tcpip" );
58                      parameters.add( hostport[1] );
59                      executor.executeCommand( command, parameters, false );
60                      parameters.clear();
61                      // ... and finally really connect
62                      executor = getExecutor();
63                      parameters.add( "connect" );
64                      parameters.add( ip );
65                      executor.executeCommand( command, parameters, false );
66                  }
67                  catch ( ExecutionException e )
68                  {
69                      throw new MojoExecutionException( String.format( "Can not connect %s", ip ), e );
70                  }
71              }
72          }
73      }
74  
75      private CommandExecutor getExecutor()
76      {
77          CommandExecutor executor = CommandExecutor.Factory.createDefaultCommmandExecutor();
78          executor.setLogger( this.getLog() );
79          executor.setCaptureStdOut( true );
80          return executor;
81      }
82  }