+
+ private static Map<String, OwnCloudFileObserver> mObserversMap;
+ private static Map<String, OwnCloudFolderObserver> mObserversFolderMap;
+ private static DownloadCompletedReceiver mDownloadReceiver;
+
+ /**
+ * Factory method to create intents that allow to start an
+ * ACTION_INIT_OBSERVED_LIST command.
+ *
+ * @param context Android context of the caller component.
+ * @return Intent that starts a command ACTION_INIT_OBSERVED_LIST when
+ * {@link Context#startService(Intent)} is called.
+ */
+ public static Intent makeInitIntent(Context context) {
+ Intent i = new Intent(context, FileObserverService.class);
+ i.setAction(ACTION_INIT_OBSERVED_LIST);
+ return i;
+ }
+
+ /**
+ * Factory method to create intents that allow to start or stop the
+ * observance of a file.
+ *
+ * @param context Android context of the caller component.
+ * @param file OCFile to start or stop to watch.
+ * @param account OC account containing file.
+ * @param watchIt 'True' creates an intent to watch, 'false' an intent to
+ * stop watching.
+ * @return Intent to start or stop the observance of a file through a call
+ * to {@link Context#startService(Intent)}.
+ */
+ public static Intent makeObservedFileIntent(Context context, OCFile file, Account account, boolean watchIt) {
+ Intent intent = new Intent(context, FileObserverService.class);
+ intent.setAction(watchIt ? FileObserverService.CMD_ADD_OBSERVED_FILE
+ : FileObserverService.CMD_DEL_OBSERVED_FILE);
+ intent.putExtra(FileObserverService.KEY_CMD_ARG_FILE, file);
+ intent.putExtra(FileObserverService.KEY_CMD_ARG_ACCOUNT, account);
+ return intent;
+ }
+
+ @Override
+ public void onCreate() {
+ Log_OC.d(TAG, "onCreate");
+ super.onCreate();
+
+ mDownloadReceiver = new DownloadCompletedReceiver();
+ IntentFilter filter = new IntentFilter();
+ filter.addAction(FileDownloader.getDownloadAddedMessage());
+ filter.addAction(FileDownloader.getDownloadFinishMessage());
+ registerReceiver(mDownloadReceiver, filter);
+
+ mObserversMap = new HashMap<String, OwnCloudFileObserver>();
+ mObserversFolderMap = new HashMap<String, OwnCloudFolderObserver>();
+ }
+
+ @Override
+ public void onLowMemory() {
+ Log_OC.d(TAG, "ON LOW MEMORY");
+
+ }
+
+ @Override
+ public void onDestroy() {
+ Log_OC.d(TAG, "onDestroy - FINISHING OBSERVATION");
+
+ unregisterReceiver(mDownloadReceiver);
+
+ Iterator<OwnCloudFileObserver> it = mObserversMap.values().iterator();
+ while (it.hasNext()) {
+ it.next().stopWatching();
+ }
+ mObserversMap.clear();
+ mObserversMap = null;
+
+ Iterator<OwnCloudFolderObserver> itOCFolder = mObserversFolderMap.values().iterator();
+ while (itOCFolder.hasNext()) {
+ itOCFolder.next().stopWatching();