Create termux-shared library package for all termux constants and shared utils

The termux plugins should use this library instead of hardcoding "com.termux" values in their source code.

The library can be included as a dependency by plugins and third party apps by including the following line in the build.gradle where x.xxx is the version number, once its published.

`implementation 'com.termux:termux-shared:x.xxx'`

The `TermuxConstants` class has been updated to `v0.17.0`, `TermuxPreferenceConstants` to `v0.9.0` and `TermuxPropertyConstants` to `v0.6.0`. Check their Changelog sections for info on changes.

Some typos and redundant code has also been fixed.
This commit is contained in:
agnostic-apollo
2021-04-07 11:31:30 +05:00
parent c9a476caf7
commit 682ce08314
73 changed files with 746 additions and 520 deletions

View File

@@ -0,0 +1,395 @@
package com.termux.shared.settings.preferences;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import com.termux.shared.logger.Logger;
import java.util.Set;
public class SharedPreferenceUtils {
private static final String LOG_TAG = "SharedPreferenceUtils";
/**
* Get {@link SharedPreferences} instance of the preferences file 'name' with the operating mode
* {@link Context#MODE_PRIVATE}. This file will be created in the app package's default
* shared preferences directory.
*
* @param context The {@link Context} to get the {@link SharedPreferences} instance.
* @param name The preferences file basename without extension.
* @return The single {@link SharedPreferences} instance that can be used to retrieve and
* modify the preference values.
*/
public static SharedPreferences getPrivateSharedPreferences(Context context, String name) {
return context.getSharedPreferences(name, Context.MODE_PRIVATE);
}
/**
* Get {@link SharedPreferences} instance of the preferences file 'name' with the operating mode
* {@link Context#MODE_PRIVATE} and {@link Context#MODE_MULTI_PROCESS}. This file will be
* created in the app package's default shared preferences directory.
*
* @param context The {@link Context} to get the {@link SharedPreferences} instance.
* @param name The preferences file basename without extension.
* @return The single {@link SharedPreferences} instance that can be used to retrieve and
* modify the preference values.
*/
public static SharedPreferences getPrivateAndMultiProcessSharedPreferences(Context context, String name) {
return context.getSharedPreferences(name, Context.MODE_PRIVATE | Context.MODE_MULTI_PROCESS);
}
/**
* Get a {@code boolean} from {@link SharedPreferences}.
*
* @param sharedPreferences The {@link SharedPreferences} to get the value from.
* @param key The key for the value.
* @param def The default value if failed to read a valid value.
* @return Returns the {@code boolean} value stored in {@link SharedPreferences}, otherwise returns
* default if failed to read a valid value, like in case of an exception.
*/
public static boolean getBoolean(SharedPreferences sharedPreferences, String key, boolean def) {
if(sharedPreferences == null) {
Logger.logError(LOG_TAG, "Error getting boolean value for the \"" + key + "\" key from null shared preferences. Returning default value \"" + def + "\".");
return def;
}
try {
return sharedPreferences.getBoolean(key, def);
}
catch (ClassCastException e) {
Logger.logStackTraceWithMessage(LOG_TAG, "Error getting boolean value for the \"" + key + "\" key from shared preferences. Returning default value \"" + def + "\".", e);
return def;
}
}
/**
* Set a {@code boolean} in {@link SharedPreferences}.
*
* @param sharedPreferences The {@link SharedPreferences} to set the value in.
* @param key The key for the value.
* @param value The value to store.
* @param commitToFile If set to {@code true}, then value will be set to shared preferences
* in-memory cache and the file synchronously. Ideally, only to be used for
* multi-process use-cases.
*/
@SuppressLint("ApplySharedPref")
public static void setBoolean(SharedPreferences sharedPreferences, String key, boolean value, boolean commitToFile) {
if(sharedPreferences == null) {
Logger.logError(LOG_TAG, "Ignoring setting boolean value \"" + value + "\" for the \"" + key + "\" key into null shared preferences.");
return;
}
if(commitToFile)
sharedPreferences.edit().putBoolean(key, value).commit();
else
sharedPreferences.edit().putBoolean(key, value).apply();
}
/**
* Get a {@code float} from {@link SharedPreferences}.
*
* @param sharedPreferences The {@link SharedPreferences} to get the value from.
* @param key The key for the value.
* @param def The default value if failed to read a valid value.
* @return Returns the {@code float} value stored in {@link SharedPreferences}, otherwise returns
* default if failed to read a valid value, like in case of an exception.
*/
public static float getFloat(SharedPreferences sharedPreferences, String key, float def) {
if(sharedPreferences == null) {
Logger.logError(LOG_TAG, "Error getting float value for the \"" + key + "\" key from null shared preferences. Returning default value \"" + def + "\".");
return def;
}
try {
return sharedPreferences.getFloat(key, def);
}
catch (ClassCastException e) {
Logger.logStackTraceWithMessage(LOG_TAG, "Error getting float value for the \"" + key + "\" key from shared preferences. Returning default value \"" + def + "\".", e);
return def;
}
}
/**
* Set a {@code float} in {@link SharedPreferences}.
*
* @param sharedPreferences The {@link SharedPreferences} to set the value in.
* @param key The key for the value.
* @param value The value to store.
* @param commitToFile If set to {@code true}, then value will be set to shared preferences
* in-memory cache and the file synchronously. Ideally, only to be used for
* multi-process use-cases.
*/
@SuppressLint("ApplySharedPref")
public static void setFloat(SharedPreferences sharedPreferences, String key, float value, boolean commitToFile) {
if(sharedPreferences == null) {
Logger.logError(LOG_TAG, "Ignoring setting float value \"" + value + "\" for the \"" + key + "\" key into null shared preferences.");
return;
}
if(commitToFile)
sharedPreferences.edit().putFloat(key, value).commit();
else
sharedPreferences.edit().putFloat(key, value).apply();
}
/**
* Get an {@code int} from {@link SharedPreferences}.
*
* @param sharedPreferences The {@link SharedPreferences} to get the value from.
* @param key The key for the value.
* @param def The default value if failed to read a valid value.
* @return Returns the {@code int} value stored in {@link SharedPreferences}, otherwise returns
* default if failed to read a valid value, like in case of an exception.
*/
public static int getInt(SharedPreferences sharedPreferences, String key, int def) {
if(sharedPreferences == null) {
Logger.logError(LOG_TAG, "Error getting int value for the \"" + key + "\" key from null shared preferences. Returning default value \"" + def + "\".");
return def;
}
try {
return sharedPreferences.getInt(key, def);
}
catch (ClassCastException e) {
Logger.logStackTraceWithMessage(LOG_TAG, "Error getting int value for the \"" + key + "\" key from shared preferences. Returning default value \"" + def + "\".", e);
return def;
}
}
/**
* Set an {@code int} in {@link SharedPreferences}.
*
* @param sharedPreferences The {@link SharedPreferences} to set the value in.
* @param key The key for the value.
* @param value The value to store.
* @param commitToFile If set to {@code true}, then value will be set to shared preferences
* in-memory cache and the file synchronously. Ideally, only to be used for
* multi-process use-cases.
*/
@SuppressLint("ApplySharedPref")
public static void setInt(SharedPreferences sharedPreferences, String key, int value, boolean commitToFile) {
if(sharedPreferences == null) {
Logger.logError(LOG_TAG, "Ignoring setting int value \"" + value + "\" for the \"" + key + "\" key into null shared preferences.");
return;
}
if(commitToFile)
sharedPreferences.edit().putInt(key, value).commit();
else
sharedPreferences.edit().putInt(key, value).apply();
}
/**
* Get a {@code long} from {@link SharedPreferences}.
*
* @param sharedPreferences The {@link SharedPreferences} to get the value from.
* @param key The key for the value.
* @param def The default value if failed to read a valid value.
* @return Returns the {@code long} value stored in {@link SharedPreferences}, otherwise returns
* default if failed to read a valid value, like in case of an exception.
*/
public static long getLong(SharedPreferences sharedPreferences, String key, long def) {
if(sharedPreferences == null) {
Logger.logError(LOG_TAG, "Error getting long value for the \"" + key + "\" key from null shared preferences. Returning default value \"" + def + "\".");
return def;
}
try {
return sharedPreferences.getLong(key, def);
}
catch (ClassCastException e) {
Logger.logStackTraceWithMessage(LOG_TAG, "Error getting long value for the \"" + key + "\" key from shared preferences. Returning default value \"" + def + "\".", e);
return def;
}
}
/**
* Set a {@code long} in {@link SharedPreferences}.
*
* @param sharedPreferences The {@link SharedPreferences} to set the value in.
* @param key The key for the value.
* @param value The value to store.
* @param commitToFile If set to {@code true}, then value will be set to shared preferences
* in-memory cache and the file synchronously. Ideally, only to be used for
* multi-process use-cases.
*/
@SuppressLint("ApplySharedPref")
public static void setLong(SharedPreferences sharedPreferences, String key, long value, boolean commitToFile) {
if(sharedPreferences == null) {
Logger.logError(LOG_TAG, "Ignoring setting long value \"" + value + "\" for the \"" + key + "\" key into null shared preferences.");
return;
}
if(commitToFile)
sharedPreferences.edit().putLong(key, value).commit();
else
sharedPreferences.edit().putLong(key, value).apply();
}
/**
* Get a {@code String} from {@link SharedPreferences}.
*
* @param sharedPreferences The {@link SharedPreferences} to get the value from.
* @param key The key for the value.
* @param def The default value if failed to read a valid value.
* @return Returns the {@code String} value stored in {@link SharedPreferences}, otherwise returns
* default if failed to read a valid value, like in case of an exception.
*/
public static String getString(SharedPreferences sharedPreferences, String key, String def) {
if(sharedPreferences == null) {
Logger.logError(LOG_TAG, "Error getting String value for the \"" + key + "\" key from null shared preferences. Returning default value \"" + def + "\".");
return def;
}
try {
return sharedPreferences.getString(key, def);
}
catch (ClassCastException e) {
Logger.logStackTraceWithMessage(LOG_TAG, "Error getting String value for the \"" + key + "\" key from shared preferences. Returning default value \"" + def + "\".", e);
return def;
}
}
/**
* Set a {@code String} in {@link SharedPreferences}.
*
* @param sharedPreferences The {@link SharedPreferences} to set the value in.
* @param key The key for the value.
* @param value The value to store.
* @param commitToFile If set to {@code true}, then value will be set to shared preferences
* in-memory cache and the file synchronously. Ideally, only to be used for
* multi-process use-cases.
*/
@SuppressLint("ApplySharedPref")
public static void setString(SharedPreferences sharedPreferences, String key, String value, boolean commitToFile) {
if(sharedPreferences == null) {
Logger.logError(LOG_TAG, "Ignoring setting String value \"" + value + "\" for the \"" + key + "\" key into null shared preferences.");
return;
}
if(commitToFile)
sharedPreferences.edit().putString(key, value).commit();
else
sharedPreferences.edit().putString(key, value).apply();
}
/**
* Get a {@code Set<String>} from {@link SharedPreferences}.
*
* @param sharedPreferences The {@link SharedPreferences} to get the value from.
* @param key The key for the value.
* @param def The default value if failed to read a valid value.
* @return Returns the {@code Set<String>} value stored in {@link SharedPreferences}, otherwise returns
* default if failed to read a valid value, like in case of an exception.
*/
public static Set<String> getStringSet(SharedPreferences sharedPreferences, String key, Set<String> def) {
if(sharedPreferences == null) {
Logger.logError(LOG_TAG, "Error getting Set<String> value for the \"" + key + "\" key from null shared preferences. Returning default value \"" + def + "\".");
return def;
}
try {
return sharedPreferences.getStringSet(key, def);
}
catch (ClassCastException e) {
Logger.logStackTraceWithMessage(LOG_TAG, "Error getting Set<String> value for the \"" + key + "\" key from shared preferences. Returning default value \"" + def + "\".", e);
return def;
}
}
/**
* Set a {@code Set<String>} in {@link SharedPreferences}.
*
* @param sharedPreferences The {@link SharedPreferences} to set the value in.
* @param key The key for the value.
* @param value The value to store.
* @param commitToFile If set to {@code true}, then value will be set to shared preferences
* in-memory cache and the file synchronously. Ideally, only to be used for
* multi-process use-cases.
*/
@SuppressLint("ApplySharedPref")
public static void setStringSet(SharedPreferences sharedPreferences, String key, Set<String> value, boolean commitToFile) {
if(sharedPreferences == null) {
Logger.logError(LOG_TAG, "Ignoring setting Set<String> value \"" + value + "\" for the \"" + key + "\" key into null shared preferences.");
return;
}
if(commitToFile)
sharedPreferences.edit().putStringSet(key, value).commit();
else
sharedPreferences.edit().putStringSet(key, value).apply();
}
/**
* Get an {@code int} from {@link SharedPreferences} that is stored as a {@link String}.
*
* @param sharedPreferences The {@link SharedPreferences} to get the value from.
* @param key The key for the value.
* @param def The default value if failed to read a valid value.
* @return Returns the {@code int} value after parsing the {@link String} value stored in
* {@link SharedPreferences}, otherwise returns default if failed to read a valid value,
* like in case of an exception.
*/
public static int getIntStoredAsString(SharedPreferences sharedPreferences, String key, int def) {
if(sharedPreferences == null) {
Logger.logError(LOG_TAG, "Error getting int value for the \"" + key + "\" key from null shared preferences. Returning default value \"" + def + "\".");
return def;
}
String stringValue;
int intValue;
try {
stringValue = sharedPreferences.getString(key, Integer.toString(def));
if(stringValue != null)
intValue = Integer.parseInt(stringValue);
else
intValue = def;
} catch (NumberFormatException | ClassCastException e) {
intValue = def;
}
return intValue;
}
/**
* Set an {@code int} into {@link SharedPreferences} that is stored as a {@link String}.
*
* @param sharedPreferences The {@link SharedPreferences} to set the value in.
* @param key The key for the value.
* @param value The value to store.
* @param commitToFile If set to {@code true}, then value will be set to shared preferences
* in-memory cache and the file synchronously. Ideally, only to be used for
* multi-process use-cases.
*/
@SuppressLint("ApplySharedPref")
public static void setIntStoredAsString(SharedPreferences sharedPreferences, String key, int value, boolean commitToFile) {
if(sharedPreferences == null) {
Logger.logError(LOG_TAG, "Ignoring setting int value \"" + value + "\" for the \"" + key + "\" key into null shared preferences.");
return;
}
if(commitToFile)
sharedPreferences.edit().putString(key, Integer.toString(value)).commit();
else
sharedPreferences.edit().putString(key, Integer.toString(value)).apply();
}
}

