com.wit.android.support.content.intent.VideoIntent.java Source code

Java tutorial

Introduction

Here is the source code for com.wit.android.support.content.intent.VideoIntent.java

Source

/*
 * =================================================================================================
 *                    Copyright (C) 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.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;

import com.wit.android.support.content.MimeType;

import java.io.File;

/**
 * <h3>Class Overview</h3>
 * <p>
 * todo: description
 * </p>
 *
 * @author Martin Albedinsky
 */
public class VideoIntent extends ContentIntent {

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

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

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

    /**
     * Flag indicating whether the output trough log-cat is enabled or not.
     */
    // private static final boolean LOG_ENABLED = true;

    /**
     * Flag indicating whether the debug output trough log-cat is enabled or not.
     */
    // private static final boolean DEBUG_ENABLED = true;

    /**
     * Flag to identify request code used to obtain video from gallery.
     */
    public static final int REQUEST_CODE_GALLERY = 0x5011;

    /**
     * Flag to identify request code used to obtain video using camera.
     */
    public static final int REQUEST_CODE_CAMERA = 0x5012;

    /**
     * Default format for video file name.
     * <p>
     * Constant value: <b>VIDEO_%s</b>
     */
    public static final String VIDEO_FILE_NAME_FORMAT = "VIDEO_%s";
    /**
     * Static members ==============================================================================
     */

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

    /**
     * Camera provider item.
     */
    private ContentProviderItem mCameraProvider;

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

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

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

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

    /**
     * Public --------------------------------------------------------------------------------------
     */

    /**
     * Creates a new instance of Intent with {@link android.content.Intent#ACTION_GET_CONTENT} and
     * {@link MimeType#VIDEO} MIME type.
     *
     * @return New intent instance.
     */
    @NonNull
    public static Intent createGalleryIntent() {
        return new Intent(Intent.ACTION_GET_CONTENT).setType(MimeType.VIDEO);
    }

    /**
     * Same as {@link #createCameraIntent(java.io.File)} with {@code null} for <var>outputFile</var>
     * parameter.
     */
    @NonNull
    public static Intent createCameraIntent() {
        return createCameraIntent((Uri) null);
    }

    /**
     * Same as {@link #createCameraIntent(android.net.Uri)} with <var>outputUri</var> created from
     * the given <var>outputFile</var>.
     *
     * @param outputFile A file used to crate Uri.
     * @see #createCameraIntent()
     */
    @NonNull
    public static Intent createCameraIntent(@Nullable File outputFile) {
        return createCameraIntent(outputFile != null ? Uri.fromFile(outputFile) : null);
    }

    /**
     * Creates a new instance of Intent with {@link android.provider.MediaStore#ACTION_VIDEO_CAPTURE}.
     *
     * @param outputUri If not {@code null}, it will be attached to intent as {@link android.provider.MediaStore#EXTRA_OUTPUT},
     *                  so when the result is received in for example {@link Activity#onActivityResult(int, int, android.content.Intent)},
     *                  the <var>outputUri</var> will address to a file which contains the currently
     *                  captured video if the camera provider's item was selected within <b>chooser dialog</b>
     *                  and user confirmed that captured video.
     * @return New intent instance.
     * @see #createCameraIntent(java.io.File)
     */
    @NonNull
    public static Intent createCameraIntent(@Nullable Uri outputUri) {
        final Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
        if (outputUri != null) {
            intent.putExtra(MediaStore.EXTRA_OUTPUT, outputUri);
        }
        return intent;
    }

    /**
     * Same as {@link #createVideoFile(String)} with <var>fileName</var> in {@link #VIDEO_FILE_NAME_FORMAT}
     * format with the current time stamp provided by {@link #createContentFileTimeStamp() }.
     */
    @Nullable
    public static File createVideoFile() {
        return createVideoFile(String.format(VIDEO_FILE_NAME_FORMAT, createContentFileTimeStamp()));
    }

    /**
     * Same as {@link #createContentFile(String, String, String)} with <b>.mp4</b> as <var>fileType</var>
     * and {@link android.os.Environment#DIRECTORY_MOVIES} as <var>environmentDirectoryType</var>.
     *
     * @param fileName Desired name for the video file.
     * @see #createVideoFile()
     */
    @Nullable
    public static File createVideoFile(@NonNull String fileName) {
        return createContentFile(fileName, ".mp4", Environment.DIRECTORY_MOVIES);
    }

    /**
     */
    @Override
    public VideoIntent withDefaultProviders() {
        withProviders(
                new ContentProviderItem().name("Gallery").intent(createGalleryIntent())
                        .requestCode(REQUEST_CODE_GALLERY),
                mCameraProvider = new ContentProviderItem().name("Camera").intent(createCameraIntent())
                        .requestCode(REQUEST_CODE_CAMERA));
        if (mUri != null) {
            mCameraProvider.intent.putExtra(MediaStore.EXTRA_OUTPUT, mUri);
        }
        return this;
    }

    /**
     * Getters + Setters ---------------------------------------------------------------------------
     */

    /**
     * If the passed <var>uri</var> is not {@code null}, the current data (MIME) type will be
     * by default set to {@link MimeType#VIDEO}.
     */
    @Override
    public ContentIntent input(@Nullable Uri uri) {
        super.input(uri);
        if (uri != null) {
            this.mDataType = MimeType.VIDEO;
        }
        return this;
    }

    /**
     * Sets the output uri for video captured by camera.
     *
     * @param uri If not {@code null}, it will be attached to {@link com.wit.android.support.content.intent.ContentIntent.ContentProviderItem}
     *            holding intent with the {@link #REQUEST_CODE_CAMERA} request code, so when the camera
     *            provider's item will be selected within a <b>chooser dialog</b> and user confirms his
     *            captured video, result about this action will be received in for example
     *            {@link Activity#onActivityResult(int, int, android.content.Intent)}. The passed
     *            <var>uri</var> will then address to a file which contains the currently captured
     *            video.
     * @return This intent builder to allow methods chaining.
     */
    @Override
    public ContentIntent output(@Nullable Uri uri) {
        if (mCameraProvider != null) {
            if (uri != null) {
                mCameraProvider.intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
            } else {
                mCameraProvider.intent.removeExtra(MediaStore.EXTRA_OUTPUT);
            }
        }
        return super.output(uri);
    }

    /**
     * Protected -----------------------------------------------------------------------------------
     */

    /**
     * Private -------------------------------------------------------------------------------------
     */

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