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

Commit 50d17aa8 authored by Daichi Hirono's avatar Daichi Hirono
Browse files

Implement queryRoot method of MtpDocumentsProvider.

BUG=20274999

Change-Id: I27aa8bffe47eab6ea1f4195023dbacb6e2f76548
parent d30c9d13
Loading
Loading
Loading
Loading
+32 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.mtp;

/**
 * Static utilities for ID.
 */
abstract class Identifier {
    // TODO: Make the ID persistent.
    static String createRootId(long deviceId, long storageId) {
        return String.format("%d:%d", deviceId, storageId);
    }

    // TODO: Make the ID persistent.
    static String createDocumentId(String rootId, long objectHandle) {
        return String.format("%s:%d", rootId, objectHandle);
    }
}
+23 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.mtp;

class MtpDocument {
    static final int DUMMY_HANDLE_FOR_ROOT = 0;

    // TODO: Implement model class for MTP document.
}
+36 −9
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.mtp;

import android.content.ContentResolver;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.os.CancellationSignal;
import android.os.ParcelFileDescriptor;
import android.provider.DocumentsContract;
@@ -76,7 +77,33 @@ public class MtpDocumentsProvider extends DocumentsProvider {

    @Override
    public Cursor queryRoots(String[] projection) throws FileNotFoundException {
        throw new FileNotFoundException();
        if (projection == null) {
            projection = MtpDocumentsProvider.DEFAULT_ROOT_PROJECTION;
        }
        final MatrixCursor cursor = new MatrixCursor(projection);
        for (final int deviceId : mMtpManager.getOpenedDeviceIds()) {
            try {
                final MtpRoot[] roots = mMtpManager.getRoots(deviceId);
                // TODO: Add retry logic here.

                for (final MtpRoot root : roots) {
                    final String rootId = Identifier.createRootId(deviceId, root.mStorageId);
                    final MatrixCursor.RowBuilder builder = cursor.newRow();
                    builder.add(Root.COLUMN_ROOT_ID, rootId);
                    builder.add(Root.COLUMN_FLAGS, Root.FLAG_SUPPORTS_IS_CHILD);
                    builder.add(Root.COLUMN_TITLE, root.mDescription);
                    builder.add(
                            Root.COLUMN_DOCUMENT_ID,
                            Identifier.createDocumentId(rootId, MtpDocument.DUMMY_HANDLE_FOR_ROOT));
                    builder.add(Root.COLUMN_AVAILABLE_BYTES , root.mFreeSpace);
                }
            } catch (IOException error) {
                Log.d(TAG, error.getMessage());
            }
        }
        cursor.setNotificationUri(
                mResolver, DocumentsContract.buildRootsUri(MtpDocumentsProvider.AUTHORITY));
        return cursor;
    }

    @Override
@@ -100,12 +127,12 @@ public class MtpDocumentsProvider extends DocumentsProvider {

    void openDevice(int deviceId) throws IOException {
        mMtpManager.openDevice(deviceId);
        notifyRootsUpdate();
        notifyRootsChange();
    }

    void closeDevice(int deviceId) throws IOException {
        mMtpManager.closeDevice(deviceId);
        notifyRootsUpdate();
        notifyRootsChange();
    }

    void closeAllDevices() {
@@ -119,18 +146,18 @@ public class MtpDocumentsProvider extends DocumentsProvider {
            }
        }
        if (closed) {
            notifyRootsUpdate();
            notifyRootsChange();
        }
    }

    private void notifyRootsUpdate() {
    boolean hasOpenedDevices() {
        return mMtpManager.getOpenedDeviceIds().length != 0;
    }

    private void notifyRootsChange() {
        mResolver.notifyChange(
                DocumentsContract.buildRootsUri(MtpDocumentsProvider.AUTHORITY),
                null,
                false);
    }

    boolean hasOpenedDevices() {
        return mMtpManager.getOpenedDeviceIds().length != 0;
    }
}
+19 −5
Original line number Diff line number Diff line
@@ -76,11 +76,7 @@ class MtpManager {
    }

    synchronized void closeDevice(int deviceId) throws IOException {
        final MtpDevice device = mDevices.get(deviceId);
        if (device == null) {
            throw new IOException("USB device " + deviceId + " is not opened.");
        }
        mDevices.get(deviceId).close();
        getDevice(deviceId).close();
        mDevices.remove(deviceId);
    }

@@ -91,4 +87,22 @@ class MtpManager {
        }
        return result;
    }

    synchronized MtpRoot[] getRoots(int deviceId) throws IOException {
        final MtpDevice device = getDevice(deviceId);
        final int[] storageIds = device.getStorageIds();
        final MtpRoot[] results = new MtpRoot[storageIds.length];
        for (int i = 0; i < storageIds.length; i++) {
            results[i] = new MtpRoot(device.getStorageInfo(storageIds[i]));
        }
        return results;
    }

    private MtpDevice getDevice(int deviceId) throws IOException {
        final MtpDevice device = mDevices.get(deviceId);
        if (device == null) {
            throw new IOException("USB device " + deviceId + " is not opened.");
        }
        return device;
    }
}
+39 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.mtp;

import android.mtp.MtpStorageInfo;

class MtpRoot {
    final long mStorageId;
    final String mDescription;
    final long mFreeSpace;
    final long mMaxCapacity;
    final String mVolumeIdentifier;

    MtpRoot(MtpStorageInfo storageInfo) {
        mStorageId = storageInfo.getStorageId();
        mDescription = storageInfo.getDescription();
        mFreeSpace = storageInfo.getFreeSpace();
        mMaxCapacity = storageInfo.getMaxCapacity();
        if (!storageInfo.getVolumeIdentifier().equals("")) {
            mVolumeIdentifier = storageInfo.getVolumeIdentifier();
        } else {
            mVolumeIdentifier = null;
        }
    }
}