diff --git a/README.md b/README.md index 267945b..1b55d6f 100644 --- a/README.md +++ b/README.md @@ -133,6 +133,7 @@ You can customize something ...
* `showTitle(Boolean) (default: true)` * `setTitleBackgroundResId(R.color.xxx)` * `setImageProvider(ImageProvider)` +* `setOnErrorListener(OnErrorListener onErrorListener)` : If you want load grid image yourself, you can use your ImageProvider #### Single Select diff --git a/app/src/main/java/gun0912/tedbottompickerdemo/MainActivity.java b/app/src/main/java/gun0912/tedbottompickerdemo/MainActivity.java index 5ebb9eb..52ce184 100644 --- a/app/src/main/java/gun0912/tedbottompickerdemo/MainActivity.java +++ b/app/src/main/java/gun0912/tedbottompickerdemo/MainActivity.java @@ -80,6 +80,13 @@ public void onImageSelected(final Uri uri) { .setSelectedUri(selectedUri) //.showVideoMedia() .setPeekHeight(1200) + .setOnErrorListener(new TedBottomPicker.OnErrorListener() { + @Override + public void onError(Throwable throwable) { + throwable.printStackTrace(); + Toast.makeText(getBaseContext(), "Error : " + throwable.getMessage(), Toast.LENGTH_SHORT).show(); + } + }) .create(); bottomSheetDialogFragment.show(getSupportFragmentManager()); @@ -131,6 +138,13 @@ public void onImagesSelected(ArrayList uriList) { .setCompleteButtonText("Done") .setEmptySelectionText("No Select") .setSelectedUriList(selectedUriList) + .setOnErrorListener(new TedBottomPicker.OnErrorListener() { + @Override + public void onError(Throwable throwable) { + throwable.printStackTrace(); + Toast.makeText(getBaseContext(), "Error : " + throwable.getMessage(), Toast.LENGTH_SHORT).show(); + } + }) .create(); bottomSheetDialogFragment.show(getSupportFragmentManager()); diff --git a/tedbottompicker/src/main/java/gun0912/tedbottompicker/TedBottomPicker.java b/tedbottompicker/src/main/java/gun0912/tedbottompicker/TedBottomPicker.java index 7399bc4..8d6c9cd 100644 --- a/tedbottompicker/src/main/java/gun0912/tedbottompicker/TedBottomPicker.java +++ b/tedbottompicker/src/main/java/gun0912/tedbottompicker/TedBottomPicker.java @@ -59,6 +59,7 @@ import java.util.Locale; import gun0912.tedbottompicker.adapter.GalleryAdapter; +import gun0912.tedbottompicker.exception.OnErrorNotImplementedException; import gun0912.tedbottompicker.util.RealPathUtil; public class TedBottomPicker extends BottomSheetDialogFragment { @@ -282,7 +283,8 @@ public void onItemClick(View view, int position) { GalleryAdapter.PickerTile pickerTile = imageGalleryAdapter.getItem(position); - switch (pickerTile.getTileType()) { + int tileType = pickerTile.getTileType(); + switch (tileType) { case GalleryAdapter.PickerTile.CAMERA: startCameraIntent(); break; @@ -295,7 +297,7 @@ public void onItemClick(View view, int position) { break; default: - errorMessage(); + onError(new IllegalStateException("Unexpected type : " + tileType)); } } @@ -428,7 +430,7 @@ private void startCameraIntent() { } if (cameraInent.resolveActivity(getActivity().getPackageManager()) == null) { - errorMessage("This Application do not have Camera Application"); + onError("This Application do not have Camera Application"); return; } @@ -478,7 +480,7 @@ private File getImageFile() { cameraImageUri = Uri.fromFile(imageFile); } catch (IOException e) { e.printStackTrace(); - errorMessage("Could not create imageFile for camera"); + onError("Could not create imageFile for camera"); } @@ -507,21 +509,21 @@ private File getVideoFile() { cameraImageUri = Uri.fromFile(videoFile); } catch (IOException e) { e.printStackTrace(); - errorMessage("Could not create imageFile for camera"); + onError("Could not create imageFile for camera"); } return videoFile; } - private void errorMessage(String message) { + private void onError(String message) { String errorMessage = message == null ? "Something wrong." : message; - if (builder.onErrorListener == null) { - Toast.makeText(getActivity(), errorMessage, Toast.LENGTH_SHORT).show(); - } else { - builder.onErrorListener.onError(errorMessage); - } + onError(new RuntimeException(errorMessage)); + } + + private void onError(Throwable throwable) { + builder.onErrorListener.onError(throwable); } private void startGalleryIntent() { @@ -537,7 +539,7 @@ private void startGalleryIntent() { } if (galleryIntent.resolveActivity(getActivity().getPackageManager()) == null) { - errorMessage("This Application do not have Gallery Application"); + onError("This Application do not have Gallery Application"); return; } @@ -554,10 +556,6 @@ public void onActivityResult(int resultCode, Intent data) { .startActivityForResult(); } - private void errorMessage() { - errorMessage(null); - } - private void setTitle() { if (!builder.showTitle) { @@ -611,7 +609,8 @@ private void onActivityResultGallery(Intent data) { Uri temp = data.getData(); if (temp == null) { - errorMessage(); + onError(new NullPointerException("Intent data is null")); + return; } String realPath = RealPathUtil.getRealPath(getActivity(), temp); @@ -637,7 +636,7 @@ public interface OnImageSelectedListener { } public interface OnErrorListener { - void onError(String message); + void onError(Throwable throwable); } public interface ImageProvider { @@ -683,9 +682,17 @@ public static class Builder implements Serializable { ArrayList selectedUriList; Uri selectedUri; + private OnErrorListener defaultErrorListener = new OnErrorListener() { + @Override + public void onError(Throwable throwable) { + throw new OnErrorNotImplementedException("Error listener is not implemented", throwable); + } + }; + public Builder(@NonNull Context context) { this.context = context; + this.onErrorListener = defaultErrorListener; setCameraTile(R.drawable.ic_camera); setGalleryTile(R.drawable.ic_gallery); diff --git a/tedbottompicker/src/main/java/gun0912/tedbottompicker/adapter/GalleryAdapter.java b/tedbottompicker/src/main/java/gun0912/tedbottompicker/adapter/GalleryAdapter.java index f13bfb2..382f3ff 100644 --- a/tedbottompicker/src/main/java/gun0912/tedbottompicker/adapter/GalleryAdapter.java +++ b/tedbottompicker/src/main/java/gun0912/tedbottompicker/adapter/GalleryAdapter.java @@ -100,6 +100,7 @@ public GalleryAdapter(Context context, TedBottomPicker.Builder builder) { } catch (Exception e) { e.printStackTrace(); + onError(e); } finally { if (cursor != null && !cursor.isClosed()) { cursor.close(); @@ -220,6 +221,9 @@ public interface OnItemClickListener { public void onItemClick(View view, int position); } + private void onError(Throwable throwable){ + builder.onErrorListener.onError(throwable); + } public static class PickerTile { @@ -263,7 +267,7 @@ public String toString() { } else if (isGalleryTile()) { return "PickerTile"; } else { - return "Invalid item"; + throw new IllegalStateException("Invalid item"); } } diff --git a/tedbottompicker/src/main/java/gun0912/tedbottompicker/exception/OnErrorNotImplementedException.java b/tedbottompicker/src/main/java/gun0912/tedbottompicker/exception/OnErrorNotImplementedException.java new file mode 100644 index 0000000..4df80f3 --- /dev/null +++ b/tedbottompicker/src/main/java/gun0912/tedbottompicker/exception/OnErrorNotImplementedException.java @@ -0,0 +1,20 @@ +package gun0912.tedbottompicker.exception; + +public class OnErrorNotImplementedException extends RuntimeException { + + public OnErrorNotImplementedException() { + super(); + } + + public OnErrorNotImplementedException(String message) { + super(message); + } + + public OnErrorNotImplementedException(String message, Throwable cause) { + super(message, cause); + } + + public OnErrorNotImplementedException(Throwable cause) { + super(cause); + } +}