From 51132c5e743df398da12337a9fe01bc01fe91bb5 Mon Sep 17 00:00:00 2001 From: coooldoggy Date: Wed, 8 Jan 2020 13:50:25 +0900 Subject: [PATCH 1/5] - Enable to choose sort order we can support multiple options to show album sorting orders and able to fix downloaded file order seems random. Former version only show album order by date taken, so that user can be confused by the difference of native gallery order. [Usage] .albumOrder() "taken" : order by date taken "added" : order by date added "size" : order by file size default : order by default album order --- .../com/zhihu/matisse/SelectionCreator.java | 24 +++++++++++++++ .../internal/entity/SelectionSpec.java | 4 +++ .../matisse/internal/loader/AlbumLoader.java | 30 +++++++++++++++++-- .../internal/loader/AlbumMediaLoader.java | 13 ++++---- .../internal/model/AlbumMediaCollection.java | 10 +++++-- .../internal/ui/MediaSelectionFragment.java | 2 +- .../zhihu/matisse/sample/SampleActivity.java | 2 ++ 7 files changed, 73 insertions(+), 12 deletions(-) diff --git a/matisse/src/main/java/com/zhihu/matisse/SelectionCreator.java b/matisse/src/main/java/com/zhihu/matisse/SelectionCreator.java index 66aeadfe3..444284165 100644 --- a/matisse/src/main/java/com/zhihu/matisse/SelectionCreator.java +++ b/matisse/src/main/java/com/zhihu/matisse/SelectionCreator.java @@ -19,6 +19,8 @@ import android.app.Activity; import android.content.Intent; import android.os.Build; +import android.provider.MediaStore; + import androidx.annotation.IntDef; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -317,6 +319,28 @@ 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){ + if (sortBy.equals("taken")){ + mSelectionSpec.orderCondition = MediaStore.Images.Media.DATE_TAKEN + " DESC"; + }else if (sortBy.equals("added")){ + mSelectionSpec.orderCondition = MediaStore.Images.Media.DATE_ADDED + " DESC"; + }else if (sortBy.equals("size")){ + mSelectionSpec.orderCondition = MediaStore.Images.Media.SIZE + " DESC"; + }else{ + mSelectionSpec.orderCondition = MediaStore.Images.Media.DEFAULT_SORT_ORDER + " DESC"; + } + 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..c0fedbe09 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,14 @@ import java.util.List; import java.util.Set; +import static com.zhihu.matisse.internal.loader.AlbumMediaLoader.ORDER_BY_DEFAULT; + 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 +96,7 @@ private void reset() { autoHideToobar = false; originalMaxSize = Integer.MAX_VALUE; showPreview = true; + orderCondition = ORDER_BY_DEFAULT; } public boolean singleSelectionModeEnabled() { 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..906ac2fb0 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,25 @@ 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 ORDER_BY_DEFAULT:{ + order = ORDER_BY_DEFAULT; + } + case ORDER_BY_DATE_TAKEN:{ + order = ORDER_BY_DATE_TAKEN; + } + case ORDER_BY_MEDIA_ADDED:{ + order = ORDER_BY_MEDIA_ADDED; + } + case ORDER_BY_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..f56e36844 100644 --- a/sample/src/main/java/com/zhihu/matisse/sample/SampleActivity.java +++ b/sample/src/main/java/com/zhihu/matisse/sample/SampleActivity.java @@ -21,6 +21,7 @@ import android.content.pm.ActivityInfo; import android.net.Uri; import android.os.Bundle; +import android.provider.MediaStore; import android.util.Log; import android.view.LayoutInflater; import android.view.View; @@ -104,6 +105,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: From c50047de681c5ee18f1ceb5215a9ef066d80bba4 Mon Sep 17 00:00:00 2001 From: coooldoggy Date: Wed, 8 Jan 2020 14:53:33 +0900 Subject: [PATCH 2/5] Removed unused import --- .../src/main/java/com/zhihu/matisse/sample/SampleActivity.java | 1 - 1 file changed, 1 deletion(-) 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 f56e36844..ae26ad195 100644 --- a/sample/src/main/java/com/zhihu/matisse/sample/SampleActivity.java +++ b/sample/src/main/java/com/zhihu/matisse/sample/SampleActivity.java @@ -21,7 +21,6 @@ import android.content.pm.ActivityInfo; import android.net.Uri; import android.os.Bundle; -import android.provider.MediaStore; import android.util.Log; import android.view.LayoutInflater; import android.view.View; From 7c0e31a4a583dd2e9cb2284a831a8ed03d3d53a5 Mon Sep 17 00:00:00 2001 From: coooldoggy Date: Wed, 8 Jan 2020 15:07:10 +0900 Subject: [PATCH 3/5] following coding style --- .../java/com/zhihu/matisse/SelectionCreator.java | 11 +---------- .../matisse/internal/entity/SelectionSpec.java | 13 +++++++++++++ 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/matisse/src/main/java/com/zhihu/matisse/SelectionCreator.java b/matisse/src/main/java/com/zhihu/matisse/SelectionCreator.java index 444284165..4df4e29bc 100644 --- a/matisse/src/main/java/com/zhihu/matisse/SelectionCreator.java +++ b/matisse/src/main/java/com/zhihu/matisse/SelectionCreator.java @@ -19,7 +19,6 @@ import android.app.Activity; import android.content.Intent; import android.os.Build; -import android.provider.MediaStore; import androidx.annotation.IntDef; import androidx.annotation.NonNull; @@ -329,15 +328,7 @@ public SelectionCreator imageEngine(ImageEngine imageEngine) { * @return */ public SelectionCreator albumOrder(String sortBy){ - if (sortBy.equals("taken")){ - mSelectionSpec.orderCondition = MediaStore.Images.Media.DATE_TAKEN + " DESC"; - }else if (sortBy.equals("added")){ - mSelectionSpec.orderCondition = MediaStore.Images.Media.DATE_ADDED + " DESC"; - }else if (sortBy.equals("size")){ - mSelectionSpec.orderCondition = MediaStore.Images.Media.SIZE + " DESC"; - }else{ - mSelectionSpec.orderCondition = MediaStore.Images.Media.DEFAULT_SORT_ORDER + " DESC"; - } + mSelectionSpec.setOrderCondition(sortBy); return this; } 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 c0fedbe09..52330dc80 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 @@ -17,6 +17,7 @@ package com.zhihu.matisse.internal.entity; import android.content.pm.ActivityInfo; +import android.provider.MediaStore; import androidx.annotation.StyleRes; @@ -122,4 +123,16 @@ public boolean onlyShowGif() { private static final class InstanceHolder { private static final SelectionSpec INSTANCE = new SelectionSpec(); } + + public void setOrderCondition(String condition){ + if (condition.equals("taken")){ + this.orderCondition = MediaStore.Images.Media.DATE_TAKEN + " DESC"; + }else if (condition.equals("added")){ + this.orderCondition = MediaStore.Images.Media.DATE_ADDED + " DESC"; + }else if (condition.equals("size")){ + this.orderCondition = MediaStore.Images.Media.SIZE + " DESC"; + }else{ + this.orderCondition = MediaStore.Images.Media.DEFAULT_SORT_ORDER + " DESC"; + } + } } From 27244230e3f1fec622d4ac2208d54cbba825ae13 Mon Sep 17 00:00:00 2001 From: coooldoggy Date: Wed, 8 Jan 2020 15:26:07 +0900 Subject: [PATCH 4/5] following coding style --- .../internal/entity/SelectionSpec.java | 22 ++++++++++++------- .../matisse/internal/loader/AlbumLoader.java | 16 +++++--------- 2 files changed, 20 insertions(+), 18 deletions(-) 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 52330dc80..90f3673e9 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 @@ -32,7 +32,10 @@ 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 { @@ -125,14 +128,17 @@ private static final class InstanceHolder { } public void setOrderCondition(String condition){ - if (condition.equals("taken")){ - this.orderCondition = MediaStore.Images.Media.DATE_TAKEN + " DESC"; - }else if (condition.equals("added")){ - this.orderCondition = MediaStore.Images.Media.DATE_ADDED + " DESC"; - }else if (condition.equals("size")){ - this.orderCondition = MediaStore.Images.Media.SIZE + " DESC"; - }else{ - this.orderCondition = MediaStore.Images.Media.DEFAULT_SORT_ORDER + " DESC"; + 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 906ac2fb0..7f95a90b4 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 @@ -159,21 +159,17 @@ public static CursorLoader newInstance(Context context) { } switch (SelectionSpec.getInstance().orderCondition) { - case ORDER_BY_DEFAULT:{ + case "default": order = ORDER_BY_DEFAULT; - } - case ORDER_BY_DATE_TAKEN:{ + case "taken": order = ORDER_BY_DATE_TAKEN; - } - case ORDER_BY_MEDIA_ADDED:{ + case "added": order = ORDER_BY_MEDIA_ADDED; - } - case ORDER_BY_SIZE:{ + case "size": order = ORDER_BY_SIZE; - } - default:{ + default: order = ORDER_BY_DEFAULT; - } + } return new AlbumLoader(context, selection, selectionArgs, order); } From ee0c6c28ae17e4782bc8a0f16890a3afc9ec08e1 Mon Sep 17 00:00:00 2001 From: yulim Date: Wed, 8 Jan 2020 21:40:39 +0900 Subject: [PATCH 5/5] following code style --- .../src/main/java/com/zhihu/matisse/SelectionCreator.java | 5 +++-- .../com/zhihu/matisse/internal/entity/SelectionSpec.java | 5 ++--- .../java/com/zhihu/matisse/internal/loader/AlbumLoader.java | 1 - 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/matisse/src/main/java/com/zhihu/matisse/SelectionCreator.java b/matisse/src/main/java/com/zhihu/matisse/SelectionCreator.java index 4df4e29bc..12ab39d6e 100644 --- a/matisse/src/main/java/com/zhihu/matisse/SelectionCreator.java +++ b/matisse/src/main/java/com/zhihu/matisse/SelectionCreator.java @@ -164,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) @@ -217,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. */ @@ -327,7 +328,7 @@ public SelectionCreator imageEngine(ImageEngine imageEngine) { * @param sortBy * @return */ - public SelectionCreator albumOrder(String sortBy){ + public SelectionCreator albumOrder(String sortBy) { mSelectionSpec.setOrderCondition(sortBy); return this; } 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 90f3673e9..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 @@ -17,7 +17,6 @@ package com.zhihu.matisse.internal.entity; import android.content.pm.ActivityInfo; -import android.provider.MediaStore; import androidx.annotation.StyleRes; @@ -127,8 +126,8 @@ private static final class InstanceHolder { private static final SelectionSpec INSTANCE = new SelectionSpec(); } - public void setOrderCondition(String condition){ - switch (condition){ + public void setOrderCondition(String condition) { + switch (condition) { case "default": this.orderCondition = ORDER_BY_DEFAULT; case "taken": 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 7f95a90b4..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 @@ -169,7 +169,6 @@ public static CursorLoader newInstance(Context context) { order = ORDER_BY_SIZE; default: order = ORDER_BY_DEFAULT; - } return new AlbumLoader(context, selection, selectionArgs, order); }