Skip to content
This repository has been archived by the owner on Oct 26, 2022. It is now read-only.

Commit

Permalink
Merge pull request #78 from nohana/add-size-filter
Browse files Browse the repository at this point in the history
Add size filter
  • Loading branch information
hiroyuki-seto authored Jan 22, 2018
2 parents e1e8523 + dd08f6c commit cb7721a
Show file tree
Hide file tree
Showing 9 changed files with 224 additions and 2 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,30 @@ Laevatein.from(this)
.forResult(REQUEST_CODE_CHOOSE);
```

### Selectable photo size limitation

Set selectable photo size by pixel with `size(int, int)`.
Default is `0 <= pixels <= Integer.MAX_VALUE`.

```java
Laevatein.from(this)
.choose(MimeType.of(MimeType.JPEG))
.size(300, 400) // minimum width = 300px, minimum height = 400px;
.forResult(REQUEST_CODE_CHOOSE);
```

### Selectable photo size limitation

Set selectable photo size by pixel with `size(int, int, int, int)`.
Default is `0 <= pixels <= Integer.MAX_VALUE`.

```java
Laevatein.from(this)
.choose(MimeType.of(MimeType.JPEG))
.size(300, 400, Integer.MAX_VALUE, Integer.MAX_VALUE) // minimum width = 300px, minimum height = 400px, max width = Integer.MAX_VALUEpx, max height = Integer.MAX_VALUEpx;
.forResult(REQUEST_CODE_CHOOSE);
```

### Use custom cell layout

Set your layout and ids for the image cell with `bindEachImageWith(int, int, int)`.
Expand Down
75 changes: 75 additions & 0 deletions laevatein/src/main/java/com/laevatein/SelectionSpecBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public final class SelectionSpecBuilder {
private ErrorViewResources mCountOverErrorSpec;
private ErrorViewResources mUnderQualityErrorSpec;
private ErrorViewResources mOverQualityErrorSpec;
private ErrorViewResources mUnderSizeErrorSpec;
private ErrorViewResources mOverSizeErrorSpec;
private ErrorViewResources mTypeErrorSpec;
private DialogResources mConfirmDialogSpec;
private boolean mOpenDrawer = true;
Expand Down Expand Up @@ -243,6 +245,50 @@ public SelectionSpecBuilder overQuality(ErrorViewResources.ViewType type, int er
return this;
}

/**
* Sets the error view specification for the error of size un-satisfaction.
* @param type error view type.
* @param errorMessageId an error message resource id.
* @return the specification builder context.
*/
public SelectionSpecBuilder underSize(ErrorViewResources.ViewType type, int errorMessageId) {
return underSize(type, -1, errorMessageId);
}

/**
* Sets the error view specification for the error of size un-satisfaction.
* @param type error view type.
* @param errorTitleId an error title resource id. If type is not {@see ErrorViewResources.ViewType.DIALOG}, this parameter is ignored.
* @param errorMessageId an error message resource id.
* @return the specification builder context.
*/
public SelectionSpecBuilder underSize(ErrorViewResources.ViewType type, int errorTitleId, int errorMessageId) {
mUnderSizeErrorSpec = type.createSpec(errorTitleId, errorMessageId);
return this;
}

/**
* Sets the error view specification for the error of size un-satisfaction..
* @param type error view type.
* @param errorMessageId an error message resource id.
* @return the specification builder context.
*/
public SelectionSpecBuilder overSize(ErrorViewResources.ViewType type, int errorMessageId) {
return overSize(type, -1, errorMessageId);
}

/**
* Sets the error view specification for the error of size un-satisfaction..
* @param type error view type.
* @param errorTitleId an error title resource id. If type is not {@see ErrorViewResources.ViewType.DIALOG}, this parameter is ignored.
* @param errorMessageId an error message resource id.
* @return the specification builder context.
*/
public SelectionSpecBuilder overSize(ErrorViewResources.ViewType type, int errorTitleId, int errorMessageId) {
mOverSizeErrorSpec = type.createSpec(errorTitleId, errorMessageId);
return this;
}

/**
* Sets the error view specification for the error of type validation.
* @param type error view type.
Expand Down Expand Up @@ -297,6 +343,33 @@ public SelectionSpecBuilder quality(int minPixel, int maxPixel) {
return this;
}

/**
* Sets the limitation of a selectable image size by pixel count minimum.
*
* @param minWidth minimum width value to select.
* @param minHeight minimum width value to select.
* @return the specification builder context.
*/
public SelectionSpecBuilder size(int minWidth, int minHeight) {
size(minWidth, minHeight, Integer.MAX_VALUE, Integer.MAX_VALUE);
return this;
}

/**
* Sets the limitation of a selectable image size by pixel count minimum and maximum.
*
* @param minWidth minimum width value to select.
* @param minHeight minimum width value to select.
* @param maxWidth maximum height value to select.
* @param maxHeight maximum height value to select.
* @return the specification builder context.
*/
public SelectionSpecBuilder size(int minWidth, int minHeight, int maxWidth, int maxHeight) {
mSelectionSpec.setMinSize(minWidth, minHeight);
mSelectionSpec.setMaxSize(maxWidth, maxHeight);
return this;
}

/**
* Sets the Activity instead of PhotoSelectionActivity
* @param photoSelectionActivityClass an Activity called on photo selecting
Expand Down Expand Up @@ -400,6 +473,8 @@ public void forResultWithTransition(int requestCode, View view) {
.setCountOverSpec(mCountOverErrorSpec)
.setOverQualitySpec(mOverQualityErrorSpec)
.setUnderQualitySpec(mUnderQualityErrorSpec)
.setOverSizeSpec(mOverSizeErrorSpec)
.setUnderSizeSpec(mUnderSizeErrorSpec)
.setTypeSpec(mTypeErrorSpec)
.setConfirmSpec(mConfirmDialogSpec)
.create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public ErrorViewSpec[] newArray(int size) {
private final ErrorViewResources mCountOverErrorSpec;
private final ErrorViewResources mUnderQualitySpec;
private final ErrorViewResources mOverQualitySpec;
private final ErrorViewResources mUnderSizeSpec;
private final ErrorViewResources mOverSizeSpec;
private final ErrorViewResources mTypeErrorSpec;
private final DialogResources mBackConfirmSpec;

Expand All @@ -37,17 +39,22 @@ public ErrorViewSpec[] newArray(int size) {
mCountOverErrorSpec = source.readParcelable(ErrorViewResources.class.getClassLoader());
mUnderQualitySpec = source.readParcelable(ErrorViewResources.class.getClassLoader());
mOverQualitySpec = source.readParcelable(ErrorViewResources.class.getClassLoader());
mUnderSizeSpec = source.readParcelable(ErrorViewResources.class.getClassLoader());
mOverSizeSpec = source.readParcelable(ErrorViewResources.class.getClassLoader());
mTypeErrorSpec = source.readParcelable(ErrorViewResources.class.getClassLoader());
mBackConfirmSpec = source.readParcelable(DialogResources.class.getClassLoader());
}

/* package */ ErrorViewSpec(ErrorViewResources countUnderSpec, ErrorViewResources countOverSpec,
ErrorViewResources underQualitySpec, ErrorViewResources overQualitySpec,
ErrorViewResources underSizeSpec, ErrorViewResources overSizeSpec,
ErrorViewResources typeErrorSpec, DialogResources backConfirmSpec) {
mCountUnderErrorSpec = countUnderSpec;
mCountOverErrorSpec = countOverSpec;
mUnderQualitySpec = underQualitySpec;
mOverQualitySpec = overQualitySpec;
mUnderSizeSpec = underSizeSpec;
mOverSizeSpec = overSizeSpec;
mTypeErrorSpec = typeErrorSpec;
mBackConfirmSpec = backConfirmSpec;
}
Expand All @@ -63,6 +70,8 @@ public void writeToParcel(Parcel dest, int flags) {
dest.writeParcelable(mCountOverErrorSpec, flags);
dest.writeParcelable(mUnderQualitySpec, flags);
dest.writeParcelable(mOverQualitySpec, flags);
dest.writeParcelable(mUnderSizeSpec, flags);
dest.writeParcelable(mOverSizeSpec, flags);
dest.writeParcelable(mTypeErrorSpec, flags);
dest.writeParcelable(mBackConfirmSpec, flags);
}
Expand All @@ -83,6 +92,14 @@ public ErrorViewResources getOverQualitySpec() {
return mOverQualitySpec;
}

public ErrorViewResources getUnderSizeSpec() {
return mUnderSizeSpec;
}

public ErrorViewResources getOverSizeSpec() {
return mOverSizeSpec;
}

public ErrorViewResources getTypeErrorSpec() {
return mTypeErrorSpec;
}
Expand All @@ -96,6 +113,8 @@ public static class Builder {
private ErrorViewResources mCountOverSpec;
private ErrorViewResources mUnderQualitySpec;
private ErrorViewResources mOverQualitySpec;
private ErrorViewResources mUnderSizeSpec;
private ErrorViewResources mOverSizeSpec;
private ErrorViewResources mTypeSpec;
private DialogResources mBackSpec;

Expand All @@ -119,6 +138,16 @@ public Builder setOverQualitySpec(ErrorViewResources spec) {
return this;
}

public Builder setUnderSizeSpec(ErrorViewResources spec) {
mUnderSizeSpec = spec;
return this;
}

public Builder setOverSizeSpec(ErrorViewResources spec) {
mOverSizeSpec = spec;
return this;
}

public Builder setTypeSpec(ErrorViewResources spec) {
mTypeSpec = spec;
return this;
Expand All @@ -142,13 +171,20 @@ public ErrorViewSpec create() {
if (mOverQualitySpec == null) {
mOverQualitySpec = ErrorViewResources.ViewType.DIALOG.createSpec(-1, R.string.l_error_quality);
}
if (mUnderSizeSpec == null) {
mUnderSizeSpec = ErrorViewResources.ViewType.DIALOG.createSpec(-1, R.string.l_error_size);
}
if (mOverSizeSpec == null) {
mOverSizeSpec = ErrorViewResources.ViewType.DIALOG.createSpec(-1, R.string.l_error_size);
}
if (mTypeSpec == null) {
mTypeSpec = ErrorViewResources.ViewType.DIALOG.createSpec(-1, R.string.l_error_invalid_format);
}
if (mBackSpec == null) {
mBackSpec = new DialogResources(R.string.l_confirm_dialog_title, R.string.l_confirm_dialog_message);
}
return new ErrorViewSpec(mCountUnderSpec, mCountOverSpec, mUnderQualitySpec, mOverQualitySpec, mTypeSpec, mBackSpec);
return new ErrorViewSpec(mCountUnderSpec, mCountOverSpec, mUnderQualitySpec,
mOverQualitySpec, mUnderSizeSpec, mOverSizeSpec, mTypeSpec, mBackSpec);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,32 @@ public SelectionSpec[] newArray(int size) {
private int mMinSelectable;
private long mMinPixels;
private long mMaxPixels;
private int mMinWidthPixels;
private int mMinHeightPixels;
private int mMaxWidthPixels;
private int mMaxHeightPixels;
private Set<MimeType> mMimeTypeSet;

public SelectionSpec() {
mMinSelectable = 0;
mMaxSelectable = 1;
mMinPixels = 0L;
mMaxPixels = Long.MAX_VALUE;
mMinWidthPixels = 0;
mMinHeightPixels = 0;
mMaxWidthPixels = Integer.MAX_VALUE;
mMaxHeightPixels = Integer.MAX_VALUE;
}

/* package */ SelectionSpec(Parcel source) {
mMinSelectable = source.readInt();
mMaxSelectable = source.readInt();
mMinPixels = source.readLong();
mMaxPixels = source.readLong();
mMinWidthPixels = source.readInt();
mMinHeightPixels = source.readInt();
mMaxWidthPixels = source.readInt();
mMaxHeightPixels = source.readInt();
List<MimeType> list = new ArrayList<>();
source.readList(list, MimeType.class.getClassLoader());
mMimeTypeSet = EnumSet.copyOf(list);
Expand All @@ -77,6 +89,10 @@ public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(mMaxSelectable);
dest.writeLong(mMinPixels);
dest.writeLong(mMaxPixels);
dest.writeInt(mMinWidthPixels);
dest.writeInt(mMinHeightPixels);
dest.writeInt(mMaxWidthPixels);
dest.writeInt(mMaxHeightPixels);
dest.writeList(new ArrayList<>(mMimeTypeSet));
}

Expand All @@ -96,6 +112,16 @@ public void setMaxPixels(long maxPixels) {
mMaxPixels = maxPixels;
}

public void setMinSize(int minWidthPixels, int minHeightPixels) {
this.mMinWidthPixels = minWidthPixels;
this.mMinHeightPixels = minHeightPixels;
}

public void setMaxSize(int maxWidthPixels, int maxHeightPixels) {
this.mMaxWidthPixels = maxWidthPixels;
this.mMaxHeightPixels = maxHeightPixels;
}

public void setMimeTypeSet(Set<MimeType> set) {
mMimeTypeSet = set;
}
Expand All @@ -116,6 +142,22 @@ public long getMaxPixels() {
return mMaxPixels;
}

public int getMinWidthPixels() {
return mMinWidthPixels;
}

public int getMinHeightPixels() {
return mMinHeightPixels;
}

public int getMaxWidthPixels() {
return mMaxWidthPixels;
}

public int getMaxHeightPixels() {
return mMaxHeightPixels;
}

public Set<MimeType> getMimeTypeSet() {
return mMimeTypeSet;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ public ErrorViewResources getErrorResources(ErrorViewSpec spec) {
return spec.getOverQualitySpec();
}
},
UNDER_SIZE {
@Override
public ErrorViewResources getErrorResources(ErrorViewSpec spec) {
return spec.getUnderSizeSpec();
}
},
OVER_SIZE {
@Override
public ErrorViewResources getErrorResources(ErrorViewSpec spec) {
return spec.getOverSizeSpec();
}
},
FILE_TYPE {
@Override
public ErrorViewResources getErrorResources(ErrorViewSpec spec) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
*/
public final class PhotoMetadataUtils {
public static final String TAG = PhotoMetadataUtils.class.getSimpleName();
private static final int MAX_WIDTH = 1600;
private static final String SCHEME_CONTENT = "content";

private PhotoMetadataUtils() {
Expand Down Expand Up @@ -103,6 +102,12 @@ public static UncapableCause isAcceptable(Context context, SelectionSpec spec, U
if (!hasOverAtLeastQuality(context, spec, uri)) {
return UncapableCause.UNDER_QUALITY;
}
if (!isSmallerThanMaxSize(context, spec, uri)) {
return UncapableCause.OVER_SIZE;
}
if (!isLargerThanMinSize(context, spec, uri)) {
return UncapableCause.UNDER_SIZE;
}
return null;
}

Expand All @@ -124,6 +129,23 @@ public static boolean hasUnderAtMostQuality(Context context, SelectionSpec spec,
return pixels <= spec.getMaxPixels();
}

public static boolean isLargerThanMinSize(Context context, SelectionSpec spec, Uri uri) {
if (context == null) {
return false;
}
Point p = PhotoMetadataUtils.getBitmapBound(context.getContentResolver(), uri);
return p.x >= spec.getMinWidthPixels() && p.y >= spec.getMinHeightPixels();
}

public static boolean isSmallerThanMaxSize(Context context, SelectionSpec spec, Uri uri) {
if (context == null) {
return false;
}

Point p = PhotoMetadataUtils.getBitmapBound(context.getContentResolver(), uri);
return p.x <= spec.getMaxWidthPixels() && p.y <= spec.getMaxHeightPixels();
}

public static boolean isSelectableType(Context context, SelectionSpec spec, Uri uri) {
if (context == null) {
return false;
Expand Down
1 change: 1 addition & 0 deletions laevatein/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

<string name="l_error_invalid_format">Not allowed to select this photo due to invalid file format.</string>
<string name="l_error_quality">Not allowed to select this photo due to not enough quality.</string>
<string name="l_error_size">Not allowed to select this photo due to not suitable size.</string>

<string name="l_album_name_all">All Photos</string>
<string name="l_album_name_selected">Selected Photos</string>
Expand Down
Loading

0 comments on commit cb7721a

Please sign in to comment.