Merge branch 'master' of github.com:phonegap/phonegap-android

This commit is contained in:
Brock Whitten 2010-02-15 14:00:25 -08:00
commit 17528c15bb
2 changed files with 69 additions and 82 deletions

View File

@ -1,10 +1,13 @@
#!/usr/bin/env ruby
require 'fileutils'
class Build
attr_reader :android_sdk_path, :name, :pkg, :www, :path
attr_reader :android_sdk_path, :name, :pkg, :www, :path, :dir
def initialize(*a)
@android_sdk_path, @name, @pkg, @www, @path = a
@s = File::SEPARATOR
@dir = Dir.pwd + @s
end
# runs the build script
@ -15,62 +18,83 @@ class Build
copy_libs
add_name_to_strings
write_java
puts "Complete!"
end
# removes local.properties and recreates based on android_sdk_path
# then generates framework/phonegap.jar
def build_jar
`rm framework/local.properties` if File.exists? 'framework/local.properties'
`rm framework/phonegap.jar` if File.exists? 'framework/phonegap.jar'
`rm framework/phonegap.js` if File.exists? 'framework/phonegap.js'
`ECHO 'sdk-location=#{ @android_sdk_path }' > framework/local.properties`
`cd framework; ant jar`
end
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|
f.puts "sdk.dir=#{ @android_sdk_path }"
end
Dir.chdir(@dir + "framework")
`ant jar`
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..."
`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 }"
end
# creates an AndroidManifest.xml for the project
def generate_manifest
puts "Generating manifest..."
manifest = ""
open('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"'
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"'
end
open("#{ @path }/AndroidManifest.xml", 'w') { |x| x.puts manifest }
open("#{ @path }#{@s}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
`mkdir -p #{ @path }/assets/wwww`
`cp framework/phonegap.jar #{ @path }/libs`
`cp framework/res/values/strings.xml #{ @path }/res/values/strings.xml`
`cp framework/res/layout/main.xml #{ @path }/res/layout/main.xml`
`cp framework/res/layout/preview.xml #{ @path }/res/layout/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|
`cp framework/res/drawable/icon.png #{ @path }/res/#{ e }/icon.png`
end
`cp -R example #{ @path }/assets`
`mv #{ @path }/assets/example #{ @path }/assets/www`
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) }
end
# puts app name in strings
def add_name_to_strings
puts "Adding some application name to strings.xml..."
x = "<?xml version=\"1.0\" encoding=\"utf-8\"?>
<resources>
<string name=\"app_name\">#{ @name }</string>
<string name=\"go\">Snap</string>
</resources>
"
open("#{ @path }/res/values/strings.xml", 'w') do |f|
open("#{ @path }#{@s}res#{@s}values#{@s}strings.xml", 'w') do |f|
f.puts x.gsub(' ','')
end
end
@ -78,6 +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..."
j = "
package #{ @pkg };
@ -95,7 +120,7 @@ class Build
}
}
"
dir = "#{ @path }/src/#{ @pkg.gsub '.', '/' }";
dir = "#{ @path }#{@s}src#{@s}#{ @pkg.gsub '.', '/' }";
cls = "#{ @name }.java"
pth = File.join(dir,cls)
open(pth,'w') { |f| f.puts j.gsub(' ','') }

View File

@ -14,46 +14,12 @@ function Geolocation() {
};
};
/**
* Asynchronously aquires the current position.
* @param {Function} successCallback The function to call when the position
* data is available
* @param {Function} errorCallback The function to call when there is an error
* getting the position data.
* @param {PositionOptions} options The options for getting the position data
* such as timeout.
*/
Geolocation.prototype.getCurrentPosition = function(successCallback, errorCallback, options) {
var referenceTime = 0;
if (this.lastPosition)
referenceTime = this.lastPosition.timeout;
else
this.start(options);
var timeout = 20000;
var interval = 500;
if (typeof(options) == 'object' && options.interval)
interval = options.interval;
if (typeof(successCallback) != 'function')
successCallback = function() {};
if (typeof(errorCallback) != 'function')
errorCallback = function() {};
var dis = this;
var delay = 0;
var timer = setInterval(function() {
delay += interval;
if (typeof(dis.lastPosition) == 'object' && dis.lastPosition.timestamp > referenceTime) {
successCallback(dis.lastPosition);
clearInterval(timer);
} else if (delay >= timeout) {
errorCallback();
clearInterval(timer);
}
}, interval);
};
Geolocation.prototype.getCurrentPosition = function(successCallback, errorCallback, options)
{
var position = Geo.getCurrentLocation();
this.global_success = successCallback;
this.fail = errorCallback;
}
/**
* Asynchronously aquires the position repeatedly at a given interval.
@ -111,23 +77,6 @@ Geolocation.prototype.setError = function(message) {
f(message);
}
};
PhoneGap.addConstructor(function() {
if (typeof navigator.geolocation == "undefined") navigator.geolocation = new Geolocation();
});
/*
* Since we can't guarantee that we will have the most recent, we just try our best!
*
* Also, the API doesn't specify which version is the best version of the API
*/
Geolocation.prototype.getCurrentPosition = function(successCallback, errorCallback, options)
{
var position = Geo.getCurrentLocation();
this.global_success = successCallback;
this.fail = errorCallback;
}
// Run the global callback
Geolocation.prototype.gotCurrentPosition = function(lat, lng, alt, altacc, head, vel, stamp)
@ -185,3 +134,16 @@ Geolocation.prototype.clearWatch = function(watchId)
{
Geo.stop(watchId);
}
// Taken from Jesse's geo fix (similar problem) in PhoneGap iPhone. Go figure, same browser!
function __proxyObj(origObj, proxyObj, funkList) {
for (var v in funkList) {
origObj[funkList[v]] = proxyObj[funkList[v]];
}
}
PhoneGap.addConstructor(function() {
navigator._geo = new Geolocation();
__proxyObj(navigator.geolocation, navigator._geo,
["setLocation", "getCurrentPosition", "watchPosition",
"clearWatch", "setError", "start", "stop", "gotCurrentPosition"]
);
});