diff options
5 files changed, 149 insertions, 14 deletions
diff --git a/packages/MtpDocumentsProvider/src/com/android/mtp/Identifier.java b/packages/MtpDocumentsProvider/src/com/android/mtp/Identifier.java new file mode 100644 index 000000000000..aafe8846b8c3 --- /dev/null +++ b/packages/MtpDocumentsProvider/src/com/android/mtp/Identifier.java @@ -0,0 +1,32 @@ +/* + * 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); + } +} diff --git a/packages/MtpDocumentsProvider/src/com/android/mtp/MtpDocument.java b/packages/MtpDocumentsProvider/src/com/android/mtp/MtpDocument.java new file mode 100644 index 000000000000..12eb91e7e865 --- /dev/null +++ b/packages/MtpDocumentsProvider/src/com/android/mtp/MtpDocument.java @@ -0,0 +1,23 @@ +/* + * 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. +} diff --git a/packages/MtpDocumentsProvider/src/com/android/mtp/MtpDocumentsProvider.java b/packages/MtpDocumentsProvider/src/com/android/mtp/MtpDocumentsProvider.java index b0ba200acc30..58203c292196 100644 --- a/packages/MtpDocumentsProvider/src/com/android/mtp/MtpDocumentsProvider.java +++ b/packages/MtpDocumentsProvider/src/com/android/mtp/MtpDocumentsProvider.java @@ -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; - } } diff --git a/packages/MtpDocumentsProvider/src/com/android/mtp/MtpManager.java b/packages/MtpDocumentsProvider/src/com/android/mtp/MtpManager.java index 466a3ac28b54..05e3a72b3f9a 100644 --- a/packages/MtpDocumentsProvider/src/com/android/mtp/MtpManager.java +++ b/packages/MtpDocumentsProvider/src/com/android/mtp/MtpManager.java @@ -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; + } } diff --git a/packages/MtpDocumentsProvider/src/com/android/mtp/MtpRoot.java b/packages/MtpDocumentsProvider/src/com/android/mtp/MtpRoot.java new file mode 100644 index 000000000000..c446ec05c98c --- /dev/null +++ b/packages/MtpDocumentsProvider/src/com/android/mtp/MtpRoot.java @@ -0,0 +1,39 @@ +/* + * 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; + } + } +} |