Skip to content

Commit

Permalink
Add support for disabling soft keyboard completely with the "soft_key…
Browse files Browse the repository at this point in the history
…board_enabled" SharedPreferences key

Users can toggle the state from Settings -> Keyboard I/O -> Soft Keyboard toggle.

Android phone should also have an internal setting for disabling soft keyboard when a hardware keyboard is connected in Language and Input android settings or from the input mode selection notification, but the above setting will be Termux app specific and will allow soft keyboard to still be shown in other apps.

The `TermuxPreferenceConstants` classes has been updated to `v0.7.0`. Check its Changelog section for info on changes.
  • Loading branch information
agnostic-apollo committed Mar 27, 2021
1 parent 04da4b2 commit 2a8d5e2
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 9 deletions.
18 changes: 18 additions & 0 deletions app/src/main/java/com/termux/app/TermuxActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,13 @@ public void onStart() {
mTerminalView.onScreenUpdated();
}

@Override
public void onResume() {
super.onResume();

setSoftKeyboardState();
}

/**
* Part of the {@link ServiceConnection} interface. The service is bound with
* {@link #bindService(Intent, ServiceConnection, int)} in {@link #onCreate(Bundle)} which will cause a call to this
Expand Down Expand Up @@ -414,6 +421,15 @@ private void setToggleKeyboardView() {
toggleTerminalToolbar();
return true;
});
}

private void setSoftKeyboardState() {
// If soft keyboard is to disabled
if(!mPreferences.getSoftKeyboardEnabled()) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM, WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
} else {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
}

// If soft keyboard is to be hidden on startup
if(mProperties.shouldSoftKeyboardBeHiddenOnStartup()) {
Expand Down Expand Up @@ -723,6 +739,8 @@ public void onReceive(Context context, Intent intent) {

setTerminalToolbarHeight();

setSoftKeyboardState();

// To change the activity and drawer theme, activity needs to be recreated.
// But this will destroy the activity, and will call the onCreate() again.
// We need to investigate if enabling this is wise, since all stored variables and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.termux.app.utils.Logger;

public class DebuggingPreferencesFragment extends PreferenceFragmentCompat {

@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
PreferenceManager preferenceManager = getPreferenceManager();
Expand Down Expand Up @@ -126,4 +127,5 @@ public boolean getBoolean(String key, boolean defValue) {
return false;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.termux.app.fragments.settings;

import android.content.Context;
import android.os.Bundle;

import androidx.annotation.Nullable;

import androidx.preference.PreferenceDataStore;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceManager;

import com.termux.R;
import com.termux.app.settings.preferences.TermuxAppSharedPreferences;

public class TerminalIOPreferencesFragment extends PreferenceFragmentCompat {

@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
PreferenceManager preferenceManager = getPreferenceManager();
preferenceManager.setPreferenceDataStore(TerminalIOPreferencesDataStore.getInstance(getContext()));

setPreferencesFromResource(R.xml.terminal_io_preferences, rootKey);
}

}

class TerminalIOPreferencesDataStore extends PreferenceDataStore {

private final Context mContext;
private final TermuxAppSharedPreferences mPreferences;

private static TerminalIOPreferencesDataStore mInstance;

private TerminalIOPreferencesDataStore(Context context) {
mContext = context;
mPreferences = new TermuxAppSharedPreferences(context);
}

public static synchronized TerminalIOPreferencesDataStore getInstance(Context context) {
if (mInstance == null) {
mInstance = new TerminalIOPreferencesDataStore(context.getApplicationContext());
}
return mInstance;
}



@Override
public void putBoolean(String key, boolean value) {
if(key == null) return;

switch (key) {
case "soft_keyboard_enabled":
mPreferences.setSoftKeyboardEnabled(value);
break;
default:
break;
}
}

@Override
public boolean getBoolean(String key, boolean defValue) {
switch (key) {
case "soft_keyboard_enabled":
return mPreferences.getSoftKeyboardEnabled();
default:
return false;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ public boolean toogleShowTerminalToolbar() {



public boolean getSoftKeyboardEnabled() {
return SharedPreferenceUtils.getBoolean(mSharedPreferences, TERMUX_APP.KEY_SOFT_KEYBOARD_ENABLED, TERMUX_APP.DEFAULT_VALUE_KEY_SOFT_KEYBOARD_ENABLED);
}

public void setSoftKeyboardEnabled(boolean value) {
SharedPreferenceUtils.setBoolean(mSharedPreferences, TERMUX_APP.KEY_SOFT_KEYBOARD_ENABLED, value, false);
}



public boolean getKeepScreenOn() {
return SharedPreferenceUtils.getBoolean(mSharedPreferences, TERMUX_APP.KEY_KEEP_SCREEN_ON, TERMUX_APP.DEFAULT_VALUE_KEEP_SCREEN_ON);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.termux.app.settings.preferences;

/*
* Version: v0.6.0
* Version: v0.7.0
*
* Changelog
*
Expand Down Expand Up @@ -29,6 +29,10 @@
*
* - 0.6.0 (2021-03-24)
* - Change `DEFAULT_VALUE_KEEP_SCREEN_ON` value to `false` in `TERMUX_APP`.
*
* - 0.7.0 (2021-03-27)
* - Added following to `TERMUX_APP`:
* `KEY_SOFT_KEYBOARD_ENABLED` and `DEFAULT_VALUE_KEY_SOFT_KEYBOARD_ENABLED`.
*/

