ContactsImportJob.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Nextcloud Android client application
  3. *
  4. * @author Tobias Kaminsky
  5. * Copyright (C) 2017 Tobias Kaminsky
  6. * Copyright (C) 2017 Nextcloud GmbH.
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. package com.owncloud.android.jobs;
  22. import android.database.Cursor;
  23. import android.net.Uri;
  24. import android.provider.ContactsContract;
  25. import com.evernote.android.job.Job;
  26. import com.evernote.android.job.util.support.PersistableBundleCompat;
  27. import com.owncloud.android.lib.common.utils.Log_OC;
  28. import com.owncloud.android.ui.fragment.contactsbackup.ContactListFragment;
  29. import java.io.File;
  30. import java.io.IOException;
  31. import java.io.InputStream;
  32. import java.util.ArrayList;
  33. import java.util.Collections;
  34. import java.util.TreeMap;
  35. import androidx.annotation.NonNull;
  36. import ezvcard.Ezvcard;
  37. import ezvcard.VCard;
  38. import third_parties.ezvcard_android.ContactOperations;
  39. /**
  40. * Job to import contacts
  41. */
  42. public class ContactsImportJob extends Job {
  43. public static final String TAG = "ContactsImportJob";
  44. public static final String ACCOUNT_TYPE = "account_type";
  45. public static final String ACCOUNT_NAME = "account_name";
  46. public static final String VCARD_FILE_PATH = "vcard_file_path";
  47. public static final String CHECKED_ITEMS_ARRAY = "checked_items_array";
  48. @NonNull
  49. @Override
  50. protected Result onRunJob(@NonNull Params params) {
  51. PersistableBundleCompat bundle = params.getExtras();
  52. String vCardFilePath = bundle.getString(VCARD_FILE_PATH, "");
  53. String accountName = bundle.getString(ACCOUNT_NAME, "");
  54. String accountType = bundle.getString(ACCOUNT_TYPE, "");
  55. int[] intArray = bundle.getIntArray(CHECKED_ITEMS_ARRAY);
  56. File file = new File(vCardFilePath);
  57. ArrayList<VCard> vCards = new ArrayList<>();
  58. Cursor cursor = null;
  59. try {
  60. ContactOperations operations = new ContactOperations(getContext(), accountName, accountType);
  61. vCards.addAll(Ezvcard.parse(file).all());
  62. Collections.sort(vCards, new ContactListFragment.VCardComparator());
  63. cursor = getContext().getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null,
  64. null, null, null);
  65. TreeMap<VCard, Long> ownContactList = new TreeMap<>(new ContactListFragment.VCardComparator());
  66. if (cursor != null && cursor.getCount() > 0) {
  67. cursor.moveToFirst();
  68. for (int i = 0; i < cursor.getCount(); i++) {
  69. VCard vCard = getContactFromCursor(cursor);
  70. if (vCard != null) {
  71. ownContactList.put(vCard, cursor.getLong(cursor.getColumnIndex("NAME_RAW_CONTACT_ID")));
  72. }
  73. cursor.moveToNext();
  74. }
  75. }
  76. for (int checkedItem : intArray) {
  77. VCard vCard = vCards.get(checkedItem);
  78. if (ContactListFragment.getDisplayName(vCard).length() != 0) {
  79. if (!ownContactList.containsKey(vCard)) {
  80. operations.insertContact(vCard);
  81. } else {
  82. operations.updateContact(vCard, ownContactList.get(vCard));
  83. }
  84. } else {
  85. operations.insertContact(vCard); //Insert All the contacts without name
  86. }
  87. }
  88. } catch (Exception e) {
  89. Log_OC.e(TAG, e.getMessage());
  90. } finally {
  91. if (cursor != null) {
  92. cursor.close();
  93. }
  94. }
  95. return Result.SUCCESS;
  96. }
  97. private VCard getContactFromCursor(Cursor cursor) {
  98. String lookupKey = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
  99. Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey);
  100. VCard vCard = null;
  101. try (InputStream inputStream = getContext().getContentResolver().openInputStream(uri)){
  102. ArrayList<VCard> vCardList = new ArrayList<>();
  103. vCardList.addAll(Ezvcard.parse(inputStream).all());
  104. if (vCardList.size() > 0) {
  105. vCard = vCardList.get(0);
  106. }
  107. } catch (IOException e) {
  108. Log_OC.d(TAG, e.getMessage());
  109. }
  110. return vCard;
  111. }
  112. }