Move general static functions from TermuxSharedProperties to SharedProperties class

This will allow `SharedProperties` class to be used directly to get property values and their internal values via static functions instead of using `TermuxSharedProperties` where the functions don't belong in.

The `TermuxPropertyConstants` classes has been updated to `v0.4.0`. Check its Changelog sections for info on changes.
This commit is contained in:
agnostic-apollo
2021-03-16 22:52:20 +05:00
parent 2e7fd480f4
commit 2a940dfca6
4 changed files with 267 additions and 239 deletions

View File

@@ -14,8 +14,8 @@ import android.os.IBinder;
import com.termux.R; import com.termux.R;
import com.termux.app.TermuxConstants.TERMUX_APP.RUN_COMMAND_SERVICE; import com.termux.app.TermuxConstants.TERMUX_APP.RUN_COMMAND_SERVICE;
import com.termux.app.TermuxConstants.TERMUX_APP.TERMUX_SERVICE; import com.termux.app.TermuxConstants.TERMUX_APP.TERMUX_SERVICE;
import com.termux.app.settings.properties.SharedProperties;
import com.termux.app.settings.properties.TermuxPropertyConstants; import com.termux.app.settings.properties.TermuxPropertyConstants;
import com.termux.app.settings.properties.TermuxSharedProperties;
import com.termux.app.utils.Logger; import com.termux.app.utils.Logger;
import java.io.File; import java.io.File;
@@ -114,7 +114,7 @@ public class RunCommandService extends Service {
} }
// If allow-external-apps property is not set to "true" // If allow-external-apps property is not set to "true"
if (!TermuxSharedProperties.isPropertyValueTrue(this, TermuxPropertyConstants.getTermuxPropertiesFile(), TermuxConstants.PROP_ALLOW_EXTERNAL_APPS)) { if (!SharedProperties.isPropertyValueTrue(this, TermuxPropertyConstants.getTermuxPropertiesFile(), TermuxConstants.PROP_ALLOW_EXTERNAL_APPS)) {
Logger.logError(LOG_TAG, "RunCommandService requires allow-external-apps property to be set to \"true\" in \"" + TermuxConstants.TERMUX_PROPERTIES_PRIMARY_FILE_PATH + "\" file"); Logger.logError(LOG_TAG, "RunCommandService requires allow-external-apps property to be set to \"true\" in \"" + TermuxConstants.TERMUX_PROPERTIES_PRIMARY_FILE_PATH + "\" file");
return Service.START_NOT_STICKY; return Service.START_NOT_STICKY;
} }

View File

