package com.tencent.mm.plugin.luggage.natives.component.widget;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build.VERSION;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.MeasureSpec;
import android.widget.AbsListView.LayoutParams;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.GridView;
import android.widget.ListAdapter;
import com.google.android.gms.common.util.CrashUtils.ErrorDialogData;
import com.tencent.matrix.trace.core.AppMethodBeat;
import java.lang.reflect.Field;
import java.util.ArrayList;

public class LuGridViewWithHeaderAndFooter extends GridView {
    public static boolean DEBUG = false;
    private OnItemClickListener arw;
    private int enT = -1;
    private int mNumColumns = -1;
    private ArrayList<a> oiP = new ArrayList();
    private OnItemLongClickListener oiQ;
    private View oiR = null;
    private ArrayList<a> oiS = new ArrayList();
    private ListAdapter oiT;
    private c oiU;

    static /* synthetic */ int b(LuGridViewWithHeaderAndFooter luGridViewWithHeaderAndFooter) {
        AppMethodBeat.i(116890);
        int numColumnsCompatible = luGridViewWithHeaderAndFooter.getNumColumnsCompatible();
        AppMethodBeat.o(116890);
        return numColumnsCompatible;
    }

    public LuGridViewWithHeaderAndFooter(Context context) {
        super(context);
        AppMethodBeat.i(116871);
        AppMethodBeat.o(116871);
    }

    public LuGridViewWithHeaderAndFooter(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        AppMethodBeat.i(116872);
        AppMethodBeat.o(116872);
    }

    public LuGridViewWithHeaderAndFooter(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        AppMethodBeat.i(116873);
        AppMethodBeat.o(116873);
    }

    /* Access modifiers changed, original: protected */
    public void onMeasure(int i, int i2) {
        AppMethodBeat.i(116874);
        super.onMeasure(i, i2);
        ListAdapter adapter = getAdapter();
        if (adapter != null && (adapter instanceof b)) {
            ((b) adapter).setNumColumns(getNumColumnsCompatible());
            ((b) adapter).enT = getRowHeight();
        }
        AppMethodBeat.o(116874);
    }

    public void setClipChildren(boolean z) {
    }

    public void setClipChildrenSupper(boolean z) {
        AppMethodBeat.i(116875);
        super.setClipChildren(false);
        AppMethodBeat.o(116875);
    }

    public int getHeaderViewCount() {
        AppMethodBeat.i(116876);
        int size = this.oiP.size();
        AppMethodBeat.o(116876);
        return size;
    }

    public int getFooterViewCount() {
        AppMethodBeat.i(116877);
        int size = this.oiS.size();
        AppMethodBeat.o(116877);
        return size;
    }

    @TargetApi(11)
    private int getNumColumnsCompatible() {
        AppMethodBeat.i(116878);
        int numColumns;
        if (VERSION.SDK_INT >= 11) {
            numColumns = super.getNumColumns();
            AppMethodBeat.o(116878);
            return numColumns;
        }
        try {
            Field declaredField = GridView.class.getDeclaredField("mNumColumns");
            declaredField.setAccessible(true);
            numColumns = declaredField.getInt(this);
            AppMethodBeat.o(116878);
            return numColumns;
        } catch (Exception e) {
            if (this.mNumColumns != -1) {
                numColumns = this.mNumColumns;
                AppMethodBeat.o(116878);
                return numColumns;
            }
            RuntimeException runtimeException = new RuntimeException("Can not determine the mNumColumns for this API platform, please call setNumColumns to set it.");
            AppMethodBeat.o(116878);
            throw runtimeException;
        }
    }

    @TargetApi(16)
    private int getColumnWidthCompatible() {
        RuntimeException runtimeException;
        AppMethodBeat.i(116879);
        int columnWidth;
        if (VERSION.SDK_INT >= 16) {
            columnWidth = super.getColumnWidth();
            AppMethodBeat.o(116879);
            return columnWidth;
        }
        try {
            Field declaredField = GridView.class.getDeclaredField("mColumnWidth");
            declaredField.setAccessible(true);
            columnWidth = declaredField.getInt(this);
            AppMethodBeat.o(116879);
            return columnWidth;
        } catch (NoSuchFieldException e) {
            runtimeException = new RuntimeException(e);
            AppMethodBeat.o(116879);
            throw runtimeException;
        } catch (IllegalAccessException e2) {
            runtimeException = new RuntimeException(e2);
            AppMethodBeat.o(116879);
            throw runtimeException;
        }
    }

