Added local contact groups

pull/207/head
M66B 3 years ago
parent 8d1e5ae483
commit 1391db53e5

File diff suppressed because it is too large Load Diff

@ -153,8 +153,8 @@ public class AdapterContact extends RecyclerView.Adapter<AdapterContact.ViewHold
}
}
tvName.setText(contact.name == null ? "-" : contact.name);
tvEmail.setText(contact.accountName + "/" + contact.email);
tvName.setText(contact.name == null ? "-" : contact.name + (contact.group == null ? "" : "/" + contact.group));
tvEmail.setText(contact.email + "/" + contact.accountName);
tvTimes.setText(NF.format(contact.times_contacted));
tvLast.setText(contact.last_contacted == null ? null
: Helper.getRelativeTimeSpanString(context, contact.last_contacted));
@ -316,6 +316,7 @@ public class AdapterContact extends RecyclerView.Adapter<AdapterContact.ViewHold
args.putInt("type", contact.type);
args.putString("email", contact.email);
args.putString("name", contact.name);
args.putString("group", contact.group);
FragmentContacts.FragmentDialogEditContact fragment = new FragmentContacts.FragmentDialogEditContact();
fragment.setArguments(args);

@ -71,7 +71,7 @@ import io.requery.android.database.sqlite.SQLiteDatabase;
// https://developer.android.com/topic/libraries/architecture/room.html
@Database(
version = 227,
version = 228,
entities = {
EntityIdentity.class,
EntityAccount.class,
@ -2299,6 +2299,12 @@ public abstract class DB extends RoomDatabase {
db.execSQL("DROP TRIGGER IF EXISTS `attachment_delete`");
createTriggers(db, true);
}
}).addMigrations(new Migration(227, 228) {
@Override
public void migrate(@NonNull SupportSQLiteDatabase db) {
logMigration(startVersion, endVersion);
db.execSQL("ALTER TABLE `contact` ADD COLUMN `group` TEXT");
}
}).addMigrations(new Migration(998, 999) {
@Override
public void migrate(@NonNull SupportSQLiteDatabase db) {

@ -63,6 +63,21 @@ public interface DaoContact {
" AND email = :email COLLATE NOCASE")
EntityContact getContact(long account, int type, String email);
@Query("SELECT -1 AS _id, `group` AS title" +
", COUNT(*) AS summ_count" +
", :name AS account_name, :type AS account_type" +
" FROM contact" +
" WHERE (:account IS NULL OR account = :account)" +
" AND `group` IS NOT NULL" +
" GROUP BY `group`" +
" ORDER BY `group` COLLATE NOCASE")
Cursor getGroups(Long account, String name, String type);
@Query("SELECT * FROM contact" +
" WHERE `group` = :group" +
" AND type <> " + EntityContact.TYPE_JUNK)
List<EntityContact> getContacts(String group);
@Query("SELECT *" +
" FROM contact" +
" WHERE (:account IS NULL OR account = :account)" +

@ -84,6 +84,7 @@ public class EntityContact implements Serializable {
@NonNull
public String email;
public String name;
public String group;
public String avatar;
@NonNull
@ -234,6 +235,7 @@ public class EntityContact implements Serializable {
json.put("type", type);
json.put("email", email);
json.put("name", name);
json.put("group", group);
json.put("avatar", avatar);
json.put("times_contacted", times_contacted);
json.put("first_contacted", first_contacted);
@ -251,6 +253,9 @@ public class EntityContact implements Serializable {
if (json.has("name") && !json.isNull("name"))
contact.name = json.getString("name");
if (json.has("group") && !json.isNull("group"))
contact.group = json.getString("group");
if (json.has("avatar") && !json.isNull("avatar"))
contact.avatar = json.getString("avatar");
@ -270,6 +275,7 @@ public class EntityContact implements Serializable {
this.type == other.type &&
this.email.equals(other.email) &&
Objects.equals(this.name, other.name) &&
Objects.equals(this.group, other.group) &&
Objects.equals(this.avatar, other.avatar) &&
this.times_contacted.equals(other.times_contacted) &&
this.first_contacted.equals(first_contacted) &&

@ -42,6 +42,7 @@ import android.content.pm.PackageManager;
import android.content.res.ColorStateList;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.database.MergeCursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
@ -3794,42 +3795,52 @@ public class FragmentCompose extends FragmentBase {
long id = args.getLong("id");
int target = args.getInt("target");
long group = args.getLong("group");
String gname = args.getString("name");
String to = args.getString("to");
String cc = args.getString("cc");
String bcc = args.getString("bcc");
EntityLog.log(context, "Selected group=" + group);
EntityLog.log(context, "Selected group=" + group + "/" + gname);
List<Address> selected = new ArrayList<>();
try (Cursor cursor = context.getContentResolver().query(
ContactsContract.Data.CONTENT_URI,
new String[]{ContactsContract.Data.CONTACT_ID},
ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID + "= ?" + " AND "
+ ContactsContract.CommonDataKinds.GroupMembership.MIMETYPE + "='"
+ ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE + "'",
new String[]{String.valueOf(group)}, null)) {
while (cursor != null && cursor.moveToNext()) {
try (Cursor contact = getContext().getContentResolver().query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
new String[]{
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Email.DATA
},
ContactsContract.Data.CONTACT_ID + " = ?",
new String[]{cursor.getString(0)},
null)) {
if (contact != null && contact.moveToNext()) {
String name = contact.getString(0);
String email = contact.getString(1);
Address address = new InternetAddress(email, name, StandardCharsets.UTF_8.name());
EntityLog.log(context, "Selected group=" + group +
" address=" + MessageHelper.formatAddresses(new Address[]{address}));
selected.add(address);
if (group < 0) {
DB db = DB.getInstance(context);
List<EntityContact> contacts = db.contact().getContacts(gname);
if (contacts != null)
for (EntityContact contact : contacts) {
Address address = new InternetAddress(contact.email, contact.name, StandardCharsets.UTF_8.name());
selected.add(address);
}
} else
try (Cursor cursor = context.getContentResolver().query(
ContactsContract.Data.CONTENT_URI,
new String[]{ContactsContract.Data.CONTACT_ID},
ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID + "= ?" + " AND "
+ ContactsContract.CommonDataKinds.GroupMembership.MIMETYPE + "='"
+ ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE + "'",
new String[]{String.valueOf(group)}, null)) {
while (cursor != null && cursor.moveToNext()) {
try (Cursor contact = getContext().getContentResolver().query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
new String[]{
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Email.DATA
},
ContactsContract.Data.CONTACT_ID + " = ?",
new String[]{cursor.getString(0)},
null)) {
if (contact != null && contact.moveToNext()) {
String name = contact.getString(0);
String email = contact.getString(1);
Address address = new InternetAddress(email, name, StandardCharsets.UTF_8.name());
EntityLog.log(context, "Selected group=" + group +
" address=" + MessageHelper.formatAddresses(new Address[]{address}));
selected.add(address);
}
}
}
}
}
EntityMessage draft;
DB db = DB.getInstance(context);
@ -6725,8 +6736,6 @@ public class FragmentCompose extends FragmentBase {
int focussed = args.getInt("focussed");
final Context context = getContext();
final ContentResolver resolver = context.getContentResolver();
View dview = LayoutInflater.from(context).inflate(R.layout.dialog_contact_group, null);
final ImageButton ibInfo = dview.findViewById(R.id.ibInfo);
final Spinner spGroup = dview.findViewById(R.id.spGroup);
@ -6739,61 +6748,83 @@ public class FragmentCompose extends FragmentBase {
}
});
final String[] projection = new String[]{
ContactsContract.Groups._ID,
ContactsContract.Groups.TITLE,
ContactsContract.Groups.SUMMARY_COUNT,
ContactsContract.Groups.ACCOUNT_NAME,
ContactsContract.Groups.ACCOUNT_TYPE,
};
Cursor groups;
try {
groups = resolver.query(
ContactsContract.Groups.CONTENT_SUMMARY_URI,
projection,
// ContactsContract.Groups.GROUP_VISIBLE + " = 1" + " AND " +
ContactsContract.Groups.DELETED + " = 0" +
" AND " + ContactsContract.Groups.SUMMARY_COUNT + " > 0",
null,
ContactsContract.Groups.TITLE
);
} catch (SecurityException ex) {
Log.w(ex);
groups = new MatrixCursor(projection);
}
new SimpleTask<Cursor>() {
@Override
protected Cursor onExecute(Context context, Bundle args) {
final String[] projection = new String[]{
ContactsContract.Groups._ID,
ContactsContract.Groups.TITLE,
ContactsContract.Groups.SUMMARY_COUNT,
ContactsContract.Groups.ACCOUNT_NAME,
ContactsContract.Groups.ACCOUNT_TYPE,
};
Cursor contacts;
try {
ContentResolver resolver = context.getContentResolver();
contacts = resolver.query(
ContactsContract.Groups.CONTENT_SUMMARY_URI,
projection,
// ContactsContract.Groups.GROUP_VISIBLE + " = 1" + " AND " +
ContactsContract.Groups.DELETED + " = 0" +
" AND " + ContactsContract.Groups.SUMMARY_COUNT + " > 0",
null,
ContactsContract.Groups.TITLE
);
} catch (SecurityException ex) {
Log.w(ex);
contacts = new MatrixCursor(projection);
}
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
context,
R.layout.spinner_contact_group,
groups,
new String[]{ContactsContract.Groups.TITLE, ContactsContract.Groups.ACCOUNT_NAME},
new int[]{R.id.tvGroup, R.id.tvAccount},
0);
DB db = DB.getInstance(context);
Cursor local = db.contact().getGroups(
null,
context.getString(R.string.app_name),
BuildConfig.APPLICATION_ID);
final NumberFormat NF = NumberFormat.getInstance();
return new MergeCursor(new Cursor[]{contacts, local});
}
adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if (view.getId() == R.id.tvGroup) {
String title = cursor.getString(1);
if (TextUtils.isEmpty(title))
title = "-";
int count = cursor.getInt(2);
((TextView) view).setText(context.getString(R.string.title_name_count, title, NF.format(count)));
return true;
} else if (view.getId() == R.id.tvAccount && BuildConfig.DEBUG) {
String account = cursor.getString(3);
String type = cursor.getString(4);
((TextView) view).setText(account + (BuildConfig.DEBUG ? "/" + type : ""));
return true;
} else
return false;
protected void onExecuted(Bundle args, Cursor cursor) {
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
context,
R.layout.spinner_contact_group,
cursor,
new String[]{ContactsContract.Groups.TITLE, ContactsContract.Groups.ACCOUNT_NAME},
new int[]{R.id.tvGroup, R.id.tvAccount},
0);
final NumberFormat NF = NumberFormat.getInstance();
adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if (view.getId() == R.id.tvGroup) {
String title = cursor.getString(1);
if (TextUtils.isEmpty(title))
title = "-";
int count = cursor.getInt(2);
((TextView) view).setText(context.getString(R.string.title_name_count, title, NF.format(count)));
return true;
} else if (view.getId() == R.id.tvAccount) {
String account = cursor.getString(3);
String type = cursor.getString(4);
((TextView) view).setText(account + (BuildConfig.DEBUG ? "/" + type : ""));
return true;
} else
return false;
}
});
spGroup.setAdapter(adapter);
}
});
spGroup.setAdapter(adapter);
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(getParentFragmentManager(), ex);
}
}.execute(this, new Bundle(), "compose:groups");
spTarget.setSelection(focussed);
@ -6806,11 +6837,13 @@ public class FragmentCompose extends FragmentBase {
Cursor cursor = (Cursor) spGroup.getSelectedItem();
if (target != INVALID_POSITION && cursor != null) {
long group = cursor.getLong(0);
String name = cursor.getString(1);
Bundle args = getArguments();
args.putLong("id", working);
args.putInt("target", target);
args.putLong("group", group);
args.putString("name", name);
sendResult(RESULT_OK);
} else

@ -513,6 +513,7 @@ public class FragmentContacts extends FragmentBase {
int type = args.getInt("type");
String email = args.getString("email");
String name = args.getString("name");
String group = args.getString("group");
if (TextUtils.isEmpty(email))
throw new IllegalArgumentException(context.getString(R.string.title_no_email));
@ -520,6 +521,8 @@ public class FragmentContacts extends FragmentBase {
throw new IllegalArgumentException(context.getString(R.string.title_email_invalid, email));
if (TextUtils.isEmpty(name))
name = null;
if (TextUtils.isEmpty(group))
group = null;
DB db = DB.getInstance(context);
@ -528,10 +531,13 @@ public class FragmentContacts extends FragmentBase {
contact = db.contact().getContact(id);
else
contact = new EntityContact();
contact.account = account;
contact.type = type;
contact.email = email;
contact.name = name;
contact.group = group;
if (id > 0)
db.contact().updateContact(contact);
else {
@ -602,11 +608,13 @@ public class FragmentContacts extends FragmentBase {
final Spinner spType = view.findViewById(R.id.spType);
final EditText etEmail = view.findViewById(R.id.etEmail);
final EditText etName = view.findViewById(R.id.etName);
final EditText etGroup = view.findViewById(R.id.etGroup);
final Bundle args = getArguments();
spType.setSelection(args.getInt("type"));
etEmail.setText(args.getString("email"));
etName.setText(args.getString("name"));
etGroup.setText(args.getString("group"));
return new AlertDialog.Builder(getContext())
.setView(view)
@ -614,8 +622,9 @@ public class FragmentContacts extends FragmentBase {
@Override
public void onClick(DialogInterface dialog, int which) {
args.putInt("type", spType.getSelectedItemPosition());
args.putString("email", etEmail.getText().toString());
args.putString("email", etEmail.getText().toString().trim());
args.putString("name", etName.getText().toString());
args.putString("group", etGroup.getText().toString().trim());
sendResult(RESULT_OK);
}
})

@ -56,4 +56,18 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/etEmail" />
<eu.faircode.email.EditTextPlain
android:id="@+id/etGroup"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:autofillHints="name"
android:hint="@string/title_contact_group"
android:imeOptions="actionDone"
android:inputType="textPersonName|textCapWords"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/etName" />
</androidx.constraintlayout.widget.ConstraintLayout>

@ -1025,6 +1025,7 @@
<string name="title_edit_contact">Edit contact</string>
<string name="title_contact_email">Email</string>
<string name="title_contact_name">Name</string>
<string name="title_contact_group">Group</string>
<string name="title_import_contacts">Import vCards</string>
<string name="title_export_contacts">Export vCards</string>
<string name="title_create_sub_folder">Create sub folder</string>

Loading…
Cancel
Save