diff --git a/app/src/main/java/com/termux/app/TermuxActivity.java b/app/src/main/java/com/termux/app/TermuxActivity.java index eaf3d284..93d76625 100644 --- a/app/src/main/java/com/termux/app/TermuxActivity.java +++ b/app/src/main/java/com/termux/app/TermuxActivity.java @@ -245,9 +245,15 @@ public final class TermuxActivity extends Activity implements ServiceConnection editText.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { - String s = editText.getText().toString() + "\n"; - getCurrentTermSession().write(s); - editText.setText(""); + TerminalSession session = getCurrentTermSession(); + if (session != null) { + if (session.isRunning()) { + session.write(editText.getText().toString() + "\n"); + } else { + removeFinishedSession(session); + } + editText.setText(""); + } return true; } }); @@ -846,4 +852,21 @@ public final class TermuxActivity extends Activity implements ServiceConnection mLastToast.show(); } + public void removeFinishedSession(TerminalSession finishedSession) { + // Return pressed with finished session - remove it. + TermuxService service = mTermService; + + int index = service.removeTermSession(finishedSession); + mListViewAdapter.notifyDataSetChanged(); + if (mTermService.getSessions().isEmpty()) { + // There are no sessions to show, so finish the activity. + finish(); + } else { + if (index >= service.getSessions().size()) { + index = service.getSessions().size() - 1; + } + switchToSession(service.getSessions().get(index)); + } + } + } diff --git a/app/src/main/java/com/termux/app/TermuxKeyListener.java b/app/src/main/java/com/termux/app/TermuxKeyListener.java index 46695a29..f76cc3ba 100644 --- a/app/src/main/java/com/termux/app/TermuxKeyListener.java +++ b/app/src/main/java/com/termux/app/TermuxKeyListener.java @@ -55,31 +55,12 @@ public final class TermuxKeyListener implements TerminalKeyListener { mActivity.getDrawer().setDrawerLockMode(copyMode ? DrawerLayout.LOCK_MODE_LOCKED_CLOSED : DrawerLayout.LOCK_MODE_UNLOCKED); } - private void returnOnFinishedSession(TerminalSession currentSession) { - // Return pressed with finished session - remove it. - currentSession.finishIfRunning(); - - TermuxService service = mActivity.mTermService; - - int index = service.removeTermSession(currentSession); - mActivity.mListViewAdapter.notifyDataSetChanged(); - if (mActivity.mTermService.getSessions().isEmpty()) { - // There are no sessions to show, so finish the activity. - mActivity.finish(); - } else { - if (index >= service.getSessions().size()) { - index = service.getSessions().size() - 1; - } - mActivity.switchToSession(service.getSessions().get(index)); - } - } - @Override public boolean onKeyDown(int keyCode, KeyEvent e, TerminalSession currentSession) { if (handleVirtualKeys(keyCode, e, true)) return true; if (keyCode == KeyEvent.KEYCODE_ENTER && !currentSession.isRunning()) { - returnOnFinishedSession(currentSession); + mActivity.removeFinishedSession(currentSession); return true; } else if (e.isCtrlPressed() && e.isShiftPressed()) { // Get the unmodified code point: @@ -244,7 +225,7 @@ public final class TermuxKeyListener implements TerminalKeyListener { return true; } else if (ctrlDown) { if (codePoint == 106 /* Ctrl+j or \n */ && !session.isRunning()) { - returnOnFinishedSession(session); + mActivity.removeFinishedSession(session); return true; }