Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

PluginResult.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. Licensed to the Apache Software Foundation (ASF) under one
  3. or more contributor license agreements. See the NOTICE file
  4. distributed with this work for additional information
  5. regarding copyright ownership. The ASF licenses this file
  6. to you under the Apache License, Version 2.0 (the
  7. "License"); you may not use this file except in compliance
  8. with the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing,
  11. software distributed under the License is distributed on an
  12. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. KIND, either express or implied. See the License for the
  14. specific language governing permissions and limitations
  15. under the License.
  16. */
  17. package org.apache.cordova;
  18. import java.util.List;
  19. import org.json.JSONArray;
  20. import org.json.JSONObject;
  21. import android.util.Base64;
  22. public class PluginResult {
  23. private final int status;
  24. private final int messageType;
  25. private boolean keepCallback = false;
  26. private String strMessage;
  27. private String encodedMessage;
  28. private List<PluginResult> multipartMessages;
  29. public PluginResult(Status status) {
  30. this(status, PluginResult.StatusMessages[status.ordinal()]);
  31. }
  32. public PluginResult(Status status, String message) {
  33. this.status = status.ordinal();
  34. this.messageType = message == null ? MESSAGE_TYPE_NULL : MESSAGE_TYPE_STRING;
  35. this.strMessage = message;
  36. }
  37. public PluginResult(Status status, JSONArray message) {
  38. this.status = status.ordinal();
  39. this.messageType = MESSAGE_TYPE_JSON;
  40. encodedMessage = message.toString();
  41. }
  42. public PluginResult(Status status, JSONObject message) {
  43. this.status = status.ordinal();
  44. this.messageType = MESSAGE_TYPE_JSON;
  45. encodedMessage = message.toString();
  46. }
  47. public PluginResult(Status status, int i) {
  48. this.status = status.ordinal();
  49. this.messageType = MESSAGE_TYPE_NUMBER;
  50. this.encodedMessage = ""+i;
  51. }
  52. public PluginResult(Status status, float f) {
  53. this.status = status.ordinal();
  54. this.messageType = MESSAGE_TYPE_NUMBER;
  55. this.encodedMessage = ""+f;
  56. }
  57. public PluginResult(Status status, boolean b) {
  58. this.status = status.ordinal();
  59. this.messageType = MESSAGE_TYPE_BOOLEAN;
  60. this.encodedMessage = Boolean.toString(b);
  61. }
  62. public PluginResult(Status status, byte[] data) {
  63. this(status, data, false);
  64. }
  65. public PluginResult(Status status, byte[] data, boolean binaryString) {
  66. this.status = status.ordinal();
  67. this.messageType = binaryString ? MESSAGE_TYPE_BINARYSTRING : MESSAGE_TYPE_ARRAYBUFFER;
  68. this.encodedMessage = Base64.encodeToString(data, Base64.NO_WRAP);
  69. }
  70. // The keepCallback and status of multipartMessages are ignored.
  71. public PluginResult(Status status, List<PluginResult> multipartMessages) {
  72. this.status = status.ordinal();
  73. this.messageType = MESSAGE_TYPE_MULTIPART;
  74. this.multipartMessages = multipartMessages;
  75. }
  76. public void setKeepCallback(boolean b) {
  77. this.keepCallback = b;
  78. }
  79. public int getStatus() {
  80. return status;
  81. }
  82. public int getMessageType() {
  83. return messageType;
  84. }
  85. public String getMessage() {
  86. if (encodedMessage == null) {
  87. encodedMessage = JSONObject.quote(strMessage);
  88. }
  89. return encodedMessage;
  90. }
  91. public int getMultipartMessagesSize() {
  92. return multipartMessages.size();
  93. }
  94. public PluginResult getMultipartMessage(int index) {
  95. return multipartMessages.get(index);
  96. }
  97. /**
  98. * If messageType == MESSAGE_TYPE_STRING, then returns the message string.
  99. * Otherwise, returns null.
  100. */
  101. public String getStrMessage() {
  102. return strMessage;
  103. }
  104. public boolean getKeepCallback() {
  105. return this.keepCallback;
  106. }
  107. @Deprecated // Use sendPluginResult instead of sendJavascript.
  108. public String getJSONString() {
  109. return "{\"status\":" + this.status + ",\"message\":" + this.getMessage() + ",\"keepCallback\":" + this.keepCallback + "}";
  110. }
  111. @Deprecated // Use sendPluginResult instead of sendJavascript.
  112. public String toCallbackString(String callbackId) {
  113. // If no result to be sent and keeping callback, then no need to sent back to JavaScript
  114. if ((status == PluginResult.Status.NO_RESULT.ordinal()) && keepCallback) {
  115. return null;
  116. }
  117. // Check the success (OK, NO_RESULT & !KEEP_CALLBACK)
  118. if ((status == PluginResult.Status.OK.ordinal()) || (status == PluginResult.Status.NO_RESULT.ordinal())) {
  119. return toSuccessCallbackString(callbackId);
  120. }
  121. return toErrorCallbackString(callbackId);
  122. }
  123. @Deprecated // Use sendPluginResult instead of sendJavascript.
  124. public String toSuccessCallbackString(String callbackId) {
  125. return "cordova.callbackSuccess('"+callbackId+"',"+this.getJSONString()+");";
  126. }
  127. @Deprecated // Use sendPluginResult instead of sendJavascript.
  128. public String toErrorCallbackString(String callbackId) {
  129. return "cordova.callbackError('"+callbackId+"', " + this.getJSONString()+ ");";
  130. }
  131. public static final int MESSAGE_TYPE_STRING = 1;
  132. public static final int MESSAGE_TYPE_JSON = 2;
  133. public static final int MESSAGE_TYPE_NUMBER = 3;
  134. public static final int MESSAGE_TYPE_BOOLEAN = 4;
  135. public static final int MESSAGE_TYPE_NULL = 5;
  136. public static final int MESSAGE_TYPE_ARRAYBUFFER = 6;
  137. // Use BINARYSTRING when your string may contain null characters.
  138. // This is required to work around a bug in the platform :(.
  139. public static final int MESSAGE_TYPE_BINARYSTRING = 7;
  140. public static final int MESSAGE_TYPE_MULTIPART = 8;
  141. public static String[] StatusMessages = new String[] {
  142. "No result",
  143. "OK",
  144. "Class not found",
  145. "Illegal access",
  146. "Instantiation error",
  147. "Malformed url",
  148. "IO error",
  149. "Invalid action",
  150. "JSON error",
  151. "Error"
  152. };
  153. public enum Status {
  154. NO_RESULT,
  155. OK,
  156. CLASS_NOT_FOUND_EXCEPTION,
  157. ILLEGAL_ACCESS_EXCEPTION,
  158. INSTANTIATION_EXCEPTION,
  159. MALFORMED_URL_EXCEPTION,
  160. IO_EXCEPTION,
  161. INVALID_ACTION,
  162. JSON_EXCEPTION,
  163. ERROR
  164. }
  165. }