UploadDbObject.java 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. package com.owncloud.android.db;
  2. import java.io.ByteArrayInputStream;
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.ObjectInputStream;
  5. import java.io.ObjectOutputStream;
  6. import java.io.Serializable;
  7. import java.util.Calendar;
  8. import java.util.GregorianCalendar;
  9. import android.accounts.Account;
  10. import android.content.Context;
  11. import android.util.Base64;
  12. import com.owncloud.android.authentication.AccountUtils;
  13. import com.owncloud.android.datamodel.OCFile;
  14. import com.owncloud.android.db.UploadDbHandler.UploadStatus;
  15. import com.owncloud.android.files.services.FileUploadService.LocalBehaviour;
  16. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  17. import com.owncloud.android.lib.common.utils.Log_OC;
  18. /**
  19. * Stores all information in order to start upload operations. PersistentUploadObject can
  20. * be stored persistently by {@link UploadDbHandler}.
  21. *
  22. * @author LukeOwncloud
  23. *
  24. */
  25. public class UploadDbObject implements Serializable {
  26. /** Generated - should be refreshed every time the class changes!! */
  27. ;
  28. private static final long serialVersionUID = -2306246191385279928L;
  29. private static final String TAG = "UploadDbObject";
  30. public UploadDbObject(OCFile ocFile) {
  31. this.ocFile = ocFile;
  32. }
  33. // /**
  34. // * Local path to file which is to be uploaded.
  35. // */
  36. // String localPath;
  37. // /**
  38. // * Remote path where file is to be uploaded to.
  39. // */
  40. // String remotePath;
  41. //
  42. // /**
  43. // * Mime type of upload file.
  44. // */
  45. // String mimeType;
  46. OCFile ocFile;
  47. public OCFile getOCFile() {
  48. return ocFile;
  49. }
  50. /**
  51. * Local action for upload.
  52. */
  53. LocalBehaviour localAction;
  54. /**
  55. * Date and time when this upload was first requested.
  56. */
  57. Calendar uploadTime = new GregorianCalendar();
  58. public Calendar getUploadTime() {
  59. return uploadTime;
  60. }
  61. /**
  62. * @return the uploadStatus
  63. */
  64. public UploadStatus getUploadStatus() {
  65. return uploadStatus;
  66. }
  67. /**
  68. * Sets uploadStatus AND SETS lastResult = null;
  69. * @param uploadStatus the uploadStatus to set
  70. */
  71. public void setUploadStatus(UploadStatus uploadStatus) {
  72. this.uploadStatus = uploadStatus;
  73. setLastResult(null);
  74. }
  75. /**
  76. * @return the lastResult
  77. */
  78. public RemoteOperationResult getLastResult() {
  79. return lastResult;
  80. }
  81. /**
  82. * @param lastResult the lastResult to set
  83. */
  84. public void setLastResult(RemoteOperationResult lastResult) {
  85. this.lastResult = lastResult;
  86. }
  87. /**
  88. * Overwrite destination file?
  89. */
  90. boolean forceOverwrite;
  91. /**
  92. * Create destination folder?
  93. */
  94. boolean isCreateRemoteFolder;
  95. /**
  96. * Upload only via wifi?
  97. */
  98. boolean isUseWifiOnly;
  99. /**
  100. * Upload only if phone being charged?
  101. */
  102. boolean isWhileChargingOnly;
  103. /**
  104. * Earliest time when upload may be started. Negative if not set.
  105. */
  106. long uploadTimestamp;
  107. /**
  108. * Name of Owncloud account to upload file to.
  109. */
  110. String accountName;
  111. /**
  112. * Status of upload (later, in_progress, ...).
  113. */
  114. UploadStatus uploadStatus;
  115. /**
  116. * Result from last upload operation. Can be null.
  117. */
  118. RemoteOperationResult lastResult;
  119. /**
  120. * @return the localPath
  121. */
  122. public String getLocalPath() {
  123. return ocFile.getStoragePath();
  124. }
  125. /**
  126. * @return the remotePath
  127. */
  128. public String getRemotePath() {
  129. return ocFile.getRemotePath();
  130. }
  131. /**
  132. * @return the mimeType
  133. */
  134. public String getMimeType() {
  135. return ocFile.getMimetype();
  136. }
  137. /**
  138. * @return the localAction
  139. */
  140. public LocalBehaviour getLocalAction() {
  141. // return null;
  142. return localAction;
  143. }
  144. /**
  145. * @param localAction the localAction to set
  146. */
  147. public void setLocalAction(LocalBehaviour localAction) {
  148. this.localAction = localAction;
  149. }
  150. /**
  151. * @return the forceOverwrite
  152. */
  153. public boolean isForceOverwrite() {
  154. return forceOverwrite;
  155. }
  156. /**
  157. * @param forceOverwrite the forceOverwrite to set
  158. */
  159. public void setForceOverwrite(boolean forceOverwrite) {
  160. this.forceOverwrite = forceOverwrite;
  161. }
  162. /**
  163. * @return the isCreateRemoteFolder
  164. */
  165. public boolean isCreateRemoteFolder() {
  166. return isCreateRemoteFolder;
  167. }
  168. /**
  169. * @param isCreateRemoteFolder the isCreateRemoteFolder to set
  170. */
  171. public void setCreateRemoteFolder(boolean isCreateRemoteFolder) {
  172. this.isCreateRemoteFolder = isCreateRemoteFolder;
  173. }
  174. /**
  175. * @return the isUseWifiOnly
  176. */
  177. public boolean isUseWifiOnly() {
  178. return isUseWifiOnly;
  179. }
  180. /**
  181. * @param isUseWifiOnly the isUseWifiOnly to set
  182. */
  183. public void setUseWifiOnly(boolean isUseWifiOnly) {
  184. this.isUseWifiOnly = isUseWifiOnly;
  185. }
  186. /**
  187. * @return the accountName
  188. */
  189. public String getAccountName() {
  190. return accountName;
  191. }
  192. /**
  193. * @param accountName the accountName to set
  194. */
  195. public void setAccountName(String accountName) {
  196. this.accountName = accountName;
  197. }
  198. /**
  199. * Returns a base64 encoded serialized string of this object.
  200. */
  201. @Override
  202. public String toString() {
  203. // serialize the object
  204. try {
  205. ByteArrayOutputStream bo = new ByteArrayOutputStream();
  206. ObjectOutputStream so = new ObjectOutputStream(bo);
  207. so.writeObject(this);
  208. so.flush();
  209. String serializedObjectBase64 = Base64.encodeToString(bo.toByteArray(), Base64.DEFAULT);
  210. so.close();
  211. bo.close();
  212. return serializedObjectBase64;
  213. } catch (Exception e) {
  214. Log_OC.e(TAG, "Cannot serialize UploadDbObject with localPath:" + getLocalPath(), e);
  215. }
  216. return null;
  217. }
  218. /**
  219. * Accepts a base64 encoded serialized string of an {@link UploadDbObject}
  220. * and instantiates and returns an according object.
  221. *
  222. * @param serializedObjectBase64
  223. * @return
  224. */
  225. static public UploadDbObject fromString(String serializedObjectBase64) {
  226. // deserialize the object
  227. try {
  228. byte[] b = Base64.decode(serializedObjectBase64, Base64.DEFAULT);
  229. ByteArrayInputStream bi = new ByteArrayInputStream(b);
  230. ObjectInputStream si = new ObjectInputStream(bi);
  231. UploadDbObject obj = (UploadDbObject) si.readObject();
  232. return obj;
  233. } catch (Exception e) {
  234. Log_OC.e(TAG, "Cannot deserialize UploadDbObject " + serializedObjectBase64, e);
  235. }
  236. return null;
  237. }
  238. /**
  239. * Returns owncloud account as {@link Account} object.
  240. */
  241. public Account getAccount(Context context) {
  242. return AccountUtils.getOwnCloudAccountByName(context, getAccountName());
  243. }
  244. public void setWhileChargingOnly(boolean isWhileChargingOnly) {
  245. this.isWhileChargingOnly = isWhileChargingOnly;
  246. }
  247. public boolean isWhileChargingOnly() {
  248. return isWhileChargingOnly;
  249. }
  250. /**
  251. * Earliest time when upload may be started. Negative if not set.
  252. * @return the uploadTimestamp
  253. */
  254. public long getUploadTimestamp() {
  255. return uploadTimestamp;
  256. }
  257. /**
  258. * Earliest time when upload may be started. Set to negative value for immediate upload.
  259. * @param uploadTimestamp the uploadTimestamp to set
  260. */
  261. public void setUploadTimestamp(long uploadTimestamp) {
  262. this.uploadTimestamp = uploadTimestamp;
  263. }
  264. /**
  265. * For debugging purposes only.
  266. */
  267. public String toFormattedString() {
  268. return getLocalPath() + " status:"+getUploadStatus() + " result:" + getLastResult();
  269. }
  270. }