+/**
+ * Service keeping a list of {@link FileObserver} instances that watch for local
+ * changes in favorite files (formerly known as kept-in-sync files) and try to
+ * synchronize them with the OC server as soon as possible.
+ *
+ * Tries to be alive as long as possible; that is the reason why stopSelf() is
+ * never called.
+ *
+ * It is expected that the system eventually kills the service when runs low of
+ * memory. To minimize the impact of this, the service always returns
+ * Service.START_STICKY, and the later restart of the service is explicitly
+ * considered in {@link FileObserverService#onStartCommand(Intent, int, int)}.
+ *
+ * @author David A. Velasco
+ */
+public class FileObserverService extends Service {
+
+ public final static String MY_NAME = FileObserverService.class.getCanonicalName();
+ public final static String ACTION_INIT_OBSERVED_LIST = MY_NAME + ".action.INIT_OBSERVED_LIST";
+ public final static String CMD_ADD_OBSERVED_FILE = MY_NAME + ".action.ADD_OBSERVED_FILE";
+ public final static String CMD_DEL_OBSERVED_FILE = MY_NAME + ".action.DEL_OBSERVED_FILE";
+
+ public final static String KEY_CMD_ARG_FILE = "KEY_CMD_ARG_FILE";
+ public final static String KEY_CMD_ARG_ACCOUNT = "KEY_CMD_ARG_ACCOUNT";
+
+ private static String TAG = FileObserverService.class.getSimpleName();
+
+ 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);