Copyright note fixes
[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.AccountUtils;
32 import com.owncloud.android.datamodel.DataStorageManager;
33 import com.owncloud.android.network.OwnCloudClientUtils;
34
35 import android.accounts.Account;
36 import android.accounts.AccountManager;
37 import android.accounts.AuthenticatorException;
38 import android.accounts.OperationCanceledException;
39 import android.content.AbstractThreadedSyncAdapter;
40 import android.content.ContentProviderClient;
41 import android.content.Context;
42 import eu.alefzero.webdav.WebdavClient;
43
44 /**
45 * Base SyncAdapter for OwnCloud Designed to be subclassed for the concrete
46 * SyncAdapter, like ConcatsSync, CalendarSync, FileSync etc..
47 *
48 * @author sassman
49 *
50 */
51 public abstract class AbstractOwnCloudSyncAdapter extends
52 AbstractThreadedSyncAdapter {
53
54 private AccountManager accountManager;
55 private Account account;
56 private ContentProviderClient contentProvider;
57 private Date lastUpdated;
58 private DataStorageManager mStoreManager;
59
60 private WebdavClient mClient = null;
61
62 public AbstractOwnCloudSyncAdapter(Context context, boolean autoInitialize) {
63 super(context, autoInitialize);
64 this.setAccountManager(AccountManager.get(context));
65 }
66
67 public AccountManager getAccountManager() {
68 return accountManager;
69 }
70
71 public void setAccountManager(AccountManager accountManager) {
72 this.accountManager = accountManager;
73 }
74
75 public Account getAccount() {
76 return account;
77 }
78
79 public void setAccount(Account account) {
80 this.account = account;
81 }
82
83 public ContentProviderClient getContentProvider() {
84 return contentProvider;
85 }
86
87 public void setContentProvider(ContentProviderClient contentProvider) {
88 this.contentProvider = contentProvider;
89 }
90
91 public Date getLastUpdated() {
92 return lastUpdated;
93 }
94
95 public void setLastUpdated(Date lastUpdated) {
96 this.lastUpdated = lastUpdated;
97 }
98
99 public void setStorageManager(DataStorageManager storage_manager) {
100 mStoreManager = storage_manager;
101 }
102
103 public DataStorageManager getStorageManager() {
104 return mStoreManager;
105 }
106
107 protected ConnectionKeepAliveStrategy getKeepAliveStrategy() {
108 return new ConnectionKeepAliveStrategy() {
109 public long getKeepAliveDuration(HttpResponse response,
110 HttpContext context) {
111 // Change keep alive straategy basing on response: ie
112 // forbidden/not found/etc
113 // should have keep alive 0
114 // default return: 5s
115 int statusCode = response.getStatusLine().getStatusCode();
116
117 // HTTP 400, 500 Errors as well as HTTP 118 - Connection timed
118 // out
119 if ((statusCode >= 400 && statusCode <= 418)
120 || (statusCode >= 421 && statusCode <= 426)
121 || (statusCode >= 500 && statusCode <= 510)
122 || statusCode == 118) {
123 return 0;
124 }
125
126 return 5 * 1000;
127 }
128 };
129 }
130
131 protected HttpResponse fireRawRequest(HttpRequest query)
132 throws ClientProtocolException, OperationCanceledException,
133 AuthenticatorException, IOException {
134 /*
135 * BasicHttpContext httpContext = new BasicHttpContext(); BasicScheme
136 * basicAuth = new BasicScheme();
137 * httpContext.setAttribute("preemptive-auth", basicAuth);
138 *
139 * HttpResponse response = getClient().execute(mHost, query,
140 * httpContext);
141 */
142 return null;
143 }
144
145 protected void initClientForCurrentAccount() throws UnknownHostException {
146 if (AccountUtils.constructFullURLForAccount(getContext(), account) == null) {
147 throw new UnknownHostException();
148 }
149 mClient = OwnCloudClientUtils.createOwnCloudClient(account, getContext());
150 }
151
152 protected WebdavClient getClient() {
153 return mClient;
154 }
155 }