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