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. 学习PHP函数:preg_match_all

    <?php $str = '10.10.10.10, 10.10.10.11'; preg_match_all('#\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}#s', ...

  2. oc唯一标时一部设备

    ios7之前可以使用mac地址 ios7之后首先创建两个类 #import MyKeyChainManager.h @implementation MyKeyChainManager : NSObje ...

  3. ASP:GB2312格式文本文件转换成UTF-8格式

    '-------------------------------------------------'函数名称:gb2utf_file'作用:利用AdoDb.Stream对象来把GB2312格式文本文 ...

  4. Hibernate主键生成方式之hilo

    当利用Hibernate的getHibernateTemplate().save(obj);插入的对象的主键ID为null的时候自动生成5位数的主键ID进行插入. 此笔记的由来: 老夫在此处上传材料后 ...

  5. Json与Gson讲解

    1 json的含义: JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.JSON采用完全独立于语言的文本格式,这些特性使JSON成为理想的数据交换语言.易于 ...

  6. 【0-1 背包模板】 poj 3624

    先看个未经优化的二维空间dp: #include <iostream> #include <cstdio> #include <cmath> #include &l ...

  7. HDU 1010 Tempter of the Bone DFS(奇偶剪枝优化)

    需要剪枝否则会超时,然后就是基本的深搜了 #include<cstdio> #include<stdio.h> #include<cstdlib> #include ...

  8. Linux学习 -- 文件系统管理

    1 分区和文件系统 分区类型 主分区:<= 4个 扩展分区:只能有一个,也算主分区的一种   不能存储数据和格式化,只能用来包含逻辑分区 逻辑分区:扩展分区中划分的   IDE--最多59个   ...

  9. Codeforces Round #349 (Div. 2) C. Reberland Linguistics (DP)

    C. Reberland Linguistics time limit per test 1 second memory limit per test 256 megabytes input stan ...

  10. 【转】PHP代码审计

    PHP代码审计 目录 1. 概述3 2. 输入验证和输出显示3 2.1 命令注入4 2.2 跨站脚本4 2.3 文件包含5 2.4 代码注入5 2.5 SQL注入6 2.6 XPath注入6 2.7 ...