mirror of
https://github.com/ArcaneChat/android.git
synced 2026-07-03 14:05:24 +02:00
allow to pin, archive, etc. when searching in the chatlist (#3002)
This commit is contained in:
committed by
GitHub
parent
97b84e1959
commit
217478e90b
@@ -1,6 +1,7 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<FrameLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
@@ -17,4 +18,14 @@
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"/>
|
||||
|
||||
</FrameLayout>
|
||||
<org.thoughtcrime.securesms.components.registration.PulsingFloatingActionButton
|
||||
android:id="@+id/fab"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="bottom|end"
|
||||
android:layout_margin="16dp"
|
||||
app:backgroundTint="?attr/fab_color"
|
||||
android:src="@drawable/ic_add_white_24dp"
|
||||
android:focusable="true"
|
||||
android:contentDescription="@string/menu_new_chat"/>
|
||||
</FrameLayout>
|
||||
|
||||
@@ -2,6 +2,16 @@
|
||||
<menu xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||
|
||||
<item android:title="@string/pin_chat"
|
||||
android:id="@+id/menu_pin_selected"
|
||||
android:icon="@drawable/ic_pin_white"
|
||||
app:showAsAction="always"/>
|
||||
|
||||
<item android:title="@string/menu_archive_chat"
|
||||
android:id="@+id/menu_archive_selected"
|
||||
android:icon="@drawable/ic_archive_white_24dp"
|
||||
app:showAsAction="always"/>
|
||||
|
||||
<item android:title="@string/menu_delete_chat"
|
||||
android:id="@+id/menu_delete_selected"
|
||||
android:icon="?menu_trash_icon"
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<menu xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||
|
||||
<item android:title="@string/menu_archive_chat"
|
||||
android:id="@+id/menu_archive_selected"
|
||||
android:icon="@drawable/ic_archive_white_24dp"
|
||||
app:showAsAction="always"/>
|
||||
|
||||
</menu>
|
||||
@@ -1,10 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<menu xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||
|
||||
<item android:title="@string/pin_chat"
|
||||
android:id="@+id/menu_pin_selected"
|
||||
android:icon="@drawable/ic_pin_white"
|
||||
app:showAsAction="always"/>
|
||||
|
||||
</menu>
|
||||
@@ -1,10 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<menu xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||
|
||||
<item android:title="@string/menu_unarchive_chat"
|
||||
android:id="@+id/menu_archive_selected"
|
||||
android:icon="@drawable/ic_unarchive_white_24dp"
|
||||
app:showAsAction="always"/>
|
||||
|
||||
</menu>
|
||||
@@ -0,0 +1,32 @@
|
||||
package org.thoughtcrime.securesms;
|
||||
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public abstract class BaseConversationListAdapter<T extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<T> {
|
||||
protected final Set<Long> batchSet = Collections.synchronizedSet(new HashSet<Long>());
|
||||
protected boolean batchMode = false;
|
||||
|
||||
public abstract void selectAllThreads();
|
||||
|
||||
public void initializeBatchMode(boolean toggle) {
|
||||
batchMode = toggle;
|
||||
batchSet.clear();
|
||||
notifyDataSetChanged();
|
||||
}
|
||||
|
||||
public void toggleThreadInBatchSet(long threadId) {
|
||||
if (batchSet.contains(threadId)) {
|
||||
batchSet.remove(threadId);
|
||||
} else if (threadId != -1) {
|
||||
batchSet.add(threadId);
|
||||
}
|
||||
}
|
||||
|
||||
public Set<Long> getBatchSelections() {
|
||||
return batchSet;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,380 @@
|
||||
package org.thoughtcrime.securesms;
|
||||
|
||||
import static org.thoughtcrime.securesms.util.RelayUtil.acquireRelayMessageContent;
|
||||
import static org.thoughtcrime.securesms.util.RelayUtil.getSharedText;
|
||||
import static org.thoughtcrime.securesms.util.RelayUtil.getSharedUris;
|
||||
import static org.thoughtcrime.securesms.util.RelayUtil.isForwarding;
|
||||
import static org.thoughtcrime.securesms.util.RelayUtil.isRelayingMessageContent;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.res.TypedArray;
|
||||
import android.graphics.Color;
|
||||
import android.net.Uri;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Build;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuInflater;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.appcompat.view.ActionMode;
|
||||
import androidx.core.content.ContextCompat;
|
||||
import androidx.fragment.app.Fragment;
|
||||
|
||||
import com.b44t.messenger.DcChat;
|
||||
import com.b44t.messenger.DcContext;
|
||||
import com.google.android.material.snackbar.Snackbar;
|
||||
|
||||
import org.thoughtcrime.securesms.components.registration.PulsingFloatingActionButton;
|
||||
import org.thoughtcrime.securesms.connect.DcHelper;
|
||||
import org.thoughtcrime.securesms.connect.DirectShareUtil;
|
||||
import org.thoughtcrime.securesms.util.SendRelayedMessageUtil;
|
||||
import org.thoughtcrime.securesms.util.task.SnackbarAsyncTask;
|
||||
import org.thoughtcrime.securesms.util.views.ProgressDialog;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.Locale;
|
||||
import java.util.Set;
|
||||
|
||||
public abstract class BaseConversationListFragment extends Fragment implements ActionMode.Callback {
|
||||
protected ActionMode actionMode;
|
||||
protected PulsingFloatingActionButton fab;
|
||||
|
||||
protected abstract boolean offerToArchive();
|
||||
protected abstract void setFabVisibility(boolean isActionMode);
|
||||
protected abstract BaseConversationListAdapter getListAdapter();
|
||||
|
||||
protected void onItemClick(long chatId) {
|
||||
if (actionMode == null) {
|
||||
((ConversationSelectedListener) requireActivity()).onCreateConversation((int)chatId);
|
||||
} else {
|
||||
BaseConversationListAdapter adapter = getListAdapter();
|
||||
adapter.toggleThreadInBatchSet(chatId);
|
||||
|
||||
if (adapter.getBatchSelections().isEmpty()) {
|
||||
actionMode.finish();
|
||||
} else {
|
||||
updateActionModeItems(actionMode.getMenu());
|
||||
actionMode.setTitle(String.valueOf(adapter.getBatchSelections().size()));
|
||||
}
|
||||
|
||||
adapter.notifyDataSetChanged();
|
||||
}
|
||||
}
|
||||
public void onItemLongClick(long chatId) {
|
||||
actionMode = ((AppCompatActivity)requireActivity()).startSupportActionMode(this);
|
||||
|
||||
if (actionMode != null) {
|
||||
getListAdapter().initializeBatchMode(true);
|
||||
getListAdapter().toggleThreadInBatchSet(chatId);
|
||||
getListAdapter().notifyDataSetChanged();
|
||||
Menu menu = actionMode.getMenu();
|
||||
if (menu != null) {
|
||||
updateActionModeItems(menu);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected void initializeFabClickListener(boolean isActionMode) {
|
||||
Intent intent = new Intent(getActivity(), NewConversationActivity.class);
|
||||
if (isRelayingMessageContent(getActivity())) {
|
||||
if (isActionMode) {
|
||||
fab.setOnClickListener(v -> {
|
||||
final Set<Long> selectedChats = getListAdapter().getBatchSelections();
|
||||
ArrayList<Uri> uris = getSharedUris(getActivity());
|
||||
String message;
|
||||
if (isForwarding(getActivity())) {
|
||||
message = String.format(Locale.getDefault(), getString(R.string.ask_forward_multiple), selectedChats.size());
|
||||
} else if (!uris.isEmpty()) {
|
||||
message = String.format(Locale.getDefault(), getString(R.string.share_multiple_attachments_multiple_chats), uris.size(), selectedChats.size());
|
||||
} else {
|
||||
message = String.format(Locale.getDefault(), getString(R.string.share_text_multiple_chats), selectedChats.size(), getSharedText(getActivity()));
|
||||
}
|
||||
Context context = getContext();
|
||||
if (context != null) {
|
||||
new AlertDialog.Builder(context)
|
||||
.setMessage(message)
|
||||
.setCancelable(false)
|
||||
.setNegativeButton(android.R.string.cancel, ((dialog, which) -> {}))
|
||||
.setPositiveButton(R.string.menu_send, (dialog, which) -> {
|
||||
SendRelayedMessageUtil.immediatelyRelay(getActivity(), selectedChats.toArray(new Long[selectedChats.size()]));
|
||||
actionMode.finish();
|
||||
actionMode = null;
|
||||
getActivity().finish();
|
||||
})
|
||||
.show();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
acquireRelayMessageContent(getActivity(), intent);
|
||||
fab.setOnClickListener(v -> requireActivity().startActivity(intent));
|
||||
}
|
||||
} else {
|
||||
fab.setOnClickListener(v -> startActivity(intent));
|
||||
}
|
||||
}
|
||||
|
||||
private boolean areSomeSelectedChatsUnpinned() {
|
||||
DcContext dcContext = DcHelper.getContext(requireActivity());
|
||||
final Set<Long> selectedChats = getListAdapter().getBatchSelections();
|
||||
for (long chatId : selectedChats) {
|
||||
DcChat dcChat = dcContext.getChat((int)chatId);
|
||||
if (dcChat.getVisibility()!=DcChat.DC_CHAT_VISIBILITY_PINNED) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean areSomeSelectedChatsUnmuted() {
|
||||
DcContext dcContext = DcHelper.getContext(requireActivity());
|
||||
final Set<Long> selectedChats = getListAdapter().getBatchSelections();
|
||||
for (long chatId : selectedChats) {
|
||||
DcChat dcChat = dcContext.getChat((int)chatId);
|
||||
if (!dcChat.isMuted()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private void handlePinAllSelected() {
|
||||
final DcContext dcContext = DcHelper.getContext(requireActivity());
|
||||
final Set<Long> selectedConversations = new HashSet<Long>(getListAdapter().getBatchSelections());
|
||||
boolean doPin = areSomeSelectedChatsUnpinned();
|
||||
for (long chatId : selectedConversations) {
|
||||
dcContext.setChatVisibility((int)chatId,
|
||||
doPin? DcChat.DC_CHAT_VISIBILITY_PINNED : DcChat.DC_CHAT_VISIBILITY_NORMAL);
|
||||
}
|
||||
if (actionMode != null) {
|
||||
actionMode.finish();
|
||||
actionMode = null;
|
||||
}
|
||||
}
|
||||
|
||||
private void handleMuteAllSelected() {
|
||||
final DcContext dcContext = DcHelper.getContext(requireActivity());
|
||||
final Set<Long> selectedConversations = new HashSet<Long>(getListAdapter().getBatchSelections());
|
||||
if (areSomeSelectedChatsUnmuted()) {
|
||||
MuteDialog.show(getActivity(), duration -> {
|
||||
for (long chatId : selectedConversations) {
|
||||
dcContext.setChatMuteDuration((int)chatId, duration);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
// unmute
|
||||
for (long chatId : selectedConversations) {
|
||||
dcContext.setChatMuteDuration((int)chatId, 0);
|
||||
}
|
||||
}
|
||||
|
||||
if (actionMode != null) {
|
||||
actionMode.finish();
|
||||
actionMode = null;
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressLint("StaticFieldLeak")
|
||||
private void handleArchiveAllSelected() {
|
||||
final DcContext dcContext = DcHelper.getContext(requireActivity());
|
||||
final Set<Long> selectedConversations = new HashSet<Long>(getListAdapter().getBatchSelections());
|
||||
final boolean archive = offerToArchive();
|
||||
|
||||
int snackBarTitleId;
|
||||
|
||||
if (archive) snackBarTitleId = R.plurals.chat_archived;
|
||||
else snackBarTitleId = R.plurals.chat_unarchived;
|
||||
|
||||
int count = selectedConversations.size();
|
||||
String snackBarTitle = getResources().getQuantityString(snackBarTitleId, count, count);
|
||||
|
||||
new SnackbarAsyncTask<Void>(getView(), snackBarTitle,
|
||||
getString(R.string.undo),
|
||||
Snackbar.LENGTH_LONG, true)
|
||||
{
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(Void result) {
|
||||
super.onPostExecute(result);
|
||||
|
||||
if (actionMode != null) {
|
||||
actionMode.finish();
|
||||
actionMode = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void executeAction(@Nullable Void parameter) {
|
||||
for (long chatId : selectedConversations) {
|
||||
dcContext.setChatVisibility((int)chatId,
|
||||
archive? DcChat.DC_CHAT_VISIBILITY_ARCHIVED : DcChat.DC_CHAT_VISIBILITY_NORMAL);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void reverseAction(@Nullable Void parameter) {
|
||||
for (long threadId : selectedConversations) {
|
||||
dcContext.setChatVisibility((int)threadId,
|
||||
archive? DcChat.DC_CHAT_VISIBILITY_NORMAL : DcChat.DC_CHAT_VISIBILITY_ARCHIVED);
|
||||
}
|
||||
}
|
||||
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||
}
|
||||
|
||||
@SuppressLint("StaticFieldLeak")
|
||||
private void handleDeleteAllSelected() {
|
||||
final DcContext dcContext = DcHelper.getContext(requireActivity());
|
||||
int conversationsCount = getListAdapter().getBatchSelections().size();
|
||||
AlertDialog.Builder alert = new AlertDialog.Builder(requireActivity());
|
||||
alert.setMessage(requireActivity().getResources().getQuantityString(R.plurals.ask_delete_chat,
|
||||
conversationsCount, conversationsCount));
|
||||
alert.setCancelable(true);
|
||||
|
||||
alert.setPositiveButton(R.string.delete, (dialog, which) -> {
|
||||
final Set<Long> selectedConversations = getListAdapter().getBatchSelections();
|
||||
|
||||
if (!selectedConversations.isEmpty()) {
|
||||
new AsyncTask<Void, Void, Void>() {
|
||||
private ProgressDialog dialog;
|
||||
|
||||
@Override
|
||||
protected void onPreExecute() {
|
||||
dialog = ProgressDialog.show(getActivity(),
|
||||
"",
|
||||
requireActivity().getString(R.string.one_moment),
|
||||
true, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Void doInBackground(Void... params) {
|
||||
int accountId = dcContext.getAccountId();
|
||||
for (long chatId : selectedConversations) {
|
||||
DcHelper.getNotificationCenter(requireContext()).removeNotifications(accountId, (int) chatId);
|
||||
dcContext.deleteChat((int) chatId);
|
||||
DirectShareUtil.clearShortcut(requireContext(), (int) chatId);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(Void result) {
|
||||
dialog.dismiss();
|
||||
if (actionMode != null) {
|
||||
actionMode.finish();
|
||||
actionMode = null;
|
||||
}
|
||||
}
|
||||
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||
}
|
||||
});
|
||||
|
||||
alert.setNegativeButton(android.R.string.cancel, null);
|
||||
alert.show();
|
||||
}
|
||||
|
||||
private void handleSelectAllThreads() {
|
||||
getListAdapter().selectAllThreads();
|
||||
actionMode.setTitle(String.valueOf(getListAdapter().getBatchSelections().size()));
|
||||
updateActionModeItems(actionMode.getMenu());
|
||||
}
|
||||
|
||||
private void updateActionModeItems(Menu menu) {
|
||||
// We do not show action mode icons when relaying (= sharing or forwarding).
|
||||
if (!isRelayingMessageContent(requireActivity())) {
|
||||
MenuItem archiveItem = menu.findItem(R.id.menu_archive_selected);
|
||||
if (offerToArchive()) {
|
||||
archiveItem.setIcon(R.drawable.ic_archive_white_24dp);
|
||||
archiveItem.setTitle(R.string.menu_archive_chat);
|
||||
} else {
|
||||
archiveItem.setIcon(R.drawable.ic_unarchive_white_24dp);
|
||||
archiveItem.setTitle(R.string.menu_unarchive_chat);
|
||||
}
|
||||
MenuItem pinItem = menu.findItem(R.id.menu_pin_selected);
|
||||
if (areSomeSelectedChatsUnpinned()) {
|
||||
pinItem.setIcon(R.drawable.ic_pin_white);
|
||||
pinItem.setTitle(R.string.pin_chat);
|
||||
} else {
|
||||
pinItem.setIcon(R.drawable.ic_unpin_white);
|
||||
pinItem.setTitle(R.string.unpin_chat);
|
||||
}
|
||||
MenuItem muteItem = menu.findItem(R.id.menu_mute_selected);
|
||||
if (areSomeSelectedChatsUnmuted()) {
|
||||
muteItem.setTitle(R.string.menu_mute);
|
||||
} else {
|
||||
muteItem.setTitle(R.string.menu_unmute);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
|
||||
if (isRelayingMessageContent(getActivity())) {
|
||||
Context context = getContext();
|
||||
if (context != null) {
|
||||
fab.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_send_sms_white_24dp));
|
||||
}
|
||||
setFabVisibility(true);
|
||||
initializeFabClickListener(true);
|
||||
} else {
|
||||
|
||||
MenuInflater inflater = requireActivity().getMenuInflater();
|
||||
inflater.inflate(R.menu.conversation_list, menu);
|
||||
}
|
||||
|
||||
mode.setTitle("1");
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
||||
requireActivity().getWindow().setStatusBarColor(getResources().getColor(R.color.action_mode_status_bar));
|
||||
}
|
||||
|
||||
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;
|
||||
case R.id.menu_pin_selected: handlePinAllSelected(); return true;
|
||||
case R.id.menu_archive_selected: handleArchiveAllSelected(); return true;
|
||||
case R.id.menu_mute_selected: handleMuteAllSelected(); return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroyActionMode(ActionMode mode) {
|
||||
actionMode = null;
|
||||
getListAdapter().initializeBatchMode(false);
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
||||
TypedArray color = requireActivity().getTheme().obtainStyledAttributes(new int[] {android.R.attr.statusBarColor});
|
||||
requireActivity().getWindow().setStatusBarColor(color.getColor(0, Color.BLACK));
|
||||
color.recycle();
|
||||
}
|
||||
|
||||
Context context = getContext();
|
||||
if (context != null) {
|
||||
fab.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_add_white_24dp));
|
||||
}
|
||||
setFabVisibility(false);
|
||||
initializeFabClickListener(false);
|
||||
}
|
||||
|
||||
public interface ConversationSelectedListener {
|
||||
void onCreateConversation(int chatId);
|
||||
void onSwitchToArchive();
|
||||
}
|
||||
}
|
||||
@@ -35,17 +35,14 @@ import org.thoughtcrime.securesms.mms.GlideRequests;
|
||||
import org.thoughtcrime.securesms.util.ViewUtil;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Locale;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* A CursorAdapter for building a list of conversation threads.
|
||||
*
|
||||
* @author Moxie Marlinspike
|
||||
*/
|
||||
class ConversationListAdapter extends RecyclerView.Adapter {
|
||||
class ConversationListAdapter extends BaseConversationListAdapter<ConversationListAdapter.ViewHolder> {
|
||||
|
||||
private static final int MESSAGE_TYPE_SWITCH_ARCHIVE = 1;
|
||||
private static final int MESSAGE_TYPE_THREAD = 2;
|
||||
@@ -59,9 +56,6 @@ class ConversationListAdapter extends RecyclerView.Adapter {
|
||||
private final @NonNull LayoutInflater inflater;
|
||||
private final @Nullable ItemClickListener clickListener;
|
||||
|
||||
private final Set<Long> batchSet = Collections.synchronizedSet(new HashSet<Long>());
|
||||
private boolean batchMode = false;
|
||||
|
||||
protected static class ViewHolder extends RecyclerView.ViewHolder {
|
||||
public <V extends View & BindableConversationListItem> ViewHolder(final @NonNull V itemView)
|
||||
{
|
||||
@@ -99,8 +93,9 @@ class ConversationListAdapter extends RecyclerView.Adapter {
|
||||
setHasStableIds(true);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
||||
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
||||
if (viewType == MESSAGE_TYPE_SWITCH_ARCHIVE) {
|
||||
final ConversationListItem item = (ConversationListItem)inflater.inflate(R.layout.conversation_list_item_view, parent, false);
|
||||
item.getLayoutParams().height = ViewUtil.dpToPx(54);
|
||||
@@ -131,16 +126,15 @@ class ConversationListAdapter extends RecyclerView.Adapter {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {
|
||||
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
|
||||
Context context = this.context.get();
|
||||
if (context == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
ViewHolder holder = (ViewHolder)viewHolder;
|
||||
DcChat chat = dcContext.getChat(dcChatlist.getChatId(i));
|
||||
DcLot summary = dcChatlist.getSummary(i, chat);
|
||||
holder.getItem().bind(DcHelper.getThreadRecord(context, summary, chat), dcChatlist.getMsgId(i), summary, glideRequests, locale, batchSet, batchMode);
|
||||
viewHolder.getItem().bind(DcHelper.getThreadRecord(context, summary, chat), dcChatlist.getMsgId(i), summary, glideRequests, locale, batchSet, batchMode);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -156,36 +150,15 @@ class ConversationListAdapter extends RecyclerView.Adapter {
|
||||
}
|
||||
}
|
||||
|
||||
void toggleThreadInBatchSet(long threadId) {
|
||||
if (batchSet.contains(threadId)) {
|
||||
batchSet.remove(threadId);
|
||||
} else if (threadId != -1) {
|
||||
batchSet.add(threadId);
|
||||
}
|
||||
}
|
||||
|
||||
Set<Long> getBatchSelections() {
|
||||
return batchSet;
|
||||
}
|
||||
|
||||
void initializeBatchMode(boolean toggle) {
|
||||
this.batchMode = toggle;
|
||||
unselectAllThreads();
|
||||
}
|
||||
|
||||
private void unselectAllThreads() {
|
||||
this.batchSet.clear();
|
||||
this.notifyDataSetChanged();
|
||||
}
|
||||
|
||||
void selectAllThreads() {
|
||||
@Override
|
||||
public void selectAllThreads() {
|
||||
for (int i = 0; i < dcChatlist.getCnt(); i++) {
|
||||
long threadId = dcChatlist.getChatId(i);
|
||||
if (threadId > DcChat.DC_CHAT_ID_LAST_SPECIAL) {
|
||||
batchSet.add(threadId);
|
||||
}
|
||||
}
|
||||
this.notifyDataSetChanged();
|
||||
notifyDataSetChanged();
|
||||
}
|
||||
|
||||
interface ItemClickListener {
|
||||
|
||||
@@ -20,83 +20,52 @@ import android.Manifest;
|
||||
import android.annotation.SuppressLint;
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.res.TypedArray;
|
||||
import android.graphics.Color;
|
||||
import android.net.Uri;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuInflater;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.appcompat.view.ActionMode;
|
||||
import androidx.core.content.ContextCompat;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import com.b44t.messenger.DcChat;
|
||||
import com.b44t.messenger.DcChatlist;
|
||||
import com.b44t.messenger.DcContext;
|
||||
import com.b44t.messenger.DcEvent;
|
||||
import com.b44t.messenger.DcMsg;
|
||||
import com.google.android.material.snackbar.Snackbar;
|
||||
|
||||
import org.thoughtcrime.securesms.ConversationListAdapter.ItemClickListener;
|
||||
import org.thoughtcrime.securesms.components.recyclerview.DeleteItemAnimator;
|
||||
import org.thoughtcrime.securesms.components.registration.PulsingFloatingActionButton;
|
||||
import org.thoughtcrime.securesms.components.reminder.DozeReminder;
|
||||
import org.thoughtcrime.securesms.connect.DcEventCenter;
|
||||
import org.thoughtcrime.securesms.connect.DcHelper;
|
||||
import org.thoughtcrime.securesms.connect.DirectShareUtil;
|
||||
import org.thoughtcrime.securesms.mms.GlideApp;
|
||||
import org.thoughtcrime.securesms.permissions.Permissions;
|
||||
import org.thoughtcrime.securesms.util.Prefs;
|
||||
import org.thoughtcrime.securesms.util.RelayUtil;
|
||||
import org.thoughtcrime.securesms.util.SendRelayedMessageUtil;
|
||||
import org.thoughtcrime.securesms.util.Util;
|
||||
import org.thoughtcrime.securesms.util.ViewUtil;
|
||||
import org.thoughtcrime.securesms.util.task.SnackbarAsyncTask;
|
||||
import org.thoughtcrime.securesms.util.views.ProgressDialog;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.Locale;
|
||||
import java.util.Set;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
|
||||
import static org.thoughtcrime.securesms.util.RelayUtil.acquireRelayMessageContent;
|
||||
import static org.thoughtcrime.securesms.util.RelayUtil.getSharedText;
|
||||
import static org.thoughtcrime.securesms.util.RelayUtil.getSharedUris;
|
||||
import static org.thoughtcrime.securesms.util.RelayUtil.isForwarding;
|
||||
import static org.thoughtcrime.securesms.util.RelayUtil.isRelayingMessageContent;
|
||||
|
||||
|
||||
public class ConversationListFragment extends Fragment
|
||||
implements ActionMode.Callback, ItemClickListener, DcEventCenter.DcEventDelegate {
|
||||
public class ConversationListFragment extends BaseConversationListFragment
|
||||
implements ItemClickListener, DcEventCenter.DcEventDelegate {
|
||||
public static final String ARCHIVE = "archive";
|
||||
public static final String RELOAD_LIST = "reload_list";
|
||||
|
||||
private static final String TAG = ConversationListFragment.class.getSimpleName();
|
||||
|
||||
private ActionMode actionMode;
|
||||
private RecyclerView list;
|
||||
private View emptyState;
|
||||
private TextView emptySearch;
|
||||
private PulsingFloatingActionButton fab;
|
||||
private Locale locale;
|
||||
private final String queryFilter = "";
|
||||
private boolean archive;
|
||||
@@ -110,7 +79,7 @@ public class ConversationListFragment extends Fragment
|
||||
locale = (Locale) getArguments().getSerializable(PassphraseRequiredActionBarActivity.LOCALE_EXTRA);
|
||||
archive = getArguments().getBoolean(ARCHIVE, false);
|
||||
|
||||
DcEventCenter eventCenter = DcHelper.getEventCenter(getActivity());
|
||||
DcEventCenter eventCenter = DcHelper.getEventCenter(requireActivity());
|
||||
eventCenter.addMultiAccountObserver(DcContext.DC_EVENT_INCOMING_MSG, this);
|
||||
eventCenter.addMultiAccountObserver(DcContext.DC_EVENT_MSGS_NOTICED, this);
|
||||
eventCenter.addObserver(DcContext.DC_EVENT_CHAT_MODIFIED, this);
|
||||
@@ -127,7 +96,7 @@ public class ConversationListFragment extends Fragment
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
DcHelper.getEventCenter(getActivity()).removeObservers(this);
|
||||
DcHelper.getEventCenter(requireActivity()).removeObservers(this);
|
||||
}
|
||||
|
||||
@SuppressLint("RestrictedApi")
|
||||
@@ -163,7 +132,7 @@ public class ConversationListFragment extends Fragment
|
||||
|
||||
setHasOptionsMenu(true);
|
||||
initializeFabClickListener(false);
|
||||
list.setAdapter(new ConversationListAdapter(getActivity(), GlideApp.with(this), locale, this));
|
||||
list.setAdapter(new ConversationListAdapter(requireActivity(), GlideApp.with(this), locale, this));
|
||||
loadChatlistAsync();
|
||||
chatlistJustLoaded = true;
|
||||
}
|
||||
@@ -174,7 +143,7 @@ public class ConversationListFragment extends Fragment
|
||||
|
||||
updateReminders();
|
||||
|
||||
if (getActivity().getIntent().getIntExtra(RELOAD_LIST, 0) == 1
|
||||
if (requireActivity().getIntent().getIntExtra(RELOAD_LIST, 0) == 1
|
||||
&& !chatlistJustLoaded) {
|
||||
Log.i(TAG, "🤠 resuming chatlist: loading chatlist");
|
||||
loadChatlist();
|
||||
@@ -210,53 +179,15 @@ public class ConversationListFragment extends Fragment
|
||||
initializeFabClickListener(actionMode != null);
|
||||
}
|
||||
|
||||
public ConversationListAdapter getListAdapter() {
|
||||
return (ConversationListAdapter) list.getAdapter();
|
||||
}
|
||||
|
||||
private void initializeFabClickListener(boolean isActionMode) {
|
||||
Intent intent = new Intent(getActivity(), NewConversationActivity.class);
|
||||
if (isRelayingMessageContent(getActivity())) {
|
||||
if (isActionMode) {
|
||||
fab.setOnClickListener(v -> {
|
||||
final Set<Long> selectedChats = getListAdapter().getBatchSelections();
|
||||
ArrayList<Uri> uris = getSharedUris(getActivity());
|
||||
String message;
|
||||
if (isForwarding(getActivity())) {
|
||||
message = String.format(Locale.getDefault(), getString(R.string.ask_forward_multiple), selectedChats.size());
|
||||
} else if (uris.size() > 0) {
|
||||
message = String.format(Locale.getDefault(), getString(R.string.share_multiple_attachments_multiple_chats), uris.size(), selectedChats.size());
|
||||
} else {
|
||||
message = String.format(Locale.getDefault(), getString(R.string.share_text_multiple_chats), selectedChats.size(), getSharedText(getActivity()));
|
||||
}
|
||||
Context context = getContext();
|
||||
if (context != null) {
|
||||
new AlertDialog.Builder(context)
|
||||
.setMessage(message)
|
||||
.setCancelable(false)
|
||||
.setNegativeButton(android.R.string.cancel, ((dialog, which) -> {}))
|
||||
.setPositiveButton(R.string.menu_send, (dialog, which) -> {
|
||||
SendRelayedMessageUtil.immediatelyRelay(getActivity(), selectedChats.toArray(new Long[selectedChats.size()]));
|
||||
actionMode.finish();
|
||||
actionMode = null;
|
||||
getActivity().finish();
|
||||
})
|
||||
.show();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
acquireRelayMessageContent(getActivity(), intent);
|
||||
fab.setOnClickListener(v -> getActivity().startActivity(intent));
|
||||
}
|
||||
} else {
|
||||
fab.setOnClickListener(v -> startActivity(intent));
|
||||
}
|
||||
@Override
|
||||
public BaseConversationListAdapter getListAdapter() {
|
||||
return (BaseConversationListAdapter) list.getAdapter();
|
||||
}
|
||||
|
||||
@SuppressLint({"StaticFieldLeak", "NewApi"})
|
||||
private void updateReminders() {
|
||||
// by the time onPostExecute() is asynchronously run, getActivity() might return null, so get the activity here:
|
||||
Activity activity = getActivity();
|
||||
Activity activity = requireActivity();
|
||||
new AsyncTask<Context, Void, Void>() {
|
||||
@Override
|
||||
protected Void doInBackground(Context... params) {
|
||||
@@ -298,150 +229,6 @@ public class ConversationListFragment extends Fragment
|
||||
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, activity);
|
||||
}
|
||||
|
||||
private void handlePinAllSelected() {
|
||||
final DcContext dcContext = DcHelper.getContext(getActivity());
|
||||
final Set<Long> selectedConversations = new HashSet<>(getListAdapter().getBatchSelections());
|
||||
boolean doPin = areSomeSelectedChatsUnpinned();
|
||||
for (long chatId : selectedConversations) {
|
||||
dcContext.setChatVisibility((int)chatId,
|
||||
doPin? DcChat.DC_CHAT_VISIBILITY_PINNED : DcChat.DC_CHAT_VISIBILITY_NORMAL);
|
||||
}
|
||||
if (actionMode != null) {
|
||||
actionMode.finish();
|
||||
actionMode = null;
|
||||
}
|
||||
}
|
||||
|
||||
private void handleMuteAllSelected() {
|
||||
final DcContext dcContext = DcHelper.getContext(getActivity());
|
||||
final Set<Long> selectedConversations = new HashSet<>(getListAdapter().getBatchSelections());
|
||||
if (areSomeSelectedChatsUnmuted()) {
|
||||
MuteDialog.show(getActivity(), duration -> {
|
||||
for (long chatId : selectedConversations) {
|
||||
dcContext.setChatMuteDuration((int)chatId, duration);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
// unmute
|
||||
for (long chatId : selectedConversations) {
|
||||
dcContext.setChatMuteDuration((int)chatId, 0);
|
||||
}
|
||||
}
|
||||
|
||||
if (actionMode != null) {
|
||||
actionMode.finish();
|
||||
actionMode = null;
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressLint("StaticFieldLeak")
|
||||
private void handleArchiveAllSelected() {
|
||||
final DcContext dcContext = DcHelper.getContext(getActivity());
|
||||
final Set<Long> selectedConversations = new HashSet<>(getListAdapter().getBatchSelections());
|
||||
final boolean archive = this.archive;
|
||||
|
||||
int snackBarTitleId;
|
||||
|
||||
if (archive) snackBarTitleId = R.plurals.chat_unarchived;
|
||||
else snackBarTitleId = R.plurals.chat_archived;
|
||||
|
||||
int count = selectedConversations.size();
|
||||
String snackBarTitle = getResources().getQuantityString(snackBarTitleId, count, count);
|
||||
|
||||
new SnackbarAsyncTask<Void>(getView(), snackBarTitle,
|
||||
getString(R.string.undo),
|
||||
Snackbar.LENGTH_LONG, true)
|
||||
{
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(Void result) {
|
||||
super.onPostExecute(result);
|
||||
|
||||
if (actionMode != null) {
|
||||
actionMode.finish();
|
||||
actionMode = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void executeAction(@Nullable Void parameter) {
|
||||
for (long chatId : selectedConversations) {
|
||||
dcContext.setChatVisibility((int)chatId,
|
||||
!archive? DcChat.DC_CHAT_VISIBILITY_ARCHIVED : DcChat.DC_CHAT_VISIBILITY_NORMAL);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void reverseAction(@Nullable Void parameter) {
|
||||
for (long threadId : selectedConversations) {
|
||||
dcContext.setChatVisibility((int)threadId,
|
||||
!archive? DcChat.DC_CHAT_VISIBILITY_NORMAL : DcChat.DC_CHAT_VISIBILITY_ARCHIVED);
|
||||
}
|
||||
}
|
||||
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||
}
|
||||
|
||||
@SuppressLint("StaticFieldLeak")
|
||||
private void handleDeleteAllSelected() {
|
||||
final DcContext dcContext = DcHelper.getContext(getActivity());
|
||||
int conversationsCount = getListAdapter().getBatchSelections().size();
|
||||
AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
|
||||
alert.setMessage(getActivity().getResources().getQuantityString(R.plurals.ask_delete_chat,
|
||||
conversationsCount, conversationsCount));
|
||||
alert.setCancelable(true);
|
||||
|
||||
alert.setPositiveButton(R.string.delete, (dialog, which) -> {
|
||||
final Set<Long> selectedConversations = (getListAdapter())
|
||||
.getBatchSelections();
|
||||
|
||||
if (!selectedConversations.isEmpty()) {
|
||||
new AsyncTask<Void, Void, Void>() {
|
||||
private ProgressDialog dialog;
|
||||
|
||||
@Override
|
||||
protected void onPreExecute() {
|
||||
dialog = ProgressDialog.show(getActivity(),
|
||||
"",
|
||||
getActivity().getString(R.string.one_moment),
|
||||
true, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Void doInBackground(Void... params) {
|
||||
int accountId = dcContext.getAccountId();
|
||||
for (long chatId : selectedConversations) {
|
||||
DcHelper.getNotificationCenter(getContext()).removeNotifications(accountId, (int) chatId);
|
||||
dcContext.deleteChat((int) chatId);
|
||||
DirectShareUtil.clearShortcut(getContext(), (int) chatId);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(Void result) {
|
||||
dialog.dismiss();
|
||||
if (actionMode != null) {
|
||||
actionMode.finish();
|
||||
actionMode = null;
|
||||
}
|
||||
}
|
||||
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||
}
|
||||
});
|
||||
|
||||
alert.setNegativeButton(android.R.string.cancel, null);
|
||||
alert.show();
|
||||
}
|
||||
|
||||
private void handleSelectAllThreads() {
|
||||
getListAdapter().selectAllThreads();
|
||||
actionMode.setTitle(String.valueOf(getListAdapter().getBatchSelections().size()));
|
||||
}
|
||||
|
||||
private void handleCreateConversation(int chatId) {
|
||||
((ConversationSelectedListener)getActivity()).onCreateConversation(chatId);
|
||||
}
|
||||
|
||||
private final Object loadChatlistLock = new Object();
|
||||
private boolean inLoadChatlist;
|
||||
private boolean needsAnotherLoad;
|
||||
@@ -508,171 +295,38 @@ public class ConversationListFragment extends Fragment
|
||||
fab.stopPulse();
|
||||
}
|
||||
|
||||
getListAdapter().changeData(chatlist);
|
||||
((ConversationListAdapter)list.getAdapter()).changeData(chatlist);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean offerToArchive() {
|
||||
return !archive;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setFabVisibility(boolean isActionMode) {
|
||||
fab.setVisibility((isActionMode || !archive)? View.VISIBLE : View.GONE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onItemClick(ConversationListItem item) {
|
||||
if (actionMode == null) {
|
||||
int chatId = (int)item.getChatId();
|
||||
handleCreateConversation(chatId);
|
||||
} else {
|
||||
ConversationListAdapter adapter = (ConversationListAdapter) list.getAdapter();
|
||||
adapter.toggleThreadInBatchSet(item.getChatId());
|
||||
|
||||
if (adapter.getBatchSelections().size() == 0) {
|
||||
actionMode.finish();
|
||||
} else {
|
||||
updateActionModeItems(actionMode.getMenu());
|
||||
actionMode.setTitle(String.valueOf(getListAdapter().getBatchSelections().size()));
|
||||
}
|
||||
|
||||
adapter.notifyDataSetChanged();
|
||||
}
|
||||
onItemClick(item.getChatId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onItemLongClick(ConversationListItem item) {
|
||||
actionMode = ((AppCompatActivity)getActivity()).startSupportActionMode(ConversationListFragment.this);
|
||||
|
||||
if (actionMode != null) {
|
||||
getListAdapter().initializeBatchMode(true);
|
||||
getListAdapter().toggleThreadInBatchSet(item.getChatId());
|
||||
getListAdapter().notifyDataSetChanged();
|
||||
Menu menu = actionMode.getMenu();
|
||||
if (menu != null) {
|
||||
updateActionModeItems(menu);
|
||||
}
|
||||
}
|
||||
onItemLongClick(item.getChatId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSwitchToArchive() {
|
||||
((ConversationSelectedListener)getActivity()).onSwitchToArchive();
|
||||
}
|
||||
|
||||
public interface ConversationSelectedListener {
|
||||
void onCreateConversation(int chatId);
|
||||
void onSwitchToArchive();
|
||||
}
|
||||
|
||||
private boolean areSomeSelectedChatsUnpinned() {
|
||||
DcContext dcContext = DcHelper.getContext(getActivity());
|
||||
final Set<Long> selectedChats = getListAdapter().getBatchSelections();
|
||||
for (long chatId : selectedChats) {
|
||||
DcChat dcChat = dcContext.getChat((int)chatId);
|
||||
if (dcChat.getVisibility()!=DcChat.DC_CHAT_VISIBILITY_PINNED) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean areSomeSelectedChatsUnmuted() {
|
||||
DcContext dcContext = DcHelper.getContext(getActivity());
|
||||
final Set<Long> selectedChats = getListAdapter().getBatchSelections();
|
||||
for (long chatId : selectedChats) {
|
||||
DcChat dcChat = dcContext.getChat((int)chatId);
|
||||
if (!dcChat.isMuted()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private void updateActionModeItems(Menu menu) {
|
||||
// We do not show action mode icons when relaying (= sharing or forwarding).
|
||||
if (!isRelayingMessageContent(getActivity())) {
|
||||
MenuItem pinItem = menu.findItem(R.id.menu_pin_selected);
|
||||
if (areSomeSelectedChatsUnpinned()) {
|
||||
pinItem.setIcon(R.drawable.ic_pin_white);
|
||||
pinItem.setTitle(R.string.pin_chat);
|
||||
} else {
|
||||
pinItem.setIcon(R.drawable.ic_unpin_white);
|
||||
pinItem.setTitle(R.string.unpin_chat);
|
||||
}
|
||||
MenuItem muteItem = menu.findItem(R.id.menu_mute_selected);
|
||||
if (areSomeSelectedChatsUnmuted()) {
|
||||
muteItem.setTitle(R.string.menu_mute);
|
||||
} else {
|
||||
muteItem.setTitle(R.string.menu_unmute);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
|
||||
if (isRelayingMessageContent(getActivity())) {
|
||||
Context context = getContext();
|
||||
if (context != null) {
|
||||
fab.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_send_sms_white_24dp));
|
||||
}
|
||||
fab.setVisibility(View.VISIBLE);
|
||||
initializeFabClickListener(true);
|
||||
} else {
|
||||
|
||||
MenuInflater inflater = getActivity().getMenuInflater();
|
||||
|
||||
inflater.inflate(R.menu.conversation_list_batch_pin, menu);
|
||||
|
||||
if (archive) inflater.inflate(R.menu.conversation_list_batch_unarchive, menu);
|
||||
else inflater.inflate(R.menu.conversation_list_batch_archive, menu);
|
||||
|
||||
inflater.inflate(R.menu.conversation_list, menu);
|
||||
}
|
||||
|
||||
mode.setTitle("1");
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
||||
getActivity().getWindow().setStatusBarColor(getResources().getColor(R.color.action_mode_status_bar));
|
||||
}
|
||||
|
||||
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;
|
||||
case R.id.menu_pin_selected: handlePinAllSelected(); return true;
|
||||
case R.id.menu_archive_selected: handleArchiveAllSelected(); return true;
|
||||
case R.id.menu_mute_selected: handleMuteAllSelected(); return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroyActionMode(ActionMode mode) {
|
||||
getListAdapter().initializeBatchMode(false);
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
Context context = getContext();
|
||||
if (context != null) {
|
||||
fab.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_add_white_24dp));
|
||||
}
|
||||
if (archive) fab.setVisibility(View.GONE);
|
||||
else fab.setVisibility(View.VISIBLE);
|
||||
initializeFabClickListener(false);
|
||||
|
||||
actionMode = null;
|
||||
((ConversationSelectedListener)requireActivity()).onSwitchToArchive();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleEvent(@NonNull DcEvent event) {
|
||||
if (event.getAccountId() != DcHelper.getContext(getActivity()).getAccountId()) {
|
||||
if (event.getAccountId() != DcHelper.getContext(requireActivity()).getAccountId()) {
|
||||
Activity activity = getActivity();
|
||||
if (activity instanceof ConversationListActivity) {
|
||||
((ConversationListActivity) activity).refreshUnreadIndicator();
|
||||
|
||||
@@ -1,28 +1,33 @@
|
||||
package org.thoughtcrime.securesms.search;
|
||||
|
||||
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
import androidx.lifecycle.ViewModelProviders;
|
||||
import static org.thoughtcrime.securesms.util.RelayUtil.isRelayingMessageContent;
|
||||
|
||||
import android.content.res.Configuration;
|
||||
import android.os.Bundle;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
import android.text.TextUtils;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
import androidx.lifecycle.ViewModelProviders;
|
||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import com.b44t.messenger.DcChat;
|
||||
import com.b44t.messenger.DcChatlist;
|
||||
import com.b44t.messenger.DcContact;
|
||||
import com.b44t.messenger.DcContext;
|
||||
import com.b44t.messenger.DcEvent;
|
||||
import com.b44t.messenger.DcMsg;
|
||||
|
||||
import org.thoughtcrime.securesms.BaseConversationListAdapter;
|
||||
import org.thoughtcrime.securesms.BaseConversationListFragment;
|
||||
import org.thoughtcrime.securesms.ConversationListActivity;
|
||||
import org.thoughtcrime.securesms.R;
|
||||
import org.thoughtcrime.securesms.connect.DcEventCenter;
|
||||
@@ -32,13 +37,13 @@ import org.thoughtcrime.securesms.search.model.SearchResult;
|
||||
import org.thoughtcrime.securesms.util.StickyHeaderDecoration;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
import static org.thoughtcrime.securesms.util.RelayUtil.isRelayingMessageContent;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* A fragment that is displayed to do full-text search of messages, groups, and contacts.
|
||||
*/
|
||||
public class SearchFragment extends Fragment implements SearchListAdapter.EventListener, DcEventCenter.DcEventDelegate {
|
||||
public class SearchFragment extends BaseConversationListFragment
|
||||
implements SearchListAdapter.EventListener, DcEventCenter.DcEventDelegate {
|
||||
|
||||
public static final String TAG = "SearchFragment";
|
||||
public static final String EXTRA_LOCALE = "locale";
|
||||
@@ -68,8 +73,8 @@ public class SearchFragment extends Fragment implements SearchListAdapter.EventL
|
||||
|
||||
this.locale = (Locale) getArguments().getSerializable(EXTRA_LOCALE);
|
||||
|
||||
viewModel = ViewModelProviders.of(this, (ViewModelProvider.Factory) new SearchViewModel.Factory(getContext())).get(SearchViewModel.class);
|
||||
DcEventCenter eventCenter = DcHelper.getEventCenter(getContext());
|
||||
viewModel = ViewModelProviders.of(this, (ViewModelProvider.Factory) new SearchViewModel.Factory(requireContext())).get(SearchViewModel.class);
|
||||
DcEventCenter eventCenter = DcHelper.getEventCenter(requireContext());
|
||||
eventCenter.addObserver(DcContext.DC_EVENT_CHAT_MODIFIED, this);
|
||||
eventCenter.addObserver(DcContext.DC_EVENT_CONTACTS_CHANGED, this);
|
||||
eventCenter.addObserver(DcContext.DC_EVENT_INCOMING_MSG, this);
|
||||
@@ -95,10 +100,12 @@ public class SearchFragment extends Fragment implements SearchListAdapter.EventL
|
||||
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
||||
noResultsView = view.findViewById(R.id.search_no_results);
|
||||
listView = view.findViewById(R.id.search_list);
|
||||
fab = view.findViewById(R.id.fab);
|
||||
|
||||
listAdapter = new SearchListAdapter(getContext(), GlideApp.with(this), this, locale);
|
||||
listDecoration = new StickyHeaderDecoration(listAdapter, false, true);
|
||||
|
||||
fab.setVisibility(View.GONE);
|
||||
listView.setAdapter(listAdapter);
|
||||
listView.addItemDecoration(listDecoration);
|
||||
listView.setLayoutManager(new LinearLayoutManager(getContext()));
|
||||
@@ -107,7 +114,7 @@ public class SearchFragment extends Fragment implements SearchListAdapter.EventL
|
||||
@Override
|
||||
public void onStart() {
|
||||
super.onStart();
|
||||
viewModel.includeMessageQueries(!isRelayingMessageContent(getActivity()));
|
||||
viewModel.setForwardingMode(isRelayingMessageContent(getActivity()));
|
||||
viewModel.getSearchResult().observe(this, result -> {
|
||||
result = result != null ? result : SearchResult.EMPTY;
|
||||
|
||||
@@ -129,7 +136,7 @@ public class SearchFragment extends Fragment implements SearchListAdapter.EventL
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConfigurationChanged(Configuration newConfig) {
|
||||
public void onConfigurationChanged(@NonNull Configuration newConfig) {
|
||||
super.onConfigurationChanged(newConfig);
|
||||
|
||||
if (listDecoration != null) {
|
||||
@@ -139,26 +146,32 @@ public class SearchFragment extends Fragment implements SearchListAdapter.EventL
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
DcHelper.getEventCenter(getContext()).removeObservers(this);
|
||||
DcHelper.getEventCenter(requireContext()).removeObservers(this);
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConversationClicked(@NonNull DcChatlist.Item chatlistItem) {
|
||||
ConversationListActivity conversationList = (ConversationListActivity) getActivity();
|
||||
if (conversationList != null) {
|
||||
conversationList.onCreateConversation(chatlistItem.chatId);
|
||||
}
|
||||
onItemClick(chatlistItem.chatId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConversationLongClicked(@NonNull DcChatlist.Item chatlistItem) {
|
||||
onItemLongClick(chatlistItem.chatId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onContactClicked(@NonNull DcContact contact) {
|
||||
if (actionMode != null) {
|
||||
return;
|
||||
}
|
||||
|
||||
ConversationListActivity conversationList = (ConversationListActivity) getActivity();
|
||||
if (conversationList != null) {
|
||||
DcContext dcContext = DcHelper.getContext(getContext());
|
||||
DcContext dcContext = DcHelper.getContext(requireContext());
|
||||
int chatId = dcContext.getChatIdByContactId(contact.getId());
|
||||
if(chatId==0) {
|
||||
new AlertDialog.Builder(getContext())
|
||||
new AlertDialog.Builder(requireContext())
|
||||
.setMessage(getString(R.string.ask_start_chat_with, contact.getNameNAddr()))
|
||||
.setCancelable(true)
|
||||
.setNegativeButton(android.R.string.cancel, null)
|
||||
@@ -175,9 +188,13 @@ public class SearchFragment extends Fragment implements SearchListAdapter.EventL
|
||||
|
||||
@Override
|
||||
public void onMessageClicked(@NonNull DcMsg message) {
|
||||
if (actionMode != null) {
|
||||
return;
|
||||
}
|
||||
|
||||
ConversationListActivity conversationList = (ConversationListActivity) getActivity();
|
||||
if (conversationList != null) {
|
||||
DcContext dcContext = DcHelper.getContext(getContext());
|
||||
DcContext dcContext = DcHelper.getContext(requireContext());
|
||||
int chatId = message.getChatId();
|
||||
int startingPosition = DcMsg.getMessagePosition(message, dcContext);
|
||||
conversationList.openConversation(chatId, startingPosition);
|
||||
@@ -198,4 +215,31 @@ public class SearchFragment extends Fragment implements SearchListAdapter.EventL
|
||||
viewModel.updateQuery();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean offerToArchive() {
|
||||
DcContext dcContext = DcHelper.getContext(requireActivity());
|
||||
final Set<Long> selectedChats = listAdapter.getBatchSelections();
|
||||
for (long chatId : selectedChats) {
|
||||
DcChat dcChat = dcContext.getChat((int)chatId);
|
||||
if (dcChat.getVisibility() != DcChat.DC_CHAT_VISIBILITY_ARCHIVED) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setFabVisibility(boolean isActionMode) {
|
||||
if (isActionMode && isRelayingMessageContent(getActivity())) {
|
||||
fab.setVisibility(View.VISIBLE);
|
||||
} else {
|
||||
fab.setVisibility(View.GONE);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected BaseConversationListAdapter getListAdapter() {
|
||||
return listAdapter;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,19 +1,21 @@
|
||||
package org.thoughtcrime.securesms.search;
|
||||
|
||||
import android.content.Context;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import com.b44t.messenger.DcChatlist;
|
||||
import com.b44t.messenger.DcContact;
|
||||
import com.b44t.messenger.DcContext;
|
||||
import com.b44t.messenger.DcMsg;
|
||||
|
||||
import org.thoughtcrime.securesms.BaseConversationListAdapter;
|
||||
import org.thoughtcrime.securesms.ConversationListItem;
|
||||
import org.thoughtcrime.securesms.R;
|
||||
import org.thoughtcrime.securesms.connect.DcHelper;
|
||||
@@ -22,10 +24,10 @@ import org.thoughtcrime.securesms.mms.GlideRequests;
|
||||
import org.thoughtcrime.securesms.search.model.SearchResult;
|
||||
import org.thoughtcrime.securesms.util.StickyHeaderDecoration;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Locale;
|
||||
import java.util.Set;
|
||||
|
||||
class SearchListAdapter extends RecyclerView.Adapter<SearchListAdapter.SearchResultViewHolder>
|
||||
class SearchListAdapter extends BaseConversationListAdapter<SearchListAdapter.SearchResultViewHolder>
|
||||
implements StickyHeaderDecoration.StickyHeaderAdapter<SearchListAdapter.HeaderViewHolder>
|
||||
{
|
||||
private static final int TYPE_CHATS = 1;
|
||||
@@ -66,7 +68,7 @@ class SearchListAdapter extends RecyclerView.Adapter<SearchListAdapter.Search
|
||||
DcChatlist.Item conversationResult = getConversationResult(position);
|
||||
|
||||
if (conversationResult != null) {
|
||||
holder.bind(context, conversationResult, glideRequests, eventListener, locale, searchResult.getQuery());
|
||||
holder.bind(context, conversationResult, glideRequests, eventListener, locale, batchSet, batchMode, searchResult.getQuery());
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -146,6 +148,14 @@ class SearchListAdapter extends RecyclerView.Adapter<SearchListAdapter.Search
|
||||
notifyDataSetChanged();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void selectAllThreads() {
|
||||
for (int i = 0; i < searchResult.getChats().getCnt(); i++) {
|
||||
batchSet.add((long)searchResult.getChats().getItem(i).chatId);
|
||||
}
|
||||
notifyDataSetChanged();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private DcChatlist.Item getConversationResult(int position) {
|
||||
if (position < searchResult.getChats().getCnt()) {
|
||||
@@ -180,6 +190,7 @@ class SearchListAdapter extends RecyclerView.Adapter<SearchListAdapter.Search
|
||||
|
||||
public interface EventListener {
|
||||
void onConversationClicked(@NonNull DcChatlist.Item chatlistItem);
|
||||
void onConversationLongClicked(@NonNull DcChatlist.Item chatlistItem);
|
||||
void onContactClicked(@NonNull DcContact contact);
|
||||
void onMessageClicked(@NonNull DcMsg message);
|
||||
}
|
||||
@@ -198,12 +209,18 @@ class SearchListAdapter extends RecyclerView.Adapter<SearchListAdapter.Search
|
||||
@NonNull GlideRequests glideRequests,
|
||||
@NonNull EventListener eventListener,
|
||||
@NonNull Locale locale,
|
||||
@NonNull Set<Long> selectedThreads,
|
||||
boolean batchMode,
|
||||
@Nullable String query)
|
||||
{
|
||||
DcContext dcContext = DcHelper.getContext(context);
|
||||
ThreadRecord threadRecord = DcHelper.getThreadRecord(context, chatlistItem.summary, dcContext.getChat(chatlistItem.chatId));
|
||||
root.bind(threadRecord, chatlistItem.msgId, chatlistItem.summary, glideRequests, locale, Collections.emptySet(), false, query);
|
||||
root.bind(threadRecord, chatlistItem.msgId, chatlistItem.summary, glideRequests, locale, selectedThreads, batchMode, query);
|
||||
root.setOnClickListener(view -> eventListener.onConversationClicked(chatlistItem));
|
||||
root.setOnLongClickListener(view -> {
|
||||
eventListener.onConversationLongClicked(chatlistItem);
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
void bind(@NonNull DcContact contactResult,
|
||||
|
||||
@@ -22,7 +22,7 @@ class SearchViewModel extends ViewModel {
|
||||
private final ObservingLiveData searchResult;
|
||||
private String lastQuery;
|
||||
private final DcContext dcContext;
|
||||
private boolean queryMessages = true;
|
||||
private boolean forwarding = false;
|
||||
private boolean inBgSearch;
|
||||
private boolean needsAnotherBgSearch;
|
||||
|
||||
@@ -35,8 +35,8 @@ class SearchViewModel extends ViewModel {
|
||||
return searchResult;
|
||||
}
|
||||
|
||||
public void includeMessageQueries(boolean include) {
|
||||
queryMessages = include;
|
||||
public void setForwardingMode(boolean forwarding) {
|
||||
this.forwarding = forwarding;
|
||||
}
|
||||
|
||||
|
||||
@@ -79,7 +79,7 @@ class SearchViewModel extends ViewModel {
|
||||
|
||||
// #1 search for chats
|
||||
long startMs = System.currentTimeMillis();
|
||||
DcChatlist conversations = dcContext.getChatlist(0, query, 0);
|
||||
DcChatlist conversations = dcContext.getChatlist(forwarding? DcContext.DC_GCL_FOR_FORWARDING : 0, query, 0);
|
||||
overallCnt += conversations.getCnt();
|
||||
Log.i(TAG, "⏰ getChatlist(" + query + "): " + (System.currentTimeMillis() - startMs) + "ms");
|
||||
|
||||
@@ -96,7 +96,7 @@ class SearchViewModel extends ViewModel {
|
||||
Log.i(TAG, "⏰ getContacts(" + query + "): " + (System.currentTimeMillis() - startMs) + "ms");
|
||||
|
||||
// #3 search for messages
|
||||
if (!queryMessages) {
|
||||
if (forwarding) {
|
||||
Log.i(TAG, "... searchMsgs() disabled by caller");
|
||||
callback.onResult(new SearchResult(query, contacts, conversations, new int[0]));
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user