package epson.print.widgets;

import android.content.ContentResolver;
import android.content.Context;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AlphabetIndexer;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import epson.print.R;
import java.util.Vector;

public abstract class AbstractListBuilder extends AbstractBuilder {
    protected BaseAdapter mAdapter;
    protected ContentResolver mContentResolver;
    protected Context mContext;
    protected Cursor mCursor;
    protected Vector<Object> mData;
    private Handler mHandlerObserver = new Handler(new Handler.Callback() {
        public boolean handleMessage(Message message) {
            new BuildData().execute(new Void[0]);
            return false;
        }
    });
    protected FactoryContentObserver mObserver;
    protected String[] mProjection;
    protected String mSelection;
    protected String[] mSelectionArgs;
    protected String mSortOrder;
    protected Uri mUri;

    public abstract void addPrinter(String[] strArr);

    protected abstract void buildData();

    public abstract View buildViewHolder();

    public abstract Vector<View> buildViewHolderContent(View view);

    public abstract void buildViewHolderContentData(int i, Vector<View> vector, Vector<Object> vector2);

    public abstract AlphabetIndexer getIndexer();

    protected abstract void notifyDataChange();

    protected Cursor query() {
        return this.mContentResolver.query(this.mUri, this.mProjection, this.mSelection, this.mSelectionArgs, this.mSortOrder);
    }

    protected void delete() {
        this.mContentResolver.delete(this.mUri, this.mSelection, this.mSelectionArgs);
    }

    protected class FactoryContentObserver extends ContentObserver {
        Handler mHandler;

        public FactoryContentObserver(Handler handler) {
            super(handler);
            this.mHandler = handler;
        }

        public void onChange(boolean z) {
            super.onChange(z);
            this.mHandler.sendEmptyMessage(0);
        }
    }

    protected void registerObserver() {
        this.mContext.getContentResolver().registerContentObserver(this.mUri, true, this.mObserver);
    }

    protected void triggerEmptyView() {
        TextView textView = (TextView) this.mLayout.findViewById(16908292);
        if (this.mData.size() > 0) {
            textView.setVisibility(8);
            return;
        }
        textView.setVisibility(0);
        this.mLayout.findViewById(16908298).setVisibility(8);
    }

    public AbstractListBuilder(Context context, ViewGroup viewGroup) {
        super(context, viewGroup);
        this.mContext = context;
        this.mData = new Vector<>();
        this.mContentResolver = this.mContext.getContentResolver();
        this.mObserver = new FactoryContentObserver(this.mHandlerObserver);
    }

    public BaseAdapter getAdapter() {
        return this.mAdapter;
    }

    private class BuildData extends AsyncTask<Void, Void, Void> {
        private BuildData() {
        }

        protected void onPreExecute() {
            super.onPreExecute();
            AbstractListBuilder.this.mLayout.findViewById(16908298).setVisibility(8);
        }

        protected void onPostExecute(Void voidR) {
            super.onPostExecute(voidR);
            AbstractListBuilder.this.mLayout.findViewById(16908298).setVisibility(0);
            AbstractListBuilder.this.mLayout.postInvalidate();
            AbstractListBuilder.this.mAdapter.notifyDataSetChanged();
            AbstractListBuilder.this.notifyDataChange();
        }

        protected Void doInBackground(Void... voidArr) {
            synchronized (AbstractListBuilder.this.mData) {
                AbstractListBuilder.this.buildData();
            }
            return null;
        }
    }

    public void build() {
        buildLayout(this.mParentView);
    }

    public void refresh() {
        buildData();
        this.mAdapter.notifyDataSetChanged();
    }

    public Vector<Object> getData() {
        return this.mData;
    }

    protected void buildLayout(ViewGroup viewGroup) {
        this.mLayout = (ViewGroup) this.mLayoutInflater.inflate(R.layout.mylistview, viewGroup);
        ListView listView = (ListView) this.mLayout.findViewById(16908298);
        this.mAdapter = new MyAdapter(this);
        listView.setAdapter(this.mAdapter);
        LinearLayout linearLayout = new LinearLayout(this.mLayout.getContext());
        linearLayout.setVisibility(8);
        listView.setEmptyView(linearLayout);
    }

    public void destructor() {
        this.mData.clear();
    }

    public ListView getListView() {
        return (ListView) this.mLayout.findViewById(16908298);
    }
}
