2015-01-18 16:11:30 -10:00
|
|
|
/**
|
|
|
|
* Copyright (C) 2015 Open Whisper Systems
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
package org.thoughtcrime.securesms;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.database.Cursor;
|
2015-09-15 15:28:27 -07:00
|
|
|
import android.support.annotation.NonNull;
|
2015-01-18 16:11:30 -10:00
|
|
|
import android.support.v7.widget.RecyclerView;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.View.OnClickListener;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
|
2017-02-01 03:49:19 +01:00
|
|
|
import org.thoughtcrime.securesms.MediaAdapter.ViewHolder;
|
2015-03-31 15:44:41 -07:00
|
|
|
import org.thoughtcrime.securesms.components.ThumbnailView;
|
2015-01-18 16:11:30 -10:00
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
2017-09-20 18:14:28 -07:00
|
|
|
import org.thoughtcrime.securesms.database.Address;
|
2015-01-18 16:11:30 -10:00
|
|
|
import org.thoughtcrime.securesms.database.CursorRecyclerViewAdapter;
|
2017-02-01 03:49:19 +01:00
|
|
|
import org.thoughtcrime.securesms.database.MediaDatabase.MediaRecord;
|
2015-01-18 16:11:30 -10:00
|
|
|
import org.thoughtcrime.securesms.mms.Slide;
|
|
|
|
import org.thoughtcrime.securesms.util.MediaUtil;
|
|
|
|
|
2017-02-01 03:49:19 +01:00
|
|
|
public class MediaAdapter extends CursorRecyclerViewAdapter<ViewHolder> {
|
|
|
|
private static final String TAG = MediaAdapter.class.getSimpleName();
|
2015-01-18 16:11:30 -10:00
|
|
|
|
|
|
|
private final MasterSecret masterSecret;
|
2017-09-20 18:14:28 -07:00
|
|
|
private final Address address;
|
2015-01-18 16:11:30 -10:00
|
|
|
|
|
|
|
public static class ViewHolder extends RecyclerView.ViewHolder {
|
2015-03-31 15:44:41 -07:00
|
|
|
public ThumbnailView imageView;
|
2015-01-18 16:11:30 -10:00
|
|
|
|
|
|
|
public ViewHolder(View v) {
|
|
|
|
super(v);
|
2015-03-31 15:44:41 -07:00
|
|
|
imageView = (ThumbnailView) v.findViewById(R.id.image);
|
2015-01-18 16:11:30 -10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-20 18:14:28 -07:00
|
|
|
public MediaAdapter(Context context, MasterSecret masterSecret, Cursor c, Address address) {
|
2015-01-18 16:11:30 -10:00
|
|
|
super(context, c);
|
|
|
|
this.masterSecret = masterSecret;
|
2017-09-20 18:14:28 -07:00
|
|
|
this.address = address;
|
2015-01-18 16:11:30 -10:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-08-04 12:29:26 -07:00
|
|
|
public ViewHolder onCreateItemViewHolder(final ViewGroup viewGroup, final int i) {
|
2015-01-18 16:11:30 -10:00
|
|
|
final View view = LayoutInflater.from(getContext()).inflate(R.layout.media_overview_item, viewGroup, false);
|
|
|
|
return new ViewHolder(view);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-08-04 12:29:26 -07:00
|
|
|
public void onBindItemViewHolder(final ViewHolder viewHolder, final @NonNull Cursor cursor) {
|
2015-03-31 15:44:41 -07:00
|
|
|
final ThumbnailView imageView = viewHolder.imageView;
|
2017-03-28 12:05:30 -07:00
|
|
|
final MediaRecord mediaRecord = MediaRecord.from(getContext(), masterSecret, cursor);
|
2015-01-18 16:11:30 -10:00
|
|
|
|
2017-02-01 03:49:19 +01:00
|
|
|
Slide slide = MediaUtil.getSlideForAttachment(getContext(), mediaRecord.getAttachment());
|
2015-01-18 16:11:30 -10:00
|
|
|
|
2015-01-29 20:37:01 -10:00
|
|
|
if (slide != null) {
|
2017-04-19 21:23:57 -07:00
|
|
|
imageView.setImageResource(masterSecret, slide, false, false);
|
2015-01-29 20:37:01 -10:00
|
|
|
}
|
2015-01-18 16:11:30 -10:00
|
|
|
|
2017-02-01 03:49:19 +01:00
|
|
|
imageView.setOnClickListener(new OnMediaClickListener(mediaRecord));
|
2015-01-18 16:11:30 -10:00
|
|
|
}
|
|
|
|
|
|
|
|
private class OnMediaClickListener implements OnClickListener {
|
2017-02-01 03:49:19 +01:00
|
|
|
private final MediaRecord mediaRecord;
|
2015-01-18 16:11:30 -10:00
|
|
|
|
2017-02-01 03:49:19 +01:00
|
|
|
private OnMediaClickListener(MediaRecord mediaRecord) {
|
|
|
|
this.mediaRecord = mediaRecord;
|
2015-01-18 16:11:30 -10:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
2017-02-01 03:49:19 +01:00
|
|
|
if (mediaRecord.getAttachment().getDataUri() != null) {
|
2016-12-11 13:37:27 -08:00
|
|
|
Intent intent = new Intent(getContext(), MediaPreviewActivity.class);
|
2017-02-01 03:49:19 +01:00
|
|
|
intent.putExtra(MediaPreviewActivity.DATE_EXTRA, mediaRecord.getDate());
|
|
|
|
intent.putExtra(MediaPreviewActivity.SIZE_EXTRA, mediaRecord.getAttachment().getSize());
|
2017-09-20 18:14:28 -07:00
|
|
|
intent.putExtra(MediaPreviewActivity.ADDRESS_EXTRA, address);
|
2016-12-11 13:37:27 -08:00
|
|
|
|
2017-07-26 09:59:15 -07:00
|
|
|
if (mediaRecord.getAddress() != null) {
|
|
|
|
intent.putExtra(MediaPreviewActivity.ADDRESS_EXTRA, mediaRecord.getAddress());
|
2015-01-18 16:11:30 -10:00
|
|
|
}
|
2017-07-26 09:59:15 -07:00
|
|
|
|
2017-02-01 03:49:19 +01:00
|
|
|
intent.setDataAndType(mediaRecord.getAttachment().getDataUri(), mediaRecord.getContentType());
|
2016-12-11 13:37:27 -08:00
|
|
|
getContext().startActivity(intent);
|
2015-01-18 16:11:30 -10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|