no more silly json strings in there

This commit is contained in:
Dave Johnson 2010-08-19 16:10:50 -07:00
parent 56f0e8e087
commit ae800455b1
2 changed files with 42 additions and 27 deletions

View File

@ -18,9 +18,6 @@ import com.phonegap.DroidGap;
* *
*/ */
public final class CommandManager { public final class CommandManager {
private static final String EXCEPTION_PREFIX = "[PhoneGap] *ERROR* Exception executing command [";
private static final String EXCEPTION_SUFFIX = "]: ";
private Command[] commands; private Command[] commands;
private final Context ctx; private final Context ctx;
@ -86,23 +83,19 @@ public final class CommandManager {
} }
} }
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
cr = new CommandResult(CommandResult.Status.CLASSNOTFOUNDEXCEPTION, cr = new CommandResult(CommandResult.Status.CLASS_NOT_FOUND_EXCEPTION);
"{ message: 'ClassNotFoundException', status: "+CommandResult.Status.CLASSNOTFOUNDEXCEPTION.ordinal()+" }");
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
cr = new CommandResult(CommandResult.Status.ILLEGALACCESSEXCEPTION, cr = new CommandResult(CommandResult.Status.ILLEGAL_ACCESS_EXCEPTION);
"{ message: 'IllegalAccessException', status: "+CommandResult.Status.ILLEGALACCESSEXCEPTION.ordinal()+" }");
} catch (InstantiationException e) { } catch (InstantiationException e) {
cr = new CommandResult(CommandResult.Status.INSTANTIATIONEXCEPTION, cr = new CommandResult(CommandResult.Status.INSTANTIATION_EXCEPTION);
"{ message: 'InstantiationException', status: "+CommandResult.Status.INSTANTIATIONEXCEPTION.ordinal()+" }");
} catch (JSONException e) { } catch (JSONException e) {
cr = new CommandResult(CommandResult.Status.JSONEXCEPTION, cr = new CommandResult(CommandResult.Status.JSON_EXCEPTION);
"{ message: 'JSONException', status: "+CommandResult.Status.JSONEXCEPTION.ordinal()+" }");
} }
// if async we have already returned at this point unless there was an error... // if async we have already returned at this point unless there was an error...
if (async) { if (async) {
app.loadUrl(cr.toErrorCallbackString(callbackId)); 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' }" );
} }
/** /**

View File

@ -1,38 +1,60 @@
package com.phonegap.api; package com.phonegap.api;
import java.net.URLEncoder;
public class CommandResult { public class CommandResult {
private final int status; 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.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() { public int getStatus() {
return status; return status;
} }
public String getResult() { public String getMessage() {
return result; return message;
}
public String getJSONString() {
return "{ status: " + this.getStatus() + ", message: '" + URLEncoder.encode(this.getMessage()) + "' }";
} }
public String toSuccessCallbackString(String callbackId) { public String toSuccessCallbackString(String callbackId) {
return "javascript:PhoneGap.callbackSuccess('"+callbackId+"', " + this.getResult()+ ");"; return "javascript:PhoneGap.callbackSuccess('"+callbackId+"', " + this.getJSONString() + " );";
} }
public String toErrorCallbackString(String callbackId) { 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 { public enum Status {
OK, OK,
CLASSNOTFOUNDEXCEPTION, CLASS_NOT_FOUND_EXCEPTION,
ILLEGALACCESSEXCEPTION, ILLEGAL_ACCESS_EXCEPTION,
INSTANTIATIONEXCEPTION, INSTANTIATION_EXCEPTION,
MALFORMEDURLEXCEPTION, MALFORMED_URL_EXCEPTION,
IOEXCEPTION, IO_EXCEPTION,
INVALIDACTION, INVALID_ACTION,
JSONEXCEPTION JSON_EXCEPTION
} }
} }