Class: Apu::AndroidProject

Inherits:
Object
  • Object
show all
Defined in:
lib/apu/android_project.rb

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ AndroidProject

Returns a new instance of AndroidProject.



7
8
9
10
11
12
13
14
# File 'lib/apu/android_project.rb', line 7

def initialize(path)
  @base_path = "#{path}/"
  @settings_gradle_path = settings_gradle_file
  @modules = find_modules
  path, @execute_command = sample_project #supa hack
  @execute_line = @execute_command

end

Instance Method Details

#clear_app_dataObject



120
121
122
# File 'lib/apu/android_project.rb', line 120

def clear_app_data
  system(get_clear_app_command)
end

#execution_lineObject



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

def execution_line
  @execute_command
end

#find_modulesObject



51
52
53
54
55
56
57
# File 'lib/apu/android_project.rb', line 51

def find_modules
  return [] unless is_valid

  content = File.open(@settings_gradle_path, "rb").read
  modules = content.scan(/'([^']*)'/)
  modules.each {|replacement| replacement.first.gsub!(':', '/')}
end

#get_clear_app_commandObject



108
109
110
# File 'lib/apu/android_project.rb', line 108

def get_clear_app_command
  "adb shell pm clear #{@package}"
end

#get_execute_lineObject



85
86
87
# File 'lib/apu/android_project.rb', line 85

def get_execute_line
  @execute_line
end

#get_execution_line_command(path_to_sample) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/apu/android_project.rb', line 128

def get_execution_line_command(path_to_sample)
  path_to_manifest = File.join(path_to_sample, 'src/main/AndroidManifest.xml')

  if !File.exist?(path_to_manifest)
    return false
  end

  f = File.open(path_to_manifest)
  doc = Nokogiri::XML(f)

  @package = get_package(doc)
  @launcher_activity = get_launcher_activity(doc)

  if !@launcher_activity
    return false
  end

  f.close

  "adb shell am start -n \"#{get_launchable_activity}\" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER"
end

#get_launchable_activityObject



150
151
152
153
# File 'lib/apu/android_project.rb', line 150

def get_launchable_activity
  full_path_to_launcher = "#{@package}#{@launcher_activity.gsub(@package,'')}"
  "#{@package}/#{full_path_to_launcher}"
end

#get_launcher_activity(doc) ⇒ Object



159
160
161
162
163
164
165
166
167
168
# File 'lib/apu/android_project.rb', line 159

def get_launcher_activity(doc)
  activities = doc.css('activity')
  activities.each do |child|
    intent_filter = child.css('intent-filter')
    if intent_filter != nil and intent_filter.length != 0
      return child.attr('android:name')
    end
  end
  false
end

#get_package(doc) ⇒ Object



155
156
157
# File 'lib/apu/android_project.rb', line 155

def get_package(doc)
   doc.xpath("//manifest").attr('package').value
end

#get_package_nameObject



116
117
118
# File 'lib/apu/android_project.rb', line 116

def get_package_name
  @package
end

#get_uninstall_commandObject



112
113
114
# File 'lib/apu/android_project.rb', line 112

def get_uninstall_command
  "adb uninstall #{@package}"
end

#installObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/apu/android_project.rb', line 59

def install
  Dir.chdir @base_path

  path, @execute_line = sample_project

  if path == false and execute_line == false
    puts "Couldn't open the sample project, sorry!".red
    exit 1
  end

  builder = "gradle"

  if File.exist?('gradlew')
    system('chmod +x gradlew')

    builder = 'sh gradlew'
  end

  # Generate the gradle/ folder
  system('gradle wrap') if File.exist?('gradlew') and !is_gradle_wrapped

  puts "Installing #{@package.green}...\n"
  system("#{builder} clean installDebug")

end

#is_gradle_wrappedObject



89
90
91
92
93
# File 'lib/apu/android_project.rb', line 89

def is_gradle_wrapped
  return false if !File.directory?('gradle/')

  File.exist?('gradle/wrapper/gradle-wrapper.properties') and File.exist?('gradle/wrapper/gradle-wrapper.jar')
end

#is_valid(settings_path = @settings_gradle_path) ⇒ Object



47
48
49
# File 'lib/apu/android_project.rb', line 47

def is_valid(settings_path = @settings_gradle_path)
  File.exist?(settings_path)
end

#sample_projectObject



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/apu/android_project.rb', line 95

def sample_project

    @modules.each do |child|
      full_path = File.join(@base_path, child.first)
      @path_to_sample = full_path

      execution_line_command = get_execution_line_command(full_path)
      return full_path, execution_line_command if execution_line_command

  end
  [false, false]
end

#settings_gradle_file(path = @base_path) ⇒ Object

def remove_local_properties

Dir.chdir @base_path
file_name = 'local.properties'

File.delete(file_name) if File.exist?(file_name)

system("touch #{file_name}")

end

def remove_application_id

# Open temporary file
tmp = Tempfile.new("extract")

file = "#{@path_to_sample}/build.gradle"

# Write good lines to temporary file
open(file, 'r').each { |l| tmp << l unless l.include? 'applicationId' }
tmp.close

# Move temp file to origin
FileUtils.mv(tmp.path, file)

end



43
44
45
# File 'lib/apu/android_project.rb', line 43

def settings_gradle_file(path = @base_path)
  File.join(path, 'settings.gradle')
end

#uninstall_applicationObject



124
125
126
# File 'lib/apu/android_project.rb', line 124

def uninstall_application
  system(get_uninstall_command) # > /dev/null 2>&1")
end