Class: DeviceAPI::Android::Device

Inherits:
Device
  • Object
show all
Defined in:
lib/device_api/android/device.rb

Overview

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

Direct Known Subclasses

Kindle, Samsung

Constant Summary collapse

@@subclasses =
{}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Device

Returns a new instance of Device.



27
28
29
30
# File 'lib/device_api/android/device.rb', line 27

def initialize(options = {})
  @serial = options[:serial]
  @state = options[:state]
end

Class Method Details

.create(type, options = {}) ⇒ Object

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



22
23
24
25
# File 'lib/device_api/android/device.rb', line 22

def self.create(type, options = {} )
  return @@subclasses[type.to_sym].new(options) if @@subclasses[type.to_sym]
  return self.new(options)
end

.inherited(klass) ⇒ Object

Called by any inheritors to register themselves with the parent class



16
17
18
19
# File 'lib/device_api/android/device.rb', line 16

def self.inherited(klass)
  key = /::([^:]+)$/.match(klass.to_s.downcase)[1].to_sym
  @@subclasses[key] = klass
end

Instance Method Details

#app_version_number(apk) ⇒ String, Exception

Return the app version number for a specified apk

Parameters:

  • apk (String)

    string containing path to the apk

Returns:

  • (String, Exception)

    app version number if it can be found, otherwise an error is raised



161
162
163
164
165
166
# File 'lib/device_api/android/device.rb', line 161

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

#batteryObject



192
193
194
# File 'lib/device_api/android/device.rb', line 192

def battery
  get_battery_info
end

#battery_infoObject



231
232
233
# File 'lib/device_api/android/device.rb', line 231

def battery_info
  ADB.get_battery_info(serial)
end

#battery_levelString

Return the battery level

Returns:

  • (String)

    device battery level



80
81
82
# File 'lib/device_api/android/device.rb', line 80

def battery_level
  get_battery_info['level']
end

#block_package(package) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/device_api/android/device.rb', line 90

def block_package(package)
  if version < "5.0.0"
    ADB.block_package(serial, package)
  else
    ADB.hide_package(serial, package)
  end
end

#deviceString

Return the device type

Returns:

  • (String)

    device type string



56
57
58
# File 'lib/device_api/android/device.rb', line 56

def device
  get_prop('ro.product.device')
end

#diskstatHash

Returns disk status

Returns:

  • (Hash)

    containing disk statistics



249
250
251
# File 'lib/device_api/android/device.rb', line 249

def diskstat
  get_disk_info
end

#dpiString

Return the DPI of the attached device

Returns:

  • (String)

    DPI of attached device



211
212
213
# File 'lib/device_api/android/device.rb', line 211

def dpi
  get_dpi(serial)
end

#imeiString

Get the IMEI number of the device

Returns:

  • (String)

    IMEI number of current device



182
183
184
# File 'lib/device_api/android/device.rb', line 182

def imei
  get_phoneinfo['Device ID']
end

#install(apk) ⇒ Symbol, Exception

Install a specified apk

Parameters:

  • apk (String)

    string containing path to the apk to install

Returns:

  • (Symbol, Exception)

    :success when the apk installed successfully, otherwise an error is raised



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/device_api/android/device.rb', line 118

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) ⇒ String

Return the stdout of executed intent

Parameters:

  • command (String)

    to start the intent

Returns:

  • (String)

    stdout



238
239
240
# File 'lib/device_api/android/device.rb', line 238

def intent(command)
  ADB.am(serial, command)
end

#ip_addressObject

Returns the Wifi IP address



259
260
261
262
263
# File 'lib/device_api/android/device.rb', line 259

def ip_address
  network = get_network_info
  wlan0 = network.detect { |a| a[:name] == 'wlan0' }
  wlan0[:ip] unless wlan0.nil?
end

#list_installed_packagesObject



153
154
155
156
# File 'lib/device_api/android/device.rb', line 153

def list_installed_packages
  packages = ADB.pm(serial, 'list packages')
  packages.split("\r\n")
end

#manufacturerString

Return the device manufacturer

Returns:

  • (String)

    device manufacturer string



68
69
70
# File 'lib/device_api/android/device.rb', line 68

def manufacturer
  get_prop('ro.product.manufacturer')
end

#memoryDeviceAPI::Android::Plugins::Memory

Get the memory information for the current device

Returns:

  • (DeviceAPI::Android::Plugins::Memory)

    the memory plugin containing relevant information



