6fe23e78aba29eac7c83f798325cd76262a0416e
[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
23 import org.apache.http.HttpRequest;
24 import org.apache.http.HttpResponse;
25 import org.apache.http.client.ClientProtocolException;
26
27 import com.owncloud.android.MainApp;
28 import com.owncloud.android.datamodel.FileDataStorageManager;
29 import com.owncloud.android.lib.common.accounts.AccountUtils;
30 import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException;
31 import com.owncloud.android.lib.common.OwnCloudAccount;
32 import com.owncloud.android.lib.common.OwnCloudClient;
33 import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
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
43 /**
44 * Base synchronization adapter for ownCloud designed to be subclassed for different
45 * resource types, like FileSync, ConcatsSync, CalendarSync, etc..
46 *
47 * Implements the standard {@link AbstractThreadedSyncAdapter}.
48 *
49 * @author sassman
50 * @author David A. Velasco
51 */
52 public abstract class AbstractOwnCloudSyncAdapter extends
53 AbstractThreadedSyncAdapter {
54
55 private AccountManager accountManager;
56 private Account account;
57 private ContentProviderClient mContentProviderClient;
58 private FileDataStorageManager mStoreManager;
59
60 private OwnCloudClient mClient = null;
61
62 public AbstractOwnCloudSyncAdapter(Context context, boolean autoInitialize) {
63 super(context, autoInitialize);
64 this.setAccountManager(AccountManager.get(context));
65 }
66
67 public AbstractOwnCloudSyncAdapter(Context context, boolean autoInitialize, boolean allowParallelSyncs) {
68 super(context, autoInitialize, allowParallelSyncs);
69 this.setAccountManager(AccountManager.get(context));
70 }
71
72 public AccountManager getAccountManager() {
73 return accountManager;
74 }
75
76 public void setAccountManager(AccountManager accountManager) {
77 this.accountManager = accountManager;
78 }
79
80 public Account getAccount() {
81 return account;
82 }
83
84 public void setAccount(Account account) {
85 this.account = account;
86 }
87
88 public ContentProviderClient getContentProviderClient() {
89 return mContentProviderClient;
90 }
91
92 public void setContentProviderClient(ContentProviderClient contentProvider) {
93 this.mContentProviderClient = contentProvider;
94 }
95
96 public void setStorageManager(FileDataStorageManager storage_manager) {
97 mStoreManager = storage_manager;
98 }
99
100 public FileDataStorageManager getStorageManager() {
101 return mStoreManager;
102 }
103
104 protected void initClientForCurrentAccount() throws OperationCanceledException, AuthenticatorException, IOException, AccountNotFoundException {
105 AccountUtils.constructFullURLForAccount(getContext(), account);
106 OwnCloudAccount ocAccount = new OwnCloudAccount(account, getContext());
107 mClient = OwnCloudClientManagerFactory.getDefaultSingleton().
108 getClientFor(ocAccount, getContext());
109 }
110
111 protected OwnCloudClient getClient() {
112 return mClient;
113 }
114
115
116 /* method called by ContactSyncAdapter, that is never used */
117 protected HttpResponse fireRawRequest(HttpRequest query)
118 throws ClientProtocolException, OperationCanceledException,
119 AuthenticatorException, IOException {
120 return null;
121 }
122
123 }