-
- private static final String TAG = "ContactSyncAdapter";
-
- public ContactSyncAdapter(Context context, boolean autoInitialize) {
- super(context, autoInitialize);
- }
-
- @Override
- public synchronized void onPerformSync(
- Account account,
- Bundle extras,
- String authority,
- ContentProviderClient provider,
- SyncResult syncResult) {
-
- this.setAccount(account);
- this.setContentProvider(provider);
-
- // TODO find all contacts on ownCloud that not synced or the sync date is behind than the last sync date
- Cursor cursor = getContacts();
- if (cursor != null && cursor.getCount() > 0) {
- while (cursor.moveToNext()) {
- String id = cursor.getString(
- cursor.getColumnIndex(ContactsContract.Contacts._ID));
- String lookup = cursor.getString(
- cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
-
- try {
- FileInputStream fis = getContactVcard(lookup);
-
- HttpPut query = new HttpPut(
- getUri() +
- "/addressbooks/"+
- getAccount().name.split("@")[0]+
- "/default/"+
- lookup+
- ".vcf"
- );
- byte[] b = new byte[fis.available()];
- fis.read(b);
- query.setEntity(new ByteArrayEntity(b));
- HttpResponse response = fireRawRequest(query);
- if(201 != response.getStatusLine().getStatusCode()) {
- syncResult.stats.numIoExceptions++;
- }
- // TODO make a webdav request based on the stream
- // TODO send request to the ownCloud server
- // TODO mark the current contact as synced - where to store?
- fis.close();
- } catch (IOException e) {
- syncResult.stats.numIoExceptions++;
- } catch (OperationCanceledException e) {
- //TODO maybe to a better break here
- return;
- } catch (AuthenticatorException e) {
- syncResult.stats.numAuthExceptions++;
- }
- }
- }
+ private String mAddrBookUri;
+
+ public ContactSyncAdapter(Context context, boolean autoInitialize) {
+ super(context, autoInitialize);
+ mAddrBookUri = null;
+ }
+
+ @Override
+ public void onPerformSync(Account account, Bundle extras, String authority,
+ ContentProviderClient provider, SyncResult syncResult) {
+ setAccount(account);
+ setContentProvider(provider);
+ Cursor c = getLocalContacts(false);
+ if (c.moveToFirst()) {
+ do {
+ String lookup = c.getString(c.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
+ String a = getAddressBookUri();
+ String uri = a + lookup + ".vcf";
+ FileInputStream f;
+ try {
+ f = getContactVcard(lookup);
+ HttpPut query = new HttpPut(uri);
+ byte[] b = new byte[f.available()];
+ f.read(b);
+ query.setEntity(new ByteArrayEntity(b));
+ HttpResponse response = fireRawRequest(query);
+ } catch (IOException e) {
+ e.printStackTrace();
+ return;
+ } catch (OperationCanceledException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (AuthenticatorException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }while (c.moveToNext());
+ //} while (c.moveToNext());