View File

@@ -0,0 +1,174 @@
package com.termux.shared.settings.preferences;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.TypedValue;
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;
import javax.annotation.Nonnull;
public class TermuxAppSharedPreferences {
private final Context mContext;
private final SharedPreferences mSharedPreferences;
private int MIN_FONTSIZE;
private int MAX_FONTSIZE;
private int DEFAULT_FONTSIZE;
private static final String LOG_TAG = "TermuxAppSharedPreferences";
public TermuxAppSharedPreferences(@Nonnull Context context) {
// We use the default context if failed to get termux package context
mContext = DataUtils.getDefaultIfNull(TermuxUtils.getTermuxPackageContext(context), context);
mSharedPreferences = getPrivateSharedPreferences(mContext);
setFontVariables(context);
}
private static SharedPreferences getPrivateSharedPreferences(Context context) {
return SharedPreferenceUtils.getPrivateSharedPreferences(context, TermuxConstants.TERMUX_DEFAULT_PREFERENCES_FILE_BASENAME_WITHOUT_EXTENSION);
}
public boolean getShowTerminalToolbar() {
return SharedPreferenceUtils.getBoolean(mSharedPreferences, TERMUX_APP.KEY_SHOW_TERMINAL_TOOLBAR, TERMUX_APP.DEFAULT_VALUE_SHOW_TERMINAL_TOOLBAR);
}
public void setShowTerminalToolbar(boolean value) {
SharedPreferenceUtils.setBoolean(mSharedPreferences, TERMUX_APP.KEY_SHOW_TERMINAL_TOOLBAR, value, false);
}
public boolean toogleShowTerminalToolbar() {
boolean currentValue = getShowTerminalToolbar();
setShowTerminalToolbar(!currentValue);
return !currentValue;
}
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);
}
public boolean getKeepScreenOn() {
return SharedPreferenceUtils.getBoolean(mSharedPreferences, TERMUX_APP.KEY_KEEP_SCREEN_ON, TERMUX_APP.DEFAULT_VALUE_KEEP_SCREEN_ON);
}
public void setKeepScreenOn(boolean value) {
SharedPreferenceUtils.setBoolean(mSharedPreferences, TERMUX_APP.KEY_KEEP_SCREEN_ON, value, false);
}
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 = SharedPreferenceUtils.getIntStoredAsString(mSharedPreferences, TERMUX_APP.KEY_FONTSIZE, DEFAULT_FONTSIZE);
return DataUtils.clamp(fontSize, MIN_FONTSIZE, MAX_FONTSIZE);
}
public void setFontSize(int value) {
SharedPreferenceUtils.setIntStoredAsString(mSharedPreferences, TERMUX_APP.KEY_FONTSIZE, value, false);
}
public void changeFontSize(boolean increase) {
int fontSize = getFontSize();
fontSize += (increase ? 1 : -1) * 2;
fontSize = Math.max(MIN_FONTSIZE, Math.min(fontSize, MAX_FONTSIZE));
setFontSize(fontSize);
}
public String getCurrentSession() {
return SharedPreferenceUtils.getString(mSharedPreferences, TERMUX_APP.KEY_CURRENT_SESSION, null);
}
public void setCurrentSession(String value) {
SharedPreferenceUtils.setString(mSharedPreferences, TERMUX_APP.KEY_CURRENT_SESSION, value, false);
}
public int getLogLevel() {
return SharedPreferenceUtils.getInt(mSharedPreferences, TERMUX_APP.KEY_LOG_LEVEL, Logger.DEFAULT_LOG_LEVEL);
}
public void setLogLevel(Context context, int logLevel) {
logLevel = Logger.setLogLevel(context, logLevel);
SharedPreferenceUtils.setInt(mSharedPreferences, TERMUX_APP.KEY_LOG_LEVEL, logLevel, false);
}
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);
}
public boolean getTerminalViewKeyLoggingEnabled() {
return SharedPreferenceUtils.getBoolean(mSharedPreferences, TERMUX_APP.KEY_TERMINAL_VIEW_KEY_LOGGING_ENABLED, TERMUX_APP.DEFAULT_VALUE_TERMINAL_VIEW_KEY_LOGGING_ENABLED);
}
public void setTerminalViewKeyLoggingEnabled(boolean value) {
SharedPreferenceUtils.setBoolean(mSharedPreferences, TERMUX_APP.KEY_TERMINAL_VIEW_KEY_LOGGING_ENABLED, value, false);
}
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);
}
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);
}
}

