2016-11-26 06:37:23 +00:00
|
|
|
package org.thoughtcrime.securesms.components;
|
|
|
|
|
|
|
|
|
2019-01-17 20:15:00 +00:00
|
|
|
import android.annotation.TargetApi;
|
2016-11-26 06:37:23 +00:00
|
|
|
import android.content.Context;
|
|
|
|
import android.database.Cursor;
|
|
|
|
import android.net.Uri;
|
2018-11-20 17:59:23 +00:00
|
|
|
import android.os.Build;
|
2016-11-26 06:37:23 +00:00
|
|
|
import android.os.Bundle;
|
|
|
|
import android.provider.MediaStore;
|
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
import android.support.annotation.Nullable;
|
|
|
|
import android.support.v4.app.LoaderManager;
|
|
|
|
import android.support.v4.content.Loader;
|
|
|
|
import android.support.v7.widget.DefaultItemAnimator;
|
|
|
|
import android.support.v7.widget.LinearLayoutManager;
|
|
|
|
import android.support.v7.widget.RecyclerView;
|
|
|
|
import android.util.AttributeSet;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.FrameLayout;
|
|
|
|
import android.widget.ImageView;
|
|
|
|
|
|
|
|
import com.bumptech.glide.load.Key;
|
2017-01-08 21:06:11 +00:00
|
|
|
import com.bumptech.glide.load.engine.DiskCacheStrategy;
|
2016-11-26 06:37:23 +00:00
|
|
|
import com.bumptech.glide.signature.MediaStoreSignature;
|
|
|
|
|
2019-07-24 02:30:23 +00:00
|
|
|
import network.loki.messenger.R;
|
2016-11-26 06:37:23 +00:00
|
|
|
import org.thoughtcrime.securesms.database.CursorRecyclerViewAdapter;
|
|
|
|
import org.thoughtcrime.securesms.database.loaders.RecentPhotosLoader;
|
2017-10-12 00:12:46 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.GlideApp;
|
2016-11-26 06:37:23 +00:00
|
|
|
import org.thoughtcrime.securesms.util.ViewUtil;
|
|
|
|
|
|
|
|
public class RecentPhotoViewRail extends FrameLayout implements LoaderManager.LoaderCallbacks<Cursor> {
|
|
|
|
|
|
|
|
@NonNull private final RecyclerView recyclerView;
|
|
|
|
@Nullable private OnItemClickedListener listener;
|
|
|
|
|
|
|
|
public RecentPhotoViewRail(Context context) {
|
|
|
|
this(context, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public RecentPhotoViewRail(Context context, AttributeSet attrs) {
|
|
|
|
this(context, attrs, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
public RecentPhotoViewRail(Context context, AttributeSet attrs, int defStyleAttr) {
|
|
|
|
super(context, attrs, defStyleAttr);
|
|
|
|
|
|
|
|
inflate(context, R.layout.recent_photo_view, this);
|
|
|
|
|
|
|
|
this.recyclerView = ViewUtil.findById(this, R.id.photo_list);
|
|
|
|
this.recyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
|
|
|
|
this.recyclerView.setItemAnimator(new DefaultItemAnimator());
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setListener(@Nullable OnItemClickedListener listener) {
|
|
|
|
this.listener = listener;
|
|
|
|
|
|
|
|
if (this.recyclerView.getAdapter() != null) {
|
|
|
|
((RecentPhotoAdapter)this.recyclerView.getAdapter()).setListener(listener);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-22 16:51:56 +00:00
|
|
|
public @NonNull Loader<Cursor> onCreateLoader(int id, Bundle args) {
|
2016-11-26 06:37:23 +00:00
|
|
|
return new RecentPhotosLoader(getContext());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-22 16:51:56 +00:00
|
|
|
public void onLoadFinished(@NonNull Loader<Cursor> loader, Cursor data) {
|
2016-11-26 06:37:23 +00:00
|
|
|
this.recyclerView.setAdapter(new RecentPhotoAdapter(getContext(), data, RecentPhotosLoader.BASE_URL, listener));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-22 16:51:56 +00:00
|
|
|
public void onLoaderReset(@NonNull Loader<Cursor> loader) {
|
2016-11-26 06:37:23 +00:00
|
|
|
((CursorRecyclerViewAdapter)this.recyclerView.getAdapter()).changeCursor(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static class RecentPhotoAdapter extends CursorRecyclerViewAdapter<RecentPhotoAdapter.RecentPhotoViewHolder> {
|
|
|
|
|
2017-11-25 06:00:30 +00:00
|
|
|
@SuppressWarnings("unused")
|
2018-12-06 20:14:20 +00:00
|
|
|
private static final String TAG = RecentPhotoAdapter.class.getSimpleName();
|
2017-01-08 21:06:11 +00:00
|
|
|
|
2016-11-26 06:37:23 +00:00
|
|
|
@NonNull private final Uri baseUri;
|
|
|
|
@Nullable private OnItemClickedListener clickedListener;
|
|
|
|
|
|
|
|
private RecentPhotoAdapter(@NonNull Context context, @NonNull Cursor cursor, @NonNull Uri baseUri, @Nullable OnItemClickedListener listener) {
|
|
|
|
super(context, cursor);
|
|
|
|
this.baseUri = baseUri;
|
|
|
|
this.clickedListener = listener;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public RecentPhotoViewHolder onCreateItemViewHolder(ViewGroup parent, int viewType) {
|
|
|
|
View itemView = LayoutInflater.from(parent.getContext())
|
|
|
|
.inflate(R.layout.recent_photo_view_item, parent, false);
|
|
|
|
|
|
|
|
return new RecentPhotoViewHolder(itemView);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBindItemViewHolder(RecentPhotoViewHolder viewHolder, @NonNull Cursor cursor) {
|
|
|
|
viewHolder.imageView.setImageDrawable(null);
|
|
|
|
|
|
|
|
long id = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns._ID));
|
|
|
|
long dateTaken = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.DATE_TAKEN));
|
|
|
|
long dateModified = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.DATE_MODIFIED));
|
|
|
|
String mimeType = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.MIME_TYPE));
|
2018-11-20 17:59:23 +00:00
|
|
|
String bucketId = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.BUCKET_ID));
|
2016-11-26 06:37:23 +00:00
|
|
|
int orientation = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.ORIENTATION));
|
2019-03-15 00:01:23 +00:00
|
|
|
long size = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.SIZE));
|
2019-03-20 22:09:27 +00:00
|
|
|
int width = cursor.getInt(cursor.getColumnIndexOrThrow(getWidthColumn(orientation)));
|
|
|
|
int height = cursor.getInt(cursor.getColumnIndexOrThrow(getHeightColumn(orientation)));
|
2016-11-26 06:37:23 +00:00
|
|
|
|
|
|
|
final Uri uri = Uri.withAppendedPath(baseUri, Long.toString(id));
|
|
|
|
|
|
|
|
Key signature = new MediaStoreSignature(mimeType, dateModified, orientation);
|
|
|
|
|
2017-10-16 20:11:42 +00:00
|
|
|
GlideApp.with(getContext().getApplicationContext())
|
2017-10-12 00:12:46 +00:00
|
|
|
.load(uri)
|
|
|
|
.signature(signature)
|
|
|
|
.diskCacheStrategy(DiskCacheStrategy.NONE)
|
|
|
|
.into(viewHolder.imageView);
|
2016-11-26 06:37:23 +00:00
|
|
|
|
2017-11-25 06:00:30 +00:00
|
|
|
viewHolder.imageView.setOnClickListener(v -> {
|
2019-03-15 00:01:23 +00:00
|
|
|
if (clickedListener != null) clickedListener.onItemClicked(uri, mimeType, bucketId, dateTaken, width, height, size);
|
2016-11-26 06:37:23 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-01-17 20:15:00 +00:00
|
|
|
@TargetApi(16)
|
|
|
|
@SuppressWarnings("SuspiciousNameCombination")
|
|
|
|
private String getWidthColumn(int orientation) {
|
|
|
|
if (orientation == 0 || orientation == 180) return MediaStore.Images.ImageColumns.WIDTH;
|
|
|
|
else return MediaStore.Images.ImageColumns.HEIGHT;
|
|
|
|
}
|
|
|
|
|
|
|
|
@TargetApi(16)
|
|
|
|
@SuppressWarnings("SuspiciousNameCombination")
|
|
|
|
private String getHeightColumn(int orientation) {
|
|
|
|
if (orientation == 0 || orientation == 180) return MediaStore.Images.ImageColumns.HEIGHT;
|
|
|
|
else return MediaStore.Images.ImageColumns.WIDTH;
|
|
|
|
}
|
|
|
|
|
2016-11-26 06:37:23 +00:00
|
|
|
public void setListener(@Nullable OnItemClickedListener listener) {
|
|
|
|
this.clickedListener = listener;
|
|
|
|
}
|
|
|
|
|
|
|
|
static class RecentPhotoViewHolder extends RecyclerView.ViewHolder {
|
|
|
|
|
|
|
|
ImageView imageView;
|
|
|
|
|
|
|
|
RecentPhotoViewHolder(View itemView) {
|
|
|
|
super(itemView);
|
|
|
|
|
|
|
|
this.imageView = ViewUtil.findById(itemView, R.id.thumbnail);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public interface OnItemClickedListener {
|
2019-03-15 00:01:23 +00:00
|
|
|
void onItemClicked(Uri uri, String mimeType, String bucketId, long dateTaken, int width, int height, long size);
|
2016-11-26 06:37:23 +00:00
|
|
|
}
|
|
|
|
}
|