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
.datamodel
;
22 import java
.util
.ArrayList
;
23 import java
.util
.Collections
;
24 import java
.util
.Iterator
;
25 import java
.util
.List
;
26 import java
.util
.Vector
;
28 import com
.owncloud
.android
.db
.ProviderMeta
;
29 import com
.owncloud
.android
.db
.ProviderMeta
.ProviderTableMeta
;
30 import com
.owncloud
.android
.utils
.FileStorageUtils
;
32 import android
.accounts
.Account
;
33 import android
.content
.ContentProviderClient
;
34 import android
.content
.ContentProviderOperation
;
35 import android
.content
.ContentProviderResult
;
36 import android
.content
.ContentResolver
;
37 import android
.content
.ContentValues
;
38 import android
.content
.OperationApplicationException
;
39 import android
.database
.Cursor
;
40 import android
.net
.Uri
;
41 import android
.os
.RemoteException
;
42 import android
.util
.Log
;
44 public class FileDataStorageManager
implements DataStorageManager
{
46 private ContentResolver mContentResolver
;
47 private ContentProviderClient mContentProvider
;
48 private Account mAccount
;
50 private static String TAG
= "FileDataStorageManager";
52 public FileDataStorageManager(Account account
, ContentResolver cr
) {
53 mContentProvider
= null
;
54 mContentResolver
= cr
;
58 public FileDataStorageManager(Account account
, ContentProviderClient cp
) {
59 mContentProvider
= cp
;
60 mContentResolver
= null
;
65 public OCFile
getFileByPath(String path
) {
66 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_PATH
, path
);
68 if (c
.moveToFirst()) {
69 file
= createFileInstance(c
);
76 public OCFile
getFileById(long id
) {
77 Cursor c
= getCursorForValue(ProviderTableMeta
._ID
, String
.valueOf(id
));
79 if (c
.moveToFirst()) {
80 file
= createFileInstance(c
);
86 public OCFile
getFileByLocalPath(String path
) {
87 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_STORAGE_PATH
, path
);
89 if (c
.moveToFirst()) {
90 file
= createFileInstance(c
);
97 public boolean fileExists(long id
) {
98 return fileExists(ProviderTableMeta
._ID
, String
.valueOf(id
));
102 public boolean fileExists(String path
) {
103 return fileExists(ProviderTableMeta
.FILE_PATH
, path
);
107 public boolean saveFile(OCFile file
) {
108 boolean overriden
= false
;
109 ContentValues cv
= new ContentValues();
110 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
111 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
112 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
113 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
114 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
115 if (file
.getParentId() != 0)
116 cv
.put(ProviderTableMeta
.FILE_PARENT
, file
.getParentId());
117 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
118 if (!file
.isDirectory())
119 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
120 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
121 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDate());
122 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.keepInSync() ?
1 : 0);
124 if (fileExists(file
.getRemotePath())) {
125 OCFile oldFile
= getFileByPath(file
.getRemotePath());
126 if (file
.getStoragePath() == null
&& oldFile
.getStoragePath() != null
)
127 file
.setStoragePath(oldFile
.getStoragePath());
128 if (!file
.isDirectory());
129 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
130 file
.setFileId(oldFile
.getFileId());
133 if (getContentResolver() != null
) {
134 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
,
135 ProviderTableMeta
._ID
+ "=?",
136 new String
[] { String
.valueOf(file
.getFileId()) });
139 getContentProvider().update(ProviderTableMeta
.CONTENT_URI
,
140 cv
, ProviderTableMeta
._ID
+ "=?",
141 new String
[] { String
.valueOf(file
.getFileId()) });
142 } catch (RemoteException e
) {
144 "Fail to insert insert file to database "
149 Uri result_uri
= null
;
150 if (getContentResolver() != null
) {
151 result_uri
= getContentResolver().insert(
152 ProviderTableMeta
.CONTENT_URI_FILE
, cv
);
155 result_uri
= getContentProvider().insert(
156 ProviderTableMeta
.CONTENT_URI_FILE
, cv
);
157 } catch (RemoteException e
) {
159 "Fail to insert insert file to database "
163 if (result_uri
!= null
) {
164 long new_id
= Long
.parseLong(result_uri
.getPathSegments()
166 file
.setFileId(new_id
);
170 if (file
.isDirectory() && file
.needsUpdatingWhileSaving())
171 for (OCFile f
: getDirectoryContent(file
))
179 public void saveFiles(List
<OCFile
> files
) {
181 Iterator
<OCFile
> filesIt
= files
.iterator();
182 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>(files
.size());
185 // prepare operations to perform
186 while (filesIt
.hasNext()) {
187 file
= filesIt
.next();
188 ContentValues cv
= new ContentValues();
189 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
190 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
191 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
192 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
193 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
194 if (file
.getParentId() != 0)
195 cv
.put(ProviderTableMeta
.FILE_PARENT
, file
.getParentId());
196 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
197 if (!file
.isDirectory())
198 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
199 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
200 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDate());
201 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.keepInSync() ?
1 : 0);
203 if (fileExists(file
.getRemotePath())) {
204 OCFile tmpfile
= getFileByPath(file
.getRemotePath());
205 file
.setStoragePath(tmpfile
.getStoragePath());
206 if (!file
.isDirectory());
207 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
208 file
.setFileId(tmpfile
.getFileId());
210 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
212 withSelection( ProviderTableMeta
._ID
+ "=?",
213 new String
[] { String
.valueOf(file
.getFileId()) })
217 operations
.add(ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI
).withValues(cv
).build());
221 // apply operations in batch
222 ContentProviderResult
[] results
= null
;
224 if (getContentResolver() != null
) {
225 results
= getContentResolver().applyBatch(ProviderMeta
.AUTHORITY_FILES
, operations
);
228 results
= getContentProvider().applyBatch(operations
);
231 } catch (OperationApplicationException e
) {
232 Log
.e(TAG
, "Fail to update/insert list of files to database " + e
.getMessage());
234 } catch (RemoteException e
) {
235 Log
.e(TAG
, "Fail to update/insert list of files to database " + e
.getMessage());
238 // update new id in file objects for insertions
239 if (results
!= null
) {
241 for (int i
=0; i
<results
.length
; i
++) {
242 if (results
[i
].uri
!= null
) {
243 newId
= Long
.parseLong(results
[i
].uri
.getPathSegments().get(1));
244 files
.get(i
).setFileId(newId
);
245 //Log.v(TAG, "Found and added id in insertion for " + files.get(i).getRemotePath());
250 for (OCFile aFile
: files
) {
251 if (aFile
.isDirectory() && aFile
.needsUpdatingWhileSaving())
252 saveFiles(getDirectoryContent(aFile
));
257 public void setAccount(Account account
) {
261 public Account
getAccount() {
265 public void setContentResolver(ContentResolver cr
) {
266 mContentResolver
= cr
;
269 public ContentResolver
getContentResolver() {
270 return mContentResolver
;
273 public void setContentProvider(ContentProviderClient cp
) {
274 mContentProvider
= cp
;
277 public ContentProviderClient
getContentProvider() {
278 return mContentProvider
;
282 public Vector
<OCFile
> getDirectoryContent(OCFile f
) {
283 if (f
!= null
&& f
.isDirectory() && f
.getFileId() != -1) {
284 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
286 Uri req_uri
= Uri
.withAppendedPath(
287 ProviderTableMeta
.CONTENT_URI_DIR
,
288 String
.valueOf(f
.getFileId()));
291 if (getContentProvider() != null
) {
293 c
= getContentProvider().query(req_uri
, null
,
294 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?",
295 new String
[] { mAccount
.name
}, null
);
296 } catch (RemoteException e
) {
297 Log
.e(TAG
, e
.getMessage());
301 c
= getContentResolver().query(req_uri
, null
,
302 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?",
303 new String
[] { mAccount
.name
}, null
);
306 if (c
.moveToFirst()) {
308 OCFile child
= createFileInstance(c
);
310 } while (c
.moveToNext());
315 Collections
.sort(ret
);
322 private boolean fileExists(String cmp_key
, String value
) {
324 if (getContentResolver() != null
) {
325 c
= getContentResolver()
326 .query(ProviderTableMeta
.CONTENT_URI
,
329 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
331 new String
[] { value
, mAccount
.name
}, null
);
334 c
= getContentProvider().query(
335 ProviderTableMeta
.CONTENT_URI
,
338 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?",
339 new String
[] { value
, mAccount
.name
}, null
);
340 } catch (RemoteException e
) {
342 "Couldn't determine file existance, assuming non existance: "
347 boolean retval
= c
.moveToFirst();
352 private Cursor
getCursorForValue(String key
, String value
) {
354 if (getContentResolver() != null
) {
355 c
= getContentResolver()
356 .query(ProviderTableMeta
.CONTENT_URI
,
359 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
361 new String
[] { value
, mAccount
.name
}, null
);
364 c
= getContentProvider().query(
365 ProviderTableMeta
.CONTENT_URI
,
367 key
+ "=? AND " + ProviderTableMeta
.FILE_ACCOUNT_OWNER
368 + "=?", new String
[] { value
, mAccount
.name
},
370 } catch (RemoteException e
) {
371 Log
.e(TAG
, "Could not get file details: " + e
.getMessage());
378 private OCFile
createFileInstance(Cursor c
) {
381 file
= new OCFile(c
.getString(c
382 .getColumnIndex(ProviderTableMeta
.FILE_PATH
)));
383 file
.setFileId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
)));
384 file
.setParentId(c
.getLong(c
385 .getColumnIndex(ProviderTableMeta
.FILE_PARENT
)));
386 file
.setMimetype(c
.getString(c
387 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_TYPE
)));
388 if (!file
.isDirectory()) {
389 file
.setStoragePath(c
.getString(c
390 .getColumnIndex(ProviderTableMeta
.FILE_STORAGE_PATH
)));
391 if (file
.getStoragePath() == null
) {
392 // try to find existing file and bind it with current account
393 File f
= new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, file
));
395 file
.setStoragePath(f
.getAbsolutePath());
398 file
.setFileLength(c
.getLong(c
399 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_LENGTH
)));
400 file
.setCreationTimestamp(c
.getLong(c
401 .getColumnIndex(ProviderTableMeta
.FILE_CREATION
)));
402 file
.setModificationTimestamp(c
.getLong(c
403 .getColumnIndex(ProviderTableMeta
.FILE_MODIFIED
)));
404 file
.setLastSyncDate(c
.getLong(c
405 .getColumnIndex(ProviderTableMeta
.FILE_LAST_SYNC_DATE
)));
406 file
.setKeepInSync(c
.getInt(
407 c
.getColumnIndex(ProviderTableMeta
.FILE_KEEP_IN_SYNC
)) == 1 ? true
: false
);
413 public void removeFile(OCFile file
, boolean removeLocalCopy
) {
414 Uri file_uri
= Uri
.withAppendedPath(ProviderTableMeta
.CONTENT_URI_FILE
, ""+file
.getFileId());
415 if (getContentProvider() != null
) {
417 getContentProvider().delete(file_uri
,
418 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+"=?",
419 new String
[]{mAccount
.name
});
420 } catch (RemoteException e
) {
424 getContentResolver().delete(file_uri
,
425 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+"=?",
426 new String
[]{mAccount
.name
});
428 if (file
.isDown() && removeLocalCopy
) {
429 new File(file
.getStoragePath()).delete();