Skip to content

Commit

Permalink
Fix issues where soft keyboard was not shown in some cases when hardw…
Browse files Browse the repository at this point in the history
…are keyboard was attached v2

This is an update to 4d1851e commit.

The toggle logic change previously was actually being applied to ctrl+alt+k hardware keyboard shortcut instead of the mentioned extra keys "KEYBOARD" toggle. However, now it applies to the extra keys "KEYBOARD" toggle button as well, in addition to  drawer "KEYBOARD" toggle button and ctrl+alt+k hardware keyboard shortcut. They will all behave the same now.

Updated onSingleTapUp() to also forcefully show keyboard.

Fixed issue where "hide-soft-keyboard-on-startup" property wasn't respected anymore due to forced keyboard showing done in 4d1851e.

Removed "stateAlwaysVisible" flag from AndroidManifest since its ignored in Android 10 by default and not needed due to usage of InputMethodManager.showSoftInput(). https://developer.android.com/reference/android/view/WindowManager.LayoutParams#SOFT_INPUT_STATE_ALWAYS_VISIBLE

Moved "adjustResize" from AndroidManifest into java code (which is also deprecated in API 30) to centralize keyboard logic.
  • Loading branch information
agnostic-apollo committed May 9, 2021
1 parent 4d1851e commit 39c69db
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 25 deletions.
3 changes: 1 addition & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@
android:configChanges="orientation|screenSize|smallestScreenSize|density|screenLayout|uiMode|keyboard|keyboardHidden|navigation"
android:label="@string/application_name"
android:launchMode="singleTask"
android:resizeableActivity="true"
android:windowSoftInputMode="adjustResize|stateAlwaysVisible">
android:resizeableActivity="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
13 changes: 7 additions & 6 deletions app/src/main/java/com/termux/app/TermuxActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.autofill.AutofillManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
Expand Down Expand Up @@ -237,7 +236,7 @@ public void onStart() {
public void onResume() {
super.onResume();

mTermuxTerminalViewClient.setSoftKeyboardState();
mTermuxTerminalViewClient.setSoftKeyboardState(true);
}

/**
Expand Down Expand Up @@ -418,7 +417,7 @@ private void setNewSessionButtonView() {

private void setToggleKeyboardView() {
findViewById(R.id.toggle_keyboard_button).setOnClickListener(v -> {
TermuxTerminalViewClient.toggleSoftKeyboard(this);
mTermuxTerminalViewClient.onToggleSoftKeyboardRequest();
getDrawer().closeDrawers();
});

Expand Down Expand Up @@ -447,8 +446,6 @@ private void setTermuxTerminalViewAndClients() {
// Set {@link TerminalView#TERMINAL_VIEW_KEY_LOGGING_ENABLED} value
mTerminalView.setIsTerminalViewKeyLoggingEnabled(mPreferences.getTerminalViewKeyLoggingEnabled());

mTerminalView.requestFocus();

mTermuxTerminalSessionClient.checkForFontAndColors();
}

Expand Down Expand Up @@ -677,6 +674,10 @@ public TerminalView getTerminalView() {
return mTerminalView;
}

public TermuxTerminalViewClient getTermuxTerminalViewClient() {
return mTermuxTerminalViewClient;
}

public TermuxTerminalSessionClient getTermuxTerminalSessionClient() {
return mTermuxTerminalSessionClient;
}
Expand Down Expand Up @@ -767,7 +768,7 @@ private void reloadTermuxActivityStyling() {

setTerminalToolbarHeight();

mTermuxTerminalViewClient.setSoftKeyboardState();
mTermuxTerminalViewClient.setSoftKeyboardState(true);

// To change the activity and drawer theme, activity needs to be recreated.
// But this will destroy the activity, and will call the onCreate() again.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.termux.app.terminal;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.ClipData;
Expand All @@ -15,6 +16,7 @@
import android.view.InputDevice;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.ListView;
Expand All @@ -38,7 +40,6 @@
import com.termux.terminal.KeyHandler;
import com.termux.terminal.TerminalEmulator;
import com.termux.terminal.TerminalSession;
import com.termux.view.TerminalView;

import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -75,8 +76,7 @@ public float onScale(float scale) {

@Override
public void onSingleTapUp(MotionEvent e) {
InputMethodManager mgr = (InputMethodManager) mActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(mActivity.getTerminalView(), InputMethodManager.SHOW_IMPLICIT);
showSoftKeyboard(mActivity, mActivity.getTerminalView());
}

@Override
Expand Down Expand Up @@ -125,7 +125,7 @@ public boolean onKeyDown(int keyCode, KeyEvent e, TerminalSession currentSession
} else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
mActivity.getDrawer().closeDrawers();
} else if (unicodeChar == 'k'/* keyboard */) {
toggleSoftKeyboard(mActivity);
onToggleSoftKeyboardRequest();
} else if (unicodeChar == 'm'/* menu */) {
mActivity.getTerminalView().showContextMenu();
} else if (unicodeChar == 'r'/* rename */) {
Expand Down Expand Up @@ -372,23 +372,83 @@ public static void toggleSoftKeyboard(Context context) {
* check by passing {@code 0} as {@code flags}.
* https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:frameworks/base/core/java/android/inputmethodservice/InputMethodService.java;l=2022
*/
public static void showSoftKeyboard(Context context, TerminalView terminalView) {
public static void showSoftKeyboard(Context context, View view) {
InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(terminalView, 0);
inputMethodManager.showSoftInput(view, 0);
}

public void setSoftKeyboardState() {
// If soft keyboard is to disabled
public static void hideSoftKeyboard(Context context, View view) {
InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

public static void disableSoftKeyboard(Activity activity, View view) {
hideSoftKeyboard(activity, view);
setDisableSoftKeyboardFlags(activity);
}

public static void setDisableSoftKeyboardFlags(Activity activity) {
activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM, WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
}

public static void clearDisableSoftKeyboardFlags(Activity activity) {
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
}

public void setResizeTerminalViewForSoftKeyboardFlags() {
// TODO: The flag is deprecated for API 30 and WindowInset API should be used
// https://developer.android.com/reference/android/view/WindowManager.LayoutParams#SOFT_INPUT_ADJUST_RESIZE
// https://medium.com/androiddevelopers/animating-your-keyboard-fb776a8fb66d
// https://stackoverflow.com/a/65194077/14686958
mActivity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
}

/**
* Called when user requests the soft keyboard to be toggled via "KEYBOARD" toggle button in
* drawer or extra keys, or with ctrl+alt+k hardware keyboard shortcut.
*/
public void onToggleSoftKeyboardRequest() {
// If soft keyboard is disabled by user for Termux
if (!mActivity.getPreferences().getSoftKeyboardEnabled()) {
mActivity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM, WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
disableSoftKeyboard(mActivity, mActivity.getTerminalView());
} else {
mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
showSoftKeyboard(mActivity, mActivity.getTerminalView());
clearDisableSoftKeyboardFlags(mActivity);
toggleSoftKeyboard(mActivity);
}
}

public void setSoftKeyboardState(boolean isStartup) {
// If soft keyboard is disabled by user for Termux
if (!mActivity.getPreferences().getSoftKeyboardEnabled()) {
disableSoftKeyboard(mActivity, mActivity.getTerminalView());
} else {
// Set flag to automatically push up TerminalView when keyboard is opened instead of showing over it
setResizeTerminalViewForSoftKeyboardFlags();

// Clear any previous flags to disable soft keyboard in case setting updated
clearDisableSoftKeyboardFlags(mActivity);

// If soft keyboard is to be hidden on startup
if (isStartup && mActivity.getProperties().shouldSoftKeyboardBeHiddenOnStartup()) {
hideSoftKeyboard(mActivity, mActivity.getTerminalView());
} else {
// Force show soft keyboard
showSoftKeyboard(mActivity, mActivity.getTerminalView());

mActivity.getTerminalView().setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasFocus) {
if (hasFocus) {
showSoftKeyboard(mActivity, mActivity.getTerminalView());
} else {
hideSoftKeyboard(mActivity, mActivity.getTerminalView());
}
}
});

// If soft keyboard is to be hidden on startup
if (mActivity.getProperties().shouldSoftKeyboardBeHiddenOnStartup()) {
mActivity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
// Request focus for TerminalView
mActivity.getTerminalView().requestFocus();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public Object instantiateItem(@NonNull ViewGroup collection, int position) {
if (position == 0) {
layout = inflater.inflate(R.layout.view_terminal_toolbar_extra_keys, collection, false);
ExtraKeysView extraKeysView = (ExtraKeysView) layout;
extraKeysView.setTermuxTerminalViewClient(mActivity.getTermuxTerminalViewClient());
mActivity.setExtraKeysView(extraKeysView);
extraKeysView.reload(mActivity.getProperties().getExtraKeysInfo());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.PopupWindow;

import com.termux.R;
import com.termux.app.terminal.TermuxTerminalViewClient;
import com.termux.view.TerminalView;

import androidx.drawerlayout.widget.DrawerLayout;
Expand All @@ -44,6 +44,8 @@ public final class ExtraKeysView extends GridLayout {
private static final int INTERESTING_COLOR = 0xFF80DEEA;
private static final int BUTTON_PRESSED_COLOR = 0xFF7F7F7F;

TermuxTerminalViewClient mTermuxTerminalViewClient;

public ExtraKeysView(Context context, AttributeSet attrs) {
super(context, attrs);
}
Expand Down Expand Up @@ -82,8 +84,8 @@ public ExtraKeysView(Context context, AttributeSet attrs) {
private void sendKey(View view, String keyName, boolean forceCtrlDown, boolean forceLeftAltDown) {
TerminalView terminalView = view.findViewById(R.id.terminal_view);
if ("KEYBOARD".equals(keyName)) {
InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, 0);
if(mTermuxTerminalViewClient != null)
mTermuxTerminalViewClient.onToggleSoftKeyboardRequest();
} else if ("DRAWER".equals(keyName)) {
DrawerLayout drawer = view.findViewById(R.id.drawer_layout);
drawer.openDrawer(Gravity.LEFT);
Expand Down Expand Up @@ -379,4 +381,8 @@ public void reload(ExtraKeysInfo infos) {
}
}

public void setTermuxTerminalViewClient(TermuxTerminalViewClient termuxTerminalViewClient) {
this.mTermuxTerminalViewClient = termuxTerminalViewClient;
}

}

0 comments on commit 39c69db

Please sign in to comment.