Request scan media on push

After a file is pushed, send a broadcast to request the media scanner to
scan the "push target" directory.

In practice, this allows some apps to detect the new file immediately.
This commit is contained in:
Romain Vimont
2021-06-13 22:32:36 +02:00
parent cda4aa1623
commit 190a90f45d
10 changed files with 118 additions and 4 deletions

View File

@@ -17,6 +17,7 @@ public final class ControlMessage {
public static final int TYPE_SET_CLIPBOARD = 9;
public static final int TYPE_SET_SCREEN_POWER_MODE = 10;
public static final int TYPE_ROTATE_DEVICE = 11;
public static final int TYPE_SCAN_MEDIA = 12;
private int type;
private String text;
@@ -97,6 +98,13 @@ public final class ControlMessage {
return msg;
}
public static ControlMessage createScanMedia(String path) {
ControlMessage msg = new ControlMessage();
msg.type = TYPE_SCAN_MEDIA;
msg.text = path;
return msg;
}
public static ControlMessage createEmpty(int type) {
ControlMessage msg = new ControlMessage();
msg.type = type;

View File

@@ -76,6 +76,9 @@ public class ControlMessageReader {
case ControlMessage.TYPE_SET_SCREEN_POWER_MODE:
msg = parseSetScreenPowerMode();
break;
case ControlMessage.TYPE_SCAN_MEDIA:
msg = parseScanMedia();
break;
case ControlMessage.TYPE_EXPAND_NOTIFICATION_PANEL:
case ControlMessage.TYPE_EXPAND_SETTINGS_PANEL:
case ControlMessage.TYPE_COLLAPSE_PANELS:
@@ -182,6 +185,14 @@ public class ControlMessageReader {
return ControlMessage.createSetScreenPowerMode(mode);
}
private ControlMessage parseScanMedia() {
String path = parseString();
if (path == null) {
return null;
}
return ControlMessage.createScanMedia(path);
}
private static Position readPosition(ByteBuffer buffer) {
int x = buffer.getInt();
int y = buffer.getInt();

View File

@@ -1,5 +1,7 @@
package com.genymobile.scrcpy;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.SystemClock;
import android.view.InputDevice;
@@ -7,6 +9,7 @@ import android.view.KeyCharacterMap;
import android.view.KeyEvent;
import android.view.MotionEvent;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
@@ -135,6 +138,13 @@ public class Controller {
case ControlMessage.TYPE_ROTATE_DEVICE:
Device.rotateDevice();
break;
case ControlMessage.TYPE_SCAN_MEDIA:
String path = msg.getText();
@SuppressWarnings("deprecation")
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(new File(path)));
Device.sendBroadcast(intent);
break;
default:
// do nothing
}

View File

@@ -314,6 +314,26 @@ public class ControlMessageReaderTest {
Assert.assertEquals(ControlMessage.TYPE_ROTATE_DEVICE, event.getType());
}
@Test
public void testScanMedia() throws IOException {
ControlMessageReader reader = new ControlMessageReader();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
dos.writeByte(ControlMessage.TYPE_SCAN_MEDIA);
byte[] text = "/sdcard/Download/".getBytes(StandardCharsets.UTF_8);
dos.writeInt(text.length);
dos.write(text);
byte[] packet = bos.toByteArray();
reader.readFrom(new ByteArrayInputStream(packet));
ControlMessage event = reader.next();
Assert.assertEquals(ControlMessage.TYPE_SCAN_MEDIA, event.getType());
Assert.assertEquals("/sdcard/Download/", event.getText());
}
@Test
public void testMultiEvents() throws IOException {
ControlMessageReader reader = new ControlMessageReader();