From dc5078306d9d294b180d6110ce0650af217cbdf6 Mon Sep 17 00:00:00 2001 From: Andrew Grieve Date: Mon, 15 Oct 2012 13:58:44 -0400 Subject: [PATCH] Remove manual catching of JSONException where possible. Delegate the catching to caller instead. Related to refactoring of Plugin->CordovaPlugin. --- .../src/org/apache/cordova/AudioHandler.java | 103 +++++++++--------- .../org/apache/cordova/CameraLauncher.java | 82 +++++++------- framework/src/org/apache/cordova/Capture.java | 56 ++++------ .../org/apache/cordova/CompassListener.java | 96 ++++++++-------- .../org/apache/cordova/ContactManager.java | 91 ++++++++-------- framework/src/org/apache/cordova/Device.java | 34 +++--- .../src/org/apache/cordova/FileUtils.java | 4 +- .../src/org/apache/cordova/GeoBroker.java | 48 ++++---- .../src/org/apache/cordova/Notification.java | 74 ++++++------- framework/src/org/apache/cordova/Storage.java | 42 ++++--- 10 files changed, 290 insertions(+), 340 deletions(-) diff --git a/framework/src/org/apache/cordova/AudioHandler.java b/framework/src/org/apache/cordova/AudioHandler.java index 5872ddff..ed5757fe 100644 --- a/framework/src/org/apache/cordova/AudioHandler.java +++ b/framework/src/org/apache/cordova/AudioHandler.java @@ -63,64 +63,59 @@ public class AudioHandler extends CordovaPlugin { * @param callbackContext The callback context used when calling back into JavaScript. * @return A PluginResult object with a status and message. */ - public boolean execute(String action, JSONArray args, CallbackContext callbackContext) { + public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { PluginResult.Status status = PluginResult.Status.OK; String result = ""; - try { - if (action.equals("startRecordingAudio")) { - this.startRecordingAudio(args.getString(0), FileUtils.stripFileProtocol(args.getString(1))); - } - else if (action.equals("stopRecordingAudio")) { - this.stopRecordingAudio(args.getString(0)); - } - else if (action.equals("startPlayingAudio")) { - this.startPlayingAudio(args.getString(0), FileUtils.stripFileProtocol(args.getString(1))); - } - else if (action.equals("seekToAudio")) { - this.seekToAudio(args.getString(0), args.getInt(1)); - } - else if (action.equals("pausePlayingAudio")) { - this.pausePlayingAudio(args.getString(0)); - } - else if (action.equals("stopPlayingAudio")) { - this.stopPlayingAudio(args.getString(0)); - } else if (action.equals("setVolume")) { - try { - this.setVolume(args.getString(0), Float.parseFloat(args.getString(1))); - } catch (NumberFormatException nfe) { - //no-op - } - } else if (action.equals("getCurrentPositionAudio")) { - float f = this.getCurrentPositionAudio(args.getString(0)); - callbackContext.sendPluginResult(new PluginResult(status, f)); - return true; - } - else if (action.equals("getDurationAudio")) { - float f = this.getDurationAudio(args.getString(0), args.getString(1)); - callbackContext.sendPluginResult(new PluginResult(status, f)); - return true; - } - else if (action.equals("create")) { - String id = args.getString(0); - String src = FileUtils.stripFileProtocol(args.getString(1)); - AudioPlayer audio = new AudioPlayer(this, id, src); - this.players.put(id, audio); - } - else if (action.equals("release")) { - boolean b = this.release(args.getString(0)); - callbackContext.sendPluginResult(new PluginResult(status, b)); - return true; - } - else { // Unrecognized action. - return false; - } - - callbackContext.sendPluginResult(new PluginResult(status, result)); - } catch (JSONException e) { - e.printStackTrace(); - callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION)); + if (action.equals("startRecordingAudio")) { + this.startRecordingAudio(args.getString(0), FileUtils.stripFileProtocol(args.getString(1))); } + else if (action.equals("stopRecordingAudio")) { + this.stopRecordingAudio(args.getString(0)); + } + else if (action.equals("startPlayingAudio")) { + this.startPlayingAudio(args.getString(0), FileUtils.stripFileProtocol(args.getString(1))); + } + else if (action.equals("seekToAudio")) { + this.seekToAudio(args.getString(0), args.getInt(1)); + } + else if (action.equals("pausePlayingAudio")) { + this.pausePlayingAudio(args.getString(0)); + } + else if (action.equals("stopPlayingAudio")) { + this.stopPlayingAudio(args.getString(0)); + } else if (action.equals("setVolume")) { + try { + this.setVolume(args.getString(0), Float.parseFloat(args.getString(1))); + } catch (NumberFormatException nfe) { + //no-op + } + } else if (action.equals("getCurrentPositionAudio")) { + float f = this.getCurrentPositionAudio(args.getString(0)); + callbackContext.sendPluginResult(new PluginResult(status, f)); + return true; + } + else if (action.equals("getDurationAudio")) { + float f = this.getDurationAudio(args.getString(0), args.getString(1)); + callbackContext.sendPluginResult(new PluginResult(status, f)); + return true; + } + else if (action.equals("create")) { + String id = args.getString(0); + String src = FileUtils.stripFileProtocol(args.getString(1)); + AudioPlayer audio = new AudioPlayer(this, id, src); + this.players.put(id, audio); + } + else if (action.equals("release")) { + boolean b = this.release(args.getString(0)); + callbackContext.sendPluginResult(new PluginResult(status, b)); + return true; + } + else { // Unrecognized action. + return false; + } + + callbackContext.sendPluginResult(new PluginResult(status, result)); return true; } diff --git a/framework/src/org/apache/cordova/CameraLauncher.java b/framework/src/org/apache/cordova/CameraLauncher.java index 59e9f212..f9cfb94a 100755 --- a/framework/src/org/apache/cordova/CameraLauncher.java +++ b/framework/src/org/apache/cordova/CameraLauncher.java @@ -116,57 +116,51 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect * @param callbackContext The callback id used when calling back into JavaScript. * @return A PluginResult object with a status and message. */ - public boolean execute(String action, JSONArray args, CallbackContext callbackContext) { + public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { this.callbackContext = callbackContext; - try { - if (action.equals("takePicture")) { - int srcType = CAMERA; - int destType = FILE_URI; - this.saveToPhotoAlbum = false; - this.targetHeight = 0; - this.targetWidth = 0; - this.encodingType = JPEG; - this.mediaType = PICTURE; - this.mQuality = 80; + if (action.equals("takePicture")) { + int srcType = CAMERA; + int destType = FILE_URI; + this.saveToPhotoAlbum = false; + this.targetHeight = 0; + this.targetWidth = 0; + this.encodingType = JPEG; + this.mediaType = PICTURE; + this.mQuality = 80; - this.mQuality = args.getInt(0); - destType = args.getInt(1); - srcType = args.getInt(2); - this.targetWidth = args.getInt(3); - this.targetHeight = args.getInt(4); - this.encodingType = args.getInt(5); - this.mediaType = args.getInt(6); - //this.allowEdit = args.getBoolean(7); // This field is unused. - this.correctOrientation = args.getBoolean(8); - this.saveToPhotoAlbum = args.getBoolean(9); + this.mQuality = args.getInt(0); + destType = args.getInt(1); + srcType = args.getInt(2); + this.targetWidth = args.getInt(3); + this.targetHeight = args.getInt(4); + this.encodingType = args.getInt(5); + this.mediaType = args.getInt(6); + //this.allowEdit = args.getBoolean(7); // This field is unused. + this.correctOrientation = args.getBoolean(8); + this.saveToPhotoAlbum = args.getBoolean(9); - // If the user specifies a 0 or smaller width/height - // make it -1 so later comparisons succeed - if (this.targetWidth < 1) { - this.targetWidth = -1; - } - if (this.targetHeight < 1) { - this.targetHeight = -1; - } - - if (srcType == CAMERA) { - this.takePicture(destType, encodingType); - } - else if ((srcType == PHOTOLIBRARY) || (srcType == SAVEDPHOTOALBUM)) { - this.getImage(srcType, destType); - } - PluginResult r = new PluginResult(PluginResult.Status.NO_RESULT); - r.setKeepCallback(true); - callbackContext.sendPluginResult(r); - return true; + // If the user specifies a 0 or smaller width/height + // make it -1 so later comparisons succeed + if (this.targetWidth < 1) { + this.targetWidth = -1; } - return false; - } catch (JSONException e) { - e.printStackTrace(); - callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION)); + if (this.targetHeight < 1) { + this.targetHeight = -1; + } + + if (srcType == CAMERA) { + this.takePicture(destType, encodingType); + } + else if ((srcType == PHOTOLIBRARY) || (srcType == SAVEDPHOTOALBUM)) { + this.getImage(srcType, destType); + } + PluginResult r = new PluginResult(PluginResult.Status.NO_RESULT); + r.setKeepCallback(true); + callbackContext.sendPluginResult(r); return true; } + return false; } //-------------------------------------------------------------------------- diff --git a/framework/src/org/apache/cordova/Capture.java b/framework/src/org/apache/cordova/Capture.java index 1e03d7b3..4cf46dd5 100644 --- a/framework/src/org/apache/cordova/Capture.java +++ b/framework/src/org/apache/cordova/Capture.java @@ -76,7 +76,7 @@ public class Capture extends CordovaPlugin { // } @Override - public boolean execute(String action, JSONArray args, CallbackContext callbackContext) { + public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { this.callbackContext = callbackContext; this.limit = 1; this.duration = 0.0f; @@ -89,12 +89,8 @@ public class Capture extends CordovaPlugin { } if (action.equals("getFormatData")) { - try { - JSONObject obj = getFormatData(args.getString(0), args.getString(1)); - callbackContext.success(obj); - } catch (JSONException e) { - callbackContext.error(""); - } + JSONObject obj = getFormatData(args.getString(0), args.getString(1)); + callbackContext.success(obj); return true; } else if (action.equals("captureAudio")) { @@ -120,34 +116,30 @@ public class Capture extends CordovaPlugin { * @param mimeType of the file * @return a MediaFileData object */ - private JSONObject getFormatData(String filePath, String mimeType) { + private JSONObject getFormatData(String filePath, String mimeType) throws JSONException { JSONObject obj = new JSONObject(); - try { - // setup defaults - obj.put("height", 0); - obj.put("width", 0); - obj.put("bitrate", 0); - obj.put("duration", 0); - obj.put("codecs", ""); + // setup defaults + obj.put("height", 0); + obj.put("width", 0); + obj.put("bitrate", 0); + obj.put("duration", 0); + obj.put("codecs", ""); - // If the mimeType isn't set the rest will fail - // so let's see if we can determine it. - if (mimeType == null || mimeType.equals("")) { - mimeType = FileUtils.getMimeType(filePath); - } - Log.d(LOG_TAG, "Mime type = " + mimeType); + // If the mimeType isn't set the rest will fail + // so let's see if we can determine it. + if (mimeType == null || mimeType.equals("")) { + mimeType = FileUtils.getMimeType(filePath); + } + Log.d(LOG_TAG, "Mime type = " + mimeType); - if (mimeType.equals(IMAGE_JPEG) || filePath.endsWith(".jpg")) { - obj = getImageData(filePath, obj); - } - else if (mimeType.endsWith(AUDIO_3GPP)) { - obj = getAudioVideoData(filePath, obj, false); - } - else if (mimeType.equals(VIDEO_3GPP) || mimeType.equals(VIDEO_MP4)) { - obj = getAudioVideoData(filePath, obj, true); - } - } catch (JSONException e) { - Log.d(LOG_TAG, "Error: setting media file data object"); + if (mimeType.equals(IMAGE_JPEG) || filePath.endsWith(".jpg")) { + obj = getImageData(filePath, obj); + } + else if (mimeType.endsWith(AUDIO_3GPP)) { + obj = getAudioVideoData(filePath, obj, false); + } + else if (mimeType.equals(VIDEO_3GPP) || mimeType.equals(VIDEO_MP4)) { + obj = getAudioVideoData(filePath, obj, true); } return obj; } diff --git a/framework/src/org/apache/cordova/CompassListener.java b/framework/src/org/apache/cordova/CompassListener.java index df62da5a..7e783b91 100755 --- a/framework/src/org/apache/cordova/CompassListener.java +++ b/framework/src/org/apache/cordova/CompassListener.java @@ -88,50 +88,46 @@ public class CompassListener extends CordovaPlugin implements SensorEventListene * @param args JSONArry of arguments for the plugin. * @param callbackS=Context The callback id used when calling back into JavaScript. * @return True if the action was valid. + * @throws JSONException */ - public boolean execute(String action, JSONArray args, CallbackContext callbackContext) { - try { - if (action.equals("start")) { - this.start(); - } - else if (action.equals("stop")) { - this.stop(); - } - else if (action.equals("getStatus")) { - int i = this.getStatus(); - callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, i)); - } - else if (action.equals("getHeading")) { - // If not running, then this is an async call, so don't worry about waiting - if (this.status != CompassListener.RUNNING) { - int r = this.start(); - if (r == CompassListener.ERROR_FAILED_TO_START) { - callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.IO_EXCEPTION, CompassListener.ERROR_FAILED_TO_START)); - return true; - } - // Set a timeout callback on the main thread. - Handler handler = new Handler(Looper.getMainLooper()); - handler.postDelayed(new Runnable() { - public void run() { - CompassListener.this.timeout(); - } - }, 2000); + public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { + if (action.equals("start")) { + this.start(); + } + else if (action.equals("stop")) { + this.stop(); + } + else if (action.equals("getStatus")) { + int i = this.getStatus(); + callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, i)); + } + else if (action.equals("getHeading")) { + // If not running, then this is an async call, so don't worry about waiting + if (this.status != CompassListener.RUNNING) { + int r = this.start(); + if (r == CompassListener.ERROR_FAILED_TO_START) { + callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.IO_EXCEPTION, CompassListener.ERROR_FAILED_TO_START)); + return true; } - callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, getCompassHeading())); + // Set a timeout callback on the main thread. + Handler handler = new Handler(Looper.getMainLooper()); + handler.postDelayed(new Runnable() { + public void run() { + CompassListener.this.timeout(); + } + }, 2000); } - else if (action.equals("setTimeout")) { - this.setTimeout(args.getLong(0)); - } - else if (action.equals("getTimeout")) { - long l = this.getTimeout(); - callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, l)); - } else { - // Unsupported action - return false; - } - } catch (JSONException e) { - e.printStackTrace(); - callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION)); + callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, getCompassHeading())); + } + else if (action.equals("setTimeout")) { + this.setTimeout(args.getLong(0)); + } + else if (action.equals("getTimeout")) { + long l = this.getTimeout(); + callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, l)); + } else { + // Unsupported action + return false; } return true; } @@ -281,19 +277,15 @@ public class CompassListener extends CordovaPlugin implements SensorEventListene * * @return a compass heading */ - private JSONObject getCompassHeading() { + private JSONObject getCompassHeading() throws JSONException { JSONObject obj = new JSONObject(); - try { - obj.put("magneticHeading", this.getHeading()); - obj.put("trueHeading", this.getHeading()); - // Since the magnetic and true heading are always the same our and accuracy - // is defined as the difference between true and magnetic always return zero - obj.put("headingAccuracy", 0); - obj.put("timestamp", this.timeStamp); - } catch (JSONException e) { - // Should never happen - } + obj.put("magneticHeading", this.getHeading()); + obj.put("trueHeading", this.getHeading()); + // Since the magnetic and true heading are always the same our and accuracy + // is defined as the difference between true and magnetic always return zero + obj.put("headingAccuracy", 0); + obj.put("timestamp", this.timeStamp); return obj; } diff --git a/framework/src/org/apache/cordova/ContactManager.java b/framework/src/org/apache/cordova/ContactManager.java index 5235c65a..8ea64a64 100755 --- a/framework/src/org/apache/cordova/ContactManager.java +++ b/framework/src/org/apache/cordova/ContactManager.java @@ -53,7 +53,7 @@ public class ContactManager extends CordovaPlugin { * @param callbackContext The callback context used when calling back into JavaScript. * @return True if the action was valid, false otherwise. */ - public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) { + public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException { /** * Check to see if we are on an Android 1.X device. If we are return an error as we * do not support this as of Cordova 1.0. @@ -71,56 +71,51 @@ public class ContactManager extends CordovaPlugin { this.contactAccessor = new ContactAccessorSdk5(this.webView, this.cordova); } - try { - if (action.equals("search")) { - final JSONArray filter = args.getJSONArray(0); - final JSONObject options = args.getJSONObject(1); - this.cordova.getThreadPool().execute(new Runnable() { - public void run() { - JSONArray res = contactAccessor.search(filter, options); + if (action.equals("search")) { + final JSONArray filter = args.getJSONArray(0); + final JSONObject options = args.getJSONObject(1); + this.cordova.getThreadPool().execute(new Runnable() { + public void run() { + JSONArray res = contactAccessor.search(filter, options); + callbackContext.success(res); + } + }); + } + else if (action.equals("save")) { + final JSONObject contact = args.getJSONObject(0); + this.cordova.getThreadPool().execute(new Runnable() { + public void run() { + JSONObject res = null; + String id = contactAccessor.save(contact); + if (id != null) { + try { + res = contactAccessor.getContactById(id); + } catch (JSONException e) { + Log.e(LOG_TAG, "JSON fail.", e); + } + } + if (res != null) { callbackContext.success(res); + } else { + callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, UNKNOWN_ERROR)); } - }); - } - else if (action.equals("save")) { - final JSONObject contact = args.getJSONObject(0); - this.cordova.getThreadPool().execute(new Runnable() { - public void run() { - JSONObject res = null; - String id = contactAccessor.save(contact); - if (id != null) { - try { - res = contactAccessor.getContactById(id); - } catch (JSONException e) { - Log.e(LOG_TAG, "JSON fail.", e); - } - } - if (res != null) { - callbackContext.success(res); - } else { - callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, UNKNOWN_ERROR)); - } + } + }); + } + else if (action.equals("remove")) { + final String contactId = args.getString(0); + this.cordova.getThreadPool().execute(new Runnable() { + public void run() { + if (contactAccessor.remove(contactId)) { + callbackContext.success(); + } else { + callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, UNKNOWN_ERROR)); } - }); - } - else if (action.equals("remove")) { - final String contactId = args.getString(0); - this.cordova.getThreadPool().execute(new Runnable() { - public void run() { - if (contactAccessor.remove(contactId)) { - callbackContext.success(); - } else { - callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, UNKNOWN_ERROR)); - } - } - }); - } - else { - return false; - } - } catch (JSONException e) { - Log.e(LOG_TAG, e.getMessage(), e); - callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION)); + } + }); + } + else { + return false; } return true; } diff --git a/framework/src/org/apache/cordova/Device.java b/framework/src/org/apache/cordova/Device.java index 08217329..bc0ff906 100644 --- a/framework/src/org/apache/cordova/Device.java +++ b/framework/src/org/apache/cordova/Device.java @@ -72,25 +72,21 @@ public class Device extends CordovaPlugin { * @param callbackContext The callback id used when calling back into JavaScript. * @return True if the action was valid, false if not. */ - public boolean execute(String action, JSONArray args, CallbackContext callbackContext) { - try { - if (action.equals("getDeviceInfo")) { - JSONObject r = new JSONObject(); - r.put("uuid", Device.uuid); - r.put("version", this.getOSVersion()); - r.put("platform", Device.platform); - r.put("name", this.getProductName()); - r.put("cordova", Device.cordovaVersion); - //JSONObject pg = new JSONObject(); - //pg.put("version", Device.CordovaVersion); - //r.put("cordova", pg); - callbackContext.success(r); - } - else { - return false; - } - } catch (JSONException e) { - callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION)); + public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { + if (action.equals("getDeviceInfo")) { + JSONObject r = new JSONObject(); + r.put("uuid", Device.uuid); + r.put("version", this.getOSVersion()); + r.put("platform", Device.platform); + r.put("name", this.getProductName()); + r.put("cordova", Device.cordovaVersion); + //JSONObject pg = new JSONObject(); + //pg.put("version", Device.CordovaVersion); + //r.put("cordova", pg); + callbackContext.success(r); + } + else { + return false; } return true; } diff --git a/framework/src/org/apache/cordova/FileUtils.java b/framework/src/org/apache/cordova/FileUtils.java index 7d5e5a14..447e4f2c 100755 --- a/framework/src/org/apache/cordova/FileUtils.java +++ b/framework/src/org/apache/cordova/FileUtils.java @@ -92,7 +92,7 @@ public class FileUtils extends CordovaPlugin { * @param callbackContext The callback context used when calling back into JavaScript. * @return True if the action was valid, false otherwise. */ - public boolean execute(String action, JSONArray args, CallbackContext callbackContext) { + public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { try { if (action.equals("testSaveLocationExists")) { boolean b = DirectoryManager.testSaveLocationExists(); @@ -199,8 +199,6 @@ public class FileUtils extends CordovaPlugin { callbackContext.error(FileUtils.PATH_EXISTS_ERR); } catch (NoModificationAllowedException e) { callbackContext.error(FileUtils.NO_MODIFICATION_ALLOWED_ERR); - } catch (JSONException e) { - callbackContext.error(FileUtils.NO_MODIFICATION_ALLOWED_ERR); } catch (InvalidModificationException e) { callbackContext.error(FileUtils.INVALID_MODIFICATION_ERR); } catch (MalformedURLException e) { diff --git a/framework/src/org/apache/cordova/GeoBroker.java b/framework/src/org/apache/cordova/GeoBroker.java index 6bda653c..5377e365 100755 --- a/framework/src/org/apache/cordova/GeoBroker.java +++ b/framework/src/org/apache/cordova/GeoBroker.java @@ -54,7 +54,7 @@ public class GeoBroker extends CordovaPlugin { * @param callbackContext The callback id used when calling back into JavaScript. * @return True if the action was valid, or false if not. */ - public boolean execute(String action, JSONArray args, CallbackContext callbackContext) { + public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { if (this.locationManager == null) { this.locationManager = (LocationManager) this.cordova.getActivity().getSystemService(Context.LOCATION_SERVICE); this.networkListener = new NetworkListener(this.locationManager, this); @@ -70,32 +70,28 @@ public class GeoBroker extends CordovaPlugin { result.setKeepCallback(true); - try { - if (action.equals("getLocation")) { - boolean enableHighAccuracy = args.getBoolean(0); - int maximumAge = args.getInt(1); - Location last = this.locationManager.getLastKnownLocation((enableHighAccuracy ? LocationManager.GPS_PROVIDER : LocationManager.NETWORK_PROVIDER)); - // Check if we can use lastKnownLocation to get a quick reading and use less battery - if (last != null && (System.currentTimeMillis() - last.getTime()) <= maximumAge) { - result = new PluginResult(PluginResult.Status.OK, this.returnLocationJSON(last)); - } else { - this.getCurrentLocation(callbackContext, enableHighAccuracy); - } + if (action.equals("getLocation")) { + boolean enableHighAccuracy = args.getBoolean(0); + int maximumAge = args.getInt(1); + Location last = this.locationManager.getLastKnownLocation((enableHighAccuracy ? LocationManager.GPS_PROVIDER : LocationManager.NETWORK_PROVIDER)); + // Check if we can use lastKnownLocation to get a quick reading and use less battery + if (last != null && (System.currentTimeMillis() - last.getTime()) <= maximumAge) { + result = new PluginResult(PluginResult.Status.OK, this.returnLocationJSON(last)); + } else { + this.getCurrentLocation(callbackContext, enableHighAccuracy); } - else if (action.equals("addWatch")) { - String id = args.getString(0); - boolean enableHighAccuracy = args.getBoolean(1); - this.addWatch(id, callbackContext, enableHighAccuracy); - } - else if (action.equals("clearWatch")) { - String id = args.getString(0); - this.clearWatch(id); - } - else { - return false; - } - } catch (JSONException e) { - result = new PluginResult(PluginResult.Status.JSON_EXCEPTION, e.getMessage()); + } + else if (action.equals("addWatch")) { + String id = args.getString(0); + boolean enableHighAccuracy = args.getBoolean(1); + this.addWatch(id, callbackContext, enableHighAccuracy); + } + else if (action.equals("clearWatch")) { + String id = args.getString(0); + this.clearWatch(id); + } + else { + return false; } } callbackContext.sendPluginResult(result); diff --git a/framework/src/org/apache/cordova/Notification.java b/framework/src/org/apache/cordova/Notification.java index 8d44d2e9..f0d77b71 100755 --- a/framework/src/org/apache/cordova/Notification.java +++ b/framework/src/org/apache/cordova/Notification.java @@ -56,46 +56,42 @@ public class Notification extends CordovaPlugin { * @param callbackContext The callback context used when calling back into JavaScript. * @return True when the action was valid, false otherwise. */ - public boolean execute(String action, JSONArray args, CallbackContext callbackContext) { - try { - if (action.equals("beep")) { - this.beep(args.getLong(0)); - } - else if (action.equals("vibrate")) { - this.vibrate(args.getLong(0)); - } - else if (action.equals("alert")) { - this.alert(args.getString(0), args.getString(1), args.getString(2), callbackContext); - return true; - } - else if (action.equals("confirm")) { - this.confirm(args.getString(0), args.getString(1), args.getString(2), callbackContext); - return true; - } - else if (action.equals("activityStart")) { - this.activityStart(args.getString(0), args.getString(1)); - } - else if (action.equals("activityStop")) { - this.activityStop(); - } - else if (action.equals("progressStart")) { - this.progressStart(args.getString(0), args.getString(1)); - } - else if (action.equals("progressValue")) { - this.progressValue(args.getInt(0)); - } - else if (action.equals("progressStop")) { - this.progressStop(); - } - else { - return false; - } - - // Only alert and confirm are async. - callbackContext.success(); - } catch (JSONException e) { - callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION)); + public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { + if (action.equals("beep")) { + this.beep(args.getLong(0)); } + else if (action.equals("vibrate")) { + this.vibrate(args.getLong(0)); + } + else if (action.equals("alert")) { + this.alert(args.getString(0), args.getString(1), args.getString(2), callbackContext); + return true; + } + else if (action.equals("confirm")) { + this.confirm(args.getString(0), args.getString(1), args.getString(2), callbackContext); + return true; + } + else if (action.equals("activityStart")) { + this.activityStart(args.getString(0), args.getString(1)); + } + else if (action.equals("activityStop")) { + this.activityStop(); + } + else if (action.equals("progressStart")) { + this.progressStart(args.getString(0), args.getString(1)); + } + else if (action.equals("progressValue")) { + this.progressValue(args.getInt(0)); + } + else if (action.equals("progressStop")) { + this.progressStop(); + } + else { + return false; + } + + // Only alert and confirm are async. + callbackContext.success(); return true; } diff --git a/framework/src/org/apache/cordova/Storage.java b/framework/src/org/apache/cordova/Storage.java index 0c382810..c76c9e4e 100755 --- a/framework/src/org/apache/cordova/Storage.java +++ b/framework/src/org/apache/cordova/Storage.java @@ -65,32 +65,28 @@ public class Storage extends CordovaPlugin { * The callback context used when calling back into JavaScript. * @return True if the action was valid, false otherwise. */ - public boolean execute(String action, JSONArray args, CallbackContext callbackContext) { - try { - if (action.equals("openDatabase")) { - this.openDatabase(args.getString(0), args.getString(1), - args.getString(2), args.getLong(3)); - } else if (action.equals("executeSql")) { - String[] s = null; - if (args.isNull(1)) { - s = new String[0]; - } else { - JSONArray a = args.getJSONArray(1); - int len = a.length(); - s = new String[len]; - for (int i = 0; i < len; i++) { - s[i] = a.getString(i); - } + public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { + if (action.equals("openDatabase")) { + this.openDatabase(args.getString(0), args.getString(1), + args.getString(2), args.getLong(3)); + } else if (action.equals("executeSql")) { + String[] s = null; + if (args.isNull(1)) { + s = new String[0]; + } else { + JSONArray a = args.getJSONArray(1); + int len = a.length(); + s = new String[len]; + for (int i = 0; i < len; i++) { + s[i] = a.getString(i); } - this.executeSql(args.getString(0), s, args.getString(2)); } - else { - return false; - } - callbackContext.success(); - } catch (JSONException e) { - callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION)); + this.executeSql(args.getString(0), s, args.getString(2)); } + else { + return false; + } + callbackContext.success(); return true; }