Files
FairEmail/app/src/main/java/eu/faircode/email/FragmentMessage.java

855 lines
35 KiB
Java
Raw Normal View History

2018-08-02 13:33:06 +00:00
package eu.faircode.email;
/*
2018-08-14 05:53:24 +00:00
This file is part of FairEmail.
2018-08-02 13:33:06 +00:00
2018-08-14 05:53:24 +00:00
FairEmail is free software: you can redistribute it and/or modify
2018-08-02 13:33:06 +00:00
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.
NetGuard 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 NetGuard. If not, see <http://www.gnu.org/licenses/>.
Copyright 2018 by Marcel Bokhorst (M66B)
*/
2018-08-04 18:52:09 +00:00
import android.content.Context;
2018-08-02 13:33:06 +00:00
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Typeface;
2018-08-08 05:02:25 +00:00
import android.net.Uri;
2018-08-02 13:33:06 +00:00
import android.os.Bundle;
2018-08-08 12:28:21 +00:00
import android.preference.PreferenceManager;
2018-08-02 13:33:06 +00:00
import android.text.Html;
import android.text.Layout;
import android.text.Spannable;
2018-08-02 13:33:06 +00:00
import android.text.method.LinkMovementMethod;
import android.text.style.URLSpan;
2018-08-02 13:33:06 +00:00
import android.view.LayoutInflater;
2018-08-03 07:39:43 +00:00
import android.view.Menu;
import android.view.MenuInflater;
2018-08-02 13:33:06 +00:00
import android.view.MenuItem;
import android.view.MotionEvent;
2018-08-02 13:33:06 +00:00
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;
2018-08-09 20:45:42 +00:00
import android.widget.Toast;
2018-08-02 13:33:06 +00:00
2018-08-08 06:55:47 +00:00
import com.google.android.material.bottomnavigation.BottomNavigationView;
2018-08-16 12:45:03 +00:00
import com.google.android.material.floatingactionbutton.FloatingActionButton;
2018-08-08 06:55:47 +00:00
2018-08-04 21:11:19 +00:00
import java.text.Collator;
2018-08-02 13:33:06 +00:00
import java.text.DateFormat;
import java.text.SimpleDateFormat;
2018-08-13 18:31:42 +00:00
import java.util.ArrayList;
2018-08-04 21:11:19 +00:00
import java.util.Collections;
import java.util.Comparator;
2018-08-02 13:33:06 +00:00
import java.util.Date;
2018-08-03 13:46:25 +00:00
import java.util.List;
2018-08-04 21:11:19 +00:00
import java.util.Locale;
2018-08-02 13:33:06 +00:00
2018-08-08 06:55:47 +00:00
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.PopupMenu;
import androidx.browser.customtabs.CustomTabsIntent;
import androidx.constraintlayout.widget.Group;
2018-08-12 15:31:43 +00:00
import androidx.fragment.app.FragmentManager;
2018-08-08 06:55:47 +00:00
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
public class FragmentMessage extends FragmentEx {
2018-08-13 13:53:46 +00:00
private ViewGroup view;
2018-08-02 13:33:06 +00:00
private TextView tvFrom;
2018-08-11 09:18:49 +00:00
private TextView tvTime;
private TextView tvTo;
2018-08-11 09:18:49 +00:00
private TextView tvSubject;
private TextView tvCount;
private TextView tvReplyTo;
2018-08-03 07:39:43 +00:00
private TextView tvCc;
private TextView tvBcc;
2018-08-03 13:46:25 +00:00
private RecyclerView rvAttachment;
2018-08-11 09:18:49 +00:00
private TextView tvError;
2018-08-02 13:33:06 +00:00
private BottomNavigationView top_navigation;
private TextView tvBody;
2018-08-16 12:45:03 +00:00
private FloatingActionButton fab;
2018-08-02 13:33:06 +00:00
private BottomNavigationView bottom_navigation;
private ProgressBar pbWait;
2018-08-16 12:45:03 +00:00
private Group grpReady;
private Group grpHeader;
2018-08-10 17:13:42 +00:00
private Group grpAddresses;
2018-08-03 13:46:25 +00:00
private Group grpAttachments;
2018-08-02 13:33:06 +00:00
2018-08-16 12:45:03 +00:00
private boolean free = false;
2018-08-03 13:46:25 +00:00
private AdapterAttachment adapter;
2018-08-02 13:33:06 +00:00
private boolean debug;
2018-08-02 13:33:06 +00:00
private DateFormat df = SimpleDateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
@Override
@Nullable
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
2018-08-13 13:53:46 +00:00
view = (ViewGroup) inflater.inflate(R.layout.fragment_message, container, false);
2018-08-02 13:33:06 +00:00
// Get arguments
2018-08-05 16:14:43 +00:00
Bundle args = getArguments();
final long id = (args == null ? -1 : args.getLong("id"));
debug = PreferenceManager.getDefaultSharedPreferences(getContext()).getBoolean("debug", false);
2018-08-02 13:33:06 +00:00
// Get controls
2018-08-03 07:39:43 +00:00
tvFrom = view.findViewById(R.id.tvFrom);
2018-08-11 09:18:49 +00:00
tvTime = view.findViewById(R.id.tvTime);
tvTo = view.findViewById(R.id.tvTo);
2018-08-11 09:18:49 +00:00
tvSubject = view.findViewById(R.id.tvSubject);
tvCount = view.findViewById(R.id.tvCount);
tvReplyTo = view.findViewById(R.id.tvReplyTo);
2018-08-03 07:39:43 +00:00
tvCc = view.findViewById(R.id.tvCc);
tvBcc = view.findViewById(R.id.tvBcc);
2018-08-03 13:46:25 +00:00
rvAttachment = view.findViewById(R.id.rvAttachment);
2018-08-11 09:18:49 +00:00
tvError = view.findViewById(R.id.tvError);
2018-08-02 13:33:06 +00:00
top_navigation = view.findViewById(R.id.top_navigation);
tvBody = view.findViewById(R.id.tvBody);
2018-08-16 12:45:03 +00:00
fab = view.findViewById(R.id.fab);
2018-08-02 13:33:06 +00:00
bottom_navigation = view.findViewById(R.id.bottom_navigation);
pbWait = view.findViewById(R.id.pbWait);
2018-08-16 12:45:03 +00:00
grpReady = view.findViewById(R.id.grpReady);
grpHeader = view.findViewById(R.id.grpHeader);
2018-08-10 17:13:42 +00:00
grpAddresses = view.findViewById(R.id.grpAddresses);
2018-08-03 13:46:25 +00:00
grpAttachments = view.findViewById(R.id.grpAttachments);
2018-08-02 13:33:06 +00:00
2018-08-03 07:39:43 +00:00
setHasOptionsMenu(true);
2018-08-03 13:46:25 +00:00
tvBody.setMovementMethod(new LinkMovementMethod() {
public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {
if (event.getAction() != MotionEvent.ACTION_UP)
return super.onTouchEvent(widget, buffer, event);
int x = (int) event.getX();
int y = (int) event.getY();
x -= widget.getTotalPaddingLeft();
y -= widget.getTotalPaddingTop();
x += widget.getScrollX();
y += widget.getScrollY();
Layout layout = widget.getLayout();
int line = layout.getLineForVertical(y);
int off = layout.getOffsetForHorizontal(line, x);
URLSpan[] link = buffer.getSpans(off, off, URLSpan.class);
if (link.length != 0) {
2018-08-08 05:02:25 +00:00
String url = link[0].getURL();
if (true) {
// https://developer.chrome.com/multidevice/android/customtabs
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
builder.setToolbarColor(Helper.resolveColor(getContext(), R.attr.colorPrimary));
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(getContext(), Uri.parse(url));
} else {
Bundle args = new Bundle();
args.putString("link", url);
FragmentWebView fragment = new FragmentWebView();
fragment.setArguments(args);
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content_frame, fragment).addToBackStack("webview");
fragmentTransaction.commit();
}
}
return true;
}
});
2018-08-02 13:33:06 +00:00
// Wire controls
top_navigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.action_thread:
onActionThread(id);
return true;
2018-08-02 13:33:06 +00:00
case R.id.action_seen:
onActionSeen(id);
return true;
case R.id.action_edit:
onActionEdit(id);
2018-08-02 13:33:06 +00:00
return true;
case R.id.action_forward:
onActionForward(id);
return true;
case R.id.action_reply_all:
onActionReplyAll(id);
return true;
}
return false;
}
});
2018-08-16 12:45:03 +00:00
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
free = (top_navigation.getVisibility() != View.GONE);
getActivity().invalidateOptionsMenu();
grpHeader.setVisibility(free ? View.GONE : View.VISIBLE);
if (free) {
fab.setImageResource(R.drawable.baseline_fullscreen_exit_24);
grpAddresses.setVisibility(View.GONE);
grpAttachments.setVisibility(View.GONE);
} else {
fab.setImageResource(R.drawable.baseline_fullscreen_24);
if (rvAttachment.getAdapter().getItemCount() > 0)
grpAttachments.setVisibility(View.VISIBLE);
}
}
});
2018-08-02 13:33:06 +00:00
bottom_navigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.action_spam:
onActionSpam(id);
return true;
2018-08-09 05:49:51 +00:00
case R.id.action_trash:
onActionDelete(id);
return true;
2018-08-04 20:00:11 +00:00
case R.id.action_move:
onActionMove(id);
return true;
2018-08-02 13:33:06 +00:00
case R.id.action_archive:
onActionArchive(id);
return true;
case R.id.action_reply:
onActionReply(id);
return true;
}
return false;
}
});
// Initialize
2018-08-10 17:13:42 +00:00
grpAddresses.setVisibility(View.GONE);
2018-08-03 13:46:25 +00:00
grpAttachments.setVisibility(View.GONE);
tvError.setVisibility(View.GONE);
top_navigation.setVisibility(View.GONE);
2018-08-04 18:52:09 +00:00
bottom_navigation.setVisibility(View.GONE);
2018-08-02 13:33:06 +00:00
grpReady.setVisibility(View.GONE);
pbWait.setVisibility(View.VISIBLE);
2018-08-03 13:46:25 +00:00
rvAttachment.setHasFixedSize(false);
LinearLayoutManager llm = new LinearLayoutManager(getContext());
rvAttachment.setLayoutManager(llm);
2018-08-14 14:16:29 +00:00
adapter = new AdapterAttachment(getContext(), getViewLifecycleOwner(), true);
2018-08-03 13:46:25 +00:00
rvAttachment.setAdapter(adapter);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Get arguments
Bundle args = getArguments();
final long id = (args == null ? -1 : args.getLong("id"));
2018-08-03 13:46:25 +00:00
final DB db = DB.getInstance(getContext());
2018-08-02 13:33:06 +00:00
// Observe message
db.message().liveMessage(id).observe(getViewLifecycleOwner(), new Observer<TupleMessageEx>() {
2018-08-02 13:33:06 +00:00
@Override
2018-08-05 15:42:02 +00:00
public void onChanged(@Nullable final TupleMessageEx message) {
if (message == null || (!(debug && BuildConfig.DEBUG) && message.ui_hide)) {
2018-08-02 13:33:06 +00:00
// Message gone (moved, deleted)
if (FragmentMessage.this.isVisible())
getFragmentManager().popBackStack();
} else {
2018-08-05 15:42:02 +00:00
setSubtitle(Helper.localizeFolderName(getContext(), message.folderName));
2018-08-09 21:33:14 +00:00
tvFrom.setText(message.from == null ? null : MessageHelper.getFormattedAddresses(message.from, true));
2018-08-02 13:33:06 +00:00
tvTime.setText(message.sent == null ? null : df.format(new Date(message.sent)));
tvTo.setText(message.to == null ? null : MessageHelper.getFormattedAddresses(message.to, true));
2018-08-02 13:33:06 +00:00
tvSubject.setText(message.subject);
2018-08-11 09:18:49 +00:00
tvCount.setText(Integer.toString(message.count));
tvCount.setVisibility(message.count > 1 ? View.VISIBLE : View.GONE);
2018-08-02 13:33:06 +00:00
tvReplyTo.setText(message.reply == null ? null : MessageHelper.getFormattedAddresses(message.reply, true));
2018-08-11 09:18:49 +00:00
tvCc.setText(message.cc == null ? null : MessageHelper.getFormattedAddresses(message.cc, true));
tvBcc.setText(message.bcc == null ? null : MessageHelper.getFormattedAddresses(message.bcc, true));
2018-08-07 20:12:45 +00:00
int typeface = (message.ui_seen ? Typeface.NORMAL : Typeface.BOLD);
tvFrom.setTypeface(null, typeface);
tvTime.setTypeface(null, typeface);
tvSubject.setTypeface(null, typeface);
tvCount.setTypeface(null, typeface);
int colorUnseen = Helper.resolveColor(getContext(), message.ui_seen
? android.R.attr.textColorSecondary : R.attr.colorUnread);
tvFrom.setTextColor(colorUnseen);
tvTime.setTextColor(colorUnseen);
2018-08-02 13:33:06 +00:00
2018-08-11 09:18:49 +00:00
tvError.setText(message.error);
tvError.setVisibility(message.error == null ? View.GONE : View.VISIBLE);
2018-08-02 13:33:06 +00:00
MenuItem actionSeen = top_navigation.getMenu().findItem(R.id.action_seen);
actionSeen.setIcon(message.ui_seen
? R.drawable.baseline_visibility_off_24
: R.drawable.baseline_visibility_24);
actionSeen.setTitle(message.ui_seen ? R.string.title_unseen : R.string.title_seen);
tvBody.setText(message.body == null
? null
: Html.fromHtml(HtmlHelper.sanitize(getContext(), message.body, false)));
2018-08-05 15:42:02 +00:00
db.folder().liveFolders(message.account).removeObservers(getViewLifecycleOwner());
db.folder().liveFolders(message.account).observe(getViewLifecycleOwner(), new Observer<List<TupleFolderEx>>() {
2018-08-05 15:42:02 +00:00
@Override
2018-08-13 18:31:42 +00:00
public void onChanged(@Nullable List<TupleFolderEx> folders) {
if (folders == null)
folders = new ArrayList<>();
2018-08-09 20:45:42 +00:00
boolean inInbox = EntityFolder.INBOX.equals(message.folderType);
boolean inOutbox = EntityFolder.OUTBOX.equals(message.folderType);
boolean inArchive = EntityFolder.ARCHIVE.equals(message.folderType);
//boolean inDafts = EntityFolder.DRAFTS.equals(message.folderType);
boolean inTrash = EntityFolder.TRASH.equals(message.folderType);
boolean inJunk = EntityFolder.JUNK.equals(message.folderType);
//boolean inSent = EntityFolder.SENT.equals(message.folderType);
2018-08-05 15:42:02 +00:00
boolean hasTrash = false;
boolean hasJunk = false;
boolean hasArchive = false;
boolean hasUser = false;
2018-08-12 15:31:43 +00:00
if (folders != null)
for (EntityFolder folder : folders) {
if (EntityFolder.TRASH.equals(folder.type))
hasTrash = true;
else if (EntityFolder.JUNK.equals(folder.type))
hasJunk = true;
else if (EntityFolder.ARCHIVE.equals(folder.type))
hasArchive = true;
else if (EntityFolder.USER.equals(folder.type))
hasUser = true;
}
2018-08-05 15:42:02 +00:00
2018-08-09 20:45:42 +00:00
bottom_navigation.setTag(inTrash || !hasTrash);
top_navigation.getMenu().findItem(R.id.action_thread).setVisible(message.count > 1);
2018-08-12 15:31:43 +00:00
top_navigation.getMenu().findItem(R.id.action_seen).setVisible(!inOutbox);
2018-08-09 20:45:42 +00:00
top_navigation.getMenu().findItem(R.id.action_edit).setVisible(inTrash);
top_navigation.getMenu().findItem(R.id.action_forward).setVisible(!inOutbox);
top_navigation.getMenu().findItem(R.id.action_reply_all).setVisible(!inOutbox && message.cc != null);
top_navigation.setVisibility(View.VISIBLE);
2018-08-16 14:27:32 +00:00
bottom_navigation.getMenu().findItem(R.id.action_spam).setVisible(message.uid != null && !inOutbox && !inArchive && !inJunk && hasJunk);
bottom_navigation.getMenu().findItem(R.id.action_trash).setVisible(message.uid != null && !inOutbox && hasTrash);
bottom_navigation.getMenu().findItem(R.id.action_move).setVisible(message.uid != null && !inOutbox && (!inInbox || hasUser));
bottom_navigation.getMenu().findItem(R.id.action_archive).setVisible(message.uid != null && !inOutbox && !inArchive && hasArchive);
2018-08-09 20:45:42 +00:00
bottom_navigation.getMenu().findItem(R.id.action_reply).setVisible(!inOutbox);
2018-08-05 15:42:02 +00:00
bottom_navigation.setVisibility(View.VISIBLE);
}
});
2018-08-02 13:33:06 +00:00
}
2018-08-04 18:52:09 +00:00
pbWait.setVisibility(View.GONE);
grpReady.setVisibility(View.VISIBLE);
2018-08-02 13:33:06 +00:00
}
});
2018-08-13 06:23:46 +00:00
// Observe attachments
db.attachment().liveAttachments(id).observe(getViewLifecycleOwner(),
2018-08-13 08:58:36 +00:00
new Observer<List<EntityAttachment>>() {
2018-08-13 06:23:46 +00:00
@Override
2018-08-13 08:58:36 +00:00
public void onChanged(@Nullable List<EntityAttachment> attachments) {
2018-08-13 18:31:42 +00:00
if (attachments == null)
attachments = new ArrayList<>();
adapter.set(attachments);
2018-08-16 12:45:03 +00:00
if (!free)
grpAttachments.setVisibility(attachments.size() > 0 ? View.VISIBLE : View.GONE);
2018-08-13 06:23:46 +00:00
}
});
2018-08-02 13:33:06 +00:00
}
2018-08-03 07:39:43 +00:00
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
2018-08-10 17:13:42 +00:00
inflater.inflate(R.menu.menu_view, menu);
2018-08-03 07:39:43 +00:00
super.onCreateOptionsMenu(menu, inflater);
}
2018-08-16 12:45:03 +00:00
@Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
menu.findItem(R.id.menu_addresses).setVisible(!free);
}
2018-08-03 07:39:43 +00:00
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
2018-08-10 17:13:42 +00:00
case R.id.menu_addresses:
onMenuAddresses();
2018-08-03 07:39:43 +00:00
return true;
default:
return super.onOptionsItemSelected(item);
}
}
2018-08-10 17:13:42 +00:00
private void onMenuAddresses() {
2018-08-03 13:46:25 +00:00
if (grpReady.getVisibility() == View.VISIBLE)
2018-08-10 17:13:42 +00:00
grpAddresses.setVisibility(grpAddresses.getVisibility() == View.GONE ? View.VISIBLE : View.GONE);
2018-08-03 07:39:43 +00:00
}
private void onActionThread(long id) {
2018-08-12 15:31:43 +00:00
getFragmentManager().popBackStack("thread", FragmentManager.POP_BACK_STACK_INCLUSIVE);
Bundle args = new Bundle();
args.putLong("thread", id); // message ID
FragmentMessages fragment = new FragmentMessages();
fragment.setArguments(args);
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content_frame, fragment).addToBackStack("thread");
fragmentTransaction.commit();
}
2018-08-09 20:45:42 +00:00
private void onActionSeen(long id) {
2018-08-13 13:53:46 +00:00
Helper.setViewsEnabled(view, false);
2018-08-09 20:45:42 +00:00
Bundle args = new Bundle();
args.putLong("id", id);
new SimpleTask<Void>() {
2018-08-02 13:33:06 +00:00
@Override
protected Void onLoad(Context context, Bundle args) {
2018-08-09 20:45:42 +00:00
long id = args.getLong("id");
DB db = DB.getInstance(context);
2018-08-02 20:45:16 +00:00
try {
2018-08-09 20:45:42 +00:00
db.beginTransaction();
2018-08-02 20:45:16 +00:00
EntityMessage message = db.message().getMessage(id);
for (EntityMessage tmessage : db.message().getMessageByThread(message.account, message.thread))
if (message.uid != null) { // Skip drafts and outbox
db.message().setMessageUiSeen(tmessage.id, !message.ui_seen);
2018-08-09 20:45:42 +00:00
EntityOperation.queue(db, tmessage, EntityOperation.SEEN, !tmessage.ui_seen);
}
2018-08-09 20:45:42 +00:00
db.setTransactionSuccessful();
} finally {
db.endTransaction();
2018-08-02 20:45:16 +00:00
}
2018-08-09 20:45:42 +00:00
EntityOperation.process(context);
2018-08-09 20:45:42 +00:00
return null;
2018-08-02 13:33:06 +00:00
}
2018-08-09 20:45:42 +00:00
@Override
protected void onLoaded(Bundle args, Void data) {
2018-08-13 13:53:46 +00:00
Helper.setViewsEnabled(view, true);
2018-08-11 05:02:13 +00:00
}
2018-08-09 20:45:42 +00:00
2018-08-11 05:02:13 +00:00
@Override
public void onException(Bundle args, Throwable ex) {
2018-08-13 13:53:46 +00:00
Helper.setViewsEnabled(view, true);
2018-08-11 05:02:13 +00:00
Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show();
2018-08-09 20:45:42 +00:00
}
}.load(this, args);
2018-08-02 13:33:06 +00:00
}
private void onActionEdit(final long id) {
2018-08-13 13:53:46 +00:00
Helper.setViewsEnabled(view, false);
2018-08-09 20:45:42 +00:00
Bundle args = new Bundle();
args.putLong("id", id);
new SimpleTask<Void>() {
@Override
protected Void onLoad(Context context, Bundle args) {
2018-08-09 20:45:42 +00:00
long id = args.getLong("id");
DB db = DB.getInstance(context);
try {
db.beginTransaction();
EntityMessage draft = db.message().getMessage(id);
EntityFolder drafts = db.folder().getFolderByType(draft.account, EntityFolder.DRAFTS);
draft.id = null;
draft.folder = drafts.id;
draft.uid = null;
draft.id = db.message().insertMessage(draft);
2018-08-12 15:31:43 +00:00
EntityOperation.queue(db, draft, EntityOperation.ADD);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
2018-08-12 15:31:43 +00:00
EntityOperation.process(context);
return null;
2018-08-09 20:45:42 +00:00
}
@Override
protected void onLoaded(Bundle args, Void data) {
2018-08-13 13:53:46 +00:00
Helper.setViewsEnabled(view, true);
2018-08-11 05:02:13 +00:00
getContext().startActivity(
new Intent(getContext(), ActivityCompose.class)
.putExtra("action", "edit")
.putExtra("id", id));
}
2018-08-11 05:02:13 +00:00
@Override
public void onException(Bundle args, Throwable ex) {
2018-08-13 13:53:46 +00:00
Helper.setViewsEnabled(view, true);
2018-08-11 05:02:13 +00:00
Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show();
}
}.load(this, args);
2018-08-02 13:33:06 +00:00
}
private void onActionForward(long id) {
startActivity(new Intent(getContext(), ActivityCompose.class)
.putExtra("action", "forward")
.putExtra("reference", id));
2018-08-02 13:33:06 +00:00
}
private void onActionReplyAll(long id) {
startActivity(new Intent(getContext(), ActivityCompose.class)
.putExtra("action", "reply_all")
.putExtra("reference", id));
2018-08-02 13:33:06 +00:00
}
2018-08-09 05:49:51 +00:00
private void onActionSpam(final long id) {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder
.setMessage(R.string.title_ask_spam)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
2018-08-13 13:53:46 +00:00
Helper.setViewsEnabled(view, false);
2018-08-09 20:45:42 +00:00
Bundle args = new Bundle();
args.putLong("id", id);
new SimpleTask<Void>() {
2018-08-09 05:49:51 +00:00
@Override
protected Void onLoad(Context context, Bundle args) {
2018-08-09 20:45:42 +00:00
long id = args.getLong("id");
DB db = DB.getInstance(context);
2018-08-09 05:49:51 +00:00
try {
2018-08-09 20:45:42 +00:00
db.beginTransaction();
2018-08-13 14:44:47 +00:00
db.message().setMessageUiHide(id, true);
2018-08-09 05:49:51 +00:00
2018-08-13 14:44:47 +00:00
EntityMessage message = db.message().getMessage(id);
EntityFolder spam = db.folder().getFolderByType(message.account, EntityFolder.JUNK);
EntityOperation.queue(db, message, EntityOperation.MOVE, spam.id);
2018-08-09 20:45:42 +00:00
db.setTransactionSuccessful();
} finally {
db.endTransaction();
2018-08-09 05:49:51 +00:00
}
2018-08-09 20:45:42 +00:00
EntityOperation.process(context);
2018-08-09 20:45:42 +00:00
return null;
}
@Override
protected void onLoaded(Bundle args, Void result) {
2018-08-13 13:53:46 +00:00
Helper.setViewsEnabled(view, true);
2018-08-11 05:02:13 +00:00
}
2018-08-09 20:45:42 +00:00
2018-08-11 05:02:13 +00:00
@Override
protected void onException(Bundle args, Throwable ex) {
2018-08-13 13:53:46 +00:00
Helper.setViewsEnabled(view, true);
2018-08-11 05:02:13 +00:00
Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show();
2018-08-09 05:49:51 +00:00
}
}.load(FragmentMessage.this, args);
2018-08-09 05:49:51 +00:00
}
})
.setNegativeButton(android.R.string.cancel, null).show();
}
2018-08-02 13:33:06 +00:00
private void onActionDelete(final long id) {
2018-08-09 20:45:42 +00:00
boolean delete = (Boolean) bottom_navigation.getTag();
if (delete) {
// No trash or is trash
2018-08-04 18:52:09 +00:00
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder
.setMessage(R.string.title_ask_delete)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
2018-08-13 13:53:46 +00:00
Helper.setViewsEnabled(view, false);
2018-08-09 20:45:42 +00:00
Bundle args = new Bundle();
args.putLong("id", id);
new SimpleTask<Void>() {
2018-08-04 18:52:09 +00:00
@Override
protected Void onLoad(Context context, Bundle args) {
2018-08-09 20:45:42 +00:00
long id = args.getLong("id");
DB db = DB.getInstance(context);
2018-08-04 18:52:09 +00:00
try {
2018-08-09 20:45:42 +00:00
db.beginTransaction();
2018-08-04 18:52:09 +00:00
2018-08-13 14:44:47 +00:00
db.message().setMessageUiHide(id, true);
2018-08-09 20:45:42 +00:00
EntityMessage message = db.message().getMessage(id);
2018-08-10 06:24:39 +00:00
EntityOperation.queue(db, message, EntityOperation.DELETE);
2018-08-09 20:45:42 +00:00
db.setTransactionSuccessful();
} finally {
db.endTransaction();
2018-08-04 18:52:09 +00:00
}
2018-08-09 20:45:42 +00:00
EntityOperation.process(context);
2018-08-09 20:45:42 +00:00
return null;
2018-08-02 20:45:16 +00:00
}
2018-08-09 20:45:42 +00:00
@Override
protected void onLoaded(Bundle args, Void result) {
2018-08-13 13:53:46 +00:00
Helper.setViewsEnabled(view, true);
2018-08-11 05:02:13 +00:00
}
2018-08-09 20:45:42 +00:00
2018-08-11 05:02:13 +00:00
@Override
protected void onException(Bundle args, Throwable ex) {
2018-08-13 13:53:46 +00:00
Helper.setViewsEnabled(view, true);
2018-08-11 05:02:13 +00:00
Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show();
2018-08-09 20:45:42 +00:00
}
}.load(FragmentMessage.this, args);
2018-08-04 18:52:09 +00:00
}
})
.setNegativeButton(android.R.string.cancel, null).show();
} else {
2018-08-13 13:53:46 +00:00
Helper.setViewsEnabled(view, false);
2018-08-09 20:45:42 +00:00
Bundle args = new Bundle();
args.putLong("id", id);
new SimpleTask<Void>() {
2018-08-04 18:52:09 +00:00
@Override
protected Void onLoad(Context context, Bundle args) {
2018-08-09 20:45:42 +00:00
long id = args.getLong("id");
DB db = DB.getInstance(context);
2018-08-04 18:52:09 +00:00
try {
2018-08-09 20:45:42 +00:00
db.beginTransaction();
if (debug && BuildConfig.DEBUG)
db.message().deleteMessage(id);
else {
2018-08-13 14:44:47 +00:00
db.message().setMessageUiHide(id, true);
2018-08-04 18:52:09 +00:00
2018-08-13 14:44:47 +00:00
EntityMessage message = db.message().getMessage(id);
EntityFolder trash = db.folder().getFolderByType(message.account, EntityFolder.TRASH);
EntityOperation.queue(db, message, EntityOperation.MOVE, trash.id);
}
2018-08-09 20:45:42 +00:00
db.setTransactionSuccessful();
} finally {
db.endTransaction();
2018-08-02 13:33:06 +00:00
}
2018-08-09 20:45:42 +00:00
EntityOperation.process(context);
2018-08-09 20:45:42 +00:00
return null;
2018-08-04 18:52:09 +00:00
}
2018-08-09 20:45:42 +00:00
@Override
protected void onLoaded(Bundle args, Void result) {
2018-08-13 13:53:46 +00:00
Helper.setViewsEnabled(view, true);
2018-08-11 05:02:13 +00:00
}
2018-08-09 20:45:42 +00:00
2018-08-11 05:02:13 +00:00
@Override
protected void onException(Bundle args, Throwable ex) {
2018-08-13 13:53:46 +00:00
Helper.setViewsEnabled(view, true);
2018-08-11 05:02:13 +00:00
Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show();
2018-08-09 20:45:42 +00:00
}
}.load(FragmentMessage.this, args);
2018-08-04 18:52:09 +00:00
}
2018-08-02 13:33:06 +00:00
}
2018-08-09 20:45:42 +00:00
private void onActionMove(long id) {
2018-08-04 21:11:19 +00:00
Bundle args = new Bundle();
2018-08-07 20:12:45 +00:00
args.putLong("id", id);
new SimpleTask<List<EntityFolder>>() {
@Override
protected List<EntityFolder> onLoad(Context context, Bundle args) {
EntityMessage message;
List<EntityFolder> folders;
DB db = DB.getInstance(getContext());
try {
db.beginTransaction();
message = db.message().getMessage(args.getLong("id"));
folders = db.folder().getUserFolders(message.account);
for (int i = 0; i < folders.size(); i++)
if (folders.get(i).id.equals(message.folder)) {
folders.remove(i);
break;
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
final Collator collator = Collator.getInstance(Locale.getDefault());
collator.setStrength(Collator.SECONDARY); // Case insensitive, process accents etc
Collections.sort(folders, new Comparator<EntityFolder>() {
@Override
public int compare(EntityFolder f1, EntityFolder f2) {
return collator.compare(f1.name, f2.name);
}
});
EntityFolder inbox = db.folder().getFolderByType(message.account, EntityFolder.INBOX);
if (!message.folder.equals(inbox.id))
folders.add(0, inbox);
return folders;
}
@Override
protected void onLoaded(final Bundle args, List<EntityFolder> folders) {
View anchor = bottom_navigation.findViewById(R.id.action_move);
PopupMenu popupMenu = new PopupMenu(getContext(), anchor);
int order = 0;
for (EntityFolder folder : folders)
popupMenu.getMenu().add(Menu.NONE, folder.id.intValue(), order++,
Helper.localizeFolderName(getContext(), folder.name));
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(final MenuItem target) {
2018-08-13 13:53:46 +00:00
Helper.setViewsEnabled(view, false);
args.putLong("target", target.getItemId());
new SimpleTask<Void>() {
@Override
protected Void onLoad(Context context, Bundle args) {
long id = args.getLong("id");
long target = args.getLong("target");
DB db = DB.getInstance(context);
try {
db.beginTransaction();
EntityMessage message = db.message().getMessage(id);
2018-08-12 15:31:43 +00:00
EntityFolder folder = db.folder().getFolder(message.folder);
2018-08-13 14:44:47 +00:00
if (!EntityFolder.ARCHIVE.equals(folder.type))
db.message().setMessageUiHide(message.id, true);
EntityOperation.queue(db, message, EntityOperation.MOVE, target);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
EntityOperation.process(context);
return null;
}
@Override
protected void onLoaded(Bundle args, Void result) {
2018-08-13 13:53:46 +00:00
Helper.setViewsEnabled(view, true);
}
@Override
protected void onException(Bundle args, Throwable ex) {
2018-08-13 13:53:46 +00:00
Helper.setViewsEnabled(view, true);
Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show();
}
}.load(FragmentMessage.this, args);
return true;
}
});
popupMenu.show();
}
}.load(FragmentMessage.this, args);
2018-08-04 20:00:11 +00:00
}
2018-08-09 20:45:42 +00:00
private void onActionArchive(long id) {
2018-08-13 13:53:46 +00:00
Helper.setViewsEnabled(view, false);
2018-08-09 20:45:42 +00:00
Bundle args = new Bundle();
args.putLong("id", id);
new SimpleTask<Void>() {
2018-08-02 13:33:06 +00:00
@Override
protected Void onLoad(Context context, Bundle args) {
2018-08-09 20:45:42 +00:00
long id = args.getLong("id");
DB db = DB.getInstance(context);
2018-08-02 20:45:16 +00:00
try {
2018-08-09 20:45:42 +00:00
db.beginTransaction();
2018-08-13 14:44:47 +00:00
db.message().setMessageUiHide(id, true);
2018-08-02 13:33:06 +00:00
2018-08-13 14:44:47 +00:00
EntityMessage message = db.message().getMessage(id);
EntityFolder archive = db.folder().getFolderByType(message.account, EntityFolder.ARCHIVE);
EntityOperation.queue(db, message, EntityOperation.MOVE, archive.id);
2018-08-09 20:45:42 +00:00
db.setTransactionSuccessful();
} finally {
db.endTransaction();
2018-08-02 20:45:16 +00:00
}
2018-08-09 20:45:42 +00:00
EntityOperation.process(context);
2018-08-09 20:45:42 +00:00
return null;
2018-08-02 13:33:06 +00:00
}
2018-08-09 20:45:42 +00:00
@Override
protected void onLoaded(Bundle args, Void result) {
2018-08-13 13:53:46 +00:00
Helper.setViewsEnabled(view, true);
2018-08-11 05:02:13 +00:00
}
2018-08-09 20:45:42 +00:00
2018-08-11 05:02:13 +00:00
@Override
protected void onException(Bundle args, Throwable ex) {
2018-08-13 13:53:46 +00:00
Helper.setViewsEnabled(view, true);
2018-08-11 05:02:13 +00:00
Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show();
2018-08-09 20:45:42 +00:00
}
}.load(FragmentMessage.this, args);
2018-08-02 13:33:06 +00:00
}
private void onActionReply(long id) {
startActivity(new Intent(getContext(), ActivityCompose.class)
.putExtra("action", "reply")
.putExtra("reference", id));
2018-08-02 13:33:06 +00:00
}
}