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

Commit 8719c406 authored by Mike Lockwood's avatar Mike Lockwood
Browse files

DO NOT MERGE: MTP: Disable MTP when the keyguard is locked and secure



BUG:  3402847

Change-Id: Id0e20597423131a13ac7cef13ec5c39b962b3e0b
Signed-off-by: default avatarMike Lockwood <lockwood@android.com>
parent 0600fff9
Loading
Loading
Loading
Loading
+7 −0
Original line number Original line Diff line number Diff line
@@ -54,6 +54,12 @@ public class MtpServer {
        native_set_ptp_mode(usePtp);
        native_set_ptp_mode(usePtp);
    }
    }


    // Used to disable MTP by removing all storage units.
    // This is done to disable access to file transfer when the device is locked.
    public void setLocked(boolean locked) {
        native_set_locked(locked);
    }

    private native final void native_setup(MtpDatabase database, String storagePath,
    private native final void native_setup(MtpDatabase database, String storagePath,
            long reserveSpace);
            long reserveSpace);
    private native final void native_start();
    private native final void native_start();
@@ -61,4 +67,5 @@ public class MtpServer {
    private native final void native_send_object_added(int handle);
    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_removed(int handle);
    private native final void native_set_ptp_mode(boolean usePtp);
    private native final void native_set_ptp_mode(boolean usePtp);
    private native final void native_set_locked(boolean locked);
}
}
+30 −1
Original line number Original line Diff line number Diff line
@@ -60,6 +60,7 @@ private:
    MtpStorage*     mStorage;
    MtpStorage*     mStorage;
    Mutex           mMutex;
    Mutex           mMutex;
    bool            mUsePtp;
    bool            mUsePtp;
    bool            mLocked;
    int             mFd;
    int             mFd;


public:
public:
@@ -67,6 +68,8 @@ public:
        :   mDatabase(database),
        :   mDatabase(database),
            mServer(NULL),
            mServer(NULL),
            mStorage(storage),
            mStorage(storage),
            mUsePtp(false),
            mLocked(false),
            mFd(-1)
            mFd(-1)
    {
    {
    }
    }
@@ -81,6 +84,20 @@ public:
        mMutex.unlock();
        mMutex.unlock();
    }
    }


    void setLocked(bool locked) {
        mMutex.lock();
        if (locked != mLocked) {
            if (mServer) {
                if (locked)
                    mServer->removeStorage(mStorage);
                else
                    mServer->addStorage(mStorage);
            }
            mLocked = locked;
        }
        mMutex.unlock();
    }

    virtual bool threadLoop() {
    virtual bool threadLoop() {
        mMutex.lock();
        mMutex.lock();
        mFd = open("/dev/mtp_usb", O_RDWR);
        mFd = open("/dev/mtp_usb", O_RDWR);
@@ -89,6 +106,7 @@ public:
                    (mUsePtp ? MTP_INTERFACE_MODE_PTP : MTP_INTERFACE_MODE_MTP));
                    (mUsePtp ? MTP_INTERFACE_MODE_PTP : MTP_INTERFACE_MODE_MTP));


            mServer = new MtpServer(mFd, mDatabase, AID_MEDIA_RW, 0664, 0775);
            mServer = new MtpServer(mFd, mDatabase, AID_MEDIA_RW, 0664, 0775);
            if (!mLocked)
                mServer->addStorage(mStorage);
                mServer->addStorage(mStorage);


            mMutex.unlock();
            mMutex.unlock();
@@ -199,6 +217,16 @@ android_mtp_MtpServer_set_ptp_mode(JNIEnv *env, jobject thiz, jboolean usePtp)
#endif
#endif
}
}


static void
android_mtp_MtpServer_set_locked(JNIEnv *env, jobject thiz, jboolean locked)
{
#ifdef HAVE_ANDROID_OS
    MtpThread *thread = sThread.get();
    if (thread)
        thread->setLocked(locked);
#endif
}

// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------


static JNINativeMethod gMethods[] = {
static JNINativeMethod gMethods[] = {
@@ -209,6 +237,7 @@ static JNINativeMethod gMethods[] = {
    {"native_send_object_added",    "(I)V", (void *)android_mtp_MtpServer_send_object_added},
    {"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_removed",  "(I)V", (void *)android_mtp_MtpServer_send_object_removed},
    {"native_set_ptp_mode",         "(Z)V", (void *)android_mtp_MtpServer_set_ptp_mode},
    {"native_set_ptp_mode",         "(Z)V", (void *)android_mtp_MtpServer_set_ptp_mode},
    {"native_set_locked",           "(Z)V", (void *)android_mtp_MtpServer_set_locked},
};
};


static const char* const kClassPathName = "android/mtp/MtpServer";
static const char* const kClassPathName = "android/mtp/MtpServer";