CallbackContext.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 org.json.JSONArray;
  19. import org.apache.cordova.CordovaWebView;
  20. import org.apache.cordova.PluginResult;
  21. import org.json.JSONObject;
  22. public class CallbackContext {
  23. private static final String LOG_TAG = "CordovaPlugin";
  24. private String callbackId;
  25. private CordovaWebView webView;
  26. protected boolean finished;
  27. private int changingThreads;
  28. public CallbackContext(String callbackId, CordovaWebView webView) {
  29. this.callbackId = callbackId;
  30. this.webView = webView;
  31. }
  32. public boolean isFinished() {
  33. return finished;
  34. }
  35. public boolean isChangingThreads() {
  36. return changingThreads > 0;
  37. }
  38. public String getCallbackId() {
  39. return callbackId;
  40. }
  41. public void sendPluginResult(PluginResult pluginResult) {
  42. synchronized (this) {
  43. if (finished) {
  44. LOG.w(LOG_TAG, "Attempted to send a second callback for ID: " + callbackId + "\nResult was: " + pluginResult.getMessage());
  45. return;
  46. } else {
  47. finished = !pluginResult.getKeepCallback();
  48. }
  49. }
  50. webView.sendPluginResult(pluginResult, callbackId);
  51. }
  52. /**
  53. * Helper for success callbacks that just returns the Status.OK by default
  54. *
  55. * @param message The message to add to the success result.
  56. */
  57. public void success(JSONObject message) {
  58. sendPluginResult(new PluginResult(PluginResult.Status.OK, message));
  59. }
  60. /**
  61. * Helper for success callbacks that just returns the Status.OK by default
  62. *
  63. * @param message The message to add to the success result.
  64. */
  65. public void success(String message) {
  66. sendPluginResult(new PluginResult(PluginResult.Status.OK, message));
  67. }
  68. /**
  69. * Helper for success callbacks that just returns the Status.OK by default
  70. *
  71. * @param message The message to add to the success result.
  72. */
  73. public void success(JSONArray message) {
  74. sendPluginResult(new PluginResult(PluginResult.Status.OK, message));
  75. }
  76. /**
  77. * Helper for success callbacks that just returns the Status.OK by default
  78. *
  79. * @param message The message to add to the success result.
  80. */
  81. public void success(byte[] message) {
  82. sendPluginResult(new PluginResult(PluginResult.Status.OK, message));
  83. }
  84. /**
  85. * Helper for success callbacks that just returns the Status.OK by default
  86. *
  87. * @param message The message to add to the success result.
  88. */
  89. public void success(int message) {
  90. sendPluginResult(new PluginResult(PluginResult.Status.OK, message));
  91. }
  92. /**
  93. * Helper for success callbacks that just returns the Status.OK by default
  94. */
  95. public void success() {
  96. sendPluginResult(new PluginResult(PluginResult.Status.OK));
  97. }
  98. /**
  99. * Helper for error callbacks that just returns the Status.ERROR by default
  100. *
  101. * @param message The message to add to the error result.
  102. */
  103. public void error(JSONObject message) {
  104. sendPluginResult(new PluginResult(PluginResult.Status.ERROR, message));
  105. }
  106. /**
  107. * Helper for error callbacks that just returns the Status.ERROR by default
  108. *
  109. * @param message The message to add to the error result.
  110. */
  111. public void error(String message) {
  112. sendPluginResult(new PluginResult(PluginResult.Status.ERROR, message));
  113. }
  114. /**
  115. * Helper for error callbacks that just returns the Status.ERROR by default
  116. *
  117. * @param message The message to add to the error result.
  118. */
  119. public void error(int message) {
  120. sendPluginResult(new PluginResult(PluginResult.Status.ERROR, message));
  121. }
  122. }