package com.mattprecious.telescope;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.support.annotation.WorkerThread;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Set;

/**
 * <p>
 * A basic {@link Lens} implementation that composes an email with the provided addresses and
 * subject (optional).
 * </p>
 *
 * <p>The {@link #getBody()} method can be overridden to pre-populate the body of the email.</p>
 */
public class EmailLens extends Lens {
  private final Context context;
  private final String subject;
  private final String[] addresses;

  /** @deprecated Use {@link #EmailLens(Context, String, String...)}. */
  @Deprecated
  public EmailLens(Context context, String[] addresses, String subject) {
    this(context, subject, addresses);
  }

  public EmailLens(Context context, String subject, String... addresses) {
    this.context = context;
    this.addresses = addresses == null ? null : addresses.clone();
    this.subject = subject;
  }

  /** Create the email body. */
  @WorkerThread protected String getBody() {
    return null;
  }

  @Override public void onCapture(File screenshot) {
    new CreateIntentTask(context, screenshot).execute();
  }

  @WorkerThread protected Set<Uri> getAdditionalAttachments() {
    return Collections.emptySet();
  }

  private final class CreateIntentTask extends AsyncTask<Void, Void, Intent> {
    private final Context context;
    private final File screenshot;

    CreateIntentTask(Context context, File screenshot) {
      this.context = context;
      this.screenshot = screenshot;
    }

    @Override protected Intent doInBackground(Void... params) {
      Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
      intent.setType("message/rfc822");

      if (subject != null) {
        intent.putExtra(Intent.EXTRA_SUBJECT, subject);
      }

      if (addresses != null) {
        intent.putExtra(Intent.EXTRA_EMAIL, addresses);
      }

      String body = getBody();
      if (body != null) {
        intent.putExtra(Intent.EXTRA_TEXT, body);
      }

      Set<Uri> additionalAttachments = getAdditionalAttachments();
      ArrayList<Uri> attachments = new ArrayList<>(additionalAttachments.size() + 1 /* screen */);
      if (!additionalAttachments.isEmpty()) {
        attachments.addAll(additionalAttachments);
      }
      if (screenshot != null) {
        attachments.add(TelescopeFileProvider.getUriForFile(context, screenshot));
      }

      if (!attachments.isEmpty()) {
        intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, attachments);
      }

      return intent;
    }

    @Override protected void onPostExecute(Intent intent) {
      context.startActivity(intent);
    }
  }
}

@@ -30,16 +30,66 @@
 scope;%0A%0A
+import android.content.ActivityNotFoundException;%0A
 import a
@@ -244,16 +244,45 @@
 Thread;%0A
+import android.widget.Toast;%0A
 import j
@@ -2837,24 +2837,38 @@
 t intent) %7B%0A
+      try %7B%0A  
       contex
@@ -2888,24 +2888,192 @@
 ty(intent);%0A
+      %7D catch (ActivityNotFoundException e) %7B%0A        Toast.makeText(context, %22%5CuD83D%5CuDD2D No email apps installed!%22, Toast.LENGTH_SHORT)%0A            .show();%0A      %7D%0A
     %7D%0A  %7D%0A%7D%0A

Fail silently if the device lacks an email app.