class Android::Command::Line::Tools::CLI

Public Instance Methods

clear() click to toggle source
# File lib/android/command/line/tools/cli.rb, line 32
def clear
  return (puts 'device not connected') unless device

  packages = list_installed_packages(device)
  package = select_installed_package(packages)
  execute_with_serial('adb', device, "shell pm clear #{package}")
end
install() click to toggle source
# File lib/android/command/line/tools/cli.rb, line 21
def install
  return (puts 'device not connected') unless device

  files = list_apk_files
  return (puts 'apk file not found') if files.empty?

  file = PecoSelector.select_from(files).first
  execute_with_serial('adb', device, "install -r #{file}")
end
screenrecord() click to toggle source
# File lib/android/command/line/tools/cli.rb, line 53
def screenrecord
  return (puts 'device not connected') unless device

  device_movie_path = "/sdcard/Download/movie_#{timestamp}.mp4"
  begin
    execute_with_serial('adb', device, "shell screenrecord #{device_movie_path}")
  rescue Interrupt
  end

  movie_path = Tempfile.new('movie_original').path + 'mp4'
  execute_with_serial('adb', device, "pull #{device_movie_path} #{movie_path}")

  resized_path = Tempfile.new('movie_resized').path + '.mp4'
  execute("ffmpeg -y -i #{movie_path} -vf scale=360:-1 -r 12 -f mp4 #{resized_path}")
  palette_path = Tempfile.new('movie_palette').path + '.png'
  execute("ffmpeg -i #{resized_path} -vf palettegen=max_colors=256 #{palette_path}")
  movie_path = "#{ENV['HOME']}/Desktop/movie_#{timestamp}.gif"
  execute("ffmpeg -i #{resized_path} -i #{palette_path} -f gif -filter_complex paletteuse #{movie_path}")

  execute("open #{movie_path}")
end
screenshot() click to toggle source
# File lib/android/command/line/tools/cli.rb, line 41
def screenshot
  return (puts 'device not connected') unless device

  outfile = "#{ENV['HOME']}/Desktop/screenshot_#{timestamp}.png"
  execute_with_serial('screenshot2', device, outfile)
  execute("open #{outfile}")
end
uninstall() click to toggle source
# File lib/android/command/line/tools/cli.rb, line 12
def uninstall
  return (puts 'device not connected') unless device

  packages = list_installed_packages(device)
  package = select_installed_package(packages)
  execute_with_serial('adb', device, "uninstall -r #{file}")
end

Private Instance Methods

device() click to toggle source
# File lib/android/command/line/tools/cli.rb, line 86
def device
  return @device if @device

  devices = DeviceAPI::Android.devices
  return devices.first if devices.size <= 1

  @device ||= PecoSelector.select_from(devices.map{|device|
    ["#{device.model} (#{device.serial})", device]
  }).first
end
execute(command) click to toggle source
# File lib/android/command/line/tools/cli.rb, line 81
def execute(command)
  puts "+ #{command}"
  system command
end
execute_with_serial(command, device, options) click to toggle source
# File lib/android/command/line/tools/cli.rb, line 76
def execute_with_serial(command, device, options)
  serial_option = device.nil? ? '' : "-s #{device.serial}"
  execute("#{command} #{serial_option} #{options}")
end
list_apk_files() click to toggle source
# File lib/android/command/line/tools/cli.rb, line 105
def list_apk_files
  Find.find(Dir.pwd).to_a.select{|f| f =~ /\.apk$/}
end
list_installed_packages(device) click to toggle source
# File lib/android/command/line/tools/cli.rb, line 97
def list_installed_packages(device)
  device.list_installed_packages.map{|p| p.gsub('package:', '')}
end
select_installed_package(packages) click to toggle source
# File lib/android/command/line/tools/cli.rb, line 101
def select_installed_package(packages)
  PecoSelector.select_from(packages).first
end
timestamp() click to toggle source
# File lib/android/command/line/tools/cli.rb, line 109
def timestamp
  Time.now.strftime('%Y%m%d%H%M%S')
end