/src/com/vodafone360/people/engine/content/ContentEngine.java

https://github.com/jelford/360-Engine-for-Android · Java · 259 lines · 121 code · 29 blank · 109 comment · 22 complexity · 594003ba84498d180d41712773424d61 MD5 · raw file

  1. /*
  2. * CDDL HEADER START
  3. *
  4. * The contents of this file are subject to the terms of the Common Development
  5. * and Distribution License (the "License").
  6. * You may not use this file except in compliance with the License.
  7. *
  8. * You can obtain a copy of the license at
  9. * src/com/vodafone360/people/VODAFONE.LICENSE.txt or
  10. * http://github.com/360/360-Engine-for-Android
  11. * See the License for the specific language governing permissions and
  12. * limitations under the License.
  13. *
  14. * When distributing Covered Code, include this CDDL HEADER in each file and
  15. * include the License file at src/com/vodafone360/people/VODAFONE.LICENSE.txt.
  16. * If applicable, add the following below this CDDL HEADER, with the fields
  17. * enclosed by brackets "[]" replaced with your own identifying information:
  18. * Portions Copyright [yyyy] [name of copyright owner]
  19. *
  20. * CDDL HEADER END
  21. *
  22. * Copyright 2010 Vodafone Sales & Services Ltd. All rights reserved.
  23. * Use is subject to license terms.
  24. */
  25. package com.vodafone360.people.engine.content;
  26. import java.util.Hashtable;
  27. import java.util.List;
  28. import com.vodafone360.people.database.DatabaseHelper;
  29. import com.vodafone360.people.datatypes.BaseDataType;
  30. import com.vodafone360.people.datatypes.ExternalResponseObject;
  31. import com.vodafone360.people.datatypes.ServerError;
  32. import com.vodafone360.people.datatypes.SystemNotification;
  33. import com.vodafone360.people.engine.BaseEngine;
  34. import com.vodafone360.people.engine.EngineManager.EngineId;
  35. import com.vodafone360.people.service.ServiceUiRequest;
  36. import com.vodafone360.people.service.io.QueueManager;
  37. import com.vodafone360.people.service.io.Request;
  38. import com.vodafone360.people.service.io.ResponseQueue.DecodedResponse;
  39. /**
  40. * Content engine for downloading and uploading all kind of content (pictures,
  41. * videos, files)
  42. */
  43. public class ContentEngine extends BaseEngine {
  44. /**
  45. * Constructor for the ContentEngine.
  46. *
  47. * @param eventCallback IEngineEventCallback for calling the constructor of
  48. * the super class
  49. * @param dbHelper Instance of DatabaseHelper
  50. */
  51. public ContentEngine(final IEngineEventCallback eventCallback, final DatabaseHelper dbHelper) {
  52. super(eventCallback);
  53. this.mDbHelper = dbHelper;
  54. mEngineId = EngineId.CONTENT_ENGINE;
  55. }
  56. /**
  57. * Queue with unprocessed ContentObjects.
  58. */
  59. private FiFoQueue mUnprocessedQueue = new FiFoQueue();
  60. /**
  61. * Queue with ContentObjects for downloads.
  62. */
  63. private FiFoQueue mDownloadQueue = new FiFoQueue();
  64. /**
  65. * Queue with ContentObjects for uploads.
  66. */
  67. private FiFoQueue mUploadQueue = new FiFoQueue();
  68. /**
  69. * Instance of DatabaseHelper.
  70. */
  71. private DatabaseHelper mDbHelper;
  72. /**
  73. * Hashtable to match requests to ContentObjects.
  74. */
  75. private Hashtable<Integer, ContentObject> requestContentObjectMatchTable = new Hashtable<Integer, ContentObject>();
  76. /**
  77. * Getter for the local instance of DatabaseHelper.
  78. *
  79. * @return local instance of DatabaseHelper
  80. */
  81. public final DatabaseHelper getDatabaseHelper() {
  82. return mDbHelper;
  83. }
  84. /**
  85. * Processes one ContentObject.
  86. *
  87. * @param co ContentObject to be processed
  88. */
  89. public final void processContentObject(final ContentObject co) {
  90. mUnprocessedQueue.add(co);
  91. }
  92. /**
  93. * Iterates over the ContentObject list and processes every element.
  94. *
  95. * @param list List with ContentObjects which are to be processed
  96. */
  97. public final void processContentObjects(final List<ContentObject> list) {
  98. for (ContentObject co : list) {
  99. processContentObject(co);
  100. }
  101. }
  102. /**
  103. * Processes the main queue and splits it into the download and upload
  104. * queues.
  105. */
  106. private void processQueue() {
  107. ContentObject co;
  108. // picking unprocessed ContentObjects
  109. while ((co = mUnprocessedQueue.poll()) != null) {
  110. // putting them to downloadqueue ...
  111. if (co.getDirection() == ContentObject.TransferDirection.DOWNLOAD) {
  112. mDownloadQueue.add(co);
  113. } else {
  114. // ... or the uploadqueue
  115. mUploadQueue.add(co);
  116. }
  117. }
  118. }
  119. /**
  120. * Determines the next RunTime of this Engine It first processes the
  121. * in-queue and then look.
  122. *
  123. * @return time in milliseconds from now when the engine should be run
  124. */
  125. @Override
  126. public final long getNextRunTime() {
  127. processQueue();
  128. // if there are CommsResponses outstanding, run now
  129. if (isCommsResponseOutstanding()) {
  130. return 0;
  131. }
  132. return (mDownloadQueue.size() + mUploadQueue.size() > 0) ? 0 : -1;
  133. }
  134. /**
  135. * Empty implementation without function at the moment.
  136. */
  137. @Override
  138. public void onCreate() {
  139. }
  140. /**
  141. * Empty implementation without function at the moment.
  142. */
  143. @Override
  144. public void onDestroy() {
  145. }
  146. /**
  147. * Empty implementation without function at the moment.
  148. */
  149. @Override
  150. protected void onRequestComplete() {
  151. }
  152. /**
  153. * Empty implementation without function at the moment.
  154. */
  155. @Override
  156. protected void onTimeoutEvent() {
  157. }
  158. /**
  159. * Processes the response Finds the matching contentobject for the repsonse
  160. * using the id of the response and sets its status to done. At last the
  161. * TransferComplete method of the ContentObject is called.
  162. *
  163. * @param resp Response object that has been processed
  164. */
  165. @Override
  166. protected final void processCommsResponse(final DecodedResponse resp) {
  167. ContentObject co = requestContentObjectMatchTable.remove(resp.mReqId);
  168. if (co == null) { // check if we have an invalid response
  169. return;
  170. }
  171. List<BaseDataType> mDataTypes = resp.mDataTypes;
  172. // Sometimes it is null or empty
  173. if (mDataTypes == null || mDataTypes.size()==0) {
  174. co.setTransferStatus(ContentObject.TransferStatus.ERROR);
  175. RuntimeException exc = new RuntimeException("Empty response returned");
  176. co.getTransferListener().transferError(co, exc);
  177. return;
  178. }
  179. Object data = mDataTypes.get(0);
  180. if (mDataTypes.get(0).getType() == BaseDataType.SERVER_ERROR_DATA_TYPE
  181. || mDataTypes.get(0).getType() == BaseDataType.SYSTEM_NOTIFICATION_DATA_TYPE) {
  182. co.setTransferStatus(ContentObject.TransferStatus.ERROR);
  183. RuntimeException exc = new RuntimeException(data.toString());
  184. co.getTransferListener().transferError(co, exc);
  185. } else {
  186. co.setTransferStatus(ContentObject.TransferStatus.DONE);
  187. co.setExtResponse((ExternalResponseObject) data);
  188. co.getTransferListener().transferComplete(co);
  189. }
  190. }
  191. /**
  192. * Empty implementation of abstract method from BaseEngine.
  193. */
  194. @Override
  195. protected void processUiRequest(final ServiceUiRequest requestId, final Object data) {
  196. }
  197. /**
  198. * run method of this engine iterates over the downloadqueue, makes requests
  199. * out of ContentObjects and puts them into QueueManager queue.
  200. */
  201. @Override
  202. public final void run() {
  203. if (isCommsResponseOutstanding() && processCommsInQueue()) {
  204. return;
  205. }
  206. ContentObject co;
  207. boolean queueChanged = false;
  208. while ((co = mDownloadQueue.poll()) != null) {
  209. queueChanged = true;
  210. // set the status of this contentobject to transferring
  211. co.setTransferStatus(ContentObject.TransferStatus.TRANSFERRING);
  212. Request request = new Request(co.getUrl().toString(), co.getUrlParams(), engineId());
  213. QueueManager.getInstance().addRequest(request);
  214. // important: later we will match done requests back to the
  215. // contentobject using this map
  216. requestContentObjectMatchTable.put(request.getRequestId(), co);
  217. }
  218. if (queueChanged) {
  219. QueueManager.getInstance().fireQueueStateChanged();
  220. }
  221. }
  222. @Override
  223. public void onReset() {
  224. super.onReset();
  225. requestContentObjectMatchTable.clear();
  226. mUnprocessedQueue.clear();
  227. mDownloadQueue.clear();
  228. mUploadQueue.clear();
  229. }
  230. }