cordova-android/framework/src/com/phonegap/GeoBroker.java

138 lines
3.7 KiB
Java
Raw Normal View History

package com.phonegap;
2009-04-02 07:56:43 +08:00
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
2010-09-08 02:59:54 +08:00
import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult;
import android.content.Intent;
2009-04-02 07:56:43 +08:00
import android.webkit.WebView;
/*
* This class is the interface to the Geolocation. It's bound to the geo object.
*
* This class only starts and stops various GeoListeners, which consist of a GPS and a Network Listener
*/
2010-09-08 02:59:54 +08:00
public class GeoBroker implements Plugin {
WebView webView; // WebView object
DroidGap ctx; // DroidGap object
private HashMap<String, GeoListener> geoListeners;
2010-07-07 04:37:19 +08:00
private GeoListener global;
2009-04-02 07:56:43 +08:00
/**
* Constructor.
*/
public GeoBroker() {
this.geoListeners = new HashMap<String, GeoListener>();
2009-04-02 07:56:43 +08:00
}
/**
* Sets the context of the Command. This can then be used to do things like
* get file paths associated with the Activity.
*
* @param ctx The context of the main Activity.
*/
public void setContext(DroidGap ctx) {
this.ctx = ctx;
}
/**
* Sets the main View of the application, this is the WebView within which
* a PhoneGap app runs.
*
* @param webView The PhoneGap WebView
*/
public void setView(WebView webView) {
this.webView = webView;
}
/**
* Executes the request and returns CommandResult.
*
* @param action The command to execute.
* @param args JSONArry of arguments for the command.
* @return A CommandResult object with a status and message.
*/
2010-09-08 02:59:54 +08:00
public PluginResult execute(String action, JSONArray args) {
PluginResult.Status status = PluginResult.Status.OK;
String result = "";
try {
if (action.equals("getCurrentLocation")) {
this.getCurrentLocation();
}
else if (action.equals("start")) {
String s = this.start(args.getInt(0), args.getString(1));
2010-09-08 02:59:54 +08:00
return new PluginResult(status, s);
}
else if (action.equals("stop")) {
this.stop(args.getString(0));
}
2010-09-08 02:59:54 +08:00
return new PluginResult(status, result);
} catch (JSONException e) {
2010-09-08 02:59:54 +08:00
return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
}
}
/**
* Called when the system is about to start resuming a previous activity.
*/
public void onPause() {
}
/**
* Called when the activity will start interacting with the user.
*/
public void onResume() {
}
/**
* Called by AccelBroker when listener is to be shut down.
* Stop listener.
*/
public void onDestroy() {
}
/**
* Called when an activity you launched exits, giving you the requestCode you started it with,
* the resultCode it returned, and any additional data from it.
*
* @param requestCode The request code originally supplied to startActivityForResult(),
* allowing you to identify who this result came from.
* @param resultCode The integer result code returned by the child activity through its setResult().
* @param data An Intent, which can return result data to the caller (various data can be attached to Intent "extras").
*/
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
}
//--------------------------------------------------------------------------
// LOCAL METHODS
//--------------------------------------------------------------------------
public void getCurrentLocation() {
2010-07-07 04:37:19 +08:00
//It's supposed to run async!
if (global == null) {
global = new GeoListener("global", this.ctx, 10000, this.webView);
}
else {
2010-07-07 04:37:19 +08:00
global.start(10000);
}
2009-04-02 07:56:43 +08:00
}
public String start(int freq, String key) {
GeoListener listener = new GeoListener(key, this.ctx, freq, this.webView);
2009-04-02 07:56:43 +08:00
geoListeners.put(key, listener);
return key;
}
public void stop(String key) {
2009-04-02 07:56:43 +08:00
GeoListener geo = geoListeners.get(key);
}
}