Reputation: 209
This question is extension of this question. Posting another question because that is closed.
I am implementing the FileObserver Class as mentioned by @SushiHangover but when the event (OnEvent) is fired then the value of path is null and the value of e is 1073741856.
This is how I am initializing the FileObserver as suggested by @SushiHangover
[Activity(Label = "Main Menu")]
public class MainView : MvxActivity
{
public MusicFolderObserver MusicFolderObserver;
protected override void OnViewModelSet()
{
File musicFolder = new File(Environment.GetExternalStoragePublicDirectory(Environment.DirectoryMusic), "/MyFolder");
MusicFolderObserver = new MusicFolderObserver (musicFolder.AbsolutePath);
MusicFolderObserver.StartWatching();
SetContentView(Resource.Layout.View_Main);
}
}
and following is the File observer Class
public class MusicFolderObserver : FileObserver
{
static FileObserverEvents _Events = (FileObserverEvents.AllEvents);
const string tag = "StackoverFlow";
public MusicFolderObserver (String rootPath) : base(rootPath, _Events)
{
Log.Info(tag, String.Format("Watching : {0}", rootPath));
}
public MusicFolderObserver (String rootPath, FileObserverEvents events) : base(rootPath, events)
{
Log.Info(tag, String.Format("Watching : {0} : {1}", rootPath, events));
}
public override void OnEvent(FileObserverEvents e, String path)
{
Log.Info(tag, String.Format("{0}:{1}", path, e));
}
}
Please let me know how can I make this observer active for the entire duration the app is running. I am trying to make an app which is active 24/7 and triggers if there is any change in the observed Music folder
Upvotes: 0
Views: 496
Reputation: 74209
A FileObserverEvents
of 1073741856
contains a FileObserverEvents.Open
flag:
if (e.HasFlag(FileObserverEvents.Open))
{
Log.Debug(tag, $"File Opened : {path}");
}
When Android (Linux) manipulates a file in a directory you might get a series of events as it alters the contents of the directory, a lot of these actions (FileObserverEvents
) will have no path attached to them and those can be ignored in your case (if you want details of all the event flags when you get a null path, consult a Linux file system reference).
OnEvent
override example:public override void OnEvent(FileObserverEvents e, String path)
{
if (path == null) return;
Log.Debug(tag, $"{path} : {e}");
}
Touch
a new file:adb shell touch /sdcard/Music/MyFolder/BohemianRhapsody100.mp3
Results in:
SO : BohemianRhapsody100.mp3 : Create
SO : BohemianRhapsody100.mp3 : Open
SO : BohemianRhapsody100.mp3 : CloseNowrite
adb shell mv /sdcard/Music/MyFolder/BohemianRhapsody100.mp3 /sdcard/Music/MyFolder/BohemianRhapsody101.mp3
Results in:
SO : BohemianRhapsody100.mp3 : MovedFrom
SO : BohemianRhapsody101.mp3 : MovedTo
adb shell rm /sdcard/Music/MyFolder/BohemianRhapsody101.mp3
Results in:
SO : BohemianRhapsody101.mp3 : Delete
etc....
Upvotes: 0