Android supports detecting supported types
This commit is contained in:
parent
4e470e994e
commit
e8af34ef61
@ -22,15 +22,30 @@ import java.util.concurrent.TimeUnit;
|
||||
*/
|
||||
public class GalleryAdapter extends RecyclerView.Adapter<GalleryAdapter.StupidHolder> {
|
||||
|
||||
private class Image {
|
||||
String uri;
|
||||
Integer id;
|
||||
String mimeType;
|
||||
|
||||
public Image(String uri, Integer id, String mimeType) {
|
||||
this.uri = uri;
|
||||
this.id = id;
|
||||
this.mimeType = mimeType;
|
||||
}
|
||||
}
|
||||
|
||||
public static final String[] PROJECTION = new String[]{
|
||||
MediaStore.Images.Media.DATA,
|
||||
MediaStore.Images.Media._ID
|
||||
MediaStore.Images.Media._ID,
|
||||
MediaStore.Images.Media.MIME_TYPE
|
||||
};
|
||||
|
||||
private ArrayList<String> uris = new ArrayList<>();
|
||||
private ArrayList<Integer> ids = new ArrayList<>();
|
||||
// private ArrayList<String> uris = new ArrayList<>();
|
||||
// private ArrayList<Integer> ids = new ArrayList<>();
|
||||
private ArrayList<Image> images = new ArrayList<>();
|
||||
|
||||
private ArrayList<String> selectedUris = new ArrayList<>();
|
||||
private ArrayList<String> supportedFileTypes = new ArrayList<>();
|
||||
private String albumName = "";
|
||||
private Drawable selectedDrawable;
|
||||
private Drawable unselectedDrawable;
|
||||
@ -53,12 +68,17 @@ public class GalleryAdapter extends RecyclerView.Adapter<GalleryAdapter.StupidHo
|
||||
return unselectedDrawable;
|
||||
}
|
||||
|
||||
public void setSupportedFileTypes(ArrayList<String> supportedFileTypes) {
|
||||
this.supportedFileTypes = supportedFileTypes;
|
||||
}
|
||||
|
||||
public class StupidHolder extends RecyclerView.ViewHolder {
|
||||
public StupidHolder(View itemView) {
|
||||
super(itemView);
|
||||
}
|
||||
int id;
|
||||
String uri;
|
||||
// int id;
|
||||
// String uri;
|
||||
Image image;
|
||||
}
|
||||
|
||||
private GalleryView view;
|
||||
@ -88,8 +108,9 @@ public class GalleryAdapter extends RecyclerView.Adapter<GalleryAdapter.StupidHo
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
ids.clear();
|
||||
uris.clear();
|
||||
// ids.clear();
|
||||
// uris.clear();
|
||||
images.clear();
|
||||
|
||||
String selection = "";
|
||||
String[] args = null;
|
||||
@ -109,14 +130,17 @@ public class GalleryAdapter extends RecyclerView.Adapter<GalleryAdapter.StupidHo
|
||||
if (cursor.moveToFirst()) {
|
||||
int dataIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
|
||||
int idIndex = cursor.getColumnIndex(MediaStore.Images.Media._ID);
|
||||
int mimeIndex = cursor.getColumnIndex(MediaStore.Images.Media.MIME_TYPE);
|
||||
do {
|
||||
uris.add(cursor.getString(dataIndex));
|
||||
ids.add(cursor.getInt(idIndex));
|
||||
// uris.add(cursor.getString(dataIndex));
|
||||
// ids.add(cursor.getInt(idIndex));
|
||||
images.add(new Image(cursor.getString(dataIndex), cursor.getInt(idIndex), cursor.getString(mimeIndex)));
|
||||
} while(cursor.moveToNext());
|
||||
}
|
||||
|
||||
Collections.reverse(uris);
|
||||
Collections.reverse(ids);
|
||||
//
|
||||
// Collections.reverse(uris);
|
||||
// Collections.reverse(ids);
|
||||
Collections.reverse(images);
|
||||
|
||||
cursor.close();
|
||||
|
||||
@ -141,10 +165,10 @@ public class GalleryAdapter extends RecyclerView.Adapter<GalleryAdapter.StupidHo
|
||||
});
|
||||
}
|
||||
|
||||
private void setData(ArrayList<Integer> ids, ArrayList<String> uris) {
|
||||
this.ids = ids;
|
||||
this.uris = uris;
|
||||
}
|
||||
// private void setData(ArrayList<Integer> ids, ArrayList<String> uris) {
|
||||
// this.ids = ids;
|
||||
// this.uris = uris;
|
||||
// }
|
||||
|
||||
|
||||
@Override
|
||||
@ -158,21 +182,37 @@ public class GalleryAdapter extends RecyclerView.Adapter<GalleryAdapter.StupidHo
|
||||
@Override
|
||||
public void onBindViewHolder(final StupidHolder holder, final int position) {
|
||||
final SelectableImage selectableImageView = (SelectableImage)holder.itemView;
|
||||
holder.id = ids.get(position);
|
||||
holder.uri = uris.get(position);
|
||||
boolean selected = (selectedUris.indexOf(holder.uri) + 1) > 0;
|
||||
// holder.id = ids.get(position);
|
||||
// holder.uri = uris.get(position);
|
||||
// holder.supported =
|
||||
holder.image = images.get(position);
|
||||
boolean selected = (selectedUris.indexOf(holder.image.uri) + 1) > 0;
|
||||
boolean supported = isSupported(holder.image);
|
||||
selectableImageView.setDrawables(selectedDrawable, unselectedDrawable);
|
||||
selectableImageView.bind(executor, selected, holder.id);
|
||||
selectableImageView.bind(executor, selected, holder.image.id, supported);
|
||||
selectableImageView.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
view.onTapImage(holder.uri);
|
||||
view.onTapImage(holder.image.uri);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private boolean isSupported(Image image) {
|
||||
if(supportedFileTypes.isEmpty()) {
|
||||
return true;
|
||||
} else {
|
||||
for(String supportedMime : supportedFileTypes) {
|
||||
if (image.mimeType.toLowerCase().equals(supportedMime.toLowerCase())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return ids.size();
|
||||
return images.size();
|
||||
}
|
||||
}
|
||||
|
||||
@ -125,4 +125,8 @@ public class GalleryView extends RecyclerView {
|
||||
rctEventEmitter.receiveEvent(getId(), "onTapImage", event);
|
||||
}
|
||||
}
|
||||
|
||||
public void setSupportedFileTypes(ArrayList<String> supportedFileTypes) {
|
||||
adapter.setSupportedFileTypes(supportedFileTypes);
|
||||
}
|
||||
}
|
||||
|
||||
@ -99,6 +99,47 @@ public class GalleryViewManager extends SimpleViewManager<GalleryView> {
|
||||
}).start();
|
||||
}
|
||||
|
||||
// fileTypeSupport={{
|
||||
// supportedFileTypes: ['image/jpeg', 'image/png'],
|
||||
// unsupportedOverlayColor: "#00000055",
|
||||
// unsupportedImage: require('./images/unsupportedImage.png'),
|
||||
// unsupportedText: 'Unsupported',
|
||||
// unsupportedTextColor: '#ffffff'
|
||||
// }}
|
||||
|
||||
@ReactProp(name = "fileTypeSupport")
|
||||
public void setFileTypeSupport(final GalleryView view, final ReadableMap fileTypeSupport) {
|
||||
final ReadableArray supportedFileTypes = fileTypeSupport.getArray("supportedFileTypes");
|
||||
final String unsupportedOverlayColor = fileTypeSupport.getString("unsupportedOverlayColor");
|
||||
final String unsupportedImageSource = fileTypeSupport.getString("unsupportedImage");
|
||||
final String unsupportedText = fileTypeSupport.getString("unsupportedText");
|
||||
final String unsupportedTextColor = fileTypeSupport.getString("unsupportedTextColor");
|
||||
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
Drawable unsupportedImage = null;
|
||||
if(unsupportedImageSource != null) {
|
||||
unsupportedImage = ResourceDrawableIdHelper.getIcon(view.getContext(), unsupportedImageSource);
|
||||
}
|
||||
final ArrayList<String> supportedFileTypesList = new ArrayList<String>();
|
||||
if(supportedFileTypes != null && supportedFileTypes.size() != 0) {
|
||||
for (int i = 0; i < supportedFileTypes.size(); i++) {
|
||||
supportedFileTypesList.add(supportedFileTypes.getString(i));
|
||||
}
|
||||
}
|
||||
|
||||
reactContext.runOnUiQueueThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
view.setSupportedFileTypes(supportedFileTypesList);
|
||||
}
|
||||
});
|
||||
}
|
||||
}).start();
|
||||
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public Map getExportedCustomDirectEventTypeConstants() {
|
||||
|
||||
@ -32,6 +32,7 @@ public class SelectableImage extends FrameLayout {
|
||||
private Runnable currentLoader;
|
||||
private Drawable selectedDrawable;
|
||||
private Drawable unselectedDrawable;
|
||||
private FrameLayout unsupportedFrame;
|
||||
|
||||
public SelectableImage(Context context) {
|
||||
super(context);
|
||||
@ -42,6 +43,13 @@ public class SelectableImage extends FrameLayout {
|
||||
LayoutParams params = new FrameLayout.LayoutParams(dp22, dp22, Gravity.TOP | Gravity.RIGHT);
|
||||
params.setMargins(30,30,30,30);
|
||||
addView(selectedView, params);
|
||||
createUnsupportedView();
|
||||
}
|
||||
|
||||
private void createUnsupportedView() {
|
||||
unsupportedFrame = new FrameLayout(getContext());
|
||||
unsupportedFrame.setBackgroundColor(Color.RED);
|
||||
addView(unsupportedFrame, MATCH_PARENT, MATCH_PARENT);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -53,7 +61,7 @@ public class SelectableImage extends FrameLayout {
|
||||
imageView.setScaleType(scaleType);
|
||||
}
|
||||
|
||||
public void bind(ThreadPoolExecutor executor, boolean selected, final Integer id) {
|
||||
public void bind(ThreadPoolExecutor executor, boolean selected, final Integer id, boolean supported) {
|
||||
// selectedView.setVisibility(selected ? VISIBLE : INVISIBLE);
|
||||
selectedView.setImageDrawable(selected ? selectedDrawable : unselectedDrawable);
|
||||
if (this.id != id) {
|
||||
@ -84,6 +92,7 @@ public class SelectableImage extends FrameLayout {
|
||||
};
|
||||
executor.execute(currentLoader);
|
||||
}
|
||||
unsupportedFrame.setVisibility(supported ? GONE : VISIBLE);
|
||||
}
|
||||
|
||||
public void setDrawables(Drawable selectedDrawable, Drawable unselectedDrawable) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user