diff --git a/matisse/src/main/java/com/zhihu/matisse/SelectionCreator.java b/matisse/src/main/java/com/zhihu/matisse/SelectionCreator.java index 66aeadfe3..12ab39d6e 100644 --- a/matisse/src/main/java/com/zhihu/matisse/SelectionCreator.java +++ b/matisse/src/main/java/com/zhihu/matisse/SelectionCreator.java @@ -19,6 +19,7 @@ import android.app.Activity; import android.content.Intent; import android.os.Build; + import androidx.annotation.IntDef; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -163,7 +164,7 @@ public SelectionCreator maxSelectable(int maxSelectable) { * * @param maxImageSelectable Maximum selectable count for image. * @param maxVideoSelectable Maximum selectable count for video. - * @return {@link SelectionCreator} for fluent API. + * @return {@link SelectionCreator} for fluent API. */ public SelectionCreator maxSelectablePerMediaType(int maxImageSelectable, int maxVideoSelectable) { if (maxImageSelectable < 1 || maxVideoSelectable < 1) @@ -216,6 +217,7 @@ public SelectionCreator originalEnable(boolean enable) { /** * Determines Whether to hide top and bottom toolbar in PreView mode ,when user tap the picture + * * @param enable * @return {@link SelectionCreator} for fluent API. */ @@ -317,6 +319,20 @@ public SelectionCreator imageEngine(ImageEngine imageEngine) { return this; } + /*** + * Set a Album sort order + * taken : Sort by date taken + * added : Sort by date added + * size : Sort by file size + * default value : Sort by album default order + * @param sortBy + * @return + */ + public SelectionCreator albumOrder(String sortBy) { + mSelectionSpec.setOrderCondition(sortBy); + return this; + } + /** * Set listener for callback immediately when user select or unselect something. *

diff --git a/matisse/src/main/java/com/zhihu/matisse/internal/entity/SelectionSpec.java b/matisse/src/main/java/com/zhihu/matisse/internal/entity/SelectionSpec.java index 80b7d1098..f1b40a5e4 100644 --- a/matisse/src/main/java/com/zhihu/matisse/internal/entity/SelectionSpec.java +++ b/matisse/src/main/java/com/zhihu/matisse/internal/entity/SelectionSpec.java @@ -31,11 +31,17 @@ import java.util.List; import java.util.Set; +import static com.zhihu.matisse.internal.loader.AlbumMediaLoader.ORDER_BY_DATE_TAKEN; +import static com.zhihu.matisse.internal.loader.AlbumMediaLoader.ORDER_BY_DEFAULT; +import static com.zhihu.matisse.internal.loader.AlbumMediaLoader.ORDER_BY_MEDIA_ADDED; +import static com.zhihu.matisse.internal.loader.AlbumMediaLoader.ORDER_BY_SIZE; + public final class SelectionSpec { public Set mimeTypeSet; public boolean mediaTypeExclusive; public boolean showSingleMediaType; + public String orderCondition; @StyleRes public int themeId; public int orientation; @@ -93,6 +99,7 @@ private void reset() { autoHideToobar = false; originalMaxSize = Integer.MAX_VALUE; showPreview = true; + orderCondition = ORDER_BY_DEFAULT; } public boolean singleSelectionModeEnabled() { @@ -118,4 +125,19 @@ public boolean onlyShowGif() { private static final class InstanceHolder { private static final SelectionSpec INSTANCE = new SelectionSpec(); } + + public void setOrderCondition(String condition) { + switch (condition) { + case "default": + this.orderCondition = ORDER_BY_DEFAULT; + case "taken": + this.orderCondition = ORDER_BY_DATE_TAKEN; + case "added": + this.orderCondition = ORDER_BY_MEDIA_ADDED; + case "size": + this.orderCondition = ORDER_BY_SIZE; + default: + this.orderCondition = ORDER_BY_DEFAULT; + } + } } diff --git a/matisse/src/main/java/com/zhihu/matisse/internal/loader/AlbumLoader.java b/matisse/src/main/java/com/zhihu/matisse/internal/loader/AlbumLoader.java index 157a56349..1603e2bfe 100644 --- a/matisse/src/main/java/com/zhihu/matisse/internal/loader/AlbumLoader.java +++ b/matisse/src/main/java/com/zhihu/matisse/internal/loader/AlbumLoader.java @@ -36,6 +36,11 @@ import java.util.Map; import java.util.Set; +import static com.zhihu.matisse.internal.loader.AlbumMediaLoader.ORDER_BY_DATE_TAKEN; +import static com.zhihu.matisse.internal.loader.AlbumMediaLoader.ORDER_BY_DEFAULT; +import static com.zhihu.matisse.internal.loader.AlbumMediaLoader.ORDER_BY_MEDIA_ADDED; +import static com.zhihu.matisse.internal.loader.AlbumMediaLoader.ORDER_BY_SIZE; + /** * Load all albums (grouped by bucket_id) into a single cursor. */ @@ -118,20 +123,21 @@ private static String[] getSelectionArgsForSingleMediaGifType(int mediaType) { private static final String BUCKET_ORDER_BY = "datetaken DESC"; - private AlbumLoader(Context context, String selection, String[] selectionArgs) { + private AlbumLoader(Context context, String selection, String[] selectionArgs, String orderBy) { super( context, QUERY_URI, beforeAndroidTen() ? PROJECTION : PROJECTION_29, selection, selectionArgs, - BUCKET_ORDER_BY + orderBy ); } public static CursorLoader newInstance(Context context) { String selection; String[] selectionArgs; + String order; if (SelectionSpec.getInstance().onlyShowGif()) { selection = beforeAndroidTen() ? SELECTION_FOR_SINGLE_MEDIA_GIF_TYPE : SELECTION_FOR_SINGLE_MEDIA_GIF_TYPE_29; @@ -151,7 +157,20 @@ public static CursorLoader newInstance(Context context) { selection = beforeAndroidTen() ? SELECTION : SELECTION_29; selectionArgs = SELECTION_ARGS; } - return new AlbumLoader(context, selection, selectionArgs); + + switch (SelectionSpec.getInstance().orderCondition) { + case "default": + order = ORDER_BY_DEFAULT; + case "taken": + order = ORDER_BY_DATE_TAKEN; + case "added": + order = ORDER_BY_MEDIA_ADDED; + case "size": + order = ORDER_BY_SIZE; + default: + order = ORDER_BY_DEFAULT; + } + return new AlbumLoader(context, selection, selectionArgs, order); } @Override diff --git a/matisse/src/main/java/com/zhihu/matisse/internal/loader/AlbumMediaLoader.java b/matisse/src/main/java/com/zhihu/matisse/internal/loader/AlbumMediaLoader.java index 7bba0d616..132df433f 100644 --- a/matisse/src/main/java/com/zhihu/matisse/internal/loader/AlbumMediaLoader.java +++ b/matisse/src/main/java/com/zhihu/matisse/internal/loader/AlbumMediaLoader.java @@ -120,15 +120,18 @@ private static String[] getSelectionAlbumArgsForGifType(int mediaType, String al } // =============================================================== - private static final String ORDER_BY = MediaStore.Images.Media.DATE_TAKEN + " DESC"; + public static final String ORDER_BY_DEFAULT = MediaStore.Images.Media.DEFAULT_SORT_ORDER + " DESC"; + public static final String ORDER_BY_DATE_TAKEN = MediaStore.Images.Media.DATE_TAKEN + " DESC"; + public static final String ORDER_BY_MEDIA_ADDED = MediaStore.Images.Media.DATE_ADDED + " DESC"; + public static final String ORDER_BY_SIZE = MediaStore.Images.Media.SIZE + " DESC"; private final boolean mEnableCapture; - private AlbumMediaLoader(Context context, String selection, String[] selectionArgs, boolean capture) { - super(context, QUERY_URI, PROJECTION, selection, selectionArgs, ORDER_BY); + private AlbumMediaLoader(Context context, String selection, String[] selectionArgs, boolean capture, String orderCondition) { + super(context, QUERY_URI, PROJECTION, selection, selectionArgs, orderCondition); mEnableCapture = capture; } - public static CursorLoader newInstance(Context context, Album album, boolean capture) { + public static CursorLoader newInstance(Context context, Album album, boolean capture, String orderCondition) { String selection; String[] selectionArgs; boolean enableCapture; @@ -176,7 +179,7 @@ public static CursorLoader newInstance(Context context, Album album, boolean cap } enableCapture = false; } - return new AlbumMediaLoader(context, selection, selectionArgs, enableCapture); + return new AlbumMediaLoader(context, selection, selectionArgs, enableCapture, orderCondition); } @Override diff --git a/matisse/src/main/java/com/zhihu/matisse/internal/model/AlbumMediaCollection.java b/matisse/src/main/java/com/zhihu/matisse/internal/model/AlbumMediaCollection.java index fa25939ec..baeb2c512 100644 --- a/matisse/src/main/java/com/zhihu/matisse/internal/model/AlbumMediaCollection.java +++ b/matisse/src/main/java/com/zhihu/matisse/internal/model/AlbumMediaCollection.java @@ -30,10 +30,13 @@ import java.lang.ref.WeakReference; +import static com.zhihu.matisse.internal.loader.AlbumMediaLoader.ORDER_BY_DEFAULT; + public class AlbumMediaCollection implements LoaderManager.LoaderCallbacks { private static final int LOADER_ID = 2; private static final String ARGS_ALBUM = "args_album"; private static final String ARGS_ENABLE_CAPTURE = "args_enable_capture"; + private static final String ARGS_SORT_ORDER = "args_album_sort_order"; private WeakReference mContext; private LoaderManager mLoaderManager; private AlbumMediaCallbacks mCallbacks; @@ -51,7 +54,7 @@ public Loader onCreateLoader(int id, Bundle args) { } return AlbumMediaLoader.newInstance(context, album, - album.isAll() && args.getBoolean(ARGS_ENABLE_CAPTURE, false)); + album.isAll() && args.getBoolean(ARGS_ENABLE_CAPTURE, false), args.getString(ARGS_SORT_ORDER)); } @Override @@ -88,13 +91,14 @@ public void onDestroy() { } public void load(@Nullable Album target) { - load(target, false); + load(target, false, ORDER_BY_DEFAULT); } - public void load(@Nullable Album target, boolean enableCapture) { + public void load(@Nullable Album target, boolean enableCapture, String order) { Bundle args = new Bundle(); args.putParcelable(ARGS_ALBUM, target); args.putBoolean(ARGS_ENABLE_CAPTURE, enableCapture); + args.putString(ARGS_SORT_ORDER, order); mLoaderManager.initLoader(LOADER_ID, args, this); } diff --git a/matisse/src/main/java/com/zhihu/matisse/internal/ui/MediaSelectionFragment.java b/matisse/src/main/java/com/zhihu/matisse/internal/ui/MediaSelectionFragment.java index 86c46e2f7..11fdf75c1 100644 --- a/matisse/src/main/java/com/zhihu/matisse/internal/ui/MediaSelectionFragment.java +++ b/matisse/src/main/java/com/zhihu/matisse/internal/ui/MediaSelectionFragment.java @@ -110,7 +110,7 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) { mRecyclerView.addItemDecoration(new MediaGridInset(spanCount, spacing, false)); mRecyclerView.setAdapter(mAdapter); mAlbumMediaCollection.onCreate(getActivity(), this); - mAlbumMediaCollection.load(album, selectionSpec.capture); + mAlbumMediaCollection.load(album, selectionSpec.capture, selectionSpec.orderCondition); } @Override diff --git a/sample/src/main/java/com/zhihu/matisse/sample/SampleActivity.java b/sample/src/main/java/com/zhihu/matisse/sample/SampleActivity.java index 9788a6014..ae26ad195 100644 --- a/sample/src/main/java/com/zhihu/matisse/sample/SampleActivity.java +++ b/sample/src/main/java/com/zhihu/matisse/sample/SampleActivity.java @@ -104,6 +104,7 @@ private void startAction(View v) { .setOnCheckedListener(isChecked -> { Log.e("isChecked", "onCheck: isChecked=" + isChecked); }) + .albumOrder("added") .forResult(REQUEST_CODE_CHOOSE); break; case R.id.dracula: