package com.basic.security.fragment; import android.os.Bundle; import android.text.TextUtils; import android.util.Log; import android.view.GestureDetector; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.EditText; import android.widget.TextView; import com.basic.security.base.BaseFragment; import com.basic.security.manager.BaseSettingManager; import com.basic.security.utils.ResolutionAdaptation; import com.basic.security.utils.ToastUtil; import com.basic.security.utils.socket.RelayServerUtil2; import org.androidannotations.annotations.AfterViews; import org.androidannotations.annotations.Click; import org.androidannotations.annotations.EFragment; import org.androidannotations.annotations.UiThread; import org.androidannotations.annotations.ViewById; @EFragment public class PasswordOpenDoorFragment extends BaseFragment { public static long showKeyboardTime = 0; @ViewById public TextView textView; @ViewById public View keyboard; @ViewById public EditText password; public GestureDetector mDetector; public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(ResolutionAdaptation.fragment_password_open_door(), container, false); } @Click public void kEnter() { String passwordStr = password.getText().toString(); if (TextUtils.isEmpty(passwordStr)) { ToastUtil.show("密码不能为空"); return; } String openDoorPassword = BaseSettingManager.getOpenDoorPassword(); if (!passwordStr.equals(openDoorPassword)) { ToastUtil.show("密码不正确"); password.setText(""); return; } if (BaseSettingManager.getOpenDoorPasswordEnable()) { ToastUtil.show("开门成功"); RelayServerUtil2.open(); } hideKeyboard(); } @Click public void k0() { PasswordOpenDoorFragment.showKeyboardTime = System.currentTimeMillis(); password.setText(password.getText().toString() + "0"); } @Click public void k1() { PasswordOpenDoorFragment.showKeyboardTime = System.currentTimeMillis(); password.setText(password.getText().toString() + "1"); } @Click public void k2() { PasswordOpenDoorFragment.showKeyboardTime = System.currentTimeMillis(); password.setText(password.getText().toString() + "2"); } @Click public void k3() { PasswordOpenDoorFragment.showKeyboardTime = System.currentTimeMillis(); password.setText(password.getText().toString() + "3"); } @Click public void k4() { PasswordOpenDoorFragment.showKeyboardTime = System.currentTimeMillis(); password.setText(password.getText().toString() + "4"); } @Click public void k5() { PasswordOpenDoorFragment.showKeyboardTime = System.currentTimeMillis(); password.setText(password.getText().toString() + "5"); } @Click public void k6() { PasswordOpenDoorFragment.showKeyboardTime = System.currentTimeMillis(); password.setText(password.getText().toString() + "6"); } @Click public void k7() { PasswordOpenDoorFragment.showKeyboardTime = System.currentTimeMillis(); password.setText(password.getText().toString() + "7"); } @Click public void k8() { PasswordOpenDoorFragment.showKeyboardTime = System.currentTimeMillis(); password.setText(password.getText().toString() + "8"); } @Click public void k9() { PasswordOpenDoorFragment.showKeyboardTime = System.currentTimeMillis(); password.setText(password.getText().toString() + "9"); } @Click public void kDelete() { PasswordOpenDoorFragment.showKeyboardTime = System.currentTimeMillis(); String passwordStr = password.getText().toString(); if (passwordStr.length() > 0) { passwordStr = passwordStr.substring(0, passwordStr.length() - 1); } password.setText(passwordStr); } @AfterViews public void afterViews() { mainActivity().setupUI(getView()); mDetector = new GestureDetector(mainActivity, new MyGestureListener()); textView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { showKeyboardTime = System.currentTimeMillis(); mainActivity().refreshCurrentFragmentShowTime(); return mDetector.onTouchEvent(motionEvent); // return false; } }); keyboard.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { showKeyboardTime = System.currentTimeMillis(); mainActivity().refreshCurrentFragmentShowTime(); return true; } }); } @UiThread public void showKeyboard() { if (BaseSettingManager.getOpenDoorPasswordEnable()) { showKeyboardTime = System.currentTimeMillis(); keyboard.setVisibility(View.VISIBLE); } } public void hideKeyboardFromTimeout() { if (System.currentTimeMillis() - showKeyboardTime > BaseSettingManager.getBackToHomeInSeconds() * 1000) { hideKeyboard(); } } @UiThread public void hideKeyboard() { try { if (password != null) { password.setText(""); } if (keyboard != null) { keyboard.setVisibility(View.GONE); } } catch (Exception e) { e.printStackTrace(); } } // In the SimpleOnGestureListener subclass you should override // onDown and any other gesture that you want to detect. class MyGestureListener extends GestureDetector.SimpleOnGestureListener { @Override public boolean onDown(MotionEvent event) { Log.d("TAG", "onDown: "); // don't return false here or else none of the other // gestures will work return true; } @Override public boolean onSingleTapConfirmed(MotionEvent e) { hideKeyboard(); //Log.i("TAG", "onSingleTapConfirmed: "); return true; } @Override public void onLongPress(MotionEvent e) { //Log.i("TAG", "onLongPress: "); } @Override public boolean onDoubleTap(MotionEvent e) { // showKeyboard(); //Log.i("TAG", "onDoubleTap: "); return true; } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { //Log.i("TAG", "onScroll: "); return true; } @Override public boolean onFling(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY) { Log.d("TAG", "onFling: "); showKeyboard(); return true; } } }