Java tutorial
/* * ================================================================================================= * Copyright (C) 2013 - 2014 Martin Albedinsky [Wolf-ITechnologies] * ================================================================================================= * 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.wit.android.support.content.intent; import android.app.Activity; import android.content.Intent; import android.net.Uri; 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 android.util.Log; import com.wit.android.support.content.MimeType; import java.util.ArrayList; /** * <h3>Class Overview</h3> * Intent builder specialized to create SHARE intents. * * @author Martin Albedinsky */ public class ShareIntent extends BaseIntent<ShareIntent> { /** * Interface =================================================================================== */ /** * Constants =================================================================================== */ /** * Log TAG. */ private static final String TAG = "ShareIntent"; /** * Flag indicating whether the debug output trough log-cat is enabled or not. */ // private static final boolean DEBUG_ENABLED = true; /** * Flag indicating whether the output trough log-cat is enabled or not. */ // private static final boolean LOG_ENABLED = ContentConfig.LIBRARY_LOG_ENABLED; /** * 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; /** * Array of Uris to content to share. */ private ArrayList<Uri> mUris; /** * Constructors ================================================================================ */ /** * Creates a new instance of ShareIntent for the given <var>activity</var> context. * See {@link com.wit.android.support.content.intent.BaseIntent#BaseIntent(android.app.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.wit.android.support.content.intent.BaseIntent#BaseIntent(android.support.v4.app.Fragment)} * for additional info. */ public ShareIntent(@NonNull Fragment fragment) { super(fragment); } /** * Methods ===================================================================================== */ /** * Public -------------------------------------------------------------------------------------- */ /** */ @Nullable @Override public Intent buildIntent() { if (TextUtils.isEmpty(mDataType)) { Log.e(TAG, "Can not to create SHARE intent. Missing MIME type."); return null; } 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, mUris); } return intent; } /** */ @Override protected boolean onStart(@NonNull Intent intent) { startActivity(Intent.createChooser(intent, mDialogTitle)); return true; } /** * Getters + Setters --------------------------------------------------------------------------- */ /** * Sets the MIME type of the content passed to {@link #content(CharSequence)} or {@link #uri(android.net.Uri)} * or {@link #uris(java.util.ArrayList)}. * <p> * <b>Note</b>, that this parameter is required to create valid SHARE intent. * * @param type The desired content MIME type. Use one of {@link com.wit.android.support.content.MimeType#TEXT_PLAIN}, * {@link com.wit.android.support.content.MimeType#TEXT_HTML}, {@link com.wit.android.support.content.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 MIME type of content to share. * * @return MIME type of the current content to share. * @see #mimeType(String) */ @ContentIntent.DataType public String getMimeType() { return mDataType; } /** * Same as {@link #title(CharSequence)}, where text will be obtained from the current context wrapper * (activity/fragment). * * @param resId Resource id of the desired title text placed within an application resources. */ public ShareIntent title(@StringRes int resId) { return title(obtainText(resId)); } /** * Sets the title for sharing message. * * @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 title text which should be used as title for sharing message. * * @return Title for sharing message. */ @Nullable public CharSequence getTitle() { return mTitle; } /** * Same as {@link #content(CharSequence)}, where text will be obtained from the current context wrapper * (activity/fragment). * * @param resId Resource id of the desired content text placed within an application resources. */ public ShareIntent content(@StringRes int resId) { return content(obtainText(resId)); } /** * Sets the content for sharing message. * * @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 text which should be used as content for sharing message. * * @return Content for sharing message. */ @Nullable public CharSequence getContent() { return mContent; } /** * Sets the Uri to content to share. * * @param uri Content Uri. * @return This intent builder to allow methods chaining. */ public ShareIntent uri(@Nullable Uri uri) { this.mUri = uri; return this; } /** * Returns Uri to content to share. * * @return Current content Uri to share. */ @Nullable public Uri getUri() { return mUri; } /** * Sets the set of Uris to content to share. * * @param uris Content uris. * @return This intent builder to allow methods chaining. */ public ShareIntent uris(@NonNull ArrayList<Uri> uris) { this.mUris = uris; return this; } /** * Returns set of Uris to content to share. * * @return Current content Uris to share. */ @Nullable public ArrayList<Uri> getUris() { return mUris; } /** * Protected ----------------------------------------------------------------------------------- */ /** * Private ------------------------------------------------------------------------------------- */ /** * Inner classes =============================================================================== */ }