From ae800455b1d5633907698a8f6de72e191476aa7d Mon Sep 17 00:00:00 2001 From: Dave Johnson Date: Thu, 19 Aug 2010 16:10:50 -0700 Subject: [PATCH] no more silly json strings in there --- .../src/com/phonegap/api/CommandManager.java | 19 +++---- .../src/com/phonegap/api/CommandResult.java | 50 +++++++++++++------ 2 files changed, 42 insertions(+), 27 deletions(-) diff --git a/framework/src/com/phonegap/api/CommandManager.java b/framework/src/com/phonegap/api/CommandManager.java index b3a9c804..924af9c8 100644 --- a/framework/src/com/phonegap/api/CommandManager.java +++ b/framework/src/com/phonegap/api/CommandManager.java @@ -17,10 +17,7 @@ import com.phonegap.DroidGap; * @author davejohnson * */ -public final class CommandManager { - private static final String EXCEPTION_PREFIX = "[PhoneGap] *ERROR* Exception executing command ["; - private static final String EXCEPTION_SUFFIX = "]: "; - +public final class CommandManager { private Command[] commands; private final Context ctx; @@ -86,23 +83,19 @@ public final class CommandManager { } } } catch (ClassNotFoundException e) { - cr = new CommandResult(CommandResult.Status.CLASSNOTFOUNDEXCEPTION, - "{ message: 'ClassNotFoundException', status: "+CommandResult.Status.CLASSNOTFOUNDEXCEPTION.ordinal()+" }"); + cr = new CommandResult(CommandResult.Status.CLASS_NOT_FOUND_EXCEPTION); } catch (IllegalAccessException e) { - cr = new CommandResult(CommandResult.Status.ILLEGALACCESSEXCEPTION, - "{ message: 'IllegalAccessException', status: "+CommandResult.Status.ILLEGALACCESSEXCEPTION.ordinal()+" }"); + cr = new CommandResult(CommandResult.Status.ILLEGAL_ACCESS_EXCEPTION); } catch (InstantiationException e) { - cr = new CommandResult(CommandResult.Status.INSTANTIATIONEXCEPTION, - "{ message: 'InstantiationException', status: "+CommandResult.Status.INSTANTIATIONEXCEPTION.ordinal()+" }"); + cr = new CommandResult(CommandResult.Status.INSTANTIATION_EXCEPTION); } catch (JSONException e) { - cr = new CommandResult(CommandResult.Status.JSONEXCEPTION, - "{ message: 'JSONException', status: "+CommandResult.Status.JSONEXCEPTION.ordinal()+" }"); + cr = new CommandResult(CommandResult.Status.JSON_EXCEPTION); } // if async we have already returned at this point unless there was an error... if (async) { app.loadUrl(cr.toErrorCallbackString(callbackId)); } - return ( cr != null ? cr.getResult() : "{ status: 0, message: 'all good' }" ); + return ( cr != null ? cr.getJSONString() : "{ status: 0, message: 'all good' }" ); } /** diff --git a/framework/src/com/phonegap/api/CommandResult.java b/framework/src/com/phonegap/api/CommandResult.java index 75843d99..ee5e4f84 100644 --- a/framework/src/com/phonegap/api/CommandResult.java +++ b/framework/src/com/phonegap/api/CommandResult.java @@ -1,38 +1,60 @@ package com.phonegap.api; +import java.net.URLEncoder; + public class CommandResult { private final int status; - private final String result; + private final String message; - public CommandResult(Status status, String result) { + public CommandResult(Status status) { this.status = status.ordinal(); - this.result = result; + this.message = CommandResult.StatusMessages[this.status]; + } + + public CommandResult(Status status, String message) { + this.status = status.ordinal(); + this.message = message; } public int getStatus() { return status; } - public String getResult() { - return result; + public String getMessage() { + return message; + } + + public String getJSONString() { + return "{ status: " + this.getStatus() + ", message: '" + URLEncoder.encode(this.getMessage()) + "' }"; } public String toSuccessCallbackString(String callbackId) { - return "javascript:PhoneGap.callbackSuccess('"+callbackId+"', " + this.getResult()+ ");"; + return "javascript:PhoneGap.callbackSuccess('"+callbackId+"', " + this.getJSONString() + " );"; } public String toErrorCallbackString(String callbackId) { - return "javascript:PhoneGap.callbackError('"+callbackId+"', " + this.getResult()+ ");"; + return "javascript:PhoneGap.callbackError('"+callbackId+"', " + this.getJSONString()+ ");"; } + public static String[] StatusMessages = new String[] { + "OK", + "Class not found", + "Illegal access", + "Instantiation error", + "Malformed url", + "IO error", + "Invalid action", + "JSON error" + }; + public enum Status { OK, - CLASSNOTFOUNDEXCEPTION, - ILLEGALACCESSEXCEPTION, - INSTANTIATIONEXCEPTION, - MALFORMEDURLEXCEPTION, - IOEXCEPTION, - INVALIDACTION, - JSONEXCEPTION + CLASS_NOT_FOUND_EXCEPTION, + ILLEGAL_ACCESS_EXCEPTION, + INSTANTIATION_EXCEPTION, + MALFORMED_URL_EXCEPTION, + IO_EXCEPTION, + INVALID_ACTION, + JSON_EXCEPTION } }