1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.support.v4.view;
18
19import android.content.Context;
20import android.os.Handler;
21import android.os.Handler.Callback;
22import android.os.Looper;
23import android.os.Message;
24import android.support.annotation.LayoutRes;
25import android.support.annotation.NonNull;
26import android.support.annotation.Nullable;
27import android.support.annotation.UiThread;
28import android.support.v4.util.Pools.SynchronizedPool;
29import android.util.AttributeSet;
30import android.util.Log;
31import android.view.LayoutInflater;
32import android.view.View;
33import android.view.ViewGroup;
34
35import java.util.concurrent.ArrayBlockingQueue;
36
37/**
38 * <p>Helper class for inflating layouts asynchronously. To use, construct
39 * an instance of {@link AsyncLayoutInflater} on the UI thread and call
40 * {@link #inflate(int, ViewGroup, OnInflateFinishedListener)}. The
41 * {@link OnInflateFinishedListener} will be invoked on the UI thread
42 * when the inflate request has completed.
43 *
44 * <p>This is intended for parts of the UI that are created lazily or in
45 * response to user interactions. This allows the UI thread to continue
46 * to be responsive & animate while the relatively heavy inflate
47 * is being performed.
48 *
49 * <p>For a layout to be inflated asynchronously it needs to have a parent
50 * whose {@link ViewGroup#generateLayoutParams(AttributeSet)} is thread-safe
51 * and all the Views being constructed as part of inflation must not create
52 * any {@link Handler}s or otherwise call {@link Looper#myLooper()}. If the
53 * layout that is trying to be inflated cannot be constructed
54 * asynchronously for whatever reason, {@link AsyncLayoutInflater} will
55 * automatically fall back to inflating on the UI thread.
56 *
57 * <p>NOTE that the inflated View hierarchy is NOT added to the parent. It is
58 * equivalent to calling {@link LayoutInflater#inflate(int, ViewGroup, boolean)}
59 * with attachToRoot set to false. Callers will likely want to call
60 * {@link ViewGroup#addView(View)} in the {@link OnInflateFinishedListener}
61 * callback at a minimum.
62 *
63 * <p>This inflater does not support setting a {@link LayoutInflater.Factory}
64 * nor {@link LayoutInflater.Factory2}. Similarly it does not support inflating
65 * layouts that contain fragments.
66 */
67public final class AsyncLayoutInflater {
68    private static final String TAG = "AsyncLayoutInflater";
69
70    LayoutInflater mInflater;
71    Handler mHandler;
72    InflateThread mInflateThread;
73
74    public AsyncLayoutInflater(@NonNull Context context) {
75        mInflater = new BasicInflater(context);
76        mHandler = new Handler(mHandlerCallback);
77        mInflateThread = InflateThread.getInstance();
78    }
79
80    @UiThread
81    public void inflate(@LayoutRes int resid, @Nullable ViewGroup parent,
82            @NonNull OnInflateFinishedListener callback) {
83        if (callback == null) {
84            throw new NullPointerException("callback argument may not be null!");
85        }
86        InflateRequest request = mInflateThread.obtainRequest();
87        request.inflater = this;
88        request.resid = resid;
89        request.parent = parent;
90        request.callback = callback;
91        mInflateThread.enqueue(request);
92    }
93
94    private Callback mHandlerCallback = new Callback() {
95        @Override
96        public boolean handleMessage(Message msg) {
97            InflateRequest request = (InflateRequest) msg.obj;
98            if (request.view == null) {
99                request.view = mInflater.inflate(
100                        request.resid, request.parent, false);
101            }
102            request.callback.onInflateFinished(
103                    request.view, request.resid, request.parent);
104            mInflateThread.releaseRequest(request);
105            return true;
106        }
107    };
108
109    public interface OnInflateFinishedListener {
110        void onInflateFinished(View view, int resid, ViewGroup parent);
111    }
112
113    private static class InflateRequest {
114        AsyncLayoutInflater inflater;
115        ViewGroup parent;
116        int resid;
117        View view;
118        OnInflateFinishedListener callback;
119
120        InflateRequest() {
121        }
122    }
123
124    private static class BasicInflater extends LayoutInflater {
125        private static final String[] sClassPrefixList = {
126            "android.widget.",
127            "android.webkit.",
128            "android.app."
129        };
130
131        BasicInflater(Context context) {
132            super(context);
133        }
134
135        @Override
136        public LayoutInflater cloneInContext(Context newContext) {
137            return new BasicInflater(newContext);
138        }
139
140        @Override
141        protected View onCreateView(String name, AttributeSet attrs) throws ClassNotFoundException {
142            for (String prefix : sClassPrefixList) {
143                try {
144                    View view = createView(name, prefix, attrs);
145                    if (view != null) {
146                        return view;
147                    }
148                } catch (ClassNotFoundException e) {
149                    // In this case we want to let the base class take a crack
150                    // at it.
151                }
152            }
153
154            return super.onCreateView(name, attrs);
155        }
156    }
157
158    private static class InflateThread extends Thread {
159        private static final InflateThread sInstance;
160        static {
161            sInstance = new InflateThread();
162            sInstance.start();
163        }
164
165        public static InflateThread getInstance() {
166            return sInstance;
167        }
168
169        private ArrayBlockingQueue<InflateRequest> mQueue = new ArrayBlockingQueue<>(10);
170        private SynchronizedPool<InflateRequest> mRequestPool = new SynchronizedPool<>(10);
171
172        @Override
173        public void run() {
174            while (true) {
175                InflateRequest request;
176                try {
177                    request = mQueue.take();
178                } catch (InterruptedException ex) {
179                    // Odd, just continue
180                    Log.w(TAG, ex);
181                    continue;
182                }
183
184                try {
185                    request.view = request.inflater.mInflater.inflate(
186                            request.resid, request.parent, false);
187                } catch (RuntimeException ex) {
188                    // Probably a Looper failure, retry on the UI thread
189                    Log.w(TAG, "Failed to inflate resource in the background! Retrying on the UI"
190                            + " thread", ex);
191                }
192                Message.obtain(request.inflater.mHandler, 0, request)
193                        .sendToTarget();
194            }
195        }
196
197        public InflateRequest obtainRequest() {
198            InflateRequest obj = mRequestPool.acquire();
199            if (obj == null) {
200                obj = new InflateRequest();
201            }
202            return obj;
203        }
204
205        public void releaseRequest(InflateRequest obj) {
206            obj.callback = null;
207            obj.inflater = null;
208            obj.parent = null;
209            obj.resid = 0;
210            obj.view = null;
211            mRequestPool.release(obj);
212        }
213
214        public void enqueue(InflateRequest request) {
215            try {
216                mQueue.put(request);
217            } catch (InterruptedException e) {
218                throw new RuntimeException(
219                        "Failed to enqueue async inflate request", e);
220            }
221        }
222    }
223}
224