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

Commit 697e23e8 authored by Mike Lockwood's avatar Mike Lockwood Committed by Android (Google) Code Review
Browse files

Merge "MTP: allow renaming directories"

parents c00972bb 6a6a3aff
Loading
Loading
Loading
Loading
+5 −10
Original line number Diff line number Diff line
@@ -59,10 +59,9 @@ public class MtpDatabase {
    private static final String[] ID_PROJECTION = new String[] {
            Files.FileColumns._ID, // 0
    };
    private static final String[] PATH_FORMAT_PROJECTION = new String[] {
    private static final String[] PATH_PROJECTION = new String[] {
            Files.FileColumns._ID, // 0
            Files.FileColumns.DATA, // 1
            Files.FileColumns.FORMAT, // 2
    };
    private static final String[] PATH_SIZE_PROJECTION = new String[] {
            Files.FileColumns._ID, // 0
@@ -479,13 +478,11 @@ public class MtpDatabase {

        // first compute current path
        String path = null;
        int format = 0;
        String[] whereArgs = new String[] {  Integer.toString(handle) };
        try {
            c = mMediaProvider.query(mObjectsUri, PATH_FORMAT_PROJECTION, ID_WHERE, whereArgs, null);
            c = mMediaProvider.query(mObjectsUri, PATH_PROJECTION, ID_WHERE, whereArgs, null);
            if (c != null && c.moveToNext()) {
                path = c.getString(1);
                format = c.getInt(2);
            }
        } catch (RemoteException e) {
            Log.e(TAG, "RemoteException in getObjectFilePath", e);
@@ -498,10 +495,6 @@ public class MtpDatabase {
        if (path == null) {
            return MtpConstants.RESPONSE_INVALID_OBJECT_HANDLE;
        }
        if (format == MtpConstants.FORMAT_ASSOCIATION) {
            // Only files can be renamed
            return MtpConstants.RESPONSE_ACCESS_DENIED;
        }

        // now rename the file.  make sure this succeeds before updating database
        File oldFile = new File(path);
@@ -522,11 +515,13 @@ public class MtpDatabase {
        values.put(Files.FileColumns.DATA, newPath);
        int updated = 0;
        try {
            // note - we are relying on a special case in MediaProvider.update() to update
            // the paths for all children in the case where this is a directory.
            updated = mMediaProvider.update(mObjectsUri, values, ID_WHERE, whereArgs);
        } catch (RemoteException e) {
            Log.e(TAG, "RemoteException in mMediaProvider.update", e);
        }
        if (updated != 1) {
        if (updated == 0) {
            Log.e(TAG, "Unable to update path for " + path + " to " + newPath);
            // this shouldn't happen, but if it does we need to rename the file to its original name
            newFile.renameTo(oldFile);
+2 −3
Original line number Diff line number Diff line
@@ -817,9 +817,8 @@ MtpProperty* MyMtpDatabase::getObjectPropertyDesc(MtpObjectProperty property,
            result = new MtpProperty(property, MTP_TYPE_STR);
            break;
        case MTP_PROPERTY_OBJECT_FILE_NAME:
            // We allow renaming files but not folders
            result = new MtpProperty(property, MTP_TYPE_STR,
                    format != MTP_FORMAT_ASSOCIATION);
            // We allow renaming files and folders
            result = new MtpProperty(property, MTP_TYPE_STR, true);
            break;
    }