Java tutorial
/* * Copyright (C) 2014 nohana, Inc. * Copyright 2017 Zhihu Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.xbm.android.matisse; import android.app.Activity; import android.content.Intent; import android.os.Build; import android.support.annotation.IntDef; import android.support.annotation.NonNull; import android.support.annotation.RequiresApi; import android.support.annotation.StyleRes; import android.support.v4.app.Fragment; import com.xbm.android.matisse.engine.ImageEngine; import com.xbm.android.matisse.filter.Filter; import com.xbm.android.matisse.internal.entity.CaptureStrategy; import com.xbm.android.matisse.internal.entity.SelectionSpec; import com.xbm.android.matisse.ui.MatisseActivity; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.util.ArrayList; import java.util.Set; import static android.content.pm.ActivityInfo.*; /** * Fluent API for building media select specification. */ @SuppressWarnings("unused") public final class SelectionCreator { private final Matisse mMatisse; private final SelectionSpec mSelectionSpec; @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2) @IntDef({ SCREEN_ORIENTATION_UNSPECIFIED, SCREEN_ORIENTATION_LANDSCAPE, SCREEN_ORIENTATION_PORTRAIT, SCREEN_ORIENTATION_USER, SCREEN_ORIENTATION_BEHIND, SCREEN_ORIENTATION_SENSOR, SCREEN_ORIENTATION_NOSENSOR, SCREEN_ORIENTATION_SENSOR_LANDSCAPE, SCREEN_ORIENTATION_SENSOR_PORTRAIT, SCREEN_ORIENTATION_REVERSE_LANDSCAPE, SCREEN_ORIENTATION_REVERSE_PORTRAIT, SCREEN_ORIENTATION_FULL_SENSOR, SCREEN_ORIENTATION_USER_LANDSCAPE, SCREEN_ORIENTATION_USER_PORTRAIT, SCREEN_ORIENTATION_FULL_USER, SCREEN_ORIENTATION_LOCKED }) @Retention(RetentionPolicy.SOURCE) @interface ScreenOrientation { } /** * Constructs a new specification builder on the context. * * @param matisse a requester context wrapper. * @param mimeTypes MIME type set to select. */ SelectionCreator(Matisse matisse, @NonNull Set<MimeType> mimeTypes, boolean mediaTypeExclusive) { mMatisse = matisse; mSelectionSpec = SelectionSpec.getCleanInstance(); mSelectionSpec.mimeTypeSet = mimeTypes; mSelectionSpec.mediaTypeExclusive = mediaTypeExclusive; mSelectionSpec.orientation = SCREEN_ORIENTATION_UNSPECIFIED; } /** * Whether to show only one media type if choosing medias are only images or videos. * * @param showSingleMediaType whether to show only one media type, either images or videos. * @return {@link SelectionCreator} for fluent API. * @see SelectionSpec#onlyShowImages() * @see SelectionSpec#onlyShowVideos() */ public SelectionCreator showSingleMediaType(boolean showSingleMediaType) { mSelectionSpec.showSingleMediaType = showSingleMediaType; return this; } /** * Theme for media selecting Activity. * <p> * There are two built-in themes: * 1. com.zhihu.matisse.R.style.Matisse_Zhihu; * 2. com.zhihu.matisse.R.style.Matisse_Dracula * you can define a custom theme derived from the above ones or other themes. * * @param themeId theme resource id. Default value is com.zhihu.matisse.R.style.Matisse_Zhihu. * @return {@link SelectionCreator} for fluent API. */ public SelectionCreator theme(@StyleRes int themeId) { mSelectionSpec.themeId = themeId; return this; } /** * Show a auto-increased number or a check mark when user select media. * * @param countable true for a auto-increased number from 1, false for a check mark. Default * value is false. * @return {@link SelectionCreator} for fluent API. */ public SelectionCreator countable(boolean countable) { mSelectionSpec.countable = countable; return this; } /** * Maximum selectable count. * * @param maxSelectable Maximum selectable count. Default value is 1. * @return {@link SelectionCreator} for fluent API. */ public SelectionCreator maxSelectable(int maxSelectable) { if (maxSelectable < 1) throw new IllegalArgumentException("maxSelectable must be greater than or equal to one"); mSelectionSpec.maxSelectable = maxSelectable; return this; } /** * Add filter to filter each selecting item. * * @param filter {@link Filter} * @return {@link SelectionCreator} for fluent API. */ public SelectionCreator addFilter(@NonNull Filter filter) { if (mSelectionSpec.filters == null) { mSelectionSpec.filters = new ArrayList<>(); } if (filter == null) throw new IllegalArgumentException("filter cannot be null"); mSelectionSpec.filters.add(filter); return this; } /** * Determines whether the photo capturing is enabled or not on the media grid view. * <p> * If this value is set true, photo capturing entry will appear only on All Media's page. * * @param enable Whether to enable capturing or not. Default value is false; * @return {@link SelectionCreator} for fluent API. */ public SelectionCreator capture(boolean enable) { mSelectionSpec.capture = enable; return this; } /** * Capture strategy provided for the location to save photos including internal and external * storage and also a authority for {@link android.support.v4.content.FileProvider}. * * @param captureStrategy {@link CaptureStrategy}, needed only when capturing is enabled. * @return {@link SelectionCreator} for fluent API. */ public SelectionCreator captureStrategy(CaptureStrategy captureStrategy) { mSelectionSpec.captureStrategy = captureStrategy; return this; } /** * Set the desired orientation of this activity. * * @param orientation An orientation constant as used in {@link ScreenOrientation}. * Default value is {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_PORTRAIT}. * @return {@link SelectionCreator} for fluent API. * @see Activity#setRequestedOrientation(int) */ public SelectionCreator restrictOrientation(@ScreenOrientation int orientation) { mSelectionSpec.orientation = orientation; return this; } /** * Set a fixed span count for the media grid. Same for different screen orientations. * <p> * This will be ignored when {@link #gridExpectedSize(int)} is set. * * @param spanCount Requested span count. * @return {@link SelectionCreator} for fluent API. */ public SelectionCreator spanCount(int spanCount) { if (spanCount < 1) throw new IllegalArgumentException("spanCount cannot be less than 1"); mSelectionSpec.spanCount = spanCount; return this; } /** * Set expected size for media grid to adapt to different screen sizes. This won't necessarily * be applied cause the media grid should fill the view container. The measured media grid's * size will be as close to this value as possible. * * @param size Expected media grid size in pixel. * @return {@link SelectionCreator} for fluent API. */ public SelectionCreator gridExpectedSize(int size) { mSelectionSpec.gridExpectedSize = size; return this; } /** * Photo thumbnail's scale compared to the View's size. It should be a float value in (0.0, * 1.0]. * * @param scale Thumbnail's scale in (0.0, 1.0]. Default value is 0.5. * @return {@link SelectionCreator} for fluent API. */ public SelectionCreator thumbnailScale(float scale) { if (scale <= 0f || scale > 1f) throw new IllegalArgumentException("Thumbnail scale must be between (0.0, 1.0]"); mSelectionSpec.thumbnailScale = scale; return this; } /** * Provide an image engine. * <p> * There are two built-in image engines: * 1. {@link com.xbm.android.matisse.engine.impl.GlideEngine} * 2. {@link com.xbm.android.matisse.engine.impl.PicassoEngine} * And you can implement your own image engine. * * @param imageEngine {@link ImageEngine} * @return {@link SelectionCreator} for fluent API. */ public SelectionCreator imageEngine(ImageEngine imageEngine) { mSelectionSpec.imageEngine = imageEngine; return this; } /** * Start to select media and wait for result. * * @param requestCode Identity of the request Activity or Fragment. */ public void forResult(int requestCode) { Activity activity = mMatisse.getActivity(); if (activity == null) { return; } Intent intent = new Intent(activity, MatisseActivity.class); Fragment fragment = mMatisse.getFragment(); if (fragment != null) { fragment.startActivityForResult(intent, requestCode); } else { activity.startActivityForResult(intent, requestCode); } } }