26bfff7eeb1c1ab645da9760c200f144eba69a98
[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 synchronization adapter for ownCloud designed to be subclassed for different
47 * resource types, like FileSync, ConcatsSync, CalendarSync, etc..
48 *
49 * Implements the standard {@link AbstractThreadedSyncAdapter}.
50 *
51 * @author sassman
52 * @author David A. Velasco
53 */
54 public abstract class AbstractOwnCloudSyncAdapter extends
55 AbstractThreadedSyncAdapter {
56
57 private AccountManager accountManager;
58 private Account account;
59 private ContentProviderClient contentProvider;
60 //private Date lastUpdated;
61 private DataStorageManager mStoreManager;
62
63 private WebdavClient mClient = null;
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 void setStorageManager(DataStorageManager storage_manager) {
95 mStoreManager = storage_manager;
96 }
97
98 public DataStorageManager getStorageManager() {
99 return mStoreManager;
100 }
101
102 protected void initClientForCurrentAccount() throws OperationCanceledException, AuthenticatorException, IOException, AccountNotFoundException {
103 AccountUtils.constructFullURLForAccount(getContext(), account);
104 mClient = OwnCloudClientUtils.createOwnCloudClient(account, getContext());
105 }
106
107 protected WebdavClient getClient() {
108 return mClient;
109 }
110
111
112 /* method called by ContactSyncAdapter, that is never used */
113 protected HttpResponse fireRawRequest(HttpRequest query)
114 throws ClientProtocolException, OperationCanceledException,
115 AuthenticatorException, IOException {
116 /*
117 * BasicHttpContext httpContext = new BasicHttpContext(); BasicScheme
118 * basicAuth = new BasicScheme();
119 * httpContext.setAttribute("preemptive-auth", basicAuth);
120 *
121 * HttpResponse response = getClient().execute(mHost, query,
122 * httpContext);
123 */
124 return null;
125 }
126
127 /* methods never used below */
128 /*
129 public Date getLastUpdated() {
130 return lastUpdated;
131 }
132
133 public void setLastUpdated(Date lastUpdated) {
134 this.lastUpdated = lastUpdated;
135 }
136
137 protected ConnectionKeepAliveStrategy getKeepAliveStrategy() {
138 return new ConnectionKeepAliveStrategy() {
139 public long getKeepAliveDuration(HttpResponse response,
140 HttpContext context) {
141 // Change keep alive straategy basing on response: ie
142 // forbidden/not found/etc
143 // should have keep alive 0
144 // default return: 5s
145 int statusCode = response.getStatusLine().getStatusCode();
146
147 // HTTP 400, 500 Errors as well as HTTP 118 - Connection timed
148 // out
149 if ((statusCode >= 400 && statusCode <= 418)
150 || (statusCode >= 421 && statusCode <= 426)
151 || (statusCode >= 500 && statusCode <= 510)
152 || statusCode == 118) {
153 return 0;
154 }
155
156 return 5 * 1000;
157 }
158 };
159 }
160 */
161 }