package com.ifpdos.debugmenu.action;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Looper;
import android.text.Editable;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import c.l.j;
import c.l.m;
import c.l.n;
import c.l.u;
import com.ifpdos.debugmenu.R;
import com.ifpdos.debugmenu.action.InputCMEICodeAction;
import com.ifpdos.debugmenu.action.base.ItemButtonAction;
import com.ifpdos.debugmenu.event.ActivityEvent;
import com.ifpdos.debugmenu.event.ViewEvent;
import com.ifpdos.debugmenu.view.CustomEditText;
import d.f.a.c1;
import d.f.a.i2.a;
import d.f.a.r2.h3;
import d.f.a.s1.e;
import java.util.Objects;
import k.c.a.c;
import k.c.a.j;
import org.greenrobot.eventbus.ThreadMode;

/* JADX INFO: loaded from: classes.dex */
public class InputCMEICodeAction extends ItemButtonAction implements c1, m {
    public static final /* synthetic */ boolean $assertionsDisabled = false;
    private static final String CMEI_CODE_LENGTH = "persist.sys.cmei.length";
    private static final String CMEI_CODE_START_WITH = "persist.sys.cmei.code";
    private static final int CMEI_LENGTH = getCteiLength();
    private static final String CMEI_START_WITH = "11000013";
    private static final int DEFAULT_CMEI_LENGTH = 15;
    private static final int DISMISS_DIALOG_DELAY_MILLIS = 1000;
    private static final int HIDE_SOFT_INPUT = 0;
    private static final int SHOW_SOFT_INPUT = 1;
    private static final int SHOW_SOFT_INPUT_DELAY_MILLIS = 200;
    private static final String TAG = "InputCMEICodeAction";
    private e actionFinishCall;
    private CustomEditText cmeiInput;
    private final InputFilter codeInputFilter;
    private InputMethodManager inputMethodManager;
    private final InputFilter lengthFilter;
    private View mDialogView;
    private final Runnable mDismissDialog;
    private final Handler mHandler;
    private boolean mHasDoubleCheck;
    private String mLastInput;
    private TextView mTipsView;

    public InputCMEICodeAction(Context context) {
        super(context);
        this.mHandler = new Handler(Looper.getMainLooper());
        this.mLastInput = "";
        this.mDismissDialog = new Runnable() { // from class: d.f.a.p1.d3
            @Override // java.lang.Runnable
            public final void run() {
                this.f2705b.h();
            }
        };
        this.codeInputFilter = new InputFilter() { // from class: d.f.a.p1.e3
            @Override // android.text.InputFilter
            public final CharSequence filter(CharSequence charSequence, int i2, int i3, Spanned spanned, int i4, int i5) {
                boolean z = InputCMEICodeAction.$assertionsDisabled;
                if (charSequence.toString().matches("[0-9]+")) {
                    return null;
                }
                return "";
            }
        };
        this.lengthFilter = new InputFilter.LengthFilter(getCteiLength());
        a.a().f2585b.add(this);
        c.c().k(this);
    }

    private boolean checkCMEICode(String str) {
        return str.length() == CMEI_LENGTH && str.startsWith(getCteiStartWith());
    }

    private static int getCteiLength() {
        return d.i.f.c.c.c(CMEI_CODE_LENGTH, 15);
    }

