ContactsImportJob.java 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. if (intArray[i] == 1){
  55. operations.insertContact(vCards.get(i));
  56. }
  57. }
  58. } catch (Exception e) {
  59. Log_OC.e(TAG, e.getMessage());
  60. }
  61. return Result.SUCCESS;
  62. }
  63. }