Skip to content

Commit 2a8d5e2

Browse files
Add support for disabling soft keyboard completely with the "soft_keyboard_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.
1 parent 04da4b2 commit 2a8d5e2

8 files changed

+155
-9
lines changed

app/src/main/java/com/termux/app/TermuxActivity.java

+18
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,13 @@ public void onStart() {
228228
mTerminalView.onScreenUpdated();
229229
}
230230

231+
@Override
232+
public void onResume() {
233+
super.onResume();
234+
235+
setSoftKeyboardState();
236+
}
237+
231238
/**
232239
* Part of the {@link ServiceConnection} interface. The service is bound with
233240
* {@link #bindService(Intent, ServiceConnection, int)} in {@link #onCreate(Bundle)} which will cause a call to this
@@ -414,6 +421,15 @@ private void setToggleKeyboardView() {
414421
toggleTerminalToolbar();
415422
return true;
416423
});
424+
}
425+
426+
private void setSoftKeyboardState() {
427+
// If soft keyboard is to disabled
428+
if(!mPreferences.getSoftKeyboardEnabled()) {
429+
getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM, WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
430+
} else {
431+
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
432+
}
417433

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

724740
setTerminalToolbarHeight();
725741

742+
setSoftKeyboardState();
743+
726744
// To change the activity and drawer theme, activity needs to be recreated.
727745
// But this will destroy the activity, and will call the onCreate() again.
728746
// We need to investigate if enabling this is wise, since all stored variables and

app/src/main/java/com/termux/app/fragments/settings/DebuggingPreferencesFragment.java

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import com.termux.app.utils.Logger;
1616

1717
public class DebuggingPreferencesFragment extends PreferenceFragmentCompat {
18+
1819
@Override
1920
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
2021
PreferenceManager preferenceManager = getPreferenceManager();
@@ -126,4 +127,5 @@ public boolean getBoolean(String key, boolean defValue) {
126127
return false;
127128
}
128129
}
130+
129131
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.termux.app.fragments.settings;
2+
3+
import android.content.Context;
4+
import android.os.Bundle;
5+
6+
import androidx.annotation.Nullable;
7+
8+
import androidx.preference.PreferenceDataStore;
9+
import androidx.preference.PreferenceFragmentCompat;
10+
import androidx.preference.PreferenceManager;
11+
12+
import com.termux.R;
13+
import com.termux.app.settings.preferences.TermuxAppSharedPreferences;
14+
15+
public class TerminalIOPreferencesFragment extends PreferenceFragmentCompat {
16+
17+
@Override
18+
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
19+
PreferenceManager preferenceManager = getPreferenceManager();
20+
preferenceManager.setPreferenceDataStore(TerminalIOPreferencesDataStore.getInstance(getContext()));
21+
22+
setPreferencesFromResource(R.xml.terminal_io_preferences, rootKey);
23+
}
24+
25+
}
26+
27+
class TerminalIOPreferencesDataStore extends PreferenceDataStore {
28+
29+
private final Context mContext;
30+
private final TermuxAppSharedPreferences mPreferences;
31+
32+
private static TerminalIOPreferencesDataStore mInstance;
33+
34+
private TerminalIOPreferencesDataStore(Context context) {
35+
mContext = context;
36+
mPreferences = new TermuxAppSharedPreferences(context);
37+
}
38+
39+
public static synchronized TerminalIOPreferencesDataStore getInstance(Context context) {
40+
if (mInstance == null) {
41+
mInstance = new TerminalIOPreferencesDataStore(context.getApplicationContext());
42+
}
43+
return mInstance;
44+
}
45+
46+
47+
48+
@Override
49+
public void putBoolean(String key, boolean value) {
50+
if(key == null) return;
51+
52+
switch (key) {
53+
case "soft_keyboard_enabled":
54+
mPreferences.setSoftKeyboardEnabled(value);
55+
break;
56+
default:
57+
break;
58+
}
59+
}
60+
61+
@Override
62+
public boolean getBoolean(String key, boolean defValue) {
63+
switch (key) {
64+
case "soft_keyboard_enabled":
65+
return mPreferences.getSoftKeyboardEnabled();
66+
default:
67+
return false;
68+
}
69+
}
70+
71+
}

app/src/main/java/com/termux/app/settings/preferences/TermuxAppSharedPreferences.java

+10
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ public boolean toogleShowTerminalToolbar() {
5353

5454

5555

56+
public boolean getSoftKeyboardEnabled() {
57+
return SharedPreferenceUtils.getBoolean(mSharedPreferences, TERMUX_APP.KEY_SOFT_KEYBOARD_ENABLED, TERMUX_APP.DEFAULT_VALUE_KEY_SOFT_KEYBOARD_ENABLED);
58+
}
59+
60+
public void setSoftKeyboardEnabled(boolean value) {
61+
SharedPreferenceUtils.setBoolean(mSharedPreferences, TERMUX_APP.KEY_SOFT_KEYBOARD_ENABLED, value, false);
62+
}
63+
64+
65+
5666
public boolean getKeepScreenOn() {
5767
return SharedPreferenceUtils.getBoolean(mSharedPreferences, TERMUX_APP.KEY_KEEP_SCREEN_ON, TERMUX_APP.DEFAULT_VALUE_KEEP_SCREEN_ON);
5868
}

app/src/main/java/com/termux/app/settings/preferences/TermuxPreferenceConstants.java

+21-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.termux.app.settings.preferences;
22

33
/*
4-
* Version: v0.6.0
4+
* Version: v0.7.0
55
*
66
* Changelog
77
*
@@ -29,6 +29,10 @@
2929
*
3030
* - 0.6.0 (2021-03-24)
3131
* - Change `DEFAULT_VALUE_KEEP_SCREEN_ON` value to `false` in `TERMUX_APP`.
32+
*
33+
* - 0.7.0 (2021-03-27)
34+
* - Added following to `TERMUX_APP`:
35+
* `KEY_SOFT_KEYBOARD_ENABLED` and `DEFAULT_VALUE_KEY_SOFT_KEYBOARD_ENABLED`.
3236
*/
3337

