Class: DeviceAPI::Android::Device
- Inherits:
-
Device
- Object
- Device
- DeviceAPI::Android::Device
- Defined in:
- lib/device_api/android/device.rb
Overview
Device class used for containing the accessors of the physical device information
Constant Summary collapse
- @@subclasses =
{}
Instance Attribute Summary collapse
-
#qualifier ⇒ Object
readonly
Returns the value of attribute qualifier.
Class Method Summary collapse
-
.create(type, options = {}) ⇒ Object
Returns an object of the specified type, if it exists.
-
.inherited(klass) ⇒ Object
Called by any inheritors to register themselves with the parent class.
Instance Method Summary collapse
-
#app_version_number(apk) ⇒ String, Exception
Return the app version number for a specified apk.
- #battery ⇒ Object
- #battery_info ⇒ Object
-
#battery_level ⇒ String
Return the battery level.
- #block_package(package) ⇒ Object
- #connect ⇒ Object
-
#device ⇒ String
Return the device type.
- #disconnect ⇒ Object
-
#diskstat ⇒ Hash
Returns disk status.
-
#dpi ⇒ String
Return the DPI of the attached device.
-
#imei ⇒ String
Get the IMEI number of the device.
-
#initialize(options = {}) ⇒ Device
constructor
A new instance of Device.
-
#install(apk) ⇒ Symbol, Exception
Install a specified apk.
-
#intent(command) ⇒ String
Return the stdout of executed intent.
-
#ip_address ⇒ Object
Returns the Wifi IP address.
-
#is_connected? ⇒ Boolean
Return whether device is connected or not.
- #is_remote? ⇒ Boolean
- #list_installed_packages ⇒ Object
-
#manufacturer ⇒ String
Return the device manufacturer.
-
#memory ⇒ DeviceAPI::Android::Plugins::Memory
Get the memory information for the current device.
-
#model ⇒ String
Return the device model.
-
#monkey(args) ⇒ Object
Initiate monkey tests.
-
#orientation ⇒ String
Return the device orientation.
-
#package_name(apk) ⇒ String, Exception
Return the package name for a specified apk.
-
#powered? ⇒ Boolean
Is the device currently being powered?.
-
#range ⇒ String
Return the device range.
-
#reboot ⇒ Object
Reboots the device.
-
#screen_on? ⇒ Boolean
Check if the devices screen is currently turned on.
-
#screenshot(args) ⇒ Object
Capture screenshot on device.
-
#serial_no ⇒ String
Return the serial number of device.
- #set_ip_and_port ⇒ Object
-
#status ⇒ String
Mapping of device status - used to provide a consistent status across platforms.
-
#type ⇒ Symbol
Return the device type based on the DPI.
-
#uninstall(package_name) ⇒ Symbol, Exception
Uninstall a specified package.
-
#unlock ⇒ Object
Unlock the device by sending a wakeup command.
-
#uptime ⇒ Object
Returns the device uptime.
-
#version ⇒ String
Return the Android OS version.
-
#wifi_mac_address ⇒ Object
Returns the Wifi mac address.
-
#wifi_status ⇒ Hash
Returns wifi status and access point name.
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( = {}) # For devices connected with USB, qualifier and serial are same @qualifier = @serial = [:serial] @state = [:state] @remote = [:remote] ? true : false if is_remote? set_ip_and_port @serial = self.serial_no if !["unknown", "offline"].include? @state end end |
Instance Attribute Details
#qualifier ⇒ Object (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, = {} ) return @@subclasses[type.to_sym].new() if @@subclasses[type.to_sym] return self.new() 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
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 |
#battery ⇒ Object
232 233 234 |
# File 'lib/device_api/android/device.rb', line 232 def battery get_battery_info end |
#battery_info ⇒ Object
271 272 273 |
# File 'lib/device_api/android/device.rb', line 271 def battery_info ADB.get_battery_info(qualifier) end |
#battery_level ⇒ String
Return the 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 |
#connect ⇒ Object
62 63 64 |
# File 'lib/device_api/android/device.rb', line 62 def connect ADB.connect(@ip_address, @port) end |
#device ⇒ String
Return the device type
96 97 98 |
# File 'lib/device_api/android/device.rb', line 96 def device get_prop('ro.product.device') end |
#disconnect ⇒ Object
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 |
#diskstat ⇒ Hash
Returns disk status
289 290 291 |
# File 'lib/device_api/android/device.rb', line 289 def diskstat get_disk_info end |
#dpi ⇒ String
Return the DPI of the attached device
251 252 253 |
# File 'lib/device_api/android/device.rb', line 251 def dpi get_dpi(qualifier) end |
#imei ⇒ String
Get the IMEI number of the 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
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
278 279 280 |
# File 'lib/device_api/android/device.rb', line 278 def intent(command) ADB.am(qualifier, command) end |
#ip_address ⇒ Object
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
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
45 46 47 |
# File 'lib/device_api/android/device.rb', line 45 def is_remote? @remote || false end |
#list_installed_packages ⇒ Object
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 |
#manufacturer ⇒ String
Return the device manufacturer
108 109 110 |
# File 'lib/device_api/android/device.rb', line 108 def manufacturer get_prop('ro.product.manufacturer') end |
#memory ⇒ DeviceAPI::Android::Plugins::Memory
Get the memory information for the current device
228 229 230 |
# File 'lib/device_api/android/device.rb', line 228 def memory get_memory_info end |
#model ⇒ String
Return the device model
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
210 211 212 |
# File 'lib/device_api/android/device.rb', line 210 def monkey(args) ADB.monkey(qualifier, args) end |
#orientation ⇒ String
Return the 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
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?
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 |
#range ⇒ String
Return the device range
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 |
#reboot ⇒ Object
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
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
216 217 218 |
# File 'lib/device_api/android/device.rb', line 216 def screenshot(args) ADB.screencap(qualifier, args) end |
#serial_no ⇒ String
Return the serial number of device
90 91 92 |
# File 'lib/device_api/android/device.rb', line 90 def serial_no get_prop('ro.serialno') end |
#set_ip_and_port ⇒ Object
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 |
#status ⇒ String
Mapping of device status - used to provide a consistent status across platforms
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 |
#type ⇒ Symbol
Return the device type based on the 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
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 |
#unlock ⇒ Object
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 |
#uptime ⇒ Object
Returns the device uptime
294 295 296 |
# File 'lib/device_api/android/device.rb', line 294 def uptime ADB.get_uptime(qualifier) end |
#version ⇒ String
Return the Android OS version
114 115 116 |
# File 'lib/device_api/android/device.rb', line 114 def version get_prop('ro.build.version.release') end |
#wifi_mac_address ⇒ Object
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_status ⇒ Hash
Returns wifi status and access point name
267 268 269 |
# File 'lib/device_api/android/device.rb', line 267 def wifi_status ADB.wifi(qualifier) end |