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

Commit f83ccbd7 authored by Daichi Hirono's avatar Daichi Hirono
Browse files

Drop device name from storage document name.

When device has multiple storages, storage are shown as directory under
the device root. In this case, we would not like to add device name to
storage.

Note that we still use "device name + storage name" for root name if the
device has a single storage because we skip storage directory in this
case and shows storage's contents directly under the device.

BUG=26625708

Change-Id: Ie13b044e71ae9b5131c4a01ee9d605023d05f168
parent 19aa9324
Loading
Loading
Loading
Loading
+2 −6
Original line number Diff line number Diff line
@@ -20,11 +20,9 @@ import static com.android.mtp.MtpDatabaseConstants.*;

import android.annotation.Nullable;
import android.content.ContentValues;
import android.content.res.Resources;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.mtp.MtpObjectInfo;
import android.provider.DocumentsContract.Document;
import android.provider.DocumentsContract.Root;
@@ -87,12 +85,10 @@ class Mapper {
    /**
     * Puts root information to database.
     * @param parentDocumentId Document ID of device document.
     * @param resources Resources required to localize root name.
     * @param roots List of root information.
     * @return If roots are added or removed from the database.
     */
    synchronized boolean putStorageDocuments(
            String parentDocumentId, Resources resources, MtpRoot[] roots) {
    synchronized boolean putStorageDocuments(String parentDocumentId, MtpRoot[] roots) {
        final SQLiteDatabase database = mDatabase.getSQLiteDatabase();
        database.beginTransaction();
        try {
@@ -117,7 +113,7 @@ class Mapper {
                valuesList[i] = new ContentValues();
                extraValuesList[i] = new ContentValues();
                MtpDatabase.getStorageDocumentValues(
                        valuesList[i], extraValuesList[i], resources, parentDocumentId, roots[i]);
                        valuesList[i], extraValuesList[i], parentDocumentId, roots[i]);
            }
            final boolean changed = putDocuments(
                    valuesList,
+9 −8
Original line number Diff line number Diff line
@@ -108,7 +108,7 @@ class MtpDatabase {
     * @param columnNames Column names defined in {@link android.provider.DocumentsContract.Root}.
     * @return Database cursor.
     */
    Cursor queryRoots(String[] columnNames) {
    Cursor queryRoots(Resources resources, String[] columnNames) {
        final String selection =
                COLUMN_ROW_STATE + " IN (?, ?) AND " + COLUMN_DOCUMENT_TYPE + " = ?";
        final Cursor deviceCursor = mDatabase.query(
@@ -183,10 +183,14 @@ class MtpDatabase {
                    }
                    if (storageCursor.getCount() == 1 && values.containsKey(Root.COLUMN_TITLE)) {
                        storageCursor.moveToFirst();
                        // Add storage name to device name if we have only 1 storage.
                        values.put(
                                Root.COLUMN_TITLE,
                                resources.getString(
                                        R.string.root_name,
                                        values.getAsString(Root.COLUMN_TITLE),
                                        storageCursor.getString(
                                        storageCursor.getColumnIndex(Root.COLUMN_TITLE)));
                                                storageCursor.getColumnIndex(Root.COLUMN_TITLE))));
                    }
                } finally {
                    storageCursor.close();
@@ -533,13 +537,11 @@ class MtpDatabase {
    /**
     * Gets {@link ContentValues} for the given root.
     * @param values {@link ContentValues} that receives values.
     * @param resources Resources used to get localized root name.
     * @param root Root to be converted {@link ContentValues}.
     */
    static void getStorageDocumentValues(
            ContentValues values,
            ContentValues extraValues,
            Resources resources,
            String parentDocumentId,
            MtpRoot root) {
        values.clear();
@@ -550,13 +552,12 @@ class MtpDatabase {
        values.put(COLUMN_ROW_STATE, ROW_STATE_VALID);
        values.put(COLUMN_DOCUMENT_TYPE, DOCUMENT_TYPE_STORAGE);
        values.put(Document.COLUMN_MIME_TYPE, Document.MIME_TYPE_DIR);
        values.put(Document.COLUMN_DISPLAY_NAME, root.getRootName(resources));
        values.put(Document.COLUMN_DISPLAY_NAME, root.mDescription);
        values.putNull(Document.COLUMN_SUMMARY);
        values.putNull(Document.COLUMN_LAST_MODIFIED);
        values.put(Document.COLUMN_ICON, R.drawable.ic_root_mtp);
        values.put(Document.COLUMN_FLAGS, 0);
        values.put(Document.COLUMN_SIZE,
                (int) Math.min(root.mMaxCapacity - root.mFreeSpace, Integer.MAX_VALUE));
        values.put(Document.COLUMN_SIZE, root.mMaxCapacity - root.mFreeSpace);

        extraValues.put(
                Root.COLUMN_FLAGS,
+4 −4
Original line number Diff line number Diff line
@@ -58,7 +58,7 @@ public class MtpDocumentsProvider extends DocumentsProvider {
            Document.COLUMN_FLAGS, Document.COLUMN_SIZE,
    };

    static final boolean DEBUG = true;
    static final boolean DEBUG = false;

    private final Object mDeviceListLock = new Object();

@@ -89,7 +89,7 @@ public class MtpDocumentsProvider extends DocumentsProvider {
        mResolver = getContext().getContentResolver();
        mDeviceToolkits = new HashMap<Integer, DeviceToolkit>();
        mDatabase = new MtpDatabase(getContext(), MtpDatabaseConstants.FLAG_DATABASE_IN_FILE);
        mRootScanner = new RootScanner(mResolver, mResources, mMtpManager, mDatabase);
        mRootScanner = new RootScanner(mResolver, mMtpManager, mDatabase);
        mAppFuse = new AppFuse(TAG, new AppFuseCallback());
        mIntentSender = new ServiceIntentSender(getContext());
        // TODO: Mount AppFuse on demands.
@@ -116,7 +116,7 @@ public class MtpDocumentsProvider extends DocumentsProvider {
        mResolver = resolver;
        mDeviceToolkits = new HashMap<Integer, DeviceToolkit>();
        mDatabase = database;
        mRootScanner = new RootScanner(mResolver, mResources, mMtpManager, mDatabase);
        mRootScanner = new RootScanner(mResolver, mMtpManager, mDatabase);
        mAppFuse = new AppFuse(TAG, new AppFuseCallback());
        mIntentSender = intentSender;
        // TODO: Mount AppFuse on demands.
@@ -135,7 +135,7 @@ public class MtpDocumentsProvider extends DocumentsProvider {
        if (projection == null) {
            projection = MtpDocumentsProvider.DEFAULT_ROOT_PROJECTION;
        }
        final Cursor cursor = mDatabase.queryRoots(projection);
        final Cursor cursor = mDatabase.queryRoots(mResources, projection);
        cursor.setNotificationUri(
                mResolver, DocumentsContract.buildRootsUri(MtpDocumentsProvider.AUTHORITY));
        return cursor;
+1 −3
Original line number Diff line number Diff line
@@ -273,9 +273,7 @@ class MtpManager {
            final MtpRoot[] results = new MtpRoot[storageIds.length];
            for (int i = 0; i < storageIds.length; i++) {
                results[i] = new MtpRoot(
                        device.getDeviceId(),
                        device.getDeviceInfo().getModel(),
                        device.getStorageInfo(storageIds[i]));
                        device.getDeviceId(), device.getStorageInfo(storageIds[i]));
            }
            return results;
        }
+4 −17
Original line number Diff line number Diff line
@@ -16,7 +16,6 @@

package com.android.mtp;

import android.content.res.Resources;
import android.mtp.MtpStorageInfo;

import com.android.internal.annotations.VisibleForTesting;
@@ -24,7 +23,6 @@ import com.android.internal.annotations.VisibleForTesting;
class MtpRoot {
    final int mDeviceId;
    final int mStorageId;
    final String mDeviceModelName;
    final String mDescription;
    final long mFreeSpace;
    final long mMaxCapacity;
@@ -33,24 +31,21 @@ class MtpRoot {
    @VisibleForTesting
    MtpRoot(int deviceId,
            int storageId,
            String deviceName,
            String description,
            long freeSpace,
            long maxCapacity,
            String volumeIdentifier) {
        mDeviceId = deviceId;
        mStorageId = storageId;
        mDeviceModelName = deviceName;
        mDescription = description;
        mFreeSpace = freeSpace;
        mMaxCapacity = maxCapacity;
        mVolumeIdentifier = volumeIdentifier;
    }

    MtpRoot(int deviceId, String deviceModelName, MtpStorageInfo storageInfo) {
    MtpRoot(int deviceId, MtpStorageInfo storageInfo) {
        mDeviceId = deviceId;
        mStorageId = storageInfo.getStorageId();
        mDeviceModelName = deviceModelName;
        mDescription = storageInfo.getDescription();
        mFreeSpace = storageInfo.getFreeSpace();
        mMaxCapacity = storageInfo.getMaxCapacity();
@@ -64,7 +59,6 @@ class MtpRoot {
        final MtpRoot other = (MtpRoot) object;
        return mDeviceId == other.mDeviceId &&
                mStorageId == other.mStorageId &&
                mDeviceModelName.equals(other.mDeviceModelName) &&
                mDescription.equals(other.mDescription) &&
                mFreeSpace == other.mFreeSpace &&
                mMaxCapacity == other.mMaxCapacity &&
@@ -73,19 +67,12 @@ class MtpRoot {

    @Override
    public int hashCode() {
        return mDeviceId ^ mStorageId ^ mDeviceModelName.hashCode() ^ mDescription.hashCode() ^
        return mDeviceId ^ mStorageId ^ mDescription.hashCode() ^
                ((int) mFreeSpace) ^ ((int) mMaxCapacity) ^ mVolumeIdentifier.hashCode();
    }

    @Override
    public String toString() {
        return "MtpRoot{Name: " + mDeviceModelName + " " + mDescription + "}";
    }
    
    String getRootName(Resources resources) {
        return String.format(
                resources.getString(R.string.root_name),
                mDeviceModelName,
                mDescription);
        return "MtpRoot{Name: " + mDescription + "}";
    }
}
Loading