AbstractOwnCloudSyncAdapter.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* ownCloud Android client application
  2. * Copyright (C) 2011 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.IOException;
  20. import java.net.UnknownHostException;
  21. import java.util.Date;
  22. import org.apache.http.HttpRequest;
  23. import org.apache.http.HttpResponse;
  24. import org.apache.http.client.ClientProtocolException;
  25. import org.apache.http.conn.ConnectionKeepAliveStrategy;
  26. import org.apache.http.protocol.HttpContext;
  27. import com.owncloud.android.authentication.AccountUtils;
  28. import com.owncloud.android.authentication.AccountUtils.AccountNotFoundException;
  29. import com.owncloud.android.datamodel.DataStorageManager;
  30. import com.owncloud.android.network.OwnCloudClientUtils;
  31. import android.accounts.Account;
  32. import android.accounts.AccountManager;
  33. import android.accounts.AuthenticatorException;
  34. import android.accounts.OperationCanceledException;
  35. import android.content.AbstractThreadedSyncAdapter;
  36. import android.content.ContentProviderClient;
  37. import android.content.Context;
  38. import eu.alefzero.webdav.WebdavClient;
  39. /**
  40. * Base SyncAdapter for OwnCloud Designed to be subclassed for the concrete
  41. * SyncAdapter, like ConcatsSync, CalendarSync, FileSync etc..
  42. *
  43. * @author sassman
  44. *
  45. */
  46. public abstract class AbstractOwnCloudSyncAdapter extends
  47. AbstractThreadedSyncAdapter {
  48. private AccountManager accountManager;
  49. private Account account;
  50. private ContentProviderClient contentProvider;
  51. private Date lastUpdated;
  52. private DataStorageManager mStoreManager;
  53. private WebdavClient mClient = null;
  54. public AbstractOwnCloudSyncAdapter(Context context, boolean autoInitialize) {
  55. super(context, autoInitialize);
  56. this.setAccountManager(AccountManager.get(context));
  57. }
  58. public AccountManager getAccountManager() {
  59. return accountManager;
  60. }
  61. public void setAccountManager(AccountManager accountManager) {
  62. this.accountManager = accountManager;
  63. }
  64. public Account getAccount() {
  65. return account;
  66. }
  67. public void setAccount(Account account) {
  68. this.account = account;
  69. }
  70. public ContentProviderClient getContentProvider() {
  71. return contentProvider;
  72. }
  73. public void setContentProvider(ContentProviderClient contentProvider) {
  74. this.contentProvider = contentProvider;
  75. }
  76. public Date getLastUpdated() {
  77. return lastUpdated;
  78. }
  79. public void setLastUpdated(Date lastUpdated) {
  80. this.lastUpdated = lastUpdated;
  81. }
  82. public void setStorageManager(DataStorageManager storage_manager) {
  83. mStoreManager = storage_manager;
  84. }
  85. public DataStorageManager getStorageManager() {
  86. return mStoreManager;
  87. }
  88. protected ConnectionKeepAliveStrategy getKeepAliveStrategy() {
  89. return new ConnectionKeepAliveStrategy() {
  90. public long getKeepAliveDuration(HttpResponse response,
  91. HttpContext context) {
  92. // Change keep alive straategy basing on response: ie
  93. // forbidden/not found/etc
  94. // should have keep alive 0
  95. // default return: 5s
  96. int statusCode = response.getStatusLine().getStatusCode();
  97. // HTTP 400, 500 Errors as well as HTTP 118 - Connection timed
  98. // out
  99. if ((statusCode >= 400 && statusCode <= 418)
  100. || (statusCode >= 421 && statusCode <= 426)
  101. || (statusCode >= 500 && statusCode <= 510)
  102. || statusCode == 118) {
  103. return 0;
  104. }
  105. return 5 * 1000;
  106. }
  107. };
  108. }
  109. protected HttpResponse fireRawRequest(HttpRequest query)
  110. throws ClientProtocolException, OperationCanceledException,
  111. AuthenticatorException, IOException {
  112. /*
  113. * BasicHttpContext httpContext = new BasicHttpContext(); BasicScheme
  114. * basicAuth = new BasicScheme();
  115. * httpContext.setAttribute("preemptive-auth", basicAuth);
  116. *
  117. * HttpResponse response = getClient().execute(mHost, query,
  118. * httpContext);
  119. */
  120. return null;
  121. }
  122. protected void initClientForCurrentAccount() throws OperationCanceledException, AuthenticatorException, IOException, AccountNotFoundException {
  123. AccountUtils.constructFullURLForAccount(getContext(), account);
  124. mClient = OwnCloudClientUtils.createOwnCloudClient(account, getContext());
  125. }
  126. protected WebdavClient getClient() {
  127. return mClient;
  128. }
  129. }