com.albedinsky.android.support.intent.ShareIntent.java Source code

Java tutorial

Introduction

Here is the source code for com.albedinsky.android.support.intent.ShareIntent.java

Source

/*
 * =================================================================================================
 *                             Copyright (C) 2014 Martin Albedinsky
 * =================================================================================================
 *         Licensed under the Apache License, Version 2.0 or later (further "License" only).
 * -------------------------------------------------------------------------------------------------
 * You may use this file only in compliance with the License. More details and copy of this License
 * you may obtain at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * You can redistribute, modify or publish any part of the code written within this file but as it
 * is described in the License, the software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES or CONDITIONS OF ANY KIND.
 *
 * See the License for the specific language governing permissions and limitations under the License.
 * =================================================================================================
 */
package com.albedinsky.android.support.intent;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Parcelable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v4.app.Fragment;
import android.text.TextUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * A {@link BaseIntent} builder implementation providing API for building of intents targeting
 * a <b>content sharing</b> related applications.
 * <p>
 * This intent builder requires a mime type of the sharing content to be specified via {@link #mimeType(String)}.
 * You can use one of predefined mime types from {@link MimeType} class. The content that you want to
 * share can be specified as string based content via {@link #content(CharSequence)} or if you want
 * to share a content which is represented by an Uri, such Uri can be specified via {@link #uri(Uri)}
 * or {@link #uris(List)}.
 *
 * @author Martin Albedinsky
 */
public class ShareIntent extends BaseIntent<ShareIntent> {

    /**
     * Interface ===================================================================================
     */

    /**
     * Constants ===================================================================================
     */

    /**
     * Log TAG.
     */
    // private static final String TAG = "ShareIntent";

    /**
     * Static members ==============================================================================
     */

    /**
     * Members =====================================================================================
     */

    /**
     * Mime type of content to share.
     */
    private String mDataType = MimeType.TEXT;

    /**
     * Title for share message.
     */
    private CharSequence mTitle;

    /**
     * Text for share message.
     */
    private CharSequence mContent;

    /**
     * Uri to content to share.
     */
    private Uri mUri;

    /**
     * List of Uris to content to share.
     */
    private List<Uri> mUris;

    /**
     * Constructors ================================================================================
     */

    /**
     * Creates a new instance of ShareIntent for the given <var>activity</var> context.
     * See {@link com.albedinsky.android.support.intent.BaseIntent#BaseIntent(Activity)}
     * for additional info.
     */
    public ShareIntent(@NonNull Activity activity) {
        super(activity);
    }

    /**
     * Creates a new instance of ShareIntent for the given <var>fragment</var> context.
     * See {@link com.albedinsky.android.support.intent.BaseIntent#BaseIntent(android.support.v4.app.Fragment)}
     * for additional info.
     */
    public ShareIntent(@NonNull Fragment fragment) {
        super(fragment);
    }

    /**
     * Methods =====================================================================================
     */

    /**
     * Same as {@link #content(CharSequence)} for resource id.
     *
     * @param resId Resource id of the desired content text.
     */
    public ShareIntent content(@StringRes int resId) {
        return content(obtainText(resId));
    }

    /**
     * Sets a content for sharing intent.
     *
     * @param text The desired content text.
     * @return This intent builder to allow methods chaining.
     */
    public ShareIntent content(@Nullable CharSequence text) {
        this.mContent = text;
        return this;
    }

    /**
     * Returns the text that will be used as content for sharing intent.
     *
     * @return Content for sharing intent or empty string if not specified yet.
     */
    @NonNull
    public CharSequence content() {
        return mContent != null ? mContent : "";
    }

    /**
     * Sets an Uri to content that should be shared.
     *
     * @param uri The desired content Uri.
     * @return This intent builder to allow methods chaining.
     */
    public ShareIntent uri(@Nullable Uri uri) {
        this.mUri = uri;
        return this;
    }

    /**
     * Returns the Uri to content to share.
     *
     * @return Content Uri to share or {@code null} if not specified yet.
     */
    @Nullable
    public Uri uri() {
        return mUri;
    }

    /**
     * Same as {@link #uris(List)} for var-Uris.
     * @param uris The desired set of content uris.
     */
    public ShareIntent uris(@NonNull Uri... uris) {
        return uris(Arrays.asList(uris));
    }

    /**
     * Sets a set of Uris to content that should be shared.
     *
     * @param uris The desired set of content uris.
     * @return This intent builder to allow methods chaining.
     */
    public ShareIntent uris(@NonNull List<Uri> uris) {
        this.mUris = uris;
        return this;
    }

    /**
     * Returns the set of Uris to content to share.
     *
     * @return Content Uris to share or {@link Collections#EMPTY_LIST} if there were no Uris
     * specified yet.
     */
    @NonNull
    @SuppressWarnings("unchecked")
    public List<Uri> uris() {
        return mUris != null ? mUris : Collections.EMPTY_LIST;
    }

    /**
     * Sets a MIME type of the content passed to {@link #content(CharSequence)} or {@link #uri(Uri)}
     * or {@link #uris(List)}.
     *
     * @param type The desired content MIME type. Use one of {@link MimeType#TEXT_PLAIN},
     *             {@link MimeType#TEXT_HTML}, {@link MimeType#IMAGE}, ..., for commonly used types.
     * @return This intent builder to allow methods chaining.
     */
    public ShareIntent mimeType(@NonNull @ContentIntent.DataType String type) {
        this.mDataType = type;
        return this;
    }

    /**
     * Returns the MIME type of the content to share.
     * <p>
     * Default value: <b>{@link MimeType#TEXT}</b>
     *
     * @return One of mime types specified in {@link MimeType} class.
     * @see #mimeType(String)
     */
    @NonNull
    @ContentIntent.DataType
    public String mimeType() {
        return mDataType;
    }

    /**
     * Same as {@link #title(CharSequence)} for resource id.
     *
     * @param resId Resource id of the desired title text.
     */
    public ShareIntent title(@StringRes int resId) {
        return title(obtainText(resId));
    }

    /**
     * Sets a title for sharing intent.
     *
     * @param title The desired title text.
     * @return This intent builder to allow methods chaining.
     */
    public ShareIntent title(@Nullable CharSequence title) {
        this.mTitle = title;
        return this;
    }

    /**
     * Returns the title text that will be used as title for sharing intent.
     *
     * @return Title for sharing intent or empty string if not specified yet.
     */
    @NonNull
    public CharSequence title() {
        return mTitle != null ? mTitle : "";
    }

    /**
     */
    @Override
    protected void ensureCanBuildOrThrow() {
        super.ensureCanBuildOrThrow();
        if (TextUtils.isEmpty(mContent) && mUri == null && mUris == null) {
            throw cannotBuildIntentException("No content to share specified.");
        }
        if (TextUtils.isEmpty(mDataType)) {
            throw cannotBuildIntentException("No content's MIME type specified.");
        }
    }

    /**
     */
    @NonNull
    @Override
    protected Intent onBuild() {
        final Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType(mDataType);
        if (!TextUtils.isEmpty(mTitle)) {
            intent.putExtra(Intent.EXTRA_TITLE, mTitle);
        }
        if (!TextUtils.isEmpty(mContent)) {
            intent.putExtra(Intent.EXTRA_TEXT, mContent);
        }
        if (mUri != null) {
            intent.putExtra(Intent.EXTRA_STREAM, mUri);
        } else if (mUris != null) {
            intent.setAction(Intent.ACTION_SEND_MULTIPLE);
            intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, new ArrayList<Parcelable>(mUris));
        }
        return intent;
    }

    /**
     */
    @Override
    protected boolean onStart(@NonNull Intent intent) {
        startActivity(Intent.createChooser(intent, mDialogTitle));
        return true;
    }

    /**
     * Inner classes ===============================================================================
     */
}