From f907684ef2f18b3c5f2f9590a00c00439ba15cbc Mon Sep 17 00:00:00 2001 From: Fredrik Fornwall Date: Wed, 23 Dec 2015 01:39:49 +0100 Subject: [PATCH] Read support for customizing through properties By using the file $HOME/.config/termux/termux.properties it will be possible to configure the behaviour of: - The bell character. - Tapping the terminal. - The back key. --- .../com/termux/app/TermuxPreferences.java | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/app/src/main/java/com/termux/app/TermuxPreferences.java b/app/src/main/java/com/termux/app/TermuxPreferences.java index 562b853a..be85f7a1 100644 --- a/app/src/main/java/com/termux/app/TermuxPreferences.java +++ b/app/src/main/java/com/termux/app/TermuxPreferences.java @@ -5,12 +5,38 @@ import com.termux.terminal.TerminalSession; import android.content.Context; import android.content.SharedPreferences; import android.preference.PreferenceManager; +import android.support.annotation.IntDef; +import android.util.Log; import android.util.TypedValue; +import android.widget.Toast; + +import java.io.File; +import java.io.FileInputStream; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.util.Properties; final class TermuxPreferences { + @IntDef({BELL_VIBRATE, BELL_BEEP, BELL_IGNORE}) + @Retention(RetentionPolicy.SOURCE) + public @interface AsciiBellBehaviour {} + + static final int BELL_VIBRATE = 1; + static final int BELL_BEEP = 2; + static final int BELL_IGNORE = 3; + + @IntDef({TAP_TOGGLE_KEYBOARD, TAP_SHOW_MENU, TAP_IGNORE}) + @Retention(RetentionPolicy.SOURCE) + public @interface TapTerminalBehaviour {} + + static final int TAP_TOGGLE_KEYBOARD = 1; + static final int TAP_SHOW_MENU = 2; + static final int TAP_IGNORE = 3; + private final int MIN_FONTSIZE; private static final int MAX_FONTSIZE = 256; + private static final String FULLSCREEN_KEY = "fullscreen"; private static final String FONTSIZE_KEY = "fontsize"; private static final String CURRENT_SESSION_KEY = "current_session"; @@ -19,7 +45,16 @@ final class TermuxPreferences { private boolean mFullScreen; private int mFontSize; + @AsciiBellBehaviour + int mBellBehaviour = BELL_VIBRATE; + + @TapTerminalBehaviour + int mTapBehaviour = TAP_TOGGLE_KEYBOARD; + + boolean mBackIsEscape = true; + TermuxPreferences(Context context) { + reloadFromProperties(context); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); float dipInPixels = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, context.getResources().getDisplayMetrics()); @@ -86,4 +121,49 @@ final class TermuxPreferences { PreferenceManager.getDefaultSharedPreferences(context).edit().putBoolean(SHOW_WELCOME_DIALOG_KEY, false).apply(); } + public void reloadFromProperties(Context context) { + try { + File propsFile = new File(TermuxService.HOME_PATH + "/.config/termux/termux.properties"); + if (propsFile.isFile() && propsFile.canRead()) { + Properties props = new Properties(); + try (FileInputStream in = new FileInputStream(propsFile)) { + props.load(in); + } + + switch (props.getProperty("bell-character", "vibrate")) { + case "beep": + mBellBehaviour = BELL_BEEP; + break; + case "ignore": + mBellBehaviour = BELL_IGNORE; + break; + default: // "vibrate". + mBellBehaviour = BELL_VIBRATE; + break; + } + + switch (props.getProperty("tap-screen", "toggle-keyboard")) { + case "show-menu": + mTapBehaviour = TAP_SHOW_MENU; + break; + case "ignore": + mTapBehaviour = TAP_IGNORE; + break; + default: // "toggle-keyboard". + mTapBehaviour = TAP_TOGGLE_KEYBOARD; + break; + } + + mBackIsEscape = !"back".equals(props.getProperty("back-key", "escape")); + } else { + mBellBehaviour = BELL_VIBRATE; + mTapBehaviour = TAP_TOGGLE_KEYBOARD; + mBackIsEscape = true; + } + } catch (Exception e) { + Toast.makeText(context, "Error loading properties: " + e.getMessage(), Toast.LENGTH_LONG).show(); + Log.e("termux", "Error loading props", e); + } + } + }