Added ARenzi`s Reachability fixes.

This commit is contained in:
filmaj 2010-02-23 11:07:30 -08:00
parent b0054aa1b4
commit 02c750134c
4 changed files with 126 additions and 184 deletions

View File

@ -2,12 +2,12 @@
require 'fileutils' require 'fileutils'
class Build class Build
attr_reader :android_sdk_path, :name, :pkg, :www, :path attr_reader :android_sdk_path, :name, :pkg, :www, :path, :dir
def initialize(*a) def initialize(*a)
@android_sdk_path, @name, @pkg, @www, @path = a @android_sdk_path, @name, @pkg, @www, @path = a
@android_dir = File.expand_path(File.dirname(__FILE__)) @s = File::SEPARATOR
@framework_dir = File.join(@android_dir, "framework") @dir = Dir.pwd + @s
end end
# runs the build script # runs the build script
@ -25,18 +25,15 @@ class Build
# then generates framework/phonegap.jar # then generates framework/phonegap.jar
def build_jar def build_jar
puts "Building the JAR..." puts "Building the JAR..."
FileUtils.rm "#{ @dir }framework#{@s}local.properties" if File.exists? "#{ @dir }framework#{@s}local.properties"
%w(local.properties phonegap.js phonegap.jar).each do |f| FileUtils.rm "#{ @dir }framework#{@s}phonegap.js" if File.exists? "#{ @dir }framework#{@s}phonegap.js"
FileUtils.rm File.join(@framework_dir, f) if File.exists? File.join(@framework_dir, f) FileUtils.rm "#{ @dir }framework#{@s}phonegap.jar" if File.exists? "#{ @dir }framework#{@s}phonegap.jar"
end open("#{ @dir }framework#{@s}local.properties", 'w') do |f|
open(File.join(@framework_dir, "local.properties"), 'w') do |f|
f.puts "sdk.dir=#{ @android_sdk_path }" f.puts "sdk.dir=#{ @android_sdk_path }"
end end
Dir.chdir(@dir + "framework")
Dir.chdir(@framework_dir)
`ant jar` `ant jar`
Dir.chdir(@android_dir) Dir.chdir(@dir)
end end
# runs android create project # runs android create project
@ -44,66 +41,48 @@ class Build
# TODO validate Android SDK # TODO validate Android SDK
def create_android def create_android
puts "Creating Android project..." 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 FileUtils.mkdir_p "#{ @path }#{@s}assets#{@s}www"
FileUtils.cp_r "#{ @www }#{ @s }.", "#{ @path }#{ @s }assets#{ @s }www#{ @s }"
`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")
end end
# creates an AndroidManifest.xml for the project # creates an AndroidManifest.xml for the project
def generate_manifest def generate_manifest
puts "Generating manifest..." puts "Generating manifest..."
manifest = "" manifest = ""
open(File.join(@framework_dir, "AndroidManifest.xml"), 'r') do |old| open(@dir + 'framework/AndroidManifest.xml', 'r') do |old|
manifest = old.read manifest = old.read
manifest.gsub! 'android:versionCode="5"', 'android:versionCode="1"' manifest.gsub! 'android:versionCode="5"', 'android:versionCode="1"'
manifest.gsub! 'package="com.phonegap"', "package=\"#{ @pkg }\"" manifest.gsub! 'package="com.phonegap"', "package=\"#{ @pkg }\""
manifest.gsub! 'android:name=".StandAlone"', "android:name=\".#{ @name }\"" manifest.gsub! 'android:name=".StandAlone"', "android:name=\".#{ @name }\""
manifest.gsub! 'android:minSdkVersion="5"', 'android:minSdkVersion="3"' manifest.gsub! 'android:minSdkVersion="5"', 'android:minSdkVersion="3"'
end end
open(File.join(@path, "AndroidManifest.xml"), 'w') { |x| x.puts manifest } open("#{ @path }#{@s}AndroidManifest.xml", 'w') { |x| x.puts manifest }
end end
# copies stuff from framework into the project # copies stuff from framework into the project
# TODO need to allow for www import inc icon # TODO need to allow for www import inc icon
def copy_libs def copy_libs
puts "Copying over libraries and assets and creating phonegap.js..." puts "Copying over libraries and assets and creating phonegap.js..."
FileUtils.cp "#{ @dir }framework#{@s}phonegap.jar", "#{ @path }#{@s}libs"
framework_res_dir = File.join(@framework_dir, "res") FileUtils.cp "#{ @dir }framework#{@s}res#{@s}values#{@s}strings.xml", "#{ @path }#{@s}res#{@s}values#{@s}strings.xml"
app_res_dir = File.join(@path, "res") 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"
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| %w(drawable-hdpi drawable-ldpi drawable-mdpi).each do |e|
FileUtils.mkdir_p File.join(app_res_dir, e) FileUtils.cp "#{ @dir }framework#{@s}res#{@s}drawable#{@s}icon.png", "#{ @path }#{@s}res#{@s}#{ e }#{@s}icon.png"
FileUtils.cp File.join(framework_res_dir, "drawable", "icon.png"), File.join(app_res_dir, e, "icon.png")
end end
# concat JS and put into www folder. # concat JS and put into www folder.
js_dir = File.join(@framework_dir, "assets", "js") Dir.chdir("#{ @dir }framework#{ @s }assets#{ @s }js")
basedir = "."
phonegapjs = IO.read(File.join(js_dir, 'phonegap.js.base')) js = Dir.new(basedir).entries
phonegapjs = IO.read('phonegap.js.base');
Dir.new(js_dir).entries.each do |script| js.each do |script|
next if script[0].chr == "." or script == "phonegap.js.base" 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"
phonegapjs << "\n\n"
end end
Dir.chdir("#{ @dir}")
File.open(File.join(@path, "assets", "www", "phonegap.js"), 'w') {|f| f.write(phonegapjs) } File.open("#{ @path }#{ @s }assets#{ @s }www#{ @s }phonegap.js", 'w') {|f| f.write(phonegapjs) }
end end
# puts app name in strings # puts app name in strings
@ -115,7 +94,7 @@ class Build
<string name=\"go\">Snap</string> <string name=\"go\">Snap</string>
</resources> </resources>
" "
open(File.join(@path, "res", "values", "strings.xml"), 'w') do |f| open("#{ @path }#{@s}res#{@s}values#{@s}strings.xml", 'w') do |f|
f.puts x.gsub(' ','') f.puts x.gsub(' ','')
end end
end end
@ -141,11 +120,10 @@ class Build
} }
} }
" "
dir = "#{ @path }#{@s}src#{@s}#{ @pkg.gsub '.', '/' }";
code_dir = File.join(@path, "src", @pkg.gsub('.', File::SEPARATOR)) cls = "#{ @name }.java"
pth = File.join(dir,cls)
FileUtils.mkdir_p(code_dir) open(pth,'w') { |f| f.puts j.gsub(' ','') }
open(File.join(code_dir, "#{@name}.java"),'w') { |f| f.puts j.gsub(' ','') }
end end
# #
end end

