package com.me;

import android.inputmethodservice.KeyboardView;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.common.listener.ReceiveDataListener;
import com.common.listener.ReceiveDataListenerManager;
import com.common.uitl.SharePersistent;
import com.home.base.LedBleActivity;
import com.home.base.LedBleApplication;
import com.home.constant.Constant;
import com.home.net.NetConnectBle;
import com.home.view.custom.keyboard.KeyboardUtil;
import com.ledlamp.R;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/* loaded from: classes.dex */
public class PasswordActivity extends LedBleActivity implements View.OnClickListener, ReceiveDataListener, KeyboardUtil.TextChangeListener {
    private static final String TAG = "PasswordActivity";
    private Button btnConfirm;
    private FrameLayout flKeyboardView;
    private String inputTextString = "";
    private ImageView ivBack;
    private KeyboardUtil keyboardUtil;
    private KeyboardView keyboard_view;
    private LinearLayout llPassword;
    private LinearLayout llTVPW;
    private TextView tvPW1;
    private TextView tvPW2;
    private TextView tvPW3;
    private TextView tvPW4;
    private TextView tvPW5;
    private TextView tvPW6;
    private TextView tvPW7;
    private TextView tvPW8;
    private TextView tvTitle;

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // com.home.base.LedBleActivity, me.imid.swipebacklayout.lib.app.SwipeBackActivity, androidx.appcompat.app.AppCompatActivity, androidx.fragment.app.FragmentActivity, androidx.core.app.ComponentActivity, android.app.Activity
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        Window window = getWindow();
        window.addFlags(Integer.MIN_VALUE);
        window.setStatusBarColor(getResources().getColor(R.color.blue));
        getWindow().setSoftInputMode(3);
        setContentView(R.layout.activity_password);
        ReceiveDataListenerManager.getInstance().registerListtener(this);
        this.tvPW1 = (TextView) findViewById(R.id.tvPW1);
        this.tvPW2 = (TextView) findViewById(R.id.tvPW2);
        this.tvPW3 = (TextView) findViewById(R.id.tvPW3);
        this.tvPW4 = (TextView) findViewById(R.id.tvPW4);
        this.tvPW5 = (TextView) findViewById(R.id.tvPW5);
        this.tvPW6 = (TextView) findViewById(R.id.tvPW6);
        this.tvPW7 = (TextView) findViewById(R.id.tvPW7);
        this.tvPW8 = (TextView) findViewById(R.id.tvPW8);
        LinearLayout linearLayout = (LinearLayout) findViewById(R.id.llTVPW);
        this.llTVPW = linearLayout;
        linearLayout.setOnClickListener(new View.OnClickListener() { // from class: com.me.PasswordActivity.1
            @Override // android.view.View.OnClickListener
            public void onClick(View view) {
                PasswordActivity.this.tvPW1.setBackground(PasswordActivity.this.getDrawable(R.drawable.border_blue));
                if (PasswordActivity.this.keyboardUtil != null && PasswordActivity.this.keyboardUtil.isShow()) {
                    PasswordActivity.this.flKeyboardView.setVisibility(0);
                } else {
                    if (PasswordActivity.this.keyboardUtil.isShow()) {
                        return;
                    }
                    PasswordActivity.this.keyboardUtil.showKeyboard();
                }
            }
        });
        LinearLayout linearLayout2 = (LinearLayout) findViewById(R.id.llPassword);
        this.llPassword = linearLayout2;
        linearLayout2.setOnClickListener(new View.OnClickListener() { // from class: com.me.PasswordActivity.2
            @Override // android.view.View.OnClickListener
            public void onClick(View view) {
                if (PasswordActivity.this.keyboardUtil == null || !PasswordActivity.this.keyboardUtil.isShow()) {
                    return;
                }
                PasswordActivity.this.flKeyboardView.setVisibility(4);
            }
        });
        this.keyboard_view = (KeyboardView) findViewById(R.id.keyboard_view);
        this.flKeyboardView = (FrameLayout) findViewById(R.id.flKeyboardView);
        this.ivBack = (ImageView) findViewById(R.id.ivBack);
        this.btnConfirm = (Button) findViewById(R.id.btnConfirm);
        TextView textView = (TextView) findViewById(R.id.tvTitle);
        this.tvTitle = textView;
        textView.setText(getString(R.string.password));
        this.ivBack.setOnClickListener(this);
        this.btnConfirm.setOnClickListener(this);
        initKeyboard(this.keyboard_view);
        String perference = SharePersistent.getPerference(this, Constant.PasswordSet);
        if (!perference.equalsIgnoreCase("")) {
            this.inputTextString = perference;
            this.tvPW1.setBackground(getDrawable(R.drawable.border_blue));
            this.tvPW1.setText(perference.substring(0, 1));
            this.tvPW2.setBackground(getDrawable(R.drawable.border_blue));
            this.tvPW2.setText(perference.substring(1, 2));
            this.tvPW3.setBackground(getDrawable(R.drawable.border_blue));
            this.tvPW3.setText(perference.substring(2, 3));
            this.tvPW4.setBackground(getDrawable(R.drawable.border_blue));
            this.tvPW4.setText(perference.substring(3, 4));
            this.tvPW5.setBackground(getDrawable(R.drawable.border_blue));
            this.tvPW5.setText(perference.substring(4, 5));
            this.tvPW6.setBackground(getDrawable(R.drawable.border_blue));
            this.tvPW6.setText(perference.substring(5, 6));
            this.tvPW7.setBackground(getDrawable(R.drawable.border_blue));
            this.tvPW7.setText(perference.substring(6, 7));
            this.tvPW8.setBackground(getDrawable(R.drawable.border_blue));
            this.tvPW8.setText(perference.substring(7, 8));
            return;
        }
        this.inputTextString = "A1234567";
        SharePersistent.savePerference(this, Constant.PasswordSet, "A1234567");
    }

    private void initKeyboard(View view) {
        KeyboardUtil keyboardUtil = new KeyboardUtil(getApplicationContext(), view, 8);
        this.keyboardUtil = keyboardUtil;
        keyboardUtil.showKeyboard();
        this.keyboardUtil.setOnTextChangeListener(this);
    }

    @Override // com.home.view.custom.keyboard.KeyboardUtil.TextChangeListener
    public void addText(int i, String str) {
        if (this.inputTextString.length() == 8) {
            return;
        }
        Log.e(TAG, "sub ======== " + str);
        if (this.inputTextString.isEmpty()) {
            this.tvPW1.setBackground(getDrawable(R.drawable.border_blue));
            this.tvPW1.setText("" + str);
        } else if (this.inputTextString.length() == 1) {
            this.tvPW2.setBackground(getDrawable(R.drawable.border_blue));
            this.tvPW2.setText("" + str);
        } else if (this.inputTextString.length() == 2) {
            this.tvPW3.setBackground(getDrawable(R.drawable.border_blue));
            this.tvPW3.setText("" + str);
        } else if (this.inputTextString.length() == 3) {
            this.tvPW4.setBackground(getDrawable(R.drawable.border_blue));
            this.tvPW4.setText("" + str);
        } else if (this.inputTextString.length() == 4) {
            this.tvPW5.setBackground(getDrawable(R.drawable.border_blue));
            this.tvPW5.setText("" + str);
        } else if (this.inputTextString.length() == 5) {
            this.tvPW6.setBackground(getDrawable(R.drawable.border_blue));
            this.tvPW6.setText("" + str);
        } else if (this.inputTextString.length() == 6) {
            this.tvPW7.setBackground(getDrawable(R.drawable.border_blue));
            this.tvPW7.setText("" + str);
        } else if (this.inputTextString.length() == 7) {
            this.tvPW8.setBackground(getDrawable(R.drawable.border_blue));
            this.tvPW8.setText("" + str);
        }
        this.inputTextString += str;
    }

    @Override // com.home.view.custom.keyboard.KeyboardUtil.TextChangeListener
    public void delText(int i) {
        if (this.inputTextString.length() == 1) {
            this.tvPW1.setBackground(getDrawable(R.drawable.border_gray));
            this.tvPW1.setText("");
            this.inputTextString = removeCharAt(this.inputTextString, 0);
        } else if (this.inputTextString.length() == 2) {
            this.tvPW2.setBackground(getDrawable(R.drawable.border_gray));
            this.tvPW2.setText("");
            this.inputTextString = removeCharAt(this.inputTextString, 1);
        } else if (this.inputTextString.length() == 3) {
            this.tvPW3.setBackground(getDrawable(R.drawable.border_gray));
            this.tvPW3.setText("");
            this.inputTextString = removeCharAt(this.inputTextString, 2);
        } else if (this.inputTextString.length() == 4) {
            this.tvPW4.setBackground(getDrawable(R.drawable.border_gray));
            this.tvPW4.setText("");
            this.inputTextString = removeCharAt(this.inputTextString, 3);
        } else if (this.inputTextString.length() == 5) {
            this.tvPW5.setBackground(getDrawable(R.drawable.border_gray));
            this.tvPW5.setText("");
            this.inputTextString = removeCharAt(this.inputTextString, 4);
        } else if (this.inputTextString.length() == 6) {
            this.tvPW6.setBackground(getDrawable(R.drawable.border_gray));
            this.tvPW6.setText("");
            this.inputTextString = removeCharAt(this.inputTextString, 5);
        } else if (this.inputTextString.length() == 7) {
            this.tvPW7.setBackground(getDrawable(R.drawable.border_gray));
            this.tvPW7.setText("");
            this.inputTextString = removeCharAt(this.inputTextString, 6);
        } else if (this.inputTextString.length() == 8) {
            this.tvPW8.setBackground(getDrawable(R.drawable.border_gray));
            this.tvPW8.setText("");
            this.inputTextString = removeCharAt(this.inputTextString, 7);
        }
        Log.e(TAG, "inputTextString ======== " + this.inputTextString);
    }

    @Override // com.home.view.custom.keyboard.KeyboardUtil.TextChangeListener
    public void clearText() {
        Log.e(TAG, "clearText ======== ");
    }

    public String removeCharAt(String str, int i) {
        if (i <= 0) {
            return "";
        }
        return str.substring(0, i) + str.substring(i + 1);
    }

    @Override // com.home.base.LedBleActivity, me.imid.swipebacklayout.lib.app.SwipeBackActivity, androidx.appcompat.app.AppCompatActivity, androidx.fragment.app.FragmentActivity, android.app.Activity
    public void onDestroy() {
        super.onDestroy();
        ReceiveDataListenerManager.getInstance().unRegisterListener(this);
    }

    @Override // com.common.listener.ReceiveDataListener
    public void notifyReceiveData(String str, String str2) {
        if (!str.equalsIgnoreCase(Constant.PasswordSet) || str2 == null) {
            return;
        }
        if (str2.contains("2A") || str2.contains("2a")) {
            int parseInt = Integer.parseInt(str2.split(" ")[2], 16);
            LedBleApplication.getApp().setSafetyLevel(parseInt);
            if (parseInt == 1) {
                Toast.makeText(this, "" + getResources().getString(R.string.password_error), 1).show();
            } else if (parseInt == 2) {
                Toast.makeText(this, "" + getResources().getString(R.string.The_password_cannot_be_the_ame_number_or_letter), 1).show();
            } else {
                if (parseInt != 3) {
                    return;
                }
                Toast.makeText(this, "" + getResources().getString(R.string.The_system_has_entered_the_breathing_mode), 1).show();
            }
        }
    }

    @Override // android.view.View.OnClickListener
    public void onClick(View view) {
        String str = this.inputTextString;
        int id = view.getId();
        if (id != R.id.btnConfirm) {
            if (id != R.id.ivBack) {
                return;
            }
            SharePersistent.savePerference(this, Constant.Activity, "");
            finish();
            return;
        }
        if (TextUtils.isEmpty(str)) {
            Toast.makeText(this, getString(R.string.input_not_null), 0).show();
            return;
        }
        if (str.length() != 8) {
            Toast.makeText(this, getString(R.string.input_length_error), 0).show();
            return;
        }
        if (str.length() == 8) {
            Toast.makeText(this, getResources().getString(R.string.sent_success), 0).show();
            SharePersistent.savePerference(this, Constant.PasswordSet, str);
            Date date = new Date(System.currentTimeMillis());
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH");
            SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("mm");
            int parseInt = Integer.parseInt(simpleDateFormat.format(date).trim());
            int parseInt2 = Integer.parseInt(simpleDateFormat2.format(date).trim());
            Calendar.getInstance().setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
            int i = (new int[]{7, 1, 2, 3, 4, 5, 6}[Calendar.getInstance().get(7) - 1] << 5) | parseInt;
            if (str != null && str.length() == 8) {
                NetConnectBle.getInstanceByGroup("").setPassword(1, Integer.parseInt(str.substring(0, 2), 16), Integer.parseInt(str.substring(2, 4), 16), Integer.parseInt(str.substring(4, 6), 16), Integer.parseInt(str.substring(6, 8), 16), i, parseInt2);
                return;
            }
            NetConnectBle.getInstanceByGroup("").setPassword(1, 161, 35, 69, 103, i, parseInt2);
        }
    }

    public static boolean isOrderNumeric(String str) {
        String upperCase = str.toUpperCase();
        String substring = upperCase.substring(0, 1);
        return substring.equals(upperCase.substring(1, 2)) && substring.equals(upperCase.substring(2, 3)) && substring.equals(upperCase.substring(3, 4)) && substring.equals(upperCase.substring(4, 5)) && substring.equals(upperCase.substring(5, 6)) && substring.equals(upperCase.substring(6, 7)) && substring.equals(upperCase.substring(7, 8));
    }

    @Override // androidx.appcompat.app.AppCompatActivity, android.app.Activity, android.view.KeyEvent.Callback
    public boolean onKeyDown(int i, KeyEvent keyEvent) {
        if (i == 4) {
            finish();
            return true;
        }
        return super.onKeyDown(i, keyEvent);
    }
}
