ContactSyncAdapter.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* ownCloud Android client application
  2. * Copyright (C) 2012 Bartek Przybylski
  3. * Copyright (C) 2012-2013 ownCloud Inc.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2,
  7. * as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. */
  18. package com.owncloud.android.syncadapter;
  19. import java.io.FileInputStream;
  20. import java.io.IOException;
  21. import org.apache.http.client.methods.HttpPut;
  22. import org.apache.http.entity.ByteArrayEntity;
  23. import com.owncloud.android.authentication.AccountAuthenticator;
  24. import com.owncloud.android.authentication.AccountUtils;
  25. import android.accounts.Account;
  26. import android.accounts.AccountManager;
  27. import android.accounts.AuthenticatorException;
  28. import android.accounts.OperationCanceledException;
  29. import android.content.ContentProviderClient;
  30. import android.content.Context;
  31. import android.content.SyncResult;
  32. import android.content.res.AssetFileDescriptor;
  33. import android.database.Cursor;
  34. import android.net.Uri;
  35. import android.os.Bundle;
  36. import android.provider.ContactsContract;
  37. public class ContactSyncAdapter extends AbstractOwnCloudSyncAdapter {
  38. private String mAddrBookUri;
  39. public ContactSyncAdapter(Context context, boolean autoInitialize) {
  40. super(context, autoInitialize);
  41. mAddrBookUri = null;
  42. }
  43. @Override
  44. public void onPerformSync(Account account, Bundle extras, String authority,
  45. ContentProviderClient provider, SyncResult syncResult) {
  46. setAccount(account);
  47. setContentProvider(provider);
  48. Cursor c = getLocalContacts(false);
  49. if (c.moveToFirst()) {
  50. do {
  51. String lookup = c.getString(c
  52. .getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
  53. String a = getAddressBookUri();
  54. String uri = a + lookup + ".vcf";
  55. FileInputStream f;
  56. try {
  57. f = getContactVcard(lookup);
  58. HttpPut query = new HttpPut(uri);
  59. byte[] b = new byte[f.available()];
  60. f.read(b);
  61. query.setEntity(new ByteArrayEntity(b));
  62. fireRawRequest(query);
  63. } catch (IOException e) {
  64. e.printStackTrace();
  65. return;
  66. } catch (OperationCanceledException e) {
  67. // TODO Auto-generated catch block
  68. e.printStackTrace();
  69. } catch (AuthenticatorException e) {
  70. // TODO Auto-generated catch block
  71. e.printStackTrace();
  72. }
  73. } while (c.moveToNext());
  74. // } while (c.moveToNext());
  75. }
  76. }
  77. private String getAddressBookUri() {
  78. if (mAddrBookUri != null)
  79. return mAddrBookUri;
  80. AccountManager am = getAccountManager();
  81. @SuppressWarnings("deprecation")
  82. String uri = am.getUserData(getAccount(),
  83. AccountAuthenticator.KEY_OC_URL).replace(
  84. AccountUtils.WEBDAV_PATH_2_0, AccountUtils.CARDDAV_PATH_2_0);
  85. uri += "/addressbooks/"
  86. + getAccount().name.substring(0,
  87. getAccount().name.lastIndexOf('@')) + "/default/";
  88. mAddrBookUri = uri;
  89. return uri;
  90. }
  91. private FileInputStream getContactVcard(String lookupKey)
  92. throws IOException {
  93. Uri uri = Uri.withAppendedPath(
  94. ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey);
  95. AssetFileDescriptor fd = getContext().getContentResolver()
  96. .openAssetFileDescriptor(uri, "r");
  97. return fd.createInputStream();
  98. }
  99. private Cursor getLocalContacts(boolean include_hidden_contacts) {
  100. return getContext().getContentResolver().query(
  101. ContactsContract.Contacts.CONTENT_URI,
  102. new String[] { ContactsContract.Contacts._ID,
  103. ContactsContract.Contacts.LOOKUP_KEY },
  104. ContactsContract.Contacts.IN_VISIBLE_GROUP + " = ?",
  105. new String[] { (include_hidden_contacts ? "0" : "1") },
  106. ContactsContract.Contacts._ID + " DESC");
  107. }
  108. }