图中绿色箭头表示的就是SD卡挂载消息从底向上传递的一个流程。本文主要是分析红色箭头的传递了,因为现在消息要在上层反应出来,这里是从VolumeManager开始分析,我们把从Mount SD/USB到VolumeManager之间的流程总体当作Vold来讲,也就是Vold向上层反馈SD卡挂载的消息。
上文我们分析到,SD卡被doMount方法执行挂载了,该消息由setState方法将消息传递到上层,setState是通过发送一个广播,这里所说的广播不是Android中的BroadCast,这里实际山是Socket,上层负责监听这个Socket,并解析其中的内容。我们需要从MountService.java开始查找。这里我要解释以下为什么要从这里开始找,不是说一开始我就知道这个类里面有我们需要的东西,这是在查找SD卡挂载过程的时候,通过不同的线索联系起来的。那我们先来看看MountService吧。
MountService
位于AndroidSourcecode2.3/frameworks/base/services/java/com/android/server/MountService.java
MountService是一个服务类,通过ServiceManager注册为系统服务,对外部存储设备提供管理和查询等服务,在外部存储设备状态发生改变的时候发出相应的通知给注册了该服务的应用程序。MountService相当于一个中间桥梁,负责接收Vold的消息并传递给上层应用。这里就不详细阐述MountService是如何启动的了,对于Android服务这一块,我将另写后续的文章分析。
MountService在SystemServer.java(AndroidSourcecode2.3/frameworks/base/services/java/com/android/server/SystemServer.java)中启动,在启动的时候调用MountService的构造函数:
- ServiceManager.addService("mount", new MountService(context));
- public MountService(Context context) {
- mContext = context;
- // XXX: This will go away soon in favor of IMountServiceObserver
- mPms = (PackageManagerService) ServiceManager.getService("package");
- mContext.registerReceiver(mBroadcastReceiver,
- new IntentFilter(Intent.ACTION_BOOT_COMPLETED), null, null);
- mHandlerThread = new HandlerThread("MountService");
- mHandlerThread.start();
- mHandler = new MountServiceHandler(mHandlerThread.getLooper());
- // Add OBB Action Handler to MountService thread.
- mObbActionHandler = new ObbActionHandler(mHandlerThread.getLooper());
- /*
- * Vold does not run in the simulator, so pretend the connector thread
- * ran and did its thing.
- */
- if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
- mReady = true;
- mUmsEnabling = true;
- return;
- }
- /*
- * 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.
- */
- <span style="color:#000000;">mConnector = new NativeDaemonConnector(this, "vold",
- PackageManagerService.MAX_CONTAINERS * 2, VOLD_TAG);//通过调用带参数的构造函数生成了一个Runnable对象
- mReady = false;
- </span><span style="color:#ff0000;"><span style="color:#000000;">Thread thread = new Thread(mConnector, VOLD_TAG);</span><span style="color:#000000;">//这里开启了一个新线程,传递了一个Runnable对象
- thread.start();</span>
- </span> }
1.监听者
listenToSocket();
//代码路径:AndroidSourcecode2.3/frameworks/base/services/java/com/android/server/NativeDaemonConnector.java/run()方法中
//该方法负责监听来自Vold的Socket消息,这些消息包括SD卡的插入,SD的检测,SD卡的挂载等等。
- public void run() {
- while (true) {
- try {
- <span style="color:#ff0000;"><span style="color:#000000;">listenToSocket();</span>
- </span> } catch (Exception e) {
- Slog.e(TAG, "Error in NativeDaemonConnector", e);
- SystemClock.sleep(5000);
- }
- }
- }
- private void listenToSocket() throws IOException {
- LocalSocket socket = null;<span style="color:#000000;">//这些Socket就是用来与底层通信的,接收底层传递上来的关于SD卡挂载的信息</span>
- try {
- socket = new LocalSocket();
- LocalSocketAddress address = new LocalSocketAddress(mSocket,
- LocalSocketAddress.Namespace.RESERVED);
- socket.connect(address);
- <span style="color:#000000;">mCallbacks.onDaemonConnected();</span>//主要处理队列中的event方法,后文将接着这里分析。
- InputStream inputStream = socket.getInputStream();
- mOutputStream = socket.getOutputStream();<span style="color:#000000;">//同时也可以向底层发出控制命令</span>
- byte[] buffer = new byte[BUFFER_SIZE];
- int start = 0;
- while (true) {
- int count = inputStream.read(buffer, start, BUFFER_SIZE - start);<span style="color:#000000;">//通过inputStream.read来读取Socket中的信息</span>
- if (count < 0) break;
- // Add our starting point to the count and reset the start.
- count += start;
- start = 0;
- for (int i = 0; i < count; i++) {//对信息进行处理
- if (buffer[i] == 0) {
- String event = new String(buffer, start, i - start);
- if (LOCAL_LOGD) Slog.d(TAG, String.format("RCV <- {%s}", event));
- String[] tokens = event.split(" ");
- try {
- int code = Integer.parseInt(tokens[0]);
- if (code >= ResponseCode.UnsolicitedInformational) {
- try {
- if (!mCallbacks.onEvent(code, event, tokens)) {
- Slog.w(TAG, String.format(
- "Unhandled event (%s)", event));
- }
- } catch (Exception ex) {
- Slog.e(TAG, String.format(
- "Error handling '%s'", event), ex);
- }
- } else {
- try {//将系统能够识别的event存入Block队列
- mResponseQueue.put(event);
- } catch (InterruptedException ex) {
- Slog.e(TAG, "Failed to put response onto queue", ex);
- }
- }
- } catch (NumberFormatException nfe) {
- Slog.w(TAG, String.format("Bad msg (%s)", event));
- }
- start = i + 1;
- }
- }
- // We should end at the amount we read. If not, compact then
- // buffer and read again.
- if (start != count) {
- final int remaining = BUFFER_SIZE - start;
- System.arraycopy(buffer, start, buffer, 0, remaining);
- start = remaining;
- } else {
- start = 0;
- }
- }
- } catch (IOException ex) {
- Slog.e(TAG, "Communications error", ex);
- throw ex;
- } finally {
- synchronized (this) {//发送控制命令完成之后需要关闭流
- if (mOutputStream != null) {
- try {
- mOutputStream.close();
- } catch (IOException e) {
- Slog.w(TAG, "Failed closing output stream", e);
- }
- mOutputStream = null;
- }
- }
- try {//关闭socket
- if (socket != null) {
- socket.close();
- }
- } catch (IOException ex) {
- Slog.w(TAG, "Failed closing socket", ex);
- }
- }
- }
2.处理者
mCallbacks.onDaemonConnected();
//代码路径:AndroidSourcecode2.3/frameworks/base/services/java/com/android/server/MountService.java
//因为MountService实现了INativeDaemonConnectorCallbacks接口并覆写了其中的方法,因此这里会调用MountService中的onDaemonConnected()方法。该方法完成了对挂载消息的处理
- public void onDaemonConnected() {
- /*
- * Since we'll be calling back into the NativeDaemonConnector,
- * we need to do our work in a new thread.
- */
- new Thread() {
- public void run() {
- /**
- * Determine media state and UMS detection status
- */
- String path = Environment.getExternalStorageDirectory().getPath();//获取系统SD卡挂载路径该路径在Environment中写死了的
- String state = Environment.MEDIA_REMOVED;//初始状态默认为MEDIA_REMOVED
- try {//该方法可以从Socket中取出处理之后的event消息并放在字符串数组中,该方法中使用的BlockQueue队列是阻塞队列
- //如果队列中没有event将会阻塞直到有event之后再开始处理
- <span style="color:#ff0000;"><span style="color:#000000;">String[] vols = mConnector.doListCommand(
- "volume list", VoldResponseCode.VolumeListResult);</span>
- </span> for (String volstr : vols) {
- String[] tok = volstr.split(" ");
- // FMT: <label> <mountpoint> <state>
- if (!tok[1].equals(path)) {
- Slog.w(TAG, String.format(
- "Skipping unknown volume '%s'",tok[1]));
- continue;
- }
- int st = Integer.parseInt(tok[2]);
- if (st == VolumeState.NoMedia) {
- state = Environment.MEDIA_REMOVED;
- } else if (st == VolumeState.Idle) {
- state = Environment.MEDIA_UNMOUNTED;
- } else if (st == VolumeState.Mounted) {//这里我们是SD卡挂载 因此会执行此处代码
- state = Environment.MEDIA_MOUNTED;
- Slog.i(TAG, "Media already mounted on daemon connection");
- } else if (st == VolumeState.Shared) {
- state = Environment.MEDIA_SHARED;
- Slog.i(TAG, "Media shared on daemon connection");
- } else {
- throw new Exception(String.format("Unexpected state %d", st));
- }
- }
- if (state != null) {//如果state不为空将会执行 因为前面我们已经知道state为挂载消息 所以这里会执行
- if (DEBUG_EVENTS) Slog.i(TAG, "Updating valid state " + state);
- updatePublicVolumeState(path, state);
- }
- } catch (Exception e) {
- Slog.e(TAG, "Error processing initial volume state", e);
- updatePublicVolumeState(path, Environment.MEDIA_REMOVED);
- }
- try {
- boolean avail = doGetShareMethodAvailable("ums");
- notifyShareAvailabilityChange("ums", avail);
- } catch (Exception ex) {
- Slog.w(TAG, "Failed to get share availability");
- }
- /*
- * Now that we've done our initialization, release
- * the hounds!
- */
- mReady = true;
- }
- }.start();
- }
- String[] vols = mConnector.doListCommand(
- "volume list", VoldResponseCode.VolumeListResult);
- public String[] doListCommand(String cmd, int expectedResponseCode)
- throws NativeDaemonConnectorException {
- ArrayList<String> rsp = <span style="color:#000000;">doCommand</span>(cmd);
- String[] rdata = new String[rsp.size()-1];
- int idx = 0;
- for (int i = 0; i < rsp.size(); i++) {
- String line = rsp.get(i);
- try {
- String[] tok = line.split(" ");
- int code = Integer.parseInt(tok[0]);
- if (code == expectedResponseCode) {
- rdata[idx++] = line.substring(tok[0].length() + 1);
- } else if (code == NativeDaemonConnector.ResponseCode.CommandOkay) {
- if (LOCAL_LOGD) Slog.d(TAG, String.format("List terminated with {%s}", line));
- int last = rsp.size() -1;
- if (i != last) {
- Slog.w(TAG, String.format("Recv'd %d lines after end of list {%s}", (last-i), cmd));
- for (int j = i; j <= last ; j++) {
- Slog.w(TAG, String.format("ExtraData <%s>", rsp.get(i)));
- }
- }
- return rdata;
- } else {
- throw new NativeDaemonConnectorException(
- String.format("Expected list response %d, but got %d",
- expectedResponseCode, code));
- }
- } catch (NumberFormatException nfe) {
- throw new NativeDaemonConnectorException(
- String.format("Error reading code '%s'", line));
- }
- }
- throw new NativeDaemonConnectorException("Got an empty response");
- }
- public synchronized ArrayList<String> doCommand(String cmd)
- throws NativeDaemonConnectorException {
- mResponseQueue.clear();
- <span style="color:#000000;">sendCommand</span>(cmd);//向底层发送之前传递的“volume list”指令
- ArrayList<String> response = new ArrayList<String>();
- boolean complete = false;
- int code = -1;
- while (!complete) {
- try {
- // TODO - this should not block forever
- String line = mResponseQueue.take();//从队列中取出event
- if (LOCAL_LOGD) Slog.d(TAG, String.format("RSP <- {%s}", line));
- String[] tokens = line.split(" ");
- try {
- code = Integer.parseInt(tokens[0]);
- } catch (NumberFormatException nfe) {
- throw new NativeDaemonConnectorException(
- String.format("Invalid response from daemon (%s)", line));
- }
- if ((code >= 200) && (code < 600)) {
- complete = true;
- }
- response.add(line);
- } catch (InterruptedException ex) {
- Slog.e(TAG, "Failed to process response", ex);
- }
- }
- if (code >= ResponseCode.FailedRangeStart &&
- code <= ResponseCode.FailedRangeEnd) {
- /*
- * Note: The format of the last response in this case is
- * "NNN <errmsg>"
- */
- throw new NativeDaemonConnectorException(
- code, cmd, response.get(response.size()-1).substring(4));
- }
- return response;//将经过分析之后复合要求的event放入该ArrayList中并返回<pre name="code" class="java">}
返回doListCommand方法中,大致信息是从ArrayList中取出之前存入的符合要求的event,然后对这些event进行拆分,并截取其中的前部分存放在rdata这个字符串数组中返回。
这里继续返回到onDaemonConnected()新开的线程中,接着往下走,对返回的这个字符串数组再次进行分拆并分析,从代码中可以知道,这些字符串中存储了SD卡的挂载路径以及目前的状态信息。因为我们从之前的分析中可以知道,我们的SD卡已经挂载成功了,因此这里的状态是state = Environment.MEDIA_MOUNTED然后执行updatePublicVolumeState(path, state);方法。
3.中转站
private void updatePublicVolumeState(String path, String state)
//代码路径:AndroidSourcecode2.3/frameworks/base/services/java/com/android/server/MountService.java
- private void updatePublicVolumeState(String path, String state) {
- if (!path.equals(Environment.getExternalStorageDirectory().getPath())) {
- Slog.w(TAG, "Multiple volumes not currently supported");
- return;
- }
- if (mLegacyState.equals(state)) {
- Slog.w(TAG, String.format("Duplicate state transition (%s -> %s)", mLegacyState, state));
- return;
- }
- if (Environment.MEDIA_UNMOUNTED.equals(state)) {
- // Tell the package manager the media is gone.
- mPms.updateExternalMediaStatus(false, false);
- /*
- * Some OBBs might have been unmounted when this volume was
- * unmounted, so send a message to the handler to let it know to
- * remove those from the list of mounted OBBS.
- */
- mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_FLUSH_MOUNT_STATE,
- path));
- }<span style="color:#ff0000;"> <span style="color:#000000;">else if (Environment.MEDIA_MOUNTED.equals(state)) {
- // Tell the package manager the media is available for use.
- mPms.updateExternalMediaStatus(true, false);</span>
- </span>}
- String oldState = mLegacyState;
- mLegacyState = state;
- synchronized (mListeners) {
- for (int i = mListeners.size() -1; i >= 0; i--) {
- MountServiceBinderListener bl = mListeners.get(i);
- try {
- bl.mListener.onStorageStateChanged(path, oldState, state);
- } catch (RemoteException rex) {
- Slog.e(TAG, "Listener dead");
- mListeners.remove(i);
- } catch (Exception ex) {
- Slog.e(TAG, "Listener failed", ex);
- }
- }
- }
- }
- mPms.updateExternalMediaStatus(true, false);
接下来我们看看
- synchronized (mListeners) {
- for (int i = mListeners.size() -1; i >= 0; i--) {
- MountServiceBinderListener bl = mListeners.get(i);
- try {
- bl.mListener.onStorageStateChanged(path, oldState, state);
- } catch (RemoteException rex) {
- Slog.e(TAG, "Listener dead");
- mListeners.remove(i);
- } catch (Exception ex) {
- Slog.e(TAG, "Listener failed", ex);
- }
- }
- }
- bl.mListener.onStorageStateChanged(path, oldState, state);
说了这么多废话,下一篇文章将继续分析SD卡挂载消息是如何在"设置"中显示出来的。