OCFile.java 18 KB

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