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
.Vector
;
24 import eu
.alefzero
.owncloud
.db
.ProviderMeta
.ProviderTableMeta
;
25 import android
.accounts
.Account
;
26 import android
.content
.ContentProvider
;
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
.RemoteException
;
33 import android
.util
.Log
;
36 private static String TAG
= "OCFile";
39 private long parent_id_
;
41 private long creation_timestamp_
;
42 private long modified_timestamp_
;
44 private String storage_path_
;
45 private String mimetype_
;
47 private ContentResolver cr_
;
48 private ContentProviderClient cp_
;
49 private Account account_
;
51 public static OCFile
createNewFile(ContentProviderClient cr
, Account account
,
52 String path
, long length
, long creation_timestamp
,
53 long modified_timestamp
, String mimetype
, long parent_id
) {
54 OCFile new_file
= new OCFile(cr
, account
);
57 Cursor c
= new_file
.cp_
.query(ProviderTableMeta
.CONTENT_URI_FILE
, null
,
58 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND "
59 + ProviderTableMeta
.FILE_PATH
+ "=?", new String
[]{new_file
.account_
.name
,
62 new_file
.setFileData(c
);
64 } catch (RemoteException e
) {
65 Log
.e(TAG
, e
.getMessage());
68 new_file
.path_
= path
;
69 new_file
.length_
= length
;
70 new_file
.creation_timestamp_
= creation_timestamp
;
71 new_file
.modified_timestamp_
= modified_timestamp
;
72 new_file
.mimetype_
= mimetype
;
73 new_file
.parent_id_
= parent_id
;
78 public static OCFile
createNewFile(ContentResolver contentResolver
, Account a
,
79 String path
, int length
, int creation_timestamp
, int modified_timestamp
,
80 String mimetype
, long parent_id
) {
81 OCFile new_file
= new OCFile(contentResolver
, a
);
82 Cursor c
= new_file
.cr_
.query(ProviderTableMeta
.CONTENT_URI_FILE
, null
,
83 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND "
84 + ProviderTableMeta
.FILE_PATH
+ "=?", new String
[]{new_file
.account_
.name
,
87 new_file
.setFileData(c
);
90 new_file
.path_
= path
;
91 new_file
.length_
= length
;
92 new_file
.creation_timestamp_
= creation_timestamp
;
93 new_file
.modified_timestamp_
= modified_timestamp
;
94 new_file
.mimetype_
= mimetype
;
95 new_file
.parent_id_
= parent_id
;
100 public OCFile(ContentResolver cr
, Account account
, long id
) {
103 Cursor c
= cr_
.query(ProviderTableMeta
.CONTENT_URI_FILE
, null
,
104 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND "
105 + ProviderTableMeta
._ID
+ "=?",
106 new String
[]{account_
.name
, String
.valueOf(id
)}, null
);
111 public OCFile(ContentResolver cr
, Account account
, String path
) {
115 Cursor c
= cr_
.query(ProviderTableMeta
.CONTENT_URI_FILE
, null
,
116 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND "
117 + ProviderTableMeta
.FILE_PATH
+ "=?", new String
[]{account_
.name
,
119 if (c
.moveToFirst()) {
126 public OCFile(ContentProviderClient cp
, Account account
, String path
) {
131 Cursor c
= cp_
.query(ProviderTableMeta
.CONTENT_URI_FILE
, null
,
132 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND "
133 + ProviderTableMeta
.FILE_PATH
+ "=?", new String
[]{account_
.name
,
135 if (c
.moveToFirst()) {
140 } catch (RemoteException e
) {
141 Log
.d(TAG
, e
.getMessage());
145 public long getFileId() {
149 public String
getPath() {
153 public boolean fileExtist() {
157 public boolean isDirectory() {
158 return mimetype_
!= null
&& mimetype_
.equals("DIR");
161 public boolean isDownloaded() {
162 return storage_path_
!= null
;
165 public String
getStoragePath() {
166 return storage_path_
;
168 public void setStoragePath(String storage_path
) {
169 storage_path_
= storage_path
;
172 public long getCreationTimestamp() {
173 return creation_timestamp_
;
175 public void setCreationTimestamp(long creation_timestamp
) {
176 creation_timestamp_
= creation_timestamp
;
179 public long getModificationTimestamp() {
180 return modified_timestamp_
;
182 public void setModificationTimestamp(long modification_timestamp
) {
183 modified_timestamp_
= modification_timestamp
;
186 public String
getFileName() {
188 File f
= new File(path_
);
189 return f
.getName().equals("") ?
"/" : f
.getName();
194 public String
getMimetype() {
199 ContentValues cv
= new ContentValues();
200 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, modified_timestamp_
);
201 cv
.put(ProviderTableMeta
.FILE_CREATION
, creation_timestamp_
);
202 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, length_
);
203 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, mimetype_
);
204 cv
.put(ProviderTableMeta
.FILE_NAME
, getFileName());
206 cv
.put(ProviderTableMeta
.FILE_PARENT
, parent_id_
);
207 cv
.put(ProviderTableMeta
.FILE_PATH
, path_
);
208 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, storage_path_
);
209 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, account_
.name
);
214 cp_
.update(ProviderTableMeta
.CONTENT_URI
, cv
, ProviderTableMeta
._ID
215 + "=?", new String
[]{String
.valueOf(id_
)});
216 } catch (RemoteException e
) {
217 Log
.e(TAG
, e
.getMessage());
221 cr_
.update(ProviderTableMeta
.CONTENT_URI
, cv
, ProviderTableMeta
._ID
222 + "=?", new String
[]{String
.valueOf(id_
)});
225 Uri new_entry
= null
;
228 new_entry
= cp_
.insert(ProviderTableMeta
.CONTENT_URI_FILE
, cv
);
229 } catch (RemoteException e
) {
230 Log
.e(TAG
, e
.getMessage());
235 new_entry
= cr_
.insert(ProviderTableMeta
.CONTENT_URI_FILE
, cv
);
238 String p
= new_entry
.getEncodedPath();
239 id_
= Integer
.parseInt(p
.substring(p
.lastIndexOf('/')+1));
240 } catch (NumberFormatException e
) {
241 Log
.e(TAG
, "Can't retrieve file id from uri: " + new_entry
.toString()
242 + ", reason: " + e
.getMessage());
248 public Vector
<OCFile
> getDirectoryContent() {
249 if (isDirectory() && id_
!= -1) {
250 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
252 Uri req_uri
= Uri
.withAppendedPath(ProviderTableMeta
.CONTENT_URI_DIR
,
253 String
.valueOf(id_
));
257 c
= cp_
.query(req_uri
, null
, null
, null
, null
);
258 } catch (RemoteException e
) {
259 Log
.e(TAG
, e
.getMessage());
263 c
= cr_
.query(req_uri
, null
, null
, null
, null
);
268 OCFile child
= new OCFile(cp_
, account_
);
269 child
.setFileData(c
);
271 } while (c
.moveToNext());
279 public void addFile(OCFile file
) {
280 file
.parent_id_
= id_
;
284 private OCFile(ContentProviderClient cp
, Account account
) {
290 private OCFile(ContentResolver cr
, Account account
) {
296 private void resetData() {
300 storage_path_
= null
;
303 creation_timestamp_
= 0;
304 modified_timestamp_
= 0;
307 private void setFileData(Cursor c
) {
310 id_
= c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
));
311 path_
= c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_PATH
));
312 parent_id_
= c
.getLong(c
.getColumnIndex(ProviderTableMeta
.FILE_PARENT
));
313 storage_path_
= c
.getString(c
314 .getColumnIndex(ProviderTableMeta
.FILE_STORAGE_PATH
));
315 mimetype_
= c
.getString(c
316 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_TYPE
));
317 length_
= c
.getLong(c
318 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_LENGTH
));
319 creation_timestamp_
= c
.getLong(c
320 .getColumnIndex(ProviderTableMeta
.FILE_CREATION
));
321 modified_timestamp_
= c
.getLong(c
322 .getColumnIndex(ProviderTableMeta
.FILE_MODIFIED
));