/**
Expand All @@ -46,39 +50,47 @@ public final class TermuxPreferenceConstants {
public static final class TERMUX_APP {

/**
* Defines the key for whether to show terminal toolbar containing extra keys and text input field
* Defines the key for whether to show terminal toolbar containing extra keys and text input field.
*/
public static final String KEY_SHOW_TERMINAL_TOOLBAR = "show_extra_keys";
public static final boolean DEFAULT_VALUE_SHOW_TERMINAL_TOOLBAR = true;


/**
* Defines the key for whether to always keep screen on
* Defines the key for whether the soft keyboard will be enabled, for cases where users want
* to use a hardware keyboard instead.
*/
public static final String KEY_SOFT_KEYBOARD_ENABLED = "soft_keyboard_enabled";
public static final boolean DEFAULT_VALUE_KEY_SOFT_KEYBOARD_ENABLED = true;


/**
* Defines the key for whether to always keep screen on.
*/
public static final String KEY_KEEP_SCREEN_ON = "screen_always_on";
public static final boolean DEFAULT_VALUE_KEEP_SCREEN_ON = false;


/**
* Defines the key for font size of termux terminal view
* Defines the key for font size of termux terminal view.
*/
public static final String KEY_FONTSIZE = "fontsize";


/**
* Defines the key for current termux terminal session
* Defines the key for current termux terminal session.
*/
public static final String KEY_CURRENT_SESSION = "current_session";


/**
* Defines the key for current termux log level
* Defines the key for current termux log level.
*/
public static final String KEY_LOG_LEVEL = "log_level";


/**
* Defines the key for last used notification id
* Defines the key for last used notification id.
*/
public static final String KEY_LAST_NOTIFICATION_ID = "last_notification_id";
public static final int DEFAULT_VALUE_KEY_LAST_NOTIFICATION_ID = 0;
Expand All @@ -91,7 +103,7 @@ public static final class TERMUX_APP {
public static final boolean DEFAULT_VALUE_TERMINAL_VIEW_KEY_LOGGING_ENABLED = false;

/**
* Defines the key for whether flashes and notifications for plugin errors are enabled or not
* Defines the key for whether flashes and notifications for plugin errors are enabled or not.
*/
public static final String KEY_PLUGIN_ERROR_NOTIFICATIONS_ENABLED = "plugin_error_notifications_enabled";
public static final boolean DEFAULT_VALUE_PLUGIN_ERROR_NOTIFICATIONS_ENABLED = true;
Expand All @@ -104,7 +116,7 @@ public static final class TERMUX_APP {
public static final class TERMUX_TASKER_APP {

/**
* Defines the key for current termux log level
* Defines the key for current termux log level.
*/
public static final String KEY_LOG_LEVEL = "log_level";

Expand Down
13 changes: 13 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,17 @@
<string name="plugin_error_notifications_off">Disable flashes and notifications for plugin errors.</string>
<string name="plugin_error_notifications_on">Show flashes and notifications for plugin errors. (Default)</string>


<!-- Terminal IO Preferences -->
<string name="terminal_io_preferences">Terminal I/O</string>

<!-- Keyboard Category -->
<string name="keyboard_header">Keyboard</string>

<!-- Soft Keyboard -->
<string name="soft_keyboard_title">Soft Keyboard</string>
<string name="soft_keyboard_off">Soft keyboard will be disabled.</string>
<string name="soft_keyboard_on">Soft keyboard will be enabled. (Default)</string>


</resources>
5 changes: 5 additions & 0 deletions app/src/main/res/xml/root_preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@
app:summary="Preferences for debugging"
app:fragment="com.termux.app.fragments.settings.DebuggingPreferencesFragment"/>

<Preference
app:title="@string/terminal_io_preferences"
app:summary="Preferences for terminal I/O"
app:fragment="com.termux.app.fragments.settings.TerminalIOPreferencesFragment"/>

</PreferenceScreen>
15 changes: 15 additions & 0 deletions app/src/main/res/xml/terminal_io_preferences.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto">

<PreferenceCategory
app:key="keyboard"
app:title="@string/keyboard_header">

<SwitchPreferenceCompat
app:key="soft_keyboard_enabled"
app:summaryOff="@string/soft_keyboard_off"
app:summaryOn="@string/soft_keyboard_on"
app:title="@string/soft_keyboard_title" />

</PreferenceCategory>

</PreferenceScreen>

0 comments on commit 2a8d5e2

Please sign in to comment.