ContactsImportJob.java 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /**
  2. * Nextcloud Android client application
  3. *
  4. * @author Tobias Kaminsky
  5. * Copyright (C) 2017 Tobias Kaminsky
  6. * Copyright (C) 2017 Nextcloud GmbH.
  7. * <p>
  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. * <p>
  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. * <p>
  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.services;
  22. import android.support.annotation.NonNull;
  23. import com.evernote.android.job.Job;
  24. import com.evernote.android.job.util.support.PersistableBundleCompat;
  25. import com.owncloud.android.lib.common.utils.Log_OC;
  26. import java.io.File;
  27. import java.util.ArrayList;
  28. import ezvcard.Ezvcard;
  29. import ezvcard.VCard;
  30. import third_parties.ezvcard_android.ContactOperations;
  31. /**
  32. * Job to import contacts
  33. */
  34. public class ContactsImportJob extends Job {
  35. public static final String TAG = "ContactsImportJob";
  36. public static final String ACCOUNT_TYPE = "account_type";
  37. public static final String ACCOUNT_NAME = "account_name";
  38. public static final String VCARD_FILE_PATH = "vcard_file_path";
  39. public static final String CHECKED_ITEMS_ARRAY = "checked_items_array";
  40. @NonNull
  41. @Override
  42. protected Result onRunJob(Params params) {
  43. PersistableBundleCompat bundle = params.getExtras();
  44. String vCardFilePath = bundle.getString(VCARD_FILE_PATH, "");
  45. String accountName = bundle.getString(ACCOUNT_NAME, "");
  46. String accountType = bundle.getString(ACCOUNT_TYPE, "");
  47. int[] intArray = bundle.getIntArray(CHECKED_ITEMS_ARRAY);
  48. File file = new File(vCardFilePath);
  49. ArrayList<VCard> vCards = new ArrayList<>();
  50. try {
  51. ContactOperations operations = new ContactOperations(getContext(), accountName, accountType);
  52. vCards.addAll(Ezvcard.parse(file).all());
  53. for (int i = 0; i < intArray.length; i++ ){
  54. operations.insertContact(vCards.get(intArray[i]));
  55. }
  56. } catch (Exception e) {
  57. Log_OC.e(TAG, e.getMessage());
  58. }
  59. return Result.SUCCESS;
  60. }
  61. }