@@ -3,6 +3,7 @@ package com.termux.app.settings.properties;
import android.content.Context; import android.content.Context;
import android.widget.Toast; import android.widget.Toast;
import com.google.common.collect.ImmutableBiMap;
import com.google.common.primitives.Primitives; import com.google.common.primitives.Primitives;
import com.termux.app.utils.Logger; import com.termux.app.utils.Logger;
@@ -21,11 +22,16 @@ import javax.annotation.Nullable;
/** /**
* An implementation similar to android's {@link android.content.SharedPreferences} interface for * An implementation similar to android's {@link android.content.SharedPreferences} interface for
* reading and writing to and from ".properties" files which also maintains an in-memory cache for * reading and writing to and from ".properties" files which also maintains an in-memory cache for
* the key/value pairs. Operations are does under synchronization locks and should be thread safe. * the key/value pairs when an instance object is used. Operations are done under
* synchronization locks and should be thread safe.
* *
* Two types of in-memory cache maps are maintained, one for the literal {@link String} values found * If {@link SharedProperties} instance object is used, then two types of in-memory cache maps are
* in the file for the keys and an additional one that stores (near) primitive {@link Object} values for * maintained, one for the literal {@link String} values found in the file for the keys and an
* internal use by the caller. * additional one that stores (near) primitive {@link Object} values for internal use by the caller.
*
* The {@link SharedProperties} also provides static functions that can be used to read properties
* from files or individual key values or even their internal values. An automatic mapping to a
* boolean as internal value can also be done. An in-memory cache is not maintained, nor a locks used.
* *
* This currently only has read support, write support can/will be added later if needed. Check android's * This currently only has read support, write support can/will be added later if needed. Check android's
* SharedPreferencesImpl class for reference implementation. * SharedPreferencesImpl class for reference implementation.
@@ -57,6 +63,20 @@ public class SharedProperties {
private final Object mLock = new Object(); private final Object mLock = new Object();
/** Defines the bidirectional map for boolean values and their internal values */
public static final ImmutableBiMap<String, Boolean> MAP_GENERIC_BOOLEAN =
new ImmutableBiMap.Builder<String, Boolean>()
.put("true", true)
.put("false", false)
.build();
/** Defines the bidirectional map for inverted boolean values and their internal values */
public static final ImmutableBiMap<String, Boolean> MAP_GENERIC_INVERTED_BOOLEAN =
new ImmutableBiMap.Builder<String, Boolean>()
.put("true", false)
.put("false", true)
.build();
private static final String LOG_TAG = "SharedProperties"; private static final String LOG_TAG = "SharedProperties";
/** /**
@@ -65,7 +85,7 @@ public class SharedProperties {
* @param context The Context for operations. * @param context The Context for operations.
* @param propertiesFile The {@link File} object to load properties from. * @param propertiesFile The {@link File} object to load properties from.
* @param propertiesList The {@link Set<String>} object that defined which properties to load. * @param propertiesList The {@link Set<String>} object that defined which properties to load.
* @param sharedPropertiesParser that implements the {@link SharedPropertiesParser} interface. * @param sharedPropertiesParser The implementation of the {@link SharedPropertiesParser} interface.
*/ */
public SharedProperties(@Nonnull Context context, @Nullable File propertiesFile, @Nonnull Set<String> propertiesList, @Nonnull SharedPropertiesParser sharedPropertiesParser) { public SharedProperties(@Nonnull Context context, @Nullable File propertiesFile, @Nonnull Set<String> propertiesList, @Nonnull SharedPropertiesParser sharedPropertiesParser) {
mContext = context; mContext = context;
@@ -102,7 +122,7 @@ public class SharedProperties {
Logger.logDebug(LOG_TAG, key + " : " + value); Logger.logDebug(LOG_TAG, key + " : " + value);
// Call the {@link SharedPropertiesParser#getInternalPropertyValueFromValue(Context,String,String)} // Call the {@link SharedPropertiesParser#getInternalPropertyValueFromValue(Context,String,String)}
// interface method to get the internal value to store in the {@link #mMap}. // interface method to get the internal value to store in the {@link #mMap}.
internalValue = mSharedPropertiesParser.getInternalPropertyValueFromValue(mContext, key, value); internalValue = mSharedPropertiesParser.getInternalPropertyValueFromValue(mContext, key, value);
// If the internal value was successfully added to map, then also add value to newProperties // If the internal value was successfully added to map, then also add value to newProperties
@@ -117,6 +137,178 @@ public class SharedProperties {
} }
} }
/**
* Get the {@link Properties} object for the {@link #mPropertiesFile}. The {@link Properties}
* object will also contain properties not defined by the {@link #mPropertiesList} if cache
* value is {@code false}.
*
* @param cached If {@code true}, then the {@link #mProperties} in-memory cache is returned. Otherwise
* the {@link Properties} object is directly read from the {@link #mPropertiesFile}.
* @return Returns the {@link Properties} object if read from file, otherwise a copy of {@link #mProperties}.
*/
public Properties getProperties(boolean cached) {
synchronized (mLock) {
if (cached) {
if (mProperties == null) mProperties = new Properties();
return getPropertiesCopy(mProperties);
} else {
return getPropertiesFromFile(mContext, mPropertiesFile);
}
}
}
/**
* Get the {@link String} value for the key passed from the {@link #mPropertiesFile}.
*
* @param key The key to read from the {@link Properties} object.
* @param cached If {@code true}, then the value is returned from the {@link #mProperties} in-memory cache.
* Otherwise the {@link Properties} object is read directly from the {@link #mPropertiesFile}
* and value is returned from it against the key.
* @return Returns the {@link String} object. This will be {@code null} if key is not found.
*/
public String getProperty(String key, boolean cached) {
synchronized (mLock) {
return (String) getProperties(cached).get(key);
}
}
/**
* Get the {@link #mMap} object for the {@link #mPropertiesFile}. A call to
* {@link #loadPropertiesFromDisk()} must be made before this.
*
* @return Returns a copy of {@link #mMap} object.
*/
public Map<String, Object> getInternalProperties() {
synchronized (mLock) {
if (mMap == null) mMap = new HashMap<String, Object>();
return getMapCopy(mMap);
}
}
/**
* Get the internal {@link Object} value for the key passed from the {@link #mPropertiesFile}.
* The value is returned from the {@link #mMap} in-memory cache, so a call to
* {@link #loadPropertiesFromDisk()} must be made before this.
*
* @param key The key to read from the {@link #mMap} object.
* @return Returns the {@link Object} object. This will be {@code null} if key is not found or
* if object was {@code null}. Use {@link HashMap#containsKey(Object)} to detect the later.
* situation.
*/
public Object getInternalProperty(String key) {
synchronized (mLock) {
// null keys are not allowed to be stored in mMap
if (key != null)
return getInternalProperties().get(key);
else
return null;
}
}
/**
* A static function to get the {@link Properties} object for the propertiesFile. A lock is not
* taken when this function is called.
*
* @param context The {@link Context} to use to show a flash if an exception is raised while
* reading the file. If context is {@code null}, then flash will not be shown.
* @param propertiesFile The {@link File} to read the {@link Properties} from.
* @return Returns the {@link Properties} object. It will be {@code null} if an exception is
* raised while reading the file.
*/
public static Properties getPropertiesFromFile(Context context, File propertiesFile) {
Properties properties = new Properties();
if (propertiesFile == null) {
Logger.logError(LOG_TAG, "Not loading properties since file is null");
return properties;
}
try {
try (FileInputStream in = new FileInputStream(propertiesFile)) {
Logger.logVerbose(LOG_TAG, "Loading properties from \"" + propertiesFile.getAbsolutePath() + "\" file");
properties.load(new InputStreamReader(in, StandardCharsets.UTF_8));
}
} catch (Exception e) {
if(context != null)
Toast.makeText(context, "Could not open properties file \"" + propertiesFile.getAbsolutePath() + "\": " + e.getMessage(), Toast.LENGTH_LONG).show();
Logger.logStackTraceWithMessage(LOG_TAG, "Error loading properties file \"" + propertiesFile.getAbsolutePath() + "\"", e);
return null;
}
return properties;
}
/**
* A static function to get the {@link String} value for the {@link Properties} key read from
* the propertiesFile file.
*
* @param context The {@link Context} for the {@link #getPropertiesFromFile(Context,File)} call.
* @param propertiesFile The {@link File} to read the {@link Properties} from.
* @param key The key to read.
* @param def The default value.
* @return Returns the {@link String} object. This will be {@code null} if key is not found.
*/
public static String getProperty(Context context, File propertiesFile, String key, String def) {
return (String) getDefaultIfNull(getDefaultIfNull(getPropertiesFromFile(context, propertiesFile), new Properties()).get(key), def);
}
/**
* A static function to get the internal {@link Object} value for the {@link String} value for
* the {@link Properties} key read from the propertiesFile file.
*
* @param context The {@link Context} for the {@link #getPropertiesFromFile(Context,File)} call.
* @param propertiesFile The {@link File} to read the {@link Properties} from.
* @param key The key to read.
* @param sharedPropertiesParser The implementation of the {@link SharedPropertiesParser} interface.
* @return Returns the {@link String} Object returned by the call to
* {@link SharedPropertiesParser#getInternalPropertyValueFromValue(Context,String,String)}.
*/
public static Object getInternalProperty(Context context, File propertiesFile, String key, @Nonnull SharedPropertiesParser sharedPropertiesParser) {
String value = (String) getDefaultIfNull(getPropertiesFromFile(context, propertiesFile), new Properties()).get(key);
// Call the {@link SharedPropertiesParser#getInternalPropertyValueFromValue(Context,String,String)}
// interface method to get the internal value to return.
return sharedPropertiesParser.getInternalPropertyValueFromValue(context, key, value);
}
/**
* A static function to check if the value is {@code true} for {@link Properties} key read from
* the propertiesFile file.
*
* @param context The {@link Context} for the {@link #getPropertiesFromFile(Context,File)}call.
* @param propertiesFile The {@link File} to read the {@link Properties} from.
* @param key The key to read.
* @return Returns the {@code true} if the {@link Properties} key {@link String} value equals "true",
* regardless of case. If the key does not exist in the file or does not equal "true", then
* {@code false} will be returned.
*/
public static boolean isPropertyValueTrue(Context context, File propertiesFile, String key) {
return (boolean) getBooleanValueForStringValue((String) getProperty(context, propertiesFile, key, null), false);
}
/**
* A static function to check if the value is {@code false} for {@link Properties} key read from
* the propertiesFile file.
*
* @param context The {@link Context} for the {@link #getPropertiesFromFile(Context,File)} call.
* @param propertiesFile The {@link File} to read the {@link Properties} from.
* @param key The key to read.
* @return Returns the {@code true} if the {@link Properties} key {@link String} value equals "false",
* regardless of case. If the key does not exist in the file or does not equal "false", then
* {@code true} will be returned.
*/
public static boolean isPropertyValueFalse(Context context, File propertiesFile, String key) {
return (boolean) getInvertedBooleanValueForStringValue((String) getProperty(context, propertiesFile, key, null), true);
}
/** /**
* Put a value in a {@link #mMap}. * Put a value in a {@link #mMap}.
* The key cannot be {@code null}. * The key cannot be {@code null}.
@@ -194,107 +386,6 @@ public class SharedProperties {
return true; return true;
} }
/**
* A static function to get the {@link Properties} object for the propertiesFile. A lock is not
* taken when this function is called.
*
* @param context The {@link Context} to use to show a flash if an exception is raised while
* reading the file. If context is {@code null}, then flash will not be shown.
* @param propertiesFile The {@link File} to read the {@link Properties} from.
* @return Returns the {@link Properties} object. It will be {@code null} if an exception is
* raised while reading the file.
*/
public static Properties getPropertiesFromFile(Context context, File propertiesFile) {
Properties properties = new Properties();
if (propertiesFile == null) {
Logger.logError(LOG_TAG, "Not loading properties since file is null");
return properties;
}
try {
try (FileInputStream in = new FileInputStream(propertiesFile)) {
Logger.logVerbose(LOG_TAG, "Loading properties from \"" + propertiesFile.getAbsolutePath() + "\" file");
properties.load(new InputStreamReader(in, StandardCharsets.UTF_8));
}
} catch (Exception e) {
if(context != null)
Toast.makeText(context, "Could not open properties file \"" + propertiesFile.getAbsolutePath() + "\": " + e.getMessage(), Toast.LENGTH_LONG).show();
Logger.logStackTraceWithMessage(LOG_TAG, "Error loading properties file \"" + propertiesFile.getAbsolutePath() + "\"", e);
return null;
}
return properties;
}
/**
* Get the {@link Properties} object for the {@link #mPropertiesFile}. The {@link Properties}
* object will also contain properties not defined by the {@link #mPropertiesList} if cache
* value is {@code false}.
*
* @param cached If {@code true}, then the {@link #mProperties} in-memory cache is returned. Otherwise
* the {@link Properties} object is directly read from the {@link #mPropertiesFile}.
* @return Returns the {@link Properties} object if read from file, otherwise a copy of {@link #mProperties}.
*/
public Properties getProperties(boolean cached) {
synchronized (mLock) {
if (cached) {
if (mProperties == null) mProperties = new Properties();
return getPropertiesCopy(mProperties);
} else {
return getPropertiesFromFile(mContext, mPropertiesFile);
}
}
}
/**
* Get the {@link String} value for the key passed from the {@link #mPropertiesFile}.
*
* @param key The key to read from the {@link Properties} object.
* @param cached If {@code true}, then the value is returned from the {@link #mProperties} in-memory cache.
* Otherwise the {@link Properties} object is read directly from the {@link #mPropertiesFile}
* and value is returned from it against the key.
* @return Returns the {@link String} object. This will be {@code null} if key is not found.
*/
public String getProperty(String key, boolean cached) {
synchronized (mLock) {
return (String) getProperties(cached).get(key);
}
}
/**
* Get the {@link #mMap} object for the {@link #mPropertiesFile}. A call to
* {@link #loadPropertiesFromDisk()} must be made before this.
*
* @return Returns a copy of {@link #mMap} object.
*/
public Map<String, Object> getInternalProperties() {
synchronized (mLock) {
if (mMap == null) mMap = new HashMap<String, Object>();
return getMapCopy(mMap);
}
}
/**
* Get the internal {@link Object} value for the key passed from the {@link #mPropertiesFile}.
* The value is returned from the {@link #mMap} in-memory cache, so a call to
* {@link #loadPropertiesFromDisk()} must be made before this.
*
* @param key The key to read from the {@link #mMap} object.
* @return Returns the {@link Object} object. This will be {@code null} if key is not found or
* if object was {@code null}. Use {@link HashMap#containsKey(Object)} to detect the later.
* situation.
*/
public Object getInternalProperty(String key) {
synchronized (mLock) {
// null keys are not allowed to be stored in mMap
if (key != null)
return getInternalProperties().get(key);
else
return null;
}
}
public static Properties getPropertiesCopy(Properties inputProperties) { public static Properties getPropertiesCopy(Properties inputProperties) {
if(inputProperties == null) return null; if(inputProperties == null) return null;
@@ -311,4 +402,53 @@ public class SharedProperties {
return new HashMap<String, Object>(map); return new HashMap<String, Object>(map);
} }
/**
* Get the boolean value for the {@link String} value.
*
* @param value The {@link String} value to convert.
* @param def The default {@link boolean} value to return.
* @return Returns {@code true} or {@code false} if value is the literal string "true" or "false" respectively,
* regardless of case. Otherwise returns default value.
*/
public static boolean getBooleanValueForStringValue(String value, boolean def) {
return (boolean) getDefaultIfNull(MAP_GENERIC_BOOLEAN.get(toLowerCase(value)), def);
}
/**
* Get the inverted boolean value for the {@link String} value.
*
* @param value The {@link String} value to convert.
* @param def The default {@link boolean} value to return.
* @return Returns {@code true} or {@code false} if value is the literal string "false" or "true" respectively,
* regardless of case. Otherwise returns default value.
*/
public static boolean getInvertedBooleanValueForStringValue(String value, boolean def) {
return (boolean) getDefaultIfNull(MAP_GENERIC_INVERTED_BOOLEAN.get(toLowerCase(value)), def);
}
/**
* Get the object itself if it is not {@code null}, otherwise default.
*
* @param object The {@link Object} to check.
* @param def The default {@link Object}.
* @return Returns {@code object} if it is not {@code null}, otherwise returns {@code def}.
*/
public static <T> T getDefaultIfNull(@androidx.annotation.Nullable T object, @androidx.annotation.Nullable T def) {
return (object == null) ? def : object;
}
/**
* Covert the {@link String} value to lowercase.
*
* @param value The {@link String} value to convert.
* @return Returns the lowercased value.
*/
public static String toLowerCase(String value) {
if (value == null) return null; else return value.toLowerCase();
}
} }

