Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2db48e30 authored by James Wei's avatar James Wei Committed by Bruno Martins
Browse files

MTP: Add support of ObjectInfoChanged Event

Under MTP mode, new file created at device side shown size zero at PC side
due to file information is not updated to PC in time with ObjectAdded event

Bug: 77883345
Test: 1. Take screenshot of device under MTP mode with Win10 / Linux
    2. file copy, delete, rename between PC and device
    3. file copy, delete, rename on device with adb shell command

Change-Id: If428064fb0104b53b8afd5b050a4fab4a09312f0
Merged-In: If428064fb0104b53b8afd5b050a4fab4a09312f0
parent 7c65f70e
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -267,6 +267,12 @@ public class MtpDatabase implements AutoCloseable {
                if (MtpDatabase.this.mServer != null)
                    MtpDatabase.this.mServer.sendObjectRemoved(id);
            }

            @Override
            public void sendObjectInfoChanged(int id) {
                if (MtpDatabase.this.mServer != null)
                    MtpDatabase.this.mServer.sendObjectInfoChanged(id);
            }
        }, subDirectories == null ? null : Sets.newHashSet(subDirectories));

        initDeviceProperties(context);
+5 −0
Original line number Diff line number Diff line
@@ -77,6 +77,10 @@ public class MtpServer implements Runnable {
        native_send_object_removed(handle);
    }

    public void sendObjectInfoChanged(int handle) {
        native_send_object_info_changed(handle);
    }

    public void sendDevicePropertyChanged(int property) {
        native_send_device_property_changed(property);
    }
@@ -106,6 +110,7 @@ public class MtpServer implements Runnable {
    private native final void native_cleanup();
    private native final void native_send_object_added(int handle);
    private native final void native_send_object_removed(int handle);
    private native final void native_send_object_info_changed(int handle);
    private native final void native_send_device_property_changed(int property);
    private native final void native_add_storage(MtpStorage storage);
    private native final void native_remove_storage(int storageId);
+30 −1
Original line number Diff line number Diff line
@@ -57,7 +57,8 @@ public class MtpStorageManager {

        MtpObjectObserver(MtpObject object) {
            super(object.getPath().toString(),
                    MOVED_FROM | MOVED_TO | DELETE | CREATE | IN_ONLYDIR);
                    MOVED_FROM | MOVED_TO | DELETE | CREATE | IN_ONLYDIR
                  | CLOSE_WRITE);
            mObject = object;
        }

@@ -87,6 +88,10 @@ public class MtpStorageManager {
                    if (mObject.mObserver != null)
                        mObject.mObserver.stopWatching();
                    mObject.mObserver = null;
                } else if ((event & CLOSE_WRITE) != 0) {
                    if (sDebug)
                        Log.i(TAG, "inotify for " + mObject.getPath() + " CLOSE_WRITE: " + path);
                    handleChangedObject(mObject, path);
                } else {
                    Log.w(TAG, "Got unrecognized event " + path + " " + event);
                }
@@ -304,6 +309,11 @@ public class MtpStorageManager {
         * Called when an object is deleted.
         */
        public abstract void sendObjectRemoved(int id);

        /**
         * Called when an object info is changed.
         */
        public abstract void sendObjectInfoChanged(int id);
    }

    private MtpNotifier mMtpNotifier;
@@ -736,6 +746,25 @@ public class MtpStorageManager {
            Log.i(TAG, state + " transitioned to " + obj.getState() + " in op " + op);
    }

    private synchronized void handleChangedObject(MtpObject parent, String path) {
        MtpOperation op = MtpOperation.NONE;
        MtpObject obj = parent.getChild(path);
        if (obj != null) {
            // Only handle files for size change notification event
            if ((!obj.isDir()) && (obj.getSize() > 0))
            {
                MtpObjectState state = obj.getState();
                op = obj.getOperation();
                MtpStorageManager.this.mMtpNotifier.sendObjectInfoChanged(obj.getId());
                if (sDebug)
                    Log.d(TAG, "sendObjectInfoChanged: id=" + obj.getId() + ",size=" + obj.getSize());
            }
        } else {
            if (sDebug)
                Log.w(TAG, "object " + path + " null");
        }
    }

    /**
     * Block the caller until all events currently in the event queue have been
     * read and processed. Used for testing purposes.
+13 −0
Original line number Diff line number Diff line
@@ -134,6 +134,18 @@ android_mtp_MtpServer_send_object_removed(JNIEnv *env, jobject thiz, jint handle
        ALOGE("server is null in send_object_removed");
}

static void
android_mtp_MtpServer_send_object_info_changed(JNIEnv *env, jobject thiz, jint handle)
{
    Mutex::Autolock autoLock(sMutex);

    MtpServer* server = getMtpServer(env, thiz);
    if (server)
        server->sendObjectInfoChanged(handle);
    else
        ALOGE("server is null in send_object_info_changed");
}

static void
android_mtp_MtpServer_send_device_property_changed(JNIEnv *env, jobject thiz, jint property)
{
@@ -202,6 +214,7 @@ static const JNINativeMethod gMethods[] = {
    {"native_cleanup",              "()V",  (void *)android_mtp_MtpServer_cleanup},
    {"native_send_object_added",    "(I)V", (void *)android_mtp_MtpServer_send_object_added},
    {"native_send_object_removed",  "(I)V", (void *)android_mtp_MtpServer_send_object_removed},
    {"native_send_object_info_changed",  "(I)V", (void *)android_mtp_MtpServer_send_object_info_changed},
    {"native_send_device_property_changed",  "(I)V",
                                    (void *)android_mtp_MtpServer_send_device_property_changed},
    {"native_add_storage",          "(Landroid/mtp/MtpStorage;)V",
+140 −56

File changed.

Preview size limit exceeded, changes collapsed.