/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/package org.apache.cordova.api;
import org.apache.cordova.json4j.JSONArray;
import org.apache.cordova.json4j.JSONObject;
/**
* This class defines the standard object that should be returned as the
* result of any Cordova plugin invocation.
*/publicclass PluginResult {
privatefinalint status;
privatefinal String message;
private boolean keepCallback = false;
public PluginResult(Status status) {
this.status = status.ordinal();
this.message = JSONObject.quote(status.getMessage());
}
public PluginResult(Status status, String message) {
this.status = status.ordinal();
this.message = JSONObject.quote(message);
}
public PluginResult(Status status, JSONArray message) {
this.status = status.ordinal();
this.message = message.toString();
}
public PluginResult(Status status, JSONObject message) {
this.status = status.ordinal();
this.message = (message != null) ? message.toString(): "null";
}
public PluginResult(Status status, int i) {
this.status = status.ordinal();
this.message = ""+i;
}
public PluginResult(Status status, float f) {
this.status = status.ordinal();
this.message = ""+f;
}
public PluginResult(Status status, boolean b) {
this.status = status.ordinal();
this.message = ""+b;
}
public PluginResult(Status status, long l) {
this.status = status.ordinal();
this.message = ""+l;
}
publicint getStatus() {
return status;
}
public String getMessage() {
return message;
}
publicvoid setKeepCallback(boolean b) {
this.keepCallback = b;
}
public boolean getKeepCallback() {
returnthis.keepCallback;
}
public String getJSONString() {
return"{
\"status\":" + this.status + ",\"message\":" + this.message + ",\"keepCallback\":" + this.keepCallback + "
}";
}
/**
* Returns the JavaScript string that executes the success callback for the
* appropriate Cordova plugin. The string is intended to be passed to the
* JavaScript engine.
* @param callbackId Unique id of the callback that is associated with the invoked plugin
* @return JavaScript string that invokes the appropriate plugin success callback
*/public String toSuccessCallbackString(String callbackId) {
return"try {
cordova.callbackSuccess('"+callbackId+"', " + this.getJSONString() + ");
} catch(e) {
alert('error in callbackSuccess:' + e.message);
}";
}
/**
* Returns the JavaScript string that executes the error callback for the
* appropriate Cordova plugin. The string is intended to be passed to the
* JavaScript engine.
* @param callbackId Unique id of the callback that is associated with the invoked plugin
* @return JavaScript string that invokes the appropriate plugin error callback
*/public String toErrorCallbackString(String callbackId) {
return"try {
cordova.callbackError('"+callbackId+"', " + this.getJSONString() + ");
} catch(e) {
alert('error in callbackError:' + e.message);
}";
}
public String toErrorString() {
return"alert('general error');";
}
/**
* Enumerates PluginResult status.
*/publicstaticclass Status
{
privateint val;
private String message;
protected Status(int val, String message) {
this.val = val;
this.message = message;
}
publicint ordinal() {
returnthis.val;
}
public String getMessage() {
returnthis.message;
}
publicstaticfinal Status NO_RESULT = new Status(0, "No result");
publicstaticfinal Status OK = new Status(1, "OK");
publicstaticfinal Status CLASS_NOT_FOUND_EXCEPTION = new Status(2, "Class not found");
publicstaticfinal Status ILLEGAL_ACCESS_EXCEPTION = new Status(3, "Illegal access");
publicstaticfinal Status INSTANTIATION_EXCEPTION = new Status(4, "Instantiation error");
publicstaticfinal Status MALFORMED_URL_EXCEPTION = new Status(5, "Malformed URL");
publicstaticfinal Status IO_EXCEPTION = new Status(6, "IO error");
publicstaticfinal Status INVALID_ACTION = new Status(7, "Invalid action");
publicstaticfinal Status JSON_EXCEPTION = new Status(8, "JSON error");
publicstaticfinal Status ERROR = new Status(9, "Error");
}
}