ResumeCallback.java 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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.JSONException;
  19. import org.json.JSONObject;
  20. import java.util.ArrayList;
  21. import java.util.List;
  22. public class ResumeCallback extends CallbackContext {
  23. private final String TAG = "CordovaResumeCallback";
  24. private String serviceName;
  25. private PluginManager pluginManager;
  26. public ResumeCallback(String serviceName, PluginManager pluginManager) {
  27. super("resumecallback", null);
  28. this.serviceName = serviceName;
  29. this.pluginManager = pluginManager;
  30. }
  31. @Override
  32. public void sendPluginResult(PluginResult pluginResult) {
  33. synchronized (this) {
  34. if (finished) {
  35. LOG.w(TAG, serviceName + " attempted to send a second callback to ResumeCallback\nResult was: " + pluginResult.getMessage());
  36. return;
  37. } else {
  38. finished = true;
  39. }
  40. }
  41. JSONObject event = new JSONObject();
  42. JSONObject pluginResultObject = new JSONObject();
  43. try {
  44. pluginResultObject.put("pluginServiceName", this.serviceName);
  45. pluginResultObject.put("pluginStatus", PluginResult.StatusMessages[pluginResult.getStatus()]);
  46. event.put("action", "resume");
  47. event.put("pendingResult", pluginResultObject);
  48. } catch (JSONException e) {
  49. LOG.e(TAG, "Unable to create resume object for Activity Result");
  50. }
  51. PluginResult eventResult = new PluginResult(PluginResult.Status.OK, event);
  52. // We send a list of results to the js so that we don't have to decode
  53. // the PluginResult passed to this CallbackContext into JSON twice.
  54. // The results are combined into an event payload before the event is
  55. // fired on the js side of things (see platform.js)
  56. List<PluginResult> result = new ArrayList<PluginResult>();
  57. result.add(eventResult);
  58. result.add(pluginResult);
  59. CoreAndroid appPlugin = (CoreAndroid) pluginManager.getPlugin(CoreAndroid.PLUGIN_NAME);
  60. appPlugin.sendResumeEvent(new PluginResult(PluginResult.Status.OK, result));
  61. }
  62. }