FileSyncAdapter.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* ownCloud Android client application
  2. * Copyright (C) 2011 Bartek Przybylski
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  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 eu.alefzero.owncloud.syncadapter;
  19. import java.io.IOException;
  20. import org.apache.jackrabbit.webdav.DavException;
  21. import org.apache.jackrabbit.webdav.MultiStatus;
  22. import org.apache.jackrabbit.webdav.client.methods.PropFindMethod;
  23. import android.accounts.Account;
  24. import android.accounts.AuthenticatorException;
  25. import android.accounts.OperationCanceledException;
  26. import android.content.ContentProviderClient;
  27. import android.content.Context;
  28. import android.content.Intent;
  29. import android.content.SyncResult;
  30. import android.os.Bundle;
  31. import android.util.Log;
  32. import eu.alefzero.owncloud.datamodel.FileDataStorageManager;
  33. import eu.alefzero.owncloud.datamodel.OCFile;
  34. import eu.alefzero.webdav.WebdavEntry;
  35. /**
  36. * SyncAdapter implementation for syncing sample SyncAdapter contacts to the
  37. * platform ContactOperations provider.
  38. *
  39. * @author Bartek Przybylski
  40. */
  41. public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {
  42. public FileSyncAdapter(Context context, boolean autoInitialize) {
  43. super(context, autoInitialize);
  44. }
  45. @Override
  46. public synchronized void onPerformSync(
  47. Account account,
  48. Bundle extras,
  49. String authority,
  50. ContentProviderClient provider,
  51. SyncResult syncResult) {
  52. this.setAccount(account);
  53. this.setContentProvider(provider);
  54. this.setStorageManager(new FileDataStorageManager(account, getContentProvider()));
  55. Log.d("ASD", "syncing owncloud account " + account.name);
  56. Intent i = new Intent(FileSyncService.SYNC_MESSAGE);
  57. i.putExtra(FileSyncService.IN_PROGRESS, true);
  58. i.putExtra(FileSyncService.ACCOUNT_NAME, account.name);
  59. getContext().sendStickyBroadcast(i);
  60. PropFindMethod query;
  61. try {
  62. Log.e("ASD", getUri().toString());
  63. query = new PropFindMethod(getUri().toString()+"/");
  64. getClient().executeMethod(query);
  65. MultiStatus resp = null;
  66. resp = query.getResponseBodyAsMultiStatus();
  67. if (resp.getResponses().length > 0) {
  68. WebdavEntry we = new WebdavEntry(resp.getResponses()[0]);
  69. OCFile file = fillOCFile(we);
  70. file.setParentId(0);
  71. getStorageManager().saveFile(file);
  72. fetchData(getUri().toString(), syncResult, file.getFileId());
  73. }
  74. } catch (OperationCanceledException e) {
  75. e.printStackTrace();
  76. } catch (AuthenticatorException e) {
  77. syncResult.stats.numAuthExceptions++;
  78. e.printStackTrace();
  79. } catch (IOException e) {
  80. syncResult.stats.numIoExceptions++;
  81. e.printStackTrace();
  82. } catch (DavException e) {
  83. syncResult.stats.numIoExceptions++;
  84. e.printStackTrace();
  85. }
  86. i.putExtra(FileSyncService.IN_PROGRESS, false);
  87. getContext().sendStickyBroadcast(i);
  88. }
  89. private void fetchData(String uri, SyncResult syncResult, long parentId) {
  90. try {
  91. PropFindMethod query = new PropFindMethod(uri);
  92. getClient().executeMethod(query);
  93. MultiStatus resp = null;
  94. resp = query.getResponseBodyAsMultiStatus();
  95. for (int i = 1; i < resp.getResponses().length; ++i) {
  96. WebdavEntry we = new WebdavEntry(resp.getResponses()[i]);
  97. OCFile file = fillOCFile(we);
  98. file.setParentId(parentId);
  99. getStorageManager().saveFile(file);
  100. if (parentId == 0) parentId = file.getFileId();
  101. if (we.contentType().equals("DIR"))
  102. fetchData(getUri().toString() + we.path(), syncResult, file.getFileId());
  103. }
  104. } catch (OperationCanceledException e) {
  105. e.printStackTrace();
  106. } catch (AuthenticatorException e) {
  107. syncResult.stats.numAuthExceptions++;
  108. e.printStackTrace();
  109. } catch (IOException e) {
  110. syncResult.stats.numIoExceptions++;
  111. e.printStackTrace();
  112. } catch (DavException e) {
  113. syncResult.stats.numIoExceptions++;
  114. e.printStackTrace();
  115. }
  116. }
  117. private OCFile fillOCFile(WebdavEntry we) {
  118. OCFile file = new OCFile(we.path());
  119. file.setCreationTimestamp(we.createTimestamp());
  120. file.setFileLength(we.contentLength());
  121. file.setMimetype(we.contentType());
  122. file.setModificationTimestamp(we.modifiedTimesamp());
  123. return file;
  124. }
  125. }