Class: Flick::Android

Inherits:
Object
  • Object
show all
Defined in:
lib/flick/android.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Android

Returns a new instance of Android.



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/flick/android.rb', line 5

def initialize options
  Flick::Checker.system_dependency "adb"
  self.flick_dir = "#{Dir.home}/.flick"
  self.dir_name = "sdcard/flick"
  self.udid = options.fetch(:udid, get_device_udid(options))
  self.name = options.fetch(:name, self.udid)
  self.outdir = options.fetch(:outdir, Dir.pwd)
  self.unique = options.fetch(:unique, true).to_b
  self.limit = options.fetch(:limit, 180)
  self.specs = options.fetch(:specs, false)
  create_flick_dirs
end

Instance Attribute Details

#dir_nameObject

Returns the value of attribute dir_name.



3
4
5
# File 'lib/flick/android.rb', line 3

def dir_name
  @dir_name
end

#flick_dirObject

Returns the value of attribute flick_dir.



3
4
5
# File 'lib/flick/android.rb', line 3

def flick_dir
  @flick_dir
end

#limitObject

Returns the value of attribute limit.



3
4
5
# File 'lib/flick/android.rb', line 3

def limit
  @limit
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/flick/android.rb', line 3

def name
  @name
end

#outdirObject

Returns the value of attribute outdir.



3
4
5
# File 'lib/flick/android.rb', line 3

def outdir
  @outdir
end

#specsObject

Returns the value of attribute specs.



3
4
5
# File 'lib/flick/android.rb', line 3

def specs
  @specs
end

#udidObject

Returns the value of attribute udid.



3
4
5
# File 'lib/flick/android.rb', line 3

def udid
  @udid
end

#uniqueObject

Returns the value of attribute unique.



3
4
5
# File 'lib/flick/android.rb', line 3

def unique
  @unique
end

Instance Method Details

#check_for_devicesObject



36
37
38
39
40
41
# File 'lib/flick/android.rb', line 36

def check_for_devices
  unless devices_connected?
    puts "\nNo Devices Connected or Authorized!!!\nMake sure at least one device (emulator/simulator) is started!\n".red
    abort
  end
end

#clear_filesObject



23
24
25
26
# File 'lib/flick/android.rb', line 23

def clear_files
  %x(adb -s #{udid} shell rm '#{dir_name}/*' >> /dev/null 2>&1)
  Flick::System.clean_system_dir flick_dir, udid
end

#create_flick_dirsObject



18
19
20
21
# File 'lib/flick/android.rb', line 18

def create_flick_dirs
  Flick::System.setup_system_dir flick_dir
  %x(adb -s #{udid} shell 'mkdir #{dir_name}' >> /dev/null 2>&1)
end

#devicesObject



28
29
30
# File 'lib/flick/android.rb', line 28

def devices
  (`adb devices`).scan(/\n(.*)\t/).flatten
end

#devices_connected?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/flick/android.rb', line 32

def devices_connected?
  !devices.empty?
end

#get_device_udid(opts_hash) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/flick/android.rb', line 43

def get_device_udid opts_hash
  devices_connected?
  return unless opts_hash[:udid].nil?
  if devices.size == 1
    devices[0]
  else
    puts "\nMultiple android devices '#{devices}' found.\nSpecify a single UDID. e.g. -u #{devices.sample}\n".red
    abort unless specs
  end
end

#infoObject



54
55
56
57
58
59
60
61
62
# File 'lib/flick/android.rb', line 54

def info
  specs = { os: "ro.build.version.release", manufacturer: "ro.product.manufacturer", model: "ro.product.model", sdk: "ro.build.version.sdk" }
  hash = { udid: udid }
  specs.each do |key, spec|
    value = `adb -s #{udid} shell getprop "#{spec}"`.strip
    hash.merge!({key=> "#{value}"})
  end
  hash
end

#log(name) ⇒ Object



72
73
74
# File 'lib/flick/android.rb', line 72

def log name
  %x(adb -s #{udid} logcat -v long > #{outdir}/#{name}.log)
end

#os_versionObject



64
65
66
# File 'lib/flick/android.rb', line 64

def os_version
  `adb -s #{udid} shell getprop "ro.build.version.release"`.strip.to_f
end

#pull_file(file, dir) ⇒ Object



84
85
86
# File 'lib/flick/android.rb', line 84

def pull_file file, dir
  %x(adb -s #{udid} pull #{file} #{dir} >> /dev/null 2>&1)
end

#pull_filesObject



100
101
102
103
104
105
106
107
108
# File 'lib/flick/android.rb', line 100

def pull_files
  if unique
    files = unique_files
  else
    files = (`adb -s #{udid} shell "ls #{dir_name}/#{udid}*"`).split("\r\n")
  end
  return if files.empty?
  Parallel.map(files, in_threads: 10) { |file| pull_file file, flick_dir }
end

#recordable?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/flick/android.rb', line 76

def recordable?
  (`adb -s #{udid} shell 'ls /system/bin/screenrecord'`).strip == "/system/bin/screenrecord"
end

#screenrecord(name) ⇒ Object



80
81
82
# File 'lib/flick/android.rb', line 80

def screenrecord name
  %x(adb -s #{udid} shell screenrecord --time-limit #{limit} --size 720x1280 #{dir_name}/#{name}.mp4)
end

#screenshot(name) ⇒ Object



68
69
70
# File 'lib/flick/android.rb', line 68

def screenshot name
  %x(adb -s #{udid} shell screencap #{dir_name}/#{name}.png)
end

#screenshots_exist?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/flick/android.rb', line 110

def screenshots_exist?
  (`adb -s #{udid} shell "ls #{dir_name}/#{udid}-*.png | wc -l"`).to_i > 0
end

#unique_filesObject



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/flick/android.rb', line 88

def unique_files
  if os_version < 6.0
    command = "md5"
  else
    command = "md5sum"
  end
  files = `adb -s #{udid} shell "#{command} #{dir_name}/#{udid}*"`
  hash = files.split("\r\n").map { |file| { md5: file.match(/(.*) /)[1].strip, file: file.match(/ (.*)/)[1].strip } }
  hash.uniq! { |e| e[:md5] }
  hash.map { |file| file[:file] }
end