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;
|
|
|
|
|
2013-02-04 02:41:34 +00:00
|
|
|
import android.app.ProgressDialog;
|
2015-07-22 20:31:41 +00:00
|
|
|
import android.content.Context;
|
2012-07-18 22:35:13 +00:00
|
|
|
import android.content.DialogInterface;
|
2015-01-21 07:26:58 +00:00
|
|
|
import android.content.Intent;
|
2015-07-07 21:46:21 +00:00
|
|
|
import android.content.res.TypedArray;
|
2012-07-18 22:35:13 +00:00
|
|
|
import android.database.Cursor;
|
2015-07-07 21:46:21 +00:00
|
|
|
import android.graphics.Color;
|
2013-02-04 02:41:34 +00:00
|
|
|
import android.os.AsyncTask;
|
2015-07-07 21:46:21 +00:00
|
|
|
import android.os.Build;
|
2012-07-18 22:35:13 +00:00
|
|
|
import android.os.Bundle;
|
2015-04-04 08:38:44 +00:00
|
|
|
import android.support.v4.app.Fragment;
|
2012-07-18 22:35:13 +00:00
|
|
|
import android.support.v4.app.LoaderManager;
|
|
|
|
import android.support.v4.content.Loader;
|
2015-06-09 14:37:20 +00:00
|
|
|
import android.support.v7.app.AppCompatActivity;
|
2014-06-29 03:40:57 +00:00
|
|
|
import android.support.v7.view.ActionMode;
|
2015-04-04 08:38:44 +00:00
|
|
|
import android.support.v7.widget.LinearLayoutManager;
|
|
|
|
import android.support.v7.widget.RecyclerView;
|
2015-03-11 21:23:45 +00:00
|
|
|
import android.text.TextUtils;
|
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;
|
|
|
|
|
2015-03-24 12:44:22 +00:00
|
|
|
import com.afollestad.materialdialogs.AlertDialogWrapper;
|
2015-01-21 07:26:58 +00:00
|
|
|
import com.melnykov.fab.FloatingActionButton;
|
|
|
|
|
2015-04-04 08:38:44 +00:00
|
|
|
import org.thoughtcrime.securesms.ConversationListAdapter.ItemClickListener;
|
2015-10-14 04:44:01 +00:00
|
|
|
import org.thoughtcrime.securesms.components.reminder.DefaultSmsReminder;
|
|
|
|
import org.thoughtcrime.securesms.components.reminder.ExpiredBuildReminder;
|
|
|
|
import org.thoughtcrime.securesms.components.reminder.PushRegistrationReminder;
|
|
|
|
import org.thoughtcrime.securesms.components.reminder.Reminder;
|
|
|
|
import org.thoughtcrime.securesms.components.reminder.ReminderView;
|
2015-10-28 23:33:06 +00:00
|
|
|
import org.thoughtcrime.securesms.components.reminder.ReminderView.OnDismissListener;
|
2015-10-19 18:23:12 +00:00
|
|
|
import org.thoughtcrime.securesms.components.reminder.ShareReminder;
|
2015-10-14 04:44:01 +00:00
|
|
|
import org.thoughtcrime.securesms.components.reminder.SystemSmsImportReminder;
|
2015-03-11 21:23:45 +00:00
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
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;
|
2015-10-28 23:50:45 +00:00
|
|
|
import org.thoughtcrime.securesms.util.Util;
|
2015-07-22 20:31:41 +00:00
|
|
|
import org.whispersystems.libaxolotl.util.guava.Optional;
|
2014-06-13 06:57:37 +00:00
|
|
|
|
2015-03-19 20:08:48 +00:00
|
|
|
import java.util.Locale;
|
2012-07-18 22:35:13 +00:00
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
|
2015-04-04 08:38:44 +00:00
|
|
|
public class ConversationListFragment extends Fragment
|
|
|
|
implements LoaderManager.LoaderCallbacks<Cursor>, ActionMode.Callback, ItemClickListener
|
2012-07-18 22:35:13 +00:00
|
|
|
{
|
2015-04-04 08:38:44 +00:00
|
|
|
private MasterSecret masterSecret;
|
|
|
|
private ActionMode actionMode;
|
|
|
|
private RecyclerView list;
|
|
|
|
private ReminderView reminderView;
|
|
|
|
private FloatingActionButton fab;
|
2015-03-19 20:08:48 +00:00
|
|
|
private Locale locale;
|
2015-04-04 08:38:44 +00:00
|
|
|
private String queryFilter = "";
|
2012-07-18 22:35:13 +00:00
|
|
|
|
2014-12-15 20:25:55 +00:00
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle icicle) {
|
|
|
|
super.onCreate(icicle);
|
|
|
|
masterSecret = getArguments().getParcelable("master_secret");
|
2015-03-19 20:08:48 +00:00
|
|
|
locale = (Locale) getArguments().getSerializable(PassphraseRequiredActionBarActivity.LOCALE_EXTRA);
|
2014-12-15 20:25:55 +00:00
|
|
|
}
|
|
|
|
|
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);
|
2015-04-04 08:38:44 +00:00
|
|
|
reminderView = (ReminderView) view.findViewById(R.id.reminder);
|
|
|
|
list = (RecyclerView) view.findViewById(R.id.list);
|
2015-01-21 07:26:58 +00:00
|
|
|
fab = (FloatingActionButton) view.findViewById(R.id.fab);
|
2015-10-28 23:33:06 +00:00
|
|
|
reminderView.setOnDismissListener(new OnDismissListener() {
|
|
|
|
@Override public void onDismiss() {
|
|
|
|
updateReminders();
|
|
|
|
}
|
|
|
|
});
|
2015-04-04 08:38:44 +00:00
|
|
|
list.setHasFixedSize(true);
|
|
|
|
list.setLayoutManager(new LinearLayoutManager(getActivity()));
|
2014-07-23 03:27:05 +00:00
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
2012-07-18 22:35:13 +00:00
|
|
|
@Override
|
|
|
|
public void onActivityCreated(Bundle bundle) {
|
|
|
|
super.onActivityCreated(bundle);
|
|
|
|
|
|
|
|
setHasOptionsMenu(true);
|
2015-01-21 07:26:58 +00:00
|
|
|
fab.setOnClickListener(new OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
2014-12-15 20:25:55 +00:00
|
|
|
startActivity(new Intent(getActivity(), NewConversationActivity.class));
|
2015-01-21 07:26:58 +00:00
|
|
|
}
|
|
|
|
});
|
2012-07-18 22:35:13 +00:00
|
|
|
initializeListAdapter();
|
|
|
|
}
|
|
|
|
|
2014-07-23 03:27:05 +00:00
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
|
2015-10-28 23:33:06 +00:00
|
|
|
updateReminders();
|
2015-04-04 08:38:44 +00:00
|
|
|
list.getAdapter().notifyDataSetChanged();
|
2014-07-23 03:27:05 +00:00
|
|
|
}
|
|
|
|
|
2015-04-04 08:38:44 +00:00
|
|
|
public ConversationListAdapter getListAdapter() {
|
|
|
|
return (ConversationListAdapter) list.getAdapter();
|
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
|
|
|
}
|
|
|
|
|
2015-10-28 23:33:06 +00:00
|
|
|
private void updateReminders() {
|
2015-07-22 20:31:41 +00:00
|
|
|
reminderView.hide();
|
|
|
|
new AsyncTask<Context, Void, Optional<? extends Reminder>>() {
|
|
|
|
@Override protected Optional<? extends Reminder> doInBackground(Context... params) {
|
|
|
|
final Context context = params[0];
|
|
|
|
if (ExpiredBuildReminder.isEligible(context)) {
|
2015-10-14 04:44:01 +00:00
|
|
|
return Optional.of(new ExpiredBuildReminder(context));
|
2015-07-22 20:31:41 +00:00
|
|
|
} else if (DefaultSmsReminder.isEligible(context)) {
|
|
|
|
return Optional.of(new DefaultSmsReminder(context));
|
2015-10-28 23:50:45 +00:00
|
|
|
} else if (Util.isDefaultSmsProvider(context) && SystemSmsImportReminder.isEligible(context)) {
|
2015-07-22 20:31:41 +00:00
|
|
|
return Optional.of((new SystemSmsImportReminder(context, masterSecret)));
|
|
|
|
} else if (PushRegistrationReminder.isEligible(context)) {
|
|
|
|
return Optional.of((new PushRegistrationReminder(context, masterSecret)));
|
2015-10-19 18:23:12 +00:00
|
|
|
} else if (ShareReminder.isEligible(context)) {
|
|
|
|
return Optional.of(new ShareReminder(context));
|
2015-07-22 20:31:41 +00:00
|
|
|
} else {
|
|
|
|
return Optional.absent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override protected void onPostExecute(Optional<? extends Reminder> reminder) {
|
|
|
|
if (reminder.isPresent() && getActivity() != null && !isRemoving()) {
|
|
|
|
reminderView.showReminder(reminder.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}.execute(getActivity());
|
2014-07-23 03:27:05 +00:00
|
|
|
}
|
|
|
|
|
2012-07-18 22:35:13 +00:00
|
|
|
private void initializeListAdapter() {
|
2015-03-19 20:08:48 +00:00
|
|
|
list.setAdapter(new ConversationListAdapter(getActivity(), masterSecret, locale, null, this));
|
2012-07-18 22:35:13 +00:00
|
|
|
getLoaderManager().restartLoader(0, null, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handleDeleteAllSelected() {
|
2015-10-23 21:01:53 +00:00
|
|
|
int conversationsCount = getListAdapter().getBatchSelections().size();
|
2015-03-24 12:44:22 +00:00
|
|
|
AlertDialogWrapper.Builder alert = new AlertDialogWrapper.Builder(getActivity());
|
|
|
|
alert.setIconAttribute(R.attr.dialog_alert_icon);
|
2015-10-23 21:01:53 +00:00
|
|
|
alert.setTitle(getContext().getResources().getQuantityString(
|
|
|
|
R.plurals.ConversationListFragment_delete_selected_conversations,
|
|
|
|
conversationsCount, conversationsCount));
|
|
|
|
alert.setMessage(getContext().getResources().getQuantityString(
|
|
|
|
R.plurals.ConversationListFragment_this_will_permanently_delete_all_n_selected_conversations,
|
|
|
|
conversationsCount, conversationsCount));
|
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) {
|
2015-04-04 08:38:44 +00:00
|
|
|
final Set<Long> selectedConversations = (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() {
|
2015-04-04 08:38:44 +00:00
|
|
|
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) {
|
2014-12-15 20:25:55 +00:00
|
|
|
((ConversationSelectedListener)getActivity()).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) {
|
2015-04-04 08:38:44 +00:00
|
|
|
getListAdapter().changeCursor(cursor);
|
2012-07-18 22:35:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onLoaderReset(Loader<Cursor> arg0) {
|
2015-04-04 08:38:44 +00:00
|
|
|
getListAdapter().changeCursor(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onItemClick(ConversationListItem item) {
|
|
|
|
if (actionMode == null) {
|
|
|
|
handleCreateConversation(item.getThreadId(), item.getRecipients(),
|
|
|
|
item.getDistributionType());
|
|
|
|
} else {
|
|
|
|
ConversationListAdapter adapter = (ConversationListAdapter)list.getAdapter();
|
|
|
|
adapter.toggleThreadInBatchSet(item.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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onItemLongClick(ConversationListItem item) {
|
2015-06-09 14:37:20 +00:00
|
|
|
actionMode = ((AppCompatActivity)getActivity()).startSupportActionMode(ConversationListFragment.this);
|
2015-04-04 08:38:44 +00:00
|
|
|
|
|
|
|
getListAdapter().initializeBatchMode(true);
|
|
|
|
getListAdapter().toggleThreadInBatchSet(item.getThreadId());
|
|
|
|
getListAdapter().notifyDataSetChanged();
|
2012-07-18 22:35:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public interface ConversationSelectedListener {
|
2015-04-04 08:38:44 +00:00
|
|
|
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
|
|
|
|
2015-07-07 21:46:21 +00:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
|
|
|
getActivity().getWindow()
|
|
|
|
.setStatusBarColor(getResources().getColor(R.color.action_mode_status_bar));
|
|
|
|
}
|
|
|
|
|
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) {
|
2015-04-04 08:38:44 +00:00
|
|
|
getListAdapter().initializeBatchMode(false);
|
2015-07-07 21:46:21 +00:00
|
|
|
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
|
|
|
TypedArray color = getActivity().getTheme()
|
|
|
|
.obtainStyledAttributes(new int[] { android.R.attr.statusBarColor });
|
|
|
|
getActivity().getWindow().setStatusBarColor(color.getColor(0, Color.BLACK));
|
|
|
|
color.recycle();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|