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



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



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

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



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

def battery
  get_battery_info
end

#battery_infoObject



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

def battery_info
  ADB.get_battery_info(serial)
end

#battery_levelString

Return the battery level



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

def battery_level
  get_battery_info['level']
end

#block_package(package) ⇒ Object



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

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



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

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

#diskstatHash

Returns disk status



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

def diskstat
  get_disk_info
end

#dpiString

Return the DPI of the attached device



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

def dpi
  get_dpi(serial)
end

#imeiString

Get the IMEI number of the device



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

def imei
  get_phoneinfo['Device ID']
end

#install(apk) ⇒ Symbol, Exception

Install a specified apk



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

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



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

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

#ip_addressObject

Returns the Wifi IP address



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

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

#list_installed_packagesObject



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

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

#manufacturerString

Return the device manufacturer



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

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

#memoryDeviceAPI::Android::Plugins::Memory

Get the memory information for the current device



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

def memory
  get_memory_info
end

#modelString

Return the device model



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

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

#monkey(args) ⇒ Object

Initiate monkey tests



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

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

#orientationString

Return the device orientation



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

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



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

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?



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

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

#rangeString

Return the device range



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

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

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

#rebootObject

Reboots the device



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

def reboot
  ADB.reboot(serial)
end

#screen_on?Boolean

Check if the devices screen is currently turned on



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

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



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

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

#statusString

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



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

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

#typeSymbol

Return the device type based on the DPI



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

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

#uninstall(package_name) ⇒ Symbol, Exception

Uninstall a specified package



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

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



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

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

#uptimeObject

Returns the device uptime



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

def uptime
  ADB.get_uptime(serial)
end

#versionString

Return the Android OS version



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

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

#wifi_mac_addressObject

Returns the Wifi mac address



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

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



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

def wifi_status
  ADB.wifi(serial)
end