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

1307 lines
55 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-25 18:26:07 +00:00
import android.app.PendingIntent;
2018-09-01 13:51:04 +00:00
import android.content.BroadcastReceiver;
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;
2018-09-01 13:51:04 +00:00
import android.content.IntentFilter;
import android.content.SharedPreferences;
2018-08-24 19:58:52 +00:00
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
2018-08-02 13:33:06 +00:00
import android.graphics.Typeface;
2018-08-24 19:58:52 +00:00
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
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-24 19:58:52 +00:00
import android.text.Editable;
2018-08-02 13:33:06 +00:00
import android.text.Html;
import android.text.Layout;
import android.text.Spannable;
2018-08-19 06:53:56 +00:00
import android.text.Spanned;
2018-09-01 13:51:04 +00:00
import android.text.SpannedString;
import android.text.TextUtils;
2018-08-02 13:33:06 +00:00
import android.text.method.LinkMovementMethod;
2018-09-01 13:51:04 +00:00
import android.text.style.ImageSpan;
import android.text.style.URLSpan;
2018-08-24 19:58:52 +00:00
import android.util.Log;
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;
2018-08-24 19:58:52 +00:00
import android.widget.Button;
2018-08-02 13:33:06 +00:00
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-25 18:51:01 +00:00
import com.google.android.material.snackbar.Snackbar;
2018-08-08 06:55:47 +00:00
2018-08-28 18:23:10 +00:00
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
2018-08-25 18:26:07 +00:00
import org.openintents.openpgp.OpenPgpError;
import org.openintents.openpgp.util.OpenPgpApi;
import org.openintents.openpgp.util.OpenPgpServiceConnection;
2018-08-24 19:58:52 +00:00
import org.xml.sax.XMLReader;
import java.io.BufferedOutputStream;
2018-08-25 18:26:07 +00:00
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
2018-08-25 07:17:02 +00:00
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
2018-08-24 19:58:52 +00:00
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
2018-08-24 19:58:52 +00:00
import java.net.URL;
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-25 18:26:07 +00:00
import javax.mail.internet.InternetAddress;
2018-09-01 13:51:04 +00:00
import javax.mail.internet.InternetHeaders;
import javax.mail.internet.MimeBodyPart;
2018-08-25 18:26:07 +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;
2018-08-25 11:27:54 +00:00
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
2018-08-08 06:55:47 +00:00
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
2018-08-25 18:26:07 +00:00
import static android.app.Activity.RESULT_OK;
public class FragmentMessage extends FragmentEx {
2018-08-13 13:53:46 +00:00
private ViewGroup view;
2018-08-27 09:39:27 +00:00
private View vwAnswerAnchor;
2018-08-02 13:33:06 +00:00
private TextView tvFrom;
2018-08-23 16:29:00 +00:00
private TextView tvSize;
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-23 07:52:56 +00:00
private View vSeparatorBody;
2018-08-24 19:58:52 +00:00
private Button btnImages;
2018-08-02 13:33:06 +00:00
private TextView tvBody;
2018-08-24 06:33:52 +00:00
private ProgressBar pbBody;
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 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-25 06:28:15 +00:00
private Group grpError;
2018-08-18 20:31:07 +00:00
private Group grpMessage;
2018-08-02 13:33:06 +00:00
2018-08-23 07:52:56 +00:00
private TupleMessageEx message = null;
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
2018-08-25 18:26:07 +00:00
private String decrypted = null;
private OpenPgpServiceConnection openPgpConnection = null;
private boolean debug;
2018-08-02 13:33:06 +00:00
private DateFormat df = SimpleDateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
2018-08-26 07:08:00 +00:00
private static final long CACHE_IMAGE_DURATION = 3 * 24 * 3600 * 1000L;
2018-09-01 13:51:04 +00:00
static final String ACTION_DECRYPT_MESSAGE = BuildConfig.APPLICATION_ID + ".DECRYPT_MESSAGEs";
2018-08-25 07:17:02 +00:00
2018-08-25 18:26:07 +00:00
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
2018-08-26 13:24:16 +00:00
if (savedInstanceState == null)
message = (TupleMessageEx) getArguments().getSerializable("message");
else
message = (TupleMessageEx) savedInstanceState.getSerializable("message");
2018-08-25 18:26:07 +00:00
openPgpConnection = new OpenPgpServiceConnection(getContext(), "org.sufficientlysecure.keychain");
openPgpConnection.bindToService();
}
2018-09-01 13:51:04 +00:00
@Override
public void onResume() {
super.onResume();
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(getContext());
IntentFilter iff = new IntentFilter(ACTION_DECRYPT_MESSAGE);
lbm.registerReceiver(receiver, iff);
}
@Override
public void onPause() {
super.onPause();
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(getContext());
lbm.unregisterReceiver(receiver);
}
2018-08-25 18:26:07 +00:00
@Override
public void onDestroy() {
super.onDestroy();
if (openPgpConnection != null) {
openPgpConnection.unbindFromService();
openPgpConnection = null;
}
}
2018-08-02 13:33:06 +00:00
@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
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
debug = prefs.getBoolean("debug", false);
2018-08-02 13:33:06 +00:00
// Get controls
2018-08-27 09:39:27 +00:00
vwAnswerAnchor = view.findViewById(R.id.vwAnswerAnchor);
2018-08-03 07:39:43 +00:00
tvFrom = view.findViewById(R.id.tvFrom);
2018-08-23 16:29:00 +00:00
tvSize = view.findViewById(R.id.tvSize);
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-23 07:52:56 +00:00
vSeparatorBody = view.findViewById(R.id.vSeparatorBody);
2018-08-24 19:58:52 +00:00
btnImages = view.findViewById(R.id.btnImages);
2018-08-02 13:33:06 +00:00
tvBody = view.findViewById(R.id.tvBody);
2018-08-24 06:33:52 +00:00
pbBody = view.findViewById(R.id.pbBody);
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
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-25 06:28:15 +00:00
grpError = view.findViewById(R.id.grpError);
2018-08-18 20:31:07 +00:00
grpMessage = view.findViewById(R.id.grpMessage);
2018-08-02 13:33:06 +00:00
2018-08-03 07:39:43 +00:00
setHasOptionsMenu(true);
2018-08-23 07:52:56 +00:00
tvCount.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
2018-08-26 13:24:16 +00:00
onMenuThread();
2018-08-23 07:52:56 +00:00
}
});
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();
2018-08-25 11:27:54 +00:00
Uri uri = Uri.parse(url);
2018-08-08 05:02:25 +00:00
if (!"http".equals(uri.getScheme()) && !"https".equals(uri.getScheme())) {
Toast.makeText(getContext(), getString(R.string.title_no_viewer, uri.toString()), Toast.LENGTH_LONG).show();
return true;
}
2018-08-25 11:27:54 +00:00
if (BuildConfig.APPLICATION_ID.equals(uri.getHost()) && "/activate/".equals(uri.getPath())) {
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(getContext());
lbm.sendBroadcast(
new Intent(ActivityView.ACTION_ACTIVATE_PRO)
.putExtra("uri", uri));
} else if (prefs.getBoolean("webview", false)) {
2018-08-08 05:02:25 +00:00
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();
} else {
// 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));
2018-08-08 05:02:25 +00:00
}
}
return true;
}
});
2018-08-02 13:33:06 +00:00
// Wire controls
2018-08-16 12:45:03 +00:00
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
2018-08-23 17:34:38 +00:00
free = true;
getActivity().invalidateOptionsMenu();
grpHeader.setVisibility(View.GONE);
vSeparatorBody.setVisibility(View.GONE);
fab.setVisibility(View.GONE);
tvCount.setVisibility(View.GONE);
2018-08-19 05:25:49 +00:00
grpAddresses.setVisibility(View.GONE);
grpAttachments.setVisibility(View.GONE);
2018-08-25 06:28:15 +00:00
grpError.setVisibility(View.GONE);
2018-08-24 06:25:12 +00:00
tvCount.setTag(tvCount.getVisibility());
tvCc.setTag(grpAddresses.getVisibility());
2018-08-25 06:28:15 +00:00
tvError.setTag(grpError.getVisibility());
2018-08-19 05:25:49 +00:00
}
});
((ActivityBase) getActivity()).addBackPressedListener(new ActivityBase.IBackPressedListener() {
@Override
public boolean onBackPressed() {
if (free && isVisible()) {
free = false;
getActivity().invalidateOptionsMenu();
2018-08-23 17:34:38 +00:00
grpHeader.setVisibility(View.VISIBLE);
2018-08-23 07:52:56 +00:00
vSeparatorBody.setVisibility(View.VISIBLE);
2018-08-19 05:25:49 +00:00
fab.setVisibility(View.VISIBLE);
2018-08-23 17:34:38 +00:00
2018-08-23 18:13:43 +00:00
RecyclerView.Adapter adapter = rvAttachment.getAdapter();
2018-08-23 17:34:38 +00:00
tvCount.setVisibility((int) tvCount.getTag());
grpAddresses.setVisibility((int) tvCc.getTag());
2018-08-23 18:13:43 +00:00
grpAttachments.setVisibility(adapter != null && adapter.getItemCount() > 0 ? View.VISIBLE : View.GONE);
2018-08-25 06:28:15 +00:00
grpError.setVisibility((int) tvError.getTag());
2018-08-23 17:34:38 +00:00
2018-08-19 05:25:49 +00:00
return true;
2018-08-16 12:45:03 +00:00
}
2018-08-19 05:25:49 +00:00
return false;
2018-08-16 12:45:03 +00:00
}
});
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:
2018-08-26 13:24:16 +00:00
onActionSpam();
2018-08-02 13:33:06 +00:00
return true;
2018-08-09 05:49:51 +00:00
case R.id.action_trash:
2018-08-26 13:24:16 +00:00
onActionDelete();
2018-08-09 05:49:51 +00:00
return true;
2018-08-04 20:00:11 +00:00
case R.id.action_move:
2018-08-26 13:24:16 +00:00
onActionMove();
2018-08-04 20:00:11 +00:00
return true;
2018-08-02 13:33:06 +00:00
case R.id.action_archive:
2018-08-26 13:24:16 +00:00
onActionArchive();
2018-08-02 13:33:06 +00:00
return true;
case R.id.action_reply:
2018-08-26 13:24:16 +00:00
onActionReply();
2018-08-02 13:33:06 +00:00
return true;
}
return false;
}
});
// Initialize
2018-08-18 20:31:07 +00:00
grpHeader.setVisibility(View.GONE);
2018-08-10 17:13:42 +00:00
grpAddresses.setVisibility(View.GONE);
2018-08-03 13:46:25 +00:00
grpAttachments.setVisibility(View.GONE);
2018-08-24 19:58:52 +00:00
btnImages.setVisibility(View.GONE);
2018-08-18 20:31:07 +00:00
grpMessage.setVisibility(View.GONE);
2018-08-24 06:33:52 +00:00
pbBody.setVisibility(View.GONE);
2018-08-21 13:38:47 +00:00
bottom_navigation.setVisibility(View.GONE);
2018-08-18 20:31:07 +00:00
tvCount.setVisibility(View.GONE);
2018-08-25 06:28:15 +00:00
grpError.setVisibility(View.GONE);
2018-08-19 05:25:49 +00:00
fab.setVisibility(View.GONE);
2018-08-02 13:33:06 +00:00
pbWait.setVisibility(View.VISIBLE);
2018-08-23 16:29:00 +00:00
tvSize.setText(null);
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);
2018-09-01 07:20:09 +00:00
tvCount.setTag(View.GONE);
tvCc.setTag(View.GONE);
rvAttachment.setTag(View.GONE);
tvError.setTag(View.GONE);
return view;
}
@Override
2018-08-19 05:25:49 +00:00
public void onSaveInstanceState(Bundle outState) {
2018-08-21 18:00:06 +00:00
super.onSaveInstanceState(outState);
2018-08-26 13:24:16 +00:00
outState.putSerializable("message", message);
2018-08-19 05:25:49 +00:00
outState.putBoolean("free", free);
2018-08-23 17:34:38 +00:00
if (free) {
outState.putInt("tag_count", (int) tvCount.getTag());
outState.putInt("tag_cc", (int) tvCc.getTag());
outState.putInt("tag_error", (int) tvError.getTag());
}
2018-08-25 18:51:01 +00:00
outState.putString("decrypted", decrypted);
2018-08-19 05:25:49 +00:00
}
@Override
public void onActivityCreated(@Nullable final Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
2018-08-26 13:24:16 +00:00
if (savedInstanceState == null) {
setSubtitle(Helper.localizeFolderName(getContext(), message.folderName));
2018-08-24 06:43:05 +00:00
2018-08-28 14:59:52 +00:00
tvFrom.setText(MessageHelper.getFormattedAddresses(message.from, true));
2018-08-26 13:24:16 +00:00
tvTime.setText(message.sent == null ? null : df.format(new Date(message.sent)));
2018-08-28 14:59:52 +00:00
tvTo.setText(MessageHelper.getFormattedAddresses(message.to, true));
2018-08-26 13:24:16 +00:00
tvSubject.setText(message.subject);
2018-08-24 06:43:05 +00:00
2018-08-26 13:24:16 +00:00
tvCount.setText(Integer.toString(message.count));
2018-08-24 06:43:05 +00:00
2018-08-28 14:59:52 +00:00
tvReplyTo.setText(MessageHelper.getFormattedAddresses(message.reply, true));
tvCc.setText(MessageHelper.getFormattedAddresses(message.cc, true));
tvBcc.setText(MessageHelper.getFormattedAddresses(message.bcc, true));
2018-08-11 09:18:49 +00:00
2018-08-26 13:24:16 +00:00
tvError.setText(message.error);
} else {
free = savedInstanceState.getBoolean("free");
if (free) {
tvCount.setTag(savedInstanceState.getInt("tag_count"));
tvCc.setTag(savedInstanceState.getInt("tag_cc"));
rvAttachment.setTag(savedInstanceState.getInt("tag_attachment"));
tvError.setTag(savedInstanceState.getInt("tag_error"));
}
decrypted = savedInstanceState.getString("decrypted");
}
2018-08-11 09:18:49 +00:00
2018-08-26 13:24:16 +00:00
if (tvBody.getTag() == null) {
// Spanned text needs to be loaded after recreation too
final Bundle args = new Bundle();
args.putLong("id", message.id);
args.putBoolean("show_images", false);
2018-08-24 07:12:37 +00:00
2018-08-26 13:24:16 +00:00
pbBody.setVisibility(View.VISIBLE);
2018-08-24 19:58:52 +00:00
2018-08-26 13:24:16 +00:00
bodyTask.load(FragmentMessage.this, args);
btnImages.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
v.setEnabled(false);
args.putBoolean("show_images", true);
bodyTask.load(FragmentMessage.this, args);
2018-08-24 07:12:37 +00:00
}
2018-08-26 13:24:16 +00:00
});
}
2018-08-21 17:01:19 +00:00
2018-08-27 05:44:13 +00:00
setSeen();
2018-08-21 17:01:19 +00:00
2018-08-26 13:24:16 +00:00
pbWait.setVisibility(View.GONE);
2018-08-23 17:34:38 +00:00
2018-08-26 13:24:16 +00:00
grpHeader.setVisibility(free ? View.GONE : View.VISIBLE);
vSeparatorBody.setVisibility(free ? View.GONE : View.VISIBLE);
2018-08-23 17:34:38 +00:00
2018-08-26 13:24:16 +00:00
if (free) {
tvCount.setVisibility((int) tvCount.getTag());
grpAddresses.setVisibility((int) tvCc.getTag());
grpError.setVisibility((int) tvError.getTag());
} else {
tvCount.setVisibility(!free && message.count > 1 ? View.VISIBLE : View.GONE);
grpError.setVisibility(free || message.error == null ? View.GONE : View.VISIBLE);
}
DB db = DB.getInstance(getContext());
2018-09-01 16:34:16 +00:00
// Observe message
db.message().liveMessage(message.id).observe(getViewLifecycleOwner(), new Observer<TupleMessageEx>() {
2018-08-26 13:24:16 +00:00
2018-09-01 16:34:16 +00:00
@Override
public void onChanged(@Nullable final TupleMessageEx message) {
if (message == null || (!(debug && BuildConfig.DEBUG) && message.ui_hide)) {
// Message gone (moved, deleted)
finish();
return;
}
// Messages are immutable except for flags
FragmentMessage.this.message.seen = message.seen;
FragmentMessage.this.message.ui_seen = message.ui_seen;
setSeen();
}
});
// Observe attachments
db.attachment().liveAttachments(message.id).observe(getViewLifecycleOwner(),
new Observer<List<EntityAttachment>>() {
@Override
public void onChanged(@Nullable List<EntityAttachment> attachments) {
if (attachments == null)
attachments = new ArrayList<>();
adapter.set(attachments);
grpAttachments.setVisibility(!free && attachments.size() > 0 ? View.VISIBLE : View.GONE);
2018-08-26 13:24:16 +00:00
}
2018-09-01 16:34:16 +00:00
});
2018-08-27 05:44:13 +00:00
2018-09-01 16:34:16 +00:00
// Observe folders
db.folder().liveFolders(message.account).observe(getViewLifecycleOwner(), new Observer<List<TupleFolderEx>>() {
@Override
public void onChanged(@Nullable List<TupleFolderEx> folders) {
if (folders == null)
folders = new ArrayList<>();
boolean inInbox = EntityFolder.INBOX.equals(message.folderType);
boolean inOutbox = EntityFolder.OUTBOX.equals(message.folderType);
boolean inArchive = EntityFolder.ARCHIVE.equals(message.folderType);
boolean inTrash = EntityFolder.TRASH.equals(message.folderType);
boolean inJunk = EntityFolder.JUNK.equals(message.folderType);
boolean hasTrash = false;
boolean hasJunk = false;
boolean hasArchive = false;
boolean hasUser = false;
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-23 17:34:38 +00:00
2018-09-01 16:34:16 +00:00
bottom_navigation.setTag(inTrash || !hasTrash || inOutbox);
bottom_navigation.getMenu().findItem(R.id.action_spam).setVisible(message.uid != null && !inArchive && !inJunk && hasJunk);
bottom_navigation.getMenu().findItem(R.id.action_trash).setVisible((message.uid != null && hasTrash) || (inOutbox && !TextUtils.isEmpty(message.error)));
bottom_navigation.getMenu().findItem(R.id.action_move).setVisible(message.uid != null && (!inInbox || hasUser));
bottom_navigation.getMenu().findItem(R.id.action_archive).setVisible(message.uid != null && !inArchive && hasArchive);
bottom_navigation.getMenu().findItem(R.id.action_reply).setVisible(!inOutbox);
bottom_navigation.setVisibility(View.VISIBLE);
}
});
2018-08-02 13:33:06 +00:00
}
2018-08-27 05:44:13 +00:00
private void setSeen() {
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-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);
2018-08-23 07:52:56 +00:00
2018-08-26 13:24:16 +00:00
boolean inOutbox = EntityFolder.OUTBOX.equals(message.folderType);
2018-08-23 07:52:56 +00:00
2018-08-16 12:45:03 +00:00
menu.findItem(R.id.menu_addresses).setVisible(!free);
2018-09-01 16:34:16 +00:00
menu.findItem(R.id.menu_thread).setVisible(!free && message.count > 1);
menu.findItem(R.id.menu_forward).setVisible(!free && !inOutbox);
menu.findItem(R.id.menu_reply_all).setVisible(!free && message.cc != null && !inOutbox);
2018-09-01 06:35:00 +00:00
menu.findItem(R.id.menu_decrypt).setVisible(decrypted == null && !inOutbox);
2018-08-16 12:45:03 +00:00
}
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;
2018-08-23 07:52:56 +00:00
case R.id.menu_thread:
2018-08-26 13:24:16 +00:00
onMenuThread();
2018-08-23 07:52:56 +00:00
return true;
case R.id.menu_forward:
2018-08-26 13:24:16 +00:00
onMenuForward();
2018-08-23 07:52:56 +00:00
return true;
case R.id.menu_reply_all:
2018-08-26 13:24:16 +00:00
onMenuReplyAll();
2018-08-23 07:52:56 +00:00
return true;
2018-08-27 09:39:27 +00:00
case R.id.menu_answer:
onMenuAnswer();
return true;
2018-08-25 18:26:07 +00:00
case R.id.menu_decrypt:
2018-08-26 13:24:16 +00:00
onMenuDecrypt();
2018-08-25 18:26:07 +00:00
return true;
2018-08-03 07:39:43 +00:00
default:
return super.onOptionsItemSelected(item);
}
}
2018-08-10 17:13:42 +00:00
private void onMenuAddresses() {
2018-08-18 20:31:07 +00:00
grpAddresses.setVisibility(grpAddresses.getVisibility() == View.GONE ? View.VISIBLE : View.GONE);
2018-08-03 07:39:43 +00:00
}
2018-08-26 13:24:16 +00:00
private void onMenuThread() {
2018-08-12 15:31:43 +00:00
getFragmentManager().popBackStack("thread", FragmentManager.POP_BACK_STACK_INCLUSIVE);
Bundle args = new Bundle();
2018-08-26 13:24:16 +00:00
args.putLong("thread", 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-26 13:24:16 +00:00
private void onMenuForward() {
2018-08-02 13:33:06 +00:00
startActivity(new Intent(getContext(), ActivityCompose.class)
.putExtra("action", "forward")
2018-08-26 13:24:16 +00:00
.putExtra("reference", message.id));
2018-08-02 13:33:06 +00:00
}
2018-08-26 13:24:16 +00:00
private void onMenuReplyAll() {
2018-08-02 13:33:06 +00:00
startActivity(new Intent(getContext(), ActivityCompose.class)
.putExtra("action", "reply_all")
2018-08-26 13:24:16 +00:00
.putExtra("reference", message.id));
2018-08-02 13:33:06 +00:00
}
2018-08-27 09:39:27 +00:00
private void onMenuAnswer() {
DB.getInstance(getContext()).answer().liveAnswers().observe(getViewLifecycleOwner(), new Observer<List<EntityAnswer>>() {
@Override
public void onChanged(List<EntityAnswer> answers) {
final Collator collator = Collator.getInstance(Locale.getDefault());
collator.setStrength(Collator.SECONDARY); // Case insensitive, process accents etc
Collections.sort(answers, new Comparator<EntityAnswer>() {
@Override
public int compare(EntityAnswer a1, EntityAnswer a2) {
return collator.compare(a1.name, a2.name);
}
});
PopupMenu popupMenu = new PopupMenu(getContext(), vwAnswerAnchor);
int order = 0;
for (EntityAnswer answer : answers)
popupMenu.getMenu().add(Menu.NONE, answer.id.intValue(), order++,
Helper.localizeFolderName(getContext(), answer.name));
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem target) {
2018-08-28 11:45:29 +00:00
if (PreferenceManager.getDefaultSharedPreferences(getContext()).getBoolean("pro", false)) {
startActivity(new Intent(getContext(), ActivityCompose.class)
.putExtra("action", "reply")
.putExtra("reference", message.id)
.putExtra("answer", (long) target.getItemId()));
} else {
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content_frame, new FragmentPro()).addToBackStack("pro");
fragmentTransaction.commit();
}
2018-08-27 09:39:27 +00:00
return true;
}
});
popupMenu.show();
}
});
}
2018-08-26 13:24:16 +00:00
private void onMenuDecrypt() {
2018-08-27 17:05:34 +00:00
try {
2018-08-29 05:31:00 +00:00
// Find encrypted message
2018-08-28 18:23:10 +00:00
String begin = "-----BEGIN PGP MESSAGE-----";
String end = "-----END PGP MESSAGE-----";
Document document = Jsoup.parse(message.read(getContext()));
2018-09-01 13:51:04 +00:00
2018-08-28 18:23:10 +00:00
String encrypted = document.text();
2018-09-01 13:51:04 +00:00
2018-08-28 18:23:10 +00:00
int efrom = encrypted.indexOf(begin) + begin.length();
int eto = encrypted.indexOf(end);
2018-08-29 05:31:00 +00:00
if (efrom < 0 || eto < 0)
throw new IllegalArgumentException(getString(R.string.title_not_encrypted));
2018-08-25 18:26:07 +00:00
2018-09-01 13:51:04 +00:00
encrypted = begin + "\n" + encrypted.substring(efrom, eto).replace(" ", "\n") + end + "\n";
InputStream is = new ByteArrayInputStream(encrypted.getBytes("UTF-8"));
2018-08-29 05:31:00 +00:00
2018-09-01 13:51:04 +00:00
decrypt(is, false);
2018-08-29 05:31:00 +00:00
2018-08-25 18:26:07 +00:00
} catch (Throwable ex) {
Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
2018-08-25 18:51:01 +00:00
if (ex instanceof IllegalArgumentException)
Snackbar.make(view, ex.getMessage(), Snackbar.LENGTH_LONG).show();
else
Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show();
2018-08-25 18:26:07 +00:00
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
2018-08-25 18:51:01 +00:00
Log.i(Helper.TAG, "Message onActivityResult request=" + requestCode + " result=" + resultCode + " data=" + data);
2018-08-25 18:26:07 +00:00
if (resultCode == RESULT_OK) {
2018-08-26 13:24:16 +00:00
if (requestCode == ActivityView.REQUEST_OPENPGP) {
2018-08-25 18:26:07 +00:00
Log.i(Helper.TAG, "User interacted");
2018-08-26 13:24:16 +00:00
onMenuDecrypt();
2018-08-25 18:26:07 +00:00
}
}
}
2018-08-26 13:24:16 +00:00
private void onActionSpam() {
2018-08-09 05:49:51 +00:00
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();
2018-08-26 13:24:16 +00:00
args.putLong("id", message.id);
2018-08-09 20:45:42 +00:00
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-26 13:24:16 +00:00
private void onActionDelete() {
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();
2018-08-26 13:24:16 +00:00
args.putLong("id", message.id);
2018-08-09 20:45:42 +00:00
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-09 20:45:42 +00:00
EntityMessage message = db.message().getMessage(id);
if (message.uid == null && !TextUtils.isEmpty(message.error)) // outbox
db.message().deleteMessage(id);
else {
db.message().setMessageUiHide(id, true);
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();
2018-08-26 13:24:16 +00:00
args.putLong("id", message.id);
2018-08-09 20:45:42 +00:00
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-21 14:25:42 +00:00
db.message().setMessageUiHide(id, true);
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-26 13:24:16 +00:00
private void onActionMove() {
2018-08-04 21:11:19 +00:00
Bundle args = new Bundle();
2018-08-26 13:24:16 +00:00
args.putLong("id", message.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());
2018-08-21 13:57:07 +00:00
new SimpleTask<Boolean>() {
@Override
2018-08-21 13:57:07 +00:00
protected Boolean onLoad(Context context, Bundle args) {
long id = args.getLong("id");
long target = args.getLong("target");
boolean close;
2018-08-21 13:57:07 +00:00
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-21 13:57:07 +00:00
close = EntityFolder.ARCHIVE.equals(folder.type);
if (!close)
2018-08-13 14:44:47 +00:00
db.message().setMessageUiHide(message.id, true);
EntityOperation.queue(db, message, EntityOperation.MOVE, target);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
EntityOperation.process(context);
2018-08-21 13:57:07 +00:00
return close;
}
@Override
2018-08-21 13:57:07 +00:00
protected void onLoaded(Bundle args, Boolean close) {
2018-08-13 13:53:46 +00:00
Helper.setViewsEnabled(view, true);
2018-08-21 13:57:07 +00:00
if (close) // archived message
getFragmentManager().popBackStack();
}
@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-26 13:24:16 +00:00
private void onActionArchive() {
2018-08-13 13:53:46 +00:00
Helper.setViewsEnabled(view, false);
2018-08-09 20:45:42 +00:00
Bundle args = new Bundle();
2018-08-26 13:24:16 +00:00
args.putLong("id", message.id);
2018-08-09 20:45:42 +00:00
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-21 13:57:07 +00:00
getFragmentManager().popBackStack();
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
}
2018-08-26 13:24:16 +00:00
private void onActionReply() {
2018-08-02 13:33:06 +00:00
startActivity(new Intent(getContext(), ActivityCompose.class)
.putExtra("action", "reply")
2018-08-26 13:24:16 +00:00
.putExtra("reference", message.id));
2018-08-02 13:33:06 +00:00
}
2018-09-01 13:51:04 +00:00
SimpleTask<Spanned> bodyTask = new SimpleTask<Spanned>() {
@Override
protected Spanned onLoad(final Context context, final Bundle args) throws Throwable {
final long id = args.getLong("id");
final boolean show_images = args.getBoolean("show_images");
String body = (decrypted == null ? message.read(context) : decrypted);
args.putInt("size", body.length());
return decodeHtml(context, id, body, show_images);
}
@Override
protected void onLoaded(Bundle args, Spanned body) {
boolean show_images = args.getBoolean("show_images");
SpannedString ss = new SpannedString(body);
boolean has_images = (ss.getSpans(0, ss.length(), ImageSpan.class).length > 0);
tvSize.setText(Helper.humanReadableByteCount(args.getInt("size"), false));
tvBody.setText(body);
tvBody.setTag(true);
btnImages.setVisibility(has_images && !show_images ? View.VISIBLE : View.GONE);
grpMessage.setVisibility(View.VISIBLE);
fab.setVisibility(free ? View.GONE : View.VISIBLE);
pbBody.setVisibility(View.GONE);
}
};
private static Spanned decodeHtml(final Context context, final long id, String body, final boolean show_images) {
Spanned result = Html.fromHtml(HtmlHelper.sanitize(context, body, false), new Html.ImageGetter() {
@Override
public Drawable getDrawable(String source) {
float scale = context.getResources().getDisplayMetrics().density;
int px = (int) (24 * scale + 0.5f);
if (show_images) {
// Get cache folder
File dir = new File(context.getCacheDir(), "images");
dir.mkdir();
// Cleanup cache
long now = new Date().getTime();
File[] images = dir.listFiles();
if (images != null)
for (File image : images)
if (image.isFile() && image.lastModified() + CACHE_IMAGE_DURATION < now) {
Log.i(Helper.TAG, "Deleting from image cache " + image.getName());
image.delete();
}
// Create unique file name
File file = new File(dir, id + "_" + source.hashCode());
InputStream is = null;
FileOutputStream os = null;
try {
// Get input stream
if (file.exists()) {
Log.i(Helper.TAG, "Using cached " + file);
is = new FileInputStream(file);
} else {
Log.i(Helper.TAG, "Downloading " + source);
is = new URL(source).openStream();
}
// Decode image from stream
Bitmap bm = BitmapFactory.decodeStream(is);
if (bm == null)
throw new IllegalArgumentException();
// Cache bitmap
if (!file.exists()) {
os = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 100, os);
}
// Create drawable from bitmap
Drawable d = new BitmapDrawable(context.getResources(), bm);
d.setBounds(0, 0, bm.getWidth(), bm.getHeight());
return d;
} catch (Throwable ex) {
// Show warning icon
Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
Drawable d = context.getResources().getDrawable(R.drawable.baseline_warning_24, context.getTheme());
d.setBounds(0, 0, px, px);
return d;
} finally {
// Close streams
if (is != null) {
try {
is.close();
} catch (IOException e) {
Log.w(Helper.TAG, e + "\n" + Log.getStackTraceString(e));
}
}
if (os != null) {
try {
os.close();
} catch (IOException e) {
Log.w(Helper.TAG, e + "\n" + Log.getStackTraceString(e));
}
}
}
} else {
// Show placeholder icon
Drawable d = context.getResources().getDrawable(R.drawable.baseline_image_24, context.getTheme());
d.setBounds(0, 0, px, px);
return d;
}
}
}, new Html.TagHandler() {
@Override
public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) {
Log.i(Helper.TAG, "HTML tag=" + tag + " opening=" + opening);
}
});
return result;
}
private void decrypt(InputStream is, final boolean isPart) {
Log.i(Helper.TAG, "On decrypt");
if (!PreferenceManager.getDefaultSharedPreferences(getContext()).getBoolean("pro", false)) {
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content_frame, new FragmentPro()).addToBackStack("pro");
fragmentTransaction.commit();
return;
}
if (openPgpConnection == null || !openPgpConnection.isBound())
throw new IllegalArgumentException(getString(R.string.title_no_openpgp));
if (message.to == null || message.to.length == 0)
throw new IllegalArgumentException(getString(R.string.title_to_missing));
final ByteArrayOutputStream os = new ByteArrayOutputStream();
InternetAddress to = (InternetAddress) message.to[0];
Intent data = new Intent();
data.setAction(OpenPgpApi.ACTION_DECRYPT_VERIFY);
data.putExtra(OpenPgpApi.EXTRA_USER_IDS, new String[]{to.getAddress()});
OpenPgpApi api = new OpenPgpApi(getContext(), openPgpConnection.getService());
api.executeApiAsync(data, is, os, new OpenPgpApi.IOpenPgpCallback() {
@Override
public void onReturn(Intent result) {
try {
int code = result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR);
switch (code) {
case OpenPgpApi.RESULT_CODE_SUCCESS: {
Log.i(Helper.TAG, "Decrypted");
String decrypted = os.toString("UTF-8");
if (isPart) {
InternetHeaders ih = new InternetHeaders();
ih.addHeader("Content-Type", "multipart/alternative");
final MimeBodyPart part = new MimeBodyPart(ih, decrypted.getBytes());
FragmentMessage.this.decrypted = MessageHelper.getHtml(part);
// Store attachments
new Thread(new Runnable() {
@Override
public void run() {
try {
DB db = DB.getInstance(getContext());
int sequence = db.attachment().getAttachmentCount(message.id);
for (EntityAttachment attachment : MessageHelper.getAttachments(part))
if (db.attachment().getAttachmentCount(message.id, attachment.name) == 0)
try {
db.beginTransaction();
attachment.message = message.id;
attachment.sequence = ++sequence;
attachment.id = db.attachment().insertAttachment(attachment);
File file = EntityAttachment.getFile(getContext(), attachment.id);
// Store attachment
InputStream is = null;
OutputStream os = null;
try {
is = attachment.part.getInputStream();
os = new BufferedOutputStream(new FileOutputStream(file));
int size = 0;
byte[] buffer = new byte[4096];
for (int len = is.read(buffer); len != -1; len = is.read(buffer)) {
size += len;
os.write(buffer, 0, len);
}
// Store attachment data
attachment.size = size;
attachment.progress = null;
attachment.available = true;
db.attachment().updateAttachment(attachment);
} finally {
try {
if (is != null)
is.close();
} finally {
if (os != null)
os.close();
}
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
} catch (Throwable ex) {
Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
}
}
}).start();
2018-09-01 13:51:04 +00:00
} else
FragmentMessage.this.decrypted = "<pre>" + decrypted.replaceAll("\\r?\\n", "<br />") + "</pre>";
getActivity().invalidateOptionsMenu();
Bundle args = new Bundle();
args.putLong("id", message.id);
args.putBoolean("show_images", false);
pbBody.setVisibility(View.VISIBLE);
bodyTask.load(FragmentMessage.this, args);
break;
}
case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED: {
Log.i(Helper.TAG, "User interaction");
PendingIntent pi = result.getParcelableExtra(OpenPgpApi.RESULT_INTENT);
startIntentSenderForResult(
pi.getIntentSender(),
ActivityView.REQUEST_OPENPGP,
null, 0, 0, 0,
new Bundle());
break;
}
case OpenPgpApi.RESULT_CODE_ERROR: {
OpenPgpError error = result.getParcelableExtra(OpenPgpApi.RESULT_ERROR);
throw new IllegalArgumentException(error.getMessage());
}
}
} catch (Throwable ex) {
Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
if (ex instanceof IllegalArgumentException)
Snackbar.make(view, ex.getMessage(), Snackbar.LENGTH_LONG).show();
else
Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show();
}
}
});
}
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
new SimpleTask<InputStream>() {
@Override
protected InputStream onLoad(Context context, Bundle args) throws Throwable {
File file = (File) args.getSerializable("file");
FileInputStream fis = new FileInputStream(file);
return fis;
}
@Override
protected void onLoaded(Bundle args, InputStream data) {
decrypt(data, true);
}
}.load(FragmentMessage.this, intent.getExtras());
}
};
2018-08-02 13:33:06 +00:00
}