mirror of
https://github.com/M66B/FairEmail.git
synced 2025-01-01 04:35:57 +00:00
Added known sender rule condition
This commit is contained in:
parent
b40b2b2e3e
commit
dff77e6a3f
4 changed files with 74 additions and 7 deletions
|
@ -19,11 +19,16 @@ package eu.faircode.email;
|
|||
Copyright 2018-2019 by Marcel Bokhorst (M66B)
|
||||
*/
|
||||
|
||||
import android.Manifest;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.database.Cursor;
|
||||
import android.net.Uri;
|
||||
import android.provider.ContactsContract;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.preference.PreferenceManager;
|
||||
import androidx.room.Entity;
|
||||
import androidx.room.ForeignKey;
|
||||
import androidx.room.Index;
|
||||
|
@ -102,16 +107,51 @@ public class EntityRule {
|
|||
if (jsender != null) {
|
||||
String value = jsender.getString("value");
|
||||
boolean regex = jsender.getBoolean("regex");
|
||||
boolean known = jsender.optBoolean("known");
|
||||
|
||||
boolean matches = false;
|
||||
if (message.from != null) {
|
||||
for (Address from : message.from) {
|
||||
InternetAddress ia = (InternetAddress) from;
|
||||
String email = ia.getAddress();
|
||||
String personal = ia.getPersonal();
|
||||
String formatted = ((personal == null ? "" : personal + " ") + "<" + ia.getAddress() + ">");
|
||||
if (matches(context, value, formatted, regex)) {
|
||||
matches = true;
|
||||
break;
|
||||
|
||||
if (known) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
boolean suggest_sent = prefs.getBoolean("suggest_sent", false);
|
||||
if (suggest_sent) {
|
||||
DB db = DB.getInstance(context);
|
||||
EntityContact contact =
|
||||
db.contact().getContact(message.account, EntityContact.TYPE_TO, email);
|
||||
if (contact != null) {
|
||||
Log.i(email + " is local contact");
|
||||
matches = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
boolean contacts = Helper.hasPermission(context, Manifest.permission.READ_CONTACTS);
|
||||
if (contacts) {
|
||||
Cursor cursor = context.getContentResolver().query(
|
||||
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
|
||||
new String[]{ContactsContract.CommonDataKinds.Email.CONTACT_ID},
|
||||
ContactsContract.CommonDataKinds.Email.DATA + " = ? COLLATE NOCASE",
|
||||
new String[]{email},
|
||||
null);
|
||||
|
||||
while (cursor != null && cursor.moveToNext()) {
|
||||
Log.i(email + " is Android contact");
|
||||
matches = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
String formatted = ((personal == null ? "" : personal + " ") + "<" + email + ">");
|
||||
if (matches(context, value, formatted, regex)) {
|
||||
matches = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -40,6 +40,7 @@ import android.widget.AdapterView;
|
|||
import android.widget.ArrayAdapter;
|
||||
import android.widget.Button;
|
||||
import android.widget.CheckBox;
|
||||
import android.widget.CompoundButton;
|
||||
import android.widget.EditText;
|
||||
import android.widget.ImageButton;
|
||||
import android.widget.ImageView;
|
||||
|
@ -89,6 +90,7 @@ public class FragmentRule extends FragmentBase {
|
|||
private EditText etSender;
|
||||
private CheckBox cbSender;
|
||||
private ImageView ibSender;
|
||||
private CheckBox cbKnownSender;
|
||||
|
||||
private EditText etRecipient;
|
||||
private CheckBox cbRecipient;
|
||||
|
@ -192,6 +194,7 @@ public class FragmentRule extends FragmentBase {
|
|||
etSender = view.findViewById(R.id.etSender);
|
||||
cbSender = view.findViewById(R.id.cbSender);
|
||||
ibSender = view.findViewById(R.id.ibSender);
|
||||
cbKnownSender = view.findViewById(R.id.cbKnownSender);
|
||||
|
||||
etRecipient = view.findViewById(R.id.etRecipient);
|
||||
cbRecipient = view.findViewById(R.id.cbRecipient);
|
||||
|
@ -252,6 +255,15 @@ public class FragmentRule extends FragmentBase {
|
|||
}
|
||||
});
|
||||
|
||||
cbKnownSender.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
|
||||
@Override
|
||||
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
|
||||
etSender.setEnabled(!isChecked);
|
||||
ibSender.setEnabled(!isChecked);
|
||||
cbSender.setEnabled(!isChecked);
|
||||
}
|
||||
});
|
||||
|
||||
ibRecipient.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
|
@ -628,6 +640,10 @@ public class FragmentRule extends FragmentBase {
|
|||
|
||||
etSender.setText(jsender == null ? args.getString("sender") : jsender.getString("value"));
|
||||
cbSender.setChecked(jsender != null && jsender.getBoolean("regex"));
|
||||
cbKnownSender.setChecked(jsender != null && jsender.optBoolean("known"));
|
||||
etSender.setEnabled(!cbKnownSender.isChecked());
|
||||
ibSender.setEnabled(!cbKnownSender.isChecked());
|
||||
cbSender.setEnabled(!cbKnownSender.isChecked());
|
||||
|
||||
etRecipient.setText(jrecipient == null ? args.getString("recipient") : jrecipient.getString("value"));
|
||||
cbRecipient.setChecked(jrecipient != null && jrecipient.getBoolean("regex"));
|
||||
|
@ -877,10 +893,12 @@ public class FragmentRule extends FragmentBase {
|
|||
JSONObject jcondition = new JSONObject();
|
||||
|
||||
String sender = etSender.getText().toString();
|
||||
if (!TextUtils.isEmpty(sender)) {
|
||||
boolean known = cbKnownSender.isChecked();
|
||||
if (!TextUtils.isEmpty(sender) || known) {
|
||||
JSONObject jsender = new JSONObject();
|
||||
jsender.put("value", sender);
|
||||
jsender.put("regex", cbSender.isChecked());
|
||||
jsender.put("known", known);
|
||||
jcondition.put("sender", jsender);
|
||||
}
|
||||
|
||||
|
|
|
@ -160,6 +160,14 @@
|
|||
app:layout_constraintTop_toTopOf="@id/etSender"
|
||||
app:srcCompat="@drawable/baseline_person_add_24" />
|
||||
|
||||
<CheckBox
|
||||
android:id="@+id/cbKnownSender"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/title_rule_sender_known"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@id/etSender" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tvAndSender"
|
||||
android:layout_width="wrap_content"
|
||||
|
@ -170,7 +178,7 @@
|
|||
android:textColor="?android:attr/textColorPrimary"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@+id/etSender" />
|
||||
app:layout_constraintTop_toBottomOf="@+id/cbKnownSender" />
|
||||
|
||||
<View
|
||||
android:id="@+id/vSeparatorRecipient"
|
||||
|
@ -615,7 +623,7 @@
|
|||
android:layout_height="0dp"
|
||||
app:constraint_referenced_ids="
|
||||
tvTitle,tvFolder,tvName,etName,tvOrder,etOrder,cbEnabled,cbStop,
|
||||
vSeparatorSender,tvSender,cbSender,etSender,ibSender,tvAndSender,
|
||||
vSeparatorSender,tvSender,cbSender,etSender,ibSender,cbKnownSender,tvAndSender,
|
||||
vSeparatorRecipient,tvRecipient,cbRecipient,etRecipient,ibRecipient,tvAndRecipient,
|
||||
vSeparatorSubject,tvSubject,cbSubject,etSubject,tvAndSubject,
|
||||
vSeparatorHeader,tvHeader,cbHeader,etHeader,tvAndHeader,
|
||||
|
|
|
@ -665,6 +665,7 @@
|
|||
<string name="title_rule_enabled">Enabled</string>
|
||||
<string name="title_rule_stop">Stop processing rules after executing this rule</string>
|
||||
<string name="title_rule_sender">Sender contains</string>
|
||||
<string name="title_rule_sender_known">Sender is a contact</string>
|
||||
<string name="title_rule_recipient">Recipient contains</string>
|
||||
<string name="title_rule_subject">Subject contains</string>
|
||||
<string name="title_rule_header">Header contains</string>
|
||||
|
|
Loading…
Reference in a new issue