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
;
21 import java
.util
.Vector
;
23 import eu
.alefzero
.owncloud
.db
.ProviderMeta
.ProviderTableMeta
;
24 import android
.accounts
.Account
;
25 import android
.content
.ContentProviderClient
;
26 import android
.content
.ContentResolver
;
27 import android
.content
.ContentValues
;
28 import android
.database
.Cursor
;
29 import android
.net
.Uri
;
30 import android
.os
.RemoteException
;
31 import android
.util
.Log
;
33 public class FileDataStorageManager
implements DataStorageManager
{
35 private ContentResolver mContentResolver
;
36 private ContentProviderClient mContentProvider
;
37 private Account mAccount
;
39 private static String TAG
= "FileDataStorageManager";
41 public FileDataStorageManager(Account account
, ContentResolver cr
) {
42 mContentProvider
= null
;
43 mContentResolver
= cr
;
47 public FileDataStorageManager(Account account
, ContentProviderClient cp
) {
48 mContentProvider
= cp
;
49 mContentResolver
= null
;
54 public OCFile
getFileByPath(String path
) {
55 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_PATH
, path
);
57 return createFileInstance(c
);
62 public OCFile
getFileById(long id
) {
63 Cursor c
= getCursorForValue(ProviderTableMeta
._ID
, String
.valueOf(id
));
65 return createFileInstance(c
);
70 public boolean fileExists(long id
) {
71 return fileExists(ProviderTableMeta
._ID
, String
.valueOf(id
));
75 public boolean fileExists(String path
) {
76 return fileExists(ProviderTableMeta
.FILE_PATH
, path
);
80 public boolean saveFile(OCFile file
) {
81 boolean overriden
= false
;
82 ContentValues cv
= new ContentValues();
83 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
84 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
85 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
86 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
87 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
88 if (file
.getParentId() != 0)
89 cv
.put(ProviderTableMeta
.FILE_PARENT
, file
.getParentId());
90 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getPath());
91 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
92 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
94 if (fileExists(file
.getPath())) {
95 file
.setFileId(getFileByPath(file
.getPath()).getFileId());
97 if (getContentResolver() != null
) {
98 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
,
100 ProviderTableMeta
._ID
+ "=?",
101 new String
[] {String
.valueOf(file
.getFileId())});
104 getContentProvider().update(ProviderTableMeta
.CONTENT_URI
,
106 ProviderTableMeta
._ID
+ "=?",
107 new String
[] {String
.valueOf(file
.getFileId())});
108 } catch (RemoteException e
) {
109 Log
.e(TAG
, "Fail to insert insert file to database " + e
.getMessage());
113 Uri result_uri
= null
;
114 if (getContentResolver() != null
) {
115 result_uri
= getContentResolver().insert(ProviderTableMeta
.CONTENT_URI_FILE
, cv
);
118 result_uri
= getContentProvider().insert(ProviderTableMeta
.CONTENT_URI_FILE
, cv
);
119 } catch (RemoteException e
) {
120 Log
.e(TAG
, "Fail to insert insert file to database " + e
.getMessage());
123 if (result_uri
!= null
) {
124 long new_id
= Long
.parseLong(result_uri
.getPathSegments().get(1));
125 file
.setFileId(new_id
);
129 if (file
.isDirectory() && file
.needsUpdatingWhileSaving())
130 for (OCFile f
: getDirectoryContent(file
))
136 public void setAccount(Account account
) {
140 public Account
getAccount() {
144 public void setContentResolver(ContentResolver cr
) {
145 mContentResolver
= cr
;
148 public ContentResolver
getContentResolver() {
149 return mContentResolver
;
152 public void setContentProvider(ContentProviderClient cp
) {
153 mContentProvider
= cp
;
156 public ContentProviderClient
getContentProvider() {
157 return mContentProvider
;
160 public Vector
<OCFile
> getDirectoryContent(OCFile f
) {
161 if (f
.isDirectory() && f
.getFileId() != -1) {
162 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
164 Uri req_uri
= Uri
.withAppendedPath(
165 ProviderTableMeta
.CONTENT_URI_DIR
, String
.valueOf(f
.getFileId()));
168 if (getContentProvider() != null
) {
170 c
= getContentProvider().query(req_uri
, null
, null
, null
, null
);
171 } catch (RemoteException e
) {
172 Log
.e(TAG
, e
.getMessage());
176 c
= getContentResolver().query(req_uri
, null
, null
, null
, null
);
179 if (c
.moveToFirst()) {
181 OCFile child
= createFileInstance(c
);
183 } while (c
.moveToNext());
193 private boolean fileExists(String cmp_key
, String value
) {
195 if (getContentResolver() != null
) {
196 c
= getContentResolver().query(ProviderTableMeta
.CONTENT_URI
,
199 new String
[] {value
},
203 c
= getContentProvider().query(ProviderTableMeta
.CONTENT_URI
,
206 new String
[] {value
},
208 } catch (RemoteException e
) {
209 Log
.e(TAG
, "Couldn't determine file existance, assuming non existance: " + e
.getMessage());
213 return c
.moveToFirst();
216 private Cursor
getCursorForValue(String key
, String value
) {
218 if (getContentResolver() != null
) {
219 c
= getContentResolver().query(ProviderTableMeta
.CONTENT_URI
,
222 new String
[] {value
},
226 c
= getContentProvider().query(ProviderTableMeta
.CONTENT_URI
,
231 } catch (RemoteException e
) {
232 Log
.e(TAG
, "Could not get file details: " + e
.getMessage());
239 private OCFile
createFileInstance(Cursor c
) {
242 file
= new OCFile(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_PATH
)));
243 file
.setFileId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
)));
244 file
.setParentId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
.FILE_PARENT
)));
245 file
.setStoragePath(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_STORAGE_PATH
)));
246 file
.setMimetype(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_CONTENT_TYPE
)));
247 file
.setFileLength(c
.getLong(c
.getColumnIndex(ProviderTableMeta
.FILE_CONTENT_LENGTH
)));
248 file
.setCreationTimestamp(c
.getLong(c
.getColumnIndex(ProviderTableMeta
.FILE_CREATION
)));
249 file
.setModificationTimestamp(c
.getLong(c
.getColumnIndex(ProviderTableMeta
.FILE_MODIFIED
)));