77a7c361e484f7a846416644030f6f48032ca15b
[pub/Android/ownCloud.git] / src / com / owncloud / android / syncadapter / AbstractOwnCloudSyncAdapter.java
1 /* ownCloud Android client application
2 *
3 * @author sassman
4 * @author David A. Velasco
5 * Copyright (C) 2011 Bartek Przybylski
6 * Copyright (C) 2012-2013 ownCloud Inc.
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2,
10 * as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22 package com.owncloud.android.syncadapter;
23
24 import java.io.IOException;
25
26 import org.apache.http.HttpRequest;
27 import org.apache.http.HttpResponse;
28 import org.apache.http.client.ClientProtocolException;
29
30 import com.owncloud.android.datamodel.FileDataStorageManager;
31 import com.owncloud.android.lib.common.accounts.AccountUtils;
32 import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException;
33 import com.owncloud.android.lib.common.OwnCloudAccount;
34 import com.owncloud.android.lib.common.OwnCloudClient;
35 import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
36
37 import android.accounts.Account;
38 import android.accounts.AccountManager;
39 import android.accounts.AuthenticatorException;
40 import android.accounts.OperationCanceledException;
41 import android.content.AbstractThreadedSyncAdapter;
42 import android.content.ContentProviderClient;
43 import android.content.Context;
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 public abstract class AbstractOwnCloudSyncAdapter extends
52 AbstractThreadedSyncAdapter {
53
54 private AccountManager accountManager;
55 private Account account;
56 private ContentProviderClient mContentProviderClient;
57 private FileDataStorageManager mStoreManager;
58
59 private OwnCloudClient mClient = null;
60
61 public AbstractOwnCloudSyncAdapter(Context context, boolean autoInitialize) {
62 super(context, autoInitialize);
63 this.setAccountManager(AccountManager.get(context));
64 }
65
66 public AbstractOwnCloudSyncAdapter(Context context, boolean autoInitialize, boolean allowParallelSyncs) {
67 super(context, autoInitialize, allowParallelSyncs);
68 this.setAccountManager(AccountManager.get(context));
69 }
70
71 public AccountManager getAccountManager() {
72 return accountManager;
73 }
74
75 public void setAccountManager(AccountManager accountManager) {
76 this.accountManager = accountManager;
77 }
78
79 public Account getAccount() {
80 return account;
81 }
82
83 public void setAccount(Account account) {
84 this.account = account;
85 }
86
87 public ContentProviderClient getContentProviderClient() {
88 return mContentProviderClient;
89 }
90
91 public void setContentProviderClient(ContentProviderClient contentProvider) {
92 this.mContentProviderClient = contentProvider;
93 }
94
95 public void setStorageManager(FileDataStorageManager storage_manager) {
96 mStoreManager = storage_manager;
97 }
98
99 public FileDataStorageManager getStorageManager() {
100 return mStoreManager;
101 }
102
103 protected void initClientForCurrentAccount() throws OperationCanceledException, AuthenticatorException, IOException, AccountNotFoundException {
104 AccountUtils.constructFullURLForAccount(getContext(), account);
105 OwnCloudAccount ocAccount = new OwnCloudAccount(account, getContext());
106 mClient = OwnCloudClientManagerFactory.getDefaultSingleton().
107 getClientFor(ocAccount, getContext());
108 }
109
110 protected OwnCloudClient getClient() {
111 return mClient;
112 }
113
114
115 /* method called by ContactSyncAdapter, that is never used */
116 protected HttpResponse fireRawRequest(HttpRequest query)
117 throws ClientProtocolException, OperationCanceledException,
118 AuthenticatorException, IOException {
119 return null;
120 }
121
122 }