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
# 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

end

Instance Method Details

#clear_app_dataObject



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

def clear_app_data
  system(get_clear_app_command)
end

#execution_lineObject



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

def execution_line
  @execute_command
end

#find_modulesObject



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

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



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

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

#get_execute_lineObject



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

def get_execute_line
  @execute_line
end

#get_execution_line_command(path_to_sample) ⇒ Object



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

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



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

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

#get_launcher_activity(doc) ⇒ Object



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

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



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

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

#get_package_nameObject



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

def get_package_name
  @package
end

#get_uninstall_commandObject



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

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

#installObject



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

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



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

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



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

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

#sample_projectObject



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

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



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

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

#uninstall_applicationObject



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

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