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 =
{}
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
-
#device ⇒ String
Return the device type.
-
#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.
- #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.
-
#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
27 28 29 30 |
# File 'lib/device_api/android/device.rb', line 27 def initialize( = {}) @serial = [:serial] @state = [: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, = {} ) 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
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 |
#battery ⇒ Object
191 192 193 |
# File 'lib/device_api/android/device.rb', line 191 def battery get_battery_info end |
#battery_info ⇒ Object
230 231 232 |
# File 'lib/device_api/android/device.rb', line 230 def battery_info ADB.get_battery_info(serial) end |
#battery_level ⇒ String
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 |
#device ⇒ String
Return the device type
55 56 57 |
# File 'lib/device_api/android/device.rb', line 55 def device get_prop('ro.product.device') end |
#diskstat ⇒ Hash
Returns disk status
248 249 250 |
# File 'lib/device_api/android/device.rb', line 248 def diskstat get_disk_info end |
#dpi ⇒ String
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 |
#imei ⇒ String
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_address ⇒ Object
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_packages ⇒ Object
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 |
#manufacturer ⇒ String
Return the device manufacturer
67 68 69 |
# File 'lib/device_api/android/device.rb', line 67 def manufacturer get_prop('ro.product.manufacturer') end |
#memory ⇒ DeviceAPI::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 |
#model ⇒ String
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 |
#orientation ⇒ String
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 |
#range ⇒ String
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 |
#reboot ⇒ Object
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 |
#status ⇒ String
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 |
#type ⇒ Symbol
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 |
#unlock ⇒ Object
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 |
#uptime ⇒ Object
Returns the device uptime
253 254 255 |
# File 'lib/device_api/android/device.rb', line 253 def uptime ADB.get_uptime(serial) end |
#version ⇒ String
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_address ⇒ Object
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_status ⇒ Hash
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 |