AbstractOwnCloudSyncAdapter.java 5.5 KB

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