MountService初探

MountService作为Vold的客户端,会接收来自vold的消息,并且内部定义保存了各种Volume相关的状态定义:

1、VolumeState

class VolumeState {
        public static final int Init       = -1;
        public static final int NoMedia    = 0;
        public static final int Idle       = 1;
        public static final int Pending    = 2;
        public static final int Checking   = 3;
        public static final int Mounted    = 4;
        public static final int Unmounting = 5;
        public static final int Formatting = 6;
        public static final int Shared     = 7;
        public static final int SharedMnt  = 8;
    }

VoldResponseCode

class VoldResponseCode {
        /*
         * 100 series - Requestion action was initiated; expect another reply
         *              before proceeding with a new command.
         */
        public static final int VolumeListResult               = 110;
        public static final int AsecListResult                 = 111;
        public static final int StorageUsersListResult         = 112;
        public static final int CryptfsGetfieldResult          = 113;

        /*
         * 200 series - Requestion action has been successfully completed.
         */
        public static final int ShareStatusResult              = 210;
        public static final int AsecPathResult                 = 211;
        public static final int ShareEnabledResult             = 212;

        /*
         * 400 series - Command was accepted, but the requested action
         *              did not take place.
         */
        public static final int OpFailedNoMedia                = 401;
        public static final int OpFailedMediaBlank             = 402;
        public static final int OpFailedMediaCorrupt           = 403;
        public static final int OpFailedVolNotMounted          = 404;
        public static final int OpFailedStorageBusy            = 405;
        public static final int OpFailedStorageNotFound        = 406;

        /*
         * 600 series - Unsolicited broadcasts.
         */
        public static final int VolumeStateChange              = 605;
        public static final int VolumeUuidChange               = 613;
        public static final int VolumeUserLabelChange          = 614;
        public static final int VolumeDiskInserted             = 630;
        public static final int VolumeDiskRemoved              = 631;
        public static final int VolumeBadRemoval               = 632;

        /*
         * 700 series - fstrim
         */
        public static final int FstrimCompleted                = 700;
    }

1、MountService构造方法:

1.1、初始化MountService静态对象sSelf以及PackageManagerService,readStoregeListLocked方法会读取系统配置文件,初始化mVolumes对象。

sSelf = this;

        mContext = context;

        synchronized (mVolumesLock) {
            readStorageListLocked();
        }

        // XXX: This will go away soon in favor of IMountServiceObserver
        mPms = (PackageManagerService) ServiceManager.getService("package");

readStorageListLocked方法,读取storage_list.xml文件。

