Picasso 源码分析:Dispatcher

Dispatcher

总体概览

Picasso 源码分析:Dispatcher_第1张图片
Paste_Image.png

Dispatcher是什么?

名字:

从名字来看:分发者。

方法

从其dispatch开头的方法来看:

   void dispatchSubmit(Action action) {
        handler.sendMessage(handler.obtainMessage(REQUEST_SUBMIT, action));
    }

    void dispatchCancel(Action action) {
        handler.sendMessage(handler.obtainMessage(REQUEST_CANCEL, action));
    }

    void dispatchPauseTag(Object tag) {
        handler.sendMessage(handler.obtainMessage(TAG_PAUSE, tag));
    }

    void dispatchResumeTag(Object tag) {
        handler.sendMessage(handler.obtainMessage(TAG_RESUME, tag));
    }

    void dispatchComplete(BitmapHunter hunter) {
        handler.sendMessage(handler.obtainMessage(HUNTER_COMPLETE, hunter));
    }

    void dispatchRetry(BitmapHunter hunter) {
        handler.sendMessageDelayed(handler.obtainMessage(HUNTER_RETRY, hunter), RETRY_DELAY);
    }

    void dispatchFailed(BitmapHunter hunter) {
        handler.sendMessage(handler.obtainMessage(HUNTER_DECODE_FAILED, hunter));
    }

    void dispatchNetworkStateChange(NetworkInfo info) {
        handler.sendMessage(handler.obtainMessage(NETWORK_STATE_CHANGE, info));
    }

    void dispatchAirplaneModeChange(boolean airplaneMode) {
        handler.sendMessage(handler.obtainMessage(AIRPLANE_MODE_CHANGE,
                airplaneMode ? AIRPLANE_MODE_ON : AIRPLANE_MODE_OFF, 0));
    }

dispatch相关的方法负责分发对象。

这些对象通过handler来具体的分发消息。

handler由dispatcher持有。在其构造方法中构建:

Picasso 源码分析:Dispatcher_第2张图片
Paste_Image.png

这个handler是 一个后台线程(DispatcherThread)的 handler:

DispatcherThread

这个线程是拥有Looper的线程,可以通过handler进行消息的收发,进行消息队列的处理。
DispatcherThread---->HandlerThread--->Thread


Picasso 源码分析:Dispatcher_第3张图片
Paste_Image.png

我们再来看下,DispatcherHandler对消息的处理:

private static class DispatcherHandler extends Handler {
        private final Dispatcher dispatcher;

        public DispatcherHandler(Looper looper, Dispatcher dispatcher) {
            super(looper);
            this.dispatcher = dispatcher;
        }

        @Override
        public void handleMessage(final Message msg) {
            switch (msg.what) {
                case REQUEST_SUBMIT: {
                    Action action = (Action) msg.obj;
                    dispatcher.performSubmit(action);
                    break;
                }
                case REQUEST_CANCEL: {
                    Action action = (Action) msg.obj;
                    dispatcher.performCancel(action);
                    break;
                }
                case TAG_PAUSE: {
                    Object tag = msg.obj;
                    dispatcher.performPauseTag(tag);
                    break;
                }
                case TAG_RESUME: {
                    Object tag = msg.obj;
                    dispatcher.performResumeTag(tag);
                    break;
                }
                case HUNTER_COMPLETE: {
                    BitmapHunter hunter = (BitmapHunter) msg.obj;
                    dispatcher.performComplete(hunter);
                    break;
                }
                case HUNTER_RETRY: {
                    BitmapHunter hunter = (BitmapHunter) msg.obj;
                    dispatcher.performRetry(hunter);
                    break;
                }
                case HUNTER_DECODE_FAILED: {
                    BitmapHunter hunter = (BitmapHunter) msg.obj;
                    dispatcher.performError(hunter, false);
                    break;
                }
                case HUNTER_DELAY_NEXT_BATCH: {
                    dispatcher.performBatchComplete();
                    break;
                }
                case NETWORK_STATE_CHANGE: {
                    NetworkInfo info = (NetworkInfo) msg.obj;
                    dispatcher.performNetworkStateChange(info);
                    break;
                }
                case AIRPLANE_MODE_CHANGE: {
                    dispatcher.performAirplaneModeChange(msg.arg1 == AIRPLANE_MODE_ON);
                    break;
                }
                default:
                    Picasso.HANDLER.post(new Runnable() {
                        @Override
                        public void run() {
                            throw new AssertionError("Unknown handler message received: " + msg.what);
                        }
                    });
            }
        }
    }

注意,对消息的处理,都是调用dispatcher中的 perform开头的方法!

简单的流程图,如下:

Picasso 源码分析:Dispatcher_第4张图片
Paste_Image.png

dispatcher方法全貌:

Picasso 源码分析:Dispatcher_第5张图片
Paste_Image.png

深入一个具体的执行流程

Picasso 源码分析:Dispatcher_第6张图片
Paste_Image.png

关键点:BitmapHunter依据action去搜寻bitmap。(这个会单独一篇文章分析。)

这一堆任务来了,怎么管理?

用集合来记录这一堆任务

Picasso 源码分析:Dispatcher_第7张图片
Paste_Image.png

在执行任务,暂停任务,恢复任务,取消任务的相关操作也主要是对这些集合的操作。
下面大概说一下:

  1. 暂停任务:
Picasso 源码分析:Dispatcher_第8张图片
Paste_Image.png
  1. 恢复任务:
Picasso 源码分析:Dispatcher_第9张图片
Paste_Image.png

在Picasso中执行到:

Picasso 源码分析:Dispatcher_第10张图片
Paste_Image.png

最终被执行:

Picasso 源码分析:Dispatcher_第11张图片
Paste_Image.png

你可能感兴趣的:(Picasso 源码分析:Dispatcher)