Allow copying from STK edit box to android edit text

This commit is contained in:
Benau
2019-05-25 22:19:15 +08:00
parent e1a7901c4c
commit 34e680bfbd
5 changed files with 164 additions and 72 deletions

View File

@@ -17,6 +17,11 @@ public class STKEditText extends EditText
private int m_composing_end;
STKInputConnection m_stk_input_connection;
/* Used to avoid infinite calling updateSTKEditBox if setText currently
* by jni. */
private boolean m_from_stk_editbox;
// ------------------------------------------------------------------------
private native static void editText2STKEditbox(String full_text, int start,
int end, int composing_start,
@@ -28,31 +33,29 @@ public class STKEditText extends EditText
setFocusableInTouchMode(true);
m_composing_start = 0;
m_composing_end = 0;
m_from_stk_editbox = false;
m_stk_input_connection = null;
}
// ------------------------------------------------------------------------
@Override
public InputConnection onCreateInputConnection(EditorInfo out_attrs)
{
STKInputConnection sic =
new STKInputConnection(super.onCreateInputConnection(out_attrs), this);
if (m_stk_input_connection == null)
{
m_stk_input_connection = new STKInputConnection(
super.onCreateInputConnection(out_attrs), this);
}
out_attrs.actionLabel = null;
out_attrs.inputType = InputType.TYPE_CLASS_TEXT;
out_attrs.imeOptions = EditorInfo.IME_ACTION_NEXT |
EditorInfo.IME_FLAG_NO_FULLSCREEN |
EditorInfo.IME_FLAG_NO_EXTRACT_UI;
return sic;
return m_stk_input_connection;
}
// ------------------------------------------------------------------------
@Override
public boolean onCheckIsTextEditor() { return true; }
// ------------------------------------------------------------------------
public void resetWhenFocus()
{
clearComposingText();
getText().clear();
m_composing_start = m_composing_end = 0;
}
// ------------------------------------------------------------------------
public void setComposingRegion(int start, int end)
{
// From doc of InputConnectionWrapper, it says:
@@ -71,7 +74,7 @@ public class STKEditText extends EditText
// ------------------------------------------------------------------------
public void updateSTKEditBox()
{
if (!isFocused())
if (!isFocused() || m_from_stk_editbox)
return;
editText2STKEditbox(getText().toString(), getSelectionStart(),
getSelectionEnd(), m_composing_start, m_composing_end);
@@ -82,4 +85,14 @@ public class STKEditText extends EditText
clearFocus();
setVisibility(View.GONE);
}
// ------------------------------------------------------------------------
public void setTextFromSTK(final String text)
{
m_from_stk_editbox = true;
super.setText(text);
m_from_stk_editbox = false;
}
// ------------------------------------------------------------------------
public STKInputConnection getSTKInputConnection()
{ return m_stk_input_connection; }
}

View File

@@ -31,6 +31,9 @@ public class SuperTuxKartActivity extends NativeActivity
InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm == null)
return;
imm.hideSoftInputFromWindow(m_stk_edittext.getWindowToken(), 0);
}
// ------------------------------------------------------------------------
@@ -47,6 +50,34 @@ public class SuperTuxKartActivity extends NativeActivity
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
}
// ------------------------------------------------------------------------
private void createSTKEditText()
{
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT);
m_stk_edittext = new STKEditText(this);
// For some copy-and-paste text are not done by commitText in
// STKInputConnection, so we need an extra watcher
m_stk_edittext.addTextChangedListener(new TextWatcher()
{
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {}
@Override
public void afterTextChanged(Editable edit)
{
if (m_stk_edittext != null)
m_stk_edittext.updateSTKEditBox();
}
});
addContentView(m_stk_edittext, params);
// Only focus it and make visible when soft keybord is opened
m_stk_edittext.setVisibility(View.GONE);
}
// ------------------------------------------------------------------------
@Override
public void onCreate(Bundle instance)
{
@@ -120,38 +151,12 @@ public class SuperTuxKartActivity extends NativeActivity
{
InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm == null)
return;
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT);
if (m_stk_edittext == null)
{
m_stk_edittext = new STKEditText(context);
// For some copy-and-paste text are not done by commitText
// in STKInputConnection, so we need an extra watcher
m_stk_edittext.addTextChangedListener(new TextWatcher()
{
@Override
public void onTextChanged(CharSequence s,
int start, int before,
int count) {}
@Override
public void beforeTextChanged(CharSequence s,
int start, int count,
int after) {}
@Override
public void afterTextChanged(Editable edit)
{
if (m_stk_edittext != null)
m_stk_edittext.updateSTKEditBox();
}
});
addContentView(m_stk_edittext, params);
}
else
m_stk_edittext.setLayoutParams(params);
createSTKEditText();
m_stk_edittext.resetWhenFocus();
m_stk_edittext.setVisibility(View.VISIBLE);
m_stk_edittext.requestFocus();
@@ -173,4 +178,24 @@ public class SuperTuxKartActivity extends NativeActivity
}
});
}
// ------------------------------------------------------------------------
/* Called by STK in JNI. */
public void fromSTKEditBox(final String text, final int selection_start,
final int selection_end)
{
runOnUiThread(new Runnable()
{
@Override
public void run()
{
if (m_stk_edittext == null)
createSTKEditText();
m_stk_edittext.setTextFromSTK(text);
STKInputConnection ic = m_stk_edittext.getSTKInputConnection();
if (ic == null)
return;
ic.setSelection(selection_start, selection_end);
}
});
}
}