View File

@@ -0,0 +1,138 @@
package com.termux.shared.settings.preferences;
/*
* Version: v0.9.0
*
* Changelog
*
* - 0.1.0 (2021-03-12)
* - Initial Release.
*
* - 0.2.0 (2021-03-13)
* - Added `KEY_LOG_LEVEL` and `KEY_TERMINAL_VIEW_LOGGING_ENABLED`.
*
* - 0.3.0 (2021-03-16)
* - Changed to per app scoping of variables so that the same file can store all constants of
* Termux app and its plugins. This will allow {@link com.termux.app.TermuxSettings} to
* manage preferences of plugins as well if they don't have launcher activity themselves
* and also allow plugin apps to make changes to preferences from background.
* - Added following to `TERMUX_TASKER_APP`:
* `KEY_LOG_LEVEL`.
*
* - 0.4.0 (2021-03-13)
* - Added following to `TERMUX_APP`:
* `KEY_PLUGIN_ERROR_NOTIFICATIONS_ENABLED` and `DEFAULT_VALUE_PLUGIN_ERROR_NOTIFICATIONS_ENABLED`.
*
* - 0.5.0 (2021-03-24)
* - Added following to `TERMUX_APP`:
* `KEY_LAST_NOTIFICATION_ID` and `DEFAULT_VALUE_KEY_LAST_NOTIFICATION_ID`.
*
* - 0.6.0 (2021-03-24)
* - Change `DEFAULT_VALUE_KEEP_SCREEN_ON` value to `false` in `TERMUX_APP`.
*
* - 0.7.0 (2021-03-27)
* - Added following to `TERMUX_APP`:
* `KEY_SOFT_KEYBOARD_ENABLED` and `DEFAULT_VALUE_KEY_SOFT_KEYBOARD_ENABLED`.
*
* - 0.8.0 (2021-04-06)
* - Added following to `TERMUX_APP`:
* `KEY_CRASH_REPORT_NOTIFICATIONS_ENABLED` and `DEFAULT_VALUE_CRASH_REPORT_NOTIFICATIONS_ENABLED`.
*
* - 0.9.0 (2021-04-07)
* - Updated javadocs.
*/
/**
* A class that defines shared constants of the SharedPreferences used by Termux app and its plugins.
* This class will be hosted by termux-shared lib and should be imported by other termux plugin
* apps as is instead of copying constants to random classes. The 3rd party apps can also import
* it for interacting with termux apps. If changes are made to this file, increment the version number
* and add an entry in the Changelog section above.
*/
public final class TermuxPreferenceConstants {
/**
* Termux app constants.
*/
public static final class TERMUX_APP {
/**
* Defines the key for whether to show terminal toolbar containing extra keys and text input field.
*/
public static final String KEY_SHOW_TERMINAL_TOOLBAR = "show_extra_keys";
public static final boolean DEFAULT_VALUE_SHOW_TERMINAL_TOOLBAR = true;
/**
* Defines the key for whether the soft keyboard will be enabled, for cases where users want
* to use a hardware keyboard instead.
*/
public static final String KEY_SOFT_KEYBOARD_ENABLED = "soft_keyboard_enabled";
public static final boolean DEFAULT_VALUE_KEY_SOFT_KEYBOARD_ENABLED = true;
/**
* Defines the key for whether to always keep screen on.
*/
public static final String KEY_KEEP_SCREEN_ON = "screen_always_on";
public static final boolean DEFAULT_VALUE_KEEP_SCREEN_ON = false;
/**
* Defines the key for font size of termux terminal view.
*/
public static final String KEY_FONTSIZE = "fontsize";
/**
* Defines the key for current termux terminal session.
*/
public static final String KEY_CURRENT_SESSION = "current_session";
/**
* Defines the key for current termux log level.
*/
public static final String KEY_LOG_LEVEL = "log_level";
/**
* Defines the key for last used notification id.
*/
public static final String KEY_LAST_NOTIFICATION_ID = "last_notification_id";
public static final int DEFAULT_VALUE_KEY_LAST_NOTIFICATION_ID = 0;
/**
* Defines the key for whether termux terminal view key logging is enabled or not
*/
public static final String KEY_TERMINAL_VIEW_KEY_LOGGING_ENABLED = "terminal_view_key_logging_enabled";
public static final boolean DEFAULT_VALUE_TERMINAL_VIEW_KEY_LOGGING_ENABLED = false;
/**
* Defines the key for whether flashes and notifications for plugin errors are enabled or not.
*/
public static final String KEY_PLUGIN_ERROR_NOTIFICATIONS_ENABLED = "plugin_error_notifications_enabled";
public static final boolean DEFAULT_VALUE_PLUGIN_ERROR_NOTIFICATIONS_ENABLED = true;
/**
* Defines the key for whether notifications for crash reports are enabled or not.
*/
public static final String KEY_CRASH_REPORT_NOTIFICATIONS_ENABLED = "crash_report_notifications_enabled";
public static final boolean DEFAULT_VALUE_CRASH_REPORT_NOTIFICATIONS_ENABLED = true;
}
/**
* Termux Tasker app constants.
*/
public static final class TERMUX_TASKER_APP {
/**
* Defines the key for current termux log level.
*/
public static final String KEY_LOG_LEVEL = "log_level";
}
}

