Java tutorial
/* * ================================================================================================= * 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.content.res.Resources; 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 java.io.File; /** * A {@link ContentIntent} intent builder implementation providing API to build an intent to obtain * or preview a video content. * * @author Martin Albedinsky */ public class VideoIntent extends ContentIntent<VideoIntent> { /** * Interface =================================================================================== */ /** * Constants =================================================================================== */ /** * Log TAG. */ // private static final String TAG = "VideoIntent"; /** * 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 intent handler. */ private ContentHandler mCameraHandler; /** * Constructors ================================================================================ */ /** * Creates a new instance of VideoIntent for the given <var>activity</var> context. * <p> * See {@link com.albedinsky.android.support.intent.BaseIntent#BaseIntent(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.albedinsky.android.support.intent.BaseIntent#BaseIntent(android.support.v4.app.Fragment)} * for additional info. */ public VideoIntent(@NonNull Fragment fragment) { super(fragment); } /** * Methods ===================================================================================== */ /** * Creates a new instance of Intent with {@link Intent#ACTION_GET_CONTENT} and * {@link MimeType#VIDEO} MIME type that can be used to launch a gallery app (depends on user's * choice) to pick one of available videos. * * @return New gallery intent instance. */ @NonNull public static Intent createGalleryIntent() { return new Intent(Intent.ACTION_GET_CONTENT).setType(MimeType.VIDEO); } /** * Same as {@link #createCameraIntent(File)} with {@code null} for <var>outputFile</var> * parameter. */ @NonNull public static Intent createCameraIntent() { return createCameraIntent((Uri) null); } /** * Same as {@link #createCameraIntent(Uri)} with <var>outputUri</var> created from * the given <var>outputFile</var> if not {@code null}. * * @param outputFile The desired file used to crate the output 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 MediaStore#ACTION_VIDEO_CAPTURE} * that can be used to launch a camera app to capture a single video. * * @param outputUri If not {@code null}, it will be attached to intent as {@link MediaStore#EXTRA_OUTPUT}, * so when the result is received in for example {@link Activity#onActivityResult(int, int, Intent)}, * the <var>outputUri</var> will address to a file which contains the currently * captured video. * @return New camera intent instance. * @see #createCameraIntent(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)} with <b>.mp4</b> suffix for the specified * <var>fileName</var> (if it does not contain any) and {@link Environment#DIRECTORY_MOVIES} * as <var>externalDirectoryType</var>. * * @param fileName The desired name for the video file. * @see #createVideoFile() */ @Nullable public static File createVideoFile(@NonNull String fileName) { return ContentIntent.createContentFile(appendDefaultFileSuffixIfNotPresented(fileName, ".mp4"), Environment.DIRECTORY_MOVIES); } /** */ @Override @SuppressWarnings("ConstantConditions") public VideoIntent withDefaultHandlers() { final Resources resources = mContextWrapper.getContext().getResources(); withHandlers(onCreateGalleryHandler(resources), mCameraHandler = onCreateCameraHandler(resources)); updateCameraHandlerOutputUri(); return this; } /** * Invoked from {@link #withDefaultHandlers()} to create gallery intent handler. * * @param resources Application resources. * @return Content handler with gallery intent. * @see #onCreateCameraHandler(Resources) */ @NonNull protected ContentHandler onCreateGalleryHandler(@NonNull Resources resources) { return new ContentHandler(resources.getString(R.string.intent_content_handler_gallery), createGalleryIntent()).requestCode(REQUEST_CODE_GALLERY); } /** * Invoked from {@link #withDefaultHandlers()} to create camera intent handler. * * @param resources Application resources. * @return Content handler with camera intent. * @see #onCreateGalleryHandler(Resources) */ @NonNull protected ContentHandler onCreateCameraHandler(@NonNull Resources resources) { return new ContentHandler(resources.getString(R.string.intent_content_handler_camera), createCameraIntent()) .requestCode(REQUEST_CODE_CAMERA); } /** * 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 VideoIntent input(@Nullable Uri uri) { super.input(uri); if (uri != null) { this.mDataType = MimeType.VIDEO; } return this; } /** * Sets an output uri for a video to be captured by the camera. * * @param uri If not {@code null}, it will be attached to {@link ContentHandler} * holding intent with the {@link #REQUEST_CODE_CAMERA} request code, so when the camera * handler's item will be selected within a <b>chooser dialog</b> and user confirms his * captured video, result for this action will be received in for example * {@link Activity#onActivityResult(int, int, 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 VideoIntent output(@Nullable Uri uri) { super.output(uri); updateCameraHandlerOutputUri(); return this; } /** * Updates output uri of the camera handler if it is already created. */ private void updateCameraHandlerOutputUri() { if (mCameraHandler != null) { if (mUri != null) mCameraHandler.intent.putExtra(MediaStore.EXTRA_OUTPUT, mUri); else mCameraHandler.intent.removeExtra(MediaStore.EXTRA_OUTPUT); } } /** * Inner classes =============================================================================== */ }