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;
|
2015-01-21 07:26:58 +00:00
|
|
|
import android.content.Intent;
|
2012-07-18 22:35:13 +00:00
|
|
|
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;
|
2014-06-29 03:40:57 +00:00
|
|
|
import android.support.v4.app.ListFragment;
|
2012-07-18 22:35:13 +00:00
|
|
|
import android.support.v4.app.LoaderManager;
|
|
|
|
import android.support.v4.content.Loader;
|
2014-06-29 03:40:57 +00:00
|
|
|
import android.text.TextUtils;
|
|
|
|
import android.support.v7.app.ActionBarActivity;
|
|
|
|
import android.support.v7.view.ActionMode;
|
2014-09-24 20:54:56 +00:00
|
|
|
import android.support.v4.widget.CursorAdapter;
|
2012-07-18 22:35:13 +00:00
|
|
|
import android.view.LayoutInflater;
|
2014-06-29 03:40:57 +00:00
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuInflater;
|
|
|
|
import android.view.MenuItem;
|
2012-07-18 22:35:13 +00:00
|
|
|
import android.view.View;
|
2015-01-21 07:26:58 +00:00
|
|
|
import android.view.View.OnClickListener;
|
2012-07-18 22:35:13 +00:00
|
|
|
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.ListView;
|
|
|
|
|
2015-01-21 07:26:58 +00:00
|
|
|
import com.melnykov.fab.FloatingActionButton;
|
|
|
|
|
2014-08-04 20:13:47 +00:00
|
|
|
import org.thoughtcrime.securesms.components.DefaultSmsReminder;
|
2015-01-12 04:27:34 +00:00
|
|
|
import org.thoughtcrime.securesms.components.ExpiredBuildReminder;
|
2014-08-04 20:13:47 +00:00
|
|
|
import org.thoughtcrime.securesms.components.PushRegistrationReminder;
|
|
|
|
import org.thoughtcrime.securesms.components.ReminderView;
|
|
|
|
import org.thoughtcrime.securesms.components.SystemSmsImportReminder;
|
2014-06-13 06:57:37 +00:00
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
|
|
|
import org.thoughtcrime.securesms.database.loaders.ConversationListLoader;
|
|
|
|
import org.thoughtcrime.securesms.notifications.MessageNotifier;
|
|
|
|
import org.thoughtcrime.securesms.recipients.Recipients;
|
|
|
|
import org.thoughtcrime.securesms.util.Dialogs;
|
2014-11-03 23:16:04 +00:00
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
2014-06-13 06:57:37 +00:00
|
|
|
|
2012-07-18 22:35:13 +00:00
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
|
2014-06-29 03:40:57 +00:00
|
|
|
public class ConversationListFragment extends ListFragment
|
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;
|
2014-06-13 06:57:37 +00:00
|
|
|
private MasterSecret masterSecret;
|
|
|
|
private ActionMode actionMode;
|
2014-08-04 20:13:47 +00:00
|
|
|
private ReminderView reminderView;
|
2015-01-21 07:26:58 +00:00
|
|
|
private FloatingActionButton fab;
|
2014-08-04 20:13:47 +00:00
|
|
|
private String queryFilter = "";
|
2012-07-18 22:35:13 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
|
2014-07-23 03:27:05 +00:00
|
|
|
final View view = inflater.inflate(R.layout.conversation_list_fragment, container, false);
|
2014-08-04 20:13:47 +00:00
|
|
|
reminderView = new ReminderView(getActivity());
|
2015-01-21 07:26:58 +00:00
|
|
|
fab = (FloatingActionButton) view.findViewById(R.id.fab);
|
2014-07-23 03:27:05 +00:00
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDestroyView() {
|
|
|
|
super.onDestroyView();
|
|
|
|
getListView().setAdapter(null);
|
2012-07-18 22:35:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onActivityCreated(Bundle bundle) {
|
|
|
|
super.onActivityCreated(bundle);
|
|
|
|
|
|
|
|
setHasOptionsMenu(true);
|
2014-07-23 03:27:05 +00:00
|
|
|
getListView().setAdapter(null);
|
|
|
|
getListView().addHeaderView(reminderView);
|
2015-01-21 07:26:58 +00:00
|
|
|
fab.setOnClickListener(new OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
Intent intent = new Intent(getActivity(), NewConversationActivity.class);
|
|
|
|
intent.putExtra(NewConversationActivity.MASTER_SECRET_EXTRA, masterSecret);
|
|
|
|
startActivity(intent);
|
|
|
|
}
|
|
|
|
});
|
2012-07-18 22:35:13 +00:00
|
|
|
initializeListAdapter();
|
2012-07-26 23:21:45 +00:00
|
|
|
initializeBatchListener();
|
2012-07-18 22:35:13 +00:00
|
|
|
|
|
|
|
getLoaderManager().initLoader(0, null, this);
|
|
|
|
}
|
|
|
|
|
2014-07-23 03:27:05 +00:00
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
|
|
|
|
initializeReminders();
|
2015-03-05 23:51:23 +00:00
|
|
|
((ConversationListAdapter)getListAdapter()).notifyDataSetChanged();
|
2014-07-23 03:27:05 +00:00
|
|
|
}
|
|
|
|
|
2012-07-18 22:35:13 +00:00
|
|
|
@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 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;
|
2014-06-13 06:57:37 +00:00
|
|
|
if (actionMode == null) {
|
2013-05-24 01:10:15 +00:00
|
|
|
handleCreateConversation(headerView.getThreadId(), headerView.getRecipients(),
|
|
|
|
headerView.getDistributionType());
|
|
|
|
} else {
|
|
|
|
ConversationListAdapter adapter = (ConversationListAdapter)getListAdapter();
|
2013-10-20 16:25:01 +00:00
|
|
|
adapter.toggleThreadInBatchSet(headerView.getThreadId());
|
2014-02-25 03:23:21 +00:00
|
|
|
|
|
|
|
if (adapter.getBatchSelections().size() == 0) {
|
|
|
|
actionMode.finish();
|
2014-06-13 06:57:37 +00:00
|
|
|
} else {
|
|
|
|
actionMode.setSubtitle(getString(R.string.conversation_fragment_cab__batch_selection_amount,
|
|
|
|
adapter.getBatchSelections().size()));
|
2014-02-25 03:23:21 +00:00
|
|
|
}
|
|
|
|
|
2013-05-24 01:10:15 +00:00
|
|
|
adapter.notifyDataSetChanged();
|
|
|
|
}
|
2012-07-18 22:35:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-06-29 03:40:57 +00:00
|
|
|
public void setQueryFilter(String query) {
|
2014-04-06 00:58:38 +00:00
|
|
|
this.queryFilter = query;
|
|
|
|
getLoaderManager().restartLoader(0, null, this);
|
|
|
|
}
|
|
|
|
|
2014-06-29 03:40:57 +00:00
|
|
|
public void resetQueryFilter() {
|
|
|
|
if (!TextUtils.isEmpty(this.queryFilter)) {
|
|
|
|
setQueryFilter("");
|
|
|
|
}
|
2012-07-31 20:53:00 +00:00
|
|
|
}
|
|
|
|
|
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();
|
2014-06-29 03:40:57 +00:00
|
|
|
actionMode = ((ActionBarActivity)getActivity()).startSupportActionMode(ConversationListFragment.this);
|
2012-07-26 23:21:45 +00:00
|
|
|
|
|
|
|
adapter.initializeBatchMode(true);
|
2013-10-20 16:25:01 +00:00
|
|
|
adapter.toggleThreadInBatchSet(((ConversationListItem) v).getThreadId());
|
2012-07-26 23:21:45 +00:00
|
|
|
adapter.notifyDataSetChanged();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-07-23 03:27:05 +00:00
|
|
|
private void initializeReminders() {
|
2015-01-12 04:27:34 +00:00
|
|
|
if (ExpiredBuildReminder.isEligible(getActivity())) {
|
|
|
|
reminderView.showReminder(new ExpiredBuildReminder());
|
|
|
|
} else if (DefaultSmsReminder.isEligible(getActivity())) {
|
2014-08-04 20:13:47 +00:00
|
|
|
reminderView.showReminder(new DefaultSmsReminder(getActivity()));
|
|
|
|
} else if (SystemSmsImportReminder.isEligible(getActivity())) {
|
|
|
|
reminderView.showReminder(new SystemSmsImportReminder(getActivity(), masterSecret));
|
|
|
|
} else if (PushRegistrationReminder.isEligible(getActivity())) {
|
|
|
|
reminderView.showReminder(new PushRegistrationReminder(getActivity(), masterSecret));
|
2014-07-23 03:27:05 +00:00
|
|
|
} else {
|
2014-08-04 20:13:47 +00:00
|
|
|
reminderView.hide();
|
2014-07-23 03:27:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-18 22:35:13 +00:00
|
|
|
private void initializeListAdapter() {
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
this.setListAdapter(new ConversationListAdapter(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());
|
2014-03-01 00:32:00 +00:00
|
|
|
alert.setIcon(Dialogs.resolveIcon(getActivity(), R.attr.dialog_alert_icon));
|
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(),
|
2014-06-29 03:40:57 +00:00
|
|
|
getActivity().getString(R.string.ConversationListFragment_deleting),
|
|
|
|
getActivity().getString(R.string.ConversationListFragment_deleting_selected_threads),
|
2013-02-04 19:12:03 +00:00
|
|
|
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();
|
2013-05-24 01:10:15 +00:00
|
|
|
if (actionMode != null) {
|
|
|
|
actionMode.finish();
|
|
|
|
actionMode = null;
|
|
|
|
}
|
2013-02-04 02:41:34 +00:00
|
|
|
}
|
|
|
|
}.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();
|
2014-06-16 07:42:19 +00:00
|
|
|
actionMode.setSubtitle(getString(R.string.conversation_fragment_cab__batch_selection_amount,
|
|
|
|
((ConversationListAdapter)this.getListAdapter()).getBatchSelections().size()));
|
2012-07-18 22:35:13 +00:00
|
|
|
}
|
|
|
|
|
2013-04-26 01:59:49 +00:00
|
|
|
private void handleCreateConversation(long threadId, Recipients recipients, int distributionType) {
|
|
|
|
listener.onCreateConversation(threadId, recipients, distributionType);
|
2012-07-18 22:35:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@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 {
|
2013-04-26 01:59:49 +00:00
|
|
|
public void onCreateConversation(long threadId, Recipients recipients, int distributionType);
|
2012-07-18 22:35:13 +00:00
|
|
|
}
|
|
|
|
|
2012-07-26 23:21:45 +00:00
|
|
|
@Override
|
|
|
|
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
|
2014-06-29 03:40:57 +00:00
|
|
|
MenuInflater inflater = getActivity().getMenuInflater();
|
2012-07-26 23:21:45 +00:00
|
|
|
inflater.inflate(R.menu.conversation_list_batch, menu);
|
|
|
|
|
2014-06-13 06:57:37 +00:00
|
|
|
mode.setTitle(R.string.conversation_fragment_cab__batch_selection_mode);
|
|
|
|
mode.setSubtitle(null);
|
2012-07-26 23:21:45 +00:00
|
|
|
|
|
|
|
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);
|
2013-07-22 22:30:12 +00:00
|
|
|
actionMode = null;
|
2012-07-26 23:21:45 +00:00
|
|
|
}
|
|
|
|
|
2012-07-18 22:35:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|