2021-03-12 01:01:31 +00:00
|
|
|
package com.termux.app.settings.preferences;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.util.TypedValue;
|
|
|
|
|
|
|
|
import com.termux.app.TermuxConstants;
|
2021-03-13 11:49:29 +00:00
|
|
|
import com.termux.app.utils.Logger;
|
|
|
|
import com.termux.app.utils.TermuxUtils;
|
2021-03-16 02:04:55 +00:00
|
|
|
import com.termux.app.utils.TextDataUtils;
|
2021-03-16 14:06:17 +00:00
|
|
|
import com.termux.app.settings.preferences.TermuxPreferenceConstants.TERMUX_APP;
|
2021-03-12 01:01:31 +00:00
|
|
|
|
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
|
|
|
|
public class TermuxSharedPreferences {
|
|
|
|
|
|
|
|
private final Context mContext;
|
|
|
|
private final SharedPreferences mSharedPreferences;
|
|
|
|
|
|
|
|
|
|
|
|
private int MIN_FONTSIZE;
|
|
|
|
private int MAX_FONTSIZE;
|
|
|
|
private int DEFAULT_FONTSIZE;
|
|
|
|
|
|
|
|
public TermuxSharedPreferences(@Nonnull Context context) {
|
2021-03-13 11:49:29 +00:00
|
|
|
mContext = TermuxUtils.getTermuxPackageContext(context);
|
2021-03-12 01:01:31 +00:00
|
|
|
mSharedPreferences = getSharedPreferences(mContext);
|
|
|
|
|
|
|
|
setFontVariables(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static SharedPreferences getSharedPreferences(Context context) {
|
|
|
|
return context.getSharedPreferences(TermuxConstants.TERMUX_DEFAULT_PREFERENCES_FILE_BASENAME_WITHOUT_EXTENSION, Context.MODE_PRIVATE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
Refactor TermuxActivity
This commit majorly refactors `TermuxActivity` and moves its view components and functions into dedicated classes.
- The view layouts and ids have been given meaningful names, like `termux_activity.xml`.
- The `TerminalToolbarViewPager` class has been created to handle the now called toolbar that shows on the bottom of the terminal view. It currently contains extra keys view defined by `terminal_toolbar_extra_keys_view.xml` file and a text input view defined by `terminal_toolbar_text_input_view.xml` file when user can switch to by swiping left. The input text will now be preserved if android destroys the activity or its recreated.
- The `TermuxSessionsListViewController` class has been created to handle view related functionality of the termux sessions list shown in the left drawer, namely view creation, `onItemClick()`, `onItemLongClick()`, etc. Its list view is defined by `termux_activity.xml` file and each item's layout is defined by the `terminal_sessions_list_item.xml` file.
- The `TextDataUtils` class has been added to the `com.termux.app.utils` package for text utils.
- The design for the `SessionChangedCallback` interface for `TerminalSession` has been majorly changed. Firstly, it has been renamed and moved from `TerminalSession` to the dedicated `TerminalSessionClient` class file. The interface now also supports the termux app centralized logging framework so that `TerminalSession` and `TerminalEmulator` can use them. Previously, `TermuxService` was implementing a wrapper interface, which would then call the real interface defined by the `TermuxActivity` if it was currently bound to the service. This cluttered and partially duplicated the code. Now, the implementation is defined by the `TermuxSessionClientBase` and `TermuxSessionClient` classes. The `TermuxSessionClientBase` implements the `TerminalSessionClient` interface but the definition of the activity related functions do not do anything, only the background ones like the logging functions are fully implemented. The `TermuxSessionClient` class inherits from the `TermuxSessionClientBase` class and provides the implementation for the activity related functions. The design for how this works is that if the `TermuxService` is not bound to `TermuxActivity`, it just passes the `TermuxSessionClientBase` implementation to `TerminalSession`. If the activity is bound at some point, then in `onServiceConnected()` it replaces/updates the client objects stored in `TerminalSession` and `TerminalEmulator` with `TermuxSessionClient`, and then replaces them back with `TermuxSessionClientBase` in `onDestroy()`. This seems to be working for now without an issue.
2021-03-16 00:01:09 +00:00
|
|
|
public boolean getShowTerminalToolbar() {
|
2021-03-16 14:06:17 +00:00
|
|
|
return mSharedPreferences.getBoolean(TERMUX_APP.KEY_SHOW_TERMINAL_TOOLBAR, TERMUX_APP.DEFAULT_VALUE_SHOW_TERMINAL_TOOLBAR);
|
2021-03-12 01:01:31 +00:00
|
|
|
}
|
|
|
|
|
Refactor TermuxActivity
This commit majorly refactors `TermuxActivity` and moves its view components and functions into dedicated classes.
- The view layouts and ids have been given meaningful names, like `termux_activity.xml`.
- The `TerminalToolbarViewPager` class has been created to handle the now called toolbar that shows on the bottom of the terminal view. It currently contains extra keys view defined by `terminal_toolbar_extra_keys_view.xml` file and a text input view defined by `terminal_toolbar_text_input_view.xml` file when user can switch to by swiping left. The input text will now be preserved if android destroys the activity or its recreated.
- The `TermuxSessionsListViewController` class has been created to handle view related functionality of the termux sessions list shown in the left drawer, namely view creation, `onItemClick()`, `onItemLongClick()`, etc. Its list view is defined by `termux_activity.xml` file and each item's layout is defined by the `terminal_sessions_list_item.xml` file.
- The `TextDataUtils` class has been added to the `com.termux.app.utils` package for text utils.
- The design for the `SessionChangedCallback` interface for `TerminalSession` has been majorly changed. Firstly, it has been renamed and moved from `TerminalSession` to the dedicated `TerminalSessionClient` class file. The interface now also supports the termux app centralized logging framework so that `TerminalSession` and `TerminalEmulator` can use them. Previously, `TermuxService` was implementing a wrapper interface, which would then call the real interface defined by the `TermuxActivity` if it was currently bound to the service. This cluttered and partially duplicated the code. Now, the implementation is defined by the `TermuxSessionClientBase` and `TermuxSessionClient` classes. The `TermuxSessionClientBase` implements the `TerminalSessionClient` interface but the definition of the activity related functions do not do anything, only the background ones like the logging functions are fully implemented. The `TermuxSessionClient` class inherits from the `TermuxSessionClientBase` class and provides the implementation for the activity related functions. The design for how this works is that if the `TermuxService` is not bound to `TermuxActivity`, it just passes the `TermuxSessionClientBase` implementation to `TerminalSession`. If the activity is bound at some point, then in `onServiceConnected()` it replaces/updates the client objects stored in `TerminalSession` and `TerminalEmulator` with `TermuxSessionClient`, and then replaces them back with `TermuxSessionClientBase` in `onDestroy()`. This seems to be working for now without an issue.
2021-03-16 00:01:09 +00:00
|
|
|
public void setShowTerminalToolbar(boolean value) {
|
2021-03-16 14:06:17 +00:00
|
|
|
mSharedPreferences.edit().putBoolean(TERMUX_APP.KEY_SHOW_TERMINAL_TOOLBAR, value).apply();
|
2021-03-12 01:01:31 +00:00
|
|
|
}
|
|
|
|
|
Refactor TermuxActivity
This commit majorly refactors `TermuxActivity` and moves its view components and functions into dedicated classes.
- The view layouts and ids have been given meaningful names, like `termux_activity.xml`.
- The `TerminalToolbarViewPager` class has been created to handle the now called toolbar that shows on the bottom of the terminal view. It currently contains extra keys view defined by `terminal_toolbar_extra_keys_view.xml` file and a text input view defined by `terminal_toolbar_text_input_view.xml` file when user can switch to by swiping left. The input text will now be preserved if android destroys the activity or its recreated.
- The `TermuxSessionsListViewController` class has been created to handle view related functionality of the termux sessions list shown in the left drawer, namely view creation, `onItemClick()`, `onItemLongClick()`, etc. Its list view is defined by `termux_activity.xml` file and each item's layout is defined by the `terminal_sessions_list_item.xml` file.
- The `TextDataUtils` class has been added to the `com.termux.app.utils` package for text utils.
- The design for the `SessionChangedCallback` interface for `TerminalSession` has been majorly changed. Firstly, it has been renamed and moved from `TerminalSession` to the dedicated `TerminalSessionClient` class file. The interface now also supports the termux app centralized logging framework so that `TerminalSession` and `TerminalEmulator` can use them. Previously, `TermuxService` was implementing a wrapper interface, which would then call the real interface defined by the `TermuxActivity` if it was currently bound to the service. This cluttered and partially duplicated the code. Now, the implementation is defined by the `TermuxSessionClientBase` and `TermuxSessionClient` classes. The `TermuxSessionClientBase` implements the `TerminalSessionClient` interface but the definition of the activity related functions do not do anything, only the background ones like the logging functions are fully implemented. The `TermuxSessionClient` class inherits from the `TermuxSessionClientBase` class and provides the implementation for the activity related functions. The design for how this works is that if the `TermuxService` is not bound to `TermuxActivity`, it just passes the `TermuxSessionClientBase` implementation to `TerminalSession`. If the activity is bound at some point, then in `onServiceConnected()` it replaces/updates the client objects stored in `TerminalSession` and `TerminalEmulator` with `TermuxSessionClient`, and then replaces them back with `TermuxSessionClientBase` in `onDestroy()`. This seems to be working for now without an issue.
2021-03-16 00:01:09 +00:00
|
|
|
public boolean toogleShowTerminalToolbar() {
|
|
|
|
boolean currentValue = getShowTerminalToolbar();
|
|
|
|
setShowTerminalToolbar(!currentValue);
|
2021-03-12 01:01:31 +00:00
|
|
|
return !currentValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public boolean getKeepScreenOn() {
|
2021-03-16 14:06:17 +00:00
|
|
|
return mSharedPreferences.getBoolean(TERMUX_APP.KEY_KEEP_SCREEN_ON, TERMUX_APP.DEFAULT_VALUE_KEEP_SCREEN_ON);
|
2021-03-12 01:01:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setKeepScreenOn(boolean value) {
|
2021-03-16 14:06:17 +00:00
|
|
|
mSharedPreferences.edit().putBoolean(TERMUX_APP.KEY_KEEP_SCREEN_ON, value).apply();
|
2021-03-12 01:01:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void setFontVariables(Context context) {
|
|
|
|
float dipInPixels = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, context.getResources().getDisplayMetrics());
|
|
|
|
|
|
|
|
// This is a bit arbitrary and sub-optimal. We want to give a sensible default for minimum font size
|
|
|
|
// to prevent invisible text due to zoom be mistake:
|
|
|
|
MIN_FONTSIZE = (int) (4f * dipInPixels);
|
|
|
|
|
|
|
|
// http://www.google.com/design/spec/style/typography.html#typography-line-height
|
|
|
|
int defaultFontSize = Math.round(12 * dipInPixels);
|
|
|
|
// Make it divisible by 2 since that is the minimal adjustment step:
|
|
|
|
if (defaultFontSize % 2 == 1) defaultFontSize--;
|
|
|
|
|
|
|
|
DEFAULT_FONTSIZE = defaultFontSize;
|
|
|
|
|
|
|
|
MAX_FONTSIZE = 256;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getFontSize() {
|
|
|
|
int fontSize;
|
|
|
|
String fontString;
|
|
|
|
|
|
|
|
try {
|
2021-03-16 14:06:17 +00:00
|
|
|
fontString = mSharedPreferences.getString(TERMUX_APP.KEY_FONTSIZE, Integer.toString(DEFAULT_FONTSIZE));
|
2021-03-12 01:01:31 +00:00
|
|
|
if(fontString != null)
|
|
|
|
fontSize = Integer.parseInt(fontString);
|
|
|
|
else
|
|
|
|
fontSize = DEFAULT_FONTSIZE;
|
|
|
|
} catch (NumberFormatException | ClassCastException e) {
|
|
|
|
fontSize = DEFAULT_FONTSIZE;
|
|
|
|
}
|
2021-03-16 02:04:55 +00:00
|
|
|
fontSize = TextDataUtils.clamp(fontSize, MIN_FONTSIZE, MAX_FONTSIZE);
|
2021-03-12 01:01:31 +00:00
|
|
|
|
|
|
|
return fontSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setFontSize(String value) {
|
2021-03-16 14:06:17 +00:00
|
|
|
mSharedPreferences.edit().putString(TERMUX_APP.KEY_FONTSIZE, value).apply();
|
2021-03-12 01:01:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void changeFontSize(Context context, boolean increase) {
|
|
|
|
|
|
|
|
int fontSize = getFontSize();
|
|
|
|
|
|
|
|
fontSize += (increase ? 1 : -1) * 2;
|
|
|
|
fontSize = Math.max(MIN_FONTSIZE, Math.min(fontSize, MAX_FONTSIZE));
|
|
|
|
|
|
|
|
setFontSize(Integer.toString(fontSize));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public String getCurrentSession() {
|
2021-03-16 14:06:17 +00:00
|
|
|
return mSharedPreferences.getString(TERMUX_APP.KEY_CURRENT_SESSION, "");
|
2021-03-12 01:01:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setCurrentSession(String value) {
|
2021-03-16 14:06:17 +00:00
|
|
|
mSharedPreferences.edit().putString(TERMUX_APP.KEY_CURRENT_SESSION, value).apply();
|
2021-03-12 01:01:31 +00:00
|
|
|
}
|
|
|
|
|
2021-03-13 11:49:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
public int getLogLevel() {
|
|
|
|
try {
|
2021-03-16 14:06:17 +00:00
|
|
|
return mSharedPreferences.getInt(TERMUX_APP.KEY_LOG_LEVEL, Logger.DEFAULT_LOG_LEVEL);
|
2021-03-13 11:49:29 +00:00
|
|
|
}
|
|
|
|
catch (Exception e) {
|
2021-03-16 14:06:17 +00:00
|
|
|
Logger.logStackTraceWithMessage("Error getting \"" + TERMUX_APP.KEY_LOG_LEVEL + "\" from shared preferences", e);
|
2021-03-13 11:49:29 +00:00
|
|
|
return Logger.DEFAULT_LOG_LEVEL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setLogLevel(Context context, int logLevel) {
|
|
|
|
logLevel = Logger.setLogLevel(context, logLevel);
|
2021-03-16 14:06:17 +00:00
|
|
|
mSharedPreferences.edit().putInt(TERMUX_APP.KEY_LOG_LEVEL, logLevel).apply();
|
2021-03-13 11:49:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public boolean getTerminalViewKeyLoggingEnabled() {
|
2021-03-16 14:06:17 +00:00
|
|
|
return mSharedPreferences.getBoolean(TERMUX_APP.KEY_TERMINAL_VIEW_KEY_LOGGING_ENABLED, TERMUX_APP.DEFAULT_VALUE_TERMINAL_VIEW_KEY_LOGGING_ENABLED);
|
2021-03-13 11:49:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setTerminalViewKeyLoggingEnabled(boolean value) {
|
2021-03-16 14:06:17 +00:00
|
|
|
mSharedPreferences.edit().putBoolean(TERMUX_APP.KEY_TERMINAL_VIEW_KEY_LOGGING_ENABLED, value).apply();
|
2021-03-13 11:49:29 +00:00
|
|
|
}
|
|
|
|
|
2021-03-12 01:01:31 +00:00
|
|
|
}
|