private void readStorageListLocked() {
        mVolumes.clear();
        mVolumeStates.clear();

        Resources resources = mContext.getResources();

        int id = com.android.internal.R.xml.storage_list;
        XmlResourceParser parser = resources.getXml(id);
        AttributeSet attrs = Xml.asAttributeSet(parser);

我这边的手机读取结果为:

04-21 21:50:36.230 D/MountService( 1269): got storage path: /storage/sdcard0 description: 内部存储设备 primary: true removable: false emulated: true mtpReserve: 100 allowMassStorage: false maxFileSize: 0 allowMtp: true

如果是模拟分区,则:

 if (emulated) {
                        // For devices with emulated storage, we create separate
                        // volumes for each known user.
                        mEmulatedTemplate = new StorageVolume(null, descriptionId, true, false,
                                true, mtpReserve, false, maxFileSize, null, allowMtp);

                        final UserManagerService userManager = UserManagerService.getInstance();
                        for (UserInfo user : userManager.getUsers(false)) {
                            createEmulatedVolumeForUserLocked(user.getUserHandle());
                        }

                    } 

即:初始化mEmulatedTemplate。

如果不为 :则构建StorageVolume,并加入到mVolumes中。

else {
                        if (path == null || description == null) {
                            Slog.e(TAG, "Missing storage path or description in readStorageList");
                        } else {
                            final StorageVolume volume = new StorageVolume(new File(path),
                                    descriptionId, primary, removable, emulated, mtpReserve,
                                    allowMassStorage, maxFileSize, null, allowMtp);
                            addVolumeLocked(volume);

                            // Until we hear otherwise, treat as unmounted
                            mVolumeStates.put(volume.getPath(), Environment.MEDIA_UNMOUNTED);
                            volume.setState(Environment.MEDIA_UNMOUNTED);
                        }

1.2、开辟MountServcieHandler子线程

HandlerThread hthread = new HandlerThread(TAG);
        hthread.start();
        mHandler = new MountServiceHandler(hthread.getLooper());

1.3、注册用户相关广播,多用户相关。

final IntentFilter userFilter = new IntentFilter();
        userFilter.addAction(Intent.ACTION_USER_ADDED);
        userFilter.addAction(Intent.ACTION_USER_REMOVED);
        mContext.registerReceiver(mUserReceiver, userFilter, null, mHandler);

1.4、如果主Volume设备允许massStorage,则监听usb状态:

// Watch for USB changes on primary volume
        final StorageVolume primary = getPrimaryPhysicalVolume();
        if ((primary != null && primary.allowMassStorage()) ||
            //ignore primary config, force to register if property is true
            SystemProperties.getBoolean("persist.sys.ums", true)) {
            mContext.registerReceiver(
                    mUsbReceiver, new IntentFilter(UsbManager.ACTION_USB_STATE), null, mHandler);
        }

1.5、构建NativeDaemonConnector,用户和vold进行socket通信。并为其开辟单独线程:

mConnector = new NativeDaemonConnector(this, "vold", MAX_CONTAINERS * 2, VOLD_TAG, 25,
                null);

        Thread thread = new Thread(mConnector, VOLD_TAG);
        thread.start();

NativeDaemonConnector将会和vold进程的CommandListener进行通信,第二个参数为vold,标示和vold进行通信,第一个参数为INativeDaemonConnectorCallbacks,其有三个回调:

void onDaemonConnected();
    boolean onCheckHoldWakeLock(int code);
    boolean onEvent(int code, String raw, String[] cooked);

在和vold通信过程中,会回调执行onEvent方法。

2、在系统启动时,还会执行器systemReady方法:

public void systemReady() {
        mSystemReady = true;
        mHandler.obtainMessage(H_SYSTEM_READY).sendToTarget();
    }
case H_SYSTEM_READY: {
                    try {
                        handleSystemReady();
                    } catch (Exception ex) {
                        Slog.e(TAG, "Boot-time mount exception", ex);
                    }
                    break;
                }

在handleSystemReady中,会遍历当前存储块区,并更新其状态:

// Push mounted state for all emulated storage
        synchronized (mVolumesLock) {
            for (StorageVolume volume : mVolumes) {
                if (volume.isEmulated()) {
                    updatePublicVolumeState(volume, Environment.MEDIA_MOUNTED);
                }
            }
        }

3、和vold进程通信,MountService会执行其onEvent方法:

public boolean onEvent(int code, String raw, String[] cooked) {
    if (code == VoldResponseCode.VolumeStateChange) {
    } else if (code == VoldResponseCode.VolumeUuidChange) {
    } else if (code == VoldResponseCode.VolumeUserLabelChange) {
    } else if ((code == VoldResponseCode.VolumeDiskInserted) ||
                   (code == VoldResponseCode.VolumeDiskRemoved) ||
                   (code == VoldResponseCode.VolumeBadRemoval)) {
    } else if (code == VoldResponseCode.VolumeDiskRemoved) {
    } else if (code == VoldResponseCode.VolumeBadRemoval) {
    } else if (code == VoldResponseCode.FstrimCompleted) {
    }
    。。。。
}

4、附storate_list.xml文件内容:

<StorageList xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- internal emulated storage -->
    <storage android:mountPoint="/storage/sdcard0"
            android:storageDescription="@string/storage_internal"
            android:primary="true"
            android:emulated="true"
            android:removable="false"
            android:mtpReserve="100" />
        <storage android:mountPoint="/storage/sdcard1"
            android:storageDescription="@string/storage_sd_card"
            android:primary="false"
            android:emulated="false"
            android:removable="true"
            android:allowMassStorage="true" />
        <storage android:mountPoint="/storage/uicc0"
            android:storageDescription="@string/storage_uicc"
            android:primary="false"
            android:emulated="false"
            android:removable="true"
            android:allowMassStorage="true" />
        <storage android:mountPoint="/storage/uicc1"
            android:storageDescription="@string/storage_uicc"
            android:primary="false"
            android:emulated="false"
            android:removable="true"
            android:allowMassStorage="false"
            android:allowMtp="false" />
        <storage android:mountPoint="/storage/usbotg"
            android:storageDescription="@string/storage_usb"
            android:primary="false"
            android:emulated="false"
            android:removable="true"
            android:allowMassStorage="false" />
</StorageList>

log描述:

04-21 21:50:36.230 D/MountService( 1269): got storage path: /storage/sdcard0 description: 内部存储设备 primary: true removable: false emulated: true mtpReserve: 100 allowMassStorage: false maxFileSize: 0 allowMtp: true

04-21 21:50:36.232 D/MountService( 1269): addVolumeLocked() StorageVolume:

04-21 21:50:36.232 D/MountService( 1269):     mStorageId=65537 mPath=/storage/emulated/0 mDescriptionId=17040809 

04-21 21:50:36.232 D/MountService( 1269):     mPrimary=true mRemovable=false mEmulated=true mMtpReserveSpace=100 

04-21 21:50:36.232 D/MountService( 1269):     mAllowMassStorage=false mMaxFileSize=0 mOwner=UserHandle{0} mUuid=null 

04-21 21:50:36.232 D/MountService( 1269):     mUserLabel=null mState=null mAllowMtp=true 

04-21 21:50:36.232 D/MountService( 1269): got storage path: /storage/sdcard1 description: SD卡 primary: false removable: true emulated: false mtpReserve: 0 allowMassStorage: true maxFileSize: 0 allowMtp: true

04-21 21:50:36.232 D/MountService( 1269): addVolumeLocked() StorageVolume:

04-21 21:50:36.232 D/MountService( 1269):     mStorageId=0 mPath=/storage/sdcard1 mDescriptionId=17040810 mPrimary=false 

04-21 21:50:36.232 D/MountService( 1269):     mRemovable=true mEmulated=false mMtpReserveSpace=0 mAllowMassStorage=true 

04-21 21:50:36.232 D/MountService( 1269):     mMaxFileSize=0 mOwner=null mUuid=null mUserLabel=null mState=null 

04-21 21:50:36.232 D/MountService( 1269):     mAllowMtp=true 

04-21 21:50:36.232 D/MountService( 1269): got storage path: /storage/uicc0 description: 电话卡存储设备 primary: false removable: true emulated: false mtpReserve: 0 allowMassStorage: true maxFileSize: 0 allowMtp: true

04-21 21:50:36.232 D/MountService( 1269): addVolumeLocked() StorageVolume:

04-21 21:50:36.232 D/MountService( 1269):     mStorageId=0 mPath=/storage/uicc0 mDescriptionId=17040812 mPrimary=false 

04-21 21:50:36.232 D/MountService( 1269):     mRemovable=true mEmulated=false mMtpReserveSpace=0 mAllowMassStorage=true 

04-21 21:50:36.232 D/MountService( 1269):     mMaxFileSize=0 mOwner=null mUuid=null mUserLabel=null mState=null 

04-21 21:50:36.232 D/MountService( 1269):     mAllowMtp=true 

04-21 21:50:36.232 D/MountService( 1269): got storage path: /storage/uicc1 description: 电话卡存储设备 primary: false removable: true emulated: false mtpReserve: 0 allowMassStorage: false maxFileSize: 0 allowMtp: false

04-21 21:50:36.233 D/MountService( 1269): addVolumeLocked() StorageVolume:

04-21 21:50:36.233 D/MountService( 1269):     mStorageId=0 mPath=/storage/uicc1 mDescriptionId=17040812 mPrimary=false 

04-21 21:50:36.233 D/MountService( 1269):     mRemovable=true mEmulated=false mMtpReserveSpace=0 mAllowMassStorage=false 

04-21 21:50:36.233 D/MountService( 1269):     mMaxFileSize=0 mOwner=null mUuid=null mUserLabel=null mState=null 

04-21 21:50:36.233 D/MountService( 1269):     mAllowMtp=false 

04-21 21:50:36.233 D/MountService( 1269): got storage path: /storage/usbotg description: USB存储器 primary: false removable: true emulated: false mtpReserve: 0 allowMassStorage: false maxFileSize: 0 allowMtp: true

04-21 21:50:36.233 D/MountService( 1269): addVolumeLocked() StorageVolume:

04-21 21:50:36.233 D/MountService( 1269):     mStorageId=0 mPath=/storage/usbotg mDescriptionId=17040811 mPrimary=false 

04-21 21:50:36.233 D/MountService( 1269):     mRemovable=true mEmulated=false mMtpReserveSpace=0 mAllowMassStorage=false 

04-21 21:50:36.233 D/MountService( 1269):     mMaxFileSize=0 mOwner=null mUuid=null mUserLabel=null mState=null 

04-21 21:50:36.233 D/MountService( 1269):     mAllowMtp=true 
时间: 2024-11-14 16:32:24

MountService初探的相关文章

进阶之初探nodeJS

一.前言 在"初探nodeJS"随笔中,我们对于node有了一个大致地了解,并在最后也通过一个示例,了解了如何快速地开启一个简单的服务器. 今儿,再次看了该篇随笔,发现该随笔理论知识稍多,适合初级入门node,固萌生一个想法--想在该篇随笔中,通过一步步编写一个稍大一点的node示例,让我们在整体上更加全面地了解node. so,该篇随笔是建立在"初探nodeJS"之上的,固取名为"进阶之初探nodeJS". 好了,侃了这多,那么我们即将实现一个

从273二手车的M站点初探js模块化编程

前言 这几天在看273M站点时被他们的页面交互方式所吸引,他们的首页是采用三次加载+分页的方式.也就说分为大分页和小分页两种交互.大分页就是通过分页按钮来操作,小分页是通过下拉(向下滑动)时异步加载数据. 273这个M站点是产品推荐我看的.第一眼看这个产品时我就再想他们这个三次加载和翻页按钮的方式,那么小分页的pageIndex是怎么计算的.所以就顺便看了下源码. 提到看源码时用到了Chrome浏览器的格式化工具(还是朋友推荐我的,不过这个格式化按钮的确不明显,不会的话自行百度). 三次加载和分

[转载]HDFS初探之旅

转载自 http://www.cnblogs.com/xia520pi/archive/2012/05/28/2520813.html , 感谢虾皮工作室这一系列精彩的文章. Hadoop集群(第8期)_HDFS初探之旅 1.HDFS简介 HDFS(Hadoop Distributed File System)是Hadoop项目的核心子项目,是分布式计算中数据存储管理的基础,是基于流数据模式访问和处理超大文件的需求而开发的,可以运行于廉价的商用服务器上.它所具有的高容错.高可靠性.高可扩展性.高

MongoDB初探系列之二:认识MongoDB提供的一些常用工具

在初探一中,我们已经可以顺利的将MongoDB在我们自己的机器上跑起来了.但是在其bin目录下面还有一些我们不熟知的工具.接下来,将介绍一下各个小工具的用途以及初探一中MongoDB在data文件夹下创建的文件的用途. 1.bin目录下面的各种小工具简介及使用方式 bsondump.exe 用于将导出的BSON文件格式转换为JSON格式mongo.exe mongoDB的客户端 mongod.exe 用于启动mongoDB的Server mongodump.exe 用于从mongodb数据库中导

Asynchronous Pluggable Protocols 初探

Asynchronous Pluggable Protocols,异步可插入协议,允许开发者创建可插协议处理器,MIME过滤器,以及命名空间处理器工作在微软IE4.0浏览器以及更高版本或者URL moniker中.这涉及到Urlmon.dll动态链接库所公开(输出)的可插协议诸多功能,本文不进行深入的原理讲解,只对它其中之一的应用进行解析,那就是如何将一个应用程序注册为URL协议. 应用场景: tencent协议: 当我们打开"tencent://message/?uin=要链接的QQ号 &qu

重新认识HTML,CSS,Javascript 之node-webkit 初探

今天我们来系统的.全面的 了解一下前端的一些技术,将有助于我们写出 更优秀的 产品 出来. 什么是HTML? HTML 是用来描述网页的一种语言. HTML 包含一些根节点,子节点,文本节点,属性节点,组成, 它通过一系列预定义标签来描述网页结构,如: <title>This is title</title> ,这个表明该网页的标题是 This is title. 什么是CSS? CSS 指层叠样式表 (Cascading Style Sheets),它描述浏览器显示如何显示htm

java进阶06 线程初探

线程,程序和进程是经常容易混淆的概念. 程序:就是有序严谨的指令集 进程:是一个程序及其数据在处理机上顺序执行时所发生的活动 线程:程序中不同的执行路径,就是程序中多种处理或者方法. 线程有两种方法实现 一:继承Thread 覆盖run方法 package Thread; public class Thread1 { public static void main(String[] args){ MyThread1 thread1=new MyThread1(); thread1.setName

数据加密解密初探

在一次网络通信或者是进程通信中,如果传输数据采用明文的方式,那么很容易被第三方"窃听"到,安全性难以保障. 而所谓加密是让数据从明文变成密文,传输过程中是密文,传送过去之后对方接收到的也是密文.--可以理解为密文就是乱码,看不出内在的任何意义,通常也都是逐位对应的. 在接收方接收到密文之后只有把它还原为原来的样子才可以理解对方说的具体是什么,此过程就叫做解密. 所谓系统的安全要实现的目标应该包括:机密性-confidentiality,完整性-integrity 和可用性-availa

Key/Value之王Memcached初探:三、Memcached解决Session的分布式存储场景的应用

一.高可用的Session服务器场景简介 1.1 应用服务器的无状态特性 应用层服务器(这里一般指Web服务器)处理网站应用的业务逻辑,应用的一个最显著的特点是:应用的无状态性. PS:提到无状态特性,不得不说下Http协议.我们常常听到说,Http是一个无状态协议,同一个会话的连续两个请求互相不了解,他们由最新实例化的环境进行解析,除了应用本身可能已经存储在全局对象中的所有信息外,该环境不保存与会话有关的任何信息.之所以我们在使用ASP.NET WebForm开发中会感觉不到Http的无状态特