View File

@ -72,7 +72,7 @@ PhoneGap.addConstructor = function(func) {
e.initEvent('deviceready'); e.initEvent('deviceready');
document.dispatchEvent(e); document.dispatchEvent(e);
} }
}, 1); }, 5);
})(); })();
@ -138,10 +138,11 @@ function Acceleration(x, y, z)
this.x = x; this.x = x;
this.y = y; this.y = y;
this.z = z; this.z = z;
this.timestamp = new Date().getTime();
} }
// Need to define these for android // Need to define these for android
_accel = {} _accel = {};
_accel.x = 0; _accel.x = 0;
_accel.y = 0; _accel.y = 0;
_accel.z = 0; _accel.z = 0;
@ -355,7 +356,7 @@ PhoneGap.addConstructor(function() {
if (typeof navigator.compass == "undefined") navigator.compass = new Compass(); if (typeof navigator.compass == "undefined") navigator.compass = new Compass();
}); });
var Contact = function(){ var Contact = function(){
this.name = null; this.name = new ContactName();
this.emails = []; this.emails = [];
this.phones = []; this.phones = [];
} }
@ -393,7 +394,18 @@ Contacts.prototype.find = function(obj, win, fail)
{ {
if(obj.name != null) if(obj.name != null)
{ {
ContactHook.search(name, "", ""); // Build up the search term that we'll use in SQL, based on the structure/contents of the contact object passed into find.
var searchTerm = '';
if (obj.name.givenName && obj.name.givenName.length > 0) {
searchTerm = obj.name.givenName.split(' ').join('%');
}
if (obj.name.familyName && obj.name.familyName.length > 0) {
searchTerm += obj.name.familyName.split(' ').join('%');
}
if (!obj.name.familyName && !obj.name.givenName && obj.name.formatted) {
searchTerm = obj.name.formatted;
}
ContactHook.search(searchTerm, "", "");
} }
this.win = win; this.win = win;
this.fail = fail; this.fail = fail;
@ -578,46 +590,12 @@ function Geolocation() {
}; };
}; };
/** Geolocation.prototype.getCurrentPosition = function(successCallback, errorCallback, options)
* Asynchronously aquires the current position. {
* @param {Function} successCallback The function to call when the position var position = Geo.getCurrentLocation();
* data is available this.global_success = successCallback;
* @param {Function} errorCallback The function to call when there is an error this.fail = errorCallback;
* 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);
};
/** /**
* Asynchronously aquires the position repeatedly at a given interval. * Asynchronously aquires the position repeatedly at a given interval.
@ -676,23 +654,6 @@ Geolocation.prototype.setError = function(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 // Run the global callback
Geolocation.prototype.gotCurrentPosition = function(lat, lng, alt, altacc, head, vel, stamp) Geolocation.prototype.gotCurrentPosition = function(lat, lng, alt, altacc, head, vel, stamp)
{ {
@ -704,6 +665,7 @@ Geolocation.prototype.gotCurrentPosition = function(lat, lng, alt, altacc, head,
{ {
coords = new Coordinates(lat, lng, alt, altacc, head, vel); coords = new Coordinates(lat, lng, alt, altacc, head, vel);
loc = new Position(coords, stamp); loc = new Position(coords, stamp);
this.lastPosition = loc;
this.global_success(loc); this.global_success(loc);
} }
} }
@ -749,7 +711,19 @@ Geolocation.prototype.clearWatch = function(watchId)
{ {
Geo.stop(watchId); Geo.stop(watchId);
} }
function KeyEvent() // 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"]
);
});function KeyEvent()
{ {
} }
@ -841,11 +815,9 @@ function NetworkStatus() {
this.code = null; this.code = null;
this.message = ""; this.message = "";
} }
NetworkStatus.NOT_REACHABLE = 0; NetworkStatus.NOT_REACHABLE = 0;
NetworkStatus.REACHABLE_VIA_CARRIER_DATA_NETWORK = 1; NetworkStatus.REACHABLE_VIA_CARRIER_DATA_NETWORK = 1;
NetworkStatus.REACHABLE_VIA_WIFI_NETWORK = 2; NetworkStatus.REACHABLE_VIA_WIFI_NETWORK = 2;
/** /**
* This class provides access to device Network data (reachability). * This class provides access to device Network data (reachability).
* @constructor * @constructor
@ -858,16 +830,6 @@ function Network() {
*/ */
this.lastReachability = null; this.lastReachability = null;
}; };
/**
*
* @param {Function} successCallback
* @param {Function} errorCallback
* @param {Object} options (isIpAddress:boolean)
*/
Network.prototype.isReachable = function(hostName, successCallback, options) {
}
/** /**
* Called by the geolocation framework when the reachability status has changed. * Called by the geolocation framework when the reachability status has changed.
* @param {Reachibility} reachability The current reachability status. * @param {Reachibility} reachability The current reachability status.
@ -875,25 +837,30 @@ Network.prototype.isReachable = function(hostName, successCallback, options) {
Network.prototype.updateReachability = function(reachability) { Network.prototype.updateReachability = function(reachability) {
this.lastReachability = reachability; this.lastReachability = reachability;
}; };
/**
PhoneGap.addConstructor(function() { *
if (typeof navigator.network == "undefined") navigator.network = new Network(); * @param {Object} uri
}); * @param {Function} win
* @param {Object} options (isIpAddress:boolean)
*/
Network.prototype.isReachable = function(uri, win, options) Network.prototype.isReachable = function(uri, win, options)
{ {
var status = new NetworkStatus(); var status = new NetworkStatus();
if(NetworkManager.isReachable(uri)) if(NetworkManager.isReachable(uri))
{ {
if (NetworkManager.isWifiActive) if (NetworkManager.isWifiActive()) {
status.code = 2; status.code = NetworkStatus.REACHABLE_VIA_WIFI_NETWORK;
else } else {
status.code = 1; status.code = NetworkStatus.REACHABLE_VIA_CARRIER_DATA_NETWORK;
}
} else {
status.code = NetworkStatus.NOT_REACHABLE;
} }
else
status.code = 0;
win(status); win(status);
} };
/** PhoneGap.addConstructor(function() {
if (typeof navigator.network == "undefined") navigator.network = new Network();
});/**
* This class provides access to notifications on the device. * This class provides access to notifications on the device.
*/ */
function Notification() { function Notification() {

View File

@ -6,11 +6,9 @@ function NetworkStatus() {
this.code = null; this.code = null;
this.message = ""; this.message = "";
} }
NetworkStatus.NOT_REACHABLE = 0; NetworkStatus.NOT_REACHABLE = 0;
NetworkStatus.REACHABLE_VIA_CARRIER_DATA_NETWORK = 1; NetworkStatus.REACHABLE_VIA_CARRIER_DATA_NETWORK = 1;
NetworkStatus.REACHABLE_VIA_WIFI_NETWORK = 2; NetworkStatus.REACHABLE_VIA_WIFI_NETWORK = 2;
/** /**
* This class provides access to device Network data (reachability). * This class provides access to device Network data (reachability).
* @constructor * @constructor
@ -23,16 +21,6 @@ function Network() {
*/ */
this.lastReachability = null; this.lastReachability = null;
}; };
/**
*
* @param {Function} successCallback
* @param {Function} errorCallback
* @param {Object} options (isIpAddress:boolean)
*/
Network.prototype.isReachable = function(hostName, successCallback, options) {
}
/** /**
* Called by the geolocation framework when the reachability status has changed. * Called by the geolocation framework when the reachability status has changed.
* @param {Reachibility} reachability The current reachability status. * @param {Reachibility} reachability The current reachability status.
@ -40,21 +28,27 @@ Network.prototype.isReachable = function(hostName, successCallback, options) {
Network.prototype.updateReachability = function(reachability) { Network.prototype.updateReachability = function(reachability) {
this.lastReachability = reachability; this.lastReachability = reachability;
}; };
/**
PhoneGap.addConstructor(function() { *
if (typeof navigator.network == "undefined") navigator.network = new Network(); * @param {Object} uri
}); * @param {Function} win
* @param {Object} options (isIpAddress:boolean)
*/
Network.prototype.isReachable = function(uri, win, options) Network.prototype.isReachable = function(uri, win, options)
{ {
var status = new NetworkStatus(); var status = new NetworkStatus();
if(NetworkManager.isReachable(uri)) if(NetworkManager.isReachable(uri))
{ {
if (NetworkManager.isWifiActive) if (NetworkManager.isWifiActive()) {
status.code = 2; status.code = NetworkStatus.REACHABLE_VIA_WIFI_NETWORK;
else } else {
status.code = 1; status.code = NetworkStatus.REACHABLE_VIA_CARRIER_DATA_NETWORK;
}
} else {
status.code = NetworkStatus.NOT_REACHABLE;
} }
else
status.code = 0;
win(status); win(status);
} };
PhoneGap.addConstructor(function() {
if (typeof navigator.network == "undefined") navigator.network = new Network();
});

View File

@ -32,8 +32,13 @@ public class NetworkManager {
public boolean isWifiActive() public boolean isWifiActive()
{ {
NetworkInfo info = sockMan.getActiveNetworkInfo(); NetworkInfo info = sockMan.getActiveNetworkInfo();
String type = info.getTypeName(); String type = "";
return type.equals("WIFI"); if (info!=null)
{
info.getTypeName();
return type.equals("WIFI")
}
return false;
} }
public boolean isReachable(String uri) public boolean isReachable(String uri)
@ -50,6 +55,4 @@ public class NetworkManager {
} }
return reached; return reached;
} }
} }