Android开发零碎知识点积累(一) -- 访客模式PC端不显示盘符的原因及解决方案

【现象】

这个问题主要是针对MTK平台,整理信息的来源是自己在项目过程中遇到的一个Bug,Device切换到访客模式下,连接电脑USB,打开传输模式,却发现在PC端无法显示内部存储的盘符,这是不合理的。

但其实原生代码是不存在这个问题的,MTK合入了一个patch导致出现这样的问题。

MTK导致问题的修改如下: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());                 // 跟踪volumeMountedLocked()函数
                    } else {
                        Log.e(TAG, "StorageVolume not mounted " + volume.getPath());
                    }
                }
            }
        }
    private void volumeMountedLocked(String path) {
        // For update storage
        /*
         * 问题就在这里!
         * MTK为了修改一个Bug:偶现显示两个内部存储盘符,所以在这边重新获取一遍StroageVolume,但是没有考虑多用户模式
         *
         */
        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;
            }
        }
    }

【解决方案】

    private void volumeMountedLocked(String path) {
        // For update storage, support multi-user mode
        /*
         * 我们发现原生设计对多用户读取盘符是这么操作的:mVolumes = StorageManager.getVolumeList(user.getIdentifier(), 0);
         * 我们直接进行如下修改:添加多用户模式判断逻辑
         *
         */

        StorageVolume[] volumes = mStorageManager.getVolumeList(
                                  new UserHandle(ActivityManager.getCurrentUser()).getIdentifier(), 0);    // 正解!

        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;
            }
        }
    }

猜你喜欢

转载自blog.csdn.net/pepsimaxin/article/details/81505591