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 

MountService初探的更多相关文章

  1. 初探领域驱动设计(2)Repository在DDD中的应用

    概述 上一篇我们算是粗略的介绍了一下DDD,我们提到了实体.值类型和领域服务,也稍微讲到了DDD中的分层结构.但这只能算是一个很简单的介绍,并且我们在上篇的末尾还留下了一些问题,其中大家讨论比较多的, ...

  2. CSharpGL(8)使用3D纹理渲染体数据 (Volume Rendering) 初探

    CSharpGL(8)使用3D纹理渲染体数据 (Volume Rendering) 初探 2016-08-13 由于CSharpGL一直在更新,现在这个教程已经不适用最新的代码了.CSharpGL源码 ...

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

    前言 这几天在看273M站点时被他们的页面交互方式所吸引,他们的首页是采用三次加载+分页的方式.也就说分为大分页和小分页两种交互.大分页就是通过分页按钮来操作,小分页是通过下拉(向下滑动)时异步加载数 ...

  4. JavaScript学习(一) —— 环境搭建与JavaScript初探

    1.开发环境搭建 本系列教程的开发工具,我们采用HBuilder. 可以去网上下载最新的版本,然后解压一下就能直接用了.学习JavaScript,环境搭建是非常简单的,或者说,只要你有一个浏览器,一个 ...

  5. .NET文件并发与RabbitMQ(初探RabbitMQ)

    本文版权归博客园和作者吴双本人共同所有.欢迎转载,转载和爬虫请注明原文地址:http://www.cnblogs.com/tdws/p/5860668.html 想必MQ这两个字母对于各位前辈们和老司 ...

  6. React Native初探

    前言 很久之前就想研究React Native了,但是一直没有落地的机会,我一直认为一个技术要有落地的场景才有研究的意义,刚好最近迎来了新的APP,在可控的范围内,我们可以在上面做任何想做的事情. P ...

  7. 【手把手教你全文检索】Apache Lucene初探

    PS: 苦学一周全文检索,由原来的搜索小白,到初次涉猎,感觉每门技术都博大精深,其中精髓亦是不可一日而语.那小博猪就简单介绍一下这一周的学习历程,仅供各位程序猿们参考,这其中不涉及任何私密话题,因此也 ...

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

    一.高可用的Session服务器场景简介 1.1 应用服务器的无状态特性 应用层服务器(这里一般指Web服务器)处理网站应用的业务逻辑,应用的一个最显著的特点是:应用的无状态性. PS:提到无状态特性 ...

  9. NoSQL初探之人人都爱Redis:(3)使用Redis作为消息队列服务场景应用案例

    一.消息队列场景简介 “消息”是在两台计算机间传送的数据单位.消息可以非常简单,例如只包含文本字符串:也可以更复杂,可能包含嵌入对象.消息被发送到队列中,“消息队列”是在消息的传输过程中保存消息的容器 ...

随机推荐

  1. %02d

    %d表示打印整型的,%2d表示把整型数据打印最低两位,%02d表示把整型数据打印最低两位,如果不足两位,用0补齐所以打印出来就是02了

  2. 并查集+bfs+暴力滑窗 Codeforces Round #356 (Div. 2) E

    http://codeforces.com/contest/680/problem/E 题目大意:给你一个n*n的图,然后图上的 . (我们下面都叫做‘点’)表示可以走,X表示不能走,你有如下的操作, ...

  3. 编译在android 平台上跑的C应用程序

    Android 用的是 Bionic C, 而不是通常的glibc,因此简单使用交叉工具链并不能够编译出适合运行在android 设备上的 C/C++ 程序. 交叉工具链可以很轻松在 Android ...

  4. spice-vdagent

    The spice-vdagent should be running in the guest. Have you installed the spice guest tools in your w ...

  5. vs2010在进行数据架构比较时报'text lines should not be null'错误

    通过VS2010进行服务器数据库和本地数据库比较架构(都是sql server 2008 R2)时,弹出“text lines should be not null”错误,如下图: 解决方法:在Vis ...

  6. Spring Boot 系列教程17-Cache-缓存

    缓存 缓存就是数据交换的缓冲区(称作Cache),当某一硬件要读取数据时,会首先从缓存中查找需要的数据,如果找到了则直接执行,找不到的话则从内存中找.由于缓存的运行速度比内存快得多,故缓存的作用就是帮 ...

  7. ubuntu14下python环境的配置

    1.安装build依赖包(一些包需要用pip编译) sudo apt-get install python-dev 2.安装pip包管理工具 sudo apt-get install python-p ...

  8. iOS 多语言 浅析

    什么是本地化处理? 本地化处理就是我们的应用程序有可能发布到世界的很多国家去,因为每个国家应用的语言是不一样的,所以我们要把我们的应用程序的语言要进行本地化处理一下. 本地化处理需要处理那些文件? ( ...

  9. sql日期

    当我们处理日期时,最难的任务恐怕是确保所插入的日期的格式,与数据库中日期列的格式相匹配. 只要您的数据包含的只是日期部分,运行查询就不会出问题.但是,如果涉及时间部分,情况就有点复杂了. 在讨论日期查 ...

  10. DHCPv6

    SLAAC(RFC4862)(StatelessAddressAutoconfiguration),无状态自动配置 IT网,http://www.it.net.cn DHCPv6包含以下两种形式: n ...