/** * 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 . */ package org.thoughtcrime.securesms; import android.database.Cursor; import android.os.Build; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.v4.app.Fragment; import android.support.v4.app.LoaderManager; import android.support.v4.content.Loader; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import org.thoughtcrime.securesms.components.RecyclerViewFastScroller; import org.thoughtcrime.securesms.contacts.ContactSelectionListAdapter; import org.thoughtcrime.securesms.contacts.ContactSelectionListItem; import org.thoughtcrime.securesms.contacts.ContactsCursorLoader; import org.thoughtcrime.securesms.database.CursorRecyclerViewAdapter; import org.thoughtcrime.securesms.util.StickyHeaderDecoration; import org.thoughtcrime.securesms.util.ViewUtil; import java.util.LinkedList; import java.util.List; import java.util.Map; /** * Fragment for selecting a one or more contacts from a list. * * @author Moxie Marlinspike * */ public class ContactSelectionListFragment extends Fragment implements LoaderManager.LoaderCallbacks { private static final String TAG = ContactSelectionListFragment.class.getSimpleName(); 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_SMS_ONLY = ContactsCursorLoader.MODE_SMS_ONLY; private TextView emptyText; private Map selectedContacts; private OnContactSelectedListener onContactSelectedListener; private SwipeRefreshLayout swipeRefresh; private String cursorFilter; private RecyclerView recyclerView; private RecyclerViewFastScroller fastScroller; @Override public void onActivityCreated(Bundle icicle) { super.onActivityCreated(icicle); initializeCursor(); } @Override public void onResume() { super.onResume(); } @Override public void onPause() { super.onPause(); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.contact_selection_list_fragment, container, false); 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())); swipeRefresh.setEnabled(getActivity().getIntent().getBooleanExtra(REFRESHABLE, true) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN); return view; } public @NonNull List getSelectedContacts() { List selected = new LinkedList<>(); if (selectedContacts != null) { selected.addAll(selectedContacts.values()); } return selected; } private boolean isMulti() { return getActivity().getIntent().getBooleanExtra(MULTI_SELECT, false); } private void initializeCursor() { ContactSelectionListAdapter adapter = new ContactSelectionListAdapter(getActivity(), null, new ListClickListener(), isMulti()); selectedContacts = adapter.getSelectedContacts(); recyclerView.setAdapter(adapter); recyclerView.addItemDecoration(new StickyHeaderDecoration(adapter, true, true)); this.getLoaderManager().initLoader(0, null, this); } public void setQueryFilter(String filter) { this.cursorFilter = filter; this.getLoaderManager().restartLoader(0, null, this); } public void resetQueryFilter() { setQueryFilter(null); swipeRefresh.setRefreshing(false); } public void setRefreshing(boolean refreshing) { swipeRefresh.setRefreshing(refreshing); } public void reset() { selectedContacts.clear(); getLoaderManager().restartLoader(0, null, this); } @Override public Loader onCreateLoader(int id, Bundle args) { return new ContactsCursorLoader(getActivity(), getActivity().getIntent().getIntExtra(DISPLAY_MODE, DISPLAY_MODE_ALL), cursorFilter); } @Override public void onLoadFinished(Loader loader, Cursor data) { ((CursorRecyclerViewAdapter) recyclerView.getAdapter()).changeCursor(data); emptyText.setText(R.string.contact_selection_group_activity__no_contacts); boolean useFastScroller = (recyclerView.getAdapter().getItemCount() > 20); recyclerView.setVerticalScrollBarEnabled(!useFastScroller); if (useFastScroller) { fastScroller.setVisibility(View.VISIBLE); fastScroller.setRecyclerView(recyclerView); } } @Override public void onLoaderReset(Loader loader) { ((CursorRecyclerViewAdapter) recyclerView.getAdapter()).changeCursor(null); fastScroller.setVisibility(View.GONE); } private class ListClickListener implements ContactSelectionListAdapter.ItemClickListener { @Override public void onItemClick(ContactSelectionListItem contact) { if (!isMulti() || !selectedContacts.containsKey(contact.getContactId())) { selectedContacts.put(contact.getContactId(), contact.getNumber()); contact.setChecked(true); if (onContactSelectedListener != null) onContactSelectedListener.onContactSelected(contact.getNumber()); } else { selectedContacts.remove(contact.getContactId()); contact.setChecked(false); if (onContactSelectedListener != null) onContactSelectedListener.onContactDeselected(contact.getNumber()); } } } public void setOnContactSelectedListener(OnContactSelectedListener onContactSelectedListener) { this.onContactSelectedListener = onContactSelectedListener; } public void setOnRefreshListener(SwipeRefreshLayout.OnRefreshListener onRefreshListener) { this.swipeRefresh.setOnRefreshListener(onRefreshListener); } public interface OnContactSelectedListener { void onContactSelected(String number); void onContactDeselected(String number); } }