Android implements Mtp access to browse mobile phone storage (2) Prohibit DocumentsUI files from popping up directly

Android implements Mtp access to browse mobile phone storage (1) Access Mtp directory
Android implements Mtp access to browse mobile phone storage (2) Prohibit DocumentsUI files from popping up directly

When the usb is connected, the DocumentUI of the system is opened by default . When you develop a file manager, you need to add some adaptations to respond to the startup of the system. First, prohibit the default direct popup of DocumentUI .

Option One

Shield the entire DocumentUI module, simple and rude. The disadvantage is that the three-party application cannot invoke DocumentUI to search for files.
The Android.mk or Android.bp comments of the DocumentUI module can be directly prohibited from compiling.
Source directory:

/packages/apps/DocumentsUI/

Option II

Remove the related startup Action in the following DocumentsUI source code FilesActivity:

        <activity
            android:name=".files.FilesActivity"
            android:documentLaunchMode="intoExisting"
            android:theme="@style/DocumentsTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.provider.action.BROWSE" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="vnd.android.document/root" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="vnd.android.document/root" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="vnd.android.document/directory" />
            </intent-filter>
        </activity>

third solution

Add the Aciton of android.intent.action.VIEW in the custom file manager to respond to the system's open request.

        <activity
            android:name=".ReceiverActivity"
            android:excludeFromRecents="true"
            android:launchMode="singleTask">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />

                <data android:scheme="content" />
                <data android:mimeType="vnd.android.document/root" />
            </intent-filter>
        </activity>

In the system source code directory:

Android 11 and below: frameworks/base/packages/MtpDocumentsProvider/src/com/android/mtp/ReceiverActivity.java
Android 11 and above (including Android 11):
/packages/services/Mtp/src/com/android/mtp/ReceiverActivity.java

ReceiverActivity is responsible for receiving the system Mtp device access broadcast, thereby launching the file manager through Intent.ACTION_VIEW .

public class ReceiverActivity extends Activity {
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(getIntent().getAction())) {
    
    
            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(Intent.ACTION_VIEW);
                intent.setDataAndType(uri, DocumentsContract.Root.MIME_TYPE_ITEM);
                intent.addCategory(Intent.CATEGORY_DEFAULT);
                this.startActivity(intent);
            } catch (IOException exception) {
    
    
                Log.e(MtpDocumentsProvider.TAG, "Failed to open device", exception);
            }
        }
        finish();
    }
}

Finally, the system pops up the following selection box:
insert image description here

Guess you like

Origin blog.csdn.net/CJohn1994/article/details/127157878