    /* Access modifiers changed, original: protected */
    public void onDetachedFromWindow() {
        AppMethodBeat.i(116880);
        super.onDetachedFromWindow();
        this.oiR = null;
        AppMethodBeat.o(116880);
    }

    @TargetApi(16)
    public int getVerticalSpacing() {
        AppMethodBeat.i(116881);
        int i = 0;
        try {
            if (VERSION.SDK_INT < 16) {
                Field declaredField = GridView.class.getDeclaredField("mVerticalSpacing");
                declaredField.setAccessible(true);
                i = declaredField.getInt(this);
            } else {
                i = super.getVerticalSpacing();
            }
        } catch (Exception e) {
        }
        AppMethodBeat.o(116881);
        return i;
    }

    @TargetApi(16)
    public int getHorizontalSpacing() {
        AppMethodBeat.i(116882);
        int i = 0;
        try {
            if (VERSION.SDK_INT < 16) {
                Field declaredField = GridView.class.getDeclaredField("mHorizontalSpacing");
                declaredField.setAccessible(true);
                i = declaredField.getInt(this);
            } else {
                i = super.getHorizontalSpacing();
            }
        } catch (Exception e) {
        }
        AppMethodBeat.o(116882);
        return i;
    }

    public int getRowHeight() {
        AppMethodBeat.i(116883);
        int i;
        if (this.enT > 0) {
            i = this.enT;
            AppMethodBeat.o(116883);
            return i;
        }
        ListAdapter adapter = getAdapter();
        int numColumnsCompatible = getNumColumnsCompatible();
        if (adapter == null || adapter.getCount() <= (this.oiP.size() + this.oiS.size()) * numColumnsCompatible) {
            AppMethodBeat.o(116883);
            return -1;
        }
        int columnWidthCompatible = getColumnWidthCompatible();
        View view = getAdapter().getView(numColumnsCompatible * this.oiP.size(), this.oiR, this);
        LayoutParams layoutParams = null;
        if (view.getLayoutParams() instanceof LayoutParams) {
            layoutParams = (LayoutParams) view.getLayoutParams();
        }
        if (layoutParams == null) {
            layoutParams = new LayoutParams(-1, -2, 0);
            view.setLayoutParams(layoutParams);
        }
        view.measure(getChildMeasureSpec(MeasureSpec.makeMeasureSpec(columnWidthCompatible, ErrorDialogData.SUPPRESSED), 0, layoutParams.width), getChildMeasureSpec(MeasureSpec.makeMeasureSpec(0, 0), 0, layoutParams.height));
        this.oiR = view;
        this.enT = view.getMeasuredHeight();
        i = this.enT;
        AppMethodBeat.o(116883);
        return i;
    }

    public void setAdapter(ListAdapter listAdapter) {
        AppMethodBeat.i(116884);
        this.oiT = listAdapter;
        if (this.oiP.size() > 0 || this.oiS.size() > 0) {
            b bVar = new b(this.oiP, this.oiS, listAdapter);
            int numColumnsCompatible = getNumColumnsCompatible();
            if (numColumnsCompatible > 1) {
                bVar.setNumColumns(numColumnsCompatible);
            }
            bVar.enT = getRowHeight();
            super.setAdapter(bVar);
            AppMethodBeat.o(116884);
            return;
        }
        super.setAdapter(listAdapter);
        AppMethodBeat.o(116884);
    }

    public ListAdapter getOriginalAdapter() {
        return this.oiT;
    }

    public void setNumColumns(int i) {
        AppMethodBeat.i(116885);
        super.setNumColumns(i);
        this.mNumColumns = i;
        ListAdapter adapter = getAdapter();
        if (adapter != null && (adapter instanceof b)) {
            ((b) adapter).setNumColumns(i);
        }
        AppMethodBeat.o(116885);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        AppMethodBeat.i(116886);
        this.arw = onItemClickListener;
        super.setOnItemClickListener(getItemClickHandler());
        AppMethodBeat.o(116886);
    }

    public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener) {
        AppMethodBeat.i(116887);
        this.oiQ = onItemLongClickListener;
        super.setOnItemLongClickListener(getItemClickHandler());
        AppMethodBeat.o(116887);
    }

    private c getItemClickHandler() {
        AppMethodBeat.i(116888);
        if (this.oiU == null) {
            this.oiU = new c(this, (byte) 0);
        }
        c cVar = this.oiU;
        AppMethodBeat.o(116888);
        return cVar;
    }
}
