UploadDbObject.java 6.5 KB

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