OCUpload.java 12 KB

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