Class: AdbSdkLib::Device

Inherits:
Object
  • Object
show all
Defined in:
lib/adb_sdklib/device.rb

Overview

One of android device attached to host through ADB.

This is a wrapper of com.android.ddmlib.Device object in Java.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(device) ⇒ Device

Returns a new instance of Device.

Parameters:

  • device (Rjb::Rjb_JavaProxy)

    Rjb proxy object of com.android.ddmlib.Device



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/adb_sdklib/device.rb', line 14

def initialize(device)
  unless device.instance_of?(Rjb::Rjb_JavaProxy) &&
      device._classname == 'com.android.ddmlib.Device'
    raise TypeError, "Parameter is not com.android.ddmlib.Device class"
  end
  class << device
    def call_java_method(method_name, *args)
      rjb_method_missing(method_name, *args)
    rescue => e
      raise SdkLibError.new(e.message, e.class.to_s, self._classname, method_name)
    end
    alias_method :rjb_method_missing, :method_missing
    alias_method :method_missing, :call_java_method
  end
  @device = device
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object

Calls wrapping java object’s same name method with arguments.

Parameters:

  • method_name (String)

    method name to call

  • args (Array)

    arguments

Returns:

  • (Object)

    result of the method call



206
207
208
# File 'lib/adb_sdklib/device.rb', line 206

def method_missing(method_name, *args)
  return @device.__send__(method_name, *args)
end

Instance Attribute Details

#api_levelString (readonly)

the API level of the android on the device. (same as output of ‘getprop ro.build.version.sdk’)

Returns:

  • (String)

    the API level.



97
# File 'lib/adb_sdklib/device.rb', line 97

def api_level;       property(@device.PROP_BUILD_API_LEVEL) end

#bootloader?Boolean (readonly)

Returns true if the device is in bootloader mode.

Returns:

  • (Boolean)

    true if the device is in bootloader mode.



58
# File 'lib/adb_sdklib/device.rb', line 58

def bootloader?;     @device.isBootloader end

#build_codenameString (readonly)

the build code name of the android on the device. (same as output of ‘getprop ro.build.version.codename’)

Returns:

  • (String)

    the build code name.



103
# File 'lib/adb_sdklib/device.rb', line 103

def build_codename;  property(@device.PROP_BUILD_CODENAME) end

#build_versionString (readonly)

the build version of the android on the device. (same as output of ‘getprop ro.build.version.release’)

Returns:

  • (String)

    the build version of the android.



91
# File 'lib/adb_sdklib/device.rb', line 91

def build_version;   property(@device.PROP_BUILD_VERSION) end

#debuggableString (readonly)

the device debuggable. (same as output of ‘getprop ro.debuggable’)

Returns:

  • (String)

    the device debuggable.



121
# File 'lib/adb_sdklib/device.rb', line 121

def debuggable;      property(@device.PROP_DEBUGGABLE) end

#device_manufacturerString (readonly)

the product manufacturer of the device. (same as output of ‘getprop ro.product.manufacturer’)

Returns:

  • (String)

    the product manufacturer.



115
# File 'lib/adb_sdklib/device.rb', line 115

def device_manufacturer; property(@device.PROP_DEVICE_MANUFACTURER) end

#device_modelString (readonly)

the product model of the device. (same as output of ‘getprop ro.product.model’)

Returns:

  • (String)

    the device model.



109
# File 'lib/adb_sdklib/device.rb', line 109

def device_model;    property(@device.PROP_DEVICE_MODEL) end

#emulator?Boolean (readonly)

Returns true if the device is an emulator.

Returns:

  • (Boolean)

    true if the device is an emulator.



50
# File 'lib/adb_sdklib/device.rb', line 50

def emulator?;       @device.isEmulator end

#jobjectRjb::Rjb_JavaProxy (readonly)

Returns Wrapper of com.android.ddmlib.Device object.

Returns:

  • (Rjb::Rjb_JavaProxy)

    Wrapper of com.android.ddmlib.Device object.



33
# File 'lib/adb_sdklib/device.rb', line 33

def jobject;         @device end

#offline?Boolean (readonly)

Returns true if the device is offline.

Returns:

  • (Boolean)

    true if the device is offline.



54
# File 'lib/adb_sdklib/device.rb', line 54

def offline?;        @device.isOffline end

#online?Boolean (readonly)

Returns true if the device is ready.

Returns:

  • (Boolean)

    true if the device is ready.



46
# File 'lib/adb_sdklib/device.rb', line 46

def online?;         @device.isOnline end

#serialString (readonly)

Returns the serial number of the device.

Returns:

  • (String)

    the serial number of the device.



37
# File 'lib/adb_sdklib/device.rb', line 37

def serial;          @device.getSerialNumber end

#stateSymbol (readonly)

Returns the state of the device. (:BOOTLOADER, :OFFLINE, :ONLINE, :RECOVERY).

Returns:

  • (Symbol)

    the state of the device. (:BOOTLOADER, :OFFLINE, :ONLINE, :RECOVERY)



42
# File 'lib/adb_sdklib/device.rb', line 42

def state;           @device.getState.toString.to_sym end

Instance Method Details

#battery_level(freshness_ms = nil) ⇒ Integer

Returns the battery level.

Parameters:

  • freshness_ms (Integer) (defaults to: nil)

    freshness time (milliseconds).

