OCFile.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. /**
  2. * ownCloud Android client application
  3. *
  4. * @author Bartek Przybylski
  5. * @author David A. Velasco
  6. * Copyright (C) 2012 Bartek Przybylski
  7. * Copyright (C) 2016 ownCloud Inc.
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. package com.owncloud.android.datamodel;
  23. import android.content.ContentResolver;
  24. import android.content.Context;
  25. import android.net.Uri;
  26. import android.os.Build;
  27. import android.os.Parcel;
  28. import android.os.Parcelable;
  29. import android.support.v4.content.FileProvider;
  30. import com.owncloud.android.R;
  31. import com.owncloud.android.lib.common.network.WebdavUtils;
  32. import com.owncloud.android.lib.common.utils.Log_OC;
  33. import com.owncloud.android.utils.MimeType;
  34. import java.io.File;
  35. import third_parties.daveKoeller.AlphanumComparator;
  36. public class OCFile implements Parcelable, Comparable<OCFile> {
  37. public static final Parcelable.Creator<OCFile> CREATOR = new Parcelable.Creator<OCFile>() {
  38. @Override
  39. public OCFile createFromParcel(Parcel source) {
  40. return new OCFile(source);
  41. }
  42. @Override
  43. public OCFile[] newArray(int size) {
  44. return new OCFile[size];
  45. }
  46. };
  47. private final static String PERMISSION_SHARED_WITH_ME = "S"; // TODO move to better location
  48. public static final String PATH_SEPARATOR = "/";
  49. public static final String ROOT_PATH = PATH_SEPARATOR;
  50. private static final String TAG = OCFile.class.getSimpleName();
  51. private long mId;
  52. private long mParentId;
  53. private long mLength;
  54. private long mCreationTimestamp;
  55. private long mModifiedTimestamp;
  56. private long mModifiedTimestampAtLastSyncForData;
  57. private String mRemotePath;
  58. private String mLocalPath;
  59. private String mMimeType;
  60. private boolean mNeedsUpdating;
  61. private long mLastSyncDateForProperties;
  62. private long mLastSyncDateForData;
  63. private boolean mFavorite;
  64. private String mEtag;
  65. private boolean mShareByLink;
  66. private String mPublicLink;
  67. private String mPermissions;
  68. private String mRemoteId;
  69. private boolean mNeedsUpdateThumbnail;
  70. private boolean mIsDownloading;
  71. private String mEtagInConflict; // Save file etag in the server, when there is a conflict. No conflict = null
  72. private boolean mShareWithSharee;
  73. /**
  74. * URI to the local path of the file contents, if stored in the device; cached after first call
  75. * to {@link #getStorageUri()}
  76. */
  77. private Uri mLocalUri;
  78. /**
  79. * Exportable URI to the local path of the file contents, if stored in the device.
  80. *
  81. * Cached after first call, until changed.
  82. */
  83. private Uri mExposedFileUri;
  84. /**
  85. * Create new {@link OCFile} with given path.
  86. * <p/>
  87. * The path received must be URL-decoded. Path separator must be OCFile.PATH_SEPARATOR, and it must be the first character in 'path'.
  88. *
  89. * @param path The remote path of the file.
  90. */
  91. public OCFile(String path) {
  92. resetData();
  93. mNeedsUpdating = false;
  94. if (path == null || path.length() <= 0 || !path.startsWith(PATH_SEPARATOR)) {
  95. throw new IllegalArgumentException("Trying to create a OCFile with a non valid remote path: " + path);
  96. }
  97. mRemotePath = path;
  98. }
  99. /**
  100. * Reconstruct from parcel
  101. *
  102. * @param source The source parcel
  103. */
  104. private OCFile(Parcel source) {
  105. mId = source.readLong();
  106. mParentId = source.readLong();
  107. mLength = source.readLong();
  108. mCreationTimestamp = source.readLong();
  109. mModifiedTimestamp = source.readLong();
  110. mModifiedTimestampAtLastSyncForData = source.readLong();
  111. mRemotePath = source.readString();
  112. mLocalPath = source.readString();
  113. mMimeType = source.readString();
  114. mNeedsUpdating = source.readInt() == 0;
  115. mFavorite = source.readInt() == 1;
  116. mLastSyncDateForProperties = source.readLong();
  117. mLastSyncDateForData = source.readLong();
  118. mEtag = source.readString();
  119. mShareByLink = source.readInt() == 1;
  120. mPublicLink = source.readString();
  121. mPermissions = source.readString();
  122. mRemoteId = source.readString();
  123. mNeedsUpdateThumbnail = source.readInt() == 1;
  124. mIsDownloading = source.readInt() == 1;
  125. mEtagInConflict = source.readString();
  126. mShareWithSharee = source.readInt() == 1;
  127. }
  128. @Override
  129. public void writeToParcel(Parcel dest, int flags) {
  130. dest.writeLong(mId);
  131. dest.writeLong(mParentId);
  132. dest.writeLong(mLength);
  133. dest.writeLong(mCreationTimestamp);
  134. dest.writeLong(mModifiedTimestamp);
  135. dest.writeLong(mModifiedTimestampAtLastSyncForData);
  136. dest.writeString(mRemotePath);
  137. dest.writeString(mLocalPath);
  138. dest.writeString(mMimeType);
  139. dest.writeInt(mNeedsUpdating ? 1 : 0);
  140. dest.writeInt(mFavorite ? 1 : 0);
  141. dest.writeLong(mLastSyncDateForProperties);
  142. dest.writeLong(mLastSyncDateForData);
  143. dest.writeString(mEtag);
  144. dest.writeInt(mShareByLink ? 1 : 0);
  145. dest.writeString(mPublicLink);
  146. dest.writeString(mPermissions);
  147. dest.writeString(mRemoteId);
  148. dest.writeInt(mNeedsUpdateThumbnail ? 1 : 0);
  149. dest.writeInt(mIsDownloading ? 1 : 0);
  150. dest.writeString(mEtagInConflict);
  151. dest.writeInt(mShareWithSharee ? 1 : 0);
  152. }
  153. /**
  154. * Gets the ID of the file
  155. *
  156. * @return the file ID
  157. */
  158. public long getFileId() {
  159. return mId;
  160. }
  161. /**
  162. * Returns the remote path of the file on ownCloud
  163. *
  164. * @return The remote path to the file
  165. */
  166. public String getRemotePath() {
  167. return mRemotePath;
  168. }
  169. /**
  170. * Can be used to check, whether or not this file exists in the database
  171. * already
  172. *
  173. * @return true, if the file exists in the database
  174. */
  175. public boolean fileExists() {
  176. return mId != -1;
  177. }
  178. /**
  179. * Use this to find out if this file is a folder.
  180. *
  181. * @return true if it is a folder
  182. */
  183. public boolean isFolder() {
  184. return mMimeType != null && mMimeType.equals(MimeType.DIRECTORY);
  185. }
  186. /**
  187. * Use this to check if this file is available locally
  188. *
  189. * @return true if it is
  190. */
  191. public boolean isDown() {
  192. if (mLocalPath != null && mLocalPath.length() > 0) {
  193. File file = new File(mLocalPath);
  194. return (file.exists());
  195. }
  196. return false;
  197. }
  198. /**
  199. * The path, where the file is stored locally
  200. *
  201. * @return The local path to the file
  202. */
  203. public String getStoragePath() {
  204. return mLocalPath;
  205. }
  206. /**
  207. * The URI to the file contents, if stored locally
  208. *
  209. * @return A URI to the local copy of the file, or NULL if not stored in the device
  210. */
  211. public Uri getStorageUri() {
  212. if (mLocalPath == null || mLocalPath.length() == 0) {
  213. return null;
  214. }
  215. if (mLocalUri == null) {
  216. Uri.Builder builder = new Uri.Builder();
  217. builder.scheme(ContentResolver.SCHEME_FILE);
  218. builder.path(mLocalPath);
  219. mLocalUri = builder.build();
  220. }
  221. return mLocalUri;
  222. }
  223. public Uri getExposedFileUri(Context context) {
  224. if (mLocalPath == null || mLocalPath.length() == 0) {
  225. return null;
  226. }
  227. if (mExposedFileUri == null) {
  228. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
  229. // TODO - use FileProvider with any Android version, with deeper testing -> 2.2.0
  230. mExposedFileUri = Uri.parse(
  231. ContentResolver.SCHEME_FILE + "://" + WebdavUtils.encodePath(mLocalPath)
  232. );
  233. } else {
  234. // Use the FileProvider to get a content URI
  235. try {
  236. mExposedFileUri = FileProvider.getUriForFile(
  237. context,
  238. context.getString(R.string.file_provider_authority),
  239. new File(mLocalPath)
  240. );
  241. } catch (IllegalArgumentException e) {
  242. Log_OC.e(TAG, "File can't be exported");
  243. }
  244. }
  245. }
  246. return mExposedFileUri;
  247. }
  248. /**
  249. * Can be used to set the path where the file is stored
  250. *
  251. * @param storage_path to set
  252. */
  253. public void setStoragePath(String storage_path) {
  254. mLocalPath = storage_path;
  255. mLocalUri = null;
  256. mExposedFileUri = null;
  257. }
  258. /**
  259. * Get a UNIX timestamp of the file creation time
  260. *
  261. * @return A UNIX timestamp of the time that file was created
  262. */
  263. public long getCreationTimestamp() {
  264. return mCreationTimestamp;
  265. }
  266. /**
  267. * Set a UNIX timestamp of the time the file was created
  268. *
  269. * @param creation_timestamp to set
  270. */
  271. public void setCreationTimestamp(long creation_timestamp) {
  272. mCreationTimestamp = creation_timestamp;
  273. }
  274. /**
  275. * Get a UNIX timestamp of the file modification time.
  276. *
  277. * @return A UNIX timestamp of the modification time, corresponding to the value returned by the server
  278. * in the last synchronization of the properties of this file.
  279. */
  280. public long getModificationTimestamp() {
  281. return mModifiedTimestamp;
  282. }
  283. /**
  284. * Set a UNIX timestamp of the time the time the file was modified.
  285. * <p/>
  286. * To update with the value returned by the server in every synchronization of the properties
  287. * of this file.
  288. *
  289. * @param modification_timestamp to set
  290. */
  291. public void setModificationTimestamp(long modification_timestamp) {
  292. mModifiedTimestamp = modification_timestamp;
  293. }
  294. /**
  295. * Get a UNIX timestamp of the file modification time.
  296. *
  297. * @return A UNIX timestamp of the modification time, corresponding to the value returned by the server
  298. * in the last synchronization of THE CONTENTS of this file.
  299. */
  300. public long getModificationTimestampAtLastSyncForData() {
  301. return mModifiedTimestampAtLastSyncForData;
  302. }
  303. /**
  304. * Set a UNIX timestamp of the time the time the file was modified.
  305. * <p/>
  306. * To update with the value returned by the server in every synchronization of THE CONTENTS
  307. * of this file.
  308. *
  309. * @param modificationTimestamp to set
  310. */
  311. public void setModificationTimestampAtLastSyncForData(long modificationTimestamp) {
  312. mModifiedTimestampAtLastSyncForData = modificationTimestamp;
  313. }
  314. /**
  315. * Returns the filename and "/" for the root directory
  316. *
  317. * @return The name of the file
  318. */
  319. public String getFileName() {
  320. File f = new File(getRemotePath());
  321. return f.getName().length() == 0 ? ROOT_PATH : f.getName();
  322. }
  323. /**
  324. * Sets the name of the file
  325. * <p/>
  326. * Does nothing if the new name is null, empty or includes "/" ; or if the file is the root
  327. * directory
  328. */
  329. public void setFileName(String name) {
  330. Log_OC.d(TAG, "OCFile name changin from " + mRemotePath);
  331. if (name != null && name.length() > 0 && !name.contains(PATH_SEPARATOR) &&
  332. !mRemotePath.equals(ROOT_PATH)) {
  333. String parent = (new File(getRemotePath())).getParent();
  334. parent = (parent.endsWith(PATH_SEPARATOR)) ? parent : parent + PATH_SEPARATOR;
  335. mRemotePath = parent + name;
  336. if (isFolder()) {
  337. mRemotePath += PATH_SEPARATOR;
  338. }
  339. Log_OC.d(TAG, "OCFile name changed to " + mRemotePath);
  340. }
  341. }
  342. /**
  343. * Can be used to get the Mimetype
  344. *
  345. * @return the Mimetype as a String
  346. */
  347. public String getMimetype() {
  348. return mMimeType;
  349. }
  350. /**
  351. * Used internally. Reset all file properties
  352. */
  353. private void resetData() {
  354. mId = -1;
  355. mRemotePath = null;
  356. mParentId = 0;
  357. mLocalPath = null;
  358. mMimeType = null;
  359. mLength = 0;
  360. mCreationTimestamp = 0;
  361. mModifiedTimestamp = 0;
  362. mModifiedTimestampAtLastSyncForData = 0;
  363. mLastSyncDateForProperties = 0;
  364. mLastSyncDateForData = 0;
  365. mFavorite = false;
  366. mNeedsUpdating = false;
  367. mEtag = null;
  368. mShareByLink = false;
  369. mPublicLink = null;
  370. mPermissions = null;
  371. mRemoteId = null;
  372. mNeedsUpdateThumbnail = false;
  373. mIsDownloading = false;
  374. mEtagInConflict = null;
  375. mShareWithSharee = false;
  376. }
  377. /**
  378. * Sets the ID of the file
  379. *
  380. * @param file_id to set
  381. */
  382. public void setFileId(long file_id) {
  383. mId = file_id;
  384. }
  385. /**
  386. * Sets the Mime-Type of the
  387. *
  388. * @param mimetype to set
  389. */
  390. public void setMimetype(String mimetype) {
  391. mMimeType = mimetype;
  392. }
  393. /**
  394. * Sets the ID of the parent folder
  395. *
  396. * @param parent_id to set
  397. */
  398. public void setParentId(long parent_id) {
  399. mParentId = parent_id;
  400. }
  401. /**
  402. * Sets the file size in bytes
  403. *
  404. * @param file_len to set
  405. */
  406. public void setFileLength(long file_len) {
  407. mLength = file_len;
  408. }
  409. /**
  410. * Returns the size of the file in bytes
  411. *
  412. * @return The filesize in bytes
  413. */
  414. public long getFileLength() {
  415. return mLength;
  416. }
  417. /**
  418. * Returns the ID of the parent Folder
  419. *
  420. * @return The ID
  421. */
  422. public long getParentId() {
  423. return mParentId;
  424. }
  425. /**
  426. * get remote path of parent file
  427. * @return remote path
  428. */
  429. public String getParentRemotePath() {
  430. String parentPath = new File(getRemotePath()).getParent();
  431. return (parentPath.endsWith("/")) ? parentPath : (parentPath + "/");
  432. }
  433. /**
  434. * Check, if this file needs updating
  435. *
  436. * @return
  437. */
  438. public boolean needsUpdatingWhileSaving() {
  439. return mNeedsUpdating;
  440. }
  441. public boolean needsUpdateThumbnail() {
  442. return mNeedsUpdateThumbnail;
  443. }
  444. public void setNeedsUpdateThumbnail(boolean needsUpdateThumbnail) {
  445. this.mNeedsUpdateThumbnail = needsUpdateThumbnail;
  446. }
  447. public long getLastSyncDateForProperties() {
  448. return mLastSyncDateForProperties;
  449. }
  450. public void setLastSyncDateForProperties(long lastSyncDate) {
  451. mLastSyncDateForProperties = lastSyncDate;
  452. }
  453. public long getLastSyncDateForData() {
  454. return mLastSyncDateForData;
  455. }
  456. public void setLastSyncDateForData(long lastSyncDate) {
  457. mLastSyncDateForData = lastSyncDate;
  458. }
  459. public void setFavorite(boolean favorite) {
  460. mFavorite = favorite;
  461. }
  462. public boolean isFavorite() {
  463. return mFavorite;
  464. }
  465. @Override
  466. public int describeContents() {
  467. return super.hashCode();
  468. }
  469. @Override
  470. public int compareTo(OCFile another) {
  471. if (isFolder() && another.isFolder()) {
  472. return getRemotePath().toLowerCase().compareTo(another.getRemotePath().toLowerCase());
  473. } else if (isFolder()) {
  474. return -1;
  475. } else if (another.isFolder()) {
  476. return 1;
  477. }
  478. return new AlphanumComparator().compare(this, another);
  479. }
  480. @Override
  481. public boolean equals(Object o) {
  482. if (this == o) {
  483. return true;
  484. }
  485. if (o == null || getClass() != o.getClass()) {
  486. return false;
  487. }
  488. OCFile ocFile = (OCFile) o;
  489. return mId == ocFile.mId && mParentId == ocFile.mParentId;
  490. }
  491. @Override
  492. public int hashCode() {
  493. int result = (int) (mId ^ (mId >>> 32));
  494. result = 31 * result + (int) (mParentId ^ (mParentId >>> 32));
  495. return result;
  496. }
  497. @Override
  498. public String toString() {
  499. String asString = "[id=%s, name=%s, mime=%s, downloaded=%s, local=%s, remote=%s, " +
  500. "parentId=%s, favorite=%s etag=%s]";
  501. asString = String.format(asString, mId, getFileName(), mMimeType, isDown(),
  502. mLocalPath, mRemotePath, mParentId, mFavorite,
  503. mEtag);
  504. return asString;
  505. }
  506. public String getEtag() {
  507. return mEtag;
  508. }
  509. public void setEtag(String etag) {
  510. this.mEtag = (etag != null ? etag : "");
  511. }
  512. public boolean isSharedViaLink() {
  513. return mShareByLink;
  514. }
  515. public void setShareViaLink(boolean shareByLink) {
  516. this.mShareByLink = shareByLink;
  517. }
  518. public String getPublicLink() {
  519. return mPublicLink;
  520. }
  521. public void setPublicLink(String publicLink) {
  522. this.mPublicLink = publicLink;
  523. }
  524. public long getLocalModificationTimestamp() {
  525. if (mLocalPath != null && mLocalPath.length() > 0) {
  526. File f = new File(mLocalPath);
  527. return f.lastModified();
  528. }
  529. return 0;
  530. }
  531. /**
  532. * @return 'True' if the file is hidden
  533. */
  534. public boolean isHidden() {
  535. return getFileName().startsWith(".");
  536. }
  537. public String getPermissions() {
  538. return mPermissions;
  539. }
  540. public void setPermissions(String permissions) {
  541. this.mPermissions = permissions;
  542. }
  543. public String getRemoteId() {
  544. return mRemoteId;
  545. }
  546. public void setRemoteId(String remoteId) {
  547. this.mRemoteId = remoteId;
  548. }
  549. public boolean isDownloading() {
  550. return mIsDownloading;
  551. }
  552. public void setDownloading(boolean isDownloading) {
  553. this.mIsDownloading = isDownloading;
  554. }
  555. public String getEtagInConflict() {
  556. return mEtagInConflict;
  557. }
  558. public boolean isInConflict() {
  559. return mEtagInConflict != null && !mEtagInConflict.equals("");
  560. }
  561. public void setEtagInConflict(String etagInConflict) {
  562. mEtagInConflict = etagInConflict;
  563. }
  564. public boolean isSharedWithSharee() {
  565. return mShareWithSharee;
  566. }
  567. public void setShareWithSharee(boolean shareWithSharee) {
  568. this.mShareWithSharee = shareWithSharee;
  569. }
  570. public boolean isSharedWithMe() {
  571. String permissions = getPermissions();
  572. return (permissions != null && permissions.contains(PERMISSION_SHARED_WITH_ME));
  573. }
  574. }