3d9a766fd72e33ea70ffba3053b419d4d89aa453
[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 AbstractThreadedSyncAdapter {
51
52 private AccountManager accountManager;
53 private Account account;
54 private ContentProviderClient contentProvider;
55 private Date lastUpdated;
56 private DataStorageManager mStoreManager;
57
58 private WebdavClient mClient = null;
59
60 public AbstractOwnCloudSyncAdapter(Context context, boolean autoInitialize) {
61 super(context, autoInitialize);
62 this.setAccountManager(AccountManager.get(context));
63 }
64
65 public AccountManager getAccountManager() {
66 return accountManager;
67 }
68
69 public void setAccountManager(AccountManager accountManager) {
70 this.accountManager = accountManager;
71 }
72
73 public Account getAccount() {
74 return account;
75 }
76
77 public void setAccount(Account account) {
78 this.account = account;
79 }
80
81 public ContentProviderClient getContentProvider() {
82 return contentProvider;
83 }
84
85 public void setContentProvider(ContentProviderClient contentProvider) {
86 this.contentProvider = contentProvider;
87 }
88
89 public Date getLastUpdated() {
90 return lastUpdated;
91 }
92
93 public void setLastUpdated(Date lastUpdated) {
94 this.lastUpdated = lastUpdated;
95 }
96
97 public void setStorageManager(DataStorageManager storage_manager) {
98 mStoreManager = storage_manager;
99 }
100
101 public DataStorageManager getStorageManager() {
102 return mStoreManager;
103 }
104
105 protected ConnectionKeepAliveStrategy getKeepAliveStrategy() {
106 return new ConnectionKeepAliveStrategy() {
107 public long getKeepAliveDuration(HttpResponse response,
108 HttpContext context) {
109 // Change keep alive straategy basing on response: ie
110 // forbidden/not found/etc
111 // should have keep alive 0
112 // default return: 5s
113 int statusCode = response.getStatusLine().getStatusCode();
114
115 // HTTP 400, 500 Errors as well as HTTP 118 - Connection timed
116 // out
117 if ((statusCode >= 400 && statusCode <= 418)
118 || (statusCode >= 421 && statusCode <= 426)
119 || (statusCode >= 500 && statusCode <= 510)
120 || statusCode == 118) {
121 return 0;
122 }
123
124 return 5 * 1000;
125 }
126 };
127 }
128
129 protected HttpResponse fireRawRequest(HttpRequest query)
130 throws ClientProtocolException, OperationCanceledException,
131 AuthenticatorException, IOException {
132 /*BasicHttpContext httpContext = new BasicHttpContext();
133 BasicScheme basicAuth = new BasicScheme();
134 httpContext.setAttribute("preemptive-auth", basicAuth);
135
136 HttpResponse response = getClient().execute(mHost, query, httpContext);*/
137 return null;
138 }
139
140 protected Uri getUri() {
141 return Uri.parse(this.getAccountManager().getUserData(getAccount(),
142 AccountAuthenticator.KEY_OC_URL));
143 }
144
145 protected WebdavClient getClient() throws OperationCanceledException,
146 AuthenticatorException, IOException {
147 if (mClient == null) {
148 String username = getAccount().name.split("@")[0];
149 String password = this.getAccountManager().blockingGetAuthToken(
150 getAccount(), AccountAuthenticator.AUTH_TOKEN_TYPE, true);
151 if (this.getAccountManager().getUserData(getAccount(),
152 AccountAuthenticator.KEY_OC_URL) == null) {
153 throw new UnknownHostException();
154 }
155 Uri uri = getUri();
156
157 mClient = new WebdavClient(uri);
158 mClient.setCredentials(username, password);
159 mClient.allowUnsignedCertificates();
160 //mHost = mClient.getTargetHost();
161 }
162
163 return mClient;
164 }
165 }