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

@ -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' }" );
}
/**

View File

@ -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
}
}