    private String getCteiStartWith() {
        return d.i.f.c.c.f(CMEI_CODE_START_WITH, CMEI_START_WITH);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public String getCurrentInput(String str) {
        return (this.mLastInput.startsWith(getCteiStartWith()) && this.mLastInput.length() == getCteiLength()) ? this.mLastInput : str.startsWith(getCteiStartWith()) ? str : getCteiStartWith();
    }

    private void performConfirm(final EditText editText, final e eVar) {
        this.mHandler.post(new Runnable() { // from class: d.f.a.p1.k3
            @Override // java.lang.Runnable
            public final void run() {
                this.f2790b.i(editText, eVar);
            }
        });
    }

    /* JADX WARN: Multi-variable type inference failed */
    @SuppressLint({"SetTextI18n"})
    private boolean setCMEICode(String str) {
        this.cmeiInput.setText("");
        if (!checkCMEICode(str)) {
            this.cmeiInput.setHint(this.mContext.getString(R.string.cmei_code_illegal, getCteiStartWith()));
            this.mTipsView.setText(this.mContext.getString(R.string.cmei_code_input_title, getCteiStartWith()));
        } else if (!this.mHasDoubleCheck) {
            this.mLastInput = str;
            this.mHasDoubleCheck = true;
            this.mTipsView.setText(this.mContext.getString(R.string.cmei_input_confirm_again_tips) + str);
            this.cmeiInput.setHint(this.mContext.getString(R.string.cmei_input_confirm_again_tips));
        } else {
            if (this.mLastInput.equals(str)) {
                this.mHasDoubleCheck = false;
                c.n.a.S(str);
                ((d.f.a.j2.e.e) this.mViewModel).f2607h.i(str);
                setEnable((d.f.a.j2.e.e) this.mViewModel, false);
                c.c().g(new ActivityEvent.ShowToast(R.string.cmei_done));
                c.c().g(new ViewEvent.RefreshView());
                return true;
            }
            this.mLastInput = "";
            this.mHasDoubleCheck = false;
            this.mTipsView.setText(this.mContext.getString(R.string.cmei_code_input_title, getCteiStartWith()));
            this.cmeiInput.setHint(this.mContext.getString(R.string.cmei_input_not_match_tips));
        }
        Handler handler = this.mHandler;
        final CustomEditText customEditText = this.cmeiInput;
        Objects.requireNonNull(customEditText);
        handler.postDelayed(new Runnable() { // from class: d.f.a.p1.j
            @Override // java.lang.Runnable
            public final void run() {
                customEditText.requestFocus();
            }
        }, 200L);
        return false;
    }

    private void showCMEICodeInputDialog() {
        showCMEICodeInputDialog(null);
    }

    private void showSoftInput(View view, int i2) {
        if (this.inputMethodManager == null) {
            this.inputMethodManager = (InputMethodManager) this.mContext.getSystemService("input_method");
        }
        if (1 == i2) {
            this.inputMethodManager.showSoftInput(view, 1);
        } else if (i2 == 0) {
            this.inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }

    private void updateDialog(final EditText editText) {
        String strE = c.n.a.E();
        if (h3.q(strE)) {
            this.mHandler.postDelayed(new Runnable() { // from class: d.f.a.p1.l3
                @Override // java.lang.Runnable
                public final void run() {
                    this.f2804b.p(editText);
                }
            }, 200L);
            return;
        }
        editText.setText(strE);
        editText.setEnabled(false);
        this.mTipsView.setText(R.string.cmei_has_input_cant_redo);
        this.mDialogView.findViewById(R.id.confirm_button).setVisibility(8);
    }

    @Override // com.ifpdos.debugmenu.action.base.ItemButtonAction
    public String getValue(Context context) {
        String strE = c.n.a.E();
        if (strE.isEmpty()) {
            return null;
        }
        return strE;
    }

    public /* synthetic */ void h() {
        d.b.a.a.a.X(c.c());
        showSoftInput(this.cmeiInput, 0);
        e eVar = this.actionFinishCall;
        if (eVar != null) {
            eVar.call();
        }
    }

    public /* synthetic */ void i(EditText editText, e eVar) {
        if (setCMEICode(editText.getText().toString().trim())) {
            eVar.call();
        }
    }

    @Override // com.ifpdos.debugmenu.action.base.ItemButtonAction
    public boolean isEnable() {
        return c.n.a.E().isEmpty();
    }

    public /* synthetic */ void j() {
        updateDialog(this.cmeiInput);
        this.mHandler.postDelayed(this.mDismissDialog, 1000L);
    }

    public /* synthetic */ boolean k(TextView textView, int i2, KeyEvent keyEvent) {
        performConfirm(this.cmeiInput, new e() { // from class: d.f.a.p1.h3
            @Override // d.f.a.s1.e
            public final void call() {
                this.a.j();
            }
        });
        return false;
    }

    public /* synthetic */ void l() {
        updateDialog(this.cmeiInput);
        this.mHandler.postDelayed(this.mDismissDialog, 1000L);
    }

    public /* synthetic */ void m(View view) {
        performConfirm(this.cmeiInput, new e() { // from class: d.f.a.p1.c3
            @Override // d.f.a.s1.e
            public final void call() {
                this.a.l();
            }
        });
    }

    public /* synthetic */ void n(View view) {
        this.mHandler.post(this.mDismissDialog);
    }

    public /* synthetic */ void o(DialogInterface dialogInterface) {
        this.actionFinishCall.call();
    }

    @Override // com.ifpdos.debugmenu.action.base.ItemButtonAction, d.f.a.j2.e.e.a
    public void onClick(Context context, d.f.a.j2.e.e eVar) {
        showCMEICodeInputDialog();
    }

    /* JADX WARN: Multi-variable type inference failed */
    @j(threadMode = ThreadMode.MAIN)
    public void onEvent(ViewEvent.RefreshCMEICode refreshCMEICode) {
        Log.e(TAG, "onEvent: 清空CMEI码");
        ((d.f.a.j2.e.e) this.mViewModel).f2607h.i("");
        setEnable((d.f.a.j2.e.e) this.mViewModel, true);
    }

    @Override // com.ifpdos.debugmenu.action.base.ItemButtonAction, d.f.a.j2.e.e.a
    public void onLongClick(Context context, d.f.a.j2.e.e eVar) {
    }

    @u(j.a.ON_STOP)
    public void onResume() {
        Log.i(TAG, "ON_STOP");
        this.mHandler.removeCallbacksAndMessages(null);
    }

    public /* synthetic */ void p(EditText editText) {
        showSoftInput(editText, 1);
    }

    @Override // d.f.a.c1
    public void quickAction(e eVar) {
        showCMEICodeInputDialog(eVar);
    }

    @SuppressLint({"InflateParams"})
    private void showCMEICodeInputDialog(e eVar) {
        this.mLastInput = "";
        this.mHasDoubleCheck = false;
        this.actionFinishCall = eVar;
        View viewInflate = LayoutInflater.from(this.mContext).inflate(R.layout.cmei_code_dialog, (ViewGroup) null, false);
        this.mDialogView = viewInflate;
        CustomEditText customEditText = (CustomEditText) viewInflate.findViewById(R.id.edit_text);
        this.cmeiInput = customEditText;
        customEditText.setHint(this.mContext.getString(R.string.cmei_code_input_title, getCteiStartWith()));
        this.mTipsView = (TextView) this.mDialogView.findViewById(R.id.tips_view);
        this.cmeiInput.setFilters(new InputFilter[]{this.codeInputFilter, this.lengthFilter});
        this.cmeiInput.setSingleLine();
        this.cmeiInput.setImeOptions(6);
        this.cmeiInput.setOnEditorActionListener(new TextView.OnEditorActionListener() { // from class: d.f.a.p1.f3
            @Override // android.widget.TextView.OnEditorActionListener
            public final boolean onEditorAction(TextView textView, int i2, KeyEvent keyEvent) {
                this.f2730b.k(textView, i2, keyEvent);
                return false;
            }
        });
        this.cmeiInput.addTextChangedListener(new TextWatcher() { // from class: com.ifpdos.debugmenu.action.InputCMEICodeAction.1
            @Override // android.text.TextWatcher
            public void afterTextChanged(Editable editable) {
            }

            @Override // android.text.TextWatcher
            public void beforeTextChanged(CharSequence charSequence, int i2, int i3, int i4) {
            }

            @Override // android.text.TextWatcher
            @SuppressLint({"SetTextI18n"})
            public void onTextChanged(CharSequence charSequence, int i2, int i3, int i4) {
                InputCMEICodeAction.this.mTipsView.setText(InputCMEICodeAction.this.mContext.getString(R.string.cmei_length, InputCMEICodeAction.this.getCurrentInput(charSequence.toString()), Integer.valueOf(InputCMEICodeAction.this.cmeiInput.length())));
            }
        });
        this.mDialogView.findViewById(R.id.confirm_button).setOnClickListener(new View.OnClickListener() { // from class: d.f.a.p1.i3
            @Override // android.view.View.OnClickListener
            public final void onClick(View view) {
                this.f2767b.m(view);
            }
        });
        this.mDialogView.findViewById(R.id.cancel_button).setOnClickListener(new View.OnClickListener() { // from class: d.f.a.p1.g3
            @Override // android.view.View.OnClickListener
            public final void onClick(View view) {
                this.f2743b.n(view);
            }
        });
        updateDialog(this.cmeiInput);
        c.c().g(new ActivityEvent.ShowNormalDialog(this.mDialogView, new DialogInterface.OnCancelListener() { // from class: d.f.a.p1.j3
            @Override // android.content.DialogInterface.OnCancelListener
            public final void onCancel(DialogInterface dialogInterface) {
                this.f2781b.o(dialogInterface);
            }
        }));
    }

    /* JADX WARN: Can't rename method to resolve collision */
    @Override // com.ifpdos.debugmenu.action.base.ItemButtonAction, com.ifpdos.debugmenu.action.base.AbstractItemAction
    public void onAttachViewModel(n nVar, d.f.a.j2.e.e eVar) {
        super.onAttachViewModel(nVar, eVar);
        nVar.getLifecycle().a(this);
    }

    @Override // com.ifpdos.debugmenu.action.base.ItemButtonAction, com.ifpdos.debugmenu.action.base.AbstractItemAction, d.f.a.j2.e.d.a
    public void onBind(Context context, d.f.a.j2.e.e eVar) {
        super.onBind(context, eVar);
        eVar.f2608i.i(context.getString(R.string.str_factory_intput_cmei_code_hint));
    }
}
