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

Commit 1e374444 authored by Daichi Hirono's avatar Daichi Hirono
Browse files

Open the connected device in Files app when tapping notification.

Previously when tapping the MTP devce notification, MtpDocumentsProvider
opens home direcotry in Files app. The CL adds data URI of connected
device to the intent so that MtpDocumentsProvider can open the connected
device's root in Files app.

BUG=26611224
Change-Id: I97ecd6669852cd2fc875294cbcbf5660fbfaa0da
parent db338c2e
Loading
Loading
Loading
Loading
+20 −0
Original line number Diff line number Diff line
@@ -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
+10 −0
Original line number Diff line number Diff line
@@ -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.
     */
+21 −9
Original line number Diff line number Diff line
@@ -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"));
            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();
    }