diff --git a/packages/MtpDocumentsProvider/src/com/android/mtp/MtpDatabase.java b/packages/MtpDocumentsProvider/src/com/android/mtp/MtpDatabase.java index c456be93d5a39..d41ffc11ba72a 100644 --- a/packages/MtpDocumentsProvider/src/com/android/mtp/MtpDatabase.java +++ b/packages/MtpDocumentsProvider/src/com/android/mtp/MtpDatabase.java @@ -342,6 +342,26 @@ class MtpDatabase { } } + String getDeviceDocumentId(int deviceId) throws FileNotFoundException { + try (final Cursor cursor = mDatabase.query( + TABLE_DOCUMENTS, + strings(Document.COLUMN_DOCUMENT_ID), + COLUMN_DEVICE_ID + " = ? AND " + COLUMN_DOCUMENT_TYPE + " = ? AND " + + COLUMN_ROW_STATE + " != ?", + strings(deviceId, DOCUMENT_TYPE_DEVICE, ROW_STATE_DISCONNECTED), + null, + null, + null, + "1")) { + if (cursor.getCount() > 0) { + cursor.moveToNext(); + return cursor.getString(0); + } else { + throw new FileNotFoundException("The device ID not found: " + deviceId); + } + } + } + /** * Adds new document under the parent. * The method does not affect invalidated and pending documents because we know the document is diff --git a/packages/MtpDocumentsProvider/src/com/android/mtp/MtpDocumentsProvider.java b/packages/MtpDocumentsProvider/src/com/android/mtp/MtpDocumentsProvider.java index 033845401b4b2..8618fda342db5 100644 --- a/packages/MtpDocumentsProvider/src/com/android/mtp/MtpDocumentsProvider.java +++ b/packages/MtpDocumentsProvider/src/com/android/mtp/MtpDocumentsProvider.java @@ -349,6 +349,16 @@ public class MtpDocumentsProvider extends DocumentsProvider { } } + /** + * Obtains document ID for the given device ID. + * @param deviceId + * @return document ID + * @throws FileNotFoundException device ID has not been build. + */ + public String getDeviceDocumentId(int deviceId) throws FileNotFoundException { + return mDatabase.getDeviceDocumentId(deviceId); + } + /** * Resumes root scanner to handle the update of device list. */ diff --git a/packages/MtpDocumentsProvider/src/com/android/mtp/ReceiverActivity.java b/packages/MtpDocumentsProvider/src/com/android/mtp/ReceiverActivity.java index c7206a7929e0a..84745b29e428e 100644 --- a/packages/MtpDocumentsProvider/src/com/android/mtp/ReceiverActivity.java +++ b/packages/MtpDocumentsProvider/src/com/android/mtp/ReceiverActivity.java @@ -17,10 +17,15 @@ package com.android.mtp; import android.app.Activity; -import android.content.ComponentName; import android.content.Intent; +import android.hardware.usb.UsbDevice; import android.hardware.usb.UsbManager; +import android.net.Uri; import android.os.Bundle; +import android.provider.DocumentsContract; +import android.util.Log; + +import java.io.IOException; /** * Invisible activity to receive intents. @@ -33,14 +38,21 @@ public class ReceiverActivity extends Activity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(getIntent().getAction())) { - // TODO: To obtain data URI for the attached device, we need to wait until RootScanner - // found the device and add it to database. Set correct root URI, and use ACTION_BROWSE - // to launch Documents UI. - final Intent intent = new Intent(Intent.ACTION_MAIN); - intent.addCategory(Intent.CATEGORY_LAUNCHER); - intent.setComponent(new ComponentName( - "com.android.documentsui", "com.android.documentsui.LauncherActivity")); - this.startActivity(intent); + final UsbDevice device = getIntent().getParcelableExtra(UsbManager.EXTRA_DEVICE); + try { + final MtpDocumentsProvider provider = MtpDocumentsProvider.getInstance(); + provider.openDevice(device.getDeviceId()); + final String deviceRootId = provider.getDeviceDocumentId(device.getDeviceId()); + final Uri uri = DocumentsContract.buildRootUri( + MtpDocumentsProvider.AUTHORITY, deviceRootId); + + final Intent intent = new Intent(DocumentsContract.ACTION_BROWSE); + intent.setData(uri); + intent.addCategory(Intent.CATEGORY_DEFAULT); + this.startActivity(intent); + } catch (IOException exception) { + Log.e(MtpDocumentsProvider.TAG, "Failed to open device", exception); + } } finish(); }