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.
- #display_name ⇒ Object
-
#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.
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/device_api/android/device.rb', line 29 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.
13 14 15 |
# File 'lib/device_api/android/device.rb', line 13 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
24 25 26 27 |
# File 'lib/device_api/android/device.rb', line 24 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
18 19 20 21 |
# 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 |
Instance Method Details
#app_version_number(apk) ⇒ String, Exception
Return the app version number for a specified apk
207 208 209 210 211 212 |
# 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 ⇒ Object
238 239 240 |
# File 'lib/device_api/android/device.rb', line 238 def battery get_battery_info end |
#battery_info ⇒ Object
277 278 279 |
# File 'lib/device_api/android/device.rb', line 277 def battery_info ADB.get_battery_info(qualifier) end |
#battery_level ⇒ String
Return the battery level
126 127 128 |
# File 'lib/device_api/android/device.rb', line 126 def battery_level get_battery_info.level end |
#block_package(package) ⇒ Object
136 137 138 139 140 141 142 |
# 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 ⇒ Object
63 64 65 |
# File 'lib/device_api/android/device.rb', line 63 def connect ADB.connect(@ip_address, @port) end |
#device ⇒ String
Return the device type
102 103 104 |
# File 'lib/device_api/android/device.rb', line 102 def device get_prop('ro.product.device') end |
#disconnect ⇒ Object
67 68 69 70 71 72 |
# 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 ⇒ Hash
Returns disk status
295 296 297 |
# File 'lib/device_api/android/device.rb', line 295 def diskstat get_disk_info end |
#display_name ⇒ Object
79 80 81 82 |
# 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 ⇒ String
Return the DPI of the attached device
257 258 259 |
# File 'lib/device_api/android/device.rb', line 257 def dpi get_dpi(qualifier) end |
#imei ⇒ String
Get the IMEI number of the device
228 229 230 |
# File 'lib/device_api/android/device.rb', line 228 def imei get_phoneinfo['Device ID'] end |
#install(apk) ⇒ Symbol, Exception
Install a specified apk
164 165 166 167 168 169 170 171 172 173 174 |
# 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) ⇒ String
Return the stdout of executed intent
284 285 286 |
# File 'lib/device_api/android/device.rb', line 284 def intent(command) ADB.am(qualifier, command) end |
#ip_address ⇒ Object
Returns the Wifi IP address
305 306 307 308 309 310 311 312 313 314 |
# 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? ⇒ Boolean
Return whether device is connected or not
75 76 77 |
# File 'lib/device_api/android/device.rb', line 75 def is_connected? ADB.devices.any? {|device| device.include? qualifier} end |
#is_remote? ⇒ Boolean
46 47 48 |
# File 'lib/device_api/android/device.rb', line 46 def is_remote? @remote || false end |
#list_installed_packages ⇒ Object
199 200 201 202 |
# 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 ⇒ String
Return the device manufacturer
114 115 116 |
# File 'lib/device_api/android/device.rb', line 114 def manufacturer get_prop('ro.product.manufacturer') end |
#memory ⇒ DeviceAPI::Android::Plugins::Memory
Get the memory information for the current device
234 235 236 |
# File 'lib/device_api/android/device.rb', line 234 def memory get_memory_info end |
#model ⇒ String
Return the device model
108 109 110 |
# File 'lib/device_api/android/device.rb', line 108 def model get_prop('ro.product.model') end |
#monkey(args) ⇒ Object
Initiate monkey tests
216 217 218 |
# File 'lib/device_api/android/device.rb', line 216 def monkey(args) ADB.monkey(qualifier, args) end |
#orientation ⇒ String
Return the device orientation
146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# 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) ⇒ String, Exception
Return the package name for a specified apk
192 193 194 195 196 197 |
# 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? ⇒ Boolean
Is the device currently being powered?
132 133 134 |
# File 'lib/device_api/android/device.rb', line 132 def powered? get_battery_info.powered end |
#range ⇒ String
Return the device range
86 87 88 89 90 91 92 |
# 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 ⇒ Object
Reboots the device
289 290 291 |
# File 'lib/device_api/android/device.rb', line 289 def reboot ADB.reboot(qualifier, is_remote?) end |
#screen_on? ⇒ Boolean
Check if the devices screen is currently turned on
244 245 246 247 248 |
# 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) ⇒ Object
Capture screenshot on device
222 223 224 |
# File 'lib/device_api/android/device.rb', line 222 def screenshot(args) ADB.screencap(qualifier, args) end |
#serial_no ⇒ String
Return the serial number of device
96 97 98 |
# File 'lib/device_api/android/device.rb', line 96 def serial_no get_prop('ro.serialno') end |
#set_ip_and_port ⇒ Object
40 41 42 43 44 |
# 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 ⇒ String
Mapping of device status - used to provide a consistent status across platforms
52 53 54 55 56 57 58 59 60 61 |
# 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 ⇒ Symbol
Return the device type based on the DPI
263 264 265 266 267 268 269 |
# 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) ⇒ Symbol, Exception
Uninstall a specified package
179 180 181 182 183 184 185 186 187 |
# 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 ⇒ Object
Unlock the device by sending a wakeup command
251 252 253 |
# File 'lib/device_api/android/device.rb', line 251 def unlock ADB.keyevent(qualifier, '26') unless screen_on? end |
#uptime ⇒ Object
Returns the device uptime
300 301 302 |
# File 'lib/device_api/android/device.rb', line 300 def uptime ADB.get_uptime(qualifier) end |
#version ⇒ String
Return the Android OS version
120 121 122 |
# File 'lib/device_api/android/device.rb', line 120 def version get_prop('ro.build.version.release') end |
#wifi_mac_address ⇒ Object
Returns the Wifi mac address
317 318 319 320 321 322 323 324 325 326 |
# 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 ⇒ Hash
Returns wifi status and access point name
273 274 275 |
# File 'lib/device_api/android/device.rb', line 273 def wifi_status ADB.wifi(qualifier) end |