下面列出了怎么用org.apache.cordova.PluginResult.Status的API类实例代码及写法,或者点击链接到github查看源代码。
private void javascriptCallback(String event, JSONObject arguments,
CallbackContext callbackContext) {
if (callbackContext == null) {
return;
}
JSONObject options = new JSONObject();
try {
options.putOpt("callback", event);
options.putOpt("arguments", arguments);
} catch (JSONException e) {
callbackContext.sendPluginResult(new PluginResult(
PluginResult.Status.JSON_EXCEPTION));
return;
}
PluginResult result = new PluginResult(Status.OK, options);
result.setKeepCallback(true);
callbackContext.sendPluginResult(result);
}
public void sendCallback(String action, ServiceInfo service) {
CallbackContext callbackContext = callbacks.get(service.getType());
if (callbackContext == null) {
return;
}
JSONObject status = new JSONObject();
try {
status.put("action", action);
status.put("service", jsonifyService(service));
Log.d(TAG, "Sending result: " + status.toString());
PluginResult result = new PluginResult(PluginResult.Status.OK, status);
result.setKeepCallback(true);
callbackContext.sendPluginResult(result);
} catch (JSONException e) {
Log.e(TAG, e.getMessage(), e);
callbackContext.error("Error: " + e.getMessage());
}
}
public void initialize(CordovaArgs args, final CallbackContext ctx) throws JSONException {
service.addPurchaseObserver(this);
service.init(new InAppService.InitCompletion() {
@Override
public void onInit(Error error) {
JSONObject data = new JSONObject();
try {
data.put("products", InAppServicePlugin.this.productsToJSON(service.getProducts()));
data.put("canPurchase", service.canPurchase());
if (error != null) {
data.put("error", errorToJSON(error));
}
}
catch (JSONException e) {
e.printStackTrace();
}
ctx.sendPluginResult(new PluginResult(Status.OK, data));
}
});
}
public void fetchProducts(CordovaArgs args, final CallbackContext ctx) {
JSONArray array = args.optJSONArray(0);
if (array == null) {
ctx.sendPluginResult(new PluginResult(Status.INVALID_ACTION, "Invalid argument"));
return;
}
ArrayList<String> productIds = new ArrayList<String>();
for (int i = 0; i < array.length(); ++i) {
productIds.add(array.optString(i, "empty"));
}
service.fetchProducts(productIds, new InAppService.FetchCallback() {
@Override
public void onComplete(final List<InAppProduct> products, Error error) {
if (error != null) {
ctx.sendPluginResult(new PluginResult(Status.ERROR, errorToJSON(error)));
}
else {
ctx.sendPluginResult(new PluginResult(Status.OK, productsToJSON(products)));
}
}
});
}
public void consume(CordovaArgs args, final CallbackContext ctx) {
String productId = args.optString(0);
if (productId == null) {
ctx.sendPluginResult(new PluginResult(Status.ERROR, "Invalid argument"));
return;
}
int quantity = args.optInt(1);
if (quantity < 1) {
quantity = 1;
}
service.consume(productId, quantity, new InAppService.ConsumeCallback() {
@Override
public void onComplete(int consumed, Error error) {
if (error != null) {
ctx.sendPluginResult(new PluginResult(Status.ERROR, errorToJSON(error)));
}
else {
ctx.sendPluginResult(new PluginResult(Status.OK, consumed));
}
}
});
}
public void setValidationHandler(CordovaArgs args, final CallbackContext ctx) {
boolean noValidation = args.optBoolean(0);
if (noValidation) {
service.setValidationHandler(null);
return;
}
service.setValidationHandler(new InAppService.ValidationHandler() {
@Override
public void onValidate(String receipt, String productId, ValidationCompletion completion) {
int completionId = validationIndex++;
validationCompletions.put(completionId, completion);
JSONArray array = new JSONArray();
array.put(receipt);
array.put(productId);
array.put(completionId);
PluginResult result = new PluginResult(Status.OK, array);
result.setKeepCallback(true);
ctx.sendPluginResult(result);
}
});
}
private PluginResult executeEnableWifi(JSONArray inputs, CallbackContext callbackContext) {
Log.w(LOGTAG, "executeEnableWifi");
Context context = cordova.getActivity().getApplicationContext();
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
boolean toEnable = true;
try {
toEnable = inputs.getBoolean( 0 );
} catch (JSONException e) {
Log.w(LOGTAG, String.format("Got JSON Exception: %s", e.getMessage()));
return new PluginResult(Status.JSON_EXCEPTION);
}
wifiManager.setWifiEnabled( toEnable );
callbackContext.success();
return null;
}
@Override
public void execute(String rawArgs, CallbackContext ctx) {
try {
String args = new JSONArray(rawArgs).getString(0);
Connection conn = _map.get(Integer.parseInt(args.substring(0, 8), 16));
if (conn != null) {
if (args.charAt(8) == '1') {
byte[] binary = Base64.decode(args.substring(args.indexOf(',') + 1), Base64.NO_WRAP);
conn.sendMessage(binary, 0, binary.length);
} else {
conn.sendMessage(args.substring(9));
}
} else {
}
} catch (Exception e) {
if (!ctx.isFinished()) {
PluginResult result = new PluginResult(Status.ERROR);
result.setKeepCallback(true);
ctx.sendPluginResult(result);
}
}
}
@Override
public void execute(String rawArgs, CallbackContext ctx) {
try {
JSONArray args = new JSONArray(rawArgs);
int id = Integer.parseInt(args.getString(0), 16);
int code = args.getInt(1);
String reason = args.getString(2);
Connection conn = _map.get(id);
if (conn != null) {
if (code > 0) {
conn.close(code, reason);
} else {
conn.close();
}
}
} catch (Exception e) {
if (!ctx.isFinished()) {
PluginResult result = new PluginResult(Status.ERROR);
result.setKeepCallback(true);
ctx.sendPluginResult(result);
}
}
}
private void acceptCallInvite(JSONArray arguments, final CallbackContext callbackContext) {
if (mCallInvite == null) {
callbackContext.sendPluginResult(new PluginResult(
PluginResult.Status.ERROR));
return;
}
cordova.getThreadPool().execute(new Runnable() {
public void run() {
mCallInvite.accept(cordova.getActivity(), mCallListener);
callbackContext.success();
}
});
}
private void rejectCallInvite(JSONArray arguments, final CallbackContext callbackContext) {
if (mCallInvite == null) {
callbackContext.sendPluginResult(new PluginResult(
PluginResult.Status.ERROR));
return;
}
cordova.getThreadPool().execute(new Runnable() {
public void run() {
mCallInvite.reject(cordova.getActivity());
callbackContext.success();
}
});
}
private void disconnect(JSONArray arguments, final CallbackContext callbackContext) {
if (mCall == null) {
callbackContext.sendPluginResult(new PluginResult(
PluginResult.Status.ERROR));
return;
}
cordova.getThreadPool().execute(new Runnable() {
public void run() {
mCall.disconnect();
callbackContext.success();
}
});
}
private void sendDigits(final JSONArray arguments,
final CallbackContext callbackContext) {
if (arguments == null || arguments.length() < 1 || mCall == null) {
callbackContext.sendPluginResult(new PluginResult(
PluginResult.Status.ERROR));
return;
}
cordova.getThreadPool().execute(new Runnable() {
public void run() {
mCall.sendDigits(arguments.optString(0));
callbackContext.success();
}
});
}
private void muteCall(final CallbackContext callbackContext) {
if (mCall == null) {
callbackContext.sendPluginResult(new PluginResult(
PluginResult.Status.ERROR));
return;
}
cordova.getThreadPool().execute(new Runnable() {
public void run() {
mCall.mute(true);
callbackContext.success();
}
});
}
private void unmuteCall(final CallbackContext callbackContext) {
if (mCall == null) {
callbackContext.sendPluginResult(new PluginResult(
PluginResult.Status.ERROR));
return;
}
cordova.getThreadPool().execute(new Runnable() {
public void run() {
mCall.mute(false);
callbackContext.success();
}
});
}
private void isCallMuted(CallbackContext callbackContext) {
if (mCall == null) {
callbackContext.sendPluginResult(new PluginResult(
PluginResult.Status.OK, false));
return;
}
PluginResult result = new PluginResult(PluginResult.Status.OK, mCall.isMuted());
callbackContext.sendPluginResult(result);
}
private void callStatus(CallbackContext callbackContext) {
if (mCall == null) {
callbackContext.sendPluginResult(new PluginResult(
PluginResult.Status.ERROR));
return;
}
String state = getCallState(mCall.getState());
if (state == null) {
state = "";
}
PluginResult result = new PluginResult(PluginResult.Status.OK, state);
callbackContext.sendPluginResult(result);
}
private void javascriptErrorback(int errorCode, String errorMessage, CallbackContext callbackContext) {
JSONObject object = new JSONObject();
try {
object.putOpt("message", errorMessage);
} catch (JSONException e) {
callbackContext.sendPluginResult(new PluginResult(
PluginResult.Status.JSON_EXCEPTION));
return;
}
PluginResult result = new PluginResult(Status.ERROR, object);
result.setKeepCallback(true);
callbackContext.sendPluginResult(result);
}
public void onRequestPermissionResult(int requestCode, String[] permissions,
int[] grantResults) throws JSONException {
for (int r : grantResults) {
if (r == PackageManager.PERMISSION_DENIED) {
mInitCallbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, "Permission denied"));
return;
}
}
}
/**
* Helper function that logs the error and then calls the error callback.
*/
public static PluginResult getPluginResultError(final String from, final Throwable e) {
String message = String.format("[%s] Error: %s", from, e.getMessage());
LOG.e(Constants.LOG_TAG, message, e);
return new PluginResult(Status.ERROR, message);
}
/**
* Send plugin result.
*
* @param callbackString
* @param keepCallback
*/
private void sendCallback(String callbackString, boolean keepCallback) {
if (!_ctx.isFinished()) {
PluginResult result = new PluginResult(Status.OK, callbackString);
result.setKeepCallback(keepCallback);
_ctx.sendPluginResult(result);
}
}
/**
* Send plugin result.
*
* @param callbackString
* @param keepCallback
*/
private void sendCallback(String callbackString, boolean keepCallback) {
if (!_ctx.isFinished()) {
PluginResult result = new PluginResult(Status.OK, callbackString);
result.setKeepCallback(keepCallback);
_ctx.sendPluginResult(result);
}
}
public void productforId(CordovaArgs args, CallbackContext ctx) {
String productId = args.optString(0);
InAppProduct product = null;
if (productId != null) {
product = service.productForId(productId);
}
if (product!= null) {
ctx.sendPluginResult(new PluginResult(Status.OK, product.toJSON()));
}
else {
ctx.sendPluginResult(new PluginResult(Status.OK));
}
}
public void restorePurchases(CordovaArgs args, final CallbackContext ctx) {
service.restorePurchases(new InAppService.RestoreCallback() {
@Override
public void onComplete(Error error) {
if (error != null) {
ctx.sendPluginResult(new PluginResult(Status.ERROR, errorToJSON(error)));
}
else {
ctx.sendPluginResult(new PluginResult(Status.OK));
}
}
});
}
@Override
public void onPurchaseStart(InAppService sender, String productId) {
if (listenerCtx != null) {
JSONArray data = new JSONArray();
data.put("start");
data.put(productId);
PluginResult result = new PluginResult(Status.OK, data);
result.setKeepCallback(true);
listenerCtx.sendPluginResult(result);
}
}
@Override
public void onPurchaseFail(InAppService sender, String productId, Error error) {
if (listenerCtx != null) {
JSONArray data = new JSONArray();
data.put("error");
data.put(productId);
data.put(this.errorToJSON(error));
PluginResult result = new PluginResult(Status.OK, data);
result.setKeepCallback(true);
listenerCtx.sendPluginResult(result);
}
}
@Override
public void onPurchaseComplete(InAppService sender, InAppPurchase purchase){
if (listenerCtx != null) {
JSONArray data = new JSONArray();
data.put("complete");
data.put(purchase.toJSON());
PluginResult result = new PluginResult(Status.OK, data);
result.setKeepCallback(true);
listenerCtx.sendPluginResult(result);
}
}
private PluginResult executeConnectWifi(JSONArray inputs, CallbackContext callbackContext) {
Log.w(LOGTAG, "executeConnectWifi");
boolean toEnable = true;
try {
toEnable = inputs.getBoolean( 0 );
} catch (JSONException e) {
Log.w(LOGTAG, String.format("Got JSON Exception: %s", e.getMessage()));
return new PluginResult(Status.JSON_EXCEPTION);
}
return null;
}
private PluginResult executeEnableWifiAP(JSONArray inputs, CallbackContext callbackContext) {
Log.w(LOGTAG, "executeEnableWifiAP");
boolean toEnable = true;
try {
toEnable = inputs.getBoolean( 0 );
} catch (JSONException e) {
Log.w(LOGTAG, String.format("Got JSON Exception: %s", e.getMessage()));
return new PluginResult(Status.JSON_EXCEPTION);
}
return null;
}
private PluginResult executeEnableWifiLock(JSONArray inputs, CallbackContext callbackContext) {
Log.w(LOGTAG, "executeEnableWifiLock");
boolean toEnable = true;
try {
toEnable = inputs.getBoolean( 0 );
} catch (JSONException e) {
Log.w(LOGTAG, String.format("Got JSON Exception: %s", e.getMessage()));
return new PluginResult(Status.JSON_EXCEPTION);
}
Context context = cordova.getActivity().getApplicationContext();
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
if(wifiLock == null) {
wifiLock = wifiManager.createWifiLock("Test");
}
if(wifiLock != null) {
if(toEnable) {
wifiLock.acquire();
} else {
if(wifiLock.isHeld()) {
wifiLock.release();
}
}
}
callbackContext.success();
return null;
}