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 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
;
32 import eu
.alefzero
.owncloud
.db
.ProviderMeta
.ProviderTableMeta
;
35 private static String TAG
= "OCFile";
38 private long parent_id_
;
40 private long creation_timestamp_
;
41 private long modified_timestamp_
;
43 private String storage_path_
;
44 private String mimetype_
;
46 private ContentResolver contentResolver_
;
47 private ContentProviderClient providerClient_
;
48 private Account account_
;
50 private OCFile(ContentProviderClient providerClient
, Account account
) {
52 providerClient_
= providerClient
;
56 private OCFile(ContentResolver contentResolver
, Account account
) {
58 contentResolver_
= contentResolver
;
63 * Query the database for a {@link OCFile} belonging to a given account
66 * @param resolver The {@link ContentResolver} to use
67 * @param account The {@link Account} the {@link OCFile} belongs to
68 * @param id The ID the file has in the database
70 public OCFile(ContentResolver resolver
, Account account
, long id
) {
71 contentResolver_
= resolver
;
73 Cursor c
= contentResolver_
.query(ProviderTableMeta
.CONTENT_URI_FILE
,
74 null
, ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND "
75 + ProviderTableMeta
._ID
+ "=?", new String
[] {
76 account_
.name
, String
.valueOf(id
) }, null
);
82 * Query the database for a {@link OCFile} belonging to a given account
83 * and that matches remote path
85 * @param contentResolver The {@link ContentResolver} to use
86 * @param account The {@link Account} the {@link OCFile} belongs to
87 * @param path The remote path of the file
89 public OCFile(ContentResolver contentResolver
, Account account
, String path
) {
90 contentResolver_
= contentResolver
;
93 Cursor c
= contentResolver_
.query(ProviderTableMeta
.CONTENT_URI_FILE
,
94 null
, ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND "
95 + ProviderTableMeta
.FILE_PATH
+ "=?", new String
[] {
96 account_
.name
, path
}, null
);
97 if (c
.moveToFirst()) {
104 public OCFile(ContentProviderClient cp
, Account account
, String path
) {
105 providerClient_
= cp
;
109 Cursor c
= providerClient_
.query(ProviderTableMeta
.CONTENT_URI_FILE
, null
,
110 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND "
111 + ProviderTableMeta
.FILE_PATH
+ "=?", new String
[] {
112 account_
.name
, path
}, null
);
113 if (c
.moveToFirst()) {
118 } catch (RemoteException e
) {
119 Log
.d(TAG
, e
.getMessage());
124 * Creates a new {@link OCFile}
126 * @param providerClient The {@link ContentProviderClient} to use
127 * @param account The {@link Account} that this file belongs to
128 * @param path The remote path
129 * @param length The file size in bytes
130 * @param creation_timestamp The UNIX timestamp of the creation date
131 * @param modified_timestamp The UNIX timestamp of the modification date
132 * @param mimetype The mimetype to set
133 * @param parent_id The parent folder of that file
134 * @return A new instance of {@link OCFile}
136 public static OCFile
createNewFile(ContentProviderClient providerClient
,
137 Account account
, String path
, long length
, long creation_timestamp
,
138 long modified_timestamp
, String mimetype
, long parent_id
) {
139 OCFile new_file
= new OCFile(providerClient
, account
);
142 Cursor c
= new_file
.providerClient_
.query(ProviderTableMeta
.CONTENT_URI_FILE
,
143 null
, ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND "
144 + ProviderTableMeta
.FILE_PATH
+ "=?", new String
[] {
145 new_file
.account_
.name
, path
}, null
);
147 new_file
.setFileData(c
);
149 } catch (RemoteException e
) {
150 Log
.e(TAG
, e
.getMessage());
153 new_file
.path_
= path
;
154 new_file
.length_
= length
;
155 new_file
.creation_timestamp_
= creation_timestamp
;
156 new_file
.modified_timestamp_
= modified_timestamp
;
157 new_file
.mimetype_
= mimetype
;
158 new_file
.parent_id_
= parent_id
;
164 * Creates a new {@link OCFile}
166 * @param contentResolver The {@link ContentResolver} to use
167 * @param account The {@link Account} that this file belongs to
168 * @param path The remote path
169 * @param length The file size in bytes
170 * @param creation_timestamp The UNIX timestamp of the creation date
171 * @param modified_timestamp The UNIX timestamp of the modification date
172 * @param mimetype The mimetype to set
173 * @param parent_id The parent folder of that file
174 * @return A new instance of {@link OCFile}
176 public static OCFile
createNewFile(ContentResolver contentResolver
,
177 Account account
, String path
, int length
, int creation_timestamp
,
178 int modified_timestamp
, String mimetype
, long parent_id
) {
179 OCFile new_file
= new OCFile(contentResolver
, account
);
180 Cursor c
= new_file
.contentResolver_
.query(
181 ProviderTableMeta
.CONTENT_URI_FILE
, null
,
182 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND "
183 + ProviderTableMeta
.FILE_PATH
+ "=?", new String
[] {
184 new_file
.account_
.name
, path
}, null
);
186 new_file
.setFileData(c
);
189 new_file
.path_
= path
;
190 new_file
.length_
= length
;
191 new_file
.creation_timestamp_
= creation_timestamp
;
192 new_file
.modified_timestamp_
= modified_timestamp
;
193 new_file
.mimetype_
= mimetype
;
194 new_file
.parent_id_
= parent_id
;
201 * Gets the ID of the file
203 * @return the file ID
205 public long getFileId() {
210 * Returns the path of the file
214 public String
getPath() {
219 * Can be used to check, whether or not this file exists in the database
222 * @return true, if the file exists in the database
224 public boolean fileExists() {
229 * Use this to find out if this file is a Directory
231 * @return true if it is a directory
233 public boolean isDirectory() {
234 return mimetype_
!= null
&& mimetype_
.equals("DIR");
238 * Use this to check if this file is available locally
240 * @return true if it is
242 public boolean isDownloaded() {
243 return storage_path_
!= null
;
247 * The path, where the file is stored locally
249 * @return The local path to the file
251 public String
getStoragePath() {
252 return storage_path_
;
256 * Can be used to set the path where the file is stored
258 * @param storage_path
261 public void setStoragePath(String storage_path
) {
262 storage_path_
= storage_path
;
266 * Get a UNIX timestamp of the file creation time
268 * @return A UNIX timestamp of the time that file was created
270 public long getCreationTimestamp() {
271 return creation_timestamp_
;
275 * Set a UNIX timestamp of the time the file was created
277 * @param creation_timestamp
280 public void setCreationTimestamp(long creation_timestamp
) {
281 creation_timestamp_
= creation_timestamp
;
285 * Get a UNIX timestamp of the file modification time
287 * @return A UNIX timestamp of the modification time
289 public long getModificationTimestamp() {
290 return modified_timestamp_
;
294 * Set a UNIX timestamp of the time the time the file was modified.
296 * @param modification_timestamp
299 public void setModificationTimestamp(long modification_timestamp
) {
300 modified_timestamp_
= modification_timestamp
;
304 * Returns the filename and "/" for the root directory
306 * @return The name of the file
308 public String
getFileName() {
310 File f
= new File(path_
);
311 return f
.getName().equals("") ?
"/" : f
.getName();
317 * Can be used to get the Mimetype
319 * @return the Mimetype as a String
321 public String
getMimetype() {
326 * Instruct the file to save itself to the database
329 ContentValues cv
= new ContentValues();
330 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, modified_timestamp_
);
331 cv
.put(ProviderTableMeta
.FILE_CREATION
, creation_timestamp_
);
332 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, length_
);
333 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, mimetype_
);
334 cv
.put(ProviderTableMeta
.FILE_NAME
, getFileName());
336 cv
.put(ProviderTableMeta
.FILE_PARENT
, parent_id_
);
337 cv
.put(ProviderTableMeta
.FILE_PATH
, path_
);
338 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, storage_path_
);
339 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, account_
.name
);
342 if (providerClient_
!= null
) {
344 providerClient_
.update(ProviderTableMeta
.CONTENT_URI
, cv
,
345 ProviderTableMeta
._ID
+ "=?",
346 new String
[] { String
.valueOf(id_
) });
347 } catch (RemoteException e
) {
348 Log
.e(TAG
, e
.getMessage());
352 contentResolver_
.update(ProviderTableMeta
.CONTENT_URI
, cv
,
353 ProviderTableMeta
._ID
+ "=?",
354 new String
[] { String
.valueOf(id_
) });
357 Uri new_entry
= null
;
358 if (providerClient_
!= null
) {
360 new_entry
= providerClient_
.insert(ProviderTableMeta
.CONTENT_URI_FILE
,
362 } catch (RemoteException e
) {
363 Log
.e(TAG
, e
.getMessage());
368 new_entry
= contentResolver_
.insert(
369 ProviderTableMeta
.CONTENT_URI_FILE
, cv
);
372 String p
= new_entry
.getEncodedPath();
373 id_
= Integer
.parseInt(p
.substring(p
.lastIndexOf('/') + 1));
374 } catch (NumberFormatException e
) {
376 "Can't retrieve file id from uri: "
377 + new_entry
.toString() + ", reason: "
385 * List the directory content
387 * @return The directory content or null, if the file is not a directory
389 public Vector
<OCFile
> getDirectoryContent() {
390 if (isDirectory() && id_
!= -1) {
391 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
393 Uri req_uri
= Uri
.withAppendedPath(
394 ProviderTableMeta
.CONTENT_URI_DIR
, String
.valueOf(id_
));
396 if (providerClient_
!= null
) {
398 c
= providerClient_
.query(req_uri
, null
, null
, null
, null
);
399 } catch (RemoteException e
) {
400 Log
.e(TAG
, e
.getMessage());
404 c
= contentResolver_
.query(req_uri
, null
, null
, null
, null
);
409 OCFile child
= new OCFile(providerClient_
, account_
);
410 child
.setFileData(c
);
412 } while (c
.moveToNext());
421 * Adds a file to this directory. If this file is not a directory, an
422 * exception gets thrown.
426 * @throws IllegalStateException
427 * if you try to add a something and this is not a directory
429 public void addFile(OCFile file
) throws IllegalStateException
{
431 file
.parent_id_
= id_
;
435 throw new IllegalStateException(
436 "This is not a directory where you can add stuff to!");
440 * Used internally. Reset all file properties
442 private void resetData() {
446 storage_path_
= null
;
449 creation_timestamp_
= 0;
450 modified_timestamp_
= 0;
454 * Used internally. Set properties based on the information in a {@link android.database.Cursor}
455 * @param c the Cursor containing the information
457 private void setFileData(Cursor c
) {
460 id_
= c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
));
461 path_
= c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_PATH
));
462 parent_id_
= c
.getLong(c
463 .getColumnIndex(ProviderTableMeta
.FILE_PARENT
));
464 storage_path_
= c
.getString(c
465 .getColumnIndex(ProviderTableMeta
.FILE_STORAGE_PATH
));
466 mimetype_
= c
.getString(c
467 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_TYPE
));
468 length_
= c
.getLong(c
469 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_LENGTH
));
470 creation_timestamp_
= c
.getLong(c
471 .getColumnIndex(ProviderTableMeta
.FILE_CREATION
));
472 modified_timestamp_
= c
.getLong(c
473 .getColumnIndex(ProviderTableMeta
.FILE_MODIFIED
));