OCFile.java 19 KB

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