class ADB

Class to handle Android Debug Bridge shell commands

Public Class Methods

airplane_mode() click to toggle source
# File lib/android-adb-extension/adb.rb, line 55
def airplane_mode
  `#{adb_shell_command} settings get global airplane_mode_on`.strip.to_i
end
airplane_mode?() click to toggle source
# File lib/android-adb-extension/adb.rb, line 59
def airplane_mode?
  airplane_mode.to_boolean
end
bring_to_foreground(package, activity) click to toggle source
# File lib/android-adb-extension/adb.rb, line 92
def bring_to_foreground(package, activity)
  res = `#{adb_shell_command} am start -n #{package}/#{activity}`
  res.empty? ? nil : res
end
device_name() click to toggle source
# File lib/android-adb-extension/adb.rb, line 21
def device_name
  `#{adb_shell_command} getprop ro.product.model`.strip
end
disable_airplane_mode() click to toggle source
# File lib/android-adb-extension/adb.rb, line 67
def disable_airplane_mode
  change_airplane_mode(0)
end
enable_airplane_mode() click to toggle source
# File lib/android-adb-extension/adb.rb, line 63
def enable_airplane_mode
  change_airplane_mode(1)
end
h()
Alias for: help
help() click to toggle source
# File lib/android-adb-extension/adb.rb, line 133
def help
  public_methods(false)
end
Also aliased as: h
input_text(text) click to toggle source
# File lib/android-adb-extension/adb.rb, line 117
def input_text(text)
  res = `#{adb_shell_command} input text #{text}`
  res.empty? ? nil : res
end
landscape?() click to toggle source
# File lib/android-adb-extension/adb.rb, line 36
def landscape?
  rotation = orientation
  rotation.eql?(1) || rotation.eql?(3)
end
lock() click to toggle source
# File lib/android-adb-extension/adb.rb, line 75
def lock
  cmd = "#{adb_shell_command} input keyevent 26"
  `#{cmd}`
  res = `#{cmd}`
  res.empty? ? nil : res
end
major() click to toggle source
# File lib/android-adb-extension/adb.rb, line 13
def major
  release.chomp.split('.').first.to_i
end
monkey(package, event_count = 500) click to toggle source
# File lib/android-adb-extension/adb.rb, line 71
def monkey(package, event_count = 500)
  `#{adb_shell_command} monkey -p #{package} #{event_count}`
end
orientation() click to toggle source
# File lib/android-adb-extension/adb.rb, line 25
def orientation
  `#{adb_shell_command} dumpsys input |
   grep 'SurfaceOrientation' |
   awk '{ print $2 }'`.strip.to_i
end
portrait?() click to toggle source
# File lib/android-adb-extension/adb.rb, line 31
def portrait?
  rotation = orientation
  rotation.eql?(0) || rotation.eql?(2)
end
release() click to toggle source
# File lib/android-adb-extension/adb.rb, line 9
def release
  `#{adb_shell_command} getprop ro.build.version.release`.strip
end
reset_app(package) click to toggle source
# File lib/android-adb-extension/adb.rb, line 97
def reset_app(package)
  res = `#{adb_shell_command} pm clear #{package}`
  res.empty? ? nil : res
end
sdk() click to toggle source
# File lib/android-adb-extension/adb.rb, line 5
def sdk
  `#{adb_shell_command} getprop ro.build.version.sdk`.strip.to_i
end
send_to_background() click to toggle source
# File lib/android-adb-extension/adb.rb, line 87
def send_to_background
  res = `#{adb_shell_command} input keyevent 3`
  res.empty? ? nil : res
end
serial() click to toggle source
# File lib/android-adb-extension/adb.rb, line 17
def serial
  `#{adb_shell_command} getprop ro.serialno`.strip
end
set_landscape() click to toggle source
# File lib/android-adb-extension/adb.rb, line 48
def set_landscape
  change_accelerometer_control(0)
  res = change_device_orientation(1)
  change_accelerometer_control(1)
  res.empty? ? nil : res
end
set_portrait() click to toggle source
# File lib/android-adb-extension/adb.rb, line 41
def set_portrait
  change_accelerometer_control(0)
  res = change_device_orientation(0)
  change_accelerometer_control(1)
  res.empty? ? nil : res
end
start_intent(uri) click to toggle source
# File lib/android-adb-extension/adb.rb, line 112
def start_intent(uri)
  res = `#{adb_shell_command} am start -a android.intent.action.VIEW -d #{uri}`
  res.empty? ? nil : res
end
stop_app(package) click to toggle source
# File lib/android-adb-extension/adb.rb, line 102
def stop_app(package)
  res = `#{adb_shell_command} am force-stop #{package}`
  res.empty? ? nil : res
end
swipe(x1, y1, x2, y2, duration = nil) click to toggle source
# File lib/android-adb-extension/adb.rb, line 127
def swipe(x1, y1, x2, y2, duration = nil)
  args = duration.nil? ? "#{x1} #{y1} #{x2} #{y2}" : "#{x1} #{y1} #{x2} #{y2} #{duration}"
  res = `#{adb_shell_command} input swipe #{args}`
  res.empty? ? nil : res
end
take_screenshot(file_name) click to toggle source
# File lib/android-adb-extension/adb.rb, line 122
def take_screenshot(file_name)
  res = `#{adb_shell_command} screencap -p | perl -pe 's/\x0D\x0A/\x0A/g' > #{file_name}.png`
  res.empty? ? nil : res
end
uninstall_app(package) click to toggle source
# File lib/android-adb-extension/adb.rb, line 107
def uninstall_app(package)
  res = `#{adb_shell_command} pm uninstall #{package}`
  res.empty? ? nil : res
end
unlock() click to toggle source
# File lib/android-adb-extension/adb.rb, line 82
def unlock
  res = `#{adb_shell_command} input keyevent 82`
  res.empty? ? nil : res
end

Private Class Methods

adb_settings_system_command(name, value) click to toggle source
# File lib/android-adb-extension/adb.rb, line 149
def adb_settings_system_command(name, value)
  command = "#{adb_shell_command} content insert"
  param1 = '--uri content://settings/system'
  param2 = "--bind name:s:#{name}"
  param3 = "--bind value:i:#{value}"

  `#{command} #{param1} #{param2} #{param3}`
end
adb_shell_command() click to toggle source
# File lib/android-adb-extension/adb.rb, line 167
def adb_shell_command
  'adb shell'
end
change_accelerometer_control(mode) click to toggle source
# File lib/android-adb-extension/adb.rb, line 141
def change_accelerometer_control(mode)
  adb_settings_system_command('accelerometer_rotation', mode)
end
change_airplane_mode(mode) click to toggle source
# File lib/android-adb-extension/adb.rb, line 158
def change_airplane_mode(mode)
  command1 = "#{adb_shell_command} settings put global airplane_mode_on #{mode}"
  command2 = "#{adb_shell_command} am broadcast"
  param1 = '-a android.intent.action.AIRPLANE_MODE'
  param2 = "--ez state #{mode.to_boolean}"

  `#{command1} & #{command2} #{param1} #{param2}`
end
change_device_orientation(orientation) click to toggle source
# File lib/android-adb-extension/adb.rb, line 145
def change_device_orientation(orientation)
  adb_settings_system_command('user_rotation', orientation)
end