一、MountService的创建
MountService:Android Binder服务端,运行在system_server进程,用于跟Vold进行消息通信,比如MountService向Vold发送挂载SD卡的命令,或者接收到来自Vold的外设热插拔事件。MountService作为Binder服务端,那么相应的Binder客户端便是StorageManager,通过binder IPC与MountService交互。
MountService是一个系统服务,是在SystemServer中启动的,这里new 了一个MountService,并把service添加到了ServiceManager管理:
./frameworks/base/services/java/com/android/server/SystemServer.java
private void startOtherServices() {
......
try {
/*
* NotificationManagerService is dependant on MountService,
* (for media / usb notifications) so we must start MountService first.
*/
//启动MountService服务
mSystemServiceManager.startService(MOUNT_SERVICE_CLASS);
//等价new IMountService.Stub.Proxy(),即获取MountService的proxy对象
mountService = IMountService.Stub.asInterface(
ServiceManager.getService("mount"));
} catch (Throwable e) {
reportWtf("starting Mount Service", e);
}
......
}
上面mSystemServiceManager.startService(MOUNT_SERVICE_CLASS)主要完成3件事:
1、创建MOUNT_SERVICE_CLASS所指类的Lifecycle对象;
2、将该对象添加SystemServiceManager的 mServices 服务列表;
3、最后调用Lifecycle的onStart()方法;
下面看Lifecycle的onStart方法:
public static class Lifecycle extends SystemService {
private MountService mMountService;
private String oldDefaultPath = "";
public Lifecycle(Context context) {
super(context);
}
@Override
public void onStart() {
Slog.d(TAG, "MountService onStart");
sSelf.isBootingPhase = true;
//创建MountService对象
mMountService = new MountService(getContext());
//通知Binder服务
publishBinderService("mount", mMountService);
//启动mMountService
mMountService.start();
oldDefaultPath = sSelf.getDefaultPath();
Slog.d(TAG, "get Default path onStart default path=" + oldDefaultPath);
}
......
}
上面创建了MountService对象,并通知Binder服务的大管家ServiceManager,MountService的服务名为“mount”,对应服务对象为mMountService。通知之后,其他地方需要MountService的服务时便可以通过服务名来向ServiceManager来查询具体的MountService服务。
二、MountService的启动
上面介绍了SystemServer创建MountService的流程,下面将详细介绍MountService的启动流程,先看MountService的构造函数:
frameworks/base/services/core/java/com/android/server/MountService.java
public MountService(Context context) {
sSelf = this;
mContext = context;
//FgThread线程名为“"android.fg",创建IMountServiceListener回调方法
mCallbacks = new Callbacks(FgThread.get().getLooper());
mLockPatternUtils = new LockPatternUtils(mContext);
//获取PackageManagerService的Client端对象,管理服务
mPms = (PackageManagerService) ServiceManager.getService("package");
//创建“MountService”线程,处理消息
HandlerThread hthread = new HandlerThread(TAG);
hthread.start();
//MountService中的消息处理运行在hthread线程中
mHandler = new MountServiceHandler(hthread.getLooper());
//IoThread线程名为"android.io",创建OBB操作的handler
mObbActionHandler = new ObbActionHandler(IoThread.get().getLooper());
//判断/data/system/last-fstrim文件,不存在则创建,存在则更新最后修改时间
File dataDir = Environment.getDataDirectory();
File systemDir = new File(dataDir, "system");
mLastMaintenanceFile = new File(systemDir, LAST_FSTRIM_FILE);
if (!mLastMaintenanceFile.exists()) {
// Not setting mLastMaintenance here means that we will force an
// fstrim during reboot following the OTA that installs this code.
try {
(new FileOutputStream(mLastMaintenanceFile)).close();
} catch (IOException e) {
Slog.e(TAG, "Unable to create fstrim record " + mLastMaintenanceFile.getPath());
}
} else {
mLastMaintenance = mLastMaintenanceFile.lastModified();
}
mSettingsFile = new AtomicFile(
new File(Environment.getDataSystemDirectory(), "storage.xml"));
synchronized (mLock) {
readSettingsLocked();
}
//将MountServiceInternalImpl登记到sLocalServiceObjects
LocalServices.addService(MountServiceInternal.class, mMountServiceInternal);
/*
* Create the connection to vold with a maximum queue of twice the
* amount of containers we'd ever expect to have. This keeps an
* "asec list" from blocking a thread repeatedly.
*/
//创建用于VoldConnector的NativeDaemonConnector对象
mConnector = new NativeDaemonConnector(this, "vold", MAX_CONTAINERS * 2, VOLD_TAG, 25, null);
mConnector.setDebug(true);
//创建线程名为"VoldConnector"的线程,用于跟vold通信
mConnector.setWarnIfHeld(mLock);
mConnectorThread = new Thread(mConnector, VOLD_TAG);
//创建用于CryptdConnector的NativeDaemonConnector对象
// Reuse parameters from first connector since they are tested and safe
mCryptConnector = new NativeDaemonConnector(this, "cryptd",
MAX_CONTAINERS * 2, CRYPTD_TAG, 25, null);
mCryptConnector.setDebug(true);
//创建线程名为"CryptdConnector"的线程,用于加密
mCryptConnectorThread = new Thread(mCryptConnector, CRYPTD_TAG);
//注册监听用户添加、删除的广播
final IntentFilter userFilter = new IntentFilter();
userFilter.addAction(Intent.ACTION_USER_ADDED);
userFilter.addAction(Intent.ACTION_USER_REMOVED);
userFilter.addAction(Intent.ACTION_USER_SWITCHED);
mContext.registerReceiver(mUserReceiver, userFilter, null, mHandler);
synchronized (mLock) {
addInternalVolumeLocked();
}