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作为消息队列服务场景应用案例
一.消息队列场景简介 “消息”是在两台计算机间传送的数据单位.消息可以非常简单,例如只包含文本字符串:也可以更复杂,可能包含嵌入对象.消息被发送到队列中,“消息队列”是在消息的传输过程中保存消息的容器 ...
随机推荐
- jq插件开发总结
http://www.cnblogs.com/silverLee/archive/2009/12/22/1629925.html jQuery插件的开发包括两种: 一种是类级别的插件开发,即给jQue ...
- VS2008 安装后没有模板
VS2008 安装过程没有任何报错 启动VS2008,新建项目时就成了这样,没有任何模板: 解决方法: 开始 –> 程序 –> Microsoft Visual Studio 2008– ...
- 转 当当网资深DBA:DB运维四大现代化的实现
位好,今天我的主题是 <DB运维的四个现代化> ,看标题就能明白,是关于DBA自动化运维平台的事情.http://dbaplus.cn/news-21-855-1.html 主要是分享下我 ...
- EL表达式处理字符串 是否 包含 某字符串 截取 拆分...............
EL表达式处理字符串 是否 包含 某字符串 截取 拆分............... JSP页面页头添加<%@ taglib uri="/WEB-INF/taglib/c.tld&qu ...
- 【bfs】 poj 3984 maze 队列存储
#include <iostream> #include <stdio.h> #include <cstring> #define Max 0x7f7f7f7f u ...
- JS-DOM操作应用高级(三)
appendChild 1.先把元素从原有的父级上删除 2.添加到新的父级 <title>无标题文档</title> <script> window.on ...
- RedHat虚拟机:Vmware Tools的安装
如果我们仔细看的话, 就会发现在VMware软件界面的左下角处显示着 “you don't have VMware Tools in ...
- UVALive 7291 Kinfolk(最近公共祖先)
题目中的描述就很最近公共祖先,再说其实这个题并不难,就是麻烦点(代码其实可以化简的),我写的判定比较多. 方法:求出两者的最近公共祖先lca,在求出两者到lca的距离 分析:给出a和b,如果LCA(a ...
- NGUI 添加回调函数
//缓动完成 TweenPosition tweenPos=GetComponent<TweenPosition>(); tweenPos.AddOnFinished(complete); ...
- 动态规划之----我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形。请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法?
利用动态规划,一共有n列,若从左向右放小矩形,有两种放置方式: 第一种:横着放,即占用两列.此时第二行的前两个空格只能横着放,所有,总的放置次数变为1+num(2*(n-2)),其中2*(n-2)代表 ...