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());
105 if (fileExists(file
.getRemotePath())) {
106 OCFile tmpfile
= getFileByPath(file
.getRemotePath());
107 file
.setStoragePath(tmpfile
.getStoragePath());
108 if (!file
.isDirectory());
109 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
110 file
.setFileId(tmpfile
.getFileId());
113 if (getContentResolver() != null
) {
114 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
,
115 ProviderTableMeta
._ID
+ "=?",
116 new String
[] { String
.valueOf(file
.getFileId()) });
119 getContentProvider().update(ProviderTableMeta
.CONTENT_URI
,
120 cv
, ProviderTableMeta
._ID
+ "=?",
121 new String
[] { String
.valueOf(file
.getFileId()) });
122 } catch (RemoteException e
) {
124 "Fail to insert insert file to database "
129 Uri result_uri
= null
;
130 if (getContentResolver() != null
) {
131 result_uri
= getContentResolver().insert(
132 ProviderTableMeta
.CONTENT_URI_FILE
, cv
);
135 result_uri
= getContentProvider().insert(
136 ProviderTableMeta
.CONTENT_URI_FILE
, cv
);
137 } catch (RemoteException e
) {
139 "Fail to insert insert file to database "
143 if (result_uri
!= null
) {
144 long new_id
= Long
.parseLong(result_uri
.getPathSegments()
146 file
.setFileId(new_id
);
150 if (file
.isDirectory() && file
.needsUpdatingWhileSaving())
151 for (OCFile f
: getDirectoryContent(file
))
157 public void setAccount(Account account
) {
161 public Account
getAccount() {
165 public void setContentResolver(ContentResolver cr
) {
166 mContentResolver
= cr
;
169 public ContentResolver
getContentResolver() {
170 return mContentResolver
;
173 public void setContentProvider(ContentProviderClient cp
) {
174 mContentProvider
= cp
;
177 public ContentProviderClient
getContentProvider() {
178 return mContentProvider
;
181 public Vector
<OCFile
> getDirectoryContent(OCFile f
) {
182 if (f
!= null
&& f
.isDirectory() && f
.getFileId() != -1) {
183 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
185 Uri req_uri
= Uri
.withAppendedPath(
186 ProviderTableMeta
.CONTENT_URI_DIR
,
187 String
.valueOf(f
.getFileId()));
190 if (getContentProvider() != null
) {
192 c
= getContentProvider().query(req_uri
, null
,
193 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?",
194 new String
[] { mAccount
.name
}, null
);
195 } catch (RemoteException e
) {
196 Log
.e(TAG
, e
.getMessage());
200 c
= getContentResolver().query(req_uri
, null
,
201 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?",
202 new String
[] { mAccount
.name
}, null
);
205 if (c
.moveToFirst()) {
207 OCFile child
= createFileInstance(c
);
209 } while (c
.moveToNext());
214 Collections
.sort(ret
);
221 private boolean fileExists(String cmp_key
, String value
) {
223 if (getContentResolver() != null
) {
224 c
= getContentResolver()
225 .query(ProviderTableMeta
.CONTENT_URI
,
228 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
230 new String
[] { value
, mAccount
.name
}, null
);
233 c
= getContentProvider().query(
234 ProviderTableMeta
.CONTENT_URI
,
237 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?",
238 new String
[] { value
, mAccount
.name
}, null
);
239 } catch (RemoteException e
) {
241 "Couldn't determine file existance, assuming non existance: "
246 boolean retval
= c
.moveToFirst();
251 private Cursor
getCursorForValue(String key
, String value
) {
253 if (getContentResolver() != null
) {
254 c
= getContentResolver()
255 .query(ProviderTableMeta
.CONTENT_URI
,
258 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
260 new String
[] { value
, mAccount
.name
}, null
);
263 c
= getContentProvider().query(
264 ProviderTableMeta
.CONTENT_URI
,
266 key
+ "=? AND " + ProviderTableMeta
.FILE_ACCOUNT_OWNER
267 + "=?", new String
[] { value
, mAccount
.name
},
269 } catch (RemoteException e
) {
270 Log
.e(TAG
, "Could not get file details: " + e
.getMessage());
277 private OCFile
createFileInstance(Cursor c
) {
280 file
= new OCFile(c
.getString(c
281 .getColumnIndex(ProviderTableMeta
.FILE_PATH
)));
282 file
.setFileId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
)));
283 file
.setParentId(c
.getLong(c
284 .getColumnIndex(ProviderTableMeta
.FILE_PARENT
)));
285 file
.setMimetype(c
.getString(c
286 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_TYPE
)));
287 if (!file
.isDirectory()) {
288 file
.setStoragePath(c
.getString(c
289 .getColumnIndex(ProviderTableMeta
.FILE_STORAGE_PATH
)));
290 if (file
.getStoragePath() == null
) {
291 // try to find existing file and bind it with current account
292 File sdCard
= Environment
.getExternalStorageDirectory();
293 File f
= new File(sdCard
.getAbsolutePath() + "/owncloud/" + mAccount
.name
+ file
.getURLDecodedRemotePath());
295 file
.setStoragePath(f
.getAbsolutePath());
298 file
.setFileLength(c
.getLong(c
299 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_LENGTH
)));
300 file
.setCreationTimestamp(c
.getLong(c
301 .getColumnIndex(ProviderTableMeta
.FILE_CREATION
)));
302 file
.setModificationTimestamp(c
.getLong(c
303 .getColumnIndex(ProviderTableMeta
.FILE_MODIFIED
)));
304 file
.setLastSyncDate(c
.getLong(c
305 .getColumnIndex(ProviderTableMeta
.FILE_LAST_SYNC_DATE
)));
310 public void removeFile(OCFile file
) {
311 Uri file_uri
= Uri
.withAppendedPath(ProviderTableMeta
.CONTENT_URI_FILE
, ""+file
.getFileId());
312 if (getContentProvider() != null
) {
314 getContentProvider().delete(file_uri
,
315 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+"=?",
316 new String
[]{mAccount
.name
});
317 } catch (RemoteException e
) {
321 getContentResolver().delete(file_uri
,
322 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+"=?",
323 new String
[]{mAccount
.name
});
325 if (file
.getStoragePath() != null
) {
326 new File(file
.getStoragePath()).delete();