3438
/**
@@ -46,39 +50,47 @@ public final class TermuxPreferenceConstants {
4650
public static final class TERMUX_APP {
4751

4852
/**
49-
* Defines the key for whether to show terminal toolbar containing extra keys and text input field
53+
* Defines the key for whether to show terminal toolbar containing extra keys and text input field.
5054
*/
5155
public static final String KEY_SHOW_TERMINAL_TOOLBAR = "show_extra_keys";
5256
public static final boolean DEFAULT_VALUE_SHOW_TERMINAL_TOOLBAR = true;
5357

5458

5559
/**
56-
* Defines the key for whether to always keep screen on
60+
* Defines the key for whether the soft keyboard will be enabled, for cases where users want
61+
* to use a hardware keyboard instead.
62+
*/
63+
public static final String KEY_SOFT_KEYBOARD_ENABLED = "soft_keyboard_enabled";
64+
public static final boolean DEFAULT_VALUE_KEY_SOFT_KEYBOARD_ENABLED = true;
65+
66+
67+
/**
68+
* Defines the key for whether to always keep screen on.
5769
*/
5870
public static final String KEY_KEEP_SCREEN_ON = "screen_always_on";
5971
public static final boolean DEFAULT_VALUE_KEEP_SCREEN_ON = false;
6072

6173

6274
/**
63-
* Defines the key for font size of termux terminal view
75+
* Defines the key for font size of termux terminal view.
6476
*/
6577
public static final String KEY_FONTSIZE = "fontsize";
6678

6779

6880
/**
69-
* Defines the key for current termux terminal session
81+
* Defines the key for current termux terminal session.
7082
*/
7183
public static final String KEY_CURRENT_SESSION = "current_session";
7284

7385

7486
/**
75-
* Defines the key for current termux log level
87+
* Defines the key for current termux log level.
7688
*/
7789
public static final String KEY_LOG_LEVEL = "log_level";
7890

7991

8092
/**
81-
* Defines the key for last used notification id
93+
* Defines the key for last used notification id.
8294
*/
8395
public static final String KEY_LAST_NOTIFICATION_ID = "last_notification_id";
8496
public static final int DEFAULT_VALUE_KEY_LAST_NOTIFICATION_ID = 0;
@@ -91,7 +103,7 @@ public static final class TERMUX_APP {
91103
public static final boolean DEFAULT_VALUE_TERMINAL_VIEW_KEY_LOGGING_ENABLED = false;
92104

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

106118
/**
107-
* Defines the key for current termux log level
119+
* Defines the key for current termux log level.
108120
*/
109121
public static final String KEY_LOG_LEVEL = "log_level";
110122

app/src/main/res/values/strings.xml

+13
Original file line numberDiff line numberDiff line change
@@ -172,4 +172,17 @@
172172
<string name="plugin_error_notifications_off">Disable flashes and notifications for plugin errors.</string>
173173
<string name="plugin_error_notifications_on">Show flashes and notifications for plugin errors. (Default)</string>
174174

175+
176+
<!-- Terminal IO Preferences -->
177+
<string name="terminal_io_preferences">Terminal I/O</string>
178+
179+
<!-- Keyboard Category -->
180+
<string name="keyboard_header">Keyboard</string>
181+
182+
<!-- Soft Keyboard -->
183+
<string name="soft_keyboard_title">Soft Keyboard</string>
184+
<string name="soft_keyboard_off">Soft keyboard will be disabled.</string>
185+
<string name="soft_keyboard_on">Soft keyboard will be enabled. (Default)</string>
186+
187+
175188
</resources>

app/src/main/res/xml/root_preferences.xml

+5
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,9 @@
55
app:summary="Preferences for debugging"
66
app:fragment="com.termux.app.fragments.settings.DebuggingPreferencesFragment"/>
77

8+
<Preference
9+
app:title="@string/terminal_io_preferences"
10+
app:summary="Preferences for terminal I/O"
11+
app:fragment="com.termux.app.fragments.settings.TerminalIOPreferencesFragment"/>
12+
813
</PreferenceScreen>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto">
2+
3+
<PreferenceCategory
4+
app:key="keyboard"
5+
app:title="@string/keyboard_header">
6+
7+
<SwitchPreferenceCompat
8+
app:key="soft_keyboard_enabled"
9+
app:summaryOff="@string/soft_keyboard_off"
10+
app:summaryOn="@string/soft_keyboard_on"
11+
app:title="@string/soft_keyboard_title" />
12+
13+
</PreferenceCategory>
14+
15+
</PreferenceScreen>

0 commit comments

Comments
 (0)