package com.SalmanDev.EmailPlus;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.text.Html;
import android.net.Uri;
import android.webkit.MimeTypeMap;
import com.google.appinventor.components.annotations.*;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.common.PropertyTypeConstants;
import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;
import com.google.appinventor.components.runtime.Form;
import com.google.appinventor.components.runtime.ComponentContainer;
import com.google.appinventor.components.runtime.EventDispatcher;
import com.google.appinventor.components.runtime.util.ErrorMessages;
import com.google.appinventor.components.runtime.util.NougatUtil;
import com.google.appinventor.components.runtime.util.YailList;
import java.util.ArrayList;
import java.util.List;
import java.io.File;
@DesignerComponent(
version = 1,
description = "Created by " + "Salman Developer",
category = ComponentCategory.EXTENSION,
nonVisible = true,
iconName = "https://img.icons8.com/external-sbts2018-blue-sbts2018/16/null/external-email-social-media-basic-1-sbts2018-blue-sbts2018.png")
@SimpleObject(external = true)
//Libraries
@UsesLibraries(libraries = "")
//Permissions
@UsesPermissions(permissionNames = "android.permission.READ_EXTERNAL_STORAGE")
public class EmailPlus extends AndroidNonvisibleComponent {
//Activity and Context
private ComponentContainer container;
private Context context;
private Activity activity;
private Form form;
private String packageName = "com.google.android.gm";
public EmailPlus(ComponentContainer container){
super(container.$form());
this.activity = container.$context();
this.context = container.$context();
this.form = container.$form();
}
@DesignerProperty(editorType = PropertyTypeConstants.PROPERTY_TYPE_STRING, defaultValue = "com.google.android.gm")
@SimpleProperty(description = "")
public void AppPackageName(String name) {
packageName = name;
}
@SimpleProperty(category = PropertyCategory.APPEARANCE, description = "")
public String AppPackageName() {
return packageName;
}
@SimpleFunction(description = "")
public void SendMail(String to, String cc, String bcc, String subject, String message, YailList attachments) {
ArrayList ListFilesUri = new ArrayList();
for(Object item : attachments.toArray()) {
String filePath = item.toString();
if (!filePath.startsWith("file://")) {
filePath = "file://" + filePath;
}
Uri uri = Uri.parse(filePath);
File file = new File(uri.getPath());
if (file.isFile()) {
Uri shareableUri = NougatUtil.getPackageUri(form, file);
ListFilesUri.add(shareableUri);
}
}
try {
Intent shareIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
shareIntent.setPackage(packageName);
shareIntent.putExtra(Intent.EXTRA_EMAIL, new String[] {to});
shareIntent.putExtra(Intent.EXTRA_CC, new String[] {cc});
shareIntent.putExtra(Intent.EXTRA_BCC, new String[] {bcc});
shareIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
shareIntent.putExtra(Intent.EXTRA_TEXT, message);
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, ListFilesUri);
shareIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
shareIntent.setType("*/*");
form.startActivity(shareIntent);
} catch (Exception e) {
if (e.getMessage().contains("No Activity found to handle Intent")){
AppNotAvailable();
}
}
}
@SimpleEvent(description = "Event is triggered when the email app is not available.")
public void AppNotAvailable(){
EventDispatcher.dispatchEvent(this, "AppNotAvailable");
}
}