/* Copyright 2013 appPlant UG 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 de.appplant.cordova.plugin.emailcomposer; import java.io.File; import java.util.ArrayList; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.content.Intent; import android.net.Uri; import android.text.Html; import org.apache.cordova.CordovaPlugin; import org.apache.cordova.CallbackContext; import org.apache.cordova.PluginResult; public class EmailComposer extends CordovaPlugin { @Override public boolean execute (String action, JSONArray args, CallbackContext callbackContext) throws JSONException { // Eine E-Mail soll versendet werden if ("open".equals(action)) { open(args, callbackContext); return true; } // Es soll überprüft werden, ob ein Dienst zum Versenden der E-Mail zur Verfügung steht if ("isServiceAvailable".equals(action)) { isServiceAvailable(callbackContext); return true; } // Returning false results in a "MethodNotFound" error. return false; } /** * Überprüft, ob Emails versendet werden können. */ private void isServiceAvailable (CallbackContext ctx) { Boolean available = isEmailAccountConfigured(); PluginResult result = new PluginResult(PluginResult.Status.OK, available); ctx.sendPluginResult(result); } /** * Öffnet den Email-Kontroller mit vorausgefüllten Daten. */ private void open (JSONArray args, CallbackContext ctx) throws JSONException { JSONObject properties = args.getJSONObject(0); Intent draft = getDraftWithProperties(properties); openDraft(draft); } /** * Erstellt den ViewController für Mails und fügt die übergebenen Eigenschaften ein. * * @param {JSONObject} params (Subject, Body, Recipients, ...) */ private Intent getDraftWithProperties (JSONObject params) throws JSONException { Intent mail = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE); if (params.has("subject")) setSubject(params.getString("subject"), mail); if (params.has("body")) setBody(params.getString("body"), params.optBoolean("isHtml"), mail); if (params.has("to")) setRecipients(params.getJSONArray("to"), mail); if (params.has("cc")) setCcRecipients(params.getJSONArray("cc"), mail); if (params.has("bcc")) setBccRecipients(params.getJSONArray("bcc"), mail); if (params.has("attachments")) setAttachments(params.getJSONArray("attachments"), mail); mail.setType("application/octet-stream"); return mail; } /** * Zeigt den ViewController zum Versenden/Bearbeiten der Mail an. */ private void openDraft (final Intent draft) { final EmailComposer plugin = this; this.cordova.getActivity().runOnUiThread(new Runnable() { public void run() { plugin.cordova.startActivityForResult(plugin, draft, 0); } }); } /** * Setzt den Subject der Mail. */ private void setSubject (String subject, Intent draft) { draft.putExtra(android.content.Intent.EXTRA_SUBJECT, subject); } /** * Setzt den Body der Mail. */ private void setBody (String body, Boolean isHTML, Intent draft) { if (isHTML) { draft.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml(body)); draft.setType("text/html"); } else { draft.putExtra(android.content.Intent.EXTRA_TEXT, body); draft.setType("text/plain"); } } /** * Setzt die Empfänger der Mail. */ private void setRecipients (JSONArray recipients, Intent draft) throws JSONException { String[] receivers = new String[recipients.length()]; for (int i = 0; i < recipients.length(); i++) { receivers[i] = recipients.getString(i); } draft.putExtra(android.content.Intent.EXTRA_EMAIL, receivers); } /** * Setzt die CC-Empfänger der Mail. */ private void setCcRecipients (JSONArray ccRecipients, Intent draft) throws JSONException { String[] receivers = new String[ccRecipients.length()]; for (int i = 0; i < ccRecipients.length(); i++) { receivers[i] = ccRecipients.getString(i); } draft.putExtra(android.content.Intent.EXTRA_CC, receivers); } /** * Setzt die BCC-Empfänger der Mail. */ private void setBccRecipients (JSONArray bccRecipients, Intent draft) throws JSONException { String[] receivers = new String[bccRecipients.length()]; for (int i = 0; i < bccRecipients.length(); i++) { receivers[i] = bccRecipients.getString(i); } draft.putExtra(android.content.Intent.EXTRA_BCC, receivers); } /** * Fügt die Anhände zur Mail hinzu. */ private void setAttachments (JSONArray attachments, Intent draft) throws JSONException { ArrayList<Uri> attachmentUris = new ArrayList<Uri>(); for (int i = 0; i < attachments.length(); i++) { Uri attachmentUri = Uri.parse(attachments.getString(i)); File file = new File(attachmentUri.getPath()); if (file.exists()) { attachmentUris.add(attachmentUri); } } draft.putParcelableArrayListExtra(Intent.EXTRA_STREAM, attachmentUris); } /** * Gibt an, ob es eine Anwendung gibt, welche E-Mails versenden kann. */ private Boolean isEmailAccountConfigured () { Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto","max@mustermann.com", null)); Boolean available = cordova.getActivity().getPackageManager().queryIntentActivities(intent, 0).size() > 1; return available; } }