【Cordova开发】cordova之PluginResult.java源码

原创 2017年01月03日 15:25:10
/*
 * 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.
 */
public class PluginResult {
 
 
    private final int status;
    private final 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;
     
}
 
    public int getStatus() {
 
        return status;
     
}
 
    public String getMessage() {
 
        return message;
     
}
 
    public void setKeepCallback(boolean b) {
 
        this.keepCallback = b;
     
}
 
    public boolean getKeepCallback() {
 
        return this.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.
     */
    public static class Status
    {
 
        private int val;
        private String message;
 
        protected Status(int val, String message) {
 
            this.val = val;
            this.message = message;
         
}
 
        public int ordinal() {
 
            return this.val;
         
}
 
        public String getMessage() {
 
            return this.message;
         
}
 
        public static final Status NO_RESULT = new Status(0, "No result");
        public static final Status OK = new Status(1, "OK");
        public static final Status CLASS_NOT_FOUND_EXCEPTION = new Status(2, "Class not found");
        public static final Status ILLEGAL_ACCESS_EXCEPTION = new Status(3, "Illegal access");
        public static final Status INSTANTIATION_EXCEPTION = new Status(4, "Instantiation error");
        public static final Status MALFORMED_URL_EXCEPTION = new Status(5, "Malformed URL");
        public static final Status IO_EXCEPTION = new Status(6, "IO error");
        public static final Status INVALID_ACTION = new Status(7, "Invalid action");
        public static final Status JSON_EXCEPTION = new Status(8, "JSON error");
        public static final Status ERROR = new Status(9, "Error");
     
}
 
}
版权声明:本文为博主原创文章,未经博主允许不得转载。

Cordova Android源码分析系列一(项目总览和CordovaActivity分析)

PhoneGap/Cordova是一个专业的移动应用开发框架,是一个全面的WEB APP开发的框架,提供了以WEB形式来访问终端设备的API的功能。这对于采用WEB APP进行开发者来说是个福音,这可...
  • offbye
  • offbye
  • 2014年06月17日 16:50
  • 5980

cordova开发自定义插件

使用cordova自定义插件
  • cool_fuwei
  • cool_fuwei
  • 2016年04月14日 17:32
  • 9546

android cordova混合开发(交互部分)

最近公司需要混合开发一个app,先讲个基础需求,需求是这样的:js脚本调用本地的相机,拍照完成后,上传服务器然后再通知js,,OK,这就涉及到2个问题: 1.js调用android本地端method...
  • u010819959
  • u010819959
  • 2016年01月29日 17:56
  • 7468

cordova开发介绍(android平台)

本文主要介绍android平台下cordova的开发环境,包含其各配置文件,plugin的开发等等,并且提供一个简单示例以供参考。...
  • sproll
  • sproll
  • 2016年03月16日 12:44
  • 7299

android 框架之Cordova 集成开发过程

1.cordova简介: Apache Cordova是一个开源的移动开发框架。它允许您使用标准web技术——HTML5,CSS3和JavaScript进行跨平台开发。应用程序内执行包装针...
  • qq_26337701
  • qq_26337701
  • 2016年08月20日 18:17
  • 748

创建一个Cordova完整应用

本文承接上篇《创建Cordova插件》,通过实现一个简单的应用作为这个Cordova初级系列的结束。 前边对Cordova编程已经讲了不少了,还没有拿真实应用为例完整的演练一遍构建过程。这里将用一个...
  • luopeiyuan1990
  • luopeiyuan1990
  • 2016年03月06日 09:40
  • 1709

cordova-android源代码分析

最近研究了一下cordova的源码,以下是从初始化到载入页面的部分。 从代码看,主activity是下面的MainActivity: 如上图。可以看出此类继承自CordovaActivit...
  • u014359108
  • u014359108
  • 2015年07月06日 15:36
  • 1454

cordova 混合开发

cordova也叫phonegap,可以用来打包网页生成android或ios或其他平台的APP,它提供了一些native api,可以让网页调用本地函数,极大地增强了网页的功能。 cordova的...
  • crazyman2010
  • crazyman2010
  • 2015年06月30日 13:39
  • 13337

跨平台移动开发phonegap/cordova 3.3全系列教程-helloworld

1.    建立专案(cordova) 打开cmd命令行 cordova create ACESMobile aces.mobile ACES cd acesmobile 如图 2.    安装...
  • mansai
  • mansai
  • 2014年02月06日 15:54
  • 47590

Android Cordova 插件开发之编写自定义插件

前言本文适合Android+web的复合型人才,因为cordova本身就是混合开发,所以在Android开发的基础上,还要懂web相关技术(HTML+CSS+JS),但是也有例外,比如我,只需负责An...
  • a631855639
  • a631855639
  • 2016年03月03日 18:48
  • 15727
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:【Cordova开发】cordova之PluginResult.java源码
举报原因:
原因补充:

(最多只允许输入30个字)