package epson.print.screen;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.RemoteException;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AlphabetIndexer;
import android.widget.ListView;
import android.widget.TextView;
import com.epson.mobilephone.common.escpr.MediaInfo;
import epson.print.ActivityIACommon;
import epson.print.R;
import epson.print.Util.EPLog;
import epson.print.screen.PrintSetting;
import epson.print.service.EpsonService;
import epson.print.service.IEpsonService;
import epson.print.service.IEpsonServiceCallback;
import epson.print.widgets.AbstractListBuilder;
import java.util.ArrayList;
import java.util.Vector;

public class PaperSourceSettingScr extends ActivityIACommon {
    private static final int ID_BINDSERVICE = 1;
    private static final int ID_NOT_ESCPRSUPPORT = 10;
    public static final String PRINT_SETTING_TYPE = "print-setting-type";
    private static final int SETTING_DONE = 3;
    ArrayList<PaperSourceSetting> aPaperSourceSetting;
    AdapterView.OnItemClickListener listItemClickLister = new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long j) {
            PaperSourceSetting paperSourceSetting = (PaperSourceSetting) PaperSourceSettingScr.this.mListBuilder.getData().elementAt(i);
            if (!paperSourceSetting.bSupportESCPR) {
                PaperSourceSettingScr.this.showDialog(10);
                return;
            }
            PaperSourceSettingScr paperSourceSettingScr = PaperSourceSettingScr.this;
            PrintSetting instance = PrintSetting.getInstance(paperSourceSettingScr, paperSourceSettingScr.mPrintSettingTypeName);
            instance.loadSettings();
            instance.paperSizeValue = paperSourceSetting.paperSizeId;
            instance.paperTypeValue = paperSourceSetting.paperTypeId;
            instance.paperSourceValue = paperSourceSetting.paperSource;
            instance.saveSettings();
            try {
                PaperSourceSettingScr.this.mEpsonService.updatePrinterSettings(PaperSourceSettingScr.this.mPrintSettingTypeName);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
            PaperSourceSettingScr.this.setResult(3);
            PaperSourceSettingScr.this.finish();
        }
    };
    /* access modifiers changed from: private */
    public IEpsonServiceCallback mCallback = new IEpsonServiceCallback.Stub() {
        public void onFindPrinterResult(String str, String str2, String str3, String str4, String str5) throws RemoteException {
        }

        public void onGetInkState() throws RemoteException {
        }

        public void onGetStatusState() throws RemoteException {
        }

        public void onNotifyContinueable(int i) throws RemoteException {
        }

        public void onNotifyEndJob(int i) throws RemoteException {
        }

        public void onNotifyError(int i, int i2, boolean z) throws RemoteException {
        }

        public void onNotifyProgress(int i, int i2) throws RemoteException {
        }
    };
    private ServiceConnection mEpsonConnection = new ServiceConnection() {
        public void onServiceDisconnected(ComponentName componentName) {
            EPLog.v("SettingScr", "onServiceDisconnected");
            try {
                PaperSourceSettingScr.this.mEpsonService.unregisterCallback(PaperSourceSettingScr.this.mCallback);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
            IEpsonService unused = PaperSourceSettingScr.this.mEpsonService = null;
        }

        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            IEpsonService unused = PaperSourceSettingScr.this.mEpsonService = IEpsonService.Stub.asInterface(iBinder);
            EPLog.v("SettingScr", "onServiceConnected");
            if (PaperSourceSettingScr.this.mEpsonService != null) {
                try {
                    PaperSourceSettingScr.this.mEpsonService.registerCallback(PaperSourceSettingScr.this.mCallback);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                PaperSourceSettingScr.this.mHandler.sendEmptyMessage(1);
            }
        }
    };
    /* access modifiers changed from: private */
    public IEpsonService mEpsonService = null;
    Handler mHandler = new Handler(new Handler.Callback() {
        public boolean handleMessage(Message message) {
            int i = message.what;
            if (i == 1) {
                PaperSourceSettingScr paperSourceSettingScr = PaperSourceSettingScr.this;
                paperSourceSettingScr.aPaperSourceSetting = paperSourceSettingScr.getIntent().getParcelableArrayListExtra(PaperSourceInfo.KEY_PAPERSOURCEINFO);
                PaperSourceSettingScr.this.addPaparSettings();
                PaperSourceSettingScr.this.progress.dismiss();
                return false;
            } else if (i != 100) {
                return false;
            } else {
                PaperSourceSettingScr.this.aPaperSourceSetting = message.getData().getParcelableArrayList(PaperSourceInfo.KEY_PAPERSOURCEINFO);
                PaperSourceSettingScr.this.addPaparSettings();
                return false;
            }
        }
    });
    PaperSourceInfoBuilder mListBuilder = null;
    /* access modifiers changed from: private */
    public String mPrintSettingTypeName = PrintSetting.Kind.photo.name();
    PaperSourceInfo paperSourceInfo = null;
    WorkingDialog progress = null;

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        ViewGroup viewGroup = (ViewGroup) getLayoutInflater().inflate(R.layout.paper_source_setting, (ViewGroup) null);
        this.mListBuilder = new PaperSourceInfoBuilder(getBaseContext(), viewGroup);
        this.mListBuilder.build();
        ((TextView) viewGroup.findViewById(R.id.empty)).setText(R.string.ph_Detail);
        ((ListView) viewGroup.findViewById(16908298)).setOnItemClickListener(this.listItemClickLister);
        setContentView((View) viewGroup);
        setActionBar((int) R.string.ph_Title, true);
        this.mPrintSettingTypeName = getIntent().getStringExtra(PRINT_SETTING_TYPE);
        PrintSetting instance = PrintSetting.getInstance(this, this.mPrintSettingTypeName);
        instance.loadSettings();
        PaperSourceSetting paperSourceSetting = new PaperSourceSetting();
        paperSourceSetting.paperSizeId = instance.paperSizeValue;
        paperSourceSetting.paperTypeId = instance.paperTypeValue;
        paperSourceSetting.paperSource = instance.paperSourceValue;
        this.mListBuilder.setResource(paperSourceSetting);
        this.paperSourceInfo = PaperSourceInfo.getInstance(this);
        if (this.mEpsonService == null) {
            Intent intent = new Intent();
            intent.setClass(this, EpsonService.class);
            bindService(intent, this.mEpsonConnection, 1);
        }
        this.progress = new WorkingDialog(this);
        this.progress.show();
    }

    protected void onDestroy() {
        super.onDestroy();
        IEpsonService iEpsonService = this.mEpsonService;
        if (iEpsonService != null) {
            try {
                iEpsonService.unregisterCallback(this.mCallback);
                unbindService(this.mEpsonConnection);
                this.mEpsonService = null;
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    }

    protected void onResume() {
        super.onResume();
        this.paperSourceInfo.start(this, this.mHandler);
    }

    protected void onPause() {
        super.onPause();
        this.paperSourceInfo.stop();
    }

    private void addPaparSettings() {
        int i;
        int i2;
        if (this.mEpsonService != null && this.aPaperSourceSetting != null) {
            this.mListBuilder.refresh();
            for (int i3 = 0; i3 < this.aPaperSourceSetting.size(); i3++) {
                PaperSourceSetting paperSourceSetting = this.aPaperSourceSetting.get(i3);
                try {
                    paperSourceSetting.bSupportESCPR = false;
                    int[] paperSize = this.mEpsonService.getPaperSize();
                    if (paperSize != null) {
                        i = 0;
                        while (true) {
                            if (i >= paperSize.length) {
                                break;
                            } else if (paperSize[i] == paperSourceSetting.paperSizeId) {
                                break;
                            } else {
                                i++;
                            }
                        }
                        this.mListBuilder.addPaperInfoSetting(paperSourceSetting);
                    } else {
                        i = 0;
                    }
                    int[] paperType = this.mEpsonService.getPaperType(i);
                    if (paperType != null) {
                        i2 = 0;
                        while (true) {
                            if (i2 >= paperType.length) {
                                break;
                            } else if (paperType[i2] == paperSourceSetting.paperTypeId) {
                                break;
                            } else {
                                i2++;
                            }
                        }
                    } else {
                        i2 = 0;
                    }
                    int[] paperSource = this.mEpsonService.getPaperSource(i, i2, 1);
                    if (paperSource != null) {
                        int i4 = 0;
                        while (true) {
                            if (i4 >= paperSource.length) {
                                break;
                            } else if (paperSource[i4] == paperSourceSetting.paperSource) {
                                break;
                            } else {
                                i4++;
                            }
                        }
                        this.mListBuilder.addPaperInfoSetting(paperSourceSetting);
                    }
                    paperSourceSetting.bSupportESCPR = true;
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                this.mListBuilder.addPaperInfoSetting(paperSourceSetting);
            }
        }
    }

    protected Dialog onCreateDialog(int i) {
        Dialog onCreateDialog = super.onCreateDialog(i);
        if (i != 10) {
            return onCreateDialog;
        }
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(getResources().getString(R.string.ph_cannotSet_Title)).setMessage(getResources().getString(R.string.ph_cannotSet_Detail)).setPositiveButton(getResources().getString(R.string.f343ok), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        });
        return builder.create();
    }

    public class PaperSourceInfoBuilder extends AbstractListBuilder {
        private static final int ACTIVE = 2;
        private static final int SIZE = 0;
        private static final int TYPE = 1;
        PaperSourceSetting curSetting = null;
        MediaInfo.PaperSize mPaperSizeLookupTable = new MediaInfo.PaperSize();
        MediaInfo.PaperType mPaperTypeLookupTable = new MediaInfo.PaperType();

        public void addPrinter(String[] strArr) {
        }

        public AlphabetIndexer getIndexer() {
            return null;
        }

        protected void notifyDataChange() {
        }

        public PaperSourceInfoBuilder(Context context, ViewGroup viewGroup) {
            super(context, viewGroup);
        }

        protected void buildData() {
            this.mData.clear();
        }

        public View buildViewHolder() {
            return this.mLayoutInflater.inflate(R.layout.printer_item, (ViewGroup) null);
        }

        public Vector<View> buildViewHolderContent(View view) {
            Vector<View> vector = new Vector<>();
            vector.add(view.findViewById(R.id.name));
            vector.add(view.findViewById(R.id.f336ip));
            vector.add(view.findViewById(R.id.active_icon));
            return vector;
        }

        public void buildViewHolderContentData(int i, Vector<View> vector, Vector<Object> vector2) {
            PaperSourceSetting paperSourceSetting = (PaperSourceSetting) vector2.elementAt(i);
            ((TextView) vector.elementAt(0)).setText(PaperSourceSettingScr.this.getResources().getString(this.mPaperSizeLookupTable.getStringId(paperSourceSetting.paperSizeId)));
            ((TextView) vector.elementAt(1)).setText(PaperSourceSettingScr.this.getResources().getString(this.mPaperTypeLookupTable.getStringId(paperSourceSetting.paperTypeId)));
            vector.elementAt(2).setVisibility(this.curSetting.equals(paperSourceSetting) ? 0 : 8);
            if (!paperSourceSetting.bSupportESCPR) {
                ((TextView) vector.elementAt(0)).setTextColor(this.mContext.getResources().getColor(R.color.light_gray));
            } else {
                ((TextView) vector.elementAt(0)).setTextColor(this.mContext.getResources().getColor(R.color.all_black));
            }
        }

        public void setResource(Object obj) {
            this.curSetting = (PaperSourceSetting) obj;
        }

        public void addPaperInfoSetting(PaperSourceSetting paperSourceSetting) {
            this.mData.add(paperSourceSetting);
            this.mAdapter.notifyDataSetChanged();
        }
    }
}
