Class: DeviceAPI::Android::ADB
- Inherits:
-
Execution
- Object
- Execution
- DeviceAPI::Android::ADB
- Defined in:
- lib/device_api/android/adb.rb
Overview
Namespace for all methods encapsulating adb calls
Class Method Summary collapse
-
.am(serial, command) ⇒ Object
Starts intent using adb Returns stdout DeviceAPI::ADB.am(serial, “start -a android.intent.action.MAIN -n com.android.settings/.wifi.WifiSettings”).
-
.block_package(serial, package) ⇒ Object
Blocks a package, used on Android versions less than KitKat Returns boolean.
- .change_apk(options = {}) ⇒ Object
-
.devices ⇒ Array
Returns an array representing connected devices DeviceAPI::ADB.devices #=> { ‘1232132’ => ‘device’ }.
-
.dumpsys(serial, command) ⇒ Array
Returns the ‘dumpsys’ information from the specified device.
-
.get_battery_info(serial) ⇒ Hash
Get the ‘battery’ information from dumpsys.
- .get_device_dpi(serial) ⇒ Object
-
.get_network_info(serial) ⇒ Object
Get the network information.
-
.get_state(serial) ⇒ String
Retrieve device state for a single device.
-
.get_uptime(serial) ⇒ Float
Returns the uptime of the specified device.
-
.getdumpsys(serial) ⇒ Hash
Get the ‘input’ information from dumpsys.
-
.getphoneinfo(serial) ⇒ Hash
Get the ‘iphonesubinfo’ from dumpsys.
-
.getpowerinfo(serial) ⇒ Hash
Get the ‘power’ information from dumpsys.
-
.getprop(serial) ⇒ Hash
Get the properties of a specified device.
-
.hide_package(serial, package) ⇒ Object
Blocks a package on KitKat and above Returns boolean.
-
.install_apk(options = {}) ⇒ String
Installs a specified apk to a specific device.
-
.keyevent(serial, keyevent) ⇒ Object
Sends a key event to the specified device.
-
.monkey(serial, args) ⇒ Object
Runs monkey testing.
-
.pm(serial, command) ⇒ Object
Package manager commands.
-
.process_dumpsys(regex_string, data) ⇒ Hash
Processes the results from dumpsys to format them into a hash.
-
.reboot(serial) ⇒ nil
Reboots the specified device.
-
.screencap(serial, args) ⇒ Object
Take a screenshot from the device.
-
.shell(serial, command) ⇒ Object
ADB Shell command.
-
.swipe(serial, coords = {x_from: 0, x_to: 0, y_from: 0, y_to: 0 }) ⇒ Object
Sends a swipe command to the specified device.
-
.uninstall_apk(options = {}) ⇒ String
Uninstalls a specified package from a specified device.
-
.wifi(serial) ⇒ Object
Returns wifi status and access point name.
Class Method Details
.am(serial, command) ⇒ Object
Starts intent using adb Returns stdout DeviceAPI::ADB.am(serial, “start -a android.intent.action.MAIN -n com.android.settings/.wifi.WifiSettings”)
283 284 285 |
# File 'lib/device_api/android/adb.rb', line 283 def self.am(serial, command) shell(serial, "am #{command}").stdout end |
.block_package(serial, package) ⇒ Object
Blocks a package, used on Android versions less than KitKat Returns boolean
299 300 301 302 |
# File 'lib/device_api/android/adb.rb', line 299 def self.block_package(serial, package) result = pm(serial, "block #{package}") result.include?('true') end |
.change_apk(options = {}) ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/device_api/android/adb.rb', line 144 def self.change_apk( = {}) package_name = [:package_name] apk = [:apk] serial = [:serial] action = [:action] case action when :install command = "adb -s #{serial} install #{apk}" when :uninstall command = "adb -s #{serial} uninstall #{package_name}" else raise ADBCommandError.new('No action specified') end result = execute(command) raise ADBCommandError.new(result.stderr) if result.exit != 0 lines = result.stdout.split("\n").map { |line| line.strip } lines.last end |
.devices ⇒ Array
Returns an array representing connected devices DeviceAPI::ADB.devices #=> { ‘1232132’ => ‘device’ }
17 18 19 20 21 22 |
# File 'lib/device_api/android/adb.rb', line 17 def self.devices result = execute_with_timeout_and_retry('adb devices') raise ADBCommandError.new(result.stderr) if result.exit != 0 result.stdout.scan(/(.*)\t(.*)/).map { |a,b| {a => b}} end |
.dumpsys(serial, command) ⇒ Array
Returns the ‘dumpsys’ information from the specified device
119 120 121 122 |
# File 'lib/device_api/android/adb.rb', line 119 def self.dumpsys(serial, command) result = shell(serial, "dumpsys #{command}") result.stdout.split("\n").map { |line| line.strip } end |
.get_battery_info(serial) ⇒ Hash
Get the ‘battery’ information from dumpsys
67 68 69 70 |
# File 'lib/device_api/android/adb.rb', line 67 def self.get_battery_info(serial) lines = dumpsys(serial, 'battery') process_dumpsys('(.*):\s+(.*)', lines) end |
.get_device_dpi(serial) ⇒ Object
105 106 107 108 109 110 111 112 113 114 |
# File 'lib/device_api/android/adb.rb', line 105 def self.get_device_dpi(serial) lines = dumpsys(serial, 'window') dpi = nil lines.each do |line| if /sw(\d*)dp/.match(line) dpi = Regexp.last_match[1] end end dpi end |
.get_network_info(serial) ⇒ Object
Get the network information
73 74 75 76 77 78 79 |
# File 'lib/device_api/android/adb.rb', line 73 def self.get_network_info(serial) lines = shell(serial, 'netcfg') lines.stdout.split("\n").map do |a| b = a.split(" ") { name: b[0], ip: b[2].split('/')[0], mac: b[4] } end end |
.get_state(serial) ⇒ String
Retrieve device state for a single device
27 28 29 30 31 32 33 34 35 |
# File 'lib/device_api/android/adb.rb', line 27 def self.get_state(serial) result = execute('adb get-state -s #{serial}') raise ADBCommandError.new(result.stderr) if result.exit != 0 lines = result.stdout.split("\n") /(.*)/.match(lines.last) Regexp.last_match[0].strip end |
.get_uptime(serial) ⇒ Float
Returns the uptime of the specified device
171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/device_api/android/adb.rb', line 171 def self.get_uptime(serial) result = shell(serial, 'cat /proc/uptime') lines = result.stdout.split("\n") uptime = 0 lines.each do |l| if /([\d.]*)\s+[\d.]*/.match(l) uptime = Regexp.last_match[0].to_f.round end end uptime end |
.getdumpsys(serial) ⇒ Hash
Get the ‘input’ information from dumpsys
51 52 53 54 |
# File 'lib/device_api/android/adb.rb', line 51 def self.getdumpsys(serial) lines = dumpsys(serial, 'input') process_dumpsys('(.*):\s+(.*)', lines) end |
.getphoneinfo(serial) ⇒ Hash
Get the ‘iphonesubinfo’ from dumpsys
59 60 61 62 |
# File 'lib/device_api/android/adb.rb', line 59 def self.getphoneinfo(serial) lines = dumpsys(serial, 'iphonesubinfo') process_dumpsys('(.*) =\s+(.*)', lines) end |
.getpowerinfo(serial) ⇒ Hash
Get the ‘power’ information from dumpsys
100 101 102 103 |
# File 'lib/device_api/android/adb.rb', line 100 def self.getpowerinfo(serial) lines = dumpsys(serial, 'power') process_dumpsys('(.*)=(.*)', lines) end |
.getprop(serial) ⇒ Hash
Get the properties of a specified device
40 41 42 43 44 45 46 |
# File 'lib/device_api/android/adb.rb', line 40 def self.getprop(serial) result = shell(serial, 'getprop') lines = result.stdout.encode('UTF-16', 'UTF-8', invalid: :replace, replace: '').encode('UTF-8', 'UTF-16').split("\n") process_dumpsys('\[(.*)\]:\s+\[(.*)\]', lines) end |
.hide_package(serial, package) ⇒ Object
Blocks a package on KitKat and above Returns boolean
308 309 310 311 |
# File 'lib/device_api/android/adb.rb', line 308 def self.hide_package(serial, package) result = pm(serial, "hide #{package}") result.include?('true') end |
.install_apk(options = {}) ⇒ String
Installs a specified apk to a specific device
129 130 131 132 |
# File 'lib/device_api/android/adb.rb', line 129 def self.install_apk( = {}) [:action] = :install change_apk() end |
.keyevent(serial, keyevent) ⇒ Object
Sends a key event to the specified device
244 245 246 |
# File 'lib/device_api/android/adb.rb', line 244 def self.keyevent(serial, keyevent) shell(serial, "input keyevent #{keyevent}").stdout end |
.monkey(serial, args) ⇒ Object
Runs monkey testing
201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/device_api/android/adb.rb', line 201 def self.monkey(serial, args) events = args[:events] || 10000 package = args[:package] or raise "package name not provided (:package => 'bbc.iplayer')" seed = args[:seed] throttle = args[:throttle] cmd = "monkey -p #{package} -v #{events}" cmd = cmd + " -s #{seed}" if seed cmd = cmd + " -t #{throttle}" if throttle shell(serial, cmd) end |
.pm(serial, command) ⇒ Object
Package manager commands
291 292 293 |
# File 'lib/device_api/android/adb.rb', line 291 def self.pm(serial, command) shell(serial, "pm #{command}").stdout end |
.process_dumpsys(regex_string, data) ⇒ Hash
Processes the results from dumpsys to format them into a hash
85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/device_api/android/adb.rb', line 85 def self.process_dumpsys(regex_string, data) props = {} regex = Regexp.new(regex_string) data.each do |line| if regex.match(line) props[Regexp.last_match[1]] = Regexp.last_match[2] end end props end |
.reboot(serial) ⇒ nil
Reboots the specified device
187 188 189 190 |
# File 'lib/device_api/android/adb.rb', line 187 def self.reboot(serial) result = execute("adb -s #{serial} reboot") raise ADBCommandError.new(result.stderr) if result.exit != 0 end |
.screencap(serial, args) ⇒ Object
Take a screenshot from the device
221 222 223 224 225 226 227 228 229 |
# File 'lib/device_api/android/adb.rb', line 221 def self.screencap( serial, args ) filename = args[:filename] or raise "filename not provided (:filename => '/tmp/myfile.png')" convert_carriage_returns = %q{perl -pe 's/\x0D\x0A/\x0A/g'} cmd = "screencap -p | #{convert_carriage_returns} > #{filename}" shell(serial, cmd) end |
.shell(serial, command) ⇒ Object
ADB Shell command
251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/device_api/android/adb.rb', line 251 def self.shell(serial, command) result = execute("adb -s #{serial} shell #{command}") case result.stderr when /^error: device unauthorized./ raise DeviceAPI::UnauthorizedDevice, result.stderr when /^error: device not found/ raise DeviceAPI::DeviceNotFound, result.stderr else raise ADBCommandError.new(result.stderr) end if result.exit != 0 result end |
.swipe(serial, coords = {x_from: 0, x_to: 0, y_from: 0, y_to: 0 }) ⇒ Object
Sends a swipe command to the specified device
273 274 275 |
# File 'lib/device_api/android/adb.rb', line 273 def self.swipe(serial, coords = {x_from: 0, x_to: 0, y_from: 0, y_to: 0 }) shell(serial, "input swipe #{coords[:x_from]} #{coords[:x_to]} #{coords[:y_from]} #{coords[:y_to]}").stdout end |
.uninstall_apk(options = {}) ⇒ String
Uninstalls a specified package from a specified device
139 140 141 142 |
# File 'lib/device_api/android/adb.rb', line 139 def self.uninstall_apk( = {}) [:action] = :uninstall change_apk() end |
.wifi(serial) ⇒ Object
Returns wifi status and access point name
235 236 237 238 239 |
# File 'lib/device_api/android/adb.rb', line 235 def self.wifi(serial) result = shell(serial, 'dumpsys wifi | grep mNetworkInfo') {:status => result.stdout.match("state:(.*?),")[1].strip, :access_point => result.stdout.match("extra:(.*?),")[1].strip.gsub(/"/,'')} end |