View Javadoc
1   package com.simpligility.maven.plugins.android;
2   
3   import com.android.ddmlib.IDevice;
4   import com.github.rtyley.android.screenshot.paparazzo.OnDemandScreenshotService;
5   import com.github.rtyley.android.screenshot.paparazzo.processors.AnimatedGifCreator;
6   import com.github.rtyley.android.screenshot.paparazzo.processors.ImageSaver;
7   import com.github.rtyley.android.screenshot.paparazzo.processors.ImageScaler;
8   
9   import org.apache.maven.plugin.MojoExecutionException;
10  import org.apache.maven.plugin.MojoFailureException;
11  import org.apache.maven.plugin.logging.Log;
12  import org.apache.maven.project.MavenProject;
13  
14  import java.io.File;
15  import java.io.IOException;
16  
17  import static com.github.rtyley.android.screenshot.paparazzo.processors.util.Dimensions.square;
18  import static com.simpligility.maven.plugins.android.common.DeviceHelper.getDescriptiveName;
19  import static org.apache.commons.io.FileUtils.forceMkdir;
20  
21  /**
22   * ScreenshotServiceWrapper wraps the feature to capture a screenshot during an instrumentation test run.
23   */
24  public class ScreenshotServiceWrapper implements DeviceCallback
25  {
26  
27      private final DeviceCallback delegate;
28      private final Log log;
29      private final File screenshotParentDir;
30      private static final int MAX_BOUNDS = 320;
31  
32      public ScreenshotServiceWrapper( DeviceCallback delegate, MavenProject project, Log log )
33      {
34          this.delegate = delegate;
35          this.log = log;
36          screenshotParentDir = new File( project.getBuild().getDirectory(), "screenshots" );
37          create( screenshotParentDir );
38      }
39  
40  
41      @Override
42      public void doWithDevice( final IDevice device ) throws MojoExecutionException, MojoFailureException
43      {
44          String deviceName = getDescriptiveName( device );
45  
46          File deviceGifFile = new File( screenshotParentDir, deviceName + ".gif" );
47          File deviceScreenshotDir = new File( screenshotParentDir, deviceName );
48          create( deviceScreenshotDir );
49  
50  
51          OnDemandScreenshotService screenshotService = new OnDemandScreenshotService( device,
52                  new ImageSaver( deviceScreenshotDir ),
53                  new ImageScaler( new AnimatedGifCreator( deviceGifFile ), square( MAX_BOUNDS ) ) );
54  
55          screenshotService.start();
56  
57          delegate.doWithDevice( device );
58  
59          screenshotService.finish();
60      }
61  
62      private void create( File dir )
63      {
64          try
65          {
66              forceMkdir( dir );
67          }
68          catch ( IOException e )
69          {
70              log.warn( "Unable to create screenshot directory: " + screenshotParentDir.getAbsolutePath(), e );
71          }
72      }
73  }