2017-10-16 20:11:42 +00:00
|
|
|
/*
|
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;
|
2017-10-16 20:11:42 +00:00
|
|
|
import android.support.annotation.NonNull;
|
2015-01-19 02:11:30 +00:00
|
|
|
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;
|
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;
|
2017-10-16 20:11:42 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.GlideRequests;
|
2015-01-19 02:11:30 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.Slide;
|
|
|
|
import org.thoughtcrime.securesms.util.MediaUtil;
|
|
|
|
|
2018-03-15 18:17:40 +00:00
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.HashSet;
|
2017-09-24 04:30:00 +00:00
|
|
|
import java.util.Locale;
|
2018-03-15 18:17:40 +00:00
|
|
|
import java.util.Set;
|
2017-09-24 04:30:00 +00:00
|
|
|
|
2019-07-24 02:30:23 +00:00
|
|
|
import network.loki.messenger.R;
|
|
|
|
|
2017-10-16 20:11:42 +00:00
|
|
|
class MediaGalleryAdapter extends StickyHeaderGridAdapter {
|
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
@SuppressWarnings("unused")
|
2017-09-25 15:32:45 +00:00
|
|
|
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;
|
2017-10-16 20:11:42 +00:00
|
|
|
private final GlideRequests glideRequests;
|
2017-09-24 04:30:00 +00:00
|
|
|
private final Locale locale;
|
2018-03-15 18:17:40 +00:00
|
|
|
private final ItemClickListener itemClickListener;
|
|
|
|
private final Set<MediaRecord> selected;
|
2017-09-24 04:30:00 +00:00
|
|
|
|
|
|
|
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;
|
2018-03-15 18:17:40 +00:00
|
|
|
View selectedIndicator;
|
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);
|
2018-03-15 18:17:40 +00:00
|
|
|
imageView = v.findViewById(R.id.image);
|
|
|
|
selectedIndicator = v.findViewById(R.id.selected_indicator);
|
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);
|
2017-10-16 20:11:42 +00:00
|
|
|
textView = itemView.findViewById(R.id.text);
|
2017-09-24 04:30:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-15 18:17:40 +00:00
|
|
|
MediaGalleryAdapter(@NonNull Context context,
|
|
|
|
@NonNull GlideRequests glideRequests,
|
|
|
|
BucketedThreadMedia media,
|
|
|
|
Locale locale,
|
|
|
|
ItemClickListener clickListener)
|
2017-10-16 20:11:42 +00:00
|
|
|
{
|
2018-03-15 18:17:40 +00:00
|
|
|
this.context = context;
|
|
|
|
this.glideRequests = glideRequests;
|
|
|
|
this.locale = locale;
|
|
|
|
this.media = media;
|
|
|
|
this.itemClickListener = clickListener;
|
|
|
|
this.selected = new HashSet<>();
|
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) {
|
2018-03-15 18:17:40 +00:00
|
|
|
MediaRecord mediaRecord = media.get(section, offset);
|
|
|
|
ThumbnailView thumbnailView = ((ViewHolder)viewHolder).imageView;
|
|
|
|
View selectedIndicator = ((ViewHolder)viewHolder).selectedIndicator;
|
|
|
|
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) {
|
2018-01-25 03:17:44 +00:00
|
|
|
thumbnailView.setImageResource(glideRequests, slide, false, false);
|
2015-01-30 06:37:01 +00:00
|
|
|
}
|
2015-01-19 02:11:30 +00:00
|
|
|
|
2018-03-15 18:17:40 +00:00
|
|
|
thumbnailView.setOnClickListener(view -> itemClickListener.onMediaClicked(mediaRecord));
|
|
|
|
thumbnailView.setOnLongClickListener(view -> {
|
|
|
|
itemClickListener.onMediaLongClicked(mediaRecord);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
selectedIndicator.setVisibility(selected.contains(mediaRecord) ? View.VISIBLE : View.GONE);
|
2017-09-24 04:30:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@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);
|
|
|
|
}
|
|
|
|
|
2018-03-15 18:17:40 +00:00
|
|
|
public void toggleSelection(@NonNull MediaRecord mediaRecord) {
|
|
|
|
if (!selected.remove(mediaRecord)) {
|
|
|
|
selected.add(mediaRecord);
|
|
|
|
}
|
|
|
|
notifyDataSetChanged();
|
|
|
|
}
|
2017-09-24 04:30:00 +00:00
|
|
|
|
2018-03-15 18:17:40 +00:00
|
|
|
public int getSelectedMediaCount() {
|
|
|
|
return selected.size();
|
|
|
|
}
|
2015-01-19 02:11:30 +00:00
|
|
|
|
2018-03-15 18:17:40 +00:00
|
|
|
@NonNull
|
|
|
|
public Collection<MediaRecord> getSelectedMedia() {
|
|
|
|
return new HashSet<>(selected);
|
|
|
|
}
|
2015-01-19 02:11:30 +00:00
|
|
|
|
2018-03-15 18:17:40 +00:00
|
|
|
public void clearSelection() {
|
|
|
|
selected.clear();
|
|
|
|
notifyDataSetChanged();
|
2015-01-19 02:11:30 +00:00
|
|
|
}
|
2017-09-24 04:30:00 +00:00
|
|
|
|
2018-04-06 19:29:52 +00:00
|
|
|
void selectAllMedia() {
|
|
|
|
for (int section = 0; section < media.getSectionCount(); section++) {
|
|
|
|
for (int item = 0; item < media.getSectionItemCount(section); item++) {
|
|
|
|
selected.add(media.get(section, item));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
|
2018-03-15 18:17:40 +00:00
|
|
|
interface ItemClickListener {
|
|
|
|
void onMediaClicked(@NonNull MediaRecord mediaRecord);
|
|
|
|
void onMediaLongClicked(MediaRecord mediaRecord);
|
|
|
|
}
|
2015-01-19 02:11:30 +00:00
|
|
|
}
|