session-android/src/org/thoughtcrime/securesms/ContactSelectionListFragment.java

189 lines
6.5 KiB
Java
Raw Normal View History

2014-01-19 03:17:08 +01:00
/**
* Copyright (C) 2015 Open Whisper Systems
2014-01-19 03:17:08 +01: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;
import android.os.Build;
2014-01-19 03:17:08 +01:00
import android.os.Bundle;
2015-10-19 20:23:12 +02:00
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
2014-01-19 03:17:08 +01:00
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import android.support.v4.widget.CursorAdapter;
import android.support.v4.widget.SwipeRefreshLayout;
2014-01-19 03:17:08 +01:00
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
2014-01-19 03:17:08 +01:00
import android.widget.TextView;
import org.thoughtcrime.securesms.contacts.ContactSelectionListAdapter;
import org.thoughtcrime.securesms.contacts.ContactSelectionListItem;
2015-07-14 23:31:03 +02:00
import org.thoughtcrime.securesms.contacts.ContactsCursorLoader;
2014-01-19 03:17:08 +01:00
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import se.emilsjolander.stickylistheaders.StickyListHeadersListView;
2014-01-19 03:17:08 +01:00
/**
* Fragment for selecting a one or more contacts from a list.
2014-01-19 03:17:08 +01:00
*
* @author Moxie Marlinspike
*
*/
public class ContactSelectionListFragment extends Fragment
implements LoaderManager.LoaderCallbacks<Cursor>
2014-01-19 03:17:08 +01:00
{
private static final String TAG = ContactSelectionListFragment.class.getSimpleName();
2015-10-19 20:23:12 +02: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;
private TextView emptyText;
2014-01-19 03:17:08 +01:00
private Map<Long, String> selectedContacts;
private OnContactSelectedListener onContactSelectedListener;
private StickyListHeadersListView listView;
private SwipeRefreshLayout swipeRefresh;
private String cursorFilter;
2014-01-19 03:17:08 +01:00
@Override
public void onActivityCreated(Bundle icicle) {
super.onCreate(icicle);
initializeCursor();
}
@Override
public void onResume() {
super.onResume();
2014-01-19 03:17:08 +01:00
}
@Override
public void onPause() {
super.onPause();
2014-01-19 03:17:08 +01:00
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.contact_selection_list_fragment, container, false);
2015-07-15 23:28:51 +02:00
emptyText = (TextView) view.findViewById(android.R.id.empty);
swipeRefresh = (SwipeRefreshLayout) view.findViewById(R.id.swipe_refresh);
listView = (StickyListHeadersListView) view.findViewById(android.R.id.list);
listView.setFocusable(true);
listView.setFastScrollEnabled(true);
listView.setDrawingListUnderStickyHeader(false);
listView.setOnItemClickListener(new ListClickListener());
2015-10-19 20:23:12 +02:00
swipeRefresh.setEnabled(getActivity().getIntent().getBooleanExtra(REFRESHABLE, true) &&
Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN);
return view;
2014-01-19 03:17:08 +01:00
}
2015-10-19 20:23:12 +02:00
public @NonNull List<String> getSelectedContacts() {
List<String> selected = new LinkedList<>();
2015-10-19 20:23:12 +02:00
if (selectedContacts != null) {
selected.addAll(selectedContacts.values());
}
2014-01-19 03:17:08 +01:00
return selected;
2014-01-19 03:17:08 +01:00
}
2015-10-19 20:23:12 +02:00
private boolean isMulti() {
return getActivity().getIntent().getBooleanExtra(MULTI_SELECT, false);
2014-01-19 03:17:08 +01:00
}
private void initializeCursor() {
2015-10-19 20:23:12 +02:00
ContactSelectionListAdapter adapter = new ContactSelectionListAdapter(getActivity(), null, isMulti());
selectedContacts = adapter.getSelectedContacts();
listView.setAdapter(adapter);
2014-01-19 03:17:08 +01:00
this.getLoaderManager().initLoader(0, null, this);
}
public void setQueryFilter(String filter) {
this.cursorFilter = filter;
this.getLoaderManager().restartLoader(0, null, this);
2014-01-19 03:17:08 +01:00
}
public void resetQueryFilter() {
setQueryFilter(null);
swipeRefresh.setRefreshing(false);
2014-01-19 03:17:08 +01:00
}
2015-10-19 20:23:12 +02:00
public void reset() {
selectedContacts.clear();
getLoaderManager().restartLoader(0, null, this);
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
2015-10-19 20:23:12 +02:00
return new ContactsCursorLoader(getActivity(),
getActivity().getIntent().getIntExtra(DISPLAY_MODE, DISPLAY_MODE_ALL),
cursorFilter);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
((CursorAdapter) listView.getAdapter()).changeCursor(data);
emptyText.setText(R.string.contact_selection_group_activity__no_contacts);
2014-01-19 03:17:08 +01:00
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
((CursorAdapter) listView.getAdapter()).changeCursor(null);
2014-01-19 03:17:08 +01:00
}
private class ListClickListener implements AdapterView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> l, View v, int position, long id) {
ContactSelectionListItem contact = (ContactSelectionListItem)v;
2015-10-19 20:23:12 +02:00
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);
2015-10-19 20:23:12 +02:00
if (onContactSelectedListener != null) onContactSelectedListener.onContactDeselected(contact.getNumber());
2014-01-19 03:17:08 +01:00
}
}
}
public void setOnContactSelectedListener(OnContactSelectedListener onContactSelectedListener) {
this.onContactSelectedListener = onContactSelectedListener;
2014-01-19 03:17:08 +01:00
}
public void setOnRefreshListener(SwipeRefreshLayout.OnRefreshListener onRefreshListener) {
this.swipeRefresh.setOnRefreshListener(onRefreshListener);
}
public interface OnContactSelectedListener {
2015-10-19 20:23:12 +02:00
void onContactSelected(String number);
void onContactDeselected(String number);
2014-01-19 03:17:08 +01:00
}
}