AbstractOwnCloudSyncAdapter.java 5.6 KB

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