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
MountService初探的更多相关文章
- 初探领域驱动设计(2)Repository在DDD中的应用
概述 上一篇我们算是粗略的介绍了一下DDD,我们提到了实体.值类型和领域服务,也稍微讲到了DDD中的分层结构.但这只能算是一个很简单的介绍,并且我们在上篇的末尾还留下了一些问题,其中大家讨论比较多的, ...
- CSharpGL(8)使用3D纹理渲染体数据 (Volume Rendering) 初探
CSharpGL(8)使用3D纹理渲染体数据 (Volume Rendering) 初探 2016-08-13 由于CSharpGL一直在更新,现在这个教程已经不适用最新的代码了.CSharpGL源码 ...
- 从273二手车的M站点初探js模块化编程
前言 这几天在看273M站点时被他们的页面交互方式所吸引,他们的首页是采用三次加载+分页的方式.也就说分为大分页和小分页两种交互.大分页就是通过分页按钮来操作,小分页是通过下拉(向下滑动)时异步加载数 ...
- JavaScript学习(一) —— 环境搭建与JavaScript初探
1.开发环境搭建 本系列教程的开发工具,我们采用HBuilder. 可以去网上下载最新的版本,然后解压一下就能直接用了.学习JavaScript,环境搭建是非常简单的,或者说,只要你有一个浏览器,一个 ...
- .NET文件并发与RabbitMQ(初探RabbitMQ)
本文版权归博客园和作者吴双本人共同所有.欢迎转载,转载和爬虫请注明原文地址:http://www.cnblogs.com/tdws/p/5860668.html 想必MQ这两个字母对于各位前辈们和老司 ...
- React Native初探
前言 很久之前就想研究React Native了,但是一直没有落地的机会,我一直认为一个技术要有落地的场景才有研究的意义,刚好最近迎来了新的APP,在可控的范围内,我们可以在上面做任何想做的事情. P ...
- 【手把手教你全文检索】Apache Lucene初探
PS: 苦学一周全文检索,由原来的搜索小白,到初次涉猎,感觉每门技术都博大精深,其中精髓亦是不可一日而语.那小博猪就简单介绍一下这一周的学习历程,仅供各位程序猿们参考,这其中不涉及任何私密话题,因此也 ...
- Key/Value之王Memcached初探:三、Memcached解决Session的分布式存储场景的应用
一.高可用的Session服务器场景简介 1.1 应用服务器的无状态特性 应用层服务器(这里一般指Web服务器)处理网站应用的业务逻辑,应用的一个最显著的特点是:应用的无状态性. PS:提到无状态特性 ...
- NoSQL初探之人人都爱Redis:(3)使用Redis作为消息队列服务场景应用案例
一.消息队列场景简介 “消息”是在两台计算机间传送的数据单位.消息可以非常简单,例如只包含文本字符串:也可以更复杂,可能包含嵌入对象.消息被发送到队列中,“消息队列”是在消息的传输过程中保存消息的容器 ...
随机推荐
- centos中apache-tomcat的配置
在centos中配置Apache-toncat需要先安装jdk,前面文章已经写了怎么配置jdk,这里略过. 首先到官网下载好Apache-tomcat安装包,我这里下载的是apache-tomcat- ...
- Codeforces Round #371 (Div. 2) C 大模拟
http://codeforces.com/contest/714/problem/C 题目大意:有t个询问,每个询问有三种操作 ①加入一个数值为a[i]的数字 ②消除一个数值为a[i]的数字 ③给一 ...
- mxml日期显示使用
mxml代码: <?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx= ...
- wifi的UI控制。打不开UI
部分主要是您要清楚WiFi作为什么功能,即WiFi是仅仅作为station功能,还是要开启p2p功能,当WiFi作为station功能时候开启的是wpa_supplicant服务,当WiFi作为p2p ...
- JavaScript 常用功能实现一览(一)
摘自于网络:http://www.cnblogs.com/joinger/articles/1506482.html 适合阅读范围:对JavaScript一无所知-离精通只差一步之遥的人基础知识:HT ...
- 转 oracle 如何停下oracle 服务
设置SET ORACLE_SID=ORCL ORCL--实例名称 --停止oracle实例服务 C:\Users\Administrator>net stop oracleserviceORCl ...
- iOS 枚举写法
1.第一种 typedef enum { kPRStateNormal = 0, kPRStatePulling = 1, kPRStateLoading = 2, kPRStateHitTheEnd ...
- shell 各种循环判断
shell支持的循环有 Shell if else Shell case esac Shell for循环 Shell while循环 Shell until循环
- 为Android增加硬件抽象层(HAL)模块访问Linux内核驱动程序
在Android硬件抽象层(HAL)概要介绍和学习计划一文中,我们简要介绍了在Android系统为为硬件编写驱动程序的方法.简单来说,硬件驱动程序一方面分布在Linux内核中,另一方面分布在用户空间的 ...
- 初识Jmeter(一)
倒霉熊的推荐: 文本学习网址:http://m.open-open.com/m/doc/category/105 视频学习网址: 软件学习网:http://www.ask3.cn/index.html ...