Skip to content

Display and control your Android device

Trigger media scan after file push

eret96161b0fc0c0c821
diff --git a/app/src/control_msg.c b/app/src/control_msg.c
index fa8ce480..b75b6e75 100644
--- a/app/src/control_msg.c
+++ b/app/src/control_msg.c
@@ -189,6 +189,11 @@ sc_control_msg_serialize(const struct sc_control_msg *msg, uint8_t *buf) {
sc_write16be(&buf[1], msg->resize_display.width);
sc_write16be(&buf[3], msg->resize_display.height);
return 5;
+ case SC_CONTROL_MSG_TYPE_SCAN_FILE: {
+ size_t len = write_string(&buf[1], msg->scan_file.path,
+ SC_CONTROL_MSG_SCAN_FILE_PATH_MAX_LENGTH);
+ return 1 + len;
+ };
case SC_CONTROL_MSG_TYPE_EXPAND_NOTIFICATION_PANEL:
case SC_CONTROL_MSG_TYPE_EXPAND_SETTINGS_PANEL:
case SC_CONTROL_MSG_TYPE_COLLAPSE_PANELS:
@@ -341,6 +346,9 @@ sc_control_msg_log(const struct sc_control_msg *msg) {
case SC_CONTROL_MSG_TYPE_CAMERA_ZOOM_OUT:
LOG_CMSG("camera zoom out");
break;
+ case SC_CONTROL_MSG_TYPE_SCAN_FILE:
+ LOG_CMSG("scan file \"%s\"", msg->scan_file.path);
+ break;
default:
LOG_CMSG("unknown type: %u", (unsigned) msg->type);
break;
@@ -369,6 +377,9 @@ sc_control_msg_destroy(struct sc_control_msg *msg) {
case SC_CONTROL_MSG_TYPE_START_APP:
free(msg->start_app.name);
break;
+ case SC_CONTROL_MSG_TYPE_SCAN_FILE:
+ free(msg->scan_file.path);
+ break;
default:
// do nothing
break;
diff --git a/app/src/control_msg.h b/app/src/control_msg.h
index 2f69e1f0..1cf1564d 100644
--- a/app/src/control_msg.h
+++ b/app/src/control_msg.h
@@ -18,6 +18,8 @@
// type: 1 byte; sequence: 8 bytes; paste flag: 1 byte; length: 4 bytes
#define SC_CONTROL_MSG_CLIPBOARD_TEXT_MAX_LENGTH (SC_CONTROL_MSG_MAX_SIZE - 14)
+#define SC_CONTROL_MSG_SCAN_FILE_PATH_MAX_LENGTH 256
+
#define SC_POINTER_ID_MOUSE UINT64_C(-1)
#define SC_POINTER_ID_GENERIC_FINGER UINT64_C(-2)
@@ -47,6 +49,7 @@ enum sc_control_msg_type {
SC_CONTROL_MSG_TYPE_CAMERA_ZOOM_IN,
SC_CONTROL_MSG_TYPE_CAMERA_ZOOM_OUT,
SC_CONTROL_MSG_TYPE_RESIZE_DISPLAY,
+ SC_CONTROL_MSG_TYPE_SCAN_FILE,
};
enum sc_copy_key {
@@ -122,6 +125,9 @@ struct sc_control_msg {
uint16_t width;
uint16_t height;
} resize_display;
+ struct {
+ char *path; // owned, to be freed by free()
+ } scan_file;
};
};
diff --git a/app/src/file_pusher.c b/app/src/file_pusher.c
index 681fb5d6..8517ea8c 100644
--- a/app/src/file_pusher.c
+++ b/app/src/file_pusher.c
@@ -5,6 +5,8 @@
#include <string.h>
#include "adb/adb.h"
+#include "control_msg.h"
+#include "controller.h"
#include "util/log.h"
#define DEFAULT_PUSH_TARGET "/sdcard/Download/"
@@ -15,8 +17,9 @@ sc_file_pusher_request_destroy(struct sc_file_pusher_request *req) {
}
bool
-sc_file_pusher_init(struct sc_file_pusher *fp, const char *serial,
- const char *push_target) {
+sc_file_pusher_init(struct sc_file_pusher *fp, struct sc_controller *controller,
+ const char *serial, const char *push_target) {
+ assert(controller);
assert(serial);
sc_vecdeque_init(&fp->queue);
@@ -54,6 +57,7 @@ sc_file_pusher_init(struct sc_file_pusher *fp, const char *serial,
fp->stopped = false;
fp->push_target = push_target ? push_target : DEFAULT_PUSH_TARGET;
+ fp->controller = controller;
return true;
}
@@ -108,6 +112,24 @@ sc_file_pusher_request(struct sc_file_pusher *fp,
return true;
}
+static bool
+request_scan_file(struct sc_file_pusher *fp) {
+ struct sc_control_msg msg;
+ msg.type = SC_CONTROL_MSG_TYPE_SCAN_FILE;
+ msg.scan_file.path = strdup(fp->push_target);
+ if (!msg.scan_file.path) {
+ LOG_OOM();
+ return false;
+ }
+
+ if (!sc_controller_push_msg(fp->controller, &msg)) {
+ LOGW("Could not request 'scan file'");
+ return false;
+ }
+
+ return true;
+}
+
static int
run_file_pusher(void *data) {
struct sc_file_pusher *fp = data;
@@ -147,6 +169,7 @@ run_file_pusher(void *data) {
bool ok = sc_adb_push(intr, serial, req.file, push_target, 0);
if (ok) {
LOGI("%s successfully pushed to %s", req.file, push_target);
+ request_scan_file(fp); // any error already logged
} else {
LOGE("Failed to push %s to %s", req.file, push_target);
}
diff --git a/app/src/file_pusher.h b/app/src/file_pusher.h
index 0ffb3721..e6d33cb1 100644
--- a/app/src/file_pusher.h
+++ b/app/src/file_pusher.h
@@ -9,6 +9,8 @@
#include "util/thread.h"
#include "util/vecdeque.h"
+struct sc_controller;
+
enum sc_file_pusher_action {
SC_FILE_PUSHER_ACTION_INSTALL_APK,
SC_FILE_PUSHER_ACTION_PUSH_FILE,
@@ -22,6 +24,7 @@ struct sc_file_pusher_request {
struct sc_file_pusher_request_queue SC_VECDEQUE(struct sc_file_pusher_request);
struct sc_file_pusher {
+ struct sc_controller *controller; // used to send SCAN_FILE requests
char *serial;
const char *push_target;
sc_thread thread;
@@ -35,8 +38,8 @@ struct sc_file_pusher {
};
bool
-sc_file_pusher_init(struct sc_file_pusher *fp, const char *serial,
- const char *push_target);
+sc_file_pusher_init(struct sc_file_pusher *fp, struct sc_controller *controller,
+ const char *serial, const char *push_target);
void
sc_file_pusher_destroy(struct sc_file_pusher *fp);
diff --git a/app/src/scrcpy.c b/app/src/scrcpy.c
index 89c348cc..7c0ae874 100644
--- a/app/src/scrcpy.c
+++ b/app/src/scrcpy.c
@@ -498,7 +498,7 @@ scrcpy(struct scrcpy_options *options) {
struct sc_file_pusher *fp = NULL;
if (options->window && options->control) {
- if (!sc_file_pusher_init(&s->file_pusher, serial,
+ if (!sc_file_pusher_init(&s->file_pusher, &s->controller, serial,
options->push_target)) {
goto end;
}
diff --git a/app/tests/test_control_msg_serialize.c b/app/tests/test_control_msg_serialize.c
index f674b704..0fb110aa 100644
--- a/app/tests/test_control_msg_serialize.c
+++ b/app/tests/test_control_msg_serialize.c
@@ -516,6 +516,27 @@ static void test_serialize_resize_display(void) {
assert(!memcmp(buf, expected, sizeof(expected)));
}
+static void test_serialize_scan_file(void) {
+ struct sc_control_msg msg = {
+ .type = SC_CONTROL_MSG_TYPE_SCAN_FILE,
+ .scan_file = {
+ .path = "/sdcard/Download",
+ },
+ };
+
+ uint8_t buf[SC_CONTROL_MSG_MAX_SIZE];
+ size_t size = sc_control_msg_serialize(&msg, buf);
+ assert(size == 21);
+
+ const uint8_t expected[] = {
+ SC_CONTROL_MSG_TYPE_SCAN_FILE,
+ 0x00, 0x00, 0x00, 0x10, // path length
+ '/', 's', 'd', 'c', 'a', 'r', 'd',
+ '/', 'D', 'o', 'w', 'n', 'l', 'o', 'a', 'd', // path
+ };
+ assert(!memcmp(buf, expected, sizeof(expected)));
+}
+
int main(int argc, char *argv[]) {
(void) argc;
(void) argv;
@@ -544,5 +565,6 @@ int main(int argc, char *argv[]) {
test_serialize_camera_zoom_in();
test_serialize_camera_zoom_out();
test_serialize_resize_display();
+ test_serialize_scan_file();
return 0;
}
diff --git a/doc/control.md b/doc/control.md
index 86c0efe6..9fc6cd47 100644
--- a/doc/control.md
+++ b/doc/control.md
@@ -138,3 +138,11 @@ The target directory can be changed on start:
```bash
scrcpy --push-target=/sdcard/Movies/
```
+
+After each successful push, _scrcpy_ requests the media scanner to scan the
+"push directory" so that the new files immediately appear in media apps.
+
+Note that some gallery apps only show files from a fixed list of folders
+(typically `DCIM/Camera`) in their main view; the file is then still reachable
+through the system Photo Picker and the folder view, but may not appear on the
+gallery home screen.
diff --git a/server/src/main/java/com/genymobile/scrcpy/control/ControlMessage.java b/server/src/main/java/com/genymobile/scrcpy/control/ControlMessage.java
index d7a5c78e..777873b1 100644
--- a/server/src/main/java/com/genymobile/scrcpy/control/ControlMessage.java
+++ b/server/src/main/java/com/genymobile/scrcpy/control/ControlMessage.java
@@ -29,6 +29,7 @@ public final class ControlMessage {
public static final int TYPE_CAMERA_ZOOM_IN = 19;
public static final int TYPE_CAMERA_ZOOM_OUT = 20;
public static final int TYPE_RESIZE_DISPLAY = 21;
+ public static final int TYPE_SCAN_FILE = 22;
public static final long SEQUENCE_INVALID = 0;
@@ -187,6 +188,13 @@ public final class ControlMessage {
return msg;
}
+ public static ControlMessage createScanFile(String path) {
+ ControlMessage msg = new ControlMessage();
+ msg.type = TYPE_SCAN_FILE;
+ msg.text = path;
+ return msg;
+ }
+
public int getType() {
return type;
}
diff --git a/server/src/main/java/com/genymobile/scrcpy/control/ControlMessageReader.java b/server/src/main/java/com/genymobile/scrcpy/control/ControlMessageReader.java
index a2e3a4a7..b2591d6d 100644
--- a/server/src/main/java/com/genymobile/scrcpy/control/ControlMessageReader.java
+++ b/server/src/main/java/com/genymobile/scrcpy/control/ControlMessageReader.java
@@ -62,6 +62,8 @@ public class ControlMessageReader {
return parseCameraSetTorch();
case ControlMessage.TYPE_RESIZE_DISPLAY:
return parseResizeDisplay();
+ case ControlMessage.TYPE_SCAN_FILE:
+ return parseScanFile();
default:
throw new ControlProtocolException("Unknown event type: " + type);
}
@@ -183,6 +185,11 @@ public class ControlMessageReader {
return ControlMessage.createResizeDisplay(width, height);
}
+ private ControlMessage parseScanFile() throws IOException {
+ String path = parseString();
+ return ControlMessage.createScanFile(path);
+ }
+
private Position parsePosition() throws IOException {
int x = dis.readInt();
int y = dis.readInt();
diff --git a/server/src/main/java/com/genymobile/scrcpy/control/Controller.java b/server/src/main/java/com/genymobile/scrcpy/control/Controller.java
index 2e604b35..55d6e11b 100644
--- a/server/src/main/java/com/genymobile/scrcpy/control/Controller.java
+++ b/server/src/main/java/com/genymobile/scrcpy/control/Controller.java
@@ -23,6 +23,7 @@ import com.genymobile.scrcpy.wrappers.InputManager;
import com.genymobile.scrcpy.wrappers.ServiceManager;
import android.content.Intent;
+import android.net.Uri;
import android.os.Build;
import android.os.SystemClock;
import android.util.Pair;
@@ -31,6 +32,7 @@ import android.view.KeyCharacterMap;
import android.view.KeyEvent;
import android.view.MotionEvent;
+import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.ExecutorService;
@@ -411,6 +413,9 @@ public class Controller implements AsyncProcessor, VirtualDisplayListener {
case ControlMessage.TYPE_RESIZE_DISPLAY:
resizeDisplay(msg.getWidth(), msg.getHeight());
return true;
+ case ControlMessage.TYPE_SCAN_FILE:
+ scanFile(msg.getText());
+ return true;
default:
// fall through
}
@@ -875,4 +880,14 @@ public class Controller implements AsyncProcessor, VirtualDisplayListener {
NewDisplayCapture newDisplayCapture = (NewDisplayCapture) surfaceCapture;
newDisplayCapture.requestResize(width, height);
}
+
+ private void scanFile(String path) {
+ try {
+ @SuppressWarnings("deprecation")
+ Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(path)));
+ Device.sendBroadcast(intent);
+ } catch (Throwable t) {
+ Ln.e("MediaStore scan failed for " + path, t);
+ }
+ }
}
diff --git a/server/src/test/java/com/genymobile/scrcpy/control/ControlMessageReaderTest.java b/server/src/test/java/com/genymobile/scrcpy/control/ControlMessageReaderTest.java
index 57ff063d..b3d18bf5 100644
--- a/server/src/test/java/com/genymobile/scrcpy/control/ControlMessageReaderTest.java
+++ b/server/src/test/java/com/genymobile/scrcpy/control/ControlMessageReaderTest.java
@@ -492,6 +492,27 @@ public class ControlMessageReaderTest {
Assert.assertEquals(-1, bis.read()); // EOS
}
+ @Test
+ public void testParseScanFile() throws IOException {
+ byte[] path = "/sdcard/Download".getBytes(StandardCharsets.UTF_8);
+
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ DataOutputStream dos = new DataOutputStream(bos);
+ dos.writeByte(ControlMessage.TYPE_SCAN_FILE);
+ dos.writeInt(path.length);
+ dos.write(path);
+ byte[] packet = bos.toByteArray();
+
+ ByteArrayInputStream bis = new ByteArrayInputStream(packet);
+ ControlMessageReader reader = new ControlMessageReader(bis);
+
+ ControlMessage event = reader.read();
+ Assert.assertEquals(ControlMessage.TYPE_SCAN_FILE, event.getType());
+ Assert.assertEquals("/sdcard/Download", event.getText());
+
+ Assert.assertEquals(-1, bis.read()); // EOS
+ }
+
@Test
public void testMultiEvents() throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();