Skip to content

Commit

Permalink
pref: Trigger media store rescan on process finish (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
deltazefiro committed Apr 20, 2024
1 parent 15413fe commit b8c6202
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

public abstract class BaseFileHider {

private final Context context;
protected final Context context;

public BaseFileHider(Context context) {
this.context = context;
Expand Down
15 changes: 10 additions & 5 deletions app/src/main/java/deltazero/amarok/filehider/ChmodFileHider.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package deltazero.amarok.filehider;

import android.content.Context;
import android.media.MediaScannerConnection;
import android.util.Log;

import com.topjohnwu.superuser.Shell;
Expand All @@ -18,17 +19,21 @@ public ChmodFileHider(Context context) {

@Override
protected void process(Set<String> targetDirs, ProcessMethod method) throws InterruptedException {

var processDirs = new HashSet<String>();
for (var d : targetDirs) {
if (d.startsWith("/storage/emulated/0/"))
processDirs.add(d.replace("/storage/emulated/", "/data/media/"));
else
Log.w("ChmodFileHider", String.format("Unsupported path: %s", d));
else Log.w("ChmodFileHider", String.format("Unsupported path: %s", d));
}

for (String d : processDirs)
Shell.cmd(String.format("chmod -R %s %s", method == ProcessMethod.HIDE ? 0 : 2770, d)).submit();
Shell.Job processJob = Shell.getShell().newJob();

processDirs.stream()
.map(d -> String.format("chmod -R %s %s", method == ProcessMethod.HIDE ? 0 : 2770, d))
.forEach(processJob::add);

processJob.submit(result -> MediaScannerConnection.scanFile(context, targetDirs.toArray(new String[0]), null,
(ignore, ignore2) -> Log.d("ChmodFileHider", "MediaStore cache refreshed")));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package deltazero.amarok.filehider;

import android.content.Context;
import android.media.MediaScannerConnection;
import android.util.Log;

import java.nio.file.Paths;
Expand Down Expand Up @@ -48,6 +49,9 @@ protected void process(Set<String> targetDirs, ProcessMethod method) throws Inte
Log.w(TAG, String.format("Error while processing %s: ", dir), e);
}
}

MediaScannerConnection.scanFile(context, targetDirs.toArray(new String[0]), null,
(ignore, ignore2) -> Log.d(TAG, "MediaStore cache refreshed"));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static deltazero.amarok.filehider.BaseFileHider.ProcessMethod.UNHIDE;

import android.content.Context;
import android.media.MediaScannerConnection;
import android.util.Base64;
import android.util.Log;

Expand Down Expand Up @@ -57,6 +58,8 @@ protected void process(Set<String> targetDirs, ProcessMethod method) throws Inte
Log.w(TAG, String.format("Failed to process %s: ", dir), e);
}
}
MediaScannerConnection.scanFile(context, targetDirs.toArray(new String[0]), null,
(ignore, ignore2) -> Log.d(TAG, "MediaStore cache refreshed"));
}

@Override
Expand Down

0 comments on commit b8c6202

Please sign in to comment.