From 05283bd7745ee789b5468186a8d47e6c7700af6b Mon Sep 17 00:00:00 2001 From: agnostic-apollo Date: Thu, 10 Mar 2022 02:26:59 +0500 Subject: [PATCH] Changed: Load termux.properties into a single static app wide TermuxAppSharedProperties class The `TermuxAppSharedProperties.properties` will exist in `termux-shared` library and only the single static instance will be reloaded whenever needed, instead of different activities and services maintaining their own instances. The classes in `termux-shared` library will also get access to the properties for their own needs. The night mode set in `TermuxApplication` and terminal rows set in `TermuxService` will no longer require loading props from disk. Updating `allow-external-apps` value will now require restarting termux or running `termux-reload-settings` since value will no longer be loaded from disk every time. --- .../java/com/termux/app/TermuxActivity.java | 47 ++++--- .../com/termux/app/TermuxApplication.java | 8 +- .../java/com/termux/app/TermuxService.java | 29 ++--- .../properties/TermuxAppSharedProperties.java | 116 ------------------ .../terminal/TermuxTerminalSessionClient.java | 2 +- .../terminal/TermuxTerminalViewClient.java | 38 +++++- .../terminal/io/TerminalToolbarViewPager.java | 5 +- .../terminal/io/TermuxTerminalExtraKeys.java | 61 ++++++++- .../com/termux/app/utils/PluginUtils.java | 8 +- .../properties/TermuxAppSharedProperties.java | 42 +++++++ .../properties/TermuxPropertyConstants.java | 2 +- .../properties/TermuxSharedProperties.java | 4 + 12 files changed, 200 insertions(+), 162 deletions(-) delete mode 100644 app/src/main/java/com/termux/app/settings/properties/TermuxAppSharedProperties.java create mode 100644 termux-shared/src/main/java/com/termux/shared/termux/settings/properties/TermuxAppSharedProperties.java diff --git a/app/src/main/java/com/termux/app/TermuxActivity.java b/app/src/main/java/com/termux/app/TermuxActivity.java index afcb6689..af85b92f 100644 --- a/app/src/main/java/com/termux/app/TermuxActivity.java +++ b/app/src/main/java/com/termux/app/TermuxActivity.java @@ -30,6 +30,7 @@ import android.widget.Toast; import com.termux.R; import com.termux.app.terminal.TermuxActivityRootView; +import com.termux.app.terminal.io.TermuxTerminalExtraKeys; import com.termux.shared.activities.ReportActivity; import com.termux.shared.activity.ActivityUtils; import com.termux.shared.activity.media.AppCompatActivityUtils; @@ -45,10 +46,10 @@ import com.termux.app.terminal.io.TerminalToolbarViewPager; import com.termux.app.terminal.TermuxTerminalSessionClient; import com.termux.app.terminal.TermuxTerminalViewClient; import com.termux.shared.termux.extrakeys.ExtraKeysView; -import com.termux.app.settings.properties.TermuxAppSharedProperties; import com.termux.shared.termux.interact.TextInputDialogUtils; import com.termux.shared.logger.Logger; import com.termux.shared.termux.TermuxUtils; +import com.termux.shared.termux.settings.properties.TermuxAppSharedProperties; import com.termux.shared.termux.theme.TermuxThemeUtils; import com.termux.shared.theme.NightMode; import com.termux.shared.view.ViewUtils; @@ -108,7 +109,7 @@ public final class TermuxActivity extends AppCompatActivity implements ServiceCo private TermuxAppSharedPreferences mPreferences; /** - * Termux app shared properties manager, loaded from termux.properties + * Termux app SharedProperties loaded from termux.properties */ private TermuxAppSharedProperties mProperties; @@ -127,6 +128,11 @@ public final class TermuxActivity extends AppCompatActivity implements ServiceCo */ ExtraKeysView mExtraKeysView; + /** + * The client for the {@link #mExtraKeysView}. + */ + TermuxTerminalExtraKeys mTermuxTerminalExtraKeys; + /** * The termux sessions list controller. */ @@ -201,8 +207,9 @@ public final class TermuxActivity extends AppCompatActivity implements ServiceCo // Delete ReportInfo serialized object files from cache older than 14 days ReportActivity.deleteReportInfoFilesOlderThanXDays(this, 14, false); - // Load termux shared properties - mProperties = new TermuxAppSharedProperties(this); + // Load Termux app SharedProperties from disk + mProperties = TermuxAppSharedProperties.getProperties(); + reloadProperties(); setActivityTheme(); @@ -374,7 +381,6 @@ public final class TermuxActivity extends AppCompatActivity implements ServiceCo */ @Override public void onServiceConnected(ComponentName componentName, IBinder service) { - Logger.logDebug(LOG_TAG, "onServiceConnected"); mTermuxService = ((TermuxService.LocalBinder) service).service; @@ -421,7 +427,6 @@ public final class TermuxActivity extends AppCompatActivity implements ServiceCo @Override public void onServiceDisconnected(ComponentName name) { - Logger.logDebug(LOG_TAG, "onServiceDisconnected"); // Respect being stopped from the {@link TermuxService} notification action. @@ -432,6 +437,16 @@ public final class TermuxActivity extends AppCompatActivity implements ServiceCo + + private void reloadProperties() { + mProperties.loadTermuxPropertiesFromDisk(); + + if (mTermuxTerminalViewClient != null) + mTermuxTerminalViewClient.onReloadProperties(); + } + + + private void setActivityTheme() { // Update NightMode.APP_NIGHT_MODE TermuxThemeUtils.setAppNightMode(mProperties.getNightMode()); @@ -489,6 +504,9 @@ public final class TermuxActivity extends AppCompatActivity implements ServiceCo private void setTerminalToolbarView(Bundle savedInstanceState) { + mTermuxTerminalExtraKeys = new TermuxTerminalExtraKeys(this, mTerminalView, + mTermuxTerminalViewClient, mTermuxTerminalSessionClient); + final ViewPager terminalToolbarViewPager = getTerminalToolbarViewPager(); if (mPreferences.shouldShowTerminalToolbar()) terminalToolbarViewPager.setVisibility(View.VISIBLE); @@ -511,7 +529,7 @@ public final class TermuxActivity extends AppCompatActivity implements ServiceCo ViewGroup.LayoutParams layoutParams = terminalToolbarViewPager.getLayoutParams(); layoutParams.height = (int) Math.round(mTerminalToolbarDefaultHeight * - (mProperties.getExtraKeysInfo() == null ? 0 : mProperties.getExtraKeysInfo().getMatrix().length) * + (mTermuxTerminalExtraKeys.getExtraKeysInfo() == null ? 0 : mTermuxTerminalExtraKeys.getExtraKeysInfo().getMatrix().length) * mProperties.getTerminalToolbarHeightScaleFactor()); terminalToolbarViewPager.setLayoutParams(layoutParams); } @@ -800,6 +818,10 @@ public final class TermuxActivity extends AppCompatActivity implements ServiceCo return mExtraKeysView; } + public TermuxTerminalExtraKeys getTermuxTerminalExtraKeys() { + return mTermuxTerminalExtraKeys; + } + public void setExtraKeysView(ExtraKeysView extraKeysView) { mExtraKeysView = extraKeysView; } @@ -929,11 +951,11 @@ public final class TermuxActivity extends AppCompatActivity implements ServiceCo private void reloadActivityStyling(boolean recreateActivity) { if (mProperties != null) { - mProperties.loadTermuxPropertiesFromDisk(); + reloadProperties(); if (mExtraKeysView != null) { mExtraKeysView.setButtonTextAllCaps(mProperties.shouldExtraKeysTextBeAllCaps()); - mExtraKeysView.reload(mProperties.getExtraKeysInfo()); + mExtraKeysView.reload(mTermuxTerminalExtraKeys.getExtraKeysInfo()); } // Update NightMode.APP_NIGHT_MODE @@ -944,13 +966,10 @@ public final class TermuxActivity extends AppCompatActivity implements ServiceCo setTerminalToolbarHeight(); if (mTermuxTerminalSessionClient != null) - mTermuxTerminalSessionClient.onReload(); + mTermuxTerminalSessionClient.onReloadActivityStyling(); if (mTermuxTerminalViewClient != null) - mTermuxTerminalViewClient.onReload(); - - if (mTermuxService != null) - mTermuxService.setTerminalTranscriptRows(); + mTermuxTerminalViewClient.onReloadActivityStyling(); // To change the activity and drawer theme, activity needs to be recreated. // It will destroy the activity, including all stored variables and views, and onCreate() diff --git a/app/src/main/java/com/termux/app/TermuxApplication.java b/app/src/main/java/com/termux/app/TermuxApplication.java index 994168ac..3d438f71 100644 --- a/app/src/main/java/com/termux/app/TermuxApplication.java +++ b/app/src/main/java/com/termux/app/TermuxApplication.java @@ -7,10 +7,12 @@ import com.termux.shared.termux.TermuxConstants; import com.termux.shared.termux.crash.TermuxCrashUtils; import com.termux.shared.termux.settings.preferences.TermuxAppSharedPreferences; import com.termux.shared.logger.Logger; +import com.termux.shared.termux.settings.properties.TermuxAppSharedProperties; import com.termux.shared.termux.theme.TermuxThemeUtils; public class TermuxApplication extends Application { + public void onCreate() { super.onCreate(); @@ -24,8 +26,11 @@ public class TermuxApplication extends Application { Logger.logDebug("Starting Application"); + // Init app wide SharedProperties loaded from termux.properties + TermuxAppSharedProperties properties = TermuxAppSharedProperties.init(context); + // Set NightMode.APP_NIGHT_MODE - TermuxThemeUtils.setAppNightMode(context); + TermuxThemeUtils.setAppNightMode(properties.getNightMode()); } public static void setLogConfig(Context context) { @@ -36,5 +41,6 @@ public class TermuxApplication extends Application { if (preferences == null) return; preferences.setLogLevel(null, preferences.getLogLevel()); } + } diff --git a/app/src/main/java/com/termux/app/TermuxService.java b/app/src/main/java/com/termux/app/TermuxService.java index b7443560..a04d49a1 100644 --- a/app/src/main/java/com/termux/app/TermuxService.java +++ b/app/src/main/java/com/termux/app/TermuxService.java @@ -19,7 +19,6 @@ import android.widget.ArrayAdapter; import androidx.annotation.Nullable; import com.termux.R; -import com.termux.app.settings.properties.TermuxAppSharedProperties; import com.termux.app.terminal.TermuxTerminalSessionClient; import com.termux.app.utils.PluginUtils; import com.termux.shared.data.IntentUtils; @@ -27,6 +26,7 @@ import com.termux.shared.net.uri.UriUtils; import com.termux.shared.errors.Errno; import com.termux.shared.shell.ShellUtils; import com.termux.shared.shell.command.runner.app.AppShell; +import com.termux.shared.termux.settings.properties.TermuxAppSharedProperties; import com.termux.shared.termux.shell.TermuxShellEnvironmentClient; import com.termux.shared.termux.shell.TermuxShellUtils; import com.termux.shared.termux.TermuxConstants; @@ -102,6 +102,11 @@ public final class TermuxService extends Service implements AppShell.AppShellCli */ final TermuxTerminalSessionClientBase mTermuxTerminalSessionClientBase = new TermuxTerminalSessionClientBase(); + /** + * Termux app shared properties manager, loaded from termux.properties + */ + private TermuxAppSharedProperties mProperties; + /** The wake lock and wifi lock are always acquired and released together. */ private PowerManager.WakeLock mWakeLock; private WifiManager.WifiLock mWifiLock; @@ -109,13 +114,16 @@ public final class TermuxService extends Service implements AppShell.AppShellCli /** If the user has executed the {@link TERMUX_SERVICE#ACTION_STOP_SERVICE} intent. */ boolean mWantsToStop = false; - public Integer mTerminalTranscriptRows; - private static final String LOG_TAG = "TermuxService"; @Override public void onCreate() { Logger.logVerbose(LOG_TAG, "onCreate"); + + // Get Termux app SharedProperties without loading from disk since TermuxApplication handles + // load and TermuxActivity handles reloads + mProperties = TermuxAppSharedProperties.getProperties(); + runStartForeground(); } @@ -515,7 +523,7 @@ public final class TermuxService extends Service implements AppShell.AppShellCli // If the execution command was started for a plugin, only then will the stdout be set // Otherwise if command was manually started by the user like by adding a new terminal session, // then no need to set stdout - executionCommand.terminalTranscriptRows = getTerminalTranscriptRows(); + executionCommand.terminalTranscriptRows = mProperties.getTerminalTranscriptRows(); TermuxSession newTermuxSession = TermuxSession.execute(this, executionCommand, getTermuxTerminalSessionClient(), this, new TermuxShellEnvironmentClient(), sessionName, executionCommand.isPluginExecutionCommand); if (newTermuxSession == null) { Logger.logError(LOG_TAG, "Failed to execute new TermuxSession command for:\n" + executionCommand.getCommandIdAndLabelLogString()); @@ -580,19 +588,6 @@ public final class TermuxService extends Service implements AppShell.AppShellCli updateNotification(); } - /** Get the terminal transcript rows to be used for new {@link TermuxSession}. */ - public Integer getTerminalTranscriptRows() { - if (mTerminalTranscriptRows == null) - setTerminalTranscriptRows(); - return mTerminalTranscriptRows; - } - - public void setTerminalTranscriptRows() { - // TermuxService only uses this termux property currently, so no need to load them all into - // an internal values map like TermuxActivity does - mTerminalTranscriptRows = TermuxAppSharedProperties.getTerminalTranscriptRows(this); - } - diff --git a/app/src/main/java/com/termux/app/settings/properties/TermuxAppSharedProperties.java b/app/src/main/java/com/termux/app/settings/properties/TermuxAppSharedProperties.java deleted file mode 100644 index 737fdcf7..00000000 --- a/app/src/main/java/com/termux/app/settings/properties/TermuxAppSharedProperties.java +++ /dev/null @@ -1,116 +0,0 @@ -package com.termux.app.settings.properties; - -import android.content.Context; - -import androidx.annotation.NonNull; - -import com.termux.app.terminal.io.KeyboardShortcut; -import com.termux.shared.termux.extrakeys.ExtraKeysConstants; -import com.termux.shared.termux.extrakeys.ExtraKeysConstants.EXTRA_KEY_DISPLAY_MAPS; -import com.termux.shared.termux.extrakeys.ExtraKeysInfo; -import com.termux.shared.logger.Logger; -import com.termux.shared.termux.settings.properties.TermuxPropertyConstants; -import com.termux.shared.termux.settings.properties.TermuxSharedProperties; -import com.termux.shared.termux.TermuxConstants; - -import org.json.JSONException; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - -public class TermuxAppSharedProperties extends TermuxSharedProperties { - - private ExtraKeysInfo mExtraKeysInfo; - private List mSessionShortcuts; - - private static final String LOG_TAG = "TermuxAppSharedProperties"; - - public TermuxAppSharedProperties(@NonNull Context context) { - super(context, TermuxConstants.TERMUX_APP_NAME, TermuxPropertyConstants.getTermuxPropertiesFile(), - TermuxPropertyConstants.TERMUX_PROPERTIES_LIST, new SharedPropertiesParserClient()); - } - - /** - * Reload the termux properties from disk into an in-memory cache. - */ - @Override - public void loadTermuxPropertiesFromDisk() { - super.loadTermuxPropertiesFromDisk(); - - setExtraKeys(); - setSessionShortcuts(); - } - - /** - * Set the terminal extra keys and style. - */ - private void setExtraKeys() { - mExtraKeysInfo = null; - - try { - // The mMap stores the extra key and style string values while loading properties - // Check {@link #getExtraKeysInternalPropertyValueFromValue(String)} and - // {@link #getExtraKeysStyleInternalPropertyValueFromValue(String)} - String extrakeys = (String) getInternalPropertyValue(TermuxPropertyConstants.KEY_EXTRA_KEYS, true); - String extraKeysStyle = (String) getInternalPropertyValue(TermuxPropertyConstants.KEY_EXTRA_KEYS_STYLE, true); - - ExtraKeysConstants.ExtraKeyDisplayMap extraKeyDisplayMap = ExtraKeysInfo.getCharDisplayMapForStyle(extraKeysStyle); - if (EXTRA_KEY_DISPLAY_MAPS.DEFAULT_CHAR_DISPLAY.equals(extraKeyDisplayMap) && !TermuxPropertyConstants.DEFAULT_IVALUE_EXTRA_KEYS_STYLE.equals(extraKeysStyle)) { - Logger.logError(TermuxSharedProperties.LOG_TAG, "The style \"" + extraKeysStyle + "\" for the key \"" + TermuxPropertyConstants.KEY_EXTRA_KEYS_STYLE + "\" is invalid. Using default style instead."); - extraKeysStyle = TermuxPropertyConstants.DEFAULT_IVALUE_EXTRA_KEYS_STYLE; - } - - mExtraKeysInfo = new ExtraKeysInfo(extrakeys, extraKeysStyle, ExtraKeysConstants.CONTROL_CHARS_ALIASES); - } catch (JSONException e) { - Logger.showToast(mContext, "Could not load and set the \"" + TermuxPropertyConstants.KEY_EXTRA_KEYS + "\" property from the properties file: " + e.toString(), true); - Logger.logStackTraceWithMessage(LOG_TAG, "Could not load and set the \"" + TermuxPropertyConstants.KEY_EXTRA_KEYS + "\" property from the properties file: ", e); - - try { - mExtraKeysInfo = new ExtraKeysInfo(TermuxPropertyConstants.DEFAULT_IVALUE_EXTRA_KEYS, TermuxPropertyConstants.DEFAULT_IVALUE_EXTRA_KEYS_STYLE, ExtraKeysConstants.CONTROL_CHARS_ALIASES); - } catch (JSONException e2) { - Logger.showToast(mContext, "Can't create default extra keys",true); - Logger.logStackTraceWithMessage(LOG_TAG, "Could create default extra keys: ", e); - mExtraKeysInfo = null; - } - } - } - - /** - * Set the terminal sessions shortcuts. - */ - private void setSessionShortcuts() { - mSessionShortcuts = new ArrayList<>(); - - // The {@link TermuxPropertyConstants#MAP_SESSION_SHORTCUTS} stores the session shortcut key and action pair - for (Map.Entry entry : TermuxPropertyConstants.MAP_SESSION_SHORTCUTS.entrySet()) { - // The mMap stores the code points for the session shortcuts while loading properties - Integer codePoint = (Integer) getInternalPropertyValue(entry.getKey(), true); - // If codePoint is null, then session shortcut did not exist in properties or was invalid - // as parsed by {@link #getCodePointForSessionShortcuts(String,String)} - // If codePoint is not null, then get the action for the MAP_SESSION_SHORTCUTS key and - // add the code point to sessionShortcuts - if (codePoint != null) - mSessionShortcuts.add(new KeyboardShortcut(codePoint, entry.getValue())); - } - } - - public List getSessionShortcuts() { - return mSessionShortcuts; - } - - public ExtraKeysInfo getExtraKeysInfo() { - return mExtraKeysInfo; - } - - - - /** - * Load the {@link TermuxPropertyConstants#KEY_TERMINAL_TRANSCRIPT_ROWS} value from termux properties file on disk. - */ - public static int getTerminalTranscriptRows(Context context) { - return (int) TermuxSharedProperties.getTermuxInternalPropertyValue(context, - TermuxPropertyConstants.KEY_TERMINAL_TRANSCRIPT_ROWS); - } - -} diff --git a/app/src/main/java/com/termux/app/terminal/TermuxTerminalSessionClient.java b/app/src/main/java/com/termux/app/terminal/TermuxTerminalSessionClient.java index c54c3870..e15b6e7d 100644 --- a/app/src/main/java/com/termux/app/terminal/TermuxTerminalSessionClient.java +++ b/app/src/main/java/com/termux/app/terminal/TermuxTerminalSessionClient.java @@ -103,7 +103,7 @@ public class TermuxTerminalSessionClient extends TermuxTerminalSessionClientBase /** * Should be called when mActivity.reloadActivityStyling() is called */ - public void onReload() { + public void onReloadActivityStyling() { // Set terminal fonts and colors checkForFontAndColors(); } diff --git a/app/src/main/java/com/termux/app/terminal/TermuxTerminalViewClient.java b/app/src/main/java/com/termux/app/terminal/TermuxTerminalViewClient.java index 5bcd7ce0..1d293ca1 100644 --- a/app/src/main/java/com/termux/app/terminal/TermuxTerminalViewClient.java +++ b/app/src/main/java/com/termux/app/terminal/TermuxTerminalViewClient.java @@ -44,10 +44,12 @@ import com.termux.terminal.KeyHandler; import com.termux.terminal.TerminalEmulator; import com.termux.terminal.TerminalSession; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.LinkedHashSet; import java.util.List; +import java.util.Map; import androidx.drawerlayout.widget.DrawerLayout; @@ -67,6 +69,8 @@ public class TermuxTerminalViewClient extends TermuxTerminalViewClientBase { private boolean mTerminalCursorBlinkerStateAlreadySet; + private List mSessionShortcuts; + private static final String LOG_TAG = "TermuxTerminalViewClient"; public TermuxTerminalViewClient(TermuxActivity activity, TermuxTerminalSessionClient termuxTerminalSessionClient) { @@ -82,6 +86,8 @@ public class TermuxTerminalViewClient extends TermuxTerminalViewClientBase { * Should be called when mActivity.onCreate() is called */ public void onCreate() { + onReloadProperties(); + mActivity.getTerminalView().setTextSize(mActivity.getPreferences().getFontSize()); mActivity.getTerminalView().setKeepScreenOn(mActivity.getPreferences().shouldKeepScreenOn()); } @@ -127,10 +133,17 @@ public class TermuxTerminalViewClient extends TermuxTerminalViewClientBase { setTerminalCursorBlinkerState(false); } + /** + * Should be called when mActivity.reloadProperties() is called + */ + public void onReloadProperties() { + setSessionShortcuts(); + } + /** * Should be called when mActivity.reloadActivityStyling() is called */ - public void onReload() { + public void onReloadActivityStyling() { // Show the soft keyboard if required setSoftKeyboardState(false, true); @@ -452,7 +465,7 @@ public class TermuxTerminalViewClient extends TermuxTerminalViewClientBase { return true; } - List shortcuts = mActivity.getProperties().getSessionShortcuts(); + List shortcuts = mSessionShortcuts; if (shortcuts != null && !shortcuts.isEmpty()) { int codePointLowerCase = Character.toLowerCase(codePoint); for (int i = shortcuts.size() - 1; i >= 0; i--) { @@ -480,6 +493,27 @@ public class TermuxTerminalViewClient extends TermuxTerminalViewClientBase { return false; } + /** + * Set the terminal sessions shortcuts. + */ + private void setSessionShortcuts() { + mSessionShortcuts = new ArrayList<>(); + + // The {@link TermuxPropertyConstants#MAP_SESSION_SHORTCUTS} stores the session shortcut key and action pair + for (Map.Entry entry : TermuxPropertyConstants.MAP_SESSION_SHORTCUTS.entrySet()) { + // The mMap stores the code points for the session shortcuts while loading properties + Integer codePoint = (Integer) mActivity.getProperties().getInternalPropertyValue(entry.getKey(), true); + // If codePoint is null, then session shortcut did not exist in properties or was invalid + // as parsed by {@link #getCodePointForSessionShortcuts(String,String)} + // If codePoint is not null, then get the action for the MAP_SESSION_SHORTCUTS key and + // add the code point to sessionShortcuts + if (codePoint != null) + mSessionShortcuts.add(new KeyboardShortcut(codePoint, entry.getValue())); + } + } + + + public void changeFontSize(boolean increase) { diff --git a/app/src/main/java/com/termux/app/terminal/io/TerminalToolbarViewPager.java b/app/src/main/java/com/termux/app/terminal/io/TerminalToolbarViewPager.java index 7a3f945e..ac1e2e4d 100644 --- a/app/src/main/java/com/termux/app/terminal/io/TerminalToolbarViewPager.java +++ b/app/src/main/java/com/termux/app/terminal/io/TerminalToolbarViewPager.java @@ -44,11 +44,10 @@ public class TerminalToolbarViewPager { if (position == 0) { layout = inflater.inflate(R.layout.view_terminal_toolbar_extra_keys, collection, false); ExtraKeysView extraKeysView = (ExtraKeysView) layout; - extraKeysView.setExtraKeysViewClient(new TermuxTerminalExtraKeys(mActivity.getTerminalView(), - mActivity.getTermuxTerminalViewClient(), mActivity.getTermuxTerminalSessionClient())); + extraKeysView.setExtraKeysViewClient(mActivity.getTermuxTerminalExtraKeys()); extraKeysView.setButtonTextAllCaps(mActivity.getProperties().shouldExtraKeysTextBeAllCaps()); mActivity.setExtraKeysView(extraKeysView); - extraKeysView.reload(mActivity.getProperties().getExtraKeysInfo()); + extraKeysView.reload(mActivity.getTermuxTerminalExtraKeys().getExtraKeysInfo()); // apply extra keys fix if enabled in prefs if (mActivity.getProperties().isUsingFullScreen() && mActivity.getProperties().isUsingFullScreenWorkAround()) { diff --git a/app/src/main/java/com/termux/app/terminal/io/TermuxTerminalExtraKeys.java b/app/src/main/java/com/termux/app/terminal/io/TermuxTerminalExtraKeys.java index 030f852f..48cd6be2 100644 --- a/app/src/main/java/com/termux/app/terminal/io/TermuxTerminalExtraKeys.java +++ b/app/src/main/java/com/termux/app/terminal/io/TermuxTerminalExtraKeys.java @@ -7,23 +7,78 @@ import android.view.View; import androidx.annotation.NonNull; import androidx.drawerlayout.widget.DrawerLayout; +import com.termux.app.TermuxActivity; import com.termux.app.terminal.TermuxTerminalSessionClient; import com.termux.app.terminal.TermuxTerminalViewClient; +import com.termux.shared.logger.Logger; +import com.termux.shared.termux.extrakeys.ExtraKeysConstants; +import com.termux.shared.termux.extrakeys.ExtraKeysInfo; +import com.termux.shared.termux.settings.properties.TermuxPropertyConstants; +import com.termux.shared.termux.settings.properties.TermuxSharedProperties; import com.termux.shared.termux.terminal.io.TerminalExtraKeys; import com.termux.view.TerminalView; +import org.json.JSONException; + public class TermuxTerminalExtraKeys extends TerminalExtraKeys { + private ExtraKeysInfo mExtraKeysInfo; - TermuxTerminalViewClient mTermuxTerminalViewClient; - TermuxTerminalSessionClient mTermuxTerminalSessionClient; + final TermuxActivity mActivity; + final TermuxTerminalViewClient mTermuxTerminalViewClient; + final TermuxTerminalSessionClient mTermuxTerminalSessionClient; - public TermuxTerminalExtraKeys(@NonNull TerminalView terminalView, + private static final String LOG_TAG = "TermuxTerminalExtraKeys"; + + public TermuxTerminalExtraKeys(TermuxActivity activity, @NonNull TerminalView terminalView, TermuxTerminalViewClient termuxTerminalViewClient, TermuxTerminalSessionClient termuxTerminalSessionClient) { super(terminalView); + + mActivity = activity; mTermuxTerminalViewClient = termuxTerminalViewClient; mTermuxTerminalSessionClient = termuxTerminalSessionClient; + + setExtraKeys(); + } + + + /** + * Set the terminal extra keys and style. + */ + private void setExtraKeys() { + mExtraKeysInfo = null; + + try { + // The mMap stores the extra key and style string values while loading properties + // Check {@link #getExtraKeysInternalPropertyValueFromValue(String)} and + // {@link #getExtraKeysStyleInternalPropertyValueFromValue(String)} + String extrakeys = (String) mActivity.getProperties().getInternalPropertyValue(TermuxPropertyConstants.KEY_EXTRA_KEYS, true); + String extraKeysStyle = (String) mActivity.getProperties().getInternalPropertyValue(TermuxPropertyConstants.KEY_EXTRA_KEYS_STYLE, true); + + ExtraKeysConstants.ExtraKeyDisplayMap extraKeyDisplayMap = ExtraKeysInfo.getCharDisplayMapForStyle(extraKeysStyle); + if (ExtraKeysConstants.EXTRA_KEY_DISPLAY_MAPS.DEFAULT_CHAR_DISPLAY.equals(extraKeyDisplayMap) && !TermuxPropertyConstants.DEFAULT_IVALUE_EXTRA_KEYS_STYLE.equals(extraKeysStyle)) { + Logger.logError(TermuxSharedProperties.LOG_TAG, "The style \"" + extraKeysStyle + "\" for the key \"" + TermuxPropertyConstants.KEY_EXTRA_KEYS_STYLE + "\" is invalid. Using default style instead."); + extraKeysStyle = TermuxPropertyConstants.DEFAULT_IVALUE_EXTRA_KEYS_STYLE; + } + + mExtraKeysInfo = new ExtraKeysInfo(extrakeys, extraKeysStyle, ExtraKeysConstants.CONTROL_CHARS_ALIASES); + } catch (JSONException e) { + Logger.showToast(mActivity, "Could not load and set the \"" + TermuxPropertyConstants.KEY_EXTRA_KEYS + "\" property from the properties file: " + e.toString(), true); + Logger.logStackTraceWithMessage(LOG_TAG, "Could not load and set the \"" + TermuxPropertyConstants.KEY_EXTRA_KEYS + "\" property from the properties file: ", e); + + try { + mExtraKeysInfo = new ExtraKeysInfo(TermuxPropertyConstants.DEFAULT_IVALUE_EXTRA_KEYS, TermuxPropertyConstants.DEFAULT_IVALUE_EXTRA_KEYS_STYLE, ExtraKeysConstants.CONTROL_CHARS_ALIASES); + } catch (JSONException e2) { + Logger.showToast(mActivity, "Can't create default extra keys",true); + Logger.logStackTraceWithMessage(LOG_TAG, "Could create default extra keys: ", e); + mExtraKeysInfo = null; + } + } + } + + public ExtraKeysInfo getExtraKeysInfo() { + return mExtraKeysInfo; } @SuppressLint("RtlHardcoded") diff --git a/app/src/main/java/com/termux/app/utils/PluginUtils.java b/app/src/main/java/com/termux/app/utils/PluginUtils.java index 2607d3c9..37493d01 100644 --- a/app/src/main/java/com/termux/app/utils/PluginUtils.java +++ b/app/src/main/java/com/termux/app/utils/PluginUtils.java @@ -27,9 +27,8 @@ import com.termux.shared.termux.TermuxConstants.TERMUX_APP.TERMUX_SERVICE; import com.termux.shared.logger.Logger; import com.termux.shared.termux.settings.preferences.TermuxAppSharedPreferences; import com.termux.shared.termux.settings.preferences.TermuxPreferenceConstants.TERMUX_APP; -import com.termux.shared.settings.properties.SharedProperties; import com.termux.shared.models.ReportInfo; -import com.termux.shared.termux.settings.properties.TermuxPropertyConstants; +import com.termux.shared.termux.settings.properties.TermuxAppSharedProperties; import com.termux.shared.shell.command.ExecutionCommand; import com.termux.app.models.UserAction; import com.termux.shared.data.DataUtils; @@ -366,8 +365,9 @@ public class PluginUtils { */ public static String checkIfAllowExternalAppsPolicyIsViolated(final Context context, String apiName) { String errmsg = null; - if (!SharedProperties.isPropertyValueTrue(context, TermuxPropertyConstants.getTermuxPropertiesFile(), - TermuxConstants.PROP_ALLOW_EXTERNAL_APPS, true)) { + + TermuxAppSharedProperties mProperties = TermuxAppSharedProperties.getProperties(); + if (mProperties == null || !mProperties.shouldAllowExternalApps()) { errmsg = context.getString(R.string.error_allow_external_apps_ungranted, apiName, TermuxFileUtils.getUnExpandedTermuxPath(TermuxConstants.TERMUX_PROPERTIES_PRIMARY_FILE_PATH)); } diff --git a/termux-shared/src/main/java/com/termux/shared/termux/settings/properties/TermuxAppSharedProperties.java b/termux-shared/src/main/java/com/termux/shared/termux/settings/properties/TermuxAppSharedProperties.java new file mode 100644 index 00000000..a0189c96 --- /dev/null +++ b/termux-shared/src/main/java/com/termux/shared/termux/settings/properties/TermuxAppSharedProperties.java @@ -0,0 +1,42 @@ +package com.termux.shared.termux.settings.properties; + +import android.content.Context; + +import androidx.annotation.NonNull; + +import com.termux.shared.termux.TermuxConstants; + +public class TermuxAppSharedProperties extends TermuxSharedProperties { + + private static TermuxAppSharedProperties properties; + + + private TermuxAppSharedProperties(@NonNull Context context) { + super(context, TermuxConstants.TERMUX_APP_NAME, + TermuxPropertyConstants.getTermuxPropertiesFile(), TermuxPropertyConstants.TERMUX_APP_PROPERTIES_LIST, + new TermuxSharedProperties.SharedPropertiesParserClient()); + } + + /** + * Initialize the {@link #properties} and load properties from disk. + * + * @param context The {@link Context} for operations. + * @return Returns the {@link TermuxAppSharedProperties}. + */ + public static TermuxAppSharedProperties init(@NonNull Context context) { + if (properties == null) + properties = new TermuxAppSharedProperties(context); + + return properties; + } + + /** + * Get the {@link #properties}. + * + * @return Returns the {@link TermuxAppSharedProperties}. + */ + public static TermuxAppSharedProperties getProperties() { + return properties; + } + +} diff --git a/termux-shared/src/main/java/com/termux/shared/termux/settings/properties/TermuxPropertyConstants.java b/termux-shared/src/main/java/com/termux/shared/termux/settings/properties/TermuxPropertyConstants.java index 30febbd3..8c30abc6 100644 --- a/termux-shared/src/main/java/com/termux/shared/termux/settings/properties/TermuxPropertyConstants.java +++ b/termux-shared/src/main/java/com/termux/shared/termux/settings/properties/TermuxPropertyConstants.java @@ -355,7 +355,7 @@ public final class TermuxPropertyConstants { /** Defines the set for keys loaded by termux * Setting this to {@code null} will make {@link SharedProperties} throw an exception. * */ - public static final Set TERMUX_PROPERTIES_LIST = new HashSet<>(Arrays.asList( + public static final Set TERMUX_APP_PROPERTIES_LIST = new HashSet<>(Arrays.asList( /* boolean */ KEY_DISABLE_HARDWARE_KEYBOARD_SHORTCUTS, KEY_DISABLE_TERMINAL_SESSION_CHANGE_TOAST, diff --git a/termux-shared/src/main/java/com/termux/shared/termux/settings/properties/TermuxSharedProperties.java b/termux-shared/src/main/java/com/termux/shared/termux/settings/properties/TermuxSharedProperties.java index bf1ac1e8..36543015 100644 --- a/termux-shared/src/main/java/com/termux/shared/termux/settings/properties/TermuxSharedProperties.java +++ b/termux-shared/src/main/java/com/termux/shared/termux/settings/properties/TermuxSharedProperties.java @@ -8,6 +8,7 @@ import com.termux.shared.logger.Logger; import com.termux.shared.data.DataUtils; import com.termux.shared.settings.properties.SharedProperties; import com.termux.shared.settings.properties.SharedPropertiesParser; +import com.termux.shared.termux.TermuxConstants; import java.io.File; import java.util.HashMap; @@ -539,6 +540,9 @@ public abstract class TermuxSharedProperties { + public boolean shouldAllowExternalApps() { + return (boolean) getInternalPropertyValue(TermuxConstants.PROP_ALLOW_EXTERNAL_APPS, true); + } public boolean areHardwareKeyboardShortcutsDisabled() { return (boolean) getInternalPropertyValue(TermuxPropertyConstants.KEY_DISABLE_HARDWARE_KEYBOARD_SHORTCUTS, true); }