package com.mattprecious.telescope;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
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. */
  protected String getBody() {
    return null;
  }

  @Override public void onCapture(File screenshot) {
    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 /* screenshot */);
    if (!additionalAttachments.isEmpty()) {
      attachments.addAll(additionalAttachments);
    }
    if (screenshot != null) {
      attachments.add(TelescopeFileProvider.getUriForFile(context, screenshot));
    }

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

    context.startActivity(intent);
  }

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

@@ -117,16 +117,93 @@
 et.Uri;%0A
+import android.os.AsyncTask;%0Aimport android.support.annotation.WorkerThread;%0A
 import j
@@ -1118,16 +1118,30 @@
 y. */%0A  
+@WorkerThread 
 protecte
@@ -1232,24 +1232,536 @@
 reenshot) %7B%0A
+    new CreateIntentTask(context, screenshot).execute();%0A  %7D%0A%0A  @WorkerThread protected Set%3CUri%3E getAdditionalAttachments() %7B%0A    return Collections.emptySet();%0A  %7D%0A%0A  private final class CreateIntentTask extends AsyncTask%3CVoid, Void, Intent%3E %7B%0A    private final Context context;%0A    private final File screenshot;%0A%0A    CreateIntentTask(Context context, File screenshot) %7B%0A      this.context = context;%0A      this.screenshot = screenshot;%0A    %7D%0A%0A    @Override protected Intent doInBackground(Void... params) %7B%0A  
     Intent i
@@ -1809,24 +1809,26 @@
 TIPLE);%0A    
+  
 intent.setTy
@@ -1850,16 +1850,18 @@
 822%22);%0A%0A
+  
     if (
@@ -1877,32 +1877,34 @@
 = null) %7B%0A      
+  
 intent.putExtra(
@@ -1935,27 +1935,31 @@
 bject);%0A    
+  
 %7D%0A%0A
+  
     if (addr
@@ -1973,32 +1973,34 @@
 = null) %7B%0A      
+  
 intent.putExtra(
@@ -2035,23 +2035,27 @@
 s);%0A    
+  
 %7D%0A%0A    
+ 
+ 
 String b
@@ -2067,24 +2067,26 @@
  getBody();%0A
+  
     if (body
@@ -2095,32 +2095,34 @@
 = null) %7B%0A      
+  
 intent.putExtra(
@@ -2147,27 +2147,31 @@
  body);%0A    
+ 
+ 
 %7D%0A%0A
+  
     Set%3CUri%3E
@@ -2219,24 +2219,26 @@
 achments();%0A
+  
     ArrayLis
@@ -2320,18 +2320,16 @@
 reen
-shot
  */);%0A
+  
     
@@ -2366,32 +2366,34 @@
 mpty()) %7B%0A      
+  
 attachments.addA
@@ -2427,14 +2427,18 @@
     
+  
 %7D%0A    
+ 
+ 
 if (
@@ -2455,24 +2455,26 @@
  != null) %7B%0A
+  
       attach
@@ -2542,16 +2542,18 @@
 shot));%0A
+  
     %7D%0A%0A 
@@ -2551,24 +2551,26 @@
      %7D%0A%0A    
+  
 if (!attachm
@@ -2583,24 +2583,26 @@
 sEmpty()) %7B%0A
+  
       intent
@@ -2673,105 +2673,105 @@
     
+ 
+ 
 %7D%0A%0A    
-context.startActivity(
+  return 
 intent
-)
 ;%0A  
+ 
+ 
 %7D%0A%0A  
-protected Set%3CUri%3E getAdditionalAttachm
+  @Override protected void onPostExecute(Intent int
 ent
-s(
 ) %7B%0A    
 retu
@@ -2770,38 +2770,46 @@
     
-return Collections.emptySet();
+  context.startActivity(intent);%0A    %7D
 %0A  %7D

Move EmailLens' intent creation to a bg thread