From 1f9571a3da75fbe83d4e6d4a2e40e69526f73373 Mon Sep 17 00:00:00 2001 From: Brock Whitten Date: Fri, 5 Feb 2010 16:08:24 -0800 Subject: [PATCH 1/7] Changing the simple example --- assets/www/index.html | 147 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 assets/www/index.html diff --git a/assets/www/index.html b/assets/www/index.html new file mode 100644 index 00000000..b53776da --- /dev/null +++ b/assets/www/index.html @@ -0,0 +1,147 @@ + + + + + + PhoneGap + + + + + +

Welcome to PhoneGap!

+

this file is located at assets/index.html

+
+

Platform:  

+

Version:  

+

UUID:  

+
+
+
X:
 
+
Y:
 
+
Z:
 
+
+ Watch Accelerometer + Get Location + Call 411 + Beep + Vibrate + Get a Picture + Get phone's contacts + + + From c638fbfa69eb1ac2aa68805ba860c933c31072fc Mon Sep 17 00:00:00 2001 From: Brock Whitten Date: Tue, 16 Feb 2010 10:28:07 -0800 Subject: [PATCH 2/7] Fixing nulls --- framework/src/com/phonegap/GeoListener.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/framework/src/com/phonegap/GeoListener.java b/framework/src/com/phonegap/GeoListener.java index 45267b6b..c9c8f3b0 100644 --- a/framework/src/com/phonegap/GeoListener.java +++ b/framework/src/com/phonegap/GeoListener.java @@ -66,8 +66,10 @@ public class GeoListener { // This stops the listener void stop() { - mGps.stop(); - mNetwork.stop(); + if(mGps != null) + mGps.stop(); + if(mNetwork != null) + mNetwork.stop(); } public Location getCurrentLocation() { From 3d91a98b3a55360aeac1a3d2428ccaa207adec7c Mon Sep 17 00:00:00 2001 From: Brock Whitten Date: Tue, 16 Feb 2010 12:11:43 -0800 Subject: [PATCH 3/7] Fixing up StandAlone for debugging purposes --- framework/src/com/phonegap/StandAlone.java | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/framework/src/com/phonegap/StandAlone.java b/framework/src/com/phonegap/StandAlone.java index 408d2f22..933bd9fa 100644 --- a/framework/src/com/phonegap/StandAlone.java +++ b/framework/src/com/phonegap/StandAlone.java @@ -11,22 +11,7 @@ public class StandAlone extends DroidGap { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - /* Load a URI from the strings.xml file */ - Class c = R.string.class; - Field f; - String uri; - - int i = 0; - - try { - f = c.getField("url"); - i = f.getInt(f); - uri = this.getResources().getString(i); - } catch (Exception e) - { - uri = "http://www.phonegap.com"; - } - super.loadUrl(uri); + super.loadUrl("file:///android_asset/www/index.html"); } } From 78204511084a8695dd65f96e05e2aa41448216ad Mon Sep 17 00:00:00 2001 From: Immad Naseer Date: Tue, 16 Feb 2010 14:34:50 -0800 Subject: [PATCH 4/7] small string concatenation performance optimization --- droidgap | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/droidgap b/droidgap index 86a7469c..c5edbfd7 100755 --- a/droidgap +++ b/droidgap @@ -78,8 +78,8 @@ class Build phonegapjs = IO.read('phonegap.js.base'); js.each do |script| next if script[0].chr == "." or script == "phonegap.js.base" - phonegapjs += IO.read(script) - phonegapjs += "\n\n" + phonegapjs << IO.read(script) + phonegapjs << "\n\n" end Dir.chdir("#{ @dir}") File.open("#{ @path }#{ @s }assets#{ @s }www#{ @s }phonegap.js", 'w') {|f| f.write(phonegapjs) } From 10d455d560120eab0d2e5c85985c6eaf1a8a22b8 Mon Sep 17 00:00:00 2001 From: Immad Naseer Date: Tue, 16 Feb 2010 14:59:22 -0800 Subject: [PATCH 5/7] Changing the tabs in droidgap to spaces --- droidgap | 72 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 36 insertions(+), 36 deletions(-) diff --git a/droidgap b/droidgap index c5edbfd7..914493d6 100755 --- a/droidgap +++ b/droidgap @@ -6,8 +6,8 @@ class Build def initialize(*a) @android_sdk_path, @name, @pkg, @www, @path = a - @s = File::SEPARATOR - @dir = Dir.pwd + @s + @s = File::SEPARATOR + @dir = Dir.pwd + @s end # runs the build script @@ -18,44 +18,44 @@ class Build copy_libs add_name_to_strings write_java - puts "Complete!" + puts "Complete!" end # removes local.properties and recreates based on android_sdk_path # then generates framework/phonegap.jar def build_jar - puts "Building the JAR..." - FileUtils.rm "#{ @dir }framework#{@s}local.properties" if File.exists? "#{ @dir }framework#{@s}local.properties" + puts "Building the JAR..." + FileUtils.rm "#{ @dir }framework#{@s}local.properties" if File.exists? "#{ @dir }framework#{@s}local.properties" FileUtils.rm "#{ @dir }framework#{@s}phonegap.js" if File.exists? "#{ @dir }framework#{@s}phonegap.js" - FileUtils.rm "#{ @dir }framework#{@s}phonegap.jar" if File.exists? "#{ @dir }framework#{@s}phonegap.jar" + FileUtils.rm "#{ @dir }framework#{@s}phonegap.jar" if File.exists? "#{ @dir }framework#{@s}phonegap.jar" open("#{ @dir }framework#{@s}local.properties", 'w') do |f| f.puts "sdk.dir=#{ @android_sdk_path }" end - Dir.chdir(@dir + "framework") + Dir.chdir(@dir + "framework") `ant jar` - Dir.chdir(@dir) + Dir.chdir(@dir) end # runs android create project # TODO need to allow more flexible SDK targetting # TODO validate Android SDK def create_android - puts "Creating Android project..." + puts "Creating Android project..." `android create project -t 5 -k #{ @pkg } -a #{ @name } -n #{ @name } -p #{ @path }` - FileUtils.mkdir_p "#{ @path }#{@s}assets#{@s}www" + FileUtils.mkdir_p "#{ @path }#{@s}assets#{@s}www" FileUtils.cp_r "#{ @www }#{ @s }.", "#{ @path }#{ @s }assets#{ @s }www#{ @s }" end # creates an AndroidManifest.xml for the project def generate_manifest - puts "Generating manifest..." + puts "Generating manifest..." manifest = "" open(@dir + 'framework/AndroidManifest.xml', 'r') do |old| - manifest = old.read - manifest.gsub! 'android:versionCode="5"', 'android:versionCode="1"' - manifest.gsub! 'package="com.phonegap"', "package=\"#{ @pkg }\"" - manifest.gsub! 'android:name=".StandAlone"', "android:name=\".#{ @name }\"" - manifest.gsub! 'android:minSdkVersion="5"', 'android:minSdkVersion="3"' + manifest = old.read + manifest.gsub! 'android:versionCode="5"', 'android:versionCode="1"' + manifest.gsub! 'package="com.phonegap"', "package=\"#{ @pkg }\"" + manifest.gsub! 'android:name=".StandAlone"', "android:name=\".#{ @name }\"" + manifest.gsub! 'android:minSdkVersion="5"', 'android:minSdkVersion="3"' end open("#{ @path }#{@s}AndroidManifest.xml", 'w') { |x| x.puts manifest } end @@ -63,31 +63,31 @@ class Build # copies stuff from framework into the project # TODO need to allow for www import inc icon def copy_libs - puts "Copying over libraries and assets and creating phonegap.js..." - FileUtils.cp "#{ @dir }framework#{@s}phonegap.jar", "#{ @path }#{@s}libs" - FileUtils.cp "#{ @dir }framework#{@s}res#{@s}values#{@s}strings.xml", "#{ @path }#{@s}res#{@s}values#{@s}strings.xml" - FileUtils.cp "#{ @dir }framework#{@s}res#{@s}layout#{@s}main.xml", "#{ @path }#{@s}res#{@s}layout#{@s}main.xml" - FileUtils.cp "#{ @dir }framework#{@s}res#{@s}layout#{@s}preview.xml", "#{ @path }#{@s}res#{@s}layout#{@s}preview.xml" + puts "Copying over libraries and assets and creating phonegap.js..." + FileUtils.cp "#{ @dir }framework#{@s}phonegap.jar", "#{ @path }#{@s}libs" + FileUtils.cp "#{ @dir }framework#{@s}res#{@s}values#{@s}strings.xml", "#{ @path }#{@s}res#{@s}values#{@s}strings.xml" + FileUtils.cp "#{ @dir }framework#{@s}res#{@s}layout#{@s}main.xml", "#{ @path }#{@s}res#{@s}layout#{@s}main.xml" + FileUtils.cp "#{ @dir }framework#{@s}res#{@s}layout#{@s}preview.xml", "#{ @path }#{@s}res#{@s}layout#{@s}preview.xml" %w(drawable-hdpi drawable-ldpi drawable-mdpi).each do |e| - FileUtils.cp "#{ @dir }framework#{@s}res#{@s}drawable#{@s}icon.png", "#{ @path }#{@s}res#{@s}#{ e }#{@s}icon.png" + FileUtils.cp "#{ @dir }framework#{@s}res#{@s}drawable#{@s}icon.png", "#{ @path }#{@s}res#{@s}#{ e }#{@s}icon.png" end - # concat JS and put into www folder. - Dir.chdir("#{ @dir }framework#{ @s }assets#{ @s }js") - basedir = "." - js = Dir.new(basedir).entries - phonegapjs = IO.read('phonegap.js.base'); - js.each do |script| - next if script[0].chr == "." or script == "phonegap.js.base" - phonegapjs << IO.read(script) - phonegapjs << "\n\n" - end - Dir.chdir("#{ @dir}") - File.open("#{ @path }#{ @s }assets#{ @s }www#{ @s }phonegap.js", 'w') {|f| f.write(phonegapjs) } + # concat JS and put into www folder. + Dir.chdir("#{ @dir }framework#{ @s }assets#{ @s }js") + basedir = "." + js = Dir.new(basedir).entries + phonegapjs = IO.read('phonegap.js.base'); + js.each do |script| + next if script[0].chr == "." or script == "phonegap.js.base" + phonegapjs << IO.read(script) + phonegapjs << "\n\n" + end + Dir.chdir("#{ @dir}") + File.open("#{ @path }#{ @s }assets#{ @s }www#{ @s }phonegap.js", 'w') {|f| f.write(phonegapjs) } end # puts app name in strings def add_name_to_strings - puts "Adding some application name to strings.xml..." + puts "Adding some application name to strings.xml..." x = " #{ @name } @@ -102,7 +102,7 @@ class Build # this is so fucking unholy yet oddly beautiful # not sure if I should thank Ruby or apologize for this abusive use of string interpolation def write_java - puts "Writing application Java code..." + puts "Writing application Java code..." j = " package #{ @pkg }; From 37c3c2528dd07131f0d89fabfabb7f25f25aafc8 Mon Sep 17 00:00:00 2001 From: Immad Naseer Date: Tue, 16 Feb 2010 15:22:42 -0800 Subject: [PATCH 6/7] Refactored the code dealing w/ file/directory manipulation. - The code makes sure the target directory structure exists before copying files. - Minimized usage of Dir.chdir - Cleaned up the code dealing w/ cross-platform file paths. --- droidgap | 86 ++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 53 insertions(+), 33 deletions(-) diff --git a/droidgap b/droidgap index 914493d6..f599c575 100755 --- a/droidgap +++ b/droidgap @@ -2,12 +2,12 @@ require 'fileutils' class Build - attr_reader :android_sdk_path, :name, :pkg, :www, :path, :dir + attr_reader :android_sdk_path, :name, :pkg, :www, :path def initialize(*a) @android_sdk_path, @name, @pkg, @www, @path = a - @s = File::SEPARATOR - @dir = Dir.pwd + @s + @android_dir = File.expand_path(File.dirname(__FILE__)) + @framework_dir = File.join(@android_dir, "framework") end # runs the build script @@ -25,15 +25,18 @@ class Build # then generates framework/phonegap.jar def build_jar puts "Building the JAR..." - FileUtils.rm "#{ @dir }framework#{@s}local.properties" if File.exists? "#{ @dir }framework#{@s}local.properties" - FileUtils.rm "#{ @dir }framework#{@s}phonegap.js" if File.exists? "#{ @dir }framework#{@s}phonegap.js" - FileUtils.rm "#{ @dir }framework#{@s}phonegap.jar" if File.exists? "#{ @dir }framework#{@s}phonegap.jar" - open("#{ @dir }framework#{@s}local.properties", 'w') do |f| + + %w(local.properties phonegap.js phonegap.jar).each do |f| + FileUtils.rm File.join(@framework_dir, f) if File.exists? File.join(@framework_dir, f) + end + + open(File.join(@framework_dir, "local.properties"), 'w') do |f| f.puts "sdk.dir=#{ @android_sdk_path }" - end - Dir.chdir(@dir + "framework") + end + + Dir.chdir(@framework_dir) `ant jar` - Dir.chdir(@dir) + Dir.chdir(@android_dir) end # runs android create project @@ -41,48 +44,64 @@ class Build # TODO validate Android SDK def create_android puts "Creating Android project..." + `android create project -t 5 -k #{ @pkg } -a #{ @name } -n #{ @name } -p #{ @path }` - FileUtils.mkdir_p "#{ @path }#{@s}assets#{@s}www" - FileUtils.cp_r "#{ @www }#{ @s }.", "#{ @path }#{ @s }assets#{ @s }www#{ @s }" + + FileUtils.mkdir_p File.join(@path, "assets", "www") + FileUtils.cp_r File.join(@www, "."), File.join(@path, "assets", "www") end # creates an AndroidManifest.xml for the project def generate_manifest puts "Generating manifest..." manifest = "" - open(@dir + 'framework/AndroidManifest.xml', 'r') do |old| + open(File.join(@framework_dir, "AndroidManifest.xml"), 'r') do |old| manifest = old.read manifest.gsub! 'android:versionCode="5"', 'android:versionCode="1"' manifest.gsub! 'package="com.phonegap"', "package=\"#{ @pkg }\"" manifest.gsub! 'android:name=".StandAlone"', "android:name=\".#{ @name }\"" manifest.gsub! 'android:minSdkVersion="5"', 'android:minSdkVersion="3"' end - open("#{ @path }#{@s}AndroidManifest.xml", 'w') { |x| x.puts manifest } + open(File.join(@path, "AndroidManifest.xml"), 'w') { |x| x.puts manifest } end # copies stuff from framework into the project # TODO need to allow for www import inc icon def copy_libs puts "Copying over libraries and assets and creating phonegap.js..." - FileUtils.cp "#{ @dir }framework#{@s}phonegap.jar", "#{ @path }#{@s}libs" - FileUtils.cp "#{ @dir }framework#{@s}res#{@s}values#{@s}strings.xml", "#{ @path }#{@s}res#{@s}values#{@s}strings.xml" - FileUtils.cp "#{ @dir }framework#{@s}res#{@s}layout#{@s}main.xml", "#{ @path }#{@s}res#{@s}layout#{@s}main.xml" - FileUtils.cp "#{ @dir }framework#{@s}res#{@s}layout#{@s}preview.xml", "#{ @path }#{@s}res#{@s}layout#{@s}preview.xml" - %w(drawable-hdpi drawable-ldpi drawable-mdpi).each do |e| - FileUtils.cp "#{ @dir }framework#{@s}res#{@s}drawable#{@s}icon.png", "#{ @path }#{@s}res#{@s}#{ e }#{@s}icon.png" + + framework_res_dir = File.join(@framework_dir, "res") + app_res_dir = File.join(@path, "res") + + FileUtils.mkdir_p File.join(@path, "libs") + FileUtils.cp File.join(@framework_dir, "phonegap.jar"), File.join(@path, "libs") + + FileUtils.mkdir_p File.join(app_res_dir, "values") + FileUtils.cp File.join(framework_res_dir, "values","strings.xml"), File.join(app_res_dir, "values", "strings.xml") + + FileUtils.mkdir_p File.join(app_res_dir, "layout") + %w(main.xml preview.xml).each do |f| + FileUtils.cp File.join(framework_res_dir, "layout", f), File.join(app_res_dir, "layout", f) end + + %w(drawable-hdpi drawable-ldpi drawable-mdpi).each do |e| + FileUtils.mkdir_p File.join(app_res_dir, e) + FileUtils.cp File.join(framework_res_dir, "drawable", "icon.png"), File.join(app_res_dir, e, "icon.png") + end + # concat JS and put into www folder. - Dir.chdir("#{ @dir }framework#{ @s }assets#{ @s }js") - basedir = "." - js = Dir.new(basedir).entries - phonegapjs = IO.read('phonegap.js.base'); - js.each do |script| + js_dir = File.join(@framework_dir, "assets", "js") + + phonegapjs = IO.read(File.join(js_dir, 'phonegap.js.base')) + + Dir.new(js_dir).entries.each do |script| next if script[0].chr == "." or script == "phonegap.js.base" - phonegapjs << IO.read(script) + + phonegapjs << IO.read(File.join(js_dir, script)) phonegapjs << "\n\n" end - Dir.chdir("#{ @dir}") - File.open("#{ @path }#{ @s }assets#{ @s }www#{ @s }phonegap.js", 'w') {|f| f.write(phonegapjs) } + + File.open(File.join(@path, "assets", "www", "phonegap.js"), 'w') {|f| f.write(phonegapjs) } end # puts app name in strings @@ -94,7 +113,7 @@ class Build Snap " - open("#{ @path }#{@s}res#{@s}values#{@s}strings.xml", 'w') do |f| + open(File.join(@path, "res", "values", "strings.xml"), 'w') do |f| f.puts x.gsub(' ','') end end @@ -120,10 +139,11 @@ class Build } } " - dir = "#{ @path }#{@s}src#{@s}#{ @pkg.gsub '.', '/' }"; - cls = "#{ @name }.java" - pth = File.join(dir,cls) - open(pth,'w') { |f| f.puts j.gsub(' ','') } + + code_dir = File.join(@path, "src", @pkg.gsub('.', File::SEPARATOR)) + + FileUtils.mkdir_p(code_dir) + open(File.join(code_dir, "#{@name}.java"),'w') { |f| f.puts j.gsub(' ','') } end # end From 594e77690c3ea072feb173168127c8c4fe83aeec Mon Sep 17 00:00:00 2001 From: Immad Naseer Date: Wed, 17 Feb 2010 15:31:40 -0800 Subject: [PATCH 7/7] droidgap parses the output of `android list targets` to find the id for 'android-5' or defaults to 5 if it can't find the id in the output --- droidgap | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/droidgap b/droidgap index f599c575..03367e16 100755 --- a/droidgap +++ b/droidgap @@ -45,7 +45,9 @@ class Build def create_android puts "Creating Android project..." - `android create project -t 5 -k #{ @pkg } -a #{ @name } -n #{ @name } -p #{ @path }` + target_id = `android list targets` =~ /id:\s*(\d+).*android-5/ ? $1 : 5 + + `android create project -t #{target_id} -k #{ @pkg } -a #{ @name } -n #{ @name } -p #{ @path }` FileUtils.mkdir_p File.join(@path, "assets", "www") FileUtils.cp_r File.join(@www, "."), File.join(@path, "assets", "www")