package au.com.jtribe.shelly;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;

/**
 * Represents a basic share of text, image, or video.
 */
public final class SocialShare {
    private Context context;
    private String text;
    private Uri uri;
    private String mimeType;

    SocialShare(Context context) {
        this.context = context;
        this.mimeType = Mime.PLAIN_TEXT;
    }

    /**
     * Set the text to share. This will overwrite any existing text that has been set.
     *
     * @param text Text to share.
     * @return Object this method was called on for method chaining.
     */
    public SocialShare text(String text) {
        if (text == null) throw new IllegalArgumentException("text == null");

        this.text = text;
        return this;
    }

    /**
     * Sets the image to share. This can only be called once, and you can only share a video or an image. Not both.
     *
     * @param imageUri Uri that represents an image
     * @return Object this method was called on for method chaining.
     */
    public SocialShare image(Uri imageUri) {
        if (imageUri == null) throw new IllegalArgumentException("imageUri == null");
        if (this.uri != null) throw new IllegalStateException("Not allowed multiple uri's");

        this.uri = imageUri;
        this.mimeType = Mime.ANY_IMAGE;
        return this;
    }

    /**
     * Sets the video to share. This can only be called once, and you can only share a video or an image. Not both.
     *
     * @param videoUri Uri that represents a video
     * @return Object this method was called on for method chaining.
     */
    public SocialShare video(Uri videoUri) {
        if (videoUri == null) throw new IllegalArgumentException("videoUri == null");
        if (this.uri != null) throw new IllegalStateException("Not allowed multiple uri's");

        this.uri = videoUri;
        this.mimeType = Mime.ANY_VIDEO;
        return this;
    }

    /**
     * Starts an activity to share with the configured details.
     */
    public void send() {
        Intent shareIntent = new Intent(Intent.ACTION_SEND);
        shareIntent.setType(this.mimeType);
        shareIntent.putExtra(Intent.EXTRA_TEXT, this.text);

        if (this.uri != null) {
            shareIntent.putExtra(Intent.EXTRA_STREAM, this.uri);
        }

        this.context.startActivity(shareIntent);
    }
}

@@ -110,16 +110,41 @@
 net.Uri;
+%0Aimport android.os.Build;
 %0A%0A/**%0A *
@@ -2379,24 +2379,307 @@
 %0A        %7D%0A%0A
+        if (Build.VERSION.SDK_INT %3E= Build.VERSION_CODES.LOLLIPOP) %7B%0A            shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);%0A        %7D else %7B%0A            //noinspection deprecation%0A            shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);%0A        %7D%0A%0A
         this

Add the NEW_DOCUMENT flag to intents so they are opened in a new task.