Нет описания

EmailComposer.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. Copyright 2013-2016 appPlant UG
  3. Licensed to the Apache Software Foundation (ASF) under one
  4. or more contributor license agreements. See the NOTICE file
  5. distributed with this work for additional information
  6. regarding copyright ownership. The ASF licenses this file
  7. to you under the Apache License, Version 2.0 (the
  8. "License"); you may not use this file except in compliance
  9. with the License. You may obtain a copy of the License at
  10. http://www.apache.org/licenses/LICENSE-2.0
  11. Unless required by applicable law or agreed to in writing,
  12. software distributed under the License is distributed on an
  13. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. KIND, either express or implied. See the License for the
  15. specific language governing permissions and limitations
  16. under the License.
  17. */
  18. package de.martinreinhardt.cordova.plugins.email;
  19. import android.content.Context;
  20. import android.content.Intent;
  21. import org.apache.cordova.CallbackContext;
  22. import org.apache.cordova.CordovaInterface;
  23. import org.apache.cordova.CordovaPlugin;
  24. import org.apache.cordova.CordovaWebView;
  25. import org.apache.cordova.LOG;
  26. import org.apache.cordova.PluginResult;
  27. import org.json.JSONArray;
  28. import org.json.JSONException;
  29. import org.json.JSONObject;
  30. import java.util.ArrayList;
  31. import java.util.List;
  32. @SuppressWarnings("Convert2Diamond")
  33. public class EmailComposer extends CordovaPlugin {
  34. /**
  35. * The log tag for this plugin
  36. */
  37. static protected final String LOG_TAG = "EmailComposer";
  38. // Implementation of the plugin.
  39. private final EmailComposerImpl impl = new EmailComposerImpl();
  40. // The callback context used when calling back into JavaScript
  41. private CallbackContext command;
  42. /**
  43. * Delete externalCacheDirectory on appstart
  44. *
  45. * @param cordova Cordova-instance
  46. * @param webView CordovaWebView-instance
  47. */
  48. @Override
  49. public void initialize(CordovaInterface cordova, CordovaWebView webView) {
  50. super.initialize(cordova, webView);
  51. impl.cleanupAttachmentFolder(getContext());
  52. }
  53. /**
  54. * Executes the request.
  55. * <p>
  56. * This method is called from the WebView thread.
  57. * To do a non-trivial amount of work, use:
  58. * cordova.getThreadPool().execute(runnable);
  59. * <p>
  60. * To run on the UI thread, use:
  61. * cordova.getActivity().runOnUiThread(runnable);
  62. *
  63. * @param action The action to execute.
  64. * @param args The exec() arguments in JSON form.
  65. * @param callback The callback context used when calling
  66. * back into JavaScript.
  67. * @return Whether the action was valid.
  68. */
  69. @Override
  70. public boolean execute(String action, JSONArray args,
  71. CallbackContext callback) throws JSONException {
  72. this.command = callback;
  73. if ("open".equalsIgnoreCase(action)) {
  74. open(args);
  75. return true;
  76. }
  77. if ("isAvailable".equalsIgnoreCase(action)) {
  78. isAvailable();
  79. return true;
  80. }
  81. return false;
  82. }
  83. /**
  84. * Returns the application context.
  85. */
  86. private Context getContext() {
  87. return cordova.getActivity();
  88. }
  89. /**
  90. * Tells if the device has the capability to send emails.
  91. */
  92. private void isAvailable() {
  93. cordova.getThreadPool().execute(new Runnable() {
  94. public void run() {
  95. boolean[] available = impl.canSendMail(null, getContext());
  96. List<PluginResult> messages = new ArrayList<PluginResult>();
  97. messages.add(new PluginResult(PluginResult.Status.OK, available[0]));
  98. messages.add(new PluginResult(PluginResult.Status.OK, available[1]));
  99. PluginResult result = new PluginResult(
  100. PluginResult.Status.OK, messages);
  101. if (available[0] && available[1]) {
  102. command.success("Can send emails");
  103. } else {
  104. command.error("Can not send emails");
  105. }
  106. }
  107. });
  108. }
  109. /**
  110. * Sends an intent to the email app.
  111. *
  112. * @param args The email properties like subject or body
  113. * @throws JSONException
  114. */
  115. private void open(JSONArray args) throws JSONException {
  116. JSONObject props = args.getJSONObject(0);
  117. String appId = props.optString("app");
  118. if (!(impl.canSendMail(appId, getContext()))[0]) {
  119. LOG.i(LOG_TAG, "No client or account found for.");
  120. return;
  121. }
  122. Intent draft = impl.getDraftWithProperties(props, getContext());
  123. String header = props.optString("chooserHeader", "Open with");
  124. final Intent chooser = Intent.createChooser(draft, header);
  125. final EmailComposer plugin = this;
  126. cordova.getThreadPool().execute(new Runnable() {
  127. public void run() {
  128. cordova.startActivityForResult(plugin, chooser, 0);
  129. }
  130. });
  131. }
  132. /**
  133. * Called when an activity you launched exits, giving you the reqCode you
  134. * started it with, the resCode it returned, and any additional data from it.
  135. *
  136. * @param reqCode The request code originally supplied to startActivityForResult(),
  137. * allowing you to identify who this result came from.
  138. * @param resCode The integer result code returned by the child activity
  139. * through its setResult().
  140. * @param intent An Intent, which can return result data to the caller
  141. * (various data can be attached to Intent "extras").
  142. */
  143. @Override
  144. public void onActivityResult(int reqCode, int resCode, Intent intent) {
  145. if (command != null) {
  146. command.success();
  147. }
  148. }
  149. }