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())) {
96 if (getContentResolver() != null
) {
97 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
,
99 ProviderTableMeta
._ID
+ "=?",
100 new String
[] {String
.valueOf(file
.getFileId())});
103 getContentProvider().update(ProviderTableMeta
.CONTENT_URI
,
105 ProviderTableMeta
._ID
+ "=?",
106 new String
[] {String
.valueOf(file
.getFileId())});
107 } catch (RemoteException e
) {
108 Log
.e(TAG
, "Fail to insert insert file to database " + e
.getMessage());
112 Uri result_uri
= null
;
113 if (getContentResolver() != null
) {
114 result_uri
= getContentResolver().insert(ProviderTableMeta
.CONTENT_URI_FILE
, cv
);
117 result_uri
= getContentProvider().insert(ProviderTableMeta
.CONTENT_URI_FILE
, cv
);
118 } catch (RemoteException e
) {
119 Log
.e(TAG
, "Fail to insert insert file to database " + e
.getMessage());
122 if (result_uri
!= null
) {
123 long new_id
= Long
.parseLong(result_uri
.getPathSegments().get(1));
124 file
.setFileId(new_id
);
128 if (file
.isDirectory() && file
.needsUpdatingWhileSaving())
129 for (OCFile f
: getDirectoryContent(file
))
135 public void setAccount(Account account
) {
139 public Account
getAccount() {
143 public void setContentResolver(ContentResolver cr
) {
144 mContentResolver
= cr
;
147 public ContentResolver
getContentResolver() {
148 return mContentResolver
;
151 public void setContentProvider(ContentProviderClient cp
) {
152 mContentProvider
= cp
;
155 public ContentProviderClient
getContentProvider() {
156 return mContentProvider
;
159 public Vector
<OCFile
> getDirectoryContent(OCFile f
) {
160 if (f
.isDirectory() && f
.getFileId() != -1) {
161 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
163 Uri req_uri
= Uri
.withAppendedPath(
164 ProviderTableMeta
.CONTENT_URI_DIR
, String
.valueOf(f
.getFileId()));
167 if (getContentProvider() != null
) {
169 c
= getContentProvider().query(req_uri
, null
, null
, null
, null
);
170 } catch (RemoteException e
) {
171 Log
.e(TAG
, e
.getMessage());
175 c
= getContentResolver().query(req_uri
, null
, null
, null
, null
);
178 if (c
.moveToFirst()) {
180 OCFile child
= createFileInstance(c
);
182 } while (c
.moveToNext());
192 private boolean fileExists(String cmp_key
, String value
) {
194 if (getContentResolver() != null
) {
195 c
= getContentResolver().query(ProviderTableMeta
.CONTENT_URI
,
198 new String
[] {value
},
202 c
= getContentProvider().query(ProviderTableMeta
.CONTENT_URI
,
205 new String
[] {value
},
207 } catch (RemoteException e
) {
208 Log
.e(TAG
, "Couldn't determine file existance, assuming non existance: " + e
.getMessage());
212 return c
.moveToFirst();
215 private Cursor
getCursorForValue(String key
, String value
) {
217 if (getContentResolver() != null
) {
218 c
= getContentResolver().query(ProviderTableMeta
.CONTENT_URI
,
221 new String
[] {value
},
225 c
= getContentProvider().query(ProviderTableMeta
.CONTENT_URI
,
230 } catch (RemoteException e
) {
231 Log
.e(TAG
, "Could not get file details: " + e
.getMessage());
238 private OCFile
createFileInstance(Cursor c
) {
241 file
= new OCFile(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_PATH
)));
242 file
.setFileId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
)));
243 file
.setParentId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
.FILE_PARENT
)));
244 file
.setStoragePath(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_STORAGE_PATH
)));
245 file
.setMimetype(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_CONTENT_TYPE
)));
246 file
.setFileLength(c
.getLong(c
.getColumnIndex(ProviderTableMeta
.FILE_CONTENT_LENGTH
)));
247 file
.setCreationTimestamp(c
.getLong(c
.getColumnIndex(ProviderTableMeta
.FILE_CREATION
)));
248 file
.setModificationTimestamp(c
.getLong(c
.getColumnIndex(ProviderTableMeta
.FILE_MODIFIED
)));