2012-07-18 22:35:13 +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.app.Activity;
|
|
|
|
import android.app.AlertDialog;
|
2013-02-04 02:41:34 +00:00
|
|
|
import android.app.ProgressDialog;
|
2012-07-18 22:35:13 +00:00
|
|
|
import android.content.DialogInterface;
|
|
|
|
import android.database.Cursor;
|
2013-02-04 02:41:34 +00:00
|
|
|
import android.os.AsyncTask;
|
2012-07-18 22:35:13 +00:00
|
|
|
import android.os.Bundle;
|
|
|
|
import android.support.v4.app.LoaderManager;
|
|
|
|
import android.support.v4.content.Loader;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
2012-07-26 23:21:45 +00:00
|
|
|
import android.widget.AdapterView;
|
2012-07-18 22:35:13 +00:00
|
|
|
import android.widget.CursorAdapter;
|
|
|
|
import android.widget.ListView;
|
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
|
|
|
import org.thoughtcrime.securesms.database.loaders.ConversationListLoader;
|
2013-02-08 19:57:54 +00:00
|
|
|
import org.thoughtcrime.securesms.notifications.MessageNotifier;
|
2012-07-18 22:35:13 +00:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipients;
|
|
|
|
|
2013-02-04 02:41:34 +00:00
|
|
|
import com.actionbarsherlock.app.SherlockListFragment;
|
|
|
|
import com.actionbarsherlock.view.ActionMode;
|
|
|
|
import com.actionbarsherlock.view.Menu;
|
|
|
|
import com.actionbarsherlock.view.MenuInflater;
|
|
|
|
import com.actionbarsherlock.view.MenuItem;
|
2013-02-25 03:37:37 +00:00
|
|
|
import com.actionbarsherlock.widget.SearchView;
|
2013-02-04 02:41:34 +00:00
|
|
|
|
2012-07-18 22:35:13 +00:00
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
|
|
|
|
public class ConversationListFragment extends SherlockListFragment
|
2012-07-26 23:21:45 +00:00
|
|
|
implements LoaderManager.LoaderCallbacks<Cursor>, ActionMode.Callback
|
2012-07-18 22:35:13 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
private ConversationSelectedListener listener;
|
|
|
|
private MasterSecret masterSecret;
|
2012-07-31 20:53:00 +00:00
|
|
|
private String queryFilter = "";
|
2012-07-18 22:35:13 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
|
|
|
|
return inflater.inflate(R.layout.conversation_list_fragment, container, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onActivityCreated(Bundle bundle) {
|
|
|
|
super.onActivityCreated(bundle);
|
|
|
|
|
|
|
|
setHasOptionsMenu(true);
|
|
|
|
initializeListAdapter();
|
2012-07-26 23:21:45 +00:00
|
|
|
initializeBatchListener();
|
2012-07-18 22:35:13 +00:00
|
|
|
|
|
|
|
getLoaderManager().initLoader(0, null, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAttach(Activity activity) {
|
2013-01-10 04:39:05 +00:00
|
|
|
super.onAttach(activity);
|
|
|
|
this.listener = (ConversationSelectedListener)activity;
|
2012-07-18 22:35:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPrepareOptionsMenu(Menu menu) {
|
2012-07-31 20:53:00 +00:00
|
|
|
MenuInflater inflater = this.getSherlockActivity().getSupportMenuInflater();
|
|
|
|
|
2013-02-25 03:37:37 +00:00
|
|
|
if (this.masterSecret != null) {
|
2012-07-26 23:21:45 +00:00
|
|
|
inflater.inflate(R.menu.conversation_list, menu);
|
2013-02-25 03:37:37 +00:00
|
|
|
initializeSearch((SearchView)menu.findItem(R.id.menu_search).getActionView());
|
2012-07-31 20:53:00 +00:00
|
|
|
} else {
|
|
|
|
inflater.inflate(R.menu.conversation_list_empty, menu);
|
2012-07-18 22:35:13 +00:00
|
|
|
}
|
|
|
|
|
2012-07-26 23:21:45 +00:00
|
|
|
super.onPrepareOptionsMenu(menu);
|
2012-07-18 22:35:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onListItemClick(ListView l, View v, int position, long id) {
|
2012-07-26 23:21:45 +00:00
|
|
|
if (v instanceof ConversationListItem) {
|
|
|
|
ConversationListItem headerView = (ConversationListItem) v;
|
2012-07-18 22:35:13 +00:00
|
|
|
handleCreateConversation(headerView.getThreadId(), headerView.getRecipients());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setMasterSecret(MasterSecret masterSecret) {
|
2013-01-10 04:39:05 +00:00
|
|
|
if (this.masterSecret != masterSecret) {
|
|
|
|
this.masterSecret = masterSecret;
|
|
|
|
initializeListAdapter();
|
|
|
|
}
|
2012-07-18 22:35:13 +00:00
|
|
|
}
|
|
|
|
|
2013-02-25 03:37:37 +00:00
|
|
|
private void initializeSearch(SearchView searchView) {
|
|
|
|
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
|
2012-07-31 20:53:00 +00:00
|
|
|
@Override
|
|
|
|
public boolean onQueryTextSubmit(String query) {
|
|
|
|
ConversationListFragment.this.queryFilter = query;
|
|
|
|
ConversationListFragment.this.getLoaderManager().restartLoader(0, null, ConversationListFragment.this);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public boolean onQueryTextChange(String newText) {
|
|
|
|
return onQueryTextSubmit(newText);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-07-26 23:21:45 +00:00
|
|
|
private void initializeBatchListener() {
|
|
|
|
getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
|
|
|
|
@Override
|
|
|
|
public boolean onItemLongClick(AdapterView<?> arg0, View v, int position, long id) {
|
|
|
|
ConversationListAdapter adapter = (ConversationListAdapter)getListAdapter();
|
|
|
|
getSherlockActivity().startActionMode(ConversationListFragment.this);
|
|
|
|
|
|
|
|
adapter.initializeBatchMode(true);
|
|
|
|
adapter.addToBatchSet(((ConversationListItem)v).getThreadId());
|
|
|
|
adapter.notifyDataSetChanged();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-07-18 22:35:13 +00:00
|
|
|
private void initializeListAdapter() {
|
|
|
|
if (this.masterSecret == null) {
|
|
|
|
this.setListAdapter(new ConversationListAdapter(getActivity(), null));
|
|
|
|
} else {
|
|
|
|
this.setListAdapter(new DecryptingConversationListAdapter(getActivity(), null, masterSecret));
|
|
|
|
}
|
|
|
|
|
2013-01-06 23:46:26 +00:00
|
|
|
getListView().setRecyclerListener((ConversationListAdapter)getListAdapter());
|
2012-07-18 22:35:13 +00:00
|
|
|
getLoaderManager().restartLoader(0, null, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handleDeleteAllSelected() {
|
|
|
|
AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
|
|
|
|
alert.setIcon(android.R.drawable.ic_dialog_alert);
|
2012-09-20 02:56:04 +00:00
|
|
|
alert.setTitle(R.string.ConversationListFragment_delete_threads_question);
|
|
|
|
alert.setMessage(R.string.ConversationListFragment_are_you_sure_you_wish_to_delete_all_selected_conversation_threads);
|
2012-07-18 22:35:13 +00:00
|
|
|
alert.setCancelable(true);
|
|
|
|
|
2012-09-22 19:53:56 +00:00
|
|
|
alert.setPositiveButton(R.string.delete, new DialogInterface.OnClickListener() {
|
2013-02-04 02:41:34 +00:00
|
|
|
@Override
|
2012-07-18 22:35:13 +00:00
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
2013-02-04 02:41:34 +00:00
|
|
|
final Set<Long> selectedConversations = ((ConversationListAdapter)getListAdapter())
|
2012-07-18 22:35:13 +00:00
|
|
|
.getBatchSelections();
|
|
|
|
|
|
|
|
if (!selectedConversations.isEmpty()) {
|
2013-02-04 02:41:34 +00:00
|
|
|
new AsyncTask<Void, Void, Void>() {
|
|
|
|
private ProgressDialog dialog;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPreExecute() {
|
2013-02-04 19:12:03 +00:00
|
|
|
dialog = ProgressDialog.show(getActivity(),
|
|
|
|
getSherlockActivity().getString(R.string.ConversationListFragment_deleting),
|
|
|
|
getSherlockActivity().getString(R.string.ConversationListFragment_deleting_selected_threads),
|
|
|
|
true, false);
|
2013-02-04 02:41:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected Void doInBackground(Void... params) {
|
|
|
|
DatabaseFactory.getThreadDatabase(getActivity()).deleteConversations(selectedConversations);
|
2013-02-08 19:57:54 +00:00
|
|
|
MessageNotifier.updateNotification(getActivity(), masterSecret);
|
2013-02-04 02:41:34 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Void result) {
|
|
|
|
dialog.dismiss();
|
|
|
|
}
|
|
|
|
}.execute();
|
2012-07-18 22:35:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2012-09-22 19:53:56 +00:00
|
|
|
alert.setNegativeButton(android.R.string.cancel, null);
|
2012-07-18 22:35:13 +00:00
|
|
|
alert.show();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handleSelectAllThreads() {
|
|
|
|
((ConversationListAdapter)this.getListAdapter()).selectAllThreads();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handleUnselectAllThreads() {
|
|
|
|
((ConversationListAdapter)this.getListAdapter()).selectAllThreads();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handleCreateConversation(long threadId, Recipients recipients) {
|
|
|
|
listener.onCreateConversation(threadId, recipients);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
|
2012-07-31 20:53:00 +00:00
|
|
|
return new ConversationListLoader(getActivity(), queryFilter);
|
2012-07-18 22:35:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onLoadFinished(Loader<Cursor> arg0, Cursor cursor) {
|
|
|
|
((CursorAdapter)getListAdapter()).changeCursor(cursor);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onLoaderReset(Loader<Cursor> arg0) {
|
|
|
|
((CursorAdapter)getListAdapter()).changeCursor(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public interface ConversationSelectedListener {
|
|
|
|
public void onCreateConversation(long threadId, Recipients recipients);
|
|
|
|
}
|
|
|
|
|
2012-07-26 23:21:45 +00:00
|
|
|
@Override
|
|
|
|
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
|
|
|
|
MenuInflater inflater = getSherlockActivity().getSupportMenuInflater();
|
|
|
|
inflater.inflate(R.menu.conversation_list_batch, menu);
|
|
|
|
|
|
|
|
LayoutInflater layoutInflater = getSherlockActivity().getLayoutInflater();
|
|
|
|
View actionModeView = layoutInflater.inflate(R.layout.conversation_fragment_cab, null);
|
|
|
|
|
|
|
|
mode.setCustomView(actionModeView);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
case R.id.menu_select_all: handleSelectAllThreads(); return true;
|
|
|
|
case R.id.menu_delete_selected: handleDeleteAllSelected(); return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDestroyActionMode(ActionMode mode) {
|
|
|
|
((ConversationListAdapter)getListAdapter()).initializeBatchMode(false);
|
|
|
|
}
|
|
|
|
|
2012-07-18 22:35:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|