class DeviceAPI::Android::Device

Device class used for containing the accessors of the physical device information

Attributes

qualifier[R]

Public Class Methods

create(type, options = {} ) click to toggle source

Returns an object of the specified type, if it exists. Defaults to returning self

# File lib/device_api/android/device.rb, line 24
def self.create(type, options = {} )
  return @@subclasses[type.to_sym].new(options) if @@subclasses[type.to_sym]
  return self.new(options)
end
inherited(klass) click to toggle source

Called by any inheritors to register themselves with the parent class

# File lib/device_api/android/device.rb, line 18
def self.inherited(klass)
  key = /::([^:]+)$/.match(klass.to_s.downcase)[1].to_sym
  @@subclasses[key] = klass
end
new(options = {}) click to toggle source
# File lib/device_api/android/device.rb, line 29
def initialize(options = {})
  # For devices connected with USB, qualifier and serial are same
  @qualifier = @serial = options[:serial] 
  @state = options[:state]
  @remote = options[:remote] ? true : false
  if is_remote?
    set_ip_and_port
    @serial = self.serial_no if !["unknown", "offline"].include? @state
  end
end

Public Instance Methods

app_version_number(apk) click to toggle source

Return the app version number for a specified apk @param [String] apk string containing path to the apk @return [String, Exception] app version number if it can be found, otherwise an error is raised

# File lib/device_api/android/device.rb, line 207
def app_version_number(apk)
  @apk = apk
  result = get_app_props('package')['versionName']
  fail StandardError, 'Version number not found', caller if result.nil?
  result
end
battery() click to toggle source
# File lib/device_api/android/device.rb, line 238
def battery
  get_battery_info
end
battery_info() click to toggle source
# File lib/device_api/android/device.rb, line 277
def battery_info
  ADB.get_battery_info(qualifier)
end
battery_level() click to toggle source

Return the battery level @return (String) device battery level

# File lib/device_api/android/device.rb, line 126
def battery_level
  get_battery_info.level
end
block_package(package) click to toggle source
# File lib/device_api/android/device.rb, line 136
def block_package(package)
  if version < "5.0.0"
    ADB.block_package(qualifier, package)
  else
    ADB.hide_package(qualifier, package)
  end
end
connect() click to toggle source
# File lib/device_api/android/device.rb, line 63
def connect
  ADB.connect(@ip_address, @port)
end
device() click to toggle source

Return the device type @return (String) device type string

# File lib/device_api/android/device.rb, line 102
def device
  get_prop('ro.product.device')
end
disconnect() click to toggle source
# File lib/device_api/android/device.rb, line 67
def disconnect
  unless is_remote?
    raise DeviceAPI::Android::DeviceDisconnectedWhenNotARemoteDevice.new("Asked to disconnect device #{qualifier} when it is not a remote device")
  end
  ADB.disconnect(@ip_address, @port)
end
diskstat() click to toggle source

Returns disk status @return [Hash] containing disk statistics

# File lib/device_api/android/device.rb, line 295
def diskstat
  get_disk_info
end
display_name() click to toggle source
# File lib/device_api/android/device.rb, line 79
def display_name
  device = Android::Devices.search_by_model(model)
  device.model unless device.nil?
end
dpi() click to toggle source

Return the DPI of the attached device @return [String] DPI of attached device

# File lib/device_api/android/device.rb, line 257
def dpi
  get_dpi(qualifier)
end
imei() click to toggle source

Get the IMEI number of the device @return (String) IMEI number of current device

# File lib/device_api/android/device.rb, line 228
def imei
  get_phoneinfo['Device ID']
end
install(apk) click to toggle source

Install a specified apk @param [String] apk string containing path to the apk to install @return [Symbol, Exception] :success when the apk installed successfully, otherwise an error is raised

# File lib/device_api/android/device.rb, line 164
def install(apk)
  fail StandardError, 'No apk specified.', caller if apk.empty?
  res = install_apk(apk)

  case res
    when 'Success'
      :success
    else
      fail StandardError, res, caller
  end
end
intent(command) click to toggle source

@param [String] command to start the intent Return the stdout of executed intent @return [String] stdout

# File lib/device_api/android/device.rb, line 284
def intent(command)
  ADB.am(qualifier, command)
end
ip_address() click to toggle source

Returns the Wifi IP address

