Merge branch 'release-1.4.1'
[pub/Android/ownCloud.git] / src / com / owncloud / android / syncadapter / AbstractOwnCloudSyncAdapter.java
1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2,
7 * as published by the Free Software Foundation.
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 com.owncloud.android.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 com.owncloud.android.authentication.AccountUtils;
32 import com.owncloud.android.authentication.AccountUtils.AccountNotFoundException;
33 import com.owncloud.android.datamodel.DataStorageManager;
34 import com.owncloud.android.network.OwnCloudClientUtils;
35
36 import android.accounts.Account;
37 import android.accounts.AccountManager;
38 import android.accounts.AuthenticatorException;
39 import android.accounts.OperationCanceledException;
40 import android.content.AbstractThreadedSyncAdapter;
41 import android.content.ContentProviderClient;
42 import android.content.Context;
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 WebdavClient mClient = null;
62
63 public AbstractOwnCloudSyncAdapter(Context context, boolean autoInitialize) {
64 super(context, autoInitialize);
65 this.setAccountManager(AccountManager.get(context));
66 }
67
68 public AccountManager getAccountManager() {
69 return accountManager;
70 }
71
72 public void setAccountManager(AccountManager accountManager) {
73 this.accountManager = accountManager;
74 }
75
76 public Account getAccount() {
77 return account;
78 }
79
80 public void setAccount(Account account) {
81 this.account = account;
82 }
83
84 public ContentProviderClient getContentProvider() {
85 return contentProvider;
86 }
87
88 public void setContentProvider(ContentProviderClient contentProvider) {
89 this.contentProvider = contentProvider;
90 }
91
92 public Date getLastUpdated() {
93 return lastUpdated;
94 }
95
96 public void setLastUpdated(Date lastUpdated) {
97 this.lastUpdated = lastUpdated;
98 }
99
100 public void setStorageManager(DataStorageManager storage_manager) {
101 mStoreManager = storage_manager;
102 }
103
104 public DataStorageManager getStorageManager() {
105 return mStoreManager;
106 }
107
108 protected ConnectionKeepAliveStrategy getKeepAliveStrategy() {
109 return new ConnectionKeepAliveStrategy() {
110 public long getKeepAliveDuration(HttpResponse response,
111 HttpContext context) {
112 // Change keep alive straategy basing on response: ie
113 // forbidden/not found/etc
114 // should have keep alive 0
115 // default return: 5s
116 int statusCode = response.getStatusLine().getStatusCode();
117
118 // HTTP 400, 500 Errors as well as HTTP 118 - Connection timed
119 // out
120 if ((statusCode >= 400 && statusCode <= 418)
121 || (statusCode >= 421 && statusCode <= 426)
122 || (statusCode >= 500 && statusCode <= 510)
123 || statusCode == 118) {
124 return 0;
125 }
126
127 return 5 * 1000;
128 }
129 };
130 }
131
132 protected HttpResponse fireRawRequest(HttpRequest query)
133 throws ClientProtocolException, OperationCanceledException,
134 AuthenticatorException, IOException {
135 /*
136 * BasicHttpContext httpContext = new BasicHttpContext(); BasicScheme
137 * basicAuth = new BasicScheme();
138 * httpContext.setAttribute("preemptive-auth", basicAuth);
139 *
140 * HttpResponse response = getClient().execute(mHost, query,
141 * httpContext);
142 */
143 return null;
144 }
145
146 protected void initClientForCurrentAccount() throws OperationCanceledException, AuthenticatorException, IOException, AccountNotFoundException {
147 AccountUtils.constructFullURLForAccount(getContext(), account);
148 mClient = OwnCloudClientUtils.createOwnCloudClient(account, getContext());
149 }
150
151 protected WebdavClient getClient() {
152 return mClient;
153 }
154 }