ScreamingHawk / android-slideshow

A simple slideshow application for your mobile.
MIT License
43 stars 45 forks source link

Watch directory for changes #133

Closed mettyw closed 5 years ago

mettyw commented 5 years ago

It would be great if the selected directory would be monitored for changes and new files would be discovered automatically. Especially for STOP_ON_COMPLETE=false, this would be extremely useful.

PS: I would volunteer to implement this, if this is okay with you? I have in mind a refactoring of the FileList related stuff from ImageActivity into an own class and then the introduction of a FileObeserver.

ScreamingHawk commented 5 years ago

Please do @mettyw !!

Couple things that jump to mind:

mettyw commented 5 years ago

Do you expect a performance impact?

FileObserver is coming directly from the Android SDK, according to documentation, it uses the underlying Linux kernels inotify which I remember to be performing pretty well. It is not an active watch like a while and a sleep. So I would not expect any noticeable impact.

How will this work when the "Random" option is selected?

My current proposal: on change, it would randomize the new file list again and then search for the current image within the list.

But when stop on complete is enabled, it then might skip or re-display images. Any suggestions?

Any new permissions? (I wouldn't think so)

No.

ScreamingHawk commented 5 years ago

All those sound fine. Let's do it!

In the event it does cause problems would you mind putting it behind a preference flag, disabled by default?

Vaelatern commented 5 years ago

This app is the closest to what I need except for this feature.

So I'm testing the open source dream. Here is my current patch, I'm building it now and will test it on my devices. I assume some little cleanup would be in order, but I'm not donating my time here, just whatever patch I end up using to get this feature. I will PR later when testing is done.

--- a/app/src/main/java/link/standen/michael/slideshow/ImageActivity.java
+++ b/app/src/main/java/link/standen/michael/slideshow/ImageActivity.java
@@ -58,6 +58,7 @@ public class ImageActivity extends BaseActivity implements ImageStrategy.ImageSt
        private static boolean STOP_ON_COMPLETE;
        private static boolean REVERSE_ORDER;
        private static boolean RANDOM_ORDER;
+       private static boolean REFRESH_FOLDER;
        private static int SLIDESHOW_DELAY;
        private static boolean IMAGE_DETAILS;
        private static boolean IMAGE_DETAILS_DURING;
@@ -435,6 +436,8 @@ public class ImageActivity extends BaseActivity implements ImageStrategy.ImageSt
                Log.d(TAG, String.format("REVERSE_ORDER: %b", REVERSE_ORDER));
                RANDOM_ORDER = preferences.getBoolean("random_order", false);
                Log.d(TAG, String.format("RANDOM_ORDER: %b", RANDOM_ORDER));
+               REFRESH_FOLDER = preferences.getBoolean("refresh_folder", false);
+               Log.d(TAG, String.format("REFRESH_FOLDER: %b", REFRESH_FOLDER));
                IMAGE_DETAILS = preferences.getBoolean("image_details", false);
                Log.d(TAG, String.format("IMAGE_DETAILS: %b", IMAGE_DETAILS));
                IMAGE_DETAILS_DURING = preferences.getBoolean("image_details_during", false);
@@ -478,6 +481,13 @@ public class ImageActivity extends BaseActivity implements ImageStrategy.ImageSt

                int current = imagePosition;
                int newPosition = imagePosition;
+               if (REFRESH_FOLDER && newPosition == 0) { // Time to reload, easy base case
+                       fileList = new FileItemHelper(this).getFileList(currentPath, false, getIntent().getStringExtra("imagePath") == null);
+                       if (RANDOM_ORDER){
+                               Collections.shuffle(fileList);
+                       }
+               }
+
                do {
                        newPosition += forwards ? 1 : -1;
                        if (newPosition < 0){
diff --git a/app/src/main/java/link/standen/michael/slideshow/SettingsActivity.java b/app/src/main/java/link/standen/michael/slideshow/SettingsActivity.java
index 6eb1ce6..240f218 100644
--- a/app/src/main/java/link/standen/michael/slideshow/SettingsActivity.java
+++ b/app/src/main/java/link/standen/michael/slideshow/SettingsActivity.java
@@ -108,6 +108,8 @@ public class SettingsActivity extends AppCompatPreferenceActivity {
                        addPreferencesFromResource(R.xml.preferences);
                        setHasOptionsMenu(true);

+                       final SwitchPreference reloadFolderPref = (SwitchPreference)findPreference("reload_folder");
+
                        final SwitchPreference reverseOrderPref = (SwitchPreference)findPreference("reverse_order");
                        final SwitchPreference randomOrderPref = (SwitchPreference)findPreference("random_order");
                        // Enabling reverse disables random
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index 7baf77a..963e70f 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -169,6 +169,10 @@
                order.
        </string>

+       <string name="pref_title_refresh_folder">Refresh Folder</string>
+       <string name="pref_description_refresh_folder">Refresh the folder every time through the show.
+       </string>
+
        <string name="pref_title_random_order">Random order</string>
        <string name="pref_description_random_order">Display the images in the slideshow in a random
                order.
diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml
index 1720a40..911fbe9 100644
--- a/app/src/main/res/xml/preferences.xml
+++ b/app/src/main/res/xml/preferences.xml
@@ -110,6 +110,12 @@
                android:summary="@string/pref_description_random_order"
                android:title="@string/pref_title_random_order"/>

+       <SwitchPreference
+               android:defaultValue="false"
+               android:key="refresh_folder"
+               android:summary="@string/pref_description_refresh_folder"
+               android:title="@string/pref_title_refresh_folder"/>
+
        <SwitchPreference
                android:defaultValue="true"
                android:key="delete_warning"