Details of Android 8.1 source code -- the reason and solution of no display of drive letter on PC in visitor mode

[phenomenon]

This problem is mainly aimed at MTK platform. The source of sorting information is a Bug encountered in the process of the project. Device switches to visitor mode, connects computer USB, opens transmission mode, but finds that the internal stored drive letter cannot be displayed on the PC end, which is unreasonable.

But in fact, this problem does not exist in the native code. MTK merges a patch and causes this problem.

MTK causes the following modifications: MtpService.java

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        UserHandle user = new UserHandle(ActivityManager.getCurrentUser());
        synchronized (this) {
            if (sServerHolder != null) {
                Log.d(TAG, "MTP server is still running.");
            } else {
                mVolumeMap = new HashMap<>();
                mStorageMap = new HashMap<>();
                mStorageManager.registerListener(mStorageEventListener);
                mVolumes = StorageManager.getVolumeList(user.getIdentifier(), 0);
                for (StorageVolume volume : mVolumes) {
                    if (Environment.MEDIA_MOUNTED.equals(volume.getState())) {
                        volumeMountedLocked(volume.getPath());                 // Trace volumeMountedLocked() function
                    } else {
                        Log.e(TAG, "StorageVolume not mounted " + volume.getPath());
                    }
                }
            }
        }
    private void volumeMountedLocked(String path) {
        // For update storage
        /*
         * That's the problem!
         * MTK In order to modify a Bug, two internal storage drive letters are occasionally displayed, so we need to retrieve the StroageVolume again here, but the multi-user mode is not considered
         *
         */
        StorageVolume[] volumes = mStorageManager.getVolumeList(new UserHandle();
        mVolumes = volumes;
        for (int i = 0; i < mVolumes.length; i++) {
            StorageVolume volume = mVolumes[i];
            if (volume.getPath().equals(path)) {
                mVolumeMap.put(path, volume);
                if (!mMtpDisabled) {
                    // In PTP mode we support only primary storage
                    if (volume.isPrimary() || !mPtpMode) {
                        addStorageLocked(volume);
                    }
                }
                break;
            }
        }
    }

[solution]

    private void volumeMountedLocked(String path) {
        // For update storage, support multi-user mode
        /*
         * We find that the native design reads the drive letter for multiple users in this way: mVolumes = StorageManager.getVolumeList(user.getIdentifier(), 0);
         * We directly modify it as follows: add multi-user mode judgment logic
         *
         */
         
        StorageVolume[] volumes = mStorageManager.getVolumeList(
                                  new UserHandle(ActivityManager.getCurrentUser()).getIdentifier(), 0);    // Positive solution!
                                  
        mVolumes = volumes;
        for (int i = 0; i < mVolumes.length; i++) {
            StorageVolume volume = mVolumes[i];
            if (volume.getPath().equals(path)) {
                mVolumeMap.put(path, volume);
                if (!mMtpDisabled) {
                    // In PTP mode we support only primary storage
                    if (volume.isPrimary() || !mPtpMode) {
                        addStorageLocked(volume);
                    }
                }
                break;
            }
        }
    }

Keywords: Android Java

Added by k4pil on Tue, 03 Dec 2019 14:27:03 +0200