FileSyncAdapter.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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.http.entity.StringEntity;
  21. import android.accounts.Account;
  22. import android.accounts.AuthenticatorException;
  23. import android.accounts.OperationCanceledException;
  24. import android.content.ContentProviderClient;
  25. import android.content.ContentValues;
  26. import android.content.Context;
  27. import android.content.SyncResult;
  28. import android.database.Cursor;
  29. import android.net.Uri;
  30. import android.os.Bundle;
  31. import android.os.RemoteException;
  32. import android.util.Log;
  33. import eu.alefzero.owncloud.db.ProviderMeta.ProviderTableMeta;
  34. import eu.alefzero.webdav.HttpPropFind;
  35. import eu.alefzero.webdav.TreeNode;
  36. import eu.alefzero.webdav.TreeNode.NodeProperty;
  37. import eu.alefzero.webdav.WebdavUtils;
  38. /**
  39. * SyncAdapter implementation for syncing sample SyncAdapter contacts to the
  40. * platform ContactOperations provider.
  41. *
  42. * @author Bartek Przybylski
  43. */
  44. public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {
  45. private static final String TAG = "FileSyncAdapter";
  46. public FileSyncAdapter(Context context, boolean autoInitialize) {
  47. super(context, autoInitialize);
  48. }
  49. @Override
  50. public synchronized void onPerformSync(
  51. Account account,
  52. Bundle extras,
  53. String authority,
  54. ContentProviderClient provider,
  55. SyncResult syncResult) {
  56. try {
  57. this.setAccount(account);
  58. this.setContentProvider(provider);
  59. HttpPropFind query = this.getPropFindQuery();
  60. query.setEntity(new StringEntity(WebdavUtils.prepareXmlForPropFind()));
  61. TreeNode root = this.fireRequest(query);
  62. commitToDatabase(root, null);
  63. } catch (OperationCanceledException e) {
  64. e.printStackTrace();
  65. } catch (AuthenticatorException e) {
  66. syncResult.stats.numAuthExceptions++;
  67. e.printStackTrace();
  68. } catch (IOException e) {
  69. syncResult.stats.numIoExceptions++;
  70. e.printStackTrace();
  71. } catch (RemoteException e) {
  72. e.printStackTrace();
  73. }
  74. }
  75. private void commitToDatabase(TreeNode root, String parentId) throws RemoteException {
  76. for (TreeNode n : root.getChildList()) {
  77. Log.d(TAG, n.toString());
  78. ContentValues cv = new ContentValues();
  79. cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, n.getProperty(NodeProperty.CONTENT_LENGTH));
  80. cv.put(ProviderTableMeta.FILE_MODIFIED, n.getProperty(NodeProperty.LAST_MODIFIED_DATE));
  81. cv.put(ProviderTableMeta.FILE_CONTENT_TYPE, n.getProperty(NodeProperty.RESOURCE_TYPE));
  82. cv.put(ProviderTableMeta.FILE_PARENT, parentId);
  83. String name = n.getProperty(NodeProperty.NAME),
  84. path = n.getProperty(NodeProperty.PATH);
  85. Cursor c = this.getContentProvider().query(ProviderTableMeta.CONTENT_URI_FILE,
  86. null,
  87. ProviderTableMeta.FILE_NAME+"=? AND " + ProviderTableMeta.FILE_PATH + "=? AND " + ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?",
  88. new String[]{name, path, this.getAccount().name},
  89. null);
  90. if (c.moveToFirst()) {
  91. this.getContentProvider().update(ProviderTableMeta.CONTENT_URI,
  92. cv,
  93. ProviderTableMeta._ID+"=?",
  94. new String[]{c.getString(c.getColumnIndex(ProviderTableMeta._ID))});
  95. Log.d(TAG, "ID of: "+name+":"+c.getString(c.getColumnIndex(ProviderTableMeta._ID)));
  96. } else {
  97. cv.put(ProviderTableMeta.FILE_NAME, n.getProperty(NodeProperty.NAME));
  98. cv.put(ProviderTableMeta.FILE_PATH, n.getProperty(NodeProperty.PATH));
  99. cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, this.getAccount().name);
  100. Uri entry = this.getContentProvider().insert(ProviderTableMeta.CONTENT_URI_FILE, cv);
  101. Log.d(TAG, "Inserting new entry " + path);
  102. c = this.getContentProvider().query(entry, null, null, null, null);
  103. c.moveToFirst();
  104. }
  105. if (n.getProperty(NodeProperty.RESOURCE_TYPE).equals("DIR")) {
  106. commitToDatabase(n, c.getString(c.getColumnIndex(ProviderTableMeta._ID)));
  107. }
  108. }
  109. // clean removed files
  110. String[] selection = new String[root.getChildList().size()+2];
  111. selection[0] = this.getAccount().name;
  112. selection[1] = parentId;
  113. String qm = "";
  114. for (int i = 2; i < selection.length-1; ++i) {
  115. qm += "?,";
  116. selection[i] = root.getChildList().get(i-2).getProperty(NodeProperty.NAME);
  117. }
  118. if (selection.length >= 3) {
  119. selection[selection.length-1] = root.getChildrenNames()[selection.length-3];
  120. qm += "?";
  121. }
  122. for (int i = 0; i < selection.length; ++i) {
  123. Log.d(TAG,selection[i]+"");
  124. }
  125. Log.d(TAG,"Removing files "+ parentId);
  126. this.getContentProvider().delete(ProviderTableMeta.CONTENT_URI,
  127. ProviderTableMeta.FILE_ACCOUNT_OWNER+"=? AND " + ProviderTableMeta.FILE_PARENT + (parentId==null?" IS ":"=")+"? AND " + ProviderTableMeta.FILE_NAME + " NOT IN ("+qm+")",
  128. selection);
  129. }
  130. }