Search in sources :

Example 1 with IInterceptor

use of com.alibaba.android.arouter.facade.template.IInterceptor in project ARouter by alibaba.

the class InterceptorServiceImpl method _excute.

/**
     * Excute interceptor
     *
     * @param index    current interceptor index
     * @param counter  interceptor counter
     * @param postcard routeMeta
     */
private static void _excute(final int index, final CancelableCountDownLatch counter, final Postcard postcard) {
    if (index < Warehouse.interceptors.size()) {
        IInterceptor iInterceptor = Warehouse.interceptors.get(index);
        iInterceptor.process(postcard, new InterceptorCallback() {

            @Override
            public void onContinue(Postcard postcard) {
                // Last interceptor excute over with no exception.
                counter.countDown();
                // When counter is down, it will be execute continue ,but index bigger than interceptors size, then U know.
                _excute(index + 1, counter, postcard);
            }

            @Override
            public void onInterrupt(Throwable exception) {
                // Last interceptor excute over with fatal exception.
                // save the exception message for backup.
                postcard.setTag(null == exception ? new HandlerException("No message.") : exception.getMessage());
                counter.cancel();
            // Be attention, maybe the thread in callback has been changed,
            // then the catch block(L207) will be invalid.
            // The worst is the thread changed to main thread, then the app will be crash, if you throw this exception!
            //                    if (!Looper.getMainLooper().equals(Looper.myLooper())) {    // You shouldn't throw the exception if the thread is main thread.
            //                        throw new HandlerException(exception.getMessage());
            //                    }
            }
        });
    }
}
Also used : Postcard(com.alibaba.android.arouter.facade.Postcard) HandlerException(com.alibaba.android.arouter.exception.HandlerException) IInterceptor(com.alibaba.android.arouter.facade.template.IInterceptor) InterceptorCallback(com.alibaba.android.arouter.facade.callback.InterceptorCallback)

Aggregations

HandlerException (com.alibaba.android.arouter.exception.HandlerException)1 Postcard (com.alibaba.android.arouter.facade.Postcard)1 InterceptorCallback (com.alibaba.android.arouter.facade.callback.InterceptorCallback)1 IInterceptor (com.alibaba.android.arouter.facade.template.IInterceptor)1