2
2
3
3
import android .content .Context ;
4
4
import android .content .SharedPreferences ;
5
- import android .content .res .Configuration ;
6
5
import android .preference .PreferenceManager ;
7
- import android .util .Log ;
8
6
import android .util .TypedValue ;
9
- import android .widget .Toast ;
10
- import com .termux .terminal .TerminalSession ;
11
- import org .json .JSONArray ;
12
- import org .json .JSONException ;
13
- import org .json .JSONObject ;
14
-
15
- import java .io .File ;
16
- import java .io .FileInputStream ;
17
- import java .io .IOException ;
18
- import java .io .InputStreamReader ;
19
- import java .lang .annotation .Retention ;
20
- import java .lang .annotation .RetentionPolicy ;
21
- import java .nio .charset .StandardCharsets ;
22
- import java .util .ArrayList ;
23
- import java .util .HashMap ;
24
- import java .util .Iterator ;
25
- import java .util .List ;
26
- import java .util .Map ;
27
- import java .util .Properties ;
28
7
29
- import androidx .annotation .IntDef ;
30
-
31
- import static com .termux .terminal .EmulatorDebug .LOG_TAG ;
8
+ import com .termux .terminal .TerminalSession ;
32
9
33
10
final class TermuxPreferences {
34
11
35
- @ IntDef ({BELL_VIBRATE , BELL_BEEP , BELL_IGNORE })
36
- @ Retention (RetentionPolicy .SOURCE )
37
- @interface AsciiBellBehaviour {
38
- }
39
-
40
- final static class KeyboardShortcut {
41
-
42
- KeyboardShortcut (int codePoint , int shortcutAction ) {
43
- this .codePoint = codePoint ;
44
- this .shortcutAction = shortcutAction ;
45
- }
46
-
47
- final int codePoint ;
48
- final int shortcutAction ;
49
- }
50
-
51
- static final int SHORTCUT_ACTION_CREATE_SESSION = 1 ;
52
- static final int SHORTCUT_ACTION_NEXT_SESSION = 2 ;
53
- static final int SHORTCUT_ACTION_PREVIOUS_SESSION = 3 ;
54
- static final int SHORTCUT_ACTION_RENAME_SESSION = 4 ;
55
-
56
- static final int BELL_VIBRATE = 1 ;
57
- static final int BELL_BEEP = 2 ;
58
- static final int BELL_IGNORE = 3 ;
59
-
60
12
private final int MIN_FONTSIZE ;
61
13
private static final int MAX_FONTSIZE = 256 ;
62
14
@@ -65,34 +17,11 @@ final static class KeyboardShortcut {
65
17
private static final String CURRENT_SESSION_KEY = "current_session" ;
66
18
private static final String SCREEN_ALWAYS_ON_KEY = "screen_always_on" ;
67
19
68
- private boolean mUseDarkUI ;
69
20
private boolean mScreenAlwaysOn ;
70
21
private int mFontSize ;
71
-
72
- private boolean mUseFullScreen ;
73
- private boolean mUseFullScreenWorkAround ;
74
-
75
- @ AsciiBellBehaviour
76
- int mBellBehaviour = BELL_VIBRATE ;
77
-
78
- boolean mBackIsEscape ;
79
- boolean mDisableVolumeVirtualKeys ;
80
22
boolean mShowExtraKeys ;
81
- String mDefaultWorkingDir ;
82
-
83
- ExtraKeysInfos mExtraKeys ;
84
-
85
- final List <KeyboardShortcut > shortcuts = new ArrayList <>();
86
-
87
- /**
88
- * If value is not in the range [min, max], set it to either min or max.
89
- */
90
- static int clamp (int value , int min , int max ) {
91
- return Math .min (Math .max (value , min ), max );
92
- }
93
23
94
24
TermuxPreferences (Context context ) {
95
- reloadFromProperties (context );
96
25
SharedPreferences prefs = PreferenceManager .getDefaultSharedPreferences (context );
97
26
98
27
float dipInPixels = TypedValue .applyDimension (TypedValue .COMPLEX_UNIT_DIP , 1 , context .getResources ().getDisplayMetrics ());
@@ -139,18 +68,6 @@ boolean isScreenAlwaysOn() {
139
68
return mScreenAlwaysOn ;
140
69
}
141
70
142
- boolean isUsingBlackUI () {
143
- return mUseDarkUI ;
144
- }
145
-
146
- boolean isUsingFullScreen () {
147
- return mUseFullScreen ;
148
- }
149
-
150
- boolean isUsingFullScreenWorkAround () {
151
- return mUseFullScreenWorkAround ;
152
- }
153
-
154
71
void setScreenAlwaysOn (Context context , boolean newValue ) {
155
72
mScreenAlwaysOn = newValue ;
156
73
PreferenceManager .getDefaultSharedPreferences (context ).edit ().putBoolean (SCREEN_ALWAYS_ON_KEY , newValue ).apply ();
@@ -169,108 +86,11 @@ static TerminalSession getCurrentSession(TermuxActivity context) {
169
86
return null ;
170
87
}
171
88
172
- void reloadFromProperties (Context context ) {
173
- File propsFile = new File (TermuxConstants .TERMUX_PROPERTIES_PRIMARY_PATH );
174
- if (!propsFile .exists ())
175
- propsFile = new File (TermuxConstants .TERMUX_PROPERTIES_SECONDARY_PATH );
176
-
177
- Properties props = new Properties ();
178
- try {
179
- if (propsFile .isFile () && propsFile .canRead ()) {
180
- try (FileInputStream in = new FileInputStream (propsFile )) {
181
- props .load (new InputStreamReader (in , StandardCharsets .UTF_8 ));
182
- }
183
- }
184
- } catch (Exception e ) {
185
- Toast .makeText (context , "Could not open properties file termux.properties: " + e .getMessage (), Toast .LENGTH_LONG ).show ();
186
- Log .e ("termux" , "Error loading props" , e );
187
- }
188
-
189
- switch (props .getProperty ("bell-character" , "vibrate" )) {
190
- case "beep" :
191
- mBellBehaviour = BELL_BEEP ;
192
- break ;
193
- case "ignore" :
194
- mBellBehaviour = BELL_IGNORE ;
195
- break ;
196
- default : // "vibrate".
197
- mBellBehaviour = BELL_VIBRATE ;
198
- break ;
199
- }
200
-
201
- switch (props .getProperty ("use-black-ui" , "" ).toLowerCase ()) {
202
- case "true" :
203
- mUseDarkUI = true ;
204
- break ;
205
- case "false" :
206
- mUseDarkUI = false ;
207
- break ;
208
- default :
209
- int nightMode = context .getResources ().getConfiguration ().uiMode & Configuration .UI_MODE_NIGHT_MASK ;
210
- mUseDarkUI = nightMode == Configuration .UI_MODE_NIGHT_YES ;
211
- }
212
-
213
- mUseFullScreen = "true" .equals (props .getProperty ("fullscreen" , "false" ).toLowerCase ());
214
- mUseFullScreenWorkAround = "true" .equals (props .getProperty ("use-fullscreen-workaround" , "false" ).toLowerCase ());
215
-
216
- mDefaultWorkingDir = props .getProperty ("default-working-directory" , TermuxConstants .HOME_PATH );
217
- File workDir = new File (mDefaultWorkingDir );
218
- if (!workDir .exists () || !workDir .isDirectory ()) {
219
- // Fallback to home directory if user configured working directory is not exist
220
- // or is a regular file.
221
- mDefaultWorkingDir = TermuxConstants .HOME_PATH ;
222
- }
223
-
224
- String defaultExtraKeys = "[[ESC, TAB, CTRL, ALT, {key: '-', popup: '|'}, DOWN, UP]]" ;
225
-
226
- try {
227
- String extrakeyProp = props .getProperty ("extra-keys" , defaultExtraKeys );
228
- String extraKeysStyle = props .getProperty ("extra-keys-style" , "default" );
229
- mExtraKeys = new ExtraKeysInfos (extrakeyProp , extraKeysStyle );
230
- } catch (JSONException e ) {
231
- Toast .makeText (context , "Could not load the extra-keys property from the config: " + e .toString (), Toast .LENGTH_LONG ).show ();
232
- Log .e ("termux" , "Error loading props" , e );
233
-
234
- try {
235
- mExtraKeys = new ExtraKeysInfos (defaultExtraKeys , "default" );
236
- } catch (JSONException e2 ) {
237
- e2 .printStackTrace ();
238
- Toast .makeText (context , "Can't create default extra keys" , Toast .LENGTH_LONG ).show ();
239
- mExtraKeys = null ;
240
- }
241
- }
242
-
243
- mBackIsEscape = "escape" .equals (props .getProperty ("back-key" , "back" ));
244
- mDisableVolumeVirtualKeys = "volume" .equals (props .getProperty ("volume-keys" , "virtual" ));
245
-
246
- shortcuts .clear ();
247
- parseAction ("shortcut.create-session" , SHORTCUT_ACTION_CREATE_SESSION , props );
248
- parseAction ("shortcut.next-session" , SHORTCUT_ACTION_NEXT_SESSION , props );
249
- parseAction ("shortcut.previous-session" , SHORTCUT_ACTION_PREVIOUS_SESSION , props );
250
- parseAction ("shortcut.rename-session" , SHORTCUT_ACTION_RENAME_SESSION , props );
251
- }
252
-
253
- private void parseAction (String name , int shortcutAction , Properties props ) {
254
- String value = props .getProperty (name );
255
- if (value == null ) return ;
256
- String [] parts = value .toLowerCase ().trim ().split ("\\ +" );
257
- String input = parts .length == 2 ? parts [1 ].trim () : null ;
258
- if (!(parts .length == 2 && parts [0 ].trim ().equals ("ctrl" )) || input .isEmpty () || input .length () > 2 ) {
259
- Log .e ("termux" , "Keyboard shortcut '" + name + "' is not Ctrl+<something>" );
260
- return ;
261
- }
262
-
263
- char c = input .charAt (0 );
264
- int codePoint = c ;
265
- if (Character .isLowSurrogate (c )) {
266
- if (input .length () != 2 || Character .isHighSurrogate (input .charAt (1 ))) {
267
- Log .e ("termux" , "Keyboard shortcut '" + name + "' is not Ctrl+<something>" );
268
- return ;
269
- } else {
270
- codePoint = Character .toCodePoint (input .charAt (1 ), c );
271
- }
272
- }
273
- shortcuts .add (new KeyboardShortcut (codePoint , shortcutAction ));
89
+ /**
90
+ * If value is not in the range [min, max], set it to either min or max.
91
+ */
92
+ static int clamp (int value , int min , int max ) {
93
+ return Math .min (Math .max (value , min ), max );
274
94
}
275
95
276
96
}
0 commit comments