# File lib/device_api/android/device.rb, line 305
def ip_address
  interface = ADB.get_network_interface(qualifier, 'wlan0')
  if interface.match(/ip (.*) mask/)
    Regexp.last_match[1]
  elsif interface.match(/inet addr:(.*)\s+Bcast/)
    Regexp.last_match[1].strip
  else
    # No match, wifi down?
  end
end
is_connected?() click to toggle source

Return whether device is connected or not

# File lib/device_api/android/device.rb, line 75
def is_connected?
  ADB.devices.any? {|device| device.include? qualifier}
end
is_remote?() click to toggle source
# File lib/device_api/android/device.rb, line 46
def is_remote?
  @remote || false
end
list_installed_packages() click to toggle source
# File lib/device_api/android/device.rb, line 199
def list_installed_packages
  packages = ADB.pm(qualifier, 'list packages')
  packages.split("\r\n")
end
manufacturer() click to toggle source

Return the device manufacturer @return (String) device manufacturer string

# File lib/device_api/android/device.rb, line 114
def manufacturer
  get_prop('ro.product.manufacturer')
end
memory() click to toggle source

Get the memory information for the current device @return [DeviceAPI::Android::Plugins::Memory] the memory plugin containing relevant information

# File lib/device_api/android/device.rb, line 234
def memory
  get_memory_info
end
model() click to toggle source

Return the device model @return (String) device model string

# File lib/device_api/android/device.rb, line 108
def model
  get_prop('ro.product.model')
end
monkey(args) click to toggle source

Initiate monkey tests @param [Hash] args arguments to pass on to ADB.monkey

# File lib/device_api/android/device.rb, line 216
def monkey(args)
  ADB.monkey(qualifier, args)
end
orientation() click to toggle source

Return the device orientation @return (String) current device orientation

# File lib/device_api/android/device.rb, line 146
def orientation
  res = get_dumpsys('SurfaceOrientation')

  case res
    when '0','2'
      :portrait
    when '1', '3'
      :landscape
    when nil
      fail StandardError, 'No output returned is there a device connected?', caller
    else
      fail StandardError, "Device orientation not returned got: #{res}.", caller
  end
end
package_name(apk) click to toggle source

Return the package name for a specified apk @param [String] apk string containing path to the apk @return [String, Exception] package name if it can be found, otherwise an error is raised

# File lib/device_api/android/device.rb, line 192
def package_name(apk)
  @apk = apk
  result = get_app_props('package')['name']
  fail StandardError, 'Package name not found', caller if result.nil?
  result
end
powered?() click to toggle source

Is the device currently being powered? @return (Boolean) true if it is being powered in some way, false if it is unpowered

# File lib/device_api/android/device.rb, line 132
def powered?
  get_battery_info.powered
end
range() click to toggle source

Return the device range @return (String) device range string

# File lib/device_api/android/device.rb, line 86
def range
  device = self.device
  model  = self.model

  return device if device == model
  "#{device}_#{model}"
end
reboot() click to toggle source

Reboots the device

# File lib/device_api/android/device.rb, line 289
def reboot
  ADB.reboot(qualifier, is_remote?)
end
resolution() click to toggle source
# File lib/device_api/android/device.rb, line 328
def resolution
 res = ADB.dumpsys(qualifier, 'window | grep mUnrestrictedScreen')
 /^.* (.*)x(.*)$/.match(res.first)
end
screen_on?() click to toggle source

Check if the devices screen is currently turned on @return [Boolean] true if the screen is on, otherwise false

# File lib/device_api/android/device.rb, line 244
def screen_on?
  power = get_powerinfo
  return true if power['mScreenOn'].to_s.downcase == 'true' || power['Display Power: state'].to_s.downcase == 'on'
  false
end
screenshot(args) click to toggle source

Capture screenshot on device @param [Hash] args arguments to pass on to ADB.screencap

# File lib/device_api/android/device.rb, line 222
def screenshot(args)
  ADB.screencap(qualifier, args)
end
serial_no() click to toggle source

Return the serial number of device @return (String) serial number

# File lib/device_api/android/device.rb, line 96
def serial_no
  get_prop('ro.serialno')
end
set_ip_and_port() click to toggle source
# File lib/device_api/android/device.rb, line 40
def set_ip_and_port
  address = @qualifier.split(":")
  @ip_address = address.first
  @port = address.last
end
status() click to toggle source

Mapping of device status - used to provide a consistent status across platforms @return (String) common status string

# File lib/device_api/android/device.rb, line 52
def status
  {
      'device' => :ok,
      'no device' => :dead,
      'offline' => :offline,
      'unauthorized' => :unauthorized,
      'no permissions' => :no_permissions,
      'unknown' => :unknown
  }[@state]
