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

311 lines
11 KiB
Java
Raw Normal View History

/**
* 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;
import android.app.ProgressDialog;
import android.content.DialogInterface;
2015-01-21 07:26:58 +00:00
import android.content.Intent;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
2014-06-29 03:40:57 +00:00
import android.support.v4.app.ListFragment;
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;
import android.support.v4.widget.CursorAdapter;
import android.view.LayoutInflater;
2014-06-29 03:40:57 +00:00
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
2015-01-21 07:26:58 +00:00
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
2015-01-21 07:26:58 +00:00
import com.melnykov.fab.FloatingActionButton;
import org.thoughtcrime.securesms.components.DefaultSmsReminder;
import org.thoughtcrime.securesms.components.ExpiredBuildReminder;
import org.thoughtcrime.securesms.components.PushRegistrationReminder;
import org.thoughtcrime.securesms.components.ReminderView;
import org.thoughtcrime.securesms.components.SystemSmsImportReminder;
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;
import org.thoughtcrime.securesms.crypto.MasterSecret;
import java.util.Set;
2014-06-29 03:40:57 +00:00
public class ConversationListFragment extends ListFragment
implements LoaderManager.LoaderCallbacks<Cursor>, ActionMode.Callback
{
private ConversationSelectedListener listener;
private MasterSecret masterSecret;
private ActionMode actionMode;
private ReminderView reminderView;
2015-01-21 07:26:58 +00:00
private FloatingActionButton fab;
private String queryFilter = "";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
final View view = inflater.inflate(R.layout.conversation_list_fragment, container, false);
reminderView = new ReminderView(getActivity());
2015-01-21 07:26:58 +00:00
fab = (FloatingActionButton) view.findViewById(R.id.fab);
return view;
}
@Override
public void onDestroyView() {
super.onDestroyView();
getListView().setAdapter(null);
}
@Override
public void onActivityCreated(Bundle bundle) {
super.onActivityCreated(bundle);
setHasOptionsMenu(true);
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);
}
});
initializeListAdapter();
initializeBatchListener();
getLoaderManager().initLoader(0, null, this);
}
@Override
public void onResume() {
super.onResume();
initializeReminders();
((ConversationListAdapter)getListAdapter()).notifyDataSetChanged();
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
this.listener = (ConversationSelectedListener)activity;
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
if (v instanceof ConversationListItem) {
ConversationListItem headerView = (ConversationListItem) v;
if (actionMode == null) {
handleCreateConversation(headerView.getThreadId(), headerView.getRecipients(),
headerView.getDistributionType());
} else {
ConversationListAdapter adapter = (ConversationListAdapter)getListAdapter();
2013-10-20 16:25:01 +00:00
adapter.toggleThreadInBatchSet(headerView.getThreadId());
if (adapter.getBatchSelections().size() == 0) {
actionMode.finish();
} else {
actionMode.setSubtitle(getString(R.string.conversation_fragment_cab__batch_selection_amount,
adapter.getBatchSelections().size()));
}
adapter.notifyDataSetChanged();
}
}
}
public void setMasterSecret(MasterSecret masterSecret) {
if (this.masterSecret != masterSecret) {
this.masterSecret = masterSecret;
initializeListAdapter();
}
}
2014-06-29 03:40:57 +00:00
public void setQueryFilter(String query) {
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
}
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);
adapter.initializeBatchMode(true);
2013-10-20 16:25:01 +00:00
adapter.toggleThreadInBatchSet(((ConversationListItem) v).getThreadId());
adapter.notifyDataSetChanged();
return true;
}
});
}
private void initializeReminders() {
if (ExpiredBuildReminder.isEligible(getActivity())) {
reminderView.showReminder(new ExpiredBuildReminder());
} else if (DefaultSmsReminder.isEligible(getActivity())) {
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));
} else {
reminderView.hide();
}
}
private void initializeListAdapter() {
this.setListAdapter(new ConversationListAdapter(getActivity(), null, masterSecret));
getListView().setRecyclerListener((ConversationListAdapter)getListAdapter());
getLoaderManager().restartLoader(0, null, this);
}
private void handleDeleteAllSelected() {
AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
alert.setIcon(Dialogs.resolveIcon(getActivity(), R.attr.dialog_alert_icon));
alert.setTitle(R.string.ConversationListFragment_delete_threads_question);
alert.setMessage(R.string.ConversationListFragment_are_you_sure_you_wish_to_delete_all_selected_conversation_threads);
alert.setCancelable(true);
alert.setPositiveButton(R.string.delete, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
final Set<Long> selectedConversations = ((ConversationListAdapter)getListAdapter())
.getBatchSelections();
if (!selectedConversations.isEmpty()) {
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);
}
@Override
protected Void doInBackground(Void... params) {
DatabaseFactory.getThreadDatabase(getActivity()).deleteConversations(selectedConversations);
MessageNotifier.updateNotification(getActivity(), masterSecret);
return null;
}
@Override
protected void onPostExecute(Void result) {
dialog.dismiss();
if (actionMode != null) {
actionMode.finish();
actionMode = null;
}
}
}.execute();
}
}
});
alert.setNegativeButton(android.R.string.cancel, null);
alert.show();
}
private void handleSelectAllThreads() {
((ConversationListAdapter)this.getListAdapter()).selectAllThreads();
actionMode.setSubtitle(getString(R.string.conversation_fragment_cab__batch_selection_amount,
((ConversationListAdapter)this.getListAdapter()).getBatchSelections().size()));
}
2013-04-26 01:59:49 +00:00
private void handleCreateConversation(long threadId, Recipients recipients, int distributionType) {
listener.onCreateConversation(threadId, recipients, distributionType);
}
@Override
public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
2012-07-31 20:53:00 +00:00
return new ConversationListLoader(getActivity(), queryFilter);
}
@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);
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
2014-06-29 03:40:57 +00:00
MenuInflater inflater = getActivity().getMenuInflater();
inflater.inflate(R.menu.conversation_list_batch, menu);
mode.setTitle(R.string.conversation_fragment_cab__batch_selection_mode);
mode.setSubtitle(null);
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;
}
}