2014-01-19 02:17:08 +00:00
|
|
|
/**
|
2015-07-14 21:31:03 +00:00
|
|
|
* Copyright (C) 2015 Open Whisper Systems
|
2014-01-19 02:17:08 +00:00
|
|
|
*
|
|
|
|
* 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.database.Cursor;
|
2015-11-03 01:40:41 +00:00
|
|
|
import android.graphics.Canvas;
|
|
|
|
import android.graphics.Rect;
|
2015-07-17 05:36:40 +00:00
|
|
|
import android.os.Build;
|
2015-11-03 01:40:41 +00:00
|
|
|
import android.os.Build.VERSION;
|
2014-01-19 02:17:08 +00:00
|
|
|
import android.os.Bundle;
|
2015-10-19 18:23:12 +00:00
|
|
|
import android.support.annotation.NonNull;
|
2014-03-18 06:25:09 +00:00
|
|
|
import android.support.v4.app.Fragment;
|
2014-01-19 02:17:08 +00:00
|
|
|
import android.support.v4.app.LoaderManager;
|
|
|
|
import android.support.v4.content.Loader;
|
2015-11-03 01:40:41 +00:00
|
|
|
import android.support.v4.view.ViewCompat;
|
2015-07-14 21:31:03 +00:00
|
|
|
import android.support.v4.widget.SwipeRefreshLayout;
|
2015-11-03 01:40:41 +00:00
|
|
|
import android.support.v7.widget.LinearLayoutManager;
|
|
|
|
import android.support.v7.widget.RecyclerView;
|
2014-01-19 02:17:08 +00:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
2015-11-03 01:40:41 +00:00
|
|
|
import org.thoughtcrime.securesms.components.RecyclerViewFastScroller;
|
2014-03-18 06:25:09 +00:00
|
|
|
import org.thoughtcrime.securesms.contacts.ContactSelectionListAdapter;
|
2015-05-19 21:00:54 +00:00
|
|
|
import org.thoughtcrime.securesms.contacts.ContactSelectionListItem;
|
2015-07-14 21:31:03 +00:00
|
|
|
import org.thoughtcrime.securesms.contacts.ContactsCursorLoader;
|
2015-11-03 01:40:41 +00:00
|
|
|
import org.thoughtcrime.securesms.database.CursorRecyclerViewAdapter;
|
|
|
|
import org.thoughtcrime.securesms.util.ViewUtil;
|
2014-01-19 02:17:08 +00:00
|
|
|
|
2015-11-03 01:40:41 +00:00
|
|
|
import java.util.HashMap;
|
2014-01-19 02:17:08 +00:00
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
2014-03-18 06:25:09 +00:00
|
|
|
import java.util.Map;
|
|
|
|
|
2014-01-19 02:17:08 +00:00
|
|
|
/**
|
2014-03-18 06:25:09 +00:00
|
|
|
* Fragment for selecting a one or more contacts from a list.
|
2014-01-19 02:17:08 +00:00
|
|
|
*
|
|
|
|
* @author Moxie Marlinspike
|
|
|
|
*
|
|
|
|
*/
|
2015-07-14 21:31:03 +00:00
|
|
|
public class ContactSelectionListFragment extends Fragment
|
|
|
|
implements LoaderManager.LoaderCallbacks<Cursor>
|
2014-01-19 02:17:08 +00:00
|
|
|
{
|
2015-07-14 21:31:03 +00:00
|
|
|
private static final String TAG = ContactSelectionListFragment.class.getSimpleName();
|
2014-03-18 06:25:09 +00:00
|
|
|
|
2015-10-19 18:23:12 +00:00
|
|
|
public final static String DISPLAY_MODE = "display_mode";
|
|
|
|
public final static String MULTI_SELECT = "multi_select";
|
|
|
|
public final static String REFRESHABLE = "refreshable";
|
|
|
|
|
|
|
|
public final static int DISPLAY_MODE_ALL = ContactsCursorLoader.MODE_ALL;
|
|
|
|
public final static int DISPLAY_MODE_PUSH_ONLY = ContactsCursorLoader.MODE_PUSH_ONLY;
|
|
|
|
public final static int DISPLAY_MODE_OTHER_ONLY = ContactsCursorLoader.MODE_OTHER_ONLY;
|
|
|
|
|
2014-03-18 06:25:09 +00:00
|
|
|
private TextView emptyText;
|
2014-01-19 02:17:08 +00:00
|
|
|
|
2015-05-19 21:00:54 +00:00
|
|
|
private Map<Long, String> selectedContacts;
|
2014-03-18 06:25:09 +00:00
|
|
|
private OnContactSelectedListener onContactSelectedListener;
|
2015-07-14 21:31:03 +00:00
|
|
|
private SwipeRefreshLayout swipeRefresh;
|
2014-04-01 23:40:16 +00:00
|
|
|
private String cursorFilter;
|
2015-11-03 01:40:41 +00:00
|
|
|
private RecyclerView recyclerView;
|
|
|
|
private RecyclerViewFastScroller fastScroller;
|
2014-02-24 22:43:38 +00:00
|
|
|
|
2014-01-19 02:17:08 +00:00
|
|
|
@Override
|
|
|
|
public void onActivityCreated(Bundle icicle) {
|
|
|
|
super.onCreate(icicle);
|
|
|
|
initializeCursor();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-03-18 06:25:09 +00:00
|
|
|
public void onResume() {
|
|
|
|
super.onResume();
|
2014-01-19 02:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-03-18 06:25:09 +00:00
|
|
|
public void onPause() {
|
|
|
|
super.onPause();
|
2014-01-19 02:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-03-18 06:25:09 +00:00
|
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
2015-07-14 21:31:03 +00:00
|
|
|
View view = inflater.inflate(R.layout.contact_selection_list_fragment, container, false);
|
|
|
|
|
2015-11-03 01:40:41 +00:00
|
|
|
emptyText = ViewUtil.findById(view, android.R.id.empty);
|
|
|
|
recyclerView = ViewUtil.findById(view, R.id.recycler_view);
|
|
|
|
swipeRefresh = ViewUtil.findById(view, R.id.swipe_refresh);
|
|
|
|
fastScroller = ViewUtil.findById(view, R.id.fast_scroller);
|
|
|
|
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
|
2015-07-14 21:31:03 +00:00
|
|
|
|
2015-10-19 18:23:12 +00:00
|
|
|
swipeRefresh.setEnabled(getActivity().getIntent().getBooleanExtra(REFRESHABLE, true) &&
|
|
|
|
Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN);
|
2015-07-17 05:36:40 +00:00
|
|
|
|
2015-07-14 21:31:03 +00:00
|
|
|
return view;
|
2014-01-19 02:17:08 +00:00
|
|
|
}
|
|
|
|
|
2015-10-19 18:23:12 +00:00
|
|
|
public @NonNull List<String> getSelectedContacts() {
|
2015-05-19 21:00:54 +00:00
|
|
|
List<String> selected = new LinkedList<>();
|
2015-10-19 18:23:12 +00:00
|
|
|
if (selectedContacts != null) {
|
|
|
|
selected.addAll(selectedContacts.values());
|
|
|
|
}
|
2014-01-19 02:17:08 +00:00
|
|
|
|
2014-02-03 19:52:27 +00:00
|
|
|
return selected;
|
2014-01-19 02:17:08 +00:00
|
|
|
}
|
|
|
|
|
2015-10-19 18:23:12 +00:00
|
|
|
private boolean isMulti() {
|
|
|
|
return getActivity().getIntent().getBooleanExtra(MULTI_SELECT, false);
|
2014-01-19 02:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void initializeCursor() {
|
2015-11-03 01:40:41 +00:00
|
|
|
ContactSelectionListAdapter adapter = new ContactSelectionListAdapter(getActivity(),
|
|
|
|
null,
|
|
|
|
new ListClickListener(),
|
|
|
|
isMulti());
|
2014-03-18 06:25:09 +00:00
|
|
|
selectedContacts = adapter.getSelectedContacts();
|
2015-11-03 01:40:41 +00:00
|
|
|
recyclerView.setAdapter(adapter);
|
|
|
|
recyclerView.addItemDecoration(new StickyHeaderDecoration(adapter, true));
|
2014-01-19 02:17:08 +00:00
|
|
|
this.getLoaderManager().initLoader(0, null, this);
|
|
|
|
}
|
|
|
|
|
2015-07-14 21:31:03 +00:00
|
|
|
public void setQueryFilter(String filter) {
|
|
|
|
this.cursorFilter = filter;
|
|
|
|
this.getLoaderManager().restartLoader(0, null, this);
|
2014-01-19 02:17:08 +00:00
|
|
|
}
|
|
|
|
|
2015-07-14 21:31:03 +00:00
|
|
|
public void resetQueryFilter() {
|
|
|
|
setQueryFilter(null);
|
|
|
|
swipeRefresh.setRefreshing(false);
|
2014-01-19 02:17:08 +00:00
|
|
|
}
|
|
|
|
|
2015-11-16 23:25:39 +00:00
|
|
|
public void setRefreshing(boolean refreshing) {
|
|
|
|
swipeRefresh.setRefreshing(refreshing);
|
|
|
|
}
|
|
|
|
|
2015-10-19 18:23:12 +00:00
|
|
|
public void reset() {
|
|
|
|
selectedContacts.clear();
|
|
|
|
getLoaderManager().restartLoader(0, null, this);
|
|
|
|
}
|
|
|
|
|
2014-03-18 06:25:09 +00:00
|
|
|
@Override
|
|
|
|
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
|
2015-10-19 18:23:12 +00:00
|
|
|
return new ContactsCursorLoader(getActivity(),
|
|
|
|
getActivity().getIntent().getIntExtra(DISPLAY_MODE, DISPLAY_MODE_ALL),
|
|
|
|
cursorFilter);
|
2014-02-07 02:06:23 +00:00
|
|
|
}
|
|
|
|
|
2014-03-18 06:25:09 +00:00
|
|
|
@Override
|
|
|
|
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
|
2015-11-03 01:40:41 +00:00
|
|
|
((CursorRecyclerViewAdapter) recyclerView.getAdapter()).changeCursor(data);
|
2014-03-18 06:25:09 +00:00
|
|
|
emptyText.setText(R.string.contact_selection_group_activity__no_contacts);
|
2016-09-07 19:26:34 +00:00
|
|
|
boolean useFastScroller = (recyclerView.getAdapter().getItemCount() > 20);
|
|
|
|
recyclerView.setVerticalScrollBarEnabled(!useFastScroller);
|
|
|
|
if (useFastScroller) {
|
2015-11-23 18:46:01 +00:00
|
|
|
fastScroller.setVisibility(View.VISIBLE);
|
|
|
|
fastScroller.setRecyclerView(recyclerView);
|
|
|
|
}
|
2014-01-19 02:17:08 +00:00
|
|
|
}
|
|
|
|
|
2014-03-18 06:25:09 +00:00
|
|
|
@Override
|
|
|
|
public void onLoaderReset(Loader<Cursor> loader) {
|
2015-11-03 01:40:41 +00:00
|
|
|
((CursorRecyclerViewAdapter) recyclerView.getAdapter()).changeCursor(null);
|
2015-11-23 18:46:01 +00:00
|
|
|
fastScroller.setVisibility(View.GONE);
|
2014-01-19 02:17:08 +00:00
|
|
|
}
|
|
|
|
|
2015-11-03 01:40:41 +00:00
|
|
|
private class ListClickListener implements ContactSelectionListAdapter.ItemClickListener {
|
2014-03-18 06:25:09 +00:00
|
|
|
@Override
|
2015-11-03 01:40:41 +00:00
|
|
|
public void onItemClick(ContactSelectionListItem contact) {
|
2015-05-19 21:00:54 +00:00
|
|
|
|
2015-10-19 18:23:12 +00:00
|
|
|
if (!isMulti() || !selectedContacts.containsKey(contact.getContactId())) {
|
2015-05-19 21:00:54 +00:00
|
|
|
selectedContacts.put(contact.getContactId(), contact.getNumber());
|
|
|
|
contact.setChecked(true);
|
|
|
|
if (onContactSelectedListener != null) onContactSelectedListener.onContactSelected(contact.getNumber());
|
|
|
|
} else {
|
|
|
|
selectedContacts.remove(contact.getContactId());
|
|
|
|
contact.setChecked(false);
|
2015-10-19 18:23:12 +00:00
|
|
|
if (onContactSelectedListener != null) onContactSelectedListener.onContactDeselected(contact.getNumber());
|
2014-01-19 02:17:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-18 06:25:09 +00:00
|
|
|
public void setOnContactSelectedListener(OnContactSelectedListener onContactSelectedListener) {
|
|
|
|
this.onContactSelectedListener = onContactSelectedListener;
|
2014-01-19 02:17:08 +00:00
|
|
|
}
|
|
|
|
|
2015-07-14 21:31:03 +00:00
|
|
|
public void setOnRefreshListener(SwipeRefreshLayout.OnRefreshListener onRefreshListener) {
|
|
|
|
this.swipeRefresh.setOnRefreshListener(onRefreshListener);
|
|
|
|
}
|
|
|
|
|
2014-03-18 06:25:09 +00:00
|
|
|
public interface OnContactSelectedListener {
|
2015-10-19 18:23:12 +00:00
|
|
|
void onContactSelected(String number);
|
|
|
|
void onContactDeselected(String number);
|
2014-01-19 02:17:08 +00:00
|
|
|
}
|
2015-11-03 01:40:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A sticky header decoration for android's RecyclerView.
|
|
|
|
*/
|
|
|
|
public static class StickyHeaderDecoration extends RecyclerView.ItemDecoration {
|
|
|
|
|
|
|
|
private Map<Long, RecyclerView.ViewHolder> mHeaderCache;
|
|
|
|
|
|
|
|
private StickyHeaderAdapter mAdapter;
|
|
|
|
|
|
|
|
private boolean mRenderInline;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param adapter the sticky header adapter to use
|
|
|
|
*/
|
|
|
|
public StickyHeaderDecoration(StickyHeaderAdapter adapter, boolean renderInline) {
|
|
|
|
mAdapter = adapter;
|
|
|
|
mHeaderCache = new HashMap<>();
|
|
|
|
mRenderInline = renderInline;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
|
|
|
|
RecyclerView.State state)
|
|
|
|
{
|
|
|
|
int position = parent.getChildAdapterPosition(view);
|
|
|
|
|
|
|
|
int headerHeight = 0;
|
|
|
|
if (position != RecyclerView.NO_POSITION && hasHeader(position)) {
|
|
|
|
View header = getHeader(parent, position).itemView;
|
|
|
|
headerHeight = getHeaderHeightForLayout(header);
|
|
|
|
}
|
|
|
|
|
|
|
|
outRect.set(0, headerHeight, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean hasHeader(int position) {
|
|
|
|
if (position == 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int previous = position - 1;
|
|
|
|
return mAdapter.getHeaderId(position) != mAdapter.getHeaderId(previous);
|
|
|
|
}
|
|
|
|
|
|
|
|
private RecyclerView.ViewHolder getHeader(RecyclerView parent, int position) {
|
|
|
|
final long key = mAdapter.getHeaderId(position);
|
|
|
|
|
|
|
|
if (mHeaderCache.containsKey(key)) {
|
|
|
|
return mHeaderCache.get(key);
|
|
|
|
} else {
|
|
|
|
final RecyclerView.ViewHolder holder = mAdapter.onCreateHeaderViewHolder(parent);
|
|
|
|
final View header = holder.itemView;
|
|
|
|
|
|
|
|
//noinspection unchecked
|
|
|
|
mAdapter.onBindHeaderViewHolder(holder, position);
|
|
|
|
|
|
|
|
int widthSpec = View.MeasureSpec.makeMeasureSpec(parent.getWidth(), View.MeasureSpec.EXACTLY);
|
|
|
|
int heightSpec = View.MeasureSpec.makeMeasureSpec(parent.getHeight(), View.MeasureSpec.UNSPECIFIED);
|
|
|
|
|
|
|
|
int childWidth = ViewGroup.getChildMeasureSpec(widthSpec,
|
|
|
|
parent.getPaddingLeft() + parent.getPaddingRight(), header.getLayoutParams().width);
|
|
|
|
int childHeight = ViewGroup.getChildMeasureSpec(heightSpec,
|
|
|
|
parent.getPaddingTop() + parent.getPaddingBottom(), header.getLayoutParams().height);
|
|
|
|
|
|
|
|
header.measure(childWidth, childHeight);
|
|
|
|
header.layout(0, 0, header.getMeasuredWidth(), header.getMeasuredHeight());
|
|
|
|
|
|
|
|
mHeaderCache.put(key, holder);
|
|
|
|
|
|
|
|
return holder;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
|
|
|
|
final int count = parent.getChildCount();
|
|
|
|
|
|
|
|
for (int layoutPos = 0; layoutPos < count; layoutPos++) {
|
|
|
|
final View child = parent.getChildAt(layoutPos);
|
|
|
|
|
|
|
|
final int adapterPos = parent.getChildAdapterPosition(child);
|
|
|
|
|
|
|
|
if (adapterPos != RecyclerView.NO_POSITION && (layoutPos == 0 || hasHeader(adapterPos))) {
|
|
|
|
View header = getHeader(parent, adapterPos).itemView;
|
|
|
|
c.save();
|
|
|
|
final int left = child.getLeft();
|
|
|
|
final int top = getHeaderTop(parent, child, header, adapterPos, layoutPos);
|
|
|
|
c.translate(left, top);
|
|
|
|
header.draw(c);
|
|
|
|
c.restore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private int getHeaderTop(RecyclerView parent, View child, View header, int adapterPos,
|
|
|
|
int layoutPos)
|
|
|
|
{
|
|
|
|
int headerHeight = getHeaderHeightForLayout(header);
|
|
|
|
int top = getChildY(parent, child) - headerHeight;
|
|
|
|
if (layoutPos == 0) {
|
|
|
|
final int count = parent.getChildCount();
|
|
|
|
final long currentId = mAdapter.getHeaderId(adapterPos);
|
|
|
|
// find next view with header and compute the offscreen push if needed
|
|
|
|
for (int i = 1; i < count; i++) {
|
|
|
|
int adapterPosHere = parent.getChildAdapterPosition(parent.getChildAt(i));
|
|
|
|
if (adapterPosHere != RecyclerView.NO_POSITION) {
|
|
|
|
long nextId = mAdapter.getHeaderId(adapterPosHere);
|
|
|
|
if (nextId != currentId) {
|
|
|
|
final View next = parent.getChildAt(i);
|
|
|
|
final int offset = getChildY(parent, next) - (headerHeight + getHeader(parent, adapterPosHere).itemView.getHeight());
|
|
|
|
if (offset < 0) {
|
|
|
|
return offset;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
top = Math.max(0, top);
|
|
|
|
}
|
|
|
|
|
|
|
|
return top;
|
|
|
|
}
|
|
|
|
|
|
|
|
private int getChildY(RecyclerView parent, View child) {
|
|
|
|
if (VERSION.SDK_INT < 11) {
|
|
|
|
Rect rect = new Rect();
|
|
|
|
parent.getChildVisibleRect(child, rect, null);
|
|
|
|
return rect.top;
|
|
|
|
} else {
|
|
|
|
return (int)ViewCompat.getY(child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private int getHeaderHeightForLayout(View header) {
|
|
|
|
return mRenderInline ? 0 : header.getHeight();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The adapter to assist the {@link StickyHeaderDecoration} in creating and binding the header views.
|
|
|
|
*
|
|
|
|
* @param <T> the header view holder
|
|
|
|
*/
|
|
|
|
public interface StickyHeaderAdapter<T extends RecyclerView.ViewHolder> {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the header id for the item at the given position.
|
|
|
|
*
|
|
|
|
* @param position the item position
|
|
|
|
* @return the header id
|
|
|
|
*/
|
|
|
|
long getHeaderId(int position);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new header ViewHolder.
|
|
|
|
*
|
|
|
|
* @param parent the header's view parent
|
|
|
|
* @return a view holder for the created view
|
|
|
|
*/
|
|
|
|
T onCreateHeaderViewHolder(ViewGroup parent);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the header view to reflect the header data for the given position
|
|
|
|
* @param viewHolder the header view holder
|
|
|
|
* @param position the header's item position
|
|
|
|
*/
|
|
|
|
void onBindHeaderViewHolder(T viewHolder, int position);
|
|
|
|
}
|
2014-01-19 02:17:08 +00:00
|
|
|
}
|