end
type() click to toggle source

Return the device type based on the DPI @return [Symbol] :tablet or :mobile based upon the devices DPI

# File lib/device_api/android/device.rb, line 263
def type
  if get_dpi.to_i > 533
    :tablet
  else
    :mobile
  end
end
uninstall(package_name) click to toggle source

Uninstall a specified package @param [String] package_name name of the package to uninstall @return [Symbol, Exception] :success when the package is removed, otherwise an error is raised

# File lib/device_api/android/device.rb, line 179
def uninstall(package_name)
  res = uninstall_apk(package_name)
  case res
    when 'Success'
      :success
    else
      fail StandardError, "Unable to install 'package_name' Error Reported: #{res}", caller
  end
end
unlock() click to toggle source

Unlock the device by sending a wakeup command

# File lib/device_api/android/device.rb, line 251
def unlock
  ADB.keyevent(qualifier, '26') unless screen_on?
end
uptime() click to toggle source

Returns the device uptime

# File lib/device_api/android/device.rb, line 300
def uptime
  ADB.get_uptime(qualifier)
end
version() click to toggle source

Return the Android OS version @return (String) device Android version

# File lib/device_api/android/device.rb, line 120
def version
  get_prop('ro.build.version.release')
end
wifi_mac_address() click to toggle source

Returns the Wifi mac address

# File lib/device_api/android/device.rb, line 317
def wifi_mac_address
  interface = ADB.get_network_interface(qualifier, 'wlan0')
  if interface.match(/HWaddr (.*)/)
    Regexp.last_match[1].strip
  else
    network = get_network_info
    wifi = network.detect { |a| a[:name] == 'wlan0' }
    wifi[:mac] unless wifi.nil?
  end
end
wifi_status() click to toggle source

Returns wifi status and access point name @return [Hash] :status and :access_point

# File lib/device_api/android/device.rb, line 273
def wifi_status
  ADB.wifi(qualifier)
end

Private Instance Methods

get_app_props(key) click to toggle source
# File lib/device_api/android/device.rb, line 354
def get_app_props(key)
  unless @app_props
    @app_props = AAPT.get_app_props(@apk)
  end
  @app_props.each { |x| break x[key] }
end
get_battery_info() click to toggle source
# File lib/device_api/android/device.rb, line 344
def get_battery_info
  @battery = DeviceAPI::Android::Plugin::Battery.new(qualifier: qualifier) unless @battery
  @battery
end
get_disk_info() click to toggle source
# File lib/device_api/android/device.rb, line 339
def get_disk_info
  @diskstat = DeviceAPI::Android::Plugin::Disk.new(qualifier: qualifier) unless @diskstat
  @diskstat.process_stats      
end
get_dpi() click to toggle source
# File lib/device_api/android/device.rb, line 389
def get_dpi
  ADB.get_device_dpi(qualifier)
end
get_dumpsys(key) click to toggle source
# File lib/device_api/android/device.rb, line 368
def get_dumpsys(key)
  @props = ADB.getdumpsys(qualifier)
  @props[key]
end
get_memory_info() click to toggle source
# File lib/device_api/android/device.rb, line 349
def get_memory_info
  @memory = DeviceAPI::Android::Plugin::Memory.new(qualifier: qualifier) unless @memory
  @memory
end
get_network_info() click to toggle source
# File lib/device_api/android/device.rb, line 335
def get_network_info
  ADB.get_network_info(qualifier)
end
get_phoneinfo() click to toggle source
# File lib/device_api/android/device.rb, line 377
def get_phoneinfo
  ADB.getphoneinfo(qualifier)
end
get_powerinfo() click to toggle source
# File lib/device_api/android/device.rb, line 373
def get_powerinfo
  ADB.getpowerinfo(qualifier)
end
get_prop(key) click to toggle source
# File lib/device_api/android/device.rb, line 361
def get_prop(key)
  if !@props || !@props[key]
    @props = ADB.getprop(qualifier)
  end
  @props[key]
end
install_apk(apk) click to toggle source
# File lib/device_api/android/device.rb, line 381
def install_apk(apk)
  ADB.install_apk(apk: apk, qualifier: qualifier)
end
uninstall_apk(package_name) click to toggle source
# File lib/device_api/android/device.rb, line 385
def uninstall_apk(package_name)
  ADB.uninstall_apk(package_name: package_name, qualifier: qualifier)
end