1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
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.
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/>.
19 package com
.owncloud
.android
.files
.services
;
21 import java
.util
.ArrayList
;
22 import java
.util
.List
;
24 import com
.owncloud
.android
.AccountUtils
;
25 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
26 import com
.owncloud
.android
.db
.ProviderMeta
.ProviderTableMeta
;
27 import com
.owncloud
.android
.files
.OwnCloudFileObserver
;
28 import com
.owncloud
.android
.files
.OwnCloudFileObserver
.FileObserverStatusListener
;
29 import com
.owncloud
.android
.ui
.activity
.ConflictsResolveActivity
;
31 import android
.accounts
.Account
;
32 import android
.accounts
.AccountManager
;
33 import android
.app
.Service
;
34 import android
.content
.BroadcastReceiver
;
35 import android
.content
.Context
;
36 import android
.content
.Intent
;
37 import android
.content
.IntentFilter
;
38 import android
.database
.Cursor
;
39 import android
.os
.Binder
;
40 import android
.os
.IBinder
;
41 import android
.util
.Log
;
43 public class FileObserverService
extends Service
implements FileObserverStatusListener
{
45 public final static String KEY_FILE_CMD
= "KEY_FILE_CMD";
46 public final static String KEY_CMD_ARG
= "KEY_CMD_ARG";
48 public final static int CMD_INIT_OBSERVED_LIST
= 1;
49 public final static int CMD_ADD_OBSERVED_FILE
= 2;
50 public final static int CMD_DEL_OBSERVED_FILE
= 3;
51 public final static int CMD_ADD_DOWNLOADING_FILE
= 4;
53 private static String TAG
= FileObserverService
.class.getSimpleName();
54 private static List
<OwnCloudFileObserver
> mObservers
;
55 private static List
<DownloadCompletedReceiver
> mDownloadReceivers
;
56 private static Object mReceiverListLock
= new Object();
57 private IBinder mBinder
= new LocalBinder();
59 public class LocalBinder
extends Binder
{
60 FileObserverService
getService() {
61 return FileObserverService
.this;
66 public IBinder
onBind(Intent intent
) {
71 public int onStartCommand(Intent intent
, int flags
, int startId
) {
72 // this occurs when system tries to restart
73 // service, so we need to reinitialize observers
75 initializeObservedList();
76 return Service
.START_STICKY
;
79 if (!intent
.hasExtra(KEY_FILE_CMD
)) {
80 Log
.e(TAG
, "No KEY_FILE_CMD argument given");
81 return Service
.START_STICKY
;
84 switch (intent
.getIntExtra(KEY_FILE_CMD
, -1)) {
85 case CMD_INIT_OBSERVED_LIST
:
86 initializeObservedList();
88 case CMD_ADD_OBSERVED_FILE
:
89 addObservedFile(intent
.getStringExtra(KEY_CMD_ARG
));
91 case CMD_DEL_OBSERVED_FILE
:
92 removeObservedFile(intent
.getStringExtra(KEY_CMD_ARG
));
94 case CMD_ADD_DOWNLOADING_FILE
:
95 addDownloadingFile(intent
.getStringExtra(KEY_CMD_ARG
));
98 Log
.wtf(TAG
, "Incorrect key given");
101 return Service
.START_STICKY
;
104 private void initializeObservedList() {
105 if (mObservers
!= null
) return; // nothing to do here
106 mObservers
= new ArrayList
<OwnCloudFileObserver
>();
107 mDownloadReceivers
= new ArrayList
<DownloadCompletedReceiver
>();
108 Cursor c
= getContentResolver().query(
109 ProviderTableMeta
.CONTENT_URI
,
111 ProviderTableMeta
.FILE_KEEP_IN_SYNC
+ " = ?",
112 new String
[] {String
.valueOf(1)},
114 if (!c
.moveToFirst()) return;
115 AccountManager acm
= AccountManager
.get(this);
116 Account
[] accounts
= acm
.getAccounts();
118 Account account
= null
;
119 for (Account a
: accounts
)
120 if (a
.name
.equals(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_ACCOUNT_OWNER
)))) {
125 if (account
== null
) continue;
126 FileDataStorageManager storage
=
127 new FileDataStorageManager(account
, getContentResolver());
128 if (!storage
.fileExists(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_PATH
))))
131 String path
= c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_STORAGE_PATH
));
132 OwnCloudFileObserver observer
=
133 new OwnCloudFileObserver(path
, OwnCloudFileObserver
.CHANGES_ONLY
);
134 observer
.setContext(getApplicationContext());
135 observer
.setAccount(account
);
136 observer
.setStorageManager(storage
);
137 observer
.setOCFile(storage
.getFileByPath(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_PATH
))));
138 observer
.addObserverStatusListener(this);
139 observer
.startWatching();
140 mObservers
.add(observer
);
141 Log
.d(TAG
, "Started watching file " + path
);
143 } while (c
.moveToNext());
147 private void addObservedFile(String path
) {
148 if (path
== null
) return;
149 if (mObservers
== null
) {
150 // this is very rare case when service was killed by system
151 // and observers list was deleted in that procedure
152 initializeObservedList();
154 boolean duplicate
= false
;
155 OwnCloudFileObserver observer
= null
;
156 for (int i
= 0; i
< mObservers
.size(); ++i
) {
157 observer
= mObservers
.get(i
);
158 if (observer
.getPath().equals(path
))
160 observer
.setContext(getBaseContext());
162 if (duplicate
) return;
163 observer
= new OwnCloudFileObserver(path
, OwnCloudFileObserver
.CHANGES_ONLY
);
164 observer
.setContext(getBaseContext());
165 Account account
= AccountUtils
.getCurrentOwnCloudAccount(getBaseContext());
166 observer
.setAccount(account
);
167 FileDataStorageManager storage
=
168 new FileDataStorageManager(account
, getContentResolver());
169 observer
.setStorageManager(storage
);
170 observer
.setOCFile(storage
.getFileByLocalPath(path
));
171 observer
.addObserverStatusListener(this);
173 DownloadCompletedReceiver receiver
= new DownloadCompletedReceiver(path
, observer
);
174 registerReceiver(receiver
, new IntentFilter(FileDownloader
.DOWNLOAD_FINISH_MESSAGE
));
176 mObservers
.add(observer
);
177 Log
.d(TAG
, "Observer added for path " + path
);
180 private void removeObservedFile(String path
) {
181 if (path
== null
) return;
182 if (mObservers
== null
) {
183 initializeObservedList();
185 for (int i
= 0; i
< mObservers
.size(); ++i
) {
186 OwnCloudFileObserver observer
= mObservers
.get(i
);
187 if (observer
.getPath().equals(path
)) {
188 observer
.stopWatching();
189 mObservers
.remove(i
);
190 Log
.d(TAG
, "Stopped watching " + path
);
196 private void addDownloadingFile(String remotePath
) {
197 OwnCloudFileObserver observer
= null
;
198 for (OwnCloudFileObserver o
: mObservers
) {
199 if (o
.getRemotePath().equals(remotePath
)) {
204 if (observer
== null
) {
205 Log
.e(TAG
, "Couldn't find observer for remote file " + remotePath
);
208 observer
.stopWatching();
209 DownloadCompletedReceiver dcr
= new DownloadCompletedReceiver(observer
.getPath(), observer
);
210 registerReceiver(dcr
, new IntentFilter(FileDownloader
.DOWNLOAD_FINISH_MESSAGE
));
214 private static void addReceiverToList(DownloadCompletedReceiver r
) {
215 synchronized(mReceiverListLock
) {
216 mDownloadReceivers
.add(r
);
220 private static void removeReceiverFromList(DownloadCompletedReceiver r
) {
221 synchronized(mReceiverListLock
) {
222 mDownloadReceivers
.remove(r
);
227 public void OnObservedFileStatusUpdate(String localPath
, String remotePath
, Account account
, Status status
) {
231 // ISSUE 5: if the user is not running the app (this is a service!), this can be very intrusive; a notification should be preferred
232 Intent i
= new Intent(getApplicationContext(), ConflictsResolveActivity
.class);
233 i
.setFlags(i
.getFlags() | Intent
.FLAG_ACTIVITY_NEW_TASK
);
234 i
.putExtra("remotepath", remotePath
);
235 i
.putExtra("localpath", localPath
);
236 i
.putExtra("account", account
);
240 case SENDING_TO_UPLOADER
:
244 Log
.wtf(TAG
, "Unhandled status " + status
);
248 private class DownloadCompletedReceiver
extends BroadcastReceiver
{
250 OwnCloudFileObserver mObserver
;
252 public DownloadCompletedReceiver(String path
, OwnCloudFileObserver observer
) {
254 mObserver
= observer
;
255 addReceiverToList(this);
259 public void onReceive(Context context
, Intent intent
) {
260 if (mPath
.equals(intent
.getStringExtra(FileDownloader
.EXTRA_FILE_PATH
))) { // ISSUE 3: this condition will be false if the download failed; in that case, the download won't ever be retried
261 context
.unregisterReceiver(this);
262 removeReceiverFromList(this);
263 mObserver
.startWatching();
264 Log
.d(TAG
, "Started watching " + mPath
);
270 public boolean equals(Object o
) {
271 if (o
instanceof DownloadCompletedReceiver
)
272 return mPath
.equals(((DownloadCompletedReceiver
)o
).mPath
);
273 return super.equals(o
);