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/apkanalyzer.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 Classes: Apkanalyzer

Constant Summary collapse

VERSION =
'0.4.1'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.device_serialObject

Returns the value of attribute device_serial.



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

def device_serial
  @device_serial
end

Class Method Details

.adb_serialString

Returns adb command with serial.

Returns:

  • (String)

    adb command with serial



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

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



177
178
179
# 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) ⇒ 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



27
28
29
30
31
32
# 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_activityString

Activity

Returns:

  • (String)

    message from adb command regarding current activities



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

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



105
106
107
108
109
# 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_screenString

Returns message from adb command.

Returns:

  • (String)

    message from adb command



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

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



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

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



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

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

.dump_all_cpuinfosString

resources

Returns:

  • (String)

    about cpuinfo via adb command



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

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



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

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)


61
62
63
64
65
66
67
# 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) ⇒ String

Returns message from adb command.

Parameters:

  • referrer (String)

    To broadcast

Returns:

  • (String)

    message from adb command



170
171
172
# 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 = "") ⇒ String

Returns message from adb command.

Parameters:

  • app (String)

    Application path

Returns:

  • (String)

    message from adb command

Raises:

  • (RuntimeError)


81
82
83
84
85
86
87
# 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) ⇒ String

Returns message from adb command.

Parameters:

  • app (String)

    Application path

Returns:

  • (String)

    message from adb command

Raises:

  • (RuntimeError)


71
72
73
74
75
76
77
# 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) ⇒ 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



113
114
115
116
117
118
# 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) ⇒ Array

Returns all package names.

Parameters:

  • package (String)

    A package name you would like to collect similar package

Returns:

  • (Array)

    all package names



122
123
124
125
# 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) ⇒ String

Returns message from adb command.

Parameters:

  • account_type (String)

    accountType of Android OS

Returns:

  • (String)

    message from adb command



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

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



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

def push
  "#{adb_serial} push"
end

.screen_on_or_offString

Returns message from adb command.

Returns:

  • (String)

    message from adb command



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

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



152
153
154
# 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) ⇒ String

Returns adb command with serial.

Parameters:

  • ip (String|Integer)

    Port number set to android device.

Returns:

  • (String)

    adb command with serial



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

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

.shellString

Returns adb shell command.

Returns:

  • (String)

    adb shell command



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

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



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

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



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

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



98
99
100
101
# 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) ⇒ String

Returns message from adb command.

Parameters:

  • text (String)

    Pin code to unlock

Returns:

  • (String)

    message from adb command



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

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



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

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