View File

@@ -10,19 +10,23 @@ import java.util.HashSet;
import java.util.Set; import java.util.Set;
/* /*
* Version: v0.3.0 * Version: v0.4.0
* *
* Changelog * Changelog
* *
* - 0.1.0 (2021-03-11) * - 0.1.0 (2021-03-11)
* - Initial Release. * - Initial Release.
*
* - 0.2.0 (2021-03-11) * - 0.2.0 (2021-03-11)
* - Renamed `HOME_PATH` to `TERMUX_HOME_DIR_PATH` * - Renamed `HOME_PATH` to `TERMUX_HOME_DIR_PATH`
* - Renamed `TERMUX_PROPERTIES_PRIMARY_PATH` to `TERMUX_PROPERTIES_PRIMARY_FILE_PATH` * - Renamed `TERMUX_PROPERTIES_PRIMARY_PATH` to `TERMUX_PROPERTIES_PRIMARY_FILE_PATH`
* - Renamed `TERMUX_PROPERTIES_SECONDARY_FILE_PATH` to `TERMUX_PROPERTIES_SECONDARY_FILE_PATH` * - Renamed `TERMUX_PROPERTIES_SECONDARY_FILE_PATH` to `TERMUX_PROPERTIES_SECONDARY_FILE_PATH`
*
* - 0.3.0 (2021-03-16) * - 0.3.0 (2021-03-16)
* - Add `*TERMINAL_TOOLBAR_HEIGHT_SCALE_FACTOR*` * - Add `*TERMINAL_TOOLBAR_HEIGHT_SCALE_FACTOR*`
* *
* - 0.4.0 (2021-03-16)
* - Removed `MAP_GENERIC_BOOLEAN` and `MAP_GENERIC_INVERTED_BOOLEAN`.
*/ */
/** /**
@@ -38,22 +42,6 @@ import java.util.Set;
*/ */
public final class TermuxPropertyConstants { public final class TermuxPropertyConstants {
/** Defines the bidirectional map for boolean values and their internal values */
public static final ImmutableBiMap<String, Boolean> MAP_GENERIC_BOOLEAN =
new ImmutableBiMap.Builder<String, Boolean>()
.put("true", true)
.put("false", false)
.build();
/** Defines the bidirectional map for inverted boolean values and their internal values */
public static final ImmutableBiMap<String, Boolean> MAP_GENERIC_INVERTED_BOOLEAN =
new ImmutableBiMap.Builder<String, Boolean>()
.put("true", false)
.put("false", true)
.build();
/** Defines the key for whether to use back key as the escape key */ /** Defines the key for whether to use back key as the escape key */
public static final String KEY_USE_BACK_KEY_AS_ESCAPE_KEY = "back-key"; // Default: "back-key" public static final String KEY_USE_BACK_KEY_AS_ESCAPE_KEY = "back-key"; // Default: "back-key"

View File

@@ -3,8 +3,6 @@ package com.termux.app.settings.properties;
import android.content.Context; import android.content.Context;
import android.content.res.Configuration; import android.content.res.Configuration;
import androidx.annotation.Nullable;
import com.termux.app.terminal.io.extrakeys.ExtraKeysInfo; import com.termux.app.terminal.io.extrakeys.ExtraKeysInfo;
import com.termux.app.terminal.io.KeyboardShortcut; import com.termux.app.terminal.io.KeyboardShortcut;
import com.termux.app.utils.Logger; import com.termux.app.utils.Logger;
@@ -39,9 +37,6 @@ public class TermuxSharedProperties implements SharedPropertiesParser {
loadTermuxPropertiesFromDisk(); loadTermuxPropertiesFromDisk();
} }
/** /**
* Reload the termux properties from disk into an in-memory cache. * Reload the termux properties from disk into an in-memory cache.
*/ */
@@ -102,66 +97,6 @@ public class TermuxSharedProperties implements SharedPropertiesParser {
/**
* A static function to get the {@link Properties} from the propertiesFile file.
*
* @param context The {@link Context} for the {@link SharedProperties#getPropertiesFromFile(Context,File)} call.
* @param propertiesFile The {@link File} to read the {@link Properties} from.
* @return Returns the {@link Properties} object. It will be {@code null} if an exception is
* raised while reading the file.
*/
public static Properties getPropertiesFromFile(Context context, File propertiesFile) {
return SharedProperties.getPropertiesFromFile(context, propertiesFile);
}
/**
* A static function to get the {@link String} value for the {@link Properties} key read from
* the propertiesFile file.
*
* @param context The {@link Context} for the {@link SharedProperties#getPropertiesFromFile(Context,File)} call.
* @param propertiesFile The {@link File} to read the {@link Properties} from.
* @param key The key to read.
* @param def The default value.
* @return Returns the {@link String} object. This will be {@code null} if key is not found.
*/
public static String getPropertyValue(Context context, File propertiesFile, String key, String def) {
return (String) getDefaultIfNull(getDefaultIfNull(getPropertiesFromFile(context, propertiesFile), new Properties()).get(key), def);
}
/**
* A static function to check if the value is {@code true} for {@link Properties} key read from
* the propertiesFile file.
*
* @param context The {@link Context} for the {@link SharedProperties#getPropertiesFromFile(Context,File)}call.
* @param propertiesFile The {@link File} to read the {@link Properties} from.
* @param key The key to read.
* @return Returns the {@code true} if the {@link Properties} key {@link String} value equals "true",
* regardless of case. If the key does not exist in the file or does not equal "true", then
* {@code false} will be returned.
*/
public static boolean isPropertyValueTrue(Context context, File propertiesFile, String key) {
return (boolean) getBooleanValueForStringValue((String) getPropertyValue(context, propertiesFile, key, null), false);
}
/**
* A static function to check if the value is {@code false} for {@link Properties} key read from
* the propertiesFile file.
*
* @param context The {@link Context} for the {@link SharedProperties#getPropertiesFromFile(Context,File)} call.
* @param propertiesFile The {@link File} to read the {@link Properties} from.
* @param key The key to read.
* @return Returns the {@code true} if the {@link Properties} key {@link String} value equals "false",
* regardless of case. If the key does not exist in the file or does not equal "false", then
* {@code true} will be returned.
*/
public static boolean isPropertyValueFalse(Context context, File propertiesFile, String key) {
return (boolean) getInvertedBooleanValueForStringValue((String) getPropertyValue(context, propertiesFile, key, null), true);
}
/** /**
* Get the {@link Properties} from the {@link #mPropertiesFile} file. * Get the {@link Properties} from the {@link #mPropertiesFile} file.
* *
@@ -186,7 +121,7 @@ public class TermuxSharedProperties implements SharedPropertiesParser {
* @return Returns the {@link String} object. This will be {@code null} if key is not found. * @return Returns the {@link String} object. This will be {@code null} if key is not found.
*/ */
public String getPropertyValue(String key, String def, boolean cached) { public String getPropertyValue(String key, String def, boolean cached) {
return getDefaultIfNull(mSharedProperties.getProperty(key, cached), def); return SharedProperties.getDefaultIfNull(mSharedProperties.getProperty(key, cached), def);
} }
/** /**
@@ -202,7 +137,7 @@ public class TermuxSharedProperties implements SharedPropertiesParser {
* {@code false} will be returned. * {@code false} will be returned.
*/ */
public boolean isPropertyValueTrue(String key, boolean cached) { public boolean isPropertyValueTrue(String key, boolean cached) {
return (boolean) getBooleanValueForStringValue((String) getPropertyValue(key, null, cached), false); return (boolean) SharedProperties.getBooleanValueForStringValue((String) getPropertyValue(key, null, cached), false);
} }
/** /**
@@ -218,7 +153,7 @@ public class TermuxSharedProperties implements SharedPropertiesParser {
* {@code true} will be returned. * {@code true} will be returned.
*/ */
public boolean isPropertyValueFalse(String key, boolean cached) { public boolean isPropertyValueFalse(String key, boolean cached) {
return (boolean) getInvertedBooleanValueForStringValue((String) getPropertyValue(key, null, cached), true); return (boolean) SharedProperties.getInvertedBooleanValueForStringValue((String) getPropertyValue(key, null, cached), true);
} }
@@ -335,10 +270,10 @@ public class TermuxSharedProperties implements SharedPropertiesParser {
default: default:
// default boolean behaviour // default boolean behaviour
if(TermuxPropertyConstants.TERMUX_DEFAULT_BOOLEAN_BEHAVIOUR_PROPERTIES_LIST.contains(key)) if(TermuxPropertyConstants.TERMUX_DEFAULT_BOOLEAN_BEHAVIOUR_PROPERTIES_LIST.contains(key))
return (boolean) getBooleanValueForStringValue(value, false); return (boolean) SharedProperties.getBooleanValueForStringValue(value, false);
// default inverted boolean behaviour // default inverted boolean behaviour
else if(TermuxPropertyConstants.TERMUX_DEFAULT_INVERETED_BOOLEAN_BEHAVIOUR_PROPERTIES_LIST.contains(key)) else if(TermuxPropertyConstants.TERMUX_DEFAULT_INVERETED_BOOLEAN_BEHAVIOUR_PROPERTIES_LIST.contains(key))
return (boolean) getInvertedBooleanValueForStringValue(value, true); return (boolean) SharedProperties.getInvertedBooleanValueForStringValue(value, true);
// just use String object as is (may be null) // just use String object as is (may be null)
else else
return value; return value;
@@ -349,30 +284,6 @@ public class TermuxSharedProperties implements SharedPropertiesParser {
/**
* Get the boolean value for the {@link String} value.
*
* @param value The {@link String} value to convert.
* @param def The default {@link boolean} value to return.
* @return Returns {@code true} or {@code false} if value is the literal string "true" or "false" respectively,
* regardless of case. Otherwise returns default value.
*/
public static boolean getBooleanValueForStringValue(String value, boolean def) {
return (boolean) getDefaultIfNull(TermuxPropertyConstants.MAP_GENERIC_BOOLEAN.get(toLowerCase(value)), def);
}
/**
* Get the inverted boolean value for the {@link String} value.
*
* @param value The {@link String} value to convert.
* @param def The default {@link boolean} value to return.
* @return Returns {@code true} or {@code false} if value is the literal string "false" or "true" respectively,
* regardless of case. Otherwise returns default value.
*/
public static boolean getInvertedBooleanValueForStringValue(String value, boolean def) {
return (boolean) getDefaultIfNull(TermuxPropertyConstants.MAP_GENERIC_INVERTED_BOOLEAN.get(toLowerCase(value)), def);
}
/** /**
* Returns {@code true} if value is not {@code null} and equals {@link TermuxPropertyConstants#VALUE_BACK_KEY_BEHAVIOUR_ESCAPE}, otherwise false. * Returns {@code true} if value is not {@code null} and equals {@link TermuxPropertyConstants#VALUE_BACK_KEY_BEHAVIOUR_ESCAPE}, otherwise false.
* *
@@ -380,7 +291,7 @@ public class TermuxSharedProperties implements SharedPropertiesParser {
* @return Returns the internal value for value. * @return Returns the internal value for value.
*/ */
public static boolean getUseBackKeyAsEscapeKeyInternalPropertyValueFromValue(String value) { public static boolean getUseBackKeyAsEscapeKeyInternalPropertyValueFromValue(String value) {
return getDefaultIfNull(value, TermuxPropertyConstants.VALUE_BACK_KEY_BEHAVIOUR_BACK).equals(TermuxPropertyConstants.VALUE_BACK_KEY_BEHAVIOUR_ESCAPE); return SharedProperties.getDefaultIfNull(value, TermuxPropertyConstants.VALUE_BACK_KEY_BEHAVIOUR_BACK).equals(TermuxPropertyConstants.VALUE_BACK_KEY_BEHAVIOUR_ESCAPE);
} }
/** /**
@@ -392,7 +303,7 @@ public class TermuxSharedProperties implements SharedPropertiesParser {
*/ */
public static boolean getUseBlackUIInternalPropertyValueFromValue(Context context, String value) { public static boolean getUseBlackUIInternalPropertyValueFromValue(Context context, String value) {
int nightMode = context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK; int nightMode = context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
return getBooleanValueForStringValue(value, nightMode == Configuration.UI_MODE_NIGHT_YES); return SharedProperties.getBooleanValueForStringValue(value, nightMode == Configuration.UI_MODE_NIGHT_YES);
} }
/** /**
@@ -403,7 +314,7 @@ public class TermuxSharedProperties implements SharedPropertiesParser {
* @return Returns the internal value for value. * @return Returns the internal value for value.
*/ */
public static boolean getVolumeKeysDisabledInternalPropertyValueFromValue(String value) { public static boolean getVolumeKeysDisabledInternalPropertyValueFromValue(String value) {
return getDefaultIfNull(value, TermuxPropertyConstants.VALUE_VOLUME_KEY_BEHAVIOUR_VIRTUAL).equals(TermuxPropertyConstants.VALUE_VOLUME_KEY_BEHAVIOUR_VOLUME); return SharedProperties.getDefaultIfNull(value, TermuxPropertyConstants.VALUE_VOLUME_KEY_BEHAVIOUR_VIRTUAL).equals(TermuxPropertyConstants.VALUE_VOLUME_KEY_BEHAVIOUR_VOLUME);
} }
/** /**
@@ -415,7 +326,7 @@ public class TermuxSharedProperties implements SharedPropertiesParser {
* @return Returns the internal value for value. * @return Returns the internal value for value.
*/ */
public static int getBellBehaviourInternalPropertyValueFromValue(String value) { public static int getBellBehaviourInternalPropertyValueFromValue(String value) {
return getDefaultIfNull(TermuxPropertyConstants.MAP_BELL_BEHAVIOUR.get(toLowerCase(value)), TermuxPropertyConstants.DEFAULT_IVALUE_BELL_BEHAVIOUR); return SharedProperties.getDefaultIfNull(TermuxPropertyConstants.MAP_BELL_BEHAVIOUR.get(SharedProperties.toLowerCase(value)), TermuxPropertyConstants.DEFAULT_IVALUE_BELL_BEHAVIOUR);
} }
/** /**
@@ -505,7 +416,7 @@ public class TermuxSharedProperties implements SharedPropertiesParser {
* @return Returns the internal value for value. * @return Returns the internal value for value.
*/ */
public static String getExtraKeysInternalPropertyValueFromValue(String value) { public static String getExtraKeysInternalPropertyValueFromValue(String value) {
return getDefaultIfNull(value, TermuxPropertyConstants.DEFAULT_IVALUE_EXTRA_KEYS); return SharedProperties.getDefaultIfNull(value, TermuxPropertyConstants.DEFAULT_IVALUE_EXTRA_KEYS);
} }
/** /**
@@ -515,7 +426,7 @@ public class TermuxSharedProperties implements SharedPropertiesParser {
* @return Returns the internal value for value. * @return Returns the internal value for value.
*/ */
public static String getExtraKeysStyleInternalPropertyValueFromValue(String value) { public static String getExtraKeysStyleInternalPropertyValueFromValue(String value) {
return getDefaultIfNull(value, TermuxPropertyConstants.DEFAULT_IVALUE_EXTRA_KEYS_STYLE); return SharedProperties.getDefaultIfNull(value, TermuxPropertyConstants.DEFAULT_IVALUE_EXTRA_KEYS_STYLE);
} }
@@ -574,17 +485,6 @@ public class TermuxSharedProperties implements SharedPropertiesParser {
public static <T> T getDefaultIfNull(@Nullable T object, @Nullable T def) {
return (object == null) ? def : object;
}
private static String toLowerCase(String value) {
if (value == null) return null; else return value.toLowerCase();
}
public void dumpPropertiesToLog() { public void dumpPropertiesToLog() {
Properties properties = getProperties(true); Properties properties = getProperties(true);
StringBuilder propertiesDump = new StringBuilder(); StringBuilder propertiesDump = new StringBuilder();
@@ -598,7 +498,7 @@ public class TermuxSharedProperties implements SharedPropertiesParser {
propertiesDump.append(" null"); propertiesDump.append(" null");
} }
Logger.logDebug(LOG_TAG, propertiesDump.toString()); Logger.logVerbose(LOG_TAG, propertiesDump.toString());
} }
public void dumpInternalPropertiesToLog() { public void dumpInternalPropertiesToLog() {
@@ -612,7 +512,7 @@ public class TermuxSharedProperties implements SharedPropertiesParser {
} }
} }
Logger.logDebug(LOG_TAG, internalPropertiesDump.toString()); Logger.logVerbose(LOG_TAG, internalPropertiesDump.toString());
} }
} }