Export/import contacts by type

This commit is contained in:
M66B 2022-03-27 12:15:32 +02:00
parent 3888f7710a
commit d6e9514dc9
1 changed files with 35 additions and 11 deletions

View File

@ -72,6 +72,7 @@ import ezvcard.io.text.VCardReader;
import ezvcard.io.text.VCardWriter; import ezvcard.io.text.VCardWriter;
import ezvcard.property.Email; import ezvcard.property.Email;
import ezvcard.property.FormattedName; import ezvcard.property.FormattedName;
import ezvcard.property.RawProperty;
public class FragmentContacts extends FragmentBase { public class FragmentContacts extends FragmentBase {
private RecyclerView rvContacts; private RecyclerView rvContacts;
@ -93,6 +94,8 @@ public class FragmentContacts extends FragmentBase {
static final int REQUEST_EDIT_ACCOUNT = 5; static final int REQUEST_EDIT_ACCOUNT = 5;
static final int REQUEST_EDIT_CONTACT = 6; static final int REQUEST_EDIT_CONTACT = 6;
private static final String VCF_TYPE = "X-FAIREMAIL-TYPE";
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -239,8 +242,6 @@ public class FragmentContacts extends FragmentBase {
public void onPrepareOptionsMenu(@NonNull Menu menu) { public void onPrepareOptionsMenu(@NonNull Menu menu) {
super.onPrepareOptionsMenu(menu); super.onPrepareOptionsMenu(menu);
menu.findItem(R.id.menu_junk).setChecked(junk); menu.findItem(R.id.menu_junk).setChecked(junk);
menu.findItem(R.id.menu_import).setVisible(!junk);
menu.findItem(R.id.menu_export).setVisible(!junk);
} }
@Override @Override
@ -409,12 +410,22 @@ public class FragmentContacts extends FragmentBase {
long now = new Date().getTime(); long now = new Date().getTime();
Log.i("Reading URI=" + uri); EntityLog.log(context, "Importing " + uri +
" junk=" + junk + " account=" + account);
int count = 0;
ContentResolver resolver = context.getContentResolver(); ContentResolver resolver = context.getContentResolver();
try (InputStream is = new BufferedInputStream(resolver.openInputStream(uri))) { try (InputStream is = new BufferedInputStream(resolver.openInputStream(uri))) {
VCardReader reader = new VCardReader(is); VCardReader reader = new VCardReader(is);
VCard vcard; VCard vcard;
while ((vcard = reader.readNext()) != null) { while ((vcard = reader.readNext()) != null) {
Integer type = null;
RawProperty xtype = vcard.getExtendedProperty(VCF_TYPE);
if (xtype != null)
type = Helper.parseInt(xtype.getValue());
if (type == null)
type = EntityContact.TYPE_TO;
List<Email> emails = vcard.getEmails(); List<Email> emails = vcard.getEmails();
if (emails == null) if (emails == null)
continue; continue;
@ -439,12 +450,14 @@ public class FragmentContacts extends FragmentBase {
account, account,
addresses.toArray(new Address[0]), addresses.toArray(new Address[0]),
group, group,
EntityContact.TYPE_TO, type,
now); now);
count += addresses.size();
} }
} }
Log.i("Imported contacts"); Log.i("Imported contacts=" + count);
return null; return null;
} }
@ -467,6 +480,7 @@ public class FragmentContacts extends FragmentBase {
private void handleExport(Intent data) { private void handleExport(Intent data) {
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putParcelable("uri", data.getData()); args.putParcelable("uri", data.getData());
args.putBoolean("junk", junk);
args.putLong("account", selected_account); args.putLong("account", selected_account);
new SimpleTask<Void>() { new SimpleTask<Void>() {
@ -478,26 +492,38 @@ public class FragmentContacts extends FragmentBase {
@Override @Override
protected Void onExecute(Context context, Bundle args) throws Throwable { protected Void onExecute(Context context, Bundle args) throws Throwable {
Uri uri = args.getParcelable("uri"); Uri uri = args.getParcelable("uri");
boolean junk = args.getBoolean("junk");
long account = args.getLong("account"); long account = args.getLong("account");
if (uri == null) if (uri == null)
throw new FileNotFoundException(); throw new FileNotFoundException();
EntityLog.log(context, "Exporting " + uri); EntityLog.log(context, "Exporting " + uri +
" junk=" + junk + " account=" + account);
if (!"content".equals(uri.getScheme())) { if (!"content".equals(uri.getScheme())) {
Log.w("Export uri=" + uri); Log.w("Export uri=" + uri);
throw new IllegalArgumentException(context.getString(R.string.title_no_stream)); throw new IllegalArgumentException(context.getString(R.string.title_no_stream));
} }
List<Integer> types = new ArrayList<>();
if (junk) {
types.add(EntityContact.TYPE_JUNK);
types.add(EntityContact.TYPE_NO_JUNK);
} else {
types.add(EntityContact.TYPE_TO);
types.add(EntityContact.TYPE_FROM);
}
List<VCard> vcards = new ArrayList<>(); List<VCard> vcards = new ArrayList<>();
DB db = DB.getInstance(context); DB db = DB.getInstance(context);
List<EntityContact> contacts = db.contact().getContacts(account); List<EntityContact> contacts = db.contact().getContacts(account);
for (EntityContact contact : contacts) for (EntityContact contact : contacts)
if (contact.type == EntityContact.TYPE_TO || if (contact.account.equals(account) &&
contact.type == EntityContact.TYPE_FROM) { types.contains(contact.type)) {
VCard vcard = new VCard(); VCard vcard = new VCard();
vcard.addExtendedProperty(VCF_TYPE, Integer.toString(contact.type));
vcard.addEmail(contact.email); vcard.addEmail(contact.email);
if (!TextUtils.isEmpty(contact.name)) if (!TextUtils.isEmpty(contact.name))
vcard.setFormattedName(contact.name); vcard.setFormattedName(contact.name);
@ -514,9 +540,7 @@ public class FragmentContacts extends FragmentBase {
} }
} }
EntityLog.log(context, "Exported" + EntityLog.log(context, "Exported contact=" + vcards.size() + "/" + contacts.size());
" contacts=" + contacts.size() +
" count=" + vcards.size());
return null; return null;
} }