Added MediaController user interface to handle audio files in the MediaService
[pub/Android/ownCloud.git] / src / com / owncloud / android / datamodel / OCFile.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 *
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.
8 *
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.
13 *
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/>.
16 *
17 */
18
19 package com.owncloud.android.datamodel;
20
21 import java.io.File;
22
23 import android.os.Parcel;
24 import android.os.Parcelable;
25 import android.util.Log;
26
27 public class OCFile implements Parcelable, Comparable<OCFile> {
28
29 public static final Parcelable.Creator<OCFile> CREATOR = new Parcelable.Creator<OCFile>() {
30 @Override
31 public OCFile createFromParcel(Parcel source) {
32 return new OCFile(source);
33 }
34
35 @Override
36 public OCFile[] newArray(int size) {
37 return new OCFile[size];
38 }
39 };
40
41 public static final String PATH_SEPARATOR = "/";
42
43 private static final String TAG = OCFile.class.getSimpleName();
44
45 private long mId;
46 private long mParentId;
47 private long mLength;
48 private long mCreationTimestamp;
49 private long mModifiedTimestamp;
50 private long mModifiedTimestampAtLastSyncForData;
51 private String mRemotePath;
52 private String mLocalPath;
53 private String mMimeType;
54 private boolean mNeedsUpdating;
55 private long mLastSyncDateForProperties;
56 private long mLastSyncDateForData;
57 private boolean mKeepInSync;
58
59 private String mEtag;
60
61 /**
62 * Create new {@link OCFile} with given path.
63 *
64 * The path received must be URL-decoded. Path separator must be OCFile.PATH_SEPARATOR, and it must be the first character in 'path'.
65 *
66 * @param path The remote path of the file.
67 */
68 public OCFile(String path) {
69 resetData();
70 mNeedsUpdating = false;
71 if (path == null || path.length() <= 0 || !path.startsWith(PATH_SEPARATOR)) {
72 throw new IllegalArgumentException("Trying to create a OCFile with a non valid remote path: " + path);
73 }
74 mRemotePath = path;
75 }
76
77 /**
78 * Reconstruct from parcel
79 *
80 * @param source The source parcel
81 */
82 private OCFile(Parcel source) {
83 mId = source.readLong();
84 mParentId = source.readLong();
85 mLength = source.readLong();
86 mCreationTimestamp = source.readLong();
87 mModifiedTimestamp = source.readLong();
88 mModifiedTimestampAtLastSyncForData = source.readLong();
89 mRemotePath = source.readString();
90 mLocalPath = source.readString();
91 mMimeType = source.readString();
92 mNeedsUpdating = source.readInt() == 0;
93 mKeepInSync = source.readInt() == 1;
94 mLastSyncDateForProperties = source.readLong();
95 mLastSyncDateForData = source.readLong();
96 }
97
98 @Override
99 public void writeToParcel(Parcel dest, int flags) {
100 dest.writeLong(mId);
101 dest.writeLong(mParentId);
102 dest.writeLong(mLength);
103 dest.writeLong(mCreationTimestamp);
104 dest.writeLong(mModifiedTimestamp);
105 dest.writeLong(mModifiedTimestampAtLastSyncForData);
106 dest.writeString(mRemotePath);
107 dest.writeString(mLocalPath);
108 dest.writeString(mMimeType);
109 dest.writeInt(mNeedsUpdating ? 1 : 0);
110 dest.writeInt(mKeepInSync ? 1 : 0);
111 dest.writeLong(mLastSyncDateForProperties);
112 dest.writeLong(mLastSyncDateForData);
113 }
114
115 /**
116 * Gets the ID of the file
117 *
118 * @return the file ID
119 */
120 public long getFileId() {
121 return mId;
122 }
123
124 /**
125 * Returns the remote path of the file on ownCloud
126 *
127 * @return The remote path to the file
128 */
129 public String getRemotePath() {
130 return mRemotePath;
131 }
132
133 /**
134 * Can be used to check, whether or not this file exists in the database
135 * already
136 *
137 * @return true, if the file exists in the database
138 */
139 public boolean fileExists() {
140 return mId != -1;
141 }
142
143 /**
144 * Use this to find out if this file is a Directory
145 *
146 * @return true if it is a directory
147 */
148 public boolean isDirectory() {
149 return mMimeType != null && mMimeType.equals("DIR");
150 }
151
152 /**
153 * Use this to check if this file is available locally
154 *
155 * @return true if it is
156 */
157 public boolean isDown() {
158 if (mLocalPath != null && mLocalPath.length() > 0) {
159 File file = new File(mLocalPath);
160 return (file.exists());
161 }
162 return false;
163 }
164
165 /**
166 * The path, where the file is stored locally
167 *
168 * @return The local path to the file
169 */
170 public String getStoragePath() {
171 return mLocalPath;
172 }
173
174 /**
175 * Can be used to set the path where the file is stored
176 *
177 * @param storage_path to set
178 */
179 public void setStoragePath(String storage_path) {
180 mLocalPath = storage_path;
181 }
182
183 /**
184 * Get a UNIX timestamp of the file creation time
185 *
186 * @return A UNIX timestamp of the time that file was created
187 */
188 public long getCreationTimestamp() {
189 return mCreationTimestamp;
190 }
191
192 /**
193 * Set a UNIX timestamp of the time the file was created
194 *
195 * @param creation_timestamp to set
196 */
197 public void setCreationTimestamp(long creation_timestamp) {
198 mCreationTimestamp = creation_timestamp;
199 }
200
201 /**
202 * Get a UNIX timestamp of the file modification time.
203 *
204 * @return A UNIX timestamp of the modification time, corresponding to the value returned by the server
205 * in the last synchronization of the properties of this file.
206 */
207 public long getModificationTimestamp() {
208 return mModifiedTimestamp;
209 }
210
211 /**
212 * Set a UNIX timestamp of the time the time the file was modified.
213 *
214 * To update with the value returned by the server in every synchronization of the properties
215 * of this file.
216 *
217 * @param modification_timestamp to set
218 */
219 public void setModificationTimestamp(long modification_timestamp) {
220 mModifiedTimestamp = modification_timestamp;
221 }
222
223
224 /**
225 * Get a UNIX timestamp of the file modification time.
226 *
227 * @return A UNIX timestamp of the modification time, corresponding to the value returned by the server
228 * in the last synchronization of THE CONTENTS of this file.
229 */
230 public long getModificationTimestampAtLastSyncForData() {
231 return mModifiedTimestampAtLastSyncForData;
232 }
233
234 /**
235 * Set a UNIX timestamp of the time the time the file was modified.
236 *
237 * To update with the value returned by the server in every synchronization of THE CONTENTS
238 * of this file.
239 *
240 * @param modification_timestamp to set
241 */
242 public void setModificationTimestampAtLastSyncForData(long modificationTimestamp) {
243 mModifiedTimestampAtLastSyncForData = modificationTimestamp;
244 }
245
246
247
248 /**
249 * Returns the filename and "/" for the root directory
250 *
251 * @return The name of the file
252 */
253 public String getFileName() {
254 File f = new File(getRemotePath());
255 return f.getName().length() == 0 ? PATH_SEPARATOR : f.getName();
256 }
257
258 /**
259 * Sets the name of the file
260 *
261 * Does nothing if the new name is null, empty or includes "/" ; or if the file is the root directory
262 */
263 public void setFileName(String name) {
264 Log.d(TAG, "OCFile name changin from " + mRemotePath);
265 if (name != null && name.length() > 0 && !name.contains(PATH_SEPARATOR) && !mRemotePath.equals(PATH_SEPARATOR)) {
266 String parent = (new File(getRemotePath())).getParent();
267 parent = (parent.endsWith(PATH_SEPARATOR)) ? parent : parent + PATH_SEPARATOR;
268 mRemotePath = parent + name;
269 if (isDirectory()) {
270 mRemotePath += PATH_SEPARATOR;
271 }
272 Log.d(TAG, "OCFile name changed to " + mRemotePath);
273 }
274 }
275
276 /**
277 * Can be used to get the Mimetype
278 *
279 * @return the Mimetype as a String
280 */
281 public String getMimetype() {
282 return mMimeType;
283 }
284
285 /**
286 * Adds a file to this directory. If this file is not a directory, an
287 * exception gets thrown.
288 *
289 * @param file to add
290 * @throws IllegalStateException if you try to add a something and this is
291 * not a directory
292 */
293 public void addFile(OCFile file) throws IllegalStateException {
294 if (isDirectory()) {
295 file.mParentId = mId;
296 mNeedsUpdating = true;
297 return;
298 }
299 throw new IllegalStateException(
300 "This is not a directory where you can add stuff to!");
301 }
302
303 /**
304 * Used internally. Reset all file properties
305 */
306 private void resetData() {
307 mId = -1;
308 mRemotePath = null;
309 mParentId = 0;
310 mLocalPath = null;
311 mMimeType = null;
312 mLength = 0;
313 mCreationTimestamp = 0;
314 mModifiedTimestamp = 0;
315 mModifiedTimestampAtLastSyncForData = 0;
316 mLastSyncDateForProperties = 0;
317 mLastSyncDateForData = 0;
318 mKeepInSync = false;
319 mNeedsUpdating = false;
320 }
321
322 /**
323 * Sets the ID of the file
324 *
325 * @param file_id to set
326 */
327 public void setFileId(long file_id) {
328 mId = file_id;
329 }
330
331 /**
332 * Sets the Mime-Type of the
333 *
334 * @param mimetype to set
335 */
336 public void setMimetype(String mimetype) {
337 mMimeType = mimetype;
338 }
339
340 /**
341 * Sets the ID of the parent folder
342 *
343 * @param parent_id to set
344 */
345 public void setParentId(long parent_id) {
346 mParentId = parent_id;
347 }
348
349 /**
350 * Sets the file size in bytes
351 *
352 * @param file_len to set
353 */
354 public void setFileLength(long file_len) {
355 mLength = file_len;
356 }
357
358 /**
359 * Returns the size of the file in bytes
360 *
361 * @return The filesize in bytes
362 */
363 public long getFileLength() {
364 return mLength;
365 }
366
367 /**
368 * Returns the ID of the parent Folder
369 *
370 * @return The ID
371 */
372 public long getParentId() {
373 return mParentId;
374 }
375
376 /**
377 * Check, if this file needs updating
378 *
379 * @return
380 */
381 public boolean needsUpdatingWhileSaving() {
382 return mNeedsUpdating;
383 }
384
385 public long getLastSyncDateForProperties() {
386 return mLastSyncDateForProperties;
387 }
388
389 public void setLastSyncDateForProperties(long lastSyncDate) {
390 mLastSyncDateForProperties = lastSyncDate;
391 }
392
393 public long getLastSyncDateForData() {
394 return mLastSyncDateForData;
395 }
396
397 public void setLastSyncDateForData(long lastSyncDate) {
398 mLastSyncDateForData = lastSyncDate;
399 }
400
401 public void setKeepInSync(boolean keepInSync) {
402 mKeepInSync = keepInSync;
403 }
404
405 public boolean keepInSync() {
406 return mKeepInSync;
407 }
408
409 @Override
410 public int describeContents() {
411 return this.hashCode();
412 }
413
414 @Override
415 public int compareTo(OCFile another) {
416 if (isDirectory() && another.isDirectory()) {
417 return getRemotePath().toLowerCase().compareTo(another.getRemotePath().toLowerCase());
418 } else if (isDirectory()) {
419 return -1;
420 } else if (another.isDirectory()) {
421 return 1;
422 }
423 return getRemotePath().toLowerCase().compareTo(another.getRemotePath().toLowerCase());
424 }
425
426 @Override
427 public boolean equals(Object o) {
428 if(o instanceof OCFile){
429 OCFile that = (OCFile) o;
430 if(that != null){
431 return this.mId == that.mId;
432 }
433 }
434
435 return false;
436 }
437
438 @Override
439 public String toString() {
440 String asString = "[id=%s, name=%s, mime=%s, downloaded=%s, local=%s, remote=%s, parentId=%s, keepInSinc=%s]";
441 asString = String.format(asString, Long.valueOf(mId), getFileName(), mMimeType, isDown(), mLocalPath, mRemotePath, Long.valueOf(mParentId), Boolean.valueOf(mKeepInSync));
442 return asString;
443 }
444
445 public String getEtag() {
446 return mEtag;
447 }
448
449 public long getLocalModificationTimestamp() {
450 if (mLocalPath != null && mLocalPath.length() > 0) {
451 File f = new File(mLocalPath);
452 return f.lastModified();
453 }
454 return 0;
455 }
456
457 public boolean isAudio() {
458 return (mMimeType.startsWith("audio/"));
459 }
460
461 }