mirror of
https://github.com/apache/cordova-android.git
synced 2026-04-23 00:00:09 +08:00
Change to use Commands and CommandManager.
This commit is contained in:
@@ -2,8 +2,13 @@ package com.phonegap;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
import android.content.Context;
|
||||
import android.location.Location;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
|
||||
import com.phonegap.api.Command;
|
||||
import com.phonegap.api.CommandResult;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.webkit.WebView;
|
||||
|
||||
/*
|
||||
@@ -12,38 +17,121 @@ import android.webkit.WebView;
|
||||
* This class only starts and stops various GeoListeners, which consist of a GPS and a Network Listener
|
||||
*/
|
||||
|
||||
public class GeoBroker extends Module {
|
||||
private WebView mAppView;
|
||||
private DroidGap mCtx;
|
||||
private HashMap<String, GeoListener> geoListeners;
|
||||
public class GeoBroker implements Command {
|
||||
|
||||
WebView webView; // WebView object
|
||||
DroidGap ctx; // DroidGap object
|
||||
|
||||
private HashMap<String, GeoListener> geoListeners;
|
||||
private GeoListener global;
|
||||
|
||||
public GeoBroker(WebView view, DroidGap ctx)
|
||||
{
|
||||
super(view, ctx);
|
||||
mCtx = ctx;
|
||||
mAppView = view;
|
||||
geoListeners = new HashMap<String, GeoListener>();
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
public GeoBroker() {
|
||||
this.geoListeners = new HashMap<String, GeoListener>();
|
||||
}
|
||||
|
||||
public void getCurrentLocation()
|
||||
{
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
public CommandResult execute(String action, JSONArray args) {
|
||||
CommandResult.Status status = CommandResult.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));
|
||||
return new CommandResult(status, s);
|
||||
}
|
||||
else if (action.equals("stop")) {
|
||||
this.stop(args.getString(0));
|
||||
}
|
||||
return new CommandResult(status, result);
|
||||
} catch (JSONException e) {
|
||||
return new CommandResult(CommandResult.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() {
|
||||
//It's supposed to run async!
|
||||
if(global == null)
|
||||
global = new GeoListener("global", mCtx, 10000, mAppView);
|
||||
else
|
||||
if (global == null) {
|
||||
global = new GeoListener("global", this.ctx, 10000, this.webView);
|
||||
}
|
||||
else {
|
||||
global.start(10000);
|
||||
}
|
||||
}
|
||||
|
||||
public String start(int freq, String key)
|
||||
{
|
||||
GeoListener listener = new GeoListener(key, mCtx, freq, mAppView);
|
||||
public String start(int freq, String key) {
|
||||
GeoListener listener = new GeoListener(key, this.ctx, freq, this.webView);
|
||||
geoListeners.put(key, listener);
|
||||
return key;
|
||||
}
|
||||
|
||||
public void stop(String key)
|
||||
{
|
||||
public void stop(String key) {
|
||||
GeoListener geo = geoListeners.get(key);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user