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 =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Device

Returns a new instance of Device.



28
29
30
31
32
33
34
35
36
37
# File 'lib/device_api/android/device.rb', line 28

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

Instance Attribute Details

#qualifierObject (readonly)

Returns the value of attribute qualifier.



12
13
14
# File 'lib/device_api/android/device.rb', line 12

def qualifier
  @qualifier
end

Class Method Details

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

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



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

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



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

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



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

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



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

def battery
  get_battery_info
end

#battery_infoObject



271
272
273
# File 'lib/device_api/android/device.rb', line 271

def battery_info
  ADB.get_battery_info(qualifier)
end

#battery_levelString

Return the battery level

Returns:

  • (String)

    device battery level



120
121
122
# File 'lib/device_api/android/device.rb', line 120

def battery_level
  get_battery_info['level']
end

#block_package(package) ⇒ Object



130
131
132
133
134
135
136
# File 'lib/device_api/android/device.rb', line 130

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

#connectObject



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

def connect
  ADB.connect(@ip_address, @port)
end

#deviceString

Return the device type

Returns:

  • (String)

    device type string



96
97
98
# File 'lib/device_api/android/device.rb', line 96

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

#disconnectObject



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

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

#diskstatHash

Returns disk status

Returns:

  • (Hash)

    containing disk statistics



289
290
291
# File 'lib/device_api/android/device.rb', line 289

def diskstat
  get_disk_info
end

#dpiString

Return the DPI of the attached device

Returns:

  • (String)

    DPI of attached device



251
252
253
# File 'lib/device_api/android/device.rb', line 251

def dpi
  get_dpi(qualifier)
end

#imeiString

Get the IMEI number of the device

Returns:

  • (String)

    IMEI number of current device



222
223
224
# File 'lib/device_api/android/device.rb', line 222

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



158
159
160
161
162
163
164
165
166
167
168
# File 'lib/device_api/android/device.rb', line 158

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



278
279
280
# File 'lib/device_api/android/device.rb', line 278

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

#ip_addressObject

Returns the Wifi IP address



299
300
301
302
303
304
305
306
307
308
# File 'lib/device_api/android/device.rb', line 299

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

Return whether device is connected or not

Returns:

  • (Boolean)


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

def is_connected?
  ADB.devices.any? {|device| device.include? qualifier}
end

#is_remote?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/device_api/android/device.rb', line 45

def is_remote?
  @remote || false
end

#list_installed_packagesObject



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

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

#manufacturerString

Return the device manufacturer

Returns:

  • (String)

    device manufacturer string



108
109
110
# File 'lib/device_api/android/device.rb', line 108

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



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

def memory
  get_memory_info
end

#modelString

Return the device model

Returns:

  • (String)

    device model string



102
103
104
# File 'lib/device_api/android/device.rb', line 102

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

#monkey(args) ⇒ Object

Initiate monkey tests

Parameters:

  • args (Hash)

    arguments to pass on to ADB.monkey



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

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

#orientationString

Return the device orientation

Returns:

  • (String)

    current device orientation



140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/device_api/android/device.rb', line 140

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



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

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



126
127
128
# File 'lib/device_api/android/device.rb', line 126

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



80
81
82
83
84
85
86
# File 'lib/device_api/android/device.rb', line 80

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

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

#rebootObject

Reboots the device



283
284
285
# File 'lib/device_api/android/device.rb', line 283

def reboot
  ADB.reboot(qualifier, is_remote?)
end

#screen_on?Boolean

Check if the devices screen is currently turned on

Returns:

  • (Boolean)

    true if the screen is on, otherwise false



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

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



216
217
218
# File 'lib/device_api/android/device.rb', line 216

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

#serial_noString

Return the serial number of device

Returns:

  • (String)

    serial number



90
91
92
# File 'lib/device_api/android/device.rb', line 90

def serial_no
  get_prop('ro.serialno')
end

#set_ip_and_portObject



39
40
41
42
43
# File 'lib/device_api/android/device.rb', line 39

def set_ip_and_port
  address = @qualifier.split(":")
  @ip_address = address.first
  @port = address.last
end

#statusString

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

Returns:

  • (String)

    common status string



51
52
53
54
55
56
57
58
59
60
# File 'lib/device_api/android/device.rb', line 51

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

#typeSymbol

Return the device type based on the DPI

Returns:

  • (Symbol)

    :tablet or :mobile based upon the devices DPI



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

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



173
174
175
176
177
178
179
180
181
# File 'lib/device_api/android/device.rb', line 173

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



245
246
247
# File 'lib/device_api/android/device.rb', line 245

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

#uptimeObject

Returns the device uptime



294
295
296
# File 'lib/device_api/android/device.rb', line 294

def uptime
  ADB.get_uptime(qualifier)
end

#versionString

Return the Android OS version

Returns:

  • (String)

    device Android version



114
115
116
# File 'lib/device_api/android/device.rb', line 114

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

#wifi_mac_addressObject

Returns the Wifi mac address



311
312
313
314
315
316
317
318
319
320
# File 'lib/device_api/android/device.rb', line 311

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_statusHash

Returns wifi status and access point name

Returns:

  • (Hash)

    :status and :access_point



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

def wifi_status
  ADB.wifi(qualifier)
end