OCFile.java 19 KB

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