Android 2.3 SD卡挂载流程浅析(五)

来源:互联网 发布:pdf转exc软件 编辑:程序博客网 时间:2024/05/08 04:30

      前面四篇博文:《Android 2.3 SD卡挂载流程浅析(一)》、《Android 2.3 SD卡挂载流程浅析(二)》、《Android 2.3 SD卡挂载流程浅析(三)》、《Android 2.3 SD卡挂载流程浅析(四)》主要是对SD卡的挂载流程从底到上的一个分析,本文将继续接着《Android 2.3 SD卡挂载流程浅析(四)》文章分析,前文主要分析了C/C++的一些代码,本文将主要分析Java代码。废话不多说,依然上这张老图:

       图中绿色箭头表示的就是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));
      找到MountService的构造函数:

    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.         */        mConnector = new NativeDaemonConnector(this, "vold",                PackageManagerService.MAX_CONTAINERS * 2, VOLD_TAG);//通过调用带参数的构造函数生成了一个Runnable对象        mReady = false;        Thread thread = new Thread(mConnector, VOLD_TAG);//这里开启了一个新线程,传递了一个Runnable对象        thread.start();    }
       这里我们重点关注最后两句,这两句的意思我相信有一点java基础的人都知道吧,对,没错,就是开启一个新线程,我继续跟踪这个传进来的Runnable对象mConnector,查看NativeDaemonConnector.java后可以知道,该类实现了Runnable接口,同时也覆写了Runnable中的run()方法,在该方法中有一个死循环,主要负责监听来自Vold的Socket消息,这是一个阻塞方法。

       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 {                listenToSocket();            } catch (Exception e) {                Slog.e(TAG, "Error in NativeDaemonConnector", e);                SystemClock.sleep(5000);            }        }    }
       我们查看listenToSocket()中的代码,如下:

    private void listenToSocket() throws IOException {        LocalSocket socket = null;//这些Socket就是用来与底层通信的,接收底层传递上来的关于SD卡挂载的信息        try {            socket = new LocalSocket();            LocalSocketAddress address = new LocalSocketAddress(mSocket,                    LocalSocketAddress.Namespace.RESERVED);            socket.connect(address);            mCallbacks.onDaemonConnected();//主要处理队列中的event方法,后文将接着这里分析。            InputStream inputStream = socket.getInputStream();            mOutputStream = socket.getOutputStream();//同时也可以向底层发出控制命令            byte[] buffer = new byte[BUFFER_SIZE];            int start = 0;            while (true) {                int count = inputStream.read(buffer, start, BUFFER_SIZE - start);//通过inputStream.read来读取Socket中的信息                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之后再开始处理                    String[] vols = mConnector.doListCommand(                        "volume list", VoldResponseCode.VolumeListResult);                    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);
       继续跟踪doListCommand可以知道:

    public String[] doListCommand(String cmd, int expectedResponseCode)            throws NativeDaemonConnectorException {        ArrayList<String> rsp = doCommand(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");    }
       继续跟踪doCommand:

    public synchronized ArrayList<String> doCommand(String cmd)            throws NativeDaemonConnectorException  {        mResponseQueue.clear();        sendCommand(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中并返回
}

       返回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));        } else if (Environment.MEDIA_MOUNTED.equals(state)) {            // Tell the package manager the media is available for use.            mPms.updateExternalMediaStatus(true, false);        }        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);
       该方法位于PackageManagerService.java中,作用是告诉PackageManager外置media可用。在updateExternalMediaStatus方法中,通知PackageManagerService去更新外置media的状态,这包括了读取SD卡中的内容并识别。通过这个步骤以后,我们打开SD卡才能发现哪些东西是系统已经识别的,哪些东西系统不能识别。

       接下来我们看看

        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);
     分析到这里,如果不去了解StorageManager和MountService关系的话,后面是没有办法分析下去的。我们的目的是从底层SD卡的挂载信息如何传递到上层的"设置-存储-SD卡"这个界面中,从而理清一条从底向上的线路。如果对于这一块也有疑问的朋友,希望能够真的去看看源码,并逐步自己一步一步的跟踪看看,有的时候真的有的复杂,很多机制不懂更多的是没听过的机制,但是只要自己想要弄清楚,那么就坚持下去吧。一开始我插入SD卡系统居然有时候识别不到,我就跟踪上层的源码,结果发现解决不了问题,那么就跟踪下去吧,不会的就一边查资料一边问别人,同时一边做记录,这些记录一方面可以帮助自己整理学习的资料,另一方面可以帮助也遇到同样问题的朋友。所以在此写下这些自己的拙见,错误百出但初衷是单纯的。

      说了这么多废话,下一篇文章将继续分析SD卡挂载消息是如何在"设置"中显示出来的。


	
				
		
原创粉丝点击