package eu.faircode.email;
/*
This file is part of FairEmail.
FairEmail is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FairEmail 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 FairEmail. If not, see .
Copyright 2018-2020 by Marcel Bokhorst (M66B)
*/
import android.Manifest;
import android.app.Activity;
import android.app.Dialog;
import android.app.PendingIntent;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.ContentResolver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentSender;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.res.ColorStateList;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkRequest;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.OperationCanceledException;
import android.provider.ContactsContract;
import android.provider.MediaStore;
import android.security.KeyChain;
import android.security.KeyChainException;
import android.text.Editable;
import android.text.Html;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.text.style.QuoteSpan;
import android.text.style.StyleSpan;
import android.text.style.URLSpan;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.FilterQueryProvider;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.MultiAutoCompleteTextView;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.PopupMenu;
import androidx.constraintlayout.widget.Group;
import androidx.core.content.FileProvider;
import androidx.cursoradapter.widget.SimpleCursorAdapter;
import androidx.documentfile.provider.DocumentFile;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.Observer;
import androidx.preference.PreferenceManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.bottomnavigation.LabelVisibilityMode;
import com.google.android.material.snackbar.Snackbar;
import org.bouncycastle.cert.jcajce.JcaCertStore;
import org.bouncycastle.cms.CMSAlgorithm;
import org.bouncycastle.cms.CMSEnvelopedData;
import org.bouncycastle.cms.CMSEnvelopedDataGenerator;
import org.bouncycastle.cms.CMSProcessableByteArray;
import org.bouncycastle.cms.CMSSignedData;
import org.bouncycastle.cms.CMSSignedDataGenerator;
import org.bouncycastle.cms.CMSTypedData;
import org.bouncycastle.cms.RecipientInfoGenerator;
import org.bouncycastle.cms.SignerInfoGenerator;
import org.bouncycastle.cms.jcajce.JcaSignerInfoGeneratorBuilder;
import org.bouncycastle.cms.jcajce.JceCMSContentEncryptorBuilder;
import org.bouncycastle.cms.jcajce.JceKeyTransRecipientInfoGenerator;
import org.bouncycastle.operator.ContentSigner;
import org.bouncycastle.operator.DigestCalculatorProvider;
import org.bouncycastle.operator.OutputEncryptor;
import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder;
import org.bouncycastle.operator.jcajce.JcaDigestCalculatorProviderBuilder;
import org.bouncycastle.util.Store;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.nodes.TextNode;
import org.jsoup.select.Elements;
import org.jsoup.select.NodeFilter;
import org.openintents.openpgp.OpenPgpError;
import org.openintents.openpgp.util.OpenPgpApi;
import org.openintents.openpgp.util.OpenPgpServiceConnection;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.UnknownHostException;
import java.security.PrivateKey;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.text.Collator;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.regex.Pattern;
import javax.activation.DataHandler;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.MessageRemovedException;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.Session;
import javax.mail.internet.AddressException;
import javax.mail.internet.ContentType;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.ParseException;
import javax.mail.util.ByteArrayDataSource;
import biweekly.Biweekly;
import biweekly.ICalendar;
import biweekly.component.VEvent;
import biweekly.property.Organizer;
import static android.app.Activity.RESULT_CANCELED;
import static android.app.Activity.RESULT_FIRST_USER;
import static android.app.Activity.RESULT_OK;
import static android.widget.AdapterView.INVALID_POSITION;
public class FragmentCompose extends FragmentBase {
private enum State {NONE, LOADING, LOADED}
private ViewGroup view;
private Spinner spIdentity;
private EditText etExtra;
private TextView tvDomain;
private MultiAutoCompleteTextView etTo;
private ImageButton ibToAdd;
private MultiAutoCompleteTextView etCc;
private ImageButton ibCcAdd;
private MultiAutoCompleteTextView etBcc;
private ImageButton ibBccAdd;
private EditText etSubject;
private ImageButton ibCcBcc;
private RecyclerView rvAttachment;
private TextView tvNoInternetAttachments;
private ImageButton ibCloseUnusedImagesHint;
private EditTextCompose etBody;
private TextView tvNoInternet;
private TextView tvSignature;
private CheckBox cbSignature;
private TextView tvReference;
private ImageButton ibCloseRefHint;
private ImageButton ibReferenceEdit;
private ImageButton ibReferenceImages;
private BottomNavigationView style_bar;
private BottomNavigationView media_bar;
private BottomNavigationView bottom_navigation;
private ContentLoadingProgressBar pbWait;
private Group grpHeader;
private Group grpExtra;
private Group grpAddresses;
private Group grpAttachments;
private Group grpUnusedImagesHint;
private Group grpBody;
private Group grpSignature;
private Group grpReferenceHint;
private ContentResolver resolver;
private AdapterAttachment adapter;
private boolean prefix_once = false;
private boolean monospaced = false;
private Integer encrypt = null;
private boolean media = true;
private boolean compact = false;
private int zoom = 0;
private long working = -1;
private State state = State.NONE;
private boolean show_images = false;
private int last_available = 0; // attachments
private Uri photoURI = null;
private OpenPgpServiceConnection pgpService;
private String[] pgpUserIds;
private long[] pgpKeyIds;
private long pgpSignKeyId;
private static final int REDUCED_IMAGE_SIZE = 1440; // pixels
private static final int REDUCED_IMAGE_QUALITY = 90; // percent
private static final int ADDRESS_ELLIPSIZE = 50;
private static final int RECIPIENTS_WARNING = 10;
private static final int REQUEST_CONTACT_TO = 1;
private static final int REQUEST_CONTACT_CC = 2;
private static final int REQUEST_CONTACT_BCC = 3;
private static final int REQUEST_IMAGE = 4;
private static final int REQUEST_IMAGE_FILE = 5;
private static final int REQUEST_ATTACHMENT = 6;
private static final int REQUEST_TAKE_PHOTO = 7;
private static final int REQUEST_RECORD_AUDIO = 8;
private static final int REQUEST_OPENPGP = 9;
private static final int REQUEST_COLOR = 10;
private static final int REQUEST_CONTACT_GROUP = 11;
private static final int REQUEST_ANSWER = 12;
private static final int REQUEST_LINK = 13;
private static final int REQUEST_DISCARD = 14;
private static final int REQUEST_SEND = 15;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
prefix_once = prefs.getBoolean("prefix_once", true);
monospaced = prefs.getBoolean("monospaced", false);
media = prefs.getBoolean("compose_media", true);
compact = prefs.getBoolean("compose_compact", false);
zoom = prefs.getInt("compose_zoom", compact ? 0 : 1);
setTitle(R.string.page_compose);
setSubtitle(getResources().getQuantityString(R.plurals.page_message, 1));
}
@Override
@Nullable
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = (ViewGroup) inflater.inflate(R.layout.fragment_compose, container, false);
// Get controls
spIdentity = view.findViewById(R.id.spIdentity);
etExtra = view.findViewById(R.id.etExtra);
tvDomain = view.findViewById(R.id.tvDomain);
etTo = view.findViewById(R.id.etTo);
ibToAdd = view.findViewById(R.id.ivToAdd);
etCc = view.findViewById(R.id.etCc);
ibCcAdd = view.findViewById(R.id.ivCcAdd);
etBcc = view.findViewById(R.id.etBcc);
ibBccAdd = view.findViewById(R.id.ivBccAdd);
etSubject = view.findViewById(R.id.etSubject);
ibCcBcc = view.findViewById(R.id.ivCcBcc);
rvAttachment = view.findViewById(R.id.rvAttachment);
tvNoInternetAttachments = view.findViewById(R.id.tvNoInternetAttachments);
ibCloseUnusedImagesHint = view.findViewById(R.id.ibCloseUnusedImagesHint);
etBody = view.findViewById(R.id.etBody);
tvNoInternet = view.findViewById(R.id.tvNoInternet);
tvSignature = view.findViewById(R.id.tvSignature);
cbSignature = view.findViewById(R.id.cbSignature);
tvReference = view.findViewById(R.id.tvReference);
ibCloseRefHint = view.findViewById(R.id.ibCloseRefHint);
ibReferenceEdit = view.findViewById(R.id.ibReferenceEdit);
ibReferenceImages = view.findViewById(R.id.ibReferenceImages);
style_bar = view.findViewById(R.id.style_bar);
media_bar = view.findViewById(R.id.media_bar);
bottom_navigation = view.findViewById(R.id.bottom_navigation);
pbWait = view.findViewById(R.id.pbWait);
grpHeader = view.findViewById(R.id.grpHeader);
grpExtra = view.findViewById(R.id.grpExtra);
grpAddresses = view.findViewById(R.id.grpAddresses);
grpAttachments = view.findViewById(R.id.grpAttachments);
grpBody = view.findViewById(R.id.grpBody);
grpUnusedImagesHint = view.findViewById(R.id.grpUnusedImagesHint);
grpSignature = view.findViewById(R.id.grpSignature);
grpReferenceHint = view.findViewById(R.id.grpReferenceHint);
resolver = getContext().getContentResolver();
// Wire controls
spIdentity.setOnItemSelectedListener(identitySelected);
View.OnTouchListener onTouchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
EditText et = (EditText) v;
int sstart = et.getSelectionStart();
int send = et.getSelectionEnd();
if (sstart == send && event.getAction() == MotionEvent.ACTION_DOWN) {
float x = event.getX() + et.getScrollX();
float y = event.getY() + et.getScrollY();
int pos = et.getOffsetForPosition(x, y);
if (pos >= 0)
et.setSelection(pos);
}
return false;
}
};
View.OnLongClickListener longClickListener = new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
EditText et = (EditText) v;
int sstart = et.getSelectionStart();
int send = et.getSelectionEnd();
String text = et.getText().toString();
if (send < 0 || send > sstart)
return false;
int ecomma = text.indexOf(',', sstart);
if (ecomma < 0)
return false;
int scomma = text.substring(0, ecomma).lastIndexOf(',');
scomma = (scomma < 0 ? 0 : scomma + 1);
et.setSelection(scomma, ecomma + 1);
return false;
}
};
etTo.setMaxLines(Integer.MAX_VALUE);
etTo.setHorizontallyScrolling(false);
etTo.setOnTouchListener(onTouchListener);
etTo.setOnLongClickListener(longClickListener);
etCc.setMaxLines(Integer.MAX_VALUE);
etCc.setHorizontallyScrolling(false);
etCc.setOnTouchListener(onTouchListener);
etCc.setOnLongClickListener(longClickListener);
etBcc.setMaxLines(Integer.MAX_VALUE);
etBcc.setHorizontallyScrolling(false);
etBcc.setOnTouchListener(onTouchListener);
etBcc.setOnLongClickListener(longClickListener);
etSubject.setMaxLines(Integer.MAX_VALUE);
etSubject.setHorizontallyScrolling(false);
ibCcBcc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onMenuAddresses();
}
});
ibCcBcc.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
onMenuAddresses();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
prefs.edit().putBoolean("cc_bcc", grpAddresses.getVisibility() == View.VISIBLE).apply();
return true;
}
});
View.OnClickListener onPick = new View.OnClickListener() {
@Override
public void onClick(View view) {
int request;
switch (view.getId()) {
case R.id.ivToAdd:
request = REQUEST_CONTACT_TO;
break;
case R.id.ivCcAdd:
request = REQUEST_CONTACT_CC;
break;
case R.id.ivBccAdd:
request = REQUEST_CONTACT_BCC;
break;
default:
return;
}
Intent pick = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Email.CONTENT_URI);
if (pick.resolveActivity(getContext().getPackageManager()) == null)
Snackbar.make(view, R.string.title_no_contacts, Snackbar.LENGTH_LONG).show();
else
startActivityForResult(Helper.getChooser(getContext(), pick), request);
}
};
ibToAdd.setOnClickListener(onPick);
ibCcAdd.setOnClickListener(onPick);
ibBccAdd.setOnClickListener(onPick);
setZoom();
ibCloseUnusedImagesHint.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
prefs.edit().putBoolean("inline_image_hint", false).apply();
grpUnusedImagesHint.setVisibility(View.GONE);
}
});
etBody.setInputContentListener(new EditTextCompose.IInputContentListener() {
@Override
public void onInputContent(Uri uri) {
onAddAttachment(Arrays.asList(uri), true, 0);
}
});
etBody.setSelectionListener(new EditTextCompose.ISelection() {
@Override
public void onSelected(boolean selection) {
style_bar.setVisibility(selection ? View.VISIBLE : View.GONE);
}
});
etBody.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// Do nothing
}
@Override
public void onTextChanged(CharSequence text, int start, int before, int count) {
Activity activity = getActivity();
if (activity != null)
activity.onUserInteraction();
if (before == 0 && count == 1 && start > 0 && text.charAt(start) == '\n') {
// break block quotes
boolean broken = false;
SpannableStringBuilder ssb = new SpannableStringBuilder(text);
StyledQuoteSpan[] spans = ssb.getSpans(start + 1, start + 1, StyledQuoteSpan.class);
for (StyledQuoteSpan span : spans) {
int s = ssb.getSpanStart(span);
int e = ssb.getSpanEnd(span);
int f = ssb.getSpanFlags(span);
Log.i("Span " + s + "..." + e + " start=" + start);
if (s > 0 && start - s > 0 && e - (start + 1) > 0 &&
ssb.charAt(s - 1) == '\n' && ssb.charAt(start - 1) == '\n' &&
ssb.charAt(start) == '\n' && ssb.charAt(e - 1) == '\n') {
broken = true;
StyledQuoteSpan q1 = new StyledQuoteSpan(getContext(), span.getColor());
ssb.setSpan(q1, s, start, f);
Log.i("Span " + s + "..." + start);
StyledQuoteSpan q2 = new StyledQuoteSpan(getContext(), span.getColor());
ssb.setSpan(q2, start + 1, e, f);
Log.i("Span " + (start + 1) + "..." + e);
ssb.removeSpan(span);
}
}
if (broken) {
StyleSpan[] sspan = ssb.getSpans(start, start, StyleSpan.class);
for (StyleSpan span : sspan) {
int s = ssb.getSpanStart(span);
int e = ssb.getSpanEnd(span);
int f = ssb.getSpanFlags(span);
Log.i("Style span " + s + "..." + e + " start=" + start);
StyleSpan s1 = new StyleSpan(span.getStyle());
ssb.setSpan(s1, s, start, f);
Log.i("Style span " + s + "..." + start);
StyleSpan s2 = new StyleSpan(span.getStyle());
ssb.setSpan(s2, start + 1, e, f);
Log.i("Style span " + (start + 1) + "..." + e);
ssb.removeSpan(span);
}
int color = Helper.resolveColor(getContext(), android.R.attr.textColorPrimary);
int flags = (Spanned.SPAN_INCLUSIVE_INCLUSIVE | Spanned.SPAN_COMPOSING);
ssb.setSpan(new ForegroundColorSpan(color), start, start, flags);
etBody.setText(ssb);
etBody.setSelection(start);
}
}
}
@Override
public void afterTextChanged(Editable s) {
// Do nothing
}
});
cbSignature.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
Object tag = cbSignature.getTag();
if (tag == null || !tag.equals(checked)) {
cbSignature.setTag(checked);
if (tag != null)
onAction(R.id.action_save, "signature");
}
}
});
ibCloseRefHint.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
prefs.edit().putBoolean("compose_reference", false).apply();
grpReferenceHint.setVisibility(View.GONE);
}
});
ibReferenceEdit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onReferenceEdit();
}
});
ibReferenceImages.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ibReferenceImages.setVisibility(View.GONE);
onReferenceImages();
}
});
etBody.setTypeface(monospaced ? Typeface.MONOSPACE : Typeface.DEFAULT);
tvReference.setTypeface(monospaced ? Typeface.MONOSPACE : Typeface.DEFAULT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
etBody.setRevealOnFocusHint(false); // Doesn't work
tvSignature.setRevealOnFocusHint(false);
tvReference.setRevealOnFocusHint(false);
}
style_bar.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
return onActionStyle(item.getItemId());
}
});
media_bar.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int action = item.getItemId();
switch (action) {
case R.id.menu_record_audio:
onActionRecordAudio();
return true;
case R.id.menu_take_photo:
onActionImage(true);
return true;
case R.id.menu_image:
onActionImage(false);
return true;
case R.id.menu_attachment:
onActionAttachment();
return true;
case R.id.menu_link:
onActionLink();
return true;
default:
return false;
}
}
});
setCompact(compact);
bottom_navigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
final int action = item.getItemId();
switch (action) {
case R.id.action_delete:
onActionDiscard();
break;
case R.id.action_send:
onAction(R.id.action_check, "check");
break;
default:
onAction(action, "navigation");
}
return true;
}
});
addKeyPressedListener(onKeyPressedListener);
// Initialize
setHasOptionsMenu(true);
etExtra.setHint("");
tvDomain.setText(null);
etBody.setText(null);
grpHeader.setVisibility(View.GONE);
grpExtra.setVisibility(View.GONE);
ibCcBcc.setVisibility(View.GONE);
grpAttachments.setVisibility(View.GONE);
tvNoInternet.setVisibility(View.GONE);
grpBody.setVisibility(View.GONE);
grpSignature.setVisibility(View.GONE);
grpReferenceHint.setVisibility(View.GONE);
ibReferenceEdit.setVisibility(View.GONE);
ibReferenceImages.setVisibility(View.GONE);
tvReference.setVisibility(View.GONE);
style_bar.setVisibility(View.GONE);
media_bar.setVisibility(View.GONE);
bottom_navigation.setVisibility(View.GONE);
pbWait.setVisibility(View.VISIBLE);
getActivity().invalidateOptionsMenu();
Helper.setViewsEnabled(view, false);
final DB db = DB.getInstance(getContext());
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
final boolean suggest_sent = prefs.getBoolean("suggest_sent", true);
final boolean suggest_received = prefs.getBoolean("suggest_received", false);
final boolean suggest_frequently = prefs.getBoolean("suggest_frequently", false);
final boolean cc_bcc = prefs.getBoolean("cc_bcc", false);
final boolean circular = prefs.getBoolean("circular", true);
final float dp3 = Helper.dp2pixels(getContext(), 3);
SimpleCursorAdapter cadapter = new SimpleCursorAdapter(
getContext(),
R.layout.spinner_contact,
null,
new String[]{"name", "email", "photo"},
new int[]{R.id.tvName, R.id.tvEmail, R.id.ivPhoto},
0);
cadapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
private int colName = -1;
private int colLocal = -1;
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
int id = view.getId();
if (id == R.id.tvName) {
if (colName < 0)
colName = cursor.getColumnIndex("name");
if (cursor.isNull(colName)) {
((TextView) view).setText("-");
return true;
}
} else if (id == R.id.ivPhoto) {
if (colLocal < 0)
colLocal = cursor.getColumnIndex("local");
ImageView photo = (ImageView) view;
GradientDrawable bg = new GradientDrawable();
if (circular)
bg.setShape(GradientDrawable.OVAL);
else
bg.setCornerRadius(dp3);
photo.setBackground(bg);
photo.setClipToOutline(true);
if (cursor.getInt(colLocal) == 1)
photo.setImageDrawable(null);
else {
String uri = cursor.getString(columnIndex);
if (uri == null)
photo.setImageResource(R.drawable.baseline_person_24);
else
photo.setImageURI(Uri.parse(uri));
}
return true;
}
return false;
}
});
cadapter.setCursorToStringConverter(new SimpleCursorAdapter.CursorToStringConverter() {
private int colName = -1;
private int colEmail = -1;
public CharSequence convertToString(Cursor cursor) {
if (colName < 0)
colName = cursor.getColumnIndex("name");
if (colEmail < 0)
colEmail = cursor.getColumnIndex("email");
String name = cursor.getString(colName);
String email = MessageHelper.sanitizeEmail(cursor.getString(colEmail));
StringBuilder sb = new StringBuilder();
if (name == null)
sb.append(email);
else {
sb.append("\"").append(name).append("\" ");
sb.append("<").append(email).append(">");
}
return sb.toString();
}
});
cadapter.setFilterQueryProvider(new FilterQueryProvider() {
public Cursor runQuery(CharSequence typed) {
Log.i("Suggest contact=" + typed);
MatrixCursor result = new MatrixCursor(new String[]{"_id", "name", "email", "photo", "local"});
if (typed == null)
return result;
String wildcard = "%" + typed + "%";
Map map = new HashMap<>();
boolean contacts = Helper.hasPermission(getContext(), Manifest.permission.READ_CONTACTS);
if (contacts) {
Cursor cursor = resolver.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
new String[]{
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Email.DATA,
ContactsContract.Contacts.PHOTO_THUMBNAIL_URI
},
ContactsContract.CommonDataKinds.Email.DATA + " <> ''" +
" AND (" + ContactsContract.Contacts.DISPLAY_NAME + " LIKE ?" +
" OR " + ContactsContract.CommonDataKinds.Email.DATA + " LIKE ?)",
new String[]{wildcard, wildcard},
null);
while (cursor != null && cursor.moveToNext()) {
EntityContact item = new EntityContact();
item.id = 0L;
item.name = cursor.getString(0);
item.email = cursor.getString(1);
item.avatar = cursor.getString(2);
item.times_contacted = 0;
item.last_contacted = 0L;
EntityContact existing = map.get(item.email);
if (existing == null ||
(existing.avatar == null && item.avatar != null))
map.put(item.email, item);
}
}
List items = new ArrayList<>();
if (suggest_sent)
items.addAll(db.contact().searchContacts(null, EntityContact.TYPE_TO, wildcard));
if (suggest_received)
items.addAll(db.contact().searchContacts(null, EntityContact.TYPE_FROM, wildcard));
for (EntityContact item : items) {
EntityContact existing = map.get(item.email);
if (existing == null)
map.put(item.email, item);
else {
existing.times_contacted = Math.max(existing.times_contacted, item.times_contacted);
existing.last_contacted = Math.max(existing.last_contacted, item.last_contacted);
}
}
items = new ArrayList<>(map.values());
final Collator collator = Collator.getInstance(Locale.getDefault());
collator.setStrength(Collator.SECONDARY); // Case insensitive, process accents etc
Collections.sort(items, new Comparator() {
@Override
public int compare(EntityContact i1, EntityContact i2) {
try {
if (suggest_frequently) {
int t = -i1.times_contacted.compareTo(i2.times_contacted);
if (t != 0)
return t;
int l = -i1.last_contacted.compareTo(i2.last_contacted);
if (l != 0)
return l;
} else {
int a = -Boolean.compare(i1.id == 0, i2.id == 0);
if (a != 0)
return a;
}
if (TextUtils.isEmpty(i1.name) && TextUtils.isEmpty(i2.name))
return 0;
if (TextUtils.isEmpty(i1.name) && !TextUtils.isEmpty(i2.name))
return 1;
if (!TextUtils.isEmpty(i1.name) && TextUtils.isEmpty(i2.name))
return -1;
int n = collator.compare(i1.name, i2.name);
if (n != 0)
return n;
return collator.compare(i1.email, i2.email);
} catch (Throwable ex) {
Log.e(ex);
return 0;
}
}
});
for (int i = 0; i < items.size(); i++) {
EntityContact item = items.get(i);
result.newRow()
.add(i + 1) // id
.add(item.name)
.add(item.email)
.add(item.avatar)
.add(item.id == 0 ? 0 : 1);
}
return result;
}
});
etTo.setAdapter(cadapter);
etCc.setAdapter(cadapter);
etBcc.setAdapter(cadapter);
etTo.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
etCc.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
etBcc.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
grpAddresses.setVisibility(cc_bcc ? View.VISIBLE : View.GONE);
rvAttachment.setHasFixedSize(false);
LinearLayoutManager llm = new LinearLayoutManager(getContext());
rvAttachment.setLayoutManager(llm);
rvAttachment.setItemAnimator(null);
adapter = new AdapterAttachment(this, false);
rvAttachment.setAdapter(adapter);
tvNoInternetAttachments.setVisibility(View.GONE);
grpUnusedImagesHint.setVisibility(View.GONE);
String pkg = Helper.getOpenKeychainPackage(getContext());
Log.i("Binding to " + pkg);
pgpService = new OpenPgpServiceConnection(getContext(), pkg);
pgpService.bindToService();
return view;
}
private void onReferenceEdit() {
PopupMenuLifecycle popupMenu = new PopupMenuLifecycle(getContext(), getViewLifecycleOwner(), ibReferenceEdit);
popupMenu.getMenu().add(Menu.NONE, R.string.title_edit_plain_text, 1, R.string.title_edit_plain_text);
popupMenu.getMenu().add(Menu.NONE, R.string.title_edit_formatted_text, 2, R.string.title_edit_formatted_text);
popupMenu.getMenu().add(Menu.NONE, R.string.title_delete, 3, R.string.title_delete);
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.string.title_edit_plain_text:
convertRef(true);
return true;
case R.string.title_edit_formatted_text:
convertRef(false);
return true;
case R.string.title_delete:
deleteRef();
return true;
default:
return false;
}
}
private void convertRef(boolean plain) {
Bundle args = new Bundle();
args.putLong("id", working);
args.putBoolean("plain", plain);
args.putString("body", HtmlHelper.toHtml(etBody.getText()));
new SimpleTask() {
@Override
protected void onPreExecute(Bundle args) {
ibReferenceEdit.setEnabled(false);
}
@Override
protected void onPostExecute(Bundle args) {
ibReferenceEdit.setEnabled(true);
}
@Override
protected String onExecute(Context context, Bundle args) throws Throwable {
long id = args.getLong("id");
boolean plain = args.getBoolean("plain");
String body = args.getString("body");
File rfile = EntityMessage.getFile(context, id);
Document doc = JsoupEx.parse(rfile);
Elements ref = doc.select("div[fairemail=reference]");
ref.removeAttr("fairemail");
Document document = JsoupEx.parse(body);
if (plain) {
String text = HtmlHelper.getText(ref.outerHtml());
String[] line = text.split("\\r?\\n");
for (int i = 0; i < line.length; i++)
line[i] = Html.escapeHtml(line[i]);
Element p = document.createElement("p");
p.html(TextUtils.join("
", line));
document.body().appendChild(p);
} else {
Document d = HtmlHelper.sanitizeCompose(context, ref.outerHtml(), true);
Element b = d.body();
b.tagName("div");
document.body().appendChild(b);
}
return document.html();
}
@Override
protected void onExecuted(Bundle args, String html) {
Bundle extras = new Bundle();
extras.putString("html", html);
extras.putBoolean("show", true);
onAction(R.id.action_save, extras, "refedit");
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(getParentFragmentManager(), ex);
}
}.execute(FragmentCompose.this, args, "compose:convert");
}
private void deleteRef() {
Bundle extras = new Bundle();
extras.putString("html", HtmlHelper.toHtml(etBody.getText()));
extras.putBoolean("show", true);
onAction(R.id.action_save, extras, "refdelete");
}
});
popupMenu.show();
}
private void onReferenceImages() {
show_images = true;
Bundle extras = new Bundle();
extras.putBoolean("show", true);
onAction(R.id.action_save, extras, "refimages");
}
@Override
public void onDestroyView() {
adapter = null;
if (pgpService != null && pgpService.isBound())
pgpService.unbindFromService();
super.onDestroyView();
}
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putLong("fair:working", working);
outState.putBoolean("fair:show_images", show_images);
outState.putParcelable("fair:photo", photoURI);
super.onSaveInstanceState(outState);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
state = State.NONE;
if (savedInstanceState == null) {
if (working < 0) {
Bundle a = getArguments();
if (a == null) {
a = new Bundle();
a.putString("action", "new");
setArguments(a);
}
Bundle args = new Bundle();
args.putString("action", a.getString("action"));
args.putLong("id", a.getLong("id", -1));
args.putLong("account", a.getLong("account", -1));
args.putLong("identity", a.getLong("identity", -1));
args.putLong("reference", a.getLong("reference", -1));
args.putSerializable("ics", a.getSerializable("ics"));
args.putString("status", a.getString("status"));
args.putBoolean("raw", a.getBoolean("raw", false));
args.putLong("answer", a.getLong("answer", -1));
args.putString("to", a.getString("to"));
args.putString("cc", a.getString("cc"));
args.putString("bcc", a.getString("bcc"));
args.putString("subject", a.getString("subject"));
args.putString("body", a.getString("body"));
args.putString("text", a.getString("text"));
args.putString("selected", a.getString("selected"));
args.putParcelableArrayList("attachments", a.getParcelableArrayList("attachments"));
draftLoader.execute(this, args, "compose:new");
} else {
Bundle args = new Bundle();
args.putString("action", "edit");
args.putLong("id", working);
draftLoader.execute(this, args, "compose:edit");
}
} else {
working = savedInstanceState.getLong("fair:working");
show_images = savedInstanceState.getBoolean("fair:show_images");
photoURI = savedInstanceState.getParcelable("fair:photo");
Bundle args = new Bundle();
args.putString("action", working < 0 ? "new" : "edit");
args.putLong("id", working);
draftLoader.execute(this, args, "compose:instance");
}
}
@Override
public void onResume() {
super.onResume();
ConnectivityManager cm = (ConnectivityManager) getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkRequest.Builder builder = new NetworkRequest.Builder();
builder.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
cm.registerNetworkCallback(builder.build(), networkCallback);
if (!pgpService.isBound())
pgpService.bindToService();
}
@Override
public void onPause() {
if (state == State.LOADED) {
Bundle extras = new Bundle();
extras.putBoolean("autosave", true);
onAction(R.id.action_save, extras, "pause");
}
ConnectivityManager cm = (ConnectivityManager) getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
cm.unregisterNetworkCallback(networkCallback);
super.onPause();
}
ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(Network network) {
check();
}
@Override
public void onCapabilitiesChanged(Network network, NetworkCapabilities networkCapabilities) {
check();
}
@Override
public void onLost(Network network) {
check();
}
private void check() {
Activity activity = getActivity();
if (activity != null)
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED))
checkInternet();
}
});
}
};
private void checkInternet() {
boolean suitable = ConnectionHelper.getNetworkState(getContext()).isSuitable();
Boolean content = (Boolean) tvNoInternet.getTag();
tvNoInternet.setVisibility(!suitable && content != null && !content ? View.VISIBLE : View.GONE);
Boolean downloading = (Boolean) rvAttachment.getTag();
tvNoInternetAttachments.setVisibility(!suitable && downloading != null && downloading ? View.VISIBLE : View.GONE);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_compose, menu);
menu.findItem(R.id.menu_encrypt).setActionView(R.layout.action_button);
ImageButton ib = (ImageButton) menu.findItem(R.id.menu_encrypt).getActionView();
ib.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onMenuEncrypt();
}
});
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
menu.findItem(R.id.menu_encrypt).setEnabled(state == State.LOADED);
menu.findItem(R.id.menu_zoom).setEnabled(state == State.LOADED);
menu.findItem(R.id.menu_media).setEnabled(state == State.LOADED);
menu.findItem(R.id.menu_compact).setEnabled(state == State.LOADED);
menu.findItem(R.id.menu_contact_group).setEnabled(
state == State.LOADED && hasPermission(Manifest.permission.READ_CONTACTS));
menu.findItem(R.id.menu_answer).setEnabled(state == State.LOADED);
menu.findItem(R.id.menu_clear).setEnabled(state == State.LOADED);
int colorEncrypt = Helper.resolveColor(getContext(), R.attr.colorEncrypt);
ImageButton ib = (ImageButton) menu.findItem(R.id.menu_encrypt).getActionView();
ib.setEnabled(state == State.LOADED);
if (EntityMessage.PGP_SIGNONLY.equals(encrypt) || EntityMessage.SMIME_SIGNONLY.equals(encrypt)) {
ib.setImageResource(R.drawable.baseline_gesture_24);
ib.setImageTintList(null);
} else if (EntityMessage.PGP_SIGNENCRYPT.equals(encrypt) || EntityMessage.SMIME_SIGNENCRYPT.equals(encrypt)) {
ib.setImageResource(R.drawable.baseline_lock_24);
ib.setImageTintList(ColorStateList.valueOf(colorEncrypt));
} else {
ib.setImageResource(R.drawable.baseline_lock_open_24);
ib.setImageTintList(null);
}
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
boolean send_dialog = prefs.getBoolean("send_dialog", true);
boolean image_dialog = prefs.getBoolean("image_dialog", true);
menu.findItem(R.id.menu_send_dialog).setChecked(send_dialog);
menu.findItem(R.id.menu_image_dialog).setChecked(image_dialog);
menu.findItem(R.id.menu_media).setChecked(media);
menu.findItem(R.id.menu_compact).setChecked(compact);
if (EntityMessage.PGP_SIGNONLY.equals(encrypt) ||
EntityMessage.SMIME_SIGNONLY.equals(encrypt))
bottom_navigation.getMenu().findItem(R.id.action_send).setTitle(R.string.title_sign);
else if (EntityMessage.PGP_SIGNENCRYPT.equals(encrypt) ||
EntityMessage.SMIME_SIGNENCRYPT.equals(encrypt))
bottom_navigation.getMenu().findItem(R.id.action_send).setTitle(R.string.title_encrypt);
else
bottom_navigation.getMenu().findItem(R.id.action_send).setTitle(R.string.title_send);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_encrypt:
onMenuEncrypt();
return true;
case R.id.menu_zoom:
onMenuZoom();
return true;
case R.id.menu_send_dialog:
onMenuSendDialog();
return true;
case R.id.menu_image_dialog:
onMenuImageDialog();
return true;
case R.id.menu_media:
onMenuMediaBar();
return true;
case R.id.menu_compact:
onMenuCompact();
return true;
case R.id.menu_contact_group:
onMenuContactGroup();
return true;
case R.id.menu_answer:
onMenuAnswer();
return true;
case R.id.menu_clear:
StyleHelper.apply(R.id.menu_clear, etBody);
return true;
case R.id.menu_legend:
onMenuLegend();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void onMenuAddresses() {
grpAddresses.setVisibility(grpAddresses.getVisibility() == View.GONE ? View.VISIBLE : View.GONE);
new Handler().post(new Runnable() {
@Override
public void run() {
if (grpAddresses.getVisibility() == View.GONE)
etSubject.requestFocus();
else
etCc.requestFocus();
}
});
}
private void onMenuEncrypt() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
String encrypt_method = prefs.getString("default_encrypt_method", "pgp");
if ("pgp".equals(encrypt_method)) {
if (EntityMessage.ENCRYPT_NONE.equals(encrypt) || encrypt == null)
encrypt = EntityMessage.PGP_SIGNENCRYPT;
else if (EntityMessage.PGP_SIGNENCRYPT.equals(encrypt))
encrypt = EntityMessage.PGP_SIGNONLY;
else
encrypt = EntityMessage.ENCRYPT_NONE;
} else {
if (EntityMessage.ENCRYPT_NONE.equals(encrypt) || encrypt == null)
encrypt = EntityMessage.SMIME_SIGNENCRYPT;
else if (EntityMessage.SMIME_SIGNENCRYPT.equals(encrypt))
encrypt = EntityMessage.SMIME_SIGNONLY;
else
encrypt = EntityMessage.ENCRYPT_NONE;
}
getActivity().invalidateOptionsMenu();
Bundle args = new Bundle();
args.putLong("id", working);
args.putInt("encrypt", encrypt);
new SimpleTask() {
@Override
protected Void onExecute(Context context, Bundle args) {
long id = args.getLong("id");
int encrypt = args.getInt("encrypt");
DB db = DB.getInstance(context);
if (EntityMessage.ENCRYPT_NONE.equals(encrypt))
db.message().setMessageUiEncrypt(id, null);
else
db.message().setMessageUiEncrypt(id, encrypt);
return null;
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(getParentFragmentManager(), ex);
}
}.execute(this, args, "compose:encrypt");
}
private void onMenuZoom() {
zoom = ++zoom % 3;
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
prefs.edit().putInt("compose_zoom", zoom).apply();
setZoom();
}
private void setZoom() {
float textSize = Helper.getTextSize(getContext(), zoom);
if (textSize != 0) {
etBody.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
tvReference.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
}
}
private void onMenuSendDialog() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
boolean send_dialog = prefs.getBoolean("send_dialog", true);
prefs.edit().putBoolean("send_dialog", !send_dialog).apply();
}
private void onMenuImageDialog() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
boolean image_dialog = prefs.getBoolean("image_dialog", true);
prefs.edit().putBoolean("image_dialog", !image_dialog).apply();
}
private void onMenuMediaBar() {
media = !media;
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
prefs.edit().putBoolean("compose_media", media).apply();
media_bar.setVisibility(media ? View.VISIBLE : View.GONE);
}
private void onMenuCompact() {
compact = !compact;
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
prefs.edit().putBoolean("compose_compact", compact).apply();
setCompact(compact);
}
private void setCompact(boolean compact) {
bottom_navigation.setLabelVisibilityMode(compact
? LabelVisibilityMode.LABEL_VISIBILITY_UNLABELED
: LabelVisibilityMode.LABEL_VISIBILITY_LABELED);
ViewGroup.LayoutParams params = bottom_navigation.getLayoutParams();
params.height = Helper.dp2pixels(view.getContext(), compact ? 36 : 56);
bottom_navigation.setLayoutParams(params);
}
private void onMenuLegend() {
if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED))
getParentFragmentManager().popBackStack("legend", FragmentManager.POP_BACK_STACK_INCLUSIVE);
Bundle args = new Bundle();
args.putString("tab", "compose");
Fragment fragment = new FragmentLegend();
fragment.setArguments(args);
FragmentTransaction fragmentTransaction = getParentFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content_frame, fragment).addToBackStack("legend");
fragmentTransaction.commit();
}
private void onMenuContactGroup() {
Bundle args = new Bundle();
args.putLong("working", working);
int focussed = 0;
View v = view.findFocus();
if (v != null) {
if (v.getId() == R.id.etCc)
focussed = 1;
else if (v.getId() == R.id.etBcc)
focussed = 2;
}
args.putInt("focussed", focussed);
FragmentDialogContactGroup fragment = new FragmentDialogContactGroup();
fragment.setArguments(args);
fragment.setTargetFragment(this, REQUEST_CONTACT_GROUP);
fragment.show(getParentFragmentManager(), "compose:groups");
}
private void onMenuAnswer() {
if (!ActivityBilling.isPro(getContext())) {
startActivity(new Intent(getContext(), ActivityBilling.class));
return;
}
FragmentDialogAnswer fragment = new FragmentDialogAnswer();
fragment.setArguments(new Bundle());
fragment.setTargetFragment(this, REQUEST_ANSWER);
fragment.show(getParentFragmentManager(), "compose:answer");
}
private boolean onActionStyle(int action) {
Log.i("Style action=" + action);
if (action == R.id.menu_color) {
InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Activity.INPUT_METHOD_SERVICE);
if (imm != null)
imm.hideSoftInputFromWindow(etBody.getWindowToken(), 0);
Bundle args = new Bundle();
args.putInt("color", Color.TRANSPARENT);
args.putString("title", getString(R.string.title_style_color));
args.putInt("start", etBody.getSelectionStart());
args.putInt("end", etBody.getSelectionEnd());
FragmentDialogColor fragment = new FragmentDialogColor();
fragment.setArguments(args);
fragment.setTargetFragment(FragmentCompose.this, REQUEST_COLOR);
fragment.show(getParentFragmentManager(), "account:color");
return true;
} else
return StyleHelper.apply(action, etBody);
}
private void onActionRecordAudio() {
// https://developer.android.com/reference/android/provider/MediaStore.Audio.Media.html#RECORD_SOUND_ACTION
PackageManager pm = getContext().getPackageManager();
Intent intent = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
if (intent.resolveActivity(pm) == null) {
Snackbar snackbar = Snackbar.make(view, getString(R.string.title_no_recorder), Snackbar.LENGTH_INDEFINITE);
snackbar.setAction(R.string.title_fix, new View.OnClickListener() {
@Override
public void onClick(View v) {
Helper.view(getContext(), Uri.parse(BuildConfig.RECORDER_URI), false);
}
});
snackbar.show();
} else
try {
startActivityForResult(intent, REQUEST_RECORD_AUDIO);
} catch (SecurityException ex) {
Log.w(ex);
Snackbar.make(view, getString(R.string.title_no_viewer, intent.getAction()), Snackbar.LENGTH_INDEFINITE).show();
}
}
private void onActionImage(boolean photo) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
boolean image_dialog = prefs.getBoolean("image_dialog", true);
if (image_dialog) {
Bundle args = new Bundle();
args.putBoolean("photo", photo);
FragmentDialogAddImage fragment = new FragmentDialogAddImage();
fragment.setArguments(args);
fragment.setTargetFragment(this, REQUEST_IMAGE);
fragment.show(getParentFragmentManager(), "compose:image");
} else
onAddImage(photo);
}
private void onActionAttachment() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
PackageManager pm = getContext().getPackageManager();
if (intent.resolveActivity(pm) == null)
noStorageAccessFramework();
else
startActivityForResult(Helper.getChooser(getContext(), intent), REQUEST_ATTACHMENT);
}
private void noStorageAccessFramework() {
Snackbar snackbar = Snackbar.make(view, R.string.title_no_saf, Snackbar.LENGTH_LONG);
snackbar.setAction(R.string.title_fix, new View.OnClickListener() {
@Override
public void onClick(View v) {
Helper.viewFAQ(getContext(), 25);
}
});
snackbar.show();
}
private void onActionLink() {
Uri uri = null;
if (etBody.hasSelection()) {
int start = etBody.getSelectionStart();
URLSpan[] spans = etBody.getText().getSpans(start, start, URLSpan.class);
if (spans.length > 0) {
String url = spans[0].getURL();
if (url != null) {
uri = Uri.parse(url);
if (uri.getScheme() == null)
uri = null;
}
}
}
if (uri == null) {
ClipboardManager cbm = (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE);
if (cbm != null && cbm.hasPrimaryClip()) {
String link = cbm.getPrimaryClip().getItemAt(0).coerceToText(getContext()).toString();
uri = Uri.parse(link);
if (uri.getScheme() == null)
uri = null;
}
}
Bundle args = new Bundle();
args.putParcelable("uri", uri);
FragmentDialogLink fragment = new FragmentDialogLink();
fragment.setArguments(args);
fragment.setTargetFragment(this, REQUEST_LINK);
fragment.show(getParentFragmentManager(), "compose:link");
}
private void onActionDiscard() {
if (isEmpty())
onAction(R.id.action_delete, "discard");
else {
Bundle args = new Bundle();
args.putString("question", getString(R.string.title_ask_discard));
FragmentDialogAsk fragment = new FragmentDialogAsk();
fragment.setArguments(args);
fragment.setTargetFragment(this, REQUEST_DISCARD);
fragment.show(getParentFragmentManager(), "compose:discard");
}
}
private void onEncrypt(final EntityMessage draft, final int action, final boolean interactive) {
if (EntityMessage.SMIME_SIGNONLY.equals(draft.ui_encrypt) ||
EntityMessage.SMIME_SIGNENCRYPT.equals(draft.ui_encrypt)) {
Bundle args = new Bundle();
args.putLong("id", draft.id);
args.putInt("type", draft.ui_encrypt);
new SimpleTask() {
@Override
protected EntityIdentity onExecute(Context context, Bundle args) throws KeyChainException, InterruptedException {
long id = args.getLong("id");
DB db = DB.getInstance(context);
EntityMessage draft = db.message().getMessage(id);
if (draft == null || draft.identity == null)
return null;
EntityIdentity identity = db.identity().getIdentity(draft.identity);
if (identity != null && identity.sign_key_alias != null)
try {
PrivateKey key = KeyChain.getPrivateKey(context, identity.sign_key_alias);
args.putBoolean("available", key != null);
} catch (Throwable ex) {
Log.w(ex);
}
return identity;
}
@Override
protected void onExecuted(final Bundle args, EntityIdentity identity) {
if (identity == null)
return;
boolean available = args.getBoolean("available");
if (available) {
args.putString("alias", identity.sign_key_alias);
onSmime(args, action);
return;
}
if (interactive)
Helper.selectKeyAlias(getActivity(), getViewLifecycleOwner(), identity.sign_key_alias, new Helper.IKeyAlias() {
@Override
public void onSelected(String alias) {
args.putString("alias", alias);
if (alias != null)
onSmime(args, action);
}
@Override
public void onNothingSelected() {
Snackbar snackbar = Snackbar.make(view, R.string.title_no_key, Snackbar.LENGTH_LONG);
final Intent intent = KeyChain.createInstallIntent();
if (intent.resolveActivity(getContext().getPackageManager()) != null)
snackbar.setAction(R.string.title_fix, new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(intent);
}
});
snackbar.show();
}
});
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(getParentFragmentManager(), ex);
}
}.execute(this, args, "compose:alias");
} else {
if (pgpService.isBound())
try {
List recipients = new ArrayList<>();
if (draft.from != null)
recipients.addAll(Arrays.asList(draft.from));
if (draft.to != null)
recipients.addAll(Arrays.asList(draft.to));
if (draft.cc != null)
recipients.addAll(Arrays.asList(draft.cc));
if (draft.bcc != null)
recipients.addAll(Arrays.asList(draft.bcc));
if (recipients.size() == 0)
throw new IllegalArgumentException(getString(R.string.title_to_missing));
pgpUserIds = new String[recipients.size()];
for (int i = 0; i < recipients.size(); i++) {
InternetAddress recipient = (InternetAddress) recipients.get(i);
pgpUserIds[i] = recipient.getAddress().toLowerCase();
}
Intent intent;
if (EntityMessage.PGP_SIGNONLY.equals(draft.ui_encrypt))
intent = new Intent(OpenPgpApi.ACTION_GET_SIGN_KEY_ID);
else if (EntityMessage.PGP_SIGNENCRYPT.equals(draft.ui_encrypt)) {
intent = new Intent(OpenPgpApi.ACTION_GET_KEY_IDS);
intent.putExtra(OpenPgpApi.EXTRA_USER_IDS, pgpUserIds);
} else
throw new IllegalArgumentException("Invalid encrypt=" + draft.ui_encrypt);
Bundle largs = new Bundle();
largs.putLong("id", working);
largs.putInt("action", action);
largs.putBoolean("interactive", interactive);
intent.putExtra(BuildConfig.APPLICATION_ID, largs);
onPgp(intent);
} catch (Throwable ex) {
if (ex instanceof IllegalArgumentException)
Snackbar.make(view, ex.getMessage(), Snackbar.LENGTH_LONG).show();
else {
Log.e(ex);
Log.unexpectedError(getParentFragmentManager(), ex);
}
}
else {
Snackbar snackbar = Snackbar.make(view, R.string.title_no_openpgp, Snackbar.LENGTH_LONG);
if (Helper.getIntentOpenKeychain().resolveActivity(getContext().getPackageManager()) != null)
snackbar.setAction(R.string.title_fix, new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(Helper.getIntentOpenKeychain());
}
});
snackbar.show();
}
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
switch (requestCode) {
case REQUEST_CONTACT_TO:
case REQUEST_CONTACT_CC:
case REQUEST_CONTACT_BCC:
if (resultCode == RESULT_OK && data != null)
onPickContact(requestCode, data);
break;
case REQUEST_IMAGE:
if (resultCode == RESULT_OK)
onAddImage(data.getBundleExtra("args").getBoolean("photo"));
break;
case REQUEST_IMAGE_FILE:
if (resultCode == RESULT_OK && data != null)
onAddImageFile(getUris(data));
break;
case REQUEST_TAKE_PHOTO:
if (resultCode == RESULT_OK) {
if (photoURI != null)
onAddImageFile(Arrays.asList(photoURI));
}
break;
case REQUEST_ATTACHMENT:
case REQUEST_RECORD_AUDIO:
if (resultCode == RESULT_OK && data != null)
onAddAttachment(getUris(data), false, 0);
break;
case REQUEST_OPENPGP:
if (resultCode == RESULT_OK && data != null)
onPgp(data);
break;
case REQUEST_CONTACT_GROUP:
if (resultCode == RESULT_OK && data != null)
onContactGroupSelected(data.getBundleExtra("args"));
break;
case REQUEST_ANSWER:
if (resultCode == RESULT_OK && data != null)
onAnswerSelected(data.getBundleExtra("args"));
break;
case REQUEST_COLOR:
if (resultCode == RESULT_OK && data != null)
onColorSelected(data.getBundleExtra("args"));
break;
case REQUEST_LINK:
if (resultCode == RESULT_OK && data != null)
onLinkSelected(data.getBundleExtra("args"));
break;
case REQUEST_DISCARD:
if (resultCode == RESULT_OK)
onActionDiscardConfirmed();
break;
case REQUEST_SEND:
if (resultCode == RESULT_OK)
onAction(R.id.action_send, "send");
else if (resultCode == RESULT_FIRST_USER) {
Bundle extras = new Bundle();
extras.putBoolean("now", true);
onAction(R.id.action_send, extras, "sendnow");
}
break;
}
} catch (Throwable ex) {
Log.e(ex);
}
}
private void onPickContact(int requestCode, Intent data) {
Uri uri = data.getData();
if (uri == null)
return;
Bundle args = new Bundle();
args.putLong("id", working);
args.putInt("requestCode", requestCode);
args.putParcelable("uri", uri);
new SimpleTask() {
@Override
protected EntityMessage onExecute(Context context, Bundle args) throws Throwable {
long id = args.getLong("id");
int requestCode = args.getInt("requestCode");
Uri uri = args.getParcelable("uri");
EntityMessage draft = null;
DB db = DB.getInstance(context);
try (Cursor cursor = context.getContentResolver().query(
uri,
new String[]{
ContactsContract.CommonDataKinds.Email.ADDRESS,
ContactsContract.Contacts.DISPLAY_NAME
},
null, null, null)) {
if (cursor != null && cursor.moveToFirst()) {
int colEmail = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS);
int colName = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
String email = MessageHelper.sanitizeEmail(cursor.getString(colEmail));
String name = cursor.getString(colName);
try {
db.beginTransaction();
draft = db.message().getMessage(id);
if (draft == null)
return null;
Address[] address = null;
if (requestCode == REQUEST_CONTACT_TO)
address = draft.to;
else if (requestCode == REQUEST_CONTACT_CC)
address = draft.cc;
else if (requestCode == REQUEST_CONTACT_BCC)
address = draft.bcc;
List list = new ArrayList<>();
if (address != null)
list.addAll(Arrays.asList(address));
list.add(new InternetAddress(email, name));
if (requestCode == REQUEST_CONTACT_TO)
draft.to = list.toArray(new Address[0]);
else if (requestCode == REQUEST_CONTACT_CC)
draft.cc = list.toArray(new Address[0]);
else if (requestCode == REQUEST_CONTACT_BCC)
draft.bcc = list.toArray(new Address[0]);
db.message().updateMessage(draft);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
}
}
return draft;
}
@Override
protected void onExecuted(Bundle args, EntityMessage draft) {
if (draft != null) {
etTo.setText(MessageHelper.formatAddressesCompose(draft.to));
etCc.setText(MessageHelper.formatAddressesCompose(draft.cc));
etBcc.setText(MessageHelper.formatAddressesCompose(draft.bcc));
}
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(getParentFragmentManager(), ex);
}
}.execute(this, args, "compose:picked");
}
private void onAddImage(boolean photo) {
if (photo) {
// https://developer.android.com/training/camera/photobasics
PackageManager pm = getContext().getPackageManager();
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(pm) == null) {
Snackbar snackbar = Snackbar.make(view, getString(R.string.title_no_camera), Snackbar.LENGTH_LONG);
snackbar.setAction(R.string.title_fix, new View.OnClickListener() {
@Override
public void onClick(View v) {
Helper.view(getContext(), Uri.parse(BuildConfig.CAMERA_URI), false);
}
});
snackbar.show();
} else {
File dir = new File(getContext().getCacheDir(), "photo");
if (!dir.exists())
dir.mkdir();
File file = new File(dir, working + ".jpg");
try {
photoURI = FileProvider.getUriForFile(getContext(), BuildConfig.APPLICATION_ID, file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(intent, REQUEST_TAKE_PHOTO);
} catch (SecurityException ex) {
Log.w(ex);
Snackbar.make(view, getString(R.string.title_no_viewer, intent.getAction()), Snackbar.LENGTH_LONG).show();
}
}
} else {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
PackageManager pm = getContext().getPackageManager();
if (intent.resolveActivity(pm) == null)
noStorageAccessFramework();
else
startActivityForResult(Helper.getChooser(getContext(), intent), REQUEST_IMAGE_FILE);
}
}
private void onAddImageFile(List uri) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
boolean add_inline = prefs.getBoolean("add_inline", true);
boolean resize_images = prefs.getBoolean("resize_images", true);
int resize = prefs.getInt("resize", FragmentCompose.REDUCED_IMAGE_SIZE);
onAddAttachment(uri, add_inline, resize_images ? resize : 0);
}
private void onAddAttachment(List uris, boolean image, int resize) {
Bundle args = new Bundle();
args.putLong("id", working);
args.putParcelableArrayList("uris", new ArrayList<>(uris));
args.putBoolean("image", image);
args.putInt("resize", resize);
args.putCharSequence("body", etBody.getText());
args.putInt("start", etBody.getSelectionStart());
new SimpleTask() {
@Override
protected Spanned onExecute(Context context, Bundle args) throws IOException {
long id = args.getLong("id");
List uris = args.getParcelableArrayList("uris");
boolean image = args.getBoolean("image");
int resize = args.getInt("resize");
CharSequence body = args.getCharSequence("body");
int start = args.getInt("start");
SpannableStringBuilder s = new SpannableStringBuilder(body);
for (Uri uri : uris) {
EntityAttachment attachment = addAttachment(context, id, uri, image, resize);
if (!image)
continue;
File file = attachment.getFile(context);
Uri cid = Uri.parse("cid:" + BuildConfig.APPLICATION_ID + "." + attachment.id);
Drawable d = Drawable.createFromPath(file.getAbsolutePath());
if (d == null)
throw new IllegalArgumentException(context.getString(R.string.title_no_image));
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
s.insert(start, " ");
ImageSpan is = new ImageSpan(context, cid);
s.setSpan(is, start + 1, start + 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
start += 3;
}
if (!image)
return null;
return HtmlHelper.fromHtml(HtmlHelper.toHtml(s), new Html.ImageGetter() {
@Override
public Drawable getDrawable(String source) {
return ImageHelper.decodeImage(context, id, source, true, zoom, etBody);
}
}, null);
}
@Override
protected void onExecuted(Bundle args, final Spanned body) {
if (body != null) {
int start = args.getInt("start");
etBody.setText(body);
if (start < body.length())
etBody.setSelection(start);
// Save text with image
onAction(R.id.action_save, "image");
}
}
@Override
protected void onException(Bundle args, Throwable ex) {
// External app sending absolute file
if (ex instanceof SecurityException)
handleFileShare();
else if (ex instanceof IllegalArgumentException)
Snackbar.make(view, ex.toString(), Snackbar.LENGTH_LONG).show();
else
Log.unexpectedError(getParentFragmentManager(), ex);
}
}.execute(this, args, "compose:attachment:add");
}
private List getUris(Intent data) {
List result = new ArrayList<>();
ClipData clipData = data.getClipData();
if (clipData == null) {
Uri uri = data.getData();
if (uri != null)
result.add(uri);
} else {
for (int i = 0; i < clipData.getItemCount(); i++) {
ClipData.Item item = clipData.getItemAt(i);
Uri uri = item.getUri();
if (uri != null)
result.add(uri);
}
}
return result;
}
private void onPgp(Intent data) {
final Bundle args = new Bundle();
args.putParcelable("data", data);
new SimpleTask