2 * ownCloud Android client application
4 * Copyright (C) 2012 Bartek Przybylski
5 * Copyright (C) 2015 ownCloud Inc.
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2,
9 * as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 package com
.owncloud
.android
.datamodel
;
23 import android
.content
.ContentResolver
;
24 import android
.net
.Uri
;
25 import android
.os
.Parcel
;
26 import android
.os
.Parcelable
;
27 import android
.webkit
.MimeTypeMap
;
29 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
33 import third_parties
.daveKoeller
.AlphanumComparator
;
34 public class OCFile
implements Parcelable
, Comparable
<OCFile
> {
36 public static final Parcelable
.Creator
<OCFile
> CREATOR
= new Parcelable
.Creator
<OCFile
>() {
38 public OCFile
createFromParcel(Parcel source
) {
39 return new OCFile(source
);
43 public OCFile
[] newArray(int size
) {
44 return new OCFile
[size
];
48 private final static String PERMISSION_SHARED_WITH_ME
= "S"; // TODO move to better location
50 public static final String PATH_SEPARATOR
= "/";
51 public static final String ROOT_PATH
= PATH_SEPARATOR
;
53 private static final String TAG
= OCFile
.class.getSimpleName();
56 private long mParentId
;
58 private long mCreationTimestamp
;
59 private long mModifiedTimestamp
;
60 private long mModifiedTimestampAtLastSyncForData
;
61 private String mRemotePath
;
62 private String mLocalPath
;
63 private String mMimeType
;
64 private boolean mNeedsUpdating
;
65 private long mLastSyncDateForProperties
;
66 private long mLastSyncDateForData
;
67 private boolean mFavorite
;
71 private boolean mShareByLink
;
72 private String mPublicLink
;
74 private String mPermissions
;
75 private String mRemoteId
;
77 private boolean mNeedsUpdateThumbnail
;
79 private boolean mIsDownloading
;
81 private String mEtagInConflict
; // Save file etag in the server, when there is a conflict. No conflict = null
83 private boolean mShareWithSharee
;
86 * URI to the local path of the file contents, if stored in the device; cached after first call
87 * to {@link #getStorageUri()}
89 private Uri mLocalUri
;
93 * Create new {@link OCFile} with given path.
95 * The path received must be URL-decoded. Path separator must be OCFile.PATH_SEPARATOR, and it must be the first character in 'path'.
97 * @param path The remote path of the file.
99 public OCFile(String path
) {
101 mNeedsUpdating
= false
;
102 if (path
== null
|| path
.length() <= 0 || !path
.startsWith(PATH_SEPARATOR
)) {
103 throw new IllegalArgumentException("Trying to create a OCFile with a non valid remote path: " + path
);
109 * Reconstruct from parcel
111 * @param source The source parcel
113 private OCFile(Parcel source
) {
114 mId
= source
.readLong();
115 mParentId
= source
.readLong();
116 mLength
= source
.readLong();
117 mCreationTimestamp
= source
.readLong();
118 mModifiedTimestamp
= source
.readLong();
119 mModifiedTimestampAtLastSyncForData
= source
.readLong();
120 mRemotePath
= source
.readString();
121 mLocalPath
= source
.readString();
122 mMimeType
= source
.readString();
123 mNeedsUpdating
= source
.readInt() == 0;
124 mFavorite
= source
.readInt() == 1;
125 mLastSyncDateForProperties
= source
.readLong();
126 mLastSyncDateForData
= source
.readLong();
127 mEtag
= source
.readString();
128 mShareByLink
= source
.readInt() == 1;
129 mPublicLink
= source
.readString();
130 mPermissions
= source
.readString();
131 mRemoteId
= source
.readString();
132 mNeedsUpdateThumbnail
= source
.readInt() == 1;
133 mIsDownloading
= source
.readInt() == 1;
134 mEtagInConflict
= source
.readString();
135 mShareWithSharee
= source
.readInt() == 1;
140 public void writeToParcel(Parcel dest
, int flags
) {
142 dest
.writeLong(mParentId
);
143 dest
.writeLong(mLength
);
144 dest
.writeLong(mCreationTimestamp
);
145 dest
.writeLong(mModifiedTimestamp
);
146 dest
.writeLong(mModifiedTimestampAtLastSyncForData
);
147 dest
.writeString(mRemotePath
);
148 dest
.writeString(mLocalPath
);
149 dest
.writeString(mMimeType
);
150 dest
.writeInt(mNeedsUpdating ?
1 : 0);
151 dest
.writeInt(mFavorite ?
1 : 0);
152 dest
.writeLong(mLastSyncDateForProperties
);
153 dest
.writeLong(mLastSyncDateForData
);
154 dest
.writeString(mEtag
);
155 dest
.writeInt(mShareByLink ?
1 : 0);
156 dest
.writeString(mPublicLink
);
157 dest
.writeString(mPermissions
);
158 dest
.writeString(mRemoteId
);
159 dest
.writeInt(mNeedsUpdateThumbnail ?
1 : 0);
160 dest
.writeInt(mIsDownloading ?
1 : 0);
161 dest
.writeString(mEtagInConflict
);
162 dest
.writeInt(mShareWithSharee ?
1 : 0);
166 * Gets the ID of the file
168 * @return the file ID
170 public long getFileId() {
175 * Returns the remote path of the file on ownCloud
177 * @return The remote path to the file
179 public String
getRemotePath() {
184 * Can be used to check, whether or not this file exists in the database
187 * @return true, if the file exists in the database
189 public boolean fileExists() {
194 * Use this to find out if this file is a folder.
196 * @return true if it is a folder
198 public boolean isFolder() {
199 return mMimeType
!= null
&& mMimeType
.equals("DIR");
203 * Use this to check if this file is available locally
205 * @return true if it is
207 public boolean isDown() {
208 if (mLocalPath
!= null
&& mLocalPath
.length() > 0) {
209 File file
= new File(mLocalPath
);
210 return (file
.exists());
216 * The path, where the file is stored locally
218 * @return The local path to the file
220 public String
getStoragePath() {
225 * The URI to the file contents, if stored locally
227 * @return A URI to the local copy of the file, or NULL if not stored in the device
229 public Uri
getStorageUri() {
230 if (mLocalPath
== null
|| mLocalPath
.length() == 0) {
233 if (mLocalUri
== null
) {
234 Uri
.Builder builder
= new Uri
.Builder();
235 builder
.scheme(ContentResolver
.SCHEME_FILE
);
236 builder
.path(mLocalPath
);
237 mLocalUri
= builder
.build();
243 * Can be used to set the path where the file is stored
245 * @param storage_path to set
247 public void setStoragePath(String storage_path
) {
248 mLocalPath
= storage_path
;
253 * Get a UNIX timestamp of the file creation time
255 * @return A UNIX timestamp of the time that file was created
257 public long getCreationTimestamp() {
258 return mCreationTimestamp
;
262 * Set a UNIX timestamp of the time the file was created
264 * @param creation_timestamp to set
266 public void setCreationTimestamp(long creation_timestamp
) {
267 mCreationTimestamp
= creation_timestamp
;
271 * Get a UNIX timestamp of the file modification time.
273 * @return A UNIX timestamp of the modification time, corresponding to the value returned by the server
274 * in the last synchronization of the properties of this file.
276 public long getModificationTimestamp() {
277 return mModifiedTimestamp
;
281 * Set a UNIX timestamp of the time the time the file was modified.
283 * To update with the value returned by the server in every synchronization of the properties
286 * @param modification_timestamp to set
288 public void setModificationTimestamp(long modification_timestamp
) {
289 mModifiedTimestamp
= modification_timestamp
;
294 * Get a UNIX timestamp of the file modification time.
296 * @return A UNIX timestamp of the modification time, corresponding to the value returned by the server
297 * in the last synchronization of THE CONTENTS of this file.
299 public long getModificationTimestampAtLastSyncForData() {
300 return mModifiedTimestampAtLastSyncForData
;
304 * Set a UNIX timestamp of the time the time the file was modified.
306 * To update with the value returned by the server in every synchronization of THE CONTENTS
309 * @param modificationTimestamp to set
311 public void setModificationTimestampAtLastSyncForData(long modificationTimestamp
) {
312 mModifiedTimestampAtLastSyncForData
= modificationTimestamp
;
317 * Returns the filename and "/" for the root directory
319 * @return The name of the file
321 public String
getFileName() {
322 File f
= new File(getRemotePath());
323 return f
.getName().length() == 0 ? ROOT_PATH
: f
.getName();
327 * Sets the name of the file
329 * Does nothing if the new name is null, empty or includes "/" ; or if the file is the root
332 public void setFileName(String name
) {
333 Log_OC
.d(TAG
, "OCFile name changin from " + mRemotePath
);
334 if (name
!= null
&& name
.length() > 0 && !name
.contains(PATH_SEPARATOR
) &&
335 !mRemotePath
.equals(ROOT_PATH
)) {
336 String parent
= (new File(getRemotePath())).getParent();
337 parent
= (parent
.endsWith(PATH_SEPARATOR
)) ? parent
: parent
+ PATH_SEPARATOR
;
338 mRemotePath
= parent
+ name
;
340 mRemotePath
+= PATH_SEPARATOR
;
342 Log_OC
.d(TAG
, "OCFile name changed to " + mRemotePath
);
347 * Can be used to get the Mimetype
349 * @return the Mimetype as a String
351 public String
getMimetype() {
356 * Used internally. Reset all file properties
358 private void resetData() {
365 mCreationTimestamp
= 0;
366 mModifiedTimestamp
= 0;
367 mModifiedTimestampAtLastSyncForData
= 0;
368 mLastSyncDateForProperties
= 0;
369 mLastSyncDateForData
= 0;
371 mNeedsUpdating
= false
;
373 mShareByLink
= false
;
377 mNeedsUpdateThumbnail
= false
;
378 mIsDownloading
= false
;
379 mEtagInConflict
= null
;
380 mShareWithSharee
= false
;
384 * Sets the ID of the file
386 * @param file_id to set
388 public void setFileId(long file_id
) {
393 * Sets the Mime-Type of the
395 * @param mimetype to set
397 public void setMimetype(String mimetype
) {
398 mMimeType
= mimetype
;
402 * Sets the ID of the parent folder
404 * @param parent_id to set
406 public void setParentId(long parent_id
) {
407 mParentId
= parent_id
;
411 * Sets the file size in bytes
413 * @param file_len to set
415 public void setFileLength(long file_len
) {
420 * Returns the size of the file in bytes
422 * @return The filesize in bytes
424 public long getFileLength() {
429 * Returns the ID of the parent Folder
433 public long getParentId() {
438 * Check, if this file needs updating
442 public boolean needsUpdatingWhileSaving() {
443 return mNeedsUpdating
;
446 public boolean needsUpdateThumbnail() {
447 return mNeedsUpdateThumbnail
;
450 public void setNeedsUpdateThumbnail(boolean needsUpdateThumbnail
) {
451 this.mNeedsUpdateThumbnail
= needsUpdateThumbnail
;
454 public long getLastSyncDateForProperties() {
455 return mLastSyncDateForProperties
;
458 public void setLastSyncDateForProperties(long lastSyncDate
) {
459 mLastSyncDateForProperties
= lastSyncDate
;
462 public long getLastSyncDateForData() {
463 return mLastSyncDateForData
;
466 public void setLastSyncDateForData(long lastSyncDate
) {
467 mLastSyncDateForData
= lastSyncDate
;
470 public void setFavorite(boolean favorite
) {
471 mFavorite
= favorite
;
474 public boolean isFavorite() {
479 public int describeContents() {
480 return super.hashCode();
484 public int compareTo(OCFile another
) {
485 if (isFolder() && another
.isFolder()) {
486 return getRemotePath().toLowerCase().compareTo(another
.getRemotePath().toLowerCase());
487 } else if (isFolder()) {
489 } else if (another
.isFolder()) {
492 return new AlphanumComparator().compare(this, another
);
496 public boolean equals(Object o
) {
497 if (o
instanceof OCFile
) {
498 OCFile that
= (OCFile
) o
;
500 return this.mId
== that
.mId
;
508 public String
toString() {
509 String asString
= "[id=%s, name=%s, mime=%s, downloaded=%s, local=%s, remote=%s, " +
510 "parentId=%s, favorite=%s etag=%s]";
511 asString
= String
.format(asString
, Long
.valueOf(mId
), getFileName(), mMimeType
, isDown(),
512 mLocalPath
, mRemotePath
, Long
.valueOf(mParentId
), Boolean
.valueOf(mFavorite
),
517 public String
getEtag() {
521 public void setEtag(String etag
) {
522 this.mEtag
= (etag
!= null ? etag
: "");
526 public boolean isSharedViaLink() {
530 public void setShareViaLink(boolean shareByLink
) {
531 this.mShareByLink
= shareByLink
;
534 public String
getPublicLink() {
538 public void setPublicLink(String publicLink
) {
539 this.mPublicLink
= publicLink
;
542 public long getLocalModificationTimestamp() {
543 if (mLocalPath
!= null
&& mLocalPath
.length() > 0) {
544 File f
= new File(mLocalPath
);
545 return f
.lastModified();
551 * @return 'True' if the file contains audio
553 public boolean isAudio() {
554 return (mMimeType
!= null
&& mMimeType
.startsWith("audio/"));
558 * @return 'True' if the file contains video
560 public boolean isVideo() {
561 return (mMimeType
!= null
&& mMimeType
.startsWith("video/"));
565 * @return 'True' if the file contains an image
567 public boolean isImage() {
568 return ((mMimeType
!= null
&& mMimeType
.startsWith("image/")) ||
569 getMimeTypeFromName().startsWith("image/"));
573 * @return 'True' if the file is simple text (e.g. not application-dependent, like .doc or .docx)
575 public boolean isText() {
576 return ((mMimeType
!= null
&& mMimeType
.startsWith("text/")) ||
577 getMimeTypeFromName().startsWith("text/"));
580 public String
getMimeTypeFromName() {
581 String extension
= "";
582 int pos
= mRemotePath
.lastIndexOf('.');
584 extension
= mRemotePath
.substring(pos
+ 1);
586 String result
= MimeTypeMap
.getSingleton().
587 getMimeTypeFromExtension(extension
.toLowerCase());
588 return (result
!= null
) ? result
: "";
592 * @return 'True' if the file is hidden
594 public boolean isHidden() {
595 return getFileName().startsWith(".");
598 public String
getPermissions() {
602 public void setPermissions(String permissions
) {
603 this.mPermissions
= permissions
;
606 public String
getRemoteId() {
610 public void setRemoteId(String remoteId
) {
611 this.mRemoteId
= remoteId
;
614 public boolean isDownloading() {
615 return mIsDownloading
;
618 public void setDownloading(boolean isDownloading
) {
619 this.mIsDownloading
= isDownloading
;
622 public String
getEtagInConflict() {
623 return mEtagInConflict
;
626 public void setEtagInConflict(String etagInConflict
) {
627 mEtagInConflict
= etagInConflict
;
630 public boolean isSharedWithSharee() {
631 return mShareWithSharee
;
634 public void setShareWithSharee(boolean shareWithSharee
) {
635 this.mShareWithSharee
= shareWithSharee
;
638 public boolean isSharedWithMe() {
639 String permissions
= getPermissions();
640 return (permissions
!= null
&& permissions
.contains(PERMISSION_SHARED_WITH_ME
));