class Mobile::Emulator::Screencapture::Android

Constants

DEVICE_SCREENRECORD_PATH
DEVICE_SCREENSHOT_PATH

Attributes

bit_rate[R]
height[R]
time_limit[R]
width[R]

Public Class Methods

new(**args) click to toggle source
Calls superclass method
# File lib/mobile/emulator/screencapture/android.rb, line 11
def initialize(**args)
  super(args)
  @width = args[:width]
  @height = args[:height]
  @bit_rate = args[:bit_rate]
  @time_limit = args[:time_limit]

  @screenrecord_path = nil
end

Public Instance Methods

screenshot(image_name) click to toggle source
# File lib/mobile/emulator/screencapture/android.rb, line 21
def screenshot(image_name)
  raise "Argument image_name is nil" if image_name.nil?

  screenshot_path = File.join(@screenshot_dir, "#{image_name}.png")
  _native_screenshot
  _pull_screenshot(screenshot_path)

  screenshot_path
end
start_screenrecord(video_name) click to toggle source
# File lib/mobile/emulator/screencapture/android.rb, line 31
def start_screenrecord(video_name)
  raise "Argument video_name is nil" if video_name.nil?
  raise "Screenrecord process already started. pid: #{@screenrecord_pid}" if @screenrecord_pid

  @screenrecord_path = File.join(@screenrecord_dir, "#{video_name}.mp4")
  @screenrecord_pid = _native_start_screenrecord

  @screenrecord_path
end
stop_screenrecord() click to toggle source
# File lib/mobile/emulator/screencapture/android.rb, line 41
def stop_screenrecord
  raise 'Any screenrecord process did not started' unless @screenrecord_pid
  screenrecord_path = @screenrecord_path

  _native_stop_screenrecord
  _pull_screenrecord(screenrecord_path)

  @screenrecord_pid = nil
  @screenrecord_path = nil

  screenrecord_path
end

Private Instance Methods

_adb(command) click to toggle source
# File lib/mobile/emulator/screencapture/android.rb, line 56
def _adb(command)
  `adb #{command}`
end
_adb_spawn(command) click to toggle source
# File lib/mobile/emulator/screencapture/android.rb, line 60
def _adb_spawn(command)
  pid = spawn("adb #{command}", out: '/dev/null')
  Process.detach(pid)
  pid
end
_native_screenshot() click to toggle source
# File lib/mobile/emulator/screencapture/android.rb, line 66
def _native_screenshot
  _adb("shell screencap #{DEVICE_SCREENSHOT_PATH}")
end
_native_start_screenrecord() click to toggle source
# File lib/mobile/emulator/screencapture/android.rb, line 75
def _native_start_screenrecord
  command = ["shell screenrecord #{DEVICE_SCREENRECORD_PATH}"]
  command << "--size #{@width}x#{@height}" if @width && @height
  command << "--bit-rate #{@bit_rate}" if @bit_rate
  command << "--time-limit #{@time_limit}" if @time_limit
  _adb_spawn(command.join(' '))
end
_native_stop_screenrecord() click to toggle source
# File lib/mobile/emulator/screencapture/android.rb, line 83
def _native_stop_screenrecord
  killed_process_num = Process.kill('SIGINT', @screenrecord_pid)
  raise "Kill pid: #{@screenrecord_pid} did not end correctly." unless killed_process_num.positive?

  # If 'adb pull' after kill recording process soon, the video will be broken.
  sleep 2
end
_pull_screenrecord(screenrecord_path) click to toggle source
# File lib/mobile/emulator/screencapture/android.rb, line 91
def _pull_screenrecord(screenrecord_path)
  _adb("pull #{DEVICE_SCREENRECORD_PATH} #{screenrecord_path}")
  _adb("shell rm #{DEVICE_SCREENRECORD_PATH}")
end
_pull_screenshot(screenshot_path) click to toggle source
# File lib/mobile/emulator/screencapture/android.rb, line 70
def _pull_screenshot(screenshot_path)
  _adb("pull #{DEVICE_SCREENSHOT_PATH} #{screenshot_path}")
  _adb("shell rm #{DEVICE_SCREENSHOT_PATH}")
end