2021-04-07 06:31:30 +00:00
|
|
|
package com.termux.shared.settings.preferences;
|
2021-03-12 01:01:31 +00:00
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.util.TypedValue;
|
|
|
|
|
2021-04-07 06:31:30 +00:00
|
|
|
import com.termux.shared.termux.TermuxConstants;
|
|
|
|
import com.termux.shared.logger.Logger;
|
|
|
|
import com.termux.shared.termux.TermuxUtils;
|
|
|
|
import com.termux.shared.data.DataUtils;
|
|
|
|
import com.termux.shared.settings.preferences.TermuxPreferenceConstants.TERMUX_APP;
|
2021-03-12 01:01:31 +00:00
|
|
|
|
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
|
2021-03-16 15:51:41 +00:00
|
|
|
public class TermuxAppSharedPreferences {
|
2021-03-12 01:01:31 +00:00
|
|
|
|
|
|
|
private final Context mContext;
|
|
|
|
private final SharedPreferences mSharedPreferences;
|
|
|
|
|
|
|
|
|
|
|
|
private int MIN_FONTSIZE;
|
|
|
|
private int MAX_FONTSIZE;
|
|
|
|
private int DEFAULT_FONTSIZE;
|
|
|
|
|
2021-03-16 15:51:41 +00:00
|
|
|
private static final String LOG_TAG = "TermuxAppSharedPreferences";
|
|
|
|
|
|
|
|
public TermuxAppSharedPreferences(@Nonnull Context context) {
|
2021-03-28 04:06:17 +00:00
|
|
|
// We use the default context if failed to get termux package context
|
2021-03-28 04:08:45 +00:00
|
|
|
mContext = DataUtils.getDefaultIfNull(TermuxUtils.getTermuxPackageContext(context), context);
|
2021-03-16 22:16:37 +00:00
|
|
|
mSharedPreferences = getPrivateSharedPreferences(mContext);
|
2021-03-12 01:01:31 +00:00
|
|
|
|
|
|
|
setFontVariables(context);
|
|
|
|
}
|
|
|
|
|
2021-03-16 22:16:37 +00:00
|
|
|
private static SharedPreferences getPrivateSharedPreferences(Context context) {
|
|
|
|
return SharedPreferenceUtils.getPrivateSharedPreferences(context, TermuxConstants.TERMUX_DEFAULT_PREFERENCES_FILE_BASENAME_WITHOUT_EXTENSION);
|
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 getShowTerminalToolbar() {
|
2021-03-16 15:51:41 +00:00
|
|
|
return SharedPreferenceUtils.getBoolean(mSharedPreferences, 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 22:16:37 +00:00
|
|
|
SharedPreferenceUtils.setBoolean(mSharedPreferences, TERMUX_APP.KEY_SHOW_TERMINAL_TOOLBAR, value, false);
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-03-27 02:52:12 +00:00
|
|
|
public boolean getSoftKeyboardEnabled() {
|
|
|
|
return SharedPreferenceUtils.getBoolean(mSharedPreferences, TERMUX_APP.KEY_SOFT_KEYBOARD_ENABLED, TERMUX_APP.DEFAULT_VALUE_KEY_SOFT_KEYBOARD_ENABLED);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setSoftKeyboardEnabled(boolean value) {
|
|
|
|
SharedPreferenceUtils.setBoolean(mSharedPreferences, TERMUX_APP.KEY_SOFT_KEYBOARD_ENABLED, value, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-03-12 01:01:31 +00:00
|
|
|
public boolean getKeepScreenOn() {
|
2021-03-16 15:51:41 +00:00
|
|
|
return SharedPreferenceUtils.getBoolean(mSharedPreferences, 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 22:16:37 +00:00
|
|
|
SharedPreferenceUtils.setBoolean(mSharedPreferences, TERMUX_APP.KEY_KEEP_SCREEN_ON, value, false);
|
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() {
|
2021-03-16 15:51:41 +00:00
|
|
|
int fontSize = SharedPreferenceUtils.getIntStoredAsString(mSharedPreferences, TERMUX_APP.KEY_FONTSIZE, DEFAULT_FONTSIZE);
|
2021-03-28 04:08:45 +00:00
|
|
|
return DataUtils.clamp(fontSize, MIN_FONTSIZE, MAX_FONTSIZE);
|
2021-03-12 01:01:31 +00:00
|
|
|
}
|
|
|
|
|
2021-03-16 15:51:41 +00:00
|
|
|
public void setFontSize(int value) {
|
2021-03-16 22:16:37 +00:00
|
|
|
SharedPreferenceUtils.setIntStoredAsString(mSharedPreferences, TERMUX_APP.KEY_FONTSIZE, value, false);
|
2021-03-12 01:01:31 +00:00
|
|
|
}
|
|
|
|
|
2021-03-16 15:51:41 +00:00
|
|
|
public void changeFontSize(boolean increase) {
|
2021-03-12 01:01:31 +00:00
|
|
|
|
|
|
|
int fontSize = getFontSize();
|
|
|
|
|
|
|
|
fontSize += (increase ? 1 : -1) * 2;
|
|
|
|
fontSize = Math.max(MIN_FONTSIZE, Math.min(fontSize, MAX_FONTSIZE));
|
|
|
|
|
2021-03-16 15:51:41 +00:00
|
|
|
setFontSize(fontSize);
|
2021-03-12 01:01:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public String getCurrentSession() {
|
2021-03-16 15:51:41 +00:00
|
|
|
return SharedPreferenceUtils.getString(mSharedPreferences, TERMUX_APP.KEY_CURRENT_SESSION, null);
|
2021-03-12 01:01:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setCurrentSession(String value) {
|
2021-03-16 22:16:37 +00:00
|
|
|
SharedPreferenceUtils.setString(mSharedPreferences, TERMUX_APP.KEY_CURRENT_SESSION, value, false);
|
2021-03-12 01:01:31 +00:00
|
|
|
}
|
|
|
|
|
2021-03-13 11:49:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
public int getLogLevel() {
|
2021-03-16 15:51:41 +00:00
|
|
|
return SharedPreferenceUtils.getInt(mSharedPreferences, TERMUX_APP.KEY_LOG_LEVEL, Logger.DEFAULT_LOG_LEVEL);
|
2021-03-13 11:49:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setLogLevel(Context context, int logLevel) {
|
|
|
|
logLevel = Logger.setLogLevel(context, logLevel);
|
2021-03-16 22:16:37 +00:00
|
|
|
SharedPreferenceUtils.setInt(mSharedPreferences, TERMUX_APP.KEY_LOG_LEVEL, logLevel, false);
|
2021-03-13 11:49:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-03-23 22:25:25 +00:00
|
|
|
public int getLastNotificationId() {
|
|
|
|
return SharedPreferenceUtils.getInt(mSharedPreferences, TERMUX_APP.KEY_LAST_NOTIFICATION_ID, TERMUX_APP.DEFAULT_VALUE_KEY_LAST_NOTIFICATION_ID);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setLastNotificationId(int notificationId) {
|
|
|
|
SharedPreferenceUtils.setInt(mSharedPreferences, TERMUX_APP.KEY_LAST_NOTIFICATION_ID, notificationId, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-03-13 11:49:29 +00:00
|
|
|
public boolean getTerminalViewKeyLoggingEnabled() {
|
2021-03-16 15:51:41 +00:00
|
|
|
return SharedPreferenceUtils.getBoolean(mSharedPreferences, 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 22:16:37 +00:00
|
|
|
SharedPreferenceUtils.setBoolean(mSharedPreferences, TERMUX_APP.KEY_TERMINAL_VIEW_KEY_LOGGING_ENABLED, value, false);
|
2021-03-13 11:49:29 +00:00
|
|
|
}
|
|
|
|
|
2021-03-19 17:49:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
public boolean getPluginErrorNotificationsEnabled() {
|
|
|
|
return SharedPreferenceUtils.getBoolean(mSharedPreferences, TERMUX_APP.KEY_PLUGIN_ERROR_NOTIFICATIONS_ENABLED, TERMUX_APP.DEFAULT_VALUE_PLUGIN_ERROR_NOTIFICATIONS_ENABLED);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setPluginErrorNotificationsEnabled(boolean value) {
|
|
|
|
SharedPreferenceUtils.setBoolean(mSharedPreferences, TERMUX_APP.KEY_PLUGIN_ERROR_NOTIFICATIONS_ENABLED, value, false);
|
|
|
|
}
|
|
|
|
|
2021-04-06 07:48:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
public boolean getCrashReportNotificationsEnabled() {
|
|
|
|
return SharedPreferenceUtils.getBoolean(mSharedPreferences, TERMUX_APP.KEY_CRASH_REPORT_NOTIFICATIONS_ENABLED, TERMUX_APP.DEFAULT_VALUE_CRASH_REPORT_NOTIFICATIONS_ENABLED);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setCrashReportNotificationsEnabled(boolean value) {
|
|
|
|
SharedPreferenceUtils.setBoolean(mSharedPreferences, TERMUX_APP.KEY_CRASH_REPORT_NOTIFICATIONS_ENABLED, value, false);
|
|
|
|
}
|
|
|
|
|
2021-03-12 01:01:31 +00:00
|
|
|
}
|