1 /* ownCloud Android client application 
   2  *   Copyright (C) 2012 Bartek Przybylski 
   3  *   Copyright (C) 2012-2013 ownCloud Inc. 
   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. 
   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. 
  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/>. 
  19 package com
.owncloud
.android
.syncadapter
; 
  21 import java
.io
.FileInputStream
; 
  22 import java
.io
.IOException
; 
  24 import org
.apache
.http
.client
.methods
.HttpPut
; 
  25 import org
.apache
.http
.entity
.ByteArrayEntity
; 
  27 import com
.owncloud
.android
.AccountUtils
; 
  28 import com
.owncloud
.android
.authentication
.AccountAuthenticator
; 
  30 import android
.accounts
.Account
; 
  31 import android
.accounts
.AccountManager
; 
  32 import android
.accounts
.AuthenticatorException
; 
  33 import android
.accounts
.OperationCanceledException
; 
  34 import android
.content
.ContentProviderClient
; 
  35 import android
.content
.Context
; 
  36 import android
.content
.SyncResult
; 
  37 import android
.content
.res
.AssetFileDescriptor
; 
  38 import android
.database
.Cursor
; 
  39 import android
.net
.Uri
; 
  40 import android
.os
.Bundle
; 
  41 import android
.provider
.ContactsContract
; 
  43 public class ContactSyncAdapter 
extends AbstractOwnCloudSyncAdapter 
{ 
  44     private String mAddrBookUri
; 
  46     public ContactSyncAdapter(Context context
, boolean autoInitialize
) { 
  47         super(context
, autoInitialize
); 
  52     public void onPerformSync(Account account
, Bundle extras
, String authority
, 
  53             ContentProviderClient provider
, SyncResult syncResult
) { 
  55         setContentProvider(provider
); 
  56         Cursor c 
= getLocalContacts(false
); 
  57         if (c
.moveToFirst()) { 
  59                 String lookup 
= c
.getString(c
 
  60                         .getColumnIndex(ContactsContract
.Contacts
.LOOKUP_KEY
)); 
  61                 String a 
= getAddressBookUri(); 
  62                 String uri 
= a 
+ lookup 
+ ".vcf"; 
  65                     f 
= getContactVcard(lookup
); 
  66                     HttpPut query 
= new HttpPut(uri
); 
  67                     byte[] b 
= new byte[f
.available()]; 
  69                     query
.setEntity(new ByteArrayEntity(b
)); 
  70                     fireRawRequest(query
); 
  71                 } catch (IOException e
) { 
  74                 } catch (OperationCanceledException e
) { 
  75                     // TODO Auto-generated catch block 
  77                 } catch (AuthenticatorException e
) { 
  78                     // TODO Auto-generated catch block 
  81             } while (c
.moveToNext()); 
  82             // } while (c.moveToNext()); 
  87     private String 
getAddressBookUri() { 
  88         if (mAddrBookUri 
!= null
) 
  91         AccountManager am 
= getAccountManager(); 
  92         @SuppressWarnings("deprecation") 
  93         String uri 
= am
.getUserData(getAccount(), 
  94                 AccountAuthenticator
.KEY_OC_URL
).replace( 
  95                 AccountUtils
.WEBDAV_PATH_2_0
, AccountUtils
.CARDDAV_PATH_2_0
); 
  96         uri 
+= "/addressbooks/" 
  97                 + getAccount().name
.substring(0, 
  98                         getAccount().name
.lastIndexOf('@')) + "/default/"; 
 103     private FileInputStream 
getContactVcard(String lookupKey
) 
 105         Uri uri 
= Uri
.withAppendedPath( 
 106                 ContactsContract
.Contacts
.CONTENT_VCARD_URI
, lookupKey
); 
 107         AssetFileDescriptor fd 
= getContext().getContentResolver() 
 108                 .openAssetFileDescriptor(uri
, "r"); 
 109         return fd
.createInputStream(); 
 112     private Cursor 
getLocalContacts(boolean include_hidden_contacts
) { 
 113         return getContext().getContentResolver().query( 
 114                 ContactsContract
.Contacts
.CONTENT_URI
, 
 115                 new String
[] { ContactsContract
.Contacts
._ID
, 
 116                         ContactsContract
.Contacts
.LOOKUP_KEY 
}, 
 117                 ContactsContract
.Contacts
.IN_VISIBLE_GROUP 
+ " = ?", 
 118                 new String
[] { (include_hidden_contacts ? 
"0" : "1") }, 
 119                 ContactsContract
.Contacts
._ID 
+ " DESC");