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
Returns a new instance of 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
161 162 163 164 165 166 |
# File 'lib/device_api/android/device.rb', line 161 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
192 193 194 |
# File 'lib/device_api/android/device.rb', line 192 def battery get_battery_info end |
#battery_info ⇒ Object
231 232 233 |
# File 'lib/device_api/android/device.rb', line 231 def battery_info ADB.get_battery_info(serial) end |
#battery_level ⇒ String
Return the battery level
80 81 82 |
# File 'lib/device_api/android/device.rb', line 80 def battery_level get_battery_info['level'] end |
#block_package(package) ⇒ Object
90 91 92 93 94 95 96 |
# File 'lib/device_api/android/device.rb', line 90 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
56 57 58 |
# File 'lib/device_api/android/device.rb', line 56 def device get_prop('ro.product.device') end |
#diskstat ⇒ Hash
Returns disk status
249 250 251 |
# File 'lib/device_api/android/device.rb', line 249 def diskstat get_disk_info end |
#dpi ⇒ String
Return the DPI of the attached device
211 212 213 |
# File 'lib/device_api/android/device.rb', line 211 def dpi get_dpi(serial) end |
#imei ⇒ String
Get the IMEI number of the device
182 183 184 |
# File 'lib/device_api/android/device.rb', line 182 def imei get_phoneinfo['Device ID'] end |
#install(apk) ⇒ Symbol, Exception
Install a specified apk
118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/device_api/android/device.rb', line 118 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
238 239 240 |
# File 'lib/device_api/android/device.rb', line 238 def intent(command) ADB.am(serial, command) end |
#ip_address ⇒ Object
Returns the Wifi IP address
259 260 261 262 263 |
# File 'lib/device_api/android/device.rb', line 259 def ip_address network = get_network_info wlan0 = network.detect { |a| a[:name] == 'wlan0' } wlan0[:ip] unless wlan0.nil? end |
#list_installed_packages ⇒ Object
153 154 155 156 |
# File 'lib/device_api/android/device.rb', line 153 def list_installed_packages packages = ADB.pm(serial, 'list packages') packages.split("\r\n") end |
#manufacturer ⇒ String
Return the device manufacturer
68 69 70 |
# File 'lib/device_api/android/device.rb', line 68 def manufacturer get_prop('ro.product.manufacturer') end |
#memory ⇒ DeviceAPI::Android::Plugins::Memory
Get the memory information for the current device
188 189 190 |
# File 'lib/device_api/android/device.rb', line 188 def memory get_memory_info end |
#model ⇒ String
Return the device model
62 63 64 |
# File 'lib/device_api/android/device.rb', line 62 def model get_prop('ro.product.model') end |
#monkey(args) ⇒ Object
Initiate monkey tests
170 171 172 |
# File 'lib/device_api/android/device.rb', line 170 def monkey(args) ADB.monkey(serial, args) end |
#orientation ⇒ String
Return the device orientation
100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/device_api/android/device.rb', line 100 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
146 147 148 149 150 151 |
# File 'lib/device_api/android/device.rb', line 146 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?
86 87 88 |
# File 'lib/device_api/android/device.rb', line 86 def powered? !get_battery_info.select { |keys| keys.include?('powered')}.select { |_,v| v == 'true' }.empty? end |
#range ⇒ String
Return the device range
46 47 48 49 50 51 52 |
# File 'lib/device_api/android/device.rb', line 46 def range device = self.device model = self.model return device if device == model "#{device}_#{model}" end |
#reboot ⇒ Object
Reboots the device
243 244 245 |
# File 'lib/device_api/android/device.rb', line 243 def reboot ADB.reboot(serial) end |
#screen_on? ⇒ Boolean
Check if the devices screen is currently turned on
198 199 200 201 202 |
# File 'lib/device_api/android/device.rb', line 198 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
176 177 178 |
# File 'lib/device_api/android/device.rb', line 176 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 42 |
# File 'lib/device_api/android/device.rb', line 34 def status { 'device' => :ok, 'no device' => :dead, 'offline' => :offline, 'unauthorized' => :unauthorized, 'no_permissions' => :no_permissions }[@state] end |
#type ⇒ Symbol
Return the device type based on the DPI
217 218 219 220 221 222 223 |
# File 'lib/device_api/android/device.rb', line 217 def type if get_dpi.to_i > 533 :tablet else :mobile end end |
#uninstall(package_name) ⇒ Symbol, Exception
Uninstall a specified package
133 134 135 136 137 138 139 140 141 |
# File 'lib/device_api/android/device.rb', line 133 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
205 206 207 |
# File 'lib/device_api/android/device.rb', line 205 def unlock ADB.keyevent(serial, '26') unless screen_on? end |
#uptime ⇒ Object
Returns the device uptime
254 255 256 |
# File 'lib/device_api/android/device.rb', line 254 def uptime ADB.get_uptime(serial) end |
#version ⇒ String
Return the Android OS version
74 75 76 |
# File 'lib/device_api/android/device.rb', line 74 def version get_prop('ro.build.version.release') end |
#wifi_mac_address ⇒ Object
Returns the Wifi mac address
266 267 268 269 270 |
# File 'lib/device_api/android/device.rb', line 266 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
227 228 229 |
# File 'lib/device_api/android/device.rb', line 227 def wifi_status ADB.wifi(serial) end |