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