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 parentId
;
40 private long creationTimestamp
;
41 private long modifiedTimestamp
;
42 private String remotePath
;
43 private String localStoragePath
;
44 private String mimeType
;
46 private ContentResolver contentResolver
;
47 private ContentProviderClient providerClient
;
48 private Account account
;
50 private OCFile(ContentProviderClient providerClient
, Account account
) {
51 this.account
= account
;
52 this.providerClient
= providerClient
;
56 private OCFile(ContentResolver contentResolver
, Account account
) {
57 this.account
= account
;
58 this.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 this.contentResolver
= resolver
;
72 this.account
= account
;
73 Cursor c
= this.contentResolver
.query(ProviderTableMeta
.CONTENT_URI_FILE
,
74 null
, ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND "
75 + ProviderTableMeta
._ID
+ "=?", new String
[] {
76 this.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 this.contentResolver
= contentResolver
;
91 this.account
= account
;
93 Cursor c
= this.contentResolver
.query(ProviderTableMeta
.CONTENT_URI_FILE
,
94 null
, ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND "
95 + ProviderTableMeta
.FILE_PATH
+ "=?", new String
[] {
96 this.account
.name
, path
}, null
);
97 if (c
.moveToFirst()) {
99 if (remotePath
!= null
)
104 public OCFile(ContentProviderClient cp
, Account account
, String path
) {
105 this.providerClient
= cp
;
106 this.account
= account
;
109 Cursor c
= this.providerClient
.query(ProviderTableMeta
.CONTENT_URI_FILE
, null
,
110 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND "
111 + ProviderTableMeta
.FILE_PATH
+ "=?", new String
[] {
112 this.account
.name
, path
}, null
);
113 if (c
.moveToFirst()) {
115 if (remotePath
!= null
)
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
.remotePath
= path
;
154 new_file
.length
= length
;
155 new_file
.creationTimestamp
= creation_timestamp
;
156 new_file
.modifiedTimestamp
= modified_timestamp
;
157 new_file
.mimeType
= mimetype
;
158 new_file
.parentId
= 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
.remotePath
= path
;
190 new_file
.length
= length
;
191 new_file
.creationTimestamp
= creation_timestamp
;
192 new_file
.modifiedTimestamp
= modified_timestamp
;
193 new_file
.mimeType
= mimetype
;
194 new_file
.parentId
= 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 localStoragePath
!= null
;
247 * The path, where the file is stored locally
249 * @return The local path to the file
251 public String
getStoragePath() {
252 return localStoragePath
;
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 localStoragePath
= 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 creationTimestamp
;
275 * Set a UNIX timestamp of the time the file was created
277 * @param creation_timestamp
280 public void setCreationTimestamp(long creation_timestamp
) {
281 creationTimestamp
= 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 modifiedTimestamp
;
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 modifiedTimestamp
= modification_timestamp
;
304 * Returns the filename and "/" for the root directory
306 * @return The name of the file
308 public String
getFileName() {
309 if (remotePath
!= null
) {
310 File f
= new File(remotePath
);
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
, modifiedTimestamp
);
331 cv
.put(ProviderTableMeta
.FILE_CREATION
, creationTimestamp
);
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
, parentId
);
337 cv
.put(ProviderTableMeta
.FILE_PATH
, remotePath
);
338 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, localStoragePath
);
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
{
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 localStoragePath
= null
;
449 creationTimestamp
= 0;
450 modifiedTimestamp
= 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 remotePath
= c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_PATH
));
462 parentId
= c
.getLong(c
463 .getColumnIndex(ProviderTableMeta
.FILE_PARENT
));
464 localStoragePath
= c
.getString(c
465 .getColumnIndex(ProviderTableMeta
.FILE_STORAGE_PATH
));
466 mimeType
= c
.getString(c
467 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_TYPE
));
469 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_LENGTH
));
470 creationTimestamp
= c
.getLong(c
471 .getColumnIndex(ProviderTableMeta
.FILE_CREATION
));
472 modifiedTimestamp
= c
.getLong(c
473 .getColumnIndex(ProviderTableMeta
.FILE_MODIFIED
));