OCFile.java 20 KB

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