2014-01-19 02:17:08 +00:00
|
|
|
/**
|
|
|
|
* Copyright (C) 2011 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.database.Cursor;
|
|
|
|
import android.os.Bundle;
|
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;
|
2014-03-18 06:25:09 +00:00
|
|
|
import android.support.v4.widget.CursorAdapter;
|
2014-04-01 23:40:16 +00:00
|
|
|
import android.text.Editable;
|
|
|
|
import android.text.TextWatcher;
|
2014-01-19 02:17:08 +00:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
2014-03-18 06:25:09 +00:00
|
|
|
import android.widget.AdapterView;
|
2014-04-01 23:40:16 +00:00
|
|
|
import android.widget.EditText;
|
2014-01-19 02:17:08 +00:00
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.contacts.ContactAccessor;
|
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;
|
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;
|
|
|
|
|
|
|
|
import se.emilsjolander.stickylistheaders.StickyListHeadersListView;
|
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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2014-03-18 06:25:09 +00:00
|
|
|
public class PushContactSelectionListFragment extends Fragment
|
|
|
|
implements LoaderManager.LoaderCallbacks<Cursor>
|
2014-01-19 02:17:08 +00:00
|
|
|
{
|
2014-03-18 06:25:09 +00:00
|
|
|
private static final String TAG = "ContactSelectFragment";
|
|
|
|
|
|
|
|
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;
|
|
|
|
private StickyListHeadersListView listView;
|
2014-04-01 23:40:16 +00:00
|
|
|
private String cursorFilter;
|
2014-02-24 22:43:38 +00:00
|
|
|
|
2015-05-19 21:00:54 +00:00
|
|
|
private boolean multi = false;
|
2014-01-19 02:17:08 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onActivityCreated(Bundle icicle) {
|
|
|
|
super.onCreate(icicle);
|
|
|
|
initializeResources();
|
|
|
|
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) {
|
|
|
|
return inflater.inflate(R.layout.push_contact_selection_list_activity, container, false);
|
2014-01-19 02:17:08 +00:00
|
|
|
}
|
|
|
|
|
2015-05-19 21:00:54 +00:00
|
|
|
public List<String> getSelectedContacts() {
|
2014-03-18 06:25:09 +00:00
|
|
|
if (selectedContacts == null) return null;
|
|
|
|
|
2015-05-19 21:00:54 +00:00
|
|
|
List<String> selected = new LinkedList<>();
|
2014-02-03 19:52:27 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-03-18 06:25:09 +00:00
|
|
|
public void setMultiSelect(boolean multi) {
|
|
|
|
this.multi = multi;
|
2014-01-19 02:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void initializeCursor() {
|
2014-03-18 06:25:09 +00:00
|
|
|
ContactSelectionListAdapter adapter = new ContactSelectionListAdapter(getActivity(), null, multi);
|
|
|
|
selectedContacts = adapter.getSelectedContacts();
|
|
|
|
listView.setAdapter(adapter);
|
2014-01-19 02:17:08 +00:00
|
|
|
this.getLoaderManager().initLoader(0, null, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void initializeResources() {
|
2014-03-18 06:25:09 +00:00
|
|
|
emptyText = (TextView) getView().findViewById(android.R.id.empty);
|
|
|
|
listView = (StickyListHeadersListView) getView().findViewById(android.R.id.list);
|
|
|
|
listView.setFocusable(true);
|
|
|
|
listView.setFastScrollEnabled(true);
|
|
|
|
listView.setDrawingListUnderStickyHeader(false);
|
|
|
|
listView.setOnItemClickListener(new ListClickListener());
|
2015-05-19 21:00:54 +00:00
|
|
|
|
|
|
|
EditText filterEditText = (EditText) getView().findViewById(R.id.filter);
|
2014-04-01 23:40:16 +00:00
|
|
|
filterEditText.addTextChangedListener(new TextWatcher() {
|
|
|
|
@Override
|
|
|
|
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
|
|
|
|
cursorFilter = charSequence.toString();
|
|
|
|
getLoaderManager().restartLoader(0, null, PushContactSelectionListFragment.this);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void afterTextChanged(Editable editable) {
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
cursorFilter = null;
|
2014-01-19 02:17:08 +00:00
|
|
|
}
|
|
|
|
|
2014-03-18 06:25:09 +00:00
|
|
|
public void update() {
|
|
|
|
this.getLoaderManager().restartLoader(0, null, this);
|
2014-01-19 02:17:08 +00:00
|
|
|
}
|
|
|
|
|
2014-03-18 06:25:09 +00:00
|
|
|
@Override
|
|
|
|
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
|
2014-04-01 23:40:16 +00:00
|
|
|
if (getActivity().getIntent().getBooleanExtra(PushContactSelectionActivity.PUSH_ONLY_EXTRA, false)) {
|
|
|
|
return ContactAccessor.getInstance().getCursorLoaderForPushContacts(getActivity(), cursorFilter);
|
|
|
|
} else {
|
|
|
|
return ContactAccessor.getInstance().getCursorLoaderForContacts(getActivity(), 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) {
|
|
|
|
((CursorAdapter) listView.getAdapter()).changeCursor(data);
|
|
|
|
emptyText.setText(R.string.contact_selection_group_activity__no_contacts);
|
2014-04-01 23:40:16 +00:00
|
|
|
if (data != null && data.getCount() < 40) listView.setFastScrollAlwaysVisible(false);
|
|
|
|
else listView.setFastScrollAlwaysVisible(true);
|
2014-01-19 02:17:08 +00:00
|
|
|
}
|
|
|
|
|
2014-03-18 06:25:09 +00:00
|
|
|
@Override
|
|
|
|
public void onLoaderReset(Loader<Cursor> loader) {
|
|
|
|
((CursorAdapter) listView.getAdapter()).changeCursor(null);
|
2014-01-19 02:17:08 +00:00
|
|
|
}
|
|
|
|
|
2014-03-18 06:25:09 +00:00
|
|
|
private class ListClickListener implements AdapterView.OnItemClickListener {
|
|
|
|
@Override
|
|
|
|
public void onItemClick(AdapterView<?> l, View v, int position, long id) {
|
2015-05-19 21:00:54 +00:00
|
|
|
ContactSelectionListItem contact = (ContactSelectionListItem)v;
|
|
|
|
|
|
|
|
if (!multi || !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);
|
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
|
|
|
}
|
|
|
|
|
2014-03-18 06:25:09 +00:00
|
|
|
public interface OnContactSelectedListener {
|
2015-05-19 21:00:54 +00:00
|
|
|
public void onContactSelected(String number);
|
2014-01-19 02:17:08 +00:00
|
|
|
}
|
|
|
|
}
|