160 lines
5.9 KiB
Java
160 lines
5.9 KiB
Java
package androidx.asynclayoutinflater.view;
|
|
|
|
import android.content.Context;
|
|
import android.os.Handler;
|
|
import android.os.Message;
|
|
import android.util.AttributeSet;
|
|
import android.util.Log;
|
|
import android.view.LayoutInflater;
|
|
import android.view.View;
|
|
import android.view.ViewGroup;
|
|
import androidx.annotation.LayoutRes;
|
|
import androidx.annotation.NonNull;
|
|
import androidx.annotation.Nullable;
|
|
import androidx.annotation.UiThread;
|
|
import androidx.core.util.Pools;
|
|
import java.util.Objects;
|
|
import java.util.concurrent.ArrayBlockingQueue;
|
|
public final class AsyncLayoutInflater {
|
|
private static final String TAG = "AsyncLayoutInflater";
|
|
public Handler mHandler;
|
|
private Handler.Callback mHandlerCallback = new AnonymousClass1();
|
|
public InflateThread mInflateThread;
|
|
public LayoutInflater mInflater;
|
|
|
|
/* renamed from: androidx.asynclayoutinflater.view.AsyncLayoutInflater$1 reason: invalid class name */
|
|
public class AnonymousClass1 implements Handler.Callback {
|
|
public AnonymousClass1() {
|
|
}
|
|
|
|
@Override // android.os.Handler.Callback
|
|
public boolean handleMessage(Message message) {
|
|
InflateRequest inflateRequest = (InflateRequest) message.obj;
|
|
if (inflateRequest.view == null) {
|
|
inflateRequest.view = AsyncLayoutInflater.this.mInflater.inflate(inflateRequest.resid, inflateRequest.parent, false);
|
|
}
|
|
inflateRequest.callback.onInflateFinished(inflateRequest.view, inflateRequest.resid, inflateRequest.parent);
|
|
AsyncLayoutInflater.this.mInflateThread.releaseRequest(inflateRequest);
|
|
return true;
|
|
}
|
|
}
|
|
|
|
public static class BasicInflater extends LayoutInflater {
|
|
private static final String[] sClassPrefixList = {"android.widget.", "android.webkit.", "android.app."};
|
|
|
|
public BasicInflater(Context context) {
|
|
super(context);
|
|
}
|
|
|
|
@Override // android.view.LayoutInflater
|
|
public LayoutInflater cloneInContext(Context context) {
|
|
return new BasicInflater(context);
|
|
}
|
|
|
|
@Override // android.view.LayoutInflater
|
|
public View onCreateView(String str, AttributeSet attributeSet) throws ClassNotFoundException {
|
|
for (String str2 : sClassPrefixList) {
|
|
try {
|
|
View createView = createView(str, str2, attributeSet);
|
|
if (createView != null) {
|
|
return createView;
|
|
}
|
|
} catch (ClassNotFoundException unused) {
|
|
}
|
|
}
|
|
return super.onCreateView(str, attributeSet);
|
|
}
|
|
}
|
|
|
|
public static class InflateRequest {
|
|
public OnInflateFinishedListener callback;
|
|
public AsyncLayoutInflater inflater;
|
|
public ViewGroup parent;
|
|
public int resid;
|
|
public View view;
|
|
}
|
|
|
|
public static class InflateThread extends Thread {
|
|
private static final InflateThread sInstance;
|
|
private ArrayBlockingQueue<InflateRequest> mQueue = new ArrayBlockingQueue<>(10);
|
|
private Pools.SynchronizedPool<InflateRequest> mRequestPool = new Pools.SynchronizedPool<>(10);
|
|
|
|
static {
|
|
InflateThread inflateThread = new InflateThread();
|
|
sInstance = inflateThread;
|
|
inflateThread.start();
|
|
}
|
|
|
|
private InflateThread() {
|
|
}
|
|
|
|
public static InflateThread getInstance() {
|
|
return sInstance;
|
|
}
|
|
|
|
public void enqueue(InflateRequest inflateRequest) {
|
|
try {
|
|
this.mQueue.put(inflateRequest);
|
|
} catch (InterruptedException e) {
|
|
throw new RuntimeException("Failed to enqueue async inflate request", e);
|
|
}
|
|
}
|
|
|
|
public InflateRequest obtainRequest() {
|
|
InflateRequest acquire = this.mRequestPool.acquire();
|
|
return acquire == null ? new InflateRequest() : acquire;
|
|
}
|
|
|
|
public void releaseRequest(InflateRequest inflateRequest) {
|
|
inflateRequest.callback = null;
|
|
inflateRequest.inflater = null;
|
|
inflateRequest.parent = null;
|
|
inflateRequest.resid = 0;
|
|
inflateRequest.view = null;
|
|
this.mRequestPool.release(inflateRequest);
|
|
}
|
|
|
|
@Override // java.lang.Thread, java.lang.Runnable
|
|
public void run() {
|
|
while (true) {
|
|
runInner();
|
|
}
|
|
}
|
|
|
|
public void runInner() {
|
|
try {
|
|
InflateRequest take = this.mQueue.take();
|
|
try {
|
|
take.view = take.inflater.mInflater.inflate(take.resid, take.parent, false);
|
|
} catch (RuntimeException e) {
|
|
Log.w(AsyncLayoutInflater.TAG, "Failed to inflate resource in the background! Retrying on the UI thread", e);
|
|
}
|
|
Message.obtain(take.inflater.mHandler, 0, take).sendToTarget();
|
|
} catch (InterruptedException e2) {
|
|
Log.w(AsyncLayoutInflater.TAG, e2);
|
|
}
|
|
}
|
|
}
|
|
|
|
public interface OnInflateFinishedListener {
|
|
void onInflateFinished(@NonNull View view, @LayoutRes int i, @Nullable ViewGroup viewGroup);
|
|
}
|
|
|
|
public AsyncLayoutInflater(@NonNull Context context) {
|
|
this.mInflater = new BasicInflater(context);
|
|
this.mHandler = new Handler(this.mHandlerCallback);
|
|
this.mInflateThread = InflateThread.getInstance();
|
|
}
|
|
|
|
@UiThread
|
|
public void inflate(@LayoutRes int i, @Nullable ViewGroup viewGroup, @NonNull OnInflateFinishedListener onInflateFinishedListener) {
|
|
Objects.requireNonNull(onInflateFinishedListener, "callback argument may not be null!");
|
|
InflateRequest obtainRequest = this.mInflateThread.obtainRequest();
|
|
obtainRequest.inflater = this;
|
|
obtainRequest.resid = i;
|
|
obtainRequest.parent = viewGroup;
|
|
obtainRequest.callback = onInflateFinishedListener;
|
|
this.mInflateThread.enqueue(obtainRequest);
|
|
}
|
|
}
|