Module: DroidAdbs

Defined in:
lib/droid_adbs.rb,
lib/droid_adbs/aapt.rb,
lib/droid_adbs/version.rb,
lib/droid_adbs/commons/wm.rb,
lib/droid_adbs/commons/ime.rb,
lib/droid_adbs/commons/doze.rb,
lib/droid_adbs/commons/grant.rb,
lib/droid_adbs/commons/backup.rb,
lib/droid_adbs/commons/logcat.rb,
lib/droid_adbs/commons/record.rb,
lib/droid_adbs/commons/devices.rb,
lib/droid_adbs/commons/dumpsys.rb,
lib/droid_adbs/commons/settings.rb

Defined Under Namespace

Modules: Aapt, BackupUtils, Devices, Doze, Dumpsys, Grant, IME, Logcat, ScreenRecord, Settings, WM

Constant Summary collapse

VERSION =
"0.3.1"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.device_serialObject

Returns the value of attribute device_serial.



15
16
17
# File 'lib/droid_adbs.rb', line 15

def device_serial
  @device_serial
end

Class Method Details

.adb_serialString

Returns adb command with serial.

Returns:

  • (String)

    adb command with serial



34
35
36
# File 'lib/droid_adbs.rb', line 34

def adb_serial
  "#{adb} #{device_serial_option}"
end

.broad_install_referrer(ref) ⇒ String

send referrer for TVs

Parameters:

  • ref (String)

    to broadcast

Returns:

  • (String)

    message from adb command



176
177
178
# File 'lib/droid_adbs.rb', line 176

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) ⇒ String

Returns adb command with serial.

Parameters:

  • host (String) (defaults to: 'localhost')

    Host name to android device. e.g. 198.168.255.1

  • port (String|Integer) (defaults to: 5555)

    Port number set to android device with set_tcpip. e.g. 5555(default)

Returns:

  • (String)

    adb command with serial



26
27
28
29
30
31
# File 'lib/droid_adbs.rb', line 26

def connect(host = 'localhost', port = 5555)
  connect_to = %(#{host}:#{port})

  self.device_serial = connect_to
  `#{adb_serial} connect #{connect_to}`.strip
end

.current_activityString

Activity

Returns:

  • (String)

    message from adb command regarding current activities



163
164
165
# File 'lib/droid_adbs.rb', line 163

def current_activity
  `#{shell} dumpsys window windows | grep -E "mCurrentFocus|mFocusedApp"`.strip
end

.delete_data(package) ⇒ String

Returns message from adb command.

Parameters:

  • package (String)

    A package name you would like to delete data in device local

Returns:

  • (String)

    message from adb command



104
105
106
107
108
# File 'lib/droid_adbs.rb', line 104

def delete_data(package)
  result = `#{shell} pm clear #{package}`.strip
  puts "failed to delete data:[original log] #{result}" unless result == "Success"
  result
end

.device_screenString

Returns message from adb command.

Returns:

  • (String)

    message from adb command



199
200
201
# File 'lib/droid_adbs.rb', line 199

def device_screen
  `#{shell} input keyevent KEYCODE_HOME`.strip
end

.device_serialsArray

Returns array of serial number to specify adb command.

Returns:

  • (Array)

    array of serial number to specify adb command



54
55
56
# File 'lib/droid_adbs.rb', line 54

def device_serials
  `#{adb} devices`.scan(/^.*\t/).map(&:strip)
end

.devicesString

Returns Get results of adb devices command.

Returns:

  • (String)

    Get results of adb devices command



49
50
51
# File 'lib/droid_adbs.rb', line 49

def devices
  `#{adb} devices`.strip
end

.dump_all_cpuinfosString

resources

Returns:

  • (String)

    about cpuinfo via adb command



157
158
159
# File 'lib/droid_adbs.rb', line 157

def dump_all_cpuinfos
  `#{shell} dumpsys cpuinfo`.scan(/^.*$/).map(&:strip)
end

.force_stop(package) ⇒ String

Returns message from adb command.

Parameters:

  • package (String)

    A package name you would like to stop

Returns:

  • (String)

    message from adb command



144
145
146
# File 'lib/droid_adbs.rb', line 144

def force_stop(package)
  `#{shell} am force-stop #{package}`.strip
end

.install(app) ⇒ String

Returns message from adb command.

Parameters:

  • app (String)

    Application path

Returns:

  • (String)

    message from adb command

Raises:

  • (RuntimeError)


60
61
62
63
64
65
66
# File 'lib/droid_adbs.rb', line 60

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) ⇒ String

Returns message from adb command.

Parameters:

  • referrer (String)

    To broadcast

Returns:

  • (String)

    message from adb command



169
170
171
# File 'lib/droid_adbs.rb', line 169

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 = "") ⇒ String

Returns message from adb command.

Parameters:

  • app (String)

    Application path