View File

@@ -0,0 +1,52 @@
package com.termux.shared.settings.preferences;
import android.content.Context;
import android.content.SharedPreferences;
import com.termux.shared.termux.TermuxConstants;
import com.termux.shared.settings.preferences.TermuxPreferenceConstants.TERMUX_TASKER_APP;
import com.termux.shared.data.DataUtils;
import com.termux.shared.logger.Logger;
import com.termux.shared.termux.TermuxUtils;
import javax.annotation.Nonnull;
public class TermuxTaskerAppSharedPreferences {
private final Context mContext;
private final SharedPreferences mSharedPreferences;
private final SharedPreferences mMultiProcessSharedPreferences;
private static final String LOG_TAG = "TermuxTaskerAppSharedPreferences";
public TermuxTaskerAppSharedPreferences(@Nonnull Context context) {
// We use the default context if failed to get termux-tasker package context
mContext = DataUtils.getDefaultIfNull(TermuxUtils.getTermuxTaskerPackageContext(context), context);
mSharedPreferences = getPrivateSharedPreferences(mContext);
mMultiProcessSharedPreferences = getPrivateAndMultiProcessSharedPreferences(mContext);
}
private static SharedPreferences getPrivateSharedPreferences(Context context) {
return SharedPreferenceUtils.getPrivateSharedPreferences(context, TermuxConstants.TERMUX_TASKER_DEFAULT_PREFERENCES_FILE_BASENAME_WITHOUT_EXTENSION);
}
private static SharedPreferences getPrivateAndMultiProcessSharedPreferences(Context context) {
return SharedPreferenceUtils.getPrivateAndMultiProcessSharedPreferences(context, TermuxConstants.TERMUX_TASKER_DEFAULT_PREFERENCES_FILE_BASENAME_WITHOUT_EXTENSION);
}
public int getLogLevel(boolean readFromFfile) {
if(readFromFfile)
return SharedPreferenceUtils.getInt(mMultiProcessSharedPreferences, TERMUX_TASKER_APP.KEY_LOG_LEVEL, Logger.DEFAULT_LOG_LEVEL);
else
return SharedPreferenceUtils.getInt(mSharedPreferences, TERMUX_TASKER_APP.KEY_LOG_LEVEL, Logger.DEFAULT_LOG_LEVEL);
}
public void setLogLevel(Context context, int logLevel, boolean commitToFile) {
logLevel = Logger.setLogLevel(context, logLevel);
SharedPreferenceUtils.setInt(mSharedPreferences, TERMUX_TASKER_APP.KEY_LOG_LEVEL, logLevel, commitToFile);
}
}