diff --git a/cloudphone/src/main/java/com/huawei/cloudphone/apiimpl/CloudPhoneImeMgr.java b/cloudphone/src/main/java/com/huawei/cloudphone/apiimpl/CloudPhoneImeMgr.java index 23ea88c73aa91f1505611e5cace9ec5473688f8b..deb2078e8dfa115eda71bc86c0e2abfe1a8c6ed1 100644 --- a/cloudphone/src/main/java/com/huawei/cloudphone/apiimpl/CloudPhoneImeMgr.java +++ b/cloudphone/src/main/java/com/huawei/cloudphone/apiimpl/CloudPhoneImeMgr.java @@ -10,6 +10,7 @@ import android.text.InputFilter; import android.text.TextWatcher; import android.util.TypedValue; import android.view.Gravity; +import android.view.KeyEvent; import android.view.View; import android.view.ViewGroup; import android.view.WindowManager; @@ -39,7 +40,7 @@ public class CloudPhoneImeMgr { private static final int IME_PASTE_FROM_CLOUD = 6; private static final int IME_PASTE_TO_CLOUD = 7; private static final int IME_MSG_HEADER_LEN = 3; - private static final String IME_CURSOR_SEPARATOR = "="; + private static final int IME_KEY_EVENT_LEN = 2; private ViewGroup mRootViewGroup; private ViewGroup mImeViewGroup; @@ -48,8 +49,6 @@ public class CloudPhoneImeMgr { private PopupWindow mPopWindow; private CloudPhoneTextWatchListener mTextWatchListener; private CloudPhoneClipboardListener mClipboardListener; - private int mCurrentCursor; - private boolean mCursorInEnd; @SuppressLint("Range") public CloudPhoneImeMgr(Context context, ViewGroup viewGroup) { @@ -80,13 +79,9 @@ public class CloudPhoneImeMgr { @Override public void onTextChanged(CharSequence charSequence, int start, int before, int count) { - String text = ""; - int cursorPosition = mImeEditText.getSelectionStart(); - if (!mCursorInEnd|| (mCurrentCursor == before && mCurrentCursor == count)) { - text = charSequence.toString() + IME_CURSOR_SEPARATOR + mCurrentCursor; - mCursorInEnd = true; - } else { - text = charSequence.toString() + IME_CURSOR_SEPARATOR + cursorPosition; + String text = charSequence.toString(); + if (text.isEmpty()) { + return; } byte[] data = text.getBytes(); byte[] msg = new byte[IME_MSG_HEADER_LEN + data.length]; @@ -98,6 +93,7 @@ public class CloudPhoneImeMgr { if (mTextWatchListener != null) { mTextWatchListener.onTextChange(msg); } + mImeEditText.getText().clear(); } @Override @@ -106,6 +102,26 @@ public class CloudPhoneImeMgr { } }); + mImeEditText.setOnKeyListener(new View.OnKeyListener() { + @Override + public boolean onKey(View view, int keyCode, KeyEvent keyEvent) { + if (keyCode == KeyEvent.KEYCODE_DEL && keyEvent.getAction() == KeyEvent.ACTION_DOWN) { + byte[] msg = new byte[IME_MSG_HEADER_LEN + IME_KEY_EVENT_LEN]; + msg[0] = IME_KEY_EVENT; + msg[1] = (byte) ((IME_KEY_EVENT_LEN >> 8) & 0xFF); + msg[2] = (byte) (IME_KEY_EVENT_LEN & 0XFF); + msg[3] = (byte) ((KeyEvent.KEYCODE_DEL >> 8) & 0xFF); + msg[4] = (byte) (KeyEvent.KEYCODE_DEL & 0XFF); + + if (mTextWatchListener != null) { + mTextWatchListener.onTextChange(msg); + } + return true; + } + return false; + } + }); + mPopWindow = new PopupWindow(mImeViewGroup, WindowManager.LayoutParams.MATCH_PARENT, dip2px(BOX_INPUT_HEIGHT), true); mPopWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED); mPopWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); @@ -117,17 +133,7 @@ public class CloudPhoneImeMgr { if (mPopWindow.isShowing()) { return 0; } - if (text != null && text.length() > 0) { - int lastIndex = text.lastIndexOf(IME_CURSOR_SEPARATOR); - String content = text.substring(0 ,lastIndex); - int cursor = Integer.parseInt(text.substring(lastIndex + 1)); - mCurrentCursor = cursor; - mCursorInEnd = cursor == content.length() ? true : false; - mImeEditText.getText().replace(0, mImeEditText.length(), content); - mImeEditText.setSelection(cursor); - } else { - mImeEditText.getText().clear(); - } + mImeEditText.getText().clear(); mPopWindow.setFocusable(true); mImeEditText.requestFocus(); mPopWindow.showAtLocation(mRootViewGroup, Gravity.BOTTOM, 0, 0);