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: 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值