UploadDbObject.java 6.8 KB

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