|
| 1 | +/* |
| 2 | + * Copyright 2018 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 | + |
| 17 | +package com.aprz.base.inflater; |
| 18 | + |
| 19 | +import android.content.Context; |
| 20 | +import android.os.Handler; |
| 21 | +import android.os.Handler.Callback; |
| 22 | +import android.os.Looper; |
| 23 | +import android.os.Message; |
| 24 | +import android.util.AttributeSet; |
| 25 | +import android.util.Log; |
| 26 | +import android.view.LayoutInflater; |
| 27 | +import android.view.View; |
| 28 | +import android.view.ViewGroup; |
| 29 | + |
| 30 | +import androidx.annotation.LayoutRes; |
| 31 | +import androidx.annotation.NonNull; |
| 32 | +import androidx.annotation.Nullable; |
| 33 | +import androidx.annotation.UiThread; |
| 34 | +import androidx.core.util.Pools.SynchronizedPool; |
| 35 | + |
| 36 | +import java.util.concurrent.ArrayBlockingQueue; |
| 37 | + |
| 38 | +/** |
| 39 | + * <p>Helper class for inflating layouts asynchronously. To use, construct |
| 40 | + * an instance of {@link AsyncLayoutInflater} on the UI thread and call |
| 41 | + * {@link #inflate(int, ViewGroup, OnInflateFinishedListener)}. The |
| 42 | + * {@link OnInflateFinishedListener} will be invoked on the UI thread |
| 43 | + * when the inflate request has completed. |
| 44 | + * |
| 45 | + * <p>This is intended for parts of the UI that are created lazily or in |
| 46 | + * response to user interactions. This allows the UI thread to continue |
| 47 | + * to be responsive & animate while the relatively heavy inflate |
| 48 | + * is being performed. |
| 49 | + * |
| 50 | + * <p>For a layout to be inflated asynchronously it needs to have a parent |
| 51 | + * whose {@link ViewGroup#generateLayoutParams(AttributeSet)} is thread-safe |
| 52 | + * and all the Views being constructed as part of inflation must not create |
| 53 | + * any {@link Handler}s or otherwise call {@link Looper#myLooper()}. If the |
| 54 | + * layout that is trying to be inflated cannot be constructed |
| 55 | + * asynchronously for whatever reason, {@link AsyncLayoutInflater} will |
| 56 | + * automatically fall back to inflating on the UI thread. |
| 57 | + * |
| 58 | + * <p>NOTE that the inflated View hierarchy is NOT added to the parent. It is |
| 59 | + * equivalent to calling {@link LayoutInflater#inflate(int, ViewGroup, boolean)} |
| 60 | + * with attachToRoot set to false. Callers will likely want to call |
| 61 | + * {@link ViewGroup#addView(View)} in the {@link OnInflateFinishedListener} |
| 62 | + * callback at a minimum. |
| 63 | + * |
| 64 | + * <p>This inflater does not support setting a {@link LayoutInflater.Factory} |
| 65 | + * nor {@link LayoutInflater.Factory2}. Similarly it does not support inflating |
| 66 | + * layouts that contain fragments. |
| 67 | + */ |
| 68 | +public final class AsyncLayoutInflater { |
| 69 | + private static final String TAG = "AsyncLayoutInflater"; |
| 70 | + |
| 71 | + LayoutInflater mInflater; |
| 72 | + Handler mHandler; |
| 73 | + InflateThread mInflateThread; |
| 74 | + |
| 75 | + /** |
| 76 | + * fix material 的问题 |
| 77 | + */ |
| 78 | + public AsyncLayoutInflater(@NonNull Context context, LayoutInflater layoutInflater) { |
| 79 | + mInflater = layoutInflater; |
| 80 | + mHandler = new Handler(mHandlerCallback); |
| 81 | + mInflateThread = InflateThread.getInstance(); |
| 82 | + } |
| 83 | + |
| 84 | + @UiThread |
| 85 | + public void inflate(@LayoutRes int resid, @Nullable ViewGroup parent, |
| 86 | + @NonNull OnInflateFinishedListener callback) { |
| 87 | + if (callback == null) { |
| 88 | + throw new NullPointerException("callback argument may not be null!"); |
| 89 | + } |
| 90 | + InflateRequest request = mInflateThread.obtainRequest(); |
| 91 | + request.inflater = this; |
| 92 | + request.resid = resid; |
| 93 | + request.parent = parent; |
| 94 | + request.callback = callback; |
| 95 | + mInflateThread.enqueue(request); |
| 96 | + } |
| 97 | + |
| 98 | + private final Callback mHandlerCallback = new Callback() { |
| 99 | + @Override |
| 100 | + public boolean handleMessage(Message msg) { |
| 101 | + InflateRequest request = (InflateRequest) msg.obj; |
| 102 | + if (request.view == null) { |
| 103 | + request.view = mInflater.inflate( |
| 104 | + request.resid, request.parent, false); |
| 105 | + } |
| 106 | + request.callback.onInflateFinished( |
| 107 | + request.view, request.resid, request.parent); |
| 108 | + mInflateThread.releaseRequest(request); |
| 109 | + return true; |
| 110 | + } |
| 111 | + }; |
| 112 | + |
| 113 | + public interface OnInflateFinishedListener { |
| 114 | + void onInflateFinished(@NonNull View view, @LayoutRes int resid, |
| 115 | + @Nullable ViewGroup parent); |
| 116 | + } |
| 117 | + |
| 118 | + private static class InflateRequest { |
| 119 | + AsyncLayoutInflater inflater; |
| 120 | + ViewGroup parent; |
| 121 | + int resid; |
| 122 | + View view; |
| 123 | + OnInflateFinishedListener callback; |
| 124 | + |
| 125 | + InflateRequest() { |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + private static class InflateThread extends Thread { |
| 130 | + private static final InflateThread sInstance; |
| 131 | + |
| 132 | + static { |
| 133 | + sInstance = new InflateThread(); |
| 134 | + sInstance.start(); |
| 135 | + } |
| 136 | + |
| 137 | + public static InflateThread getInstance() { |
| 138 | + return sInstance; |
| 139 | + } |
| 140 | + |
| 141 | + private ArrayBlockingQueue<InflateRequest> mQueue = new ArrayBlockingQueue<>(10); |
| 142 | + private SynchronizedPool<InflateRequest> mRequestPool = new SynchronizedPool<>(10); |
| 143 | + |
| 144 | + // Extracted to its own method to ensure locals have a constrained liveness |
| 145 | + // scope by the GC. This is needed to avoid keeping previous request references |
| 146 | + // alive for an indeterminate amount of time, see b/33158143 for details |
| 147 | + public void runInner() { |
| 148 | + InflateRequest request; |
| 149 | + try { |
| 150 | + request = mQueue.take(); |
| 151 | + } catch (InterruptedException ex) { |
| 152 | + // Odd, just continue |
| 153 | + Log.w(TAG, ex); |
| 154 | + return; |
| 155 | + } |
| 156 | + |
| 157 | + try { |
| 158 | + request.view = request.inflater.mInflater.inflate( |
| 159 | + request.resid, request.parent, false); |
| 160 | + } catch (RuntimeException ex) { |
| 161 | + // Probably a Looper failure, retry on the UI thread |
| 162 | + Log.w(TAG, "Failed to inflate resource in the background! Retrying on the UI" |
| 163 | + + " thread", ex); |
| 164 | + } |
| 165 | + Message.obtain(request.inflater.mHandler, 0, request) |
| 166 | + .sendToTarget(); |
| 167 | + } |
| 168 | + |
| 169 | + @Override |
| 170 | + public void run() { |
| 171 | + while (true) { |
| 172 | + runInner(); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + public InflateRequest obtainRequest() { |
| 177 | + InflateRequest obj = mRequestPool.acquire(); |
| 178 | + if (obj == null) { |
| 179 | + obj = new InflateRequest(); |
| 180 | + } |
| 181 | + return obj; |
| 182 | + } |
| 183 | + |
| 184 | + public void releaseRequest(InflateRequest obj) { |
| 185 | + obj.callback = null; |
| 186 | + obj.inflater = null; |
| 187 | + obj.parent = null; |
| 188 | + obj.resid = 0; |
| 189 | + obj.view = null; |
| 190 | + mRequestPool.release(obj); |
| 191 | + } |
| 192 | + |
| 193 | + public void enqueue(InflateRequest request) { |
| 194 | + try { |
| 195 | + mQueue.put(request); |
| 196 | + } catch (InterruptedException e) { |
| 197 | + throw new RuntimeException( |
| 198 | + "Failed to enqueue async inflate request", e); |
| 199 | + } |
| 200 | + } |
| 201 | + } |
| 202 | +} |
0 commit comments