OCUpload.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /**
  2. * ownCloud Android client application
  3. *
  4. * @author LukeOwncloud
  5. * @author masensio
  6. * @author David A. Velasco
  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. package com.owncloud.android.db;
  22. import android.accounts.Account;
  23. import android.content.Context;
  24. import android.os.Parcel;
  25. import android.os.Parcelable;
  26. import com.owncloud.android.authentication.AccountUtils;
  27. import com.owncloud.android.datamodel.OCFile;
  28. import com.owncloud.android.datamodel.UploadsStorageManager;
  29. import com.owncloud.android.datamodel.UploadsStorageManager.UploadStatus;
  30. import com.owncloud.android.files.services.FileUploader;
  31. import com.owncloud.android.lib.common.utils.Log_OC;
  32. import com.owncloud.android.operations.UploadFileOperation;
  33. import com.owncloud.android.utils.MimeTypeUtil;
  34. import java.io.File;
  35. /**
  36. * Stores all information in order to start upload operations. PersistentUploadObject can
  37. * be stored persistently by {@link UploadsStorageManager}.
  38. *
  39. */
  40. public class OCUpload implements Parcelable {
  41. private static final String TAG = OCUpload.class.getSimpleName();
  42. private long mId;
  43. /**
  44. * Absolute path in the local file system to the file to be uploaded
  45. */
  46. private String mLocalPath;
  47. /**
  48. * Absolute path in the remote account to set to the uploaded file (not for its parent folder!)
  49. */
  50. private String mRemotePath;
  51. /**
  52. * Name of Owncloud account to upload file to.
  53. */
  54. private String mAccountName;
  55. /**
  56. * File size
  57. */
  58. private long mFileSize;
  59. /**
  60. * Local action for upload. (0 - COPY, 1 - MOVE, 2 - FORGET)
  61. */
  62. private int mLocalAction;
  63. /**
  64. * Overwrite destination file?
  65. */
  66. private boolean mForceOverwrite;
  67. /**
  68. * Create destination folder?
  69. */
  70. private boolean mIsCreateRemoteFolder;
  71. /**
  72. * Status of upload (later, in_progress, ...).
  73. */
  74. private UploadStatus mUploadStatus;
  75. /**
  76. * Result from last upload operation. Can be null.
  77. */
  78. private UploadResult mLastResult;
  79. /**
  80. * Defines the origin of the upload; see constants CREATED_ in {@link UploadFileOperation}
  81. */
  82. private int mCreatedBy;
  83. /*
  84. * When the upload ended
  85. */
  86. private long mUploadEndTimeStamp;
  87. /**
  88. * Main constructor
  89. *
  90. * @param localPath Absolute path in the local file system to the file to be uploaded.
  91. * @param remotePath Absolute path in the remote account to set to the uploaded file.
  92. * @param accountName Name of an ownCloud account to update the file to.
  93. */
  94. public OCUpload(String localPath, String remotePath, String accountName) {
  95. if (localPath == null || !localPath.startsWith(File.separator)) {
  96. throw new IllegalArgumentException("Local path must be an absolute path in the local file system");
  97. }
  98. if (remotePath == null || !remotePath.startsWith(OCFile.PATH_SEPARATOR)) {
  99. throw new IllegalArgumentException("Remote path must be an absolute path in the local file system");
  100. }
  101. if (accountName == null || accountName.length() < 1) {
  102. throw new IllegalArgumentException("Invalid account name");
  103. }
  104. resetData();
  105. mLocalPath = localPath;
  106. mRemotePath = remotePath;
  107. mAccountName = accountName;
  108. }
  109. /**
  110. * Convenience constructor to reupload already existing {@link OCFile}s
  111. *
  112. * @param ocFile {@link OCFile} instance to update in the remote server.
  113. * @param account ownCloud {@link Account} where ocFile is contained.
  114. */
  115. public OCUpload(OCFile ocFile, Account account) {
  116. this(ocFile.getStoragePath(), ocFile.getRemotePath(), account.name);
  117. }
  118. /**
  119. * Reset all the fields to default values.
  120. */
  121. private void resetData() {
  122. mRemotePath = "";
  123. mLocalPath = "";
  124. mAccountName = "";
  125. mFileSize = -1;
  126. mId = -1;
  127. mLocalAction = FileUploader.LOCAL_BEHAVIOUR_COPY;
  128. mForceOverwrite = false;
  129. mIsCreateRemoteFolder = false;
  130. mUploadStatus = UploadStatus.UPLOAD_IN_PROGRESS;
  131. mLastResult = UploadResult.UNKNOWN;
  132. mCreatedBy = UploadFileOperation.CREATED_BY_USER;
  133. }
  134. // Getters & Setters
  135. public void setUploadId(long id) {
  136. mId = id;
  137. }
  138. public long getUploadId() {
  139. return mId;
  140. }
  141. /**
  142. * @return the uploadStatus
  143. */
  144. public UploadStatus getUploadStatus() {
  145. return mUploadStatus;
  146. }
  147. /**
  148. * Sets uploadStatus AND SETS lastResult = null;
  149. * @param uploadStatus the uploadStatus to set
  150. */
  151. public void setUploadStatus(UploadStatus uploadStatus) {
  152. this.mUploadStatus = uploadStatus;
  153. setLastResult(UploadResult.UNKNOWN);
  154. }
  155. /**
  156. * @return the lastResult
  157. */
  158. public UploadResult getLastResult() {
  159. return mLastResult;
  160. }
  161. /**
  162. * @param lastResult the lastResult to set
  163. */
  164. public void setLastResult(UploadResult lastResult) {
  165. this.mLastResult = ((lastResult != null) ? lastResult : UploadResult.UNKNOWN);
  166. }
  167. /**
  168. * @return the localPath
  169. */
  170. public String getLocalPath() {
  171. return mLocalPath;
  172. }
  173. public void setLocalPath(String localPath) {
  174. mLocalPath = localPath;
  175. }
  176. /**
  177. * @return the remotePath
  178. */
  179. public String getRemotePath() {
  180. return mRemotePath;
  181. }
  182. /**
  183. * @param remotePath
  184. */
  185. public void setRemotePath(String remotePath) {
  186. mRemotePath = remotePath;
  187. }
  188. /**
  189. * @return File size
  190. */
  191. public long getFileSize() {
  192. return mFileSize;
  193. }
  194. public void setFileSize(long fileSize) {
  195. mFileSize = fileSize;
  196. }
  197. /**
  198. * @return the mimeType
  199. */
  200. public String getMimeType() {
  201. return MimeTypeUtil.getBestMimeTypeByFilename(mLocalPath);
  202. }
  203. /**
  204. * @return the localAction
  205. */
  206. public int getLocalAction() {
  207. return mLocalAction;
  208. }
  209. /**
  210. * @param localAction the localAction to set
  211. */
  212. public void setLocalAction(int localAction) {
  213. this.mLocalAction = localAction;
  214. }
  215. /**
  216. * @return the forceOverwrite
  217. */
  218. public boolean isForceOverwrite() {
  219. return mForceOverwrite;
  220. }
  221. /**
  222. * @param forceOverwrite the forceOverwrite to set
  223. */
  224. public void setForceOverwrite(boolean forceOverwrite) {
  225. this.mForceOverwrite = forceOverwrite;
  226. }
  227. /**
  228. * @return the isCreateRemoteFolder
  229. */
  230. public boolean isCreateRemoteFolder() {
  231. return mIsCreateRemoteFolder;
  232. }
  233. /**
  234. * @param isCreateRemoteFolder the isCreateRemoteFolder to set
  235. */
  236. public void setCreateRemoteFolder(boolean isCreateRemoteFolder) {
  237. this.mIsCreateRemoteFolder = isCreateRemoteFolder;
  238. }
  239. /**
  240. * @return the accountName
  241. */
  242. public String getAccountName() {
  243. return mAccountName;
  244. }
  245. /**
  246. * Returns owncloud account as {@link Account} object.
  247. */
  248. public Account getAccount(Context context) {
  249. return AccountUtils.getOwnCloudAccountByName(context, getAccountName());
  250. }
  251. public void setCreatedBy(int createdBy) {
  252. mCreatedBy = createdBy;
  253. }
  254. public int getCreadtedBy() {
  255. return mCreatedBy;
  256. }
  257. public void setUploadEndTimestamp(long uploadEndTimestamp) {
  258. mUploadEndTimeStamp = uploadEndTimestamp;
  259. }
  260. public long getUploadEndTimestamp() {
  261. return mUploadEndTimeStamp;
  262. }
  263. /**
  264. * For debugging purposes only.
  265. */
  266. public String toFormattedString() {
  267. try {
  268. String localPath = getLocalPath() != null ? getLocalPath() : "";
  269. return localPath + " status:" + getUploadStatus() + " result:" +
  270. (getLastResult() == null ? "null" : getLastResult().getValue());
  271. } catch (NullPointerException e) {
  272. Log_OC.d(TAG, "Exception " + e.toString());
  273. return (e.toString());
  274. }
  275. }
  276. /****
  277. *
  278. */
  279. public static final Parcelable.Creator<OCUpload> CREATOR = new Parcelable.Creator<OCUpload>() {
  280. @Override
  281. public OCUpload createFromParcel(Parcel source) {
  282. return new OCUpload(source);
  283. }
  284. @Override
  285. public OCUpload[] newArray(int size) {
  286. return new OCUpload[size];
  287. }
  288. };
  289. /**
  290. * Reconstruct from parcel
  291. *
  292. * @param source The source parcel
  293. */
  294. protected OCUpload(Parcel source) {
  295. readFromParcel(source);
  296. }
  297. public void readFromParcel(Parcel source) {
  298. mId = source.readLong();
  299. mLocalPath = source.readString();
  300. mRemotePath = source.readString();
  301. mAccountName = source.readString();
  302. mLocalAction = source.readInt();
  303. mForceOverwrite = (source.readInt() == 1);
  304. mIsCreateRemoteFolder = (source.readInt() == 1);
  305. try {
  306. mUploadStatus = UploadStatus.valueOf(source.readString());
  307. } catch (IllegalArgumentException x) {
  308. mUploadStatus = UploadStatus.UPLOAD_IN_PROGRESS;
  309. }
  310. mUploadEndTimeStamp = source.readLong();
  311. try {
  312. mLastResult = UploadResult.valueOf(source.readString());
  313. } catch (IllegalArgumentException x) {
  314. mLastResult = UploadResult.UNKNOWN;
  315. }
  316. mCreatedBy = source.readInt();
  317. }
  318. @Override
  319. public int describeContents() {
  320. return this.hashCode();
  321. }
  322. @Override
  323. public void writeToParcel(Parcel dest, int flags) {
  324. dest.writeLong(mId);
  325. dest.writeString(mLocalPath);
  326. dest.writeString(mRemotePath);
  327. dest.writeString(mAccountName);
  328. dest.writeInt(mLocalAction);
  329. dest.writeInt(mForceOverwrite ? 1 : 0);
  330. dest.writeInt(mIsCreateRemoteFolder ? 1 : 0);
  331. dest.writeString(mUploadStatus.name());
  332. dest.writeLong(mUploadEndTimeStamp);
  333. dest.writeString(((mLastResult == null) ? "" : mLastResult.name()));
  334. dest.writeInt(mCreatedBy);
  335. }
  336. enum CanUploadFileNowStatus {NOW, LATER, FILE_GONE, ERROR}
  337. }