View Javadoc
1   package com.simpligility.maven.plugins.android.common;
2   
3   import com.android.ddmlib.IDevice;
4   import org.apache.commons.lang3.StringUtils;
5   
6   /**
7    * A bunch of helper methods for dealing with IDevice instances.
8    *
9    * @author Manfred Moser - manfred@simpligility.com
10   */
11  public class DeviceHelper
12  {
13  
14      private static final String MANUFACTURER_PROPERTY = "ro.product.manufacturer";
15      private static final String MODEL_PROPERTY = "ro.product.model";
16      private static final String SEPARATOR = "_";
17      /**
18       * Get a device identifier string that is suitable for filenames as well as log messages.
19       * This means it is human readable and contains no spaces.
20       * Used for instrumentation test report file names so see more at
21       * AbstractInstrumentationMojo#testCreateReport javadoc since
22       * that is the public documentation.
23       */
24      public static String getDescriptiveName( IDevice device )
25      {
26          // if any of this logic changes update javadoc for
27          // AbstractInstrumentationMojo#testCreateReport
28          StringBuilder identfier = new StringBuilder().append( device.getSerialNumber() );
29          if ( device.getAvdName() != null )
30          {
31              identfier.append( SEPARATOR ).append( device.getAvdName() );
32          }
33          String manufacturer = getManufacturer( device );
34          if ( StringUtils.isNotBlank( manufacturer ) )
35          {
36              identfier.append( SEPARATOR ).append( manufacturer );
37          }
38          String model = getModel( device );
39          if ( StringUtils.isNotBlank( model ) )
40          {
41              identfier.append( SEPARATOR ).append( model );
42          }
43  
44          return FileNameHelper.fixFileName( identfier.toString() );
45      }
46      
47      public static String getDeviceLogLinePrefix( IDevice device ) 
48      {
49          return getDescriptiveName( device ) + " :   ";
50      }
51  
52      /**
53       * @return the manufacturer of the device as set in #MANUFACTURER_PROPERTY, typically "unknown" for emulators
54       */
55      public static String getManufacturer( IDevice device )
56      {
57          return StringUtils.deleteWhitespace( device.getProperty( MANUFACTURER_PROPERTY ) );
58      }
59  
60      /**
61       * @return the model of the device as set in #MODEL_PROPERTY, typically "sdk" for emulators
62       */
63      public static String getModel( IDevice device )
64      {
65          return StringUtils.deleteWhitespace( device.getProperty( MODEL_PROPERTY ) );
66      }
67  
68      /**
69       * @return the descriptive name with online/offline/unknown status string appended.
70       */
71      public static String getDescriptiveNameWithStatus( IDevice device )
72      {
73          String status;
74          if ( device.isOnline() )
75          {
76              status = "Online";
77          }
78          else
79          {
80              if ( device.isOffline() )
81              {
82                  status = "Offline";
83              }
84              else
85              {
86                  status = "Unknown";
87              }
88          }
89          return getDescriptiveName( device ) + " " + status;
90      }
91  }