From a73228b1097b9a1cfd162992b5fada3e482a0408 Mon Sep 17 00:00:00 2001 From: Fredrik Fornwall Date: Mon, 1 Aug 2016 06:37:49 +0200 Subject: [PATCH] Fix Enter to finish session in more cases Detect the Enter key to finish a session not only on KeyEvent:s, but also when the IME uses InputConnection.commitText() to send \n. This seems to be triggered more after switching to the Uri input type. Closes #124 Also bump app version for a quick release. --- app/build.gradle | 4 +- .../com/termux/app/TermuxKeyListener.java | 43 ++++++++++++------- 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index f4f6598c..246d9738 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -13,8 +13,8 @@ android { applicationId "com.termux" minSdkVersion 21 targetSdkVersion 24 - versionCode 36 - versionName "0.35" + versionCode 37 + versionName "0.37" ndk { moduleName "libtermux" diff --git a/app/src/main/java/com/termux/app/TermuxKeyListener.java b/app/src/main/java/com/termux/app/TermuxKeyListener.java index 7c609b4d..b98abe53 100644 --- a/app/src/main/java/com/termux/app/TermuxKeyListener.java +++ b/app/src/main/java/com/termux/app/TermuxKeyListener.java @@ -3,6 +3,7 @@ package com.termux.app; import android.content.Context; import android.media.AudioManager; import android.support.v4.widget.DrawerLayout; +import android.util.Log; import android.view.Gravity; import android.view.InputDevice; import android.view.KeyEvent; @@ -54,27 +55,31 @@ 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; - TermuxService service = mActivity.mTermService; - if (keyCode == KeyEvent.KEYCODE_ENTER && !currentSession.isRunning()) { - // Return pressed with finished session - remove it. - currentSession.finishIfRunning(); - - 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)); - } + returnOnFinishedSession(currentSession); return true; } else if (e.isCtrlPressed() && e.isShiftPressed()) { // Get the unmodified code point: @@ -111,6 +116,7 @@ public final class TermuxKeyListener implements TerminalKeyListener { mActivity.changeFontSize(false); } else if (unicodeChar >= '1' && unicodeChar <= '9') { int num = unicodeChar - '1'; + TermuxService service = mActivity.mTermService; if (service.getSessions().size() > num) mActivity.switchToSession(service.getSessions().get(num)); } @@ -237,6 +243,11 @@ public final class TermuxKeyListener implements TerminalKeyListener { } return true; } else if (ctrlDown) { + if (codePoint == 106 /* Ctrl+j or \n */ && !session.isRunning()) { + returnOnFinishedSession(session); + return true; + } + List shortcuts = mActivity.mSettings.shortcuts; if (!shortcuts.isEmpty()) { for (int i = shortcuts.size() - 1; i >= 0; i--) {