2015-01-19 02:11:30 +00: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.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
2017-09-24 04:30:00 +00:00
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
import com.codewaves.stickyheadergrid.StickyHeaderGridAdapter;
|
2015-01-19 02:11:30 +00:00
|
|
|
|
2015-03-31 22:44:41 +00:00
|
|
|
import org.thoughtcrime.securesms.components.ThumbnailView;
|
2015-01-19 02:11:30 +00:00
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
2017-09-21 01:14:28 +00:00
|
|
|
import org.thoughtcrime.securesms.database.Address;
|
2017-02-01 02:49:19 +00:00
|
|
|
import org.thoughtcrime.securesms.database.MediaDatabase.MediaRecord;
|
2017-09-24 04:30:00 +00:00
|
|
|
import org.thoughtcrime.securesms.database.loaders.BucketedThreadMediaLoader.BucketedThreadMedia;
|
2015-01-19 02:11:30 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.Slide;
|
|
|
|
import org.thoughtcrime.securesms.util.MediaUtil;
|
|
|
|
|
2017-09-24 04:30:00 +00:00
|
|
|
import java.util.Locale;
|
|
|
|
|
2017-09-25 15:32:45 +00:00
|
|
|
public class MediaGalleryAdapter extends StickyHeaderGridAdapter {
|
|
|
|
private static final String TAG = MediaGalleryAdapter.class.getSimpleName();
|
2015-01-19 02:11:30 +00:00
|
|
|
|
2017-09-24 04:30:00 +00:00
|
|
|
private final Context context;
|
|
|
|
private final MasterSecret masterSecret;
|
|
|
|
private final Locale locale;
|
|
|
|
private final Address address;
|
|
|
|
|
|
|
|
private BucketedThreadMedia media;
|
2015-01-19 02:11:30 +00:00
|
|
|
|
2017-09-24 04:30:00 +00:00
|
|
|
private static class ViewHolder extends StickyHeaderGridAdapter.ItemViewHolder {
|
|
|
|
ThumbnailView imageView;
|
2015-01-19 02:11:30 +00:00
|
|
|
|
2017-09-24 04:30:00 +00:00
|
|
|
ViewHolder(View v) {
|
2015-01-19 02:11:30 +00:00
|
|
|
super(v);
|
2015-03-31 22:44:41 +00:00
|
|
|
imageView = (ThumbnailView) v.findViewById(R.id.image);
|
2015-01-19 02:11:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-24 04:30:00 +00:00
|
|
|
private static class HeaderHolder extends StickyHeaderGridAdapter.HeaderViewHolder {
|
|
|
|
TextView textView;
|
|
|
|
|
|
|
|
HeaderHolder(View itemView) {
|
|
|
|
super(itemView);
|
|
|
|
textView = (TextView) itemView.findViewById(R.id.text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-25 15:32:45 +00:00
|
|
|
public MediaGalleryAdapter(Context context, MasterSecret masterSecret, BucketedThreadMedia media, Locale locale, Address address) {
|
2017-09-24 04:30:00 +00:00
|
|
|
this.context = context;
|
2015-01-19 02:11:30 +00:00
|
|
|
this.masterSecret = masterSecret;
|
2017-09-24 04:30:00 +00:00
|
|
|
this.locale = locale;
|
|
|
|
this.media = media;
|
2017-09-21 01:14:28 +00:00
|
|
|
this.address = address;
|
2015-01-19 02:11:30 +00:00
|
|
|
}
|
|
|
|
|
2017-09-24 04:30:00 +00:00
|
|
|
public void setMedia(BucketedThreadMedia media) {
|
|
|
|
this.media = media;
|
|
|
|
}
|
|
|
|
|
2015-01-19 02:11:30 +00:00
|
|
|
@Override
|
2017-09-24 04:30:00 +00:00
|
|
|
public StickyHeaderGridAdapter.HeaderViewHolder onCreateHeaderViewHolder(ViewGroup parent, int headerType) {
|
2017-09-25 15:32:45 +00:00
|
|
|
return new HeaderHolder(LayoutInflater.from(context).inflate(R.layout.media_overview_gallery_item_header, parent, false));
|
2015-01-19 02:11:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-09-24 04:30:00 +00:00
|
|
|
public ItemViewHolder onCreateItemViewHolder(ViewGroup parent, int itemType) {
|
2017-09-25 15:32:45 +00:00
|
|
|
return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.media_overview_gallery_item, parent, false));
|
2017-09-24 04:30:00 +00:00
|
|
|
}
|
2015-01-19 02:11:30 +00:00
|
|
|
|
2017-09-24 04:30:00 +00:00
|
|
|
@Override
|
|
|
|
public void onBindHeaderViewHolder(StickyHeaderGridAdapter.HeaderViewHolder viewHolder, int section) {
|
|
|
|
((HeaderHolder)viewHolder).textView.setText(media.getName(section, locale));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBindItemViewHolder(ItemViewHolder viewHolder, int section, int offset) {
|
|
|
|
MediaRecord mediaRecord = media.get(section, offset);
|
|
|
|
ThumbnailView thumbnailView = ((ViewHolder)viewHolder).imageView;
|
|
|
|
|
|
|
|
Slide slide = MediaUtil.getSlideForAttachment(context, mediaRecord.getAttachment());
|
2015-01-19 02:11:30 +00:00
|
|
|
|
2015-01-30 06:37:01 +00:00
|
|
|
if (slide != null) {
|
2017-09-24 04:30:00 +00:00
|
|
|
thumbnailView.setImageResource(masterSecret, slide, false, false);
|
2015-01-30 06:37:01 +00:00
|
|
|
}
|
2015-01-19 02:11:30 +00:00
|
|
|
|
2017-09-24 04:30:00 +00:00
|
|
|
thumbnailView.setOnClickListener(new OnMediaClickListener(mediaRecord));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getSectionCount() {
|
|
|
|
return media.getSectionCount();
|
2015-01-19 02:11:30 +00:00
|
|
|
}
|
|
|
|
|
2017-09-24 04:30:00 +00:00
|
|
|
@Override
|
|
|
|
public int getSectionItemCount(int section) {
|
|
|
|
return media.getSectionItemCount(section);
|
|
|
|
}
|
|
|
|
|
|
|
|
private class OnMediaClickListener implements View.OnClickListener {
|
|
|
|
|
2017-02-01 02:49:19 +00:00
|
|
|
private final MediaRecord mediaRecord;
|
2015-01-19 02:11:30 +00:00
|
|
|
|
2017-02-01 02:49:19 +00:00
|
|
|
private OnMediaClickListener(MediaRecord mediaRecord) {
|
|
|
|
this.mediaRecord = mediaRecord;
|
2015-01-19 02:11:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
2017-02-01 02:49:19 +00:00
|
|
|
if (mediaRecord.getAttachment().getDataUri() != null) {
|
2017-09-24 04:30:00 +00:00
|
|
|
Intent intent = new Intent(context, MediaPreviewActivity.class);
|
2017-02-01 02:49:19 +00:00
|
|
|
intent.putExtra(MediaPreviewActivity.DATE_EXTRA, mediaRecord.getDate());
|
|
|
|
intent.putExtra(MediaPreviewActivity.SIZE_EXTRA, mediaRecord.getAttachment().getSize());
|
2017-09-21 01:14:28 +00:00
|
|
|
intent.putExtra(MediaPreviewActivity.ADDRESS_EXTRA, address);
|
2017-10-04 18:35:16 +00:00
|
|
|
intent.putExtra(MediaPreviewActivity.OUTGOING_EXTRA, mediaRecord.isOutgoing());
|
2016-12-11 21:37:27 +00:00
|
|
|
|
2017-07-26 16:59:15 +00:00
|
|
|
if (mediaRecord.getAddress() != null) {
|
|
|
|
intent.putExtra(MediaPreviewActivity.ADDRESS_EXTRA, mediaRecord.getAddress());
|
2015-01-19 02:11:30 +00:00
|
|
|
}
|
2017-07-26 16:59:15 +00:00
|
|
|
|
2017-02-01 02:49:19 +00:00
|
|
|
intent.setDataAndType(mediaRecord.getAttachment().getDataUri(), mediaRecord.getContentType());
|
2017-09-24 04:30:00 +00:00
|
|
|
context.startActivity(intent);
|
2015-01-19 02:11:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-09-24 04:30:00 +00:00
|
|
|
|
2015-01-19 02:11:30 +00:00
|
|
|
}
|