Returns:

  • (Integer)

    the battery level.



126
127
128
129
130
131
132
# File 'lib/adb_sdklib/device.rb', line 126

def battery_level(freshness_ms = nil)
  if freshness_ms.nil?
    @device.getBatteryLevel.intValue
  else
    @device.getBatteryLevel(freshness_ms).intValue
  end
end

#inspectString

Returns the human-readable formatted information.

Returns:

  • (String)

    the human-readable formatted information



216
# File 'lib/adb_sdklib/device.rb', line 216

def inspect; "#<AdbSdkLib::Device:#{self.serial}>" end

#propertiesHash<String, String>

Returns the device properties. It contains the whole output of ‘getprop’

Returns:

  • (Hash<String, String>)

    the device properties



81
82
83
84
85
# File 'lib/adb_sdklib/device.rb', line 81

def properties
  convert_map_to_hash(@device.getProperties) do |hash, key, value|
    hash[key.toString] = value.toString
  end
end

#property(name) ⇒ String?

Returns a property value.

Parameters:

  • name (String)

    the name of the value to return.

Returns:

  • (String, nil)

    the value or nil if the property does not exist.



77
# File 'lib/adb_sdklib/device.rb', line 77

def property(name);  @device.getProperty(name) end

#property_countInteger

Returns the property count.

Returns:

  • (Integer)

    the number of property for this device.



72
# File 'lib/adb_sdklib/device.rb', line 72

def property_count;  @device.getPropertyCount end

#pull(remotefile, localfile) ⇒ self

Pulls a file from the device.

If localfile path ends with ‘/’, complements by the basename of remotefile.

Examples:

device = AdbSdkLib::Adb.new.devices.first
device.pull('/data/local/tmp/remote.txt', 'path/to/local.txt')
device.pull('/data/local/tmp/file.txt', 'path/to/dir/') # uses file.txt

Parameters:

  • remotefile (String)

    the name of the remote file on the device to get

  • localfile (String)

    the name of the local file or directory

Returns:

  • (self)

    self



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/adb_sdklib/device.rb', line 185

def pull(remotefile, localfile)
  if localfile.end_with?('/') || File.directory?(localfile)
    localdir = localfile.chomp('/')
    localfilename = nil
  else
    localdir = File.dirname(localfile)
    localfilename = File.basename(localfile)
  end
  unless File.exist?(localdir)
    FileUtils.mkdir_p(localdir)
  end
  
  localfilename = File.basename(remotefile) if localfilename.nil?
  @device.pullFile(remotefile, "#{localdir}/#{localfilename}")
  self
end

#push(localfile, remotefile) ⇒ self

Pushes a file to the device.

If remotefile path ends with ‘/’, complements by the basename of localfile.

Examples:

device = AdbSdkLib::Adb.new.devices.first
device.push('path/to/local.txt', '/data/local/tmp/remote.txt')
device.push('path/to/file.txt', '/data/local/tmp/') # uses file.txt

Parameters:

  • localfile (String)

    the name of the local file to send

  • remotefile (String)

    the name of the remote file or directory on the device

Returns:

  • (self)

    self

Raises:

  • (ArgumentError)

    If localfile is not found



165
166
167
168
169
170
171
172
# File 'lib/adb_sdklib/device.rb', line 165

def push(localfile, remotefile)
  raise ArgumentError, "Not found #{localfile}" unless File.exist?(localfile)
  if remotefile.end_with?('/')
    remotefile = "#{remotefile}#{File.basename(localfile)}"
  end
  @device.pushFile(localfile, remotefile)
  self
end

#reboot(into = nil) ⇒ self

Reboot the device

Parameters:

  • into (String, nil) (defaults to: nil)

    the bootloader name to reboot into, or nil to just reboot the device

Returns:

  • (self)


68
# File 'lib/adb_sdklib/device.rb', line 68

def reboot(into=nil) @device.reboot(into); self end

#screenshotAdbSdkLib::RawImage

Get a screenshot from the device

Returns:



62
# File 'lib/adb_sdklib/device.rb', line 62

def screenshot(); RawImage.new(@device.getScreenshot()) end

#shell(command) ⇒ String #shell(command) {|line| ... } ⇒ self

Executes a shell command on the device, and receives the result.

Overloads:

  • #shell(command) ⇒ String

    Returns all results of the command.

    Parameters:

    • command (String)

      the command to execute

    Returns:

    • (String)

      all results of the command.

  • #shell(command) {|line| ... } ⇒ self

    Returns self.

    Parameters:

    • command (String)

      the command to execute

    Yields:

    • (line)

    Yield Parameters:

    • line (String)

      each line of results of the command.

    Returns:

    • (self)

      self

Returns:

  • (String, self)


145
146
147
148
149
150
# File 'lib/adb_sdklib/device.rb', line 145

def shell(command, &block)
  capture = CommandCapture.new(block_given? ? block : nil)
  receiver = Rjb::bind(capture, 'com.android.ddmlib.IShellOutputReceiver')
  @device.executeShellCommand(command.to_s, receiver)
  block_given? ? self : capture.to_s
end

#to_sString

Converts self to string.

Returns:

  • (String)

    convert to string



212
# File 'lib/adb_sdklib/device.rb', line 212

def to_s; "Android:#{self.serial}" end