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
.Collections
;
23 import java
.util
.Vector
;
25 import eu
.alefzero
.owncloud
.db
.ProviderMeta
.ProviderTableMeta
;
26 import android
.accounts
.Account
;
27 import android
.content
.ContentProviderClient
;
28 import android
.content
.ContentResolver
;
29 import android
.content
.ContentValues
;
30 import android
.database
.Cursor
;
31 import android
.net
.Uri
;
32 import android
.os
.Environment
;
33 import android
.os
.RemoteException
;
34 import android
.util
.Log
;
36 public class FileDataStorageManager
implements DataStorageManager
{
38 private ContentResolver mContentResolver
;
39 private ContentProviderClient mContentProvider
;
40 private Account mAccount
;
42 private static String TAG
= "FileDataStorageManager";
44 public FileDataStorageManager(Account account
, ContentResolver cr
) {
45 mContentProvider
= null
;
46 mContentResolver
= cr
;
50 public FileDataStorageManager(Account account
, ContentProviderClient cp
) {
51 mContentProvider
= cp
;
52 mContentResolver
= null
;
57 public OCFile
getFileByPath(String path
) {
58 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_PATH
, path
);
60 if (c
.moveToFirst()) {
61 file
= createFileInstance(c
);
68 public OCFile
getFileById(long id
) {
69 Cursor c
= getCursorForValue(ProviderTableMeta
._ID
, String
.valueOf(id
));
71 if (c
.moveToFirst()) {
72 file
= createFileInstance(c
);
79 public boolean fileExists(long id
) {
80 return fileExists(ProviderTableMeta
._ID
, String
.valueOf(id
));
84 public boolean fileExists(String path
) {
85 return fileExists(ProviderTableMeta
.FILE_PATH
, path
);
89 public boolean saveFile(OCFile file
) {
90 boolean overriden
= false
;
91 ContentValues cv
= new ContentValues();
92 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
93 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
94 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
95 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
96 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
97 if (file
.getParentId() != 0)
98 cv
.put(ProviderTableMeta
.FILE_PARENT
, file
.getParentId());
99 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
100 if (!file
.isDirectory())
101 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
102 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
103 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDate());
104 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.keepInSync() ?
1 : 0);
106 if (fileExists(file
.getRemotePath())) {
107 OCFile tmpfile
= getFileByPath(file
.getRemotePath());
108 file
.setStoragePath(tmpfile
.getStoragePath());
109 if (!file
.isDirectory());
110 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
111 file
.setFileId(tmpfile
.getFileId());
114 if (getContentResolver() != null
) {
115 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
,
116 ProviderTableMeta
._ID
+ "=?",
117 new String
[] { String
.valueOf(file
.getFileId()) });
120 getContentProvider().update(ProviderTableMeta
.CONTENT_URI
,
121 cv
, ProviderTableMeta
._ID
+ "=?",
122 new String
[] { String
.valueOf(file
.getFileId()) });
123 } catch (RemoteException e
) {
125 "Fail to insert insert file to database "
130 Uri result_uri
= null
;
131 if (getContentResolver() != null
) {
132 result_uri
= getContentResolver().insert(
133 ProviderTableMeta
.CONTENT_URI_FILE
, cv
);
136 result_uri
= getContentProvider().insert(
137 ProviderTableMeta
.CONTENT_URI_FILE
, cv
);
138 } catch (RemoteException e
) {
140 "Fail to insert insert file to database "
144 if (result_uri
!= null
) {
145 long new_id
= Long
.parseLong(result_uri
.getPathSegments()
147 file
.setFileId(new_id
);
151 if (file
.isDirectory() && file
.needsUpdatingWhileSaving())
152 for (OCFile f
: getDirectoryContent(file
))
158 public void setAccount(Account account
) {
162 public Account
getAccount() {
166 public void setContentResolver(ContentResolver cr
) {
167 mContentResolver
= cr
;
170 public ContentResolver
getContentResolver() {
171 return mContentResolver
;
174 public void setContentProvider(ContentProviderClient cp
) {
175 mContentProvider
= cp
;
178 public ContentProviderClient
getContentProvider() {
179 return mContentProvider
;
182 public Vector
<OCFile
> getDirectoryContent(OCFile f
) {
183 if (f
!= null
&& f
.isDirectory() && f
.getFileId() != -1) {
184 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
186 Uri req_uri
= Uri
.withAppendedPath(
187 ProviderTableMeta
.CONTENT_URI_DIR
,
188 String
.valueOf(f
.getFileId()));
191 if (getContentProvider() != null
) {
193 c
= getContentProvider().query(req_uri
, null
,
194 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?",
195 new String
[] { mAccount
.name
}, null
);
196 } catch (RemoteException e
) {
197 Log
.e(TAG
, e
.getMessage());
201 c
= getContentResolver().query(req_uri
, null
,
202 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?",
203 new String
[] { mAccount
.name
}, null
);
206 if (c
.moveToFirst()) {
208 OCFile child
= createFileInstance(c
);
210 } while (c
.moveToNext());
215 Collections
.sort(ret
);
222 private boolean fileExists(String cmp_key
, String value
) {
224 if (getContentResolver() != null
) {
225 c
= getContentResolver()
226 .query(ProviderTableMeta
.CONTENT_URI
,
229 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
231 new String
[] { value
, mAccount
.name
}, null
);
234 c
= getContentProvider().query(
235 ProviderTableMeta
.CONTENT_URI
,
238 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?",
239 new String
[] { value
, mAccount
.name
}, null
);
240 } catch (RemoteException e
) {
242 "Couldn't determine file existance, assuming non existance: "
247 boolean retval
= c
.moveToFirst();
252 private Cursor
getCursorForValue(String key
, String value
) {
254 if (getContentResolver() != null
) {
255 c
= getContentResolver()
256 .query(ProviderTableMeta
.CONTENT_URI
,
259 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
261 new String
[] { value
, mAccount
.name
}, null
);
264 c
= getContentProvider().query(
265 ProviderTableMeta
.CONTENT_URI
,
267 key
+ "=? AND " + ProviderTableMeta
.FILE_ACCOUNT_OWNER
268 + "=?", new String
[] { value
, mAccount
.name
},
270 } catch (RemoteException e
) {
271 Log
.e(TAG
, "Could not get file details: " + e
.getMessage());
278 private OCFile
createFileInstance(Cursor c
) {
281 file
= new OCFile(c
.getString(c
282 .getColumnIndex(ProviderTableMeta
.FILE_PATH
)));
283 file
.setFileId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
)));
284 file
.setParentId(c
.getLong(c
285 .getColumnIndex(ProviderTableMeta
.FILE_PARENT
)));
286 file
.setMimetype(c
.getString(c
287 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_TYPE
)));
288 if (!file
.isDirectory()) {
289 file
.setStoragePath(c
.getString(c
290 .getColumnIndex(ProviderTableMeta
.FILE_STORAGE_PATH
)));
291 if (file
.getStoragePath() == null
) {
292 // try to find existing file and bind it with current account
293 File sdCard
= Environment
.getExternalStorageDirectory();
294 File f
= new File(sdCard
.getAbsolutePath() + "/owncloud/" + mAccount
.name
+ file
.getURLDecodedRemotePath());
296 file
.setStoragePath(f
.getAbsolutePath());
299 file
.setFileLength(c
.getLong(c
300 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_LENGTH
)));
301 file
.setCreationTimestamp(c
.getLong(c
302 .getColumnIndex(ProviderTableMeta
.FILE_CREATION
)));
303 file
.setModificationTimestamp(c
.getLong(c
304 .getColumnIndex(ProviderTableMeta
.FILE_MODIFIED
)));
305 file
.setLastSyncDate(c
.getLong(c
306 .getColumnIndex(ProviderTableMeta
.FILE_LAST_SYNC_DATE
)));
307 file
.setKeepInSync(c
.getInt(
308 c
.getColumnIndex(ProviderTableMeta
.FILE_KEEP_IN_SYNC
)) == 1 ? true
: false
);
313 public void removeFile(OCFile file
) {
314 Uri file_uri
= Uri
.withAppendedPath(ProviderTableMeta
.CONTENT_URI_FILE
, ""+file
.getFileId());
315 if (getContentProvider() != null
) {
317 getContentProvider().delete(file_uri
,
318 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+"=?",
319 new String
[]{mAccount
.name
});
320 } catch (RemoteException e
) {
324 getContentResolver().delete(file_uri
,
325 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+"=?",
326 new String
[]{mAccount
.name
});
328 if (file
.getStoragePath() != null
) {
329 new File(file
.getStoragePath()).delete();