Returns:

  • (String)

    message from adb command

Raises:

  • (RuntimeError)


80
81
82
83
84
85
86
# File 'lib/droid_adbs.rb', line 80

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) ⇒ String

Returns message from adb command.

Parameters:

  • app (String)

    Application path

Returns:

  • (String)

    message from adb command

Raises:

  • (RuntimeError)


70
71
72
73
74
75
76
# File 'lib/droid_adbs.rb', line 70

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) ⇒ Bool

Returns If the package installed, return true. Else return false.

Parameters:

  • package (String)

    A package name you would like to check installed or not

Returns:

  • (Bool)

    If the package installed, return true. Else return false



112
113
114
115
116
117
# File 'lib/droid_adbs.rb', line 112

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) ⇒ Array

Returns all package names.

Parameters:

  • package (String)

    A package name you would like to collect similar package

Returns:

  • (Array)

    all package names



121
122
123
124
# File 'lib/droid_adbs.rb', line 121

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) ⇒ String

Returns message from adb command.

Parameters:

  • account_type (String)

    accountType of Android OS

Returns:

  • (String)

    message from adb command



134
135
136
137
138
139
140
# File 'lib/droid_adbs.rb', line 134

def ()
  if ::DroidAdbs::Devices.device_build_version_sdk.to_i >= 21
    `#{shell} am start -a android.settings.ADD_ACCOUNT_SETTINGS --esa authorities #{}`.strip
  else
    puts "Can't launch LoginActivity.java because version of sdk in the target device is lower than 21."
  end
end

.pushString

Returns adb push command.

Returns:

  • (String)

    adb push command



44
45
46
# File 'lib/droid_adbs.rb', line 44

def push
  "#{adb_serial} push"
end

.screen_on_or_offString

Returns message from adb command.

Returns:

  • (String)

    message from adb command



194
195
196
# File 'lib/droid_adbs.rb', line 194

def screen_on_or_off
  `#{shell} input keyevent 26`.strip
end

.send_broadcast(broadcats_item, broadcast_extra = "") ⇒ String

Returns message from adb command.

Parameters:

  • broadcats_item (String)

    Target item for broadcast

  • broadcast_extra (String) (defaults to: "")

    putExtra to send broadcast.

Returns:

  • (String)

    message from adb command



151
152
153
# File 'lib/droid_adbs.rb', line 151

def send_broadcast(broadcats_item, broadcast_extra = "")
  `#{shell} am broadcast -a #{broadcats_item} #{broadcast_extra}`.strip
end

.set_tcpip(ip) ⇒ String

Returns adb command with serial.

Parameters:

  • ip (String|Integer)

    Port number set to android device.

Returns:

  • (String)

    adb command with serial



19
20
21
# File 'lib/droid_adbs.rb', line 19

def set_tcpip(ip)
  `#{adb_serial} tcpip #{ip}`.strip
end

.shellString

Returns adb shell command.

Returns:

  • (String)

    adb shell command



39
40
41
# File 'lib/droid_adbs.rb', line 39

def shell
  "#{adb_serial} shell"
end

.start(activity) ⇒ String

Returns message from adb command.

Parameters:

  • activity (String)

    An activity name you would like to launch

Returns:

  • (String)

    message from adb command



128
129
130
# File 'lib/droid_adbs.rb', line 128

def start(activity)
  `#{shell} am start -n #{activity}`.strip
end

.uninstall(package) ⇒ String

Returns message from adb command.

Parameters:

  • package (String)

    A package name you would like to uninstall

Returns:

  • (String)

    message from adb command



91
92
93
# File 'lib/droid_adbs.rb', line 91

def uninstall(package)
  `#{adb_serial} uninstall #{package}`.strip
end

.uninstall_similar(package) ⇒ Array

Returns messages from adb command.

Parameters:

  • package (String)

    A package name you would like to uninstall similar ones

Returns:

  • (Array)

    messages from adb command



97
98
99
100
# File 'lib/droid_adbs.rb', line 97

def uninstall_similar(package)
  installed_packages = installed_similar(package)
  installed_packages.map { |pack| `#{adb_serial} uninstall #{pack}`.strip }
end

.unlock_with_pin(text) ⇒ String

Returns message from adb command.

Parameters:

  • text (String)

    Pin code to unlock

Returns:

  • (String)

    message from adb command



189
190
191
# File 'lib/droid_adbs.rb', line 189

def unlock_with_pin(text)
  `#{shell} input text #{text} && #{shell} input keyevent 66`.strip
end

.unlock_without_pinString

Emulator should be unlocked after launch emulator with -wipe-data option Should unlock screen if current focus is like ‘mCurrentFocus=Windowu0 StatusBar` .

Returns:

  • (String)

    message from adb command



183
184
185
# File 'lib/droid_adbs.rb', line 183

def unlock_without_pin
  `#{shell} input keyevent 82`.strip
end