188
189
190
# File 'lib/device_api/android/device.rb', line 188

def memory
  get_memory_info
end

#modelString

Return the device model

Returns:

  • (String)

    device model string



62
63
64
# File 'lib/device_api/android/device.rb', line 62

def model
  get_prop('ro.product.model')
end

#monkey(args) ⇒ Object

Initiate monkey tests

Parameters:

  • args (Hash)

    arguments to pass on to ADB.monkey



170
171
172
# File 'lib/device_api/android/device.rb', line 170

def monkey(args)
  ADB.monkey(serial, args)
end

#orientationString

Return the device orientation

Returns:

  • (String)

    current device orientation



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/device_api/android/device.rb', line 100

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) ⇒ String, Exception

Return the package name for a specified apk

Parameters:

  • apk (String)

    string containing path to the apk

Returns:

  • (String, Exception)

    package name if it can be found, otherwise an error is raised



146
147
148
149
150
151
# File 'lib/device_api/android/device.rb', line 146

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?Boolean

Is the device currently being powered?

Returns:

  • (Boolean)

    true if it is being powered in some way, false if it is unpowered



86
87
88
# File 'lib/device_api/android/device.rb', line 86

def powered?
  !get_battery_info.select { |keys| keys.include?('powered')}.select { |_,v| v == 'true' }.empty?
end

#rangeString

Return the device range

Returns:

  • (String)

    device range string



46
47
48
49
50
51
52
# File 'lib/device_api/android/device.rb', line 46

def range
  device = self.device
  model  = self.model

  return device if device == model
  "#{device}_#{model}"
end

#rebootObject

Reboots the device



243
244
245
# File 'lib/device_api/android/device.rb', line 243

def reboot
  ADB.reboot(serial)
end

#screen_on?Boolean

Check if the devices screen is currently turned on

Returns:

  • (Boolean)

    true if the screen is on, otherwise false



198
199
200
201
202
# File 'lib/device_api/android/device.rb', line 198

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) ⇒ Object

Capture screenshot on device

Parameters:

  • args (Hash)

    arguments to pass on to ADB.screencap



176
177
178
# File 'lib/device_api/android/device.rb', line 176

def screenshot(args)
  ADB.screencap(serial, args)
end

#statusString

Mapping of device status - used to provide a consistent status across platforms

Returns:

  • (String)

    common status string



34
35
36
37
38
39
40
41
42
# File 'lib/device_api/android/device.rb', line 34

def status
  {
      'device' => :ok,
      'no device' => :dead,
      'offline' => :offline,
      'unauthorized' => :unauthorized,
      'no_permissions' => :no_permissions
  }[@state]
end

#typeSymbol

Return the device type based on the DPI

Returns:

  • (Symbol)

    :tablet or :mobile based upon the devices DPI



217
218
219
220
221
222
223
# File 'lib/device_api/android/device.rb', line 217

def type
  if get_dpi.to_i > 533
    :tablet
  else
    :mobile
  end
end

#uninstall(package_name) ⇒ Symbol, Exception

Uninstall a specified package

Parameters:

  • package_name (String)

    name of the package to uninstall

Returns:

  • (Symbol, Exception)

    :success when the package is removed, otherwise an error is raised



133
134
135
136
137
138
139
140
141
# File 'lib/device_api/android/device.rb', line 133

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

#unlockObject

Unlock the device by sending a wakeup command



205
206
207
# File 'lib/device_api/android/device.rb', line 205

def unlock
  ADB.keyevent(serial, '26') unless screen_on?
end

#uptimeObject

Returns the device uptime



254
255
256
# File 'lib/device_api/android/device.rb', line 254

def uptime
  ADB.get_uptime(serial)
end

#versionString

Return the Android OS version

Returns:

  • (String)

    device Android version



74
75
76
# File 'lib/device_api/android/device.rb', line 74

def version
  get_prop('ro.build.version.release')
end

#wifi_mac_addressObject

Returns the Wifi mac address



266
267
268
269
270
# File 'lib/device_api/android/device.rb', line 266

def wifi_mac_address
  network = get_network_info
  wifi = network.detect { |a| a[:name] == 'wlan0' }
  wifi[:mac] unless wifi.nil?
end

#wifi_statusHash

Returns wifi status and access point name

Returns:

  • (Hash)

    :status and :access_point



227
228
229
# File 'lib/device_api/android/device.rb', line 227

def wifi_status
  ADB.wifi(serial)
end