module DroidAdbs

Constants

VERSION

Attributes

device_serial[RW]

Public Class Methods

adb_serial() click to toggle source

@return [String] adb command with serial

# File lib/droid_adbs.rb, line 35
def adb_serial
  "#{adb} #{device_serial_option}"
end
broad_install_referrer(ref) click to toggle source

send referrer for TVs @param [String] ref to broadcast @return [String] message from adb command

# File lib/droid_adbs.rb, line 177
def broad_install_referrer(ref)
  `#{shell} am broadcast -a com.android.vending.INSTALL_REFERRER --include-stopped-packages --es referrer #{ref}`.strip
end
connect(host = 'localhost', port = 5555) click to toggle source

@param [String] host Host name to android device. e.g. 198.168.255.1 @param [String|Integer] port Port number set to android device with set_tcpip. e.g. 5555(default) @return [String] adb command with serial

# File lib/droid_adbs.rb, line 27
def connect(host = 'localhost', port = 5555)
  connect_to = %(#{host}:#{port})

  self.device_serial = connect_to
  `#{adb_serial} connect #{connect_to}`.strip
end
current_activity() click to toggle source
Activity

@return [String] message from adb command regarding current activities

# File lib/droid_adbs.rb, line 164
def current_activity
  `#{shell} dumpsys window windows | grep -E "mCurrentFocus|mFocusedApp"`.strip
end
delete_data(package) click to toggle source

@param [String] package A package name you would like to delete data in device local @return [String] message from adb command

# File lib/droid_adbs.rb, line 105
def delete_data(package)
  result = `#{shell} pm clear #{package}`.strip
  puts "failed to delete data:[original log] #{result}" unless result == "Success"
  result
end
device_screen() click to toggle source

@return [String] message from adb command

# File lib/droid_adbs.rb, line 200
def device_screen
  `#{shell} input keyevent KEYCODE_HOME`.strip
end
device_serials() click to toggle source

@return [Array] array of serial number to specify adb command

# File lib/droid_adbs.rb, line 55
def device_serials
  `#{adb} devices`.scan(/^.*\t/).map(&:strip)
end
devices() click to toggle source

@return [String] Get results of adb devices command

# File lib/droid_adbs.rb, line 50
def devices
  `#{adb} devices`.strip
end
dump_all_cpuinfos() click to toggle source
resources

@return [String] about cpuinfo via adb command

# File lib/droid_adbs.rb, line 158
def dump_all_cpuinfos
  `#{shell} dumpsys cpuinfo`.scan(/^.*$/).map(&:strip)
end
force_stop(package) click to toggle source

@param [String] package A package name you would like to stop @return [String] message from adb command

# File lib/droid_adbs.rb, line 145
def force_stop(package)
  `#{shell} am force-stop #{package}`.strip
end
install(app) click to toggle source

@param [String] app Application path @return [String] message from adb command

# File lib/droid_adbs.rb, line 61
def install(app)
  result = `#{adb_serial} install -r #{app}`.strip
  raise RuntimeError, result if result.include?("Error:")
  raise RuntimeError, "invalid APK" if result.include?("Invalid APK file:")
  raise RuntimeError, "failed to update apk because INSTALL_FAILED_VERSION_DOWNGRADE" if result.include?("INSTALL_FAILED_VERSION_DOWNGRADE")
  result
end
install_referrer_broadcast(referrer) click to toggle source

@param referrer [String] To broadcast @return [String] message from adb command

# File lib/droid_adbs.rb, line 170
def install_referrer_broadcast(referrer)
  `#{shell} broadcast window -a com.android.vending.INSTALL_REFERRER --include-stopped-packages --es referrer #{referrer}`.strip
end
install_with(app, option = "") click to toggle source

@param [String] app Application path @return [String] message from adb command

# File lib/droid_adbs.rb, line 81
def install_with(app, option = "")
  result = `#{adb_serial} install #{option} #{app}`.strip
  raise RuntimeError, result if result.include?("Error:")
  raise RuntimeError, "invalid APK" if result.include?("Invalid APK file:")
  raise RuntimeError, "failed to update apk because INSTALL_FAILED_VERSION_DOWNGRADE" if result.include?("INSTALL_FAILED_VERSION_DOWNGRADE")
  result
end
install_with_grant(app) click to toggle source

@param [String] app Application path @return [String] message from adb command

# File lib/droid_adbs.rb, line 71
def install_with_grant(app)
  result = `#{adb_serial} install -gr #{app}`.strip
  raise RuntimeError, result if result.include?("Error:")
  raise RuntimeError, "invalid APK" if result.include?("Invalid APK file:")
  raise RuntimeError, "failed to update apk because INSTALL_FAILED_VERSION_DOWNGRADE" if result.include?("INSTALL_FAILED_VERSION_DOWNGRADE")
  result
end
installed?(package) click to toggle source

@param [String] package A package name you would like to check installed or not @return [Bool] If the package installed, return true. Else return false

# File lib/droid_adbs.rb, line 113
def installed?(package)
  packages = `#{shell} pm list packages -e #{package}`.strip
  result = packages.each_line.find { |p| p == "package:#{package}" }
  return true if result
  false
end
installed_similar(package) click to toggle source

@param [String] package A package name you would like to collect similar package @return [Array] all package names

# File lib/droid_adbs.rb, line 122
def installed_similar(package)
  packages = `#{shell} pm list packages -e #{package}`.strip
  packages.each_line.map { |pack|  pack.strip.sub("package:", "") }
end
launch_login_activity(account_type) click to toggle source

@param [String] account_type accountType of Android OS @return [String] message from adb command

# File lib/droid_adbs.rb, line 135
def launch_login_activity(account_type)
  if ::DroidAdbs::Devices.device_build_version_sdk.to_i >= 21
    `#{shell} am start -a android.settings.ADD_ACCOUNT_SETTINGS --esa authorities #{account_type}`.strip
  else
    puts "Can't launch LoginActivity.java because version of sdk in the target device is lower than 21."
  end
end
push() click to toggle source

@return [String] adb push command

# File lib/droid_adbs.rb, line 45
def push
  "#{adb_serial} push"
end
screen_on_or_off() click to toggle source

@return [String] message from adb command

# File lib/droid_adbs.rb, line 195
def screen_on_or_off
  `#{shell} input keyevent 26`.strip
end
send_broadcast(broadcats_item, broadcast_extra = "") click to toggle source

@param [String] broadcats_item Target item for broadcast @param [String] broadcast_extra putExtra to send broadcast. @return [String] message from adb command

# File lib/droid_adbs.rb, line 152
def send_broadcast(broadcats_item, broadcast_extra = "")
  `#{shell} am broadcast -a #{broadcats_item} #{broadcast_extra}`.strip
end
set_tcpip(ip) click to toggle source

@param [String|Integer] ip Port number set to android device. @return [String] adb command with serial

# File lib/droid_adbs.rb, line 20
def set_tcpip(ip)
  `#{adb_serial} tcpip #{ip}`.strip
end
shell() click to toggle source

@return [String] adb shell command

# File lib/droid_adbs.rb, line 40
def shell
  "#{adb_serial} shell"
end
start(activity) click to toggle source

@param [String] activity An activity name you would like to launch @return [String] message from adb command

# File lib/droid_adbs.rb, line 129
def start(activity)
  `#{shell} am start -n #{activity}`.strip
end
uninstall(package) click to toggle source

@param [String] package A package name you would like to uninstall @return [String] message from adb command

# File lib/droid_adbs.rb, line 92
def uninstall(package)
  `#{adb_serial} uninstall #{package}`.strip
end
uninstall_similar(package) click to toggle source

@param [String] package A package name you would like to uninstall similar ones @return [Array] messages from adb command

# File lib/droid_adbs.rb, line 98
def uninstall_similar(package)
  installed_packages = installed_similar(package)
  installed_packages.map { |pack| `#{adb_serial} uninstall #{pack}`.strip }
end
unlock_with_pin(text) click to toggle source

@param [String] text Pin code to unlock @return [String] message from adb command

# File lib/droid_adbs.rb, line 190
def unlock_with_pin(text)
  `#{shell} input text #{text} && #{shell} input keyevent 66`.strip
end
unlock_without_pin() click to toggle source

Emulator should be unlocked after launch emulator with -wipe-data option Should unlock screen if current focus is like `mCurrentFocus=Window{2e85df18 u0 StatusBar}` . @return [String] message from adb command

# File lib/droid_adbs.rb, line 184
def unlock_without_pin
  `#{shell} input keyevent 82`.strip
end

Private Class Methods

adb() click to toggle source
# File lib/droid_adbs.rb, line 206
def adb
  raise "Please set ANDROID_HOME" unless ENV["ANDROID_HOME"]
  "#{ENV["ANDROID_HOME"]}/platform-tools/adb"
end
device_serial_option() click to toggle source
# File lib/droid_adbs.rb, line 211
def device_serial_option
  return "" unless device_serial && !device_serial.empty?
  "-s #{device_serial}"
end