Added: Add support for ~/.termux/termux.float.properties

This commit is contained in:
agnostic-apollo
2021-09-02 12:45:28 +05:00
parent 9f1203f049
commit bc779d2ffb
8 changed files with 66 additions and 20 deletions

View File

@@ -1,5 +1,7 @@
package com.termux.shared.settings.properties;
import androidx.annotation.NonNull;
import com.google.common.collect.ImmutableBiMap;
import com.termux.shared.termux.TermuxConstants;
import com.termux.shared.logger.Logger;
@@ -12,7 +14,7 @@ import java.util.HashSet;
import java.util.Set;
/*
* Version: v0.13.0
* Version: v0.14.0
*
* Changelog
*
@@ -58,6 +60,9 @@ import java.util.Set;
*
* - 0.13.0 (2021-08-25)
* - Add `*KEY_TERMINAL_MARGIN_HORIZONTAL*` and `*KEY_TERMINAL_MARGIN_VERTICAL*`.
*
* - 0.14.0 (2021-09-02)
* - Add `getTermuxFloatPropertiesFile()`.
*/
/**
@@ -390,11 +395,28 @@ public final class TermuxPropertyConstants {
* @return Returns the {@link File} object for termux properties.
*/
public static File getTermuxPropertiesFile() {
String[] possiblePropertiesFileLocations = {
return getPropertiesFile(new String[]{
TermuxConstants.TERMUX_PROPERTIES_PRIMARY_FILE_PATH,
TermuxConstants.TERMUX_PROPERTIES_SECONDARY_FILE_PATH
};
});
}
/** Returns the first {@link File} found at
* {@link TermuxConstants#TERMUX_FLOAT_PROPERTIES_PRIMARY_FILE_PATH} or
* {@link TermuxConstants#TERMUX_FLOAT_PROPERTIES_SECONDARY_FILE_PATH}
* from which termux properties can be loaded.
* If the {@link File} found is not a regular file or is not readable then null is returned.
*
* @return Returns the {@link File} object for termux properties.
*/
public static File getTermuxFloatPropertiesFile() {
return getPropertiesFile(new String[]{
TermuxConstants.TERMUX_FLOAT_PROPERTIES_PRIMARY_FILE_PATH,
TermuxConstants.TERMUX_FLOAT_PROPERTIES_SECONDARY_FILE_PATH
});
}
public static File getPropertiesFile(@NonNull String[] possiblePropertiesFileLocations) {
File propertiesFile = new File(possiblePropertiesFileLocations[0]);
int i = 0;
while (!propertiesFile.exists() && i < possiblePropertiesFileLocations.length) {
@@ -405,7 +427,7 @@ public final class TermuxPropertyConstants {
if (propertiesFile.isFile() && propertiesFile.canRead()) {
return propertiesFile;
} else {
Logger.logDebug("No readable termux.properties file found");
Logger.logDebug("No readable properties file found at: " + Arrays.toString(possiblePropertiesFileLocations));
return null;
}
}

View File

@@ -3,6 +3,8 @@ package com.termux.shared.settings.properties;
import android.content.Context;
import android.content.res.Configuration;
import androidx.annotation.NonNull;
import com.termux.shared.logger.Logger;
import com.termux.shared.data.DataUtils;
@@ -10,21 +12,25 @@ import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import javax.annotation.Nonnull;
public class TermuxSharedProperties {
public abstract class TermuxSharedProperties {
protected final Context mContext;
protected final SharedProperties mSharedProperties;
protected final String mLabel;
protected final File mPropertiesFile;
protected final SharedProperties mSharedProperties;
public static final String LOG_TAG = "TermuxSharedProperties";
public TermuxSharedProperties(@Nonnull Context context) {
public TermuxSharedProperties(@Nonnull Context context, @NonNull String label, File propertiesFile,
@NonNull Set<String> propertiesList, @NonNull SharedPropertiesParser sharedPropertiesParser) {
mContext = context;
mPropertiesFile = TermuxPropertyConstants.getTermuxPropertiesFile();
mSharedProperties = new SharedProperties(context, mPropertiesFile, TermuxPropertyConstants.TERMUX_PROPERTIES_LIST, new SharedPropertiesParserClient());
mLabel = label;
mPropertiesFile = propertiesFile;
mSharedProperties = new SharedProperties(context, mPropertiesFile, propertiesList, sharedPropertiesParser);
loadTermuxPropertiesFromDisk();
}
@@ -162,7 +168,7 @@ public class TermuxSharedProperties {
/**
* Get the internal {@link Object} value for the key passed from the file returned by
* {@link TermuxPropertyConstants#getTermuxPropertiesFile()}. The {@link Properties} object is
* {@code propertiesFile}. The {@link Properties} object is
* read directly from the file and internal value is returned for the property value against the key.
*
* @param context The context for operations.
@@ -170,8 +176,9 @@ public class TermuxSharedProperties {
* @return Returns the {@link Object} object. This will be {@code null} if key is not found or
* the object stored against the key is {@code null}.
*/
public static Object getInternalPropertyValue(Context context, String key) {
return SharedProperties.getInternalProperty(context, TermuxPropertyConstants.getTermuxPropertiesFile(), key, new SharedPropertiesParserClient());
public static Object getInternalPropertyValue(Context context, File propertiesFile, String key,
@NonNull SharedPropertiesParser sharedPropertiesParser) {
return SharedProperties.getInternalProperty(context, propertiesFile, key, sharedPropertiesParser);
}
/**
@@ -588,7 +595,7 @@ public class TermuxSharedProperties {
Properties properties = getProperties(true);
StringBuilder propertiesDump = new StringBuilder();
propertiesDump.append("Termux Properties:");
propertiesDump.append(mLabel).append(" Termux Properties:");
if (properties != null) {
for (String key : properties.stringPropertyNames()) {
propertiesDump.append("\n").append(key).append(": `").append(properties.get(key)).append("`");
@@ -604,7 +611,7 @@ public class TermuxSharedProperties {
HashMap<String, Object> internalProperties = (HashMap<String, Object>) getInternalProperties();
StringBuilder internalPropertiesDump = new StringBuilder();
internalPropertiesDump.append("Termux Internal Properties:");
internalPropertiesDump.append(mLabel).append(" Internal Properties:");
if (internalProperties != null) {
for (String key : internalProperties.keySet()) {
internalPropertiesDump.append("\n").append(key).append(": `").append(internalProperties.get(key)).append("`");

View File

@@ -0,0 +1,79 @@
package com.termux.shared.terminal.io;
import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.os.SystemClock;
import android.os.VibrationEffect;
import android.os.Vibrator;
import com.termux.shared.logger.Logger;
public class BellHandler {
private static BellHandler instance = null;
private static final Object lock = new Object();
private static final String LOG_TAG = "BellHandler";
public static BellHandler getInstance(Context context) {
if (instance == null) {
synchronized (lock) {
if (instance == null) {
instance = new BellHandler((Vibrator) context.getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE));
}
}
}
return instance;
}
private static final long DURATION = 50;
private static final long MIN_PAUSE = 3 * DURATION;
private final Handler handler = new Handler(Looper.getMainLooper());
private long lastBell = 0;
private final Runnable bellRunnable;
private BellHandler(final Vibrator vibrator) {
bellRunnable = new Runnable() {
@Override
public void run() {
if (vibrator != null) {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
vibrator.vibrate(VibrationEffect.createOneShot(DURATION, VibrationEffect.DEFAULT_AMPLITUDE));
} else {
vibrator.vibrate(DURATION);
}
} catch (Exception e) {
// Issue on samsung devices on android 8
// java.lang.NullPointerException: Attempt to read from field 'android.os.VibrationEffect com.android.server.VibratorService$Vibration.mEffect' on a null object reference
Logger.logStackTraceWithMessage(LOG_TAG, "Failed to run vibrator", e);
}
}
}
};
}
public synchronized void doBell() {
long now = now();
long timeSinceLastBell = now - lastBell;
if (timeSinceLastBell < 0) {
// there is a next bell pending; don't schedule another one
} else if (timeSinceLastBell < MIN_PAUSE) {
// there was a bell recently, schedule the next one
handler.postDelayed(bellRunnable, MIN_PAUSE - timeSinceLastBell);
lastBell = lastBell + MIN_PAUSE;
} else {
// the last bell was long ago, do it now
bellRunnable.run();
lastBell = now;
}
}
private long now() {
return SystemClock.uptimeMillis();
}
}

View File

@@ -12,7 +12,7 @@ import java.util.IllegalFormatException;
import java.util.List;
/*
* Version: v0.27.0
* Version: v0.28.0
*
* Changelog
*
@@ -187,6 +187,9 @@ import java.util.List;
* `TERMUX_FLOAT_APP.TERMUX_FLOAT_SERVICE_NAME`.
* - Added following to `TERMUX_FLOAT_APP.TERMUX_FLOAT_SERVICE`:
* `ACTION_STOP_SERVICE`, `ACTION_SHOW`, `ACTION_HIDE`.
*
* - 0.28.0 (2021-09-02)
* - Added `TERMUX_FLOAT_PROPERTIES_PRIMARY_FILE*` and `TERMUX_FLOAT_PROPERTIES_SECONDARY_FILE*`.
*/
/**
@@ -640,6 +643,17 @@ public final class TermuxConstants {
public static final File TERMUX_PROPERTIES_SECONDARY_FILE = new File(TERMUX_PROPERTIES_SECONDARY_FILE_PATH);
/** Termux Float app termux.properties primary file path */
public static final String TERMUX_FLOAT_PROPERTIES_PRIMARY_FILE_PATH = TERMUX_DATA_HOME_DIR_PATH + "/termux.float.properties"; // Default: "/data/data/com.termux/files/home/.termux/termux.float.properties"
/** Termux Float app termux.properties primary file */
public static final File TERMUX_FLOAT_PROPERTIES_PRIMARY_FILE = new File(TERMUX_FLOAT_PROPERTIES_PRIMARY_FILE_PATH);
/** Termux Float app termux.properties secondary file path */
public static final String TERMUX_FLOAT_PROPERTIES_SECONDARY_FILE_PATH = TERMUX_CONFIG_HOME_DIR_PATH + "/termux.float.properties"; // Default: "/data/data/com.termux/files/home/.config/termux/termux.float.properties"
/** Termux Float app termux.properties secondary file */
public static final File TERMUX_FLOAT_PROPERTIES_SECONDARY_FILE = new File(TERMUX_FLOAT_PROPERTIES_SECONDARY_FILE_PATH);
/** Termux app and Termux:Styling colors.properties file path */
public static final String TERMUX_COLOR_PROPERTIES_FILE_PATH = TERMUX_DATA_HOME_DIR_PATH + "/colors.properties"; // Default: "/data/data/com.termux/files/home/.termux/colors.properties"
/** Termux app and Termux:Styling colors.properties file */

Binary file not shown.