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 eu
.alefzero
.owncloud
.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 eu
.alefzero
.owncloud
.db
.ProviderMeta
;
29 import eu
.alefzero
.owncloud
.db
.ProviderMeta
.ProviderTableMeta
;
30 import eu
.alefzero
.owncloud
.files
.services
.FileDownloader
;
31 import android
.accounts
.Account
;
32 import android
.content
.ContentProviderClient
;
33 import android
.content
.ContentProviderOperation
;
34 import android
.content
.ContentProviderResult
;
35 import android
.content
.ContentResolver
;
36 import android
.content
.ContentValues
;
37 import android
.content
.OperationApplicationException
;
38 import android
.database
.Cursor
;
39 import android
.net
.Uri
;
40 import android
.os
.Environment
;
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
);
87 public boolean fileExists(long id
) {
88 return fileExists(ProviderTableMeta
._ID
, String
.valueOf(id
));
92 public boolean fileExists(String path
) {
93 return fileExists(ProviderTableMeta
.FILE_PATH
, path
);
97 public boolean saveFile(OCFile file
) {
98 boolean overriden
= false
;
99 ContentValues cv
= new ContentValues();
100 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
101 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
102 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
103 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
104 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
105 if (file
.getParentId() != 0)
106 cv
.put(ProviderTableMeta
.FILE_PARENT
, file
.getParentId());
107 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
108 if (!file
.isDirectory())
109 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
110 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
111 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDate());
112 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.keepInSync() ?
1 : 0);
114 if (fileExists(file
.getRemotePath())) {
115 OCFile oldFile
= getFileByPath(file
.getRemotePath());
116 if (file
.getStoragePath() == null
&& oldFile
.getStoragePath() != null
)
117 file
.setStoragePath(oldFile
.getStoragePath());
118 if (!file
.isDirectory());
119 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
120 file
.setFileId(oldFile
.getFileId());
123 if (getContentResolver() != null
) {
124 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
,
125 ProviderTableMeta
._ID
+ "=?",
126 new String
[] { String
.valueOf(file
.getFileId()) });
129 getContentProvider().update(ProviderTableMeta
.CONTENT_URI
,
130 cv
, ProviderTableMeta
._ID
+ "=?",
131 new String
[] { String
.valueOf(file
.getFileId()) });
132 } catch (RemoteException e
) {
134 "Fail to insert insert file to database "
139 Uri result_uri
= null
;
140 if (getContentResolver() != null
) {
141 result_uri
= getContentResolver().insert(
142 ProviderTableMeta
.CONTENT_URI_FILE
, cv
);
145 result_uri
= getContentProvider().insert(
146 ProviderTableMeta
.CONTENT_URI_FILE
, cv
);
147 } catch (RemoteException e
) {
149 "Fail to insert insert file to database "
153 if (result_uri
!= null
) {
154 long new_id
= Long
.parseLong(result_uri
.getPathSegments()
156 file
.setFileId(new_id
);
160 if (file
.isDirectory() && file
.needsUpdatingWhileSaving())
161 for (OCFile f
: getDirectoryContent(file
))
169 public void saveFiles(List
<OCFile
> files
) {
171 Iterator
<OCFile
> filesIt
= files
.iterator();
172 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>(files
.size());
175 // prepare operations to perform
176 while (filesIt
.hasNext()) {
177 file
= filesIt
.next();
178 ContentValues cv
= new ContentValues();
179 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
180 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
181 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
182 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
183 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
184 if (file
.getParentId() != 0)
185 cv
.put(ProviderTableMeta
.FILE_PARENT
, file
.getParentId());
186 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
187 if (!file
.isDirectory())
188 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
189 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
190 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDate());
191 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.keepInSync() ?
1 : 0);
193 if (fileExists(file
.getRemotePath())) {
194 OCFile tmpfile
= getFileByPath(file
.getRemotePath());
195 file
.setStoragePath(tmpfile
.getStoragePath());
196 if (!file
.isDirectory());
197 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
198 file
.setFileId(tmpfile
.getFileId());
200 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
202 withSelection( ProviderTableMeta
._ID
+ "=?",
203 new String
[] { String
.valueOf(file
.getFileId()) })
207 operations
.add(ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI
).withValues(cv
).build());
211 // apply operations in batch
212 ContentProviderResult
[] results
= null
;
214 if (getContentResolver() != null
) {
215 results
= getContentResolver().applyBatch(ProviderMeta
.AUTHORITY_FILES
, operations
);
218 results
= getContentProvider().applyBatch(operations
);
221 } catch (OperationApplicationException e
) {
222 Log
.e(TAG
, "Fail to update/insert list of files to database " + e
.getMessage());
224 } catch (RemoteException e
) {
225 Log
.e(TAG
, "Fail to update/insert list of files to database " + e
.getMessage());
228 // update new id in file objects for insertions
229 if (results
!= null
) {
231 for (int i
=0; i
<results
.length
; i
++) {
232 if (results
[i
].uri
!= null
) {
233 newId
= Long
.parseLong(results
[i
].uri
.getPathSegments().get(1));
234 files
.get(i
).setFileId(newId
);
235 //Log.v(TAG, "Found and added id in insertion for " + files.get(i).getRemotePath());
240 for (OCFile aFile
: files
) {
241 if (aFile
.isDirectory() && aFile
.needsUpdatingWhileSaving())
242 saveFiles(getDirectoryContent(aFile
));
247 public void setAccount(Account account
) {
251 public Account
getAccount() {
255 public void setContentResolver(ContentResolver cr
) {
256 mContentResolver
= cr
;
259 public ContentResolver
getContentResolver() {
260 return mContentResolver
;
263 public void setContentProvider(ContentProviderClient cp
) {
264 mContentProvider
= cp
;
267 public ContentProviderClient
getContentProvider() {
268 return mContentProvider
;
271 public Vector
<OCFile
> getDirectoryContent(OCFile f
) {
272 if (f
!= null
&& f
.isDirectory() && f
.getFileId() != -1) {
273 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
275 Uri req_uri
= Uri
.withAppendedPath(
276 ProviderTableMeta
.CONTENT_URI_DIR
,
277 String
.valueOf(f
.getFileId()));
280 if (getContentProvider() != null
) {
282 c
= getContentProvider().query(req_uri
, null
,
283 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?",
284 new String
[] { mAccount
.name
}, null
);
285 } catch (RemoteException e
) {
286 Log
.e(TAG
, e
.getMessage());
290 c
= getContentResolver().query(req_uri
, null
,
291 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?",
292 new String
[] { mAccount
.name
}, null
);
295 if (c
.moveToFirst()) {
297 OCFile child
= createFileInstance(c
);
299 } while (c
.moveToNext());
304 Collections
.sort(ret
);
311 private boolean fileExists(String cmp_key
, String value
) {
313 if (getContentResolver() != null
) {
314 c
= getContentResolver()
315 .query(ProviderTableMeta
.CONTENT_URI
,
318 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
320 new String
[] { value
, mAccount
.name
}, null
);
323 c
= getContentProvider().query(
324 ProviderTableMeta
.CONTENT_URI
,
327 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?",
328 new String
[] { value
, mAccount
.name
}, null
);
329 } catch (RemoteException e
) {
331 "Couldn't determine file existance, assuming non existance: "
336 boolean retval
= c
.moveToFirst();
341 private Cursor
getCursorForValue(String key
, String value
) {
343 if (getContentResolver() != null
) {
344 c
= getContentResolver()
345 .query(ProviderTableMeta
.CONTENT_URI
,
348 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
350 new String
[] { value
, mAccount
.name
}, null
);
353 c
= getContentProvider().query(
354 ProviderTableMeta
.CONTENT_URI
,
356 key
+ "=? AND " + ProviderTableMeta
.FILE_ACCOUNT_OWNER
357 + "=?", new String
[] { value
, mAccount
.name
},
359 } catch (RemoteException e
) {
360 Log
.e(TAG
, "Could not get file details: " + e
.getMessage());
367 private OCFile
createFileInstance(Cursor c
) {
370 file
= new OCFile(c
.getString(c
371 .getColumnIndex(ProviderTableMeta
.FILE_PATH
)));
372 file
.setFileId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
)));
373 file
.setParentId(c
.getLong(c
374 .getColumnIndex(ProviderTableMeta
.FILE_PARENT
)));
375 file
.setMimetype(c
.getString(c
376 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_TYPE
)));
377 if (!file
.isDirectory()) {
378 file
.setStoragePath(c
.getString(c
379 .getColumnIndex(ProviderTableMeta
.FILE_STORAGE_PATH
)));
380 if (file
.getStoragePath() == null
) {
381 // try to find existing file and bind it with current account
382 File f
= new File(FileDownloader
.getSavePath(mAccount
.name
) + file
.getRemotePath());
384 file
.setStoragePath(f
.getAbsolutePath());
387 file
.setFileLength(c
.getLong(c
388 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_LENGTH
)));
389 file
.setCreationTimestamp(c
.getLong(c
390 .getColumnIndex(ProviderTableMeta
.FILE_CREATION
)));
391 file
.setModificationTimestamp(c
.getLong(c
392 .getColumnIndex(ProviderTableMeta
.FILE_MODIFIED
)));
393 file
.setLastSyncDate(c
.getLong(c
394 .getColumnIndex(ProviderTableMeta
.FILE_LAST_SYNC_DATE
)));
395 file
.setKeepInSync(c
.getInt(
396 c
.getColumnIndex(ProviderTableMeta
.FILE_KEEP_IN_SYNC
)) == 1 ? true
: false
);
401 public void removeFile(OCFile file
) {
402 Uri file_uri
= Uri
.withAppendedPath(ProviderTableMeta
.CONTENT_URI_FILE
, ""+file
.getFileId());
403 if (getContentProvider() != null
) {
405 getContentProvider().delete(file_uri
,
406 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+"=?",
407 new String
[]{mAccount
.name
});
408 } catch (RemoteException e
) {
412 getContentResolver().delete(file_uri
,
413 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+"=?",
414 new String
[]{mAccount
.name
});
417 new File(file
.getStoragePath()).delete();