mirror of
https://github.com/apache/cordova-android.git
synced 2025-02-22 00:32:55 +08:00
Remove manual catching of JSONException where possible.
Delegate the catching to caller instead. Related to refactoring of Plugin->CordovaPlugin.
This commit is contained in:
parent
1bc032853c
commit
dc5078306d
@ -63,11 +63,10 @@ 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)));
|
||||
}
|
||||
@ -117,10 +116,6 @@ public class AudioHandler extends CordovaPlugin {
|
||||
}
|
||||
|
||||
callbackContext.sendPluginResult(new PluginResult(status, result));
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -116,10 +116,9 @@ 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;
|
||||
@ -162,11 +161,6 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
@ -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("");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
else if (action.equals("captureAudio")) {
|
||||
@ -120,9 +116,8 @@ 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);
|
||||
@ -146,9 +141,6 @@ public class Capture extends CordovaPlugin {
|
||||
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");
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
|
@ -88,9 +88,9 @@ 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 {
|
||||
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
|
||||
if (action.equals("start")) {
|
||||
this.start();
|
||||
}
|
||||
@ -129,10 +129,6 @@ public class CompassListener extends CordovaPlugin implements SensorEventListene
|
||||
// Unsupported action
|
||||
return false;
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
@ -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,7 +71,6 @@ 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);
|
||||
@ -118,10 +117,6 @@ public class ContactManager extends CordovaPlugin {
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
Log.e(LOG_TAG, e.getMessage(), e);
|
||||
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -72,8 +72,7 @@ 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 {
|
||||
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
|
||||
if (action.equals("getDeviceInfo")) {
|
||||
JSONObject r = new JSONObject();
|
||||
r.put("uuid", Device.uuid);
|
||||
@ -89,9 +88,6 @@ public class Device extends CordovaPlugin {
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -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) {
|
||||
|
@ -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,7 +70,6 @@ public class GeoBroker extends CordovaPlugin {
|
||||
|
||||
result.setKeepCallback(true);
|
||||
|
||||
try {
|
||||
if (action.equals("getLocation")) {
|
||||
boolean enableHighAccuracy = args.getBoolean(0);
|
||||
int maximumAge = args.getInt(1);
|
||||
@ -94,9 +93,6 @@ public class GeoBroker extends CordovaPlugin {
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
result = new PluginResult(PluginResult.Status.JSON_EXCEPTION, e.getMessage());
|
||||
}
|
||||
}
|
||||
callbackContext.sendPluginResult(result);
|
||||
return true;
|
||||
|
@ -56,8 +56,7 @@ 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 {
|
||||
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
|
||||
if (action.equals("beep")) {
|
||||
this.beep(args.getLong(0));
|
||||
}
|
||||
@ -93,9 +92,6 @@ public class Notification extends CordovaPlugin {
|
||||
|
||||
// Only alert and confirm are async.
|
||||
callbackContext.success();
|
||||
} catch (JSONException e) {
|
||||
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -65,8 +65,7 @@ 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 {
|
||||
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));
|
||||
@ -88,9 +87,6 @@ public class Storage extends CordovaPlugin {
|
||||
return false;
|
||||
}
|
||||
callbackContext.success();
|
||||
} catch (JSONException e) {
|
||||
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user